2 /*--------------------------------------------------------------------*/
3 /*--- Doing syscalls. m_syscall.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2017 Julian Seward
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
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 */
47 res
._isError
= (a3
!= (UWord
)0);
53 SysRes
VG_(mk_SysRes_mips64_linux
) ( ULong v0
, ULong v1
, ULong a3
) {
54 /* MIPS uses a3 != 0 to flag an error */
56 res
._isError
= (a3
!= (ULong
)0);
62 /* Generic constructors. */
63 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
71 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
79 SysRes
VG_(mk_SysRes_SuccessEx
) ( UWord res
, UWord resEx
) {
88 #elif defined(VGO_linux) \
89 && !defined(VGP_mips32_linux) && !defined(VGP_mips64_linux)
93 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/
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
) {
111 res
._isError
= (a0
> 0xFFFFF000ul
);
116 SysRes
VG_(mk_SysRes_x86_linux
) ( Int val
) {
118 res
._isError
= val
>= -4095 && val
<= -1;
120 res
._val
= (UInt
)(-val
);
122 res
._val
= (UInt
)val
;
128 SysRes
VG_(mk_SysRes_amd64_linux
) ( Long val
) {
130 res
._isError
= val
>= -4095 && val
<= -1;
132 res
._val
= (ULong
)(-val
);
134 res
._val
= (ULong
)val
;
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
) {
143 res
._isError
= (cr0so
& 1) != 0;
148 /* As per ppc32 version, for the sc instruction cr0.so must be in
150 For the scv 0 instruction, the return value indicates failure if
151 it is -4095..-1 (i.e., it is >= -MAX_ERRNO (-4095) as an unsigned
152 comparison), in which case the error value is the negated return value. */
153 SysRes
VG_(mk_SysRes_ppc64_linux
) ( ULong val
, ULong cr0so
, UInt flag
) {
156 if (flag
== SC_FLAG
) {
158 res
._isError
= (cr0so
& 1) != 0;
160 } else if (flag
== SCV_FLAG
) {
161 /* scv instruction */
162 if ( (Long
)val
>= -4095 && (Long
)val
<= -1) {
164 res
._val
= (ULong
)(-val
);
166 res
._isError
= False
;
167 res
._val
= (ULong
)(val
);
174 SysRes
VG_(mk_SysRes_s390x_linux
) ( Long val
) {
176 res
._isError
= val
>= -4095 && val
<= -1;
185 SysRes
VG_(mk_SysRes_arm_linux
) ( Int val
) {
187 res
._isError
= val
>= -4095 && val
<= -1;
189 res
._val
= (UInt
)(-val
);
191 res
._val
= (UInt
)val
;
196 SysRes
VG_(mk_SysRes_arm64_linux
) ( Long val
) {
198 res
._isError
= val
>= -4095 && val
<= -1;
200 res
._val
= (ULong
)(-val
);
202 res
._val
= (ULong
)val
;
207 /* Generic constructors. */
208 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
215 #if defined(VGP_nanomips_linux)
216 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
219 r
._val
= (UWord
)(-(Word
)err
);
223 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
233 #elif defined(VGO_darwin)
235 /* Darwin: Some syscalls return a double-word result. */
236 SysRes
VG_(mk_SysRes_x86_darwin
) ( UChar scclass
, Bool isErr
,
242 res
._mode
= 0; /* invalid */
243 vg_assert(isErr
== False
|| isErr
== True
);
244 vg_assert(sizeof(UWord
) == sizeof(UInt
));
246 case VG_DARWIN_SYSCALL_CLASS_UNIX
:
249 res
._mode
= isErr
? SysRes_UNIX_ERR
: SysRes_UNIX_OK
;
251 case VG_DARWIN_SYSCALL_CLASS_MACH
:
255 res
._mode
= SysRes_MACH
;
257 case VG_DARWIN_SYSCALL_CLASS_MDEP
:
261 res
._mode
= SysRes_MDEP
;
269 SysRes
VG_(mk_SysRes_amd64_darwin
) ( UChar scclass
, Bool isErr
,
270 ULong wHI
, ULong wLO
)
275 res
._mode
= 0; /* invalid */
276 vg_assert(isErr
== False
|| isErr
== True
);
277 vg_assert(sizeof(UWord
) == sizeof(ULong
));
279 case VG_DARWIN_SYSCALL_CLASS_UNIX
:
282 res
._mode
= isErr
? SysRes_UNIX_ERR
: SysRes_UNIX_OK
;
284 case VG_DARWIN_SYSCALL_CLASS_MACH
:
288 res
._mode
= SysRes_MACH
;
290 case VG_DARWIN_SYSCALL_CLASS_MDEP
:
294 res
._mode
= SysRes_MDEP
;
302 /* Generic constructors. We assume (without checking if this makes
303 any sense, from the caller's point of view) that these are for the
304 UNIX style of syscall. */
305 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
309 r
._mode
= SysRes_UNIX_ERR
;
313 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
317 r
._mode
= SysRes_UNIX_OK
;
322 #elif defined(VGO_solaris)
324 /* Generic constructors. */
325 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
333 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
341 SysRes
VG_(mk_SysRes_x86_solaris
) ( Bool isErr
, UInt val
, UInt val2
)
346 vg_assert(isErr
== True
|| isErr
== False
);
350 res
._isError
= isErr
;
354 SysRes
VG_(mk_SysRes_amd64_solaris
) ( Bool isErr
, ULong val
, ULong val2
)
359 vg_assert(isErr
== True
|| isErr
== False
);
363 res
._isError
= isErr
;
368 #elif defined(VGO_freebsd)
370 SysRes
VG_(mk_SysRes_x86_freebsd
) ( UInt val
, UInt val2
, Bool err
) {
378 SysRes
VG_(mk_SysRes_amd64_freebsd
) ( ULong val
, ULong val2
, Bool err
) {
386 SysRes
VG_(mk_SysRes_arm64_freebsd
) ( ULong val
, ULong val2
, Bool err
) {
394 /* Generic constructors. */
395 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
403 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
416 /* ---------------------------------------------------------------------
417 VG_(do_syscall): A function for doing syscalls.
418 ------------------------------------------------------------------ */
420 #if defined(VGP_x86_linux)
421 /* Incoming args (syscall number + up to 6 args) come on the stack.
422 (ie. the C calling convention).
424 The syscall number goes in %eax. The args are passed to the syscall in
425 the regs %ebx, %ecx, %edx, %esi, %edi, %ebp, ie. the kernel's syscall
428 %eax gets the return value. Not sure which registers the kernel
429 clobbers, so we preserve all the callee-save regs (%esi, %edi, %ebx,
432 extern UWord
do_syscall_WRK (
434 UWord a1
, UWord a2
, UWord a3
,
435 UWord a4
, UWord a5
, UWord a6
439 ".globl do_syscall_WRK\n"
443 " .cfi_adjust_cfa_offset 4\n"
444 " .cfi_offset %esi, -8\n"
446 " .cfi_adjust_cfa_offset 4\n"
447 " .cfi_offset %edi, -12\n"
449 " .cfi_adjust_cfa_offset 4\n"
450 " .cfi_offset %ebx, -16\n"
452 " .cfi_adjust_cfa_offset 4\n"
453 " .cfi_offset %ebp, -20\n"
454 " movl 16+ 4(%esp),%eax\n"
455 " movl 16+ 8(%esp),%ebx\n"
456 " movl 16+12(%esp),%ecx\n"
457 " movl 16+16(%esp),%edx\n"
458 " movl 16+20(%esp),%esi\n"
459 " movl 16+24(%esp),%edi\n"
460 " movl 16+28(%esp),%ebp\n"
463 " .cfi_adjust_cfa_offset -4\n"
464 " .cfi_restore %ebp\n"
466 " .cfi_adjust_cfa_offset -4\n"
467 " .cfi_restore %ebx\n"
469 " .cfi_adjust_cfa_offset -4\n"
470 " .cfi_restore %edi\n"
472 " .cfi_adjust_cfa_offset -4\n"
473 " .cfi_restore %esi\n"
479 #elif defined(VGP_amd64_linux)
480 /* Incoming args (syscall number + up to 6 args) come in %rdi, %rsi,
481 %rdx, %rcx, %r8, %r9, and the last one on the stack (ie. the C
484 The syscall number goes in %rax. The args are passed to the syscall in
485 the regs %rdi, %rsi, %rdx, %r10, %r8, %r9 (yes, really %r10, not %rcx),
486 ie. the kernel's syscall calling convention.
488 %rax gets the return value. %rcx and %r11 are clobbered by the syscall;
489 no matter, they are caller-save (the syscall clobbers no callee-save
490 regs, so we don't have to do any register saving/restoring).
492 extern UWord
do_syscall_WRK (
494 UWord a1
, UWord a2
, UWord a3
,
495 UWord a4
, UWord a5
, UWord a6
499 ".globl do_syscall_WRK\n"
501 /* Convert function calling convention --> syscall calling
509 " movq 8(%rsp), %r9\n" /* last arg from stack */
515 #elif defined(VGP_ppc32_linux)
516 /* Incoming args (syscall number + up to 6 args) come in %r3:%r9.
518 The syscall number goes in %r0. The args are passed to the syscall in
519 the regs %r3:%r8, i.e. the kernel's syscall calling convention.
521 The %cr0.so bit flags an error.
522 We return the syscall return value in %r3, and the %cr0.so in
523 the lowest bit of %r4.
524 We return a ULong, of which %r3 is the high word, and %r4 the low.
525 No callee-save regs are clobbered, so no saving/restoring is needed.
527 extern ULong
do_syscall_WRK (
529 UWord a1
, UWord a2
, UWord a3
,
530 UWord a4
, UWord a5
, UWord a6
534 ".globl do_syscall_WRK\n"
543 " sc\n" /* syscall: sets %cr0.so on error */
544 " mfcr 4\n" /* %cr -> low word of return var */
545 " rlwinm 4,4,4,31,31\n" /* rotate flag bit so to lsb, and mask it */
546 " blr\n" /* and return */
550 #elif defined(VGP_ppc64be_linux)
551 /* Due to the need to return 65 bits of result, this is completely
552 different from the ppc32 case. The single arg register points to a
553 7-word block containing the syscall # and the 6 args. The syscall
554 result proper is put in [0] of the block, and %cr0.so is in the
555 bottom bit of [1]. */
556 extern void do_syscall_WRK ( ULong
* argblock
);
559 ".globl do_syscall_WRK\n"
560 ".section \".opd\",\"aw\"\n"
563 ".quad .do_syscall_WRK,.TOC.@tocbase,0\n"
565 ".type .do_syscall_WRK,@function\n"
566 ".globl .do_syscall_WRK\n"
568 " std 3,-16(1)\n" /* stash arg */
569 " ld 8, 48(3)\n" /* sc arg 6 */
570 " ld 7, 40(3)\n" /* sc arg 5 */
571 " ld 6, 32(3)\n" /* sc arg 4 */
572 " ld 5, 24(3)\n" /* sc arg 3 */
573 " ld 4, 16(3)\n" /* sc arg 2 */
574 " ld 0, 0(3)\n" /* sc number */
575 " ld 3, 8(3)\n" /* sc arg 1 */
576 " sc\n" /* result in r3 and cr0.so */
577 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
578 " std 3,0(5)\n" /* argblock[0] = r3 */
582 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
586 #elif defined(VGP_ppc64le_linux)
587 /* Due to the need to return 65 bits of result, this is completely
588 different from the ppc32 case. The single arg register points to a
589 7-word block containing the syscall # and the 6 args. The syscall
590 result proper is put in [0] of the block, and %cr0.so is in the
591 bottom bit of [1]. */
592 extern void do_syscall_WRK ( ULong
* argblock
);
593 /* Little Endian supports ELF version 2. In the future, it may support
594 * other versions as well.
598 ".globl do_syscall_WRK\n"
599 ".type do_syscall_WRK,@function\n"
601 "#if _CALL_ELF == 2" "\n"
602 "0: addis 2,12,.TOC.-0b@ha\n"
603 " addi 2,2,.TOC.-0b@l\n"
604 " .localentry do_syscall_WRK, .-do_syscall_WRK\n"
606 /* Check which system call instruction to issue*/
607 " ld 8, 56(3)\n" /* arg 7 holds sc/scv flag */
608 " cmpdi 8,1\n" /* check sc/scv flag not equal to SC_FLAG*/
611 /* setup and issue the sc instruction */
612 " std 3,-16(1)\n" /* stash arg */
613 " ld 8, 48(3)\n" /* sc arg 6 */
614 " ld 7, 40(3)\n" /* sc arg 5 */
615 " ld 6, 32(3)\n" /* sc arg 4 */
616 " ld 5, 24(3)\n" /* sc arg 3 */
617 " ld 4, 16(3)\n" /* sc arg 2 */
618 " ld 0, 0(3)\n" /* sc number */
619 " ld 3, 8(3)\n" /* sc arg 1 */
620 " sc\n" /* result in r3 and cr0.so */
621 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
622 " std 3,0(5)\n" /* argblock[0] = r3 */
626 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
627 " blr\n" /* return */
629 /* setup to do scv instruction */
631 /* The scv instruction requires a new stack frame */
633 " std 27,40(1)\n" /* save r27 to stack frame */
634 " mflr 27\n" /* Get link register */
635 " std 27,16(1)\n" /* Save link register */
637 /* setup and issue the scv instruction */
638 " std 3,-16(1)\n" /* stash arg */
639 " ld 8, 48(3)\n" /* sc arg 6 */
640 " ld 7, 40(3)\n" /* sc arg 5 */
641 " ld 6, 32(3)\n" /* sc arg 4 */
642 " ld 5, 24(3)\n" /* sc arg 3 */
643 " ld 4, 16(3)\n" /* sc arg 2 */
644 " ld 0, 0(3)\n" /* sc number */
645 " ld 3, 8(3)\n" /* sc arg 1 */
648 " .machine \"power9\"\n"
651 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
652 " std 3,0(5)\n" /* argblock[0] = r3 */
654 /* pop off stack frame */
655 " ld 27,16(1)\n" /* Fetch LR from frame */
656 " mtlr 27\n" /* restore LR */
657 " ld 27,40(1)\n" /* restore r27 from stack frame */
660 " .size do_syscall_WRK, .-do_syscall_WRK\n"
663 #elif defined(VGP_arm_linux)
664 /* I think the conventions are:
665 args in r0 r1 r2 r3 r4 r5
667 return value in r0, w/ same conventions as x86-linux, viz r0 in
668 -4096 .. -1 is an error value. All other values are success
671 extern UWord
do_syscall_WRK (
672 UWord a1
, UWord a2
, UWord a3
,
673 UWord a4
, UWord a5
, UWord a6
,
678 ".globl do_syscall_WRK\n"
680 " push {r4, r5, r7}\n"
681 " ldr r4, [sp, #12]\n"
682 " ldr r5, [sp, #16]\n"
683 " ldr r7, [sp, #20]\n"
685 " pop {r4, r5, r7}\n"
690 #elif defined(VGP_arm64_linux)
691 /* I think the conventions are:
692 args in r0 r1 r2 r3 r4 r5
694 return value in r0, w/ same conventions as x86-linux, viz r0 in
695 -4096 .. -1 is an error value. All other values are success
698 r0 to r5 remain unchanged, but syscall_no is in r6 and needs
699 to be moved to r8 (??)
701 extern UWord
do_syscall_WRK (
702 UWord a1
, UWord a2
, UWord a3
,
703 UWord a4
, UWord a5
, UWord a6
,
708 ".globl do_syscall_WRK\n"
718 #elif defined(VGP_x86_freebsd)
719 /* Incoming args (syscall number + up to 8 args) are on the stack.
720 FreeBSD has a syscall called 'syscall' that takes all args (including
721 the syscall number) off the stack. Since we're called, the return
722 address is on the stack as expected, so we can just call syscall(2)
723 and it Just Works. Error is when carry is set.
725 extern ULong
do_syscall_WRK (
727 UWord a1
, UWord a2
, UWord a3
,
728 UWord a4
, UWord a5
, UWord a6
,
729 UWord a7
, UWord a8
, UInt
*flags
734 " movl $0,%eax\n" /* syscall number = "syscall" (0) to avoid stack frobbing
739 "1: movl 40(%esp),%ecx\n" /* store carry in *flags */
745 #elif defined(VGP_amd64_freebsd)
746 /* Convert function calling convention --> SYSCALL_STD calling
748 PJF - not sure why we don't use SYSCALL0 convention like x86
750 extern UWord
do_syscall_WRK (
751 UWord syscall_no
, /* %rdi */
757 UWord a6
, /* 8(%rsp) */
758 UWord a7
, /* 16(%rsp) */
759 UWord a8
, /* 24(%rsp) */
760 UInt
*flags
, /* 32(%rsp) */
761 UWord
*rv2
/* 40(%rsp) */
768 " movq %rdi, %rax\n" /* syscall_no */
769 " movq %rsi, %rdi\n" /* a1 */
770 " movq %rdx, %rsi\n" /* a2 */
771 " movq %rcx, %rdx\n" /* a3 */
772 " movq %r8, %r10\n" /* a4 */
773 " movq %r9, %r8\n" /* a5 */
774 " movq 16(%rbp), %r9\n" /* a6 last arg from stack, account for %rbp */
775 " movq 24(%rbp), %r11\n" /* a7 from stack */
777 " movq 32(%rbp), %r11\n" /* a8 from stack */
779 " subq $8,%rsp\n" /* fake return addr */
782 " movq 48(%rbp),%rsi\n" /* success */
783 " movq %rdx, (%rsi)\n" /* second return value */
787 "1:\n" /* error path */
788 " movq 40(%rbp), %rsi\n" /* flags */
796 #elif defined(VGP_arm64_freebsd)
799 * Arguments a1 to a8 are in registers x0 to x7.
800 * Which is just what we want for a syscall.
802 * The syscall number is on the top of the stack
803 * pointed to by sp. The flags are at sp+8 and
804 * second return value at sp+16.
807 extern UWord
do_syscall_WRK (
808 UWord a1
, UWord a2
, UWord a3
,
809 UWord a4
, UWord a5
, UWord a6
,
812 UInt
*flags
, UWord
*rv2
816 ".globl do_syscall_WRK\n"
818 " ldr x8, [sp]\n" // retrieve syscall_no, put it in x8
819 " svc 0x0\n" // do the syscall
820 " mov x9, 1\n" // flags for error will be 1 or 0
821 " csel x9, x9, xzr, cs\n" // conditionally select 1 or 0 into x9
822 " ldr x10, [sp, #8]\n" // load the address of flags
823 " str w9, [x10]\n" // store flags result
824 " ldr x10, [sp, #16]\n" // load the addres of rv2
825 " str x1, [x10]\n" // store rv2 result
830 #elif defined(VGP_x86_darwin)
832 /* Incoming args (syscall number + up to 8 args) come in on the stack
834 The kernel's syscall calling convention is:
835 * the syscall number goes in eax
836 * the args are passed to the syscall on the stack,
837 pushed onto the stack R->L (that is, the usual x86
838 calling conventions, with the leftmost arg at the lowest
845 Note that the call type can be determined from the syscall number;
846 there is no need to inspect the actual instruction. Although obviously
847 the instruction must match.
849 * MACH,MDEP: the return value comes back in eax
850 * UNIX: the return value comes back in edx:eax (hi32:lo32)
852 * MACH,MDEP: no error is returned
853 * UNIX: the carry flag indicates success or failure
855 nb here, sizeof(UWord) == sizeof(UInt)
858 __private_extern__ ULong
859 do_syscall_unix_WRK ( UWord a1
, UWord a2
, UWord a3
, /* 4(esp)..12(esp) */
860 UWord a4
, UWord a5
, UWord a6
, /* 16(esp)..24(esp) */
861 UWord a7
, UWord a8
, /* 28(esp)..32(esp) */
862 UWord syscall_no
, /* 36(esp) */
863 /*OUT*/UInt
* errflag
/* 40(esp) */ );
864 // Unix syscall: 64-bit return in edx:eax, with LSB in eax
865 // error indicated by carry flag: clear=good, set=bad
866 asm(".private_extern _do_syscall_unix_WRK\n"
867 "_do_syscall_unix_WRK:\n"
868 " movl 40(%esp), %ecx \n" /* assume syscall success */
869 " movl $0, (%ecx) \n"
870 " movl 36(%esp), %eax \n"
872 " jnc 1f \n" /* jump if success */
873 " movl 40(%esp), %ecx \n" /* syscall failed - set *errflag */
874 " movl $1, (%ecx) \n"
878 __private_extern__ UInt
879 do_syscall_mach_WRK ( UWord a1
, UWord a2
, UWord a3
, /* 4(esp)..12(esp) */
880 UWord a4
, UWord a5
, UWord a6
, /* 16(esp)..24(esp) */
881 UWord a7
, UWord a8
, /* 28(esp)..32(esp) */
882 UWord syscall_no
/* 36(esp) */ );
883 // Mach trap: 32-bit result in %eax, no error flag
884 asm(".private_extern _do_syscall_mach_WRK\n"
885 "_do_syscall_mach_WRK:\n"
886 " movl 36(%esp), %eax \n"
891 __private_extern__ UInt
892 do_syscall_mdep_WRK ( UWord a1
, UWord a2
, UWord a3
, /* 4(esp)..12(esp) */
893 UWord a4
, UWord a5
, UWord a6
, /* 16(esp)..24(esp) */
894 UWord a7
, UWord a8
, /* 28(esp)..32(esp) */
895 UWord syscall_no
/* 36(esp) */ );
896 // mdep trap: 32-bit result in %eax, no error flag
898 ".private_extern _do_syscall_mdep_WRK\n"
899 "_do_syscall_mdep_WRK:\n"
900 " movl 36(%esp), %eax \n"
906 #elif defined(VGP_amd64_darwin)
908 /* Incoming args (syscall number + up to 8 args) come in registers and stack
910 The kernel's syscall calling convention is:
911 * the syscall number goes in rax
912 * the args are passed to the syscall in registers and the stack
913 * the call instruction is 'syscall'
915 * MACH,MDEP: the return value comes back in rax
916 * UNIX: the return value comes back in rdx:rax (hi64:lo64)
918 * MACH,MDEP: no error is returned
919 * UNIX: the carry flag indicates success or failure
921 nb here, sizeof(UWord) == sizeof(ULong)
924 __private_extern__ UWord
925 do_syscall_unix_WRK ( UWord a1
, UWord a2
, UWord a3
, /* rdi, rsi, rdx */
926 UWord a4
, UWord a5
, UWord a6
, /* rcx, r8, r9 */
927 UWord a7
, UWord a8
, /* 8(rsp), 16(rsp) */
928 UWord syscall_no
, /* 24(rsp) */
929 /*OUT*/ULong
* errflag
, /* 32(rsp) */
930 /*OUT*/ULong
* res2
); /* 40(rsp) */
931 // Unix syscall: 128-bit return in rax:rdx, with LSB in rax
932 // error indicated by carry flag: clear=good, set=bad
933 asm(".private_extern _do_syscall_unix_WRK\n"
934 "_do_syscall_unix_WRK:\n"
935 " movq %rcx, %r10 \n" /* pass rcx in r10 instead */
936 " movq 32(%rsp), %rax \n" /* assume syscall success */
937 " movq $0, (%rax) \n"
938 " movq 24(%rsp), %rax \n" /* load syscall_no */
940 " jnc 1f \n" /* jump if success */
941 " movq 32(%rsp), %rcx \n" /* syscall failed - set *errflag */
942 " movq $1, (%rcx) \n"
943 " 1: movq 40(%rsp), %rcx \n" /* save 2nd result word */
944 " movq %rdx, (%rcx) \n"
945 " retq \n" /* return 1st result word */
948 __private_extern__ UWord
949 do_syscall_mach_WRK ( UWord a1
, UWord a2
, UWord a3
, /* rdi, rsi, rdx */
950 UWord a4
, UWord a5
, UWord a6
, /* rcx, r8, r9 */
951 UWord a7
, UWord a8
, /* 8(rsp), 16(rsp) */
952 UWord syscall_no
); /* 24(rsp) */
953 // Mach trap: 64-bit result, no error flag
954 asm(".private_extern _do_syscall_mach_WRK\n"
955 "_do_syscall_mach_WRK:\n"
956 " movq %rcx, %r10 \n" /* pass rcx in r10 instead */
957 " movq 24(%rsp), %rax \n" /* load syscall_no */
962 #elif defined(VGP_s390x_linux)
964 static UWord
do_syscall_WRK (
966 UWord arg1
, UWord arg2
, UWord arg3
,
967 UWord arg4
, UWord arg5
, UWord arg6
970 register UWord __arg1
asm("2") = arg1
;
971 register UWord __arg2
asm("3") = arg2
;
972 register UWord __arg3
asm("4") = arg3
;
973 register UWord __arg4
asm("5") = arg4
;
974 register UWord __arg5
asm("6") = arg5
;
975 register UWord __arg6
asm("7") = arg6
;
976 register ULong __svcres
asm("2");
978 __asm__
__volatile__ (
989 : "1", "cc", "memory");
991 return (UWord
) (__svcres
);
994 #elif defined(VGP_mips32_linux)
995 /* Incoming args (syscall number + up to 6 args) come in a0 - a3 and stack.
997 The syscall number goes in v0. The args are passed to the syscall in
998 the regs a0 - a3 and stack, i.e. the kernel's syscall calling convention.
1000 (a3 != 0) flags an error.
1001 We return the syscall return value in v0.
1004 extern int do_syscall_WRK (
1005 int a1
, int a2
, int a3
,
1006 int a4
, int a5
, int a6
, int a7
, int syscall_no
, UWord
*err
,
1007 UWord
*valHi
, UWord
* valLo
1011 ".globl do_syscall_WRK \n\t"
1012 ".type do_syscall_WRK, @function \n\t"
1014 ".set noreorder \n\t"
1015 "do_syscall_WRK: \n\t"
1016 " lw $2, 28($29) \n\t"
1018 " lw $8, 32($29) \n\t"
1020 " lw $8, 36($29) \n\t"
1021 " sw $3, ($8) \n\t" /* store valHi */
1022 " lw $8, 40($29) \n\t"
1024 " sw $2, ($8) \n\t" /* store valLo */
1025 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
1030 #elif defined(VGP_mips64_linux)
1031 extern RegWord
do_syscall_WRK ( RegWord a1
, RegWord a2
, RegWord a3
, RegWord a4
,
1032 RegWord a5
, RegWord a6
, RegWord a7
, RegWord syscall_no
,
1033 RegWord
* V1_A3_val
);
1036 ".globl do_syscall_WRK \n\t"
1037 ".type do_syscall_WRK, @function \n\t"
1039 ".set noreorder \n\t"
1040 "do_syscall_WRK: \n\t"
1041 " move $2, $11 \n\t"
1043 # if defined(_ABI64)
1044 " ld $12, 0($29) \n\t"
1045 # elif defined(_ABIN32)
1046 " lw $12, 0($29) \n\t"
1048 " sd $3, 0($12) \n\t" /* store v1 in V1_A3_val */
1050 " sd $7, 8($12) \n\t" /* store a3 in V1_A3_val */
1051 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
1056 #elif defined(VGP_nanomips_linux)
1057 extern void do_syscall_WRK (
1058 RegWord a1
, RegWord a2
, RegWord a3
,
1059 RegWord a4
, RegWord a5
, RegWord a6
,
1060 RegWord syscall_no
, RegWord
*res_a0
);
1063 ".globl do_syscall_WRK \n\t"
1064 ".type do_syscall_WRK, @function \n\t"
1066 ".set noreorder \n\t"
1067 "do_syscall_WRK: \n\t"
1068 " save 32, $a7 \n\t"
1069 " move $t4, $a6 \n\t"
1071 " restore 32, $a7 \n\t"
1072 " sw $a0, 0($a7) \n\t"
1074 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
1079 #elif defined(VGP_x86_solaris)
1082 do_syscall_WRK(UWord a1
, UWord a2
, UWord a3
, /* 4(esp)..12(esp) */
1083 UWord a4
, UWord a5
, UWord a6
, /* 16(esp)..24(esp) */
1084 UWord a7
, UWord a8
, /* 28(esp)..32(esp) */
1085 UWord syscall_no
, /* 36(esp) */
1086 /*OUT*/UInt
*errflag
); /* 40(esp) */
1087 /* Classic unix syscall.. parameters on the stack, an unused (by the kernel)
1088 return address at 0(esp), a sysno in eax, a result in edx:eax, the carry
1089 flag set on error. */
1092 ".globl do_syscall_WRK\n"
1094 " movl 40(%esp), %ecx\n" /* assume syscall success */
1095 " movl $0, (%ecx)\n"
1096 " movl 36(%esp), %eax\n"
1098 " jnc 1f\n" /* jump if success */
1099 " movl 40(%esp), %ecx\n" /* syscall failed - set *errflag */
1100 " movl $1, (%ecx)\n"
1106 do_syscall_fast_WRK(UWord syscall_no
); /* 4(esp) */
1107 /* Fasttrap syscall.. no parameters, a sysno in eax, a result in edx:eax,
1108 never fails (if the sysno is valid). */
1111 ".globl do_syscall_fast_WRK\n"
1112 "do_syscall_fast_WRK:\n"
1113 " movl 4(%esp), %eax\n"
1119 #elif defined(VGP_amd64_solaris)
1122 do_syscall_WRK(UWord a1
, UWord a2
, UWord a3
, /* rdi, rsi, rdx */
1123 UWord a4
, UWord a5
, UWord a6
, /* rcx, r8, r9 */
1124 UWord a7
, UWord a8
, /* 8(rsp), 16(rsp) */
1125 UWord syscall_no
, /* 24(rsp) */
1126 /*OUT*/ULong
*errflag
, /* 32(rsp) */
1127 /*OUT*/ULong
*res2
); /* 40(rsp) */
1128 /* First 6 parameters in registers rdi, rsi, rdx, r10, r8, r9, next
1129 2 parameters on the stack, an unused (by the kernel) return address at
1130 0(rsp), a sysno in rax, a result in rdx:rax, the carry flag set on
1134 ".globl do_syscall_WRK\n"
1136 " movq %rcx, %r10\n" /* pass rcx in r10 instead */
1137 " movq 32(%rsp), %rcx\n" /* assume syscall success */
1138 " movq $0, (%rcx)\n"
1139 " movq 24(%rsp), %rax\n"
1141 " jnc 1f\n" /* jump if success */
1142 " movq 32(%rsp), %rcx\n" /* syscall failed - set *errflag */
1143 " movq $1, (%rcx)\n"
1144 "1: movq 40(%rsp), %rcx\n" /* save 2nd result word */
1145 " movq %rdx, (%rcx)\n"
1151 do_syscall_fast_WRK(UWord syscall_no
, /* rdi */
1152 /*OUT*/ULong
*res2
); /* rsi */
1153 /* Fasttrap syscall.. no parameters, a sysno in rax, a result in rdx:rax,
1154 never fails (if the sysno is valid). */
1157 ".globl do_syscall_fast_WRK\n"
1158 "do_syscall_fast_WRK:\n"
1159 " movq %rdi, %rax\n"
1161 " movq %rdx, (%rsi)\n" /* save 2nd result word */
1167 # error Unknown platform
1171 /* Finally, the generic code. This sends the call to the right
1174 SysRes
VG_(do_syscall
) ( UWord sysno
, RegWord a1
, RegWord a2
, RegWord a3
,
1175 RegWord a4
, RegWord a5
, RegWord a6
,
1176 RegWord a7
, RegWord a8
)
1178 # if defined(VGP_x86_linux)
1179 UWord val
= do_syscall_WRK(sysno
,a1
,a2
,a3
,a4
,a5
,a6
);
1180 return VG_(mk_SysRes_x86_linux
)( val
);
1182 # elif defined(VGP_amd64_linux)
1183 UWord val
= do_syscall_WRK(sysno
,a1
,a2
,a3
,a4
,a5
,a6
);
1184 return VG_(mk_SysRes_amd64_linux
)( val
);
1186 # elif defined(VGP_x86_freebsd)
1189 val
= do_syscall_WRK(sysno
, a1
, a2
, a3
, a4
, a5
,
1191 return VG_(mk_SysRes_x86_freebsd
)( (UInt
)val
, (UInt
)(val
>>32), (err
& 1) != 0 ? True
: False
);
1193 # elif defined(VGP_amd64_freebsd)
1197 val
= do_syscall_WRK(sysno
, a1
, a2
, a3
, a4
, a5
,
1198 a6
, a7
, a8
, &err
, &val2
);
1199 return VG_(mk_SysRes_amd64_freebsd
)( val
, val2
, (err
& 1) != 0 ? True
: False
);
1201 # elif defined(VGP_arm64_freebsd)
1205 val
= do_syscall_WRK(a1
, a2
, a3
, a4
, a5
,
1206 a6
, a7
, a8
, sysno
, &err
, &val2
);
1207 return VG_(mk_SysRes_arm64_freebsd
)( val
, val2
, (err
& 1) != 0 ? True
: False
);
1209 # elif defined(VGP_ppc32_linux)
1210 ULong ret
= do_syscall_WRK(sysno
,a1
,a2
,a3
,a4
,a5
,a6
);
1211 UInt val
= (UInt
)(ret
>>32);
1212 UInt cr0so
= (UInt
)(ret
);
1213 return VG_(mk_SysRes_ppc32_linux
)( val
, cr0so
);
1215 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
1217 /* PPC system calls have at most 6 arguments. The Valgrind infrastructure
1218 supports 8 system call arguments. Argument 7 is used on PPC LE to pass
1219 the flag indicating if the sc or scv instruction should be used for the
1221 argblock
[0] = sysno
;
1229 do_syscall_WRK( &argblock
[0] );
1230 return VG_(mk_SysRes_ppc64_linux
)( argblock
[0], argblock
[1], a7
);
1232 # elif defined(VGP_arm_linux)
1233 UWord val
= do_syscall_WRK(a1
,a2
,a3
,a4
,a5
,a6
,sysno
);
1234 return VG_(mk_SysRes_arm_linux
)( val
);
1236 # elif defined(VGP_arm64_linux)
1237 UWord val
= do_syscall_WRK(a1
,a2
,a3
,a4
,a5
,a6
,sysno
);
1238 return VG_(mk_SysRes_arm64_linux
)( val
);
1240 # elif defined(VGP_x86_darwin)
1241 UInt wLO
= 0, wHI
= 0, err
= 0;
1243 UChar scclass
= VG_DARWIN_SYSNO_CLASS(sysno
);
1245 case VG_DARWIN_SYSCALL_CLASS_UNIX
:
1246 u64
= do_syscall_unix_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1247 VG_DARWIN_SYSNO_FOR_KERNEL(sysno
), &err
);
1249 wHI
= (UInt
)(u64
>> 32);
1251 case VG_DARWIN_SYSCALL_CLASS_MACH
:
1252 wLO
= do_syscall_mach_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1253 VG_DARWIN_SYSNO_FOR_KERNEL(sysno
));
1256 case VG_DARWIN_SYSCALL_CLASS_MDEP
:
1257 wLO
= do_syscall_mdep_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1258 VG_DARWIN_SYSNO_FOR_KERNEL(sysno
));
1265 return VG_(mk_SysRes_x86_darwin
)( scclass
, err
? True
: False
, wHI
, wLO
);
1267 # elif defined(VGP_amd64_darwin)
1268 ULong wLO
= 0, wHI
= 0, err
= 0;
1269 UChar scclass
= VG_DARWIN_SYSNO_CLASS(sysno
);
1271 case VG_DARWIN_SYSCALL_CLASS_UNIX
:
1272 wLO
= do_syscall_unix_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1273 VG_DARWIN_SYSNO_FOR_KERNEL(sysno
), &err
, &wHI
);
1275 case VG_DARWIN_SYSCALL_CLASS_MACH
:
1276 case VG_DARWIN_SYSCALL_CLASS_MDEP
:
1277 wLO
= do_syscall_mach_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1278 VG_DARWIN_SYSNO_FOR_KERNEL(sysno
));
1285 return VG_(mk_SysRes_amd64_darwin
)( scclass
, err
? True
: False
, wHI
, wLO
);
1287 #elif defined(VGP_s390x_linux)
1290 if (sysno
== __NR_mmap
) {
1299 val
= do_syscall_WRK(sysno
,(UWord
)&argbuf
[0],0,0,0,0,0);
1301 val
= do_syscall_WRK(sysno
,a1
,a2
,a3
,a4
,a5
,a6
);
1304 return VG_(mk_SysRes_s390x_linux
)( val
);
1306 #elif defined(VGP_mips32_linux)
1310 (void) do_syscall_WRK(a1
, a2
, a3
, a4
, a5
, a6
, a7
, sysno
, &err
, &valHi
, &valLo
);
1311 return VG_(mk_SysRes_mips32_linux
)( valLo
, valHi
, (ULong
)err
);
1313 #elif defined(VGP_mips64_linux)
1317 RegWord V0
= do_syscall_WRK(a1
, a2
, a3
, a4
, a5
, a6
, a7
, sysno
, v1_a3
);
1318 RegWord V1
= (RegWord
)v1_a3
[0];
1319 RegWord A3
= (RegWord
)v1_a3
[1];
1320 return VG_(mk_SysRes_mips64_linux
)( V0
, V1
, A3
);
1322 #elif defined(VGP_nanomips_linux)
1324 do_syscall_WRK(a1
, a2
, a3
, a4
, a5
, a6
, sysno
, ®_a0
);
1325 return VG_(mk_SysRes_nanomips_linux
)(reg_a0
);
1327 # elif defined(VGP_x86_solaris)
1328 UInt val
, val2
, err
= False
;
1331 UChar ssclass
= VG_SOLARIS_SYSNO_CLASS(sysno
);
1334 case VG_SOLARIS_SYSCALL_CLASS_CLASSIC
:
1335 /* The Solaris kernel does not restart syscalls automatically so it
1338 u64
= do_syscall_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1339 VG_SOLARIS_SYSNO_INDEX(sysno
), &err
);
1341 restart
= err
&& (val
== VKI_EINTR
|| val
== VKI_ERESTART
);
1344 case VG_SOLARIS_SYSCALL_CLASS_FASTTRAP
:
1345 u64
= do_syscall_fast_WRK(VG_SOLARIS_SYSNO_INDEX(sysno
));
1353 val2
= (UInt
)(u64
>> 32);
1354 return VG_(mk_SysRes_x86_solaris
)(err
? True
: False
, val
,
1357 # elif defined(VGP_amd64_solaris)
1358 ULong val
, val2
, err
= False
;
1360 UChar ssclass
= VG_SOLARIS_SYSNO_CLASS(sysno
);
1363 case VG_SOLARIS_SYSCALL_CLASS_CLASSIC
:
1364 /* The Solaris kernel does not restart syscalls automatically so it
1367 val
= do_syscall_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1368 VG_SOLARIS_SYSNO_INDEX(sysno
), &err
, &val2
);
1369 restart
= err
&& (val
== VKI_EINTR
|| val
== VKI_ERESTART
);
1372 case VG_SOLARIS_SYSCALL_CLASS_FASTTRAP
:
1373 val
= do_syscall_fast_WRK(VG_SOLARIS_SYSNO_INDEX(sysno
), &val2
);
1380 return VG_(mk_SysRes_amd64_solaris
)(err
? True
: False
, val
,
1384 # error Unknown platform
1388 /* ---------------------------------------------------------------------
1390 ------------------------------------------------------------------ */
1392 /* Return a string which gives the name of an error value. Note,
1393 unlike the standard C syserror fn, the returned string is not
1394 malloc-allocated or writable -- treat it as a constant.
1395 TODO: implement this properly. */
1397 const HChar
* VG_(strerror
) ( UWord errnum
)
1400 case VKI_EPERM
: return "Operation not permitted";
1401 case VKI_ENOENT
: return "No such file or directory";
1402 case VKI_ESRCH
: return "No such process";
1403 case VKI_EINTR
: return "Interrupted system call";
1404 case VKI_EIO
: return "Input/output error";
1405 case VKI_ENXIO
: return "No such device or address";
1406 case VKI_E2BIG
: return "Argument list too long";
1407 case VKI_ENOEXEC
: return "Exec format error";
1408 case VKI_EBADF
: return "Bad file descriptor";
1409 case VKI_ECHILD
: return "No child processes";
1410 case VKI_EAGAIN
: return "Resource temporarily unavailable";
1411 case VKI_ENOMEM
: return "Cannot allocate memory";
1412 case VKI_EACCES
: return "Permission denied";
1413 case VKI_EFAULT
: return "Bad address";
1414 case VKI_ENOTBLK
: return "Block device required";
1415 case VKI_EBUSY
: return "Device or resource busy";
1416 case VKI_EEXIST
: return "File exists";
1417 case VKI_EXDEV
: return "Invalid cross-device link";
1418 case VKI_ENODEV
: return "No such device";
1419 case VKI_ENOTDIR
: return "Not a directory";
1420 case VKI_EISDIR
: return "Is a directory";
1421 case VKI_EINVAL
: return "Invalid argument";
1422 case VKI_ENFILE
: return "Too many open files in system";
1423 case VKI_EMFILE
: return "Too many open files";
1424 case VKI_ENOTTY
: return "Inappropriate ioctl for device";
1425 case VKI_ETXTBSY
: return "Text file busy";
1426 case VKI_EFBIG
: return "File too large";
1427 case VKI_ENOSPC
: return "No space left on device";
1428 case VKI_ESPIPE
: return "Illegal seek";
1429 case VKI_EROFS
: return "Read-only file system";
1430 case VKI_EMLINK
: return "Too many links";
1431 case VKI_EPIPE
: return "Broken pipe";
1432 case VKI_EDOM
: return "Numerical argument out of domain";
1433 case VKI_ERANGE
: return "Numerical result out of range";
1435 case VKI_ENOSYS
: return "Function not implemented";
1436 case VKI_EOVERFLOW
: return "Value too large for defined data type";
1437 # if defined(VKI_ERESTARTSYS)
1438 case VKI_ERESTARTSYS
: return "ERESTARTSYS";
1440 # if defined(VKI_ERESTART)
1441 case VKI_ERESTART
: return "ERESTART";
1443 default: return "VG_(strerror): unknown error";
1448 /*--------------------------------------------------------------------*/
1450 /*--------------------------------------------------------------------*/