ciabatta/arch/sysv_x86-64/thread-entry.asm

55 lines
1.3 KiB
NASM
Raw Normal View History

bits 64
section .text
2023-09-06 09:37:30 +00:00
global _rt_thread_start
extern _rt_thread_finish
; flags, &stack[-2], &parent_tid, &child_tid, 0
; DESCRIPTION:
; Function to be called when using the _clone syscall
; We don't just call the _clone syscall directly because
; if we call the clone syscall wrapper that's defined in
; linux/sys, our stack will be bad and we won't be able
; to return from the syscall wrapper
; PARAMETERS:
; rdi - flags
; rsi - stack_base
; rdx - parent_tid_ptr
; rcx - child_tid_ptr
; r8 - tls
; RETURNS:
; i64 value
; 0 if returning as a parent
; 1 if returning as a child
; negative value if there was an error making the thread
2023-09-06 09:37:30 +00:00
_rt_thread_start:
mov r10, rcx
2023-08-26 02:53:58 +00:00
; Setup child stack
sub rsi, 24
mov rax, [rsp]
mov [rsi+16], rax
mov rax, [rsp+8]
mov [rsi+8], rax
mov [rsi], r9
; Call syscall right away, since the order of the first 5 arguments
; matches with the argument order of the function
mov rax, 56 ; SYS_CLONE
syscall
; Check to see if we're child
test eax, eax
jnz .exit
2023-08-26 02:53:58 +00:00
; If child, jump to thread function
pop rax ; thread_fn
pop rdi ; ctx
call rax
2023-09-06 09:37:30 +00:00
; Make return value the first arg and call thread finish routine
mov rdi, rax
2023-09-06 09:37:30 +00:00
call _rt_thread_finish
; Just to be sure...
2023-08-26 02:53:58 +00:00
hlt
.exit:
ret