xref: /openbsd-src/share/man/man9/syscall.9 (revision 307a624eaecbd09c2301d08683aff3db9a3e8bb2)
1*307a624eSjmc.\"	$OpenBSD: syscall.9,v 1.16 2023/12/13 06:39:10 jmc Exp $
2f383f976Smickey.\"
3f383f976Smickey.\" Copyright (c) 2003 Michael Shalayeff
4f383f976Smickey.\"
5f383f976Smickey.\" Redistribution and use in source and binary forms, with or without
6f383f976Smickey.\" modification, are permitted provided that the following conditions
7f383f976Smickey.\" are met:
8f383f976Smickey.\" 1. Redistributions of source code must retain the above copyright
9f383f976Smickey.\"    notice, this list of conditions and the following disclaimer.
10f383f976Smickey.\" 2. Redistributions in binary form must reproduce the above copyright
11f383f976Smickey.\"    notice, this list of conditions and the following disclaimer in the
12f383f976Smickey.\"    documentation and/or other materials provided with the distribution.
13f383f976Smickey.\"
14f383f976Smickey.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15f383f976Smickey.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f383f976Smickey.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f383f976Smickey.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18f383f976Smickey.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f383f976Smickey.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f383f976Smickey.\" OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f383f976Smickey.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f383f976Smickey.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f383f976Smickey.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f383f976Smickey.\" SUCH DAMAGE.
25f383f976Smickey.\"
26*307a624eSjmc.Dd $Mdocdate: December 13 2023 $
27f383f976Smickey.Dt SYSCALL 9
28f383f976Smickey.Os
29f383f976Smickey.Sh NAME
30f383f976Smickey.Nm syscall
3173d4fc9bSjmc.Nd system calls overview
32f383f976Smickey.Sh DESCRIPTION
33f383f976SmickeySystem calls in the kernel are implemented through a set of
34f383f976Smickeyswitch tables for each emulation type.
35f383f976SmickeyEach table is generated from the
36f383f976Smickey.Dq master
3700d0c1a6Sjmcfile by
38f383f976Smickey.Pa sys/kern/makesyscalls.sh
39f383f976Smickeythrough the appropriate rules in the
40f383f976Smickey.Pa Makefile .
41f383f976Smickey.Pp
42f383f976SmickeyThe
43f383f976Smickey.Dq master
4400d0c1a6Sjmcfile is a text file consisting of a list of lines for each
4500d0c1a6Sjmcsystem call.
4600d0c1a6SjmcLines may be split by the means of back slashing the end of the line.
4700d0c1a6SjmcEach line is a set of fields separated by whitespace:
48f383f976Smickey.Pp
4900d0c1a6Sjmc.D1 Cd number type ...
50f383f976Smickey.Pp
51f383f976SmickeyWhere:
52f383f976Smickey.Bl -tag -width number -compact
53f383f976Smickey.It number
54f383f976Smickeyis the system call number;
55f383f976Smickey.It type
56f383f976Smickeyis one of:
57f383f976Smickey.Bl -tag -width COMPAT_XXX -compact
58f383f976Smickey.It STD
59f383f976Smickeyalways included;
60f383f976Smickey.It OBSOL
61f383f976Smickeyobsolete, not included in the system;
62f383f976Smickey.It UNIMPL
63f383f976Smickeyunimplemented, not included in the system;
64f383f976Smickey.It NODEF
65f383f976Smickeyincluded, but don't define the syscall number;
66f383f976Smickey.It NOARGS
67f383f976Smickeyincluded, but don't define the syscall args structure;
68f383f976Smickey.It INDIR
69f383f976Smickeyincluded, but don't define the syscall args structure,
70f383f976Smickeyand allow it to be "really" varargs;
71f383f976Smickey.It COMPAT_XX
7200d0c1a6Sjmca compatibility system call, only included if the corresponding
73f383f976Smickeyoption is configured for the kernel (see
74f383f976Smickey.Xr options 4 ) .
75f383f976Smickey.El
76f383f976Smickey.El
77f383f976Smickey.Pp
78f383f976SmickeyThe rest of the line for the STD, NODEF, NOARGS, and COMPAT_XX
79f383f976Smickeytypes is:
80f383f976Smickey.Pp
8100d0c1a6Sjmc.D1 Cd { pseudo-proto } [alias]
82f383f976Smickey.Pp
83f383f976Smickey.Nm pseudo-proto
8400d0c1a6Sjmcis a C-like prototype used to generate the system call argument list,
85f383f976Smickeyand alias is an optional name alias for the call.
86f383f976SmickeyThe function in the prototype has to be defined somewhere in
87f383f976Smickeythe kernel sources as it will be used as an entry point for
88f383f976Smickeythe corresponding system call.
89f383f976Smickey.Pp
90f383f976SmickeyFor other types the rest of the line is a comment.
91f383f976Smickey.Pp
92f383f976SmickeyTo generate the header and code files from the
93f383f976Smickey.Dq master
94f383f976Smickeyfile a
95f383f976Smickey.Xr make 1
9600d0c1a6Sjmccommand has to be run from the directory containing the
97f383f976Smickey.Dq master
98f383f976Smickeyfile.
99f383f976Smickey.Ss Usage
1006957a4a4SjmcEntry from the user space for the system call is machine dependent.
1016957a4a4SjmcTypical code to invoke a system call from the machine dependent
102f383f976Smickeysources might look like this:
103f383f976Smickey.Bd -literal -offset indent
104f383f976Smickey
105f383f976Smickey	const struct sysent *callp;
106f383f976Smickey	register_t code, args[8], rval[2];
107f383f976Smickey	struct proc *p = curproc;
108f383f976Smickey	int code, nsys;
109f383f976Smickey
110f383f976Smickey\&...
111f383f976Smickey
112d01fe417Sbentley/* "code" is the system call number passed from the user space */
113f383f976Smickey
114f383f976Smickey\&...
115f383f976Smickey
116f383f976Smickeyif (code < 0 || code >= nsys)
117f383f976Smickey	callp += p->p_emul->e_nosys;	/* illegal */
118f383f976Smickeyelse
119f383f976Smickey	callp += code;
120f383f976Smickey
121f383f976Smickey/* copyin the arguments from the user space */
122f383f976Smickey\&...
12390ca4801Sguenther	rval[0] = 0;
124f383f976Smickey
12590ca4801Sguenther/* the following steps are now performed using mi_syscall() */
126f383f976Smickey#ifdef SYSCALL_DEBUG
127f383f976Smickey	scdebug_call(p, code, args);
128f383f976Smickey#endif
129f383f976Smickey#ifdef KTRACE
130f383f976Smickey	if (KTRPOINT(p, KTR_SYSCALL))
131f383f976Smickey		ktrsyscall(p, code, argsize, args);
132f383f976Smickey#endif
133f383f976Smickey	error = (*callp->sy_call)(p, args, rval);
13490ca4801Sguenther
135f383f976Smickey	switch (error) {
136f383f976Smickey	case 0:
137f383f976Smickey		/* normal return */
138f383f976Smickey		\&...
139f383f976Smickey		break;
140f383f976Smickey	case ERESTART:
141f383f976Smickey		/*
142f383f976Smickey		 * adjust PC to point before the system call
143f383f976Smickey		 * in the user space in order for the return
144f383f976Smickey		 * back there we reenter the kernel to repeat
145f383f976Smickey		 * the same system call
146f383f976Smickey		 */
147f383f976Smickey		\&...
148f383f976Smickey		break;
149f383f976Smickey	case EJUSTRETURN:
150f383f976Smickey		/* just return */
151f383f976Smickey		break;
152f383f976Smickey	default:
153f383f976Smickey		/*
154f383f976Smickey		 * an error returned:
155f383f976Smickey		 *	call an optional emulation errno mapping
156f383f976Smickey		 *	routine and return back to the user.
157f383f976Smickey		 */
158f383f976Smickey		if (p->p_emul->e_errno)
159f383f976Smickey			error = p->p_emul->e_errno[error];
160f383f976Smickey		\&...
161f383f976Smickey		break;
162f383f976Smickey	}
16390ca4801Sguenther
16490ca4801Sguenther/* the following steps are now performed using mi_syscall_return() */
165f383f976Smickey#ifdef SYSCALL_DEBUG
166f383f976Smickey	scdebug_ret(p, code, orig_error, rval);
167f383f976Smickey#endif
16890ca4801Sguenther	userret(p);
169f383f976Smickey#ifdef KTRACE
170f383f976Smickey	if (KTRPOINT(p, KTR_SYSRET))
171f383f976Smickey		ktrsysret(p, code, orig_error, rval[0]);
172f383f976Smickey#endif
173f383f976Smickey
174f383f976Smickey.Ed
175f383f976Smickey.Pp
176f383f976SmickeyThe
177af529ab8Sschwarze.Dv SYSCALL_DEBUG
178f4fd8b80Sschwarzeparts of the code are explained in the
17900d0c1a6Sjmc.Sx Debugging
180f4fd8b80Sschwarzesection below.
181f383f976SmickeyFor the
182af529ab8Sschwarze.Dv KTRACE
18300d0c1a6Sjmcportions of the code refer to the
184f383f976Smickey.Xr ktrace 9
1859be451b6Sjmcdocument for further explanations.
186f383f976Smickey.Ss Debugging
18700d0c1a6SjmcFor debugging purposes the line
188f383f976Smickey.Pp
18900d0c1a6Sjmc.D1 Cd option SYSCALL_DEBUG
190f383f976Smickey.Pp
19100d0c1a6Sjmcshould be included in the kernel configuration file (see
192f383f976Smickey.Xr options 4 ) .
193f383f976SmickeyThis allows tracing for calls, returns, and arguments for both
19400d0c1a6Sjmcimplemented and non-implemented system calls.
195f383f976SmickeyA global integer variable
196af529ab8Sschwarze.Va scdebug
197697f8ab4Sdavidcontains a mask for the desired logging events:
19800d0c1a6Sjmc.Pp
19900d0c1a6Sjmc.Bl -tag -width SCDEBUG_SHOWARGS__ -compact
200f383f976Smickey.It SCDEBUG_CALLS
201f383f976Smickey(0x0001) show calls;
202f383f976Smickey.It SCDEBUG_RETURNS
203f383f976Smickey(0x0002) show returns;
204f383f976Smickey.It SCDEBUG_ALL
205f383f976Smickey(0x0004) show even syscalls that are implemented;
206f383f976Smickey.It SCDEBUG_SHOWARGS
207f383f976Smickey(0x0008) show arguments to calls.
208f383f976Smickey.El
209f383f976Smickey.Pp
210f383f976SmickeyUse
211f383f976Smickey.Xr ddb 4
212af529ab8Sschwarzeto set
213af529ab8Sschwarze.Va scdebug
214af529ab8Sschwarzeto the desired value.
215f383f976Smickey.Sh CODE REFERENCES
216f383f976Smickey.Bl -tag -width sys/kern/syscalls.master -compact
217f383f976Smickey.It Pa sys/kern/makesyscalls.sh
218f383f976Smickeya
219f383f976Smickey.Xr sh 1
220f383f976Smickeyscript for generating C files out of the syscall master file;
22189f5bfa7Snaddy.It Pa sys/kern/syscalls.conf
222f383f976Smickeya configuration file for the shell script above;
22389f5bfa7Snaddy.It Pa sys/kern/syscalls.master
224f383f976Smickeymaster files describing names and numbers for the system calls;
22589f5bfa7Snaddy.It Pa sys/kern/syscalls.c
226f383f976Smickeysystem call names lists;
22789f5bfa7Snaddy.It Pa sys/kern/init_sysent.c
228f383f976Smickeysystem call switch tables;
22989f5bfa7Snaddy.It Pa sys/sys/syscallargs.h
230f383f976Smickeysystem call argument lists;
23189f5bfa7Snaddy.It Pa sys/sys/syscall.h
23290ca4801Sguenthersystem call numbers;
23390ca4801Sguenther.It Pa sys/sys/syscall_mi.h
23490ca4801SguentherMachine-independent syscall entry end return handling.
235f383f976Smickey.El
236f383f976Smickey.Sh SEE ALSO
237f383f976Smickey.Xr ktrace 2 ,
238f383f976Smickey.Xr ktrace 9 ,
239b1d599a8Stedu.Xr sysctl_int 9
240f383f976Smickey.Sh HISTORY
241f383f976SmickeyThe
242f383f976Smickey.Nm
243f383f976Smickeysection manual page appeared in
244f383f976Smickey.Ox 3.4 .
245