@[toc]

在这里插入图片描述

https://github.com/wdfk-prog/linux-study
https://github.com/wdfk-prog/other

编写原因

  1. 阅读linux源码时,看到了#define BUG()的定义,便尝试理解.发现自行理解较难,便去网上搜索.搜索了好久,才终于搞懂这一块干了什么.所以写这个文章,给后面的人减少点搜索工作量.
  2. 网上搜索的要么不够深入,要么根本不是arm架构的实现.

BUG_INSTR 的指令理解

  1. #define BUG()这块自行理解既可,这里着重说明BUG_INSTR为什么要这么写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* Use a suitable undefined instruction to use for ARM/Thumb2 bug handling.
* We need to be careful not to conflict with those used by other modules and
* the register_undef_hook() system.
*/
/*
* 使用合适的 undefined 指令进行 ARM/Thumb2 错误处理。
* 我们需要注意不要与其他模块和
* register_undef_hook() 系统。
*/
#ifdef CONFIG_THUMB2_KERNEL
#define BUG_INSTR_VALUE 0xde02
#define BUG_INSTR(__value) __inst_thumb16(__value)
#else
#define BUG_INSTR_VALUE 0xe7f001f2
#define BUG_INSTR(__value) __inst_arm(__value)
#endif
  1. 这里看到用了常数0xde020xe7f001f2.这其实是机器码操作码.通过汇编指令转换后的机器码

  2. 这里就需要根据ARM手册与thumb的手册进行理解了

  3. 参考https://cyrus-studio.github.io/blog/posts/thumb-%E6%B1%87%E7%BC%96%E6%8C%87%E4%BB%A4%E9%9B%86thumb-%E6%8C%87%E4%BB%A4%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8F%E7%BC%96%E8%AF%91-thumb-%E6%B1%87%E7%BC%96%E4%BB%A3%E7%A0%81/的文章进行了理解

  4. 使用这个网址的https://armconverter.com/?code=udf+%232工具进行快速解析既可
    在这里插入图片描述

  5. 可以得知该操作码的指令为udf #2,即未知的指令,进而触发bug异常

  6. 需要注意由于arm是小端架构,所以0xde02应该传入02de

后记

  1. 该工具其实上是逆向使用的,这里拿来当成解码工具了.
  2. 所以嵌入式底层是相通的,学好了,是公用的
    在这里插入图片描述