From f23416a40c5d56ff4eec5bad8d30857cb18bcedd Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sat, 21 Oct 2023 22:13:38 +0000 Subject: [PATCH] Don't mess up the stack pointer, wtf is this calling convention --- include/linux/aarch64/syscall.h | 4 +- lib/linux/aarch64-mes-m2/_write.c | 17 +++- lib/linux/aarch64-mes-m2/syscall.c | 133 ++++++++++++++++++++++++----- lib/m2/aarch64/aarch64_defs.M1 | 6 +- 4 files changed, 128 insertions(+), 32 deletions(-) diff --git a/include/linux/aarch64/syscall.h b/include/linux/aarch64/syscall.h index 2153abac..1ab906de 100644 --- a/include/linux/aarch64/syscall.h +++ b/include/linux/aarch64/syscall.h @@ -42,8 +42,8 @@ #define SYS_fchmodat 53 // CONSTANT SYS_faccessat 48 #define SYS_faccessat 48 -// CONSTANT SYS_brk 234 -#define SYS_brk 234 +// CONSTANT SYS_brk 214 +#define SYS_brk 214 // CONSTANT SYS_ioctl 29 #define SYS_ioctl 29 // CONSTANT SYS_fsync 82 diff --git a/lib/linux/aarch64-mes-m2/_write.c b/lib/linux/aarch64-mes-m2/_write.c index f25eb557..8e6803f1 100644 --- a/lib/linux/aarch64-mes-m2/_write.c +++ b/lib/linux/aarch64-mes-m2/_write.c @@ -22,9 +22,20 @@ void _write () { - asm ("POP_X2"); - asm ("POP_X1"); - asm ("POP_X0"); + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("DEREF_X0"); + asm ("SET_X2_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SET_X1_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_TO_SYS_WRITE"); asm ("SYSCALL"); } diff --git a/lib/linux/aarch64-mes-m2/syscall.c b/lib/linux/aarch64-mes-m2/syscall.c index d716596e..f9f57e94 100644 --- a/lib/linux/aarch64-mes-m2/syscall.c +++ b/lib/linux/aarch64-mes-m2/syscall.c @@ -26,59 +26,148 @@ int errno; int __sys_call (int sys_call) { - asm ("POP_X8"); + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_FROM_X0"); asm ("SYSCALL"); } int __sys_call1 (int sys_call, int one) { + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); - asm ("POP_X0"); - asm ("POP_X8"); asm ("SYSCALL"); } int __sys_call2 (int sys_call, int one, int two) { - asm ("POP_X1"); - asm ("POP_X0"); - asm ("POP_X8"); + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("DEREF_X0"); + asm ("SET_X1_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SYSCALL"); } int __sys_call3 (int sys_call, int one, int two, int three) { - asm ("POP_X2"); - asm ("POP_X1"); - asm ("POP_X0"); - asm ("POP_X8"); + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X2_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("DEREF_X0"); + asm ("SET_X1_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SYSCALL"); } int __sys_call4 (int sys_call, int one, int two, int three, int four) { - asm ("POP_X3"); - asm ("POP_X2"); - asm ("POP_X1"); - asm ("POP_X0"); - asm ("POP_X8"); + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SET_X3_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X2_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("DEREF_X0"); + asm ("SET_X1_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SYSCALL"); } int __sys_call6 (int sys_call, int one, int two, int three, int four, int five, int six) { - asm ("POP_X5"); - asm ("POP_X4"); - asm ("POP_X3"); - asm ("POP_X2"); - asm ("POP_X1"); - asm ("POP_X0"); - asm ("POP_X8"); + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X8_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_24"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X5_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_24"); + asm ("DEREF_X0"); + asm ("SET_X4_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SET_X3_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("SUB_X0_8"); + asm ("DEREF_X0"); + asm ("SET_X2_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_24"); + asm ("DEREF_X0"); + asm ("SET_X1_FROM_X0"); + + asm ("SET_X0_FROM_BP"); + asm ("SUB_X0_16"); + asm ("DEREF_X0"); + asm ("SYSCALL"); } diff --git a/lib/m2/aarch64/aarch64_defs.M1 b/lib/m2/aarch64/aarch64_defs.M1 index 6ed5be06..020b51c8 100644 --- a/lib/m2/aarch64/aarch64_defs.M1 +++ b/lib/m2/aarch64/aarch64_defs.M1 @@ -26,11 +26,6 @@ DEFINE PUSH_LR 5e8e1ff8 DEFINE POP_X0 408640f8 DEFINE POP_X1 418640f8 -DEFINE POP_X2 428640f8 -DEFINE POP_X3 438640f8 -DEFINE POP_X4 448640f8 -DEFINE POP_X5 458640f8 -DEFINE POP_X8 488640f8 DEFINE POP_X16 508640f8 DEFINE POP_BP 518640f8 DEFINE POP_LR 5e8640f8 @@ -93,6 +88,7 @@ DEFINE SET_X3_FROM_X0 e30300aa DEFINE SET_X4_FROM_X0 e40300aa DEFINE SET_X5_FROM_X0 e50300aa DEFINE SET_X6_FROM_X0 e60300aa +DEFINE SET_X8_FROM_X0 e80300aa DEFINE SET_X16_FROM_X0 f00300aa DEFINE SET_X16_FROM_SP f00312aa DEFINE SET_BP_FROM_X16 f10310aa