linux kernel 對 ARM 未定義指令擴充的例子

來源:互聯網
上載者:User

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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.