From e47c0cc9294b7659e62ae05115a403458d93ab50 Mon Sep 17 00:00:00 2001 From: tkchia Date: Mon, 12 Jun 2023 01:32:39 +0800 Subject: [PATCH] [metal] Clean up code for getting pointer to `struct mman` (#828) Co-authored-by: tkchia --- libc/calls/munmap-metal.c | 3 +-- libc/intrin/directmap-metal.c | 2 +- libc/runtime/efimain.greg.c | 2 +- libc/runtime/pc.internal.h | 3 +++ libc/vga/vga-init.greg.c | 4 ++-- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libc/calls/munmap-metal.c b/libc/calls/munmap-metal.c index 3b089e651..b4d69e11a 100644 --- a/libc/calls/munmap-metal.c +++ b/libc/calls/munmap-metal.c @@ -23,9 +23,8 @@ noasan int sys_munmap_metal(void *addr, size_t size) { size_t i; uint64_t *e, paddr; - struct mman *mm; + struct mman *mm = __get_mm(); uint64_t *pml4t = __get_pml4t(); - mm = (struct mman *)(BANE + 0x0500); for (i = 0; i < size; i += 4096) { e = __get_virtual(mm, pml4t, (uint64_t)addr + i, false); if (e) { diff --git a/libc/intrin/directmap-metal.c b/libc/intrin/directmap-metal.c index 116176675..d6b82395c 100644 --- a/libc/intrin/directmap-metal.c +++ b/libc/intrin/directmap-metal.c @@ -46,7 +46,7 @@ noasan struct DirectMap sys_mmap_metal(void *vaddr, size_t size, int prot, struct mman *mm; struct DirectMap res; uint64_t addr, faddr = 0, page, e, *pte, *fdpte, *pml4t; - mm = (struct mman *)(BANE + 0x0500); + mm = __get_mm(); pml4t = __get_pml4t(); size = ROUNDUP(size, 4096); addr = (uint64_t)vaddr; diff --git a/libc/runtime/efimain.greg.c b/libc/runtime/efimain.greg.c index d04645221..777f6eca8 100644 --- a/libc/runtime/efimain.greg.c +++ b/libc/runtime/efimain.greg.c @@ -176,7 +176,7 @@ __msabi noasan EFI_STATUS EfiMain(EFI_HANDLE ImageHandle, SystemTable->BootServices->AllocatePages( AllocateAddress, EfiRuntimeServicesData, ((_end - __executable_start) + 4095) / 4096, &Address); - mm = (struct mman *)0x0500; + mm = __get_mm_phy(); SystemTable->BootServices->SetMem(mm, sizeof(*mm), 0); SystemTable->BootServices->SetMem( (void *)0x79000, 0x7e000 - 0x79000 + sizeof(struct EfiArgs), 0); diff --git a/libc/runtime/pc.internal.h b/libc/runtime/pc.internal.h index cc55cbd39..6f815fd11 100644 --- a/libc/runtime/pc.internal.h +++ b/libc/runtime/pc.internal.h @@ -225,5 +225,8 @@ forceinline void outb(unsigned short port, unsigned char byte) { (uint64_t *)(BANE + cr3); \ }) +#define __get_mm() ((struct mman *)(BANE + 0x0500)) +#define __get_mm_phy() ((struct mman *)0x0500) + #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_RUNTIME_PC_H_ */ diff --git a/libc/vga/vga-init.greg.c b/libc/vga/vga-init.greg.c index a4d4de62d..bdfa3ae7a 100644 --- a/libc/vga/vga-init.greg.c +++ b/libc/vga/vga-init.greg.c @@ -35,7 +35,7 @@ struct Tty _vga_tty; void _vga_reinit(struct Tty *tty, unsigned short starty, unsigned short startx, unsigned init_flags) { - struct mman *mm = (struct mman *)(BANE + 0x0500); + struct mman *mm = __get_mm(); unsigned char vid_type = mm->pc_video_type; unsigned short height = mm->pc_video_height, width = mm->pc_video_width, stride = mm->pc_video_stride; @@ -66,7 +66,7 @@ void _vga_reinit(struct Tty *tty, unsigned short starty, unsigned short startx, textstartup void _vga_init(void) { if (IsMetal()) { - struct mman *mm = (struct mman *)(BANE + 0x0500); + struct mman *mm = __get_mm(); unsigned short starty = mm->pc_video_curs_info.y, startx = mm->pc_video_curs_info.x; _vga_reinit(&_vga_tty, starty, startx, 0);