Get deathstar demo working again on metal

This commit is contained in:
Justine Tunney 2024-07-04 03:44:17 -07:00
parent 15ea0524b3
commit bd6d9ff99a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 21 additions and 17 deletions

View file

@ -34,7 +34,7 @@ int __tailcontext(const ucontext_t *);
* @see getcontext() * @see getcontext()
*/ */
int setcontext(const ucontext_t *uc) { int setcontext(const ucontext_t *uc) {
if (IsWindows()) { if (IsWindows() || IsMetal()) {
atomic_store_explicit(&__get_tls()->tib_sigmask, uc->uc_sigmask, atomic_store_explicit(&__get_tls()->tib_sigmask, uc->uc_sigmask,
memory_order_release); memory_order_release);
} else { } else {
@ -44,7 +44,7 @@ int setcontext(const ucontext_t *uc) {
} }
int __getcontextsig(ucontext_t *uc) { int __getcontextsig(ucontext_t *uc) {
if (IsWindows()) { if (IsWindows() || IsMetal()) {
uc->uc_sigmask = uc->uc_sigmask =
atomic_load_explicit(&__get_tls()->tib_sigmask, memory_order_acquire); atomic_load_explicit(&__get_tls()->tib_sigmask, memory_order_acquire);
return 0; return 0;
@ -54,7 +54,7 @@ int __getcontextsig(ucontext_t *uc) {
} }
int __swapcontextsig(ucontext_t *x, const ucontext_t *y) { int __swapcontextsig(ucontext_t *x, const ucontext_t *y) {
if (IsWindows()) { if (IsWindows() || IsMetal()) {
x->uc_sigmask = atomic_exchange_explicit( x->uc_sigmask = atomic_exchange_explicit(
&__get_tls()->tib_sigmask, y->uc_sigmask, memory_order_acquire); &__get_tls()->tib_sigmask, y->uc_sigmask, memory_order_acquire);
return 0; return 0;

View file

@ -45,7 +45,7 @@
#ifdef __x86_64__ #ifdef __x86_64__
#define INVERT(x) (BANE + PHYSICAL((uintptr_t)(x))) #define INVERT(x) (BANE + PHYSICAL((uintptr_t)(x)))
#define NOPAGE ((uint64_t) - 1) #define NOPAGE ((uint64_t)-1)
#define APE_STACK_VADDR \ #define APE_STACK_VADDR \
({ \ ({ \
@ -69,9 +69,9 @@ texthead uint64_t __new_page(struct mman *mm) {
if (p != NOPAGE) { if (p != NOPAGE) {
uint64_t q; uint64_t q;
struct ReclaimedPage *rp = (struct ReclaimedPage *)(BANE + p); struct ReclaimedPage *rp = (struct ReclaimedPage *)(BANE + p);
unassert(p == (p & PAGE_TA)); /* unassert(p == (p & PAGE_TA)); */
q = rp->next; q = rp->next;
unassert(q == (q & PAGE_TA) || q == NOPAGE); /* unassert(q == (q & PAGE_TA) || q == NOPAGE); */
mm->frp = q; mm->frp = q;
return p; return p;
} }
@ -200,7 +200,7 @@ void __ref_pages(struct mman *mm, uint64_t *pml4t, uint64_t ps, uint64_t size) {
*/ */
static void __reclaim_page(struct mman *mm, uint64_t p) { static void __reclaim_page(struct mman *mm, uint64_t p) {
struct ReclaimedPage *rp = (struct ReclaimedPage *)(BANE + p); struct ReclaimedPage *rp = (struct ReclaimedPage *)(BANE + p);
unassert(p == (p & PAGE_TA)); /* unassert(p == (p & PAGE_TA)); */
rp->next = mm->frp; rp->next = mm->frp;
mm->frp = p; mm->frp = p;
} }

View file

@ -25,7 +25,6 @@
#include "libc/runtime/internal.h" #include "libc/runtime/internal.h"
#include "libc/thread/lock.h" #include "libc/thread/lock.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "third_party/nsync/mu.h" #include "third_party/nsync/mu.h"
/** /**

View file

@ -27,9 +27,12 @@
struct Signals __sig; struct Signals __sig;
sigset_t __sig_block(void) { sigset_t __sig_block(void) {
if (IsWindows()) { if (IsWindows() || IsMetal()) {
return atomic_exchange_explicit(&__get_tls()->tib_sigmask, -1, if (__tls_enabled)
memory_order_acquire); return atomic_exchange_explicit(&__get_tls()->tib_sigmask, -1,
memory_order_acquire);
else
return 0;
} else { } else {
sigset_t res, neu = -1; sigset_t res, neu = -1;
sys_sigprocmask(SIG_SETMASK, &neu, &res); sys_sigprocmask(SIG_SETMASK, &neu, &res);
@ -38,10 +41,12 @@ sigset_t __sig_block(void) {
} }
void __sig_unblock(sigset_t m) { void __sig_unblock(sigset_t m) {
if (IsWindows()) { if (IsWindows() || IsMetal()) {
atomic_store_explicit(&__get_tls()->tib_sigmask, m, memory_order_release); if (__tls_enabled) {
if (_weaken(__sig_check)) { atomic_store_explicit(&__get_tls()->tib_sigmask, m, memory_order_release);
_weaken(__sig_check)(); if (_weaken(__sig_check)) {
_weaken(__sig_check)();
}
} }
} else { } else {
sys_sigprocmask(SIG_SETMASK, &m, 0); sys_sigprocmask(SIG_SETMASK, &m, 0);

View file

@ -57,7 +57,7 @@ dontinstrument textstartup void __set_tls(struct CosmoTib *tib) {
uint64_t val = (uint64_t)tib; uint64_t val = (uint64_t)tib;
asm volatile("wrmsr" asm volatile("wrmsr"
: /* no outputs */ : /* no outputs */
: "c"(MSR_IA32_FS_BASE), "a"((uint32_t)val), : "c"(MSR_IA32_GS_BASE), "a"((uint32_t)val),
"d"((uint32_t)(val >> 32))); "d"((uint32_t)(val >> 32)));
} }
#elif defined(__aarch64__) #elif defined(__aarch64__)

View file

@ -118,7 +118,7 @@ static int PosixThread(void *arg, int tid) {
// set long jump handler so pthread_exit can bring control back here // set long jump handler so pthread_exit can bring control back here
if (!setjmp(pt->pt_exiter)) { if (!setjmp(pt->pt_exiter)) {
sigdelset(&pt->pt_attr.__sigmask, SIGTHR); sigdelset(&pt->pt_attr.__sigmask, SIGTHR);
if (IsWindows()) { if (IsWindows() || IsMetal()) {
atomic_store_explicit(&__get_tls()->tib_sigmask, pt->pt_attr.__sigmask, atomic_store_explicit(&__get_tls()->tib_sigmask, pt->pt_attr.__sigmask,
memory_order_release); memory_order_release);
} else { } else {