Skip to content

Commit

Permalink
Repurpose the enable_extralocking stuff to enable or disable addition…
Browse files Browse the repository at this point in the history
…al thread coherency code, disable app_nap, preliminary checks to try to figure out if a non musl/Alpine release is running.
  • Loading branch information
Mike Miller committed Oct 30, 2022
1 parent d79dbcf commit e98aefd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 7 deletions.
11 changes: 11 additions & 0 deletions app/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
extern void run_at_boot(void);
#import <Foundation/Foundation.h>
#import <Foundation/NSProcessInfo.h>

void disable_app_nap(void)
{
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)])
{
[[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"Not sleepy and don't want to nap"];
}
}

int main(int argc, char * argv[]) {
@autoreleasepool {
disable_app_nap(); // No napping I say. -mke
run_at_boot();
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
Expand Down
13 changes: 11 additions & 2 deletions emu/cpuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CPUID_H

#include "misc.h"
extern bool isGlibC;

static inline void do_cpuid(dword_t *eax, dword_t *ebx, dword_t *ecx, dword_t *edx) {
dword_t leaf = *eax;
Expand All @@ -17,12 +18,20 @@ static inline void do_cpuid(dword_t *eax, dword_t *ebx, dword_t *ecx, dword_t *e
*eax = 0x0; // say nothing about cpu model number
*ebx = 0x0; // processor number 0, flushes 0 bytes on clflush
*ecx = 0; // we support none of the features in ecx
*edx = (1 << 0) // fpu
if(isGlibC) {
*edx = (1 << 0) // fpu
| (1 << 15) // cmov
| (1 << 23) // mmx
// | (1 << 25) // sse
| (1 << 25) // sse // musl handles sse being enabled badly. -mke
| (1 << 26) // sse2
;
} else {
*edx = (1 << 0) // fpu
| (1 << 15) // cmov
| (1 << 23) // mmx
| (1 << 26) // sse2
;
}
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions jit/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ struct jit *jit_new(struct mmu *mmu) {
return jit;
}



void jit_free(struct jit *jit) {
bool signal_pending = !!(current->pending & ~current->blocked);
while((critical_region_count(current) > 2) || (locks_held_count(current)) || (current->process_info_being_read) || (signal_pending)) { // Wait for now, task is in one or more critical sections, and/or has locks, or signals in flight
nanosleep(&lock_pause, NULL);
signal_pending = !!(current->pending & ~current->blocked);
}
for (size_t i = 0; i < jit->hash_size; i++) {
struct jit_block *block, *tmp;
if (list_null(&jit->hash[i]))
Expand Down
8 changes: 5 additions & 3 deletions kernel/calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
#include "kernel/task.h"
#include "kernel/resource_locking.h"

dword_t syscall_stub() {
extern bool isGlibC;

dword_t syscall_stub(void) {
STRACE("syscall_stub()");
//STRACE("syscall_stub()");
return _ENOSYS;
}
dword_t syscall_stub_silent() {
dword_t syscall_stub_silent(void) {
STRACE("syscall_stub_silent()");
return _ENOSYS;
}
dword_t syscall_success_stub() {
dword_t syscall_success_stub(void) {
STRACE("syscall_stub_success()");
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

extern bool doEnableExtraLocking;
extern pthread_mutex_t extra_lock;
extern bool isGlibC;

static struct fd *at_fd(fd_t f) {
if (f == AT_FDCWD_)
Expand Down Expand Up @@ -94,6 +95,7 @@ fd_t sys_open(addr_t path_addr, dword_t flags, mode_t_ mode) {
}

fd_t sys_creat(addr_t path_addr, mode_t_ mode) {
isGlibC = true; // In theory, musl should never call creat -mk
dword_t flags = 0;
flags |= O_CREAT_;
flags |= O_WRONLY_;
Expand Down
7 changes: 6 additions & 1 deletion kernel/futex.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "kernel/calls.h"
#include "kernel/resource_locking.h"
#include "futex.h"
// Apple doesn't implement futex, so we have to fake it
#define FUTEX_WAIT_ 0
Expand Down Expand Up @@ -207,7 +208,11 @@ dword_t sys_futex(addr_t uaddr, dword_t op, dword_t val, addr_t timeout_or_val2,
// mytime.tv_nsec = 0;
// return futex_wait(uaddr, val, &mytime);
// } else {
return futex_wait(uaddr, val, timeout_or_val2 ? &timeout : NULL);
modify_critical_region_counter(current, 1, __FILE__, __LINE__);
dword_t return_val;
return_val = futex_wait(uaddr, val, timeout_or_val2 ? &timeout : NULL);
modify_critical_region_counter(current, -1, __FILE__, __LINE__);
return return_val;
// }
case FUTEX_WAKE_:
STRACE("futex(FUTEX_WAKE, %#x, %d)", uaddr, val);
Expand Down
1 change: 1 addition & 0 deletions kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ unsigned maxl = 10; // Max age of an extra_lock
bool BOOTING = true;

bool doEnableMulticore; // Enable multicore if toggled, should default to false
bool isGlibC = false; // Try to guess if we're running a non musl distro. -mke
bool doEnableExtraLocking; // Enable extra locking if toggled, should default to false
unsigned doLockSleepNanoseconds; // How many nanoseconds should __lock() sleep between retries

Expand Down
8 changes: 7 additions & 1 deletion util/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>

int noprintk = 0; // Used to suprress calls to printk. -mke
extern bool doEnableExtraLocking;

void cond_init(cond_t *cond) {
pthread_condattr_t attr;
Expand All @@ -32,6 +33,7 @@ static bool is_signal_pending(lock_t *lock) {
}

void modify_critical_region_counter(struct task *task, int value, __attribute__((unused)) const char *file, __attribute__((unused)) int line) { // value Should only be -1 or 1. -mke

if(task == NULL) {
if(current != NULL) {
task = current;
Expand All @@ -42,6 +44,9 @@ void modify_critical_region_counter(struct task *task, int value, __attribute__(
return;
}

if((!doEnableExtraLocking) && (task->pid > 9))
return;

pthread_mutex_lock(&task->critical_region.lock);

if(!task->critical_region.count && (value < 0)) { // Prevent our unsigned value attempting to go negative. -mke
Expand All @@ -64,8 +69,9 @@ void modify_critical_region_counter(struct task *task, int value, __attribute__(
}

void modify_critical_region_counter_wrapper(int value, __attribute__((unused)) const char *file, __attribute__((unused)) int line) { // sync.h can't know about the definition of task struct due to recursive include files. -mke
if(current != NULL)
if((current != NULL) && (doEnableExtraLocking))
modify_critical_region_counter(current, value, file, line);

return;
}

Expand Down

0 comments on commit e98aefd

Please sign in to comment.