]> Joshua Wise's Git repositories - netwatch.git/blame - netwatch/pagingstub.c
Fix LICENSE.
[netwatch.git] / netwatch / pagingstub.c
CommitLineData
3c4e084d
JP
1/* pagingstub.c
2 * Paging enable routines
3 * NetWatch system management mode administration console
4 *
5 * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved.
6 * This program is free software; you can redistribute and/or modify it under
7 * the terms found in the file LICENSE in the root of this source tree.
8 *
9 */
10
11
9e2a82e4
JP
12#include <io.h>
13#include <smram.h>
14#include <video_defines.h>
15#include <minilib.h>
16#include <smi.h>
17#include <pci-bother.h>
e68cc768 18#include <serial.h>
923ea2c2 19#include <output.h>
ed22a699 20#include "traps.h"
9e2a82e4
JP
21#include "../net/net.h"
22#include "vga-overlay.h"
23
722e5aea 24extern void smi_init();
9e2a82e4 25#include "vm_flags.h"
9e2a82e4 26
923ea2c2 27extern void smi_entry();
9e2a82e4 28void set_cr0(unsigned int);
b908495a 29void ps_switch_stack (void (*call)(), int stack);
9e2a82e4 30
9e2a82e4
JP
31#define get_cr0() \
32 ({ \
33 register unsigned int _temp__; \
34 asm volatile("mov %%cr0, %0" : "=r" (_temp__)); \
35 _temp__; \
36 })
9e2a82e4
JP
37#define set_cr3(value) \
38 { \
39 register unsigned int _temp__ = (value); \
40 asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \
41 }
e8f20fe7
JW
42
43#define get_cr4() \
44 ({ \
45 register unsigned int _temp__; \
46 asm volatile("mov %%cr4, %0" : "=r" (_temp__)); \
47 _temp__; \
48 })
49#define set_cr4(value) \
50 { \
51 register unsigned int _temp__ = (value); \
52 asm volatile("mov %0, %%cr4" : : "r" (_temp__)); \
53 }
54
9e2a82e4 55#define CR0_PG 0x80000000
e8f20fe7 56#define CR4_PSE 0x00000010
9e2a82e4
JP
57
58#define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
59
923ea2c2
JW
60static int initialized = 0;
61static int paging_enb = 0;
62static unsigned long *pd;
63
64extern int _bss, _bssend, _end;
65
9343e933
JW
66static unsigned long curmapped = 0xFFFFFFFF;
67
923ea2c2
JW
68unsigned long v2p(void *virt)
69{
70 unsigned long _virt = (unsigned long)virt;
71
72 if (!paging_enb)
73 return _virt;
74
f8903fdd
JW
75 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(_virt)];
76 unsigned long pte;
77
78 if (!(pde & PTE_PRESENT))
79 return 0xFFFFFFFF;
80
81 if (pde & PDE_PAGE_SIZE)
82 return ADDR_12_MASK(pde) + (_virt & 0x3FFFFF);
9343e933 83
f8903fdd
JW
84 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(_virt)];
85 if (!(pte & PTE_PRESENT))
86 return 0xFFFFFFFF;
87 return (pte & ~0xFFF) + (_virt & 0xFFF);
923ea2c2
JW
88}
89
90void *p2v(unsigned long phys)
91{
92 if (!paging_enb)
93 return (void*)phys;
94
95 if (phys >= 0xA0000 && phys < 0xC0000)
96 return (void*)phys;
9343e933 97 if (phys >= 0x1FF82000 && phys < 0x20000000)
923ea2c2 98 return (void*)(phys - 0x1FF82000 + 0x200000);
9343e933
JW
99 if (phys >= 0x1FF80000 && phys < 0x1FF82000)
100 return (void*)(phys - 0x1FF80000 + 0x1F0000);
101
102 if ((phys & ~0xFFF) != curmapped) /* If it's not mapped, map it in. */
103 {
104 curmapped = phys & ~0xFFF;
105 addmap(0x4000, curmapped);
106 asm volatile("invlpg 0x4000");
107 }
108 return (void*)(0x4000 + (phys & 0xFFF));
923ea2c2
JW
109}
110
9343e933 111int addmap(unsigned long vaddr, unsigned long paddr)
923ea2c2
JW
112{
113 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
114 unsigned long *pt;
115
116 if (!(pde & PTE_PRESENT))
117 return -1;
118
119 pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
120 pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
121
122 return 0;
123}
124
e8f20fe7
JW
125int addmap_4m(unsigned long vaddr, unsigned long paddr)
126{
127 /* PDE_PAGE_SIZE = (1 << 7) */
128 ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)] =
129 paddr | PDE_PRESENT | PDE_READ_WRITE | PDE_PAGE_SIZE;
130
131 return 0;
132}
133
9343e933
JW
134void *demap(unsigned long client_pd, unsigned long vaddr)
135{
136 unsigned long pde = ((unsigned long *)p2v(client_pd))[PDE_FOR(vaddr)];
137 unsigned long pte;
138
139 if (!(pde & PTE_PRESENT))
140 return (void*)0x0;
141 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(vaddr)];
142 if (!(pte & PTE_PRESENT))
143 return (void*)0x0;
144 return p2v((pte & ~0xFFF) + (vaddr & 0xFFF));
145}
146
147static void pt_setup(int tseg_start, int tseg_size) {
9e2a82e4 148 int i;
9e2a82e4 149
923ea2c2
JW
150 /* The page directory and page table live at TSEG and TSEG + 0x1000,
151 * respectively. */
152 unsigned long *pagedirectory = (unsigned long *) tseg_start;
153 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
9343e933
JW
154
155 pd = pagedirectory;
9e2a82e4
JP
156
157 /* Clear out the page directory except for one entry pointing to the
158 * page table, and clear the page table entirely. */
b908495a 159 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
9e2a82e4 160 for (i = 1; i < 1024; i++)
9e2a82e4 161 pagedirectory[i] = 0;
9e2a82e4 162
9e2a82e4 163 for (i = 0; i < 1024; i++)
9e2a82e4 164 pagetable[i] = 0;
9e2a82e4 165
923ea2c2
JW
166 /* Map 0x0A0000:0x0BFFFF to itself. */
167 for (i = 0; i < 32; i++)
9343e933 168 addmap(0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
9e2a82e4 169
9343e933 170 /* Map 0x200000:0x300000 to TSEG data */
923ea2c2 171 for (i = 0; i < 256; i++)
9343e933 172 addmap(0x200000 + i * 0x1000, tseg_start + (i + 2) * 0x1000);
9e2a82e4 173
923ea2c2 174 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
9e2a82e4 175 * RAM the first time around */
923ea2c2 176 for (i = 0; i < 256; i++)
9343e933 177 addmap(0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
9e2a82e4 178
9343e933
JW
179 /* Map 0x1F0000:0x1F2000 to TSEG paging info */
180 for (i = 0; i < 2; i++)
181 addmap(0x1F0000 + i * 0x1000, tseg_start + i * 0x1000);
9e2a82e4
JP
182}
183
b34003c2
JW
184void init_and_run(void)
185{
186 if (!initialized)
187 {
188 smi_init();
189 initialized = 1;
190 }
191
192 smi_entry();
193}
194
9e2a82e4
JP
195void c_entry(void)
196{
923ea2c2 197 paging_enb = 0;
9e2a82e4 198
923ea2c2
JW
199 outb(0x80, 0x01);
200 if (!initialized)
9343e933 201 pt_setup(0x1FF80000, 0x80000);
923ea2c2 202 outb(0x80, 0x02);
113df320
JW
203
204 /* Enable paging. */
923ea2c2 205 set_cr3((unsigned long)pd);
e8f20fe7 206 set_cr4(get_cr4() | CR4_PSE); /* ITT, we 4MByte page. */
9e2a82e4 207 set_cr0(get_cr0() | CR0_PG);
923ea2c2
JW
208 outb(0x80, 0x03);
209 paging_enb = 1;
ed22a699 210
923ea2c2
JW
211 /* If this is the first goround, copy in data. */
212 if (!initialized)
213 {
214 unsigned char *p;
215
216 outb(0x80, 0x04);
217 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
218 *p = *(p + 0x100000);
219 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
220 *p = 0;
221 outb(0x80, 0x05);
113df320 222
923ea2c2 223 /* Only now is it safe to call other functions. */
113df320 224 serial_init();
923ea2c2 225 dolog("Evacuation to TSEG complete.");
ed22a699 226 }
923ea2c2
JW
227
228 outb(0x80, 0x06);
ed22a699
JP
229
230 traps_install();
923ea2c2
JW
231
232 outb(0x80, 0x07);
ed22a699 233
b34003c2 234 ps_switch_stack(init_and_run, 0x270000);
b908495a 235 outb(0x80, 0xFA);
9e2a82e4 236}
This page took 0.049739 seconds and 4 git commands to generate.