| 1 |
|
| 2 |
|
| 3 |
#include <cbfs.h> |
| 4 |
#include <commonlib/helpers.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <cpu/cpu.h> |
| 7 |
#include <cpu/x86/cr.h> |
| 8 |
#include <cpu/x86/msr.h> |
| 9 |
#include <cpu/x86/pae.h> |
| 10 |
#include <string.h> |
| 11 |
#include <symbols.h> |
| 12 |
#include <types.h> |
| 13 |
|
| 14 |
#define PDPTE_PRES (1ULL << 0) |
| 15 |
#define PDPTE_ADDR_MASK (~((1ULL << 12) - 1)) |
| 16 |
|
| 17 |
#define PDE_PRES (1ULL << 0) |
| 18 |
#define PDE_RW (1ULL << 1) |
| 19 |
#define PDE_US (1ULL << 2) |
| 20 |
#define PDE_PWT (1ULL << 3) |
| 21 |
#define PDE_PCD (1ULL << 4) |
| 22 |
#define PDE_A (1ULL << 5) |
| 23 |
#define PDE_D (1ULL << 6) // only valid with PS=1 |
| 24 |
#define PDE_PS (1ULL << 7) |
| 25 |
#define PDE_G (1ULL << 8) // only valid with PS=1 |
| 26 |
#define PDE_PAT (1ULL << 12) // only valid with PS=1 |
| 27 |
#define PDE_XD (1ULL << 63) |
| 28 |
#define PDE_ADDR_MASK (~((1ULL << 12) - 1)) |
| 29 |
|
| 30 |
#define PTE_PRES (1ULL << 0) |
| 31 |
#define PTE_RW (1ULL << 1) |
| 32 |
#define PTE_US (1ULL << 2) |
| 33 |
#define PTE_PWT (1ULL << 3) |
| 34 |
#define PTE_PCD (1ULL << 4) |
| 35 |
#define PTE_A (1ULL << 5) |
| 36 |
#define PTE_D (1ULL << 6) |
| 37 |
#define PTE_PAT (1ULL << 7) |
| 38 |
#define PTE_G (1ULL << 8) |
| 39 |
#define PTE_XD (1ULL << 63) |
| 40 |
|
| 41 |
#define PDPTE_IDX_SHIFT 30 |
| 42 |
#define PDPTE_IDX_MASK 0x3 |
| 43 |
|
| 44 |
#define PDE_IDX_SHIFT 21 |
| 45 |
#define PDE_IDX_MASK 0x1ff |
| 46 |
|
| 47 |
#define PTE_IDX_SHIFT 12 |
| 48 |
#define PTE_IDX_MASK 0x1ff |
| 49 |
|
| 50 |
#define OVERLAP(a, b, s, e) ((b) > (s) && (a) < (e)) |
| 51 |
|
| 52 |
static const size_t s2MiB = 2 * MiB; |
| 53 |
static const size_t s4KiB = 4 * KiB; |
| 54 |
|
| 55 |
struct pde { |
| 56 |
uint32_t addr_lo; |
| 57 |
uint32_t addr_hi; |
| 58 |
} __packed; |
| 59 |
struct pg_table { |
| 60 |
struct pde pd[2048]; |
| 61 |
struct pde pdp[512]; |
| 62 |
} __packed; |
| 63 |
|
| 64 |
void paging_enable_pae_cr3(uintptr_t cr3) |
| 65 |
{ |
| 66 |
|
| 67 |
write_cr3(cr3); |
| 68 |
paging_enable_pae(); |
| 69 |
} |
| 70 |
|
| 71 |
void paging_enable_pae(void) |
| 72 |
{ |
| 73 |
CRx_TYPE cr0; |
| 74 |
CRx_TYPE cr4; |
| 75 |
|
| 76 |
|
| 77 |
cr4 = read_cr4(); |
| 78 |
cr4 |= CR4_PAE; |
| 79 |
write_cr4(cr4); |
| 80 |
|
| 81 |
|
| 82 |
cr0 = read_cr0(); |
| 83 |
cr0 |= CR0_PG; |
| 84 |
write_cr0(cr0); |
| 85 |
} |
| 86 |
|
| 87 |
void paging_disable_pae(void) |
| 88 |
{ |
| 89 |
CRx_TYPE cr0; |
| 90 |
CRx_TYPE cr4; |
| 91 |
|
| 92 |
|
| 93 |
cr0 = read_cr0(); |
| 94 |
cr0 &= ~(CRx_TYPE)CR0_PG; |
| 95 |
write_cr0(cr0); |
| 96 |
|
| 97 |
|
| 98 |
cr4 = read_cr4(); |
| 99 |
cr4 &= ~(CRx_TYPE)CR4_PAE; |
| 100 |
write_cr4(cr4); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int init_pae_pagetables(void *pgtbl) |
| 116 |
{ |
| 117 |
struct pg_table *pgtbl_buf = (struct pg_table *)pgtbl; |
| 118 |
struct pde *pd = pgtbl_buf->pd, *pdp = pgtbl_buf->pdp; |
| 119 |
|
| 120 |
printk(BIOS_DEBUG, "%s: Using address %p for page tables\n", |
| 121 |
__func__, pgtbl_buf); |
| 122 |
|
| 123 |
|
| 124 |
if (!IS_ALIGNED((uintptr_t)pgtbl_buf, s4KiB)) { |
| 125 |
printk(BIOS_ERR, "%s: Invalid alignment\n", __func__); |
| 126 |
return 1; |
| 127 |
} |
| 128 |
|
| 129 |
paging_disable_pae(); |
| 130 |
|
| 131 |
|
| 132 |
memset(pgtbl_buf->pdp, 0, sizeof(pgtbl_buf->pdp)); |
| 133 |
|
| 134 |
pdp[0].addr_lo = ((uintptr_t)&pd[512*0]) | PDPTE_PRES; |
| 135 |
pdp[1].addr_lo = ((uintptr_t)&pd[512*1]) | PDPTE_PRES; |
| 136 |
pdp[2].addr_lo = ((uintptr_t)&pd[512*2]) | PDPTE_PRES; |
| 137 |
pdp[3].addr_lo = ((uintptr_t)&pd[512*3]) | PDPTE_PRES; |
| 138 |
|
| 139 |
|
| 140 |
for (size_t i = 0; i < 2048; i++) { |
| 141 |
pd[i].addr_lo = (i << PDE_IDX_SHIFT) | PDE_PS | PDE_PRES | PDE_RW; |
| 142 |
pd[i].addr_hi = 0; |
| 143 |
} |
| 144 |
|
| 145 |
paging_enable_pae_cr3((uintptr_t)pdp); |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void pae_map_2M_page(void *pgtbl, uint64_t paddr, void *vmem_addr) |
| 169 |
{ |
| 170 |
struct pg_table *pgtbl_buf = (struct pg_table *)pgtbl; |
| 171 |
struct pde *pd; |
| 172 |
|
| 173 |
if (!IS_ALIGNED(paddr, s2MiB)) { |
| 174 |
printk(BIOS_WARNING, "%s: Aligning physical address to 2MiB\n", |
| 175 |
__func__); |
| 176 |
paddr = ALIGN_DOWN(paddr, s2MiB); |
| 177 |
} |
| 178 |
|
| 179 |
if (!IS_ALIGNED((uintptr_t)vmem_addr, s2MiB)) { |
| 180 |
printk(BIOS_WARNING, "%s: Aligning virtual address to 2MiB\n", |
| 181 |
__func__); |
| 182 |
vmem_addr = (void *)ALIGN_DOWN((uintptr_t)vmem_addr, s2MiB); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
pd = &pgtbl_buf->pd[((uintptr_t)vmem_addr) >> PDE_IDX_SHIFT]; |
| 187 |
pd->addr_lo = paddr | PDE_PS | PDE_PRES | PDE_RW; |
| 188 |
pd->addr_hi = paddr >> 32; |
| 189 |
|
| 190 |
|
| 191 |
asm volatile ("invlpg (%0)" :: "b"(vmem_addr) : "memory"); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
int memset_pae(uint64_t dest, unsigned char pat, uint64_t length, void *pgtbl, |
| 222 |
void *vmem_addr) |
| 223 |
{ |
| 224 |
ssize_t offset; |
| 225 |
const uintptr_t pgtbl_s = (uintptr_t)pgtbl; |
| 226 |
const uintptr_t pgtbl_e = pgtbl_s + sizeof(struct pg_table); |
| 227 |
|
| 228 |
printk(BIOS_DEBUG, "%s: Using virtual address %p as scratchpad\n", |
| 229 |
__func__, vmem_addr); |
| 230 |
|
| 231 |
|
| 232 |
if (!IS_ALIGNED((uintptr_t)vmem_addr, s2MiB)) { |
| 233 |
printk(BIOS_ERR, "%s: Invalid alignment\n", __func__); |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
if (OVERLAP(dest, dest + length, pgtbl_s, pgtbl_e)) { |
| 238 |
printk(BIOS_ERR, "%s: destination overlaps page tables\n", |
| 239 |
__func__); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
|
| 243 |
if (OVERLAP((uintptr_t)vmem_addr, (uintptr_t)vmem_addr + s2MiB, |
| 244 |
pgtbl_s, pgtbl_e)) { |
| 245 |
printk(BIOS_ERR, "%s: vmem address overlaps page tables\n", |
| 246 |
__func__); |
| 247 |
return 1; |
| 248 |
} |
| 249 |
|
| 250 |
if (init_pae_pagetables(pgtbl)) |
| 251 |
return 1; |
| 252 |
|
| 253 |
offset = dest - ALIGN_DOWN(dest, s2MiB); |
| 254 |
dest = ALIGN_DOWN(dest, s2MiB); |
| 255 |
|
| 256 |
do { |
| 257 |
const size_t len = MIN(length, s2MiB - offset); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
pae_map_2M_page(pgtbl, dest, vmem_addr); |
| 264 |
|
| 265 |
printk(BIOS_SPEW, "%s: Clearing %llx[%lx] - %zx\n", __func__, |
| 266 |
dest + offset, (uintptr_t)vmem_addr + offset, len); |
| 267 |
|
| 268 |
memset(vmem_addr + offset, pat, len); |
| 269 |
|
| 270 |
dest += s2MiB; |
| 271 |
length -= len; |
| 272 |
offset = 0; |
| 273 |
} while (length > 0); |
| 274 |
|
| 275 |
paging_disable_pae(); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
void paging_set_nxe(int enable) |
| 281 |
{ |
| 282 |
msr_t msr = rdmsr(IA32_EFER); |
| 283 |
|
| 284 |
if (enable) |
| 285 |
msr.lo |= EFER_NXE; |
| 286 |
else |
| 287 |
msr.lo &= ~EFER_NXE; |
| 288 |
|
| 289 |
wrmsr(IA32_EFER, msr); |
| 290 |
} |
| 291 |
|
| 292 |
void paging_set_pat(uint64_t pat) |
| 293 |
{ |
| 294 |
msr_t msr; |
| 295 |
msr.lo = pat; |
| 296 |
msr.hi = pat >> 32; |
| 297 |
wrmsr(IA32_PAT, msr); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
void paging_set_default_pat(void) |
| 317 |
{ |
| 318 |
uint64_t pat = PAT_ENCODE(WB, 0) | PAT_ENCODE(WC, 1) | |
| 319 |
PAT_ENCODE(UC_MINUS, 2) | PAT_ENCODE(UC, 3) | |
| 320 |
PAT_ENCODE(WB, 4) | PAT_ENCODE(WP, 5) | |
| 321 |
PAT_ENCODE(UC_MINUS, 6) | PAT_ENCODE(WT, 7); |
| 322 |
paging_set_pat(pat); |
| 323 |
} |
| 324 |
|
| 325 |
int paging_enable_for_car(const char *pdpt_name, const char *pt_name) |
| 326 |
{ |
| 327 |
if (!preram_symbols_available()) |
| 328 |
return -1; |
| 329 |
|
| 330 |
if (!cbfs_load(pdpt_name, _pdpt, REGION_SIZE(pdpt))) { |
| 331 |
printk(BIOS_ERR, "Couldn't load pdpt\n"); |
| 332 |
return -1; |
| 333 |
} |
| 334 |
|
| 335 |
if (!cbfs_load(pt_name, _pagetables, REGION_SIZE(pagetables))) { |
| 336 |
printk(BIOS_ERR, "Couldn't load page tables\n"); |
| 337 |
return -1; |
| 338 |
} |
| 339 |
|
| 340 |
paging_enable_pae_cr3((uintptr_t)_pdpt); |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |