AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
syscall.c
Go to the documentation of this file.
1
7
8#include <core/system.h>
9#include <core/arch.h>
10#include <sys/proc.h>
11#include <sys/sched.h>
12#include <sys/syscall.h>
13#include <sys/signal.h>
14#include <aquila/syscall.h>
15#include <aquila/errno.h>
16
17/*
18 * Runs a system call: the interrupt handler calls it for `int $0x80`. It
19 * returns to user mode when the call is done, delivering the signals that
20 * came meanwhile.
21 */
22void arch_syscall(struct x86_regs *r)
23{
24#if ARCH_BITS==32
25 syscall_dispatch(r->eax, r->ebx, r->ecx, r->edx, r->esi, r->edi);
26#else
27 /* the arguments in Linux's registers (r10 instead of rcx, which the
28 * syscall instruction overwrites) */
29 syscall_dispatch(r->rax, r->rdi, r->rsi, r->rdx, r->r10, r->r8);
30#endif
31
32 /* if the call woke up an urgent thread (the network's, for a packet
33 * sent to ourselves), it runs before we return to user mode */
34 if (sched_urgent) {
35 extern void x86_preempt(void);
36 x86_preempt();
37 }
38
39 /* deliver the pending signals, and restart the call if one
40 * interrupted it (not sigreturn, whose registers are the frame's) */
41 x86_return_to_user(r, curthread->syscall_nr != SYS_sigreturn);
42}
43
44/*
45 * Sets the result of a thread's system call: eax (rax) when it returns to
46 * user mode. That's in its saved registers (for a child of `fork()`, its
47 * copy of its parent's), or, for a thread that has never run, where it
48 * will start from.
49 */
51{
52 struct x86_thread *arch = thread->arch;
53
54#if ARCH_BITS==32
55 if (thread->spawned) arch->regs->eax = val;
56 else arch->eax = val;
57#else
58 if (thread->spawned) arch->regs->rax = val;
59 else arch->rax = val;
60#endif
61}
void arch_syscall(struct aarch64_regs *r)
Definition syscall.c:23
void arch_syscall_return(struct thread *thread, uintptr_t val)
Sets the result of a thread's system call, in the register that returns it to user mode.
Definition syscall.c:43
void x86_return_to_user(struct x86_regs *r, int syscall)
Delivers the current process's pending signals on the way back to user mode: stops and default action...
Definition signal.c:246
int sched_urgent
Definition sched.c:306
#define curthread
Definition sched.h:12
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
A thread.
Definition thread.h:23
int spawned
whether it has run yet
Definition thread.h:87
void * arch
the architecture's own data: its registers, its kernel stack
Definition thread.h:84
The registers an interrupt or exception saved, on the kernel stack, as cpu.S's stubs push them: the g...
Definition cpu.h:12
uint64_t rsi
Definition cpu.h:22
uint64_t r10
Definition cpu.h:21
uint64_t rdx
Definition cpu.h:22
uint64_t rax
Definition cpu.h:22
uint64_t rdi
Definition cpu.h:22
uint64_t r8
Definition cpu.h:21
A thread's x86 state: its kernel stack, and where it resumes.
Definition arch.h:16
uintptr_t rax
Definition arch.h:31
struct x86_regs * regs
Definition arch.h:34
void syscall_dispatch(unsigned long nr, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
Runs a system call that the current thread made from user mode.
Definition syscall.c:313