From 9f2b7319db5b9cf157a30c1f8c943817f25f0c82 Mon Sep 17 00:00:00 2001
From: Hill Ma <maahiuzeon@gmail.com>
Date: Sun, 29 Nov 2020 13:52:46 -0800
Subject: [PATCH] mach: macos: preliminary support for darwin arm64.

---
 configure.ac          |  5 ++++-
 src/handler-macos.c   |  2 +-
 src/machfault-macos.h | 27 +++++++++++++++++++++++++++
 src/stackvma-mach.c   |  2 +-
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index e632307..2ee25cb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -487,10 +487,13 @@ if test -z "$CFG_FAULT"; then
   case "$host_os" in
     macos* | darwin[[6-9]]* | darwin[[1-9]][[0-9]]*)
       case "$host_cpu" in
-        powerpc* | rs6000 | i?86 | x86_64)
+        powerpc* | rs6000 | i?86 | x86_64 | aarch64)
           CFG_MACHFAULT=machfault-macos.h
           FAULT_CONTEXT_INCLUDE='#include <signal.h>
 #include <mach/thread_status.h>
+#if defined __arm64__
+#define sigsegv_thread_state_t arm_thread_state64_t
+#endif
 #if defined __ppc64__
 #define sigsegv_thread_state_t ppc_thread_state64_t
 #elif defined __ppc__
diff --git a/src/handler-macos.c b/src/handler-macos.c
index 5593fc8..3c78010 100644
--- a/src/handler-macos.c
+++ b/src/handler-macos.c
@@ -490,7 +490,7 @@ sigsegv_leave_handler (void (*continuation) (void*, void*, void*),
           return 0;
         }
 
-#if defined __ppc64__ || defined __ppc__ || defined __x86_64__
+#if defined __arm64__ || defined __ppc64__ || defined __ppc__ || defined __x86_64__
       /* Store arguments in registers.  */
       SIGSEGV_INTEGER_ARGUMENT_1 (thread_state) = (uintptr_t) cont_arg1;
       SIGSEGV_INTEGER_ARGUMENT_2 (thread_state) = (uintptr_t) cont_arg2;
diff --git a/src/machfault-macos.h b/src/machfault-macos.h
index 3fb49d7..40d8ab7 100644
--- a/src/machfault-macos.h
+++ b/src/machfault-macos.h
@@ -15,6 +15,33 @@
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
+#if defined __arm64__
+
+#define SIGSEGV_EXC_STATE_TYPE                      arm_exception_state64_t
+#define SIGSEGV_EXC_STATE_FLAVOR                    ARM_EXCEPTION_STATE64
+#define SIGSEGV_EXC_STATE_COUNT                     ARM_EXCEPTION_STATE64_COUNT
+#define SIGSEGV_THREAD_STATE_TYPE                   arm_thread_state64_t
+#define SIGSEGV_THREAD_STATE_FLAVOR                 ARM_THREAD_STATE64
+#define SIGSEGV_THREAD_STATE_COUNT                  ARM_THREAD_STATE64_COUNT
+
+#if MacOS_X_10_5_HEADERS && __DARWIN_UNIX03
+#define SIGSEGV_FAULT_ADDRESS(thr_state,exc_state)  (exc_state).__far
+#define SIGSEGV_STACK_POINTER(thr_state)            (thr_state).__sp
+#define SIGSEGV_PROGRAM_COUNTER(thr_state)          (thr_state).__pc
+#define SIGSEGV_INTEGER_ARGUMENT_1(thr_state)       (thr_state).__x[0]
+#define SIGSEGV_INTEGER_ARGUMENT_2(thr_state)       (thr_state).__x[1]
+#define SIGSEGV_INTEGER_ARGUMENT_3(thr_state)       (thr_state).__x[2]
+#else
+#define SIGSEGV_FAULT_ADDRESS(thr_state,exc_state)  (exc_state).far
+#define SIGSEGV_STACK_POINTER(thr_state)            (thr_state).sp
+#define SIGSEGV_PROGRAM_COUNTER(thr_state)          (thr_state).pc
+#define SIGSEGV_INTEGER_ARGUMENT_1(thr_state)       (thr_state).x[0]
+#define SIGSEGV_INTEGER_ARGUMENT_2(thr_state)       (thr_state).x[1]
+#define SIGSEGV_INTEGER_ARGUMENT_3(thr_state)       (thr_state).x[2]
+#endif
+
+#endif
+
 #if defined __ppc__ || defined __ppc64__
 
 #if defined __ppc64__
diff --git a/src/stackvma-mach.c b/src/stackvma-mach.c
index 6340da2..fff6760 100644
--- a/src/stackvma-mach.c
+++ b/src/stackvma-mach.c
@@ -74,7 +74,7 @@ sigsegv_get_vma (uintptr_t req_address, struct vma_struct *vma)
          In 64-bit processes, we could use vm_region_64 or mach_vm_region.
          I choose vm_region_64 because it uses the same types as vm_region,
          resulting in less conditional code.  */
-# if defined __ppc64__ || defined __x86_64__
+# if defined __arm64__ || defined __ppc64__ || defined __x86_64__
       struct vm_region_basic_info_64 info;
       mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
 
-- 
2.24.3 (Apple Git-128)

