3 #include <video_defines.h>
6 #include <pci-bother.h>
10 #include "../net/net.h"
11 #include "vga-overlay.h"
13 extern void smi_init();
16 extern void smi_entry();
17 void set_cr0(unsigned int);
18 void ps_switch_stack (void (*call)(), int stack);
22 register unsigned int _temp__; \
23 asm volatile("mov %%cr0, %0" : "=r" (_temp__)); \
26 #define set_cr3(value) \
28 register unsigned int _temp__ = (value); \
29 asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \
34 register unsigned int _temp__; \
35 asm volatile("mov %%cr4, %0" : "=r" (_temp__)); \
38 #define set_cr4(value) \
40 register unsigned int _temp__ = (value); \
41 asm volatile("mov %0, %%cr4" : : "r" (_temp__)); \
44 #define CR0_PG 0x80000000
45 #define CR4_PSE 0x00000010
47 #define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
49 static int initialized = 0;
50 static int paging_enb = 0;
51 static unsigned long *pd;
53 extern int _bss, _bssend, _end;
55 static unsigned long curmapped = 0xFFFFFFFF;
57 unsigned long v2p(void *virt)
59 unsigned long _virt = (unsigned long)virt;
64 if (_virt >= 0xA0000 && _virt < 0xC0000)
66 if (_virt >= 0x200000 && _virt < 0x300000)
67 return _virt - 0x200000 + /* XXX */ 0x1FF82000;
68 if (_virt >= 0x1F0000 && _virt < 0x1F2000)
69 return _virt - 0x1F0000 + 0x1FF80000;
70 if ((_virt & ~0xFFF) == 0x4000)
71 return _virt - 0x4000 + curmapped;
73 outputf("WARNING: v2p(%08x)", _virt);
77 void *p2v(unsigned long phys)
82 if (phys >= 0xA0000 && phys < 0xC0000)
84 if (phys >= 0x1FF82000 && phys < 0x20000000)
85 return (void*)(phys - 0x1FF82000 + 0x200000);
86 if (phys >= 0x1FF80000 && phys < 0x1FF82000)
87 return (void*)(phys - 0x1FF80000 + 0x1F0000);
89 if ((phys & ~0xFFF) != curmapped) /* If it's not mapped, map it in. */
91 curmapped = phys & ~0xFFF;
92 addmap(0x4000, curmapped);
93 asm volatile("invlpg 0x4000");
95 return (void*)(0x4000 + (phys & 0xFFF));
98 int addmap(unsigned long vaddr, unsigned long paddr)
100 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
103 if (!(pde & PTE_PRESENT))
106 pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
107 pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
112 int addmap_4m(unsigned long vaddr, unsigned long paddr)
114 /* PDE_PAGE_SIZE = (1 << 7) */
115 ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)] =
116 paddr | PDE_PRESENT | PDE_READ_WRITE | PDE_PAGE_SIZE;
121 void *demap(unsigned long client_pd, unsigned long vaddr)
123 unsigned long pde = ((unsigned long *)p2v(client_pd))[PDE_FOR(vaddr)];
126 if (!(pde & PTE_PRESENT))
128 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(vaddr)];
129 if (!(pte & PTE_PRESENT))
131 return p2v((pte & ~0xFFF) + (vaddr & 0xFFF));
134 static void pt_setup(int tseg_start, int tseg_size) {
137 /* The page directory and page table live at TSEG and TSEG + 0x1000,
139 unsigned long *pagedirectory = (unsigned long *) tseg_start;
140 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
144 /* Clear out the page directory except for one entry pointing to the
145 * page table, and clear the page table entirely. */
146 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
147 for (i = 1; i < 1024; i++)
148 pagedirectory[i] = 0;
150 for (i = 0; i < 1024; i++)
153 /* Map 0x0A0000:0x0BFFFF to itself. */
154 for (i = 0; i < 32; i++)
155 addmap(0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
157 /* Map 0x200000:0x300000 to TSEG data */
158 for (i = 0; i < 256; i++)
159 addmap(0x200000 + i * 0x1000, tseg_start + (i + 2) * 0x1000);
161 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
162 * RAM the first time around */
163 for (i = 0; i < 256; i++)
164 addmap(0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
166 /* Map 0x1F0000:0x1F2000 to TSEG paging info */
167 for (i = 0; i < 2; i++)
168 addmap(0x1F0000 + i * 0x1000, tseg_start + i * 0x1000);
177 pt_setup(0x1FF80000, 0x80000);
181 set_cr3((unsigned long)pd);
182 set_cr4(get_cr4() | CR4_PSE); /* ITT, we 4MByte page. */
183 set_cr0(get_cr0() | CR0_PG);
187 /* If this is the first goround, copy in data. */
193 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
194 *p = *(p + 0x100000);
195 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
199 /* Only now is it safe to call other functions. */
201 dolog("Evacuation to TSEG complete.");
212 smi_init(); /* Run the firstrun. */
219 ps_switch_stack(smi_entry, 0x270000);