I am working on a syscall handler function for an Aarch64 arm bit cpu, and I was looking at how it is done in x86 assembly, but I am unable to figure out how it would be done in Aarch64 assembly.
I was looking at this example on github: https://github.com/rockytriton/LLD/blob/main/linux_os/part1/src/start.S which is written in x86 assembly.
.globl _syscall
_syscall:
movq %rdi, %rax
movq %rsi, %rdi
movq %rdx, %rsi
movq %rcx, %rdx
movq %r8, %r10
movq %r9, %r8
movq 8(%rsp), %r9
syscall
ret
And some online equivalents as shown in this answer do not satisfy the same function call design. Which is written in Aarch64 assembly.
/* Generated by gensyscalls.py. Do not edit. */
#include <private/bionic_asm.h>
.hidden __set_errno
ENTRY(write)
mov x8, __NR_write
svc #0
cmn x0, #(MAX_ERRNO + 1)
cneg x0, x0, hi
b.hi __set_errno
ret
END(write)
So far I have this code (poorly ported from x86 to Aarch64):
.globl _syscall
_syscall:
mov x8, r7
svc #0
cmn x0, #(4095 + 1)
cneg x0, x0, hi
ret
It does not work, but I have tried nonetheless, ironically when I assemble it, it doesn't seem to like the register name of r7
, I don't exactly understand why, as that should be a parameter of the function call (see below).
I have a layout for the function in a header file for my C program as so: unsigned long _syscall(int num, void *a0, void *a1, void *a2, void *a3, void *a4, void *a5)
, would anyone have any ideas on how to recreate the same syscall handler functionality in Aarch64 assembly - my attempts in porting across have yielded unsuccessful. I'm rather lost on this, as assembly isn't my strong point - ironically this is the only bit of assembly I need in my project.
Many thanks!