Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Darwin User #4

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.objs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ trace-events-subdirs += target/s390x
trace-events-subdirs += target/ppc
trace-events-subdirs += qom
trace-events-subdirs += linux-user
trace-events-subdirs += darwin-user
trace-events-subdirs += qapi
trace-events-subdirs += accel/tcg
trace-events-subdirs += accel/kvm
Expand Down
14 changes: 14 additions & 0 deletions Makefile.target
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ obj-y += gdbstub.o

endif #CONFIG_BSD_USER

#########################################################
# macOS user emulator target

ifdef CONFIG_DARWIN_USER

QEMU_CFLAGS+=-I$(SRC_PATH)/darwin-user/$(TARGET_ABI_DIR) \
-I$(SRC_PATH)/darwin-user/host/$(ARCH) \
-I$(SRC_PATH)/darwin-user

obj-y += darwin-user/
obj-y += gdbstub.o thunk.o

endif #CONFIG_DARWIN_USER

#########################################################
# System emulator target
ifdef CONFIG_SOFTMMU
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
This QEMU patch introduces irix/solaris userland emulation. It currently runs
only under linux (though BSD support would probably be feasable).
only under linux and macOS (though BSD support would probably be feasable).

### compiling

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

```
configure --target-list=irix-linux-user,irixn32-linux-user,irix64-linux-user,solaris-linux-user
make && make install
```

Or, configure QEMU for irix userland emulatin for macOS

```
./configure --target-list=irix-darwin-user \
--disable-vnc \
--disable-sdl \
--disable-gtk \
--disable-cocoa \
--disable-opengl \
--disable-capstone \
--disable-hax \
--disable-hvf \
--disable-tools
make
```

### using

I recommend using binfmt. I have prepared some scripts for this which you can
Expand Down
26 changes: 24 additions & 2 deletions accel/tcg/translate-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ static void page_init(void)
#else
FILE *f;

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

f = fopen("/compat/linux/proc/self/maps", "r");
if (f) {
Expand Down Expand Up @@ -602,8 +603,27 @@ static inline void *split_cross_256mb(void *buf1, size_t size1)
return buf1;
}
#endif

#ifdef USE_STATIC_CODE_GEN_BUFFER
#if defined(CONFIG_DARWIN_USER)
static inline void *alloc_code_gen_buffer(void)
{
int prot = PROT_WRITE | PROT_READ | PROT_EXEC;
int flags = MAP_PRIVATE | MAP_ANON;
/* let a kernel pick an address close to the executable */
uintptr_t start = 0;
/* Honor a command-line option limiting the size of the buffer. */
size_t size = DEFAULT_CODE_GEN_BUFFER_SIZE;
void *buf;

buf = mmap((void *)start, size, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
return NULL;
}

qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
return buf;
}
#else /* !CONFIG_DARWIN_USER */
static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
__attribute__((aligned(CODE_GEN_ALIGN)));

Expand Down Expand Up @@ -640,6 +660,8 @@ static inline void *alloc_code_gen_buffer(void)

return buf;
}
#endif /* CONFIG_DARWIN_USER */

