]> Joshua Wise's Git repositories - netwatch.git/blame - netwatch/pagingstub.c
First pass of 410watch UI code.
[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 135static void pt_setup(int tseg_start, int tseg_size) {
9e2a82e4 136 int i;
9e2a82e4 137
923ea2c2
JW
138 /* The page directory and page table live at TSEG and TSEG + 0x1000,
139 * respectively. */
140 unsigned long *pagedirectory = (unsigned long *) tseg_start;
141 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
9343e933
JW
142
143 pd = pagedirectory;
9e2a82e4
JP
144
145 /* Clear out the page directory except for one entry pointing to the
146 * page table, and clear the page table entirely. */
b908495a 147 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
9e2a82e4 148 for (i = 1; i < 1024; i++)
9e2a82e4 149 pagedirectory[i] = 0;
9e2a82e4 150
9e2a82e4 151 for (i = 0; i < 1024; i++)
9e2a82e4 152 pagetable[i] = 0;
9e2a82e4 153
923ea2c2
JW
154 /* Map 0x0A0000:0x0BFFFF to itself. */
155 for (i = 0; i < 32; i++)
9343e933 156 addmap(0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
9e2a82e4 157
9343e933 158 /* Map 0x200000:0x300000 to TSEG data */
923ea2c2 159 for (i = 0; i < 256; i++)
9343e933 160 addmap(0x200000 + i * 0x1000, tseg_start + (i + 2) * 0x1000);
9e2a82e4 161
923ea2c2 162 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
9e2a82e4 163 * RAM the first time around */
923ea2c2 164 for (i = 0; i < 256; i++)
9343e933 165 addmap(0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
9e2a82e4 166
9343e933
JW
167 /* Map 0x1F0000:0x1F2000 to TSEG paging info */
168 for (i = 0; i < 2; i++)
169 addmap(0x1F0000 + i * 0x1000, tseg_start + i * 0x1000);
9e2a82e4
JP
170}
171
b34003c2
JW
172void init_and_run(void)
173{
db9fad13
JAW
174 DBG(0x0A);
175
b34003c2
JW
176 if (!initialized)
177 {
db9fad13 178 DBG(0x0B);
b34003c2
JW
179 smi_init();
180 initialized = 1;
181 }
182
db9fad13 183 DBG(0x0C);
b34003c2 184 smi_entry();
db9fad13
JAW
185
186 DBG(0xCC);
b34003c2
JW
187}
188
9e2a82e4
JP
189void c_entry(void)
190{
923ea2c2 191 paging_enb = 0;
9e2a82e4 192
db9fad13
JAW
193 DBG(0x01);
194
923ea2c2 195 if (!initialized)
9343e933 196 pt_setup(0x1FF80000, 0x80000);
db9fad13
JAW
197
198 DBG(0x02);
199
113df320 200 /* Enable paging. */
923ea2c2 201 set_cr3((unsigned long)pd);
db9fad13 202 set_cr4(get_cr4() | CR4_PSE | CR4_OSFXSR); /* ITT, we 4MByte page. */
9e2a82e4 203 set_cr0(get_cr0() | CR0_PG);
db9fad13
JAW
204
205 DBG(0x03);
923ea2c2 206 paging_enb = 1;
ed22a699 207
923ea2c2
JW
208 /* If this is the first goround, copy in data. */
209 if (!initialized)
210 {
211 unsigned char *p;
db9fad13
JAW
212
213 DBG(0x04);
214
923ea2c2
JW
215 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
216 *p = *(p + 0x100000);
217 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
218 *p = 0;
db9fad13
JAW
219
220 DBG(0x05);
113df320 221
923ea2c2 222 /* Only now is it safe to call other functions. */
113df320 223 serial_init();
db9fad13 224 DBG(0x06);
923ea2c2 225 dolog("Evacuation to TSEG complete.");
db9fad13 226 DBG(0x07);
ed22a699
JP
227 }
228
db9fad13 229 DBG(0x08);
ed22a699 230 traps_install();
923ea2c2 231
db9fad13 232 DBG(0x09);
d5d37379 233 ps_switch_stack(init_and_run, 0x270000);
ed22a699 234
db9fad13 235 DBG(0xFA);
9e2a82e4 236}
This page took 0.053338 seconds and 4 git commands to generate.