/*
 * Copyright (c) 2026, Chloe M.
 * Provided under the BSD-3 clause.
 *
 * Description: System call dispatch
 * Author:      Chloe M.
 */

#include <ke/syscall.h>
#include <machine/frame.h>

/*
 * Dispatch incoming system calls
 *
 * @Frame:  Trapframe snapshot
 */
VOID
MdSyscallDispatch(TRAP_FRAME *Frame)
{
    SYSCALL_ARGS ScArgs;

    if (Frame == NULL) {
        return;
    }

    /* Is this system call number valid? */
    if (Frame->Rax == 0 || Frame->Rax >= _SYSCALL_MAX) {
        Frame->Rax = STATUS_INVALID_PARAM;
        return;
    }

    ScArgs.Arg[0] = Frame->Rdi;
    ScArgs.Arg[1] = Frame->Rsi;
    ScArgs.Arg[2] = Frame->Rdx;
    ScArgs.Arg[3] = Frame->Rcx;
    ScArgs.Arg[4] = Frame->R8;
    ScArgs.Arg[5] = Frame->R9;
    SystemCallTable[Frame->Rax](&ScArgs);
}
