I'm attempting to write a compiler in x86 Intel noprefix assembly, but I've come across an issue while implementing threads. If I run the following, the new thread runs correctly, albeit with a seg_fault.
call create_thread_stack
push rax # Push f onto new stack, followed by thread_wrapper
pop rsi # New thread should start in thread_wrapper when cloned
sub rsi, 8
lea rdi, [rip + f]
push rdi
pop [rsi]
sub rsi, 8
lea rdi, [rip + thread_wrapper]
push rdi
pop [rsi]
mov rdi, -2147479808 # CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_IO
mov rax, 56 # Sys_clone
syscall
But nothing runs in the new thread if I follow the above with this:
sub rsp, 4
mov dword ptr [rsp], eax
mov eax, dword ptr [rsp]
add rsp, 4
mov dword ptr [rbp + -4], eax
mov rax, 0
mov rsp, rbp
pop rbp
ret
I'm unsure about what I can do to fix this, and would really appreciate any help. I'm not that familiar with X86, so I'm almost certainly missing something. I've put the file contents below here. Thank you!
.intel_syntax noprefix
.globl main
.section .rodata
.text
f: # prints 1 and returns 0
push rbp
mov rbp, rsp
mov eax, 1
sub rsp, 4
mov dword ptr [rsp], eax
mov edi, dword ptr [rsp]
add rsp, 4
movsx rdi, edi
call _printi
mov eax, 0
sub rsp, 4
mov dword ptr [rsp], eax
mov eax, dword ptr [rsp]
add rsp, 4
movsx rax, eax
mov rsp, rbp
pop rbp
ret
main:
push rbp
mov rbp, rsp
sub rsp, 4
mov dword ptr [rsp], eax
call create_thread_stack
push rax # Push f onto new stack, followed by thread_wrapper
pop rsi # New thread should start in thread_wrapper when cloned
sub rsi, 8
lea rdi, [rip + f]
push rdi
pop [rsi]
sub rsi, 8
lea rdi, [rip + thread_wrapper]
push rdi
pop [rsi]
mov rdi, -2147479808 # CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_IO
mov rax, 56 # Sys_clone
syscall
sub rsp, 4
mov dword ptr [rsp], eax
mov eax, dword ptr [rsp]
add rsp, 4
mov dword ptr [rbp + -4], eax
mov rax, 0
mov rsp, rbp
pop rbp
ret
.section .rodata
.int 2
.L._printi_str0:
.asciz "%d"
.text
_printi:
push rbp
mov rbp, rsp
and rsp, -16
mov esi, edi
lea rdi, [rip + .L._printi_str0]
mov al, 0
call printf@plt
mov rdi, 0
call fflush@plt
mov rsp, rbp
pop rbp
ret
create_thread_stack: # Creates stack for new thread
mov rdi, 0
mov rsi, 4194304
mov rdx, 3
mov r10, 290
mov r9, 0
mov r8, -1
mov rax, 9
syscall
lea rax, [rax + 4194304]
ret
thread_exit:
mov rdi, 0
mov rax, 60
syscall
thread_wrapper: # Runs label below thread_wrapper on the new stack
pop rax
call rax
call thread_exit