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__)); \
28 #define set_cr3(value) \
30 register unsigned int _temp__ = (value); \
31 asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \
33 #define CR0_PG 0x80000000
35 #define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
37 static int initialized = 0;
38 static int paging_enb = 0;
39 static unsigned long *pd;
41 extern int _bss, _bssend, _end;
43 unsigned long v2p(void *virt)
45 unsigned long _virt = (unsigned long)virt;
50 if (_virt >= 0xA0000 && _virt < 0xC0000)
52 if (_virt >= 0x200000 && _virt < 0x300000)
53 return _virt - 0x200000 + /* XXX */ 0x1FF82000;
54 outputf("WARNING: v2p(%08x)", _virt);
58 void *p2v(unsigned long phys)
63 if (phys >= 0xA0000 && phys < 0xC0000)
65 if (phys >= 0x1FF80000 && phys < 0x20000000)
66 return (void*)(phys - 0x1FF82000 + 0x200000);
67 outputf("WARNING: p2v(%08x)", phys);
68 return (void *)0xFFFFFFFF;
72 inline int pt_addmap(unsigned long *pd, unsigned long vaddr, unsigned long paddr)
74 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
77 if (!(pde & PTE_PRESENT))
80 pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
81 pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
86 static void * pt_setup(int tseg_start, int tseg_size) {
89 /* The page directory and page table live at TSEG and TSEG + 0x1000,
91 unsigned long *pagedirectory = (unsigned long *) tseg_start;
92 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
94 /* Clear out the page directory except for one entry pointing to the
95 * page table, and clear the page table entirely. */
96 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
97 for (i = 1; i < 1024; i++)
100 for (i = 0; i < 1024; i++)
103 /* Map 0x0A0000:0x0BFFFF to itself. */
104 for (i = 0; i < 32; i++)
105 pt_addmap(pagedirectory, 0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
107 /* Map 0x200000:0x300000 to TSEG */
108 for (i = 0; i < 256; i++)
109 pt_addmap(pagedirectory, 0x200000 + i * 0x1000, tseg_start + 0x2000 + i * 0x1000);
111 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
112 * RAM the first time around */
113 for (i = 0; i < 256; i++)
114 pt_addmap(pagedirectory, 0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
116 return pagedirectory;
125 pd = pt_setup(0x1FF80000, 0x80000);
129 set_cr3((unsigned long)pd);
130 set_cr0(get_cr0() | CR0_PG);
134 /* If this is the first goround, copy in data. */
140 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
141 *p = *(p + 0x100000);
142 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
146 /* Only now is it safe to call other functions. */
148 dolog("Evacuation to TSEG complete.");
159 smi_init(); /* Run the firstrun. */
166 ps_switch_stack(smi_entry, 0x270000);