1ccab178bSStathis Kamperis.\" $OpenBSD: syscall.9,v 1.7 2007/05/31 19:20:01 jmc Exp $ 2ccab178bSStathis Kamperis.\" 3ccab178bSStathis Kamperis.\" Copyright (c) 2003 Michael Shalayeff 4ccab178bSStathis Kamperis.\" 5ccab178bSStathis Kamperis.\" Redistribution and use in source and binary forms, with or without 6ccab178bSStathis Kamperis.\" modification, are permitted provided that the following conditions 7ccab178bSStathis Kamperis.\" are met: 8ccab178bSStathis Kamperis.\" 1. Redistributions of source code must retain the above copyright 9ccab178bSStathis Kamperis.\" notice, this list of conditions and the following disclaimer. 10ccab178bSStathis Kamperis.\" 2. Redistributions in binary form must reproduce the above copyright 11ccab178bSStathis Kamperis.\" notice, this list of conditions and the following disclaimer in the 12ccab178bSStathis Kamperis.\" documentation and/or other materials provided with the distribution. 13ccab178bSStathis Kamperis.\" 14ccab178bSStathis Kamperis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 15ccab178bSStathis Kamperis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16ccab178bSStathis Kamperis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17ccab178bSStathis Kamperis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 18ccab178bSStathis Kamperis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19ccab178bSStathis Kamperis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20ccab178bSStathis Kamperis.\" OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21ccab178bSStathis Kamperis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22ccab178bSStathis Kamperis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23ccab178bSStathis Kamperis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24ccab178bSStathis Kamperis.\" SUCH DAMAGE. 25ccab178bSStathis Kamperis.\" 26ea735b6bSAaron LI.Dd January 19, 2021 27ccab178bSStathis Kamperis.Dt SYSCALL 9 28ccab178bSStathis Kamperis.Os 29ccab178bSStathis Kamperis.Sh NAME 30ccab178bSStathis Kamperis.Nm syscall 31ea735b6bSAaron LI.Nd system calls overview 32ccab178bSStathis Kamperis.Sh DESCRIPTION 33ccab178bSStathis KamperisA system call is an explicit request to the kernel made via a software 34ccab178bSStathis Kamperisinterrupt by some program. 35ccab178bSStathis KamperisFor example, 36ccab178bSStathis Kamperis.Fn open 37ccab178bSStathis Kamperisis a system call that is used when access to a file stored in filesystem 38ccab178bSStathis Kamperisis needed. 39ccab178bSStathis KamperisIn this sense, system calls provide the interface between a process and the 40ccab178bSStathis Kamperisoperating system. 41ccab178bSStathis Kamperis.Pp 42ccab178bSStathis KamperisThe kernel implements system calls through a set of switch tables 43ccab178bSStathis Kamperisfor each emulation type. 44ccab178bSStathis KamperisThe list of currently supported system calls along with their codes resides in 45ccab178bSStathis Kamperis.Pa sys/sys/syscall.h . 46ccab178bSStathis KamperisThis file, and a couple others which will be examined later, are 47ccab178bSStathis Kamperisautomatically generated and should not be edited manually. 48ccab178bSStathis Kamperis.Pp 49ccab178bSStathis KamperisThe first step in adding a new system call is to edit the 50a263c9acSSascha Wildner.Pa sys/kern/syscalls.master 51ccab178bSStathis Kamperisfile. 52ccab178bSStathis KamperisThe 53ccab178bSStathis Kamperis.Dq master 54ccab178bSStathis Kamperisfile is a text file consisting of a list of lines for each 55ccab178bSStathis Kamperissystem call. 56ccab178bSStathis KamperisLines may be split by the means of back slashing the end of the line. 57ccab178bSStathis KamperisEach line is a set of fields separated by whitespace: 58ccab178bSStathis Kamperis.Pp 59948bb6a2SSascha Wildner.D1 Cd number type ... 60ccab178bSStathis Kamperis.Pp 61ccab178bSStathis KamperisWhere: 62948bb6a2SSascha Wildner.Bl -tag -width number -compact 63ccab178bSStathis Kamperis.It number 64ccab178bSStathis Kamperisis the system call number; 65ccab178bSStathis Kamperis.It type 66ccab178bSStathis Kamperisis one of: 67ccab178bSStathis Kamperis.Bl -tag -width NOPROTO -compact 68ccab178bSStathis Kamperis.It STD 69ccab178bSStathis Kamperisstandard system call with full prototype and implementation; 70ccab178bSStathis Kamperis.It OBSOL 71ccab178bSStathis Kamperisobsolete, not included in the system; 72ccab178bSStathis Kamperis.It UNIMPL 73ccab178bSStathis Kamperisunimplemented, not included in the system, placeholder only; 74ccab178bSStathis Kamperis.It NODEF 75ccab178bSStathis Kamperisincluded, but don't define the syscall number; 76ccab178bSStathis Kamperis.It NOARGS 77ccab178bSStathis Kamperisincluded, but don't define the syscall args structure; 78ccab178bSStathis Kamperis.It NOPROTO 79ccab178bSStathis Kamperisimplemented elsewhere; 80ccab178bSStathis Kamperis.El 81ccab178bSStathis Kamperis.El 82ccab178bSStathis Kamperis.Pp 83abe61ffcSSascha WildnerThe rest of the line for the STD, NODEF, and NOARGS types is: 84ccab178bSStathis Kamperis.Pp 85ccab178bSStathis Kamperis.D1 Cd { pseudo-proto } [alias] 86ccab178bSStathis Kamperis.Pp 87ccab178bSStathis Kamperis.Nm pseudo-proto 88ccab178bSStathis Kamperisis a C-like prototype used to generate the system call argument list, 89ccab178bSStathis Kamperisand alias is an optional name alias for the call. 90ccab178bSStathis KamperisThe function in the prototype has to be defined somewhere in 91ccab178bSStathis Kamperisthe kernel sources as it will be used as an entry point for 92ccab178bSStathis Kamperisthe corresponding system call. 93ccab178bSStathis Kamperis.Pp 94ccab178bSStathis KamperisFor other types the rest of the line is a comment. 95ccab178bSStathis Kamperis.Pp 96ccab178bSStathis KamperisTo generate the header and code files from the 97ccab178bSStathis Kamperis.Dq master 98ccab178bSStathis Kamperisfile, 99ea735b6bSAaron LI.Cm make sysent 100ccab178bSStathis Kamperishas to be run from the directory containing the 101ccab178bSStathis Kamperis.Dq master 102ccab178bSStathis Kamperisfile. 103ccab178bSStathis KamperisPlease mind that the string 104ea735b6bSAaron LI.Dq sys_ 105ccab178bSStathis Kamperisis prepended to all system call names, but not to the structures 106ccab178bSStathis Kamperisholding the arguments. 107ea735b6bSAaron LISo, if one has added this line to the system call 108ea735b6bSAaron LI.Dq master 109ea735b6bSAaron LIfile: 110ccab178bSStathis Kamperis.Bd -literal 111ea735b6bSAaron LI503 STD { int mycall(int x, int y); } 112ccab178bSStathis Kamperis.Ed 113ccab178bSStathis Kamperis.Pp 114ea735b6bSAaron LIthe generated prototype would be: 115bc5a8594SSascha Wildner.Pp 116bc5a8594SSascha Wildner.Ft int 117ea735b6bSAaron LI.Fn sys_mycall "struct sysmsg *sysmsg" "const struct mycall_args *uap" ; 118ccab178bSStathis Kamperis.Pp 119ea735b6bSAaron LIAny value that the 120abc84a0bSSascha Wildner.Fn sys_mycall 121abc84a0bSSascha Wildnerkernel function returns ends up in 122abc84a0bSSascha Wildner.Va errno 123abc84a0bSSascha Wildnerafter executing the 124abc84a0bSSascha Wildner.Fn mycall 125abc84a0bSSascha Wildnerlibc function, and the return value of 126abc84a0bSSascha Wildner.Fn mycall 127abc84a0bSSascha Wildneris automatically -1 or 0 depending on whether 128abc84a0bSSascha Wildner.Va errno 129abc84a0bSSascha Wildnerwas set or not. 130abc84a0bSSascha WildnerA function that needs to return a different value to userland, e.g.\& a 131abc84a0bSSascha Wildnerfile descriptor, must override the default value in 132ea735b6bSAaron LI.Fa sysmsg->sysmsg_result 133ea735b6bSAaron LI(as defined in 134ea735b6bSAaron LI.Pa sys/sys/sysmsg.h ) 135ea735b6bSAaron LIand return 0. 136ea735b6bSAaron LI.Pp 137ea735b6bSAaron LIIn the 138ea735b6bSAaron LI.Lb libc , 139ea735b6bSAaron LIthe assembly wrapper (as described below) will create these symbols: 140ea735b6bSAaron LI.Sy mycall , 141ea735b6bSAaron LI.Sy _mycall 142ea735b6bSAaron LIand 143ea735b6bSAaron LI.Sy __sys_mycall . 144ea735b6bSAaron LITo export the syscall for external use, add symbol 145ea735b6bSAaron LI.Sy mycall 146ea735b6bSAaron LIto the appropriate 147ea735b6bSAaron LI.Dv DFxxx.0 148ea735b6bSAaron LIsection in the 149ea735b6bSAaron LI.Pa lib/libc/sys/Symbol.map 150ea735b6bSAaron LIfile. 151ea735b6bSAaron LIIn addition, add symbols 152ea735b6bSAaron LI.Sy _mycall 153ea735b6bSAaron LIand 154ea735b6bSAaron LI.Sy __sys_mycall 155ea735b6bSAaron LIto the 156ea735b6bSAaron LI.Dv DFprivate_1.0 157ea735b6bSAaron LIsection in the same file for internal use. 158f2589c6cSSascha Wildner.Sh IMPLEMENTATION NOTES 159ea735b6bSAaron LIIn the kernel, the syscall entry point is implemented in platform-dependent 160ea735b6bSAaron LIassembly code (e.g., 161ea735b6bSAaron LI.Pa sys/platform/pc64/x86_64/exception.S 162ea735b6bSAaron LIfor x86_64 machines), 163ea735b6bSAaron LIand the syscall itself is implemented by a 164f2589c6cSSascha Wildner.Fn sys_syscallname 165f2589c6cSSascha Wildnerfunction. 166ea735b6bSAaron LI.Pp 167f2589c6cSSascha WildnerIn userspace, the function that executes a syscall is automatically generated 168f2589c6cSSascha Wildnerusing the description in 169f2589c6cSSascha Wildner.Pa syscalls.master . 170f2589c6cSSascha WildnerThe symbols in the 171f2589c6cSSascha Wildner.Lb libc 172f2589c6cSSascha Wildnerare assembly wrappers generated in 173ea735b6bSAaron LI.Pa lib/libc/${MACHINE_ARCH} 174ea735b6bSAaron LI.Pq e.g.\& x86_64 , 175f2589c6cSSascha Wildneragain using the description in 176f2589c6cSSascha Wildner.Pa syscalls.master . 177f2589c6cSSascha WildnerThese wrappers use macros provided by the platform-dependent 178*8fec8f53SSascha Wildner.Pa SYS.h 179f2589c6cSSascha Wildnerheader file which take care of putting the syscall arguments into registers 180a80d996eSSascha Wildner(per the ABI specification) and inserting a 181f2589c6cSSascha Wildner.Li syscall 182f2589c6cSSascha Wildnerinstruction (on x86_64). 183ccab178bSStathis Kamperis.Sh FILES 184ccab178bSStathis Kamperis.Bl -tag -width sys/kern/syscalls.master -compact 185ea735b6bSAaron LI.It Pa sys/kern/syscalls.master 186ea735b6bSAaron LIthe 187ea735b6bSAaron LI.Dq master 188ea735b6bSAaron LIfile describing names and numbers for the system calls; 189ccab178bSStathis Kamperis.It Pa sys/kern/makesyscalls.sh 190ccab178bSStathis Kamperisa 191ccab178bSStathis Kamperis.Xr sh 1 192ea735b6bSAaron LIscript for generating C files out of the syscall master file above; 193ccab178bSStathis Kamperis.It Pa sys/kern/syscalls.c 194ccab178bSStathis Kamperissystem call names lists; 195ccab178bSStathis Kamperis.It Pa sys/kern/init_sysent.c 196ccab178bSStathis Kamperissystem call switch tables; 197ea735b6bSAaron LI.It Pa sys/sys/syscall.h 198ea735b6bSAaron LIsystem call numbers; 199ea735b6bSAaron LI.It Pa sys/sys/sysmsg.h 200ea735b6bSAaron LIsystem call message structure; 201ccab178bSStathis Kamperis.It Pa sys/sys/sysproto.h 202ccab178bSStathis Kamperissystem call argument lists; 203ea735b6bSAaron LI.It Pa sys/sys/sysunion.h 204ea735b6bSAaron LIsystem call argument union. 205ccab178bSStathis Kamperis.El 206ccab178bSStathis Kamperis.Sh SEE ALSO 207ccab178bSStathis Kamperis.Xr ktrace 2 , 208ccab178bSStathis Kamperis.Xr syscall 2 , 209ccab178bSStathis Kamperis.Xr SYSCALL_MODULE 9 210ccab178bSStathis Kamperis.Sh HISTORY 211ccab178bSStathis KamperisThe 212ccab178bSStathis Kamperis.Nm 213ccab178bSStathis Kamperissection manual page appeared in 214ccab178bSStathis Kamperis.Dx 2.3 . 215