kernel
Loading...
Searching...
No Matches
handler.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 syscall.c
6
7Purpose:
8
9 This module contains the implementation of the syscall C handler.
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/mt.h"
20#include "../../includes/me.h"
21#include "../../includes/ps.h"
22#include "../../mtstatus.h"
23
24extern SyscallHandler Ssdt[];
25
26void
28 IN PTRAP_FRAME TrapFrame
29)
30
31/*++
32
33 Routine description:
34
35 Handles a system call from user mode.
36
37 Arguments:
38
39 The TRAP_FRAME of syscall entry.
40
41 Return Values:
42
43 Decided by the system call, could be void, or MTSTATUS (zero extended to RAX)
44
45 Notes:
46
47 This function must be called ONLY FROM the MtSyscallEntry routine in assembly.
48
49--*/
50
51{
52 // Set previous mode to user mode, this is a system call.
54
55 // Increment system call count (cool)
56 InterlockedIncrementU64((volatile uint64_t*)&MeGetCurrentProcessor()->SystemCallCount);
57
58 // Just for future incase. (this must be kept here since after interrupts are enabled UserRsp could very much change)
59 // DO NOT Access the RSP in PTRAP_FRAME, it does not exist.
60 // And DO NOT grab the UserRsp after this sti call, as it may change immediately even, save it to a local.
61 //uint64_t* UserStack = (uint64_t*)MeGetCurrentProcessor()->UserRsp;
62
63 // Enable interrupts, its safe now.
64 __sti();
65 // Grab arguments
66 // The return value is stored in RAX, and I dont want to do more assembly
67 // Spare me.
68 uint64_t* ReturnValue = &TrapFrame->rax;
69
70 // Syscall number is in RAX.
71 uint64_t SyscallNumber = TrapFrame->rax;
72
73 // >= because 256 is an invalid index in the array (0-255)
74 if (SyscallNumber >= MAX_SYSCALLS || Ssdt[SyscallNumber] == NULL) {
75 *ReturnValue = MT_INVALID_SYSTEM_SERVICE;
76 return;
77 }
78
79 // Arugments are in RDI RSI RDX R10 (not RCX in Syscalls, since CPU clobbers it for RIP) R8 R9
80 // Above 6 arguments we receive from user stack.
81 // For now, support 6.
82 // To support more args we need a syscall that actually takes more than 6 args
83 uint64_t Arg1 = TrapFrame->rdi;
84 uint64_t Arg2 = TrapFrame->rsi;
85 uint64_t Arg3 = TrapFrame->rdx;
86 uint64_t Arg4 = TrapFrame->r10;
87 uint64_t Arg5 = TrapFrame->r8;
88 uint64_t Arg6 = TrapFrame->r9;
89
90 // Todo regular SSDT. (with limits, no direct indexing)
91 *ReturnValue = Ssdt[SyscallNumber](Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
92}
#define IN
Definition annotations.h:8
FORCEINLINE uint64_t InterlockedIncrementU64(volatile uint64_t *target)
Definition atomic.h:125
TRAP_FRAME * PTRAP_FRAME
Definition core.h:56
void MtSyscallHandler(IN PTRAP_FRAME TrapFrame)
Definition handler.c:27
SyscallHandler Ssdt[]
Definition setup.c:28
FORCEINLINE void __sti(void)
Definition intrin.h:59
FORCEINLINE PITHREAD MeGetCurrentThread(void)
Definition me.h:444
FORCEINLINE PPROCESSOR MeGetCurrentProcessor(void)
Definition me.h:369
@ UserMode
Definition mm.h:372
uint64_t(* SyscallHandler)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t)
Definition mt.h:26
#define MAX_SYSCALLS
Definition mt.h:25
#define MT_INVALID_SYSTEM_SERVICE
Definition mtstatus.h:142
enum _PRIVILEGE_MODE PreviousMode
Definition me.h:274