2 * Paging enable routines
3 * NetWatch system management mode administration console
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.
14 #include <video_defines.h>
17 #include <pci-bother.h>
21 #include "../net/net.h"
22 #include "vga-overlay.h"
24 extern void smi_init();
27 extern void smi_entry();
28 void set_cr0(unsigned int);
29 void ps_switch_stack (void (*call)(), int stack);
33 register unsigned int _temp__; \
34 asm volatile("mov %%cr0, %0" : "=r" (_temp__)); \
37 #define set_cr3(value) \
39 register unsigned int _temp__ = (value); \
40 asm volatile("mov %0, %%cr3" : : "r" (_temp__)); \
45 register unsigned int _temp__; \
46 asm volatile("mov %%cr4, %0" : "=r" (_temp__)); \
49 #define set_cr4(value) \
51 register unsigned int _temp__ = (value); \
52 asm volatile("mov %0, %%cr4" : : "r" (_temp__)); \
55 #define CR0_PG 0x80000000
56 #define CR4_PSE 0x00000010
58 #define MAP_FLAGS (PTE_PRESENT | PTE_READ_WRITE)
60 static int initialized = 0;
61 static int paging_enb = 0;
62 static unsigned long *pd;
64 extern int _bss, _bssend, _end;
66 static unsigned long curmapped = 0xFFFFFFFF;
68 unsigned long v2p(void *virt)
70 unsigned long _virt = (unsigned long)virt;
75 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(_virt)];
78 if (!(pde & PTE_PRESENT))
81 if (pde & PDE_PAGE_SIZE)
82 return ADDR_12_MASK(pde) + (_virt & 0x3FFFFF);
84 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(_virt)];
85 if (!(pte & PTE_PRESENT))
87 return (pte & ~0xFFF) + (_virt & 0xFFF);
90 void *p2v(unsigned long phys)
95 if (phys >= 0xA0000 && phys < 0xC0000)
97 if (phys >= 0x1FF82000 && phys < 0x20000000)
98 return (void*)(phys - 0x1FF82000 + 0x200000);
99 if (phys >= 0x1FF80000 && phys < 0x1FF82000)
100 return (void*)(phys - 0x1FF80000 + 0x1F0000);
102 if ((phys & ~0xFFF) != curmapped) /* If it's not mapped, map it in. */
104 curmapped = phys & ~0xFFF;
105 addmap(0x4000, curmapped);
106 asm volatile("invlpg 0x4000");
108 return (void*)(0x4000 + (phys & 0xFFF));
111 int addmap(unsigned long vaddr, unsigned long paddr)
113 unsigned long pde = ((unsigned long *)p2v((unsigned long)pd))[PDE_FOR(vaddr)];
116 if (!(pde & PTE_PRESENT))
119 pt = (unsigned long *)p2v(ADDR_12_MASK(pde));
120 pt[PTE_FOR(vaddr)] = paddr | PTE_PRESENT | PTE_READ_WRITE;
125 int addmap_4m(unsigned long vaddr, unsigned long paddr)
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;
134 void *demap(unsigned long client_pd, unsigned long vaddr)
136 unsigned long pde = ((unsigned long *)p2v(client_pd))[PDE_FOR(vaddr)];
139 if (!(pde & PTE_PRESENT))
141 pte = ((unsigned long *)p2v(ADDR_12_MASK(pde)))[PTE_FOR(vaddr)];
142 if (!(pte & PTE_PRESENT))
144 return p2v((pte & ~0xFFF) + (vaddr & 0xFFF));
147 static void pt_setup(int tseg_start, int tseg_size) {
150 /* The page directory and page table live at TSEG and TSEG + 0x1000,
152 unsigned long *pagedirectory = (unsigned long *) tseg_start;
153 unsigned long *pagetable = (unsigned long *) (tseg_start + 0x1000);
157 /* Clear out the page directory except for one entry pointing to the
158 * page table, and clear the page table entirely. */
159 pagedirectory[0] = (tseg_start + 0x1000) | PTE_PRESENT | PTE_READ_WRITE;
160 for (i = 1; i < 1024; i++)
161 pagedirectory[i] = 0;
163 for (i = 0; i < 1024; i++)
166 /* Map 0x0A0000:0x0BFFFF to itself. */
167 for (i = 0; i < 32; i++)
168 addmap(0xA0000 + i * 0x1000, 0xA0000 + i * 0x1000);
170 /* Map 0x200000:0x300000 to TSEG data */
171 for (i = 0; i < 256; i++)
172 addmap(0x200000 + i * 0x1000, tseg_start + (i + 2) * 0x1000);
174 /* Map 0x300000:0x400000 to 0x200000, so we can copy our code out of
175 * RAM the first time around */
176 for (i = 0; i < 256; i++)
177 addmap(0x300000 + i * 0x1000, 0x200000 + i * 0x1000);
179 /* Map 0x1F0000:0x1F2000 to TSEG paging info */
180 for (i = 0; i < 2; i++)
181 addmap(0x1F0000 + i * 0x1000, tseg_start + i * 0x1000);
184 void init_and_run(void)
201 pt_setup(0x1FF80000, 0x80000);
205 set_cr3((unsigned long)pd);
206 set_cr4(get_cr4() | CR4_PSE); /* ITT, we 4MByte page. */
207 set_cr0(get_cr0() | CR0_PG);
211 /* If this is the first goround, copy in data. */
217 for (p = (void *)0x200000; (void *)p < (void *)&_bss; p++)
218 *p = *(p + 0x100000);
219 for (p = (void *)&_bss; (void *)p < (void *)&_bssend; p++)
223 /* Only now is it safe to call other functions. */
225 dolog("Evacuation to TSEG complete.");
234 ps_switch_stack(init_and_run, 0x270000);