arm64: Add support for hooks to handle undefined instructions
authorPunit Agrawal <punit.agrawal@arm.com>
Tue, 18 Nov 2014 11:41:22 +0000 (11:41 +0000)
committerWill Deacon <will.deacon@arm.com>
Thu, 20 Nov 2014 16:33:43 +0000 (16:33 +0000)
Add support to register hooks for undefined instructions. The handlers
will be called when the undefined instruction and the processor state
(as contained in pstate) match criteria used at registration.

Signed-off-by: Punit Agrawal <punit.agrawal@arm.com>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
arch/arm64/include/asm/insn.h
arch/arm64/include/asm/traps.h
arch/arm64/kernel/insn.c
arch/arm64/kernel/traps.c

index 56a9e63b6c33ef45dc37741a7a842df05d1667f6..1bb043018315c53857d2a35c94d98c486d5db08f 100644 (file)
@@ -354,6 +354,8 @@ bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn);
 int aarch64_insn_patch_text_nosync(void *addr, u32 insn);
 int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt);
 int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt);
+
+bool aarch32_insn_is_wide(u32 insn);
 #endif /* __ASSEMBLY__ */
 
 #endif /* __ASM_INSN_H */
index 10ca8ff93cc25ca99a00b0bd3ed6993daea633a1..232e4ba5d314ca10f15290214ab2f3d5ef550c48 100644 (file)
 #ifndef __ASM_TRAP_H
 #define __ASM_TRAP_H
 
+#include <linux/list.h>
+
+struct pt_regs;
+
+struct undef_hook {
+       struct list_head node;
+       u32 instr_mask;
+       u32 instr_val;
+       u64 pstate_mask;
+       u64 pstate_val;
+       int (*fn)(struct pt_regs *regs, u32 instr);
+};
+
+void register_undef_hook(struct undef_hook *hook);
+void unregister_undef_hook(struct undef_hook *hook);
+
 static inline int in_exception_text(unsigned long ptr)
 {
        extern char __exception_text_start[];
index e007714ded04e6a0854657b0cc39f244e06de18c..ab00eb58d3857c27708c0566e2fccd197d201265 100644 (file)
@@ -959,3 +959,8 @@ u32 aarch64_insn_gen_logical_shifted_reg(enum aarch64_insn_register dst,
 
        return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn, shift);
 }
+
+bool aarch32_insn_is_wide(u32 insn)
+{
+       return insn >= 0xe800;
+}
index de1b085e796396be5f6ce6c5932a3e418ade432e..0a801e3743d5df0e793954d0077b0865fb15c290 100644 (file)
@@ -259,6 +259,69 @@ void arm64_notify_die(const char *str, struct pt_regs *regs,
        }
 }
 
+static LIST_HEAD(undef_hook);
+static DEFINE_RAW_SPINLOCK(undef_lock);
+
+void register_undef_hook(struct undef_hook *hook)
+{
+       unsigned long flags;
+
+       raw_spin_lock_irqsave(&undef_lock, flags);
+       list_add(&hook->node, &undef_hook);
+       raw_spin_unlock_irqrestore(&undef_lock, flags);
+}
+
+void unregister_undef_hook(struct undef_hook *hook)
+{
+       unsigned long flags;
+
+       raw_spin_lock_irqsave(&undef_lock, flags);
+       list_del(&hook->node);
+       raw_spin_unlock_irqrestore(&undef_lock, flags);
+}
+
+static int call_undef_hook(struct pt_regs *regs)
+{
+       struct undef_hook *hook;
+       unsigned long flags;
+       u32 instr;
+       int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
+       void __user *pc = (void __user *)instruction_pointer(regs);
+
+       if (!user_mode(regs))
+               return 1;
+
+       if (compat_thumb_mode(regs)) {
+               /* 16-bit Thumb instruction */
+               if (get_user(instr, (u16 __user *)pc))
+                       goto exit;
+               instr = le16_to_cpu(instr);
+               if (aarch32_insn_is_wide(instr)) {
+                       u32 instr2;
+
+                       if (get_user(instr2, (u16 __user *)(pc + 2)))
+                               goto exit;
+                       instr2 = le16_to_cpu(instr2);
+                       instr = (instr << 16) | instr2;
+               }
+       } else {
+               /* 32-bit ARM instruction */
+               if (get_user(instr, (u32 __user *)pc))
+                       goto exit;
+               instr = le32_to_cpu(instr);
+       }
+
+       raw_spin_lock_irqsave(&undef_lock, flags);
+       list_for_each_entry(hook, &undef_hook, node)
+               if ((instr & hook->instr_mask) == hook->instr_val &&
+                       (regs->pstate & hook->pstate_mask) == hook->pstate_val)
+                       fn = hook->fn;
+
+       raw_spin_unlock_irqrestore(&undef_lock, flags);
+exit:
+       return fn ? fn(regs, instr) : 1;
+}
+
 asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
 {
        siginfo_t info;
@@ -268,6 +331,9 @@ asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
        if (!aarch32_break_handler(regs))
                return;
 
+       if (call_undef_hook(regs) == 0)
+               return;
+
        if (show_unhandled_signals && unhandled_signal(current, SIGILL) &&
            printk_ratelimit()) {
                pr_info("%s[%d]: undefined instruction: pc=%p\n",