Create variables for page size

This commit is contained in:
Justine Tunney 2024-07-18 21:02:59 -07:00
parent 23dfb79d33
commit 567d8fe32d
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
34 changed files with 137 additions and 101 deletions

View file

@ -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

View file

@ -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;

View file

@ -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))

View file

@ -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.

View file

@ -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 {

View file

@ -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;
}

View file

@ -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);

View file

@ -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;
}

View file

@ -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()) {

View file

@ -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())

View file

@ -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();

View file

@ -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))

47
libc/intrin/pagesize.c Normal file
View file

@ -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);
}

View file

@ -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

View file

@ -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();
}

View file

@ -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) {

View file

@ -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);
}

View file

@ -29,5 +29,5 @@
* @see pvalloc()
*/
void *valloc(size_t n) {
return memalign(getpagesize(), n);
return memalign(__pagesize, n);
}

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -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:

View file

@ -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"))

View file

@ -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;

View file

@ -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));

View file

@ -1,2 +0,0 @@
#include "libc/sysv/macros.internal.h"
.scall getpagesize_freebsd,0xffffff040fffffff,4095,4095,64,globl,hidden

View file

@ -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

View file

@ -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;
}

View file

@ -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) {