]> Joshua Wise's Git repositories - netwatch.git/blame - aseg-paging/pagingstub.c
rfb data
[netwatch.git] / aseg-paging / pagingstub.c
CommitLineData
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>
923ea2c2 8#include <output.h>
ed22a699 9#include "traps.h"
9e2a82e4
JP
10#include "../net/net.h"
11#include "vga-overlay.h"
12
722e5aea 13extern void smi_init();
9e2a82e4 14#include "vm_flags.h"
9e2a82e4 15
923ea2c2 16extern void smi_entry();
9e2a82e4 17void set_cr0(unsigned int);
b908495a 18void ps_switch_stack (void (*call)(), int stack);
9e2a82e4 19
9e2a82e4
JP
20#define get_cr0() \
21 ({ \
22 register unsigned int _temp__; \
23 asm volatile("mov %%cr0, %0" : "=r" (_temp__)); \
24 _temp__; \
25 })
26
27
28#define set_cr3(value) \
29 { \
30 register unsigned int _temp__ = (value); \
31 asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \
32 }
33#define CR0_PG 0x80000000
34
35#define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
36
923ea2c2
JW
37static int initialized = 0;
38static int paging_enb = 0;
39static unsigned long *pd;
40
41extern int _bss, _bssend, _end;
42
9343e933
JW
43static unsigned long curmapped = 0xFFFFFFFF;
44
923ea2c2
JW
45unsigned long v2p(void *virt)
46{
47 unsigned long _virt = (unsigned long)virt;
48
49 if (!paging_enb)
50 return _virt;
51
52 if (_virt >= 0xA0000 && _virt < 0xC0000)
53 return _virt;
54 if (_virt >= 0x200000 && _virt < 0x300000)
55 return _virt - 0x200000 + /* XXX */ 0x1FF82000;
9343e933
JW
56 if (_virt >= 0x1F0000 && _virt < 0x1F2000)
57 return _virt - 0x1F0000 + 0x1FF80000;
58 if ((_virt & ~0xFFF) == 0x4000)
59 return _virt - 0x4000 + curmapped;
60
923ea2c2
JW
61 outputf("WARNING: v2p(%08x)", _virt);
62 return 0xFFFFFFFF;
63}
64
65void *p2v(unsigned long phys)
66{
67 if (!paging_enb)
68 return (void*)phys;
69
70 if (phys >= 0xA0000 && phys < 0xC0000)
71 return (void*)phys;
9343e933 72 if (phys >= 0x1FF82000 && phys < 0x20000000)
923ea2c2 73 return (void*)(phys - 0x1FF82000 + 0x200000);
9343e933
JW
74 if (phys >= 0x1FF80000 && phys < 0x1FF82000)
75 return (void*)(phys - 0x1FF80000 + 0x1F0000);
76
77 if ((phys & ~0xFFF) != curmapped) /* If it's not mapped, map it in. */
78 {
79 curmapped = phys & ~0xFFF;
80 addmap(0x4000, curmapped);
81 asm volatile("invlpg 0x4000");
82 }
83 return (void*)(0x4000 + (phys & 0xFFF));
923ea2c2
JW
84}
85
9343e933 86int addmap(unsigned long vaddr, unsigned long paddr)
923ea2c2
JW
87{
88 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
89 unsigned long *pt;
90
91 if (!(pde & PTE_PRESENT))
92 return -1;
93
94 pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
95 pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
96
97 return 0;
98}
99
9343e933
JW
100void *demap(unsigned long client_pd, unsigned long vaddr)
101{
102 unsigned long pde = ((unsigned long *)p2v(client_pd))[PDE_FOR(vaddr)];
103 unsigned long pte;
104
105 if (!(pde & PTE_PRESENT))
106 return (void*)0x0;
107 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(vaddr)];
108 if (!(pte & PTE_PRESENT))
109 return (void*)0x0;
110 return p2v((pte & ~0xFFF) + (vaddr & 0xFFF));
111}
112
113static void pt_setup(int tseg_start, int tseg_size) {
9e2a82e4 114 int i;
9e2a82e4 115
923ea2c2
JW
116 /* The page directory and page table live at TSEG and TSEG + 0x1000,
117 * respectively. */
118 unsigned long *pagedirectory = (unsigned long *) tseg_start;
119 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
9343e933
JW
120
121 pd = pagedirectory;
9e2a82e4
JP
122
123 /* Clear out the page directory except for one entry pointing to the
124 * page table, and clear the page table entirely. */
b908495a 125 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
9e2a82e4 126 for (i = 1; i < 1024; i++)
9e2a82e4 127 pagedirectory[i] = 0;
9e2a82e4 128
9e2a82e4 129 for (i = 0; i < 1024; i++)
9e2a82e4 130 pagetable[i] = 0;
9e2a82e4 131
923ea2c2
JW
132 /* Map 0x0A0000:0x0BFFFF to itself. */
133 for (i = 0; i < 32; i++)
9343e933 134 addmap(0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
9e2a82e4 135
9343e933 136 /* Map 0x200000:0x300000 to TSEG data */
923ea2c2 137 for (i = 0; i < 256; i++)
9343e933 138 addmap(0x200000 + i * 0x1000, tseg_start + (i + 2) * 0x1000);
9e2a82e4 139
923ea2c2 140 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
9e2a82e4 141 * RAM the first time around */
923ea2c2 142 for (i = 0; i < 256; i++)
9343e933 143 addmap(0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
9e2a82e4 144
9343e933
JW
145 /* Map 0x1F0000:0x1F2000 to TSEG paging info */
146 for (i = 0; i < 2; i++)
147 addmap(0x1F0000 + i * 0x1000, tseg_start + i * 0x1000);
9e2a82e4
JP
148}
149
150void c_entry(void)
151{
923ea2c2 152 paging_enb = 0;
9e2a82e4 153
923ea2c2
JW
154 outb(0x80, 0x01);
155 if (!initialized)
9343e933 156 pt_setup(0x1FF80000, 0x80000);
923ea2c2 157 outb(0x80, 0x02);
113df320
JW
158
159 /* Enable paging. */
923ea2c2 160 set_cr3((unsigned long)pd);
9e2a82e4 161 set_cr0(get_cr0() | CR0_PG);
923ea2c2
JW
162 outb(0x80, 0x03);
163 paging_enb = 1;
ed22a699 164
923ea2c2
JW
165 /* If this is the first goround, copy in data. */
166 if (!initialized)
167 {
168 unsigned char *p;
169
170 outb(0x80, 0x04);
171 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
172 *p = *(p + 0x100000);
173 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
174 *p = 0;
175 outb(0x80, 0x05);
113df320 176
923ea2c2 177 /* Only now is it safe to call other functions. */
113df320 178 serial_init();
923ea2c2 179 dolog("Evacuation to TSEG complete.");
ed22a699 180 }
923ea2c2
JW
181
182 outb(0x80, 0x06);
ed22a699
JP
183
184 traps_install();
923ea2c2
JW
185
186 outb(0x80, 0x07);
ed22a699 187
923ea2c2
JW
188 if (!initialized)
189 {
190 smi_init(); /* Run the firstrun. */
191 outb(0x80, 0x08);
113df320 192
923ea2c2 193 initialized = 1;
9e2a82e4 194 }
923ea2c2
JW
195
196 outb(0x80, 0x09);
113df320 197 ps_switch_stack(smi_entry, 0x270000);
b908495a 198 outb(0x80, 0xFA);
9e2a82e4 199}
This page took 0.047048 seconds and 4 git commands to generate.