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