| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup ia32
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <arch/pm.h>
|
|---|
| 36 | #include <config.h>
|
|---|
| 37 | #include <typedefs.h>
|
|---|
| 38 | #include <arch/interrupt.h>
|
|---|
| 39 | #include <arch/asm.h>
|
|---|
| 40 | #include <arch/context.h>
|
|---|
| 41 | #include <panic.h>
|
|---|
| 42 | #include <arch/mm/page.h>
|
|---|
| 43 | #include <mm/km.h>
|
|---|
| 44 | #include <mm/frame.h>
|
|---|
| 45 | #include <mm/slab.h>
|
|---|
| 46 | #include <memstr.h>
|
|---|
| 47 | #include <arch/boot/boot.h>
|
|---|
| 48 | #include <interrupt.h>
|
|---|
| 49 |
|
|---|
| 50 | /*
|
|---|
| 51 | * Early ia32 configuration functions and data structures.
|
|---|
| 52 | */
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | * We don't have much use for segmentation so we set up flat mode.
|
|---|
| 56 | * In this mode, we use, for each privilege level, two segments spanning the
|
|---|
| 57 | * whole memory. One is for code and one is for data.
|
|---|
| 58 | *
|
|---|
| 59 | * One special segment apart of that is for the GS register which holds
|
|---|
| 60 | * a pointer to the VREG page in its base.
|
|---|
| 61 | */
|
|---|
| 62 | descriptor_t gdt[GDT_ITEMS] = {
|
|---|
| 63 | /* NULL descriptor */
|
|---|
| 64 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|---|
| 65 | /* KTEXT descriptor */
|
|---|
| 66 | { 0xffff, 0, 0, AR_PRESENT | AR_CODE | DPL_KERNEL, 0xf, 0, 0, 1, 1, 0 },
|
|---|
| 67 | /* KDATA descriptor */
|
|---|
| 68 | { 0xffff, 0, 0, AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL, 0xf, 0, 0, 1, 1, 0 },
|
|---|
| 69 | /* UTEXT descriptor */
|
|---|
| 70 | { 0xffff, 0, 0, AR_PRESENT | AR_CODE | DPL_USER, 0xf, 0, 0, 1, 1, 0 },
|
|---|
| 71 | /* UDATA descriptor */
|
|---|
| 72 | { 0xffff, 0, 0, AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_USER, 0xf, 0, 0, 1, 1, 0 },
|
|---|
| 73 | /* TSS descriptor - set up will be completed later */
|
|---|
| 74 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|---|
| 75 | /* VREG descriptor - segment used for virtual registers, will be reinitialized later */
|
|---|
| 76 | { 0xffff, 0 , 0, AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_USER, 0xf, 0, 0, 1, 1, 0 },
|
|---|
| 77 | /* VESA Init descriptor */
|
|---|
| 78 | #ifdef CONFIG_FB
|
|---|
| 79 | { 0xffff, 0, VESA_INIT_SEGMENT >> 12, AR_PRESENT | AR_CODE | AR_READABLE | DPL_KERNEL, 0xf, 0, 0, 0, 0, 0 },
|
|---|
| 80 | { 0xffff, 0, VESA_INIT_SEGMENT >> 12, AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL, 0xf, 0, 0, 0, 0, 0 }
|
|---|
| 81 | #endif
|
|---|
| 82 | };
|
|---|
| 83 |
|
|---|
| 84 | static idescriptor_t idt[IDT_ITEMS];
|
|---|
| 85 |
|
|---|
| 86 | static tss_t tss0;
|
|---|
| 87 |
|
|---|
| 88 | tss_t *tss_p = NULL;
|
|---|
| 89 |
|
|---|
| 90 | /* gdtr is changed by kmp before next CPU is initialized */
|
|---|
| 91 | ptr_16_32_t gdtr = {
|
|---|
| 92 | .limit = sizeof(gdt),
|
|---|
| 93 | .base = (uintptr_t) gdt
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | void gdt_setbase(descriptor_t *d, uintptr_t base)
|
|---|
| 97 | {
|
|---|
| 98 | d->base_0_15 = base & 0xffff;
|
|---|
| 99 | d->base_16_23 = (base >> 16) & 0xff;
|
|---|
| 100 | d->base_24_31 = (base >> 24) & 0xff;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void gdt_setlimit(descriptor_t *d, uint32_t limit)
|
|---|
| 104 | {
|
|---|
| 105 | d->limit_0_15 = limit & 0xffff;
|
|---|
| 106 | d->limit_16_19 = (limit >> 16) & 0xf;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void idt_setoffset(idescriptor_t *d, uintptr_t offset)
|
|---|
| 110 | {
|
|---|
| 111 | /*
|
|---|
| 112 | * Offset is a linear address.
|
|---|
| 113 | */
|
|---|
| 114 | d->offset_0_15 = offset & 0xffff;
|
|---|
| 115 | d->offset_16_31 = offset >> 16;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void tss_initialize(tss_t *t)
|
|---|
| 119 | {
|
|---|
| 120 | memsetb(t, sizeof(tss_t), 0);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /*
|
|---|
| 124 | * This function takes care of proper setup of IDT and IDTR.
|
|---|
| 125 | */
|
|---|
| 126 | void idt_init(void)
|
|---|
| 127 | {
|
|---|
| 128 | idescriptor_t *d;
|
|---|
| 129 | unsigned int i;
|
|---|
| 130 |
|
|---|
| 131 | for (i = 0; i < IDT_ITEMS; i++) {
|
|---|
| 132 | d = &idt[i];
|
|---|
| 133 |
|
|---|
| 134 | d->unused = 0;
|
|---|
| 135 | d->selector = GDT_SELECTOR(KTEXT_DES);
|
|---|
| 136 |
|
|---|
| 137 | if (i == VECTOR_SYSCALL) {
|
|---|
| 138 | /*
|
|---|
| 139 | * The syscall trap gate must be callable from
|
|---|
| 140 | * userland. Interrupts will remain enabled.
|
|---|
| 141 | */
|
|---|
| 142 | d->access = AR_PRESENT | AR_TRAP | DPL_USER;
|
|---|
| 143 | } else {
|
|---|
| 144 | /*
|
|---|
| 145 | * Other interrupts use interrupt gates which
|
|---|
| 146 | * disable interrupts.
|
|---|
| 147 | */
|
|---|
| 148 | d->access = AR_PRESENT | AR_INTERRUPT;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | d = &idt[0];
|
|---|
| 153 | idt_setoffset(d++, (uintptr_t) &int_0);
|
|---|
| 154 | idt_setoffset(d++, (uintptr_t) &int_1);
|
|---|
| 155 | idt_setoffset(d++, (uintptr_t) &int_2);
|
|---|
| 156 | idt_setoffset(d++, (uintptr_t) &int_3);
|
|---|
| 157 | idt_setoffset(d++, (uintptr_t) &int_4);
|
|---|
| 158 | idt_setoffset(d++, (uintptr_t) &int_5);
|
|---|
| 159 | idt_setoffset(d++, (uintptr_t) &int_6);
|
|---|
| 160 | idt_setoffset(d++, (uintptr_t) &int_7);
|
|---|
| 161 | idt_setoffset(d++, (uintptr_t) &int_8);
|
|---|
| 162 | idt_setoffset(d++, (uintptr_t) &int_9);
|
|---|
| 163 | idt_setoffset(d++, (uintptr_t) &int_10);
|
|---|
| 164 | idt_setoffset(d++, (uintptr_t) &int_11);
|
|---|
| 165 | idt_setoffset(d++, (uintptr_t) &int_12);
|
|---|
| 166 | idt_setoffset(d++, (uintptr_t) &int_13);
|
|---|
| 167 | idt_setoffset(d++, (uintptr_t) &int_14);
|
|---|
| 168 | idt_setoffset(d++, (uintptr_t) &int_15);
|
|---|
| 169 | idt_setoffset(d++, (uintptr_t) &int_16);
|
|---|
| 170 | idt_setoffset(d++, (uintptr_t) &int_17);
|
|---|
| 171 | idt_setoffset(d++, (uintptr_t) &int_18);
|
|---|
| 172 | idt_setoffset(d++, (uintptr_t) &int_19);
|
|---|
| 173 | idt_setoffset(d++, (uintptr_t) &int_20);
|
|---|
| 174 | idt_setoffset(d++, (uintptr_t) &int_21);
|
|---|
| 175 | idt_setoffset(d++, (uintptr_t) &int_22);
|
|---|
| 176 | idt_setoffset(d++, (uintptr_t) &int_23);
|
|---|
| 177 | idt_setoffset(d++, (uintptr_t) &int_24);
|
|---|
| 178 | idt_setoffset(d++, (uintptr_t) &int_25);
|
|---|
| 179 | idt_setoffset(d++, (uintptr_t) &int_26);
|
|---|
| 180 | idt_setoffset(d++, (uintptr_t) &int_27);
|
|---|
| 181 | idt_setoffset(d++, (uintptr_t) &int_28);
|
|---|
| 182 | idt_setoffset(d++, (uintptr_t) &int_29);
|
|---|
| 183 | idt_setoffset(d++, (uintptr_t) &int_30);
|
|---|
| 184 | idt_setoffset(d++, (uintptr_t) &int_31);
|
|---|
| 185 | idt_setoffset(d++, (uintptr_t) &int_32);
|
|---|
| 186 | idt_setoffset(d++, (uintptr_t) &int_33);
|
|---|
| 187 | idt_setoffset(d++, (uintptr_t) &int_34);
|
|---|
| 188 | idt_setoffset(d++, (uintptr_t) &int_35);
|
|---|
| 189 | idt_setoffset(d++, (uintptr_t) &int_36);
|
|---|
| 190 | idt_setoffset(d++, (uintptr_t) &int_37);
|
|---|
| 191 | idt_setoffset(d++, (uintptr_t) &int_38);
|
|---|
| 192 | idt_setoffset(d++, (uintptr_t) &int_39);
|
|---|
| 193 | idt_setoffset(d++, (uintptr_t) &int_40);
|
|---|
| 194 | idt_setoffset(d++, (uintptr_t) &int_41);
|
|---|
| 195 | idt_setoffset(d++, (uintptr_t) &int_42);
|
|---|
| 196 | idt_setoffset(d++, (uintptr_t) &int_43);
|
|---|
| 197 | idt_setoffset(d++, (uintptr_t) &int_44);
|
|---|
| 198 | idt_setoffset(d++, (uintptr_t) &int_45);
|
|---|
| 199 | idt_setoffset(d++, (uintptr_t) &int_46);
|
|---|
| 200 | idt_setoffset(d++, (uintptr_t) &int_47);
|
|---|
| 201 | idt_setoffset(d++, (uintptr_t) &int_48);
|
|---|
| 202 | idt_setoffset(d++, (uintptr_t) &int_49);
|
|---|
| 203 | idt_setoffset(d++, (uintptr_t) &int_50);
|
|---|
| 204 | idt_setoffset(d++, (uintptr_t) &int_51);
|
|---|
| 205 | idt_setoffset(d++, (uintptr_t) &int_52);
|
|---|
| 206 | idt_setoffset(d++, (uintptr_t) &int_53);
|
|---|
| 207 | idt_setoffset(d++, (uintptr_t) &int_54);
|
|---|
| 208 | idt_setoffset(d++, (uintptr_t) &int_55);
|
|---|
| 209 | idt_setoffset(d++, (uintptr_t) &int_56);
|
|---|
| 210 | idt_setoffset(d++, (uintptr_t) &int_57);
|
|---|
| 211 | idt_setoffset(d++, (uintptr_t) &int_58);
|
|---|
| 212 | idt_setoffset(d++, (uintptr_t) &int_59);
|
|---|
| 213 | idt_setoffset(d++, (uintptr_t) &int_60);
|
|---|
| 214 | idt_setoffset(d++, (uintptr_t) &int_61);
|
|---|
| 215 | idt_setoffset(d++, (uintptr_t) &int_62);
|
|---|
| 216 | idt_setoffset(d++, (uintptr_t) &int_63);
|
|---|
| 217 |
|
|---|
| 218 | idt_setoffset(&idt[VECTOR_SYSCALL], (uintptr_t) &int_syscall);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /* Clean IOPL(12,13) and NT(14) flags in EFLAGS register */
|
|---|
| 222 | static void clean_IOPL_NT_flags(void)
|
|---|
| 223 | {
|
|---|
| 224 | asm volatile (
|
|---|
| 225 | "pushfl\n"
|
|---|
| 226 | "pop %%eax\n"
|
|---|
| 227 | "and $0xffff8fff, %%eax\n"
|
|---|
| 228 | "push %%eax\n"
|
|---|
| 229 | "popfl\n"
|
|---|
| 230 | ::: "eax"
|
|---|
| 231 | );
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /* Clean AM(18) flag in CR0 register */
|
|---|
| 235 | static void clean_AM_flag(void)
|
|---|
| 236 | {
|
|---|
| 237 | asm volatile (
|
|---|
| 238 | "mov %%cr0, %%eax\n"
|
|---|
| 239 | "and $0xfffbffff, %%eax\n"
|
|---|
| 240 | "mov %%eax, %%cr0\n"
|
|---|
| 241 | ::: "eax"
|
|---|
| 242 | );
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void pm_init(void)
|
|---|
| 246 | {
|
|---|
| 247 | descriptor_t *gdt_p = (descriptor_t *) gdtr.base;
|
|---|
| 248 | ptr_16_32_t idtr;
|
|---|
| 249 |
|
|---|
| 250 | /*
|
|---|
| 251 | * Update addresses in GDT and IDT to their virtual counterparts.
|
|---|
| 252 | */
|
|---|
| 253 | idtr.limit = sizeof(idt);
|
|---|
| 254 | idtr.base = (uintptr_t) idt;
|
|---|
| 255 | gdtr_load(&gdtr);
|
|---|
| 256 | idtr_load(&idtr);
|
|---|
| 257 |
|
|---|
| 258 | /*
|
|---|
| 259 | * Each CPU has its private GDT and TSS.
|
|---|
| 260 | * All CPUs share one IDT.
|
|---|
| 261 | */
|
|---|
| 262 |
|
|---|
| 263 | if (config.cpu_active == 1) {
|
|---|
| 264 | idt_init();
|
|---|
| 265 | /*
|
|---|
| 266 | * NOTE: bootstrap CPU has statically allocated TSS, because
|
|---|
| 267 | * the heap hasn't been initialized so far.
|
|---|
| 268 | */
|
|---|
| 269 | tss_p = &tss0;
|
|---|
| 270 | } else {
|
|---|
| 271 | tss_p = (tss_t *) malloc(sizeof(tss_t), FRAME_ATOMIC);
|
|---|
| 272 | if (!tss_p)
|
|---|
| 273 | panic("Cannot allocate TSS.");
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | tss_initialize(tss_p);
|
|---|
| 277 |
|
|---|
| 278 | gdt_p[TSS_DES].access = AR_PRESENT | AR_TSS | DPL_KERNEL;
|
|---|
| 279 | gdt_p[TSS_DES].special = 1;
|
|---|
| 280 | gdt_p[TSS_DES].granularity = 0;
|
|---|
| 281 |
|
|---|
| 282 | gdt_setbase(&gdt_p[TSS_DES], (uintptr_t) tss_p);
|
|---|
| 283 | gdt_setlimit(&gdt_p[TSS_DES], TSS_BASIC_SIZE - 1);
|
|---|
| 284 |
|
|---|
| 285 | /*
|
|---|
| 286 | * As of this moment, the current CPU has its own GDT pointing
|
|---|
| 287 | * to its own TSS. We just need to load the TR register.
|
|---|
| 288 | */
|
|---|
| 289 | tr_load(GDT_SELECTOR(TSS_DES));
|
|---|
| 290 |
|
|---|
| 291 | clean_IOPL_NT_flags(); /* Disable I/O on nonprivileged levels and clear NT flag. */
|
|---|
| 292 | clean_AM_flag(); /* Disable alignment check */
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /** @}
|
|---|
| 296 | */
|
|---|