Skip to content

Commit 9daaaee

Browse files
committed
Reapply "Merge pull request n64decomp#5 from tehzz/darwin-user"
This reverts commit c9c54aa.
1 parent 0588437 commit 9daaaee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+41168
-23
lines changed

Makefile.objs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ trace-events-subdirs += target/s390x
245245
trace-events-subdirs += target/ppc
246246
trace-events-subdirs += qom
247247
trace-events-subdirs += linux-user
248+
trace-events-subdirs += darwin-user
248249
trace-events-subdirs += qapi
249250
trace-events-subdirs += accel/tcg
250251
trace-events-subdirs += accel/kvm

Makefile.target

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ obj-y += gdbstub.o
129129

130130
endif #CONFIG_BSD_USER
131131

132+
#########################################################
133+
# macOS user emulator target
134+
135+
ifdef CONFIG_DARWIN_USER
136+
137+
QEMU_CFLAGS+=-I$(SRC_PATH)/darwin-user/$(TARGET_ABI_DIR) \
138+
-I$(SRC_PATH)/darwin-user/host/$(ARCH) \
139+
-I$(SRC_PATH)/darwin-user
140+
141+
obj-y += darwin-user/
142+
obj-y += gdbstub.o thunk.o
143+
144+
endif #CONFIG_DARWIN_USER
145+
132146
#########################################################
133147
# System emulator target
134148
ifdef CONFIG_SOFTMMU

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
This QEMU patch introduces irix/solaris userland emulation. It currently runs
2-
only under linux (though BSD support would probably be feasable).
2+
only under linux and macOS (though BSD support would probably be feasable).
33

44
### compiling
55

6-
Configure QEMU for irix/solaris userland emulation and compile (see the original
6+
Configure QEMU for irix/solaris userland emulation for linux and compile (see the original
77
QEMU README for further instructions):
88

99
```
1010
configure --target-list=irix-linux-user,irixn32-linux-user,irix64-linux-user,solaris-linux-user
1111
make && make install
1212
```
1313

14+
Or, configure QEMU for irix userland emulatin for macOS
15+
16+
```
17+
./configure --target-list=irix-darwin-user \
18+
--disable-vnc \
19+
--disable-sdl \
20+
--disable-gtk \
21+
--disable-cocoa \
22+
--disable-opengl \
23+
--disable-capstone \
24+
--disable-hax \
25+
--disable-hvf \
26+
--disable-tools
27+
make
28+
```
29+
1430
### using
1531

1632
I recommend using binfmt. I have prepared some scripts for this which you can

accel/tcg/translate-all.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ static void page_init(void)
430430
#else
431431
FILE *f;
432432

433-
last_brk = (unsigned long)sbrk(0);
433+
// depreciated in osx, but value is not used?
434+
//last_brk = (unsigned long)sbrk(0);
434435

435436
f = fopen("/compat/linux/proc/self/maps", "r");
436437
if (f) {
@@ -602,8 +603,27 @@ static inline void *split_cross_256mb(void *buf1, size_t size1)
602603
return buf1;
603604
}
604605
#endif
605-
606606
#ifdef USE_STATIC_CODE_GEN_BUFFER
607+
#if defined(CONFIG_DARWIN_USER)
608+
static inline void *alloc_code_gen_buffer(void)
609+
{
610+
int prot = PROT_WRITE | PROT_READ | PROT_EXEC;
611+
int flags = MAP_PRIVATE | MAP_ANON;
612+
/* let a kernel pick an address close to the executable */
613+
uintptr_t start = 0;
614+
/* Honor a command-line option limiting the size of the buffer. */
615+
size_t size = DEFAULT_CODE_GEN_BUFFER_SIZE;
616+
void *buf;
617+
618+
buf = mmap((void *)start, size, prot, flags, -1, 0);
619+
if (buf == MAP_FAILED) {
620+
return NULL;
621+
}
622+
623+
qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
624+
return buf;
625+
}
626+
#else /* !CONFIG_DARWIN_USER */
607627
static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
608628
__attribute__((aligned(CODE_GEN_ALIGN)));
609629

@@ -640,6 +660,8 @@ static inline void *alloc_code_gen_buffer(void)
640660

641661
return buf;
642662
}
663+
#endif /* CONFIG_DARWIN_USER */
664+
643665
#elif defined(_WIN32)
644666
static inline void *alloc_code_gen_buffer(void)
645667
{

accel/tcg/user-exec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ int cpu_signal_handler(int host_signum, void *pinfo,
244244
#define TRAP_sig(context) ((context)->sc_trapno)
245245
#define ERROR_sig(context) ((context)->sc_err)
246246
#define MASK_sig(context) ((context)->sc_mask)
247+
#elif defined(__APPLE__)
248+
#define PC_sig(context) ((context)->uc_mcontext->__ss.__rip)
249+
#define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
250+
#define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
251+
#define MASK_sig(context) ((context)->uc_sigmask)
247252
#elif defined(__FreeBSD__) || defined(__DragonFly__)
248253
#include <ucontext.h>
249254

configure

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ supported_target() {
249249
return 1
250250
fi
251251
;;
252+
*-darwin-user)
253+
if test "$darwin" != "yes"; then
254+
print_error "Target '$target' is only available on a Darwin host"
255+
return 1
256+
fi
257+
;;
252258
*)
253259
print_error "Invalid target name '$target'"
254260
return 1
@@ -390,6 +396,7 @@ cocoa="no"
390396
softmmu="yes"
391397
linux_user="no"
392398
bsd_user="no"
399+
darwin_user="no"
393400
blobs="yes"
394401
pkgversion=""
395402
pie=""
@@ -766,6 +773,7 @@ OpenBSD)
766773
Darwin)
767774
bsd="yes"
768775
darwin="yes"
776+
darwin_user="yes"
769777
hax="yes"
770778
hvf="yes"
771779
LDFLAGS_SHARED="-bundle -undefined dynamic_lookup"
@@ -1119,6 +1127,7 @@ for opt do
11191127
--disable-user)
11201128
linux_user="no" ;
11211129
bsd_user="no" ;
1130+
darwin_user="no" ;
11221131
;;
11231132
--enable-user) ;;
11241133
--disable-linux-user) linux_user="no"
@@ -1129,6 +1138,10 @@ for opt do
11291138
;;
11301139
--enable-bsd-user) bsd_user="yes"
11311140
;;
1141+
--disable-darwin-user) darwin_user="no"
1142+
;;
1143+
--enable-darwin-user) darwin_user="yes"
1144+
;;
11321145
--enable-pie) pie="yes"
11331146
;;
11341147
--disable-pie) pie="no"
@@ -1439,6 +1452,7 @@ QEMU_CFLAGS="$CPU_CFLAGS $QEMU_CFLAGS"
14391452
if [ "$ARCH" = "unknown" ]; then
14401453
bsd_user="no"
14411454
linux_user="no"
1455+
darwin_user="no"
14421456
fi
14431457

