From patchwork Sun Jan 11 04:03:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Long X-Patchwork-Id: 42946 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ee0-f70.google.com (mail-ee0-f70.google.com [74.125.83.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9F0A5218E0 for ; Sun, 11 Jan 2015 04:03:40 +0000 (UTC) Received: by mail-ee0-f70.google.com with SMTP id b57sf11097305eek.1 for ; Sat, 10 Jan 2015 20:03:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=+Do9RWgx9FtJjHLtuqE9xBDbRJqTlhKviW1u/7CgcDc=; b=UppjqJaXbbi9W7AcvniGgihd6Qwo4r+eHx5XvkddkmODFbIkXj07O+MJNSCdJjhxmv Ie+aTJbdGbSuI8PByKThFnIXnHMU5qKT4vsTjnHgZAHeJuud83bzA5i5zJVvSvpuPONV GaX1AiKNebC+385dUJ1AfW4k5XNMUh9+D40OjLGEDTOD7eb9i1cy0l6TpJPECT4qFMh7 o4kgtCf4M1qu4LtRZwS/kxZa0fXgYlZvXp5rW+FlZlTt7ZdYsdGEwgBA6Ta2ZzsTQ9qb 9RKNzLdWCp7Gi+gPJrXQKLb8NG4vjKelGOGfcrB9Mfz/HYp5T6GQbJ8vq0g/hQ4EBNpF ZSUQ== X-Gm-Message-State: ALoCoQkedsEFuULg0BwJR37t5/FFbQ1fWZ4z18AfrBPMWtZl8ldfz4F84JAkfpIxURd9irRIZWbN X-Received: by 10.180.208.46 with SMTP id mb14mr1328806wic.4.1420949019892; Sat, 10 Jan 2015 20:03:39 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.21.9 with SMTP id r9ls564694lae.31.gmail; Sat, 10 Jan 2015 20:03:39 -0800 (PST) X-Received: by 10.112.147.71 with SMTP id ti7mr29953482lbb.87.1420949019737; Sat, 10 Jan 2015 20:03:39 -0800 (PST) Received: from mail-lb0-f172.google.com (mail-lb0-f172.google.com. [209.85.217.172]) by mx.google.com with ESMTPS id uk4si17765082lbb.68.2015.01.10.20.03.39 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 10 Jan 2015 20:03:39 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) client-ip=209.85.217.172; Received: by mail-lb0-f172.google.com with SMTP id z12so13409868lbi.3 for ; Sat, 10 Jan 2015 20:03:39 -0800 (PST) X-Received: by 10.152.45.4 with SMTP id i4mr30133448lam.74.1420949019285; Sat, 10 Jan 2015 20:03:39 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.112.9.200 with SMTP id c8csp744910lbb; Sat, 10 Jan 2015 20:03:38 -0800 (PST) X-Received: by 10.140.97.7 with SMTP id l7mr38031482qge.66.1420949010968; Sat, 10 Jan 2015 20:03:30 -0800 (PST) Received: from mail-qc0-f171.google.com (mail-qc0-f171.google.com. [209.85.216.171]) by mx.google.com with ESMTPS id g6si18069672qgf.0.2015.01.10.20.03.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 10 Jan 2015 20:03:30 -0800 (PST) Received-SPF: pass (google.com: domain of dave.long@linaro.org designates 209.85.216.171 as permitted sender) client-ip=209.85.216.171; Received: by mail-qc0-f171.google.com with SMTP id r5so14398281qcx.2 for ; Sat, 10 Jan 2015 20:03:30 -0800 (PST) X-Received: by 10.140.101.135 with SMTP id u7mr37228345qge.2.1420949010326; Sat, 10 Jan 2015 20:03:30 -0800 (PST) Received: from localhost.localdomain (pool-72-71-243-249.cncdnh.fast00.myfairpoint.net. [72.71.243.249]) by mx.google.com with ESMTPSA id l91sm11357784qge.34.2015.01.10.20.03.29 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 10 Jan 2015 20:03:29 -0800 (PST) From: David Long To: linux-arm-kernel@lists.infradead.org, Russell King Cc: Sandeepa Prabhu , William Cohen , Steve Capper , Catalin Marinas , Will Deacon , "Jon Medhurst (Tixy)" , Masami Hiramatsu , Ananth N Mavinakayanahalli , Anil S Keshavamurthy , , linux-kernel@vger.kernel.org Subject: [PATCH v4 5/6] arm64: Add kernel return probes support(kretprobes) Date: Sat, 10 Jan 2015 23:03:20 -0500 Message-Id: <1420949002-3726-6-git-send-email-dave.long@linaro.org> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1420949002-3726-1-git-send-email-dave.long@linaro.org> References: <1420949002-3726-1-git-send-email-dave.long@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: dave.long@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Sandeepa Prabhu AArch64 ISA does not have instructions to pop the PC register value from the stack(like ARM v7 has ldmia {...,pc}) without using one of the general purpose registers. This means return probes cannot return to the actual return address directly without modifying register context, and without trapping into debug exception. So, like many other architectures, we prepare a global routine with NOPs which serve as a trampoline to hack away the function return address by placing an extra kprobe on the trampoline entry. The pre-handler of this special 'trampoline' kprobe executes the return probe handler functions and restores original return address in ELR_EL1. This way the saved pt_regs still hold the original register context to be carried back to the probed kernel function. Signed-off-by: Sandeepa Prabhu Signed-off-by: David A. Long --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kprobes.h | 1 + arch/arm64/kernel/kprobes.c | 114 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index b3f61ba..de4f056 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -68,6 +68,7 @@ config ARM64 select HAVE_RCU_TABLE_FREE select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES if !XIP_KERNEL + select HAVE_KRETPROBES if HAVE_KPROBES select IRQ_DOMAIN select MODULES_USE_ELF_RELA select NO_BOOTMEM diff --git a/arch/arm64/include/asm/kprobes.h b/arch/arm64/include/asm/kprobes.h index b35d3b9..a2de3b8 100644 --- a/arch/arm64/include/asm/kprobes.h +++ b/arch/arm64/include/asm/kprobes.h @@ -56,5 +56,6 @@ void arch_remove_kprobe(struct kprobe *); int kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr); int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, void *data); +void kretprobe_trampoline(void); #endif /* _ARM_KPROBES_H */ diff --git a/arch/arm64/kernel/kprobes.c b/arch/arm64/kernel/kprobes.c index 31a7894e..cd1069c 100644 --- a/arch/arm64/kernel/kprobes.c +++ b/arch/arm64/kernel/kprobes.c @@ -559,6 +559,117 @@ int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) return 0; } +/* + * Kretprobes: kernel return probes handling + * + * AArch64 mode does not support popping the PC value from the + * stack like on ARM 32-bit (ldmia {..,pc}), so atleast one + * register need to be used to achieve branching/return. + * It means return probes cannot return back to the original + * return address directly without modifying the register context. + * + * So like other architectures, we prepare a global routine + * with NOPs, which serve as trampoline address that hack away the + * function return, with the exact register context. + * Placing a kprobe on trampoline routine entry will trap again to + * execute return probe handlers and restore original return address + * in ELR_EL1, this way saved pt_regs still hold the original + * register values to be carried back to the caller. + */ +static void __used kretprobe_trampoline_holder(void) +{ + asm volatile (".global kretprobe_trampoline\n" + "kretprobe_trampoline:\n" + "NOP\n\t" + "NOP\n\t"); +} + +static int __kprobes +trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) +{ + struct kretprobe_instance *ri = NULL; + struct hlist_head *head, empty_rp; + struct hlist_node *tmp; + unsigned long flags, orig_ret_addr = 0; + unsigned long trampoline_address = + (unsigned long)&kretprobe_trampoline; + + INIT_HLIST_HEAD(&empty_rp); + kretprobe_hash_lock(current, &head, &flags); + + /* + * It is possible to have multiple instances associated with a given + * task either because multiple functions in the call path have + * a return probe installed on them, and/or more than one return + * probe was registered for a target function. + * + * We can handle this because: + * - instances are always inserted at the head of the list + * - when multiple return probes are registered for the same + * function, the first instance's ret_addr will point to the + * real return address, and all the rest will point to + * kretprobe_trampoline + */ + hlist_for_each_entry_safe(ri, tmp, head, hlist) { + if (ri->task != current) + /* another task is sharing our hash bucket */ + continue; + + if (ri->rp && ri->rp->handler) { + __this_cpu_write(current_kprobe, &ri->rp->kp); + get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; + ri->rp->handler(ri, regs); + __this_cpu_write(current_kprobe, NULL); + } + + orig_ret_addr = (unsigned long)ri->ret_addr; + recycle_rp_inst(ri, &empty_rp); + + if (orig_ret_addr != trampoline_address) + /* + * This is the real return address. Any other + * instances associated with this task are for + * other calls deeper on the call stack + */ + break; + } + + kretprobe_assert(ri, orig_ret_addr, trampoline_address); + /* restore the original return address */ + instruction_pointer(regs) = orig_ret_addr; + reset_current_kprobe(); + kretprobe_hash_unlock(current, &flags); + + hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { + hlist_del(&ri->hlist); + kfree(ri); + } + + kprobes_restore_local_irqflag(regs); + + /* return 1 so that post handlers not called */ + return 1; +} + +void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, + struct pt_regs *regs) +{ + ri->ret_addr = (kprobe_opcode_t *)regs->regs[30]; + + /* replace return addr (x30) with trampoline */ + regs->regs[30] = (long)&kretprobe_trampoline; +} + +static struct kprobe trampoline = { + .addr = (kprobe_opcode_t *) &kretprobe_trampoline, + .pre_handler = trampoline_probe_handler +}; + +int __kprobes arch_trampoline_kprobe(struct kprobe *p) +{ + return p->addr == (kprobe_opcode_t *) &kretprobe_trampoline; +} + /* Break Handler hook */ static struct break_hook kprobes_break_hook = { .esr_mask = BRK64_ESR_MASK, @@ -576,5 +687,6 @@ int __init arch_init_kprobes(void) register_break_hook(&kprobes_break_hook); register_step_hook(&kprobes_step_hook); - return 0; + /* register trampoline for kret probe */ + return register_kprobe(&trampoline); }