wait.2: Add ESRCH for when pid == INT_MIN
[man-pages.git] / man7 / vdso.7
blobef6bef5bd217ff597b0fce8a77209ed1941ffe63
1 .\" Written by Mike Frysinger <vapier@gentoo.org>
2 .\"
3 .\" %%%LICENSE_START(PUBLIC_DOMAIN)
4 .\" This page is in the public domain.
5 .\" %%%LICENSE_END
6 .\"
7 .\" Useful background:
8 .\"   http://articles.manugarg.com/systemcallinlinux2_6.html
9 .\"   https://lwn.net/Articles/446528/
10 .\"   http://www.linuxjournal.com/content/creating-vdso-colonels-other-chicken
11 .\"   http://www.trilithium.com/johan/2005/08/linux-gate/
12 .\"
13 .TH VDSO 7 2021-03-22 "Linux" "Linux Programmer's Manual"
14 .SH NAME
15 vdso \- overview of the virtual ELF dynamic shared object
16 .SH SYNOPSIS
17 .nf
18 .B #include <sys/auxv.h>
19 .PP
20 .B void *vdso = (uintptr_t) getauxval(AT_SYSINFO_EHDR);
21 .fi
22 .SH DESCRIPTION
23 The "vDSO" (virtual dynamic shared object) is a small shared library that
24 the kernel automatically maps into the
25 address space of all user-space applications.
26 Applications usually do not need to concern themselves with these details
27 as the vDSO is most commonly called by the C library.
28 This way you can code in the normal way using standard functions
29 and the C library will take care
30 of using any functionality that is available via the vDSO.
31 .PP
32 Why does the vDSO exist at all?
33 There are some system calls the kernel provides that
34 user-space code ends up using frequently,
35 to the point that such calls can dominate overall performance.
36 This is due both to the frequency of the call as well as the
37 context-switch overhead that results
38 from exiting user space and entering the kernel.
39 .PP
40 The rest of this documentation is geared toward the curious and/or
41 C library writers rather than general developers.
42 If you're trying to call the vDSO in your own application rather than using
43 the C library, you're most likely doing it wrong.
44 .SS Example background
45 Making system calls can be slow.
46 In x86 32-bit systems, you can trigger a software interrupt
47 .RI ( "int $0x80" )
48 to tell the kernel you wish to make a system call.
49 However, this instruction is expensive: it goes through
50 the full interrupt-handling paths
51 in the processor's microcode as well as in the kernel.
52 Newer processors have faster (but backward incompatible) instructions to
53 initiate system calls.
54 Rather than require the C library to figure out if this functionality is
55 available at run time,
56 the C library can use functions provided by the kernel in
57 the vDSO.
58 .PP
59 Note that the terminology can be confusing.
60 On x86 systems, the vDSO function
61 used to determine the preferred method of making a system call is
62 named "__kernel_vsyscall", but on x86-64,
63 the term "vsyscall" also refers to an obsolete way to ask the kernel
64 what time it is or what CPU the caller is on.
65 .PP
66 One frequently used system call is
67 .BR gettimeofday (2).
68 This system call is called both directly by user-space applications
69 as well as indirectly by
70 the C library.
71 Think timestamps or timing loops or polling\(emall of these
72 frequently need to know what time it is right now.
73 This information is also not secret\(emany application in any
74 privilege mode (root or any unprivileged user) will get the same answer.
75 Thus the kernel arranges for the information required to answer
76 this question to be placed in memory the process can access.
77 Now a call to
78 .BR gettimeofday (2)
79 changes from a system call to a normal function
80 call and a few memory accesses.
81 .SS Finding the vDSO
82 The base address of the vDSO (if one exists) is passed by the kernel to
83 each program in the initial auxiliary vector (see
84 .BR getauxval (3)),
85 via the
86 .B AT_SYSINFO_EHDR
87 tag.
88 .PP
89 You must not assume the vDSO is mapped at any particular location in the
90 user's memory map.
91 The base address will usually be randomized at run time every time a new
92 process image is created (at
93 .BR execve (2)
94 time).
95 This is done for security reasons,
96 to prevent "return-to-libc" attacks.
97 .PP
98 For some architectures, there is also an
99 .B AT_SYSINFO
100 tag.
101 This is used only for locating the vsyscall entry point and is frequently
102 omitted or set to 0 (meaning it's not available).
103 This tag is a throwback to the initial vDSO work (see
104 .IR History
105 below) and its use should be avoided.
106 .SS File format
107 Since the vDSO is a fully formed ELF image, you can do symbol lookups on it.
108 This allows new symbols to be added with newer kernel releases,
109 and allows the C library to detect available functionality at
110 run time when running under different kernel versions.
111 Oftentimes the C library will do detection with the first call and then
112 cache the result for subsequent calls.
114 All symbols are also versioned (using the GNU version format).
115 This allows the kernel to update the function signature without breaking
116 backward compatibility.
117 This means changing the arguments that the function accepts as well as the
118 return value.
119 Thus, when looking up a symbol in the vDSO,
120 you must always include the version
121 to match the ABI you expect.
123 Typically the vDSO follows the naming convention of prefixing
124 all symbols with "__vdso_" or "__kernel_"
125 so as to distinguish them from other standard symbols.
126 For example, the "gettimeofday" function is named "__vdso_gettimeofday".
128 You use the standard C calling conventions when calling
129 any of these functions.
130 No need to worry about weird register or stack behavior.
131 .SH NOTES
132 .SS Source
133 When you compile the kernel,
134 it will automatically compile and link the vDSO code for you.
135 You will frequently find it under the architecture-specific directory:
137     find arch/$ARCH/ \-name \(aq*vdso*.so*\(aq \-o \-name \(aq*gate*.so*\(aq
139 .SS vDSO names
140 The name of the vDSO varies across architectures.
141 It will often show up in things like glibc's
142 .BR ldd (1)
143 output.
144 The exact name should not matter to any code, so do not hardcode it.
145 .if t \{\
146 .ft CW
149 l l.
150 user ABI        vDSO name
152 aarch64 linux\-vdso.so.1
153 arm     linux\-vdso.so.1
154 ia64    linux\-gate.so.1
155 mips    linux\-vdso.so.1
156 ppc/32  linux\-vdso32.so.1
157 ppc/64  linux\-vdso64.so.1
158 riscv   linux\-vdso.so.1
159 s390    linux\-vdso32.so.1
160 s390x   linux\-vdso64.so.1
161 sh      linux\-gate.so.1
162 i386    linux\-gate.so.1
163 x86-64  linux\-vdso.so.1
164 x86/x32 linux\-vdso.so.1
166 .if t \{\
168 .ft P
170 .SS strace(1), seccomp(2), and the vDSO
171 When tracing systems calls with
172 .BR strace (1),
173 symbols (system calls) that are exported by the vDSO will
174 .I not
175 appear in the trace output.
176 Those system calls will likewise not be visible to
177 .BR seccomp (2)
178 filters.
179 .SH ARCHITECTURE-SPECIFIC NOTES
180 The subsections below provide architecture-specific notes
181 on the vDSO.
183 Note that the vDSO that is used is based on the ABI of your user-space code
184 and not the ABI of the kernel.
185 Thus, for example,
186 when you run an i386 32-bit ELF binary,
187 you'll get the same vDSO regardless of whether you run it under
188 an i386 32-bit kernel or under an x86-64 64-bit kernel.
189 Therefore, the name of the user-space ABI should be used to determine
190 which of the sections below is relevant.
191 .SS ARM functions
192 .\" See linux/arch/arm/vdso/vdso.lds.S
193 .\" Commit: 8512287a8165592466cb9cb347ba94892e9c56a5
194 The table below lists the symbols exported by the vDSO.
195 .if t \{\
196 .ft CW
199 l l.
200 symbol  version
202 __vdso_gettimeofday     LINUX_2.6 (exported since Linux 4.1)
203 __vdso_clock_gettime    LINUX_2.6 (exported since Linux 4.1)
205 .if t \{\
207 .ft P
210 .\" See linux/arch/arm/kernel/entry-armv.S
211 .\" See linux/Documentation/arm/kernel_user_helpers.txt
212 Additionally, the ARM port has a code page full of utility functions.
213 Since it's just a raw page of code, there is no ELF information for doing
214 symbol lookups or versioning.
215 It does provide support for different versions though.
217 For information on this code page,
218 it's best to refer to the kernel documentation
219 as it's extremely detailed and covers everything you need to know:
220 .IR Documentation/arm/kernel_user_helpers.txt .
221 .SS aarch64 functions
222 .\" See linux/arch/arm64/kernel/vdso/vdso.lds.S
223 The table below lists the symbols exported by the vDSO.
224 .if t \{\
225 .ft CW
228 l l.
229 symbol  version
231 __kernel_rt_sigreturn   LINUX_2.6.39
232 __kernel_gettimeofday   LINUX_2.6.39
233 __kernel_clock_gettime  LINUX_2.6.39
234 __kernel_clock_getres   LINUX_2.6.39
236 .if t \{\
238 .ft P
240 .SS bfin (Blackfin) functions (port removed in Linux 4.17)
241 .\" See linux/arch/blackfin/kernel/fixed_code.S
242 .\" See http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:fixed-code
243 As this CPU lacks a memory management unit (MMU),
244 it doesn't set up a vDSO in the normal sense.
245 Instead, it maps at boot time a few raw functions into
246 a fixed location in memory.
247 User-space applications then call directly into that region.
248 There is no provision for backward compatibility
249 beyond sniffing raw opcodes,
250 but as this is an embedded CPU, it can get away with things\(emsome of the
251 object formats it runs aren't even ELF based (they're bFLT/FLAT).
253 For information on this code page,
254 it's best to refer to the public documentation:
256 http://docs.blackfin.uclinux.org/doku.php?id=linux\-kernel:fixed\-code
257 .SS mips functions
258 .\" See linux/arch/mips/vdso/vdso.ld.S
260 The table below lists the symbols exported by the vDSO.
261 .if t \{\
262 .ft CW
265 l l.
266 symbol  version
268 __kernel_gettimeofday   LINUX_2.6 (exported since Linux 4.4)
269 __kernel_clock_gettime  LINUX_2.6 (exported since Linux 4.4)
271 .if t \{\
273 .ft P
275 .SS ia64 (Itanium) functions
276 .\" See linux/arch/ia64/kernel/gate.lds.S
277 .\" Also linux/arch/ia64/kernel/fsys.S and linux/Documentation/ia64/fsys.txt
278 The table below lists the symbols exported by the vDSO.
279 .if t \{\
280 .ft CW
283 l l.
284 symbol  version
286 __kernel_sigtramp       LINUX_2.5
287 __kernel_syscall_via_break      LINUX_2.5
288 __kernel_syscall_via_epc        LINUX_2.5
290 .if t \{\
292 .ft P
295 The Itanium port is somewhat tricky.
296 In addition to the vDSO above, it also has "light-weight system calls"
297 (also known as "fast syscalls" or "fsys").
298 You can invoke these via the
299 .I __kernel_syscall_via_epc
300 vDSO helper.
301 The system calls listed here have the same semantics as if you called them
302 directly via
303 .BR syscall (2),
304 so refer to the relevant
305 documentation for each.
306 The table below lists the functions available via this mechanism.
307 .if t \{\
308 .ft CW
312 function
314 clock_gettime
315 getcpu
316 getpid
317 getppid
318 gettimeofday
319 set_tid_address
321 .if t \{\
323 .ft P
325 .SS parisc (hppa) functions
326 .\" See linux/arch/parisc/kernel/syscall.S
327 .\" See linux/Documentation/parisc/registers
328 The parisc port has a code page with utility functions
329 called a gateway page.
330 Rather than use the normal ELF auxiliary vector approach,
331 it passes the address of
332 the page to the process via the SR2 register.
333 The permissions on the page are such that merely executing those addresses
334 automatically executes with kernel privileges and not in user space.
335 This is done to match the way HP-UX works.
337 Since it's just a raw page of code, there is no ELF information for doing
338 symbol lookups or versioning.
339 Simply call into the appropriate offset via the branch instruction,
340 for example:
342     ble <offset>(%sr2, %r0)
343 .if t \{\
344 .ft CW
347 l l.
348 offset  function
350 00b0    lws_entry (CAS operations)
351 00e0    set_thread_pointer (used by glibc)
352 0100    linux_gateway_entry (syscall)
354 .if t \{\
356 .ft P
358 .SS ppc/32 functions
359 .\" See linux/arch/powerpc/kernel/vdso32/vdso32.lds.S
360 The table below lists the symbols exported by the vDSO.
361 The functions marked with a
362 .I *
363 are available only when the kernel is
364 a PowerPC64 (64-bit) kernel.
365 .if t \{\
366 .ft CW
369 l l.
370 symbol  version
372 __kernel_clock_getres   LINUX_2.6.15
373 __kernel_clock_gettime  LINUX_2.6.15
374 __kernel_datapage_offset        LINUX_2.6.15
375 __kernel_get_syscall_map        LINUX_2.6.15
376 __kernel_get_tbfreq     LINUX_2.6.15
377 __kernel_getcpu \fI*\fR LINUX_2.6.15
378 __kernel_gettimeofday   LINUX_2.6.15
379 __kernel_sigtramp_rt32  LINUX_2.6.15
380 __kernel_sigtramp32     LINUX_2.6.15
381 __kernel_sync_dicache   LINUX_2.6.15
382 __kernel_sync_dicache_p5        LINUX_2.6.15
384 .if t \{\
386 .ft P
390 .B CLOCK_REALTIME_COARSE
392 .B CLOCK_MONOTONIC_COARSE
393 clocks are
394 .I not
395 supported by the
396 .I __kernel_clock_getres
398 .I __kernel_clock_gettime
399 interfaces;
400 the kernel falls back to the real system call.
401 .SS ppc/64 functions
402 .\" See linux/arch/powerpc/kernel/vdso64/vdso64.lds.S
403 The table below lists the symbols exported by the vDSO.
404 .if t \{\
405 .ft CW
408 l l.
409 symbol  version
411 __kernel_clock_getres   LINUX_2.6.15
412 __kernel_clock_gettime  LINUX_2.6.15
413 __kernel_datapage_offset        LINUX_2.6.15
414 __kernel_get_syscall_map        LINUX_2.6.15
415 __kernel_get_tbfreq     LINUX_2.6.15
416 __kernel_getcpu LINUX_2.6.15
417 __kernel_gettimeofday   LINUX_2.6.15
418 __kernel_sigtramp_rt64  LINUX_2.6.15
419 __kernel_sync_dicache   LINUX_2.6.15
420 __kernel_sync_dicache_p5        LINUX_2.6.15
422 .if t \{\
424 .ft P
428 .B CLOCK_REALTIME_COARSE
430 .B CLOCK_MONOTONIC_COARSE
431 clocks are
432 .I not
433 supported by the
434 .I __kernel_clock_getres
436 .I __kernel_clock_gettime
437 interfaces;
438 the kernel falls back to the real system call.
439 .SS riscv functions
440 .\" See linux/arch/riscv/kernel/vdso/vdso.lds.S
441 The table below lists the symbols exported by the vDSO.
442 .if t \{\
443 .ft CW
446 l l.
447 symbol  version
449 __kernel_rt_sigreturn   LINUX_4.15
450 __kernel_gettimeofday   LINUX_4.15
451 __kernel_clock_gettime  LINUX_4.15
452 __kernel_clock_getres   LINUX_4.15
453 __kernel_getcpu LINUX_4.15
454 __kernel_flush_icache   LINUX_4.15
456 .if t \{\
458 .ft P
460 .SS s390 functions
461 .\" See linux/arch/s390/kernel/vdso32/vdso32.lds.S
462 The table below lists the symbols exported by the vDSO.
463 .if t \{\
464 .ft CW
467 l l.
468 symbol  version
470 __kernel_clock_getres   LINUX_2.6.29
471 __kernel_clock_gettime  LINUX_2.6.29
472 __kernel_gettimeofday   LINUX_2.6.29
474 .if t \{\
476 .ft P
478 .SS s390x functions
479 .\" See linux/arch/s390/kernel/vdso64/vdso64.lds.S
480 The table below lists the symbols exported by the vDSO.
481 .if t \{\
482 .ft CW
485 l l.
486 symbol  version
488 __kernel_clock_getres   LINUX_2.6.29
489 __kernel_clock_gettime  LINUX_2.6.29
490 __kernel_gettimeofday   LINUX_2.6.29
492 .if t \{\
494 .ft P
496 .SS sh (SuperH) functions
497 .\" See linux/arch/sh/kernel/vsyscall/vsyscall.lds.S
498 The table below lists the symbols exported by the vDSO.
499 .if t \{\
500 .ft CW
503 l l.
504 symbol  version
506 __kernel_rt_sigreturn   LINUX_2.6
507 __kernel_sigreturn      LINUX_2.6
508 __kernel_vsyscall       LINUX_2.6
510 .if t \{\
512 .ft P
514 .SS i386 functions
515 .\" See linux/arch/x86/vdso/vdso32/vdso32.lds.S
516 The table below lists the symbols exported by the vDSO.
517 .if t \{\
518 .ft CW
521 l l.
522 symbol  version
524 __kernel_sigreturn      LINUX_2.5
525 __kernel_rt_sigreturn   LINUX_2.5
526 __kernel_vsyscall       LINUX_2.5
527 .\" Added in 7a59ed415f5b57469e22e41fc4188d5399e0b194 and updated
528 .\" in 37c975545ec63320789962bf307f000f08fabd48.
529 __vdso_clock_gettime    LINUX_2.6 (exported since Linux 3.15)
530 __vdso_gettimeofday     LINUX_2.6 (exported since Linux 3.15)
531 __vdso_time     LINUX_2.6 (exported since Linux 3.15)
533 .if t \{\
535 .ft P
537 .SS x86-64 functions
538 .\" See linux/arch/x86/vdso/vdso.lds.S
539 The table below lists the symbols exported by the vDSO.
540 All of these symbols are also available without the "__vdso_" prefix, but
541 you should ignore those and stick to the names below.
542 .if t \{\
543 .ft CW
546 l l.
547 symbol  version
549 __vdso_clock_gettime    LINUX_2.6
550 __vdso_getcpu   LINUX_2.6
551 __vdso_gettimeofday     LINUX_2.6
552 __vdso_time     LINUX_2.6
554 .if t \{\
556 .ft P
558 .SS x86/x32 functions
559 .\" See linux/arch/x86/vdso/vdso32.lds.S
560 The table below lists the symbols exported by the vDSO.
561 .if t \{\
562 .ft CW
565 l l.
566 symbol  version
568 __vdso_clock_gettime    LINUX_2.6
569 __vdso_getcpu   LINUX_2.6
570 __vdso_gettimeofday     LINUX_2.6
571 __vdso_time     LINUX_2.6
573 .if t \{\
575 .ft P
577 .SS History
578 The vDSO was originally just a single function\(emthe vsyscall.
579 In older kernels, you might see that name
580 in a process's memory map rather than "vdso".
581 Over time, people realized that this mechanism
582 was a great way to pass more functionality
583 to user space, so it was reconceived as a vDSO in the current format.
584 .SH SEE ALSO
585 .BR syscalls (2),
586 .BR getauxval (3),
587 .BR proc (5)
589 The documents, examples, and source code in the Linux source code tree:
591 .in +4n
593 Documentation/ABI/stable/vdso
594 Documentation/ia64/fsys.txt
595 Documentation/vDSO/* (includes examples of using the vDSO)
597 find arch/ \-iname \(aq*vdso*\(aq \-o \-iname \(aq*gate*\(aq