From 542043e654cabbd5db639cce81fa777e0d2aec63 Mon Sep 17 00:00:00 2001 From: "Thomas A." Date: Sat, 3 Feb 2024 16:52:21 -0800 Subject: [PATCH] Fixup Code That Calls __darling_handle_svc * If not already done, add code to save and restore `fp` & `lr`. * Update code that wasn't changed to call `__darling_handle_svc` * Rework changes made to the macros. --- gen/bsdsyscalls/SYS.h | 17 ++++++++++++++--- gen/bsdsyscalls/___vfork.S | 2 ++ libsyscall/custom/SYS.h | 17 ++++++++++++++--- libsyscall/custom/__fork.S | 4 ++++ libsyscall/custom/__vfork.S | 6 ++++++ libsyscall/custom/custom.S | 12 ++++++++++-- osfmk/mach/arm/syscall_sw.h | 21 +++++++++++++++++---- 7 files changed, 67 insertions(+), 12 deletions(-) diff --git a/gen/bsdsyscalls/SYS.h b/gen/bsdsyscalls/SYS.h index 3366b5b..8494849 100644 --- a/gen/bsdsyscalls/SYS.h +++ b/gen/bsdsyscalls/SYS.h @@ -475,13 +475,23 @@ pseudo: ;\ * TBD */ +#ifdef DARLING #define DO_SYSCALL(num, cerror) \ mov x16, #(num) %%\ -#ifdef DARLING %%\ + PUSH_FRAME %%\ bl __darling_handle_svc %%\ -#else %%\ + POP_FRAME %%\ + b.cc 2f %%\ + ARM64_STACK_PROLOG %%\ + PUSH_FRAME %%\ + bl _##cerror %%\ + POP_FRAME %%\ + ARM64_STACK_EPILOG %%\ +2: +#else +#define DO_SYSCALL(num, cerror) \ + mov x16, #(num) %%\ svc #SWI_SYSCALL %%\ -#endif %%\ b.cc 2f %%\ ARM64_STACK_PROLOG %%\ PUSH_FRAME %%\ @@ -489,6 +499,7 @@ pseudo: ;\ POP_FRAME %%\ ARM64_STACK_EPILOG %%\ 2: +#endif #define MI_GET_ADDRESS(reg,var) \ adrp reg, var@page %%\ diff --git a/gen/bsdsyscalls/___vfork.S b/gen/bsdsyscalls/___vfork.S index c754db6..97b31c1 100644 --- a/gen/bsdsyscalls/___vfork.S +++ b/gen/bsdsyscalls/___vfork.S @@ -232,7 +232,9 @@ Ltry_set_vfork: // ARM sets r1 to 1 here. I don't see why. mov w16, #SYS_vfork // Set syscall code #ifdef DARLING + PUSH_FRAME bl __darling_handle_svc + POP_FRAME #else svc #SWI_SYSCALL #endif diff --git a/libsyscall/custom/SYS.h b/libsyscall/custom/SYS.h index 3366b5b..8494849 100644 --- a/libsyscall/custom/SYS.h +++ b/libsyscall/custom/SYS.h @@ -475,13 +475,23 @@ pseudo: ;\ * TBD */ +#ifdef DARLING #define DO_SYSCALL(num, cerror) \ mov x16, #(num) %%\ -#ifdef DARLING %%\ + PUSH_FRAME %%\ bl __darling_handle_svc %%\ -#else %%\ + POP_FRAME %%\ + b.cc 2f %%\ + ARM64_STACK_PROLOG %%\ + PUSH_FRAME %%\ + bl _##cerror %%\ + POP_FRAME %%\ + ARM64_STACK_EPILOG %%\ +2: +#else +#define DO_SYSCALL(num, cerror) \ + mov x16, #(num) %%\ svc #SWI_SYSCALL %%\ -#endif %%\ b.cc 2f %%\ ARM64_STACK_PROLOG %%\ PUSH_FRAME %%\ @@ -489,6 +499,7 @@ pseudo: ;\ POP_FRAME %%\ ARM64_STACK_EPILOG %%\ 2: +#endif #define MI_GET_ADDRESS(reg,var) \ adrp reg, var@page %%\ diff --git a/libsyscall/custom/__fork.S b/libsyscall/custom/__fork.S index 9f4de9d..f80854b 100644 --- a/libsyscall/custom/__fork.S +++ b/libsyscall/custom/__fork.S @@ -154,7 +154,11 @@ MI_ENTRY_POINT(___fork) PUSH_FRAME // ARM moves a 1 in to r1 here, but I can't see why. mov x16, #SYS_fork // Syscall code +#ifdef DARLING + bl __darling_handle_svc +#else svc #SWI_SYSCALL // Trap to kernel +#endif b.cs Lbotch // Carry bit indicates failure cbz x1, Lparent // x1 == 0 indicates that we are the parent diff --git a/libsyscall/custom/__vfork.S b/libsyscall/custom/__vfork.S index 969c188..db408d5 100644 --- a/libsyscall/custom/__vfork.S +++ b/libsyscall/custom/__vfork.S @@ -231,7 +231,13 @@ Ltry_set_vfork: // ARM sets r1 to 1 here. I don't see why. mov w16, #SYS_vfork // Set syscall code +#ifdef DARLING + PUSH_FRAME + bl __darling_handle_svc + POP_FRAME +#else svc #SWI_SYSCALL +#endif b.cs Lbotch cbz w1, Lparent diff --git a/libsyscall/custom/custom.S b/libsyscall/custom/custom.S index 5bb30ce..7dc3506 100644 --- a/libsyscall/custom/custom.S +++ b/libsyscall/custom/custom.S @@ -137,14 +137,22 @@ __thread_set_tsd_base: .align 2 .globl __thread_set_tsd_base __thread_set_tsd_base: +#ifdef DARLING + stp fp, lr, [sp, #-16]! + mov fp, sp + mov x3, #2 mov x16, #0x80000000 -#ifdef DARLING bl __darling_handle_svc + + ldp fp, lr, [sp], #16 + ret #else + mov x3, #2 + mov x16, #0x80000000 svc #SWI_SYSCALL -#endif ret +#endif #else #error unknown architecture diff --git a/osfmk/mach/arm/syscall_sw.h b/osfmk/mach/arm/syscall_sw.h index f078f12..4c0e7e7 100644 --- a/osfmk/mach/arm/syscall_sw.h +++ b/osfmk/mach/arm/syscall_sw.h @@ -106,19 +106,32 @@ #elif defined(__arm64__) #include +#ifdef DARLING +// For PUSH_FRAME & POP_FRAME +#include +#endif +#ifdef DARLING +#define kernel_trap(trap_name, trap_number, num_args) \ +.globl _##trap_name %% \ +.text %% \ +.align 2 %% \ +_##trap_name: %% \ + PUSH_FRAME %% \ + mov x16, #(trap_number) %% \ + bl __darling_handle_svc %% \ + POP_FRAME %% \ + ret +#else #define kernel_trap(trap_name, trap_number, num_args) \ .globl _##trap_name %% \ .text %% \ .align 2 %% \ _##trap_name: %% \ mov x16, #(trap_number) %% \ -#ifdef DARLING %% \ - bl __darling_handle_svc %% \ -#else %% \ svc #SWI_SYSCALL %% \ + ret #endif - ret #else #error Unsupported architecture