[c245372b] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[c245372b] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[99d6fd0] | 29 | /** @addtogroup amd64
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_amd64_PM_H_
|
---|
| 36 | #define KERN_amd64_PM_H_
|
---|
[c245372b] | 37 |
|
---|
[8fc0d455] | 38 | #ifndef __ASM__
|
---|
[d99c1d2] | 39 | #include <typedefs.h>
|
---|
[99d6fd0] | 40 | #include <arch/context.h>
|
---|
[8fc0d455] | 41 | #endif
|
---|
[c245372b] | 42 |
|
---|
[99d6fd0] | 43 | #define IDT_ITEMS 64
|
---|
| 44 | #define GDT_ITEMS 8
|
---|
[e8194664] | 45 |
|
---|
| 46 |
|
---|
[99d6fd0] | 47 | #define NULL_DES 0
|
---|
| 48 | /* Warning: Do not reorder the following items, unless you look into syscall.c! */
|
---|
| 49 | #define KTEXT_DES 1
|
---|
| 50 | #define KDATA_DES 2
|
---|
| 51 | #define UDATA_DES 3
|
---|
| 52 | #define UTEXT_DES 4
|
---|
| 53 | #define KTEXT32_DES 5
|
---|
| 54 | /* End of warning */
|
---|
| 55 | #define TSS_DES 6
|
---|
[e8194664] | 56 |
|
---|
| 57 | #ifdef CONFIG_FB
|
---|
| 58 |
|
---|
[99d6fd0] | 59 | #define VESA_INIT_DES 8
|
---|
| 60 | #define VESA_INIT_SEGMENT 0x8000
|
---|
[e8194664] | 61 |
|
---|
[99d6fd0] | 62 | #undef GDT_ITEMS
|
---|
| 63 | #define GDT_ITEMS 9
|
---|
[e8194664] | 64 |
|
---|
[99d6fd0] | 65 | #endif /* CONFIG_FB */
|
---|
[c245372b] | 66 |
|
---|
[1d3d2cf] | 67 | #define GDT_SELECTOR(des) ((des) << 3)
|
---|
[c245372b] | 68 |
|
---|
[99d6fd0] | 69 | #define PL_KERNEL 0
|
---|
| 70 | #define PL_USER 3
|
---|
[c245372b] | 71 |
|
---|
[2ddcc7b] | 72 | #define AR_PRESENT (1 << 7)
|
---|
[99d6fd0] | 73 | #define AR_DATA (2 << 3)
|
---|
| 74 | #define AR_CODE (3 << 3)
|
---|
| 75 | #define AR_WRITABLE (1 << 1)
|
---|
| 76 | #define AR_READABLE (1 << 1)
|
---|
[dc0b964] | 77 | #define AR_TSS (0x09U)
|
---|
| 78 | #define AR_INTERRUPT (0x0eU)
|
---|
| 79 | #define AR_TRAP (0x0fU)
|
---|
[c245372b] | 80 |
|
---|
[99d6fd0] | 81 | #define DPL_KERNEL (PL_KERNEL << 5)
|
---|
| 82 | #define DPL_USER (PL_USER << 5)
|
---|
[c245372b] | 83 |
|
---|
[99d6fd0] | 84 | #define TSS_BASIC_SIZE 104
|
---|
[a13c6bc] | 85 | #define TSS_IOMAP_SIZE (8 * 1024 + 1) /* 8K for bitmap + 1 terminating byte for convenience */
|
---|
[c245372b] | 86 |
|
---|
[99d6fd0] | 87 | #define IO_PORTS (64 * 1024)
|
---|
[73e9b49] | 88 |
|
---|
[8fc0d455] | 89 | #ifndef __ASM__
|
---|
| 90 |
|
---|
[99d6fd0] | 91 | typedef struct {
|
---|
[c245372b] | 92 | unsigned limit_0_15: 16;
|
---|
| 93 | unsigned base_0_15: 16;
|
---|
| 94 | unsigned base_16_23: 8;
|
---|
| 95 | unsigned access: 8;
|
---|
| 96 | unsigned limit_16_19: 4;
|
---|
| 97 | unsigned available: 1;
|
---|
| 98 | unsigned longmode: 1;
|
---|
| 99 | unsigned special: 1;
|
---|
| 100 | unsigned granularity : 1;
|
---|
| 101 | unsigned base_24_31: 8;
|
---|
[99d6fd0] | 102 | } __attribute__ ((packed)) descriptor_t;
|
---|
[c245372b] | 103 |
|
---|
[99d6fd0] | 104 | typedef struct {
|
---|
[b9e97fb] | 105 | unsigned limit_0_15: 16;
|
---|
| 106 | unsigned base_0_15: 16;
|
---|
| 107 | unsigned base_16_23: 8;
|
---|
| 108 | unsigned type: 4;
|
---|
[39cea6a] | 109 | unsigned : 1;
|
---|
[b9e97fb] | 110 | unsigned dpl : 2;
|
---|
| 111 | unsigned present : 1;
|
---|
| 112 | unsigned limit_16_19: 4;
|
---|
| 113 | unsigned available: 1;
|
---|
[e3b9572] | 114 | unsigned : 2;
|
---|
[b9e97fb] | 115 | unsigned granularity : 1;
|
---|
| 116 | unsigned base_24_31: 8;
|
---|
| 117 | unsigned base_32_63 : 32;
|
---|
[e3b9572] | 118 | unsigned : 32;
|
---|
[99d6fd0] | 119 | } __attribute__ ((packed)) tss_descriptor_t;
|
---|
[b9e97fb] | 120 |
|
---|
[99d6fd0] | 121 | typedef struct {
|
---|
[c245372b] | 122 | unsigned offset_0_15: 16;
|
---|
| 123 | unsigned selector: 16;
|
---|
[b9e97fb] | 124 | unsigned ist:3;
|
---|
| 125 | unsigned unused: 5;
|
---|
| 126 | unsigned type: 5;
|
---|
| 127 | unsigned dpl: 2;
|
---|
| 128 | unsigned present : 1;
|
---|
[c245372b] | 129 | unsigned offset_16_31: 16;
|
---|
[e3b9572] | 130 | unsigned offset_32_63: 32;
|
---|
| 131 | unsigned : 32;
|
---|
[99d6fd0] | 132 | } __attribute__ ((packed)) idescriptor_t;
|
---|
[c245372b] | 133 |
|
---|
[99d6fd0] | 134 | typedef struct {
|
---|
[7f1c620] | 135 | uint16_t limit;
|
---|
| 136 | uint64_t base;
|
---|
[99d6fd0] | 137 | } __attribute__ ((packed)) ptr_16_64_t;
|
---|
[c245372b] | 138 |
|
---|
[99d6fd0] | 139 | typedef struct {
|
---|
[7f1c620] | 140 | uint16_t limit;
|
---|
| 141 | uint32_t base;
|
---|
[99d6fd0] | 142 | } __attribute__ ((packed)) ptr_16_32_t;
|
---|
[776c91a] | 143 |
|
---|
[99d6fd0] | 144 | typedef struct {
|
---|
[7f1c620] | 145 | uint32_t reserve1;
|
---|
| 146 | uint64_t rsp0;
|
---|
| 147 | uint64_t rsp1;
|
---|
| 148 | uint64_t rsp2;
|
---|
| 149 | uint64_t reserve2;
|
---|
| 150 | uint64_t ist1;
|
---|
| 151 | uint64_t ist2;
|
---|
| 152 | uint64_t ist3;
|
---|
| 153 | uint64_t ist4;
|
---|
| 154 | uint64_t ist5;
|
---|
| 155 | uint64_t ist6;
|
---|
| 156 | uint64_t ist7;
|
---|
| 157 | uint64_t reserve3;
|
---|
| 158 | uint16_t reserve4;
|
---|
| 159 | uint16_t iomap_base;
|
---|
| 160 | uint8_t iomap[TSS_IOMAP_SIZE];
|
---|
[99d6fd0] | 161 | } __attribute__ ((packed)) tss_t;
|
---|
[c245372b] | 162 |
|
---|
[39cea6a] | 163 | extern tss_t *tss_p;
|
---|
[c245372b] | 164 |
|
---|
[39cea6a] | 165 | extern descriptor_t gdt[];
|
---|
| 166 | extern idescriptor_t idt[];
|
---|
[c245372b] | 167 |
|
---|
[39cea6a] | 168 | extern ptr_16_64_t gdtr;
|
---|
| 169 | extern ptr_16_32_t protected_ap_gdtr;
|
---|
[b9e97fb] | 170 |
|
---|
[c245372b] | 171 | extern void pm_init(void);
|
---|
| 172 |
|
---|
[7f1c620] | 173 | extern void gdt_tss_setbase(descriptor_t *d, uintptr_t base);
|
---|
| 174 | extern void gdt_tss_setlimit(descriptor_t *d, uint32_t limit);
|
---|
[c245372b] | 175 |
|
---|
| 176 | extern void idt_init(void);
|
---|
[7f1c620] | 177 | extern void idt_setoffset(idescriptor_t *d, uintptr_t offset);
|
---|
[c245372b] | 178 |
|
---|
[39cea6a] | 179 | extern void tss_initialize(tss_t *t);
|
---|
[c245372b] | 180 |
|
---|
[8fc0d455] | 181 | #endif /* __ASM__ */
|
---|
| 182 |
|
---|
[c245372b] | 183 | #endif
|
---|
[b45c443] | 184 |
|
---|
[06e1e95] | 185 | /** @}
|
---|
[b45c443] | 186 | */
|
---|