diff --git a/lib/libthr/arch/aarch64/include/pthread_md.h b/lib/libthr/arch/aarch64/include/pthread_md.h --- a/lib/libthr/arch/aarch64/include/pthread_md.h +++ b/lib/libthr/arch/aarch64/include/pthread_md.h @@ -41,6 +41,9 @@ #define CPU_SPINWAIT +/* For use in _Static_assert to check structs will fit in a page */ +#define THR_PAGE_SIZE_MIN PAGE_SIZE_4K + static __inline struct pthread * _get_curthread(void) { diff --git a/lib/libthr/arch/amd64/include/pthread_md.h b/lib/libthr/arch/amd64/include/pthread_md.h --- a/lib/libthr/arch/amd64/include/pthread_md.h +++ b/lib/libthr/arch/amd64/include/pthread_md.h @@ -41,6 +41,9 @@ #define CPU_SPINWAIT __asm __volatile("pause") +/* For use in _Static_assert to check structs will fit in a page */ +#define THR_PAGE_SIZE_MIN PAGE_SIZE + static __inline struct pthread * _get_curthread(void) { diff --git a/lib/libthr/arch/arm/include/pthread_md.h b/lib/libthr/arch/arm/include/pthread_md.h --- a/lib/libthr/arch/arm/include/pthread_md.h +++ b/lib/libthr/arch/arm/include/pthread_md.h @@ -39,6 +39,9 @@ #define CPU_SPINWAIT +/* For use in _Static_assert to check structs will fit in a page */ +#define THR_PAGE_SIZE_MIN PAGE_SIZE + static __inline struct pthread * _get_curthread(void) { diff --git a/lib/libthr/arch/i386/include/pthread_md.h b/lib/libthr/arch/i386/include/pthread_md.h --- a/lib/libthr/arch/i386/include/pthread_md.h +++ b/lib/libthr/arch/i386/include/pthread_md.h @@ -41,6 +41,9 @@ #define CPU_SPINWAIT __asm __volatile("pause") +/* For use in _Static_assert to check structs will fit in a page */ +#define THR_PAGE_SIZE_MIN PAGE_SIZE + static __inline struct pthread * _get_curthread(void) { diff --git a/lib/libthr/arch/powerpc/include/pthread_md.h b/lib/libthr/arch/powerpc/include/pthread_md.h --- a/lib/libthr/arch/powerpc/include/pthread_md.h +++ b/lib/libthr/arch/powerpc/include/pthread_md.h @@ -40,6 +40,9 @@ #define CPU_SPINWAIT +/* For use in _Static_assert to check structs will fit in a page */ +#define THR_PAGE_SIZE_MIN PAGE_SIZE + static __inline struct pthread * _get_curthread(void) { diff --git a/lib/libthr/arch/riscv/include/pthread_md.h b/lib/libthr/arch/riscv/include/pthread_md.h --- a/lib/libthr/arch/riscv/include/pthread_md.h +++ b/lib/libthr/arch/riscv/include/pthread_md.h @@ -46,6 +46,9 @@ #define CPU_SPINWAIT +/* For use in _Static_assert to check structs will fit in a page */ +#define THR_PAGE_SIZE_MIN PAGE_SIZE + static __inline struct pthread * _get_curthread(void) { diff --git a/lib/libthr/thread/thr_barrier.c b/lib/libthr/thread/thr_barrier.c --- a/lib/libthr/thread/thr_barrier.c +++ b/lib/libthr/thread/thr_barrier.c @@ -37,7 +37,7 @@ #include "thr_private.h" -_Static_assert(sizeof(struct pthread_barrier) <= PAGE_SIZE, +_Static_assert(sizeof(struct pthread_barrier) <= THR_PAGE_SIZE_MIN, "pthread_barrier is too large for off-page"); __weak_reference(_pthread_barrier_init, pthread_barrier_init); diff --git a/lib/libthr/thread/thr_cond.c b/lib/libthr/thread/thr_cond.c --- a/lib/libthr/thread/thr_cond.c +++ b/lib/libthr/thread/thr_cond.c @@ -43,7 +43,7 @@ #include "thr_private.h" -_Static_assert(sizeof(struct pthread_cond) <= PAGE_SIZE, +_Static_assert(sizeof(struct pthread_cond) <= THR_PAGE_SIZE_MIN, "pthread_cond too large"); /* diff --git a/lib/libthr/thread/thr_malloc.c b/lib/libthr/thread/thr_malloc.c --- a/lib/libthr/thread/thr_malloc.c +++ b/lib/libthr/thread/thr_malloc.c @@ -51,8 +51,7 @@ return; npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d)); if (npagesizes == -1) { - npagesizes = 1; - pagesizes_d[0] = PAGE_SIZE; + PANIC("Unable to read page sizes"); } pagesizes = pagesizes_d; _thr_umutex_init(&thr_malloc_umtx); diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c --- a/lib/libthr/thread/thr_mutex.c +++ b/lib/libthr/thread/thr_mutex.c @@ -53,7 +53,7 @@ #include "thr_private.h" -_Static_assert(sizeof(struct pthread_mutex) <= PAGE_SIZE, +_Static_assert(sizeof(struct pthread_mutex) <= THR_PAGE_SIZE_MIN, "pthread_mutex is too large for off-page"); /* diff --git a/lib/libthr/thread/thr_pshared.c b/lib/libthr/thread/thr_pshared.c --- a/lib/libthr/thread/thr_pshared.c +++ b/lib/libthr/thread/thr_pshared.c @@ -50,12 +50,17 @@ #define PSHARED_KEY_HASH(key) (((unsigned long)(key) >> 8) % HASH_SIZE) /* XXXKIB: lock could be split to per-hash chain, if appears contested */ static struct urwlock pshared_lock = DEFAULT_URWLOCK; +static int page_size; void __thr_pshared_init(void) { int i; + page_size = getpagesize(); + THR_ASSERT(page_size >= THR_PAGE_SIZE_MIN, + "THR_PAGE_SIZE_MIN is too large"); + _thr_urwlock_init(&pshared_lock); for (i = 0; i < HASH_SIZE; i++) LIST_INIT(&pshared_hash[i]); @@ -112,7 +117,7 @@ if (error == 0) continue; LIST_REMOVE(h, link); - munmap(h->val, PAGE_SIZE); + munmap(h->val, page_size); free(h); } } @@ -164,7 +169,7 @@ */ if (h->key == key) { if (h->val != *val) { - munmap(*val, PAGE_SIZE); + munmap(*val, page_size); *val = h->val; } return (1); @@ -204,7 +209,7 @@ { if (val != NULL) - munmap(val, PAGE_SIZE); + munmap(val, page_size); _umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_DESTROY, key, NULL); } @@ -225,7 +230,7 @@ UMTX_SHM_LOOKUP, key, NULL); if (fd == -1) return (NULL); - res = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + res = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if (res == MAP_FAILED) return (NULL); diff --git a/lib/libthr/thread/thr_pspinlock.c b/lib/libthr/thread/thr_pspinlock.c --- a/lib/libthr/thread/thr_pspinlock.c +++ b/lib/libthr/thread/thr_pspinlock.c @@ -41,7 +41,7 @@ #include "thr_private.h" -_Static_assert(sizeof(struct pthread_spinlock) <= PAGE_SIZE, +_Static_assert(sizeof(struct pthread_spinlock) <= THR_PAGE_SIZE_MIN, "pthread_spinlock is too large for off-page"); #define SPIN_COUNT 100000 diff --git a/lib/libthr/thread/thr_rwlock.c b/lib/libthr/thread/thr_rwlock.c --- a/lib/libthr/thread/thr_rwlock.c +++ b/lib/libthr/thread/thr_rwlock.c @@ -38,7 +38,7 @@ #include "un-namespace.h" #include "thr_private.h" -_Static_assert(sizeof(struct pthread_rwlock) <= PAGE_SIZE, +_Static_assert(sizeof(struct pthread_rwlock) <= THR_PAGE_SIZE_MIN, "pthread_rwlock is too large for off-page"); __weak_reference(_thr_rwlock_destroy, pthread_rwlock_destroy);