[c245372b] | 1 | /*
|
---|
[4bb31f7] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[df4ed85] | 3 | * Copyright (c) 2005-2006 Ondrej Palkovsky
|
---|
[c245372b] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[06e1e95] | 30 | /** @addtogroup amd64
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[f74bbaf] | 36 | #include <arch.h>
|
---|
[c245372b] | 37 | #include <arch/pm.h>
|
---|
[b9e97fb] | 38 | #include <arch/asm.h>
|
---|
[a98cdc7] | 39 | #include <mm/as.h>
|
---|
[b3f8fb7] | 40 | #include <mm/frame.h>
|
---|
[b9e97fb] | 41 | #include <memstr.h>
|
---|
[085d973] | 42 | #include <mm/slab.h>
|
---|
[c245372b] | 43 |
|
---|
| 44 | /*
|
---|
| 45 | * There is no segmentation in long mode so we set up flat mode. In this
|
---|
| 46 | * mode, we use, for each privilege level, two segments spanning the
|
---|
| 47 | * whole memory. One is for code and one is for data.
|
---|
| 48 | */
|
---|
| 49 |
|
---|
[39cea6a] | 50 | descriptor_t gdt[GDT_ITEMS] = {
|
---|
[c245372b] | 51 | /* NULL descriptor */
|
---|
| 52 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
---|
| 53 | /* KTEXT descriptor */
|
---|
| 54 | { .limit_0_15 = 0xffff,
|
---|
| 55 | .base_0_15 = 0,
|
---|
| 56 | .base_16_23 = 0,
|
---|
[4bb31f7] | 57 | .access = AR_PRESENT | AR_CODE | DPL_KERNEL | AR_READABLE,
|
---|
[c245372b] | 58 | .limit_16_19 = 0xf,
|
---|
| 59 | .available = 0,
|
---|
| 60 | .longmode = 1,
|
---|
[6f878b7] | 61 | .special = 0,
|
---|
[c245372b] | 62 | .granularity = 1,
|
---|
| 63 | .base_24_31 = 0 },
|
---|
| 64 | /* KDATA descriptor */
|
---|
| 65 | { .limit_0_15 = 0xffff,
|
---|
| 66 | .base_0_15 = 0,
|
---|
| 67 | .base_16_23 = 0,
|
---|
| 68 | .access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL,
|
---|
| 69 | .limit_16_19 = 0xf,
|
---|
| 70 | .available = 0,
|
---|
| 71 | .longmode = 0,
|
---|
| 72 | .special = 0,
|
---|
[6f878b7] | 73 | .granularity = 1,
|
---|
[c245372b] | 74 | .base_24_31 = 0 },
|
---|
[dd4d6b0] | 75 | /* UDATA descriptor */
|
---|
[c245372b] | 76 | { .limit_0_15 = 0xffff,
|
---|
| 77 | .base_0_15 = 0,
|
---|
| 78 | .base_16_23 = 0,
|
---|
[dd4d6b0] | 79 | .access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_USER,
|
---|
[c245372b] | 80 | .limit_16_19 = 0xf,
|
---|
| 81 | .available = 0,
|
---|
[dd4d6b0] | 82 | .longmode = 0,
|
---|
| 83 | .special = 1,
|
---|
[b9e97fb] | 84 | .granularity = 1,
|
---|
[c245372b] | 85 | .base_24_31 = 0 },
|
---|
[dd4d6b0] | 86 | /* UTEXT descriptor */
|
---|
[c245372b] | 87 | { .limit_0_15 = 0xffff,
|
---|
| 88 | .base_0_15 = 0,
|
---|
| 89 | .base_16_23 = 0,
|
---|
[dd4d6b0] | 90 | .access = AR_PRESENT | AR_CODE | DPL_USER,
|
---|
[c245372b] | 91 | .limit_16_19 = 0xf,
|
---|
| 92 | .available = 0,
|
---|
[dd4d6b0] | 93 | .longmode = 1,
|
---|
| 94 | .special = 0,
|
---|
[c245372b] | 95 | .granularity = 1,
|
---|
| 96 | .base_24_31 = 0 },
|
---|
[3156582] | 97 | /* KTEXT 32-bit protected, for protected mode before long mode */
|
---|
[6f878b7] | 98 | { .limit_0_15 = 0xffff,
|
---|
| 99 | .base_0_15 = 0,
|
---|
| 100 | .base_16_23 = 0,
|
---|
| 101 | .access = AR_PRESENT | AR_CODE | DPL_KERNEL | AR_READABLE,
|
---|
| 102 | .limit_16_19 = 0xf,
|
---|
| 103 | .available = 0,
|
---|
| 104 | .longmode = 0,
|
---|
[946b630] | 105 | .special = 1,
|
---|
[6f878b7] | 106 | .granularity = 1,
|
---|
| 107 | .base_24_31 = 0 },
|
---|
[b9e97fb] | 108 | /* TSS descriptor - set up will be completed later,
|
---|
| 109 | * on AMD64 it is 64-bit - 2 items in table */
|
---|
| 110 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
---|
[de07bcf] | 111 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
---|
| 112 | /* VESA Init descriptor */
|
---|
[e8194664] | 113 | #ifdef CONFIG_FB
|
---|
[4bb31f7] | 114 | { 0xffff, 0, VESA_INIT_SEGMENT >> 12, AR_PRESENT | AR_CODE | DPL_KERNEL,
|
---|
| 115 | 0xf, 0, 0, 0, 0, 0
|
---|
| 116 | }
|
---|
[e8194664] | 117 | #endif
|
---|
[c245372b] | 118 | };
|
---|
| 119 |
|
---|
[39cea6a] | 120 | idescriptor_t idt[IDT_ITEMS];
|
---|
[c245372b] | 121 |
|
---|
[4bb31f7] | 122 | ptr_16_64_t gdtr = {.limit = sizeof(gdt), .base = (uint64_t) gdt };
|
---|
| 123 | ptr_16_64_t idtr = {.limit = sizeof(idt), .base = (uint64_t) idt };
|
---|
[de25b6f] | 124 |
|
---|
[39cea6a] | 125 | static tss_t tss;
|
---|
| 126 | tss_t *tss_p = NULL;
|
---|
[c245372b] | 127 |
|
---|
[7f1c620] | 128 | void gdt_tss_setbase(descriptor_t *d, uintptr_t base)
|
---|
[b9e97fb] | 129 | {
|
---|
[39cea6a] | 130 | tss_descriptor_t *td = (tss_descriptor_t *) d;
|
---|
[b9e97fb] | 131 |
|
---|
| 132 | td->base_0_15 = base & 0xffff;
|
---|
| 133 | td->base_16_23 = ((base) >> 16) & 0xff;
|
---|
| 134 | td->base_24_31 = ((base) >> 24) & 0xff;
|
---|
| 135 | td->base_32_63 = ((base) >> 32);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[7f1c620] | 138 | void gdt_tss_setlimit(descriptor_t *d, uint32_t limit)
|
---|
[b9e97fb] | 139 | {
|
---|
[39cea6a] | 140 | struct tss_descriptor *td = (tss_descriptor_t *) d;
|
---|
[b9e97fb] | 141 |
|
---|
| 142 | td->limit_0_15 = limit & 0xffff;
|
---|
| 143 | td->limit_16_19 = (limit >> 16) & 0xf;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[7f1c620] | 146 | void idt_setoffset(idescriptor_t *d, uintptr_t offset)
|
---|
[b9e97fb] | 147 | {
|
---|
| 148 | /*
|
---|
| 149 | * Offset is a linear address.
|
---|
| 150 | */
|
---|
| 151 | d->offset_0_15 = offset & 0xffff;
|
---|
| 152 | d->offset_16_31 = offset >> 16 & 0xffff;
|
---|
| 153 | d->offset_32_63 = offset >> 32;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[39cea6a] | 156 | void tss_initialize(tss_t *t)
|
---|
[b9e97fb] | 157 | {
|
---|
[e32e092] | 158 | memsetb(t, sizeof(tss_t), 0);
|
---|
[b9e97fb] | 159 | }
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * This function takes care of proper setup of IDT and IDTR.
|
---|
| 163 | */
|
---|
| 164 | void idt_init(void)
|
---|
| 165 | {
|
---|
[39cea6a] | 166 | idescriptor_t *d;
|
---|
[b9e97fb] | 167 | int i;
|
---|
| 168 |
|
---|
| 169 | for (i = 0; i < IDT_ITEMS; i++) {
|
---|
| 170 | d = &idt[i];
|
---|
| 171 |
|
---|
| 172 | d->unused = 0;
|
---|
[33ccb2c] | 173 | d->selector = gdtselector(KTEXT_DES);
|
---|
[b9e97fb] | 174 |
|
---|
| 175 | d->present = 1;
|
---|
| 176 | d->type = AR_INTERRUPT; /* masking interrupt */
|
---|
| 177 |
|
---|
[4bb31f7] | 178 | idt_setoffset(d, ((uintptr_t) interrupt_handlers) +
|
---|
| 179 | i * interrupt_handler_size);
|
---|
[b9e97fb] | 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[49a39c2] | 183 | /** Initialize segmentation - code/data/idt tables
|
---|
| 184 | *
|
---|
| 185 | */
|
---|
[b9e97fb] | 186 | void pm_init(void)
|
---|
| 187 | {
|
---|
[39cea6a] | 188 | descriptor_t *gdt_p = (struct descriptor *) gdtr.base;
|
---|
| 189 | tss_descriptor_t *tss_desc;
|
---|
[b9e97fb] | 190 |
|
---|
| 191 | /*
|
---|
| 192 | * Each CPU has its private GDT and TSS.
|
---|
| 193 | * All CPUs share one IDT.
|
---|
| 194 | */
|
---|
| 195 |
|
---|
| 196 | if (config.cpu_active == 1) {
|
---|
| 197 | idt_init();
|
---|
| 198 | /*
|
---|
| 199 | * NOTE: bootstrap CPU has statically allocated TSS, because
|
---|
| 200 | * the heap hasn't been initialized so far.
|
---|
| 201 | */
|
---|
| 202 | tss_p = &tss;
|
---|
| 203 | }
|
---|
| 204 | else {
|
---|
[a98cdc7] | 205 | /* We are going to use malloc, which may return
|
---|
| 206 | * non boot-mapped pointer, initialize the CR3 register
|
---|
| 207 | * ahead of page_init */
|
---|
[80bcaed] | 208 | write_cr3((uintptr_t) AS_KERNEL->genarch.page_table);
|
---|
[a98cdc7] | 209 |
|
---|
[39cea6a] | 210 | tss_p = (struct tss *) malloc(sizeof(tss_t), FRAME_ATOMIC);
|
---|
[b9e97fb] | 211 | if (!tss_p)
|
---|
| 212 | panic("could not allocate TSS\n");
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | tss_initialize(tss_p);
|
---|
| 216 |
|
---|
[39cea6a] | 217 | tss_desc = (tss_descriptor_t *) (&gdt_p[TSS_DES]);
|
---|
[e291e5d] | 218 | tss_desc->present = 1;
|
---|
| 219 | tss_desc->type = AR_TSS;
|
---|
| 220 | tss_desc->dpl = PL_KERNEL;
|
---|
[b9e97fb] | 221 |
|
---|
[7f1c620] | 222 | gdt_tss_setbase(&gdt_p[TSS_DES], (uintptr_t) tss_p);
|
---|
[11928d5] | 223 | gdt_tss_setlimit(&gdt_p[TSS_DES], TSS_BASIC_SIZE - 1);
|
---|
[b9e97fb] | 224 |
|
---|
[897ad60] | 225 | gdtr_load(&gdtr);
|
---|
| 226 | idtr_load(&idtr);
|
---|
[b9e97fb] | 227 | /*
|
---|
| 228 | * As of this moment, the current CPU has its own GDT pointing
|
---|
| 229 | * to its own TSS. We just need to load the TR register.
|
---|
| 230 | */
|
---|
[897ad60] | 231 | tr_load(gdtselector(TSS_DES));
|
---|
[b9e97fb] | 232 | }
|
---|
[b45c443] | 233 |
|
---|
[f74bbaf] | 234 | /* Reboot the machine by initiating
|
---|
| 235 | * a triple fault
|
---|
| 236 | */
|
---|
| 237 | void arch_reboot(void)
|
---|
| 238 | {
|
---|
| 239 | preemption_disable();
|
---|
| 240 | ipl_t ipl = interrupts_disable();
|
---|
| 241 |
|
---|
[e32e092] | 242 | memsetb(idt, sizeof(idt), 0);
|
---|
[f74bbaf] | 243 | idtr_load(&idtr);
|
---|
| 244 |
|
---|
| 245 | interrupts_restore(ipl);
|
---|
| 246 | asm volatile (
|
---|
| 247 | "int $0x03\n"
|
---|
[6aea2e00] | 248 | "cli\n"
|
---|
[f74bbaf] | 249 | "hlt\n"
|
---|
| 250 | );
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[06e1e95] | 253 | /** @}
|
---|
[b45c443] | 254 | */
|
---|