Thanks for the heads up with shmat on 10.13 - I'll see if I can check that with my previous x86 macbook at some point. When I was prodding it I did find that almost anything within the first 4G didn't allocate but > 4G seemed to work. I should dive into it in more detail at some point to see if I could identify the root cause.
The following is the
very basic patch for building Arm64 non-JIT on Apple Silicon (sorry for the inline-paste: I don't see an obvious attachment option). It doesn't contain fixes to do the NATMEM_OFFSET build for x86_64 or the (current set of) race conditions I've found but it shows you where I was heading before the rabbit holing began.
The SLIRP fix is from address-sanitizer and the gfxaccel.cpp fix is needed on Arm64 (otherwise random buffer overruns kill the emulator).
I'll do my best to get the remainder (all of the Dyngen/JIT + race condition fixes) cleaned up and in a public repo ASAP but it will take a while
Code: Select all
diff --git a/BasiliskII/src/CrossPlatform/vm_alloc.cpp b/BasiliskII/src/CrossPlatform/vm_alloc.cpp
index 3aa369dabd..e85a6766ee 100755
--- a/BasiliskII/src/CrossPlatform/vm_alloc.cpp
+++ b/BasiliskII/src/CrossPlatform/vm_alloc.cpp
@@ -229,6 +229,13 @@ void *vm_acquire_reserved(size_t size) {
return reserved_buf && size <= RESERVED_SIZE ? reserved_buf : VM_MAP_FAILED;
}
+int vm_init_reserved(void *hostAddress) {
+ int result = vm_acquire_fixed(hostAddress, RESERVED_SIZE);
+ if (result >= 0)
+ reserved_buf = hostAddress;
+ return result;
+}
+
/* Allocate zero-filled memory of SIZE bytes. The mapping is private
and default protection bits are read / write. The return value
is the actual mapping address chosen or VM_MAP_FAILED for errors. */
diff --git a/BasiliskII/src/slirp/slirp.c b/BasiliskII/src/slirp/slirp.c
index 4aad7ba45a..45c97612c7 100755
--- a/BasiliskII/src/slirp/slirp.c
+++ b/BasiliskII/src/slirp/slirp.c
@@ -87,7 +87,7 @@ static int get_dns_addr(struct in_addr *pdns_addr)
static int get_dns_addr(struct in_addr *pdns_addr)
{
char buff[512];
- char buff2[256];
+ char buff2[257];
FILE *f;
int found = 0;
struct in_addr tmp_addr;
diff --git a/SheepShaver/src/MacOSX/Info.plist.in b/SheepShaver/src/MacOSX/Info.plist.in
index dfe702403c..a70e1ec95a 100644
--- a/SheepShaver/src/MacOSX/Info.plist.in
+++ b/SheepShaver/src/MacOSX/Info.plist.in
@@ -39,10 +39,6 @@
<string>@PACKAGE_VERSION@</string>
<key>CSResourcesFileMapped</key>
<true/>
- <key>LSArchitecturePriority</key>
- <array>
- <string>x86_64</string>
- </array>
<key>LSMinimumSystemVersion</key>
<string>10.7.0</string>
<key>NSHighResolutionCapable</key>
diff --git a/SheepShaver/src/MacOSX/SheepShaver_Xcode8.xcodeproj/project.pbxproj b/SheepShaver/src/MacOSX/SheepShaver_Xcode8.xcodeproj/project.pbxproj
index 8002534c90..46958ce618 100755
--- a/SheepShaver/src/MacOSX/SheepShaver_Xcode8.xcodeproj/project.pbxproj
+++ b/SheepShaver/src/MacOSX/SheepShaver_Xcode8.xcodeproj/project.pbxproj
@@ -1210,7 +1210,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.7;
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
PRODUCT_NAME = kpx_cpu;
- VALID_ARCHS = x86_64;
+ VALID_ARCHS = "x86_64 arm64";
};
name = Debug;
};
@@ -1249,7 +1249,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.7;
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
PRODUCT_NAME = kpx_cpu;
- VALID_ARCHS = x86_64;
+ VALID_ARCHS = "x86_64 arm64";
};
name = Release;
};
@@ -1329,14 +1329,12 @@
OTHER_CFLAGS = "";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = (
- "-pagezero_size",
- 0x3000,
"-lkpx_cpu",
);
PRECOMPS_INCLUDE_HEADERS_FROM_BUILT_PRODUCTS_DIR = NO;
PRODUCT_BUNDLE_IDENTIFIER = net.cebix.sheepshaver;
PRODUCT_NAME = SheepShaver;
- VALID_ARCHS = x86_64;
+ VALID_ARCHS = "x86_64 arm64";
WARNING_LDFLAGS = "";
};
name = Debug;
@@ -1395,14 +1393,12 @@
OTHER_CFLAGS = "";
OTHER_CPLUSPLUSFLAGS = "$(OTHER_CFLAGS)";
OTHER_LDFLAGS = (
- "-pagezero_size",
- 0x3000,
"-lkpx_cpu",
);
PRECOMPS_INCLUDE_HEADERS_FROM_BUILT_PRODUCTS_DIR = NO;
PRODUCT_BUNDLE_IDENTIFIER = net.cebix.sheepshaver;
PRODUCT_NAME = SheepShaver;
- VALID_ARCHS = x86_64;
+ VALID_ARCHS = "x86_64 arm64";
};
name = Release;
};
diff --git a/SheepShaver/src/MacOSX/config/config-macosx-aarch64.h b/SheepShaver/src/MacOSX/config/config-macosx-aarch64.h
index aa04b6a952..c904f67182 100644
--- a/SheepShaver/src/MacOSX/config/config-macosx-aarch64.h
+++ b/SheepShaver/src/MacOSX/config/config-macosx-aarch64.h
@@ -405,8 +405,8 @@
ordering is the same as for multi-word integers. */
/* #undef HOST_FLOAT_WORDS_BIG_ENDIAN */
-/* Define constant offset for Mac address translation */
-/* #undef NATMEM_OFFSET */
+/* Define constant offset for Mac address translation: macosx-aarch64 is always 64bit */
+#define NATMEM_OFFSET 0x400000000000
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "Christian.Bauer@uni-mainz.de"
@@ -428,7 +428,7 @@
/* Define if the __PAGEZERO Mach-O Low Memory Globals hack works on this
system. */
-#define PAGEZERO_HACK 1
+/* #define PAGEZERO_HACK 1 */
/* Define as the return type of signal handlers (`int' or `void'). */
#define RETSIGTYPE void
diff --git a/SheepShaver/src/Unix/configure.ac b/SheepShaver/src/Unix/configure.ac
index 35f3495e63..3b3480ca46 100755
--- a/SheepShaver/src/Unix/configure.ac
+++ b/SheepShaver/src/Unix/configure.ac
@@ -1640,13 +1640,13 @@ if [[ "x$EMULATED_PPC" = "xyes" ]]; then
esac
fi
if [[ "x$have_dyngen_gcc3" = "xyes" ]]; then
- DYNGEN_OP_FLAGS="$DYNGEN_OP_FLAGS -fno-align-functions"
+ DYNGEN_OP_FLAGS="$DYNGEN_OP_FLAGS -fno-align-functions -fno-stack-protector"
else
DYNGEN_OP_FLAGS="$DYNGEN_OP_FLAGS -malign-functions=0"
fi
DYNGEN_OP_FLAGS="$DYNGEN_OP_FLAGS -finline-functions -finline-limit=10000 -fno-exceptions -g0"
if [[ "x$have_dyngen_gcc3" = "xyes" ]]; then
- DYNGEN_OP_FLAGS="$DYNGEN_OP_FLAGS -fno-reorder-blocks -fno-optimize-sibling-calls"
+ DYNGEN_OP_FLAGS="$DYNGEN_OP_FLAGS -fno-reorder-blocks -fno-optimize-sibling-calls -fno-reorder-blocks-and-partition"
fi
if [[ "x$DYNGEN_CC" != "x$CXX" ]]; then
DYNGEN_CFLAGS="-O2 $CFLAGS"
diff --git a/SheepShaver/src/Unix/main_unix.cpp b/SheepShaver/src/Unix/main_unix.cpp
index 0c5b76a1e5..5a470e6cb7 100755
--- a/SheepShaver/src/Unix/main_unix.cpp
+++ b/SheepShaver/src/Unix/main_unix.cpp
@@ -2210,6 +2210,7 @@ rti:;
}
#endif
+extern int vm_init_reserved(void *hostAddress);
/*
* Helpers to share 32-bit addressable data with MacOS
@@ -2221,7 +2222,15 @@ bool SheepMem::Init(void)
page_size = getpagesize();
// Allocate SheepShaver globals
+#ifdef NATMEM_OFFSET
+ if (vm_mac_acquire_fixed(ROM_BASE + ROM_AREA_SIZE + SIG_STACK_SIZE, size) < 0)
+ return false;
+ uint8 *adr = Mac2HostAddr(ROM_BASE + ROM_AREA_SIZE + SIG_STACK_SIZE);
+ if (vm_init_reserved(adr + size) < 0)
+ return false;
+#else
uint8 *adr = vm_mac_acquire(size);
+#endif
if (adr == VM_MAP_FAILED)
return false;
proc = base = Host2MacAddr(adr);
diff --git a/SheepShaver/src/gfxaccel.cpp b/SheepShaver/src/gfxaccel.cpp
index ed65e5d835..ec1e96a32c 100644
--- a/SheepShaver/src/gfxaccel.cpp
+++ b/SheepShaver/src/gfxaccel.cpp
@@ -85,7 +85,7 @@ static inline void do_invrect(uint8 *dest, uint32 length)
}
// Align on 32-bit boundaries
- if (bpp < 32 && (((uintptr)dest) & 2)) {
+ if (bpp < 32 && (((uintptr)dest) & 2) && length >= 2) {
INVERT_2(dest, 0);
dest += 2; length -= 2;
}
@@ -198,7 +198,7 @@ static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
}
// Align on 32-bit boundaries
- if (bpp < 32 && (((uintptr)dest) & 2)) {
+ if (bpp < 32 && (((uintptr)dest) & 2) && length >= 2) {
FILL_2(dest, 0, color);
dest += 2; length -= 2;
}
diff --git a/SheepShaver/src/kpx_cpu/src/cpu/jit/dyngen.c b/SheepShaver/src/kpx_cpu/src/cpu/jit/dyngen.c
index b6ead79221..68be566eb1 100644
--- a/SheepShaver/src/kpx_cpu/src/cpu/jit/dyngen.c
+++ b/SheepShaver/src/kpx_cpu/src/cpu/jit/dyngen.c
@@ -2396,6 +2396,7 @@ void patch_relocations(FILE *outfile, const char *name, host_ulong size, host_ul
fprintf(outfile, " *(uint32_t *)(code_ptr() + %d) = (int32_t)%s + %d;\n", slide, final_sym_name, addend);
break;
case R_X86_64_PC32:
+ case R_X86_64_PLT32:
fprintf(outfile, " *(uint32_t *)(code_ptr() + %d) = %s - (long)(code_ptr() + %d) + %d;\n",
slide, final_sym_name, slide, addend);
break;
diff --git a/SheepShaver/src/kpx_cpu/src/cpu/vm.hpp b/SheepShaver/src/kpx_cpu/src/cpu/vm.hpp
index 5c0b733076..c01dc6ac83 100755
--- a/SheepShaver/src/kpx_cpu/src/cpu/vm.hpp
+++ b/SheepShaver/src/kpx_cpu/src/cpu/vm.hpp
@@ -205,13 +205,13 @@ const uintptr VMBaseDiff = NATMEM_OFFSET;
#if REAL_ADDRESSING || DIRECT_ADDRESSING
static inline uint8 * vm_do_get_real_address(vm_addr_t addr)
{
- uintptr a = vm_wrap_address(VMBaseDiff + addr);
+ uintptr a = vm_wrap_address(addr);
#if defined(__APPLE__) && defined(__x86_64__)
extern uint8 gZeroPage[0x3000], gKernelData[0x2000];
if (a < 0x3000) return &gZeroPage[a];
else if ((a & ~0x1fff) == 0x68ffe000 || (a & ~0x1fff) == 0x5fffe000) return &gKernelData[a & 0x1fff];
#endif
- return (uint8 *)a;
+ return (uint8 *)(VMBaseDiff + a);
}
static inline vm_addr_t vm_do_get_virtual_address(uint8 *addr)
{