ARM 系统调用作为 C++ 模板 [英] ARM syscall as c++ template

查看:26
本文介绍了ARM 系统调用作为 C++ 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 newlib 存根中调用一些系统调用,当前的实现使用 C 宏,随着时间的推移,这些宏变得不可读且看起来很糟糕.(而且我讨厌宏...)但是,我使用 C++ 模板的实现仅适用于一个参数:

I need to call some syscalls in my newlib stubs and the current implementation uses C macros which got unreadable and awful looking over time. (And I hate macros...) However, my implementation with C++ templates does only work for one parameter:

template <int nr, typename RETTYPE, typename PARAM1> 
    inline RETTYPE syscall(PARAM1 p1)
{
  register PARAM1 r0 asm("r0") = p1;
  asm volatile("svc %[nr]\n"
    : "=r" (r0)
    : [nr] "i" (nr), "r" (r0)
    : "memory", "r1", "r2", "r3", "r12", "lr");
  return (RETTYPE) r0;
}

现在我可以打电话给例如malloc 使用

Now I can call e.g. malloc using

void *ptr = syscall<SYS_MALLOC, void*>(0x1000);

分配 0x1000 字节.

to allocate 0x1000 bytes.

我对四个参数的实现:

template <int nr, typename RETTYPE, typename PARAM1, typename PARAM2, typename PARAM3, typename PARAM4> 
    inline RETTYPE syscall(PARAM1 p1, PARAM2 p2, PARAM3 p3, PARAM4 p4)
{
  register PARAM1 r0 asm("r0") = p1;
  register PARAM2 r1 asm("r1") = p2;
  register PARAM3 r2 asm("r2") = p3;
  register PARAM4 r3 asm("r3") = p4;

  asm volatile("svc %[nr]\n"
    : "=r" (r0)
    : [nr] "i" (nr), "r" (r0), "r" (r1), "r" (r2), "r" (r3)
    : "memory", "r12", "lr");

  return (RETTYPE) r0;
}

不起作用,swi"指令的寄存器内容是任意的.不知何故,GCC 不再尊重注册"变量.示例:我在 svc 指令处设置断点并执行

doesn't work, the content of the registers at the "swi" instruction is arbitrary. Somehow GCC doesn't respect the "register" variables anymore. Example: I set a breakpoint at the svc instruction and execute

syscall<FWRITE, int>(ptr, 1, len, f)

但不知何故,r0 设置为 1,r1 设置为 ptr...我也试过在不优化的情况下编译,只是寄存器的顺序发生了一些变化,但它仍然是错误的顺序.我知道我可以执行mov r0, %[param1]"等操作,但这会妨碍优化,从而导致代码变慢.

But somehow r0 is set to 1 and r1 to ptr... I also tried compiling without optimizations, just the order of the registers changed a bit, but it's still the wrong order. I know I could do "mov r0, %[param1]" etc. but that would prevent optimizations and therefore result in slower code.

这是 GCC (4.8.2) 中的错误还是我忽略了什么?

Is it a bug in GCC (4.8.2) or did I overlooking something?

推荐答案

这是 bug 33661,比较老了.几年前我自己遇到过.

This is bug 33661, which is rather old. I encountered it myself several years ago.

这篇关于ARM 系统调用作为 C++ 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