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