如何使用 C 程序将 ARM 处理器置于不同的模式? [英] How Can I put ARM processor in different modes using C program?

查看:11
本文介绍了如何使用 C 程序将 ARM 处理器置于不同的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历不同模式的 ARM 处理器.我想在不同模式下检查处理器状态(例如:寄存器值).

I am going through different mode of ARM processor. I want to check the processor state(ex: register values) while it is in different mode.

那么有人可以帮我找出将处理器置于不同模式的示例代码吗?

So can someone help me to find out sample code to put processor in different mode ?

例如,我找到了未定义模式的代码:asm volatile (".short 0xffff ");

For example I found a code for undefined mode: asm volatile (".short 0xffff ");

推荐答案

如果您想从用户空间测试模式,这是一个难题.如果系统中没有 FIQ 外围设备,则可能无法进入 FIQ 模式.您的系统可能根本没有使用 Monitor 模式等.要进入 abort 模式,您可以使用无效指针,或使用 mmap.然而,如果没有内核的帮助,从用户空间回答所有模式切换将是书本(或不可能).使用 /proc/sys 文件创建测试模块并使用以下技术来实现内核代码将是最直接的方法.

If you wish to test the modes from user space, this is a difficult question. There maybe no way to go to FIQ mode, if there is no FIQ peripheral in the system. Your system may not be using Monitor mode at all, etc. To get to abort mode, you can use an invalid pointer, or use mmap. However, to answer all mode switches from user space would be book like (or impossible) without assistance from the kernel. Creating a test module with a /proc or /sys file and using the techniques below to implement the kernel code would be the most straight forward method.

您应该知道,并非所有模式切换转换都是允许的.例如,您可能永远不会从 用户模式 切换到任何其他模式,除非通过异常机制.另一个问题是每个 ARM 模式都有存储的寄存器.其中之一是非常重要的 sp(或堆栈指针)和 lr(或链接寄存器)这是C"代码的基础.

You should be aware that not all mode switch transitions are allowed. For instance, you may never switch from user mode to any other mode except through the exception mechanisms. Another issue is that each ARM mode has banked registers. One of these is the very important sp (or stack pointer) and lr (or link register) that is fundamental to 'C' code.

使用内联汇编器的宏来绑定测试片段通常比使用函数调用更安全.测试代码不得调用外部例程.这可能很困难,因为使用浮点等可能会导致编译器插入隐藏的子例程调用.您应该检查生成的汇编程序并为其他人提供评论.

It is generally more safe to use a macro of in-line assembler to bound your test snippets, than to use function calls. The test code must not call external routines. This can be difficult as using floating point, etc may result in the compiler inserting hidden sub-routine calls. You should inspect the generated assembler and provide comments for others.

 /* Get the current mode for restoration. */
 static inline unsigned int get_cpsr(void)
 {
     unsigned int rval;
     asm (" mrs %0, cpsr
" : "=r" (rval));
     return rval;
 }

你可以把它放在头文件中.编译器将内联代码,因此您只需将 msr 指令放置在例程中即可.

You can put this in a header file. The compiler will inline the code so, you will just get the msr instruction placed within a routine.

要更改模式,请使用类似的定义,

To change the mode use a define like,

 /* Change the mode */
 #define change_mode(mode) asm("cps %0" : : "I"(mode))

Tangr 的模式定义正确,

Tangr's has the correct mode defines,

#define MODE_USR        0x10   /* Never use this one, as there is no way back! */
#define MODE_FIQ        0x11   /* banked r8-r14 */
#define MODE_IRQ        0x12
#define MODE_SVC        0x13
#define MODE_MON        0x16
#define MODE_ABT        0x17
#define MODE_UND        0x1B
#define MODE_SYS        0x1F   /* Same as user... */

你还需要恢复之前的模式,

You also need to restore the previous mode,

#define restore_mode(mode) 
     mode &= 0x1f; 
     asm(" msr cpsr, %0
" : : "r"(mode) : "cc")

整理如下,

  void test_abort(void)
  {
     unsigned int old_mode = get_cpsr() & 0x1f;
     change_mode(MODE_ABT);
     /* Your test code here... must not call functions. */
     restore_mode(old_mode);
  }

这直接回答了您的问题.然而,由于所有的困难,编写汇编程序来实现测试通常更容易.我相信您正在尝试利用现有的 Linux 代码来测试所有模式.这不是 ARM-Linux 的设计目标,如果不修改源代码,将很难实现并且高度系统特定.

This answers your question directly. However, due to all of the difficulties, it is often easier to write assembler to achieve a test. I believe you were trying to leverage the existing Linux code to test all the modes. This is not an ARM-Linux design goal and without modifying the source, it will be very difficult to achieve and highly system specific if it is.

这篇关于如何使用 C 程序将 ARM 处理器置于不同的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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