简体   繁体   English

syscall包装程序asm C

[英]syscall wrapper asm C

Can someone explain this code snippet to me? 有人可以向我解释此代码段吗? Also please give me some link/URL where i can know more about this? 还请给我一些链接/ URL,我可以在此了解更多信息? This code is used as a wrapper to override the "extern int errno" in our library. 此代码用作包装程序,以覆盖我们库中的“ extern int errno”。 Can someone explain me this function, and tell why is wrapper needed in some syscalls? 有人可以向我解释此功能,并告诉我为什么在某些系统调用中需要包装器吗? Which are also called WeakSYSCALLS? 哪些也称为WeakSYSCALLS?

#define ASM_ARGS_1      ASM_ARGS_0, "r" (_a1)
#define ASM_ARGS_2      ASM_ARGS_1, "r" (_a2)
#define ASM_ARGS_3      ASM_ARGS_2, "r" (_a3)
#define LOADREGS_5(a1, a2, a3, a4, a5)          \
register int _v1 asm ("v1") = (int) (a5);     \
LOADREGS_4 (a1, a2, a3, a4)

#define LOADREGS_6(a1, a2, a3, a4, a5, a6)      \

register int _v2 asm ("v2") = (int) (a6);     \

LOADREGS_5 (a1, a2, a3, a4, a5)

#define MYLIBC_SYSCALL(name, nargs, args...)               \
({                                                        \

    unsigned int retval;                              \

    {                                                 \

    register int _a1 asm ("r0"), _nargs asm ("r7");   \
    LOADREGS_##nargs(args)                            \
    _nargs = __NR_##name;                             \
    asm volatile (                                    \
            "swi    0x0"                              \
            :"=r"(_a1)                                \
            :"r"(_nargs) ASM_ARGS_##nargs             \
            : "memory" );                             \

      retval = _a1;                                     \
    }                                                 \

    if ( retval >= 0xfffff001 )     {                 \
            errno = -retval;                          \
            retval = (unsigned int)-1;                      \
    }                                                 \
    (int) retval;                                     \
})

Doing a "syscall" means triggering the kernel to execute a special activity. 进行“系统调用”意味着触发内核执行特殊活动。 Since the kernel runs in a different address space, this cannot be done via a simple function call. 由于内核在不同的地址空间中运行,因此无法通过简单的函数调用来完成。

Depending on the operating system and hardware platform, a syscall can be invoked by, for example, triggering an interrupt, a call gate, SYSENTER, or several other methods. 根据操作系统和硬件平台的不同,可以通过触发中断,调用门,SYSENTER或其他几种方法来调用syscall。

In any case, though, you cannot simply pass a number of parameters to the kernel the way you would do with a C function call. 但是,无论如何,您不能像使用C函数调用那样简单地将许多参数传递给内核。 You can , however, place parameter values into certain registers. 但是,您可以将参数值放入某些寄存器中。 Which registers those are, and how their contents are interpreted by the kernel, is again specific to the OS in question. 那些寄存器是什么,内核如何解释它们的内容,再次取决于所讨论的OS。

Since you can neither access specific registers, nor invoke any of the kernel-triggering methods mentioned above, in plain C code, there are syscall wrappers available to you that are called like C functions, and then place the parameters in registers and trigger the kernel using ASM code. 因为你可以既不访问特定寄存器,也不调用任何上述内核触发方法的,在普通的C代码,存在可用于您的系统调用包装器称为像C函数,然后将这些参数在寄存器和触发内核使用ASM代码。

What you see above is such a syscall wrapper. 您在上面看到的是这样的syscall包装器。 You see the part where it places the number of arguments into register r7, the arguments themselves into the appropriate registers ( LOADREGS_* ), then does the trigger ( swi 0x0 , which I guess is a software interrupt - I don't know much about the ARM platform), and gets the "return value" from register A1. 您会看到将参数数目放入寄存器r7,将参数本身放入适当的寄存器( LOADREGS_* )的部分,然后执行触发器( swi 0x0 ,我猜这是软件中断-我对此不太了解(ARM平台),并从寄存器A1获取“返回值”。

System calls cannot set errno directly, instead they return a negative value. 系统调用不能直接设置errno ,而是返回负值。 errno is actually supposed to be a macro that evaluates to an lvalue . 实际上, errno应该是一个计算为左值的宏。

This code updates your copy of errno rather than the C library's. 此代码将更新您的errno副本,而不是C库的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM