linux kernel 对 ARM 未定义指令扩展的例子

     ARM 的工作模式中有一种是未定义指令终止模式UND, 如果运行的程序中有未定义的指令,那么会对 kernel 产生一个 undefine instruction trap 如果程序没有对SIGILL 有任何处理的话,那么程序会被KILL 了。

     linux 的kernel 提供一种扩展未定义指令的方法。

     例如扩展ARM 指令clz,这个指令是用来计算一个一个数字前面有多少个0

 

     register_undef_hook(&clz_hook);

static int clz_trap(struct pt_regs *regs, unsigned int instr)
{
/* Extract the source register index */
int src = instr & 0xf;
/* Extract the destination (result) register index */
int dst = (instr >> 12) & 0xf;
/* Extract the conditional code */
int cc = (instr >> 28) & 0xf;
/* Test if the conditional code passes */
if (handle_cc(regs, cc)) {
/* Implement the instruction */
regs->uregs[dst] = 32 - fls(regs->uregs[src]);
}
/* Print some stuff for debugging */
printk("emulating clz: %x src=%d (%lx) dst=%d (%lx) @ %p/n", instr,
src, regs->uregs[src], dst, regs->uregs[dst], (void*) regs->ARM_pc);
/* Increment the PC register */
regs->ARM_pc += 4;

return 0;
}

static struct undef_hook clz_hook = {
.instr_mask = 0x0fff0ff0,
.instr_val = 0x016f0f10,
.cpsr_mask = PSR_T_BIT,
.cpsr_val = 0,
.fn = clz_trap,
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值