Home | History | Annotate | Download | only in m_syswrap
      1 
      2 /*--------------------------------------------------------------------*/
      3 /*--- Linux-specific syscalls, etc.                syswrap-linux.c ---*/
      4 /*--------------------------------------------------------------------*/
      5 
      6 /*
      7    This file is part of Valgrind, a dynamic binary instrumentation
      8    framework.
      9 
     10    Copyright (C) 2000-2011 Nicholas Nethercote
     11       njn (at) valgrind.org
     12 
     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.
     17 
     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.
     22 
     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.
     27 
     28    The GNU General Public License is contained in the file COPYING.
     29 */
     30 
     31 #if defined(VGO_linux)
     32 
     33 #include "pub_core_basics.h"
     34 #include "pub_core_vki.h"
     35 #include "pub_core_vkiscnums.h"
     36 #include "pub_core_libcsetjmp.h"   // to keep _threadstate.h happy
     37 #include "pub_core_threadstate.h"
     38 #include "pub_core_aspacemgr.h"
     39 #include "pub_core_debuginfo.h"    // VG_(di_notify_*)
     40 #include "pub_core_transtab.h"     // VG_(discard_translations)
     41 #include "pub_core_xarray.h"
     42 #include "pub_core_clientstate.h"
     43 #include "pub_core_debuglog.h"
     44 #include "pub_core_libcbase.h"
     45 #include "pub_core_libcassert.h"
     46 #include "pub_core_libcfile.h"
     47 #include "pub_core_libcprint.h"
     48 #include "pub_core_libcproc.h"
     49 #include "pub_core_libcsignal.h"
     50 #include "pub_core_mallocfree.h"
     51 #include "pub_core_tooliface.h"
     52 #include "pub_core_options.h"
     53 #include "pub_core_scheduler.h"
     54 #include "pub_core_signals.h"
     55 #include "pub_core_syscall.h"
     56 #include "pub_core_syswrap.h"
     57 
     58 #include "priv_types_n_macros.h"
     59 #include "priv_syswrap-generic.h"
     60 #include "priv_syswrap-linux.h"
     61 
     62 
     63 // Run a thread from beginning to end and return the thread's
     64 // scheduler-return-code.
     65 static VgSchedReturnCode thread_wrapper(Word /*ThreadId*/ tidW)
     66 {
     67    VgSchedReturnCode ret;
     68    ThreadId     tid = (ThreadId)tidW;
     69    ThreadState* tst = VG_(get_ThreadState)(tid);
     70 
     71    VG_(debugLog)(1, "syswrap-linux",
     72                     "thread_wrapper(tid=%lld): entry\n",
     73                     (ULong)tidW);
     74 
     75    vg_assert(tst->status == VgTs_Init);
     76 
     77    /* make sure we get the CPU lock before doing anything significant */
     78    VG_(acquire_BigLock)(tid, "thread_wrapper(starting new thread)");
     79 
     80    if (0)
     81       VG_(printf)("thread tid %d started: stack = %p\n",
     82 		  tid, &tid);
     83 
     84    /* Make sure error reporting is enabled in the new thread. */
     85    tst->err_disablement_level = 0;
     86 
     87    VG_TRACK(pre_thread_first_insn, tid);
     88 
     89    tst->os_state.lwpid = VG_(gettid)();
     90    /* Set the threadgroup for real.  This overwrites the provisional
     91       value set in do_clone() syswrap-*-linux.c.  See comments in
     92       do_clone for background, also #226116. */
     93    tst->os_state.threadgroup = VG_(getpid)();
     94 
     95    /* Thread created with all signals blocked; scheduler will set the
     96       appropriate mask */
     97 
     98    ret = VG_(scheduler)(tid);
     99 
    100    vg_assert(VG_(is_exiting)(tid));
    101 
    102    vg_assert(tst->status == VgTs_Runnable);
    103    vg_assert(VG_(is_running_thread)(tid));
    104 
    105    VG_(debugLog)(1, "syswrap-linux",
    106                     "thread_wrapper(tid=%lld): exit\n",
    107                     (ULong)tidW);
    108 
    109    /* Return to caller, still holding the lock. */
    110    return ret;
    111 }
    112 
    113 
    114 /* ---------------------------------------------------------------------
    115    clone-related stuff
    116    ------------------------------------------------------------------ */
    117 
    118 /* Run a thread all the way to the end, then do appropriate exit actions
    119    (this is the last-one-out-turn-off-the-lights bit).  */
    120 static void run_a_thread_NORETURN ( Word tidW )
    121 {
    122    ThreadId          tid = (ThreadId)tidW;
    123    VgSchedReturnCode src;
    124    Int               c;
    125    ThreadState*      tst;
    126 
    127    VG_(debugLog)(1, "syswrap-linux",
    128                     "run_a_thread_NORETURN(tid=%lld): pre-thread_wrapper\n",
    129                     (ULong)tidW);
    130 
    131    tst = VG_(get_ThreadState)(tid);
    132    vg_assert(tst);
    133 
    134    /* Run the thread all the way through. */
    135    src = thread_wrapper(tid);
    136 
    137    VG_(debugLog)(1, "syswrap-linux",
    138                     "run_a_thread_NORETURN(tid=%lld): post-thread_wrapper\n",
    139                     (ULong)tidW);
    140 
    141    c = VG_(count_living_threads)();
    142    vg_assert(c >= 1); /* stay sane */
    143 
    144    // Tell the tool this thread is exiting
    145    VG_TRACK( pre_thread_ll_exit, tid );
    146 
    147    /* If the thread is exiting with errors disabled, complain loudly;
    148       doing so is bad (does the user know this has happened?)  Also,
    149       in all cases, be paranoid and clear the flag anyway so that the
    150       thread slot is safe in this respect if later reallocated.  This
    151       should be unnecessary since the flag should be cleared when the
    152       slot is reallocated, in thread_wrapper(). */
    153    if (tst->err_disablement_level > 0) {
    154       VG_(umsg)(
    155          "WARNING: exiting thread has error reporting disabled.\n"
    156          "WARNING: possibly as a result of some mistake in the use\n"
    157          "WARNING: of the VALGRIND_DISABLE_ERROR_REPORTING macros.\n"
    158       );
    159       VG_(debugLog)(
    160          1, "syswrap-linux",
    161             "run_a_thread_NORETURN(tid=%lld): "
    162             "WARNING: exiting thread has err_disablement_level = %u\n",
    163             (ULong)tidW, tst->err_disablement_level
    164       );
    165    }
    166    tst->err_disablement_level = 0;
    167 
    168    if (c == 1) {
    169 
    170       VG_(debugLog)(1, "syswrap-linux",
    171                        "run_a_thread_NORETURN(tid=%lld): "
    172                           "last one standing\n",
    173                           (ULong)tidW);
    174 
    175       /* We are the last one standing.  Keep hold of the lock and
    176          carry on to show final tool results, then exit the entire system.
    177          Use the continuation pointer set at startup in m_main. */
    178       ( * VG_(address_of_m_main_shutdown_actions_NORETURN) ) (tid, src);
    179 
    180    } else {
    181 
    182       VG_(debugLog)(1, "syswrap-linux",
    183                        "run_a_thread_NORETURN(tid=%lld): "
    184                           "not last one standing\n",
    185                           (ULong)tidW);
    186 
    187       /* OK, thread is dead, but others still exist.  Just exit. */
    188 
    189       /* This releases the run lock */
    190       VG_(exit_thread)(tid);
    191       vg_assert(tst->status == VgTs_Zombie);
    192 
    193       /* We have to use this sequence to terminate the thread to
    194          prevent a subtle race.  If VG_(exit_thread)() had left the
    195          ThreadState as Empty, then it could have been reallocated,
    196          reusing the stack while we're doing these last cleanups.
    197          Instead, VG_(exit_thread) leaves it as Zombie to prevent
    198          reallocation.  We need to make sure we don't touch the stack
    199          between marking it Empty and exiting.  Hence the
    200          assembler. */
    201 #if defined(VGP_x86_linux)
    202       asm volatile (
    203          "movl	%1, %0\n"	/* set tst->status = VgTs_Empty */
    204          "movl	%2, %%eax\n"    /* set %eax = __NR_exit */
    205          "movl	%3, %%ebx\n"    /* set %ebx = tst->os_state.exitcode */
    206          "int	$0x80\n"	/* exit(tst->os_state.exitcode) */
    207          : "=m" (tst->status)
    208          : "n" (VgTs_Empty), "n" (__NR_exit), "m" (tst->os_state.exitcode)
    209          : "eax"/*, "ebx"*/ // EBX is a PIC register.
    210       );
    211 #elif defined(VGP_amd64_linux)
    212       asm volatile (
    213          "movl	%1, %0\n"	/* set tst->status = VgTs_Empty */
    214          "movq	%2, %%rax\n"    /* set %rax = __NR_exit */
    215          "movq	%3, %%rdi\n"    /* set %rdi = tst->os_state.exitcode */
    216          "syscall\n"		/* exit(tst->os_state.exitcode) */
    217          : "=m" (tst->status)
    218          : "n" (VgTs_Empty), "n" (__NR_exit), "m" (tst->os_state.exitcode)
    219          : "rax", "rdi"
    220       );
    221 #elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
    222       { UInt vgts_empty = (UInt)VgTs_Empty;
    223         asm volatile (
    224           "stw %1,%0\n\t"          /* set tst->status = VgTs_Empty */
    225           "li  0,%2\n\t"           /* set r0 = __NR_exit */
    226           "lwz 3,%3\n\t"           /* set r3 = tst->os_state.exitcode */
    227           "sc\n\t"                 /* exit(tst->os_state.exitcode) */
    228           : "=m" (tst->status)
    229           : "r" (vgts_empty), "n" (__NR_exit), "m" (tst->os_state.exitcode)
    230           : "r0", "r3"
    231         );
    232       }
    233 #elif defined(VGP_arm_linux)
    234       asm volatile (
    235          "str  %1, %0\n"      /* set tst->status = VgTs_Empty */
    236          "mov  r7, %2\n"      /* set %r7 = __NR_exit */
    237          "ldr  r0, %3\n"      /* set %r0 = tst->os_state.exitcode */
    238          "svc  0x00000000\n"  /* exit(tst->os_state.exitcode) */
    239          : "=m" (tst->status)
    240          : "r" (VgTs_Empty), "n" (__NR_exit), "m" (tst->os_state.exitcode)
    241          : "r0", "r7"
    242       );
    243 #elif defined(VGP_s390x_linux)
    244       asm volatile (
    245          "st   %1, %0\n"        /* set tst->status = VgTs_Empty */
    246          "lg   2, %3\n"         /* set r2 = tst->os_state.exitcode */
    247          "svc %2\n"             /* exit(tst->os_state.exitcode) */
    248          : "=m" (tst->status)
    249          : "d" (VgTs_Empty), "n" (__NR_exit), "m" (tst->os_state.exitcode)
    250          : "2"
    251       );
    252 #else
    253 # error Unknown platform
    254 #endif
    255 
    256       VG_(core_panic)("Thread exit failed?\n");
    257    }
    258 
    259    /*NOTREACHED*/
    260    vg_assert(0);
    261 }
    262 
    263 Word ML_(start_thread_NORETURN) ( void* arg )
    264 {
    265    ThreadState* tst = (ThreadState*)arg;
    266    ThreadId     tid = tst->tid;
    267 
    268    run_a_thread_NORETURN ( (Word)tid );
    269    /*NOTREACHED*/
    270    vg_assert(0);
    271 }
    272 
    273 /* Allocate a stack for this thread, if it doesn't already have one.
    274    They're allocated lazily, and never freed.  Returns the initial stack
    275    pointer value to use, or 0 if allocation failed. */
    276 Addr ML_(allocstack)(ThreadId tid)
    277 {
    278    ThreadState* tst = VG_(get_ThreadState)(tid);
    279    VgStack*     stack;
    280    Addr         initial_SP;
    281 
    282    /* Either the stack_base and stack_init_SP are both zero (in which
    283       case a stack hasn't been allocated) or they are both non-zero,
    284       in which case it has. */
    285 
    286    if (tst->os_state.valgrind_stack_base == 0)
    287       vg_assert(tst->os_state.valgrind_stack_init_SP == 0);
    288 
    289    if (tst->os_state.valgrind_stack_base != 0)
    290       vg_assert(tst->os_state.valgrind_stack_init_SP != 0);
    291 
    292    /* If no stack is present, allocate one. */
    293 
    294    if (tst->os_state.valgrind_stack_base == 0) {
    295       stack = VG_(am_alloc_VgStack)( &initial_SP );
    296       if (stack) {
    297          tst->os_state.valgrind_stack_base    = (Addr)stack;
    298          tst->os_state.valgrind_stack_init_SP = initial_SP;
    299       }
    300    }
    301 
    302    if (0)
    303       VG_(printf)( "stack for tid %d at %p; init_SP=%p\n",
    304                    tid,
    305                    (void*)tst->os_state.valgrind_stack_base,
    306                    (void*)tst->os_state.valgrind_stack_init_SP );
    307 
    308    return tst->os_state.valgrind_stack_init_SP;
    309 }
    310 
    311 /* Allocate a stack for the main thread, and run it all the way to the
    312    end.  Although we already have a working VgStack
    313    (VG_(interim_stack)) it's better to allocate a new one, so that
    314    overflow detection works uniformly for all threads.
    315 */
    316 void VG_(main_thread_wrapper_NORETURN)(ThreadId tid)
    317 {
    318    Addr sp;
    319    VG_(debugLog)(1, "syswrap-linux",
    320                     "entering VG_(main_thread_wrapper_NORETURN)\n");
    321 
    322    sp = ML_(allocstack)(tid);
    323 
    324 #if defined(VGP_ppc32_linux)
    325    /* make a stack frame */
    326    sp -= 16;
    327    sp &= ~0xF;
    328    *(UWord *)sp = 0;
    329 #elif defined(VGP_ppc64_linux)
    330    /* make a stack frame */
    331    sp -= 112;
    332    sp &= ~((Addr)0xF);
    333    *(UWord *)sp = 0;
    334 #elif defined(VGP_s390x_linux)
    335    /* make a stack frame */
    336    sp -= 160;
    337    sp &= ~((Addr)0xF);
    338    *(UWord *)sp = 0;
    339 #endif
    340 
    341    /* If we can't even allocate the first thread's stack, we're hosed.
    342       Give up. */
    343    vg_assert2(sp != 0, "Cannot allocate main thread's stack.");
    344 
    345    /* shouldn't be any other threads around yet */
    346    vg_assert( VG_(count_living_threads)() == 1 );
    347 
    348    ML_(call_on_new_stack_0_1)(
    349       (Addr)sp,               /* stack */
    350       0,                      /* bogus return address */
    351       run_a_thread_NORETURN,  /* fn to call */
    352       (Word)tid               /* arg to give it */
    353    );
    354 
    355    /*NOTREACHED*/
    356    vg_assert(0);
    357 }
    358 
    359 
    360 /* Do a clone which is really a fork() */
    361 SysRes ML_(do_fork_clone) ( ThreadId tid, UInt flags,
    362                             Int* parent_tidptr, Int* child_tidptr )
    363 {
    364    vki_sigset_t fork_saved_mask;
    365    vki_sigset_t mask;
    366    SysRes       res;
    367 
    368    if (flags & (VKI_CLONE_SETTLS | VKI_CLONE_FS | VKI_CLONE_VM
    369                 | VKI_CLONE_FILES | VKI_CLONE_VFORK))
    370       return VG_(mk_SysRes_Error)( VKI_EINVAL );
    371 
    372    /* Block all signals during fork, so that we can fix things up in
    373       the child without being interrupted. */
    374    VG_(sigfillset)(&mask);
    375    VG_(sigprocmask)(VKI_SIG_SETMASK, &mask, &fork_saved_mask);
    376 
    377    VG_(do_atfork_pre)(tid);
    378 
    379    /* Since this is the fork() form of clone, we don't need all that
    380       VG_(clone) stuff */
    381 #if defined(VGP_x86_linux) \
    382     || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) \
    383     || defined(VGP_arm_linux)
    384    res = VG_(do_syscall5)( __NR_clone, flags,
    385                            (UWord)NULL, (UWord)parent_tidptr,
    386                            (UWord)NULL, (UWord)child_tidptr );
    387 #elif defined(VGP_amd64_linux)
    388    /* note that the last two arguments are the opposite way round to x86 and
    389       ppc32 as the amd64 kernel expects the arguments in a different order */
    390    res = VG_(do_syscall5)( __NR_clone, flags,
    391                            (UWord)NULL, (UWord)parent_tidptr,
    392                            (UWord)child_tidptr, (UWord)NULL );
    393 #elif defined(VGP_s390x_linux)
    394    /* Note that s390 has the stack first and then the flags */
    395    res = VG_(do_syscall4)( __NR_clone, (UWord) NULL, flags,
    396                           (UWord)parent_tidptr, (UWord)child_tidptr);
    397 #else
    398 # error Unknown platform
    399 #endif
    400 
    401    if (!sr_isError(res) && sr_Res(res) == 0) {
    402       /* child */
    403       VG_(do_atfork_child)(tid);
    404 
    405       /* restore signal mask */
    406       VG_(sigprocmask)(VKI_SIG_SETMASK, &fork_saved_mask, NULL);
    407 
    408       /* If --child-silent-after-fork=yes was specified, set the
    409          output file descriptors to 'impossible' values.  This is
    410          noticed by send_bytes_to_logging_sink in m_libcprint.c, which
    411          duly stops writing any further output. */
    412       if (VG_(clo_child_silent_after_fork)) {
    413          if (!VG_(log_output_sink).is_socket)
    414             VG_(log_output_sink).fd = -1;
    415          if (!VG_(xml_output_sink).is_socket)
    416             VG_(xml_output_sink).fd = -1;
    417       }
    418    }
    419    else
    420    if (!sr_isError(res) && sr_Res(res) > 0) {
    421       /* parent */
    422       VG_(do_atfork_parent)(tid);
    423 
    424       if (VG_(clo_trace_syscalls))
    425 	  VG_(printf)("   clone(fork): process %d created child %ld\n",
    426                       VG_(getpid)(), sr_Res(res));
    427 
    428       /* restore signal mask */
    429       VG_(sigprocmask)(VKI_SIG_SETMASK, &fork_saved_mask, NULL);
    430    }
    431 
    432    return res;
    433 }
    434 
    435 
    436 /* ---------------------------------------------------------------------
    437    PRE/POST wrappers for arch-generic, Linux-specific syscalls
    438    ------------------------------------------------------------------ */
    439 
    440 // Nb: See the comment above the generic PRE/POST wrappers in
    441 // m_syswrap/syswrap-generic.c for notes about how they work.
    442 
    443 #define PRE(name)       DEFN_PRE_TEMPLATE(linux, name)
    444 #define POST(name)      DEFN_POST_TEMPLATE(linux, name)
    445 
    446 // Macros to support 64-bit syscall args split into two 32 bit values
    447 #define LOHI64(lo,hi)   ( ((ULong)(lo)) | (((ULong)(hi)) << 32) )
    448 #if defined(VG_LITTLEENDIAN)
    449 #define MERGE64(lo,hi)   ( ((ULong)(lo)) | (((ULong)(hi)) << 32) )
    450 #define MERGE64_FIRST(name) name##_low
    451 #define MERGE64_SECOND(name) name##_high
    452 #elif defined(VG_BIGENDIAN)
    453 #define MERGE64(hi,lo)   ( ((ULong)(lo)) | (((ULong)(hi)) << 32) )
    454 #define MERGE64_FIRST(name) name##_high
    455 #define MERGE64_SECOND(name) name##_low
    456 #else
    457 #error Unknown endianness
    458 #endif
    459 
    460 /* ---------------------------------------------------------------------
    461    *mount wrappers
    462    ------------------------------------------------------------------ */
    463 
    464 PRE(sys_mount)
    465 {
    466    // Nb: depending on 'flags', the 'type' and 'data' args may be ignored.
    467    // We are conservative and check everything, except the memory pointed to
    468    // by 'data'.
    469    *flags |= SfMayBlock;
    470    PRINT("sys_mount( %#lx(%s), %#lx(%s), %#lx(%s), %#lx, %#lx )",
    471          ARG1,(Char*)ARG1, ARG2,(Char*)ARG2, ARG3,(Char*)ARG3, ARG4, ARG5);
    472    PRE_REG_READ5(long, "mount",
    473                  char *, source, char *, target, char *, type,
    474                  unsigned long, flags, void *, data);
    475    if (ARG1)
    476       PRE_MEM_RASCIIZ( "mount(source)", ARG1);
    477    PRE_MEM_RASCIIZ( "mount(target)", ARG2);
    478    PRE_MEM_RASCIIZ( "mount(type)", ARG3);
    479 }
    480 
    481 PRE(sys_oldumount)
    482 {
    483    PRINT("sys_oldumount( %#lx )", ARG1);
    484    PRE_REG_READ1(long, "umount", char *, path);
    485    PRE_MEM_RASCIIZ( "umount(path)", ARG1);
    486 }
    487 
    488 PRE(sys_umount)
    489 {
    490    PRINT("sys_umount( %#lx, %ld )", ARG1, ARG2);
    491    PRE_REG_READ2(long, "umount2", char *, path, int, flags);
    492    PRE_MEM_RASCIIZ( "umount2(path)", ARG1);
    493 }
    494 
    495 /* ---------------------------------------------------------------------
    496    16- and 32-bit uid/gid wrappers
    497    ------------------------------------------------------------------ */
    498 
    499 PRE(sys_setfsuid16)
    500 {
    501    PRINT("sys_setfsuid16 ( %ld )", ARG1);
    502    PRE_REG_READ1(long, "setfsuid16", vki_old_uid_t, uid);
    503 }
    504 
    505 PRE(sys_setfsuid)
    506 {
    507    PRINT("sys_setfsuid ( %ld )", ARG1);
    508    PRE_REG_READ1(long, "setfsuid", vki_uid_t, uid);
    509 }
    510 
    511 PRE(sys_setfsgid16)
    512 {
    513    PRINT("sys_setfsgid16 ( %ld )", ARG1);
    514    PRE_REG_READ1(long, "setfsgid16", vki_old_gid_t, gid);
    515 }
    516 
    517 PRE(sys_setfsgid)
    518 {
    519    PRINT("sys_setfsgid ( %ld )", ARG1);
    520    PRE_REG_READ1(long, "setfsgid", vki_gid_t, gid);
    521 }
    522 
    523 PRE(sys_setresuid16)
    524 {
    525    PRINT("sys_setresuid16 ( %ld, %ld, %ld )", ARG1, ARG2, ARG3);
    526    PRE_REG_READ3(long, "setresuid16",
    527                  vki_old_uid_t, ruid, vki_old_uid_t, euid, vki_old_uid_t, suid);
    528 }
    529 
    530 PRE(sys_setresuid)
    531 {
    532    PRINT("sys_setresuid ( %ld, %ld, %ld )", ARG1, ARG2, ARG3);
    533    PRE_REG_READ3(long, "setresuid",
    534                  vki_uid_t, ruid, vki_uid_t, euid, vki_uid_t, suid);
    535 }
    536 
    537 PRE(sys_getresuid16)
    538 {
    539    PRINT("sys_getresuid16 ( %#lx, %#lx, %#lx )", ARG1,ARG2,ARG3);
    540    PRE_REG_READ3(long, "getresuid16",
    541                  vki_old_uid_t *, ruid, vki_old_uid_t *, euid,
    542                  vki_old_uid_t *, suid);
    543    PRE_MEM_WRITE( "getresuid16(ruid)", ARG1, sizeof(vki_old_uid_t) );
    544    PRE_MEM_WRITE( "getresuid16(euid)", ARG2, sizeof(vki_old_uid_t) );
    545    PRE_MEM_WRITE( "getresuid16(suid)", ARG3, sizeof(vki_old_uid_t) );
    546 }
    547 POST(sys_getresuid16)
    548 {
    549    vg_assert(SUCCESS);
    550    if (RES == 0) {
    551       POST_MEM_WRITE( ARG1, sizeof(vki_old_uid_t) );
    552       POST_MEM_WRITE( ARG2, sizeof(vki_old_uid_t) );
    553       POST_MEM_WRITE( ARG3, sizeof(vki_old_uid_t) );
    554    }
    555 }
    556 
    557 PRE(sys_getresuid)
    558 {
    559    PRINT("sys_getresuid ( %#lx, %#lx, %#lx )", ARG1,ARG2,ARG3);
    560    PRE_REG_READ3(long, "getresuid",
    561                  vki_uid_t *, ruid, vki_uid_t *, euid, vki_uid_t *, suid);
    562    PRE_MEM_WRITE( "getresuid(ruid)", ARG1, sizeof(vki_uid_t) );
    563    PRE_MEM_WRITE( "getresuid(euid)", ARG2, sizeof(vki_uid_t) );
    564    PRE_MEM_WRITE( "getresuid(suid)", ARG3, sizeof(vki_uid_t) );
    565 }
    566 POST(sys_getresuid)
    567 {
    568    vg_assert(SUCCESS);
    569    if (RES == 0) {
    570       POST_MEM_WRITE( ARG1, sizeof(vki_uid_t) );
    571       POST_MEM_WRITE( ARG2, sizeof(vki_uid_t) );
    572       POST_MEM_WRITE( ARG3, sizeof(vki_uid_t) );
    573    }
    574 }
    575 
    576 PRE(sys_setresgid16)
    577 {
    578    PRINT("sys_setresgid16 ( %ld, %ld, %ld )", ARG1, ARG2, ARG3);
    579    PRE_REG_READ3(long, "setresgid16",
    580                  vki_old_gid_t, rgid,
    581                  vki_old_gid_t, egid, vki_old_gid_t, sgid);
    582 }
    583 
    584 PRE(sys_setresgid)
    585 {
    586    PRINT("sys_setresgid ( %ld, %ld, %ld )", ARG1, ARG2, ARG3);
    587    PRE_REG_READ3(long, "setresgid",
    588                  vki_gid_t, rgid, vki_gid_t, egid, vki_gid_t, sgid);
    589 }
    590 
    591 PRE(sys_getresgid16)
    592 {
    593    PRINT("sys_getresgid16 ( %#lx, %#lx, %#lx )", ARG1,ARG2,ARG3);
    594    PRE_REG_READ3(long, "getresgid16",
    595                  vki_old_gid_t *, rgid, vki_old_gid_t *, egid,
    596                  vki_old_gid_t *, sgid);
    597    PRE_MEM_WRITE( "getresgid16(rgid)", ARG1, sizeof(vki_old_gid_t) );
    598    PRE_MEM_WRITE( "getresgid16(egid)", ARG2, sizeof(vki_old_gid_t) );
    599    PRE_MEM_WRITE( "getresgid16(sgid)", ARG3, sizeof(vki_old_gid_t) );
    600 }
    601 POST(sys_getresgid16)
    602 {
    603    vg_assert(SUCCESS);
    604    if (RES == 0) {
    605       POST_MEM_WRITE( ARG1, sizeof(vki_old_gid_t) );
    606       POST_MEM_WRITE( ARG2, sizeof(vki_old_gid_t) );
    607       POST_MEM_WRITE( ARG3, sizeof(vki_old_gid_t) );
    608    }
    609 }
    610 
    611 PRE(sys_getresgid)
    612 {
    613    PRINT("sys_getresgid ( %#lx, %#lx, %#lx )", ARG1,ARG2,ARG3);
    614    PRE_REG_READ3(long, "getresgid",
    615                  vki_gid_t *, rgid, vki_gid_t *, egid, vki_gid_t *, sgid);
    616    PRE_MEM_WRITE( "getresgid(rgid)", ARG1, sizeof(vki_gid_t) );
    617    PRE_MEM_WRITE( "getresgid(egid)", ARG2, sizeof(vki_gid_t) );
    618    PRE_MEM_WRITE( "getresgid(sgid)", ARG3, sizeof(vki_gid_t) );
    619 }
    620 POST(sys_getresgid)
    621 {
    622    vg_assert(SUCCESS);
    623    if (RES == 0) {
    624       POST_MEM_WRITE( ARG1, sizeof(vki_gid_t) );
    625       POST_MEM_WRITE( ARG2, sizeof(vki_gid_t) );
    626       POST_MEM_WRITE( ARG3, sizeof(vki_gid_t) );
    627    }
    628 }
    629 
    630 /* ---------------------------------------------------------------------
    631    miscellaneous wrappers
    632    ------------------------------------------------------------------ */
    633 
    634 PRE(sys_exit_group)
    635 {
    636    ThreadId     t;
    637    ThreadState* tst;
    638 
    639    PRINT("exit_group( %ld )", ARG1);
    640    PRE_REG_READ1(void, "exit_group", int, status);
    641 
    642    tst = VG_(get_ThreadState)(tid);
    643 
    644    /* A little complex; find all the threads with the same threadgroup
    645       as this one (including this one), and mark them to exit */
    646    for (t = 1; t < VG_N_THREADS; t++) {
    647       if ( /* not alive */
    648            VG_(threads)[t].status == VgTs_Empty
    649            ||
    650 	   /* not our group */
    651            VG_(threads)[t].os_state.threadgroup != tst->os_state.threadgroup
    652          )
    653          continue;
    654 
    655       VG_(threads)[t].exitreason = VgSrc_ExitThread;
    656       VG_(threads)[t].os_state.exitcode = ARG1;
    657 
    658       if (t != tid)
    659 	 VG_(get_thread_out_of_syscall)(t); /* unblock it, if blocked */
    660    }
    661 
    662    /* We have to claim the syscall already succeeded. */
    663    SET_STATUS_Success(0);
    664 }
    665 
    666 PRE(sys_llseek)
    667 {
    668    PRINT("sys_llseek ( %ld, 0x%lx, 0x%lx, %#lx, %ld )", ARG1,ARG2,ARG3,ARG4,ARG5);
    669    PRE_REG_READ5(long, "llseek",
    670                  unsigned int, fd, unsigned long, offset_high,
    671                  unsigned long, offset_low, vki_loff_t *, result,
    672                  unsigned int, whence);
    673    if (!ML_(fd_allowed)(ARG1, "llseek", tid, False))
    674       SET_STATUS_Failure( VKI_EBADF );
    675    else
    676       PRE_MEM_WRITE( "llseek(result)", ARG4, sizeof(vki_loff_t));
    677 }
    678 POST(sys_llseek)
    679 {
    680    vg_assert(SUCCESS);
    681    if (RES == 0)
    682       POST_MEM_WRITE( ARG4, sizeof(vki_loff_t) );
    683 }
    684 
    685 PRE(sys_adjtimex)
    686 {
    687    struct vki_timex *tx = (struct vki_timex *)ARG1;
    688    PRINT("sys_adjtimex ( %#lx )", ARG1);
    689    PRE_REG_READ1(long, "adjtimex", struct timex *, buf);
    690    PRE_MEM_READ( "adjtimex(timex->modes)", ARG1, sizeof(tx->modes));
    691 
    692 #define ADJX(bits,field) 				\
    693    if (tx->modes & (bits))                              \
    694       PRE_MEM_READ( "adjtimex(timex->"#field")",	\
    695 		    (Addr)&tx->field, sizeof(tx->field))
    696 
    697    if (tx->modes & VKI_ADJ_ADJTIME) {
    698       if (!(tx->modes & VKI_ADJ_OFFSET_READONLY))
    699          PRE_MEM_READ( "adjtimex(timex->offset)", (Addr)&tx->offset, sizeof(tx->offset));
    700    } else {
    701       ADJX(VKI_ADJ_OFFSET, offset);
    702       ADJX(VKI_ADJ_FREQUENCY, freq);
    703       ADJX(VKI_ADJ_MAXERROR, maxerror);
    704       ADJX(VKI_ADJ_ESTERROR, esterror);
    705       ADJX(VKI_ADJ_STATUS, status);
    706       ADJX(VKI_ADJ_TIMECONST|VKI_ADJ_TAI, constant);
    707       ADJX(VKI_ADJ_TICK, tick);
    708    }
    709 #undef ADJX
    710 
    711    PRE_MEM_WRITE( "adjtimex(timex)", ARG1, sizeof(struct vki_timex));
    712 }
    713 
    714 POST(sys_adjtimex)
    715 {
    716    POST_MEM_WRITE( ARG1, sizeof(struct vki_timex) );
    717 }
    718 
    719 PRE(sys_ioperm)
    720 {
    721    PRINT("sys_ioperm ( %ld, %ld, %ld )", ARG1, ARG2, ARG3 );
    722    PRE_REG_READ3(long, "ioperm",
    723                  unsigned long, from, unsigned long, num, int, turn_on);
    724 }
    725 
    726 PRE(sys_syslog)
    727 {
    728    *flags |= SfMayBlock;
    729    PRINT("sys_syslog (%ld, %#lx, %ld)", ARG1,ARG2,ARG3);
    730    PRE_REG_READ3(long, "syslog", int, type, char *, bufp, int, len);
    731    switch (ARG1) {
    732    // The kernel uses magic numbers here, rather than named constants,
    733    // therefore so do we.
    734    case 2: case 3: case 4:
    735       PRE_MEM_WRITE( "syslog(bufp)", ARG2, ARG3);
    736       break;
    737    default:
    738       break;
    739    }
    740 }
    741 POST(sys_syslog)
    742 {
    743    switch (ARG1) {
    744    case 2: case 3: case 4:
    745       POST_MEM_WRITE( ARG2, ARG3 );
    746       break;
    747    default:
    748       break;
    749    }
    750 }
    751 
    752 PRE(sys_vhangup)
    753 {
    754    PRINT("sys_vhangup ( )");
    755    PRE_REG_READ0(long, "vhangup");
    756 }
    757 
    758 PRE(sys_sysinfo)
    759 {
    760    PRINT("sys_sysinfo ( %#lx )",ARG1);
    761    PRE_REG_READ1(long, "sysinfo", struct sysinfo *, info);
    762    PRE_MEM_WRITE( "sysinfo(info)", ARG1, sizeof(struct vki_sysinfo) );
    763 }
    764 POST(sys_sysinfo)
    765 {
    766    POST_MEM_WRITE( ARG1, sizeof(struct vki_sysinfo) );
    767 }
    768 
    769 PRE(sys_personality)
    770 {
    771    PRINT("sys_personality ( %llu )", (ULong)ARG1);
    772    PRE_REG_READ1(long, "personality", vki_u_long, persona);
    773 }
    774 
    775 PRE(sys_sysctl)
    776 {
    777    struct __vki_sysctl_args *args;
    778    PRINT("sys_sysctl ( %#lx )", ARG1 );
    779    args = (struct __vki_sysctl_args *)ARG1;
    780    PRE_REG_READ1(long, "sysctl", struct __sysctl_args *, args);
    781    PRE_MEM_WRITE( "sysctl(args)", ARG1, sizeof(struct __vki_sysctl_args) );
    782    if (!VG_(am_is_valid_for_client)(ARG1, sizeof(struct __vki_sysctl_args),
    783                                           VKI_PROT_READ)) {
    784       SET_STATUS_Failure( VKI_EFAULT );
    785       return;
    786    }
    787 
    788    PRE_MEM_READ("sysctl(name)", (Addr)args->name, args->nlen * sizeof(*args->name));
    789    if (args->newval != NULL)
    790       PRE_MEM_READ("sysctl(newval)", (Addr)args->newval, args->newlen);
    791    if (args->oldlenp != NULL) {
    792       PRE_MEM_READ("sysctl(oldlenp)", (Addr)args->oldlenp, sizeof(*args->oldlenp));
    793       PRE_MEM_WRITE("sysctl(oldval)", (Addr)args->oldval, *args->oldlenp);
    794    }
    795 }
    796 POST(sys_sysctl)
    797 {
    798    struct __vki_sysctl_args *args;
    799    args = (struct __vki_sysctl_args *)ARG1;
    800    if (args->oldlenp != NULL) {
    801       POST_MEM_WRITE((Addr)args->oldlenp, sizeof(*args->oldlenp));
    802       POST_MEM_WRITE((Addr)args->oldval, 1 + *args->oldlenp);
    803    }
    804 }
    805 
    806 PRE(sys_prctl)
    807 {
    808    *flags |= SfMayBlock;
    809    PRINT( "sys_prctl ( %ld, %ld, %ld, %ld, %ld )", ARG1, ARG2, ARG3, ARG4, ARG5 );
    810    switch (ARG1) {
    811    case VKI_PR_SET_PDEATHSIG:
    812       PRE_REG_READ2(int, "prctl", int, option, int, signal);
    813       break;
    814    case VKI_PR_GET_PDEATHSIG:
    815       PRE_REG_READ2(int, "prctl", int, option, int *, signal);
    816       PRE_MEM_WRITE("prctl(get-death-signal)", ARG2, sizeof(Int));
    817       break;
    818    case VKI_PR_GET_DUMPABLE:
    819       PRE_REG_READ1(int, "prctl", int, option);
    820       break;
    821    case VKI_PR_SET_DUMPABLE:
    822       PRE_REG_READ2(int, "prctl", int, option, int, dump);
    823       break;
    824    case VKI_PR_GET_UNALIGN:
    825       PRE_REG_READ2(int, "prctl", int, option, int *, value);
    826       PRE_MEM_WRITE("prctl(get-unalign)", ARG2, sizeof(Int));
    827       break;
    828    case VKI_PR_SET_UNALIGN:
    829       PRE_REG_READ2(int, "prctl", int, option, int, value);
    830       break;
    831    case VKI_PR_GET_KEEPCAPS:
    832       PRE_REG_READ1(int, "prctl", int, option);
    833       break;
    834    case VKI_PR_SET_KEEPCAPS:
    835       PRE_REG_READ2(int, "prctl", int, option, int, keepcaps);
    836       break;
    837    case VKI_PR_GET_FPEMU:
    838       PRE_REG_READ2(int, "prctl", int, option, int *, value);
    839       PRE_MEM_WRITE("prctl(get-fpemu)", ARG2, sizeof(Int));
    840       break;
    841    case VKI_PR_SET_FPEMU:
    842       PRE_REG_READ2(int, "prctl", int, option, int, value);
    843       break;
    844    case VKI_PR_GET_FPEXC:
    845       PRE_REG_READ2(int, "prctl", int, option, int *, value);
    846       PRE_MEM_WRITE("prctl(get-fpexc)", ARG2, sizeof(Int));
    847       break;
    848    case VKI_PR_SET_FPEXC:
    849       PRE_REG_READ2(int, "prctl", int, option, int, value);
    850       break;
    851    case VKI_PR_GET_TIMING:
    852       PRE_REG_READ1(int, "prctl", int, option);
    853       break;
    854    case VKI_PR_SET_TIMING:
    855       PRE_REG_READ2(int, "prctl", int, option, int, timing);
    856       break;
    857    case VKI_PR_SET_NAME:
    858       PRE_REG_READ2(int, "prctl", int, option, char *, name);
    859       PRE_MEM_RASCIIZ("prctl(set-name)", ARG2);
    860       break;
    861    case VKI_PR_GET_NAME:
    862       PRE_REG_READ2(int, "prctl", int, option, char *, name);
    863       PRE_MEM_WRITE("prctl(get-name)", ARG2, VKI_TASK_COMM_LEN);
    864       break;
    865    case VKI_PR_GET_ENDIAN:
    866       PRE_REG_READ2(int, "prctl", int, option, int *, value);
    867       PRE_MEM_WRITE("prctl(get-endian)", ARG2, sizeof(Int));
    868       break;
    869    case VKI_PR_SET_ENDIAN:
    870       PRE_REG_READ2(int, "prctl", int, option, int, value);
    871       break;
    872    default:
    873       PRE_REG_READ5(long, "prctl",
    874                     int, option, unsigned long, arg2, unsigned long, arg3,
    875                     unsigned long, arg4, unsigned long, arg5);
    876       break;
    877    }
    878 }
    879 POST(sys_prctl)
    880 {
    881    switch (ARG1) {
    882    case VKI_PR_GET_PDEATHSIG:
    883       POST_MEM_WRITE(ARG2, sizeof(Int));
    884       break;
    885    case VKI_PR_GET_UNALIGN:
    886       POST_MEM_WRITE(ARG2, sizeof(Int));
    887       break;
    888    case VKI_PR_GET_FPEMU:
    889       POST_MEM_WRITE(ARG2, sizeof(Int));
    890       break;
    891    case VKI_PR_GET_FPEXC:
    892       POST_MEM_WRITE(ARG2, sizeof(Int));
    893       break;
    894    case VKI_PR_GET_NAME:
    895       POST_MEM_WRITE(ARG2, VKI_TASK_COMM_LEN);
    896       break;
    897    case VKI_PR_GET_ENDIAN:
    898       POST_MEM_WRITE(ARG2, sizeof(Int));
    899       break;
    900    }
    901 }
    902 
    903 PRE(sys_sendfile)
    904 {
    905    *flags |= SfMayBlock;
    906    PRINT("sys_sendfile ( %ld, %ld, %#lx, %lu )", ARG1,ARG2,ARG3,ARG4);
    907    PRE_REG_READ4(ssize_t, "sendfile",
    908                  int, out_fd, int, in_fd, vki_off_t *, offset,
    909                  vki_size_t, count);
    910    if (ARG3 != 0)
    911       PRE_MEM_WRITE( "sendfile(offset)", ARG3, sizeof(vki_off_t) );
    912 }
    913 POST(sys_sendfile)
    914 {
    915    if (ARG3 != 0 ) {
    916       POST_MEM_WRITE( ARG3, sizeof( vki_off_t ) );
    917    }
    918 }
    919 
    920 PRE(sys_sendfile64)
    921 {
    922    *flags |= SfMayBlock;
    923    PRINT("sendfile64 ( %ld, %ld, %#lx, %lu )",ARG1,ARG2,ARG3,ARG4);
    924    PRE_REG_READ4(ssize_t, "sendfile64",
    925                  int, out_fd, int, in_fd, vki_loff_t *, offset,
    926                  vki_size_t, count);
    927    if (ARG3 != 0)
    928       PRE_MEM_WRITE( "sendfile64(offset)", ARG3, sizeof(vki_loff_t) );
    929 }
    930 POST(sys_sendfile64)
    931 {
    932    if (ARG3 != 0 ) {
    933       POST_MEM_WRITE( ARG3, sizeof(vki_loff_t) );
    934    }
    935 }
    936 
    937 PRE(sys_futex)
    938 {
    939    /*
    940       arg    param                              used by ops
    941 
    942       ARG1 - u32 *futex				all
    943       ARG2 - int op
    944       ARG3 - int val				WAIT,WAKE,FD,REQUEUE,CMP_REQUEUE
    945       ARG4 - struct timespec *utime		WAIT:time*	REQUEUE,CMP_REQUEUE:val2
    946       ARG5 - u32 *uaddr2			REQUEUE,CMP_REQUEUE
    947       ARG6 - int val3				CMP_REQUEUE
    948     */
    949    PRINT("sys_futex ( %#lx, %ld, %ld, %#lx, %#lx )", ARG1,ARG2,ARG3,ARG4,ARG5);
    950    switch(ARG2 & ~(VKI_FUTEX_PRIVATE_FLAG|VKI_FUTEX_CLOCK_REALTIME)) {
    951    case VKI_FUTEX_CMP_REQUEUE:
    952    case VKI_FUTEX_WAKE_OP:
    953    case VKI_FUTEX_CMP_REQUEUE_PI:
    954       PRE_REG_READ6(long, "futex",
    955                     vki_u32 *, futex, int, op, int, val,
    956                     struct timespec *, utime, vki_u32 *, uaddr2, int, val3);
    957       break;
    958    case VKI_FUTEX_REQUEUE:
    959    case VKI_FUTEX_WAIT_REQUEUE_PI:
    960       PRE_REG_READ5(long, "futex",
    961                     vki_u32 *, futex, int, op, int, val,
    962                     struct timespec *, utime, vki_u32 *, uaddr2);
    963       break;
    964    case VKI_FUTEX_WAIT_BITSET:
    965       PRE_REG_READ6(long, "futex",
    966                     vki_u32 *, futex, int, op, int, val,
    967                     struct timespec *, utime, int, dummy, int, val3);
    968       break;
    969    case VKI_FUTEX_WAKE_BITSET:
    970       PRE_REG_READ6(long, "futex",
    971                     vki_u32 *, futex, int, op, int, val,
    972                     int, dummy, int, dummy2, int, val3);
    973       break;
    974    case VKI_FUTEX_WAIT:
    975    case VKI_FUTEX_LOCK_PI:
    976       PRE_REG_READ4(long, "futex",
    977                     vki_u32 *, futex, int, op, int, val,
    978                     struct timespec *, utime);
    979       break;
    980    case VKI_FUTEX_WAKE:
    981    case VKI_FUTEX_FD:
    982    case VKI_FUTEX_TRYLOCK_PI:
    983       PRE_REG_READ3(long, "futex",
    984                     vki_u32 *, futex, int, op, int, val);
    985       break;
    986    case VKI_FUTEX_UNLOCK_PI:
    987    default:
    988       PRE_REG_READ2(long, "futex", vki_u32 *, futex, int, op);
    989       break;
    990    }
    991 
    992    *flags |= SfMayBlock;
    993 
    994    switch(ARG2 & ~(VKI_FUTEX_PRIVATE_FLAG|VKI_FUTEX_CLOCK_REALTIME)) {
    995    case VKI_FUTEX_WAIT:
    996    case VKI_FUTEX_LOCK_PI:
    997    case VKI_FUTEX_WAIT_BITSET:
    998    case VKI_FUTEX_WAIT_REQUEUE_PI:
    999       PRE_MEM_READ( "futex(futex)", ARG1, sizeof(Int) );
   1000       if (ARG4 != 0)
   1001 	 PRE_MEM_READ( "futex(timeout)", ARG4, sizeof(struct vki_timespec) );
   1002       break;
   1003 
   1004    case VKI_FUTEX_REQUEUE:
   1005    case VKI_FUTEX_CMP_REQUEUE:
   1006    case VKI_FUTEX_CMP_REQUEUE_PI:
   1007    case VKI_FUTEX_WAKE_OP:
   1008       PRE_MEM_READ( "futex(futex)", ARG1, sizeof(Int) );
   1009       PRE_MEM_READ( "futex(futex2)", ARG5, sizeof(Int) );
   1010       break;
   1011 
   1012    case VKI_FUTEX_FD:
   1013    case VKI_FUTEX_TRYLOCK_PI:
   1014    case VKI_FUTEX_UNLOCK_PI:
   1015       PRE_MEM_READ( "futex(futex)", ARG1, sizeof(Int) );
   1016      break;
   1017 
   1018    case VKI_FUTEX_WAKE:
   1019    case VKI_FUTEX_WAKE_BITSET:
   1020       /* no additional pointers */
   1021       break;
   1022 
   1023    default:
   1024       SET_STATUS_Failure( VKI_ENOSYS );   // some futex function we don't understand
   1025       break;
   1026    }
   1027 }
   1028 POST(sys_futex)
   1029 {
   1030    vg_assert(SUCCESS);
   1031    POST_MEM_WRITE( ARG1, sizeof(int) );
   1032    if (ARG2 == VKI_FUTEX_FD) {
   1033       if (!ML_(fd_allowed)(RES, "futex", tid, True)) {
   1034          VG_(close)(RES);
   1035          SET_STATUS_Failure( VKI_EMFILE );
   1036       } else {
   1037          if (VG_(clo_track_fds))
   1038             ML_(record_fd_open_nameless)(tid, RES);
   1039       }
   1040    }
   1041 }
   1042 
   1043 PRE(sys_set_robust_list)
   1044 {
   1045    PRINT("sys_set_robust_list ( %#lx, %ld )", ARG1,ARG2);
   1046    PRE_REG_READ2(long, "set_robust_list",
   1047                  struct vki_robust_list_head *, head, vki_size_t, len);
   1048 
   1049    /* Just check the robust_list_head structure is readable - don't
   1050       try and chase the list as the kernel will only read it when
   1051       the thread exits so the current contents is irrelevant. */
   1052    if (ARG1 != 0)
   1053       PRE_MEM_READ("set_robust_list(head)", ARG1, ARG2);
   1054 }
   1055 
   1056 PRE(sys_get_robust_list)
   1057 {
   1058    PRINT("sys_get_robust_list ( %ld, %#lx, %ld )", ARG1,ARG2,ARG3);
   1059    PRE_REG_READ3(long, "get_robust_list",
   1060                  int, pid,
   1061                  struct vki_robust_list_head **, head_ptr,
   1062                  vki_size_t *, len_ptr);
   1063    PRE_MEM_WRITE("get_robust_list(head_ptr)",
   1064                  ARG2, sizeof(struct vki_robust_list_head *));
   1065    PRE_MEM_WRITE("get_robust_list(len_ptr)",
   1066                  ARG3, sizeof(struct vki_size_t *));
   1067 }
   1068 POST(sys_get_robust_list)
   1069 {
   1070    POST_MEM_WRITE(ARG2, sizeof(struct vki_robust_list_head *));
   1071    POST_MEM_WRITE(ARG3, sizeof(struct vki_size_t *));
   1072 }
   1073 
   1074 PRE(sys_pselect6)
   1075 {
   1076    *flags |= SfMayBlock;
   1077    PRINT("sys_pselect6 ( %ld, %#lx, %#lx, %#lx, %#lx, %#lx )", ARG1,ARG2,ARG3,ARG4,ARG5,ARG6);
   1078    PRE_REG_READ6(long, "pselect6",
   1079                  int, n, vki_fd_set *, readfds, vki_fd_set *, writefds,
   1080                  vki_fd_set *, exceptfds, struct vki_timeval *, timeout,
   1081                  void *, sig);
   1082    // XXX: this possibly understates how much memory is read.
   1083    if (ARG2 != 0)
   1084       PRE_MEM_READ( "pselect6(readfds)",
   1085 		     ARG2, ARG1/8 /* __FD_SETSIZE/8 */ );
   1086    if (ARG3 != 0)
   1087       PRE_MEM_READ( "pselect6(writefds)",
   1088 		     ARG3, ARG1/8 /* __FD_SETSIZE/8 */ );
   1089    if (ARG4 != 0)
   1090       PRE_MEM_READ( "pselect6(exceptfds)",
   1091 		     ARG4, ARG1/8 /* __FD_SETSIZE/8 */ );
   1092    if (ARG5 != 0)
   1093       PRE_MEM_READ( "pselect6(timeout)", ARG5, sizeof(struct vki_timeval) );
   1094    if (ARG6 != 0)
   1095       PRE_MEM_READ( "pselect6(sig)", ARG6, sizeof(void *)+sizeof(vki_size_t) );
   1096 }
   1097 
   1098 PRE(sys_ppoll)
   1099 {
   1100    UInt i;
   1101    struct vki_pollfd* ufds = (struct vki_pollfd *)ARG1;
   1102    *flags |= SfMayBlock;
   1103    PRINT("sys_ppoll ( %#lx, %ld, %#lx, %#lx, %llu )\n", ARG1,ARG2,ARG3,ARG4,(ULong)ARG5);
   1104    PRE_REG_READ5(long, "ppoll",
   1105                  struct vki_pollfd *, ufds, unsigned int, nfds,
   1106                  struct vki_timespec *, tsp, vki_sigset_t *, sigmask,
   1107                  vki_size_t, sigsetsize);
   1108 
   1109    for (i = 0; i < ARG2; i++) {
   1110       PRE_MEM_READ( "ppoll(ufds.fd)",
   1111                     (Addr)(&ufds[i].fd), sizeof(ufds[i].fd) );
   1112       PRE_MEM_READ( "ppoll(ufds.events)",
   1113                     (Addr)(&ufds[i].events), sizeof(ufds[i].events) );
   1114       PRE_MEM_WRITE( "ppoll(ufd.reventss)",
   1115                      (Addr)(&ufds[i].revents), sizeof(ufds[i].revents) );
   1116    }
   1117 
   1118    if (ARG3)
   1119       PRE_MEM_READ( "ppoll(tsp)", ARG3, sizeof(struct vki_timespec) );
   1120    if (ARG4)
   1121       PRE_MEM_READ( "ppoll(sigmask)", ARG4, sizeof(vki_sigset_t) );
   1122 }
   1123 
   1124 POST(sys_ppoll)
   1125 {
   1126    if (RES > 0) {
   1127       UInt i;
   1128       struct vki_pollfd* ufds = (struct vki_pollfd *)ARG1;
   1129       for (i = 0; i < ARG2; i++)
   1130 	 POST_MEM_WRITE( (Addr)(&ufds[i].revents), sizeof(ufds[i].revents) );
   1131    }
   1132 }
   1133 
   1134 
   1135 /* ---------------------------------------------------------------------
   1136    epoll_* wrappers
   1137    ------------------------------------------------------------------ */
   1138 
   1139 PRE(sys_epoll_create)
   1140 {
   1141    PRINT("sys_epoll_create ( %ld )", ARG1);
   1142    PRE_REG_READ1(long, "epoll_create", int, size);
   1143 }
   1144 POST(sys_epoll_create)
   1145 {
   1146    vg_assert(SUCCESS);
   1147    if (!ML_(fd_allowed)(RES, "epoll_create", tid, True)) {
   1148       VG_(close)(RES);
   1149       SET_STATUS_Failure( VKI_EMFILE );
   1150    } else {
   1151       if (VG_(clo_track_fds))
   1152          ML_(record_fd_open_nameless) (tid, RES);
   1153    }
   1154 }
   1155 
   1156 PRE(sys_epoll_create1)
   1157 {
   1158    PRINT("sys_epoll_create1 ( %ld )", ARG1);
   1159    PRE_REG_READ1(long, "epoll_create1", int, flags);
   1160 }
   1161 POST(sys_epoll_create1)
   1162 {
   1163    vg_assert(SUCCESS);
   1164    if (!ML_(fd_allowed)(RES, "epoll_create1", tid, True)) {
   1165       VG_(close)(RES);
   1166       SET_STATUS_Failure( VKI_EMFILE );
   1167    } else {
   1168       if (VG_(clo_track_fds))
   1169          ML_(record_fd_open_nameless) (tid, RES);
   1170    }
   1171 }
   1172 
   1173 PRE(sys_epoll_ctl)
   1174 {
   1175    static const HChar* epoll_ctl_s[3] = {
   1176       "EPOLL_CTL_ADD",
   1177       "EPOLL_CTL_DEL",
   1178       "EPOLL_CTL_MOD"
   1179    };
   1180    PRINT("sys_epoll_ctl ( %ld, %s, %ld, %#lx )",
   1181          ARG1, ( ARG2<3 ? epoll_ctl_s[ARG2] : "?" ), ARG3, ARG4);
   1182    PRE_REG_READ4(long, "epoll_ctl",
   1183                  int, epfd, int, op, int, fd, struct vki_epoll_event *, event);
   1184    if (ARG2 != VKI_EPOLL_CTL_DEL)
   1185       PRE_MEM_READ( "epoll_ctl(event)", ARG4, sizeof(struct vki_epoll_event) );
   1186 }
   1187 
   1188 PRE(sys_epoll_wait)
   1189 {
   1190    *flags |= SfMayBlock;
   1191    PRINT("sys_epoll_wait ( %ld, %#lx, %ld, %ld )", ARG1, ARG2, ARG3, ARG4);
   1192    PRE_REG_READ4(long, "epoll_wait",
   1193                  int, epfd, struct vki_epoll_event *, events,
   1194                  int, maxevents, int, timeout);
   1195    PRE_MEM_WRITE( "epoll_wait(events)", ARG2, sizeof(struct vki_epoll_event)*ARG3);
   1196 }
   1197 POST(sys_epoll_wait)
   1198 {
   1199    vg_assert(SUCCESS);
   1200    if (RES > 0)
   1201       POST_MEM_WRITE( ARG2, sizeof(struct vki_epoll_event)*RES ) ;
   1202 }
   1203 
   1204 PRE(sys_epoll_pwait)
   1205 {
   1206    *flags |= SfMayBlock;
   1207    PRINT("sys_epoll_pwait ( %ld, %#lx, %ld, %ld, %#lx, %llu )", ARG1,ARG2,ARG3,ARG4,ARG5,(ULong)ARG6);
   1208    PRE_REG_READ6(long, "epoll_pwait",
   1209                  int, epfd, struct vki_epoll_event *, events,
   1210                  int, maxevents, int, timeout, vki_sigset_t *, sigmask,
   1211                  vki_size_t, sigsetsize);
   1212    PRE_MEM_WRITE( "epoll_pwait(events)", ARG2, sizeof(struct vki_epoll_event)*ARG3);
   1213    if (ARG4)
   1214       PRE_MEM_READ( "epoll_pwait(sigmask)", ARG5, sizeof(vki_sigset_t) );
   1215 }
   1216 POST(sys_epoll_pwait)
   1217 {
   1218    vg_assert(SUCCESS);
   1219    if (RES > 0)
   1220       POST_MEM_WRITE( ARG2, sizeof(struct vki_epoll_event)*RES ) ;
   1221 }
   1222 
   1223 PRE(sys_eventfd)
   1224 {
   1225    PRINT("sys_eventfd ( %lu )", ARG1);
   1226    PRE_REG_READ1(long, "sys_eventfd", unsigned int, count);
   1227 }
   1228 POST(sys_eventfd)
   1229 {
   1230    if (!ML_(fd_allowed)(RES, "eventfd", tid, True)) {
   1231       VG_(close)(RES);
   1232       SET_STATUS_Failure( VKI_EMFILE );
   1233    } else {
   1234       if (VG_(clo_track_fds))
   1235          ML_(record_fd_open_nameless) (tid, RES);
   1236    }
   1237 }
   1238 
   1239 PRE(sys_eventfd2)
   1240 {
   1241    PRINT("sys_eventfd2 ( %lu, %ld )", ARG1,ARG2);
   1242    PRE_REG_READ2(long, "sys_eventfd2", unsigned int, count, int, flags);
   1243 }
   1244 POST(sys_eventfd2)
   1245 {
   1246    if (!ML_(fd_allowed)(RES, "eventfd2", tid, True)) {
   1247       VG_(close)(RES);
   1248       SET_STATUS_Failure( VKI_EMFILE );
   1249    } else {
   1250       if (VG_(clo_track_fds))
   1251          ML_(record_fd_open_nameless) (tid, RES);
   1252    }
   1253 }
   1254 
   1255 PRE(sys_fallocate)
   1256 {
   1257    *flags |= SfMayBlock;
   1258 #if VG_WORDSIZE == 4
   1259    PRINT("sys_fallocate ( %ld, %ld, %lld, %lld )",
   1260          ARG1, ARG2, MERGE64(ARG3,ARG4), MERGE64(ARG5,ARG6));
   1261    PRE_REG_READ6(long, "fallocate",
   1262                  int, fd, int, mode,
   1263                  unsigned, MERGE64_FIRST(offset), unsigned, MERGE64_SECOND(offset),
   1264                  unsigned, MERGE64_FIRST(len), unsigned, MERGE64_SECOND(len));
   1265 #elif VG_WORDSIZE == 8
   1266    PRINT("sys_fallocate ( %ld, %ld, %lld, %lld )",
   1267          ARG1, ARG2, (Long)ARG3, (Long)ARG4);
   1268    PRE_REG_READ4(long, "fallocate",
   1269                  int, fd, int, mode, vki_loff_t, offset, vki_loff_t, len);
   1270 #else
   1271 #  error Unexpected word size
   1272 #endif
   1273    if (!ML_(fd_allowed)(ARG1, "fallocate", tid, False))
   1274       SET_STATUS_Failure( VKI_EBADF );
   1275 }
   1276 
   1277 PRE(sys_prlimit64)
   1278 {
   1279    PRINT("sys_prlimit64 ( %ld, %ld, %#lx, %#lx )", ARG1,ARG2,ARG3,ARG4);
   1280    PRE_REG_READ4(long, "prlimit64",
   1281                  vki_pid_t, pid, unsigned int, resource,
   1282                  const struct rlimit64 *, new_rlim,
   1283                  struct rlimit64 *, old_rlim);
   1284    if (ARG3)
   1285       PRE_MEM_READ( "rlimit64(new_rlim)", ARG3, sizeof(struct vki_rlimit64) );
   1286    if (ARG4)
   1287       PRE_MEM_WRITE( "rlimit64(old_rlim)", ARG4, sizeof(struct vki_rlimit64) );
   1288 }
   1289 
   1290 POST(sys_prlimit64)
   1291 {
   1292    if (ARG4) {
   1293       POST_MEM_WRITE( ARG4, sizeof(struct vki_rlimit64) );
   1294 
   1295       switch (ARG2) {
   1296       case VKI_RLIMIT_NOFILE:
   1297          ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(fd_soft_limit);
   1298          ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(fd_hard_limit);
   1299          break;
   1300 
   1301       case VKI_RLIMIT_DATA:
   1302          ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_data).rlim_cur;
   1303          ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_data).rlim_max;
   1304          break;
   1305 
   1306       case VKI_RLIMIT_STACK:
   1307          ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_stack).rlim_cur;
   1308          ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_stack).rlim_max;
   1309          break;
   1310       }
   1311    }
   1312 }
   1313 
   1314 /* ---------------------------------------------------------------------
   1315    tid-related wrappers
   1316    ------------------------------------------------------------------ */
   1317 
   1318 PRE(sys_gettid)
   1319 {
   1320    PRINT("sys_gettid ()");
   1321    PRE_REG_READ0(long, "gettid");
   1322 }
   1323 
   1324 PRE(sys_set_tid_address)
   1325 {
   1326    PRINT("sys_set_tid_address ( %#lx )", ARG1);
   1327    PRE_REG_READ1(long, "set_tid_address", int *, tidptr);
   1328 }
   1329 
   1330 PRE(sys_tkill)
   1331 {
   1332    PRINT("sys_tgkill ( %ld, %ld )", ARG1,ARG2);
   1333    PRE_REG_READ2(long, "tkill", int, tid, int, sig);
   1334    if (!ML_(client_signal_OK)(ARG2)) {
   1335       SET_STATUS_Failure( VKI_EINVAL );
   1336       return;
   1337    }
   1338 
   1339    /* Check to see if this kill gave us a pending signal */
   1340    *flags |= SfPollAfter;
   1341 
   1342    if (VG_(clo_trace_signals))
   1343       VG_(message)(Vg_DebugMsg, "tkill: sending signal %ld to pid %ld\n",
   1344 		   ARG2, ARG1);
   1345 
   1346    /* If we're sending SIGKILL, check to see if the target is one of
   1347       our threads and handle it specially. */
   1348    if (ARG2 == VKI_SIGKILL && ML_(do_sigkill)(ARG1, -1)) {
   1349       SET_STATUS_Success(0);
   1350       return;
   1351    }
   1352 
   1353    /* Ask to handle this syscall via the slow route, since that's the
   1354       only one that sets tst->status to VgTs_WaitSys.  If the result
   1355       of doing the syscall is an immediate run of
   1356       async_signalhandler() in m_signals, then we need the thread to
   1357       be properly tidied away.  I have the impression the previous
   1358       version of this wrapper worked on x86/amd64 only because the
   1359       kernel did not immediately deliver the async signal to this
   1360       thread (on ppc it did, which broke the assertion re tst->status
   1361       at the top of async_signalhandler()). */
   1362    *flags |= SfMayBlock;
   1363 }
   1364 POST(sys_tkill)
   1365 {
   1366    if (VG_(clo_trace_signals))
   1367       VG_(message)(Vg_DebugMsg, "tkill: sent signal %ld to pid %ld\n",
   1368                    ARG2, ARG1);
   1369 }
   1370 
   1371 PRE(sys_tgkill)
   1372 {
   1373    PRINT("sys_tgkill ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   1374    PRE_REG_READ3(long, "tgkill", int, tgid, int, tid, int, sig);
   1375    if (!ML_(client_signal_OK)(ARG3)) {
   1376       SET_STATUS_Failure( VKI_EINVAL );
   1377       return;
   1378    }
   1379 
   1380    /* Check to see if this kill gave us a pending signal */
   1381    *flags |= SfPollAfter;
   1382 
   1383    if (VG_(clo_trace_signals))
   1384       VG_(message)(Vg_DebugMsg,
   1385                    "tgkill: sending signal %ld to pid %ld/%ld\n",
   1386 		   ARG3, ARG1, ARG2);
   1387 
   1388    /* If we're sending SIGKILL, check to see if the target is one of
   1389       our threads and handle it specially. */
   1390    if (ARG3 == VKI_SIGKILL && ML_(do_sigkill)(ARG2, ARG1)) {
   1391       SET_STATUS_Success(0);
   1392       return;
   1393    }
   1394 
   1395    /* Ask to handle this syscall via the slow route, since that's the
   1396       only one that sets tst->status to VgTs_WaitSys.  If the result
   1397       of doing the syscall is an immediate run of
   1398       async_signalhandler() in m_signals, then we need the thread to
   1399       be properly tidied away.  I have the impression the previous
   1400       version of this wrapper worked on x86/amd64 only because the
   1401       kernel did not immediately deliver the async signal to this
   1402       thread (on ppc it did, which broke the assertion re tst->status
   1403       at the top of async_signalhandler()). */
   1404    *flags |= SfMayBlock;
   1405 }
   1406 POST(sys_tgkill)
   1407 {
   1408    if (VG_(clo_trace_signals))
   1409       VG_(message)(Vg_DebugMsg,
   1410                    "tgkill: sent signal %ld to pid %ld/%ld\n",
   1411                    ARG3, ARG1, ARG2);
   1412 }
   1413 
   1414 /* ---------------------------------------------------------------------
   1415    fadvise64* wrappers
   1416    ------------------------------------------------------------------ */
   1417 
   1418 PRE(sys_fadvise64)
   1419 {
   1420    PRINT("sys_fadvise64 ( %ld, %lld, %lu, %ld )",
   1421          ARG1, MERGE64(ARG2,ARG3), ARG4, ARG5);
   1422    PRE_REG_READ5(long, "fadvise64",
   1423                  int, fd, vki_u32, MERGE64_FIRST(offset), vki_u32, MERGE64_SECOND(offset),
   1424                  vki_size_t, len, int, advice);
   1425 }
   1426 
   1427 PRE(sys_fadvise64_64)
   1428 {
   1429    PRINT("sys_fadvise64_64 ( %ld, %lld, %lld, %ld )",
   1430          ARG1, MERGE64(ARG2,ARG3), MERGE64(ARG4,ARG5), ARG6);
   1431    PRE_REG_READ6(long, "fadvise64_64",
   1432                  int, fd, vki_u32, MERGE64_FIRST(offset), vki_u32, MERGE64_SECOND(offset),
   1433                  vki_u32, MERGE64_FIRST(len), vki_u32, MERGE64_SECOND(len), int, advice);
   1434 }
   1435 
   1436 /* ---------------------------------------------------------------------
   1437    io_* wrappers
   1438    ------------------------------------------------------------------ */
   1439 
   1440 // Nb: this wrapper has to pad/unpad memory around the syscall itself,
   1441 // and this allows us to control exactly the code that gets run while
   1442 // the padding is in place.
   1443 
   1444 PRE(sys_io_setup)
   1445 {
   1446    PRINT("sys_io_setup ( %lu, %#lx )", ARG1,ARG2);
   1447    PRE_REG_READ2(long, "io_setup",
   1448                  unsigned, nr_events, vki_aio_context_t *, ctxp);
   1449    PRE_MEM_WRITE( "io_setup(ctxp)", ARG2, sizeof(vki_aio_context_t) );
   1450 }
   1451 
   1452 POST(sys_io_setup)
   1453 {
   1454    SizeT size;
   1455    struct vki_aio_ring *r;
   1456 
   1457    size = VG_PGROUNDUP(sizeof(struct vki_aio_ring) +
   1458                        ARG1*sizeof(struct vki_io_event));
   1459    r = *(struct vki_aio_ring **)ARG2;
   1460    vg_assert(ML_(valid_client_addr)((Addr)r, size, tid, "io_setup"));
   1461 
   1462    ML_(notify_core_and_tool_of_mmap)( (Addr)r, size,
   1463                                       VKI_PROT_READ | VKI_PROT_WRITE,
   1464                                       VKI_MAP_ANONYMOUS, -1, 0 );
   1465 
   1466    POST_MEM_WRITE( ARG2, sizeof(vki_aio_context_t) );
   1467 }
   1468 
   1469 // Nb: This wrapper is "Special" because we need 'size' to do the unmap
   1470 // after the syscall.  We must get 'size' from the aio_ring structure,
   1471 // before the syscall, while the aio_ring structure still exists.  (And we
   1472 // know that we must look at the aio_ring structure because Tom inspected the
   1473 // kernel and glibc sources to see what they do, yuk.)
   1474 //
   1475 // XXX This segment can be implicitly unmapped when aio
   1476 // file-descriptors are closed...
   1477 PRE(sys_io_destroy)
   1478 {
   1479    SizeT size = 0;
   1480 
   1481    PRINT("sys_io_destroy ( %llu )", (ULong)ARG1);
   1482    PRE_REG_READ1(long, "io_destroy", vki_aio_context_t, ctx);
   1483 
   1484    // If we are going to seg fault (due to a bogus ARG1) do it as late as
   1485    // possible...
   1486    if (ML_(safe_to_deref)( (void*)ARG1, sizeof(struct vki_aio_ring))) {
   1487       struct vki_aio_ring *r = (struct vki_aio_ring *)ARG1;
   1488       size = VG_PGROUNDUP(sizeof(struct vki_aio_ring) +
   1489                           r->nr*sizeof(struct vki_io_event));
   1490    }
   1491 
   1492    SET_STATUS_from_SysRes( VG_(do_syscall1)(SYSNO, ARG1) );
   1493 
   1494    if (SUCCESS && RES == 0) {
   1495       Bool d = VG_(am_notify_munmap)( ARG1, size );
   1496       VG_TRACK( die_mem_munmap, ARG1, size );
   1497       if (d)
   1498          VG_(discard_translations)( (Addr64)ARG1, (ULong)size,
   1499                                     "PRE(sys_io_destroy)" );
   1500    }
   1501 }
   1502 
   1503 PRE(sys_io_getevents)
   1504 {
   1505    *flags |= SfMayBlock;
   1506    PRINT("sys_io_getevents ( %llu, %lld, %lld, %#lx, %#lx )",
   1507          (ULong)ARG1,(Long)ARG2,(Long)ARG3,ARG4,ARG5);
   1508    PRE_REG_READ5(long, "io_getevents",
   1509                  vki_aio_context_t, ctx_id, long, min_nr, long, nr,
   1510                  struct io_event *, events,
   1511                  struct timespec *, timeout);
   1512    if (ARG3 > 0)
   1513       PRE_MEM_WRITE( "io_getevents(events)",
   1514                      ARG4, sizeof(struct vki_io_event)*ARG3 );
   1515    if (ARG5 != 0)
   1516       PRE_MEM_READ( "io_getevents(timeout)",
   1517                     ARG5, sizeof(struct vki_timespec));
   1518 }
   1519 POST(sys_io_getevents)
   1520 {
   1521    Int i;
   1522    vg_assert(SUCCESS);
   1523    if (RES > 0) {
   1524       POST_MEM_WRITE( ARG4, sizeof(struct vki_io_event)*RES );
   1525       for (i = 0; i < RES; i++) {
   1526          const struct vki_io_event *vev = ((struct vki_io_event *)ARG4) + i;
   1527          const struct vki_iocb *cb = (struct vki_iocb *)(Addr)vev->obj;
   1528 
   1529          switch (cb->aio_lio_opcode) {
   1530          case VKI_IOCB_CMD_PREAD:
   1531             if (vev->result > 0)
   1532                POST_MEM_WRITE( cb->aio_buf, vev->result );
   1533             break;
   1534 
   1535          case VKI_IOCB_CMD_PWRITE:
   1536             break;
   1537 
   1538          case VKI_IOCB_CMD_FSYNC:
   1539             break;
   1540 
   1541          case VKI_IOCB_CMD_FDSYNC:
   1542             break;
   1543 
   1544          case VKI_IOCB_CMD_PREADV:
   1545 	     if (vev->result > 0) {
   1546                   struct vki_iovec * vec = (struct vki_iovec *)(Addr)cb->aio_buf;
   1547                   Int remains = vev->result;
   1548                   Int j;
   1549 
   1550                   for (j = 0; j < cb->aio_nbytes; j++) {
   1551                        Int nReadThisBuf = vec[j].iov_len;
   1552                        if (nReadThisBuf > remains) nReadThisBuf = remains;
   1553                        POST_MEM_WRITE( (Addr)vec[j].iov_base, nReadThisBuf );
   1554                        remains -= nReadThisBuf;
   1555                        if (remains < 0) VG_(core_panic)("io_getevents(PREADV): remains < 0");
   1556                   }
   1557 	     }
   1558              break;
   1559 
   1560          case VKI_IOCB_CMD_PWRITEV:
   1561              break;
   1562 
   1563          default:
   1564             VG_(message)(Vg_DebugMsg,
   1565                         "Warning: unhandled io_getevents opcode: %u\n",
   1566                         cb->aio_lio_opcode);
   1567             break;
   1568          }
   1569       }
   1570    }
   1571 }
   1572 
   1573 PRE(sys_io_submit)
   1574 {
   1575    Int i, j;
   1576 
   1577    PRINT("sys_io_submit ( %llu, %ld, %#lx )", (ULong)ARG1,ARG2,ARG3);
   1578    PRE_REG_READ3(long, "io_submit",
   1579                  vki_aio_context_t, ctx_id, long, nr,
   1580                  struct iocb **, iocbpp);
   1581    PRE_MEM_READ( "io_submit(iocbpp)", ARG3, ARG2*sizeof(struct vki_iocb *) );
   1582    if (ARG3 != 0) {
   1583       for (i = 0; i < ARG2; i++) {
   1584          struct vki_iocb *cb = ((struct vki_iocb **)ARG3)[i];
   1585          struct vki_iovec *iov;
   1586 
   1587          PRE_MEM_READ( "io_submit(iocb)", (Addr)cb, sizeof(struct vki_iocb) );
   1588          switch (cb->aio_lio_opcode) {
   1589          case VKI_IOCB_CMD_PREAD:
   1590             PRE_MEM_WRITE( "io_submit(PREAD)", cb->aio_buf, cb->aio_nbytes );
   1591             break;
   1592 
   1593          case VKI_IOCB_CMD_PWRITE:
   1594             PRE_MEM_READ( "io_submit(PWRITE)", cb->aio_buf, cb->aio_nbytes );
   1595             break;
   1596 
   1597          case VKI_IOCB_CMD_FSYNC:
   1598             break;
   1599 
   1600          case VKI_IOCB_CMD_FDSYNC:
   1601             break;
   1602 
   1603          case VKI_IOCB_CMD_PREADV:
   1604             iov = (struct vki_iovec *)(Addr)cb->aio_buf;
   1605             PRE_MEM_READ( "io_submit(PREADV)", cb->aio_buf, cb->aio_nbytes * sizeof(struct vki_iovec) );
   1606             for (j = 0; j < cb->aio_nbytes; j++)
   1607                 PRE_MEM_WRITE( "io_submit(PREADV(iov[i]))", (Addr)iov[j].iov_base, iov[j].iov_len );
   1608             break;
   1609 
   1610          case VKI_IOCB_CMD_PWRITEV:
   1611             iov = (struct vki_iovec *)(Addr)cb->aio_buf;
   1612             PRE_MEM_READ( "io_submit(PWRITEV)", cb->aio_buf, cb->aio_nbytes * sizeof(struct vki_iovec) );
   1613             for (j = 0; j < cb->aio_nbytes; j++)
   1614                 PRE_MEM_READ( "io_submit(PWRITEV(iov[i]))", (Addr)iov[j].iov_base, iov[j].iov_len );
   1615             break;
   1616 
   1617          default:
   1618             VG_(message)(Vg_DebugMsg,"Warning: unhandled io_submit opcode: %u\n",
   1619                          cb->aio_lio_opcode);
   1620             break;
   1621          }
   1622       }
   1623    }
   1624 }
   1625 
   1626 PRE(sys_io_cancel)
   1627 {
   1628    PRINT("sys_io_cancel ( %llu, %#lx, %#lx )", (ULong)ARG1,ARG2,ARG3);
   1629    PRE_REG_READ3(long, "io_cancel",
   1630                  vki_aio_context_t, ctx_id, struct iocb *, iocb,
   1631                  struct io_event *, result);
   1632    PRE_MEM_READ( "io_cancel(iocb)", ARG2, sizeof(struct vki_iocb) );
   1633    PRE_MEM_WRITE( "io_cancel(result)", ARG3, sizeof(struct vki_io_event) );
   1634 }
   1635 POST(sys_io_cancel)
   1636 {
   1637    POST_MEM_WRITE( ARG3, sizeof(struct vki_io_event) );
   1638 }
   1639 
   1640 /* ---------------------------------------------------------------------
   1641    *_mempolicy wrappers
   1642    ------------------------------------------------------------------ */
   1643 
   1644 PRE(sys_mbind)
   1645 {
   1646    PRINT("sys_mbind ( %#lx, %lu, %ld, %#lx, %lu, %lu )", ARG1,ARG2,ARG3,ARG4,ARG5,ARG6);
   1647    PRE_REG_READ6(long, "mbind",
   1648                  unsigned long, start, unsigned long, len,
   1649                  unsigned long, policy, unsigned long *, nodemask,
   1650                  unsigned long, maxnode, unsigned, flags);
   1651    if (ARG1 != 0)
   1652       PRE_MEM_READ( "mbind(nodemask)", ARG4,
   1653                     VG_ROUNDUP( ARG5-1, sizeof(UWord) * 8 ) / 8 );
   1654 }
   1655 
   1656 PRE(sys_set_mempolicy)
   1657 {
   1658    PRINT("sys_set_mempolicy ( %ld, %#lx, %ld )", ARG1,ARG2,ARG3);
   1659    PRE_REG_READ3(long, "set_mempolicy",
   1660                  int, policy, unsigned long *, nodemask,
   1661                  unsigned long, maxnode);
   1662    PRE_MEM_READ( "set_mempolicy(nodemask)", ARG2,
   1663                  VG_ROUNDUP( ARG3-1, sizeof(UWord) * 8 ) / 8 );
   1664 }
   1665 
   1666 PRE(sys_get_mempolicy)
   1667 {
   1668    PRINT("sys_get_mempolicy ( %#lx, %#lx, %ld, %#lx, %lx )", ARG1,ARG2,ARG3,ARG4,ARG5);
   1669    PRE_REG_READ5(long, "get_mempolicy",
   1670                  int *, policy, unsigned long *, nodemask,
   1671                  unsigned long, maxnode, unsigned long, addr,
   1672                  unsigned long, flags);
   1673    if (ARG1 != 0)
   1674       PRE_MEM_WRITE( "get_mempolicy(policy)", ARG1, sizeof(Int) );
   1675    if (ARG2 != 0)
   1676       PRE_MEM_WRITE( "get_mempolicy(nodemask)", ARG2,
   1677                      VG_ROUNDUP( ARG3-1, sizeof(UWord) * 8 ) / 8 );
   1678 }
   1679 POST(sys_get_mempolicy)
   1680 {
   1681    if (ARG1 != 0)
   1682       POST_MEM_WRITE( ARG1, sizeof(Int) );
   1683    if (ARG2 != 0)
   1684       POST_MEM_WRITE( ARG2, VG_ROUNDUP( ARG3-1, sizeof(UWord) * 8 ) / 8 );
   1685 }
   1686 
   1687 /* ---------------------------------------------------------------------
   1688    inotify_* wrappers
   1689    ------------------------------------------------------------------ */
   1690 
   1691 PRE(sys_inotify_init)
   1692 {
   1693    PRINT("sys_inotify_init ( )");
   1694    PRE_REG_READ0(long, "inotify_init");
   1695 }
   1696 POST(sys_inotify_init)
   1697 {
   1698    vg_assert(SUCCESS);
   1699    if (!ML_(fd_allowed)(RES, "inotify_init", tid, True)) {
   1700       VG_(close)(RES);
   1701       SET_STATUS_Failure( VKI_EMFILE );
   1702    } else {
   1703       if (VG_(clo_track_fds))
   1704          ML_(record_fd_open_nameless) (tid, RES);
   1705    }
   1706 }
   1707 
   1708 PRE(sys_inotify_init1)
   1709 {
   1710    PRINT("sys_inotify_init ( %ld )", ARG1);
   1711    PRE_REG_READ1(long, "inotify_init", int, flag);
   1712 }
   1713 
   1714 POST(sys_inotify_init1)
   1715 {
   1716    vg_assert(SUCCESS);
   1717    if (!ML_(fd_allowed)(RES, "inotify_init", tid, True)) {
   1718       VG_(close)(RES);
   1719       SET_STATUS_Failure( VKI_EMFILE );
   1720    } else {
   1721       if (VG_(clo_track_fds))
   1722          ML_(record_fd_open_nameless) (tid, RES);
   1723    }
   1724 }
   1725 
   1726 PRE(sys_inotify_add_watch)
   1727 {
   1728    PRINT( "sys_inotify_add_watch ( %ld, %#lx, %lx )", ARG1,ARG2,ARG3);
   1729    PRE_REG_READ3(long, "inotify_add_watch", int, fd, char *, path, int, mask);
   1730    PRE_MEM_RASCIIZ( "inotify_add_watch(path)", ARG2 );
   1731 }
   1732 
   1733 PRE(sys_inotify_rm_watch)
   1734 {
   1735    PRINT( "sys_inotify_rm_watch ( %ld, %lx )", ARG1,ARG2);
   1736    PRE_REG_READ2(long, "inotify_rm_watch", int, fd, int, wd);
   1737 }
   1738 
   1739 /* ---------------------------------------------------------------------
   1740    mq_* wrappers
   1741    ------------------------------------------------------------------ */
   1742 
   1743 PRE(sys_mq_open)
   1744 {
   1745    PRINT("sys_mq_open( %#lx(%s), %ld, %lld, %#lx )",
   1746          ARG1,(char*)ARG1,ARG2,(ULong)ARG3,ARG4);
   1747    PRE_REG_READ4(long, "mq_open",
   1748                  const char *, name, int, oflag, vki_mode_t, mode,
   1749                  struct mq_attr *, attr);
   1750    PRE_MEM_RASCIIZ( "mq_open(name)", ARG1 );
   1751    if ((ARG2 & VKI_O_CREAT) != 0 && ARG4 != 0) {
   1752       const struct vki_mq_attr *attr = (struct vki_mq_attr *)ARG4;
   1753       PRE_MEM_READ( "mq_open(attr->mq_maxmsg)",
   1754                      (Addr)&attr->mq_maxmsg, sizeof(attr->mq_maxmsg) );
   1755       PRE_MEM_READ( "mq_open(attr->mq_msgsize)",
   1756                      (Addr)&attr->mq_msgsize, sizeof(attr->mq_msgsize) );
   1757    }
   1758 }
   1759 POST(sys_mq_open)
   1760 {
   1761    vg_assert(SUCCESS);
   1762    if (!ML_(fd_allowed)(RES, "mq_open", tid, True)) {
   1763       VG_(close)(RES);
   1764       SET_STATUS_Failure( VKI_EMFILE );
   1765    } else {
   1766       if (VG_(clo_track_fds))
   1767          ML_(record_fd_open_with_given_name)(tid, RES, (Char*)ARG1);
   1768    }
   1769 }
   1770 
   1771 PRE(sys_mq_unlink)
   1772 {
   1773    PRINT("sys_mq_unlink ( %#lx(%s) )", ARG1,(char*)ARG1);
   1774    PRE_REG_READ1(long, "mq_unlink", const char *, name);
   1775    PRE_MEM_RASCIIZ( "mq_unlink(name)", ARG1 );
   1776 }
   1777 
   1778 PRE(sys_mq_timedsend)
   1779 {
   1780    *flags |= SfMayBlock;
   1781    PRINT("sys_mq_timedsend ( %ld, %#lx, %llu, %ld, %#lx )",
   1782          ARG1,ARG2,(ULong)ARG3,ARG4,ARG5);
   1783    PRE_REG_READ5(long, "mq_timedsend",
   1784                  vki_mqd_t, mqdes, const char *, msg_ptr, vki_size_t, msg_len,
   1785                  unsigned int, msg_prio, const struct timespec *, abs_timeout);
   1786    if (!ML_(fd_allowed)(ARG1, "mq_timedsend", tid, False)) {
   1787       SET_STATUS_Failure( VKI_EBADF );
   1788    } else {
   1789       PRE_MEM_READ( "mq_timedsend(msg_ptr)", ARG2, ARG3 );
   1790       if (ARG5 != 0)
   1791          PRE_MEM_READ( "mq_timedsend(abs_timeout)", ARG5,
   1792                         sizeof(struct vki_timespec) );
   1793    }
   1794 }
   1795 
   1796 PRE(sys_mq_timedreceive)
   1797 {
   1798    *flags |= SfMayBlock;
   1799    PRINT("sys_mq_timedreceive( %ld, %#lx, %llu, %#lx, %#lx )",
   1800          ARG1,ARG2,(ULong)ARG3,ARG4,ARG5);
   1801    PRE_REG_READ5(ssize_t, "mq_timedreceive",
   1802                  vki_mqd_t, mqdes, char *, msg_ptr, vki_size_t, msg_len,
   1803                  unsigned int *, msg_prio,
   1804                  const struct timespec *, abs_timeout);
   1805    if (!ML_(fd_allowed)(ARG1, "mq_timedreceive", tid, False)) {
   1806       SET_STATUS_Failure( VKI_EBADF );
   1807    } else {
   1808       PRE_MEM_WRITE( "mq_timedreceive(msg_ptr)", ARG2, ARG3 );
   1809       if (ARG4 != 0)
   1810          PRE_MEM_WRITE( "mq_timedreceive(msg_prio)",
   1811                         ARG4, sizeof(unsigned int) );
   1812       if (ARG5 != 0)
   1813          PRE_MEM_READ( "mq_timedreceive(abs_timeout)",
   1814                         ARG5, sizeof(struct vki_timespec) );
   1815    }
   1816 }
   1817 POST(sys_mq_timedreceive)
   1818 {
   1819    POST_MEM_WRITE( ARG2, RES );
   1820    if (ARG4 != 0)
   1821       POST_MEM_WRITE( ARG4, sizeof(unsigned int) );
   1822 }
   1823 
   1824 PRE(sys_mq_notify)
   1825 {
   1826    PRINT("sys_mq_notify( %ld, %#lx )", ARG1,ARG2 );
   1827    PRE_REG_READ2(long, "mq_notify",
   1828                  vki_mqd_t, mqdes, const struct sigevent *, notification);
   1829    if (!ML_(fd_allowed)(ARG1, "mq_notify", tid, False))
   1830       SET_STATUS_Failure( VKI_EBADF );
   1831    else if (ARG2 != 0)
   1832       PRE_MEM_READ( "mq_notify(notification)",
   1833                     ARG2, sizeof(struct vki_sigevent) );
   1834 }
   1835 
   1836 PRE(sys_mq_getsetattr)
   1837 {
   1838    PRINT("sys_mq_getsetattr( %ld, %#lx, %#lx )", ARG1,ARG2,ARG3 );
   1839    PRE_REG_READ3(long, "mq_getsetattr",
   1840                  vki_mqd_t, mqdes, const struct mq_attr *, mqstat,
   1841                  struct mq_attr *, omqstat);
   1842    if (!ML_(fd_allowed)(ARG1, "mq_getsetattr", tid, False)) {
   1843       SET_STATUS_Failure( VKI_EBADF );
   1844    } else {
   1845       if (ARG2 != 0) {
   1846          const struct vki_mq_attr *attr = (struct vki_mq_attr *)ARG2;
   1847          PRE_MEM_READ( "mq_getsetattr(mqstat->mq_flags)",
   1848                         (Addr)&attr->mq_flags, sizeof(attr->mq_flags) );
   1849       }
   1850       if (ARG3 != 0)
   1851          PRE_MEM_WRITE( "mq_getsetattr(omqstat)", ARG3,
   1852                         sizeof(struct vki_mq_attr) );
   1853    }
   1854 }
   1855 POST(sys_mq_getsetattr)
   1856 {
   1857    if (ARG3 != 0)
   1858       POST_MEM_WRITE( ARG3, sizeof(struct vki_mq_attr) );
   1859 }
   1860 
   1861 /* ---------------------------------------------------------------------
   1862    clock_* wrappers
   1863    ------------------------------------------------------------------ */
   1864 
   1865 PRE(sys_clock_settime)
   1866 {
   1867    PRINT("sys_clock_settime( %ld, %#lx )", ARG1,ARG2);
   1868    PRE_REG_READ2(long, "clock_settime",
   1869                  vki_clockid_t, clk_id, const struct timespec *, tp);
   1870    PRE_MEM_READ( "clock_settime(tp)", ARG2, sizeof(struct vki_timespec) );
   1871 }
   1872 
   1873 PRE(sys_clock_gettime)
   1874 {
   1875    PRINT("sys_clock_gettime( %ld, %#lx )" , ARG1,ARG2);
   1876    PRE_REG_READ2(long, "clock_gettime",
   1877                  vki_clockid_t, clk_id, struct timespec *, tp);
   1878    PRE_MEM_WRITE( "clock_gettime(tp)", ARG2, sizeof(struct vki_timespec) );
   1879 }
   1880 POST(sys_clock_gettime)
   1881 {
   1882    POST_MEM_WRITE( ARG2, sizeof(struct vki_timespec) );
   1883 }
   1884 
   1885 PRE(sys_clock_getres)
   1886 {
   1887    PRINT("sys_clock_getres( %ld, %#lx )" , ARG1,ARG2);
   1888    // Nb: we can't use "RES" as the param name because that's a macro
   1889    // defined above!
   1890    PRE_REG_READ2(long, "clock_getres",
   1891                  vki_clockid_t, clk_id, struct timespec *, res);
   1892    if (ARG2 != 0)
   1893       PRE_MEM_WRITE( "clock_getres(res)", ARG2, sizeof(struct vki_timespec) );
   1894 }
   1895 POST(sys_clock_getres)
   1896 {
   1897    if (ARG2 != 0)
   1898       POST_MEM_WRITE( ARG2, sizeof(struct vki_timespec) );
   1899 }
   1900 
   1901 PRE(sys_clock_nanosleep)
   1902 {
   1903    *flags |= SfMayBlock|SfPostOnFail;
   1904    PRINT("sys_clock_nanosleep( %ld, %ld, %#lx, %#lx )", ARG1,ARG2,ARG3,ARG4);
   1905    PRE_REG_READ4(int32_t, "clock_nanosleep",
   1906                  vki_clockid_t, clkid, int, flags,
   1907                  const struct timespec *, rqtp, struct timespec *, rmtp);
   1908    PRE_MEM_READ( "clock_nanosleep(rqtp)", ARG3, sizeof(struct vki_timespec) );
   1909    if (ARG4 != 0)
   1910       PRE_MEM_WRITE( "clock_nanosleep(rmtp)", ARG4, sizeof(struct vki_timespec) );
   1911 }
   1912 POST(sys_clock_nanosleep)
   1913 {
   1914    if (ARG4 != 0 && FAILURE && ERR == VKI_EINTR)
   1915       POST_MEM_WRITE( ARG4, sizeof(struct vki_timespec) );
   1916 }
   1917 
   1918 /* ---------------------------------------------------------------------
   1919    timer_* wrappers
   1920    ------------------------------------------------------------------ */
   1921 
   1922 PRE(sys_timer_create)
   1923 {
   1924    PRINT("sys_timer_create( %ld, %#lx, %#lx )", ARG1,ARG2,ARG3);
   1925    PRE_REG_READ3(long, "timer_create",
   1926                  vki_clockid_t, clockid, struct sigevent *, evp,
   1927                  vki_timer_t *, timerid);
   1928    if (ARG2 != 0)
   1929       PRE_MEM_READ( "timer_create(evp)", ARG2, sizeof(struct vki_sigevent) );
   1930    PRE_MEM_WRITE( "timer_create(timerid)", ARG3, sizeof(vki_timer_t) );
   1931 }
   1932 POST(sys_timer_create)
   1933 {
   1934    POST_MEM_WRITE( ARG3, sizeof(vki_timer_t) );
   1935 }
   1936 
   1937 PRE(sys_timer_settime)
   1938 {
   1939    PRINT("sys_timer_settime( %lld, %ld, %#lx, %#lx )", (ULong)ARG1,ARG2,ARG3,ARG4);
   1940    PRE_REG_READ4(long, "timer_settime",
   1941                  vki_timer_t, timerid, int, flags,
   1942                  const struct itimerspec *, value,
   1943                  struct itimerspec *, ovalue);
   1944    PRE_MEM_READ( "timer_settime(value)", ARG3,
   1945                   sizeof(struct vki_itimerspec) );
   1946    if (ARG4 != 0)
   1947        PRE_MEM_WRITE( "timer_settime(ovalue)", ARG4,
   1948                       sizeof(struct vki_itimerspec) );
   1949 }
   1950 POST(sys_timer_settime)
   1951 {
   1952    if (ARG4 != 0)
   1953       POST_MEM_WRITE( ARG4, sizeof(struct vki_itimerspec) );
   1954 }
   1955 
   1956 PRE(sys_timer_gettime)
   1957 {
   1958    PRINT("sys_timer_gettime( %lld, %#lx )", (ULong)ARG1,ARG2);
   1959    PRE_REG_READ2(long, "timer_gettime",
   1960                  vki_timer_t, timerid, struct itimerspec *, value);
   1961    PRE_MEM_WRITE( "timer_gettime(value)", ARG2,
   1962                   sizeof(struct vki_itimerspec));
   1963 }
   1964 POST(sys_timer_gettime)
   1965 {
   1966    POST_MEM_WRITE( ARG2, sizeof(struct vki_itimerspec) );
   1967 }
   1968 
   1969 PRE(sys_timer_getoverrun)
   1970 {
   1971    PRINT("sys_timer_getoverrun( %#lx )", ARG1);
   1972    PRE_REG_READ1(long, "timer_getoverrun", vki_timer_t, timerid);
   1973 }
   1974 
   1975 PRE(sys_timer_delete)
   1976 {
   1977    PRINT("sys_timer_delete( %#lx )", ARG1);
   1978    PRE_REG_READ1(long, "timer_delete", vki_timer_t, timerid);
   1979 }
   1980 
   1981 /* ---------------------------------------------------------------------
   1982    timerfd* wrappers
   1983    See also http://lwn.net/Articles/260172/ for an overview.
   1984    See also /usr/src/linux/fs/timerfd.c for the implementation.
   1985    ------------------------------------------------------------------ */
   1986 
   1987 /* Returns True if running on 2.6.22, else False (or False if
   1988    cannot be determined). */
   1989 static Bool linux_kernel_2_6_22(void)
   1990 {
   1991    static Int result = -1;
   1992    Int fd, read;
   1993    HChar release[64];
   1994    SysRes res;
   1995 
   1996    if (result == -1) {
   1997       res = VG_(open)("/proc/sys/kernel/osrelease", 0, 0);
   1998       if (sr_isError(res))
   1999          return False;
   2000       fd = sr_Res(res);
   2001       read = VG_(read)(fd, release, sizeof(release) - 1);
   2002       vg_assert(read >= 0);
   2003       release[read] = 0;
   2004       VG_(close)(fd);
   2005       //VG_(printf)("kernel release = %s\n", release);
   2006       result = (VG_(strncmp)(release, "2.6.22", 6) == 0
   2007                 && (release[6] < '0' || release[6] > '9'));
   2008    }
   2009    vg_assert(result == 0 || result == 1);
   2010    return result == 1;
   2011 }
   2012 
   2013 PRE(sys_timerfd_create)
   2014 {
   2015    if (linux_kernel_2_6_22()) {
   2016       /* 2.6.22 kernel: timerfd system call. */
   2017       PRINT("sys_timerfd ( %ld, %ld, %#lx )", ARG1, ARG2, ARG3);
   2018       PRE_REG_READ3(long, "sys_timerfd",
   2019                     int, fd, int, clockid, const struct itimerspec *, tmr);
   2020       PRE_MEM_READ("timerfd(tmr)", ARG3,
   2021                    sizeof(struct vki_itimerspec) );
   2022       if ((Word)ARG1 != -1L && !ML_(fd_allowed)(ARG1, "timerfd", tid, False))
   2023          SET_STATUS_Failure( VKI_EBADF );
   2024    } else {
   2025       /* 2.6.24 and later kernels: timerfd_create system call. */
   2026       PRINT("sys_timerfd_create (%ld, %ld )", ARG1, ARG2);
   2027       PRE_REG_READ2(long, "timerfd_create", int, clockid, int, flags);
   2028    }
   2029 }
   2030 POST(sys_timerfd_create)
   2031 {
   2032    if (linux_kernel_2_6_22())
   2033    {
   2034       /* 2.6.22 kernel: timerfd system call. */
   2035       if (!ML_(fd_allowed)(RES, "timerfd", tid, True)) {
   2036          VG_(close)(RES);
   2037          SET_STATUS_Failure( VKI_EMFILE );
   2038       } else {
   2039          if (VG_(clo_track_fds))
   2040             ML_(record_fd_open_nameless) (tid, RES);
   2041       }
   2042    }
   2043    else
   2044    {
   2045       /* 2.6.24 and later kernels: timerfd_create system call. */
   2046       if (!ML_(fd_allowed)(RES, "timerfd_create", tid, True)) {
   2047          VG_(close)(RES);
   2048          SET_STATUS_Failure( VKI_EMFILE );
   2049       } else {
   2050          if (VG_(clo_track_fds))
   2051             ML_(record_fd_open_nameless) (tid, RES);
   2052       }
   2053    }
   2054 }
   2055 
   2056 PRE(sys_timerfd_gettime)
   2057 {
   2058    PRINT("sys_timerfd_gettime ( %ld, %#lx )", ARG1, ARG2);
   2059    PRE_REG_READ2(long, "timerfd_gettime",
   2060                  int, ufd,
   2061                  struct vki_itimerspec*, otmr);
   2062    if (!ML_(fd_allowed)(ARG1, "timerfd_gettime", tid, False))
   2063       SET_STATUS_Failure(VKI_EBADF);
   2064    else
   2065       PRE_MEM_WRITE("timerfd_gettime(result)",
   2066                     ARG2, sizeof(struct vki_itimerspec));
   2067 }
   2068 POST(sys_timerfd_gettime)
   2069 {
   2070    if (RES == 0)
   2071       POST_MEM_WRITE(ARG2, sizeof(struct vki_itimerspec));
   2072 }
   2073 
   2074 PRE(sys_timerfd_settime)
   2075 {
   2076    PRINT("sys_timerfd_settime ( %ld, %ld, %#lx, %#lx )", ARG1, ARG2, ARG3, ARG4);
   2077    PRE_REG_READ4(long, "timerfd_settime",
   2078                  int, ufd,
   2079                  int, flags,
   2080                  const struct vki_itimerspec*, utmr,
   2081                  struct vki_itimerspec*, otmr);
   2082    if (!ML_(fd_allowed)(ARG1, "timerfd_settime", tid, False))
   2083       SET_STATUS_Failure(VKI_EBADF);
   2084    else
   2085    {
   2086       PRE_MEM_READ("timerfd_settime(result)",
   2087                    ARG3, sizeof(struct vki_itimerspec));
   2088       if (ARG4)
   2089       {
   2090          PRE_MEM_WRITE("timerfd_settime(result)",
   2091                        ARG4, sizeof(struct vki_itimerspec));
   2092       }
   2093    }
   2094 }
   2095 POST(sys_timerfd_settime)
   2096 {
   2097    if (RES == 0 && ARG4 != 0)
   2098       POST_MEM_WRITE(ARG4, sizeof(struct vki_itimerspec));
   2099 }
   2100 
   2101 /* ---------------------------------------------------------------------
   2102    capabilities wrappers
   2103    ------------------------------------------------------------------ */
   2104 
   2105 PRE(sys_capget)
   2106 {
   2107    PRINT("sys_capget ( %#lx, %#lx )", ARG1, ARG2 );
   2108    PRE_REG_READ2(long, "capget",
   2109                  vki_cap_user_header_t, header, vki_cap_user_data_t, data);
   2110    PRE_MEM_READ( "capget(header)", ARG1,
   2111                   sizeof(struct __vki_user_cap_header_struct) );
   2112    PRE_MEM_WRITE( "capget(data)", ARG2,
   2113                   sizeof(struct __vki_user_cap_data_struct) );
   2114 }
   2115 POST(sys_capget)
   2116 {
   2117    if (ARG2 != (Addr)NULL)
   2118       POST_MEM_WRITE( ARG2, sizeof(struct __vki_user_cap_data_struct) );
   2119 }
   2120 
   2121 PRE(sys_capset)
   2122 {
   2123    PRINT("sys_capset ( %#lx, %#lx )", ARG1, ARG2 );
   2124    PRE_REG_READ2(long, "capset",
   2125                  vki_cap_user_header_t, header,
   2126                  const vki_cap_user_data_t, data);
   2127    PRE_MEM_READ( "capset(header)",
   2128                   ARG1, sizeof(struct __vki_user_cap_header_struct) );
   2129    PRE_MEM_READ( "capset(data)",
   2130                   ARG2, sizeof(struct __vki_user_cap_data_struct) );
   2131 }
   2132 
   2133 /* ---------------------------------------------------------------------
   2134    16-bit uid/gid/groups wrappers
   2135    ------------------------------------------------------------------ */
   2136 
   2137 PRE(sys_getuid16)
   2138 {
   2139    PRINT("sys_getuid16 ( )");
   2140    PRE_REG_READ0(long, "getuid16");
   2141 }
   2142 
   2143 PRE(sys_setuid16)
   2144 {
   2145    PRINT("sys_setuid16 ( %ld )", ARG1);
   2146    PRE_REG_READ1(long, "setuid16", vki_old_uid_t, uid);
   2147 }
   2148 
   2149 PRE(sys_getgid16)
   2150 {
   2151    PRINT("sys_getgid16 ( )");
   2152    PRE_REG_READ0(long, "getgid16");
   2153 }
   2154 
   2155 PRE(sys_setgid16)
   2156 {
   2157    PRINT("sys_setgid16 ( %ld )", ARG1);
   2158    PRE_REG_READ1(long, "setgid16", vki_old_gid_t, gid);
   2159 }
   2160 
   2161 PRE(sys_geteuid16)
   2162 {
   2163    PRINT("sys_geteuid16 ( )");
   2164    PRE_REG_READ0(long, "geteuid16");
   2165 }
   2166 
   2167 PRE(sys_getegid16)
   2168 {
   2169    PRINT("sys_getegid16 ( )");
   2170    PRE_REG_READ0(long, "getegid16");
   2171 }
   2172 
   2173 PRE(sys_setreuid16)
   2174 {
   2175    PRINT("setreuid16 ( 0x%lx, 0x%lx )", ARG1, ARG2);
   2176    PRE_REG_READ2(long, "setreuid16", vki_old_uid_t, ruid, vki_old_uid_t, euid);
   2177 }
   2178 
   2179 PRE(sys_setregid16)
   2180 {
   2181    PRINT("sys_setregid16 ( %ld, %ld )", ARG1, ARG2);
   2182    PRE_REG_READ2(long, "setregid16", vki_old_gid_t, rgid, vki_old_gid_t, egid);
   2183 }
   2184 
   2185 PRE(sys_getgroups16)
   2186 {
   2187    PRINT("sys_getgroups16 ( %ld, %#lx )", ARG1, ARG2);
   2188    PRE_REG_READ2(long, "getgroups16", int, size, vki_old_gid_t *, list);
   2189    if (ARG1 > 0)
   2190       PRE_MEM_WRITE( "getgroups16(list)", ARG2, ARG1 * sizeof(vki_old_gid_t) );
   2191 }
   2192 POST(sys_getgroups16)
   2193 {
   2194    vg_assert(SUCCESS);
   2195    if (ARG1 > 0 && RES > 0)
   2196       POST_MEM_WRITE( ARG2, RES * sizeof(vki_old_gid_t) );
   2197 }
   2198 
   2199 PRE(sys_setgroups16)
   2200 {
   2201    PRINT("sys_setgroups16 ( %llu, %#lx )", (ULong)ARG1, ARG2);
   2202    PRE_REG_READ2(long, "setgroups16", int, size, vki_old_gid_t *, list);
   2203    if (ARG1 > 0)
   2204       PRE_MEM_READ( "setgroups16(list)", ARG2, ARG1 * sizeof(vki_old_gid_t) );
   2205 }
   2206 
   2207 /* ---------------------------------------------------------------------
   2208    *chown16 wrappers
   2209    ------------------------------------------------------------------ */
   2210 
   2211 PRE(sys_chown16)
   2212 {
   2213    PRINT("sys_chown16 ( %#lx, 0x%lx, 0x%lx )", ARG1,ARG2,ARG3);
   2214    PRE_REG_READ3(long, "chown16",
   2215                  const char *, path,
   2216                  vki_old_uid_t, owner, vki_old_gid_t, group);
   2217    PRE_MEM_RASCIIZ( "chown16(path)", ARG1 );
   2218 }
   2219 
   2220 PRE(sys_fchown16)
   2221 {
   2222    PRINT("sys_fchown16 ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   2223    PRE_REG_READ3(long, "fchown16",
   2224                  unsigned int, fd, vki_old_uid_t, owner, vki_old_gid_t, group);
   2225 }
   2226 
   2227 /* ---------------------------------------------------------------------
   2228    *xattr wrappers
   2229    ------------------------------------------------------------------ */
   2230 
   2231 PRE(sys_setxattr)
   2232 {
   2233    *flags |= SfMayBlock;
   2234    PRINT("sys_setxattr ( %#lx, %#lx, %#lx, %llu, %ld )",
   2235          ARG1, ARG2, ARG3, (ULong)ARG4, ARG5);
   2236    PRE_REG_READ5(long, "setxattr",
   2237                  char *, path, char *, name,
   2238                  void *, value, vki_size_t, size, int, flags);
   2239    PRE_MEM_RASCIIZ( "setxattr(path)", ARG1 );
   2240    PRE_MEM_RASCIIZ( "setxattr(name)", ARG2 );
   2241    PRE_MEM_READ( "setxattr(value)", ARG3, ARG4 );
   2242 }
   2243 
   2244 PRE(sys_lsetxattr)
   2245 {
   2246    *flags |= SfMayBlock;
   2247    PRINT("sys_lsetxattr ( %#lx, %#lx, %#lx, %llu, %ld )",
   2248          ARG1, ARG2, ARG3, (ULong)ARG4, ARG5);
   2249    PRE_REG_READ5(long, "lsetxattr",
   2250                  char *, path, char *, name,
   2251                  void *, value, vki_size_t, size, int, flags);
   2252    PRE_MEM_RASCIIZ( "lsetxattr(path)", ARG1 );
   2253    PRE_MEM_RASCIIZ( "lsetxattr(name)", ARG2 );
   2254    PRE_MEM_READ( "lsetxattr(value)", ARG3, ARG4 );
   2255 }
   2256 
   2257 PRE(sys_fsetxattr)
   2258 {
   2259    *flags |= SfMayBlock;
   2260    PRINT("sys_fsetxattr ( %ld, %#lx, %#lx, %llu, %ld )",
   2261          ARG1, ARG2, ARG3, (ULong)ARG4, ARG5);
   2262    PRE_REG_READ5(long, "fsetxattr",
   2263                  int, fd, char *, name, void *, value,
   2264                  vki_size_t, size, int, flags);
   2265    PRE_MEM_RASCIIZ( "fsetxattr(name)", ARG2 );
   2266    PRE_MEM_READ( "fsetxattr(value)", ARG3, ARG4 );
   2267 }
   2268 
   2269 PRE(sys_getxattr)
   2270 {
   2271    *flags |= SfMayBlock;
   2272    PRINT("sys_getxattr ( %#lx, %#lx, %#lx, %llu )", ARG1,ARG2,ARG3, (ULong)ARG4);
   2273    PRE_REG_READ4(ssize_t, "getxattr",
   2274                  char *, path, char *, name, void *, value, vki_size_t, size);
   2275    PRE_MEM_RASCIIZ( "getxattr(path)", ARG1 );
   2276    PRE_MEM_RASCIIZ( "getxattr(name)", ARG2 );
   2277    PRE_MEM_WRITE( "getxattr(value)", ARG3, ARG4 );
   2278 }
   2279 POST(sys_getxattr)
   2280 {
   2281    vg_assert(SUCCESS);
   2282    if (RES > 0 && ARG3 != (Addr)NULL) {
   2283       POST_MEM_WRITE( ARG3, RES );
   2284    }
   2285 }
   2286 
   2287 PRE(sys_lgetxattr)
   2288 {
   2289    *flags |= SfMayBlock;
   2290    PRINT("sys_lgetxattr ( %#lx, %#lx, %#lx, %llu )", ARG1,ARG2,ARG3, (ULong)ARG4);
   2291    PRE_REG_READ4(ssize_t, "lgetxattr",
   2292                  char *, path, char *, name, void *, value, vki_size_t, size);
   2293    PRE_MEM_RASCIIZ( "lgetxattr(path)", ARG1 );
   2294    PRE_MEM_RASCIIZ( "lgetxattr(name)", ARG2 );
   2295    PRE_MEM_WRITE( "lgetxattr(value)", ARG3, ARG4 );
   2296 }
   2297 POST(sys_lgetxattr)
   2298 {
   2299    vg_assert(SUCCESS);
   2300    if (RES > 0 && ARG3 != (Addr)NULL) {
   2301       POST_MEM_WRITE( ARG3, RES );
   2302    }
   2303 }
   2304 
   2305 PRE(sys_fgetxattr)
   2306 {
   2307    *flags |= SfMayBlock;
   2308    PRINT("sys_fgetxattr ( %ld, %#lx, %#lx, %llu )", ARG1, ARG2, ARG3, (ULong)ARG4);
   2309    PRE_REG_READ4(ssize_t, "fgetxattr",
   2310                  int, fd, char *, name, void *, value, vki_size_t, size);
   2311    PRE_MEM_RASCIIZ( "fgetxattr(name)", ARG2 );
   2312    PRE_MEM_WRITE( "fgetxattr(value)", ARG3, ARG4 );
   2313 }
   2314 POST(sys_fgetxattr)
   2315 {
   2316    if (RES > 0 && ARG3 != (Addr)NULL)
   2317       POST_MEM_WRITE( ARG3, RES );
   2318 }
   2319 
   2320 PRE(sys_listxattr)
   2321 {
   2322    *flags |= SfMayBlock;
   2323    PRINT("sys_listxattr ( %#lx, %#lx, %llu )", ARG1, ARG2, (ULong)ARG3);
   2324    PRE_REG_READ3(ssize_t, "listxattr",
   2325                  char *, path, char *, list, vki_size_t, size);
   2326    PRE_MEM_RASCIIZ( "listxattr(path)", ARG1 );
   2327    PRE_MEM_WRITE( "listxattr(list)", ARG2, ARG3 );
   2328 }
   2329 POST(sys_listxattr)
   2330 {
   2331    if (RES > 0 && ARG2 != (Addr)NULL)
   2332       POST_MEM_WRITE( ARG2, RES );
   2333 }
   2334 
   2335 PRE(sys_llistxattr)
   2336 {
   2337    *flags |= SfMayBlock;
   2338    PRINT("sys_llistxattr ( %#lx, %#lx, %llu )", ARG1, ARG2, (ULong)ARG3);
   2339    PRE_REG_READ3(ssize_t, "llistxattr",
   2340                  char *, path, char *, list, vki_size_t, size);
   2341    PRE_MEM_RASCIIZ( "llistxattr(path)", ARG1 );
   2342    PRE_MEM_WRITE( "llistxattr(list)", ARG2, ARG3 );
   2343 }
   2344 POST(sys_llistxattr)
   2345 {
   2346    if (RES > 0 && ARG2 != (Addr)NULL)
   2347       POST_MEM_WRITE( ARG2, RES );
   2348 }
   2349 
   2350 PRE(sys_flistxattr)
   2351 {
   2352    *flags |= SfMayBlock;
   2353    PRINT("sys_flistxattr ( %ld, %#lx, %llu )", ARG1, ARG2, (ULong)ARG3);
   2354    PRE_REG_READ3(ssize_t, "flistxattr",
   2355                  int, fd, char *, list, vki_size_t, size);
   2356    PRE_MEM_WRITE( "flistxattr(list)", ARG2, ARG3 );
   2357 }
   2358 POST(sys_flistxattr)
   2359 {
   2360    if (RES > 0 && ARG2 != (Addr)NULL)
   2361       POST_MEM_WRITE( ARG2, RES );
   2362 }
   2363 
   2364 PRE(sys_removexattr)
   2365 {
   2366    *flags |= SfMayBlock;
   2367    PRINT("sys_removexattr ( %#lx, %#lx )", ARG1, ARG2);
   2368    PRE_REG_READ2(long, "removexattr", char *, path, char *, name);
   2369    PRE_MEM_RASCIIZ( "removexattr(path)", ARG1 );
   2370    PRE_MEM_RASCIIZ( "removexattr(name)", ARG2 );
   2371 }
   2372 
   2373 PRE(sys_lremovexattr)
   2374 {
   2375    *flags |= SfMayBlock;
   2376    PRINT("sys_lremovexattr ( %#lx, %#lx )", ARG1, ARG2);
   2377    PRE_REG_READ2(long, "lremovexattr", char *, path, char *, name);
   2378    PRE_MEM_RASCIIZ( "lremovexattr(path)", ARG1 );
   2379    PRE_MEM_RASCIIZ( "lremovexattr(name)", ARG2 );
   2380 }
   2381 
   2382 PRE(sys_fremovexattr)
   2383 {
   2384    *flags |= SfMayBlock;
   2385    PRINT("sys_fremovexattr ( %ld, %#lx )", ARG1, ARG2);
   2386    PRE_REG_READ2(long, "fremovexattr", int, fd, char *, name);
   2387    PRE_MEM_RASCIIZ( "fremovexattr(name)", ARG2 );
   2388 }
   2389 
   2390 /* ---------------------------------------------------------------------
   2391    sched_* wrappers
   2392    ------------------------------------------------------------------ */
   2393 
   2394 PRE(sys_sched_setparam)
   2395 {
   2396    PRINT("sched_setparam ( %ld, %#lx )", ARG1, ARG2 );
   2397    PRE_REG_READ2(long, "sched_setparam",
   2398                  vki_pid_t, pid, struct sched_param *, p);
   2399    PRE_MEM_READ( "sched_setparam(p)", ARG2, sizeof(struct vki_sched_param) );
   2400 }
   2401 POST(sys_sched_setparam)
   2402 {
   2403    POST_MEM_WRITE( ARG2, sizeof(struct vki_sched_param) );
   2404 }
   2405 
   2406 PRE(sys_sched_getparam)
   2407 {
   2408    PRINT("sched_getparam ( %ld, %#lx )", ARG1, ARG2 );
   2409    PRE_REG_READ2(long, "sched_getparam",
   2410                  vki_pid_t, pid, struct sched_param *, p);
   2411    PRE_MEM_WRITE( "sched_getparam(p)", ARG2, sizeof(struct vki_sched_param) );
   2412 }
   2413 POST(sys_sched_getparam)
   2414 {
   2415    POST_MEM_WRITE( ARG2, sizeof(struct vki_sched_param) );
   2416 }
   2417 
   2418 PRE(sys_sched_getscheduler)
   2419 {
   2420    PRINT("sys_sched_getscheduler ( %ld )", ARG1);
   2421    PRE_REG_READ1(long, "sched_getscheduler", vki_pid_t, pid);
   2422 }
   2423 
   2424 PRE(sys_sched_setscheduler)
   2425 {
   2426    PRINT("sys_sched_setscheduler ( %ld, %ld, %#lx )", ARG1,ARG2,ARG3);
   2427    PRE_REG_READ3(long, "sched_setscheduler",
   2428                  vki_pid_t, pid, int, policy, struct sched_param *, p);
   2429    if (ARG3 != 0)
   2430       PRE_MEM_READ( "sched_setscheduler(p)",
   2431 		    ARG3, sizeof(struct vki_sched_param));
   2432 }
   2433 
   2434 PRE(sys_sched_yield)
   2435 {
   2436    *flags |= SfMayBlock;
   2437    PRINT("sched_yield()");
   2438    PRE_REG_READ0(long, "sys_sched_yield");
   2439 }
   2440 
   2441 PRE(sys_sched_get_priority_max)
   2442 {
   2443    PRINT("sched_get_priority_max ( %ld )", ARG1);
   2444    PRE_REG_READ1(long, "sched_get_priority_max", int, policy);
   2445 }
   2446 
   2447 PRE(sys_sched_get_priority_min)
   2448 {
   2449    PRINT("sched_get_priority_min ( %ld )", ARG1);
   2450    PRE_REG_READ1(long, "sched_get_priority_min", int, policy);
   2451 }
   2452 
   2453 PRE(sys_sched_rr_get_interval)
   2454 {
   2455    PRINT("sys_sched_rr_get_interval ( %ld, %#lx )", ARG1, ARG2);
   2456    PRE_REG_READ2(int, "sched_rr_get_interval",
   2457                  vki_pid_t, pid,
   2458                  struct vki_timespec *, tp);
   2459    PRE_MEM_WRITE("sched_rr_get_interval(timespec)",
   2460                  ARG2, sizeof(struct vki_timespec));
   2461 }
   2462 
   2463 POST(sys_sched_rr_get_interval)
   2464 {
   2465    POST_MEM_WRITE(ARG2, sizeof(struct vki_timespec));
   2466 }
   2467 
   2468 PRE(sys_sched_setaffinity)
   2469 {
   2470    PRINT("sched_setaffinity ( %ld, %ld, %#lx )", ARG1, ARG2, ARG3);
   2471    PRE_REG_READ3(long, "sched_setaffinity",
   2472                  vki_pid_t, pid, unsigned int, len, unsigned long *, mask);
   2473    PRE_MEM_READ( "sched_setaffinity(mask)", ARG3, ARG2);
   2474 }
   2475 
   2476 PRE(sys_sched_getaffinity)
   2477 {
   2478    PRINT("sched_getaffinity ( %ld, %ld, %#lx )", ARG1, ARG2, ARG3);
   2479    PRE_REG_READ3(long, "sched_getaffinity",
   2480                  vki_pid_t, pid, unsigned int, len, unsigned long *, mask);
   2481    PRE_MEM_WRITE( "sched_getaffinity(mask)", ARG3, ARG2);
   2482 }
   2483 POST(sys_sched_getaffinity)
   2484 {
   2485    POST_MEM_WRITE(ARG3, ARG2);
   2486 }
   2487 
   2488 /* ---------------------------------------------------------------------
   2489    miscellaneous wrappers
   2490    ------------------------------------------------------------------ */
   2491 
   2492 PRE(sys_munlockall)
   2493 {
   2494    *flags |= SfMayBlock;
   2495    PRINT("sys_munlockall ( )");
   2496    PRE_REG_READ0(long, "munlockall");
   2497 }
   2498 
   2499 // This has different signatures for different platforms.
   2500 //
   2501 //  x86:   int  sys_pipe(unsigned long __user *fildes);
   2502 //  AMD64: long sys_pipe(int *fildes);
   2503 //  ppc32: int  sys_pipe(int __user *fildes);
   2504 //  ppc64: int  sys_pipe(int __user *fildes);
   2505 //
   2506 // The type of the argument is most important, and it is an array of 32 bit
   2507 // values in all cases.  (The return type differs across platforms, but it
   2508 // is not used.)  So we use 'int' as its type.  This fixed bug #113230 which
   2509 // was caused by using an array of 'unsigned long's, which didn't work on
   2510 // AMD64.
   2511 PRE(sys_pipe)
   2512 {
   2513    PRINT("sys_pipe ( %#lx )", ARG1);
   2514    PRE_REG_READ1(int, "pipe", int *, filedes);
   2515    PRE_MEM_WRITE( "pipe(filedes)", ARG1, 2*sizeof(int) );
   2516 }
   2517 POST(sys_pipe)
   2518 {
   2519    Int *p = (Int *)ARG1;
   2520    if (!ML_(fd_allowed)(p[0], "pipe", tid, True) ||
   2521        !ML_(fd_allowed)(p[1], "pipe", tid, True)) {
   2522       VG_(close)(p[0]);
   2523       VG_(close)(p[1]);
   2524       SET_STATUS_Failure( VKI_EMFILE );
   2525    } else {
   2526       POST_MEM_WRITE( ARG1, 2*sizeof(int) );
   2527       if (VG_(clo_track_fds)) {
   2528          ML_(record_fd_open_nameless)(tid, p[0]);
   2529          ML_(record_fd_open_nameless)(tid, p[1]);
   2530       }
   2531    }
   2532 }
   2533 
   2534 /* pipe2 (a kernel 2.6.twentysomething invention) is like pipe, except
   2535    there's a second arg containing flags to be applied to the new file
   2536    descriptors.  It hardly seems worth the effort to factor out the
   2537    duplicated code, hence: */
   2538 PRE(sys_pipe2)
   2539 {
   2540    PRINT("sys_pipe2 ( %#lx, %#lx )", ARG1, ARG2);
   2541    PRE_REG_READ2(int, "pipe", int *, filedes, long, flags);
   2542    PRE_MEM_WRITE( "pipe2(filedes)", ARG1, 2*sizeof(int) );
   2543 }
   2544 POST(sys_pipe2)
   2545 {
   2546    Int *p = (Int *)ARG1;
   2547    if (!ML_(fd_allowed)(p[0], "pipe2", tid, True) ||
   2548        !ML_(fd_allowed)(p[1], "pipe2", tid, True)) {
   2549       VG_(close)(p[0]);
   2550       VG_(close)(p[1]);
   2551       SET_STATUS_Failure( VKI_EMFILE );
   2552    } else {
   2553       POST_MEM_WRITE( ARG1, 2*sizeof(int) );
   2554       if (VG_(clo_track_fds)) {
   2555          ML_(record_fd_open_nameless)(tid, p[0]);
   2556          ML_(record_fd_open_nameless)(tid, p[1]);
   2557       }
   2558    }
   2559 }
   2560 
   2561 PRE(sys_dup3)
   2562 {
   2563    PRINT("sys_dup3 ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   2564    PRE_REG_READ3(long, "dup3", unsigned int, oldfd, unsigned int, newfd, int, flags);
   2565    if (!ML_(fd_allowed)(ARG2, "dup3", tid, True))
   2566       SET_STATUS_Failure( VKI_EBADF );
   2567 }
   2568 
   2569 POST(sys_dup3)
   2570 {
   2571    vg_assert(SUCCESS);
   2572    if (VG_(clo_track_fds))
   2573       ML_(record_fd_open_named)(tid, RES);
   2574 }
   2575 
   2576 PRE(sys_quotactl)
   2577 {
   2578    PRINT("sys_quotactl (0x%lx, %#lx, 0x%lx, 0x%lx )", ARG1,ARG2,ARG3, ARG4);
   2579    PRE_REG_READ4(long, "quotactl",
   2580                  unsigned int, cmd, const char *, special, vki_qid_t, id,
   2581                  void *, addr);
   2582    PRE_MEM_RASCIIZ( "quotactl(special)", ARG2 );
   2583 }
   2584 
   2585 PRE(sys_waitid)
   2586 {
   2587    *flags |= SfMayBlock;
   2588    PRINT("sys_waitid( %ld, %ld, %#lx, %ld, %#lx )", ARG1,ARG2,ARG3,ARG4,ARG5);
   2589    PRE_REG_READ5(int32_t, "sys_waitid",
   2590                  int, which, vki_pid_t, pid, struct vki_siginfo *, infop,
   2591                  int, options, struct vki_rusage *, ru);
   2592    PRE_MEM_WRITE( "waitid(infop)", ARG3, sizeof(struct vki_siginfo) );
   2593    if (ARG5 != 0)
   2594       PRE_MEM_WRITE( "waitid(ru)", ARG5, sizeof(struct vki_rusage) );
   2595 }
   2596 POST(sys_waitid)
   2597 {
   2598    POST_MEM_WRITE( ARG3, sizeof(struct vki_siginfo) );
   2599    if (ARG5 != 0)
   2600       POST_MEM_WRITE( ARG5, sizeof(struct vki_rusage) );
   2601 }
   2602 
   2603 PRE(sys_sync_file_range)
   2604 {
   2605    *flags |= SfMayBlock;
   2606 #if VG_WORDSIZE == 4
   2607    PRINT("sys_sync_file_range ( %ld, %lld, %lld, %ld )",
   2608          ARG1,MERGE64(ARG2,ARG3),MERGE64(ARG4,ARG5),ARG6);
   2609    PRE_REG_READ6(long, "sync_file_range",
   2610                  int, fd,
   2611                  unsigned, MERGE64_FIRST(offset), unsigned, MERGE64_SECOND(offset),
   2612                  unsigned, MERGE64_FIRST(nbytes), unsigned, MERGE64_SECOND(nbytes),
   2613                  unsigned int, flags);
   2614 #elif VG_WORDSIZE == 8
   2615    PRINT("sys_sync_file_range ( %ld, %lld, %lld, %ld )",
   2616          ARG1,(Long)ARG2,(Long)ARG3,ARG4);
   2617    PRE_REG_READ4(long, "sync_file_range",
   2618                  int, fd, vki_loff_t, offset, vki_loff_t, nbytes,
   2619                  unsigned int, flags);
   2620 #else
   2621 #  error Unexpected word size
   2622 #endif
   2623    if (!ML_(fd_allowed)(ARG1, "sync_file_range", tid, False))
   2624       SET_STATUS_Failure( VKI_EBADF );
   2625 }
   2626 
   2627 PRE(sys_sync_file_range2)
   2628 {
   2629    *flags |= SfMayBlock;
   2630 #if VG_WORDSIZE == 4
   2631    PRINT("sys_sync_file_range2 ( %ld, %ld, %lld, %lld )",
   2632          ARG1,ARG2,MERGE64(ARG3,ARG4),MERGE64(ARG5,ARG6));
   2633    PRE_REG_READ6(long, "sync_file_range2",
   2634                  int, fd, unsigned int, flags,
   2635                  unsigned, MERGE64_FIRST(offset), unsigned, MERGE64_SECOND(offset),
   2636                  unsigned, MERGE64_FIRST(nbytes), unsigned, MERGE64_SECOND(nbytes));
   2637 #elif VG_WORDSIZE == 8
   2638    PRINT("sys_sync_file_range2 ( %ld, %ld, %lld, %lld )",
   2639          ARG1,ARG2,(Long)ARG3,(Long)ARG4);
   2640    PRE_REG_READ4(long, "sync_file_range2",
   2641                  int, fd, unsigned int, flags,
   2642                  vki_loff_t, offset, vki_loff_t, nbytes);
   2643 #else
   2644 #  error Unexpected word size
   2645 #endif
   2646    if (!ML_(fd_allowed)(ARG1, "sync_file_range2", tid, False))
   2647       SET_STATUS_Failure( VKI_EBADF );
   2648 }
   2649 
   2650 PRE(sys_stime)
   2651 {
   2652    PRINT("sys_stime ( %#lx )", ARG1);
   2653    PRE_REG_READ1(int, "stime", vki_time_t*, t);
   2654    PRE_MEM_READ( "stime(t)", ARG1, sizeof(vki_time_t) );
   2655 }
   2656 
   2657 PRE(sys_perf_event_open)
   2658 {
   2659    struct vki_perf_event_attr *attr;
   2660    PRINT("sys_perf_event_open ( %#lx, %ld, %ld, %ld, %ld )",
   2661          ARG1,ARG2,ARG3,ARG4,ARG5);
   2662    PRE_REG_READ5(long, "perf_event_open",
   2663                  struct vki_perf_event_attr *, attr,
   2664                  vki_pid_t, pid, int, cpu, int, group_fd,
   2665                  unsigned long, flags);
   2666    attr = (struct vki_perf_event_attr *)ARG1;
   2667    PRE_MEM_READ( "perf_event_open(attr->size)",
   2668                  (Addr)&attr->size, sizeof(attr->size) );
   2669    PRE_MEM_READ( "perf_event_open(attr)",
   2670                  (Addr)attr, attr->size );
   2671 }
   2672 
   2673 POST(sys_perf_event_open)
   2674 {
   2675    vg_assert(SUCCESS);
   2676    if (!ML_(fd_allowed)(RES, "perf_event_open", tid, True)) {
   2677       VG_(close)(RES);
   2678       SET_STATUS_Failure( VKI_EMFILE );
   2679    } else {
   2680       if (VG_(clo_track_fds))
   2681          ML_(record_fd_open_nameless)(tid, RES);
   2682    }
   2683 }
   2684 
   2685 PRE(sys_getcpu)
   2686 {
   2687    PRINT("sys_getcpu ( %#lx, %#lx, %#lx )" , ARG1,ARG2,ARG3);
   2688    PRE_REG_READ3(int, "getcpu",
   2689                  unsigned *, cpu, unsigned *, node, struct vki_getcpu_cache *, tcache);
   2690    if (ARG1 != 0)
   2691       PRE_MEM_WRITE( "getcpu(cpu)", ARG1, sizeof(unsigned) );
   2692    if (ARG2 != 0)
   2693       PRE_MEM_WRITE( "getcpu(node)", ARG2, sizeof(unsigned) );
   2694    if (ARG3 != 0)
   2695       PRE_MEM_WRITE( "getcpu(tcache)", ARG3, sizeof(struct vki_getcpu_cache) );
   2696 }
   2697 
   2698 POST(sys_getcpu)
   2699 {
   2700    if (ARG1 != 0)
   2701       POST_MEM_WRITE( ARG1, sizeof(unsigned) );
   2702    if (ARG2 != 0)
   2703       POST_MEM_WRITE( ARG2, sizeof(unsigned) );
   2704    if (ARG3 != 0)
   2705       POST_MEM_WRITE( ARG3, sizeof(struct vki_getcpu_cache) );
   2706 }
   2707 
   2708 /* ---------------------------------------------------------------------
   2709    utime wrapper
   2710    ------------------------------------------------------------------ */
   2711 
   2712 PRE(sys_utime)
   2713 {
   2714    *flags |= SfMayBlock;
   2715    PRINT("sys_utime ( %#lx, %#lx )", ARG1,ARG2);
   2716    PRE_REG_READ2(long, "utime", char *, filename, struct utimbuf *, buf);
   2717    PRE_MEM_RASCIIZ( "utime(filename)", ARG1 );
   2718    if (ARG2 != 0)
   2719       PRE_MEM_READ( "utime(buf)", ARG2, sizeof(struct vki_utimbuf) );
   2720 }
   2721 
   2722 /* ---------------------------------------------------------------------
   2723    lseek wrapper
   2724    ------------------------------------------------------------------ */
   2725 
   2726 PRE(sys_lseek)
   2727 {
   2728    PRINT("sys_lseek ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   2729    PRE_REG_READ3(vki_off_t, "lseek",
   2730                  unsigned int, fd, vki_off_t, offset, unsigned int, whence);
   2731 }
   2732 
   2733 /* ---------------------------------------------------------------------
   2734    readahead wrapper
   2735    ------------------------------------------------------------------ */
   2736 
   2737 PRE(sys_readahead)
   2738 {
   2739    *flags |= SfMayBlock;
   2740 #if VG_WORDSIZE == 4
   2741    PRINT("sys_readahead ( %ld, %lld, %ld )", ARG1, MERGE64(ARG2,ARG3), ARG4);
   2742    PRE_REG_READ4(vki_off_t, "readahead",
   2743                  int, fd, unsigned, MERGE64_FIRST(offset),
   2744                  unsigned, MERGE64_SECOND(offset), vki_size_t, count);
   2745 #elif VG_WORDSIZE == 8
   2746    PRINT("sys_readahead ( %ld, %lld, %ld )", ARG1, (Long)ARG2, ARG3);
   2747    PRE_REG_READ3(vki_off_t, "readahead",
   2748                  int, fd, vki_loff_t, offset, vki_size_t, count);
   2749 #else
   2750 #  error Unexpected word size
   2751 #endif
   2752    if (!ML_(fd_allowed)(ARG1, "readahead", tid, False))
   2753       SET_STATUS_Failure( VKI_EBADF );
   2754 }
   2755 
   2756 /* ---------------------------------------------------------------------
   2757    sig* wrappers
   2758    ------------------------------------------------------------------ */
   2759 
   2760 PRE(sys_sigpending)
   2761 {
   2762    PRINT( "sys_sigpending ( %#lx )", ARG1 );
   2763    PRE_REG_READ1(long, "sigpending", vki_old_sigset_t *, set);
   2764    PRE_MEM_WRITE( "sigpending(set)", ARG1, sizeof(vki_old_sigset_t));
   2765 }
   2766 POST(sys_sigpending)
   2767 {
   2768    POST_MEM_WRITE( ARG1, sizeof(vki_old_sigset_t) ) ;
   2769 }
   2770 
   2771 // This syscall is not used on amd64/Linux -- it only provides
   2772 // sys_rt_sigprocmask, which uses sigset_t rather than old_sigset_t.
   2773 // This wrapper is only suitable for 32-bit architectures.
   2774 // (XXX: so how is it that PRE(sys_sigpending) above doesn't need
   2775 // conditional compilation like this?)
   2776 #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_arm_linux)
   2777 PRE(sys_sigprocmask)
   2778 {
   2779    vki_old_sigset_t* set;
   2780    vki_old_sigset_t* oldset;
   2781    vki_sigset_t bigger_set;
   2782    vki_sigset_t bigger_oldset;
   2783 
   2784    PRINT("sys_sigprocmask ( %ld, %#lx, %#lx )",ARG1,ARG2,ARG3);
   2785    PRE_REG_READ3(long, "sigprocmask",
   2786                  int, how, vki_old_sigset_t *, set, vki_old_sigset_t *, oldset);
   2787    if (ARG2 != 0)
   2788       PRE_MEM_READ( "sigprocmask(set)", ARG2, sizeof(vki_old_sigset_t));
   2789    if (ARG3 != 0)
   2790       PRE_MEM_WRITE( "sigprocmask(oldset)", ARG3, sizeof(vki_old_sigset_t));
   2791 
   2792    // Nb: We must convert the smaller vki_old_sigset_t params into bigger
   2793    // vki_sigset_t params.
   2794    set    = (vki_old_sigset_t*)ARG2;
   2795    oldset = (vki_old_sigset_t*)ARG3;
   2796 
   2797    VG_(memset)(&bigger_set,    0, sizeof(vki_sigset_t));
   2798    VG_(memset)(&bigger_oldset, 0, sizeof(vki_sigset_t));
   2799    if (set)
   2800       bigger_set.sig[0] = *(vki_old_sigset_t*)set;
   2801 
   2802    SET_STATUS_from_SysRes(
   2803       VG_(do_sys_sigprocmask) ( tid, ARG1 /*how*/,
   2804                                 set ? &bigger_set    : NULL,
   2805                              oldset ? &bigger_oldset : NULL)
   2806    );
   2807 
   2808    if (oldset)
   2809       *oldset = bigger_oldset.sig[0];
   2810 
   2811    if (SUCCESS)
   2812       *flags |= SfPollAfter;
   2813 }
   2814 POST(sys_sigprocmask)
   2815 {
   2816    vg_assert(SUCCESS);
   2817    if (RES == 0 && ARG3 != 0)
   2818       POST_MEM_WRITE( ARG3, sizeof(vki_old_sigset_t));
   2819 }
   2820 
   2821 /* Convert from non-RT to RT sigset_t's */
   2822 static
   2823 void convert_sigset_to_rt(const vki_old_sigset_t *oldset, vki_sigset_t *set)
   2824 {
   2825    VG_(sigemptyset)(set);
   2826    set->sig[0] = *oldset;
   2827 }
   2828 PRE(sys_sigaction)
   2829 {
   2830    vki_sigaction_toK_t   new, *newp;
   2831    vki_sigaction_fromK_t old, *oldp;
   2832 
   2833    PRINT("sys_sigaction ( %ld, %#lx, %#lx )", ARG1,ARG2,ARG3);
   2834    PRE_REG_READ3(int, "sigaction",
   2835                  int, signum, const struct old_sigaction *, act,
   2836                  struct old_sigaction *, oldact);
   2837 
   2838    newp = oldp = NULL;
   2839 
   2840    if (ARG2 != 0) {
   2841       struct vki_old_sigaction *sa = (struct vki_old_sigaction *)ARG2;
   2842       PRE_MEM_READ( "sigaction(act->sa_handler)", (Addr)&sa->ksa_handler, sizeof(sa->ksa_handler));
   2843       PRE_MEM_READ( "sigaction(act->sa_mask)", (Addr)&sa->sa_mask, sizeof(sa->sa_mask));
   2844       PRE_MEM_READ( "sigaction(act->sa_flags)", (Addr)&sa->sa_flags, sizeof(sa->sa_flags));
   2845       if (ML_(safe_to_deref)(sa,sizeof(sa))
   2846           && (sa->sa_flags & VKI_SA_RESTORER))
   2847          PRE_MEM_READ( "sigaction(act->sa_restorer)", (Addr)&sa->sa_restorer, sizeof(sa->sa_restorer));
   2848    }
   2849 
   2850    if (ARG3 != 0) {
   2851       PRE_MEM_WRITE( "sigaction(oldact)", ARG3, sizeof(struct vki_old_sigaction));
   2852       oldp = &old;
   2853    }
   2854 
   2855    if (ARG2 != 0) {
   2856       struct vki_old_sigaction *oldnew = (struct vki_old_sigaction *)ARG2;
   2857 
   2858       new.ksa_handler = oldnew->ksa_handler;
   2859       new.sa_flags = oldnew->sa_flags;
   2860       new.sa_restorer = oldnew->sa_restorer;
   2861       convert_sigset_to_rt(&oldnew->sa_mask, &new.sa_mask);
   2862       newp = &new;
   2863    }
   2864 
   2865    SET_STATUS_from_SysRes( VG_(do_sys_sigaction)(ARG1, newp, oldp) );
   2866 
   2867    if (ARG3 != 0 && SUCCESS && RES == 0) {
   2868       struct vki_old_sigaction *oldold = (struct vki_old_sigaction *)ARG3;
   2869 
   2870       oldold->ksa_handler = oldp->ksa_handler;
   2871       oldold->sa_flags = oldp->sa_flags;
   2872       oldold->sa_restorer = oldp->sa_restorer;
   2873       oldold->sa_mask = oldp->sa_mask.sig[0];
   2874    }
   2875 }
   2876 POST(sys_sigaction)
   2877 {
   2878    vg_assert(SUCCESS);
   2879    if (RES == 0 && ARG3 != 0)
   2880       POST_MEM_WRITE( ARG3, sizeof(struct vki_old_sigaction));
   2881 }
   2882 #endif
   2883 
   2884 PRE(sys_signalfd)
   2885 {
   2886    PRINT("sys_signalfd ( %d, %#lx, %llu )", (Int)ARG1,ARG2,(ULong)ARG3);
   2887    PRE_REG_READ3(long, "sys_signalfd",
   2888                  int, fd, vki_sigset_t *, sigmask, vki_size_t, sigsetsize);
   2889    PRE_MEM_READ( "signalfd(sigmask)", ARG2, sizeof(vki_sigset_t) );
   2890    if ((int)ARG1 != -1 && !ML_(fd_allowed)(ARG1, "signalfd", tid, False))
   2891       SET_STATUS_Failure( VKI_EBADF );
   2892 }
   2893 POST(sys_signalfd)
   2894 {
   2895    if (!ML_(fd_allowed)(RES, "signalfd", tid, True)) {
   2896       VG_(close)(RES);
   2897       SET_STATUS_Failure( VKI_EMFILE );
   2898    } else {
   2899       if (VG_(clo_track_fds))
   2900          ML_(record_fd_open_nameless) (tid, RES);
   2901    }
   2902 }
   2903 
   2904 PRE(sys_signalfd4)
   2905 {
   2906    PRINT("sys_signalfd4 ( %d, %#lx, %llu, %ld )", (Int)ARG1,ARG2,(ULong)ARG3,ARG4);
   2907    PRE_REG_READ4(long, "sys_signalfd4",
   2908                  int, fd, vki_sigset_t *, sigmask, vki_size_t, sigsetsize, int, flags);
   2909    PRE_MEM_READ( "signalfd(sigmask)", ARG2, sizeof(vki_sigset_t) );
   2910    if ((int)ARG1 != -1 && !ML_(fd_allowed)(ARG1, "signalfd", tid, False))
   2911       SET_STATUS_Failure( VKI_EBADF );
   2912 }
   2913 POST(sys_signalfd4)
   2914 {
   2915    if (!ML_(fd_allowed)(RES, "signalfd4", tid, True)) {
   2916       VG_(close)(RES);
   2917       SET_STATUS_Failure( VKI_EMFILE );
   2918    } else {
   2919       if (VG_(clo_track_fds))
   2920          ML_(record_fd_open_nameless) (tid, RES);
   2921    }
   2922 }
   2923 
   2924 
   2925 /* ---------------------------------------------------------------------
   2926    rt_sig* wrappers
   2927    ------------------------------------------------------------------ */
   2928 
   2929 PRE(sys_rt_sigaction)
   2930 {
   2931    PRINT("sys_rt_sigaction ( %ld, %#lx, %#lx, %ld )", ARG1,ARG2,ARG3,ARG4);
   2932    PRE_REG_READ4(long, "rt_sigaction",
   2933                  int, signum, const struct sigaction *, act,
   2934                  struct sigaction *, oldact, vki_size_t, sigsetsize);
   2935 
   2936    if (ARG2 != 0) {
   2937       vki_sigaction_toK_t *sa = (vki_sigaction_toK_t *)ARG2;
   2938       PRE_MEM_READ( "rt_sigaction(act->sa_handler)", (Addr)&sa->ksa_handler, sizeof(sa->ksa_handler));
   2939       PRE_MEM_READ( "rt_sigaction(act->sa_mask)", (Addr)&sa->sa_mask, sizeof(sa->sa_mask));
   2940       PRE_MEM_READ( "rt_sigaction(act->sa_flags)", (Addr)&sa->sa_flags, sizeof(sa->sa_flags));
   2941       if (sa->sa_flags & VKI_SA_RESTORER)
   2942          PRE_MEM_READ( "rt_sigaction(act->sa_restorer)", (Addr)&sa->sa_restorer, sizeof(sa->sa_restorer));
   2943    }
   2944    if (ARG3 != 0)
   2945       PRE_MEM_WRITE( "rt_sigaction(oldact)", ARG3, sizeof(vki_sigaction_fromK_t));
   2946 
   2947    // XXX: doesn't seem right to be calling do_sys_sigaction for
   2948    // sys_rt_sigaction... perhaps this function should be renamed
   2949    // VG_(do_sys_rt_sigaction)()  --njn
   2950 
   2951    SET_STATUS_from_SysRes(
   2952       VG_(do_sys_sigaction)(ARG1, (const vki_sigaction_toK_t *)ARG2,
   2953                             (vki_sigaction_fromK_t *)ARG3)
   2954    );
   2955 }
   2956 POST(sys_rt_sigaction)
   2957 {
   2958    vg_assert(SUCCESS);
   2959    if (RES == 0 && ARG3 != 0)
   2960       POST_MEM_WRITE( ARG3, sizeof(vki_sigaction_fromK_t));
   2961 }
   2962 
   2963 PRE(sys_rt_sigprocmask)
   2964 {
   2965    PRINT("sys_rt_sigprocmask ( %ld, %#lx, %#lx, %llu )",ARG1,ARG2,ARG3,(ULong)ARG4);
   2966    PRE_REG_READ4(long, "rt_sigprocmask",
   2967                  int, how, vki_sigset_t *, set, vki_sigset_t *, oldset,
   2968                  vki_size_t, sigsetsize);
   2969    if (ARG2 != 0)
   2970       PRE_MEM_READ( "rt_sigprocmask(set)", ARG2, sizeof(vki_sigset_t));
   2971    if (ARG3 != 0)
   2972       PRE_MEM_WRITE( "rt_sigprocmask(oldset)", ARG3, sizeof(vki_sigset_t));
   2973 
   2974    // Like the kernel, we fail if the sigsetsize is not exactly what we expect.
   2975    if (sizeof(vki_sigset_t) != ARG4)
   2976       SET_STATUS_Failure( VKI_EMFILE );
   2977    else {
   2978       SET_STATUS_from_SysRes(
   2979                   VG_(do_sys_sigprocmask) ( tid, ARG1 /*how*/,
   2980                                             (vki_sigset_t*) ARG2,
   2981                                             (vki_sigset_t*) ARG3 )
   2982       );
   2983    }
   2984 
   2985    if (SUCCESS)
   2986       *flags |= SfPollAfter;
   2987 }
   2988 POST(sys_rt_sigprocmask)
   2989 {
   2990    vg_assert(SUCCESS);
   2991    if (RES == 0 && ARG3 != 0)
   2992       POST_MEM_WRITE( ARG3, sizeof(vki_sigset_t));
   2993 }
   2994 
   2995 PRE(sys_rt_sigpending)
   2996 {
   2997    PRINT( "sys_rt_sigpending ( %#lx )", ARG1 );
   2998    PRE_REG_READ2(long, "rt_sigpending",
   2999                  vki_sigset_t *, set, vki_size_t, sigsetsize);
   3000    PRE_MEM_WRITE( "rt_sigpending(set)", ARG1, sizeof(vki_sigset_t));
   3001 }
   3002 POST(sys_rt_sigpending)
   3003 {
   3004    POST_MEM_WRITE( ARG1, sizeof(vki_sigset_t) ) ;
   3005 }
   3006 
   3007 PRE(sys_rt_sigtimedwait)
   3008 {
   3009    *flags |= SfMayBlock;
   3010    PRINT("sys_rt_sigtimedwait ( %#lx, %#lx, %#lx, %lld )",
   3011          ARG1,ARG2,ARG3,(ULong)ARG4);
   3012    PRE_REG_READ4(long, "rt_sigtimedwait",
   3013                  const vki_sigset_t *, set, vki_siginfo_t *, info,
   3014                  const struct timespec *, timeout, vki_size_t, sigsetsize);
   3015    if (ARG1 != 0)
   3016       PRE_MEM_READ(  "rt_sigtimedwait(set)",  ARG1, sizeof(vki_sigset_t));
   3017    if (ARG2 != 0)
   3018       PRE_MEM_WRITE( "rt_sigtimedwait(info)", ARG2, sizeof(vki_siginfo_t) );
   3019    if (ARG3 != 0)
   3020       PRE_MEM_READ( "rt_sigtimedwait(timeout)",
   3021                     ARG3, sizeof(struct vki_timespec) );
   3022 }
   3023 POST(sys_rt_sigtimedwait)
   3024 {
   3025    if (ARG2 != 0)
   3026       POST_MEM_WRITE( ARG2, sizeof(vki_siginfo_t) );
   3027 }
   3028 
   3029 PRE(sys_rt_sigqueueinfo)
   3030 {
   3031    PRINT("sys_rt_sigqueueinfo(%ld, %ld, %#lx)", ARG1, ARG2, ARG3);
   3032    PRE_REG_READ3(long, "rt_sigqueueinfo",
   3033                  int, pid, int, sig, vki_siginfo_t *, uinfo);
   3034    if (ARG2 != 0)
   3035       PRE_MEM_READ( "rt_sigqueueinfo(uinfo)", ARG3, VKI_SI_MAX_SIZE );
   3036 }
   3037 POST(sys_rt_sigqueueinfo)
   3038 {
   3039    if (!ML_(client_signal_OK)(ARG2))
   3040       SET_STATUS_Failure( VKI_EINVAL );
   3041 }
   3042 
   3043 PRE(sys_rt_tgsigqueueinfo)
   3044 {
   3045    PRINT("sys_rt_tgsigqueueinfo(%ld, %ld, %ld, %#lx)", ARG1, ARG2, ARG3, ARG4);
   3046    PRE_REG_READ4(long, "rt_tgsigqueueinfo",
   3047                  int, tgid, int, pid, int, sig, vki_siginfo_t *, uinfo);
   3048    if (ARG3 != 0)
   3049       PRE_MEM_READ( "rt_tgsigqueueinfo(uinfo)", ARG4, VKI_SI_MAX_SIZE );
   3050 }
   3051 
   3052 POST(sys_rt_tgsigqueueinfo)
   3053 {
   3054    if (!ML_(client_signal_OK)(ARG3))
   3055       SET_STATUS_Failure( VKI_EINVAL );
   3056 }
   3057 
   3058 // XXX: x86-specific?  The kernel prototypes for the different archs are
   3059 //      hard to decipher.
   3060 PRE(sys_rt_sigsuspend)
   3061 {
   3062    /* The C library interface to sigsuspend just takes a pointer to
   3063       a signal mask but this system call has two arguments - a pointer
   3064       to the mask and the number of bytes used by it. The kernel insists
   3065       on the size being equal to sizeof(sigset_t) however and will just
   3066       return EINVAL if it isn't.
   3067     */
   3068    *flags |= SfMayBlock;
   3069    PRINT("sys_rt_sigsuspend ( %#lx, %ld )", ARG1,ARG2 );
   3070    PRE_REG_READ2(int, "rt_sigsuspend", vki_sigset_t *, mask, vki_size_t, size)
   3071    if (ARG1 != (Addr)NULL) {
   3072       PRE_MEM_READ( "rt_sigsuspend(mask)", ARG1, sizeof(vki_sigset_t) );
   3073    }
   3074 }
   3075 
   3076 /* ---------------------------------------------------------------------
   3077    linux msg* wrapper helpers
   3078    ------------------------------------------------------------------ */
   3079 
   3080 void
   3081 ML_(linux_PRE_sys_msgsnd) ( ThreadId tid,
   3082                             UWord arg0, UWord arg1, UWord arg2, UWord arg3 )
   3083 {
   3084    /* int msgsnd(int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg); */
   3085    struct vki_msgbuf *msgp = (struct vki_msgbuf *)arg1;
   3086    PRE_MEM_READ( "msgsnd(msgp->mtype)", (Addr)&msgp->mtype, sizeof(msgp->mtype) );
   3087    PRE_MEM_READ( "msgsnd(msgp->mtext)", (Addr)&msgp->mtext, arg2 );
   3088 }
   3089 
   3090 void
   3091 ML_(linux_PRE_sys_msgrcv) ( ThreadId tid,
   3092                             UWord arg0, UWord arg1, UWord arg2,
   3093                             UWord arg3, UWord arg4 )
   3094 {
   3095    /* ssize_t msgrcv(int msqid, struct msgbuf *msgp, size_t msgsz,
   3096                      long msgtyp, int msgflg); */
   3097    struct vki_msgbuf *msgp = (struct vki_msgbuf *)arg1;
   3098    PRE_MEM_WRITE( "msgrcv(msgp->mtype)", (Addr)&msgp->mtype, sizeof(msgp->mtype) );
   3099    PRE_MEM_WRITE( "msgrcv(msgp->mtext)", (Addr)&msgp->mtext, arg2 );
   3100 }
   3101 void
   3102 ML_(linux_POST_sys_msgrcv) ( ThreadId tid,
   3103                              UWord res,
   3104                              UWord arg0, UWord arg1, UWord arg2,
   3105                              UWord arg3, UWord arg4 )
   3106 {
   3107    struct vki_msgbuf *msgp = (struct vki_msgbuf *)arg1;
   3108    POST_MEM_WRITE( (Addr)&msgp->mtype, sizeof(msgp->mtype) );
   3109    POST_MEM_WRITE( (Addr)&msgp->mtext, res );
   3110 }
   3111 
   3112 void
   3113 ML_(linux_PRE_sys_msgctl) ( ThreadId tid,
   3114                             UWord arg0, UWord arg1, UWord arg2 )
   3115 {
   3116    /* int msgctl(int msqid, int cmd, struct msqid_ds *buf); */
   3117    switch (arg1 /* cmd */) {
   3118    case VKI_IPC_INFO:
   3119    case VKI_MSG_INFO:
   3120    case VKI_IPC_INFO|VKI_IPC_64:
   3121    case VKI_MSG_INFO|VKI_IPC_64:
   3122       PRE_MEM_WRITE( "msgctl(IPC_INFO, buf)",
   3123                      arg2, sizeof(struct vki_msginfo) );
   3124       break;
   3125    case VKI_IPC_STAT:
   3126    case VKI_MSG_STAT:
   3127       PRE_MEM_WRITE( "msgctl(IPC_STAT, buf)",
   3128                      arg2, sizeof(struct vki_msqid_ds) );
   3129       break;
   3130    case VKI_IPC_STAT|VKI_IPC_64:
   3131    case VKI_MSG_STAT|VKI_IPC_64:
   3132       PRE_MEM_WRITE( "msgctl(IPC_STAT, arg.buf)",
   3133                      arg2, sizeof(struct vki_msqid64_ds) );
   3134       break;
   3135    case VKI_IPC_SET:
   3136       PRE_MEM_READ( "msgctl(IPC_SET, arg.buf)",
   3137                     arg2, sizeof(struct vki_msqid_ds) );
   3138       break;
   3139    case VKI_IPC_SET|VKI_IPC_64:
   3140       PRE_MEM_READ( "msgctl(IPC_SET, arg.buf)",
   3141                     arg2, sizeof(struct vki_msqid64_ds) );
   3142       break;
   3143    }
   3144 }
   3145 void
   3146 ML_(linux_POST_sys_msgctl) ( ThreadId tid,
   3147                              UWord res,
   3148                              UWord arg0, UWord arg1, UWord arg2 )
   3149 {
   3150    switch (arg1 /* cmd */) {
   3151    case VKI_IPC_INFO:
   3152    case VKI_MSG_INFO:
   3153    case VKI_IPC_INFO|VKI_IPC_64:
   3154    case VKI_MSG_INFO|VKI_IPC_64:
   3155       POST_MEM_WRITE( arg2, sizeof(struct vki_msginfo) );
   3156       break;
   3157    case VKI_IPC_STAT:
   3158    case VKI_MSG_STAT:
   3159       POST_MEM_WRITE( arg2, sizeof(struct vki_msqid_ds) );
   3160       break;
   3161    case VKI_IPC_STAT|VKI_IPC_64:
   3162    case VKI_MSG_STAT|VKI_IPC_64:
   3163       POST_MEM_WRITE( arg2, sizeof(struct vki_msqid64_ds) );
   3164       break;
   3165    }
   3166 }
   3167 
   3168 /* ---------------------------------------------------------------------
   3169    *at wrappers
   3170    ------------------------------------------------------------------ */
   3171 
   3172 PRE(sys_openat)
   3173 {
   3174    HChar  name[30];
   3175    SysRes sres;
   3176 
   3177    if (ARG3 & VKI_O_CREAT) {
   3178       // 4-arg version
   3179       PRINT("sys_openat ( %ld, %#lx(%s), %ld, %ld )",ARG1,ARG2,(char*)ARG2,ARG3,ARG4);
   3180       PRE_REG_READ4(long, "openat",
   3181                     int, dfd, const char *, filename, int, flags, int, mode);
   3182    } else {
   3183       // 3-arg version
   3184       PRINT("sys_openat ( %ld, %#lx(%s), %ld )",ARG1,ARG2,(char*)ARG2,ARG3);
   3185       PRE_REG_READ3(long, "openat",
   3186                     int, dfd, const char *, filename, int, flags);
   3187    }
   3188 
   3189    if (ARG1 != VKI_AT_FDCWD && !ML_(fd_allowed)(ARG1, "openat", tid, False))
   3190       SET_STATUS_Failure( VKI_EBADF );
   3191    else
   3192       PRE_MEM_RASCIIZ( "openat(filename)", ARG2 );
   3193 
   3194    /* Handle the case where the open is of /proc/self/cmdline or
   3195       /proc/<pid>/cmdline, and just give it a copy of the fd for the
   3196       fake file we cooked up at startup (in m_main).  Also, seek the
   3197       cloned fd back to the start. */
   3198 
   3199    VG_(sprintf)(name, "/proc/%d/cmdline", VG_(getpid)());
   3200    if (ML_(safe_to_deref)( (void*)ARG2, 1 )
   3201        && (VG_(strcmp)((Char *)ARG2, name) == 0
   3202            || VG_(strcmp)((Char *)ARG2, "/proc/self/cmdline") == 0)) {
   3203       sres = VG_(dup)( VG_(cl_cmdline_fd) );
   3204       SET_STATUS_from_SysRes( sres );
   3205       if (!sr_isError(sres)) {
   3206          OffT off = VG_(lseek)( sr_Res(sres), 0, VKI_SEEK_SET );
   3207          if (off < 0)
   3208             SET_STATUS_Failure( VKI_EMFILE );
   3209       }
   3210       return;
   3211    }
   3212 
   3213    /* Otherwise handle normally */
   3214    *flags |= SfMayBlock;
   3215 }
   3216 
   3217 POST(sys_openat)
   3218 {
   3219    vg_assert(SUCCESS);
   3220    if (!ML_(fd_allowed)(RES, "openat", tid, True)) {
   3221       VG_(close)(RES);
   3222       SET_STATUS_Failure( VKI_EMFILE );
   3223    } else {
   3224       if (VG_(clo_track_fds))
   3225          ML_(record_fd_open_with_given_name)(tid, RES, (Char*)ARG2);
   3226    }
   3227 }
   3228 
   3229 PRE(sys_mkdirat)
   3230 {
   3231    *flags |= SfMayBlock;
   3232    PRINT("sys_mkdirat ( %ld, %#lx(%s), %ld )", ARG1,ARG2,(char*)ARG2,ARG3);
   3233    PRE_REG_READ3(long, "mkdirat",
   3234                  int, dfd, const char *, pathname, int, mode);
   3235    PRE_MEM_RASCIIZ( "mkdirat(pathname)", ARG2 );
   3236 }
   3237 
   3238 PRE(sys_mknodat)
   3239 {
   3240   PRINT("sys_mknodat ( %ld, %#lx(%s), 0x%lx, 0x%lx )", ARG1,ARG2,(char*)ARG2,ARG3,ARG4 );
   3241    PRE_REG_READ4(long, "mknodat",
   3242                  int, dfd, const char *, pathname, int, mode, unsigned, dev);
   3243    PRE_MEM_RASCIIZ( "mknodat(pathname)", ARG2 );
   3244 }
   3245 
   3246 PRE(sys_fchownat)
   3247 {
   3248    PRINT("sys_fchownat ( %ld, %#lx(%s), 0x%lx, 0x%lx )", ARG1,ARG2,(char*)ARG2,ARG3,ARG4);
   3249    PRE_REG_READ4(long, "fchownat",
   3250                  int, dfd, const char *, path,
   3251                  vki_uid_t, owner, vki_gid_t, group);
   3252    PRE_MEM_RASCIIZ( "fchownat(path)", ARG2 );
   3253 }
   3254 
   3255 PRE(sys_futimesat)
   3256 {
   3257    PRINT("sys_futimesat ( %ld, %#lx(%s), %#lx )", ARG1,ARG2,(char*)ARG2,ARG3);
   3258    PRE_REG_READ3(long, "futimesat",
   3259                  int, dfd, char *, filename, struct timeval *, tvp);
   3260    if (ARG2 != 0)
   3261       PRE_MEM_RASCIIZ( "futimesat(filename)", ARG2 );
   3262    if (ARG3 != 0)
   3263       PRE_MEM_READ( "futimesat(tvp)", ARG3, 2 * sizeof(struct vki_timeval) );
   3264 }
   3265 
   3266 PRE(sys_utimensat)
   3267 {
   3268    PRINT("sys_utimensat ( %ld, %#lx(%s), %#lx, 0x%lx )", ARG1,ARG2,(char*)ARG2,ARG3,ARG4);
   3269    PRE_REG_READ4(long, "utimensat",
   3270                  int, dfd, char *, filename, struct timespec *, utimes, int, flags);
   3271    if (ARG2 != 0)
   3272       PRE_MEM_RASCIIZ( "utimensat(filename)", ARG2 );
   3273    if (ARG3 != 0)
   3274       PRE_MEM_READ( "utimensat(tvp)", ARG3, 2 * sizeof(struct vki_timespec) );
   3275 }
   3276 
   3277 PRE(sys_newfstatat)
   3278 {
   3279    FUSE_COMPATIBLE_MAY_BLOCK();
   3280    PRINT("sys_newfstatat ( %ld, %#lx(%s), %#lx )", ARG1,ARG2,(char*)ARG2,ARG3);
   3281    PRE_REG_READ3(long, "fstatat",
   3282                  int, dfd, char *, file_name, struct stat *, buf);
   3283    PRE_MEM_RASCIIZ( "fstatat(file_name)", ARG2 );
   3284    PRE_MEM_WRITE( "fstatat(buf)", ARG3, sizeof(struct vki_stat) );
   3285 }
   3286 
   3287 POST(sys_newfstatat)
   3288 {
   3289    POST_MEM_WRITE( ARG3, sizeof(struct vki_stat) );
   3290 }
   3291 
   3292 PRE(sys_unlinkat)
   3293 {
   3294    *flags |= SfMayBlock;
   3295    PRINT("sys_unlinkat ( %ld, %#lx(%s) )", ARG1,ARG2,(char*)ARG2);
   3296    PRE_REG_READ2(long, "unlinkat", int, dfd, const char *, pathname);
   3297    PRE_MEM_RASCIIZ( "unlinkat(pathname)", ARG2 );
   3298 }
   3299 
   3300 PRE(sys_renameat)
   3301 {
   3302    PRINT("sys_renameat ( %ld, %#lx(%s), %ld, %#lx(%s) )", ARG1,ARG2,(char*)ARG2,ARG3,ARG4,(char*)ARG4);
   3303    PRE_REG_READ4(long, "renameat",
   3304                  int, olddfd, const char *, oldpath,
   3305                  int, newdfd, const char *, newpath);
   3306    PRE_MEM_RASCIIZ( "renameat(oldpath)", ARG2 );
   3307    PRE_MEM_RASCIIZ( "renameat(newpath)", ARG4 );
   3308 }
   3309 
   3310 PRE(sys_linkat)
   3311 {
   3312    *flags |= SfMayBlock;
   3313    PRINT("sys_linkat ( %ld, %#lx(%s), %ld, %#lx(%s), %ld )",ARG1,ARG2,(char*)ARG2,ARG3,ARG4,(char*)ARG4,ARG5);
   3314    PRE_REG_READ5(long, "linkat",
   3315                  int, olddfd, const char *, oldpath,
   3316                  int, newdfd, const char *, newpath,
   3317                  int, flags);
   3318    PRE_MEM_RASCIIZ( "linkat(oldpath)", ARG2);
   3319    PRE_MEM_RASCIIZ( "linkat(newpath)", ARG4);
   3320 }
   3321 
   3322 PRE(sys_symlinkat)
   3323 {
   3324    *flags |= SfMayBlock;
   3325    PRINT("sys_symlinkat ( %#lx(%s), %ld, %#lx(%s) )",ARG1,(char*)ARG1,ARG2,ARG3,(char*)ARG3);
   3326    PRE_REG_READ3(long, "symlinkat",
   3327                  const char *, oldpath, int, newdfd, const char *, newpath);
   3328    PRE_MEM_RASCIIZ( "symlinkat(oldpath)", ARG1 );
   3329    PRE_MEM_RASCIIZ( "symlinkat(newpath)", ARG3 );
   3330 }
   3331 
   3332 PRE(sys_readlinkat)
   3333 {
   3334    HChar name[25];
   3335    Word  saved = SYSNO;
   3336 
   3337    PRINT("sys_readlinkat ( %ld, %#lx(%s), %#lx, %llu )", ARG1,ARG2,(char*)ARG2,ARG3,(ULong)ARG4);
   3338    PRE_REG_READ4(long, "readlinkat",
   3339                  int, dfd, const char *, path, char *, buf, int, bufsiz);
   3340    PRE_MEM_RASCIIZ( "readlinkat(path)", ARG2 );
   3341    PRE_MEM_WRITE( "readlinkat(buf)", ARG3,ARG4 );
   3342 
   3343    /*
   3344     * Handle the case where readlinkat is looking at /proc/self/exe or
   3345     * /proc/<pid>/exe.
   3346     */
   3347    VG_(sprintf)(name, "/proc/%d/exe", VG_(getpid)());
   3348    if (ML_(safe_to_deref)((void*)ARG2, 1)
   3349        && (VG_(strcmp)((Char *)ARG2, name) == 0
   3350            || VG_(strcmp)((Char *)ARG2, "/proc/self/exe") == 0)) {
   3351       VG_(sprintf)(name, "/proc/self/fd/%d", VG_(cl_exec_fd));
   3352       SET_STATUS_from_SysRes( VG_(do_syscall4)(saved, ARG1, (UWord)name,
   3353                                                       ARG3, ARG4));
   3354    } else {
   3355       /* Normal case */
   3356       SET_STATUS_from_SysRes( VG_(do_syscall4)(saved, ARG1, ARG2, ARG3, ARG4));
   3357    }
   3358 
   3359    if (SUCCESS && RES > 0)
   3360       POST_MEM_WRITE( ARG3, RES );
   3361 }
   3362 
   3363 PRE(sys_fchmodat)
   3364 {
   3365    PRINT("sys_fchmodat ( %ld, %#lx(%s), %ld )", ARG1,ARG2,(char*)ARG2,ARG3);
   3366    PRE_REG_READ3(long, "fchmodat",
   3367                  int, dfd, const char *, path, vki_mode_t, mode);
   3368    PRE_MEM_RASCIIZ( "fchmodat(path)", ARG2 );
   3369 }
   3370 
   3371 PRE(sys_faccessat)
   3372 {
   3373    PRINT("sys_faccessat ( %ld, %#lx(%s), %ld )", ARG1,ARG2,(char*)ARG2,ARG3);
   3374    PRE_REG_READ3(long, "faccessat",
   3375                  int, dfd, const char *, pathname, int, mode);
   3376    PRE_MEM_RASCIIZ( "faccessat(pathname)", ARG2 );
   3377 }
   3378 
   3379 /* ---------------------------------------------------------------------
   3380    p{read,write}v wrappers
   3381    ------------------------------------------------------------------ */
   3382 
   3383 PRE(sys_preadv)
   3384 {
   3385    Int i;
   3386    struct vki_iovec * vec;
   3387    *flags |= SfMayBlock;
   3388 #if VG_WORDSIZE == 4
   3389    /* Note that the offset argument here is in lo+hi order on both
   3390       big and little endian platforms... */
   3391    PRINT("sys_preadv ( %ld, %#lx, %llu, %lld )",ARG1,ARG2,(ULong)ARG3,LOHI64(ARG4,ARG5));
   3392    PRE_REG_READ5(ssize_t, "preadv",
   3393                  unsigned long, fd, const struct iovec *, vector,
   3394                  unsigned long, count, vki_u32, offset_low,
   3395                  vki_u32, offset_high);
   3396 #elif VG_WORDSIZE == 8
   3397    PRINT("sys_preadv ( %ld, %#lx, %llu, %lld )",ARG1,ARG2,(ULong)ARG3,(Long)ARG4);
   3398    PRE_REG_READ4(ssize_t, "preadv",
   3399                  unsigned long, fd, const struct iovec *, vector,
   3400                  unsigned long, count, Word, offset);
   3401 #else
   3402 #  error Unexpected word size
   3403 #endif
   3404    if (!ML_(fd_allowed)(ARG1, "preadv", tid, False)) {
   3405       SET_STATUS_Failure( VKI_EBADF );
   3406    } else {
   3407       PRE_MEM_READ( "preadv(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) );
   3408 
   3409       if (ARG2 != 0) {
   3410          /* ToDo: don't do any of the following if the vector is invalid */
   3411          vec = (struct vki_iovec *)ARG2;
   3412          for (i = 0; i < (Int)ARG3; i++)
   3413             PRE_MEM_WRITE( "preadv(vector[...])",
   3414                            (Addr)vec[i].iov_base, vec[i].iov_len );
   3415       }
   3416    }
   3417 }
   3418 
   3419 POST(sys_preadv)
   3420 {
   3421    vg_assert(SUCCESS);
   3422    if (RES > 0) {
   3423       Int i;
   3424       struct vki_iovec * vec = (struct vki_iovec *)ARG2;
   3425       Int remains = RES;
   3426 
   3427       /* RES holds the number of bytes read. */
   3428       for (i = 0; i < (Int)ARG3; i++) {
   3429 	 Int nReadThisBuf = vec[i].iov_len;
   3430 	 if (nReadThisBuf > remains) nReadThisBuf = remains;
   3431 	 POST_MEM_WRITE( (Addr)vec[i].iov_base, nReadThisBuf );
   3432 	 remains -= nReadThisBuf;
   3433 	 if (remains < 0) VG_(core_panic)("preadv: remains < 0");
   3434       }
   3435    }
   3436 }
   3437 
   3438 PRE(sys_pwritev)
   3439 {
   3440    Int i;
   3441    struct vki_iovec * vec;
   3442    *flags |= SfMayBlock;
   3443 #if VG_WORDSIZE == 4
   3444    /* Note that the offset argument here is in lo+hi order on both
   3445       big and little endian platforms... */
   3446    PRINT("sys_pwritev ( %ld, %#lx, %llu, %lld )",ARG1,ARG2,(ULong)ARG3,LOHI64(ARG4,ARG5));
   3447    PRE_REG_READ5(ssize_t, "pwritev",
   3448                  unsigned long, fd, const struct iovec *, vector,
   3449                  unsigned long, count, vki_u32, offset_low,
   3450                  vki_u32, offset_high);
   3451 #elif VG_WORDSIZE == 8
   3452    PRINT("sys_pwritev ( %ld, %#lx, %llu, %lld )",ARG1,ARG2,(ULong)ARG3,(Long)ARG4);
   3453    PRE_REG_READ4(ssize_t, "pwritev",
   3454                  unsigned long, fd, const struct iovec *, vector,
   3455                  unsigned long, count, Word, offset);
   3456 #else
   3457 #  error Unexpected word size
   3458 #endif
   3459    if (!ML_(fd_allowed)(ARG1, "pwritev", tid, False)) {
   3460       SET_STATUS_Failure( VKI_EBADF );
   3461    } else {
   3462       PRE_MEM_READ( "pwritev(vector)",
   3463 		     ARG2, ARG3 * sizeof(struct vki_iovec) );
   3464       if (ARG2 != 0) {
   3465          /* ToDo: don't do any of the following if the vector is invalid */
   3466          vec = (struct vki_iovec *)ARG2;
   3467          for (i = 0; i < (Int)ARG3; i++)
   3468             PRE_MEM_READ( "pwritev(vector[...])",
   3469                            (Addr)vec[i].iov_base, vec[i].iov_len );
   3470       }
   3471    }
   3472 }
   3473 
   3474 /* ---------------------------------------------------------------------
   3475    key retention service wrappers
   3476    ------------------------------------------------------------------ */
   3477 
   3478 PRE(sys_request_key)
   3479 {
   3480    PRINT("sys_request_key ( %#lx(%s), %#lx(%s), %#lx(%s), %ld )",
   3481          ARG1,(char*)ARG1,ARG2,(char*)ARG2,ARG3,(char*)ARG3,ARG4);
   3482    PRE_REG_READ4(long, "request_key",
   3483                  const char *, type, const char *, description,
   3484                  const char *, callout_info, vki_key_serial_t, keyring);
   3485    PRE_MEM_RASCIIZ( "request_key(type)", ARG1);
   3486    PRE_MEM_RASCIIZ( "request_key(description)", ARG2);
   3487    if (ARG3 != (UWord)NULL)
   3488       PRE_MEM_RASCIIZ( "request_key(callout_info)", ARG3);
   3489 }
   3490 
   3491 PRE(sys_add_key)
   3492 {
   3493    PRINT("sys_add_key ( %#lx(%s), %#lx(%s), %#lx, %ld, %ld )",
   3494          ARG1,(char*)ARG1,ARG2,(char*)ARG2,ARG3,ARG4,ARG5);
   3495    PRE_REG_READ5(long, "add_key",
   3496                  const char *, type, const char *, description,
   3497                  const void *, payload, vki_size_t, plen,
   3498                  vki_key_serial_t, keyring);
   3499    PRE_MEM_RASCIIZ( "add_key(type)", ARG1);
   3500    PRE_MEM_RASCIIZ( "add_key(description)", ARG2);
   3501    if (ARG3 != (UWord)NULL)
   3502       PRE_MEM_READ( "request_key(payload)", ARG3, ARG4);
   3503 }
   3504 
   3505 PRE(sys_keyctl)
   3506 {
   3507    switch (ARG1 /* option */) {
   3508    case VKI_KEYCTL_GET_KEYRING_ID:
   3509       PRINT("sys_keyctl ( KEYCTL_GET_KEYRING_ID, %ld, %ld )", ARG2,ARG3);
   3510       PRE_REG_READ3(long, "keyctl(KEYCTL_GET_KEYRING_ID)",
   3511                     int, option, vki_key_serial_t, id, int, create);
   3512       break;
   3513    case VKI_KEYCTL_JOIN_SESSION_KEYRING:
   3514       PRINT("sys_keyctl ( KEYCTL_JOIN_SESSION_KEYRING, %#lx(%s) )", ARG2,(char*)ARG2);
   3515       PRE_REG_READ2(long, "keyctl(KEYCTL_JOIN_SESSION_KEYRING)",
   3516                     int, option, const char *, name);
   3517       if (ARG2 != (UWord)NULL)
   3518          PRE_MEM_RASCIIZ("keyctl(KEYCTL_JOIN_SESSION_KEYRING, name)", ARG2);
   3519       break;
   3520    case VKI_KEYCTL_UPDATE:
   3521       PRINT("sys_keyctl ( KEYCTL_UPDATE, %ld, %#lx, %ld )", ARG2,ARG3,ARG4);
   3522       PRE_REG_READ4(long, "keyctl(KEYCTL_UPDATE)",
   3523                     int, option, vki_key_serial_t, key,
   3524                     const void *, payload, vki_size_t, plen);
   3525       if (ARG3 != (UWord)NULL)
   3526          PRE_MEM_READ("keyctl(KEYCTL_UPDATE, payload)", ARG3, ARG4);
   3527       break;
   3528    case VKI_KEYCTL_REVOKE:
   3529       PRINT("sys_keyctl ( KEYCTL_REVOKE, %ld )", ARG2);
   3530       PRE_REG_READ2(long, "keyctl(KEYCTL_REVOKE)",
   3531                     int, option, vki_key_serial_t, id);
   3532       break;
   3533    case VKI_KEYCTL_CHOWN:
   3534       PRINT("sys_keyctl ( KEYCTL_CHOWN, %ld, %ld, %ld )", ARG2,ARG3,ARG4);
   3535       PRE_REG_READ4(long, "keyctl(KEYCTL_CHOWN)",
   3536                     int, option, vki_key_serial_t, id,
   3537                     vki_uid_t, uid, vki_gid_t, gid);
   3538       break;
   3539    case VKI_KEYCTL_SETPERM:
   3540       PRINT("sys_keyctl ( KEYCTL_SETPERM, %ld, %ld )", ARG2,ARG3);
   3541       PRE_REG_READ3(long, "keyctl(KEYCTL_SETPERM)",
   3542                     int, option, vki_key_serial_t, id, vki_key_perm_t, perm);
   3543       break;
   3544    case VKI_KEYCTL_DESCRIBE:
   3545       PRINT("sys_keyctl ( KEYCTL_DESCRIBE, %ld, %#lx, %ld )", ARG2,ARG3,ARG4);
   3546       PRE_REG_READ4(long, "keyctl(KEYCTL_DESCRIBE)",
   3547                     int, option, vki_key_serial_t, id,
   3548                     char *, buffer, vki_size_t, buflen);
   3549       if (ARG3 != (UWord)NULL)
   3550          PRE_MEM_WRITE("keyctl(KEYCTL_DESCRIBE, buffer)", ARG3, ARG4);
   3551       break;
   3552    case VKI_KEYCTL_CLEAR:
   3553       PRINT("sys_keyctl ( KEYCTL_CLEAR, %ld )", ARG2);
   3554       PRE_REG_READ2(long, "keyctl(KEYCTL_CLEAR)",
   3555                     int, option, vki_key_serial_t, keyring);
   3556       break;
   3557    case VKI_KEYCTL_LINK:
   3558       PRINT("sys_keyctl ( KEYCTL_LINK, %ld, %ld )", ARG2,ARG3);
   3559       PRE_REG_READ3(long, "keyctl(KEYCTL_LINK)", int, option,
   3560                     vki_key_serial_t, keyring, vki_key_serial_t, key);
   3561       break;
   3562    case VKI_KEYCTL_UNLINK:
   3563       PRINT("sys_keyctl ( KEYCTL_UNLINK, %ld, %ld )", ARG2,ARG3);
   3564       PRE_REG_READ3(long, "keyctl(KEYCTL_UNLINK)", int, option,
   3565                     vki_key_serial_t, keyring, vki_key_serial_t, key);
   3566       break;
   3567    case VKI_KEYCTL_SEARCH:
   3568       PRINT("sys_keyctl ( KEYCTL_SEARCH, %ld, %#lx(%s), %#lx(%s), %ld )",
   3569             ARG2,ARG3,(char*)ARG3,ARG4,(char*)ARG4,ARG5);
   3570       PRE_REG_READ5(long, "keyctl(KEYCTL_SEARCH)",
   3571                     int, option, vki_key_serial_t, keyring,
   3572                     const char *, type, const char *, description,
   3573                     vki_key_serial_t, destring);
   3574       PRE_MEM_RASCIIZ("sys_keyctl(KEYCTL_SEARCH, type)", ARG3);
   3575       PRE_MEM_RASCIIZ("sys_keyctl(KEYCTL_SEARCH, description)", ARG4);
   3576       break;
   3577    case VKI_KEYCTL_READ:
   3578       PRINT("sys_keyctl ( KEYCTL_READ, %ld, %#lx, %ld )", ARG2,ARG3,ARG4);
   3579       PRE_REG_READ4(long, "keyctl(KEYCTL_READ)",
   3580                     int, option, vki_key_serial_t, keyring,
   3581                     char *, buffer, vki_size_t, buflen);
   3582       if (ARG3 != (UWord)NULL)
   3583          PRE_MEM_WRITE("keyctl(KEYCTL_READ, buffer)", ARG3, ARG4);
   3584       break;
   3585    case VKI_KEYCTL_INSTANTIATE:
   3586       PRINT("sys_keyctl ( KEYCTL_INSTANTIATE, %ld, %#lx, %ld, %ld )",
   3587             ARG2,ARG3,ARG4,ARG5);
   3588       PRE_REG_READ5(long, "keyctl(KEYCTL_INSTANTIATE)",
   3589                     int, option, vki_key_serial_t, key,
   3590                     char *, payload, vki_size_t, plen,
   3591                     vki_key_serial_t, keyring);
   3592       if (ARG3 != (UWord)NULL)
   3593          PRE_MEM_READ("keyctl(KEYCTL_INSTANTIATE, payload)", ARG3, ARG4);
   3594       break;
   3595    case VKI_KEYCTL_NEGATE:
   3596       PRINT("sys_keyctl ( KEYCTL_NEGATE, %ld, %lu, %ld )", ARG2,ARG3,ARG4);
   3597       PRE_REG_READ4(long, "keyctl(KEYCTL_NEGATE)",
   3598                     int, option, vki_key_serial_t, key,
   3599                     unsigned, timeout, vki_key_serial_t, keyring);
   3600       break;
   3601    case VKI_KEYCTL_SET_REQKEY_KEYRING:
   3602       PRINT("sys_keyctl ( KEYCTL_SET_REQKEY_KEYRING, %ld )", ARG2);
   3603       PRE_REG_READ2(long, "keyctl(KEYCTL_SET_REQKEY_KEYRING)",
   3604                     int, option, int, reqkey_defl);
   3605       break;
   3606    case VKI_KEYCTL_SET_TIMEOUT:
   3607       PRINT("sys_keyctl ( KEYCTL_SET_TIMEOUT, %ld, %ld )", ARG2,ARG3);
   3608       PRE_REG_READ3(long, "keyctl(KEYCTL_SET_TIMEOUT)",
   3609                     int, option, vki_key_serial_t, key, unsigned, timeout);
   3610       break;
   3611    case VKI_KEYCTL_ASSUME_AUTHORITY:
   3612       PRINT("sys_keyctl ( KEYCTL_ASSUME_AUTHORITY, %ld )", ARG2);
   3613       PRE_REG_READ2(long, "keyctl(KEYCTL_ASSUME_AUTHORITY)",
   3614                     int, option, vki_key_serial_t, key);
   3615       break;
   3616    default:
   3617       PRINT("sys_keyctl ( %ld ) ", ARG1);
   3618       PRE_REG_READ1(long, "keyctl", int, option);
   3619       break;
   3620    }
   3621 }
   3622 
   3623 POST(sys_keyctl)
   3624 {
   3625    vg_assert(SUCCESS);
   3626    switch (ARG1 /* option */) {
   3627    case VKI_KEYCTL_DESCRIBE:
   3628    case VKI_KEYCTL_READ:
   3629       if (RES > ARG4)
   3630          POST_MEM_WRITE(ARG3, ARG4);
   3631       else
   3632          POST_MEM_WRITE(ARG3, RES);
   3633       break;
   3634    default:
   3635       break;
   3636    }
   3637 }
   3638 
   3639 /* ---------------------------------------------------------------------
   3640    ioprio_ wrappers
   3641    ------------------------------------------------------------------ */
   3642 
   3643 PRE(sys_ioprio_set)
   3644 {
   3645    PRINT("sys_ioprio_set ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   3646    PRE_REG_READ3(int, "ioprio_set", int, which, int, who, int, ioprio);
   3647 }
   3648 
   3649 PRE(sys_ioprio_get)
   3650 {
   3651    PRINT("sys_ioprio_get ( %ld, %ld )", ARG1,ARG2);
   3652    PRE_REG_READ2(int, "ioprio_get", int, which, int, who);
   3653 }
   3654 
   3655 /* ---------------------------------------------------------------------
   3656    _module wrappers
   3657    ------------------------------------------------------------------ */
   3658 
   3659 PRE(sys_init_module)
   3660 {
   3661    *flags |= SfMayBlock;
   3662    PRINT("sys_init_module ( %#lx, %llu, %#lx(\"%s\") )",
   3663          ARG1, (ULong)ARG2, ARG3, (char*)ARG3);
   3664    PRE_REG_READ3(long, "init_module",
   3665                  void *, umod, unsigned long, len, const char *, uargs);
   3666    PRE_MEM_READ( "init_module(umod)", ARG1, ARG2 );
   3667    PRE_MEM_RASCIIZ( "init_module(uargs)", ARG3 );
   3668 }
   3669 
   3670 PRE(sys_delete_module)
   3671 {
   3672    *flags |= SfMayBlock;
   3673    PRINT("sys_delete_module ( %#lx(\"%s\"), 0x%lx )", ARG1,(char*)ARG1, ARG2);
   3674    PRE_REG_READ2(long, "delete_module",
   3675                  const char *, name_user, unsigned int, flags);
   3676    PRE_MEM_RASCIIZ("delete_module(name_user)", ARG1);
   3677 }
   3678 
   3679 /* ---------------------------------------------------------------------
   3680    splice wrappers
   3681    ------------------------------------------------------------------ */
   3682 
   3683 PRE(sys_splice)
   3684 {
   3685    *flags |= SfMayBlock;
   3686    PRINT("sys_splice ( %ld, %#lx, %ld, %#lx, %ld, %ld )",
   3687          ARG1,ARG2,ARG3,ARG4,ARG5,ARG6);
   3688    PRE_REG_READ6(int32_t, "splice",
   3689                  int, fd_in, vki_loff_t *, off_in,
   3690                  int, fd_out, vki_loff_t *, off_out,
   3691                  vki_size_t, len, unsigned int, flags);
   3692    if (!ML_(fd_allowed)(ARG1, "splice(fd_in)", tid, False) ||
   3693        !ML_(fd_allowed)(ARG3, "splice(fd_out)", tid, False)) {
   3694       SET_STATUS_Failure( VKI_EBADF );
   3695    } else {
   3696       if (ARG2 != 0)
   3697          PRE_MEM_READ( "splice(off_in)", ARG2, sizeof(vki_loff_t));
   3698       if (ARG4 != 0)
   3699          PRE_MEM_READ( "splice(off_out)", ARG4, sizeof(vki_loff_t));
   3700    }
   3701 }
   3702 
   3703 /* ---------------------------------------------------------------------
   3704    oprofile-related wrappers
   3705    ------------------------------------------------------------------ */
   3706 
   3707 #if defined(VGP_x86_linux)
   3708 PRE(sys_lookup_dcookie)
   3709 {
   3710    PRINT("sys_lookup_dcookie (0x%llx, %#lx, %ld)",
   3711          MERGE64(ARG1,ARG2), ARG3, ARG4);
   3712    PRE_REG_READ4(long, "lookup_dcookie",
   3713                  vki_u32, MERGE64_FIRST(cookie), vki_u32, MERGE64_SECOND(cookie),
   3714                  char *, buf, vki_size_t, len);
   3715    PRE_MEM_WRITE( "lookup_dcookie(buf)", ARG3, ARG4);
   3716 }
   3717 POST(sys_lookup_dcookie)
   3718 {
   3719    vg_assert(SUCCESS);
   3720    if (ARG3 != (Addr)NULL)
   3721       POST_MEM_WRITE( ARG3, RES);
   3722 }
   3723 #endif
   3724 
   3725 #if defined(VGP_amd64_linux) || defined(VGP_s390x_linux)
   3726 PRE(sys_lookup_dcookie)
   3727 {
   3728    *flags |= SfMayBlock;
   3729    PRINT("sys_lookup_dcookie ( %llu, %#lx, %llu )",
   3730 	 (ULong)ARG1, ARG2, (ULong)ARG3);
   3731    PRE_REG_READ3(int, "lookup_dcookie",
   3732                  unsigned long long, cookie, char *, buf, vki_size_t, len);
   3733 
   3734    PRE_MEM_WRITE( "sys_lookup_dcookie(buf)", ARG2, ARG3 );
   3735 }
   3736 
   3737 POST(sys_lookup_dcookie)
   3738 {
   3739    vg_assert(SUCCESS);
   3740    if (ARG2 != (Addr)NULL)
   3741      POST_MEM_WRITE( ARG2, RES );
   3742 }
   3743 #endif
   3744 
   3745 /* ---------------------------------------------------------------------
   3746    fcntl wrappers
   3747    ------------------------------------------------------------------ */
   3748 
   3749 PRE(sys_fcntl)
   3750 {
   3751    switch (ARG2) {
   3752    // These ones ignore ARG3.
   3753    case VKI_F_GETFD:
   3754    case VKI_F_GETFL:
   3755    case VKI_F_GETOWN:
   3756    case VKI_F_GETSIG:
   3757    case VKI_F_GETLEASE:
   3758    case VKI_F_GETPIPE_SZ:
   3759       PRINT("sys_fcntl ( %ld, %ld )", ARG1,ARG2);
   3760       PRE_REG_READ2(long, "fcntl", unsigned int, fd, unsigned int, cmd);
   3761       break;
   3762 
   3763    // These ones use ARG3 as "arg".
   3764    case VKI_F_DUPFD:
   3765    case VKI_F_DUPFD_CLOEXEC:
   3766    case VKI_F_SETFD:
   3767    case VKI_F_SETFL:
   3768    case VKI_F_SETLEASE:
   3769    case VKI_F_NOTIFY:
   3770    case VKI_F_SETOWN:
   3771    case VKI_F_SETSIG:
   3772    case VKI_F_SETPIPE_SZ:
   3773       PRINT("sys_fcntl[ARG3=='arg'] ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   3774       PRE_REG_READ3(long, "fcntl",
   3775                     unsigned int, fd, unsigned int, cmd, unsigned long, arg);
   3776       break;
   3777 
   3778    // These ones use ARG3 as "lock".
   3779    case VKI_F_GETLK:
   3780    case VKI_F_SETLK:
   3781    case VKI_F_SETLKW:
   3782 #  if defined(VGP_x86_linux)
   3783    case VKI_F_GETLK64:
   3784    case VKI_F_SETLK64:
   3785    case VKI_F_SETLKW64:
   3786 #  endif
   3787       PRINT("sys_fcntl[ARG3=='lock'] ( %ld, %ld, %#lx )", ARG1,ARG2,ARG3);
   3788       PRE_REG_READ3(long, "fcntl",
   3789                     unsigned int, fd, unsigned int, cmd,
   3790                     struct flock64 *, lock);
   3791       break;
   3792 
   3793    default:
   3794       PRINT("sys_fcntl[UNKNOWN] ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   3795       I_die_here;
   3796       break;
   3797    }
   3798 
   3799 #  if defined(VGP_x86_linux)
   3800    if (ARG2 == VKI_F_SETLKW || ARG2 == VKI_F_SETLKW64)
   3801 #  else
   3802    if (ARG2 == VKI_F_SETLKW)
   3803 #  endif
   3804       *flags |= SfMayBlock;
   3805 }
   3806 
   3807 POST(sys_fcntl)
   3808 {
   3809    vg_assert(SUCCESS);
   3810    if (ARG2 == VKI_F_DUPFD) {
   3811       if (!ML_(fd_allowed)(RES, "fcntl(DUPFD)", tid, True)) {
   3812          VG_(close)(RES);
   3813          SET_STATUS_Failure( VKI_EMFILE );
   3814       } else {
   3815          if (VG_(clo_track_fds))
   3816             ML_(record_fd_open_named)(tid, RES);
   3817       }
   3818    }
   3819    else if (ARG2 == VKI_F_DUPFD_CLOEXEC) {
   3820       if (!ML_(fd_allowed)(RES, "fcntl(DUPFD_CLOEXEC)", tid, True)) {
   3821          VG_(close)(RES);
   3822          SET_STATUS_Failure( VKI_EMFILE );
   3823       } else {
   3824          if (VG_(clo_track_fds))
   3825             ML_(record_fd_open_named)(tid, RES);
   3826       }
   3827    }
   3828 }
   3829 
   3830 // XXX: wrapper only suitable for 32-bit systems
   3831 PRE(sys_fcntl64)
   3832 {
   3833    switch (ARG2) {
   3834    // These ones ignore ARG3.
   3835    case VKI_F_GETFD:
   3836    case VKI_F_GETFL:
   3837    case VKI_F_GETOWN:
   3838    case VKI_F_SETOWN:
   3839    case VKI_F_GETSIG:
   3840    case VKI_F_SETSIG:
   3841    case VKI_F_GETLEASE:
   3842       PRINT("sys_fcntl64 ( %ld, %ld )", ARG1,ARG2);
   3843       PRE_REG_READ2(long, "fcntl64", unsigned int, fd, unsigned int, cmd);
   3844       break;
   3845 
   3846    // These ones use ARG3 as "arg".
   3847    case VKI_F_DUPFD:
   3848    case VKI_F_DUPFD_CLOEXEC:
   3849    case VKI_F_SETFD:
   3850    case VKI_F_SETFL:
   3851    case VKI_F_SETLEASE:
   3852    case VKI_F_NOTIFY:
   3853       PRINT("sys_fcntl64[ARG3=='arg'] ( %ld, %ld, %ld )", ARG1,ARG2,ARG3);
   3854       PRE_REG_READ3(long, "fcntl64",
   3855                     unsigned int, fd, unsigned int, cmd, unsigned long, arg);
   3856       break;
   3857 
   3858    // These ones use ARG3 as "lock".
   3859    case VKI_F_GETLK:
   3860    case VKI_F_SETLK:
   3861    case VKI_F_SETLKW:
   3862 #  if defined(VGP_x86_linux)
   3863    case VKI_F_GETLK64:
   3864    case VKI_F_SETLK64:
   3865    case VKI_F_SETLKW64:
   3866 #  endif
   3867       PRINT("sys_fcntl64[ARG3=='lock'] ( %ld, %ld, %#lx )", ARG1,ARG2,ARG3);
   3868       PRE_REG_READ3(long, "fcntl64",
   3869                     unsigned int, fd, unsigned int, cmd,
   3870                     struct flock64 *, lock);
   3871       break;
   3872    }
   3873 
   3874 #  if defined(VGP_x86_linux)
   3875    if (ARG2 == VKI_F_SETLKW || ARG2 == VKI_F_SETLKW64)
   3876 #  else
   3877    if (ARG2 == VKI_F_SETLKW)
   3878 #  endif
   3879       *flags |= SfMayBlock;
   3880 }
   3881 
   3882 POST(sys_fcntl64)
   3883 {
   3884    vg_assert(SUCCESS);
   3885    if (ARG2 == VKI_F_DUPFD) {
   3886       if (!ML_(fd_allowed)(RES, "fcntl64(DUPFD)", tid, True)) {
   3887          VG_(close)(RES);
   3888          SET_STATUS_Failure( VKI_EMFILE );
   3889       } else {
   3890          if (VG_(clo_track_fds))
   3891             ML_(record_fd_open_named)(tid, RES);
   3892       }
   3893    }
   3894    else if (ARG2 == VKI_F_DUPFD_CLOEXEC) {
   3895       if (!ML_(fd_allowed)(RES, "fcntl64(DUPFD_CLOEXEC)", tid, True)) {
   3896          VG_(close)(RES);
   3897          SET_STATUS_Failure( VKI_EMFILE );
   3898       } else {
   3899          if (VG_(clo_track_fds))
   3900             ML_(record_fd_open_named)(tid, RES);
   3901       }
   3902    }
   3903 }
   3904 
   3905 /* ---------------------------------------------------------------------
   3906    ioctl wrappers
   3907    ------------------------------------------------------------------ */
   3908 
   3909 PRE(sys_ioctl)
   3910 {
   3911    *flags |= SfMayBlock;
   3912 
   3913    // We first handle the ones that don't use ARG3 (even as a
   3914    // scalar/non-pointer argument).
   3915    switch (ARG2 /* request */) {
   3916 
   3917       /* linux/soundcard interface (ALSA) */
   3918    case VKI_SNDRV_PCM_IOCTL_HW_FREE:
   3919    case VKI_SNDRV_PCM_IOCTL_HWSYNC:
   3920    case VKI_SNDRV_PCM_IOCTL_PREPARE:
   3921    case VKI_SNDRV_PCM_IOCTL_RESET:
   3922    case VKI_SNDRV_PCM_IOCTL_START:
   3923    case VKI_SNDRV_PCM_IOCTL_DROP:
   3924    case VKI_SNDRV_PCM_IOCTL_DRAIN:
   3925    case VKI_SNDRV_PCM_IOCTL_RESUME:
   3926    case VKI_SNDRV_PCM_IOCTL_XRUN:
   3927    case VKI_SNDRV_PCM_IOCTL_UNLINK:
   3928    case VKI_SNDRV_TIMER_IOCTL_START:
   3929    case VKI_SNDRV_TIMER_IOCTL_STOP:
   3930    case VKI_SNDRV_TIMER_IOCTL_CONTINUE:
   3931    case VKI_SNDRV_TIMER_IOCTL_PAUSE:
   3932       PRINT("sys_ioctl ( %ld, 0x%lx )",ARG1,ARG2);
   3933       PRE_REG_READ2(long, "ioctl",
   3934                     unsigned int, fd, unsigned int, request);
   3935       return;
   3936 
   3937    default:
   3938       PRINT("sys_ioctl ( %ld, 0x%lx, 0x%lx )",ARG1,ARG2,ARG3);
   3939       PRE_REG_READ3(long, "ioctl",
   3940                     unsigned int, fd, unsigned int, request, unsigned long, arg);
   3941       break;
   3942    }
   3943 
   3944    // We now handle those that do look at ARG3 (and unknown ones fall into
   3945    // this category).  Nb: some of these may well belong in the
   3946    // doesn't-use-ARG3 switch above.
   3947    switch (ARG2 /* request */) {
   3948    case VKI_TCSETS:
   3949    case VKI_TCSETSW:
   3950    case VKI_TCSETSF:
   3951       PRE_MEM_READ( "ioctl(TCSET{S,SW,SF})", ARG3, sizeof(struct vki_termios) );
   3952       break;
   3953    case VKI_TCGETS:
   3954       PRE_MEM_WRITE( "ioctl(TCGETS)", ARG3, sizeof(struct vki_termios) );
   3955       break;
   3956    case VKI_TCSETA:
   3957    case VKI_TCSETAW:
   3958    case VKI_TCSETAF:
   3959       PRE_MEM_READ( "ioctl(TCSET{A,AW,AF})", ARG3, sizeof(struct vki_termio) );
   3960       break;
   3961    case VKI_TCGETA:
   3962       PRE_MEM_WRITE( "ioctl(TCGETA)", ARG3, sizeof(struct vki_termio) );
   3963       break;
   3964    case VKI_TCSBRK:
   3965    case VKI_TCXONC:
   3966    case VKI_TCSBRKP:
   3967    case VKI_TCFLSH:
   3968       /* These just take an int by value */
   3969       break;
   3970    case VKI_TIOCGWINSZ:
   3971       PRE_MEM_WRITE( "ioctl(TIOCGWINSZ)", ARG3, sizeof(struct vki_winsize) );
   3972       break;
   3973    case VKI_TIOCSWINSZ:
   3974       PRE_MEM_READ( "ioctl(TIOCSWINSZ)",  ARG3, sizeof(struct vki_winsize) );
   3975       break;
   3976    case VKI_TIOCMBIS:
   3977       PRE_MEM_READ( "ioctl(TIOCMBIS)",    ARG3, sizeof(unsigned int) );
   3978       break;
   3979    case VKI_TIOCMBIC:
   3980       PRE_MEM_READ( "ioctl(TIOCMBIC)",    ARG3, sizeof(unsigned int) );
   3981       break;
   3982    case VKI_TIOCMSET:
   3983       PRE_MEM_READ( "ioctl(TIOCMSET)",    ARG3, sizeof(unsigned int) );
   3984       break;
   3985    case VKI_TIOCMGET:
   3986       PRE_MEM_WRITE( "ioctl(TIOCMGET)",   ARG3, sizeof(unsigned int) );
   3987       break;
   3988    case VKI_TIOCLINUX:
   3989       PRE_MEM_READ( "ioctl(TIOCLINUX)",   ARG3, sizeof(char *) );
   3990       if (*(char *)ARG3 == 11) {
   3991 	 PRE_MEM_READ( "ioctl(TIOCLINUX, 11)", ARG3, 2 * sizeof(char *) );
   3992       }
   3993       break;
   3994    case VKI_TIOCGPGRP:
   3995       /* Get process group ID for foreground processing group. */
   3996       PRE_MEM_WRITE( "ioctl(TIOCGPGRP)", ARG3, sizeof(vki_pid_t) );
   3997       break;
   3998    case VKI_TIOCSPGRP:
   3999       /* Set a process group ID? */
   4000       PRE_MEM_WRITE( "ioctl(TIOCGPGRP)", ARG3, sizeof(vki_pid_t) );
   4001       break;
   4002    case VKI_TIOCGPTN: /* Get Pty Number (of pty-mux device) */
   4003       PRE_MEM_WRITE( "ioctl(TIOCGPTN)", ARG3, sizeof(int) );
   4004       break;
   4005    case VKI_TIOCSCTTY:
   4006       /* Just takes an int value.  */
   4007       break;
   4008    case VKI_TIOCSPTLCK: /* Lock/unlock Pty */
   4009       PRE_MEM_READ( "ioctl(TIOCSPTLCK)", ARG3, sizeof(int) );
   4010       break;
   4011    case VKI_FIONBIO:
   4012       PRE_MEM_READ( "ioctl(FIONBIO)",    ARG3, sizeof(int) );
   4013       break;
   4014    case VKI_FIOASYNC:
   4015       PRE_MEM_READ( "ioctl(FIOASYNC)",   ARG3, sizeof(int) );
   4016       break;
   4017    case VKI_FIONREAD:                /* identical to SIOCINQ */
   4018       PRE_MEM_WRITE( "ioctl(FIONREAD)",  ARG3, sizeof(int) );
   4019       break;
   4020    case VKI_FIOQSIZE:
   4021       PRE_MEM_WRITE( "ioctl(FIOQSIZE)",  ARG3, sizeof(vki_loff_t) );
   4022       break;
   4023 
   4024    case VKI_TIOCSERGETLSR:
   4025       PRE_MEM_WRITE( "ioctl(TIOCSERGETLSR)", ARG3, sizeof(int) );
   4026       break;
   4027    case VKI_TIOCGICOUNT:
   4028       PRE_MEM_WRITE( "ioctl(TIOCGICOUNT)", ARG3,
   4029                      sizeof(struct vki_serial_icounter_struct) );
   4030       break;
   4031 
   4032    case VKI_SG_SET_COMMAND_Q:
   4033       PRE_MEM_READ( "ioctl(SG_SET_COMMAND_Q)", ARG3, sizeof(int) );
   4034       break;
   4035    case VKI_SG_IO:
   4036       PRE_MEM_WRITE( "ioctl(SG_IO)", ARG3, sizeof(vki_sg_io_hdr_t) );
   4037       break;
   4038    case VKI_SG_GET_SCSI_ID:
   4039       PRE_MEM_WRITE( "ioctl(SG_GET_SCSI_ID)", ARG3, sizeof(vki_sg_scsi_id_t) );
   4040       break;
   4041    case VKI_SG_SET_RESERVED_SIZE:
   4042       PRE_MEM_READ( "ioctl(SG_SET_RESERVED_SIZE)", ARG3, sizeof(int) );
   4043       break;
   4044    case VKI_SG_SET_TIMEOUT:
   4045       PRE_MEM_READ( "ioctl(SG_SET_TIMEOUT)", ARG3, sizeof(int) );
   4046       break;
   4047    case VKI_SG_GET_RESERVED_SIZE:
   4048       PRE_MEM_WRITE( "ioctl(SG_GET_RESERVED_SIZE)", ARG3, sizeof(int) );
   4049       break;
   4050    case VKI_SG_GET_TIMEOUT:
   4051       break;
   4052    case VKI_SG_GET_VERSION_NUM:
   4053       PRE_MEM_WRITE(  "ioctl(SG_GET_VERSION_NUM)",  ARG3, sizeof(int) );
   4054       break;
   4055    case VKI_SG_EMULATED_HOST: /* 0x2203 */
   4056       PRE_MEM_WRITE( "ioctl(SG_EMULATED_HOST)",    ARG3, sizeof(int) );
   4057       break;
   4058    case VKI_SG_GET_SG_TABLESIZE: /* 0x227f */
   4059       PRE_MEM_WRITE( "ioctl(SG_GET_SG_TABLESIZE)", ARG3, sizeof(int) );
   4060       break;
   4061 
   4062    case VKI_IIOCGETCPS:
   4063       PRE_MEM_WRITE( "ioctl(IIOCGETCPS)", ARG3,
   4064 		     VKI_ISDN_MAX_CHANNELS * 2 * sizeof(unsigned long) );
   4065       break;
   4066    case VKI_IIOCNETGPN:
   4067       PRE_MEM_READ( "ioctl(IIOCNETGPN)",
   4068 		     (Addr)&((vki_isdn_net_ioctl_phone *)ARG3)->name,
   4069 		     sizeof(((vki_isdn_net_ioctl_phone *)ARG3)->name) );
   4070       PRE_MEM_WRITE( "ioctl(IIOCNETGPN)", ARG3,
   4071 		     sizeof(vki_isdn_net_ioctl_phone) );
   4072       break;
   4073 
   4074       /* These all use struct ifreq AFAIK */
   4075    case VKI_SIOCGIFINDEX:        /* get iface index              */
   4076       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFINDEX)",
   4077                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4078       PRE_MEM_WRITE( "ioctl(SIOCGIFINDEX)", ARG3, sizeof(struct vki_ifreq));
   4079       break;
   4080    case VKI_SIOCGIFFLAGS:        /* get flags                    */
   4081       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFFLAGS)",
   4082                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4083       PRE_MEM_WRITE( "ioctl(SIOCGIFFLAGS)", ARG3, sizeof(struct vki_ifreq));
   4084       break;
   4085    case VKI_SIOCGIFHWADDR:       /* Get hardware address         */
   4086       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFHWADDR)",
   4087                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4088       PRE_MEM_WRITE( "ioctl(SIOCGIFHWADDR)", ARG3, sizeof(struct vki_ifreq));
   4089       break;
   4090    case VKI_SIOCGIFMTU:          /* get MTU size                 */
   4091       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFMTU)",
   4092                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4093       PRE_MEM_WRITE( "ioctl(SIOCGIFMTU)", ARG3, sizeof(struct vki_ifreq));
   4094       break;
   4095    case VKI_SIOCGIFADDR:         /* get PA address               */
   4096       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFADDR)",
   4097                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4098       PRE_MEM_WRITE( "ioctl(SIOCGIFADDR)", ARG3, sizeof(struct vki_ifreq));
   4099       break;
   4100    case VKI_SIOCGIFNETMASK:      /* get network PA mask          */
   4101       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFNETMASK)",
   4102                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4103       PRE_MEM_WRITE( "ioctl(SIOCGIFNETMASK)", ARG3, sizeof(struct vki_ifreq));
   4104       break;
   4105    case VKI_SIOCGIFMETRIC:       /* get metric                   */
   4106       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFMETRIC)",
   4107                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4108       PRE_MEM_WRITE( "ioctl(SIOCGIFMETRIC)", ARG3, sizeof(struct vki_ifreq));
   4109       break;
   4110    case VKI_SIOCGIFMAP:          /* Get device parameters        */
   4111       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFMAP)",
   4112                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4113       PRE_MEM_WRITE( "ioctl(SIOCGIFMAP)", ARG3, sizeof(struct vki_ifreq));
   4114       break;
   4115    case VKI_SIOCGIFTXQLEN:       /* Get the tx queue length      */
   4116       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFTXQLEN)",
   4117                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4118       PRE_MEM_WRITE( "ioctl(SIOCGIFTXQLEN)", ARG3, sizeof(struct vki_ifreq));
   4119       break;
   4120    case VKI_SIOCGIFDSTADDR:      /* get remote PA address        */
   4121       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFDSTADDR)",
   4122                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4123       PRE_MEM_WRITE( "ioctl(SIOCGIFDSTADDR)", ARG3, sizeof(struct vki_ifreq));
   4124       break;
   4125    case VKI_SIOCGIFBRDADDR:      /* get broadcast PA address     */
   4126       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFBRDADDR)",
   4127                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4128       PRE_MEM_WRITE( "ioctl(SIOCGIFBRDADDR)", ARG3, sizeof(struct vki_ifreq));
   4129       break;
   4130    case VKI_SIOCGIFNAME:         /* get iface name               */
   4131       PRE_MEM_READ( "ioctl(SIOCGIFNAME)",
   4132                      (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_ifindex,
   4133                      sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_ifindex) );
   4134       PRE_MEM_WRITE( "ioctl(SIOCGIFNAME)", ARG3, sizeof(struct vki_ifreq));
   4135       break;
   4136    case VKI_SIOCGMIIPHY:         /* get hardware entry           */
   4137       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFMIIPHY)",
   4138                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4139       PRE_MEM_WRITE( "ioctl(SIOCGIFMIIPHY)", ARG3, sizeof(struct vki_ifreq));
   4140       break;
   4141    case VKI_SIOCGMIIREG:         /* get hardware entry registers */
   4142       PRE_MEM_RASCIIZ( "ioctl(SIOCGIFMIIREG)",
   4143                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4144       PRE_MEM_READ( "ioctl(SIOCGIFMIIREG)",
   4145                      (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->phy_id,
   4146                      sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->phy_id) );
   4147       PRE_MEM_READ( "ioctl(SIOCGIFMIIREG)",
   4148                      (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->reg_num,
   4149                      sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->reg_num) );
   4150       PRE_MEM_WRITE( "ioctl(SIOCGIFMIIREG)", ARG3,
   4151 		     sizeof(struct vki_ifreq));
   4152       break;
   4153    case VKI_SIOCGIFCONF:         /* get iface list               */
   4154       /* WAS:
   4155 	 PRE_MEM_WRITE( "ioctl(SIOCGIFCONF)", ARG3, sizeof(struct ifconf));
   4156 	 KERNEL_DO_SYSCALL(tid,RES);
   4157 	 if (!VG_(is_kerror)(RES) && RES == 0)
   4158 	 POST_MEM_WRITE(ARG3, sizeof(struct ifconf));
   4159       */
   4160       PRE_MEM_READ( "ioctl(SIOCGIFCONF)",
   4161                     (Addr)&((struct vki_ifconf *)ARG3)->ifc_len,
   4162                     sizeof(((struct vki_ifconf *)ARG3)->ifc_len));
   4163       PRE_MEM_READ( "ioctl(SIOCGIFCONF)",
   4164                     (Addr)&((struct vki_ifconf *)ARG3)->vki_ifc_buf,
   4165                     sizeof(((struct vki_ifconf *)ARG3)->vki_ifc_buf));
   4166       if ( ARG3 ) {
   4167 	 // TODO len must be readable and writable
   4168 	 // buf pointer only needs to be readable
   4169 	 struct vki_ifconf *ifc = (struct vki_ifconf *) ARG3;
   4170 	 PRE_MEM_WRITE( "ioctl(SIOCGIFCONF).ifc_buf",
   4171 			(Addr)(ifc->vki_ifc_buf), ifc->ifc_len );
   4172       }
   4173       break;
   4174    case VKI_SIOCGSTAMP:
   4175       PRE_MEM_WRITE( "ioctl(SIOCGSTAMP)", ARG3, sizeof(struct vki_timeval));
   4176       break;
   4177    case VKI_SIOCGSTAMPNS:
   4178       PRE_MEM_WRITE( "ioctl(SIOCGSTAMPNS)", ARG3, sizeof(struct vki_timespec));
   4179       break;
   4180       /* SIOCOUTQ is an ioctl that, when called on a socket, returns
   4181 	 the number of bytes currently in that socket's send buffer.
   4182 	 It writes this value as an int to the memory location
   4183 	 indicated by the third argument of ioctl(2). */
   4184    case VKI_SIOCOUTQ:
   4185       PRE_MEM_WRITE( "ioctl(SIOCOUTQ)", ARG3, sizeof(int));
   4186       break;
   4187    case VKI_SIOCGRARP:           /* get RARP table entry         */
   4188    case VKI_SIOCGARP:            /* get ARP table entry          */
   4189       PRE_MEM_WRITE( "ioctl(SIOCGARP)", ARG3, sizeof(struct vki_arpreq));
   4190       break;
   4191 
   4192    case VKI_SIOCSIFFLAGS:        /* set flags                    */
   4193       PRE_MEM_RASCIIZ( "ioctl(SIOCSIFFLAGS)",
   4194                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4195       PRE_MEM_READ( "ioctl(SIOCSIFFLAGS)",
   4196                      (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_flags,
   4197                      sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_flags) );
   4198       break;
   4199    case VKI_SIOCSIFMAP:          /* Set device parameters        */
   4200       PRE_MEM_RASCIIZ( "ioctl(SIOCSIFMAP)",
   4201                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4202       PRE_MEM_READ( "ioctl(SIOCSIFMAP)",
   4203                      (Addr)&((struct vki_ifreq *)ARG3)->ifr_map,
   4204                      sizeof(((struct vki_ifreq *)ARG3)->ifr_map) );
   4205       break;
   4206    case VKI_SIOCSIFTXQLEN:       /* Set the tx queue length      */
   4207       PRE_MEM_RASCIIZ( "ioctl(SIOCSIFTXQLEN)",
   4208                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4209       PRE_MEM_READ( "ioctl(SIOCSIFTXQLEN)",
   4210                      (Addr)&((struct vki_ifreq *)ARG3)->ifr_qlen,
   4211                      sizeof(((struct vki_ifreq *)ARG3)->ifr_qlen) );
   4212       break;
   4213    case VKI_SIOCSIFADDR:         /* set PA address               */
   4214    case VKI_SIOCSIFDSTADDR:      /* set remote PA address        */
   4215    case VKI_SIOCSIFBRDADDR:      /* set broadcast PA address     */
   4216    case VKI_SIOCSIFNETMASK:      /* set network PA mask          */
   4217       PRE_MEM_RASCIIZ( "ioctl(SIOCSIF*ADDR)",
   4218                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4219       PRE_MEM_READ( "ioctl(SIOCSIF*ADDR)",
   4220                      (Addr)&((struct vki_ifreq *)ARG3)->ifr_addr,
   4221                      sizeof(((struct vki_ifreq *)ARG3)->ifr_addr) );
   4222       break;
   4223    case VKI_SIOCSIFMETRIC:       /* set metric                   */
   4224       PRE_MEM_RASCIIZ( "ioctl(SIOCSIFMETRIC)",
   4225                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4226       PRE_MEM_READ( "ioctl(SIOCSIFMETRIC)",
   4227                      (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_metric,
   4228                      sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_metric) );
   4229       break;
   4230    case VKI_SIOCSIFMTU:          /* set MTU size                 */
   4231       PRE_MEM_RASCIIZ( "ioctl(SIOCSIFMTU)",
   4232                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4233       PRE_MEM_READ( "ioctl(SIOCSIFMTU)",
   4234                      (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_mtu,
   4235                      sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_mtu) );
   4236       break;
   4237    case VKI_SIOCSIFHWADDR:       /* set hardware address         */
   4238       PRE_MEM_RASCIIZ( "ioctl(SIOCSIFHWADDR)",
   4239                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4240       PRE_MEM_READ( "ioctl(SIOCSIFHWADDR)",
   4241                      (Addr)&((struct vki_ifreq *)ARG3)->ifr_hwaddr,
   4242                      sizeof(((struct vki_ifreq *)ARG3)->ifr_hwaddr) );
   4243       break;
   4244    case VKI_SIOCSMIIREG:         /* set hardware entry registers */
   4245       PRE_MEM_RASCIIZ( "ioctl(SIOCSMIIREG)",
   4246                      (Addr)((struct vki_ifreq *)ARG3)->vki_ifr_name );
   4247       PRE_MEM_READ( "ioctl(SIOCSMIIREG)",
   4248                      (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->phy_id,
   4249                      sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->phy_id) );
   4250       PRE_MEM_READ( "ioctl(SIOCSMIIREG)",
   4251                      (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->reg_num,
   4252                      sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->reg_num) );
   4253       PRE_MEM_READ( "ioctl(SIOCSMIIREG)",
   4254                      (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->val_in,
   4255                      sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->val_in) );
   4256       break;
   4257       /* Routing table calls.  */
   4258    case VKI_SIOCADDRT:           /* add routing table entry      */
   4259    case VKI_SIOCDELRT:           /* delete routing table entry   */
   4260       PRE_MEM_READ( "ioctl(SIOCADDRT/DELRT)", ARG3,
   4261 		    sizeof(struct vki_rtentry));
   4262       break;
   4263 
   4264       /* RARP cache control calls. */
   4265    case VKI_SIOCDRARP:           /* delete RARP table entry      */
   4266    case VKI_SIOCSRARP:           /* set RARP table entry         */
   4267       /* ARP cache control calls. */
   4268    case VKI_SIOCSARP:            /* set ARP table entry          */
   4269    case VKI_SIOCDARP:            /* delete ARP table entry       */
   4270       PRE_MEM_READ( "ioctl(SIOCSIFFLAGS)", ARG3, sizeof(struct vki_ifreq));
   4271       break;
   4272 
   4273    case VKI_SIOCGPGRP:
   4274       PRE_MEM_WRITE( "ioctl(SIOCGPGRP)", ARG3, sizeof(int) );
   4275       break;
   4276    case VKI_SIOCSPGRP:
   4277       PRE_MEM_READ( "ioctl(SIOCSPGRP)", ARG3, sizeof(int) );
   4278       //tst->sys_flags &= ~SfMayBlock;
   4279       break;
   4280 
   4281       /* linux/soundcard interface (OSS) */
   4282    case VKI_SNDCTL_SEQ_GETOUTCOUNT:
   4283    case VKI_SNDCTL_SEQ_GETINCOUNT:
   4284    case VKI_SNDCTL_SEQ_PERCMODE:
   4285    case VKI_SNDCTL_SEQ_TESTMIDI:
   4286    case VKI_SNDCTL_SEQ_RESETSAMPLES:
   4287    case VKI_SNDCTL_SEQ_NRSYNTHS:
   4288    case VKI_SNDCTL_SEQ_NRMIDIS:
   4289    case VKI_SNDCTL_SEQ_GETTIME:
   4290    case VKI_SNDCTL_DSP_GETBLKSIZE:
   4291    case VKI_SNDCTL_DSP_GETFMTS:
   4292    case VKI_SNDCTL_DSP_GETTRIGGER:
   4293    case VKI_SNDCTL_DSP_GETODELAY:
   4294    case VKI_SNDCTL_DSP_GETSPDIF:
   4295    case VKI_SNDCTL_DSP_GETCAPS:
   4296    case VKI_SOUND_PCM_READ_RATE:
   4297    case VKI_SOUND_PCM_READ_CHANNELS:
   4298    case VKI_SOUND_PCM_READ_BITS:
   4299    case VKI_SOUND_PCM_READ_FILTER:
   4300       PRE_MEM_WRITE( "ioctl(SNDCTL_XXX|SOUND_XXX (SIOR, int))",
   4301 		     ARG3, sizeof(int));
   4302       break;
   4303    case VKI_SNDCTL_SEQ_CTRLRATE:
   4304    case VKI_SNDCTL_DSP_SPEED:
   4305    case VKI_SNDCTL_DSP_STEREO:
   4306    case VKI_SNDCTL_DSP_CHANNELS:
   4307    case VKI_SOUND_PCM_WRITE_FILTER:
   4308    case VKI_SNDCTL_DSP_SUBDIVIDE:
   4309    case VKI_SNDCTL_DSP_SETFRAGMENT:
   4310    case VKI_SNDCTL_DSP_SETFMT:
   4311    case VKI_SNDCTL_DSP_GETCHANNELMASK:
   4312    case VKI_SNDCTL_DSP_BIND_CHANNEL:
   4313    case VKI_SNDCTL_TMR_TIMEBASE:
   4314    case VKI_SNDCTL_TMR_TEMPO:
   4315    case VKI_SNDCTL_TMR_SOURCE:
   4316    case VKI_SNDCTL_MIDI_PRETIME:
   4317    case VKI_SNDCTL_MIDI_MPUMODE:
   4318       PRE_MEM_READ( "ioctl(SNDCTL_XXX|SOUND_XXX (SIOWR, int))",
   4319 		     ARG3, sizeof(int));
   4320       PRE_MEM_WRITE( "ioctl(SNDCTL_XXX|SOUND_XXX (SIOWR, int))",
   4321 		     ARG3, sizeof(int));
   4322       break;
   4323    case VKI_SNDCTL_DSP_GETOSPACE:
   4324    case VKI_SNDCTL_DSP_GETISPACE:
   4325       PRE_MEM_WRITE( "ioctl(SNDCTL_XXX|SOUND_XXX (SIOR, audio_buf_info))",
   4326                      ARG3, sizeof(vki_audio_buf_info));
   4327       break;
   4328    case VKI_SNDCTL_DSP_NONBLOCK:
   4329       break;
   4330    case VKI_SNDCTL_DSP_SETTRIGGER:
   4331       PRE_MEM_READ( "ioctl(SNDCTL_XXX|SOUND_XXX (SIOW, int))",
   4332 		     ARG3, sizeof(int));
   4333       break;
   4334 
   4335    case VKI_SNDCTL_DSP_POST:
   4336    case VKI_SNDCTL_DSP_RESET:
   4337    case VKI_SNDCTL_DSP_SYNC:
   4338    case VKI_SNDCTL_DSP_SETSYNCRO:
   4339    case VKI_SNDCTL_DSP_SETDUPLEX:
   4340       break;
   4341 
   4342       /* linux/soundcard interface (ALSA) */
   4343    case VKI_SNDRV_PCM_IOCTL_PAUSE:
   4344    case VKI_SNDRV_PCM_IOCTL_LINK:
   4345       /* these just take an int by value */
   4346       break;
   4347 
   4348       /* Real Time Clock (/dev/rtc) ioctls */
   4349    case VKI_RTC_UIE_ON:
   4350    case VKI_RTC_UIE_OFF:
   4351    case VKI_RTC_AIE_ON:
   4352    case VKI_RTC_AIE_OFF:
   4353    case VKI_RTC_PIE_ON:
   4354    case VKI_RTC_PIE_OFF:
   4355    case VKI_RTC_IRQP_SET:
   4356       break;
   4357    case VKI_RTC_RD_TIME:
   4358    case VKI_RTC_ALM_READ:
   4359       PRE_MEM_WRITE( "ioctl(RTC_RD_TIME/ALM_READ)",
   4360 		     ARG3, sizeof(struct vki_rtc_time));
   4361       break;
   4362    case VKI_RTC_ALM_SET:
   4363       PRE_MEM_READ( "ioctl(RTC_ALM_SET)", ARG3, sizeof(struct vki_rtc_time));
   4364       break;
   4365    case VKI_RTC_IRQP_READ:
   4366       PRE_MEM_WRITE( "ioctl(RTC_IRQP_READ)", ARG3, sizeof(unsigned long));
   4367       break;
   4368 
   4369       /* Block devices */
   4370    case VKI_BLKROSET:
   4371       PRE_MEM_READ( "ioctl(BLKROSET)", ARG3, sizeof(int));
   4372       break;
   4373    case VKI_BLKROGET:
   4374       PRE_MEM_WRITE( "ioctl(BLKROGET)", ARG3, sizeof(int));
   4375       break;
   4376    case VKI_BLKGETSIZE:
   4377       PRE_MEM_WRITE( "ioctl(BLKGETSIZE)", ARG3, sizeof(unsigned long));
   4378       break;
   4379    case VKI_BLKRASET:
   4380       break;
   4381    case VKI_BLKRAGET:
   4382       PRE_MEM_WRITE( "ioctl(BLKRAGET)", ARG3, sizeof(long));
   4383       break;
   4384    case VKI_BLKFRASET:
   4385       break;
   4386    case VKI_BLKFRAGET:
   4387       PRE_MEM_WRITE( "ioctl(BLKFRAGET)", ARG3, sizeof(long));
   4388       break;
   4389    case VKI_BLKSECTGET:
   4390       PRE_MEM_WRITE( "ioctl(BLKSECTGET)", ARG3, sizeof(unsigned short));
   4391       break;
   4392    case VKI_BLKSSZGET:
   4393       PRE_MEM_WRITE( "ioctl(BLKSSZGET)", ARG3, sizeof(int));
   4394       break;
   4395    case VKI_BLKBSZGET:
   4396       PRE_MEM_WRITE( "ioctl(BLKBSZGET)", ARG3, sizeof(int));
   4397       break;
   4398    case VKI_BLKBSZSET:
   4399       PRE_MEM_READ( "ioctl(BLKBSZSET)", ARG3, sizeof(int));
   4400       break;
   4401    case VKI_BLKGETSIZE64:
   4402       PRE_MEM_WRITE( "ioctl(BLKGETSIZE64)", ARG3, sizeof(unsigned long long));
   4403       break;
   4404 
   4405       /* Hard disks */
   4406    case VKI_HDIO_GETGEO: /* 0x0301 */
   4407       PRE_MEM_WRITE( "ioctl(HDIO_GETGEO)", ARG3, sizeof(struct vki_hd_geometry));
   4408       break;
   4409    case VKI_HDIO_GET_DMA: /* 0x030b */
   4410       PRE_MEM_WRITE( "ioctl(HDIO_GET_DMA)", ARG3, sizeof(long));
   4411       break;
   4412    case VKI_HDIO_GET_IDENTITY: /* 0x030d */
   4413       PRE_MEM_WRITE( "ioctl(HDIO_GET_IDENTITY)", ARG3,
   4414                      VKI_SIZEOF_STRUCT_HD_DRIVEID );
   4415       break;
   4416 
   4417       /* CD ROM stuff (??)  */
   4418    case VKI_CDROM_GET_MCN:
   4419       PRE_MEM_READ( "ioctl(CDROM_GET_MCN)", ARG3,
   4420                     sizeof(struct vki_cdrom_mcn) );
   4421       break;
   4422    case VKI_CDROM_SEND_PACKET:
   4423       PRE_MEM_READ( "ioctl(CDROM_SEND_PACKET)", ARG3,
   4424                     sizeof(struct vki_cdrom_generic_command));
   4425       break;
   4426    case VKI_CDROMSUBCHNL:
   4427       PRE_MEM_READ( "ioctl(CDROMSUBCHNL (cdsc_format, char))",
   4428 		    (Addr) &(((struct vki_cdrom_subchnl*) ARG3)->cdsc_format),
   4429 		    sizeof(((struct vki_cdrom_subchnl*) ARG3)->cdsc_format));
   4430       PRE_MEM_WRITE( "ioctl(CDROMSUBCHNL)", ARG3,
   4431 		     sizeof(struct vki_cdrom_subchnl));
   4432       break;
   4433    case VKI_CDROMREADMODE2:
   4434       PRE_MEM_READ( "ioctl(CDROMREADMODE2)", ARG3, VKI_CD_FRAMESIZE_RAW0 );
   4435       break;
   4436    case VKI_CDROMREADTOCHDR:
   4437       PRE_MEM_WRITE( "ioctl(CDROMREADTOCHDR)", ARG3,
   4438 		     sizeof(struct vki_cdrom_tochdr));
   4439       break;
   4440    case VKI_CDROMREADTOCENTRY:
   4441       PRE_MEM_READ( "ioctl(CDROMREADTOCENTRY (cdte_format, char))",
   4442 		    (Addr) &(((struct vki_cdrom_tocentry*) ARG3)->cdte_format),
   4443 		    sizeof(((struct vki_cdrom_tocentry*) ARG3)->cdte_format));
   4444       PRE_MEM_READ( "ioctl(CDROMREADTOCENTRY (cdte_track, char))",
   4445 		    (Addr) &(((struct vki_cdrom_tocentry*) ARG3)->cdte_track),
   4446 		    sizeof(((struct vki_cdrom_tocentry*) ARG3)->cdte_track));
   4447       PRE_MEM_WRITE( "ioctl(CDROMREADTOCENTRY)", ARG3,
   4448 		     sizeof(struct vki_cdrom_tocentry));
   4449       break;
   4450    case VKI_CDROMMULTISESSION: /* 0x5310 */
   4451       PRE_MEM_WRITE( "ioctl(CDROMMULTISESSION)", ARG3,
   4452 		     sizeof(struct vki_cdrom_multisession));
   4453       break;
   4454    case VKI_CDROMVOLREAD: /* 0x5313 */
   4455       PRE_MEM_WRITE( "ioctl(CDROMVOLREAD)", ARG3,
   4456 		     sizeof(struct vki_cdrom_volctrl));
   4457       break;
   4458    case VKI_CDROMREADRAW: /* 0x5314 */
   4459       PRE_MEM_READ( "ioctl(CDROMREADRAW)", ARG3, sizeof(struct vki_cdrom_msf));
   4460       PRE_MEM_WRITE( "ioctl(CDROMREADRAW)", ARG3, VKI_CD_FRAMESIZE_RAW);
   4461       break;
   4462    case VKI_CDROMREADAUDIO: /* 0x530e */
   4463       PRE_MEM_READ( "ioctl(CDROMREADAUDIO)", ARG3,
   4464 		     sizeof (struct vki_cdrom_read_audio));
   4465       if ( ARG3 ) {
   4466          /* ToDo: don't do any of the following if the structure is invalid */
   4467          struct vki_cdrom_read_audio *cra = (struct vki_cdrom_read_audio *) ARG3;
   4468 	 PRE_MEM_WRITE( "ioctl(CDROMREADAUDIO).buf",
   4469 	                (Addr)(cra->buf), cra->nframes * VKI_CD_FRAMESIZE_RAW);
   4470       }
   4471       break;
   4472    case VKI_CDROMPLAYMSF:
   4473       PRE_MEM_READ( "ioctl(CDROMPLAYMSF)", ARG3, sizeof(struct vki_cdrom_msf));
   4474       break;
   4475       /* The following two are probably bogus (should check args
   4476 	 for readability).  JRS 20021117 */
   4477    case VKI_CDROM_DRIVE_STATUS: /* 0x5326 */
   4478    case VKI_CDROM_CLEAR_OPTIONS: /* 0x5321 */
   4479       break;
   4480 
   4481    case VKI_FIGETBSZ:
   4482       PRE_MEM_WRITE( "ioctl(FIGETBSZ)", ARG3, sizeof(unsigned long));
   4483       break;
   4484    case VKI_FIBMAP:
   4485       PRE_MEM_READ( "ioctl(FIBMAP)", ARG3, sizeof(int));
   4486       break;
   4487 
   4488    case VKI_FBIOGET_VSCREENINFO: /* 0x4600 */
   4489       PRE_MEM_WRITE( "ioctl(FBIOGET_VSCREENINFO)", ARG3,
   4490                      sizeof(struct vki_fb_var_screeninfo));
   4491       break;
   4492    case VKI_FBIOPUT_VSCREENINFO:
   4493       PRE_MEM_READ( "ioctl(FBIOPUT_VSCREENINFO)", ARG3,
   4494                     sizeof(struct vki_fb_var_screeninfo));
   4495       break;
   4496    case VKI_FBIOGET_FSCREENINFO: /* 0x4602 */
   4497       PRE_MEM_WRITE( "ioctl(FBIOGET_FSCREENINFO)", ARG3,
   4498                      sizeof(struct vki_fb_fix_screeninfo));
   4499       break;
   4500    case VKI_FBIOPAN_DISPLAY:
   4501       PRE_MEM_READ( "ioctl(FBIOPAN_DISPLAY)", ARG3,
   4502                     sizeof(struct vki_fb_var_screeninfo));
   4503 
   4504       break;
   4505    case VKI_PPCLAIM:
   4506    case VKI_PPEXCL:
   4507    case VKI_PPYIELD:
   4508    case VKI_PPRELEASE:
   4509       break;
   4510    case VKI_PPSETMODE:
   4511       PRE_MEM_READ( "ioctl(PPSETMODE)",   ARG3, sizeof(int) );
   4512       break;
   4513    case VKI_PPGETMODE:
   4514       PRE_MEM_WRITE( "ioctl(PPGETMODE)",  ARG3, sizeof(int) );
   4515       break;
   4516    case VKI_PPSETPHASE:
   4517       PRE_MEM_READ(  "ioctl(PPSETPHASE)", ARG3, sizeof(int) );
   4518       break;
   4519    case VKI_PPGETPHASE:
   4520       PRE_MEM_WRITE( "ioctl(PPGETPHASE)", ARG3, sizeof(int) );
   4521       break;
   4522    case VKI_PPGETMODES:
   4523       PRE_MEM_WRITE( "ioctl(PPGETMODES)", ARG3, sizeof(unsigned int) );
   4524       break;
   4525    case VKI_PPSETFLAGS:
   4526       PRE_MEM_READ(  "ioctl(PPSETFLAGS)", ARG3, sizeof(int) );
   4527       break;
   4528    case VKI_PPGETFLAGS:
   4529       PRE_MEM_WRITE( "ioctl(PPGETFLAGS)", ARG3, sizeof(int) );
   4530       break;
   4531    case VKI_PPRSTATUS:
   4532       PRE_MEM_WRITE( "ioctl(PPRSTATUS)",  ARG3, sizeof(unsigned char) );
   4533       break;
   4534    case VKI_PPRDATA:
   4535       PRE_MEM_WRITE( "ioctl(PPRDATA)",    ARG3, sizeof(unsigned char) );
   4536       break;
   4537    case VKI_PPRCONTROL:
   4538       PRE_MEM_WRITE( "ioctl(PPRCONTROL)", ARG3, sizeof(unsigned char) );
   4539       break;
   4540    case VKI_PPWDATA:
   4541       PRE_MEM_READ(  "ioctl(PPWDATA)",    ARG3, sizeof(unsigned char) );
   4542       break;
   4543    case VKI_PPWCONTROL:
   4544       PRE_MEM_READ(  "ioctl(PPWCONTROL)", ARG3, sizeof(unsigned char) );
   4545       break;
   4546    case VKI_PPFCONTROL:
   4547       PRE_MEM_READ(  "ioctl(PPFCONTROL)", ARG3, 2 * sizeof(unsigned char) );
   4548       break;
   4549    case VKI_PPDATADIR:
   4550       PRE_MEM_READ(  "ioctl(PPDATADIR)",  ARG3, sizeof(int) );
   4551       break;
   4552    case VKI_PPNEGOT:
   4553       PRE_MEM_READ(  "ioctl(PPNEGOT)",    ARG3, sizeof(int) );
   4554       break;
   4555    case VKI_PPWCTLONIRQ:
   4556       PRE_MEM_READ(  "ioctl(PPWCTLONIRQ)",ARG3, sizeof(unsigned char) );
   4557       break;
   4558    case VKI_PPCLRIRQ:
   4559       PRE_MEM_WRITE( "ioctl(PPCLRIRQ)",   ARG3, sizeof(int) );
   4560       break;
   4561    case VKI_PPSETTIME:
   4562       PRE_MEM_READ(  "ioctl(PPSETTIME)",  ARG3, sizeof(struct vki_timeval) );
   4563       break;
   4564    case VKI_PPGETTIME:
   4565       PRE_MEM_WRITE( "ioctl(PPGETTIME)",  ARG3, sizeof(struct vki_timeval) );
   4566       break;
   4567 
   4568    case VKI_GIO_FONT:
   4569       PRE_MEM_WRITE( "ioctl(GIO_FONT)", ARG3, 32 * 256 );
   4570       break;
   4571    case VKI_PIO_FONT:
   4572       PRE_MEM_READ( "ioctl(PIO_FONT)", ARG3, 32 * 256 );
   4573       break;
   4574 
   4575    case VKI_GIO_FONTX:
   4576       PRE_MEM_READ( "ioctl(GIO_FONTX)", ARG3, sizeof(struct vki_consolefontdesc) );
   4577       if ( ARG3 ) {
   4578          /* ToDo: don't do any of the following if the structure is invalid */
   4579          struct vki_consolefontdesc *cfd = (struct vki_consolefontdesc *)ARG3;
   4580          PRE_MEM_WRITE( "ioctl(GIO_FONTX).chardata", (Addr)cfd->chardata,
   4581                         32 * cfd->charcount );
   4582       }
   4583       break;
   4584    case VKI_PIO_FONTX:
   4585       PRE_MEM_READ( "ioctl(PIO_FONTX)", ARG3, sizeof(struct vki_consolefontdesc) );
   4586       if ( ARG3 ) {
   4587          /* ToDo: don't do any of the following if the structure is invalid */
   4588          struct vki_consolefontdesc *cfd = (struct vki_consolefontdesc *)ARG3;
   4589          PRE_MEM_READ( "ioctl(PIO_FONTX).chardata", (Addr)cfd->chardata,
   4590                        32 * cfd->charcount );
   4591       }
   4592       break;
   4593 
   4594    case VKI_PIO_FONTRESET:
   4595       break;
   4596 
   4597    case VKI_GIO_CMAP:
   4598       PRE_MEM_WRITE( "ioctl(GIO_CMAP)", ARG3, 16 * 3 );
   4599       break;
   4600    case VKI_PIO_CMAP:
   4601       PRE_MEM_READ( "ioctl(PIO_CMAP)", ARG3, 16 * 3 );
   4602       break;
   4603 
   4604    case VKI_KIOCSOUND:
   4605    case VKI_KDMKTONE:
   4606       break;
   4607 
   4608    case VKI_KDGETLED:
   4609       PRE_MEM_WRITE( "ioctl(KDGETLED)", ARG3, sizeof(char) );
   4610       break;
   4611    case VKI_KDSETLED:
   4612       break;
   4613 
   4614    case VKI_KDGKBTYPE:
   4615       PRE_MEM_WRITE( "ioctl(KDGKBTYPE)", ARG3, sizeof(char) );
   4616       break;
   4617 
   4618    case VKI_KDADDIO:
   4619    case VKI_KDDELIO:
   4620    case VKI_KDENABIO:
   4621    case VKI_KDDISABIO:
   4622       break;
   4623 
   4624    case VKI_KDSETMODE:
   4625       break;
   4626    case VKI_KDGETMODE:
   4627       PRE_MEM_WRITE( "ioctl(KDGETMODE)", ARG3, sizeof(int) );
   4628       break;
   4629 
   4630    case VKI_KDMAPDISP:
   4631    case VKI_KDUNMAPDISP:
   4632       break;
   4633 
   4634    case VKI_GIO_SCRNMAP:
   4635       PRE_MEM_WRITE( "ioctl(GIO_SCRNMAP)", ARG3, VKI_E_TABSZ );
   4636       break;
   4637    case VKI_PIO_SCRNMAP:
   4638       PRE_MEM_READ( "ioctl(PIO_SCRNMAP)", ARG3, VKI_E_TABSZ  );
   4639       break;
   4640    case VKI_GIO_UNISCRNMAP:
   4641       PRE_MEM_WRITE( "ioctl(GIO_UNISCRNMAP)", ARG3,
   4642                      VKI_E_TABSZ * sizeof(unsigned short) );
   4643       break;
   4644    case VKI_PIO_UNISCRNMAP:
   4645       PRE_MEM_READ( "ioctl(PIO_UNISCRNMAP)", ARG3,
   4646                     VKI_E_TABSZ * sizeof(unsigned short) );
   4647       break;
   4648 
   4649    case VKI_GIO_UNIMAP:
   4650       if ( ARG3 ) {
   4651          struct vki_unimapdesc *desc = (struct vki_unimapdesc *) ARG3;
   4652          PRE_MEM_READ( "ioctl(GIO_UNIMAP)", (Addr)&desc->entry_ct,
   4653                        sizeof(unsigned short));
   4654          PRE_MEM_READ( "ioctl(GIO_UNIMAP)", (Addr)&desc->entries,
   4655                        sizeof(struct vki_unipair *));
   4656          PRE_MEM_WRITE( "ioctl(GIO_UNIMAP).entries", (Addr)desc->entries,
   4657                         desc->entry_ct * sizeof(struct vki_unipair));
   4658       }
   4659       break;
   4660    case VKI_PIO_UNIMAP:
   4661       if ( ARG3 ) {
   4662          struct vki_unimapdesc *desc = (struct vki_unimapdesc *) ARG3;
   4663          PRE_MEM_READ( "ioctl(GIO_UNIMAP)", (Addr)&desc->entry_ct,
   4664                        sizeof(unsigned short) );
   4665          PRE_MEM_READ( "ioctl(GIO_UNIMAP)", (Addr)&desc->entries,
   4666                        sizeof(struct vki_unipair *) );
   4667          PRE_MEM_READ( "ioctl(PIO_UNIMAP).entries", (Addr)desc->entries,
   4668                        desc->entry_ct * sizeof(struct vki_unipair) );
   4669       }
   4670       break;
   4671    case VKI_PIO_UNIMAPCLR:
   4672       PRE_MEM_READ( "ioctl(GIO_UNIMAP)", ARG3, sizeof(struct vki_unimapinit));
   4673       break;
   4674 
   4675    case VKI_KDGKBMODE:
   4676       PRE_MEM_WRITE( "ioctl(KDGKBMODE)", ARG3, sizeof(int) );
   4677       break;
   4678    case VKI_KDSKBMODE:
   4679       break;
   4680 
   4681    case VKI_KDGKBMETA:
   4682       PRE_MEM_WRITE( "ioctl(KDGKBMETA)", ARG3, sizeof(int) );
   4683       break;
   4684    case VKI_KDSKBMETA:
   4685       break;
   4686 
   4687    case VKI_KDGKBLED:
   4688       PRE_MEM_WRITE( "ioctl(KDGKBLED)", ARG3, sizeof(char) );
   4689       break;
   4690    case VKI_KDSKBLED:
   4691       break;
   4692 
   4693    case VKI_KDGKBENT:
   4694       PRE_MEM_READ( "ioctl(KDGKBENT).kb_table",
   4695                     (Addr)&((struct vki_kbentry *)ARG3)->kb_table,
   4696                     sizeof(((struct vki_kbentry *)ARG3)->kb_table) );
   4697       PRE_MEM_READ( "ioctl(KDGKBENT).kb_index",
   4698                     (Addr)&((struct vki_kbentry *)ARG3)->kb_index,
   4699                     sizeof(((struct vki_kbentry *)ARG3)->kb_index) );
   4700       PRE_MEM_WRITE( "ioctl(KDGKBENT).kb_value",
   4701 		     (Addr)&((struct vki_kbentry *)ARG3)->kb_value,
   4702 		     sizeof(((struct vki_kbentry *)ARG3)->kb_value) );
   4703       break;
   4704    case VKI_KDSKBENT:
   4705       PRE_MEM_READ( "ioctl(KDSKBENT).kb_table",
   4706                     (Addr)&((struct vki_kbentry *)ARG3)->kb_table,
   4707                     sizeof(((struct vki_kbentry *)ARG3)->kb_table) );
   4708       PRE_MEM_READ( "ioctl(KDSKBENT).kb_index",
   4709                     (Addr)&((struct vki_kbentry *)ARG3)->kb_index,
   4710                     sizeof(((struct vki_kbentry *)ARG3)->kb_index) );
   4711       PRE_MEM_READ( "ioctl(KDSKBENT).kb_value",
   4712                     (Addr)&((struct vki_kbentry *)ARG3)->kb_value,
   4713                     sizeof(((struct vki_kbentry *)ARG3)->kb_value) );
   4714       break;
   4715 
   4716    case VKI_KDGKBSENT:
   4717       PRE_MEM_READ( "ioctl(KDGKBSENT).kb_func",
   4718                     (Addr)&((struct vki_kbsentry *)ARG3)->kb_func,
   4719                     sizeof(((struct vki_kbsentry *)ARG3)->kb_func) );
   4720       PRE_MEM_WRITE( "ioctl(KDGKSENT).kb_string",
   4721 		     (Addr)((struct vki_kbsentry *)ARG3)->kb_string,
   4722 		     sizeof(((struct vki_kbsentry *)ARG3)->kb_string) );
   4723       break;
   4724    case VKI_KDSKBSENT:
   4725       PRE_MEM_READ( "ioctl(KDSKBSENT).kb_func",
   4726                     (Addr)&((struct vki_kbsentry *)ARG3)->kb_func,
   4727                     sizeof(((struct vki_kbsentry *)ARG3)->kb_func) );
   4728       PRE_MEM_RASCIIZ( "ioctl(KDSKBSENT).kb_string",
   4729                        (Addr)((struct vki_kbsentry *)ARG3)->kb_string );
   4730       break;
   4731 
   4732    case VKI_KDGKBDIACR:
   4733       PRE_MEM_WRITE( "ioctl(KDGKBDIACR)", ARG3, sizeof(struct vki_kbdiacrs) );
   4734       break;
   4735    case VKI_KDSKBDIACR:
   4736       PRE_MEM_READ( "ioctl(KDSKBDIACR)", ARG3, sizeof(struct vki_kbdiacrs) );
   4737       break;
   4738 
   4739    case VKI_KDGETKEYCODE:
   4740       PRE_MEM_READ( "ioctl(KDGETKEYCODE).scancode",
   4741                     (Addr)&((struct vki_kbkeycode *)ARG3)->scancode,
   4742                     sizeof(((struct vki_kbkeycode *)ARG3)->scancode) );
   4743       PRE_MEM_WRITE( "ioctl(KDGETKEYCODE).keycode",
   4744 		     (Addr)((struct vki_kbkeycode *)ARG3)->keycode,
   4745 		     sizeof(((struct vki_kbkeycode *)ARG3)->keycode) );
   4746       break;
   4747    case VKI_KDSETKEYCODE:
   4748       PRE_MEM_READ( "ioctl(KDSETKEYCODE).scancode",
   4749                     (Addr)&((struct vki_kbkeycode *)ARG3)->scancode,
   4750                     sizeof(((struct vki_kbkeycode *)ARG3)->scancode) );
   4751       PRE_MEM_READ( "ioctl(KDSETKEYCODE).keycode",
   4752                     (Addr)((struct vki_kbkeycode *)ARG3)->keycode,
   4753                     sizeof(((struct vki_kbkeycode *)ARG3)->keycode) );
   4754       break;
   4755 
   4756    case VKI_KDSIGACCEPT:
   4757       break;
   4758 
   4759    case VKI_KDKBDREP:
   4760       PRE_MEM_READ( "ioctl(KBKBDREP)", ARG3, sizeof(struct vki_kbd_repeat) );
   4761       break;
   4762 
   4763    case VKI_KDFONTOP:
   4764       if ( ARG3 ) {
   4765          struct vki_console_font_op *op = (struct vki_console_font_op *) ARG3;
   4766          PRE_MEM_READ( "ioctl(KDFONTOP)", (Addr)op,
   4767                        sizeof(struct vki_console_font_op) );
   4768          switch ( op->op ) {
   4769             case VKI_KD_FONT_OP_SET:
   4770                PRE_MEM_READ( "ioctl(KDFONTOP,KD_FONT_OP_SET).data",
   4771                              (Addr)op->data,
   4772                              (op->width + 7) / 8 * 32 * op->charcount );
   4773                break;
   4774             case VKI_KD_FONT_OP_GET:
   4775                if ( op->data )
   4776                   PRE_MEM_WRITE( "ioctl(KDFONTOP,KD_FONT_OP_GET).data",
   4777                                  (Addr)op->data,
   4778                                  (op->width + 7) / 8 * 32 * op->charcount );
   4779                break;
   4780             case VKI_KD_FONT_OP_SET_DEFAULT:
   4781                if ( op->data )
   4782                   PRE_MEM_RASCIIZ( "ioctl(KDFONTOP,KD_FONT_OP_SET_DEFAULT).data",
   4783                                    (Addr)op->data );
   4784                break;
   4785             case VKI_KD_FONT_OP_COPY:
   4786                break;
   4787          }
   4788       }
   4789       break;
   4790 
   4791    case VKI_VT_OPENQRY:
   4792       PRE_MEM_WRITE( "ioctl(VT_OPENQRY)", ARG3, sizeof(int) );
   4793       break;
   4794    case VKI_VT_GETMODE:
   4795       PRE_MEM_WRITE( "ioctl(VT_GETMODE)", ARG3, sizeof(struct vki_vt_mode) );
   4796       break;
   4797    case VKI_VT_SETMODE:
   4798       PRE_MEM_READ( "ioctl(VT_SETMODE)", ARG3, sizeof(struct vki_vt_mode) );
   4799       break;
   4800    case VKI_VT_GETSTATE:
   4801       PRE_MEM_WRITE( "ioctl(VT_GETSTATE).v_active",
   4802                      (Addr) &(((struct vki_vt_stat*) ARG3)->v_active),
   4803                      sizeof(((struct vki_vt_stat*) ARG3)->v_active));
   4804       PRE_MEM_WRITE( "ioctl(VT_GETSTATE).v_state",
   4805                      (Addr) &(((struct vki_vt_stat*) ARG3)->v_state),
   4806                      sizeof(((struct vki_vt_stat*) ARG3)->v_state));
   4807       break;
   4808    case VKI_VT_RELDISP:
   4809    case VKI_VT_ACTIVATE:
   4810    case VKI_VT_WAITACTIVE:
   4811    case VKI_VT_DISALLOCATE:
   4812       break;
   4813    case VKI_VT_RESIZE:
   4814       PRE_MEM_READ( "ioctl(VT_RESIZE)", ARG3, sizeof(struct vki_vt_sizes) );
   4815       break;
   4816    case VKI_VT_RESIZEX:
   4817       PRE_MEM_READ( "ioctl(VT_RESIZEX)", ARG3, sizeof(struct vki_vt_consize) );
   4818       break;
   4819    case VKI_VT_LOCKSWITCH:
   4820    case VKI_VT_UNLOCKSWITCH:
   4821       break;
   4822 
   4823    case VKI_USBDEVFS_CONTROL:
   4824       if ( ARG3 ) {
   4825          struct vki_usbdevfs_ctrltransfer *vkuc = (struct vki_usbdevfs_ctrltransfer *)ARG3;
   4826          PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).bRequestType", (Addr)&vkuc->bRequestType, sizeof(vkuc->bRequestType));
   4827          PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).bRequest", (Addr)&vkuc->bRequest, sizeof(vkuc->bRequest));
   4828          PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).wValue", (Addr)&vkuc->wValue, sizeof(vkuc->wValue));
   4829          PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).wIndex", (Addr)&vkuc->wIndex, sizeof(vkuc->wIndex));
   4830          PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).wLength", (Addr)&vkuc->wLength, sizeof(vkuc->wLength));
   4831          PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).timeout", (Addr)&vkuc->timeout, sizeof(vkuc->timeout));
   4832          if (vkuc->bRequestType & 0x80)
   4833             PRE_MEM_WRITE( "ioctl(USBDEVFS_CONTROL).data", (Addr)vkuc->data, vkuc->wLength);
   4834          else
   4835             PRE_MEM_READ( "ioctl(USBDEVFS_CONTROL).data", (Addr)vkuc->data, vkuc->wLength);
   4836       }
   4837       break;
   4838    case VKI_USBDEVFS_BULK:
   4839       if ( ARG3 ) {
   4840          struct vki_usbdevfs_bulktransfer *vkub = (struct vki_usbdevfs_bulktransfer *)ARG3;
   4841          PRE_MEM_READ( "ioctl(USBDEVFS_BULK)", ARG3, sizeof(struct vki_usbdevfs_bulktransfer));
   4842          if (vkub->ep & 0x80)
   4843             PRE_MEM_WRITE( "ioctl(USBDEVFS_BULK).data", (Addr)vkub->data, vkub->len);
   4844          else
   4845             PRE_MEM_READ( "ioctl(USBDEVFS_BULK).data", (Addr)vkub->data, vkub->len);
   4846       }
   4847       break;
   4848    case VKI_USBDEVFS_GETDRIVER:
   4849       if ( ARG3 ) {
   4850          struct vki_usbdevfs_getdriver *vkugd = (struct vki_usbdevfs_getdriver *) ARG3;
   4851          PRE_MEM_WRITE( "ioctl(USBDEVFS_GETDRIVER)", (Addr)&vkugd->driver, sizeof(vkugd->driver));
   4852       }
   4853       break;
   4854    case VKI_USBDEVFS_SUBMITURB:
   4855       if ( ARG3 ) {
   4856          struct vki_usbdevfs_urb *vkuu = (struct vki_usbdevfs_urb *)ARG3;
   4857 
   4858          /* Not the whole struct needs to be initialized */
   4859          PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).endpoint", (Addr)&vkuu->endpoint, sizeof(vkuu->endpoint));
   4860          PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).type", (Addr)&vkuu->type, sizeof(vkuu->type));
   4861          PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).flags", (Addr)&vkuu->flags, sizeof(vkuu->flags));
   4862          PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer", (Addr)&vkuu->buffer, sizeof(vkuu->buffer));
   4863          PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).signr", (Addr)&vkuu->signr, sizeof(vkuu->signr));
   4864          PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).status", (Addr)&vkuu->status, sizeof(vkuu->status));
   4865          if (vkuu->type == VKI_USBDEVFS_URB_TYPE_CONTROL) {
   4866             struct vki_usbdevfs_setuppacket *vkusp = (struct vki_usbdevfs_setuppacket *)vkuu->buffer;
   4867             PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer_length", (Addr)&vkuu->buffer_length, sizeof(vkuu->buffer_length));
   4868             PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer.setup_packet", (Addr)vkusp, sizeof(*vkusp));
   4869             if (vkusp->bRequestType & 0x80)
   4870                PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).buffer.data", (Addr)(vkusp+1), vkuu->buffer_length - sizeof(*vkusp));
   4871             else
   4872                PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer.data", (Addr)(vkusp+1), vkuu->buffer_length - sizeof(*vkusp));
   4873             PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).actual_length", (Addr)&vkuu->actual_length, sizeof(vkuu->actual_length));
   4874          } else if (vkuu->type == VKI_USBDEVFS_URB_TYPE_ISO) {
   4875             int total_length = 0;
   4876             int i;
   4877             PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).number_of_packets", (Addr)&vkuu->number_of_packets, sizeof(vkuu->number_of_packets));
   4878             for(i=0; i<vkuu->number_of_packets; i++) {
   4879                PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).iso_frame_desc[].length", (Addr)&vkuu->iso_frame_desc[i].length, sizeof(vkuu->iso_frame_desc[i].length));
   4880                PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).iso_frame_desc[].actual_length", (Addr)&vkuu->iso_frame_desc[i].actual_length, sizeof(vkuu->iso_frame_desc[i].actual_length));
   4881                PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).iso_frame_desc[].status", (Addr)&vkuu->iso_frame_desc[i].status, sizeof(vkuu->iso_frame_desc[i].status));
   4882                total_length += vkuu->iso_frame_desc[i].length;
   4883             }
   4884             if (vkuu->endpoint & 0x80)
   4885                PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).buffer", (Addr)vkuu->buffer, total_length);
   4886             else
   4887                PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer", (Addr)vkuu->buffer, total_length);
   4888             PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).error_count", (Addr)&vkuu->error_count, sizeof(vkuu->error_count));
   4889          } else {
   4890             PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer_length", (Addr)&vkuu->buffer_length, sizeof(vkuu->buffer_length));
   4891             if (vkuu->endpoint & 0x80)
   4892                PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).buffer", (Addr)vkuu->buffer, vkuu->buffer_length);
   4893             else
   4894                PRE_MEM_READ( "ioctl(USBDEVFS_SUBMITURB).buffer", (Addr)vkuu->buffer, vkuu->buffer_length);
   4895             PRE_MEM_WRITE( "ioctl(USBDEVFS_SUBMITURB).actual_length", (Addr)&vkuu->actual_length, sizeof(vkuu->actual_length));
   4896          }
   4897       }
   4898       break;
   4899    case VKI_USBDEVFS_DISCARDURB:
   4900       break;
   4901    case VKI_USBDEVFS_REAPURB:
   4902       if ( ARG3 ) {
   4903          PRE_MEM_WRITE( "ioctl(USBDEVFS_REAPURB)", ARG3, sizeof(struct vki_usbdevfs_urb **));
   4904       }
   4905       break;
   4906    case VKI_USBDEVFS_REAPURBNDELAY:
   4907       if ( ARG3 ) {
   4908          PRE_MEM_WRITE( "ioctl(USBDEVFS_REAPURBNDELAY)", ARG3, sizeof(struct vki_usbdevfs_urb **));
   4909       }
   4910       break;
   4911    case VKI_USBDEVFS_CONNECTINFO:
   4912       PRE_MEM_WRITE( "ioctl(USBDEVFS_CONNECTINFO)", ARG3, sizeof(struct vki_usbdevfs_connectinfo));
   4913       break;
   4914    case VKI_USBDEVFS_IOCTL:
   4915       if ( ARG3 ) {
   4916          struct vki_usbdevfs_ioctl *vkui = (struct vki_usbdevfs_ioctl *)ARG3;
   4917          UInt dir2, size2;
   4918          PRE_MEM_READ("ioctl(USBDEVFS_IOCTL)", (Addr)vkui, sizeof(struct vki_usbdevfs_ioctl));
   4919          dir2  = _VKI_IOC_DIR(vkui->ioctl_code);
   4920          size2 = _VKI_IOC_SIZE(vkui->ioctl_code);
   4921          if (size2 > 0) {
   4922             if (dir2 & _VKI_IOC_WRITE)
   4923                PRE_MEM_READ("ioctl(USBDEVFS_IOCTL).dataWrite", (Addr)vkui->data, size2);
   4924             else if (dir2 & _VKI_IOC_READ)
   4925                PRE_MEM_WRITE("ioctl(USBDEVFS_IOCTL).dataRead", (Addr)vkui->data, size2);
   4926          }
   4927       }
   4928       break;
   4929    case VKI_USBDEVFS_RESET:
   4930       break;
   4931 
   4932       /* I2C (/dev/i2c-*) ioctls */
   4933    case VKI_I2C_SLAVE:
   4934    case VKI_I2C_SLAVE_FORCE:
   4935    case VKI_I2C_TENBIT:
   4936    case VKI_I2C_PEC:
   4937       break;
   4938    case VKI_I2C_FUNCS:
   4939       PRE_MEM_WRITE( "ioctl(I2C_FUNCS)", ARG3, sizeof(unsigned long) );
   4940       break;
   4941 
   4942       /* Wireless extensions ioctls */
   4943    case VKI_SIOCSIWCOMMIT:
   4944    case VKI_SIOCSIWNWID:
   4945    case VKI_SIOCSIWFREQ:
   4946    case VKI_SIOCSIWMODE:
   4947    case VKI_SIOCSIWSENS:
   4948    case VKI_SIOCSIWRANGE:
   4949    case VKI_SIOCSIWPRIV:
   4950    case VKI_SIOCSIWSTATS:
   4951    case VKI_SIOCSIWSPY:
   4952    case VKI_SIOCSIWTHRSPY:
   4953    case VKI_SIOCSIWAP:
   4954    case VKI_SIOCSIWSCAN:
   4955    case VKI_SIOCSIWESSID:
   4956    case VKI_SIOCSIWRATE:
   4957    case VKI_SIOCSIWNICKN:
   4958    case VKI_SIOCSIWRTS:
   4959    case VKI_SIOCSIWFRAG:
   4960    case VKI_SIOCSIWTXPOW:
   4961    case VKI_SIOCSIWRETRY:
   4962    case VKI_SIOCSIWENCODE:
   4963    case VKI_SIOCSIWPOWER:
   4964    case VKI_SIOCSIWGENIE:
   4965    case VKI_SIOCSIWMLME:
   4966    case VKI_SIOCSIWAUTH:
   4967    case VKI_SIOCSIWENCODEEXT:
   4968    case VKI_SIOCSIWPMKSA:
   4969       break;
   4970    case VKI_SIOCGIWNAME:
   4971       if (ARG3) {
   4972          PRE_MEM_WRITE("ioctl(SIOCGIWNAME)",
   4973                        (Addr)((struct vki_iwreq *)ARG3)->u.name,
   4974                        sizeof(((struct vki_iwreq *)ARG3)->u.name));
   4975       }
   4976       break;
   4977    case VKI_SIOCGIWNWID:
   4978    case VKI_SIOCGIWSENS:
   4979    case VKI_SIOCGIWRATE:
   4980    case VKI_SIOCGIWRTS:
   4981    case VKI_SIOCGIWFRAG:
   4982    case VKI_SIOCGIWTXPOW:
   4983    case VKI_SIOCGIWRETRY:
   4984    case VKI_SIOCGIWPOWER:
   4985    case VKI_SIOCGIWAUTH:
   4986       if (ARG3) {
   4987          PRE_MEM_WRITE("ioctl(SIOCGIW[NWID|SENS|RATE|RTS|FRAG|TXPOW|"
   4988                        "RETRY|PARAM|AUTH])",
   4989                        (Addr)&((struct vki_iwreq *)ARG3)->u.nwid,
   4990                        sizeof(struct vki_iw_param));
   4991       }
   4992       break;
   4993    case VKI_SIOCGIWFREQ:
   4994       if (ARG3) {
   4995          PRE_MEM_WRITE("ioctl(SIOCGIWFREQ",
   4996                        (Addr)&((struct vki_iwreq *)ARG3)->u.freq,
   4997                        sizeof(struct vki_iw_freq));
   4998       }
   4999       break;
   5000    case VKI_SIOCGIWMODE:
   5001       if (ARG3) {
   5002          PRE_MEM_WRITE("ioctl(SIOCGIWMODE",
   5003                        (Addr)&((struct vki_iwreq *)ARG3)->u.mode,
   5004                        sizeof(__vki_u32));
   5005       }
   5006       break;
   5007    case VKI_SIOCGIWRANGE:
   5008    case VKI_SIOCGIWPRIV:
   5009    case VKI_SIOCGIWSTATS:
   5010    case VKI_SIOCGIWSPY:
   5011    case VKI_SIOCGIWTHRSPY:
   5012    case VKI_SIOCGIWAPLIST:
   5013    case VKI_SIOCGIWSCAN:
   5014    case VKI_SIOCGIWESSID:
   5015    case VKI_SIOCGIWNICKN:
   5016    case VKI_SIOCGIWENCODE:
   5017    case VKI_SIOCGIWGENIE:
   5018    case VKI_SIOCGIWENCODEEXT:
   5019       if (ARG3) {
   5020          struct vki_iw_point* point;
   5021          point = &((struct vki_iwreq *)ARG3)->u.data;
   5022          PRE_MEM_WRITE("ioctl(SIOCGIW[RANGE|PRIV|STATS|SPY|THRSPY|"
   5023                        "APLIST|SCAN|ESSID|NICKN|ENCODE|GENIE|ENCODEEXT])",
   5024                        (Addr)point->pointer, point->length);
   5025       }
   5026       break;
   5027    case VKI_SIOCGIWAP:
   5028       if (ARG3) {
   5029          PRE_MEM_WRITE("ioctl(SIOCGIWAP)",
   5030                        (Addr)&((struct vki_iwreq *)ARG3)->u.ap_addr,
   5031                        sizeof(struct vki_sockaddr));
   5032       }
   5033       break;
   5034 
   5035 #  if defined(VGPV_arm_linux_android)
   5036    /* ashmem */
   5037    case VKI_ASHMEM_GET_SIZE:
   5038    case VKI_ASHMEM_SET_SIZE:
   5039    case VKI_ASHMEM_GET_PROT_MASK:
   5040    case VKI_ASHMEM_SET_PROT_MASK:
   5041    case VKI_ASHMEM_GET_PIN_STATUS:
   5042    case VKI_ASHMEM_PURGE_ALL_CACHES:
   5043        break;
   5044    case VKI_ASHMEM_GET_NAME:
   5045        PRE_MEM_WRITE( "ioctl(ASHMEM_SET_NAME)", ARG3, VKI_ASHMEM_NAME_LEN );
   5046        break;
   5047    case VKI_ASHMEM_SET_NAME:
   5048        PRE_MEM_RASCIIZ( "ioctl(ASHMEM_SET_NAME)", ARG3);
   5049        break;
   5050    case VKI_ASHMEM_PIN:
   5051    case VKI_ASHMEM_UNPIN:
   5052        PRE_MEM_READ( "ioctl(ASHMEM_PIN|ASHMEM_UNPIN)",
   5053                      ARG3, sizeof(struct vki_ashmem_pin) );
   5054        break;
   5055 
   5056    /* binder */
   5057    case VKI_BINDER_WRITE_READ:
   5058        if (ARG3) {
   5059            struct vki_binder_write_read* bwr
   5060               = (struct vki_binder_write_read*)ARG3;
   5061 
   5062            PRE_FIELD_READ("ioctl(BINDER_WRITE_READ).write_buffer",
   5063                           bwr->write_buffer);
   5064            PRE_FIELD_READ("ioctl(BINDER_WRITE_READ).write_size",
   5065                           bwr->write_size);
   5066            PRE_FIELD_READ("ioctl(BINDER_WRITE_READ).write_consumed",
   5067                           bwr->write_consumed);
   5068            PRE_FIELD_READ("ioctl(BINDER_WRITE_READ).read_buffer",
   5069                           bwr->read_buffer);
   5070            PRE_FIELD_READ("ioctl(BINDER_WRITE_READ).read_size",
   5071                           bwr->read_size);
   5072            PRE_FIELD_READ("ioctl(BINDER_WRITE_READ).read_consumed",
   5073                           bwr->read_consumed);
   5074 
   5075            PRE_FIELD_WRITE("ioctl(BINDER_WRITE_READ).write_consumed",
   5076                            bwr->write_consumed);
   5077            PRE_FIELD_WRITE("ioctl(BINDER_WRITE_READ).read_consumed",
   5078                            bwr->read_consumed);
   5079 
   5080            if (bwr->read_size)
   5081                PRE_MEM_WRITE("ioctl(BINDER_WRITE_READ).read_buffer[]",
   5082                              (Addr)bwr->read_buffer, bwr->read_size);
   5083            if (bwr->write_size)
   5084                PRE_MEM_READ("ioctl(BINDER_WRITE_READ).write_buffer[]",
   5085                             (Addr)bwr->write_buffer, bwr->write_size);
   5086        }
   5087        break;
   5088 
   5089    case VKI_BINDER_SET_IDLE_TIMEOUT:
   5090    case VKI_BINDER_SET_MAX_THREADS:
   5091    case VKI_BINDER_SET_IDLE_PRIORITY:
   5092    case VKI_BINDER_SET_CONTEXT_MGR:
   5093    case VKI_BINDER_THREAD_EXIT:
   5094        break;
   5095    case VKI_BINDER_VERSION:
   5096        if (ARG3) {
   5097            struct vki_binder_version* bv = (struct vki_binder_version*)ARG3;
   5098            PRE_FIELD_WRITE("ioctl(BINDER_VERSION)", bv->protocol_version);
   5099        }
   5100        break;
   5101 #  endif /* defined(VGPV_arm_linux_android) */
   5102 
   5103    default:
   5104       /* EVIOC* are variable length and return size written on success */
   5105       switch (ARG2 & ~(_VKI_IOC_SIZEMASK << _VKI_IOC_SIZESHIFT)) {
   5106       case VKI_EVIOCGNAME(0):
   5107       case VKI_EVIOCGPHYS(0):
   5108       case VKI_EVIOCGUNIQ(0):
   5109       case VKI_EVIOCGKEY(0):
   5110       case VKI_EVIOCGLED(0):
   5111       case VKI_EVIOCGSND(0):
   5112       case VKI_EVIOCGSW(0):
   5113       case VKI_EVIOCGBIT(VKI_EV_SYN,0):
   5114       case VKI_EVIOCGBIT(VKI_EV_KEY,0):
   5115       case VKI_EVIOCGBIT(VKI_EV_REL,0):
   5116       case VKI_EVIOCGBIT(VKI_EV_ABS,0):
   5117       case VKI_EVIOCGBIT(VKI_EV_MSC,0):
   5118       case VKI_EVIOCGBIT(VKI_EV_SW,0):
   5119       case VKI_EVIOCGBIT(VKI_EV_LED,0):
   5120       case VKI_EVIOCGBIT(VKI_EV_SND,0):
   5121       case VKI_EVIOCGBIT(VKI_EV_REP,0):
   5122       case VKI_EVIOCGBIT(VKI_EV_FF,0):
   5123       case VKI_EVIOCGBIT(VKI_EV_PWR,0):
   5124       case VKI_EVIOCGBIT(VKI_EV_FF_STATUS,0):
   5125          PRE_MEM_WRITE("ioctl(EVIO*)", ARG3, _VKI_IOC_SIZE(ARG2));
   5126          break;
   5127       default:
   5128          ML_(PRE_unknown_ioctl)(tid, ARG2, ARG3);
   5129          break;
   5130       }
   5131       break;
   5132    }
   5133 }
   5134 
   5135 POST(sys_ioctl)
   5136 {
   5137    vg_assert(SUCCESS);
   5138 
   5139    /* --- BEGIN special IOCTL handlers for specific Android hardware --- */
   5140 
   5141 #  if defined(VGPV_arm_linux_android)
   5142 
   5143 #  if defined(ANDROID_HARDWARE_nexus_s)
   5144 
   5145    /* BEGIN undocumented ioctls for the graphics hardware (??)
   5146       (libpvr) on Nexus S */
   5147    if (ARG2 >= 0xC01C6700 && ARG2 <= 0xC01C67FF && ARG3 >= 0x1000) {
   5148       /* What's going on here: there appear to be a bunch of ioctls of
   5149          the form 0xC01C67xx which are undocumented, and if unhandled
   5150          give rise to a vast number of false positives in Memcheck.
   5151 
   5152          The "normal" intrepretation of an ioctl of this form would be
   5153          that the 3rd arg is a pointer to an area of size 0x1C (28
   5154          bytes) which is filled in by the kernel.  Hence you might
   5155          think that "POST_MEM_WRITE(ARG3, 28)" would fix it.  But it
   5156          doesn't.
   5157 
   5158          It requires POST_MEM_WRITE(ARG3, 256) to silence them.  One
   5159          interpretation of this is that ARG3 really does point to a 28
   5160          byte struct, but inside that are pointers to other areas also
   5161          filled in by the kernel.  If these happen to be allocated
   5162          just back up the stack then the 256 byte paint might cover
   5163          them too, somewhat indiscriminately.
   5164 
   5165          By printing out ARG3 and also the 28 bytes that it points at,
   5166          it's possible to guess that the 7 word structure has this form
   5167 
   5168            0            1    2    3        4    5        6
   5169            ioctl-number 0x1C ptr1 ptr1size ptr2 ptr2size aBitMask
   5170 
   5171          Unfortunately that doesn't seem to work for some reason, so
   5172          stay with the blunt-instrument approach for the time being.
   5173       */
   5174       if (1) {
   5175          /* blunt-instrument approach */
   5176          if (0) VG_(printf)("QQQQQQQQQQ c01c quick hack actioned (%08lx, %08lx)\n", ARG2, ARG3);
   5177          POST_MEM_WRITE(ARG3, 256);
   5178       } else {
   5179          /* be a bit more sophisticated */
   5180          if (0) VG_(printf)("QQQQQQQQQQ c01c quick hack actioned (%08lx, %08lx) (fancy)\n", ARG2, ARG3);
   5181          POST_MEM_WRITE(ARG3, 28);
   5182          UInt* word = (UInt*)ARG3;
   5183          if (word && word[2] && word[3] < 0x200/*stay sane*/)
   5184             POST_MEM_WRITE(word[2], word[3]); // "ptr1"
   5185          if (word && word[4] && word[5] < 0x200/*stay sane*/)
   5186             POST_MEM_WRITE(word[4], word[5]); // "ptr2"
   5187       }
   5188       if (0) {
   5189          Int i;
   5190          VG_(printf)("QQQQQQQQQQ ");
   5191          for (i = 0; i < (0x1C/4); i++) {
   5192             VG_(printf)("%08x ", ((UInt*)(ARG3))[i]);
   5193          }
   5194          VG_(printf)("\n");
   5195       }
   5196       return;
   5197    }
   5198    /* END Nexus S specific ioctls */
   5199 
   5200 #  else /* no ANDROID_HARDWARE_anything defined */
   5201 
   5202 #   warning ""
   5203 #   warning "You need to define one the CPP symbols ANDROID_HARDWARE_blah"
   5204 #   warning "at configure time, to tell Valgrind what hardware you are"
   5205 #   warning "building for.  Currently known values are"
   5206 #   warning ""
   5207 #   warning "   ANDROID_HARDWARE_nexus_s       Samsung Nexus S"
   5208 #   warning ""
   5209 #   warning "Make sure you exactly follow the steps in README.android."
   5210 #   warning ""
   5211 #   error "No CPP symbol ANDROID_HARDWARE_blah defined.  Giving up."
   5212 
   5213 #  endif /* cases for ANDROID_HARDWARE_blah */
   5214 
   5215 #  endif /* defined(VGPV_arm_linux_android) */
   5216 
   5217    /* --- END special IOCTL handlers for specific Android hardware --- */
   5218 
   5219    /* --- normal handling --- */
   5220    switch (ARG2 /* request */) {
   5221    case VKI_TCSETS:
   5222    case VKI_TCSETSW:
   5223    case VKI_TCSETSF:
   5224       break;
   5225    case VKI_TCGETS:
   5226       POST_MEM_WRITE( ARG3, sizeof(struct vki_termios) );
   5227       break;
   5228    case VKI_TCSETA:
   5229    case VKI_TCSETAW:
   5230    case VKI_TCSETAF:
   5231       break;
   5232    case VKI_TCGETA:
   5233       POST_MEM_WRITE( ARG3, sizeof(struct vki_termio) );
   5234       break;
   5235    case VKI_TCSBRK:
   5236    case VKI_TCXONC:
   5237    case VKI_TCSBRKP:
   5238    case VKI_TCFLSH:
   5239       break;
   5240    case VKI_TIOCGWINSZ:
   5241       POST_MEM_WRITE( ARG3, sizeof(struct vki_winsize) );
   5242       break;
   5243    case VKI_TIOCSWINSZ:
   5244    case VKI_TIOCMBIS:
   5245    case VKI_TIOCMBIC:
   5246    case VKI_TIOCMSET:
   5247       break;
   5248    case VKI_TIOCMGET:
   5249       POST_MEM_WRITE( ARG3, sizeof(unsigned int) );
   5250       break;
   5251    case VKI_TIOCLINUX:
   5252       POST_MEM_WRITE( ARG3, sizeof(char *) );
   5253       break;
   5254    case VKI_TIOCGPGRP:
   5255       /* Get process group ID for foreground processing group. */
   5256       POST_MEM_WRITE( ARG3, sizeof(vki_pid_t) );
   5257       break;
   5258    case VKI_TIOCSPGRP:
   5259       /* Set a process group ID? */
   5260       POST_MEM_WRITE( ARG3, sizeof(vki_pid_t) );
   5261       break;
   5262    case VKI_TIOCGPTN: /* Get Pty Number (of pty-mux device) */
   5263       POST_MEM_WRITE( ARG3, sizeof(int));
   5264       break;
   5265    case VKI_TIOCSCTTY:
   5266       break;
   5267    case VKI_TIOCSPTLCK: /* Lock/unlock Pty */
   5268       break;
   5269    case VKI_FIONBIO:
   5270       break;
   5271    case VKI_FIOASYNC:
   5272       break;
   5273    case VKI_FIONREAD:                /* identical to SIOCINQ */
   5274       POST_MEM_WRITE( ARG3, sizeof(int) );
   5275       break;
   5276    case VKI_FIOQSIZE:
   5277       POST_MEM_WRITE( ARG3, sizeof(vki_loff_t) );
   5278       break;
   5279 
   5280    case VKI_TIOCSERGETLSR:
   5281       POST_MEM_WRITE( ARG3, sizeof(int) );
   5282       break;
   5283    case VKI_TIOCGICOUNT:
   5284       POST_MEM_WRITE( ARG3, sizeof(struct vki_serial_icounter_struct) );
   5285       break;
   5286 
   5287    case VKI_SG_SET_COMMAND_Q:
   5288       break;
   5289    case VKI_SG_IO:
   5290       POST_MEM_WRITE(ARG3, sizeof(vki_sg_io_hdr_t));
   5291       break;
   5292    case VKI_SG_GET_SCSI_ID:
   5293       POST_MEM_WRITE(ARG3, sizeof(vki_sg_scsi_id_t));
   5294       break;
   5295    case VKI_SG_SET_RESERVED_SIZE:
   5296       break;
   5297    case VKI_SG_SET_TIMEOUT:
   5298       break;
   5299    case VKI_SG_GET_RESERVED_SIZE:
   5300       POST_MEM_WRITE(ARG3, sizeof(int));
   5301       break;
   5302    case VKI_SG_GET_TIMEOUT:
   5303       break;
   5304    case VKI_SG_GET_VERSION_NUM:
   5305       POST_MEM_WRITE(ARG3, sizeof(int));
   5306       break;
   5307    case VKI_SG_EMULATED_HOST:
   5308       POST_MEM_WRITE(ARG3, sizeof(int));
   5309       break;
   5310    case VKI_SG_GET_SG_TABLESIZE:
   5311       POST_MEM_WRITE(ARG3, sizeof(int));
   5312       break;
   5313 
   5314    case VKI_IIOCGETCPS:
   5315       POST_MEM_WRITE( ARG3, VKI_ISDN_MAX_CHANNELS * 2 * sizeof(unsigned long) );
   5316       break;
   5317    case VKI_IIOCNETGPN:
   5318       POST_MEM_WRITE( ARG3, sizeof(vki_isdn_net_ioctl_phone) );
   5319       break;
   5320 
   5321       /* These all use struct ifreq AFAIK */
   5322    case VKI_SIOCGIFINDEX:        /* get iface index              */
   5323       POST_MEM_WRITE( (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_ifindex,
   5324                       sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_ifindex) );
   5325       break;
   5326    case VKI_SIOCGIFFLAGS:        /* get flags                    */
   5327       POST_MEM_WRITE( (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_flags,
   5328                       sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_flags) );
   5329       break;
   5330    case VKI_SIOCGIFHWADDR:       /* Get hardware address         */
   5331       POST_MEM_WRITE( (Addr)&((struct vki_ifreq *)ARG3)->ifr_hwaddr,
   5332                       sizeof(((struct vki_ifreq *)ARG3)->ifr_hwaddr) );
   5333       break;
   5334    case VKI_SIOCGIFMTU:          /* get MTU size                 */
   5335       POST_MEM_WRITE( (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_mtu,
   5336                       sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_mtu) );
   5337       break;
   5338    case VKI_SIOCGIFADDR:         /* get PA address               */
   5339    case VKI_SIOCGIFDSTADDR:      /* get remote PA address        */
   5340    case VKI_SIOCGIFBRDADDR:      /* get broadcast PA address     */
   5341    case VKI_SIOCGIFNETMASK:      /* get network PA mask          */
   5342       POST_MEM_WRITE(
   5343                 (Addr)&((struct vki_ifreq *)ARG3)->ifr_addr,
   5344                 sizeof(((struct vki_ifreq *)ARG3)->ifr_addr) );
   5345       break;
   5346    case VKI_SIOCGIFMETRIC:       /* get metric                   */
   5347       POST_MEM_WRITE(
   5348                 (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_metric,
   5349                 sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_metric) );
   5350       break;
   5351    case VKI_SIOCGIFMAP:          /* Get device parameters        */
   5352       POST_MEM_WRITE(
   5353                 (Addr)&((struct vki_ifreq *)ARG3)->ifr_map,
   5354                 sizeof(((struct vki_ifreq *)ARG3)->ifr_map) );
   5355       break;
   5356      break;
   5357    case VKI_SIOCGIFTXQLEN:       /* Get the tx queue length      */
   5358       POST_MEM_WRITE(
   5359                 (Addr)&((struct vki_ifreq *)ARG3)->ifr_qlen,
   5360                 sizeof(((struct vki_ifreq *)ARG3)->ifr_qlen) );
   5361       break;
   5362    case VKI_SIOCGIFNAME:         /* get iface name               */
   5363       POST_MEM_WRITE(
   5364                 (Addr)&((struct vki_ifreq *)ARG3)->vki_ifr_name,
   5365                 sizeof(((struct vki_ifreq *)ARG3)->vki_ifr_name) );
   5366       break;
   5367    case VKI_SIOCGMIIPHY:         /* get hardware entry           */
   5368       POST_MEM_WRITE(
   5369                 (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->phy_id,
   5370                 sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->phy_id) );
   5371       break;
   5372    case VKI_SIOCGMIIREG:         /* get hardware entry registers */
   5373       POST_MEM_WRITE(
   5374                 (Addr)&((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->val_out,
   5375                 sizeof(((struct vki_mii_ioctl_data *)&((struct vki_ifreq *)ARG3)->vki_ifr_data)->val_out) );
   5376       break;
   5377    case VKI_SIOCGIFCONF:         /* get iface list               */
   5378       /* WAS:
   5379 	 PRE_MEM_WRITE("ioctl(SIOCGIFCONF)", ARG3, sizeof(struct ifconf));
   5380 	 KERNEL_DO_SYSCALL(tid,RES);
   5381 	 if (!VG_(is_kerror)(RES) && RES == 0)
   5382 	 POST_MEM_WRITE(ARG3, sizeof(struct ifconf));
   5383       */
   5384       if (RES == 0 && ARG3 ) {
   5385 	 struct vki_ifconf *ifc = (struct vki_ifconf *) ARG3;
   5386 	 if (ifc->vki_ifc_buf != NULL)
   5387 	    POST_MEM_WRITE( (Addr)(ifc->vki_ifc_buf), ifc->ifc_len );
   5388       }
   5389       break;
   5390    case VKI_SIOCGSTAMP:
   5391       POST_MEM_WRITE( ARG3, sizeof(struct vki_timeval) );
   5392       break;
   5393    case VKI_SIOCGSTAMPNS:
   5394       POST_MEM_WRITE( ARG3, sizeof(struct vki_timespec) );
   5395       break;
   5396       /* SIOCOUTQ is an ioctl that, when called on a socket, returns
   5397 	 the number of bytes currently in that socket's send buffer.
   5398 	 It writes this value as an int to the memory location
   5399 	 indicated by the third argument of ioctl(2). */
   5400    case VKI_SIOCOUTQ:
   5401       POST_MEM_WRITE(ARG3, sizeof(int));
   5402       break;
   5403    case VKI_SIOCGRARP:           /* get RARP table entry         */
   5404    case VKI_SIOCGARP:            /* get ARP table entry          */
   5405       POST_MEM_WRITE(ARG3, sizeof(struct vki_arpreq));
   5406       break;
   5407 
   5408    case VKI_SIOCSIFFLAGS:        /* set flags                    */
   5409    case VKI_SIOCSIFMAP:          /* Set device parameters        */
   5410    case VKI_SIOCSIFTXQLEN:       /* Set the tx queue length      */
   5411    case VKI_SIOCSIFDSTADDR:      /* set remote PA address        */
   5412    case VKI_SIOCSIFBRDADDR:      /* set broadcast PA address     */
   5413    case VKI_SIOCSIFNETMASK:      /* set network PA mask          */
   5414    case VKI_SIOCSIFMETRIC:       /* set metric                   */
   5415    case VKI_SIOCSIFADDR:         /* set PA address               */
   5416    case VKI_SIOCSIFMTU:          /* set MTU size                 */
   5417    case VKI_SIOCSIFHWADDR:       /* set hardware address         */
   5418    case VKI_SIOCSMIIREG:         /* set hardware entry registers */
   5419       break;
   5420       /* Routing table calls.  */
   5421    case VKI_SIOCADDRT:           /* add routing table entry      */
   5422    case VKI_SIOCDELRT:           /* delete routing table entry   */
   5423       break;
   5424 
   5425       /* RARP cache control calls. */
   5426    case VKI_SIOCDRARP:           /* delete RARP table entry      */
   5427    case VKI_SIOCSRARP:           /* set RARP table entry         */
   5428       /* ARP cache control calls. */
   5429    case VKI_SIOCSARP:            /* set ARP table entry          */
   5430    case VKI_SIOCDARP:            /* delete ARP table entry       */
   5431       break;
   5432 
   5433    case VKI_SIOCGPGRP:
   5434       POST_MEM_WRITE(ARG3, sizeof(int));
   5435       break;
   5436    case VKI_SIOCSPGRP:
   5437       break;
   5438 
   5439       /* linux/soundcard interface (OSS) */
   5440    case VKI_SNDCTL_SEQ_GETOUTCOUNT:
   5441    case VKI_SNDCTL_SEQ_GETINCOUNT:
   5442    case VKI_SNDCTL_SEQ_PERCMODE:
   5443    case VKI_SNDCTL_SEQ_TESTMIDI:
   5444    case VKI_SNDCTL_SEQ_RESETSAMPLES:
   5445    case VKI_SNDCTL_SEQ_NRSYNTHS:
   5446    case VKI_SNDCTL_SEQ_NRMIDIS:
   5447    case VKI_SNDCTL_SEQ_GETTIME:
   5448    case VKI_SNDCTL_DSP_GETBLKSIZE:
   5449    case VKI_SNDCTL_DSP_GETFMTS:
   5450    case VKI_SNDCTL_DSP_SETFMT:
   5451    case VKI_SNDCTL_DSP_GETTRIGGER:
   5452    case VKI_SNDCTL_DSP_GETODELAY:
   5453    case VKI_SNDCTL_DSP_GETSPDIF:
   5454    case VKI_SNDCTL_DSP_GETCAPS:
   5455    case VKI_SOUND_PCM_READ_RATE:
   5456    case VKI_SOUND_PCM_READ_CHANNELS:
   5457    case VKI_SOUND_PCM_READ_BITS:
   5458    case VKI_SOUND_PCM_READ_FILTER:
   5459       POST_MEM_WRITE(ARG3, sizeof(int));
   5460       break;
   5461    case VKI_SNDCTL_SEQ_CTRLRATE:
   5462    case VKI_SNDCTL_DSP_SPEED:
   5463    case VKI_SNDCTL_DSP_STEREO:
   5464    case VKI_SNDCTL_DSP_CHANNELS:
   5465    case VKI_SOUND_PCM_WRITE_FILTER:
   5466    case VKI_SNDCTL_DSP_SUBDIVIDE:
   5467    case VKI_SNDCTL_DSP_SETFRAGMENT:
   5468    case VKI_SNDCTL_DSP_GETCHANNELMASK:
   5469    case VKI_SNDCTL_DSP_BIND_CHANNEL:
   5470    case VKI_SNDCTL_TMR_TIMEBASE:
   5471    case VKI_SNDCTL_TMR_TEMPO:
   5472    case VKI_SNDCTL_TMR_SOURCE:
   5473    case VKI_SNDCTL_MIDI_PRETIME:
   5474    case VKI_SNDCTL_MIDI_MPUMODE:
   5475       break;
   5476    case VKI_SNDCTL_DSP_GETOSPACE:
   5477    case VKI_SNDCTL_DSP_GETISPACE:
   5478       POST_MEM_WRITE(ARG3, sizeof(vki_audio_buf_info));
   5479       break;
   5480    case VKI_SNDCTL_DSP_NONBLOCK:
   5481       break;
   5482    case VKI_SNDCTL_DSP_SETTRIGGER:
   5483       break;
   5484 
   5485    case VKI_SNDCTL_DSP_POST:
   5486    case VKI_SNDCTL_DSP_RESET:
   5487    case VKI_SNDCTL_DSP_SYNC:
   5488    case VKI_SNDCTL_DSP_SETSYNCRO:
   5489    case VKI_SNDCTL_DSP_SETDUPLEX:
   5490       break;
   5491 
   5492       /* linux/soundcard interface (ALSA) */
   5493    case VKI_SNDRV_PCM_IOCTL_HW_FREE:
   5494    case VKI_SNDRV_PCM_IOCTL_HWSYNC:
   5495    case VKI_SNDRV_PCM_IOCTL_PREPARE:
   5496    case VKI_SNDRV_PCM_IOCTL_RESET:
   5497    case VKI_SNDRV_PCM_IOCTL_START:
   5498    case VKI_SNDRV_PCM_IOCTL_DROP:
   5499    case VKI_SNDRV_PCM_IOCTL_DRAIN:
   5500    case VKI_SNDRV_PCM_IOCTL_RESUME:
   5501    case VKI_SNDRV_PCM_IOCTL_XRUN:
   5502    case VKI_SNDRV_PCM_IOCTL_UNLINK:
   5503    case VKI_SNDRV_TIMER_IOCTL_START:
   5504    case VKI_SNDRV_TIMER_IOCTL_STOP:
   5505    case VKI_SNDRV_TIMER_IOCTL_CONTINUE:
   5506    case VKI_SNDRV_TIMER_IOCTL_PAUSE:
   5507       break;
   5508 
   5509       /* Real Time Clock (/dev/rtc) ioctls */
   5510    case VKI_RTC_UIE_ON:
   5511    case VKI_RTC_UIE_OFF:
   5512    case VKI_RTC_AIE_ON:
   5513    case VKI_RTC_AIE_OFF:
   5514    case VKI_RTC_PIE_ON:
   5515    case VKI_RTC_PIE_OFF:
   5516    case VKI_RTC_IRQP_SET:
   5517       break;
   5518    case VKI_RTC_RD_TIME:
   5519    case VKI_RTC_ALM_READ:
   5520       POST_MEM_WRITE(ARG3, sizeof(struct vki_rtc_time));
   5521       break;
   5522    case VKI_RTC_ALM_SET:
   5523       break;
   5524    case VKI_RTC_IRQP_READ:
   5525       POST_MEM_WRITE(ARG3, sizeof(unsigned long));
   5526       break;
   5527 
   5528       /* Block devices */
   5529    case VKI_BLKROSET:
   5530       break;
   5531    case VKI_BLKROGET:
   5532       POST_MEM_WRITE(ARG3, sizeof(int));
   5533       break;
   5534    case VKI_BLKGETSIZE:
   5535       POST_MEM_WRITE(ARG3, sizeof(unsigned long));
   5536       break;
   5537    case VKI_BLKRASET:
   5538       break;
   5539    case VKI_BLKRAGET:
   5540       POST_MEM_WRITE(ARG3, sizeof(long));
   5541       break;
   5542    case VKI_BLKFRASET:
   5543       break;
   5544    case VKI_BLKFRAGET:
   5545       POST_MEM_WRITE(ARG3, sizeof(long));
   5546       break;
   5547    case VKI_BLKSECTGET:
   5548       POST_MEM_WRITE(ARG3, sizeof(unsigned short));
   5549       break;
   5550    case VKI_BLKSSZGET:
   5551       POST_MEM_WRITE(ARG3, sizeof(int));
   5552       break;
   5553    case VKI_BLKBSZGET:
   5554       POST_MEM_WRITE(ARG3, sizeof(int));
   5555       break;
   5556    case VKI_BLKBSZSET:
   5557       break;
   5558    case VKI_BLKGETSIZE64:
   5559       POST_MEM_WRITE(ARG3, sizeof(unsigned long long));
   5560       break;
   5561 
   5562       /* Hard disks */
   5563    case VKI_HDIO_GETGEO: /* 0x0301 */
   5564       POST_MEM_WRITE(ARG3, sizeof(struct vki_hd_geometry));
   5565       break;
   5566    case VKI_HDIO_GET_DMA: /* 0x030b */
   5567       POST_MEM_WRITE(ARG3, sizeof(long));
   5568       break;
   5569    case VKI_HDIO_GET_IDENTITY: /* 0x030d */
   5570       POST_MEM_WRITE(ARG3, VKI_SIZEOF_STRUCT_HD_DRIVEID );
   5571       break;
   5572 
   5573       /* CD ROM stuff (??)  */
   5574    case VKI_CDROMSUBCHNL:
   5575       POST_MEM_WRITE(ARG3, sizeof(struct vki_cdrom_subchnl));
   5576       break;
   5577    case VKI_CDROMREADTOCHDR:
   5578       POST_MEM_WRITE(ARG3, sizeof(struct vki_cdrom_tochdr));
   5579       break;
   5580    case VKI_CDROMREADTOCENTRY:
   5581       POST_MEM_WRITE(ARG3, sizeof(struct vki_cdrom_tocentry));
   5582       break;
   5583    case VKI_CDROMMULTISESSION:
   5584       POST_MEM_WRITE(ARG3, sizeof(struct vki_cdrom_multisession));
   5585       break;
   5586    case VKI_CDROMVOLREAD:
   5587       POST_MEM_WRITE(ARG3, sizeof(struct vki_cdrom_volctrl));
   5588       break;
   5589    case VKI_CDROMREADRAW:
   5590       POST_MEM_WRITE(ARG3, VKI_CD_FRAMESIZE_RAW);
   5591       break;
   5592    case VKI_CDROMREADAUDIO:
   5593    {
   5594       struct vki_cdrom_read_audio *cra = (struct vki_cdrom_read_audio *) ARG3;
   5595       POST_MEM_WRITE( (Addr)(cra->buf), cra->nframes * VKI_CD_FRAMESIZE_RAW);
   5596       break;
   5597    }
   5598 
   5599    case VKI_CDROMPLAYMSF:
   5600       break;
   5601       /* The following two are probably bogus (should check args
   5602 	 for readability).  JRS 20021117 */
   5603    case VKI_CDROM_DRIVE_STATUS: /* 0x5326 */
   5604    case VKI_CDROM_CLEAR_OPTIONS: /* 0x5321 */
   5605       break;
   5606 
   5607    case VKI_FIGETBSZ:
   5608       POST_MEM_WRITE(ARG3, sizeof(unsigned long));
   5609       break;
   5610    case VKI_FIBMAP:
   5611       POST_MEM_WRITE(ARG3, sizeof(int));
   5612       break;
   5613 
   5614    case VKI_FBIOGET_VSCREENINFO: //0x4600
   5615       POST_MEM_WRITE(ARG3, sizeof(struct vki_fb_var_screeninfo));
   5616       break;
   5617    case VKI_FBIOGET_FSCREENINFO: //0x4602
   5618       POST_MEM_WRITE(ARG3, sizeof(struct vki_fb_fix_screeninfo));
   5619       break;
   5620 
   5621    case VKI_PPCLAIM:
   5622    case VKI_PPEXCL:
   5623    case VKI_PPYIELD:
   5624    case VKI_PPRELEASE:
   5625    case VKI_PPSETMODE:
   5626    case VKI_PPSETPHASE:
   5627    case VKI_PPSETFLAGS:
   5628    case VKI_PPWDATA:
   5629    case VKI_PPWCONTROL:
   5630    case VKI_PPFCONTROL:
   5631    case VKI_PPDATADIR:
   5632    case VKI_PPNEGOT:
   5633    case VKI_PPWCTLONIRQ:
   5634    case VKI_PPSETTIME:
   5635       break;
   5636    case VKI_PPGETMODE:
   5637       POST_MEM_WRITE( ARG3, sizeof(int) );
   5638       break;
   5639    case VKI_PPGETPHASE:
   5640       POST_MEM_WRITE( ARG3, sizeof(int) );
   5641       break;
   5642    case VKI_PPGETMODES:
   5643       POST_MEM_WRITE( ARG3, sizeof(unsigned int) );
   5644       break;
   5645    case VKI_PPGETFLAGS:
   5646       POST_MEM_WRITE( ARG3, sizeof(int) );
   5647       break;
   5648    case VKI_PPRSTATUS:
   5649       POST_MEM_WRITE( ARG3, sizeof(unsigned char) );
   5650       break;
   5651    case VKI_PPRDATA:
   5652       POST_MEM_WRITE( ARG3, sizeof(unsigned char) );
   5653       break;
   5654    case VKI_PPRCONTROL:
   5655       POST_MEM_WRITE( ARG3, sizeof(unsigned char) );
   5656       break;
   5657    case VKI_PPCLRIRQ:
   5658       POST_MEM_WRITE( ARG3, sizeof(int) );
   5659       break;
   5660    case VKI_PPGETTIME:
   5661       POST_MEM_WRITE( ARG3, sizeof(struct vki_timeval) );
   5662       break;
   5663 
   5664    case VKI_GIO_FONT:
   5665       POST_MEM_WRITE( ARG3, 32 * 256 );
   5666       break;
   5667    case VKI_PIO_FONT:
   5668       break;
   5669 
   5670    case VKI_GIO_FONTX:
   5671       POST_MEM_WRITE( (Addr)((struct vki_consolefontdesc *)ARG3)->chardata,
   5672                       32 * ((struct vki_consolefontdesc *)ARG3)->charcount );
   5673       break;
   5674    case VKI_PIO_FONTX:
   5675       break;
   5676 
   5677    case VKI_PIO_FONTRESET:
   5678       break;
   5679 
   5680    case VKI_GIO_CMAP:
   5681       POST_MEM_WRITE( ARG3, 16 * 3 );
   5682       break;
   5683    case VKI_PIO_CMAP:
   5684       break;
   5685 
   5686    case VKI_KIOCSOUND:
   5687    case VKI_KDMKTONE:
   5688       break;
   5689 
   5690    case VKI_KDGETLED:
   5691       POST_MEM_WRITE( ARG3, sizeof(char) );
   5692       break;
   5693    case VKI_KDSETLED:
   5694       break;
   5695 
   5696    case VKI_KDGKBTYPE:
   5697       POST_MEM_WRITE( ARG3, sizeof(char) );
   5698       break;
   5699 
   5700    case VKI_KDADDIO:
   5701    case VKI_KDDELIO:
   5702    case VKI_KDENABIO:
   5703    case VKI_KDDISABIO:
   5704       break;
   5705 
   5706    case VKI_KDSETMODE:
   5707       break;
   5708    case VKI_KDGETMODE:
   5709       POST_MEM_WRITE( ARG3, sizeof(int) );
   5710       break;
   5711 
   5712    case VKI_KDMAPDISP:
   5713    case VKI_KDUNMAPDISP:
   5714       break;
   5715 
   5716    case VKI_GIO_SCRNMAP:
   5717       POST_MEM_WRITE( ARG3, VKI_E_TABSZ );
   5718       break;
   5719    case VKI_PIO_SCRNMAP:
   5720       break;
   5721    case VKI_GIO_UNISCRNMAP:
   5722       POST_MEM_WRITE( ARG3, VKI_E_TABSZ * sizeof(unsigned short) );
   5723       break;
   5724    case VKI_PIO_UNISCRNMAP:
   5725       break;
   5726 
   5727    case VKI_GIO_UNIMAP:
   5728       if ( ARG3 ) {
   5729          struct vki_unimapdesc *desc = (struct vki_unimapdesc *) ARG3;
   5730          POST_MEM_WRITE( (Addr)&desc->entry_ct, sizeof(desc->entry_ct));
   5731          POST_MEM_WRITE( (Addr)desc->entries,
   5732       	                 desc->entry_ct * sizeof(struct vki_unipair) );
   5733       }
   5734       break;
   5735    case VKI_PIO_UNIMAP:
   5736       break;
   5737    case VKI_PIO_UNIMAPCLR:
   5738       break;
   5739 
   5740    case VKI_KDGKBMODE:
   5741       POST_MEM_WRITE( ARG3, sizeof(int) );
   5742       break;
   5743    case VKI_KDSKBMODE:
   5744       break;
   5745 
   5746    case VKI_KDGKBMETA:
   5747       POST_MEM_WRITE( ARG3, sizeof(int) );
   5748       break;
   5749    case VKI_KDSKBMETA:
   5750       break;
   5751 
   5752    case VKI_KDGKBLED:
   5753       POST_MEM_WRITE( ARG3, sizeof(char) );
   5754       break;
   5755    case VKI_KDSKBLED:
   5756       break;
   5757 
   5758    case VKI_KDGKBENT:
   5759       POST_MEM_WRITE( (Addr)&((struct vki_kbentry *)ARG3)->kb_value,
   5760                       sizeof(((struct vki_kbentry *)ARG3)->kb_value) );
   5761       break;
   5762    case VKI_KDSKBENT:
   5763       break;
   5764 
   5765    case VKI_KDGKBSENT:
   5766       POST_MEM_WRITE( (Addr)((struct vki_kbsentry *)ARG3)->kb_string,
   5767                       sizeof(((struct vki_kbsentry *)ARG3)->kb_string) );
   5768       break;
   5769    case VKI_KDSKBSENT:
   5770       break;
   5771 
   5772    case VKI_KDGKBDIACR:
   5773       POST_MEM_WRITE( ARG3, sizeof(struct vki_kbdiacrs) );
   5774       break;
   5775    case VKI_KDSKBDIACR:
   5776       break;
   5777 
   5778    case VKI_KDGETKEYCODE:
   5779       POST_MEM_WRITE( (Addr)((struct vki_kbkeycode *)ARG3)->keycode,
   5780                       sizeof(((struct vki_kbkeycode *)ARG3)->keycode) );
   5781       break;
   5782    case VKI_KDSETKEYCODE:
   5783       break;
   5784 
   5785    case VKI_KDSIGACCEPT:
   5786       break;
   5787 
   5788    case VKI_KDKBDREP:
   5789       break;
   5790 
   5791    case VKI_KDFONTOP:
   5792       if ( ARG3 ) {
   5793          struct vki_console_font_op *op = (struct vki_console_font_op *) ARG3;
   5794          switch ( op->op ) {
   5795             case VKI_KD_FONT_OP_SET:
   5796                break;
   5797             case VKI_KD_FONT_OP_GET:
   5798                if ( op->data )
   5799                   POST_MEM_WRITE( (Addr) op->data,
   5800                                   (op->width + 7) / 8 * 32 * op->charcount );
   5801                break;
   5802             case VKI_KD_FONT_OP_SET_DEFAULT:
   5803                break;
   5804             case VKI_KD_FONT_OP_COPY:
   5805                break;
   5806          }
   5807          POST_MEM_WRITE( (Addr) op, sizeof(*op));
   5808       }
   5809       break;
   5810 
   5811    case VKI_VT_OPENQRY:
   5812       POST_MEM_WRITE( ARG3, sizeof(int) );
   5813       break;
   5814    case VKI_VT_GETMODE:
   5815       POST_MEM_WRITE( ARG3, sizeof(struct vki_vt_mode) );
   5816       break;
   5817    case VKI_VT_SETMODE:
   5818       break;
   5819    case VKI_VT_GETSTATE:
   5820       POST_MEM_WRITE( (Addr) &(((struct vki_vt_stat*) ARG3)->v_active),
   5821                       sizeof(((struct vki_vt_stat*) ARG3)->v_active) );
   5822       POST_MEM_WRITE( (Addr) &(((struct vki_vt_stat*) ARG3)->v_state),
   5823                       sizeof(((struct vki_vt_stat*) ARG3)->v_state) );
   5824       break;
   5825    case VKI_VT_RELDISP:
   5826    case VKI_VT_ACTIVATE:
   5827    case VKI_VT_WAITACTIVE:
   5828    case VKI_VT_DISALLOCATE:
   5829       break;
   5830    case VKI_VT_RESIZE:
   5831       break;
   5832    case VKI_VT_RESIZEX:
   5833       break;
   5834    case VKI_VT_LOCKSWITCH:
   5835    case VKI_VT_UNLOCKSWITCH:
   5836       break;
   5837 
   5838    case VKI_USBDEVFS_CONTROL:
   5839       if ( ARG3 ) {
   5840          struct vki_usbdevfs_ctrltransfer *vkuc = (struct vki_usbdevfs_ctrltransfer *)ARG3;
   5841          if (vkuc->bRequestType & 0x80)
   5842             POST_MEM_WRITE((Addr)vkuc->data, RES);
   5843       }
   5844       break;
   5845    case VKI_USBDEVFS_BULK:
   5846       if ( ARG3 ) {
   5847          struct vki_usbdevfs_bulktransfer *vkub = (struct vki_usbdevfs_bulktransfer *)ARG3;
   5848          if (vkub->ep & 0x80)
   5849             POST_MEM_WRITE((Addr)vkub->data, RES);
   5850       }
   5851       break;
   5852    case VKI_USBDEVFS_GETDRIVER:
   5853       if ( ARG3 ) {
   5854          struct vki_usbdevfs_getdriver *vkugd = (struct vki_usbdevfs_getdriver *)ARG3;
   5855          POST_MEM_WRITE((Addr)&vkugd->driver, sizeof(vkugd->driver));
   5856       }
   5857       break;
   5858    case VKI_USBDEVFS_REAPURB:
   5859    case VKI_USBDEVFS_REAPURBNDELAY:
   5860       if ( ARG3 ) {
   5861          struct vki_usbdevfs_urb **vkuu = (struct vki_usbdevfs_urb**)ARG3;
   5862          POST_MEM_WRITE((Addr)vkuu, sizeof(*vkuu));
   5863          if (!*vkuu)
   5864             break;
   5865          POST_MEM_WRITE((Addr) &((*vkuu)->status),sizeof((*vkuu)->status));
   5866          if ((*vkuu)->type == VKI_USBDEVFS_URB_TYPE_CONTROL) {
   5867             struct vki_usbdevfs_setuppacket *vkusp = (struct vki_usbdevfs_setuppacket *)(*vkuu)->buffer;
   5868             if (vkusp->bRequestType & 0x80)
   5869                POST_MEM_WRITE((Addr)(vkusp+1), (*vkuu)->buffer_length - sizeof(*vkusp));
   5870             POST_MEM_WRITE((Addr)&(*vkuu)->actual_length, sizeof((*vkuu)->actual_length));
   5871          } else if ((*vkuu)->type == VKI_USBDEVFS_URB_TYPE_ISO) {
   5872             char *bp = (*vkuu)->buffer;
   5873             int i;
   5874             for(i=0; i<(*vkuu)->number_of_packets; i++) {
   5875                POST_MEM_WRITE((Addr)&(*vkuu)->iso_frame_desc[i].actual_length, sizeof((*vkuu)->iso_frame_desc[i].actual_length));
   5876                POST_MEM_WRITE((Addr)&(*vkuu)->iso_frame_desc[i].status, sizeof((*vkuu)->iso_frame_desc[i].status));
   5877                if ((*vkuu)->endpoint & 0x80)
   5878                   POST_MEM_WRITE((Addr)bp, (*vkuu)->iso_frame_desc[i].actual_length);
   5879                bp += (*vkuu)->iso_frame_desc[i].length; // FIXME: or actual_length??
   5880             }
   5881             POST_MEM_WRITE((Addr)&(*vkuu)->error_count, sizeof((*vkuu)->error_count));
   5882          } else {
   5883             if ((*vkuu)->endpoint & 0x80)
   5884                POST_MEM_WRITE((Addr)(*vkuu)->buffer, (*vkuu)->actual_length);
   5885             POST_MEM_WRITE((Addr)&(*vkuu)->actual_length, sizeof((*vkuu)->actual_length));
   5886          }
   5887       }
   5888       break;
   5889    case VKI_USBDEVFS_CONNECTINFO:
   5890       POST_MEM_WRITE(ARG3, sizeof(struct vki_usbdevfs_connectinfo));
   5891       break;
   5892    case VKI_USBDEVFS_IOCTL:
   5893       if ( ARG3 ) {
   5894          struct vki_usbdevfs_ioctl *vkui = (struct vki_usbdevfs_ioctl *)ARG3;
   5895          UInt dir2, size2;
   5896          dir2  = _VKI_IOC_DIR(vkui->ioctl_code);
   5897          size2 = _VKI_IOC_SIZE(vkui->ioctl_code);
   5898          if (size2 > 0) {
   5899             if (dir2 & _VKI_IOC_READ)
   5900                POST_MEM_WRITE((Addr)vkui->data, size2);
   5901          }
   5902       }
   5903       break;
   5904 
   5905       /* I2C (/dev/i2c-*) ioctls */
   5906    case VKI_I2C_SLAVE:
   5907    case VKI_I2C_SLAVE_FORCE:
   5908    case VKI_I2C_TENBIT:
   5909    case VKI_I2C_PEC:
   5910       break;
   5911    case VKI_I2C_FUNCS:
   5912       POST_MEM_WRITE( ARG3, sizeof(unsigned long) );
   5913       break;
   5914 
   5915       /* Wireless extensions ioctls */
   5916    case VKI_SIOCSIWCOMMIT:
   5917    case VKI_SIOCSIWNWID:
   5918    case VKI_SIOCSIWFREQ:
   5919    case VKI_SIOCSIWMODE:
   5920    case VKI_SIOCSIWSENS:
   5921    case VKI_SIOCSIWRANGE:
   5922    case VKI_SIOCSIWPRIV:
   5923    case VKI_SIOCSIWSTATS:
   5924    case VKI_SIOCSIWSPY:
   5925    case VKI_SIOCSIWTHRSPY:
   5926    case VKI_SIOCSIWAP:
   5927    case VKI_SIOCSIWSCAN:
   5928    case VKI_SIOCSIWESSID:
   5929    case VKI_SIOCSIWRATE:
   5930    case VKI_SIOCSIWNICKN:
   5931    case VKI_SIOCSIWRTS:
   5932    case VKI_SIOCSIWFRAG:
   5933    case VKI_SIOCSIWTXPOW:
   5934    case VKI_SIOCSIWRETRY:
   5935    case VKI_SIOCSIWENCODE:
   5936    case VKI_SIOCSIWPOWER:
   5937    case VKI_SIOCSIWGENIE:
   5938    case VKI_SIOCSIWMLME:
   5939    case VKI_SIOCSIWAUTH:
   5940    case VKI_SIOCSIWENCODEEXT:
   5941    case VKI_SIOCSIWPMKSA:
   5942       break;
   5943    case VKI_SIOCGIWNAME:
   5944       if (ARG3) {
   5945          POST_MEM_WRITE((Addr)((struct vki_iwreq *)ARG3)->u.name,
   5946                         sizeof(((struct vki_iwreq *)ARG3)->u.name));
   5947       }
   5948       break;
   5949    case VKI_SIOCGIWNWID:
   5950    case VKI_SIOCGIWSENS:
   5951    case VKI_SIOCGIWRATE:
   5952    case VKI_SIOCGIWRTS:
   5953    case VKI_SIOCGIWFRAG:
   5954    case VKI_SIOCGIWTXPOW:
   5955    case VKI_SIOCGIWRETRY:
   5956    case VKI_SIOCGIWPOWER:
   5957    case VKI_SIOCGIWAUTH:
   5958       if (ARG3) {
   5959          POST_MEM_WRITE((Addr)&((struct vki_iwreq *)ARG3)->u.param,
   5960                         sizeof(struct vki_iw_param));
   5961       }
   5962       break;
   5963    case VKI_SIOCGIWFREQ:
   5964       if (ARG3) {
   5965          POST_MEM_WRITE((Addr)&((struct vki_iwreq *)ARG3)->u.freq,
   5966                         sizeof(struct vki_iw_freq));
   5967       }
   5968       break;
   5969    case VKI_SIOCGIWMODE:
   5970       if (ARG3) {
   5971          POST_MEM_WRITE((Addr)&((struct vki_iwreq *)ARG3)->u.mode,
   5972                        sizeof(__vki_u32));
   5973       }
   5974       break;
   5975    case VKI_SIOCGIWRANGE:
   5976    case VKI_SIOCGIWPRIV:
   5977    case VKI_SIOCGIWSTATS:
   5978    case VKI_SIOCGIWSPY:
   5979    case VKI_SIOCGIWTHRSPY:
   5980    case VKI_SIOCGIWAPLIST:
   5981    case VKI_SIOCGIWSCAN:
   5982    case VKI_SIOCGIWESSID:
   5983    case VKI_SIOCGIWNICKN:
   5984    case VKI_SIOCGIWENCODE:
   5985    case VKI_SIOCGIWGENIE:
   5986    case VKI_SIOCGIWENCODEEXT:
   5987       if (ARG3) {
   5988          struct vki_iw_point* point;
   5989          point = &((struct vki_iwreq *)ARG3)->u.data;
   5990          POST_MEM_WRITE((Addr)point->pointer, point->length);
   5991       }
   5992       break;
   5993    case VKI_SIOCGIWAP:
   5994       if (ARG3) {
   5995          POST_MEM_WRITE((Addr)&((struct vki_iwreq *)ARG3)->u.ap_addr,
   5996                         sizeof(struct vki_sockaddr));
   5997       }
   5998       break;
   5999 
   6000 #  if defined(VGPV_arm_linux_android)
   6001    /* ashmem */
   6002    case VKI_ASHMEM_GET_SIZE:
   6003    case VKI_ASHMEM_SET_SIZE:
   6004    case VKI_ASHMEM_GET_PROT_MASK:
   6005    case VKI_ASHMEM_SET_PROT_MASK:
   6006    case VKI_ASHMEM_GET_PIN_STATUS:
   6007    case VKI_ASHMEM_PURGE_ALL_CACHES:
   6008    case VKI_ASHMEM_SET_NAME:
   6009    case VKI_ASHMEM_PIN:
   6010    case VKI_ASHMEM_UNPIN:
   6011        break;
   6012    case VKI_ASHMEM_GET_NAME:
   6013        POST_MEM_WRITE( ARG3, VKI_ASHMEM_NAME_LEN );
   6014        break;
   6015 
   6016    /* binder */
   6017    case VKI_BINDER_WRITE_READ:
   6018        if (ARG3) {
   6019            struct vki_binder_write_read* bwr
   6020               = (struct vki_binder_write_read*)ARG3;
   6021            POST_FIELD_WRITE(bwr->write_consumed);
   6022            POST_FIELD_WRITE(bwr->read_consumed);
   6023 
   6024            if (bwr->read_size)
   6025                POST_MEM_WRITE((Addr)bwr->read_buffer, bwr->read_consumed);
   6026        }
   6027        break;
   6028 
   6029    case VKI_BINDER_SET_IDLE_TIMEOUT:
   6030    case VKI_BINDER_SET_MAX_THREADS:
   6031    case VKI_BINDER_SET_IDLE_PRIORITY:
   6032    case VKI_BINDER_SET_CONTEXT_MGR:
   6033    case VKI_BINDER_THREAD_EXIT:
   6034        break;
   6035    case VKI_BINDER_VERSION:
   6036        if (ARG3) {
   6037            struct vki_binder_version* bv = (struct vki_binder_version*)ARG3;
   6038            POST_FIELD_WRITE(bv->protocol_version);
   6039        }
   6040        break;
   6041 #  endif /* defined(VGPV_arm_linux_android) */
   6042 
   6043    default:
   6044       /* EVIOC* are variable length and return size written on success */
   6045       switch (ARG2 & ~(_VKI_IOC_SIZEMASK << _VKI_IOC_SIZESHIFT)) {
   6046       case VKI_EVIOCGNAME(0):
   6047       case VKI_EVIOCGPHYS(0):
   6048       case VKI_EVIOCGUNIQ(0):
   6049       case VKI_EVIOCGKEY(0):
   6050       case VKI_EVIOCGLED(0):
   6051       case VKI_EVIOCGSND(0):
   6052       case VKI_EVIOCGSW(0):
   6053       case VKI_EVIOCGBIT(VKI_EV_SYN,0):
   6054       case VKI_EVIOCGBIT(VKI_EV_KEY,0):
   6055       case VKI_EVIOCGBIT(VKI_EV_REL,0):
   6056       case VKI_EVIOCGBIT(VKI_EV_ABS,0):
   6057       case VKI_EVIOCGBIT(VKI_EV_MSC,0):
   6058       case VKI_EVIOCGBIT(VKI_EV_SW,0):
   6059       case VKI_EVIOCGBIT(VKI_EV_LED,0):
   6060       case VKI_EVIOCGBIT(VKI_EV_SND,0):
   6061       case VKI_EVIOCGBIT(VKI_EV_REP,0):
   6062       case VKI_EVIOCGBIT(VKI_EV_FF,0):
   6063       case VKI_EVIOCGBIT(VKI_EV_PWR,0):
   6064       case VKI_EVIOCGBIT(VKI_EV_FF_STATUS,0):
   6065          if (RES > 0)
   6066             POST_MEM_WRITE(ARG3, RES);
   6067          break;
   6068       default:
   6069          ML_(POST_unknown_ioctl)(tid, RES, ARG2, ARG3);
   6070          break;
   6071       }
   6072       break;
   6073    }
   6074 }
   6075 
   6076 /* ---------------------------------------------------------------------
   6077    socketcall wrapper helpers
   6078    ------------------------------------------------------------------ */
   6079 
   6080 void
   6081 ML_(linux_PRE_sys_getsockopt) ( ThreadId tid,
   6082                                 UWord arg0, UWord arg1, UWord arg2,
   6083                                 UWord arg3, UWord arg4 )
   6084 {
   6085    /* int getsockopt(int s, int level, int optname,
   6086                      void *optval, socklen_t *optlen); */
   6087    Addr optval_p = arg3;
   6088    Addr optlen_p = arg4;
   6089    /* vg_assert(sizeof(socklen_t) == sizeof(UInt)); */
   6090    if (optval_p != (Addr)NULL) {
   6091       ML_(buf_and_len_pre_check) ( tid, optval_p, optlen_p,
   6092                                    "socketcall.getsockopt(optval)",
   6093                                    "socketcall.getsockopt(optlen)" );
   6094       if (arg1 == VKI_SOL_SCTP &&
   6095           (arg2 == VKI_SCTP_GET_PEER_ADDRS ||
   6096            arg2 == VKI_SCTP_GET_LOCAL_ADDRS))
   6097       {
   6098          struct vki_sctp_getaddrs *ga = (struct vki_sctp_getaddrs*)arg3;
   6099          int address_bytes = sizeof(struct vki_sockaddr_in6) * ga->addr_num;
   6100          PRE_MEM_WRITE( "socketcall.getsockopt(optval.addrs)",
   6101                         (Addr)ga->addrs, address_bytes );
   6102       }
   6103    }
   6104 }
   6105 
   6106 void
   6107 ML_(linux_POST_sys_getsockopt) ( ThreadId tid,
   6108                                  SysRes res,
   6109                                  UWord arg0, UWord arg1, UWord arg2,
   6110                                  UWord arg3, UWord arg4 )
   6111 {
   6112    Addr optval_p = arg3;
   6113    Addr optlen_p = arg4;
   6114    vg_assert(!sr_isError(res)); /* guaranteed by caller */
   6115    if (optval_p != (Addr)NULL) {
   6116       ML_(buf_and_len_post_check) ( tid, res, optval_p, optlen_p,
   6117                                     "socketcall.getsockopt(optlen_out)" );
   6118       if (arg1 == VKI_SOL_SCTP &&
   6119           (arg2 == VKI_SCTP_GET_PEER_ADDRS ||
   6120            arg2 == VKI_SCTP_GET_LOCAL_ADDRS))
   6121       {
   6122          struct vki_sctp_getaddrs *ga = (struct vki_sctp_getaddrs*)arg3;
   6123          struct vki_sockaddr *a = ga->addrs;
   6124          int i;
   6125          for (i = 0; i < ga->addr_num; i++) {
   6126             int sl = 0;
   6127             if (a->sa_family == VKI_AF_INET)
   6128                sl = sizeof(struct vki_sockaddr_in);
   6129             else if (a->sa_family == VKI_AF_INET6)
   6130                sl = sizeof(struct vki_sockaddr_in6);
   6131             else {
   6132                VG_(message)(Vg_UserMsg, "Warning: getsockopt: unhandled "
   6133                                         "address type %d\n", a->sa_family);
   6134             }
   6135             a = (struct vki_sockaddr*)((char*)a + sl);
   6136          }
   6137          POST_MEM_WRITE( (Addr)ga->addrs, (char*)a - (char*)ga->addrs );
   6138       }
   6139    }
   6140 }
   6141 
   6142 #undef PRE
   6143 #undef POST
   6144 
   6145 #endif // defined(VGO_linux)
   6146 
   6147 /*--------------------------------------------------------------------*/
   6148 /*--- end                                                          ---*/
   6149 /*--------------------------------------------------------------------*/
   6150