x86-64 syscalls

A program here runs as a Linux program, so everything outside its own memory happens through syscall. Put the call number in rax, the arguments in rdi, rsi, rdx, r10, r8 and r9, and read the result from rax. A result between -1 and -4095 is an error code. rcx and r11 do not survive the call.

The ones to start with

1 - write waits

Writes rdx bytes from the buffer at rsi to the file descriptor in rdi, and returns how many it wrote. Descriptor 1 is standard output and 2 is standard error, so this is how a program prints.

rdi
file descriptor
rsi
buffer (read by the kernel)
rdx
byte count

0 - read waits

Reads up to rdx bytes from the file descriptor in rdi into the buffer at rsi, and returns how many it read. Descriptor 0 is standard input, so this is how a program reads what was typed. A return of 0 means end of input.

rdi
file descriptor
rsi
buffer (written by the kernel)
rdx
byte count

60 - exit

Ends the program with the status in rdi. It never returns, and a program that reaches the end of its code without calling it runs into whatever bytes follow.

rdi
status

231 - exit_group

Ends every thread of the program with the status in rdi. For a program with one thread it is exit.

rdi
status

2 - open waits

Opens the path at rdi with the flags in rsi and, when creating, the mode in rdx. Returns a file descriptor.

rdi
path
rsi
open flags
rdx
mode

3 - close waits

Closes the file descriptor in rdi.

rdi
file descriptor

8 - lseek

Moves the read and write position of the descriptor in rdi to the offset in rsi, interpreted according to rdx, and returns the new position.

rdi
file descriptor
rsi
offset
rdx
whence

5 - fstat

Fills the structure at rsi with what is known about the descriptor in rdi, including its size.

rdi
file descriptor
rsi
o_stat

12 - brk

Moves the end of the data segment to the address in rdi, which is the oldest way to ask for more memory. Called with 0 it returns where the segment currently ends.

rdi
value

9 - mmap

Maps memory: the length in rsi, the protection in rdx, the flags in r10. An anonymous private mapping is how a program asks for a block of memory it can write to.

rdi
pointer
rsi
size
rdx
protection flags
r10
mapping flags
r8
file descriptor
r9
offset

11 - munmap

Unmaps the mapping of rsi bytes at the address in rdi.

rdi
pointer
rsi
size

35 - nanosleep waits

Sleeps for the interval at rdi, a pair of seconds and nanoseconds.

rdi
i_time
rsi
o_time

228 - clock_gettime

Writes the time of the clock named in rdi into the structure at rsi.

rdi
clock
rsi
timespec

39 - getpid

Returns the process id.

Everything this emulator implements

Those syscalls are what the emulator implements, anything missing returns -ENOSYS.

