From d4390c6a1b5f5246e6517684a106062593b7f485 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Wed, 15 Jul 2026 21:20:35 +0000 Subject: [PATCH] ptos/amd64+ke: Add system call groundwork + exit syscall Signed-off-by: Chloe M. --- sdk/head/ptapi/syscall.h | 16 ++++++++++++ service/ptos/arch/amd64/cpu/init.c | 1 + service/ptos/arch/amd64/cpu/vector.S | 7 +++++ service/ptos/arch/amd64/ke/syscall.c | 39 ++++++++++++++++++++++++++++ service/ptos/head/arch/amd64/trap.h | 1 + service/ptos/head/ke/syscall.h | 25 ++++++++++++++++++ service/ptos/head/ps/uapi.h | 22 ++++++++++++++++ service/ptos/ke/syscall.c | 16 ++++++++++++ service/ptos/ps/thread_uapi.c | 19 ++++++++++++++ 9 files changed, 146 insertions(+) create mode 100644 sdk/head/ptapi/syscall.h create mode 100644 service/ptos/arch/amd64/ke/syscall.c create mode 100644 service/ptos/head/ke/syscall.h create mode 100644 service/ptos/head/ps/uapi.h create mode 100644 service/ptos/ke/syscall.c create mode 100644 service/ptos/ps/thread_uapi.c diff --git a/sdk/head/ptapi/syscall.h b/sdk/head/ptapi/syscall.h new file mode 100644 index 0000000..8953bdd --- /dev/null +++ b/sdk/head/ptapi/syscall.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: System calls + * Author: Chloe M. + */ + +#ifndef _PTAPI_SYSCALL_H_ +#define _PTAPI_SYSCALL_H_ 1 + +#define SYS_NOP 0x00 /* Does nothing / reserved */ +#define SYS_EXIT 0x01 /* Exit the current thread */ +#define _SYSCALL_MAX 0x02 /* Always bump when adding new system call */ + +#endif /* !_PTAPI_SYSCALL_H_ */ diff --git a/service/ptos/arch/amd64/cpu/init.c b/service/ptos/arch/amd64/cpu/init.c index 38be50b..cf62781 100644 --- a/service/ptos/arch/amd64/cpu/init.c +++ b/service/ptos/arch/amd64/cpu/init.c @@ -38,6 +38,7 @@ InitInterrupts(VOID) MdIdtSetGate(0x0C, (UPTR)TrapStackSegmentFault, IDT_TRAP_GATE, 0); MdIdtSetGate(0x0D, (UPTR)TrapGeneralProtection, IDT_TRAP_GATE, 0); MdIdtSetGate(0x0E, (UPTR)TrapPageFault, IDT_TRAP_GATE, 0); + MdIdtSetGate(0x2E, (UPTR)TrapLegacySyscall, IDT_USER_GATE, 0); MdIdtLoad(); } diff --git a/service/ptos/arch/amd64/cpu/vector.S b/service/ptos/arch/amd64/cpu/vector.S index e7c5c5a..5f3df13 100644 --- a/service/ptos/arch/amd64/cpu/vector.S +++ b/service/ptos/arch/amd64/cpu/vector.S @@ -151,3 +151,10 @@ LapicTmrIsr: call HalContextSwitch call MdLapicSendEoi INTR_EXIT() + + .globl TrapLegacySyscall +TrapLegacySyscall: + INTR_ENTRY($0x2C) + mov %rsp, %rdi + call MdSyscallDispatch + INTR_EXIT() diff --git a/service/ptos/arch/amd64/ke/syscall.c b/service/ptos/arch/amd64/ke/syscall.c new file mode 100644 index 0000000..731fb11 --- /dev/null +++ b/service/ptos/arch/amd64/ke/syscall.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: System call dispatch + * Author: Chloe M. + */ + +#include +#include + +/* + * 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); +} diff --git a/service/ptos/head/arch/amd64/trap.h b/service/ptos/head/arch/amd64/trap.h index 3bdb0ef..b3d1c07 100644 --- a/service/ptos/head/arch/amd64/trap.h +++ b/service/ptos/head/arch/amd64/trap.h @@ -28,6 +28,7 @@ #define TRAP_STACK_SEG_FAULT 0x0C #define TRAP_GENERAL_PROTECTION 0x0D #define TRAP_PAGE_FAULT 0x0E +#define TRAP_SYSCALL 0x2E /* Prototypes of trap functions */ VOID TrapDivError(VOID); diff --git a/service/ptos/head/ke/syscall.h b/service/ptos/head/ke/syscall.h new file mode 100644 index 0000000..d107f2f --- /dev/null +++ b/service/ptos/head/ke/syscall.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: System calls + * Author: Chloe M. + */ + +#ifndef _KE_SYSCALL_H_ +#define _KE_SYSCALL_H_ 1 + +#include +#include +#include + +/* + * System call arguments passed to system call handlers + */ +typedef struct { + UQUAD Arg[6]; +} SYSCALL_ARGS; + +extern PT_STATUS(*SystemCallTable[_SYSCALL_MAX])(SYSCALL_ARGS *Args); + +#endif /* !_KE_SYSCALL_H_ */ diff --git a/service/ptos/head/ps/uapi.h b/service/ptos/head/ps/uapi.h new file mode 100644 index 0000000..9777129 --- /dev/null +++ b/service/ptos/head/ps/uapi.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Process related user-API + * Author: Chloe M. + */ + +#ifndef _PS_UAPI_H_ +#define _PS_UAPI_H_ 1 + +#include +#include + +/* + * Exit the current thread: + * + * @ARG[0]: Exit status code + */ +PT_STATUS PsSysThreadExit(SYSCALL_ARGS *Args); + +#endif /* !_PS_UAPI_H_ */ diff --git a/service/ptos/ke/syscall.c b/service/ptos/ke/syscall.c new file mode 100644 index 0000000..6186904 --- /dev/null +++ b/service/ptos/ke/syscall.c @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: System calls + * Author: Chloe M. + */ + +#include +#include +#include + +/* System call table */ +PT_STATUS(*SystemCallTable[_SYSCALL_MAX])(SYSCALL_ARGS *Args) = { + [SYS_EXIT] = PsSysThreadExit +}; diff --git a/service/ptos/ps/thread_uapi.c b/service/ptos/ps/thread_uapi.c new file mode 100644 index 0000000..0738d98 --- /dev/null +++ b/service/ptos/ps/thread_uapi.c @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Thread related user-API + * Author: Chloe M. + */ + +#include +#include + +#include + +PT_STATUS +PsSysThreadExit(SYSCALL_ARGS *Args) +{ + PsExitThread(Args->Arg[0]); + UNREACHABLE(); +}