14441458
default_target_list=""
@@ -1454,6 +1468,10 @@ fi
14541468
if [ "$bsd_user" = "yes" ]; then
14551469
mak_wilds="${mak_wilds} $source_path/default-configs/*-bsd-user.mak"
14561470
fi
1471+
if [ "$darwin_user" = "yes" ]; then
1472+
mak_wilds="${mak_wilds} $source_path/default-configs/*-darwin-user.mak"
1473+
fi
1474+
14571475

14581476
for config in $mak_wilds; do
14591477
default_target_list="${default_target_list} $(basename "$config" .mak)"
@@ -1548,6 +1566,7 @@ disabled with --disable-FEATURE, default is enabled if available:
15481566
user supported user emulation targets
15491567
linux-user all linux usermode emulation targets
15501568
bsd-user all BSD usermode emulation targets
1569+
darwin-user all MacOS usermode emulation targets
15511570
docs build documentation
15521571
guest-agent build the QEMU Guest Agent
15531572
guest-agent-msi build guest agent Windows MSI installation package
@@ -5591,7 +5610,7 @@ if test "$cpu" = "s390x" ; then
55915610
fi
55925611

55935612
# Probe for the need for relocating the user-only binary.
5594-
if ( [ "$linux_user" = yes ] || [ "$bsd_user" = yes ] ) && [ "$pie" = no ]; then
5613+
if ( [ "$linux_user" = yes ] || [ "$bsd_user" = yes ] || [ "$darwin_user" = yes ] ) && [ "$pie" = no ]; then
55955614
textseg_addr=
55965615
case "$cpu" in
55975616
arm | i386 | ppc* | s390* | sparc* | x86_64 | x32)
@@ -5604,31 +5623,53 @@ if ( [ "$linux_user" = yes ] || [ "$bsd_user" = yes ] ) && [ "$pie" = no ]; then
56045623
textseg_addr=0x60000000
56055624
;;
56065625
esac
5626+
56075627
if [ -n "$textseg_addr" ]; then
56085628
cat > $TMPC <<EOF
56095629
int main(void) { return 0; }
56105630
EOF
5611-
textseg_ldflags="-Wl,-Ttext-segment=$textseg_addr"
5612-
if ! compile_prog "" "$textseg_ldflags"; then
5613-
# In case ld does not support -Ttext-segment, edit the default linker
5614-
# script via sed to set the .text start addr. This is needed on FreeBSD
5615-
# at least.
5616-
if ! $ld --verbose >/dev/null 2>&1; then
5617-
error_exit \
5618-
"We need to link the QEMU user mode binaries at a" \
5619-
"specific text address. Unfortunately your linker" \
5620-
"doesn't support either the -Ttext-segment option or" \
5621-
"printing the default linker script with --verbose." \
5622-
"If you don't want the user mode binaries, pass the" \
5623-
"--disable-user option to configure."
5624-
fi
5631+
# 64bit macOS reserves 4GB for page zero to catch truncated pointer to int casts.
5632+
# Follow suggested Wine configuration from:
5633+
# https://stackoverflow.com/questions/46916112/osx-ld-why-does-pagezero-size-default-to-4gb-on-64b-osx
5634+
if [ "$darwin_user" = yes ] ; then
5635+
pz_size_flag=",-pagezero_size,0x1000"
5636+
else
5637+
pz_size_flag=""
5638+
fi
56255639

