drd: Add a consistency check
[valgrind.git] / coregrind / m_syscall.c
blobcd339587012772946413e3326b18fff94e5e6fdd
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-2013 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, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_libcassert.h"
33 #include "pub_core_vki.h"
34 #include "pub_core_vkiscnums.h"
35 #include "pub_core_syscall.h"
37 /* ---------------------------------------------------------------------
38 Building syscall return values.
39 ------------------------------------------------------------------ */
41 #if defined(VGO_linux)
43 /* Make a SysRes value from a syscall return value. This is
44 Linux-specific.
46 From:
47 http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/
48 linux/i386/sysdep.h?
49 rev=1.28&content-type=text/x-cvsweb-markup&cvsroot=glibc
51 Linux uses a negative return value to indicate syscall errors,
52 unlike most Unices, which use the condition codes' carry flag.
54 Since version 2.1 the return value of a system call might be
55 negative even if the call succeeded. E.g., the 'lseek' system call
56 might return a large offset. Therefore we must not anymore test
57 for < 0, but test for a real error by making sure the value in %eax
58 is a real error number. Linus said he will make sure the no
59 syscall returns a value in -1 .. -4095 as a valid result so we can
60 safely test with -4095.
63 SysRes VG_(mk_SysRes_x86_linux) ( Int val ) {
64 SysRes res;
65 res._valEx = 0; /* unused except on mips-linux */
66 res._isError = val >= -4095 && val <= -1;
67 if (res._isError) {
68 res._val = (UInt)(-val);
69 } else {
70 res._val = (UInt)val;
72 return res;
75 /* Similarly .. */
76 SysRes VG_(mk_SysRes_amd64_linux) ( Long val ) {
77 SysRes res;
78 res._valEx = 0; /* unused except on mips-linux */
79 res._isError = val >= -4095 && val <= -1;
80 if (res._isError) {
81 res._val = (ULong)(-val);
82 } else {
83 res._val = (ULong)val;
85 return res;
88 /* PPC uses the CR7.SO bit to flag an error (CR0 in IBM-speak) */
89 /* Note this must be in the bottom bit of the second arg */
90 SysRes VG_(mk_SysRes_ppc32_linux) ( UInt val, UInt cr0so ) {
91 SysRes res;
92 res._valEx = 0; /* unused except on mips-linux */
93 res._isError = (cr0so & 1) != 0;
94 res._val = val;
95 return res;
98 /* As per ppc32 version, cr0.so must be in l.s.b. of 2nd arg */
99 SysRes VG_(mk_SysRes_ppc64_linux) ( ULong val, ULong cr0so ) {
100 SysRes res;
101 res._valEx = 0; /* unused except on mips-linux */
102 res._isError = (cr0so & 1) != 0;
103 res._val = val;
104 return res;
107 SysRes VG_(mk_SysRes_s390x_linux) ( Long val ) {
108 SysRes res;
109 res._valEx = 0; /* unused except on mips-linux */
110 res._isError = val >= -4095 && val <= -1;
111 if (res._isError) {
112 res._val = -val;
113 } else {
114 res._val = val;
116 return res;
119 SysRes VG_(mk_SysRes_arm_linux) ( Int val ) {
120 SysRes res;
121 res._valEx = 0; /* unused except on mips-linux */
122 res._isError = val >= -4095 && val <= -1;
123 if (res._isError) {
124 res._val = (UInt)(-val);
125 } else {
126 res._val = (UInt)val;
128 return res;
131 SysRes VG_(mk_SysRes_arm64_linux) ( Long val ) {
132 SysRes res;
133 res._valEx = 0; /* unused except on mips-linux */
134 res._isError = val >= -4095 && val <= -1;
135 if (res._isError) {
136 res._val = (ULong)(-val);
137 } else {
138 res._val = (ULong)val;
140 return res;
143 /* MIPS uses a3 != 0 to flag an error */
144 SysRes VG_(mk_SysRes_mips32_linux) ( UWord v0, UWord v1, UWord a3 ) {
145 SysRes res;
146 res._isError = (a3 != (UWord)0);
147 res._val = v0;
148 res._valEx = v1;
149 return res;
152 /* MIPS uses a3 != 0 to flag an error */
153 SysRes VG_(mk_SysRes_mips64_linux) ( ULong v0, ULong v1, ULong a3 ) {
154 SysRes res;
155 res._isError = (a3 != (ULong)0);
156 res._val = v0;
157 res._valEx = v1;
158 return res;
161 /* Generic constructors. */
162 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
163 SysRes r;
164 r._valEx = 0; /* unused except on mips-linux */
165 r._isError = True;
166 r._val = err;
167 return r;
170 SysRes VG_(mk_SysRes_Success) ( UWord res ) {
171 SysRes r;
172 r._valEx = 0; /* unused except on mips-linux */
173 r._isError = False;
174 r._val = res;
175 return r;
179 #elif defined(VGO_darwin)
181 /* Darwin: Some syscalls return a double-word result. */
182 SysRes VG_(mk_SysRes_x86_darwin) ( UChar scclass, Bool isErr,
183 UInt wHI, UInt wLO )
185 SysRes res;
186 res._wHI = 0;
187 res._wLO = 0;
188 res._mode = 0; /* invalid */
189 vg_assert(isErr == False || isErr == True);
190 vg_assert(sizeof(UWord) == sizeof(UInt));
191 switch (scclass) {
192 case VG_DARWIN_SYSCALL_CLASS_UNIX:
193 res._wLO = wLO;
194 res._wHI = wHI;
195 res._mode = isErr ? SysRes_UNIX_ERR : SysRes_UNIX_OK;
196 break;
197 case VG_DARWIN_SYSCALL_CLASS_MACH:
198 vg_assert(!isErr);
199 vg_assert(wHI == 0);
200 res._wLO = wLO;
201 res._mode = SysRes_MACH;
202 break;
203 case VG_DARWIN_SYSCALL_CLASS_MDEP:
204 vg_assert(!isErr);
205 vg_assert(wHI == 0);
206 res._wLO = wLO;
207 res._mode = SysRes_MDEP;
208 break;
209 default:
210 vg_assert(0);
212 return res;
215 SysRes VG_(mk_SysRes_amd64_darwin) ( UChar scclass, Bool isErr,
216 ULong wHI, ULong wLO )
218 SysRes res;
219 res._wHI = 0;
220 res._wLO = 0;
221 res._mode = 0; /* invalid */
222 vg_assert(isErr == False || isErr == True);
223 vg_assert(sizeof(UWord) == sizeof(ULong));
224 switch (scclass) {
225 case VG_DARWIN_SYSCALL_CLASS_UNIX:
226 res._wLO = wLO;
227 res._wHI = wHI;
228 res._mode = isErr ? SysRes_UNIX_ERR : SysRes_UNIX_OK;
229 break;
230 case VG_DARWIN_SYSCALL_CLASS_MACH:
231 vg_assert(!isErr);
232 vg_assert(wHI == 0);
233 res._wLO = wLO;
234 res._mode = SysRes_MACH;
235 break;
236 case VG_DARWIN_SYSCALL_CLASS_MDEP:
237 vg_assert(!isErr);
238 vg_assert(wHI == 0);
239 res._wLO = wLO;
240 res._mode = SysRes_MDEP;
241 break;
242 default:
243 vg_assert(0);
245 return res;
248 /* Generic constructors. We assume (without checking if this makes
249 any sense, from the caller's point of view) that these are for the
250 UNIX style of syscall. */
251 SysRes VG_(mk_SysRes_Error) ( UWord err ) {
252 SysRes r;
253 r._wHI = 0;
254 r._wLO = err;
255 r._mode = SysRes_UNIX_ERR;
256 return r;
259 SysRes VG_(mk_SysRes_Success) ( UWord res ) {
260 SysRes r;
261 r._wHI = 0;
262 r._wLO = res;
263 r._mode = SysRes_UNIX_OK;
264 return r;
268 #else
269 # error "Unknown OS"
270 #endif
273 /* ---------------------------------------------------------------------
274 VG_(do_syscall): A function for doing syscalls.
275 ------------------------------------------------------------------ */
277 #if defined(VGP_x86_linux)
278 /* Incoming args (syscall number + up to 6 args) come on the stack.
279 (ie. the C calling convention).
281 The syscall number goes in %eax. The args are passed to the syscall in
282 the regs %ebx, %ecx, %edx, %esi, %edi, %ebp, ie. the kernel's syscall
283 calling convention.
285 %eax gets the return value. Not sure which registers the kernel
286 clobbers, so we preserve all the callee-save regs (%esi, %edi, %ebx,
287 %ebp).
289 extern UWord do_syscall_WRK (
290 UWord syscall_no,
291 UWord a1, UWord a2, UWord a3,
292 UWord a4, UWord a5, UWord a6
294 asm(
295 ".text\n"
296 ".globl do_syscall_WRK\n"
297 "do_syscall_WRK:\n"
298 " push %esi\n"
299 " push %edi\n"
300 " push %ebx\n"
301 " push %ebp\n"
302 " movl 16+ 4(%esp),%eax\n"
303 " movl 16+ 8(%esp),%ebx\n"
304 " movl 16+12(%esp),%ecx\n"
305 " movl 16+16(%esp),%edx\n"
306 " movl 16+20(%esp),%esi\n"
307 " movl 16+24(%esp),%edi\n"
308 " movl 16+28(%esp),%ebp\n"
309 " int $0x80\n"
310 " popl %ebp\n"
311 " popl %ebx\n"
312 " popl %edi\n"
313 " popl %esi\n"
314 " ret\n"
315 ".previous\n"
318 #elif defined(VGP_amd64_linux)
319 /* Incoming args (syscall number + up to 6 args) come in %rdi, %rsi,
320 %rdx, %rcx, %r8, %r9, and the last one on the stack (ie. the C
321 calling convention).
323 The syscall number goes in %rax. The args are passed to the syscall in
324 the regs %rdi, %rsi, %rdx, %r10, %r8, %r9 (yes, really %r10, not %rcx),
325 ie. the kernel's syscall calling convention.
327 %rax gets the return value. %rcx and %r11 are clobbered by the syscall;
328 no matter, they are caller-save (the syscall clobbers no callee-save
329 regs, so we don't have to do any register saving/restoring).
331 extern UWord do_syscall_WRK (
332 UWord syscall_no,
333 UWord a1, UWord a2, UWord a3,
334 UWord a4, UWord a5, UWord a6
336 asm(
337 ".text\n"
338 ".globl do_syscall_WRK\n"
339 "do_syscall_WRK:\n"
340 /* Convert function calling convention --> syscall calling
341 convention */
342 " movq %rdi, %rax\n"
343 " movq %rsi, %rdi\n"
344 " movq %rdx, %rsi\n"
345 " movq %rcx, %rdx\n"
346 " movq %r8, %r10\n"
347 " movq %r9, %r8\n"
348 " movq 8(%rsp), %r9\n" /* last arg from stack */
349 " syscall\n"
350 " ret\n"
351 ".previous\n"
354 #elif defined(VGP_ppc32_linux)
355 /* Incoming args (syscall number + up to 6 args) come in %r3:%r9.
357 The syscall number goes in %r0. The args are passed to the syscall in
358 the regs %r3:%r8, i.e. the kernel's syscall calling convention.
360 The %cr0.so bit flags an error.
361 We return the syscall return value in %r3, and the %cr0.so in
362 the lowest bit of %r4.
363 We return a ULong, of which %r3 is the high word, and %r4 the low.
364 No callee-save regs are clobbered, so no saving/restoring is needed.
366 extern ULong do_syscall_WRK (
367 UWord syscall_no,
368 UWord a1, UWord a2, UWord a3,
369 UWord a4, UWord a5, UWord a6
371 asm(
372 ".text\n"
373 ".globl do_syscall_WRK\n"
374 "do_syscall_WRK:\n"
375 " mr 0,3\n"
376 " mr 3,4\n"
377 " mr 4,5\n"
378 " mr 5,6\n"
379 " mr 6,7\n"
380 " mr 7,8\n"
381 " mr 8,9\n"
382 " sc\n" /* syscall: sets %cr0.so on error */
383 " mfcr 4\n" /* %cr -> low word of return var */
384 " rlwinm 4,4,4,31,31\n" /* rotate flag bit so to lsb, and mask it */
385 " blr\n" /* and return */
386 ".previous\n"
389 #elif defined(VGP_ppc64be_linux)
390 /* Due to the need to return 65 bits of result, this is completely
391 different from the ppc32 case. The single arg register points to a
392 7-word block containing the syscall # and the 6 args. The syscall
393 result proper is put in [0] of the block, and %cr0.so is in the
394 bottom bit of [1]. */
395 extern void do_syscall_WRK ( ULong* argblock );
396 asm(
397 ".align 2\n"
398 ".globl do_syscall_WRK\n"
399 ".section \".opd\",\"aw\"\n"
400 ".align 3\n"
401 "do_syscall_WRK:\n"
402 ".quad .do_syscall_WRK,.TOC.@tocbase,0\n"
403 ".previous\n"
404 ".type .do_syscall_WRK,@function\n"
405 ".globl .do_syscall_WRK\n"
406 ".do_syscall_WRK:\n"
407 " std 3,-16(1)\n" /* stash arg */
408 " ld 8, 48(3)\n" /* sc arg 6 */
409 " ld 7, 40(3)\n" /* sc arg 5 */
410 " ld 6, 32(3)\n" /* sc arg 4 */
411 " ld 5, 24(3)\n" /* sc arg 3 */
412 " ld 4, 16(3)\n" /* sc arg 2 */
413 " ld 0, 0(3)\n" /* sc number */
414 " ld 3, 8(3)\n" /* sc arg 1 */
415 " sc\n" /* result in r3 and cr0.so */
416 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
417 " std 3,0(5)\n" /* argblock[0] = r3 */
418 " mfcr 3\n"
419 " srwi 3,3,28\n"
420 " andi. 3,3,1\n"
421 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
422 " blr\n"
425 #elif defined(VGP_ppc64le_linux)
426 /* Due to the need to return 65 bits of result, this is completely
427 different from the ppc32 case. The single arg register points to a
428 7-word block containing the syscall # and the 6 args. The syscall
429 result proper is put in [0] of the block, and %cr0.so is in the
430 bottom bit of [1]. */
431 extern void do_syscall_WRK ( ULong* argblock );
432 /* Little Endian supports ELF version 2. In the future, it may support
433 * other versions as well.
435 asm(
436 ".align 2\n"
437 ".globl do_syscall_WRK\n"
438 ".type do_syscall_WRK,@function\n"
439 "do_syscall_WRK:\n"
440 "#if _CALL_ELF == 2" "\n"
441 "0: addis 2,12,.TOC.-0b@ha\n"
442 " addi 2,2,.TOC.-0b@l\n"
443 " .localentry do_syscall_WRK, .-do_syscall_WRK\n"
444 "#endif" "\n"
445 " std 3,-16(1)\n" /* stash arg */
446 " ld 8, 48(3)\n" /* sc arg 6 */
447 " ld 7, 40(3)\n" /* sc arg 5 */
448 " ld 6, 32(3)\n" /* sc arg 4 */
449 " ld 5, 24(3)\n" /* sc arg 3 */
450 " ld 4, 16(3)\n" /* sc arg 2 */
451 " ld 0, 0(3)\n" /* sc number */
452 " ld 3, 8(3)\n" /* sc arg 1 */
453 " sc\n" /* result in r3 and cr0.so */
454 " ld 5,-16(1)\n" /* reacquire argblock ptr (r5 is caller-save) */
455 " std 3,0(5)\n" /* argblock[0] = r3 */
456 " mfcr 3\n"
457 " srwi 3,3,28\n"
458 " andi. 3,3,1\n"
459 " std 3,8(5)\n" /* argblock[1] = cr0.s0 & 1 */
460 " blr\n"
461 " .size do_syscall_WRK, .-do_syscall_WRK\n"
464 #elif defined(VGP_arm_linux)
465 /* I think the conventions are:
466 args in r0 r1 r2 r3 r4 r5
467 sysno in r7
468 return value in r0, w/ same conventions as x86-linux, viz r0 in
469 -4096 .. -1 is an error value. All other values are success
470 values.
472 extern UWord do_syscall_WRK (
473 UWord a1, UWord a2, UWord a3,
474 UWord a4, UWord a5, UWord a6,
475 UWord syscall_no
477 asm(
478 ".text\n"
479 ".globl do_syscall_WRK\n"
480 "do_syscall_WRK:\n"
481 " push {r4, r5, r7}\n"
482 " ldr r4, [sp, #12]\n"
483 " ldr r5, [sp, #16]\n"
484 " ldr r7, [sp, #20]\n"
485 " svc 0x0\n"
486 " pop {r4, r5, r7}\n"
487 " bx lr\n"
488 ".previous\n"
491 #elif defined(VGP_arm64_linux)
492 /* I think the conventions are:
493 args in r0 r1 r2 r3 r4 r5
494 sysno in r8
495 return value in r0, w/ same conventions as x86-linux, viz r0 in
496 -4096 .. -1 is an error value. All other values are success
497 values.
499 r0 to r5 remain unchanged, but syscall_no is in r6 and needs
500 to be moved to r8 (??)
502 extern UWord do_syscall_WRK (
503 UWord a1, UWord a2, UWord a3,
504 UWord a4, UWord a5, UWord a6,
505 UWord syscall_no
507 asm(
508 ".text\n"
509 ".globl do_syscall_WRK\n"
510 "do_syscall_WRK:\n"
511 " mov x8, x6\n"
512 " mov x6, 0\n"
513 " mov x7, 0\n"
514 " svc 0\n"
515 " ret\n"
516 ".previous\n"
519 #elif defined(VGP_x86_darwin)
521 /* Incoming args (syscall number + up to 8 args) come in on the stack
523 The kernel's syscall calling convention is:
524 * the syscall number goes in eax
525 * the args are passed to the syscall on the stack,
526 pushed onto the stack R->L (that is, the usual x86
527 calling conventions, with the leftmost arg at the lowest
528 address)
529 Call instruction:
530 * UNIX: sysenter
531 * UNIX: int $0x80
532 * MACH: int $0x81
533 * MDEP: int $0x82
534 Note that the call type can be determined from the syscall number;
535 there is no need to inspect the actual instruction. Although obviously
536 the instruction must match.
537 Return value:
538 * MACH,MDEP: the return value comes back in eax
539 * UNIX: the return value comes back in edx:eax (hi32:lo32)
540 Error:
541 * MACH,MDEP: no error is returned
542 * UNIX: the carry flag indicates success or failure
544 nb here, sizeof(UWord) == sizeof(UInt)
547 __private_extern__ ULong
548 do_syscall_unix_WRK ( UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
549 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
550 UWord a7, UWord a8, /* 28(esp)..32(esp) */
551 UWord syscall_no, /* 36(esp) */
552 /*OUT*/UInt* errflag /* 40(esp) */ );
553 // Unix syscall: 64-bit return in edx:eax, with LSB in eax
554 // error indicated by carry flag: clear=good, set=bad
555 asm(".private_extern _do_syscall_unix_WRK\n"
556 "_do_syscall_unix_WRK:\n"
557 " movl 40(%esp), %ecx \n" /* assume syscall success */
558 " movl $0, (%ecx) \n"
559 " movl 36(%esp), %eax \n"
560 " int $0x80 \n"
561 " jnc 1f \n" /* jump if success */
562 " movl 40(%esp), %ecx \n" /* syscall failed - set *errflag */
563 " movl $1, (%ecx) \n"
564 " 1: ret \n"
567 __private_extern__ UInt
568 do_syscall_mach_WRK ( UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
569 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
570 UWord a7, UWord a8, /* 28(esp)..32(esp) */
571 UWord syscall_no /* 36(esp) */ );
572 // Mach trap: 32-bit result in %eax, no error flag
573 asm(".private_extern _do_syscall_mach_WRK\n"
574 "_do_syscall_mach_WRK:\n"
575 " movl 36(%esp), %eax \n"
576 " int $0x81 \n"
577 " ret \n"
580 __private_extern__ UInt
581 do_syscall_mdep_WRK ( UWord a1, UWord a2, UWord a3, /* 4(esp)..12(esp) */
582 UWord a4, UWord a5, UWord a6, /* 16(esp)..24(esp) */
583 UWord a7, UWord a8, /* 28(esp)..32(esp) */
584 UWord syscall_no /* 36(esp) */ );
585 // mdep trap: 32-bit result in %eax, no error flag
586 asm(
587 ".private_extern _do_syscall_mdep_WRK\n"
588 "_do_syscall_mdep_WRK:\n"
589 " movl 36(%esp), %eax \n"
590 " int $0x82 \n"
591 " ret \n"
595 #elif defined(VGP_amd64_darwin)
597 /* Incoming args (syscall number + up to 8 args) come in registers and stack
599 The kernel's syscall calling convention is:
600 * the syscall number goes in rax
601 * the args are passed to the syscall in registers and the stack
602 * the call instruction is 'syscall'
603 Return value:
604 * MACH,MDEP: the return value comes back in rax
605 * UNIX: the return value comes back in rdx:rax (hi64:lo64)
606 Error:
607 * MACH,MDEP: no error is returned
608 * UNIX: the carry flag indicates success or failure
610 nb here, sizeof(UWord) == sizeof(ULong)
613 __private_extern__ UWord
614 do_syscall_unix_WRK ( UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */
615 UWord a4, UWord a5, UWord a6, /* rcx, r8, r9 */
616 UWord a7, UWord a8, /* 8(rsp), 16(rsp) */
617 UWord syscall_no, /* 24(rsp) */
618 /*OUT*/ULong* errflag, /* 32(rsp) */
619 /*OUT*/ULong* res2 ); /* 40(rsp) */
620 // Unix syscall: 128-bit return in rax:rdx, with LSB in rax
621 // error indicated by carry flag: clear=good, set=bad
622 asm(".private_extern _do_syscall_unix_WRK\n"
623 "_do_syscall_unix_WRK:\n"
624 " movq %rcx, %r10 \n" /* pass rcx in r10 instead */
625 " movq 32(%rsp), %rax \n" /* assume syscall success */
626 " movq $0, (%rax) \n"
627 " movq 24(%rsp), %rax \n" /* load syscall_no */
628 " syscall \n"
629 " jnc 1f \n" /* jump if success */
630 " movq 32(%rsp), %rcx \n" /* syscall failed - set *errflag */
631 " movq $1, (%rcx) \n"
632 " 1: movq 40(%rsp), %rcx \n" /* save 2nd result word */
633 " movq %rdx, (%rcx) \n"
634 " retq \n" /* return 1st result word */
637 __private_extern__ UWord
638 do_syscall_mach_WRK ( UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */
639 UWord a4, UWord a5, UWord a6, /* rcx, r8, r9 */
640 UWord a7, UWord a8, /* 8(rsp), 16(rsp) */
641 UWord syscall_no ); /* 24(rsp) */
642 // Mach trap: 64-bit result, no error flag
643 asm(".private_extern _do_syscall_mach_WRK\n"
644 "_do_syscall_mach_WRK:\n"
645 " movq %rcx, %r10 \n" /* pass rcx in r10 instead */
646 " movq 24(%rsp), %rax \n" /* load syscall_no */
647 " syscall \n"
648 " retq \n"
651 #elif defined(VGP_s390x_linux)
653 static UWord do_syscall_WRK (
654 UWord syscall_no,
655 UWord arg1, UWord arg2, UWord arg3,
656 UWord arg4, UWord arg5, UWord arg6
659 register UWord __arg1 asm("2") = arg1;
660 register UWord __arg2 asm("3") = arg2;
661 register UWord __arg3 asm("4") = arg3;
662 register UWord __arg4 asm("5") = arg4;
663 register UWord __arg5 asm("6") = arg5;
664 register UWord __arg6 asm("7") = arg6;
665 register ULong __svcres asm("2");
667 __asm__ __volatile__ (
668 "lgr %%r1,%1\n\t"
669 "svc 0\n\t"
670 : "=d" (__svcres)
671 : "a" (syscall_no),
672 "0" (__arg1),
673 "d" (__arg2),
674 "d" (__arg3),
675 "d" (__arg4),
676 "d" (__arg5),
677 "d" (__arg6)
678 : "1", "cc", "memory");
680 return (UWord) (__svcres);
683 #elif defined(VGP_mips32_linux)
684 /* Incoming args (syscall number + up to 6 args) come in a0 - a3 and stack.
686 The syscall number goes in v0. The args are passed to the syscall in
687 the regs a0 - a3 and stack, i.e. the kernel's syscall calling convention.
689 (a3 != 0) flags an error.
690 We return the syscall return value in v0.
691 MIPS version
693 extern int do_syscall_WRK (
694 int a1, int a2, int a3,
695 int a4, int a5, int a6, int syscall_no, UWord *err,
696 UWord *valHi, UWord* valLo
698 asm(
699 ".globl do_syscall_WRK\n"
700 ".ent do_syscall_WRK\n"
701 ".text\n"
702 "do_syscall_WRK:\n"
703 " lw $2, 24($29)\n"
704 " syscall\n"
705 " lw $8, 28($29)\n"
706 " sw $7, ($8)\n"
707 " lw $8, 32($29)\n"
708 " sw $3, ($8)\n" // store valHi
709 " lw $8, 36($29)\n"
710 " sw $2, ($8)\n" // store valLo
711 " jr $31\n"
712 " nop\n"
713 ".previous\n"
714 ".end do_syscall_WRK\n"
717 #elif defined(VGP_mips64_linux)
718 extern UWord do_syscall_WRK ( UWord a1, UWord a2, UWord a3, UWord a4, UWord a5,
719 UWord a6, UWord syscall_no, ULong* V1_val );
720 asm (
721 ".text\n"
722 ".globl do_syscall_WRK\n"
723 "do_syscall_WRK:\n"
724 " daddiu $29, $29, -8\n"
725 " sd $11, 0($29)\n"
726 " move $2, $10\n"
727 " syscall\n"
728 " ld $11, 0($29)\n"
729 " daddiu $29, $29, 8\n"
730 " sd $3, 0($11)\n" /* store vale of v1 in last param */
731 " sd $7, 8($11)\n" /* store vale of a3 in last param */
732 " jr $31\n"
733 ".previous\n"
736 #else
737 # error Unknown platform
738 #endif
741 /* Finally, the generic code. This sends the call to the right
742 helper. */
744 SysRes VG_(do_syscall) ( UWord sysno, UWord a1, UWord a2, UWord a3,
745 UWord a4, UWord a5, UWord a6,
746 UWord a7, UWord a8 )
748 # if defined(VGP_x86_linux)
749 UWord val = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
750 return VG_(mk_SysRes_x86_linux)( val );
752 # elif defined(VGP_amd64_linux)
753 UWord val = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
754 return VG_(mk_SysRes_amd64_linux)( val );
756 # elif defined(VGP_ppc32_linux)
757 ULong ret = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
758 UInt val = (UInt)(ret>>32);
759 UInt cr0so = (UInt)(ret);
760 return VG_(mk_SysRes_ppc32_linux)( val, cr0so );
762 # elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
763 ULong argblock[7];
764 argblock[0] = sysno;
765 argblock[1] = a1;
766 argblock[2] = a2;
767 argblock[3] = a3;
768 argblock[4] = a4;
769 argblock[5] = a5;
770 argblock[6] = a6;
771 do_syscall_WRK( &argblock[0] );
772 return VG_(mk_SysRes_ppc64_linux)( argblock[0], argblock[1] );
774 # elif defined(VGP_arm_linux)
775 UWord val = do_syscall_WRK(a1,a2,a3,a4,a5,a6,sysno);
776 return VG_(mk_SysRes_arm_linux)( val );
778 # elif defined(VGP_arm64_linux)
779 UWord val = do_syscall_WRK(a1,a2,a3,a4,a5,a6,sysno);
780 return VG_(mk_SysRes_arm64_linux)( val );
782 # elif defined(VGP_x86_darwin)
783 UInt wLO = 0, wHI = 0, err = 0;
784 ULong u64;
785 UChar scclass = VG_DARWIN_SYSNO_CLASS(sysno);
786 switch (scclass) {
787 case VG_DARWIN_SYSCALL_CLASS_UNIX:
788 u64 = do_syscall_unix_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
789 VG_DARWIN_SYSNO_FOR_KERNEL(sysno), &err);
790 wLO = (UInt)u64;
791 wHI = (UInt)(u64 >> 32);
792 break;
793 case VG_DARWIN_SYSCALL_CLASS_MACH:
794 wLO = do_syscall_mach_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
795 VG_DARWIN_SYSNO_FOR_KERNEL(sysno));
796 err = 0;
797 break;
798 case VG_DARWIN_SYSCALL_CLASS_MDEP:
799 wLO = do_syscall_mdep_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
800 VG_DARWIN_SYSNO_FOR_KERNEL(sysno));
801 err = 0;
802 break;
803 default:
804 vg_assert(0);
805 break;
807 return VG_(mk_SysRes_x86_darwin)( scclass, err ? True : False, wHI, wLO );
809 # elif defined(VGP_amd64_darwin)
810 ULong wLO = 0, wHI = 0, err = 0;
811 UChar scclass = VG_DARWIN_SYSNO_CLASS(sysno);
812 switch (scclass) {
813 case VG_DARWIN_SYSCALL_CLASS_UNIX:
814 wLO = do_syscall_unix_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
815 VG_DARWIN_SYSNO_FOR_KERNEL(sysno), &err, &wHI);
816 break;
817 case VG_DARWIN_SYSCALL_CLASS_MACH:
818 case VG_DARWIN_SYSCALL_CLASS_MDEP:
819 wLO = do_syscall_mach_WRK(a1,a2,a3,a4,a5,a6,a7,a8,
820 VG_DARWIN_SYSNO_FOR_KERNEL(sysno));
821 err = 0;
822 break;
823 default:
824 vg_assert(0);
825 break;
827 return VG_(mk_SysRes_amd64_darwin)( scclass, err ? True : False, wHI, wLO );
829 #elif defined(VGP_s390x_linux)
830 UWord val;
832 if (sysno == __NR_mmap) {
833 ULong argbuf[6];
835 argbuf[0] = a1;
836 argbuf[1] = a2;
837 argbuf[2] = a3;
838 argbuf[3] = a4;
839 argbuf[4] = a5;
840 argbuf[5] = a6;
841 val = do_syscall_WRK(sysno,(UWord)&argbuf[0],0,0,0,0,0);
842 } else {
843 val = do_syscall_WRK(sysno,a1,a2,a3,a4,a5,a6);
846 return VG_(mk_SysRes_s390x_linux)( val );
848 #elif defined(VGP_mips32_linux)
849 UWord err = 0;
850 UWord valHi = 0;
851 UWord valLo = 0;
852 (void) do_syscall_WRK(a1,a2,a3,a4,a5,a6, sysno,&err,&valHi,&valLo);
853 return VG_(mk_SysRes_mips32_linux)( valLo, valHi, (ULong)err );
855 #elif defined(VGP_mips64_linux)
856 ULong v1_a3[2];
857 v1_a3[0] = 0xFF00;
858 v1_a3[1] = 0xFF00;
859 ULong V0 = do_syscall_WRK(a1,a2,a3,a4,a5,a6,sysno,v1_a3);
860 ULong V1 = (ULong)v1_a3[0];
861 ULong A3 = (ULong)v1_a3[1];
862 return VG_(mk_SysRes_mips64_linux)( V0, V1, A3 );
864 #else
865 # error Unknown platform
866 #endif
869 /* ---------------------------------------------------------------------
870 Names of errors.
871 ------------------------------------------------------------------ */
873 /* Return a string which gives the name of an error value. Note,
874 unlike the standard C syserror fn, the returned string is not
875 malloc-allocated or writable -- treat it as a constant.
876 TODO: implement this properly. */
878 const HChar* VG_(strerror) ( UWord errnum )
880 switch (errnum) {
881 case VKI_EPERM: return "Operation not permitted";
882 case VKI_ENOENT: return "No such file or directory";
883 case VKI_ESRCH: return "No such process";
884 case VKI_EINTR: return "Interrupted system call";
885 case VKI_EIO: return "Input/output error";
886 case VKI_ENXIO: return "No such device or address";
887 case VKI_E2BIG: return "Argument list too long";
888 case VKI_ENOEXEC: return "Exec format error";
889 case VKI_EBADF: return "Bad file descriptor";
890 case VKI_ECHILD: return "No child processes";
891 case VKI_EAGAIN: return "Resource temporarily unavailable";
892 case VKI_ENOMEM: return "Cannot allocate memory";
893 case VKI_EACCES: return "Permission denied";
894 case VKI_EFAULT: return "Bad address";
895 case VKI_ENOTBLK: return "Block device required";
896 case VKI_EBUSY: return "Device or resource busy";
897 case VKI_EEXIST: return "File exists";
898 case VKI_EXDEV: return "Invalid cross-device link";
899 case VKI_ENODEV: return "No such device";
900 case VKI_ENOTDIR: return "Not a directory";
901 case VKI_EISDIR: return "Is a directory";
902 case VKI_EINVAL: return "Invalid argument";
903 case VKI_ENFILE: return "Too many open files in system";
904 case VKI_EMFILE: return "Too many open files";
905 case VKI_ENOTTY: return "Inappropriate ioctl for device";
906 case VKI_ETXTBSY: return "Text file busy";
907 case VKI_EFBIG: return "File too large";
908 case VKI_ENOSPC: return "No space left on device";
909 case VKI_ESPIPE: return "Illegal seek";
910 case VKI_EROFS: return "Read-only file system";
911 case VKI_EMLINK: return "Too many links";
912 case VKI_EPIPE: return "Broken pipe";
913 case VKI_EDOM: return "Numerical argument out of domain";
914 case VKI_ERANGE: return "Numerical result out of range";
916 case VKI_ENOSYS: return "Function not implemented";
917 case VKI_EOVERFLOW: return "Value too large for defined data type";
918 # if defined(VKI_ERESTARTSYS)
919 case VKI_ERESTARTSYS: return "ERESTARTSYS";
920 # endif
921 default: return "VG_(strerror): unknown error";
926 /*--------------------------------------------------------------------*/
927 /*--- end ---*/
928 /*--------------------------------------------------------------------*/