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