您的位置:首页 > 运维架构 > Linux

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

2009-03-25 22:17 525 查看
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,
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