raxCallArguments
4statrdi pathrsi o_stat
6lstatrdi pathrsi o_stat
7pollrdi valuersi valuerdx value
10mprotectrdi pointerrsi sizerdx protection flags
13rt_sigactionrdi signalrsi i_handrdx o_handr10 byte count
14rt_sigprocmaskrdi sighowrsi i_sigsetrdx o_sigsetr10 byte count
16ioctlrdi valuersi valuerdx value
17preadrdi file descriptorrsi buffer (written by the kernel)rdx byte countr10 offset
18pwriterdi file descriptorrsi buffer (read by the kernel)rdx byte countr10 offset
19readvrdi file descriptorrsi iovec array (written)rdx byte count
20writevrdi file descriptorrsi iovec array (read)rdx byte count
21accessrdi pathrsi accmode
22piperdi o_pfds
23selectrdi intrsi io_fdsetrdx io_fdsetr10 io_fdsetr8 io_timev
24sched_yield
25mremaprdi valuersi valuerdx valuer10 valuer8 value
26msyncrdi valuersi valuerdx value
28madviserdi valuersi valuerdx value
32duprdi file descriptor
33dup2rdi file descriptorrsi int
34pause
36getitimerrdi valuersi value
37alarmrdi int
38setitimerrdi valuersi valuerdx value
40sendfilerdi valuersi valuerdx valuer10 value
41socketrdi familyrsi socktyperdx int
42connectrdi file descriptorrsi i_addrrdx addrlen
43acceptrdi file descriptorrsi o_addrrdx long
44sendtordi file descriptorrsi buffer (read by the kernel)rdx sizer10 intr8 i_addrr9 addrlen
45recvfromrdi file descriptorrsi buffer (written by the kernel)rdx sizer10 intr8 o_addrr9 long
46sendmsgrdi valuersi valuerdx value
47recvmsgrdi valuersi valuerdx value
48shutdownrdi valuersi value
49bindrdi file descriptorrsi i_addrrdx addrlen
50listenrdi file descriptorrsi un
51getsocknamerdi file descriptorrsi o_addrrdx long
52getpeernamerdi file descriptorrsi o_addrrdx long
53socketpairrdi familyrsi socktyperdx intr10 o_pfds
54setsockoptrdi valuersi valuerdx valuer10 valuer8 value
55getsockoptrdi valuersi valuerdx valuer10 valuer8 value
56clonerdi cloneflagsrsi pointerrdx pointerr10 pointerr8 pointerr9 pointer
57fork
58vfork
59execverdi valuersi valuerdx value
61wait4rdi process idrsi o_wstatusrdx waitflagsr10 o_rusage
62killrdi process idrsi signal
63unamerdi value
72fcntlrdi file descriptorrsi wat_fcntlrdx un
73flockrdi valuersi value
74fsyncrdi file descriptor
75fdatasyncrdi file descriptor
76truncaterdi stringrsi offset
77ftruncaterdi file descriptorrsi offset
79getcwdrdi buffer (written by the kernel)rsi byte count
80chdirrdi path
81fchdirrdi file descriptor
82renamerdi pathrsi path
83mkdirrdi pathrsi mode
84rmdirrdi path
85creatrdi pathrsi mode
86linkrdi pathrsi path
87unlinkrdi path
88symlinkrdi pathrsi path
89readlinkrdi pathrsi buffer (written by the kernel)rdx byte count
90chmodrdi pathrsi mode
91fchmodrdi file descriptorrsi uid
92chownrdi pathrsi uidrdx gid
93fchownrdi file descriptorrsi uidrdx gid
94lchownrdi pathrsi uidrdx gid
95umaskrdi mode
96gettimeofdayrdi valuersi value
97getrlimitrdi resourcersi o_rlimit
98getrusagerdi valuersi value
99sysinfordi value
100timesrdi value
102getuid
104getgid
105setuidrdi uid
106setgidrdi gid
107geteuid
108getegid
109setpgidrdi valuersi value
110getppid
111getpgrp
112setsid
113setreuidrdi uidrsi uid
114setregidrdi gidrsi gid
115getgroupsrdi valuersi value
116setgroupsrdi valuersi value
117setresuidrdi uidrsi uidrdx uid
118getresuidrdi valuersi valuerdx value
119setresgidrdi gidrsi gidrdx gid
120getresgidrdi valuersi valuerdx value
121getpgidrdi process id
124getsidrdi value
127rt_sigpendingrdi value
130rt_sigsuspendrdi i_sigsetrsi byte count
131sigaltstackrdi valuersi value
132utimerdi valuersi value
133mknodrdi valuersi valuerdx value
137statfsrdi valuersi value
138fstatfsrdi valuersi value
140getpriorityrdi valuersi value
141setpriorityrdi valuersi valuerdx value
142sched_set_paramrdi valuersi value
143sched_get_paramrdi valuersi value
144sched_set_schedulerrdi valuersi valuerdx value
145sched_get_schedulerrdi value
146sched_get_priority_maxrdi value
147sched_get_priority_minrdi value
157prctlrdi valuersi valuerdx valuer10 valuer8 value
158arch_prctlrdi valuersi value
160setrlimitrdi resourcersi i_rlimit
161chrootrdi path
162sync
165mountrdi stringrsi stringrdx msflagsr10 pointerr8 un
186gettid
200tkillrdi process idrsi signal
202futexrdi valuersi valuerdx valuer10 valuer8 valuer9 value
203sched_set_affinityrdi valuersi valuerdx value
204sched_get_affinityrdi valuersi valuerdx value
213epoll_createrdi value
217getdentsrdi valuersi valuerdx value
218set_tid_addressrdi value
221fadviserdi valuersi valuerdx valuer10 value
227clock_settimerdi valuersi value
229clock_getresrdi valuersi value
230clock_nanosleeprdi clockrsi intrdx i_timer10 o_time
232epoll_waitrdi valuersi valuerdx valuer10 value
233epoll_ctlrdi valuersi valuerdx valuer10 value
234tgkillrdi valuersi valuerdx value
235utimesrdi valuersi value
257openatrdi directory descriptorrsi stringrdx open flagsr10 mode
258mkdiratrdi directory descriptorrsi pathrdx mode
259mknodatrdi valuersi valuerdx valuer10 value
260fchownatrdi directory descriptorrsi pathrdx uidr10 gidr8 atflags
261futimesatrdi valuersi valuerdx value
262fstatatrdi directory descriptorrsi pathrdx o_statr10 atflags
263unlinkatrdi directory descriptorrsi pathrdx atflags
264renameatrdi directory descriptorrsi pathrdx directory descriptorr10 path
265linkatrdi directory descriptorrsi pathrdx directory descriptorr10 pathr8 atflags
266symlinkatrdi pathrsi directory descriptorrdx path
267readlinkatrdi directory descriptorrsi stringrdx buffer (written by the kernel)r10 byte count
268fchmodatrdi directory descriptorrsi pathrdx mode
269faccessatrdi directory descriptorrsi pathrdx accmode
270pselect6rdi valuersi valuerdx valuer10 valuer8 valuer9 value
271ppollrdi valuersi valuerdx valuer10 valuer8 value
273set_robust_listrdi valuersi value
274get_robust_listrdi valuersi valuerdx value
280utimensatrdi directory descriptorrsi pathrdx o_time2r10 atflags
281epoll_pwaitrdi valuersi valuerdx valuer10 valuer8 valuer9 value
288accept4rdi file descriptorrsi o_addrrdx longr10 sockflags
291epoll_create1rdi value
292dup3rdi file descriptorrsi intrdx open flags
293pipe2rdi o_pfdsrsi open flags
295preadvrdi file descriptorrsi iovec array (written)rdx byte countr10 offset
296pwritevrdi file descriptorrsi iovec array (read)rdx byte countr10 offset
299recvmmsgrdi valuersi valuerdx valuer10 valuer8 value
302prlimitrdi process idrsi resourcerdx i_rlimitr10 o_rlimit
307sendmmsgrdi valuersi valuerdx valuer10 value
316renameat2rdi directory descriptorrsi pathrdx directory descriptorr10 pathr8 atflags
318getrandomrdi buffer (written by the kernel)rsi byte countrdx int
327preadv2rdi file descriptorrsi iovec array (written)rdx byte countr10 offsetr8 int
328pwritev2rdi file descriptorrsi iovec array (read)rdx byte countr10 offsetr8 int
436close_rangerdi valuersi valuerdx value
439faccessat2rdi directory descriptorrsi pathrdx accmoder10 atflags
441epoll_pwait2rdi valuersi valuerdx valuer10 valuer8 valuer9 value