From: punit.agrawal@arm.com (Punit Agrawal)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCHv2 1/5] arm64: Add support for hooks to handle undefined instructions
Date: Wed, 1 Oct 2014 13:07:55 +0100 [thread overview]
Message-ID: <1412165279-8709-2-git-send-email-punit.agrawal@arm.com> (raw)
In-Reply-To: <1412165279-8709-1-git-send-email-punit.agrawal@arm.com>
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>
---
arch/arm64/include/asm/traps.h | 16 ++++++++++
arch/arm64/kernel/traps.c | 68 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+)
diff --git a/arch/arm64/include/asm/traps.h b/arch/arm64/include/asm/traps.h
index 10ca8ff..4faaf03 100644
--- a/arch/arm64/include/asm/traps.h
+++ b/arch/arm64/include/asm/traps.h
@@ -18,6 +18,22 @@
#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);
+};
+
+int 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[];
diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c
index 02cd3f0..bcde9de 100644
--- a/arch/arm64/kernel/traps.c
+++ b/arch/arm64/kernel/traps.c
@@ -260,6 +260,71 @@ void arm64_notify_die(const char *str, struct pt_regs *regs,
}
}
+static LIST_HEAD(undef_hook);
+static DEFINE_RAW_SPINLOCK(undef_lock);
+
+int 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);
+
+ return 0;
+}
+
+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_instruction(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;
@@ -269,6 +334,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",
--
1.7.10.4
next prev parent reply other threads:[~2014-10-01 12:07 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-01 12:07 [PATCHv2 0/5] Legacy instruction emulation for arm64 Punit Agrawal
2014-10-01 12:07 ` Punit Agrawal [this message]
2014-10-01 12:07 ` [PATCHv2 2/5] arm64: Add AArch32 instruction set condition code checks Punit Agrawal
2014-10-01 12:07 ` [PATCHv2 3/5] arm64: Port SWP/SWPB emulation support from arm Punit Agrawal
2014-10-01 12:07 ` [PATCHv2 4/5] arm64: Emulate CP15 Barrier instructions Punit Agrawal
2014-10-01 12:07 ` [PATCHv2 5/5] arm64: Trace emulation of AArch32 legacy instructions Punit Agrawal
2014-10-01 14:40 ` Steven Rostedt
2014-10-14 16:18 ` Punit Agrawal
2014-10-01 13:37 ` [PATCHv2 0/5] Legacy instruction emulation for arm64 Punit Agrawal
2014-10-01 13:37 ` [PATCHv2 1/5] arm64: Add support for hooks to handle undefined instructions Punit Agrawal
2014-10-01 13:37 ` [PATCHv2 2/5] arm64: Add AArch32 instruction set condition code checks Punit Agrawal
2014-10-01 13:37 ` [PATCHv2 3/5] arm64: Port SWP/SWPB emulation support from arm Punit Agrawal
2014-10-06 10:52 ` Will Deacon
2014-10-01 13:37 ` [PATCHv2 4/5] arm64: Emulate CP15 Barrier instructions Punit Agrawal
2014-10-01 13:37 ` [PATCHv2 5/5] arm64: Trace emulation of AArch32 legacy instructions Punit Agrawal
2014-10-06 10:22 ` [PATCHv2 0/5] Legacy instruction emulation for arm64 Will Deacon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1412165279-8709-2-git-send-email-punit.agrawal@arm.com \
--to=punit.agrawal@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).