From patchwork Fri Sep 9 12:19:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Labath X-Patchwork-Id: 9323185 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 125F060231 for ; Fri, 9 Sep 2016 12:21:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F3CF029E4C for ; Fri, 9 Sep 2016 12:21:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E82F429E50; Fri, 9 Sep 2016 12:21:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3E2C629E4C for ; Fri, 9 Sep 2016 12:21:57 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1biKnD-0007ME-E6; Fri, 09 Sep 2016 12:20:27 +0000 Received: from mail-qk0-x234.google.com ([2607:f8b0:400d:c09::234]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1biKn7-0006E6-43 for linux-arm-kernel@lists.infradead.org; Fri, 09 Sep 2016 12:20:23 +0000 Received: by mail-qk0-x234.google.com with SMTP id v123so79501051qkh.2 for ; Fri, 09 Sep 2016 05:20:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:from:date:message-id:subject:to:cc; bh=CjND2rWTePmwj6lWmMi6Ap+KfRBtpW3tD3V/zH+9TK0=; b=PA89F/rS8K5xOw+hhYwdwbx/6U1QKtAoZsFcAZfan1XyKmrgPy1+cuZMH07TWp2XjH B1YCZvNDh8XSzh3vahyBHCHeSjYcvnDJNpGu918A5B/AMD64FCS5rSag0DK4/erHeduK NohbJ/wFikiXGFnxaeU4HxO98j58NUATcpWrDJ4mEQp21l5EQiaG/oqhSwjxIt3Ms/0x 34BTrCFqAZdNpoM8ZPVukSxa39FKVnuwBwZoUn9BDR/cnWOTxyHCp4G3z45vypncVR4g FJ7Ken6au8Z3fBEv9tgkSCsp7D/FqEMy1GBGXgVlP6h9CUL7URV15ztNBJmOaybtBe3w IGkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=CjND2rWTePmwj6lWmMi6Ap+KfRBtpW3tD3V/zH+9TK0=; b=hM2XEx6VzZTIZG387xA+zinHM1AuVPRbSN1usYvlnalSKMfqLMHDot8wzJBcdkL6VQ etW1+FHGpRF1LbaPvbg6XJGgsmetGEKmg+giyHp5c3jKj4NDX4X7mU1ada97b//pywn7 obvw2qLR5W109Na9g3bO0bfc+2vNB5inGhQaceqOVkfOViQaxBoCBBwsJk+KJxjNaCKJ 01pnpFtLIkb/HCW6LJ1r02E7Tvgp/eWe9Z5hk6FWfE/Ow1L/baDa/2W80oLrml7FmZl0 bqBHqJ+t3gHieCWdRjcSjkP56dYaXlxV9HnTr0ddtCPFtTNJpP5Yqo4dB4N0sWWc4yOT oDxQ== X-Gm-Message-State: AE9vXwNcbhgGyAPA7uZaFikj7CRxOri+vupjukQ6BUKUBx2amUfeEe7AZq37S07zWMNfBgoCC309Ayq3PplLcRCj X-Received: by 10.55.75.145 with SMTP id y139mr3482128qka.26.1473423599218; Fri, 09 Sep 2016 05:19:59 -0700 (PDT) MIME-Version: 1.0 Received: by 10.55.104.142 with HTTP; Fri, 9 Sep 2016 05:19:38 -0700 (PDT) From: Pavel Labath Date: Fri, 9 Sep 2016 13:19:38 +0100 Message-ID: Subject: [PATCH] arm64: hw_breakpoint: Handle inexact watchpoint addresses To: linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160909_052021_414513_07147A87 X-CRM114-Status: GOOD ( 29.07 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pratyush Anand , Will Deacon Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From c0cac44836ac78a2098dfdcb3e4563731f931bc8 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 8 Sep 2016 15:15:43 +0100 Subject: [PATCH] arm64: hw_breakpoint: Handle inexact watchpoint addresses Arm64 hardware does not always report a watchpoint hit address that matches one of the watchpoints set. It can also report an address "near" the watchpoint if a single instruction access both watched and unwatched addresses. There is no straight-forward way, short of disassembling the offending instruction, to map that address back to the watchpoint. Previously, when the hardware reported a watchpoint hit on an address that did not match our watchpoint (this happens in case of instructions which access large chunks of memory such as "stp") the process would enter a loop where we would be continually resuming it (because we did not recognise that watchpoint hit) and it would keep hitting the watchpoint again and again. The tracing process would never get notified of the watchpoint hit. This commit fixes the problem by looking at the watchpoints near the address reported by the hardware. If the address does not exactly match one of the watchpoints we have set, it attributes the hit to the nearest watchpoint we have. This heuristic is a bit dodgy, but I don't think we can do much more, given the hardware limitations. I include a kernel selftest which triggers this code. Signed-off-by: Pavel Labath --- arch/arm64/kernel/hw_breakpoint.c | 104 +++++++--- tools/testing/selftests/breakpoints/Makefile | 5 +- .../selftests/breakpoints/breakpoint_test-arm.c | 217 +++++++++++++++++++++ 3 files changed, 298 insertions(+), 28 deletions(-) create mode 100644 tools/testing/selftests/breakpoints/breakpoint_test-arm.c diff --git a/arch/arm64/kernel/hw_breakpoint.c b/arch/arm64/kernel/hw_breakpoint.c index 26a6bf7..d0ebfe6 100644 --- a/arch/arm64/kernel/hw_breakpoint.c +++ b/arch/arm64/kernel/hw_breakpoint.c @@ -661,50 +661,80 @@ unlock: } NOKPROBE_SYMBOL(breakpoint_handler); +/* + * Arm64 hardware does not always report a watchpoint hit address that matches + * one of the watchpoints set. It can also report an address "near" the + * watchpoint if a single instruction access both watched and unwatched + * addresses. There is no straight-forward way, short of disassembling the + * offending instruction, to map that address back to the watchpoint. This + * function computes the distance of the memory access from the watchpoint as a + * heuristic for the likelyhood that a given access triggered the watchpoint. + * + * See Section D2.10.5 "Determining the memory location that caused a Watchpoint + * exception" of ARMv8 Architecture Reference Manual for details. + * + * The function returns the distance of the address from the bytes watched by + * the watchpoint. In case of an exact match, it returns 0. + */ +static u64 get_distance_from_watchpoint(unsigned long addr, int i, + struct arch_hw_breakpoint *info) +{ + u64 val, alignment_mask, wp_low, wp_high; + u32 ctrl_reg; + int first_bit; + struct arch_hw_breakpoint_ctrl ctrl; + + /* AArch32 watchpoints are either 4 or 8 bytes aligned. */ + if (is_compat_task()) { + if (info->ctrl.len == ARM_BREAKPOINT_LEN_8) + alignment_mask = 0x7; + else + alignment_mask = 0x3; + } else { + alignment_mask = 0x7; + } + + val = read_wb_reg(AARCH64_DBG_REG_WVR, i) & ~alignment_mask; + + ctrl_reg = read_wb_reg(AARCH64_DBG_REG_WCR, i); + decode_ctrl_reg(ctrl_reg, &ctrl); + first_bit = ffs(ctrl.len); + if (first_bit == 0) + return -1; + wp_low = val + first_bit - 1; + wp_high = val + fls(ctrl.len) - 1; + if (addr < wp_low) + return wp_low - addr; + else if (addr > wp_high) + return addr - wp_high; + else + return 0; + +} + static int watchpoint_handler(unsigned long addr, unsigned int esr, struct pt_regs *regs) { - int i, step = 0, *kernel_step, access; - u32 ctrl_reg; - u64 val, alignment_mask; + int i, step = 0, *kernel_step, access, closest_match; + u64 min_dist = -1, dist; struct perf_event *wp, **slots; struct debug_info *debug_info; struct arch_hw_breakpoint *info; - struct arch_hw_breakpoint_ctrl ctrl; slots = this_cpu_ptr(wp_on_reg); debug_info = ¤t->thread.debug; + /* + * Find all watchpoints that match the reported address. If no exact + * match is found. Attribute the hit to the closest watchpoint. + */ for (i = 0; i < core_num_wrps; ++i) { rcu_read_lock(); wp = slots[i]; - if (wp == NULL) goto unlock; - info = counter_arch_bp(wp); - /* AArch32 watchpoints are either 4 or 8 bytes aligned. */ - if (is_compat_task()) { - if (info->ctrl.len == ARM_BREAKPOINT_LEN_8) - alignment_mask = 0x7; - else - alignment_mask = 0x3; - } else { - alignment_mask = 0x7; - } - - /* Check if the watchpoint value matches. */ - val = read_wb_reg(AARCH64_DBG_REG_WVR, i); - if (val != (addr & ~alignment_mask)) - goto unlock; - - /* Possible match, check the byte address select to confirm. */ - ctrl_reg = read_wb_reg(AARCH64_DBG_REG_WCR, i); - decode_ctrl_reg(ctrl_reg, &ctrl); - if (!((1 << (addr & alignment_mask)) & ctrl.len)) - goto unlock; - /* * Check that the access type matches. * 0 => load, otherwise => store @@ -714,6 +744,17 @@ static int watchpoint_handler(unsigned long addr, unsigned int esr, if (!(access & hw_breakpoint_type(wp))) goto unlock; + info = counter_arch_bp(wp); + + dist = get_distance_from_watchpoint(addr, i, info); + if (dist < min_dist) { + min_dist = dist; + closest_match = i; + } + /* Is this an exact match? */ + if (dist != 0) + goto unlock; + info->trigger = addr; perf_bp_event(wp, regs); @@ -724,6 +765,15 @@ static int watchpoint_handler(unsigned long addr, unsigned int esr, unlock: rcu_read_unlock(); } + if (min_dist > 0 && min_dist != -1) { + /* No exact match found. */ + rcu_read_lock(); + wp = slots[closest_match]; + info = counter_arch_bp(wp); + info->trigger = addr; + perf_bp_event(wp, regs); + rcu_read_unlock(); + } if (!step) return 0; diff --git a/tools/testing/selftests/breakpoints/Makefile b/tools/testing/selftests/breakpoints/Makefile index 74e533f..458a31a 100644 --- a/tools/testing/selftests/breakpoints/Makefile +++ b/tools/testing/selftests/breakpoints/Makefile @@ -5,6 +5,9 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/) ifeq ($(ARCH),x86) TEST_PROGS := breakpoint_test endif +ifeq ($(ARCH),arm64) +TEST_PROGS := breakpoint_test-arm +endif TEST_PROGS += step_after_suspend_test @@ -13,4 +16,4 @@ all: $(TEST_PROGS) include ../lib.mk clean: - rm -fr breakpoint_test step_after_suspend_test + rm -fr breakpoint_test breakpoint_test-arm step_after_suspend_test diff --git a/tools/testing/selftests/breakpoints/breakpoint_test-arm.c b/tools/testing/selftests/breakpoints/breakpoint_test-arm.c new file mode 100644 index 0000000..9f00ea6 --- /dev/null +++ b/tools/testing/selftests/breakpoints/breakpoint_test-arm.c @@ -0,0 +1,217 @@ +/* + * Copyright (C) 2016 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +enum Test { + TEST_WRITE_1, TEST_WRITE_2, TEST_WRITE_4, TEST_WRITE_8, + TEST_WRITE_16, TEST_WRITE_32, TEST_MAX +}; + +struct Data { + union { + uint8_t u8[32]; + uint16_t u16[16]; + uint32_t u32[8]; + uint64_t u64[4]; + }; +}; +volatile struct Data var __aligned(32); + + +void child(enum Test test) +{ + if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) { + perror("ptrace(PTRACE_TRACEME) failed"); + _exit(1); + } + + if (raise(SIGSTOP) != 0) { + perror("raise(SIGSTOP) failed"); + _exit(1); + } + + switch (test) { + case TEST_WRITE_1: + var.u8[31] = 47; + break; + case TEST_WRITE_2: + var.u16[15] = 47; + break; + case TEST_WRITE_4: + var.u32[7] = 47; + break; + case TEST_WRITE_8: + var.u64[3] = 47; + break; + case TEST_WRITE_16: + __asm__ volatile ("stp x29, x30, %0" : "=m" (var.u64[2])); + break; + case TEST_WRITE_32: + __asm__ volatile ("stp q29, q30, %0" : "=m" (var)); + break; + } + + _exit(0); +} + +static bool set_watchpoint(pid_t pid, const volatile void *address, + size_t size) +{ + const unsigned byte_mask = (1 << size) - 1; + const unsigned type = 2; /* Write */ + const unsigned enable = 1; + const unsigned control = byte_mask << 5 | type << 3 | enable; + struct user_hwdebug_state dreg_state; + struct iovec iov; + + memset(&dreg_state, 0, sizeof(dreg_state)); + dreg_state.dbg_regs[0].addr = (uintptr_t)address; + dreg_state.dbg_regs[0].ctrl = control; + iov.iov_base = &dreg_state; + iov.iov_len = offsetof(struct user_hwdebug_state, dbg_regs) + + sizeof(dreg_state.dbg_regs[0]); + if (ptrace(PTRACE_SETREGSET, pid, NT_ARM_HW_WATCH, &iov) == 0) + return true; + + if (errno == EIO) { + printf("ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) " + "not supported on this hardware\n"); + ksft_exit_skip(); + } + perror("ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) failed"); + return false; +} + + +bool run_test(enum Test test) +{ + int status; + siginfo_t siginfo; + pid_t pid = fork(); + pid_t wpid; + + if (pid < 0) { + perror("fork() failed"); + return false; + } + if (pid == 0) + child(test); + + wpid = waitpid(pid, &status, __WALL); + if (wpid != pid) { + perror("waitpid() failed"); + return false; + } + if (!WIFSTOPPED(status)) { + printf("child did not stop\n"); + return false; + } + if (WSTOPSIG(status) != SIGSTOP) { + printf("child did not stop with SIGSTOP\n"); + return false; + } + + if (!set_watchpoint(pid, &var.u64[3], 8)) + return false; + + if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) { + perror("ptrace(PTRACE_SINGLESTEP) failed"); + return false; + } + + alarm(3); + wpid = waitpid(pid, &status, __WALL); + if (wpid != pid) { + perror("waitpid() failed"); + return false; + } + alarm(0); + if (WIFEXITED(status)) { + printf("child did not single-step\n"); + return false; + } + if (!WIFSTOPPED(status)) { + printf("child did not stop\n"); + return false; + } + if (WSTOPSIG(status) != SIGTRAP) { + printf("child did not stop with SIGTRAP\n"); + return false; + } + if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0) { + perror("ptrace(PTRACE_GETSIGINFO)"); + return false; + } + if (siginfo.si_code != TRAP_HWBKPT) { + printf("Unexpected si_code %d\n", siginfo.si_code); + return false; + } + + kill(pid, SIGKILL); + wpid = waitpid(pid, &status, 0); + if (wpid != pid) { + perror("waitpid() failed"); + return false; + } + return true; +} + +void sigalrm(int sig) +{ +} + +int main(int argc, char **argv) +{ + int opt; + bool succeeded = true; + enum Test test; + struct sigaction act; + + act.sa_handler = sigalrm; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + sigaction(SIGALRM, &act, NULL); + for (test = 0; test < TEST_MAX; ++test) { + printf("Test %d ", test); + if (run_test(test)) { + printf("[OK]\n"); + ksft_inc_pass_cnt(); + } else { + printf("[FAILED]\n"); + ksft_inc_fail_cnt(); + succeeded = false; + } + } + + ksft_print_cnts(); + if (succeeded) + ksft_exit_pass(); + else + ksft_exit_fail(); +} + -- 2.8.0.rc3.226.g39d4020