#elif defined(_WIN32)
static inline void *alloc_code_gen_buffer(void)
{
Expand Down
5 changes: 5 additions & 0 deletions accel/tcg/user-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ int cpu_signal_handler(int host_signum, void *pinfo,
#define TRAP_sig(context) ((context)->sc_trapno)
#define ERROR_sig(context) ((context)->sc_err)
#define MASK_sig(context) ((context)->sc_mask)
#elif defined(__APPLE__)
#define PC_sig(context) ((context)->uc_mcontext->__ss.__rip)
#define TRAP_sig(context) ((context)->uc_mcontext->__es.__trapno)
#define ERROR_sig(context) ((context)->uc_mcontext->__es.__err)
#define MASK_sig(context) ((context)->uc_sigmask)
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <ucontext.h>

Expand Down
81 changes: 65 additions & 16 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ supported_target() {
return 1
fi
;;
*-darwin-user)
if test "$darwin" != "yes"; then
print_error "Target '$target' is only available on a Darwin host"
return 1
fi
;;
*)
print_error "Invalid target name '$target'"
return 1
Expand Down Expand Up @@ -390,6 +396,7 @@ cocoa="no"
softmmu="yes"
linux_user="no"
bsd_user="no"
darwin_user="no"
blobs="yes"
pkgversion=""
pie=""
Expand Down Expand Up @@ -766,6 +773,7 @@ OpenBSD)
Darwin)
bsd="yes"
darwin="yes"
darwin_user="yes"
hax="yes"
hvf="yes"
LDFLAGS_SHARED="-bundle -undefined dynamic_lookup"
Expand Down Expand Up @@ -1119,6 +1127,7 @@ for opt do
--disable-user)
linux_user="no" ;
bsd_user="no" ;
darwin_user="no" ;
;;
--enable-user) ;;
--disable-linux-user) linux_user="no"
Expand All @@ -1129,6 +1138,10 @@ for opt do
;;
--enable-bsd-user) bsd_user="yes"
;;
--disable-darwin-user) darwin_user="no"
;;
--enable-darwin-user) darwin_user="yes"
;;
--enable-pie) pie="yes"
;;
--disable-pie) pie="no"
Expand Down Expand Up @@ -1439,6 +1452,7 @@ QEMU_CFLAGS="$CPU_CFLAGS $QEMU_CFLAGS"
if [ "$ARCH" = "unknown" ]; then
bsd_user="no"
linux_user="no"
darwin_user="no"
fi

default_target_list=""
Expand All @@ -1454,6 +1468,10 @@ fi
if [ "$bsd_user" = "yes" ]; then
mak_wilds="${mak_wilds} $source_path/default-configs/*-bsd-user.mak"
fi
if [ "$darwin_user" = "yes" ]; then
mak_wilds="${mak_wilds} $source_path/default-configs/*-darwin-user.mak"
fi


for config in $mak_wilds; do
default_target_list="${default_target_list} $(basename "$config" .mak)"
Expand Down Expand Up @@ -1548,6 +1566,7 @@ disabled with --disable-FEATURE, default is enabled if available:
user supported user emulation targets
linux-user all linux usermode emulation targets
bsd-user all BSD usermode emulation targets
darwin-user all MacOS usermode emulation targets
docs build documentation
guest-agent build the QEMU Guest Agent
guest-agent-msi build guest agent Windows MSI installation package
Expand Down Expand Up @@ -5577,7 +5596,7 @@ if test "$cpu" = "s390x" ; then
fi

# Probe for the need for relocating the user-only binary.
if ( [ "$linux_user" = yes ] || [ "$bsd_user" = yes ] ) && [ "$pie" = no ]; then
if ( [ "$linux_user" = yes ] || [ "$bsd_user" = yes ] || [ "$darwin_user" = yes ] ) && [ "$pie" = no ]; then
textseg_addr=
case "$cpu" in
arm | i386 | ppc* | s390* | sparc* | x86_64 | x32)
Expand All @@ -5590,31 +5609,53 @@ if ( [ "$linux_user" = yes ] || [ "$bsd_user" = yes ] ) && [ "$pie" = no ]; then
textseg_addr=0x60000000
;;
esac

if [ -n "$textseg_addr" ]; then
cat > $TMPC <<EOF
int main(void) { return 0; }
EOF
textseg_ldflags="-Wl,-Ttext-segment=$textseg_addr"
if ! compile_prog "" "$textseg_ldflags"; then
# In case ld does not support -Ttext-segment, edit the default linker
# script via sed to set the .text start addr. This is needed on FreeBSD
# at least.
if ! $ld --verbose >/dev/null 2>&1; then
error_exit \
"We need to link the QEMU user mode binaries at a" \
"specific text address. Unfortunately your linker" \
"doesn't support either the -Ttext-segment option or" \
"printing the default linker script with --verbose." \
"If you don't want the user mode binaries, pass the" \
"--disable-user option to configure."
fi
# 64bit macOS reserves 4GB for page zero to catch truncated pointer to int casts.
# Follow suggested Wine configuration from:
# https://stackoverflow.com/questions/46916112/osx-ld-why-does-pagezero-size-default-to-4gb-on-64b-osx
if [ "$darwin_user" = yes ] ; then
pz_size_flag=",-pagezero_size,0x1000"
else
pz_size_flag=""
fi

