#include <minilib.h>
#include <smi.h>
#include <pci-bother.h>
+#include <serial.h>
+#include <output.h>
+#include "traps.h"
#include "../net/net.h"
#include "vga-overlay.h"
+extern void smi_init();
#include "vm_flags.h"
-#include "pagetable.h"
+extern void smi_entry();
void set_cr0(unsigned int);
void ps_switch_stack (void (*call)(), int stack);
-extern int entry_initialized;
-extern int _bss, _bssend, _end;
-void smi_entry();
#define get_cr0() \
({ \
register unsigned int _temp__; \
#define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
-void * pt_setup(int smbase, int tseg_start, int tseg_size) {
+static int initialized = 0;
+static int paging_enb = 0;
+static unsigned long *pd;
+
+extern int _bss, _bssend, _end;
+
+unsigned long v2p(void *virt)
+{
+ unsigned long _virt = (unsigned long)virt;
+
+ if (!paging_enb)
+ return _virt;
+
+ if (_virt >= 0xA0000 && _virt < 0xC0000)
+ return _virt;
+ if (_virt >= 0x200000 && _virt < 0x300000)
+ return _virt - 0x200000 + /* XXX */ 0x1FF82000;
+ outputf("WARNING: v2p(%08x)", _virt);
+ return 0xFFFFFFFF;
+}
+
+void *p2v(unsigned long phys)
+{
+ if (!paging_enb)
+ return (void*)phys;
+
+ if (phys >= 0xA0000 && phys < 0xC0000)
+ return (void*)phys;
+ if (phys >= 0x1FF80000 && phys < 0x20000000)
+ return (void*)(phys - 0x1FF82000 + 0x200000);
+ outputf("WARNING: p2v(%08x)", phys);
+ return (void *)0xFFFFFFFF;
+}
+
+
+inline int pt_addmap(unsigned long *pd, unsigned long vaddr, unsigned long paddr)
+{
+ unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
+ unsigned long *pt;
+
+ if (!(pde & PTE_PRESENT))
+ return -1;
+
+ pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
+ pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
+
+ return 0;
+}
+
+static void * pt_setup(int tseg_start, int tseg_size) {
int i;
- outb(0x80, 0x51);
- /* The page directory and page table live at SMBASE and SMBASE + 0x1000,
- * respectively; clear them. */
- int * pagedirectory = (int *) tseg_start;
- int * pagetable = (int *) (tseg_start + 0x1000);
+ /* The page directory and page table live at TSEG and TSEG + 0x1000,
+ * respectively. */
+ unsigned long *pagedirectory = (unsigned long *) tseg_start;
+ unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
/* Clear out the page directory except for one entry pointing to the
* page table, and clear the page table entirely. */
- outb(0x80, 0x52);
pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
- outb(0x80, 0x53);
for (i = 1; i < 1024; i++)
- {
pagedirectory[i] = 0;
- }
- outb(0x80, 0x54);
for (i = 0; i < 1024; i++)
- {
pagetable[i] = 0;
- }
- outb(0x80, 0x55);
-
- /* The page at 0x10000 - 0x10FFF points to the SMI entry point,
- * SMBASE + 0x8000. */
- pagetable[16] = (0x8000 + smbase) | MAP_FLAGS;
-
- /* 0x11000 to 0x1EFFF map to the rest of ASEG up to SMBASE + 0xF000;
- * the page containing the saved state is not mappped to our code
- * region. */
-
- for (i = 0; i < 8; i++)
- {
- pagetable[17 + i] = (i * 0x1000 + smbase) | MAP_FLAGS;
- }
- for (i = 0; i < 6; i++)
- {
- pagetable[25 + i] = (smbase + 0x9000 + i * 0x1000) | MAP_FLAGS;
- }
-
- outb(0x80, 0x56);
- /* Map 0xA8000 to itself. */
+ /* Map 0x0A0000:0x0BFFFF to itself. */
+ for (i = 0; i < 32; i++)
+ pt_addmap(pagedirectory, 0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
- for (i = 0; i < 32; i++) {
- pagetable[0xA0 + i] = (0xA0000 + i * 0x1000) | MAP_FLAGS;
- }
-
- /* Map 0x200000 to TSEG */
- for (i = 0; i < 128; i++) {
- pagetable[0x200 + i] = (tseg_start + 0x2000 + i * 0x1000) | MAP_FLAGS;
- }
+ /* Map 0x200000:0x300000 to TSEG */
+ for (i = 0; i < 256; i++)
+ pt_addmap(pagedirectory, 0x200000 + i * 0x1000, tseg_start + 0x2000 + i * 0x1000);
- /* Map 0x300000 -> 0x200000, so we can copy our code out of
+ /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
* RAM the first time around */
- for (i = 0; i < 256; i++) {
- pagetable[0x300 + i] = (0x200000 + i * 0x1000) | MAP_FLAGS;
- }
+ for (i = 0; i < 256; i++)
+ pt_addmap(pagedirectory, 0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
- outb(0x80, 0x57);
return pagedirectory;
}
void c_entry(void)
{
- unsigned char *bp;
-
- outb(0x80, 0x41);
- char * pagedir = pt_setup(0xA0000, 0x1FF80000, 0x80000);
- outb(0x80, 0x43);
- set_cr3((int)pagedir);
- outb(0x80, 0xA5);
-
- /* Turn paging on */
+ paging_enb = 0;
+
+ outb(0x80, 0x01);
+ if (!initialized)
+ pd = pt_setup(0x1FF80000, 0x80000);
+ outb(0x80, 0x02);
+
+ /* Enable paging. */
+ set_cr3((unsigned long)pd);
set_cr0(get_cr0() | CR0_PG);
- outb(0x80, 0xAA);
+ outb(0x80, 0x03);
+ paging_enb = 1;
-
- if (!entry_initialized) {
- outb(0x80, 0xAB);
- for (bp = (void *)0x200000; (void *)bp < (void *)&_bss; bp++)
- *bp = *(bp + 0x100000);
- for (bp = (void *)&_bss; (void *)bp < (void *)&_bssend; bp++)
- *bp = 0;
+ /* If this is the first goround, copy in data. */
+ if (!initialized)
+ {
+ unsigned char *p;
+
+ outb(0x80, 0x04);
+ for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
+ *p = *(p + 0x100000);
+ for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
+ *p = 0;
+ outb(0x80, 0x05);
+
+ /* Only now is it safe to call other functions. */
+ serial_init();
+ dolog("Evacuation to TSEG complete.");
}
+
+ outb(0x80, 0x06);
- outb(0x80, 0xAC);
- ps_switch_stack(smi_entry, 0x2FF000);
+ traps_install();
+
+ outb(0x80, 0x07);
+
+ if (!initialized)
+ {
+ smi_init(); /* Run the firstrun. */
+ outb(0x80, 0x08);
+
+ initialized = 1;
+ }
+
+ outb(0x80, 0x09);
+ ps_switch_stack(smi_entry, 0x270000);
outb(0x80, 0xFA);
}