]>
Commit | Line | Data |
---|---|---|
1 | #include <io.h> | |
2 | #include <smram.h> | |
3 | #include <video_defines.h> | |
4 | #include <minilib.h> | |
5 | #include <smi.h> | |
6 | #include <pci-bother.h> | |
7 | #include <serial.h> | |
8 | #include "../net/net.h" | |
9 | #include "vga-overlay.h" | |
10 | ||
11 | #include "vm_flags.h" | |
12 | #include "pagetable.h" | |
13 | ||
14 | void set_cr0(unsigned int); | |
15 | void ps_switch_stack (void (*call)(), int stack); | |
16 | ||
17 | extern int entry_initialized; | |
18 | extern int _bss, _bssend, _end; | |
19 | void smi_entry(); | |
20 | #define get_cr0() \ | |
21 | ({ \ | |
22 | register unsigned int _temp__; \ | |
23 | asm volatile("mov %%cr0, %0" : "=r" (_temp__)); \ | |
24 | _temp__; \ | |
25 | }) | |
26 | ||
27 | ||
28 | #define set_cr3(value) \ | |
29 | { \ | |
30 | register unsigned int _temp__ = (value); \ | |
31 | asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \ | |
32 | } | |
33 | #define CR0_PG 0x80000000 | |
34 | ||
35 | #define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE) | |
36 | ||
37 | void * pt_setup(int smbase, int tseg_start, int tseg_size) { | |
38 | int i; | |
39 | outb(0x80, 0x51); | |
40 | ||
41 | /* The page directory and page table live at SMBASE and SMBASE + 0x1000, | |
42 | * respectively; clear them. */ | |
43 | int * pagedirectory = (int *) tseg_start; | |
44 | int * pagetable = (int *) (tseg_start + 0x1000); | |
45 | ||
46 | /* Clear out the page directory except for one entry pointing to the | |
47 | * page table, and clear the page table entirely. */ | |
48 | outb(0x80, 0x52); | |
49 | pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE; | |
50 | outb(0x80, 0x53); | |
51 | for (i = 1; i < 1024; i++) | |
52 | { | |
53 | pagedirectory[i] = 0; | |
54 | } | |
55 | ||
56 | outb(0x80, 0x54); | |
57 | for (i = 0; i < 1024; i++) | |
58 | { | |
59 | pagetable[i] = 0; | |
60 | } | |
61 | outb(0x80, 0x55); | |
62 | ||
63 | /* The page at 0x10000 - 0x10FFF points to the SMI entry point, | |
64 | * SMBASE + 0x8000. */ | |
65 | pagetable[16] = (0x8000 + smbase) | MAP_FLAGS; | |
66 | ||
67 | /* 0x11000 to 0x1EFFF map to the rest of ASEG up to SMBASE + 0xF000; | |
68 | * the page containing the saved state is not mappped to our code | |
69 | * region. */ | |
70 | ||
71 | for (i = 0; i < 8; i++) | |
72 | { | |
73 | pagetable[17 + i] = (i * 0x1000 + smbase) | MAP_FLAGS; | |
74 | } | |
75 | ||
76 | for (i = 0; i < 6; i++) | |
77 | { | |
78 | pagetable[25 + i] = (smbase + 0x9000 + i * 0x1000) | MAP_FLAGS; | |
79 | } | |
80 | ||
81 | outb(0x80, 0x56); | |
82 | /* Map 0xA8000 to itself. */ | |
83 | ||
84 | for (i = 0; i < 32; i++) { | |
85 | pagetable[0xA0 + i] = (0xA0000 + i * 0x1000) | MAP_FLAGS; | |
86 | } | |
87 | ||
88 | /* Map 0x200000 to TSEG */ | |
89 | for (i = 0; i < 128; i++) { | |
90 | pagetable[0x200 + i] = (tseg_start + 0x2000 + i * 0x1000) | MAP_FLAGS; | |
91 | } | |
92 | ||
93 | /* Map 0x300000 -> 0x200000, so we can copy our code out of | |
94 | * RAM the first time around */ | |
95 | for (i = 0; i < 256; i++) { | |
96 | pagetable[0x300 + i] = (0x200000 + i * 0x1000) | MAP_FLAGS; | |
97 | } | |
98 | ||
99 | outb(0x80, 0x57); | |
100 | return pagedirectory; | |
101 | } | |
102 | ||
103 | void c_entry(void) | |
104 | { | |
105 | unsigned char *bp; | |
106 | ||
107 | outb(0x80, 0x41); | |
108 | char * pagedir = pt_setup(0xA0000, 0x1FF80000, 0x80000); | |
109 | outb(0x80, 0x43); | |
110 | set_cr3((int)pagedir); | |
111 | outb(0x80, 0xA5); | |
112 | ||
113 | /* Turn paging on */ | |
114 | set_cr0(get_cr0() | CR0_PG); | |
115 | serial_init(); | |
116 | serial_tx('A'); | |
117 | outb(0x80, 0xAA); | |
118 | ||
119 | ||
120 | if (!entry_initialized) { | |
121 | serial_tx('B'); | |
122 | outb(0x80, 0xAB); | |
123 | for (bp = (void *)0x200000; (void *)bp < (void *)&_bss; bp++) | |
124 | *bp = *(bp + 0x100000); | |
125 | for (bp = (void *)&_bss; (void *)bp < (void *)&_bssend; bp++) | |
126 | *bp = 0; | |
127 | } | |
128 | ||
129 | outb(0x80, 0xAC); | |
130 | ps_switch_stack(smi_entry, 0x2FF000); | |
131 | outb(0x80, 0xFA); | |
132 | } |