PatchworkOS
Loading...
Searching...
No Matches
Syscalls

System Calls. More...

Data Structures

struct  syscall_ctx_t
 Per thread syscall context. More...
 
struct  syscall_descriptor_t
 A syscall descriptor. More...
 

Macros

#define SYS_PROCESS_EXIT   0
 
#define SYS_THREAD_EXIT   1
 
#define SYS_SPAWN   2
 
#define SYS_NANOSLEEP   3
 
#define SYS_ERRNO   4
 
#define SYS_GETPID   5
 
#define SYS_GETTID   6
 
#define SYS_UPTIME   7
 
#define SYS_UNIX_EPOCH   8
 
#define SYS_OPEN   9
 
#define SYS_OPEN2   10
 
#define SYS_CLOSE   11
 
#define SYS_READ   12
 
#define SYS_WRITE   13
 
#define SYS_SEEK   14
 
#define SYS_IOCTL   15
 
#define SYS_CHDIR   16
 
#define SYS_POLL   17
 
#define SYS_STAT   18
 
#define SYS_MMAP   19
 
#define SYS_MUNMAP   20
 
#define SYS_MPROTECT   21
 
#define SYS_GETDENTS   22
 
#define SYS_THREAD_CREATE   23
 
#define SYS_YIELD   24
 
#define SYS_DUP   25
 
#define SYS_DUP2   26
 
#define SYS_FUTEX   27
 
#define SYS_REMOVE   28
 
#define SYS_LINK   29
 
#define SYS_SHARE   30
 
#define SYS_CLAIM   31
 
#define SYS_BIND   32
 
#define SYS_TOTAL_AMOUNT   33
 
#define SYSCALL_DEFINE(num, returnType, ...)
 Macro to define a syscall.
 

Functions

void syscall_entry (void)
 Assembly entry point for syscalls.
 
void syscall_ctx_init (syscall_ctx_t *ctx, stack_pointer_t *kernelStack)
 Initialize a per-thread syscall context.
 
void syscall_ctx_load (syscall_ctx_t *ctx)
 Load a syscall context into the CPU.
 
void syscall_table_init (void)
 Initialize the syscall table.
 
void syscalls_cpu_init (void)
 Initalize syscalls on the current CPU.
 
uint64_t syscall_handler (uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8, uint64_t r9, uint64_t number)
 C syscall handler.
 

Variables

syscall_descriptor_t _syscallTableStart []
 Linker defined start of the syscall table.
 
syscall_descriptor_t _syscallTableEnd []
 Linker defined end of the syscall table.
 

Detailed Description

System Calls.

System calls provide a controlled interface for user-space applications to request services from the kernel, such as file operations, process management, and inter-process communication.

See also
SYSCALL instruction
SYSRET instruction

Macro Definition Documentation

◆ SYS_BIND

#define SYS_BIND   32

Definition at line 54 of file syscalls.h.

◆ SYS_CHDIR

#define SYS_CHDIR   16

Definition at line 38 of file syscalls.h.

◆ SYS_CLAIM

#define SYS_CLAIM   31

Definition at line 53 of file syscalls.h.

◆ SYS_CLOSE

#define SYS_CLOSE   11

Definition at line 33 of file syscalls.h.

◆ SYS_DUP

#define SYS_DUP   25

Definition at line 47 of file syscalls.h.

◆ SYS_DUP2

#define SYS_DUP2   26

Definition at line 48 of file syscalls.h.

◆ SYS_ERRNO

#define SYS_ERRNO   4

Definition at line 26 of file syscalls.h.

◆ SYS_FUTEX

#define SYS_FUTEX   27

Definition at line 49 of file syscalls.h.

◆ SYS_GETDENTS

#define SYS_GETDENTS   22

Definition at line 44 of file syscalls.h.

◆ SYS_GETPID

#define SYS_GETPID   5

Definition at line 27 of file syscalls.h.

◆ SYS_GETTID

#define SYS_GETTID   6

Definition at line 28 of file syscalls.h.

◆ SYS_IOCTL

#define SYS_IOCTL   15

Definition at line 37 of file syscalls.h.

◆ SYS_LINK

#define SYS_LINK   29

Definition at line 51 of file syscalls.h.

◆ SYS_MMAP

#define SYS_MMAP   19

Definition at line 41 of file syscalls.h.

◆ SYS_MPROTECT

#define SYS_MPROTECT   21

Definition at line 43 of file syscalls.h.

◆ SYS_MUNMAP

#define SYS_MUNMAP   20

Definition at line 42 of file syscalls.h.

◆ SYS_NANOSLEEP

#define SYS_NANOSLEEP   3

Definition at line 25 of file syscalls.h.

◆ SYS_OPEN

#define SYS_OPEN   9

Definition at line 31 of file syscalls.h.

◆ SYS_OPEN2

#define SYS_OPEN2   10

Definition at line 32 of file syscalls.h.

◆ SYS_POLL

#define SYS_POLL   17

Definition at line 39 of file syscalls.h.

◆ SYS_PROCESS_EXIT

#define SYS_PROCESS_EXIT   0

Definition at line 22 of file syscalls.h.

◆ SYS_READ

#define SYS_READ   12

Definition at line 34 of file syscalls.h.

◆ SYS_REMOVE

#define SYS_REMOVE   28

Definition at line 50 of file syscalls.h.

◆ SYS_SEEK

#define SYS_SEEK   14

Definition at line 36 of file syscalls.h.

