mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +00:00
[metal] Allow programs larger than 440 KiB to run in bare metal mode (#685)
* [metal] Copy program pages to extended memory at startup * [metal] Reclaim base memory pages for later app use * [metal] Load program pages beyond 1st 440 KiB to extended memory o//examples/hellolua.com now runs correctly under QEMU (in legacy BIOS mode). * [metal] Place GDT in read/write segment The CPU absolutely needs to alter the GDT when loading the task register (via ltr). To account for this, I move the GDT into a read/write data section. There is still a "rump" read-only GDT in the text section that is used by the real mode bootloader. We also delay the loading of the task register (ltr) until after the IDT and TSS are finally set up. * [metal] Get examples/vga2.c serial output working for UEFI boot * [metal] Get examples/vga2.c VGA output working for UEFI boot * [metal] Allow munmap() to reclaim dynamically allocated pages * Place TLS sections right after .text, not after embedded zip file Co-authored-by: tkchia <tkchia-cosmo@gmx.com>
This commit is contained in:
parent
120079b0a6
commit
0da47c51de
16 changed files with 725 additions and 191 deletions
|
@ -44,7 +44,7 @@ noasan struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot,
|
|||
size_t i;
|
||||
struct mman *mm;
|
||||
struct DirectMap res;
|
||||
uint64_t addr, faddr = 0, page, *pte, *fdpte, *pml4t;
|
||||
uint64_t addr, faddr = 0, page, e, *pte, *fdpte, *pml4t;
|
||||
mm = (struct mman *)(BANE + 0x0500);
|
||||
pml4t = __get_pml4t();
|
||||
size = ROUNDUP(size, 4096);
|
||||
|
@ -78,27 +78,27 @@ noasan struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot,
|
|||
sys_mmap_metal_break = MAX(addr + size, sys_mmap_metal_break);
|
||||
}
|
||||
for (i = 0; i < size; i += 4096) {
|
||||
page = __new_page(mm);
|
||||
pte = __get_virtual(mm, pml4t, addr + i, true);
|
||||
if (pte) {
|
||||
if ((flags & MAP_ANONYMOUS_linux)) {
|
||||
page = __new_page(mm);
|
||||
if (!page) return bad_mmap();
|
||||
__clear_page(BANE + page);
|
||||
page |= PAGE_RSRV | PAGE_U;
|
||||
e = page | PAGE_RSRV | PAGE_U;
|
||||
if ((prot & PROT_WRITE))
|
||||
page |= PAGE_V | PAGE_RW;
|
||||
e |= PAGE_V | PAGE_RW;
|
||||
else if ((prot & (PROT_READ | PROT_EXEC)))
|
||||
page |= PAGE_V;
|
||||
if (!(prot & PROT_EXEC)) page |= PAGE_XD;
|
||||
e |= PAGE_V;
|
||||
if (!(prot & PROT_EXEC)) e |= PAGE_XD;
|
||||
} else {
|
||||
fdpte = __get_virtual(mm, pml4t, faddr + i, false);
|
||||
page = *fdpte;
|
||||
page |= PAGE_RSRV | PAGE_U;
|
||||
if (!(prot & PROT_WRITE)) page &= ~PAGE_RW;
|
||||
if (!(prot & PROT_EXEC)) page |= PAGE_XD;
|
||||
e = *fdpte | PAGE_RSRV | PAGE_U;
|
||||
page = e & PAGE_TA;
|
||||
if (!(prot & PROT_WRITE)) e &= ~PAGE_RW;
|
||||
if (!(prot & PROT_EXEC)) e |= PAGE_XD;
|
||||
}
|
||||
*pte = page;
|
||||
__ref_page(mm, pml4t, page);
|
||||
*pte = e;
|
||||
invlpg(addr + i);
|
||||
} else {
|
||||
addr = -1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue