]> Joshua Wise's Git repositories - netwatch.git/blame - netwatch/pagingstub.c
fix Makefile to point to the correct .lds
[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
db9fad13 57#define CR4_OSFXSR 0x00000200
9e2a82e4
JP
58
59#define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
60
923ea2c2
JW
61static int initialized = 0;
62static int paging_enb = 0;
63static unsigned long *pd;
64
65extern int _bss, _bssend, _end;
66
9343e933
JW
67static unsigned long curmapped = 0xFFFFFFFF;
68
923ea2c2
JW
69unsigned long v2p(void *virt)
70{
71 unsigned long _virt = (unsigned long)virt;
72
73 if (!paging_enb)
74 return _virt;
75
f8903fdd
JW
76 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(_virt)];
77 unsigned long pte;
78
79 if (!(pde & PTE_PRESENT))
80 return 0xFFFFFFFF;
81
82 if (pde & PDE_PAGE_SIZE)
83 return ADDR_12_MASK(pde) + (_virt & 0x3FFFFF);
9343e933 84
f8903fdd
JW
85 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(_virt)];
86 if (!(pte & PTE_PRESENT))
87 return 0xFFFFFFFF;
88 return (pte & ~0xFFF) + (_virt & 0xFFF);
923ea2c2
JW
89}
90
91void *p2v(unsigned long phys)
92{
93 if (!paging_enb)
94 return (void*)phys;
95
96 if (phys >= 0xA0000 && phys < 0xC0000)
97 return (void*)phys;
9343e933 98 if (phys >= 0x1FF82000 && phys < 0x20000000)
923ea2c2 99 return (void*)(phys - 0x1FF82000 + 0x200000);
9343e933
JW
100 if (phys >= 0x1FF80000 && phys < 0x1FF82000)
101 return (void*)(phys - 0x1FF80000 + 0x1F0000);
102
103 if ((phys & ~0xFFF) != curmapped) /* If it's not mapped, map it in. */
104 {
105 curmapped = phys & ~0xFFF;
106 addmap(0x4000, curmapped);
107 asm volatile("invlpg 0x4000");
108 }
109 return (void*)(0x4000 + (phys & 0xFFF));
923ea2c2
JW
110}
111
9343e933 112int addmap(unsigned long vaddr, unsigned long paddr)
923ea2c2
JW
113{
114 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
115 unsigned long *pt;
116
117 if (!(pde & PTE_PRESENT))
118 return -1;
119
120 pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
121 pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
122
123 return 0;
124}
125
e8f20fe7
JW
126int addmap_4m(unsigned long vaddr, unsigned long paddr)
127{
128 /* PDE_PAGE_SIZE = (1 << 7) */
129 ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)] =
130 paddr | PDE_PRESENT | PDE_READ_WRITE | PDE_PAGE_SIZE;
131
132 return 0;
133}
134
9343e933
JW
135void *demap(unsigned long client_pd, unsigned long vaddr)
136{
137 unsigned long pde = ((unsigned long *)p2v(client_pd))[PDE_FOR(vaddr)];
138 unsigned long pte;
139
140 if (!(pde & PTE_PRESENT))
141 return (void*)0x0;
142 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(vaddr)];
143 if (!(pte & PTE_PRESENT))
144 return (void*)0x0;
145 return p2v((pte & ~0xFFF) + (vaddr & 0xFFF));
146}
147
148static void pt_setup(int tseg_start, int tseg_size) {
9e2a82e4 149 int i;
9e2a82e4 150
923ea2c2
JW
151 /* The page directory and page table live at TSEG and TSEG + 0x1000,
152 * respectively. */
153 unsigned long *pagedirectory = (unsigned long *) tseg_start;
154 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
9343e933
JW
155
156 pd = pagedirectory;
9e2a82e4
JP
157
158 /* Clear out the page directory except for one entry pointing to the
159 * page table, and clear the page table entirely. */
b908495a 160 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
9e2a82e4 161 for (i = 1; i < 1024; i++)
9e2a82e4 162 pagedirectory[i] = 0;
9e2a82e4 163
9e2a82e4 164 for (i = 0; i < 1024; i++)
9e2a82e4 165 pagetable[i] = 0;
9e2a82e4 166
923ea2c2
JW
167 /* Map 0x0A0000:0x0BFFFF to itself. */
168 for (i = 0; i < 32; i++)
9343e933 169 addmap(0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
9e2a82e4 170
9343e933 171 /* Map 0x200000:0x300000 to TSEG data */
923ea2c2 172 for (i = 0; i < 256; i++)
9343e933 173 addmap(0x200000 + i * 0x1000, tseg_start + (i + 2) * 0x1000);
9e2a82e4 174
923ea2c2 175 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
9e2a82e4 176 * RAM the first time around */
923ea2c2 177 for (i = 0; i < 256; i++)
9343e933 178 addmap(0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
9e2a82e4 179
9343e933
JW
180 /* Map 0x1F0000:0x1F2000 to TSEG paging info */
181 for (i = 0; i < 2; i++)
182 addmap(0x1F0000 + i * 0x1000, tseg_start + i * 0x1000);
9e2a82e4
JP
183}
184
b34003c2
JW
185void init_and_run(void)
186{
db9fad13
JAW
187 DBG(0x0A);
188
b34003c2
JW
189 if (!initialized)
190 {
db9fad13 191 DBG(0x0B);
b34003c2
JW
192 smi_init();
193 initialized = 1;
194 }
195
db9fad13 196 DBG(0x0C);
b34003c2 197 smi_entry();
db9fad13
JAW
198
199 DBG(0xCC);
b34003c2
JW
200}
201
9e2a82e4
JP
202void c_entry(void)
203{
923ea2c2 204 paging_enb = 0;
9e2a82e4 205
db9fad13
JAW
206 DBG(0x01);
207
923ea2c2 208 if (!initialized)
9343e933 209 pt_setup(0x1FF80000, 0x80000);
db9fad13
JAW
210
211 DBG(0x02);
212
113df320 213 /* Enable paging. */
923ea2c2 214 set_cr3((unsigned long)pd);
db9fad13 215 set_cr4(get_cr4() | CR4_PSE | CR4_OSFXSR); /* ITT, we 4MByte page. */
9e2a82e4 216 set_cr0(get_cr0() | CR0_PG);
db9fad13
JAW
217
218 DBG(0x03);
923ea2c2 219 paging_enb = 1;
ed22a699 220
923ea2c2
JW
221 /* If this is the first goround, copy in data. */
222 if (!initialized)
223 {
224 unsigned char *p;
db9fad13
JAW
225
226 DBG(0x04);
227
923ea2c2
JW
228 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
229 *p = *(p + 0x100000);
230 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
231 *p = 0;
db9fad13
JAW
232
233 DBG(0x05);
113df320 234
923ea2c2 235 /* Only now is it safe to call other functions. */
113df320 236 serial_init();
db9fad13 237 DBG(0x06);
923ea2c2 238 dolog("Evacuation to TSEG complete.");
db9fad13 239 DBG(0x07);
ed22a699
JP
240 }
241
db9fad13 242 DBG(0x08);
ed22a699 243 traps_install();
923ea2c2 244
db9fad13
JAW
245 DBG(0x09);
246 ps_switch_stack(init_and_run, 0xa2000);
ed22a699 247
db9fad13 248 DBG(0xFA);
9e2a82e4 249}
This page took 0.051055 seconds and 4 git commands to generate.