bsdmachxnu

How mach trap become syscall?


I'm new to XNU kernel and partially confused.

I see that we a function in file vm_unix.c:

kern_return_t  task_for_pid(struct task_for_pid_args *args)

with the comment:

// This should be a BSD system call, not a Mach trap!!!

then 2 declarations in osfmk\mach:

/*
 *  Obsolete interfaces.
 */

extern kern_return_t task_for_pid(
mach_port_name_t target_tport,
int pid,
mach_port_name_t *t);

extern kern_return_t task_for_pid(
struct task_for_pid_args *args);

and trap decleration in kern

/* 45 */ MACH_TRAP(task_for_pid, 3, 3, munge_www),

So as you can see I found 2 declarations and only 1 implementation.

Where is the implementation of the 3 parameter match trap? How has the transition from 3 parameters to 1 parameter happened?


Solution

  • MACH_TRAP is a macro which inserts an entry into the mach_trap_table:

    #define MACH_TRAP(name, arg_count, u32_arg_words, munge32)
    { (arg_count), (kern_return_t (*)(void *)) (name), (u32_arg_words) }

    (see http://newosxbook.com/src.jl?tree=xnu&ver=6153.11.26&file=osfmk/kern/syscall_sw.h)

    the arguments are then deserialized in the mach trap invocation, by taking them through the mach_call_munger (http://newosxbook.com/src.jl?tree=xnu&ver=6153.11.26&file=osfmk/i386/bsd_i386.c) which is the wrapper that handles 32/64 bit-ness, and then passes arguments to the actual handler.

    Source: "*OS Internals" (http://NewOSXBook.com/) Volume I