diff --git a/libc/calls/madvise.c b/libc/calls/madvise.c index f1b2da64e..885e727f3 100644 --- a/libc/calls/madvise.c +++ b/libc/calls/madvise.c @@ -27,7 +27,7 @@ static int __madvise(void *addr, size_t length, int advice) { // simulate linux behavior of validating alignment - if ((uintptr_t)addr & (getpagesize() - 1)) + if ((uintptr_t)addr & (__pagesize - 1)) return einval(); // simulate linux behavior of checking for negative length diff --git a/libc/dlopen/dlopen.c b/libc/dlopen/dlopen.c index 41e0111fa..a39921b54 100644 --- a/libc/dlopen/dlopen.c +++ b/libc/dlopen/dlopen.c @@ -303,7 +303,7 @@ static wontreturn dontinstrument void foreign_helper(void **p) { static dontinline void elf_exec(const char *file, char **envp) { // get microprocessor page size - long pagesz = getpagesize(); + long pagesz = __pagesize; // load helper executable into address space struct Loaded prog; diff --git a/libc/intrin/BUILD.mk b/libc/intrin/BUILD.mk index 47f52776c..300e0631f 100644 --- a/libc/intrin/BUILD.mk +++ b/libc/intrin/BUILD.mk @@ -144,6 +144,8 @@ o/$(MODE)/libc/intrin/sched_yield.o: libc/intrin/sched_yield.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< o/$(MODE)/libc/intrin/dsohandle.o: libc/intrin/dsohandle.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< +o/$(MODE)/libc/intrin/getpagesize_freebsd.o: libc/intrin/getpagesize_freebsd.S + @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< LIBC_INTRIN_LIBS = $(foreach x,$(LIBC_INTRIN_ARTIFACTS),$($(x))) LIBC_INTRIN_HDRS = $(foreach x,$(LIBC_INTRIN_ARTIFACTS),$($(x)_HDRS)) diff --git a/libc/intrin/extend.c b/libc/intrin/extend.c index 1452d026b..f4bf9383b 100644 --- a/libc/intrin/extend.c +++ b/libc/intrin/extend.c @@ -22,7 +22,7 @@ #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" -#define G getgransize() +#define G __gransize /** * Extends static allocation. diff --git a/libc/intrin/getauxval.c b/libc/intrin/getauxval.c index 3cbeeb560..54bc4a7e5 100644 --- a/libc/intrin/getauxval.c +++ b/libc/intrin/getauxval.c @@ -16,13 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/getauxval.internal.h" -#include "libc/nt/struct/systeminfo.h" -#include "libc/nt/systeminfo.h" #include "libc/runtime/runtime.h" -#include "libc/sysv/consts/auxv.h" /** * Returns auxiliary value. @@ -36,22 +32,6 @@ unsigned long getauxval(unsigned long key) { struct AuxiliaryValue x; x = __getauxval(key); - if (key == AT_PAGESZ) { - if (!x.isfound) { - if (!IsWindows()) { -#ifdef __aarch64__ - x.value = 16384; -#else - x.value = 4096; -#endif - } else { - struct NtSystemInfo si; - GetSystemInfo(&si); - x.value = si.dwPageSize; - } - } - x.isfound = true; - } if (x.isfound) { return x.value; } else { diff --git a/libc/intrin/getgransize.c b/libc/intrin/getgransize.c index 805191a01..0b8e30418 100644 --- a/libc/intrin/getgransize.c +++ b/libc/intrin/getgransize.c @@ -16,22 +16,11 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/nt/struct/systeminfo.h" -#include "libc/nt/systeminfo.h" #include "libc/runtime/runtime.h" -#include "libc/sysv/consts/auxv.h" +/** + * Returns granularity of mmap() allocations. + */ int getgransize(void) { - static int res; - if (!res) { - if (!IsWindows()) { - res = getpagesize(); - } else { - struct NtSystemInfo si; - GetSystemInfo(&si); - res = si.dwAllocationGranularity; - } - } - return res; + return __gransize; } diff --git a/libc/intrin/getmainstack.c b/libc/intrin/getmainstack.c index e619949c1..3638898d9 100644 --- a/libc/intrin/getmainstack.c +++ b/libc/intrin/getmainstack.c @@ -105,7 +105,7 @@ static size_t __get_stack_size(int pagesz, uintptr_t start, uintptr_t top) { * This function works on every OS except Windows. */ struct AddrSize __get_main_stack(void) { - int pagesz = getpagesize(); + int pagesz = __pagesize; uintptr_t start = (uintptr_t)__argv; uintptr_t top = __get_main_top(pagesz); uintptr_t bot = top - __get_stack_size(pagesz, start, top); diff --git a/libc/intrin/getpagesize.c b/libc/intrin/getpagesize.c index 9f4149758..acfe9f298 100644 --- a/libc/intrin/getpagesize.c +++ b/libc/intrin/getpagesize.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ Copyright 2024 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -17,12 +17,11 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/runtime/runtime.h" -#include "libc/sysv/consts/auxv.h" /** - * Returns granularity of memory manager. + * Returns system page size. * @see sysconf(_SC_PAGE_SIZE) which is portable */ int getpagesize(void) { - return getauxval(AT_PAGESZ); + return __pagesize; } diff --git a/libc/intrin/maps.c b/libc/intrin/maps.c index 5ca9861ee..e7d913206 100644 --- a/libc/intrin/maps.c +++ b/libc/intrin/maps.c @@ -59,7 +59,7 @@ void __maps_stack(char *stackaddr, int pagesz, int guardsize, size_t stacksize, } void __maps_init(void) { - int pagesz = getpagesize(); + int pagesz = __pagesize; // record _start() stack mapping if (!IsWindows()) { diff --git a/libc/intrin/mmap.c b/libc/intrin/mmap.c index 25b2ead83..b54d7f900 100644 --- a/libc/intrin/mmap.c +++ b/libc/intrin/mmap.c @@ -100,7 +100,7 @@ void __maps_check(void) { #if MMDEBUG size_t maps = 0; size_t pages = 0; - int pagesz = getpagesize(); + int pagesz = __pagesize; static unsigned mono; unsigned id = ++mono; for (struct Map *map = __maps_first(); map; map = __maps_next(map)) { @@ -259,13 +259,13 @@ static void __maps_insert(struct Map *map) { if (prot == other->prot && flags == other->flags) { if (!coalesced) { if (map->addr == other->addr + other->size) { - __maps.pages += (map->size + getpagesize() - 1) / getpagesize(); + __maps.pages += (map->size + __pagesize - 1) / __pagesize; other->size += map->size; __maps_free(map); __maps_check(); coalesced = true; } else if (map->addr + map->size == other->addr) { - __maps.pages += (map->size + getpagesize() - 1) / getpagesize(); + __maps.pages += (map->size + __pagesize - 1) / __pagesize; other->addr -= map->size; other->size += map->size; __maps_free(map); @@ -288,7 +288,7 @@ static void __maps_insert(struct Map *map) { } // otherwise insert new mapping - __maps.pages += (map->size + getpagesize() - 1) / getpagesize(); + __maps.pages += (map->size + __pagesize - 1) / __pagesize; __maps_add(map); __maps_check(); } @@ -302,7 +302,7 @@ struct Map *__maps_alloc(void) { memory_order_relaxed)) return map; } - int gransz = getgransize(); + int gransz = __gransize; struct DirectMap sys = sys_mmap(0, gransz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (sys.addr == MAP_FAILED) @@ -323,8 +323,8 @@ struct Map *__maps_alloc(void) { static int __munmap(char *addr, size_t size) { // validate arguments - int pagesz = getpagesize(); - int gransz = getgransize(); + int pagesz = __pagesize; + int gransz = __gransize; if (((uintptr_t)addr & (gransz - 1)) || // !size || (uintptr_t)addr + size < size) return einval(); @@ -540,8 +540,8 @@ static void *__mmap_impl(char *addr, size_t size, int prot, int flags, int fd, static void *__mmap(char *addr, size_t size, int prot, int flags, int fd, int64_t off) { char *res; - int pagesz = getpagesize(); - int gransz = getgransize(); + int pagesz = __pagesize; + int gransz = __gransize; // validate arguments if (((uintptr_t)addr & (gransz - 1)) || // @@ -656,8 +656,8 @@ static void *__mremap_impl(char *old_addr, size_t old_size, size_t new_size, static void *__mremap(char *old_addr, size_t old_size, size_t new_size, int flags, char *new_addr) { - int pagesz = getpagesize(); - int gransz = getgransize(); + int pagesz = __pagesize; + int gransz = __gransize; // kernel support if (!IsLinux() && !IsNetbsd()) diff --git a/libc/intrin/mprotect.c b/libc/intrin/mprotect.c index cd0586fef..a3c38744a 100644 --- a/libc/intrin/mprotect.c +++ b/libc/intrin/mprotect.c @@ -60,7 +60,7 @@ int __mprotect(char *addr, size_t size, int prot) { return 0; // unix checks prot before checking size - int pagesz = getpagesize(); + int pagesz = __pagesize; if (((intptr_t)addr & (pagesz - 1)) || (uintptr_t)addr + size < size) return einval(); diff --git a/libc/intrin/msync-nt.c b/libc/intrin/msync-nt.c index 72ce53b0c..73f6ed95a 100644 --- a/libc/intrin/msync-nt.c +++ b/libc/intrin/msync-nt.c @@ -27,7 +27,7 @@ textwindows int sys_msync_nt(char *addr, size_t size, int flags) { - int pagesz = getpagesize(); + int pagesz = __pagesize; size = (size + pagesz - 1) & -pagesz; if ((uintptr_t)addr & (pagesz - 1)) diff --git a/libc/intrin/pagesize.c b/libc/intrin/pagesize.c new file mode 100644 index 000000000..a15149123 --- /dev/null +++ b/libc/intrin/pagesize.c @@ -0,0 +1,47 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/dce.h" +#include "libc/intrin/kprintf.h" +#include "libc/nt/struct/systeminfo.h" +#include "libc/nt/systeminfo.h" +#include "libc/runtime/runtime.h" +#include "libc/sysv/consts/auxv.h" + +#ifdef __x86_64__ +__static_yoink("_init_pagesize"); +#endif + +int __pagesize; +int __gransize; + +textstartup static int __pagesize_get(unsigned long *auxv) { + for (; auxv && auxv[0]; auxv += 2) + if (auxv[0] == AT_PAGESZ) + return auxv[1]; +#ifdef __aarch64__ + return 16384; +#else + return 4096; +#endif +} + +textstartup dontinstrument void __pagesize_init(unsigned long *auxv) { + if (!__pagesize) + __gransize = __pagesize = __pagesize_get(auxv); +} diff --git a/libc/intrin/pagesize_init.S b/libc/intrin/pagesize_init.S new file mode 100644 index 000000000..5c1cda3fa --- /dev/null +++ b/libc/intrin/pagesize_init.S @@ -0,0 +1,28 @@ +/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ +│ vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi │ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2024 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/macros.internal.h" + + .init.start 251,_init_pagesize + push %rdi + push %rsi + mov %r15,%rdi + call __pagesize_init + pop %rsi + pop %rdi + .init.end 251,_init_pagesize diff --git a/libc/intrin/printmaps.c b/libc/intrin/printmaps.c index 18c02812e..0a67103b3 100644 --- a/libc/intrin/printmaps.c +++ b/libc/intrin/printmaps.c @@ -48,7 +48,7 @@ void __print_maps(size_t limit) { if (!--limit) break; } - kprintf("# %'zu bytes in %'zu mappings\n", __maps.pages * getpagesize(), + kprintf("# %'zu bytes in %'zu mappings\n", __maps.pages * __pagesize, __maps.count); __maps_unlock(); } diff --git a/libc/mem/leaks.c b/libc/mem/leaks.c index aae79fcfb..be64764aa 100644 --- a/libc/mem/leaks.c +++ b/libc/mem/leaks.c @@ -44,7 +44,7 @@ void __may_leak(void *alloc) { return; pthread_mutex_lock(&lock); if (dll_is_empty(freaks)) { - int g = getgransize(); + int g = __gransize; struct Leak *p = _mapanon(g); int n = g / sizeof(struct Leak); for (int i = 0; i < n; ++i) { diff --git a/libc/mem/pvalloc.c b/libc/mem/pvalloc.c index 95fee37c7..21346ab29 100644 --- a/libc/mem/pvalloc.c +++ b/libc/mem/pvalloc.c @@ -32,9 +32,9 @@ * @see valloc() */ void *pvalloc(size_t n) { - if (ckd_add(&n, n, getpagesize() - 1)) { + if (ckd_add(&n, n, __pagesize - 1)) { errno = ENOMEM; return 0; } - return memalign(getpagesize(), n & -getpagesize()); + return memalign(__pagesize, n & -__pagesize); } diff --git a/libc/mem/valloc.c b/libc/mem/valloc.c index 71fe70275..f8f9e90bf 100644 --- a/libc/mem/valloc.c +++ b/libc/mem/valloc.c @@ -29,5 +29,5 @@ * @see pvalloc() */ void *valloc(size_t n) { - return memalign(getpagesize(), n); + return memalign(__pagesize, n); } diff --git a/libc/nexgen32e/auxv.S b/libc/nexgen32e/auxv.S index 0c921df7c..e750b74fe 100644 --- a/libc/nexgen32e/auxv.S +++ b/libc/nexgen32e/auxv.S @@ -18,13 +18,13 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/macros.internal.h" - .initbss 300,_init_auxv + .initbss 250,_init_auxv // Global variable holding _start(auxv) parameter. __auxv: .quad 0 .endobj __auxv,globl .previous - .init.start 300,_init_auxv + .init.start 250,_init_auxv mov %r15,%rax stosq - .init.end 300,_init_auxv + .init.end 250,_init_auxv diff --git a/libc/proc/fork-nt.c b/libc/proc/fork-nt.c index e5e009d52..0c670b9c0 100644 --- a/libc/proc/fork-nt.c +++ b/libc/proc/fork-nt.c @@ -224,7 +224,7 @@ textwindows void WinMainForked(void) { } // map memory into process - int granularity = getgransize(); + int granularity = __gransize; for (struct Tree *e = tree_first(maps); e; e = tree_next(e)) { struct Map *map = MAP_TREE_CONTAINER(e); if ((uintptr_t)map->addr & (granularity - 1)) @@ -277,7 +277,7 @@ textwindows void WinMainForked(void) { for (struct Tree *e = tree_first(maps); e; e = tree_next(e)) { struct Map *map = MAP_TREE_CONTAINER(e); __maps.count += 1; - __maps.pages += (map->size + getpagesize() - 1) / getpagesize(); + __maps.pages += (map->size + __pagesize - 1) / __pagesize; unsigned old_protect; if (!VirtualProtect(map->addr, map->size, __prot2nt(map->prot, map->iscow), &old_protect)) @@ -385,7 +385,7 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) { ok = WriteAll(writer, &map, sizeof(map)); } // now write content of each map to child - int granularity = getgransize(); + int granularity = __gransize; for (struct Map *map = __maps_first(); ok && map; map = __maps_next(map)) { if (map->flags & MAP_NOFORK) diff --git a/libc/runtime/cosmo2.c b/libc/runtime/cosmo2.c index 8d962a9a6..e102b8fee 100644 --- a/libc/runtime/cosmo2.c +++ b/libc/runtime/cosmo2.c @@ -66,6 +66,8 @@ extern char ape_stack_prot[] __attribute__((__weak__)); extern pthread_mutex_t __mmi_lock_obj; extern int hostos asm("__hostos"); +void __pagesize_init(unsigned long *auxv); + static const char *DecodeMagnum(const char *p, long *r) { int k = 0; unsigned long c, x = 0; @@ -164,6 +166,7 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename, __pid = sys_getpid().ax; // initialize file system + __pagesize_init(auxv); __maps_init(); __init_fds(argc, argv, envp); @@ -172,13 +175,6 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename, __enable_tls(); -#if 0 -#if IsAsan() - // TODO(jart): Figure out ASAN data model on AARCH64. - __asan_init(argc, argv, envp, auxv); -#endif -#endif - _init(); // initialize program #if SYSDEBUG diff --git a/libc/runtime/getavphyspages.c b/libc/runtime/getavphyspages.c index 4ed40d3ed..fc88bfa15 100644 --- a/libc/runtime/getavphyspages.c +++ b/libc/runtime/getavphyspages.c @@ -23,5 +23,5 @@ long __get_avphys_pages(void) { struct sysinfo si; if (sysinfo(&si) == -1) return -1; - return (((int64_t)si.freeram + si.bufferram) * si.mem_unit) / getpagesize(); + return (((int64_t)si.freeram + si.bufferram) * si.mem_unit) / __pagesize; } diff --git a/libc/runtime/getphyspages.c b/libc/runtime/getphyspages.c index 1e553894b..e732eafca 100644 --- a/libc/runtime/getphyspages.c +++ b/libc/runtime/getphyspages.c @@ -23,5 +23,5 @@ long __get_phys_pages(void) { struct sysinfo si; if (sysinfo(&si) == -1) return -1; - return ((int64_t)si.totalram * si.mem_unit) / getpagesize(); + return ((int64_t)si.totalram * si.mem_unit) / __pagesize; } diff --git a/libc/runtime/isstackoverflow.c b/libc/runtime/isstackoverflow.c index 5d19014c6..cb1068a9c 100644 --- a/libc/runtime/isstackoverflow.c +++ b/libc/runtime/isstackoverflow.c @@ -35,5 +35,5 @@ char __is_stack_overflow(siginfo_t *si, void *arg) { return false; intptr_t sp = uc->uc_mcontext.SP; intptr_t fp = (intptr_t)si->si_addr; - return ABS(fp - sp) < getpagesize(); + return ABS(fp - sp) < __pagesize; } diff --git a/libc/runtime/opensymboltable.greg.c b/libc/runtime/opensymboltable.greg.c index 433890d9d..7cd2c8d7a 100644 --- a/libc/runtime/opensymboltable.greg.c +++ b/libc/runtime/opensymboltable.greg.c @@ -49,7 +49,7 @@ static struct SymbolTable *OpenSymbolTableImpl(const char *filename) { size_t n, m, tsz, size; const Elf64_Sym *symtab, *sym; ptrdiff_t names_offset, name_base_offset, stp_offset; - long pagesz = getpagesize(); + long pagesz = __pagesize; map = MAP_FAILED; if ((fd = open(filename, O_RDONLY | O_CLOEXEC)) == -1) return 0; diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 26f857536..2492fc95a 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -71,6 +71,8 @@ char *secure_getenv(const char *) paramsnonnull() __wur nosideeffect libcesque; extern int __argc; extern char **__argv; extern char **__envp; +extern int __pagesize; +extern int __gransize; extern unsigned long *__auxv; extern intptr_t __oldstack; extern char *__program_executable_name; diff --git a/libc/runtime/sysconf.c b/libc/runtime/sysconf.c index 2603ca453..f0c32dd90 100644 --- a/libc/runtime/sysconf.c +++ b/libc/runtime/sysconf.c @@ -61,9 +61,9 @@ long sysconf(int name) { case _SC_CLK_TCK: return CLK_TCK; case _SC_PAGESIZE: - return getpagesize(); + return __pagesize; case _SC_GRANSIZE: - return getgransize(); + return __gransize; case _SC_ARG_MAX: return __get_arg_max(); case _SC_SIGSTKSZ: diff --git a/libc/runtime/winmain.greg.c b/libc/runtime/winmain.greg.c index eb75a1700..9dda29289 100644 --- a/libc/runtime/winmain.greg.c +++ b/libc/runtime/winmain.greg.c @@ -59,6 +59,7 @@ __msabi extern typeof(AddVectoredExceptionHandler) *const __imp_AddVectoredExcep __msabi extern typeof(CreateFileMapping) *const __imp_CreateFileMappingW; __msabi extern typeof(DuplicateHandle) *const __imp_DuplicateHandle; __msabi extern typeof(FreeEnvironmentStrings) *const __imp_FreeEnvironmentStringsW; +__msabi extern typeof(GetCommandLine) *const __imp_GetCommandLineW; __msabi extern typeof(GetConsoleMode) *const __imp_GetConsoleMode; __msabi extern typeof(GetCurrentDirectory) *const __imp_GetCurrentDirectoryW; __msabi extern typeof(GetCurrentProcessId) *const __imp_GetCurrentProcessId; @@ -67,6 +68,7 @@ __msabi extern typeof(GetEnvironmentVariable) *const __imp_GetEnvironmentVariabl __msabi extern typeof(GetFileAttributes) *const __imp_GetFileAttributesW; __msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle; __msabi extern typeof(GetSystemInfo) *const __imp_GetSystemInfo; +__msabi extern typeof(GetSystemInfo) *const __imp_GetSystemInfo; __msabi extern typeof(GetUserName) *const __imp_GetUserNameW; __msabi extern typeof(MapViewOfFileEx) *const __imp_MapViewOfFileEx; __msabi extern typeof(SetConsoleCP) *const __imp_SetConsoleCP; @@ -87,16 +89,6 @@ __funline int IsAlpha(int c) { return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'); } -// https://nullprogram.com/blog/2022/02/18/ -__funline char16_t *MyCommandLine(void) { - void *cmd; - asm("mov\t%%gs:(0x60),%0\n" - "mov\t0x20(%0),%0\n" - "mov\t0x78(%0),%0\n" - : "=r"(cmd)); - return cmd; -} - static abi char16_t *StrStr(const char16_t *haystack, const char16_t *needle) { size_t i; for (;;) { @@ -318,9 +310,13 @@ abi int64_t WinMain(int64_t hInstance, int64_t hPrevInstance, "sudo sh -c 'echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop'\n"); return 77 << 8; // exit(77) } + struct NtSystemInfo si; + __imp_GetSystemInfo(&si); + __pagesize = si.dwPageSize; + __gransize = si.dwAllocationGranularity; __umask = 077; __pid = __imp_GetCurrentProcessId(); - cmdline = MyCommandLine(); + cmdline = __imp_GetCommandLineW(); #if SYSDEBUG // sloppy flag-only check for early initialization if (StrStr(cmdline, u"--strace")) diff --git a/libc/runtime/zipos-get.c b/libc/runtime/zipos-get.c index 352b7e4db..e2067a400 100644 --- a/libc/runtime/zipos-get.c +++ b/libc/runtime/zipos-get.c @@ -62,7 +62,7 @@ static void __zipos_dismiss(uint8_t *map, const uint8_t *cdir, long pg) { } // unmap the executable portion beneath the local files - mo = ROUNDDOWN(lo, getgransize()); + mo = ROUNDDOWN(lo, __gransize); if (mo) munmap(map, mo); @@ -128,7 +128,7 @@ static void __zipos_init(void) { if (!fstat(fd, &st) && (map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED) { if ((cdir = GetZipEocd(map, st.st_size, &err))) { - long pagesz = getpagesize(); + long pagesz = __pagesize; __zipos_dismiss(map, cdir, pagesz); __zipos.map = map; __zipos.cdir = cdir; diff --git a/libc/runtime/zipos-stat-impl.c b/libc/runtime/zipos-stat-impl.c index 5644a1bb0..93109bbc7 100644 --- a/libc/runtime/zipos-stat-impl.c +++ b/libc/runtime/zipos-stat-impl.c @@ -31,7 +31,7 @@ int __zipos_stat_impl(struct Zipos *zipos, size_t cf, struct stat *st) { bzero(st, sizeof(*st)); st->st_nlink = 1; st->st_dev = zipos->dev; - st->st_blksize = getpagesize(); + st->st_blksize = __pagesize; if (cf == ZIPOS_SYNTHETIC_DIRECTORY) { st->st_mode = S_IFDIR | (0555 & ~atomic_load_explicit( &__umask, memory_order_acquire)); diff --git a/libc/sysv/calls/getpagesize_freebsd.S b/libc/sysv/calls/getpagesize_freebsd.S deleted file mode 100644 index 23eb9eae1..000000000 --- a/libc/sysv/calls/getpagesize_freebsd.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/macros.internal.h" -.scall getpagesize_freebsd,0xffffff040fffffff,4095,4095,64,globl,hidden diff --git a/libc/sysv/syscalls.sh b/libc/sysv/syscalls.sh index 1887b7049..930516052 100755 --- a/libc/sysv/syscalls.sh +++ b/libc/sysv/syscalls.sh @@ -679,7 +679,6 @@ scall sys_getcontext 0x133fff1a5fffffff 0xfff globl hidden #scall gethostname 0xffff0057ffffffff 0xfff globl #scall getkerninfo 0xffffff03ffffffff 0xfff globl #scall getloginclass 0xffffff20bfffffff 0xfff globl -scall getpagesize_freebsd 0xffffff040fffffff 0xfff globl hidden #scall gssd_syscall 0xffffff1f9fffffff 0xfff globl #scall jail 0xffffff152fffffff 0xfff globl #scall jail_attach 0xffffff1b4fffffff 0xfff globl diff --git a/libc/thread/pthread_attr_init.c b/libc/thread/pthread_attr_init.c index 9f23d2595..b4c82204e 100644 --- a/libc/thread/pthread_attr_init.c +++ b/libc/thread/pthread_attr_init.c @@ -38,7 +38,7 @@ errno_t pthread_attr_init(pthread_attr_t *attr) { *attr = (pthread_attr_t){ .__stacksize = GetStackSize(), - .__guardsize = getpagesize(), + .__guardsize = __pagesize, }; return 0; } diff --git a/libc/thread/pthread_create.c b/libc/thread/pthread_create.c index 55db00ef4..c0933e50a 100644 --- a/libc/thread/pthread_create.c +++ b/libc/thread/pthread_create.c @@ -144,7 +144,7 @@ static int FixupCustomStackOnOpenbsd(pthread_attr_t *attr) { size_t n; uintptr_t x, y; int e, rc, pagesz; - pagesz = getpagesize(); + pagesz = __pagesize; n = attr->__stacksize; x = (uintptr_t)attr->__stackaddr; y = ROUNDUP(x, pagesz); @@ -210,7 +210,7 @@ static errno_t pthread_create_impl(pthread_t *thread, } } else { // cosmo is managing the stack - int pagesize = getpagesize(); + int pagesize = __pagesize; pt->pt_attr.__guardsize = ROUNDUP(pt->pt_attr.__guardsize, pagesize); pt->pt_attr.__stacksize = pt->pt_attr.__stacksize; if (pt->pt_attr.__guardsize + pagesize > pt->pt_attr.__stacksize) {