◆ SYS_SHARE

#define SYS_SHARE   30

Definition at line 52 of file syscalls.h.

◆ SYS_SPAWN

#define SYS_SPAWN   2

Definition at line 24 of file syscalls.h.

◆ SYS_STAT

#define SYS_STAT   18

Definition at line 40 of file syscalls.h.

◆ SYS_THREAD_CREATE

#define SYS_THREAD_CREATE   23

Definition at line 45 of file syscalls.h.

◆ SYS_THREAD_EXIT

#define SYS_THREAD_EXIT   1

Definition at line 23 of file syscalls.h.

◆ SYS_TOTAL_AMOUNT

#define SYS_TOTAL_AMOUNT   33

Definition at line 56 of file syscalls.h.

◆ SYS_UNIX_EPOCH

#define SYS_UNIX_EPOCH   8

Definition at line 30 of file syscalls.h.

◆ SYS_UPTIME

#define SYS_UPTIME   7

Definition at line 29 of file syscalls.h.

◆ SYS_WRITE

#define SYS_WRITE   13

Definition at line 35 of file syscalls.h.

◆ SYS_YIELD

#define SYS_YIELD   24

Definition at line 46 of file syscalls.h.

◆ SYSCALL_DEFINE

#define SYSCALL_DEFINE (   num,
  returnType,
  ... 
)
Value:
returnType syscall_handler_##num(__VA_ARGS__); \
const syscall_descriptor_t __syscall_##num __attribute__((used, section(".syscall_table"))) = { \
.number = (num), \
.handler = (void*)syscall_handler_##num, \
}; \
returnType syscall_handler_##num(__VA_ARGS__)
A syscall descriptor.
Definition syscalls.h:75

Macro to define a syscall.

Uses the linker section to define a system call in the syscall table.

Parameters
numThe syscall number, must be unique, check include/kernel/syscalls.h for existing numbers.
returnTypeThe return type of the syscall handler.
...The arguments of the syscall handler, can be no more then 6 arguments. To avoid using the stack to pass arguments to a syscall.

Definition at line 100 of file syscalls.h.

Function Documentation

◆ syscall_ctx_init()

void syscall_ctx_init ( syscall_ctx_t ctx,
stack_pointer_t kernelStack 
)

Initialize a per-thread syscall context.

Parameters
ctxThe syscall context to initialize.
kernelStackThe kernel stack of the thread.

Definition at line 50 of file syscalls.c.

References syscall_ctx_t::kernelRsp, stack_pointer_t::top, and syscall_ctx_t::userRsp.

Referenced by thread_init().

◆ syscall_ctx_load()

void syscall_ctx_load ( syscall_ctx_t ctx)

Load a syscall context into the CPU.

Definition at line 56 of file syscalls.c.

References MSR_GS_BASE, MSR_KERNEL_GS_BASE, and msr_write().

Referenced by thread_load().

◆ syscall_entry()

void syscall_entry ( void  )
extern

Assembly entry point for syscalls.

Responsible for switching stacks.

Referenced by syscalls_cpu_init().

◆ syscall_handler()

uint64_t syscall_handler ( uint64_t  rdi,
uint64_t  rsi,
uint64_t  rdx,
uint64_t  rcx,
uint64_t  r8,
uint64_t  r9,
uint64_t  number 
)

C syscall handler.

This is called from the assembly syscall_entry() function.

Parameters
rdiFirst argument.
rsiSecond argument.
rdxThird argument.
rcxFourth argument.
r8Fifth argument.
r9Sixth argument.
numberThe syscall number.
Returns
The return value of the syscall.

Definition at line 72 of file syscalls.c.

References ENOSYS, ERR, errno, syscall_descriptor_t::handler, INTERRUPT_NOTE, lapic_self_id(), lapic_send_ipi(), LOG_DEBUG, note_queue_length(), NULL, sched_thread_unsafe(), and syscall_get_descriptor().

◆ syscall_table_init()

void syscall_table_init ( void  )

Initialize the syscall table.

This will sort the syscall table and verify that all syscalls are present.

Definition at line 23 of file syscalls.c.

References _syscallTableEnd, _syscallTableStart, assert, LOG_INFO, qsort(), SYS_TOTAL_AMOUNT, and syscall_descriptor_cmp().

Referenced by init_finalize().

◆ syscalls_cpu_init()

void syscalls_cpu_init ( void  )

Initalize syscalls on the current CPU.

This will setup the MSR registers required for syscalls.

Definition at line 39 of file syscalls.c.

References EFER_SYSCALL_ENABLE, GDT_KERNEL_CODE, GDT_RING3, GDT_USER_CODE, MSR_EFER, MSR_LSTAR, msr_read(), MSR_STAR, MSR_SYSCALL_FLAG_MASK, msr_write(), RFLAGS_AUX_CARRY, RFLAGS_DIRECTION, RFLAGS_INTERRUPT_ENABLE, RFLAGS_IOPL, RFLAGS_NESTED_TASK, RFLAGS_TRAP, and syscall_entry().

Referenced by cpu_init().

Variable Documentation

◆ _syscallTableEnd

syscall_descriptor_t _syscallTableEnd[]
extern

Linker defined end of the syscall table.

Referenced by syscall_table_init().

◆ _syscallTableStart

syscall_descriptor_t _syscallTableStart[]
extern

Linker defined start of the syscall table.

Referenced by syscall_get_descriptor(), and syscall_table_init().