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, cr0.so must be in l.s.b. of 2nd arg */
149 SysRes
VG_(mk_SysRes_ppc64_linux
) ( ULong val
, ULong cr0so
) {
151 res
._isError
= (cr0so
& 1) != 0;
156 SysRes
VG_(mk_SysRes_s390x_linux
) ( Long val
) {
158 res
._isError
= val
>= -4095 && val
<= -1;
167 SysRes
VG_(mk_SysRes_arm_linux
) ( Int val
) {
169 res
._isError
= val
>= -4095 && val
<= -1;
171 res
._val
= (UInt
)(-val
);
173 res
._val
= (UInt
)val
;
178 SysRes
VG_(mk_SysRes_arm64_linux
) ( Long val
) {
180 res
._isError
= val
>= -4095 && val
<= -1;
182 res
._val
= (ULong
)(-val
);
184 res
._val
= (ULong
)val
;
189 /* Generic constructors. */
190 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
197 #if defined(VGP_nanomips_linux)
198 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
201 r
._val
= (UWord
)(-(Word
)err
);
205 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
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
,
224 res
._mode
= 0; /* invalid */
225 vg_assert(isErr
== False
|| isErr
== True
);
226 vg_assert(sizeof(UWord
) == sizeof(UInt
));
228 case VG_DARWIN_SYSCALL_CLASS_UNIX
:
231 res
._mode
= isErr
? SysRes_UNIX_ERR
: SysRes_UNIX_OK
;
233 case VG_DARWIN_SYSCALL_CLASS_MACH
:
237 res
._mode
= SysRes_MACH
;
239 case VG_DARWIN_SYSCALL_CLASS_MDEP
:
243 res
._mode
= SysRes_MDEP
;
251 SysRes
VG_(mk_SysRes_amd64_darwin
) ( UChar scclass
, Bool isErr
,
252 ULong wHI
, ULong wLO
)
257 res
._mode
= 0; /* invalid */
258 vg_assert(isErr
== False
|| isErr
== True
);
259 vg_assert(sizeof(UWord
) == sizeof(ULong
));
261 case VG_DARWIN_SYSCALL_CLASS_UNIX
:
264 res
._mode
= isErr
? SysRes_UNIX_ERR
: SysRes_UNIX_OK
;
266 case VG_DARWIN_SYSCALL_CLASS_MACH
:
270 res
._mode
= SysRes_MACH
;
272 case VG_DARWIN_SYSCALL_CLASS_MDEP
:
276 res
._mode
= SysRes_MDEP
;
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
) {
291 r
._mode
= SysRes_UNIX_ERR
;
295 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
299 r
._mode
= SysRes_UNIX_OK
;
304 #elif defined(VGO_solaris)
306 /* Generic constructors. */
307 SysRes
VG_(mk_SysRes_Error
) ( UWord err
) {
315 SysRes
VG_(mk_SysRes_Success
) ( UWord res
) {
323 SysRes
VG_(mk_SysRes_x86_solaris
) ( Bool isErr
, UInt val
, UInt val2
)
328 vg_assert(isErr
== True
|| isErr
== False
);
332 res
._isError
= isErr
;
336 SysRes
VG_(mk_SysRes_amd64_solaris
) ( Bool isErr
, ULong val
, ULong val2
)
341 vg_assert(isErr
== True
|| isErr
== False
);
345 res
._isError
= isErr
;
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
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,
370 extern UWord
do_syscall_WRK (
372 UWord a1
, UWord a2
, UWord a3
,
373 UWord a4
, UWord a5
, UWord a6
377 ".globl do_syscall_WRK\n"
381 " .cfi_adjust_cfa_offset 4\n"
382 " .cfi_offset %esi, -8\n"
384 " .cfi_adjust_cfa_offset 4\n"
385 " .cfi_offset %edi, -12\n"
387 " .cfi_adjust_cfa_offset 4\n"
388 " .cfi_offset %ebx, -16\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"
401 " .cfi_adjust_cfa_offset -4\n"
402 " .cfi_restore %ebp\n"
404 " .cfi_adjust_cfa_offset -4\n"
405 " .cfi_restore %ebx\n"
407 " .cfi_adjust_cfa_offset -4\n"
408 " .cfi_restore %edi\n"
410 " .cfi_adjust_cfa_offset -4\n"
411 " .cfi_restore %esi\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
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 (
432 UWord a1
, UWord a2
, UWord a3
,
433 UWord a4
, UWord a5
, UWord a6
437 ".globl do_syscall_WRK\n"
439 /* Convert function calling convention --> syscall calling
447 " movq 8(%rsp), %r9\n" /* last arg from stack */
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 (
467 UWord a1
, UWord a2
, UWord a3
,
468 UWord a4
, UWord a5
, UWord a6
472 ".globl do_syscall_WRK\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 */
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
);
497 ".globl do_syscall_WRK\n"
498 ".section \".opd\",\"aw\"\n"
501 ".quad .do_syscall_WRK,.TOC.@tocbase,0\n"
503 ".type .do_syscall_WRK,@function\n"
504 ".globl .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 */
520 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
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.
536 ".globl do_syscall_WRK\n"
537 ".type do_syscall_WRK,@function\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"
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 */
558 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
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
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
571 extern UWord
do_syscall_WRK (
572 UWord a1
, UWord a2
, UWord a3
,
573 UWord a4
, UWord a5
, UWord a6
,
578 ".globl 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"
585 " pop {r4, r5, r7}\n"
590 #elif defined(VGP_arm64_linux)
591 /* I think the conventions are:
592 args in r0 r1 r2 r3 r4 r5
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
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
,
608 ".globl do_syscall_WRK\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
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.
637 * MACH,MDEP: the return value comes back in eax
638 * UNIX: the return value comes back in edx:eax (hi32:lo32)
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"
660 " jnc 1f \n" /* jump if success */
661 " movl 40(%esp), %ecx \n" /* syscall failed - set *errflag */
662 " movl $1, (%ecx) \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"
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
686 ".private_extern _do_syscall_mdep_WRK\n"
687 "_do_syscall_mdep_WRK:\n"
688 " movl 36(%esp), %eax \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'
703 * MACH,MDEP: the return value comes back in rax
704 * UNIX: the return value comes back in rdx:rax (hi64:lo64)
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 */
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 */
750 #elif defined(VGP_s390x_linux)
752 static UWord
do_syscall_WRK (
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__ (
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.
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
799 ".globl do_syscall_WRK \n\t"
800 ".type do_syscall_WRK, @function \n\t"
802 ".set noreorder \n\t"
803 "do_syscall_WRK: \n\t"
804 " lw $2, 28($29) \n\t"
806 " lw $8, 32($29) \n\t"
808 " lw $8, 36($29) \n\t"
809 " sw $3, ($8) \n\t" /* store valHi */
810 " lw $8, 40($29) \n\t"
812 " sw $2, ($8) \n\t" /* store valLo */
813 ".size do_syscall_WRK, .-do_syscall_WRK \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
);
824 ".globl do_syscall_WRK \n\t"
825 ".type do_syscall_WRK, @function \n\t"
827 ".set noreorder \n\t"
828 "do_syscall_WRK: \n\t"
832 " ld $12, 0($29) \n\t"
833 # elif defined(_ABIN32)
834 " lw $12, 0($29) \n\t"
836 " sd $3, 0($12) \n\t" /* store v1 in V1_A3_val */
838 " sd $7, 8($12) \n\t" /* store a3 in V1_A3_val */
839 ".size do_syscall_WRK, .-do_syscall_WRK \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
);
851 ".globl do_syscall_WRK \n\t"
852 ".type do_syscall_WRK, @function \n\t"
854 ".set noreorder \n\t"
855 "do_syscall_WRK: \n\t"
857 " move $t4, $a6 \n\t"
859 " restore 32, $a7 \n\t"
860 " sw $a0, 0($a7) \n\t"
862 ".size do_syscall_WRK, .-do_syscall_WRK \n\t"
867 #elif defined(VGP_x86_solaris)
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. */
880 ".globl do_syscall_WRK\n"
882 " movl 40(%esp), %ecx\n" /* assume syscall success */
884 " movl 36(%esp), %eax\n"
886 " jnc 1f\n" /* jump if success */
887 " movl 40(%esp), %ecx\n" /* syscall failed - set *errflag */
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). */
899 ".globl do_syscall_fast_WRK\n"
900 "do_syscall_fast_WRK:\n"
901 " movl 4(%esp), %eax\n"
907 #elif defined(VGP_amd64_solaris)
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
922 ".globl do_syscall_WRK\n"
924 " movq %rcx, %r10\n" /* pass rcx in r10 instead */
925 " movq 32(%rsp), %rcx\n" /* assume syscall success */
927 " movq 24(%rsp), %rax\n"
929 " jnc 1f\n" /* jump if success */
930 " movq 32(%rsp), %rcx\n" /* syscall failed - set *errflag */
932 "1: movq 40(%rsp), %rcx\n" /* save 2nd result word */
933 " movq %rdx, (%rcx)\n"
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). */
945 ".globl do_syscall_fast_WRK\n"
946 "do_syscall_fast_WRK:\n"
949 " movq %rdx, (%rsi)\n" /* save 2nd result word */
955 # error Unknown platform
959 /* Finally, the generic code. This sends the call to the right
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)
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;
1003 UChar scclass
= VG_DARWIN_SYSNO_CLASS(sysno
);
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
);
1009 wHI
= (UInt
)(u64
>> 32);
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
));
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
));
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
);
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
);
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
));
1045 return VG_(mk_SysRes_amd64_darwin
)( scclass
, err
? True
: False
, wHI
, wLO
);
1047 #elif defined(VGP_s390x_linux)
1050 if (sysno
== __NR_mmap
) {
1059 val
= do_syscall_WRK(sysno
,(UWord
)&argbuf
[0],0,0,0,0,0);
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)
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)
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)
1084 do_syscall_WRK(a1
, a2
, a3
, a4
, a5
, a6
, sysno
, ®_a0
);
1085 return VG_(mk_SysRes_nanomips_linux
)(reg_a0
);
1087 # elif defined(VGP_x86_solaris)
1088 UInt val
, val2
, err
= False
;
1091 UChar ssclass
= VG_SOLARIS_SYSNO_CLASS(sysno
);
1094 case VG_SOLARIS_SYSCALL_CLASS_CLASSIC
:
1095 /* The Solaris kernel does not restart syscalls automatically so it
1098 u64
= do_syscall_WRK(a1
,a2
,a3
,a4
,a5
,a6
,a7
,a8
,
1099 VG_SOLARIS_SYSNO_INDEX(sysno
), &err
);
1101 restart
= err
&& (val
== VKI_EINTR
|| val
== VKI_ERESTART
);
1104 case VG_SOLARIS_SYSCALL_CLASS_FASTTRAP
:
1105 u64
= do_syscall_fast_WRK(VG_SOLARIS_SYSNO_INDEX(sysno
));
1113 val2
= (UInt
)(u64
>> 32);
1114 return VG_(mk_SysRes_x86_solaris
)(err
? True
: False
, val
,
1117 # elif defined(VGP_amd64_solaris)
1118 ULong val
, val2
, err
= False
;
1120 UChar ssclass
= VG_SOLARIS_SYSNO_CLASS(sysno
);
1123 case VG_SOLARIS_SYSCALL_CLASS_CLASSIC
:
1124 /* The Solaris kernel does not restart syscalls automatically so it
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
);
1132 case VG_SOLARIS_SYSCALL_CLASS_FASTTRAP
:
1133 val
= do_syscall_fast_WRK(VG_SOLARIS_SYSNO_INDEX(sysno
), &val2
);
1140 return VG_(mk_SysRes_amd64_solaris
)(err
? True
: False
, val
,
1144 # error Unknown platform
1148 /* ---------------------------------------------------------------------
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
)
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";
1200 default: return "VG_(strerror): unknown error";
1205 /*--------------------------------------------------------------------*/
1207 /*--------------------------------------------------------------------*/