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,
};