kernel
Loading...
Searching...
No Matches
setup.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 syscall setup in x86.
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/mt.h"
21#include "../../includes/mm.h"
22#include "../../includes/ps.h"
23
24// From syscall.asm
25extern void MtSyscallEntry(void);
26
27// The SSDT
29
30typedef struct {
31 uint8_t Num;
32 void* Handler;
34
35// TODO Proper SSDT with offsets to handlers from SSDT base instead of raw pointers (for security)
36// Along with validating that the handler is in the .text section of the kernel
37// and idk implement patchguard on the way
38// patchguard works by queuing DPCs and KTIMERs, not by making a system thread
39// (so its always hidden), honestly microsoft engineers are brilliant.
41 // Syscalls are here.
42 {.Num = 0, .Handler = MtAllocateVirtualMemory},
43 {.Num = 1, .Handler = MtOpenProcess},
44 {.Num = 2, .Handler = MtTerminateProcess},
45 {.Num = 3, .Handler = MtReadFile},
46 {.Num = 4, .Handler = MtWriteFile},
47 {.Num = 5, .Handler = MtCreateFile},
48 {.Num = 6, .Handler = MtClose},
49 {.Num = 7, .Handler = MtTerminateThread},
50};
51
53
54void
56 void
57)
58
59{
60 // Write the Code Segment selectors into the STAR msr.
61 uint64_t STAR = ((uint64_t)KERNEL_CS << 32) | ((uint64_t)(USER_DS - 8) << 48);
62 __writemsr(IA32_STAR, STAR);
63
64 // Write the syscall entrypoint to LSTAR msr.
66
67 // Write the FMASK (flag mask) MSR to flag IF and TF.
68 __writemsr(IA32_FMASK, (1 << 8) | (1 << 9));
69
70 // Write the current processor to IA32_KERNEL_GS_BASE for swapgs
72
73 // Setup list of syscalls.
74 if (!InterlockedFetch8((volatile int8_t*) & SyscallsAlreadyInitialized)) {
75 for (size_t i = 0; i < sizeof(SyscallTable) / sizeof(SyscallTable[0]); i++) {
76 Ssdt[SyscallTable[i].Num] = SyscallTable[i].Handler;
77 }
78 // BSP Should run this first, no need for interlocked.
80 }
81
82 // Enable SysCallEnable (SCE) in EFER.
83 uint64_t EFER = __readmsr(MSR_EFER);
84 EFER |= 1; // EFER.SCE
85 __writemsr(MSR_EFER, EFER);
86}
FORCEINLINE int8_t InterlockedFetch8(volatile int8_t *target)
Definition atomic.h:213
SyscallHandler Ssdt[]
Definition setup.c:28
FORCEINLINE uint64_t __readmsr(uint32_t msr)
Definition intrin.h:209
FORCEINLINE void __writemsr(uint32_t msr, uint64_t value)
Definition intrin.h:215
#define IA32_KERNEL_GS_BASE
Definition intrin.h:19
#define IA32_FMASK
Definition intrin.h:26
#define IA32_LSTAR
Definition intrin.h:24
#define IA32_STAR
Definition intrin.h:23
#define USER_DS
Definition me.h:246
#define KERNEL_CS
Definition me.h:243
@ MSR_EFER
Definition mh.h:172
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
void MtSetupSyscall(void)
Definition setup.c:55
SYSCALL_INIT_ENTRY SyscallTable[]
Definition setup.c:40
bool SyscallsAlreadyInitialized
Definition setup.c:52
void MtSyscallEntry(void)
void * Handler
Definition setup.c:32
uint8_t Num
Definition setup.c:31
MTSTATUS MtReadFile(IN HANDLE FileHandle, IN uint64_t FileOffset, OUT void *Buffer, IN size_t BufferSize, _Out_Opt size_t *BytesRead)
MTSTATUS MtTerminateProcess(IN HANDLE ProcessHandle, IN MTSTATUS ExitStatus)
MTSTATUS MtAllocateVirtualMemory(IN HANDLE ProcessHandle, _In_Opt _Out_Opt void **BaseAddress, IN size_t NumberOfBytes, IN uint8_t AllocationType)
Definition systemcalls.c:29
MTSTATUS MtClose(IN HANDLE hObject)
MTSTATUS MtOpenProcess(IN uint32_t ProcessId, OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess)
MTSTATUS MtTerminateThread(IN HANDLE ThreadHandle, IN MTSTATUS ExitStatus)
MTSTATUS MtWriteFile(IN HANDLE FileHandle, IN uint64_t FileOffset, IN void *Buffer, IN size_t BufferSize, _Out_Opt size_t *BytesWritten)
MTSTATUS MtCreateFile(IN const char *path, IN ACCESS_MASK DesiredAccess, OUT PHANDLE FileHandleOut)