[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 |
|
---|
[06e1e95] | 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__
|
---|
[b3f8fb7] | 39 | # include <arch/types.h>
|
---|
| 40 | # include <arch/context.h>
|
---|
[8fc0d455] | 41 | #endif
|
---|
[c245372b] | 42 |
|
---|
| 43 | #define IDT_ITEMS 64
|
---|
[e8194664] | 44 | #define GDT_ITEMS 8
|
---|
[de07bcf] | 45 |
|
---|
[c245372b] | 46 |
|
---|
| 47 | #define NULL_DES 0
|
---|
[dd4d6b0] | 48 | /* Warning: Do not reorder next items, unless you look into syscall.c!!! */
|
---|
[c245372b] | 49 | #define KTEXT_DES 1
|
---|
| 50 | #define KDATA_DES 2
|
---|
[dd4d6b0] | 51 | #define UDATA_DES 3
|
---|
| 52 | #define UTEXT_DES 4
|
---|
[8fc0d455] | 53 | #define KTEXT32_DES 5
|
---|
[dd4d6b0] | 54 | /* EndOfWarning */
|
---|
[8fc0d455] | 55 | #define TSS_DES 6
|
---|
[e8194664] | 56 |
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | #ifdef CONFIG_FB
|
---|
| 60 |
|
---|
[de07bcf] | 61 | #define VESA_INIT_DES 8
|
---|
[e8194664] | 62 | #define VESA_INIT_SEGMENT 0x8000
|
---|
| 63 | #undef GDT_ITEMS
|
---|
| 64 | #define GDT_ITEMS 9
|
---|
| 65 |
|
---|
| 66 | #endif /*CONFIG_FB*/
|
---|
| 67 |
|
---|
| 68 |
|
---|
[c245372b] | 69 |
|
---|
[f476e76] | 70 | #define gdtselector(des) ((des) << 3)
|
---|
| 71 | #define idtselector(des) ((des) << 4)
|
---|
[c245372b] | 72 |
|
---|
| 73 | #define PL_KERNEL 0
|
---|
| 74 | #define PL_USER 3
|
---|
| 75 |
|
---|
| 76 | #define AR_PRESENT (1<<7)
|
---|
| 77 | #define AR_DATA (2<<3)
|
---|
| 78 | #define AR_CODE (3<<3)
|
---|
| 79 | #define AR_WRITABLE (1<<1)
|
---|
[6f878b7] | 80 | #define AR_READABLE (1<<1)
|
---|
[c245372b] | 81 | #define AR_TSS (0x9)
|
---|
[b9e97fb] | 82 | #define AR_INTERRUPT (0xe)
|
---|
| 83 | #define AR_TRAP (0xf)
|
---|
[c245372b] | 84 |
|
---|
| 85 | #define DPL_KERNEL (PL_KERNEL<<5)
|
---|
| 86 | #define DPL_USER (PL_USER<<5)
|
---|
| 87 |
|
---|
[39cea6a] | 88 | #define TSS_BASIC_SIZE 104
|
---|
[9fa16b20] | 89 | #define TSS_IOMAP_SIZE (16*1024+1) /* 16K for bitmap + 1 terminating byte for convenience */
|
---|
[c245372b] | 90 |
|
---|
[73e9b49] | 91 | #define IO_PORTS (64*1024)
|
---|
| 92 |
|
---|
[8fc0d455] | 93 | #ifndef __ASM__
|
---|
| 94 |
|
---|
[c245372b] | 95 | struct descriptor {
|
---|
| 96 | unsigned limit_0_15: 16;
|
---|
| 97 | unsigned base_0_15: 16;
|
---|
| 98 | unsigned base_16_23: 8;
|
---|
| 99 | unsigned access: 8;
|
---|
| 100 | unsigned limit_16_19: 4;
|
---|
| 101 | unsigned available: 1;
|
---|
| 102 | unsigned longmode: 1;
|
---|
| 103 | unsigned special: 1;
|
---|
| 104 | unsigned granularity : 1;
|
---|
| 105 | unsigned base_24_31: 8;
|
---|
| 106 | } __attribute__ ((packed));
|
---|
[39cea6a] | 107 | typedef struct descriptor descriptor_t;
|
---|
[c245372b] | 108 |
|
---|
[b9e97fb] | 109 | struct tss_descriptor {
|
---|
| 110 | unsigned limit_0_15: 16;
|
---|
| 111 | unsigned base_0_15: 16;
|
---|
| 112 | unsigned base_16_23: 8;
|
---|
| 113 | unsigned type: 4;
|
---|
[39cea6a] | 114 | unsigned : 1;
|
---|
[b9e97fb] | 115 | unsigned dpl : 2;
|
---|
| 116 | unsigned present : 1;
|
---|
| 117 | unsigned limit_16_19: 4;
|
---|
| 118 | unsigned available: 1;
|
---|
[e3b9572] | 119 | unsigned : 2;
|
---|
[b9e97fb] | 120 | unsigned granularity : 1;
|
---|
| 121 | unsigned base_24_31: 8;
|
---|
| 122 | unsigned base_32_63 : 32;
|
---|
[e3b9572] | 123 | unsigned : 32;
|
---|
[b9e97fb] | 124 | } __attribute__ ((packed));
|
---|
[39cea6a] | 125 | typedef struct tss_descriptor tss_descriptor_t;
|
---|
[b9e97fb] | 126 |
|
---|
[c245372b] | 127 | struct idescriptor {
|
---|
| 128 | unsigned offset_0_15: 16;
|
---|
| 129 | unsigned selector: 16;
|
---|
[b9e97fb] | 130 | unsigned ist:3;
|
---|
| 131 | unsigned unused: 5;
|
---|
| 132 | unsigned type: 5;
|
---|
| 133 | unsigned dpl: 2;
|
---|
| 134 | unsigned present : 1;
|
---|
[c245372b] | 135 | unsigned offset_16_31: 16;
|
---|
[e3b9572] | 136 | unsigned offset_32_63: 32;
|
---|
| 137 | unsigned : 32;
|
---|
[c245372b] | 138 | } __attribute__ ((packed));
|
---|
[39cea6a] | 139 | typedef struct idescriptor idescriptor_t;
|
---|
[c245372b] | 140 |
|
---|
[b9e97fb] | 141 | struct ptr_16_64 {
|
---|
[7f1c620] | 142 | uint16_t limit;
|
---|
| 143 | uint64_t base;
|
---|
[b9e97fb] | 144 | } __attribute__ ((packed));
|
---|
[39cea6a] | 145 | typedef struct ptr_16_64 ptr_16_64_t;
|
---|
[c245372b] | 146 |
|
---|
[776c91a] | 147 | struct ptr_16_32 {
|
---|
[7f1c620] | 148 | uint16_t limit;
|
---|
| 149 | uint32_t base;
|
---|
[776c91a] | 150 | } __attribute__ ((packed));
|
---|
[39cea6a] | 151 | typedef struct ptr_16_32 ptr_16_32_t;
|
---|
[776c91a] | 152 |
|
---|
[c245372b] | 153 | struct tss {
|
---|
[7f1c620] | 154 | uint32_t reserve1;
|
---|
| 155 | uint64_t rsp0;
|
---|
| 156 | uint64_t rsp1;
|
---|
| 157 | uint64_t rsp2;
|
---|
| 158 | uint64_t reserve2;
|
---|
| 159 | uint64_t ist1;
|
---|
| 160 | uint64_t ist2;
|
---|
| 161 | uint64_t ist3;
|
---|
| 162 | uint64_t ist4;
|
---|
| 163 | uint64_t ist5;
|
---|
| 164 | uint64_t ist6;
|
---|
| 165 | uint64_t ist7;
|
---|
| 166 | uint64_t reserve3;
|
---|
| 167 | uint16_t reserve4;
|
---|
| 168 | uint16_t iomap_base;
|
---|
| 169 | uint8_t iomap[TSS_IOMAP_SIZE];
|
---|
[c245372b] | 170 | } __attribute__ ((packed));
|
---|
[39cea6a] | 171 | typedef struct tss tss_t;
|
---|
[c245372b] | 172 |
|
---|
[39cea6a] | 173 | extern tss_t *tss_p;
|
---|
[c245372b] | 174 |
|
---|
[39cea6a] | 175 | extern descriptor_t gdt[];
|
---|
| 176 | extern idescriptor_t idt[];
|
---|
[c245372b] | 177 |
|
---|
[39cea6a] | 178 | extern ptr_16_64_t gdtr;
|
---|
| 179 | extern ptr_16_32_t bootstrap_gdtr;
|
---|
| 180 | extern ptr_16_32_t protected_ap_gdtr;
|
---|
[b9e97fb] | 181 |
|
---|
[c245372b] | 182 | extern void pm_init(void);
|
---|
| 183 |
|
---|
[7f1c620] | 184 | extern void gdt_tss_setbase(descriptor_t *d, uintptr_t base);
|
---|
| 185 | extern void gdt_tss_setlimit(descriptor_t *d, uint32_t limit);
|
---|
[c245372b] | 186 |
|
---|
| 187 | extern void idt_init(void);
|
---|
[7f1c620] | 188 | extern void idt_setoffset(idescriptor_t *d, uintptr_t offset);
|
---|
[c245372b] | 189 |
|
---|
[39cea6a] | 190 | extern void tss_initialize(tss_t *t);
|
---|
[c245372b] | 191 |
|
---|
[8fc0d455] | 192 | #endif /* __ASM__ */
|
---|
| 193 |
|
---|
[c245372b] | 194 | #endif
|
---|
[b45c443] | 195 |
|
---|
[06e1e95] | 196 | /** @}
|
---|
[b45c443] | 197 | */
|
---|