From 577c0f6226fde54a0018a8ebe7ff0ddbd0e16207 Mon Sep 17 00:00:00 2001 From: tkchia Date: Sun, 11 Sep 2022 14:56:54 +0000 Subject: [PATCH] Bare metal: fix some issues in initial page table creation hello4.com now works outside of an emulator. --- libc/intrin/mman.greg.c | 10 ++++++++-- libc/runtime/mman.internal.h | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/libc/intrin/mman.greg.c b/libc/intrin/mman.greg.c index fa0623e50..cf872aa22 100644 --- a/libc/intrin/mman.greg.c +++ b/libc/intrin/mman.greg.c @@ -74,6 +74,10 @@ noasan textreal uint64_t *__get_virtual(struct mman *mm, uint64_t *t, if (!(*e & PAGE_V)) { if (!maketables) return NULL; if (!(p = __new_page(mm))) return NULL; + if (mm->use_bane) + __clear_page(BANE + p); + else + __clear_page(p); *e = p | PAGE_V | PAGE_RW; } t = (uint64_t *)(BANE + (*e & PAGE_TA)); @@ -117,16 +121,18 @@ static noasan textreal void __invert_memory(struct mman *mm, uint64_t *pml4t) { uint64_t i, j, *m, p, pe; for (i = 0; i < mm->e820n; ++i) { for (p = mm->e820[i].addr, pe = mm->e820[i].addr + mm->e820[i].size; - p + 0x200000 < pe; p += 4096) { + p != pe + 0x200000; p += 4096) { m = __get_virtual(mm, pml4t, BANE + p, true); if (m && !(*m & PAGE_V)) { *m = p | PAGE_V | PAGE_RW; } } + mm->use_bane = 1; } } noasan textreal void __setup_mman(struct mman *mm, uint64_t *pml4t) { + mm->use_bane = 0; __normalize_e820(mm); __invert_memory(mm, pml4t); } @@ -150,7 +156,7 @@ noasan textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b) { v = b + p->p_offset + i; m = MAX(m, v); } else { - v = __clear_page(__new_page(mm)); + v = __clear_page(BANE + __new_page(mm)); } *__get_virtual(mm, pml4t, p->p_vaddr + i, true) = (v & PAGE_TA) | f; } diff --git a/libc/runtime/mman.internal.h b/libc/runtime/mman.internal.h index 39d3acdcc..3b3a54777 100644 --- a/libc/runtime/mman.internal.h +++ b/libc/runtime/mman.internal.h @@ -7,7 +7,7 @@ COSMOPOLITAN_C_START_ struct mman { int64_t pdp; /* 0x0500 */ int32_t pdpi; /* 0x0508 */ - int32_t e820n; /* 0x050a */ + int32_t e820n; /* 0x050c */ struct SmapEntry e820[256]; /* 0x0510 */ char pc_drive_base_table[11]; /* 0x1d10 */ unsigned char pc_drive_type; /* 0x1d1b */ @@ -17,6 +17,7 @@ struct mman { unsigned char pc_drive_last_head; /* 0x1d20 */ unsigned char pc_drive; /* 0x1d21 */ char bad_idt[6]; /* 0x1d22 */ + unsigned use_bane : 1, : 31; /* 0x1d28 */ }; COSMOPOLITAN_C_END_