The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
syscall_internal.h
Go to the documentation of this file.
1#pragma once
2
3#include "arch/x86/cpu/isr.h"
4#include "kernel/errno.h"
5#include "common/stdint.h"
6#include "common/error.h"
9
10/**
11 * one syscall argument or return value: pointer-width, so a user address
12 * passes through unchanged. handlers cast arguments to what they mean.
13 */
15
16/**
17 * read syscall argument n (0-based). the only place that knows the register
18 * convention: int 0x80 on i686 passes EBX, ECX, EDX, ESI, EDI; syscall on
19 * x86_64 passes RDI, RSI, RDX, R10, R8, R9 (RCX holds the return address).
20 * @param regs the trap frame of the syscall.
21 * @param n argument index, 0 to 4 (0 to 5 on x86_64).
22 * @return the argument.
23 */
24static inline syscall_arg_t syscall_arg(const struct registers *regs, int n)
25{
26 switch (n)
27 {
28#ifdef __x86_64__
29 case 0:
30 return regs->rdi;
31 case 1:
32 return regs->rsi;
33 case 2:
34 return regs->rdx;
35 case 3:
36 return regs->r10;
37 case 4:
38 return regs->r8;
39 default:
40 return regs->r9;
41#else
42 case 0:
43 return regs->ebx;
44 case 1:
45 return regs->ecx;
46 case 2:
47 return regs->edx;
48 case 3:
49 return regs->esi;
50 default:
51 return regs->edi;
52#endif
53 }
54}
55
56/**
57 * a 64-bit file offset, passed as two arguments: the low 32 bits in
58 * argument n and the high 32 bits in n + 1. both architectures use this
59 * form, so i686 can pass the whole range.
60 * @param regs the trap frame of the syscall.
61 * @param n the argument with the low half.
62 * @return the offset.
63 */
64static inline int64_t syscall_arg_offset(const struct registers *regs, int n)
65{
66 uint64_t low = (uint32_t)syscall_arg(regs, n);
67 uint64_t high = (uint32_t)syscall_arg(regs, n + 1);
68 return (int64_t)(low | (high << 32));
69}
70
71/**
72 * syscall function pointer type. a handler reads its arguments with
73 * syscall_arg and returns its result; the dispatcher puts the result in
74 * REG_RESULT (EAX or RAX).
75 */
77typedef syscall_ret_t (*syscall_fn_t)(struct registers *regs);
78
79
80/**
81 * copy a path string in from user memory and resolve it against the current
82 * process's cwd.
83 * @param user_path the user's path pointer.
84 * @param resolved receives the absolute path.
85 * @param size size of resolved.
86 * @return 0, or a negative errno (EFAULT for a bad pointer, EINVAL for a
87 * path too long or unresolvable).
88 */
89int32_t resolve_user_path(const char *user_path, char *resolved, uint32_t size);
90
91// the *at calls: a relative path in the working directory, or in an open one
92#define AT_FDCWD (-100)
93#define AT_SYMLINK_NOFOLLOW 0x100 // the link itself, not what it points at
94#define AT_REMOVEDIR 0x200 // unlinkat: a directory (rmdir)
95#define AT_SYMLINK_FOLLOW 0x400 // linkat: what the link points at
96#define AT_EACCESS 0x200 // faccessat: with the effective ids
97
98/**
99 * as resolve_user_path, with a relative path taken in the directory open
100 * as dirfd (AT_FDCWD: the working directory). the symbolic links along the
101 * path are replaced by their targets.
102 * @param follow also the last name, when it is a link (stat, open); not for
103 * the calls that act on the name itself (unlink, rename, lstat).
104 * @return 0, or -EFAULT, -ENAMETOOLONG, -ENOENT (an empty path), -EBADF,
105 * -ENOTDIR (dirfd is no directory), or -ELOOP.
106 */
107int32_t resolve_user_path_at(int32_t dirfd, const char *user_path, char *resolved, uint32_t size,
108 bool follow);
109
110/**
111 * give a file the caller has just created its mode (taken through the
112 * umask) and the caller's effective user and group as owner.
113 * @param path the new file's absolute path.
114 * @param mode the permission bits asked for.
115 * @return 0, or a negative errno.
116 */
117int32_t set_new_owner(const char *path, uint32_t mode);
118
119/* sys_io.c */
120syscall_ret_t sys_write(struct registers *regs);
121syscall_ret_t sys_read(struct registers *regs);
122syscall_ret_t sys_open(struct registers *regs);
123syscall_ret_t sys_flock(struct registers *regs);
125syscall_ret_t sys_close(struct registers *regs);
126syscall_ret_t sys_lseek(struct registers *regs);
128syscall_ret_t sys_dup2(struct registers *regs);
129syscall_ret_t sys_pipe(struct registers *regs);
130
131/* sys_fs.c */
132syscall_ret_t sys_stat(struct registers *regs);
133syscall_ret_t sys_mkdir(struct registers *regs);
135syscall_ret_t sys_rmdir(struct registers *regs);
147syscall_ret_t sys_clone(struct registers *regs);
152syscall_ret_t sys_futex(struct registers *regs);
154syscall_ret_t sys_poll(struct registers *regs);
155
156/* sys_cred.c */
165syscall_ret_t sys_chmod(struct registers *regs);
167syscall_ret_t sys_chown(struct registers *regs);
171syscall_ret_t sys_umask(struct registers *regs);
172
173/* kernel/time/clock.c */
177syscall_ret_t sys_times(struct registers *regs);
178
179/* kernel/signal/signal.c */
180syscall_ret_t sys_kill(struct registers *regs);
186syscall_ret_t sys_alarm(struct registers *regs);
187
188/* sys_proc.c */
189syscall_ret_t sys_exit(struct registers *regs);
190syscall_ret_t sys_exec(struct registers *regs);
193syscall_ret_t sys_sbrk(struct registers *regs);
194syscall_ret_t sys_mmap(struct registers *regs);
197
198/**
199 * SYS_fork: create a child process that is a copy of the caller.
200 * returns the child's PID in the parent and 0 in the child, or a negative
201 * errno.
202 */
203syscall_ret_t sys_fork(struct registers *regs);
204
205/**
206 * SYS_execve: replace the calling process's program with a new one.
207 */
217syscall_ret_t sys_fcntl(struct registers *regs);
218syscall_ret_t sys_fstat(struct registers *regs);
221syscall_ret_t sys_pread(struct registers *regs);
223syscall_ret_t sys_fsync(struct registers *regs);
224
225/* sys_misc.c */
227syscall_ret_t sys_uname(struct registers *regs);
231syscall_ret_t sys_chdir(struct registers *regs);
232syscall_ret_t sys_ioctl(struct registers *regs);
233
234/* sys_socket.c */
236syscall_ret_t sys_bind(struct registers *regs);
249
250/* sys_fb.c */
syscall_ret_t sys_futex(struct registers *regs)
SYS_futex: args: uaddr (an aligned 32-bit word), op, val, timeout.
Definition futex.c:144
int dirfd(DIR *dir)
the file descriptor under a directory stream.
unsigned int uint32_t
Definition fb.h:17
__INTPTR_TYPE__ intptr_t
Definition stdint.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
__INT64_TYPE__ int64_t
Definition stdint.h:16
syscall_ret_t sys_sigprocmask(struct registers *regs)
SYS_sigprocmask: read and change the blocked mask.
Definition signal.c:751
syscall_ret_t sys_sigreturn(struct registers *regs)
SYS_sigreturn: return from a signal handler (called by the restorer).
Definition signal.c:831
syscall_ret_t sys_tgkill(struct registers *regs)
SYS_tgkill: send a signal to one thread.
Definition signal.c:690
syscall_ret_t sys_alarm(struct registers *regs)
SYS_alarm: send SIGALRM after a number of seconds (0 cancels).
Definition signal.c:895
syscall_ret_t sys_kill(struct registers *regs)
SYS_kill: send a signal.
Definition signal.c:638
syscall_ret_t sys_sigpending(struct registers *regs)
SYS_sigpending: the signals that are pending and blocked.
Definition signal.c:794
syscall_ret_t sys_sigaction(struct registers *regs)
SYS_sigaction: read and change what a process does with a signal.
Definition signal.c:715
syscall_ret_t sys_sigsuspend(struct registers *regs)
SYS_sigsuspend: wait for a signal with a temporary mask.
Definition signal.c:807
saved register state pushed onto the stack by ISR stubs.
Definition isr.h:41
uint32_t ecx
Definition isr.h:43
uint32_t esi
Definition isr.h:43
uint32_t ebx
Definition isr.h:43
uint32_t edi
Definition isr.h:43
uint32_t edx
Definition isr.h:43
syscall_ret_t sys_geteuid(struct registers *regs)
Definition sys_cred.c:24
syscall_ret_t sys_fchmod(struct registers *regs)
SYS_fchmod: args: EBX = fd, ECX = mode.
Definition sys_cred.c:171
syscall_ret_t sys_setuid(struct registers *regs)
SYS_setuid: args: EBX = uid.
Definition sys_cred.c:91
syscall_ret_t sys_seteuid(struct registers *regs)
Definition sys_cred.c:107
syscall_ret_t sys_chown(struct registers *regs)
SYS_chown: args: path, uid, gid (-1 keeps one).
Definition sys_cred.c:247
syscall_ret_t sys_getgid(struct registers *regs)
Definition sys_cred.c:30
syscall_ret_t sys_setegid(struct registers *regs)
Definition sys_cred.c:113
syscall_ret_t sys_fchown(struct registers *regs)
SYS_fchown: args: fd, uid, gid.
Definition sys_cred.c:269
syscall_ret_t sys_chmod(struct registers *regs)
SYS_chmod: args: path, mode (07777 bits).
Definition sys_cred.c:151
syscall_ret_t sys_umask(struct registers *regs)
SYS_umask: args: EBX = mask.
Definition sys_cred.c:293
syscall_ret_t sys_fchownat(struct registers *regs)
SYS_fchownat: args: dirfd, path, uid, gid, flags (AT_SYMLINK_NOFOLLOW).
Definition sys_cred.c:256
syscall_ret_t sys_getuid(struct registers *regs)
Definition sys_cred.c:18
syscall_ret_t sys_fchmodat(struct registers *regs)
SYS_fchmodat: args: dirfd, path, mode, flags (AT_SYMLINK_NOFOLLOW is refused: a link has no mode of i...
Definition sys_cred.c:160
syscall_ret_t sys_setgid(struct registers *regs)
SYS_setgid: args: EBX = gid.
Definition sys_cred.c:101
syscall_ret_t sys_getegid(struct registers *regs)
Definition sys_cred.c:36
syscall_ret_t sys_pollkey(struct registers *regs)
SYS_pollkey: non-blocking keyboard poll for interactive graphics programs.
Definition sys_fb.c:23
syscall_ret_t sys_linkat(struct registers *regs)
SYS_linkat: give a file one more name (a hard link).
Definition sys_fs.c:405
int32_t resolve_user_path(const char *user_path, char *resolved, uint32_t size)
copy a path string in from user memory and resolve it against the current process's cwd.
Definition sys_fs.c:13
syscall_ret_t sys_unlinkat(struct registers *regs)
SYS_unlinkat: remove a file or (AT_REMOVEDIR) a directory, relative to a directory.
Definition sys_fs.c:316
syscall_ret_t sys_fstatat(struct registers *regs)
SYS_fstatat: stat a path relative to a directory.
Definition sys_fs.c:120
syscall_ret_t sys_mkdir(struct registers *regs)
SYS_mkdir: create a directory.
Definition sys_fs.c:256
syscall_ret_t sys_faccessat(struct registers *regs)
SYS_faccessat: access, relative to a directory.
Definition sys_fs.c:223
syscall_ret_t sys_readlinkat(struct registers *regs)
SYS_readlinkat: read the target of a symbolic link.
Definition sys_fs.c:370
syscall_ret_t sys_renameat(struct registers *regs)
SYS_renameat: rename, each path relative to its own directory.
Definition sys_fs.c:169
int32_t set_new_owner(const char *path, uint32_t mode)
give a file the caller has just created its mode (taken through the umask) and the caller's effective...
Definition sys_fs.c:72
syscall_ret_t sys_utimensat(struct registers *regs)
SYS_utimensat: set a file's access and modification times.
Definition sys_fs.c:540
syscall_ret_t sys_getdents(struct registers *regs)
SYS_getdents: read directory entries.
Definition sys_fs.c:625
syscall_ret_t sys_rename(struct registers *regs)
SYS_rename: rename a file or directory.
Definition sys_fs.c:159
syscall_ret_t sys_access(struct registers *regs)
SYS_access: check that a file exists and may be accessed.
Definition sys_fs.c:213
syscall_ret_t sys_mknodat(struct registers *regs)
SYS_mknodat: make a named pipe, a device node or an empty file.
Definition sys_fs.c:447
int32_t resolve_user_path_at(int32_t dirfd, const char *user_path, char *resolved, uint32_t size, bool follow)
as resolve_user_path, with a relative path taken in the directory open as dirfd (AT_FDCWD: the workin...
Definition sys_fs.c:18
syscall_ret_t sys_stat(struct registers *regs)
SYS_stat: get file metadata.
Definition sys_fs.c:110
syscall_ret_t sys_unlink(struct registers *regs)
SYS_unlink: remove a file.
Definition sys_fs.c:296
syscall_ret_t sys_symlinkat(struct registers *regs)
SYS_symlinkat: make a symbolic link.
Definition sys_fs.c:329
syscall_ret_t sys_mkdirat(struct registers *regs)
SYS_mkdirat: mkdir relative to a directory.
Definition sys_fs.c:266
syscall_ret_t sys_rmdir(struct registers *regs)
SYS_rmdir: remove a directory.
Definition sys_fs.c:306
syscall_ret_t sys_fcntl(struct registers *regs)
SYS_fcntl: control an open file descriptor.
Definition sys_io.c:364
syscall_ret_t sys_openat(struct registers *regs)
SYS_openat: open relative to a directory.
Definition sys_io.c:199
syscall_ret_t sys_write(struct registers *regs)
SYS_write: write bytes to a file descriptor.
Definition sys_io.c:18
syscall_ret_t sys_ftruncate(struct registers *regs)
SYS_ftruncate: cut or grow an open file to a length.
Definition sys_io.c:248
syscall_ret_t sys_open(struct registers *regs)
Definition sys_io.c:189
syscall_ret_t sys_poll(struct registers *regs)
SYS_poll: wait until one of a set of descriptors is ready.
Definition sys_io.c:506
syscall_ret_t sys_read(struct registers *regs)
SYS_read: read bytes from a file descriptor.
Definition sys_io.c:49
syscall_ret_t sys_pwrite(struct registers *regs)
SYS_pwrite: write at an offset without moving the file offset.
Definition sys_io.c:428
syscall_ret_t sys_pread(struct registers *regs)
SYS_pread: read at an offset without moving the file offset.
Definition sys_io.c:409
syscall_ret_t sys_fstat(struct registers *regs)
SYS_fstat: get the metadata of an open file.
Definition sys_io.c:330
syscall_ret_t sys_close(struct registers *regs)
SYS_close: close an open file descriptor.
Definition sys_io.c:210
syscall_ret_t sys_fsync(struct registers *regs)
SYS_fsync: write an open file's data to disk.
Definition sys_io.c:448
syscall_ret_t sys_pipe(struct registers *regs)
SYS_pipe: create a pipe (pair of connected file descriptors).
Definition sys_io.c:296
syscall_ret_t sys_lseek(struct registers *regs)
SYS_lseek: set the file offset.
Definition sys_io.c:223
syscall_ret_t sys_flock(struct registers *regs)
SYS_flock: an advisory lock on a whole file (kernel/vfs/flock.h).
Definition sys_io.c:549
syscall_ret_t sys_dup2(struct registers *regs)
SYS_dup2: make new_fd another descriptor for old_fd's open file.
Definition sys_io.c:267
syscall_ret_t sys_getcwd(struct registers *regs)
SYS_getcwd: copy the current working directory into a user buffer.
Definition sys_misc.c:18
syscall_ret_t sys_chdir(struct registers *regs)
SYS_chdir: change the current working directory.
Definition sys_misc.c:75
syscall_ret_t sys_chroot(struct registers *regs)
SYS_chroot: make a directory the caller's root: its paths start there, and ".." stops there (root onl...
Definition sys_misc.c:47
syscall_ret_t sys_ioctl(struct registers *regs)
SYS_ioctl: issue a device-control request on an open file descriptor.
Definition sys_misc.c:126
syscall_ret_t sys_getrandom(struct registers *regs)
SYS_getrandom: random bytes.
Definition sys_misc.c:178
syscall_ret_t sys_reboot(struct registers *regs)
SYS_reboot: restart (RB_AUTOBOOT) or turn off (RB_POWER_OFF) the machine.
Definition sys_misc.c:151
syscall_ret_t sys_set_tls(struct registers *regs)
SYS_set_tls: set the calling thread's thread pointer, which user code reaches its thread-local storag...
Definition sys_proc.c:364
syscall_ret_t sys_exit(struct registers *regs)
SYS_exit: terminate the current process.
Definition sys_proc.c:27
syscall_ret_t sys_sbrk(struct registers *regs)
Definition sys_proc.c:582
syscall_ret_t sys_waitpid(struct registers *regs)
SYS_waitpid: wait for a child process to exit and reap it.
Definition sys_proc.c:407
syscall_ret_t sys_getsid(struct registers *regs)
SYS_getsid: the session of a process (pid 0: the caller).
Definition sys_proc.c:876
syscall_ret_t sys_getppid(struct registers *regs)
SYS_getppid: return the parent's pid, or 0 if the parent has exited.
Definition sys_proc.c:485
syscall_ret_t sys_getpgid(struct registers *regs)
SYS_getpgid: the process group of a process (pid 0: the caller).
Definition sys_proc.c:848
syscall_ret_t sys_clone(struct registers *regs)
SYS_clone: make a thread of the current process.
Definition sys_proc.c:308
syscall_ret_t sys_getpid(struct registers *regs)
SYS_getpid: return the current process ID (the thread group's id).
Definition sys_proc.c:384
syscall_ret_t sys_gettid(struct registers *regs)
SYS_gettid: the calling thread's id (the pid for a single thread).
Definition sys_proc.c:375
syscall_ret_t sys_mprotect(struct registers *regs)
Definition sys_proc.c:791
syscall_ret_t sys_execve(struct registers *regs)
SYS_execve: replace the calling process's program.
Definition sys_proc.c:234
syscall_ret_t sys_getpriority(struct registers *regs)
SYS_getpriority: the nice value of processes.
Definition sys_proc.c:975
syscall_ret_t sys_exit_group(struct registers *regs)
SYS_exit_group: end every thread of the current process.
Definition sys_proc.c:277
syscall_ret_t sys_fork(struct registers *regs)
SYS_fork: create a child process that is a copy of the caller.
Definition sys_proc.c:263
syscall_ret_t sys_mmap(struct registers *regs)
Definition sys_proc.c:696
syscall_ret_t sys_sched_yield(struct registers *regs)
SYS_sched_yield: let other ready processes run first.
Definition sys_proc.c:495
syscall_ret_t sys_setpgid(struct registers *regs)
SYS_setpgid: move a process into a process group.
Definition sys_proc.c:820
syscall_ret_t sys_munmap(struct registers *regs)
Definition sys_proc.c:754
syscall_ret_t sys_setpriority(struct registers *regs)
SYS_setpriority: set the nice value (-20 to 19) of processes; it sets the length of their time slice.
Definition sys_proc.c:988
syscall_ret_t sys_setsid(struct registers *regs)
SYS_setsid: start a new session, with the caller as the leader of it and of a new process group,...
Definition sys_proc.c:859
syscall_ret_t sys_exec(struct registers *regs)
SYS_exec: start an ELF binary as a child process (spawn, not execve).
Definition sys_proc.c:199
syscall_ret_t sys_shm_create(struct registers *regs)
SYS_shm_create: make a shared memory object (kernel/memory/shm.h).
Definition sys_proc.c:713
syscall_ret_t sys_socket(struct registers *regs)
SYS_socket: make an unconnected socket.
Definition sys_socket.c:174
syscall_ret_t sys_socketpair(struct registers *regs)
SYS_socketpair: make two connected sockets.
Definition sys_socket.c:214
syscall_ret_t sys_listen(struct registers *regs)
SYS_listen: let a bound socket take connections.
Definition sys_socket.c:293
syscall_ret_t sys_getsockopt(struct registers *regs)
SYS_getsockopt: read a socket option (an int value).
Definition sys_socket.c:784
syscall_ret_t sys_connect(struct registers *regs)
SYS_connect: connect to the listening socket with a name.
Definition sys_socket.c:316
syscall_ret_t sys_getsockname(struct registers *regs)
SYS_getsockname: the local address of a socket.
Definition sys_socket.c:729
syscall_ret_t sys_setsockopt(struct registers *regs)
SYS_setsockopt: set a socket option (an int value).
Definition sys_socket.c:750
syscall_ret_t sys_recvmsg(struct registers *regs)
SYS_recvmsg: receive data, and the files passed with it.
Definition sys_socket.c:615
syscall_ret_t sys_sendmsg(struct registers *regs)
SYS_sendmsg: send data, and pass open files with SCM_RIGHTS.
Definition sys_socket.c:469
syscall_ret_t sys_accept(struct registers *regs)
SYS_accept: take the next connection of a listening socket (accept4).
Definition sys_socket.c:349
syscall_ret_t sys_shutdown(struct registers *regs)
SYS_shutdown: stop the reading or the writing on a socket.
Definition sys_socket.c:819
syscall_ret_t sys_bind(struct registers *regs)
SYS_bind: give a socket a name.
Definition sys_socket.c:263
syscall_ret_t sys_getpeername(struct registers *regs)
SYS_getpeername: the address a socket is connected to.
Definition sys_socket.c:739
syscall_ret_t sys_uname(struct registers *regs)
SYS_uname: the name of the system, its release and version, and the machine.
Definition sys_uname.c:50
syscall_ret_t sys_sethostname(struct registers *regs)
SYS_sethostname: set the host name (root only).
Definition sys_uname.c:29
syscall_ret_t(*) syscall_fn_t(struct registers *regs)
Definition syscall_internal.h:77
intptr_t syscall_ret_t
syscall function pointer type.
Definition syscall_internal.h:76
syscall_ret_t sys_clock_gettime(struct registers *regs)
SYS_clock_gettime: read a clock.
Definition clock.c:88
uintptr_t syscall_arg_t
one syscall argument or return value: pointer-width, so a user address passes through unchanged.
Definition syscall_internal.h:14
syscall_ret_t sys_nanosleep(struct registers *regs)
SYS_nanosleep: sleep for a time span.
Definition clock.c:131
syscall_ret_t sys_clock_settime(struct registers *regs)
SYS_clock_settime: set the realtime clock (root only).
Definition clock.c:66
syscall_ret_t sys_times(struct registers *regs)
SYS_times: the CPU time of the calling process and its reaped children.
Definition clock.c:177