.global syscall_entry

syscall_entry:
    swapgs

    push %rcx
    push %r11

    # Save original args before we clobber them
    push %rdi        # orig rdi (arg1)
    push %rsi        # orig rsi (arg2)
    push %rdx        # orig rdx (arg3)

    # Handle r8/r9 before r8 gets clobbered
    mov %r8,  %r9    # r9  = orig r8  (6th param)
    mov %r10, %r8    # r8  = orig r10 (5th param)

    # Pop in reverse order to place into correct SysV registers
    pop  %rcx        # rcx = orig rdx (4th param)
    pop  %rdx        # rdx = orig rsi (3rd param)
    pop  %rsi        # rsi = orig rdi (2nd param)
    mov  %rax, %rdi  # rdi = rax      (1st param = syscall number)

    call syscall_handler

    pop %r11
    pop %rcx

    swapgs
    sysretq