source: mainline/arch/amd64/src/pm.c@ 280a27e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 280a27e was 39cea6a, checked in by Jakub Jermar <jakub@…>, 19 years ago

Cleanup pm.c and pm.h code on ia32 and amd64.
Add before_task_runs() and before_task_runs_arch() for each architecture.
Add ia32 and amd64 code to ensure I/O Permission Bitmap update.

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[c245372b]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
[49a39c2]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
30#include <arch/pm.h>
31#include <arch/mm/page.h>
32#include <arch/types.h>
[b9e97fb]33#include <arch/interrupt.h>
34#include <arch/asm.h>
[fcfac420]35#include <interrupt.h>
[c245372b]36
[b9e97fb]37#include <config.h>
38
39#include <memstr.h>
[085d973]40#include <mm/slab.h>
[b9e97fb]41#include <debug.h>
[c245372b]42
43/*
44 * There is no segmentation in long mode so we set up flat mode. In this
45 * mode, we use, for each privilege level, two segments spanning the
46 * whole memory. One is for code and one is for data.
47 */
48
[39cea6a]49descriptor_t gdt[GDT_ITEMS] = {
[c245372b]50 /* NULL descriptor */
51 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
52 /* KTEXT descriptor */
53 { .limit_0_15 = 0xffff,
54 .base_0_15 = 0,
55 .base_16_23 = 0,
[6f878b7]56 .access = AR_PRESENT | AR_CODE | DPL_KERNEL | AR_READABLE ,
[c245372b]57 .limit_16_19 = 0xf,
58 .available = 0,
59 .longmode = 1,
[6f878b7]60 .special = 0,
[c245372b]61 .granularity = 1,
62 .base_24_31 = 0 },
63 /* KDATA descriptor */
64 { .limit_0_15 = 0xffff,
65 .base_0_15 = 0,
66 .base_16_23 = 0,
67 .access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_KERNEL,
68 .limit_16_19 = 0xf,
69 .available = 0,
70 .longmode = 0,
71 .special = 0,
[6f878b7]72 .granularity = 1,
[c245372b]73 .base_24_31 = 0 },
[dd4d6b0]74 /* UDATA descriptor */
[c245372b]75 { .limit_0_15 = 0xffff,
76 .base_0_15 = 0,
77 .base_16_23 = 0,
[dd4d6b0]78 .access = AR_PRESENT | AR_DATA | AR_WRITABLE | DPL_USER,
[c245372b]79 .limit_16_19 = 0xf,
80 .available = 0,
[dd4d6b0]81 .longmode = 0,
82 .special = 1,
[b9e97fb]83 .granularity = 1,
[c245372b]84 .base_24_31 = 0 },
[dd4d6b0]85 /* UTEXT descriptor */
[c245372b]86 { .limit_0_15 = 0xffff,
87 .base_0_15 = 0,
88 .base_16_23 = 0,
[dd4d6b0]89 .access = AR_PRESENT | AR_CODE | DPL_USER,
[c245372b]90 .limit_16_19 = 0xf,
91 .available = 0,
[dd4d6b0]92 .longmode = 1,
93 .special = 0,
[c245372b]94 .granularity = 1,
95 .base_24_31 = 0 },
[3156582]96 /* KTEXT 32-bit protected, for protected mode before long mode */
[6f878b7]97 { .limit_0_15 = 0xffff,
98 .base_0_15 = 0,
99 .base_16_23 = 0,
100 .access = AR_PRESENT | AR_CODE | DPL_KERNEL | AR_READABLE,
101 .limit_16_19 = 0xf,
102 .available = 0,
103 .longmode = 0,
[946b630]104 .special = 1,
[6f878b7]105 .granularity = 1,
106 .base_24_31 = 0 },
[b9e97fb]107 /* TSS descriptor - set up will be completed later,
108 * on AMD64 it is 64-bit - 2 items in table */
109 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
[c245372b]110 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
111};
112
[39cea6a]113idescriptor_t idt[IDT_ITEMS];
[c245372b]114
[39cea6a]115ptr_16_64_t gdtr = {.limit = sizeof(gdt), .base= (__u64) gdt };
116ptr_16_64_t idtr = {.limit = sizeof(idt), .base= (__u64) idt };
[de25b6f]117
[39cea6a]118static tss_t tss;
119tss_t *tss_p = NULL;
[c245372b]120
[39cea6a]121void gdt_tss_setbase(descriptor_t *d, __address base)
[b9e97fb]122{
[39cea6a]123 tss_descriptor_t *td = (tss_descriptor_t *) d;
[b9e97fb]124
125 td->base_0_15 = base & 0xffff;
126 td->base_16_23 = ((base) >> 16) & 0xff;
127 td->base_24_31 = ((base) >> 24) & 0xff;
128 td->base_32_63 = ((base) >> 32);
129}
130
[39cea6a]131void gdt_tss_setlimit(descriptor_t *d, __u32 limit)
[b9e97fb]132{
[39cea6a]133 struct tss_descriptor *td = (tss_descriptor_t *) d;
[b9e97fb]134
135 td->limit_0_15 = limit & 0xffff;
136 td->limit_16_19 = (limit >> 16) & 0xf;
137}
138
[39cea6a]139void idt_setoffset(idescriptor_t *d, __address offset)
[b9e97fb]140{
141 /*
142 * Offset is a linear address.
143 */
144 d->offset_0_15 = offset & 0xffff;
145 d->offset_16_31 = offset >> 16 & 0xffff;
146 d->offset_32_63 = offset >> 32;
147}
148
[39cea6a]149void tss_initialize(tss_t *t)
[b9e97fb]150{
[39cea6a]151 memsetb((__address) t, sizeof(tss_t), 0);
[b9e97fb]152}
153
154/*
155 * This function takes care of proper setup of IDT and IDTR.
156 */
157void idt_init(void)
158{
[39cea6a]159 idescriptor_t *d;
[b9e97fb]160 int i;
161
162 for (i = 0; i < IDT_ITEMS; i++) {
163 d = &idt[i];
164
165 d->unused = 0;
[33ccb2c]166 d->selector = gdtselector(KTEXT_DES);
[b9e97fb]167
168 d->present = 1;
169 d->type = AR_INTERRUPT; /* masking interrupt */
170
171 idt_setoffset(d, ((__address) interrupt_handlers) + i*interrupt_handler_size);
[49a39c2]172 exc_register(i, "undef", (iroutine)null_interrupt);
[b9e97fb]173 }
[1ee9ced]174
[fcfac420]175 exc_register( 7, "nm_fault", nm_fault);
176 exc_register(12, "ss_fault", ss_fault);
[1ee9ced]177 exc_register(13, "gp_fault", gp_fault);
[dabe6333]178 exc_register(14, "ident_mapper", ident_page_fault);
[b9e97fb]179}
180
[49a39c2]181/** Initialize segmentation - code/data/idt tables
182 *
183 */
[b9e97fb]184void pm_init(void)
185{
[39cea6a]186 descriptor_t *gdt_p = (struct descriptor *) gdtr.base;
187 tss_descriptor_t *tss_desc;
[b9e97fb]188
189 /*
190 * Each CPU has its private GDT and TSS.
191 * All CPUs share one IDT.
192 */
193
194 if (config.cpu_active == 1) {
195 idt_init();
196 /*
197 * NOTE: bootstrap CPU has statically allocated TSS, because
198 * the heap hasn't been initialized so far.
199 */
200 tss_p = &tss;
201 }
202 else {
[39cea6a]203 tss_p = (struct tss *) malloc(sizeof(tss_t), FRAME_ATOMIC);
[b9e97fb]204 if (!tss_p)
205 panic("could not allocate TSS\n");
206 }
207
208 tss_initialize(tss_p);
209
[39cea6a]210 tss_desc = (tss_descriptor_t *) (&gdt_p[TSS_DES]);
[e291e5d]211 tss_desc->present = 1;
212 tss_desc->type = AR_TSS;
213 tss_desc->dpl = PL_KERNEL;
[b9e97fb]214
215 gdt_tss_setbase(&gdt_p[TSS_DES], (__address) tss_p);
[39cea6a]216 gdt_tss_setlimit(&gdt_p[TSS_DES], sizeof(tss_t) - 1);
[b9e97fb]217
[897ad60]218 gdtr_load(&gdtr);
219 idtr_load(&idtr);
[b9e97fb]220 /*
221 * As of this moment, the current CPU has its own GDT pointing
222 * to its own TSS. We just need to load the TR register.
223 */
[897ad60]224 tr_load(gdtselector(TSS_DES));
[b9e97fb]225}
Note: See TracBrowser for help on using the repository browser.