# Check three different sets of ld flags:
# default_... for standard gnu ld (Linux)
# clang_... for llvm clang
# macos_... for macOS built-in ld
# If none of the above options are supported, edit the default linker
# script via sed to set the .text start addr. This is needed on FreeBSD
# at least.
default_ldflags="-Wl,-Ttext-segment=$textseg_addr"
clang_ldflags="-Wl,-image-base,${textseg_addr}${pz_size_flag}"
macos_ldflags="-Wl,-image_base,${textseg_addr}${pz_size_flag}"

if compile_prog "" "$default_ldflags"; then
textseg_ldflags="$default_ldflags"
elif compile_prog "" "$clang_ldflags"; then
textseg_ldflags="$clang_ldflags"
elif compile_prog "" "$macos_ldflags"; then
textseg_ldflags="$macos_ldflags"
elif $ld --verbose >/dev/null 2>&1; then
$ld --verbose | sed \
-e '1,/==================================================/d' \
-e '/==================================================/,$d' \
-e "s/[.] = [0-9a-fx]* [+] SIZEOF_HEADERS/. = $textseg_addr + SIZEOF_HEADERS/" \
-e "s/__executable_start = [0-9a-fx]*/__executable_start = $textseg_addr/" > config-host.ld
textseg_ldflags="-Wl,-T../config-host.ld"
else
error_exit \
"We need to link the QEMU user mode binaries at a" \
"specific text address. Unfortunately your linker" \
"doesn't support either the -Ttext-segment option," \
"-image_base, -image-base, or printing the default" \
"linker script with --verbose." \
"If you don't want the user mode binaries, pass the" \
"--disable-user option to configure."
fi
fi
fi
Expand Down Expand Up @@ -6677,6 +6718,7 @@ target_softmmu="no"
target_user_only="no"
target_linux_user="no"
target_bsd_user="no"
target_darwin_user="no"
case "$target" in
${target_name}-softmmu)
target_softmmu="yes"
Expand All @@ -6689,6 +6731,10 @@ case "$target" in
target_user_only="yes"
target_bsd_user="yes"
;;
${target_name}-darwin-user)
target_user_only="yes"
target_darwin_user="yes"
;;
*)
error_exit "Target '$target' not recognised"
exit 1
Expand Down Expand Up @@ -6936,6 +6982,9 @@ fi
if test "$target_linux_user" = "yes" ; then
echo "CONFIG_LINUX_USER=y" >> $config_target_mak
fi
if test "$target_darwin_user" = "yes" ; then
echo "CONFIG_DARWIN_USER=y" >> $config_target_mak
fi
list=""
if test ! -z "$gdb_xml_files" ; then
for x in $gdb_xml_files; do
Expand Down Expand Up @@ -7050,7 +7099,7 @@ if test "$gprof" = "yes" ; then
fi
fi

if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then
if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" -o "$target_darwin_user" = "yes" ; then
ldflags="$ldflags $textseg_ldflags"
fi

Expand Down
8 changes: 8 additions & 0 deletions darwin-user/Makefile.objs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
obj-y = main.o syscall.o strace.o mmap.o signal.o \
elfload.o linuxload.o uaccess.o uname.o \
shim_fallocate.o shim_gettid.o shim_timers.o

obj-$(TARGET_HAS_BFLT) += flatload.o
obj-$(TARGET_I386) += vm86.o
obj-$(TARGET_ARM) += arm/nwfpe/
obj-$(TARGET_M68K) += m68k-sim.o
Loading