5640+
# Check three different sets of ld flags:
5641+
# default_... for standard gnu ld (Linux)
5642+
# clang_... for llvm clang
5643+
# macos_... for macOS built-in ld
5644+
# If none of the above options are supported, edit the default linker
5645+
# script via sed to set the .text start addr. This is needed on FreeBSD
5646+
# at least.
5647+
default_ldflags="-Wl,-Ttext-segment=$textseg_addr"
5648+
clang_ldflags="-Wl,-image-base,${textseg_addr}${pz_size_flag}"
5649+
macos_ldflags="-Wl,-image_base,${textseg_addr}${pz_size_flag}"
5650+
5651+
if compile_prog "" "$default_ldflags"; then
5652+
textseg_ldflags="$default_ldflags"
5653+
elif compile_prog "" "$clang_ldflags"; then
5654+
textseg_ldflags="$clang_ldflags"
5655+
elif compile_prog "" "$macos_ldflags"; then
5656+
textseg_ldflags="$macos_ldflags"
5657+
elif $ld --verbose >/dev/null 2>&1; then
56265658
$ld --verbose | sed \
56275659
-e '1,/==================================================/d' \
56285660
-e '/==================================================/,$d' \
56295661
-e "s/[.] = [0-9a-fx]* [+] SIZEOF_HEADERS/. = $textseg_addr + SIZEOF_HEADERS/" \
56305662
-e "s/__executable_start = [0-9a-fx]*/__executable_start = $textseg_addr/" > config-host.ld
56315663
textseg_ldflags="-Wl,-T../config-host.ld"
5664+
else
5665+
error_exit \
5666+
"We need to link the QEMU user mode binaries at a" \
5667+
"specific text address. Unfortunately your linker" \
5668+
"doesn't support either the -Ttext-segment option," \
5669+
"-image_base, -image-base, or printing the default" \
5670+
"linker script with --verbose." \
5671+
"If you don't want the user mode binaries, pass the" \
5672+
"--disable-user option to configure."
56325673
fi
56335674
fi
56345675
fi
@@ -6695,6 +6736,7 @@ target_softmmu="no"
66956736
target_user_only="no"
66966737
target_linux_user="no"
66976738
target_bsd_user="no"
6739+
target_darwin_user="no"
66986740
case "$target" in
66996741
${target_name}-softmmu)
67006742
target_softmmu="yes"
@@ -6707,6 +6749,10 @@ case "$target" in
67076749
target_user_only="yes"
67086750
target_bsd_user="yes"
67096751
;;
6752+
${target_name}-darwin-user)
6753+
target_user_only="yes"
6754+
target_darwin_user="yes"
6755+
;;
67106756
*)
67116757
error_exit "Target '$target' not recognised"
67126758
exit 1
@@ -6954,6 +7000,9 @@ fi
69547000
if test "$target_linux_user" = "yes" ; then
69557001
echo "CONFIG_LINUX_USER=y" >> $config_target_mak
69567002
fi
7003+
if test "$target_darwin_user" = "yes" ; then
7004+
echo "CONFIG_DARWIN_USER=y" >> $config_target_mak
7005+
fi
69577006
list=""
69587007
if test ! -z "$gdb_xml_files" ; then
69597008
for x in $gdb_xml_files; do
@@ -7068,7 +7117,7 @@ if test "$gprof" = "yes" ; then
70687117
fi
70697118
fi
70707119

7071-
if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then
7120+
if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" -o "$target_darwin_user" = "yes" ; then
70727121
ldflags="$ldflags $textseg_ldflags"
70737122
fi
70747123

darwin-user/Makefile.objs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
obj-y = main.o syscall.o strace.o mmap.o signal.o \
2+
elfload.o linuxload.o uaccess.o uname.o \
3+
shim_fallocate.o shim_gettid.o shim_timers.o
4+
5+
obj-$(TARGET_HAS_BFLT) += flatload.o
6+
obj-$(TARGET_I386) += vm86.o
7+
obj-$(TARGET_ARM) += arm/nwfpe/
8+
obj-$(TARGET_M68K) += m68k-sim.o

0 commit comments

Comments
 (0)