configure.ac: Sort AC_CHECK_FUNCS() arguments alphabetically
[valgrind.git] / coregrind / m_syscall.c
blob4053d402474fe89986e1ae6aa62b75934c9bca91
2 /*--------------------------------------------------------------------*/
3 /*--- Doing syscalls. m_syscall.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2017 Julian Seward
11 jseward@acm.org
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics.h"
30 #include "pub_core_libcassert.h"
31 #include "pub_core_vki.h"
32 #include "pub_core_vkiscnums.h"
33 #include "pub_core_syscall.h"
35 /* ---------------------------------------------------------------------
36 Building syscall return values.
37 ------------------------------------------------------------------ */
39 /* Make a SysRes value from a syscall return value. This is
40 platform specific. */
42 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
44 SysRes VG_(mk_SysRes_mips32_linux) ( UWord v0, UWord v1, UWord a3 ) {
45 /* MIPS uses a3 != 0 to flag an error */
46 SysRes res;
47 res._isError = (a3 != (UWord)0);
48 res._val = v0;
49 res._valEx = v1;
50 return res;
53 SysRes VG_(mk_SysRes_mips64_linux) ( ULong v0, ULong v1, ULong a3 ) {
54 /* MIPS uses a3 != 0 to flag an error */
55 SysRes res;
56 res._isError = (a3 != (ULong)0);
57 res._val = v0;
58 res._valEx = v1;
59 return res;
62 /* Generic constructors. */
63 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
64 SysRes r;
65 r._isError = True;
66 r._val = err;
67 r._valEx = 0;
68 return r;
71 SysRes VG_(mk_SysRes_Success) ( UWord res ) {
72 SysRes r;
73 r._isError = False;
74 r._val = res;
75 r._valEx = 0;
76 return r;
79 SysRes VG_(mk_SysRes_SuccessEx) ( UWord res, UWord resEx ) {
80 SysRes r;
81 r._isError = False;
82 r._val = res;
83 r._valEx = resEx;
84 return r;
88 #elif defined(VGO_linux) \
89 && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux)
92 From:
93 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/
94 linux/i386/sysdep.h?
95 rev=1.28&content-type=text/x-cvsweb-markup&cvsroot=glibc
97 Linux uses a negative return value to indicate syscall errors,
98 unlike most Unices, which use the condition codes' carry flag.
100 Since version 2.1 the return value of a system call might be
101 negative even if the call succeeded. E.g., the 'lseek' system call
102 might return a large offset. Therefore we must not anymore test
103 for < 0, but test for a real error by making sure the value in %eax
104 is a real error number. Linus said he will make sure the no
105 syscall returns a value in -1 .. -4095 as a valid result so we can
106 safely test with -4095.
109 SysRes VG_(mk_SysRes_nanomips_linux) ( UWord a0 ) {
110 SysRes res;
111 res._isError = (a0 > 0xFFFFF000ul);
112 res._val = a0;
113 return res;
116 SysRes VG_(mk_SysRes_x86_linux) ( Int val ) {
117 SysRes res;
118 res._isError = val >= -4095 && val <= -1;
119 if (res._isError) {
120 res._val = (UInt)(-val);
121 } else {
122 res._val = (UInt)val;
124 return res;
127 /* Similarly .. */
128 SysRes VG_(mk_SysRes_amd64_linux) ( Long val ) {
129 SysRes res;
130 res._isError = val >= -4095 && val <= -1;
131 if (res._isError) {
132 res._val = (ULong)(-val);
133 } else {
134 res._val = (ULong)val;
136 return res;
139 /* PPC uses the CR7.SO bit to flag an error (CR0 in IBM-speak) */
140 /* Note this must be in the bottom bit of the second arg */
141 SysRes VG_(mk_SysRes_ppc32_linux) ( UInt val, UInt cr0so ) {
142 SysRes res;
143 res._isError = (cr0so & 1) != 0;
144 res._val = val;
145 return res;
148 /* As per ppc32 version, cr0.so must be in l.s.b. of 2nd arg */
149 SysRes VG_(mk_SysRes_ppc64_linux) ( ULong val, ULong cr0so ) {
150 SysRes res;
151 res._isError = (cr0so & 1) != 0;
152 res._val = val;
153 return res;
156 SysRes VG_(mk_SysRes_s390x_linux) ( Long val ) {
157 SysRes res;
158 res._isError = val >= -4095 && val <= -1;
159 if (res._isError) {
160 res._val = -val;
161 } else {
162 res._val = val;
164 return res;
167 SysRes VG_(mk_SysRes_arm_linux) ( Int val ) {
168 SysRes res;
169 res._isError = val >= -4095 && val <= -1;
170 if (res._isError) {
171 res._val = (UInt)(-val);
172 } else {
173 res._val = (UInt)val;
175 return res;
178 SysRes VG_(mk_SysRes_arm64_linux) ( Long val ) {
179 SysRes res;
180 res._isError = val >= -4095 && val <= -1;
181 if (res._isError) {
182 res._val = (ULong)(-val);
183 } else {
184 res._val = (ULong)val;
186 return res;
189 /* Generic constructors. */
190 SysRes VG_(mk_SysRes_Success) ( UWord res ) {
191 SysRes r;
192 r._isError = False;
193 r._val = res;
194 return r;
197 #if defined(VGP_nanomips_linux)
198 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
199 SysRes r;
200 r._isError = True;
201 r._val = (UWord)(-(Word)err);
202 return r;
204 #else
205 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
206 SysRes r;
207 r._isError = True;
208 r._val = err;
209 return r;
212 #endif
215 #elif defined(VGO_darwin)
217 /* Darwin: Some syscalls return a double-word result. */
218 SysRes VG_(mk_SysRes_x86_darwin) ( UChar scclass, Bool isErr,
219 UInt wHI, UInt wLO )
221 SysRes res;
222 res._wHI = 0;
223 res._wLO = 0;
224 res._mode = 0; /* invalid */
225 vg_assert(isErr == False || isErr == True);
226 vg_assert(sizeof(UWord) == sizeof(UInt));
227 switch (scclass) {
228 case VG_DARWIN_SYSCALL_CLASS_UNIX:
229 res._wLO = wLO;
230 res._wHI = wHI;
231 res._mode = isErr ? SysRes_UNIX_ERR : SysRes_UNIX_OK;
232 break;
233 case VG_DARWIN_SYSCALL_CLASS_MACH:
234 vg_assert(!isErr);
235 vg_assert(wHI == 0);
236 res._wLO = wLO;
237 res._mode = SysRes_MACH;
238 break;
239 case VG_DARWIN_SYSCALL_CLASS_MDEP:
240 vg_assert(!isErr);
241 vg_assert(wHI == 0);
242 res._wLO = wLO;
243 res._mode = SysRes_MDEP;
244 break;
245 default:
246 vg_assert(0);
248 return res;
251 SysRes VG_(mk_SysRes_amd64_darwin) ( UChar scclass, Bool isErr,
252 ULong wHI, ULong wLO )
254 SysRes res;
255 res._wHI = 0;
256 res._wLO = 0;
257 res._mode = 0; /* invalid */
258 vg_assert(isErr == False || isErr == True);
259 vg_assert(sizeof(UWord) == sizeof(ULong));
260 switch (scclass) {
261 case VG_DARWIN_SYSCALL_CLASS_UNIX:
262 res._wLO = wLO;
263 res._wHI = wHI;
264 res._mode = isErr ? SysRes_UNIX_ERR : SysRes_UNIX_OK;
265 break;
266 case VG_DARWIN_SYSCALL_CLASS_MACH:
267 vg_assert(!isErr);
268 vg_assert(wHI == 0);
269 res._wLO = wLO;
270 res._mode = SysRes_MACH;
271 break;
272 case VG_DARWIN_SYSCALL_CLASS_MDEP:
273 vg_assert(!isErr);
274 vg_assert(wHI == 0);
275 res._wLO = wLO;
276 res._mode = SysRes_MDEP;
277 break;
278 default:
279 vg_assert(0);
281 return res;
284 /* Generic constructors. We assume (without checking if this makes
285 any sense, from the caller's point of view) that these are for the
286 UNIX style of syscall. */
287 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
288 SysRes r;
289 r._wHI = 0;
290 r._wLO = err;
291 r._mode = SysRes_UNIX_ERR;
292 return r;
295 SysRes VG_(mk_SysRes_Success) ( UWord res ) {
296 SysRes r;
297 r._wHI = 0;
298 r._wLO = res;
299 r._mode = SysRes_UNIX_OK;
300 return r;
304 #elif defined(VGO_solaris)
306 /* Generic constructors. */
307 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
308 SysRes r;
309 r._val = err;
310 r._val2 = 0;
311 r._isError = True;
312 return r;
315 SysRes VG_(mk_SysRes_Success) ( UWord res ) {
316 SysRes r;
317 r._val = res;
318 r._val2 = 0;
319 r._isError = False;
320 return r;
323 SysRes VG_(mk_SysRes_x86_solaris) ( Bool isErr, UInt val, UInt val2 )
325 SysRes res;
327 // stay sane
328 vg_assert(isErr == True || isErr == False);
330 res._val = val;
331 res._val2 = val2;
332 res._isError = isErr;
333 return res;
336 SysRes VG_(mk_SysRes_amd64_solaris) ( Bool isErr, ULong val, ULong val2 )
338 SysRes res;
340 // stay sane
341 vg_assert(isErr == True || isErr == False);
343 res._val = val;
344 res._val2 = val2;
345 res._isError = isErr;
346 return res;
349 #else
350 # error "Unknown OS"
351 #endif
354 /* ---------------------------------------------------------------------
355 VG_(do_syscall): A function for doing syscalls.
356 ------------------------------------------------------------------ */
358 #if defined(VGP_x86_linux)
359 /* Incoming args (syscall number + up to 6 args) come on the stack.
360 (ie. the C calling convention).
362 The syscall number goes in %eax. The args are passed to the syscall in
363 the regs %ebx, %ecx, %edx, %esi, %edi, %ebp, ie. the kernel's syscall
364 calling convention.
366 %eax gets the return value. Not sure which registers the kernel
367 clobbers, so we preserve all the callee-save regs (%esi, %edi, %ebx,
368 %ebp).
370 extern UWord do_syscall_WRK (
371 UWord syscall_no,
372 UWord a1, UWord a2, UWord a3,
373 UWord a4, UWord a5, UWord a6
375 asm(
376 ".text\n"
377 ".globl do_syscall_WRK\n"
378 "do_syscall_WRK:\n"
379 " .cfi_startproc\n"
380 " push %esi\n"
381 " .cfi_adjust_cfa_offset 4\n"
382 " .cfi_offset %esi, -8\n"
383 " push %edi\n"
384 " .cfi_adjust_cfa_offset 4\n"
385 " .cfi_offset %edi, -12\n"
386 " push %ebx\n"
387 " .cfi_adjust_cfa_offset 4\n"
388 " .cfi_offset %ebx, -16\n"
389 " push %ebp\n"
390 " .cfi_adjust_cfa_offset 4\n"
391 " .cfi_offset %ebp, -20\n"
392 " movl 16+ 4(%esp),%eax\n"
393 " movl 16+ 8(%esp),%ebx\n"
394 " movl 16+12(%esp),%ecx\n"
395 " movl 16+16(%esp),%edx\n"
396 " movl 16+20(%esp),%esi\n"
397 " movl 16+24(%esp),%edi\n"
398 " movl 16+28(%esp),%ebp\n"
399 " int $0x80\n"
400 " popl %ebp\n"
401 " .cfi_adjust_cfa_offset -4\n"
402 " .cfi_restore %ebp\n"
403 " popl %ebx\n"
404 " .cfi_adjust_cfa_offset -4\n"
405 " .cfi_restore %ebx\n"
406 " popl %edi\n"
407 " .cfi_adjust_cfa_offset -4\n"
408 " .cfi_restore %edi\n"
409 " popl %esi\n"
410 " .cfi_adjust_cfa_offset -4\n"
411 " .cfi_restore %esi\n"
412 " ret\n"
413 " .cfi_endproc\n"
414 ".previous\n"
417 #elif defined(VGP_amd64_linux)
418 /* Incoming args (syscall number + up to 6 args) come in %rdi, %rsi,
419 %rdx, %rcx, %r8, %r9, and the last one on the stack (ie. the C
420 calling convention).
422 The syscall number goes in %rax. The args are passed to the syscall in
423 the regs %rdi, %rsi, %rdx, %r10, %r8, %r9 (yes, really %r10, not %rcx),
424 ie. the kernel's syscall calling convention.
426 %rax gets the return value. %rcx and %r11 are clobbered by the syscall;
427 no matter, they are caller-save (the syscall clobbers no callee-save
428 regs, so we don't have to do any register saving/restoring).
430 extern UWord do_syscall_WRK (
431 UWord syscall_no,
432 UWord a1, UWord a2, UWord a3,
433 UWord a4, UWord a5, UWord a6
435 asm(
436 ".text\n"
437 ".globl do_syscall_WRK\n"
438 "do_syscall_WRK:\n"
439 /* Convert function calling convention --> syscall calling
440 convention */
441 " movq %rdi, %rax\n"
442 " movq %rsi, %rdi\n"
443 " movq %rdx, %rsi\n"
444 " movq %rcx, %rdx\n"
445 " movq %r8, %r10\n"
446 " movq %r9, %r8\n"
447 " movq 8(%rsp), %r9\n" /* last arg from stack */
448 " syscall\n"
449 " ret\n"
450 ".previous\n"
453 #elif defined(VGP_ppc32_linux)
454 /* Incoming args (syscall number + up to 6 args) come in %r3:%r9.
456 The syscall number goes in %r0. The args are passed to the syscall in
457 the regs %r3:%r8, i.e. the kernel's syscall calling convention.
459 The %cr0.so bit flags an error.
460 We return the syscall return value in %r3, and the %cr0.so in
461 the lowest bit of %r4.
462 We return a ULong, of which %r3 is the high word, and %r4 the low.
463 No callee-save regs are clobbered, so no saving/restoring is needed.
465 extern ULong do_syscall_WRK (
466 UWord syscall_no,
467 UWord a1, UWord a2, UWord a3,
468 UWord a4, UWord a5, UWord a6
470 asm(
471 ".text\n"
472 ".globl do_syscall_WRK\n"
473 "do_syscall_WRK:\n"
474 " mr 0,3\n"
475 " mr 3,4\n"
476 " mr 4,5\n"
477 " mr 5,6\n"
478 " mr 6,7\n"
479 " mr 7,8\n"
480 " mr 8,9\n"
481 " sc\n" /* syscall: sets %cr0.so on error */
482 " mfcr 4\n" /* %cr -> low word of return var */
483 " rlwinm 4,4,4,31,31\n" /* rotate flag bit so to lsb, and mask it */
484 " blr\n" /* and return */
485 ".previous\n"
488 #elif defined(VGP_ppc64be_linux)
489 /* Due to the need to return 65 bits of result, this is completely
490 different from the ppc32 case. The single arg register points to a
491 7-word block containing the syscall # and the 6 args. The syscall
492 result proper is put in [0] of the block, and %cr0.so is in the
493 bottom bit of [1]. */
494 extern void do_syscall_WRK ( ULong* argblock );
495 asm(
496 ".align 2\n"
497 ".globl do_syscall_WRK\n"
498 ".section \".opd\",\"aw\"\n"
499 ".align 3\n"
500 "do_syscall_WRK:\n"
501 ".quad .do_syscall_WRK,.TOC.@tocbase,0\n"
502 ".previous\n"
503 ".type .do_syscall_WRK,@function\n"
504 ".globl .do_syscall_WRK\n"
505 ".do_syscall_WRK:\n"
506 " std 3,-16(1)\n" /* stash arg */
507 " ld 8, 48(3)\n" /* sc arg 6 */
508 " ld 7, 40(3)\n" /* sc arg 5 */
509 " ld 6, 32(3)\n" /* sc arg 4 */
510 " ld 5, 24(3)\n" /* sc arg 3 */
511 " ld 4, 16(3)\n" /* sc arg 2 */
512 " ld 0, 0(3)\n" /* sc number */
513 " ld 3, 8(3)\n" /* sc arg 1 */
514 " sc\n" /* result in r3 and cr0.so */
515 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
516 " std 3,0(5)\n" /* argblock[0] = r3 */
517 " mfcr 3\n"
518 " srwi 3,3,28\n"
519 " andi. 3,3,1\n"
520 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
521 " blr\n"
524 #elif defined(VGP_ppc64le_linux)
525 /* Due to the need to return 65 bits of result, this is completely
526 different from the ppc32 case. The single arg register points to a
527 7-word block containing the syscall # and the 6 args. The syscall
528 result proper is put in [0] of the block, and %cr0.so is in the
529 bottom bit of [1]. */
530 extern void do_syscall_WRK ( ULong* argblock );
531 /* Little Endian supports ELF version 2. In the future, it may support
532 * other versions as well.
534 asm(
535 ".align 2\n"
536 ".globl do_syscall_WRK\n"
537 ".type do_syscall_WRK,@function\n"
538 "do_syscall_WRK:\n"
539 "#if _CALL_ELF == 2" "\n"
540 "0: addis 2,12,.TOC.-0b@ha\n"
541 " addi 2,2,.TOC.-0b@l\n"
542 " .localentry do_syscall_WRK, .-do_syscall_WRK\n"
543 "#endif" "\n"
544 " std 3,-16(1)\n" /* stash arg */
545 " ld 8, 48(3)\n" /* sc arg 6 */
546 " ld 7, 40(3)\n" /* sc arg 5 */
547 " ld 6, 32(3)\n" /* sc arg 4 */
548 " ld 5, 24(3)\n" /* sc arg 3 */
549 " ld 4, 16(3)\n" /* sc arg 2 */
550 " ld 0, 0(3)\n" /* sc number */
551 " ld 3, 8(3)\n" /* sc arg 1 */
552 " sc\n" /* result in r3 and cr0.so */
553 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
554 " std 3,0(5)\n" /* argblock[0] = r3 */
555 " mfcr 3\n"
556 " srwi 3,3,28\n"
557 " andi. 3,3,1\n"
558 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
559 " blr\n"
560 " .size do_syscall_WRK, .-do_syscall_WRK\n"
563 #elif defined(VGP_arm_linux)
564 /* I think the conventions are:
565 args in r0 r1 r2 r3 r4 r5
566 sysno in r7
567 return value in r0, w/ same conventions as x86-linux, viz r0 in
568 -4096 .. -1 is an error value. All other values are success
569 values.
571 extern UWord do_syscall_WRK (
572 UWord a1, UWord a2, UWord a3,
573 UWord a4, UWord a5, UWord a6,
574 UWord syscall_no
576 asm(
577 ".text\n"
578 ".globl do_syscall_WRK\n"
579 "do_syscall_WRK:\n"
580 " push {r4, r5, r7}\n"
581 " ldr r4, [sp, #12]\n"
582 " ldr r5, [sp, #16]\n"
583 " ldr r7, [sp, #20]\n"
584 " svc 0x0\n"
585 " pop {r4, r5, r7}\n"
586 " bx lr\n"
587 ".previous\n"
590 #elif defined(VGP_arm64_linux)
591 /* I think the conventions are:
592 args in r0 r1 r2 r3 r4 r5
593 sysno in r8
594 return value in r0, w/ same conventions as x86-linux, viz r0 in
595 -4096 .. -1 is an error value. All other values are success
596 values.
598 r0 to r5 remain unchanged, but syscall_no is in r6 and needs
599 to be moved to r8 (??)
601 extern UWord do_syscall_WRK (
602 UWord a1, UWord a2, UWord a3,
603 UWord a4, UWord a5, UWord a6,
604 UWord syscall_no
606 asm(
607 ".text\n"
608 ".globl do_syscall_WRK\n"
609 "do_syscall_WRK:\n"
610 " mov x8, x6\n"
611 " mov x6, 0\n"
612 " mov x7, 0\n"
613 " svc 0\n"
614 " ret\n"
615 ".previous\n"
618 #elif defined(VGP_x86_darwin)
620 /* Incoming args (syscall number + up to 8 args) come in on the stack
622 The kernel's syscall calling convention is:
623 * the syscall number goes in eax
624 * the args are passed to the syscall on the stack,
625 pushed onto the stack R->L (that is, the usual x86
626 calling conventions, with the leftmost arg at the lowest
627 address)
628 Call instruction:
629 * UNIX: sysenter
630 * UNIX: int $0x80
631 * MACH: int $0x81
632 * MDEP: int $0x82
633 Note that the call type can be determined from the syscall number;
634 there is no need to inspect the actual instruction. Although obviously
635 the instruction must match.
636 Return value:
637 * MACH,MDEP: the return value comes back in eax
638 * UNIX: the return value comes back in edx:eax (hi32:lo32)
639 Error:
640 * MACH,MDEP: no error is returned
641 * UNIX: the carry flag indicates success or failure
643 nb here, sizeof(UWord) == sizeof(UInt)
646 __private_extern__ ULong
647 do_syscall_unix_WRK ( UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
648 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
649 UWord a7, UWord a8, /* 28(esp)..32(esp) */
650 UWord syscall_no, /* 36(esp) */
651 /*OUT*/UInt* errflag /* 40(esp) */ );
652 // Unix syscall: 64-bit return in edx:eax, with LSB in eax
653 // error indicated by carry flag: clear=good, set=bad
654 asm(".private_extern _do_syscall_unix_WRK\n"
655 "_do_syscall_unix_WRK:\n"
656 " movl 40(%esp), %ecx \n" /* assume syscall success */
657 " movl $0, (%ecx) \n"
658 " movl 36(%esp), %eax \n"
659 " int $0x80 \n"
660 " jnc 1f \n" /* jump if success */
661 " movl 40(%esp), %ecx \n" /* syscall failed - set *errflag */
662 " movl $1, (%ecx) \n"
663 " 1: ret \n"
666 __private_extern__ UInt
667 do_syscall_mach_WRK ( UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
668 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
669 UWord a7, UWord a8, /* 28(esp)..32(esp) */
670 UWord syscall_no /* 36(esp) */ );
671 // Mach trap: 32-bit result in %eax, no error flag
672 asm(".private_extern _do_syscall_mach_WRK\n"
673 "_do_syscall_mach_WRK:\n"
674 " movl 36(%esp), %eax \n"
675 " int $0x81 \n"
676 " ret \n"
679 __private_extern__ UInt
680 do_syscall_mdep_WRK ( UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
681 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
682 UWord a7, UWord a8, /* 28(esp)..32(esp) */
683 UWord syscall_no /* 36(esp) */ );
684 // mdep trap: 32-bit result in %eax, no error flag
685 asm(
686 ".private_extern _do_syscall_mdep_WRK\n"
687 "_do_syscall_mdep_WRK:\n"
688 " movl 36(%esp), %eax \n"
689 " int $0x82 \n"
690 " ret \n"
694 #elif defined(VGP_amd64_darwin)
696 /* Incoming args (syscall number + up to 8 args) come in registers and stack
698 The kernel's syscall calling convention is:
699 * the syscall number goes in rax
700 * the args are passed to the syscall in registers and the stack
701 * the call instruction is 'syscall'
702 Return value:
703 * MACH,MDEP: the return value comes back in rax
704 * UNIX: the return value comes back in rdx:rax (hi64:lo64)
705 Error:
706 * MACH,MDEP: no error is returned
707 * UNIX: the carry flag indicates success or failure
709 nb here, sizeof(UWord) == sizeof(ULong)
712 __private_extern__ UWord
713 do_syscall_unix_WRK ( UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */
714 UWord a4, UWord a5, UWord a6, /* rcx, r8, r9 */
715 UWord a7, UWord a8, /* 8(rsp), 16(rsp) */
716 UWord syscall_no, /* 24(rsp) */
717 /*OUT*/ULong* errflag, /* 32(rsp) */
718 /*OUT*/ULong* res2 ); /* 40(rsp) */
719 // Unix syscall: 128-bit return in rax:rdx, with LSB in rax
720 // error indicated by carry flag: clear=good, set=bad
721 asm(".private_extern _do_syscall_unix_WRK\n"
722 "_do_syscall_unix_WRK:\n"
723 " movq %rcx, %r10 \n" /* pass rcx in r10 instead */
724 " movq 32(%rsp), %rax \n" /* assume syscall success */
725 " movq $0, (%rax) \n"
726 " movq 24(%rsp), %rax \n" /* load syscall_no */
727 " syscall \n"
728 " jnc 1f \n" /* jump if success */
729 " movq 32(%rsp), %rcx \n" /* syscall failed - set *errflag */
730 " movq $1, (%rcx) \n"
731 " 1: movq 40(%rsp), %rcx \n" /* save 2nd result word */
732 " movq %rdx, (%rcx) \n"
733 " retq \n" /* return 1st result word */
736 __private_extern__ UWord
737 do_syscall_mach_WRK ( UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */
738 UWord a4, UWord a5, UWord a6, /* rcx, r8, r9 */
739 UWord a7, UWord a8, /* 8(rsp), 16(rsp) */
740 UWord syscall_no ); /* 24(rsp) */
741 // Mach trap: 64-bit result, no error flag
742 asm(".private_extern _do_syscall_mach_WRK\n"
743 "_do_syscall_mach_WRK:\n"
744 " movq %rcx, %r10 \n" /* pass rcx in r10 instead */
745 " movq 24(%rsp), %rax \n" /* load syscall_no */
746 " syscall \n"
747 " retq \n"
750 #elif defined(VGP_s390x_linux)
752 static UWord do_syscall_WRK (
753 UWord syscall_no,
754 UWord arg1, UWord arg2, UWord arg3,
755 UWord arg4, UWord arg5, UWord arg6
758 register UWord __arg1 asm("2") = arg1;
759 register UWord __arg2 asm("3") = arg2;
760 register UWord __arg3 asm("4") = arg3;
761 register UWord __arg4 asm("5") = arg4;
762 register UWord __arg5 asm("6") = arg5;
763 register UWord __arg6 asm("7") = arg6;
764 register ULong __svcres asm("2");
766 __asm__ __volatile__ (
767 "lgr %%r1,%1\n\t"
768 "svc 0\n\t"
769 : "=d" (__svcres)
770 : "a" (syscall_no),
771 "0" (__arg1),
772 "d" (__arg2),
773 "d" (__arg3),
774 "d" (__arg4),
775 "d" (__arg5),
776 "d" (__arg6)
777 : "1", "cc", "memory");
779 return (UWord) (__svcres);
782 #elif defined(VGP_mips32_linux)
783 /* Incoming args (syscall number + up to 6 args) come in a0 - a3 and stack.
785 The syscall number goes in v0. The args are passed to the syscall in
786 the regs a0 - a3 and stack, i.e. the kernel's syscall calling convention.
788 (a3 != 0) flags an error.
789 We return the syscall return value in v0.
790 MIPS version
792 extern int do_syscall_WRK (
793 int a1, int a2, int a3,
794 int a4, int a5, int a6, int a7, int syscall_no, UWord *err,
795 UWord *valHi, UWord* valLo
797 asm (
798 ".text \n\t"
799 ".globl do_syscall_WRK \n\t"
800 ".type do_syscall_WRK, @function \n\t"
801 ".set push \n\t"
802 ".set noreorder \n\t"
803 "do_syscall_WRK: \n\t"
804 " lw $2, 28($29) \n\t"
805 " syscall \n\t"
806 " lw $8, 32($29) \n\t"
807 " sw $7, ($8) \n\t"
808 " lw $8, 36($29) \n\t"
809 " sw $3, ($8) \n\t" /* store valHi */
810 " lw $8, 40($29) \n\t"
811 " jr $31 \n\t"
812 " sw $2, ($8) \n\t" /* store valLo */
813 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
814 ".set pop \n\t"
815 ".previous \n\t"
818 #elif defined(VGP_mips64_linux)
819 extern RegWord do_syscall_WRK ( RegWord a1, RegWord a2, RegWord a3, RegWord a4,
820 RegWord a5, RegWord a6, RegWord a7, RegWord syscall_no,
821 RegWord* V1_A3_val );
822 asm (
823 ".text \n\t"
824 ".globl do_syscall_WRK \n\t"
825 ".type do_syscall_WRK, @function \n\t"
826 ".set push \n\t"
827 ".set noreorder \n\t"
828 "do_syscall_WRK: \n\t"
829 " move $2, $11 \n\t"
830 " syscall \n\t"
831 # if defined(_ABI64)
832 " ld $12, 0($29) \n\t"
833 # elif defined(_ABIN32)
834 " lw $12, 0($29) \n\t"
835 # endif
836 " sd $3, 0($12) \n\t" /* store v1 in V1_A3_val */
837 " jr $31 \n\t"
838 " sd $7, 8($12) \n\t" /* store a3 in V1_A3_val */
839 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
840 ".set pop \n\t"
841 ".previous \n\t"
844 #elif defined(VGP_nanomips_linux)
845 extern void do_syscall_WRK (
846 RegWord a1, RegWord a2, RegWord a3,
847 RegWord a4, RegWord a5, RegWord a6,
848 RegWord syscall_no, RegWord *res_a0);
849 asm (
850 ".text \n\t"
851 ".globl do_syscall_WRK \n\t"
852 ".type do_syscall_WRK, @function \n\t"
853 ".set push \n\t"
854 ".set noreorder \n\t"
855 "do_syscall_WRK: \n\t"
856 " save 32, $a7 \n\t"
857 " move $t4, $a6 \n\t"
858 " syscall[32] \n\t"
859 " restore 32, $a7 \n\t"
860 " sw $a0, 0($a7) \n\t"
861 " jrc $ra \n\t"
862 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
863 ".set pop \n\t"
864 ".previous \n\t"
867 #elif defined(VGP_x86_solaris)
869 extern ULong
870 do_syscall_WRK(UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
871 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
872 UWord a7, UWord a8, /* 28(esp)..32(esp) */
873 UWord syscall_no, /* 36(esp) */
874 /*OUT*/UInt *errflag); /* 40(esp) */
875 /* Classic unix syscall.. parameters on the stack, an unused (by the kernel)
876 return address at 0(esp), a sysno in eax, a result in edx:eax, the carry
877 flag set on error. */
878 __asm__ (
879 ".text\n"
880 ".globl do_syscall_WRK\n"
881 "do_syscall_WRK:\n"
882 " movl 40(%esp), %ecx\n" /* assume syscall success */
883 " movl $0, (%ecx)\n"
884 " movl 36(%esp), %eax\n"
885 " int $0x91\n"
886 " jnc 1f\n" /* jump if success */
887 " movl 40(%esp), %ecx\n" /* syscall failed - set *errflag */
888 " movl $1, (%ecx)\n"
889 "1: ret\n"
890 ".previous\n"
893 extern ULong
894 do_syscall_fast_WRK(UWord syscall_no); /* 4(esp) */
895 /* Fasttrap syscall.. no parameters, a sysno in eax, a result in edx:eax,
896 never fails (if the sysno is valid). */
897 __asm__ (
898 ".text\n"
899 ".globl do_syscall_fast_WRK\n"
900 "do_syscall_fast_WRK:\n"
901 " movl 4(%esp), %eax\n"
902 " int $0xD2\n"
903 " ret\n"
904 ".previous\n"
907 #elif defined(VGP_amd64_solaris)
909 extern ULong
910 do_syscall_WRK(UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */
911 UWord a4, UWord a5, UWord a6, /* rcx, r8, r9 */
912 UWord a7, UWord a8, /* 8(rsp), 16(rsp) */
913 UWord syscall_no, /* 24(rsp) */
914 /*OUT*/ULong *errflag, /* 32(rsp) */
915 /*OUT*/ULong *res2); /* 40(rsp) */
916 /* First 6 parameters in registers rdi, rsi, rdx, r10, r8, r9, next
917 2 parameters on the stack, an unused (by the kernel) return address at
918 0(rsp), a sysno in rax, a result in rdx:rax, the carry flag set on
919 error. */
920 __asm__ (
921 ".text\n"
922 ".globl do_syscall_WRK\n"
923 "do_syscall_WRK:\n"
924 " movq %rcx, %r10\n" /* pass rcx in r10 instead */
925 " movq 32(%rsp), %rcx\n" /* assume syscall success */
926 " movq $0, (%rcx)\n"
927 " movq 24(%rsp), %rax\n"
928 " syscall\n"
929 " jnc 1f\n" /* jump if success */
930 " movq 32(%rsp), %rcx\n" /* syscall failed - set *errflag */
931 " movq $1, (%rcx)\n"
932 "1: movq 40(%rsp), %rcx\n" /* save 2nd result word */
933 " movq %rdx, (%rcx)\n"
934 " ret\n"
935 ".previous\n"
938 extern ULong
939 do_syscall_fast_WRK(UWord syscall_no, /* rdi */
940 /*OUT*/ULong *res2); /* rsi */
941 /* Fasttrap syscall.. no parameters, a sysno in rax, a result in rdx:rax,
942 never fails (if the sysno is valid). */
943 __asm__ (
944 ".text\n"
945 ".globl do_syscall_fast_WRK\n"
946 "do_syscall_fast_WRK:\n"
947 " movq %rdi, %rax\n"
948 " int $0xD2\n"
949 " movq %rdx, (%rsi)\n" /* save 2nd result word */
950 " ret\n"
951 ".previous\n"
954 #else
955 # error Unknown platform
956 #endif
959 /* Finally, the generic code. This sends the call to the right
960 helper. */
962 SysRes VG_(do_syscall) ( UWord sysno, RegWord a1, RegWord a2, RegWord a3,
963 RegWord a4, RegWord a5, RegWord a6,
964 RegWord a7, RegWord a8 )
966 # if defined(VGP_x86_linux)
967 UWord val = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
968 return VG_(mk_SysRes_x86_linux)( val );
970 # elif defined(VGP_amd64_linux)
971 UWord val = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
972 return VG_(mk_SysRes_amd64_linux)( val );
974 # elif defined(VGP_ppc32_linux)
975 ULong ret = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
976 UInt val = (UInt)(ret>>32);
977 UInt cr0so = (UInt)(ret);
978 return VG_(mk_SysRes_ppc32_linux)( val, cr0so );
980 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
981 ULong argblock[7];
982 argblock[0] = sysno;
983 argblock[1] = a1;
984 argblock[2] = a2;
985 argblock[3] = a3;
986 argblock[4] = a4;
987 argblock[5] = a5;
988 argblock[6] = a6;
989 do_syscall_WRK( &argblock[0] );
990 return VG_(mk_SysRes_ppc64_linux)( argblock[0], argblock[1] );
992 # elif defined(VGP_arm_linux)
993 UWord val = do_syscall_WRK(a1,a2,a3,a4,a5,a6,sysno);
994 return VG_(mk_SysRes_arm_linux)( val );
996 # elif defined(VGP_arm64_linux)
997 UWord val = do_syscall_WRK(a1,a2,a3,a4,a5,a6,sysno);
998 return VG_(mk_SysRes_arm64_linux)( val );
1000 # elif defined(VGP_x86_darwin)
1001 UInt wLO = 0, wHI = 0, err = 0;
1002 ULong u64;
1003 UChar scclass = VG_DARWIN_SYSNO_CLASS(sysno);
1004 switch (scclass) {
1005 case VG_DARWIN_SYSCALL_CLASS_UNIX:
1006 u64 = do_syscall_unix_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1007 VG_DARWIN_SYSNO_FOR_KERNEL(sysno), &err);
1008 wLO = (UInt)u64;
1009 wHI = (UInt)(u64 >> 32);
1010 break;
1011 case VG_DARWIN_SYSCALL_CLASS_MACH:
1012 wLO = do_syscall_mach_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1013 VG_DARWIN_SYSNO_FOR_KERNEL(sysno));
1014 err = 0;
1015 break;
1016 case VG_DARWIN_SYSCALL_CLASS_MDEP:
1017 wLO = do_syscall_mdep_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1018 VG_DARWIN_SYSNO_FOR_KERNEL(sysno));
1019 err = 0;
1020 break;
1021 default:
1022 vg_assert(0);
1023 break;
1025 return VG_(mk_SysRes_x86_darwin)( scclass, err ? True : False, wHI, wLO );
1027 # elif defined(VGP_amd64_darwin)
1028 ULong wLO = 0, wHI = 0, err = 0;
1029 UChar scclass = VG_DARWIN_SYSNO_CLASS(sysno);
1030 switch (scclass) {
1031 case VG_DARWIN_SYSCALL_CLASS_UNIX:
1032 wLO = do_syscall_unix_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1033 VG_DARWIN_SYSNO_FOR_KERNEL(sysno), &err, &wHI);
1034 break;
1035 case VG_DARWIN_SYSCALL_CLASS_MACH:
1036 case VG_DARWIN_SYSCALL_CLASS_MDEP:
1037 wLO = do_syscall_mach_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1038 VG_DARWIN_SYSNO_FOR_KERNEL(sysno));
1039 err = 0;
1040 break;
1041 default:
1042 vg_assert(0);
1043 break;
1045 return VG_(mk_SysRes_amd64_darwin)( scclass, err ? True : False, wHI, wLO );
1047 #elif defined(VGP_s390x_linux)
1048 UWord val;
1050 if (sysno == __NR_mmap) {
1051 ULong argbuf[6];
1053 argbuf[0] = a1;
1054 argbuf[1] = a2;
1055 argbuf[2] = a3;
1056 argbuf[3] = a4;
1057 argbuf[4] = a5;
1058 argbuf[5] = a6;
1059 val = do_syscall_WRK(sysno,(UWord)&argbuf[0],0,0,0,0,0);
1060 } else {
1061 val = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
1064 return VG_(mk_SysRes_s390x_linux)( val );
1066 #elif defined(VGP_mips32_linux)
1067 UWord err = 0;
1068 UWord valHi = 0;
1069 UWord valLo = 0;
1070 (void) do_syscall_WRK(a1, a2, a3, a4, a5, a6, a7, sysno, &err, &valHi, &valLo);
1071 return VG_(mk_SysRes_mips32_linux)( valLo, valHi, (ULong)err );
1073 #elif defined(VGP_mips64_linux)
1074 RegWord v1_a3[2];
1075 v1_a3[0] = 0xFF00;
1076 v1_a3[1] = 0xFF00;
1077 RegWord V0 = do_syscall_WRK(a1, a2, a3, a4, a5, a6, a7, sysno, v1_a3);
1078 RegWord V1 = (RegWord)v1_a3[0];
1079 RegWord A3 = (RegWord)v1_a3[1];
1080 return VG_(mk_SysRes_mips64_linux)( V0, V1, A3 );
1082 #elif defined(VGP_nanomips_linux)
1083 RegWord reg_a0 = 0;
1084 do_syscall_WRK(a1, a2, a3, a4, a5, a6, sysno, &reg_a0);
1085 return VG_(mk_SysRes_nanomips_linux)(reg_a0);
1087 # elif defined(VGP_x86_solaris)
1088 UInt val, val2, err = False;
1089 Bool restart;
1090 ULong u64;
1091 UChar ssclass = VG_SOLARIS_SYSNO_CLASS(sysno);
1093 switch (ssclass) {
1094 case VG_SOLARIS_SYSCALL_CLASS_CLASSIC:
1095 /* The Solaris kernel does not restart syscalls automatically so it
1096 is done here. */
1097 do {
1098 u64 = do_syscall_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1099 VG_SOLARIS_SYSNO_INDEX(sysno), &err);
1100 val = (UInt)u64;
1101 restart = err && (val == VKI_EINTR || val == VKI_ERESTART);
1102 } while (restart);
1103 break;
1104 case VG_SOLARIS_SYSCALL_CLASS_FASTTRAP:
1105 u64 = do_syscall_fast_WRK(VG_SOLARIS_SYSNO_INDEX(sysno));
1106 break;
1107 default:
1108 vg_assert(0);
1109 break;
1112 val = (UInt)u64;
1113 val2 = (UInt)(u64 >> 32);
1114 return VG_(mk_SysRes_x86_solaris)(err ? True : False, val,
1115 err ? 0 : val2);
1117 # elif defined(VGP_amd64_solaris)
1118 ULong val, val2, err = False;
1119 Bool restart;
1120 UChar ssclass = VG_SOLARIS_SYSNO_CLASS(sysno);
1122 switch (ssclass) {
1123 case VG_SOLARIS_SYSCALL_CLASS_CLASSIC:
1124 /* The Solaris kernel does not restart syscalls automatically so it
1125 is done here. */
1126 do {
1127 val = do_syscall_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
1128 VG_SOLARIS_SYSNO_INDEX(sysno), &err, &val2);
1129 restart = err && (val == VKI_EINTR || val == VKI_ERESTART);
1130 } while (restart);
1131 break;
1132 case VG_SOLARIS_SYSCALL_CLASS_FASTTRAP:
1133 val = do_syscall_fast_WRK(VG_SOLARIS_SYSNO_INDEX(sysno), &val2);
1134 break;
1135 default:
1136 vg_assert(0);
1137 break;
1140 return VG_(mk_SysRes_amd64_solaris)(err ? True : False, val,
1141 err ? 0 : val2);
1143 #else
1144 # error Unknown platform
1145 #endif
1148 /* ---------------------------------------------------------------------
1149 Names of errors.
1150 ------------------------------------------------------------------ */
1152 /* Return a string which gives the name of an error value. Note,
1153 unlike the standard C syserror fn, the returned string is not
1154 malloc-allocated or writable -- treat it as a constant.
1155 TODO: implement this properly. */
1157 const HChar* VG_(strerror) ( UWord errnum )
1159 switch (errnum) {
1160 case VKI_EPERM: return "Operation not permitted";
1161 case VKI_ENOENT: return "No such file or directory";
1162 case VKI_ESRCH: return "No such process";
1163 case VKI_EINTR: return "Interrupted system call";
1164 case VKI_EIO: return "Input/output error";
1165 case VKI_ENXIO: return "No such device or address";
1166 case VKI_E2BIG: return "Argument list too long";
1167 case VKI_ENOEXEC: return "Exec format error";
1168 case VKI_EBADF: return "Bad file descriptor";
1169 case VKI_ECHILD: return "No child processes";
1170 case VKI_EAGAIN: return "Resource temporarily unavailable";
1171 case VKI_ENOMEM: return "Cannot allocate memory";
1172 case VKI_EACCES: return "Permission denied";
1173 case VKI_EFAULT: return "Bad address";
1174 case VKI_ENOTBLK: return "Block device required";
1175 case VKI_EBUSY: return "Device or resource busy";
1176 case VKI_EEXIST: return "File exists";
1177 case VKI_EXDEV: return "Invalid cross-device link";
1178 case VKI_ENODEV: return "No such device";
1179 case VKI_ENOTDIR: return "Not a directory";
1180 case VKI_EISDIR: return "Is a directory";
1181 case VKI_EINVAL: return "Invalid argument";
1182 case VKI_ENFILE: return "Too many open files in system";
1183 case VKI_EMFILE: return "Too many open files";
1184 case VKI_ENOTTY: return "Inappropriate ioctl for device";
1185 case VKI_ETXTBSY: return "Text file busy";
1186 case VKI_EFBIG: return "File too large";
1187 case VKI_ENOSPC: return "No space left on device";
1188 case VKI_ESPIPE: return "Illegal seek";
1189 case VKI_EROFS: return "Read-only file system";
1190 case VKI_EMLINK: return "Too many links";
1191 case VKI_EPIPE: return "Broken pipe";
1192 case VKI_EDOM: return "Numerical argument out of domain";
1193 case VKI_ERANGE: return "Numerical result out of range";
1195 case VKI_ENOSYS: return "Function not implemented";
1196 case VKI_EOVERFLOW: return "Value too large for defined data type";
1197 # if defined(VKI_ERESTARTSYS)
1198 case VKI_ERESTARTSYS: return "ERESTARTSYS";
1199 # endif
1200 default: return "VG_(strerror): unknown error";
1205 /*--------------------------------------------------------------------*/
1206 /*--- end ---*/
1207 /*--------------------------------------------------------------------*/