From c53533a539638c1c0ad4f4c99c091ae7972cfd8b Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 15 Oct 2011 08:24:34 +0800 Subject: [PATCH] Usermode/ld-acess - Syscall stub changes - SYS_CLONE stack switch moved to usermode - ARMv7 syscall fixes --- .../Libraries/ld-acess.so_src/arch/armv7.S.h | 46 +++++++++++++++++-- .../Libraries/ld-acess.so_src/arch/x86.asm.h | 10 ++++ .../ld-acess.so_src/arch/x86_64.asm.h | 12 +++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h b/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h index 75c56d8f..85a3ac69 100644 --- a/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h +++ b/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h @@ -7,7 +7,7 @@ .globl _start .extern SoMain _start: - push {r1,r2,r3} + pop {r0} bl SoMain mov r4, r0 @@ -58,19 +58,59 @@ _errno: .long 0 @ Placed in .text, to allow use of relative addressing mov pc, lr .endm +.macro syscall5 _name, _num +.globl \_name +\_name: + push {r4} + ldr r4, [sp,#4] + svc #\_num + str r2, _errno + pop {r4} + mov pc, lr +.endm + +.macro syscall6 _name, _num +.globl \_name +\_name: + push {r4,r5} + ldr r4, [sp,#8] + ldr r5, [sp,#12] + svc #\_num + str r2, _errno + pop {r4,r5} + mov pc, lr +.endm + #define SYSCALL0(_name,_num) syscall0 _name, _num #define SYSCALL1(_name,_num) SYSCALL0(_name, _num) #define SYSCALL2(_name,_num) SYSCALL0(_name, _num) #define SYSCALL3(_name,_num) SYSCALL0(_name, _num) #define SYSCALL4(_name,_num) SYSCALL0(_name, _num) // TODO: 5/6 need special handling, because the args are on the stack -#define SYSCALL5(_name,_num) SYSCALL0(_name, _num) -#define SYSCALL6(_name,_num) SYSCALL0(_name, _num) +#define SYSCALL5(_name,_num) syscall5 _name, _num +#define SYSCALL6(_name,_num) syscall6 _name, _num // Override the clone syscall #define _exit _exit_raw +#define _clone _clone_raw #include "syscalls.s.h" #undef _exit +#undef _clone + +.globl _clone +_clone: + push {r4} + mov r4, r1 + svc #SYS_CLONE + str r2, _errno + tst r4, r4 + beq _clone_ret + @ If in child, set SP + tst r0,r0 + movne sp, r4 +_clone_ret: + pop {r4} + mov pc, lr .globl _exit _exit: diff --git a/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h b/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h index a569637d..4de25277 100644 --- a/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h +++ b/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h @@ -161,6 +161,7 @@ clone: push ebp mov ebp, esp push ebx + push edx mov ebx, [ebp+12] ; Get new stack pointer @@ -192,11 +193,20 @@ clone: sub ebx, 12 %endif .doCall: + mov edx, ebx ; Save new stack mov eax, SYS_CLONE mov ecx, ebx ; Stack mov ebx, [ebp+8] ; Flags SYSCALL_OP mov [_errno], ebx + + test eax, eax + jnz .ret + test edx, edx + jz .ret + mov esp, edx +.ret: + pop edx pop ebx pop ebp ret diff --git a/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h b/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h index ea605ef6..62fc400a 100644 --- a/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h +++ b/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h @@ -152,6 +152,9 @@ clone: push rbp mov rbp, rsp push rbx + push r11 + + mov r12, rsi ; Save in a reg for after the clone ; Check if the new stack is being used test rsi, rsi @@ -166,10 +169,19 @@ clone: mov eax, SYS_CLONE SYSCALL_OP mov [rel _errno], ebx + + ; Change stack pointer + test eax, eax + jnz .ret + test r12, r12 + jz .ret + mov rsp, rsi +.ret: pop rbx pop rbp ret + [global _exit:func] _exit: xor eax, eax -- 2.20.1