/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright © 2026 Keith Packard
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

        .section .text.syscall
        .globl syscall
        .type  syscall, @function
syscall:
#ifdef __x86_64

        # x86_64 calling convention
        #
        #       rdi     syscall
        #       rsi     arg0
        #       rdx     arg1
        #       rcx     arg2
        #       r8      arg3
        #       r9      arg4
        #       8(rsp)  arg5
        #
        # Registers passed to kernel
        #
        #       rax     syscall
        #       rcx     return address (handled by syscall instruction)
        #       r11     saved rflags (handled by syscall instruction)
        #       rdi     arg0
        #       rsi     arg1
        #       rdx     arg2
        #       r10     arg3 (can't use rcx because of syscall instruction)
        #       r8      arg4
        #       r9      arg5
        #       (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
        #

        # Shuffle args to match Linux

	movq %rdi, %rax         # syscall
	movq %rsi, %rdi         # arg0
	movq %rdx, %rsi         # arg1
	movq %rcx, %rdx         # arg2
	movq %r8, %r10          # arg3
	movq %r9, %r8           # arg4
	movq 8(%rsp),%r9        # arg5
        syscall
        cmpq $-4095, %rax
        jae 0f
        ret
0:      
        movq %rax, %rdi
        jmp  _syscall_error
#else
        pushl %ebp
        pushl %edi
        pushl %esi
        pushl %edx
        pushl %ecx
        pushl %ebx

        movl 52(%esp), %ebp
        movl 48(%esp), %edi
        movl 44(%esp), %esi
        movl 40(%esp), %edx
        movl 36(%esp), %ecx
        movl 32(%esp), %ebx
        movl 28(%esp), %eax

        int $0x80
        
        popl %ebx
        popl %ecx
        popl %edx
        popl %esi
        popl %edi
        popl %ebp

        cmpl $-4095, %eax
        jae 0f
        ret
0:
        movl %eax,4(%esp)
        jmp _syscall_error
#endif
        .size syscall, . - syscall
