debuggers.hg

view tools/firmware/vmxassist/setup.c @ 6644:29808fef9148

merge?
author cl349@firebug.cl.cam.ac.uk
date Sat Sep 03 18:24:46 2005 +0000 (2005-09-03)
parents ed474440decd f27205ea60ef
children b6c98fe62e1a
line source
1 /*
2 * setup.c: Setup the world for vmxassist.
3 *
4 * Leendert van Doorn, leendert@watson.ibm.com
5 * Copyright (c) 2005, International Business Machines Corporation.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 */
20 #include "vm86.h"
21 #include "util.h"
22 #include "machine.h"
24 #if (VMXASSIST_BASE != TEXTADDR)
25 #error VMXAssist base mismatch
26 #endif
28 #define NR_PGD (PGSIZE / sizeof(unsigned))
30 #define min(a, b) ((a) > (b) ? (b) : (a))
32 unsigned long long gdt[] __attribute__ ((aligned(32))) = {
33 0x0000000000000000ULL, /* 0x00: reserved */
34 0x0000890000000000ULL, /* 0x08: 32-bit TSS */
35 0x00CF9A000000FFFFULL, /* 0x10: CS 32-bit */
36 0x00CF92000000FFFFULL, /* 0x18: DS 32-bit */
37 };
39 struct dtr gdtr = { sizeof(gdt)-1, (unsigned long) &gdt };
41 struct tss tss __attribute__ ((aligned(4)));
43 unsigned long long idt[NR_TRAPS] __attribute__ ((aligned(32)));
45 struct dtr idtr = { sizeof(idt)-1, (unsigned long) &idt };
47 #ifdef TEST
48 unsigned pgd[NR_PGD] __attribute__ ((aligned(PGSIZE))) = { 0 };
50 struct e820entry e820map[] = {
51 { 0x0000000000000000ULL, 0x000000000009F800ULL, E820_RAM },
52 { 0x000000000009F800ULL, 0x0000000000000800ULL, E820_RESERVED },
53 { 0x00000000000A0000ULL, 0x0000000000020000ULL, E820_IO },
54 { 0x00000000000C0000ULL, 0x0000000000040000ULL, E820_RESERVED },
55 { 0x0000000000100000ULL, 0x0000000000000000ULL, E820_RAM },
56 { 0x0000000000000000ULL, 0x0000000000001000ULL, E820_SHARED },
57 { 0x0000000000000000ULL, 0x0000000000003000ULL, E820_NVS },
58 { 0x0000000000003000ULL, 0x000000000000A000ULL, E820_ACPI },
59 { 0x00000000FEC00000ULL, 0x0000000001400000ULL, E820_IO },
60 };
61 #endif /* TEST */
63 struct vmx_assist_context oldctx;
64 struct vmx_assist_context newctx;
66 unsigned long memory_size;
67 int initialize_real_mode;
69 extern char stack[], stack_top[];
70 extern unsigned trap_handlers[];
72 void
73 banner(void)
74 {
75 printf("VMXAssist (%s)\n", __DATE__);
77 /* Bochs its way to convey memory size */
78 memory_size = ((get_cmos(0x35) << 8) | get_cmos(0x34)) << 6;
79 if (memory_size > 0x3bc000)
80 memory_size = 0x3bc000;
81 memory_size = (memory_size << 10) + 0xF00000;
82 if (memory_size <= 0xF00000)
83 memory_size =
84 (((get_cmos(0x31) << 8) | get_cmos(0x30)) + 0x400) << 10;
85 memory_size += 0x400 << 10; /* + 1MB */
87 #ifdef TEST
88 /* Create an SMAP for our debug environment */
89 e820map[4].size = memory_size - e820map[4].addr - PGSIZE;
90 e820map[5].addr = memory_size - PGSIZE;
91 e820map[6].addr = memory_size;
92 e820map[7].addr += memory_size;
94 *LINUX_E820_MAP_NR = sizeof(e820map)/sizeof(e820map[0]);
95 memcpy(LINUX_E820_MAP, e820map, sizeof(e820map));
96 #endif
98 printf("Memory size %ld MB\n", memory_size >> 20);
99 printf("E820 map:\n");
100 print_e820_map(LINUX_E820_MAP, *LINUX_E820_MAP_NR);
101 printf("\n");
102 }
104 #ifdef TEST
105 void
106 setup_paging(void)
107 {
108 unsigned long i;
110 if (((unsigned)pgd & ~PGMASK) != 0)
111 panic("PGD not page aligned");
112 set_cr4(get_cr4() | CR4_PSE);
113 for (i = 0; i < NR_PGD; i++)
114 pgd[i] = (i * LPGSIZE)| PTE_PS | PTE_US | PTE_RW | PTE_P;
115 set_cr3((unsigned) pgd);
116 set_cr0(get_cr0() | (CR0_PE|CR0_PG));
117 }
118 #endif /* TEST */
120 void
121 setup_gdt(void)
122 {
123 /* setup task state segment */
124 memset(&tss, 0, sizeof(tss));
125 tss.ss0 = DATA_SELECTOR;
126 tss.esp0 = (unsigned) stack_top - 4*4;
127 tss.iomap_base = offsetof(struct tss, iomap);
129 /* initialize gdt's tss selector */
130 unsigned long long addr = (unsigned long long) &tss;
131 gdt[TSS_SELECTOR / sizeof(gdt[0])] |=
132 ((addr & 0xFF000000) << (56-24)) |
133 ((addr & 0x00FF0000) << (32-16)) |
134 ((addr & 0x0000FFFF) << (16)) |
135 (sizeof(tss) - 1);
137 /* switch to our own gdt and set current tss */
138 __asm__ __volatile__ ("lgdt %0" : : "m" (gdtr));
139 __asm__ __volatile__ ("movl %%eax,%%ds;"
140 "movl %%eax,%%es;"
141 "movl %%eax,%%fs;"
142 "movl %%eax,%%gs;"
143 "movl %%eax,%%ss" : : "a" (DATA_SELECTOR));
145 __asm__ __volatile__ ("ljmp %0,$1f; 1:" : : "i" (CODE_SELECTOR));
147 __asm__ __volatile__ ("ltr %%ax" : : "a" (TSS_SELECTOR));
148 }
150 void
151 set_intr_gate(int i, unsigned handler)
152 {
153 unsigned long long addr = handler;
155 idt[i] = ((addr & 0xFFFF0000ULL) << 32) | (0x8E00ULL << 32) |
156 (addr & 0xFFFFULL) | (CODE_SELECTOR << 16);
157 }
159 void
160 setup_idt(void)
161 {
162 int i;
164 for (i = 0; i < NR_TRAPS; i++)
165 set_intr_gate(i, trap_handlers[i]);
166 __asm__ __volatile__ ("lidt %0" : : "m" (idtr));
167 }
169 void
170 setup_pic(void)
171 {
172 /* mask all interrupts */
173 outb(PIC_MASTER + PIC_IMR, 0xFF);
174 outb(PIC_SLAVE + PIC_IMR, 0xFF);
176 /* setup master PIC */
177 outb(PIC_MASTER + PIC_CMD, 0x11); /* edge triggered, cascade, ICW4 */
178 outb(PIC_MASTER + PIC_IMR, NR_EXCEPTION_HANDLER);
179 outb(PIC_MASTER + PIC_IMR, 1 << 2); /* slave on channel 2 */
180 outb(PIC_MASTER + PIC_IMR, 0x01);
182 /* setup slave PIC */
183 outb(PIC_SLAVE + PIC_CMD, 0x11); /* edge triggered, cascade, ICW4 */
184 outb(PIC_SLAVE + PIC_IMR, NR_EXCEPTION_HANDLER + 8);
185 outb(PIC_SLAVE + PIC_IMR, 0x02); /* slave identity is 2 */
186 outb(PIC_SLAVE + PIC_IMR, 0x01);
188 /* enable all interrupts */
189 outb(PIC_MASTER + PIC_IMR, 0);
190 outb(PIC_SLAVE + PIC_IMR, 0);
191 }
193 void
194 enter_real_mode(struct regs *regs)
195 {
196 /* mask off TSS busy bit */
197 gdt[TSS_SELECTOR / sizeof(gdt[0])] &= ~0x0000020000000000ULL;
199 /* start 8086 emulation of BIOS */
200 if (initialize_real_mode) {
201 initialize_real_mode = 0;
202 regs->eflags |= EFLAGS_VM | 0x02;
203 regs->ves = regs->vds = regs->vfs = regs->vgs = 0xF000;
204 regs->cs = 0xF000; /* ROM BIOS POST entry point */
205 #ifdef TEST
206 regs->eip = 0xFFE0;
207 #else
208 regs->eip = 0xFFF0;
209 #endif
210 regs->uesp = 0;
211 regs->uss = 0;
212 printf("Starting emulated 16-bit real-mode: ip=%04x:%04x\n",
213 regs->cs, regs->eip);
215 mode = VM86_REAL; /* becomes previous mode */
216 set_mode(regs, VM86_REAL);
218 /* this should get us into 16-bit mode */
219 return;
220 } else {
221 /* go from protected to real mode */
222 regs->eflags |= EFLAGS_VM;
224 set_mode(regs, VM86_PROTECTED_TO_REAL);
226 emulate(regs);
227 }
228 }
230 /*
231 * Setup the environment for VMX assist.
232 * This environment consists of flat segments (code and data),
233 * its own gdt, idt, and tr.
234 */
235 void
236 setup_ctx(void)
237 {
238 struct vmx_assist_context *c = &newctx;
240 memset(c, 0, sizeof(*c));
241 c->eip = (unsigned long) switch_to_real_mode;
242 c->esp = (unsigned) stack_top - 4*4;
243 c->eflags = 0x2; /* no interrupts, please */
245 /*
246 * Obviously, vmx assist is not running with CR0_PE disabled.
247 * The reason why the vmx assist cr0 has CR0.PE disabled is
248 * that a transtion to CR0.PE causes a world switch. It seems
249 * more natural to enable CR0.PE to cause a world switch to
250 * protected mode rather than disabling it.
251 */
252 #ifdef TEST
253 c->cr0 = (get_cr0() | CR0_NE | CR0_PG) & ~CR0_PE;
254 c->cr3 = (unsigned long) pgd;
255 #else
256 c->cr0 = (get_cr0() | CR0_NE) & ~CR0_PE;
257 c->cr3 = 0;
258 #endif
259 c->cr4 = get_cr4();
261 c->idtr_limit = sizeof(idt)-1;
262 c->idtr_base = (unsigned long) &idt;
264 c->gdtr_limit = sizeof(gdt)-1;
265 c->gdtr_base = (unsigned long) &gdt;
267 c->cs_sel = CODE_SELECTOR;
268 c->cs_limit = 0xFFFFFFFF;
269 c->cs_base = 0;
270 c->cs_arbytes.fields.seg_type = 0xb;
271 c->cs_arbytes.fields.s = 1;
272 c->cs_arbytes.fields.dpl = 0;
273 c->cs_arbytes.fields.p = 1;
274 c->cs_arbytes.fields.avl = 0;
275 c->cs_arbytes.fields.default_ops_size = 1;
276 c->cs_arbytes.fields.g = 1;
278 c->ds_sel = DATA_SELECTOR;
279 c->ds_limit = 0xFFFFFFFF;
280 c->ds_base = 0;
281 c->ds_arbytes = c->cs_arbytes;
282 c->ds_arbytes.fields.seg_type = 0x3;
284 c->es_sel = DATA_SELECTOR;
285 c->es_limit = 0xFFFFFFFF;
286 c->es_base = 0;
287 c->es_arbytes = c->ds_arbytes;
289 c->ss_sel = DATA_SELECTOR;
290 c->ss_limit = 0xFFFFFFFF;
291 c->ss_base = 0;
292 c->ss_arbytes = c->ds_arbytes;
294 c->fs_sel = DATA_SELECTOR;
295 c->fs_limit = 0xFFFFFFFF;
296 c->fs_base = 0;
297 c->fs_arbytes = c->ds_arbytes;
299 c->gs_sel = DATA_SELECTOR;
300 c->gs_limit = 0xFFFFFFFF;
301 c->gs_base = 0;
302 c->gs_arbytes = c->ds_arbytes;
304 c->tr_sel = TSS_SELECTOR;
305 c->tr_limit = sizeof(tss) - 1;
306 c->tr_base = (unsigned long) &tss;
307 c->tr_arbytes.fields.seg_type = 0xb; /* 0x9 | 0x2 (busy) */
308 c->tr_arbytes.fields.s = 0;
309 c->tr_arbytes.fields.dpl = 0;
310 c->tr_arbytes.fields.p = 1;
311 c->tr_arbytes.fields.avl = 0;
312 c->tr_arbytes.fields.default_ops_size = 0;
313 c->tr_arbytes.fields.g = 0;
315 c->ldtr_sel = 0;
316 c->ldtr_limit = 0;
317 c->ldtr_base = 0;
318 c->ldtr_arbytes = c->ds_arbytes;
319 c->ldtr_arbytes.fields.seg_type = 0x2;
320 c->ldtr_arbytes.fields.s = 0;
321 c->ldtr_arbytes.fields.dpl = 0;
322 c->ldtr_arbytes.fields.p = 1;
323 c->ldtr_arbytes.fields.avl = 0;
324 c->ldtr_arbytes.fields.default_ops_size = 0;
325 c->ldtr_arbytes.fields.g = 0;
326 }
328 /*
329 * Start BIOS by causing a world switch to vmxassist, which causes
330 * VM8086 to be enabled and control is transfered to F000:FFF0.
331 */
332 void
333 start_bios(void)
334 {
335 unsigned long cr0;
337 printf("Start BIOS ...\n");
338 initialize_real_mode = 1;
339 cr0 = get_cr0();
340 #ifndef TEST
341 set_cr0(cr0 | CR0_PE);
342 #endif
343 set_cr0(cr0 & ~CR0_PE);
344 panic("vmxassist returned"); /* "cannot happen" */
345 }
347 int
348 main()
349 {
350 banner();
351 #ifdef TEST
352 setup_paging();
353 #endif
354 setup_gdt();
355 setup_idt();
356 #ifndef TEST
357 set_cr4(get_cr4() | CR4_VME);
358 #endif
359 setup_ctx();
360 setup_pic();
361 start_bios();
362 return 0;
363 }