From patchwork Sat Aug 29 04:21:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 52827 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f197.google.com (mail-lb0-f197.google.com [209.85.217.197]) by patches.linaro.org (Postfix) with ESMTPS id F224220503 for ; Sat, 29 Aug 2015 04:25:04 +0000 (UTC) Received: by lbcue2 with SMTP id ue2sf22501565lbc.1 for ; Fri, 28 Aug 2015 21:25:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=0Be1TGddiH1TunAyL60qKkTB6tJ9MDc+xYtjcnxuM+o=; b=J1w7nomnHU77wdto4SkKm9KoCg6Kdn+ZJALfpQHFiOTOqQUaXSjEMMXMm3hXPtANVO Ng/CQxVlbvstJaTPGR5FRE/sJbZfgHGpEPk44dowXaE2LJRBqRToButgXbGnexUvsxzC ZKNTViUBY5pSvcFgTQEfLYOb5OmNpMdUsUT5ACdXOLDbv+AZXQYACuQ5C6Qir+gcTFdT sMXVdJXw6x+6aRnPfW4/x/xU4smfWlrLMLmT/ZgVG5SWkRBwA24DmEQk+7NhOQbkORmy WlFtHSfq+GGgY3NbJ+wOU34+2vanDkKKRQ6Pyf9oJPmZ/rnmUEdFJdRe+bkOf/nrP5P+ Lf1g== X-Gm-Message-State: ALoCoQn4ryTiod6GKOrw3iuWDmPEF8d7iy4j71Rq6CY4Awv63D+O4dM9c+K+IPB4h695vf0gSCin X-Received: by 10.194.19.33 with SMTP id b1mr3554991wje.6.1440822303932; Fri, 28 Aug 2015 21:25:03 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.37.162 with SMTP id z2ls324148laj.101.gmail; Fri, 28 Aug 2015 21:25:03 -0700 (PDT) X-Received: by 10.152.207.39 with SMTP id lt7mr817117lac.88.1440822303628; Fri, 28 Aug 2015 21:25:03 -0700 (PDT) Received: from mail-la0-f53.google.com (mail-la0-f53.google.com. [209.85.215.53]) by mx.google.com with ESMTPS id g6si7672638lbo.47.2015.08.28.21.25.03 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Aug 2015 21:25:03 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.53 as permitted sender) client-ip=209.85.215.53; Received: by laboe4 with SMTP id oe4so17417338lab.0 for ; Fri, 28 Aug 2015 21:25:03 -0700 (PDT) X-Received: by 10.152.9.200 with SMTP id c8mr1230737lab.76.1440822303491; Fri, 28 Aug 2015 21:25:03 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.151.194 with SMTP id us2csp267775lbb; Fri, 28 Aug 2015 21:25:02 -0700 (PDT) X-Received: by 10.66.141.109 with SMTP id rn13mr20044468pab.127.1440822302204; Fri, 28 Aug 2015 21:25:02 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id pt9si3397699pbc.137.2015.08.28.21.25.00; Fri, 28 Aug 2015 21:25:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752874AbbH2EYX (ORCPT + 28 others); Sat, 29 Aug 2015 00:24:23 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:3777 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752777AbbH2EYQ (ORCPT ); Sat, 29 Aug 2015 00:24:16 -0400 Received: from 172.24.1.47 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.47]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BLW04310; Sat, 29 Aug 2015 12:23:22 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml431-hub.china.huawei.com (10.82.67.208) with Microsoft SMTP Server id 14.3.235.1; Sat, 29 Aug 2015 12:23:16 +0800 From: Wang Nan To: , , CC: , , , He Kuang , Wang Nan , Brendan Gregg , Daniel Borkmann , "David Ahern" , Jiri Olsa , Kaixu Xia , Masami Hiramatsu , Namhyung Kim , Paul Mackerras , Peter Zijlstra Subject: [PATCH 23/31] perf tools: Introduce arch_get_reg_info() for x86 Date: Sat, 29 Aug 2015 04:21:57 +0000 Message-ID: <1440822125-52691-24-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1440822125-52691-1-git-send-email-wangnan0@huawei.com> References: <1440822125-52691-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020205.55E133BB.0047, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 63a70a3c62a47f88f1ff9b1c89e0a7d2 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: wangnan0@huawei.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.53 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: He Kuang arch_get_reg_info() is a helper function which converts register name like "%rax" to offset of a register in 'struct pt_regs', which is required by BPF prologue generator. This patch replaces original string table by a 'struct reg_info' table, which records offset of registers according to its name. For x86, since there are two sub-archs (x86_32 and x86_64) but we can only get pt_regs for the arch we are currently on, this patch fills offset with '-1' for another sub-arch. This introduces a limitation to perf prologue that, we are unable to generate prologue on a x86_32 compiled perf for BPF programs targeted on x86_64 kernel. This limitation is acceptable, because this is a very rare usecase. Signed-off-by: Wang Nan Signed-off-by: He Kuang Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Cc: Arnaldo Carvalho de Melo Link: http://lkml.kernel.org/n/1436445342-1402-34-git-send-email-wangnan0@huawei.com --- tools/perf/arch/x86/Makefile | 1 + tools/perf/arch/x86/util/Build | 2 + tools/perf/arch/x86/util/dwarf-regs.c | 104 ++++++++++++++++++++++++---------- 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/tools/perf/arch/x86/Makefile b/tools/perf/arch/x86/Makefile index 21322e0..a84a6f6f 100644 --- a/tools/perf/arch/x86/Makefile +++ b/tools/perf/arch/x86/Makefile @@ -2,3 +2,4 @@ ifndef NO_DWARF PERF_HAVE_DWARF_REGS := 1 endif HAVE_KVM_STAT_SUPPORT := 1 +PERF_HAVE_ARCH_GET_REG_INFO := 1 diff --git a/tools/perf/arch/x86/util/Build b/tools/perf/arch/x86/util/Build index 2c55e1b..09429f6 100644 --- a/tools/perf/arch/x86/util/Build +++ b/tools/perf/arch/x86/util/Build @@ -3,6 +3,8 @@ libperf-y += tsc.o libperf-y += pmu.o libperf-y += kvm-stat.o +# BPF_PROLOGUE also need dwarf-regs.o. However, if CONFIG_BPF_PROLOGUE +# is true, CONFIG_DWARF must true. libperf-$(CONFIG_DWARF) += dwarf-regs.o libperf-$(CONFIG_LIBUNWIND) += unwind-libunwind.o diff --git a/tools/perf/arch/x86/util/dwarf-regs.c b/tools/perf/arch/x86/util/dwarf-regs.c index be22dd4..9928caf 100644 --- a/tools/perf/arch/x86/util/dwarf-regs.c +++ b/tools/perf/arch/x86/util/dwarf-regs.c @@ -22,44 +22,67 @@ #include #include +#include +#include +#include /* for offsetof */ +#include + +struct reg_info { + const char *name; /* Reg string in debuginfo */ + int offset; /* Reg offset in struct pt_regs */ +}; /* * Generic dwarf analysis helpers */ - +/* + * x86_64 compiling can't access pt_regs for x86_32, so fill offset + * with -1. + */ +#ifdef __x86_64__ +# define REG_INFO(n, f) { .name = n, .offset = -1, } +#else +# define REG_INFO(n, f) { .name = n, .offset = offsetof(struct pt_regs, f), } +#endif #define X86_32_MAX_REGS 8 -const char *x86_32_regs_table[X86_32_MAX_REGS] = { - "%ax", - "%cx", - "%dx", - "%bx", - "$stack", /* Stack address instead of %sp */ - "%bp", - "%si", - "%di", + +struct reg_info x86_32_regs_table[X86_32_MAX_REGS] = { + REG_INFO("%ax", eax), + REG_INFO("%cx", ecx), + REG_INFO("%dx", edx), + REG_INFO("%bx", ebx), + REG_INFO("$stack", esp), /* Stack address instead of %sp */ + REG_INFO("%bp", ebp), + REG_INFO("%si", esi), + REG_INFO("%di", edi), }; +#undef REG_INFO +#ifdef __x86_64__ +# define REG_INFO(n, f) { .name = n, .offset = offsetof(struct pt_regs, f), } +#else +# define REG_INFO(n, f) { .name = n, .offset = -1, } +#endif #define X86_64_MAX_REGS 16 -const char *x86_64_regs_table[X86_64_MAX_REGS] = { - "%ax", - "%dx", - "%cx", - "%bx", - "%si", - "%di", - "%bp", - "%sp", - "%r8", - "%r9", - "%r10", - "%r11", - "%r12", - "%r13", - "%r14", - "%r15", +struct reg_info x86_64_regs_table[X86_64_MAX_REGS] = { + REG_INFO("%ax", rax), + REG_INFO("%dx", rdx), + REG_INFO("%cx", rcx), + REG_INFO("%bx", rbx), + REG_INFO("%si", rsi), + REG_INFO("%di", rdi), + REG_INFO("%bp", rbp), + REG_INFO("%sp", rsp), + REG_INFO("%r8", r8), + REG_INFO("%r9", r9), + REG_INFO("%r10", r10), + REG_INFO("%r11", r11), + REG_INFO("%r12", r12), + REG_INFO("%r13", r13), + REG_INFO("%r14", r14), + REG_INFO("%r15", r15), }; -/* TODO: switching by dwarf address size */ #ifdef __x86_64__ #define ARCH_MAX_REGS X86_64_MAX_REGS #define arch_regs_table x86_64_regs_table @@ -71,5 +94,28 @@ const char *x86_64_regs_table[X86_64_MAX_REGS] = { /* Return architecture dependent register string (for kprobe-tracer) */ const char *get_arch_regstr(unsigned int n) { - return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL; + return (n <= ARCH_MAX_REGS) ? arch_regs_table[n].name : NULL; } + +#ifdef HAVE_BPF_PROLOGUE +int arch_get_reg_info(const char *name, int *offset) +{ + int i; + struct reg_info *info; + + if (!name || !offset) + return -1; + + for (i = 0; i < ARCH_MAX_REGS; i++) { + info = &arch_regs_table[i]; + if (strcmp(info->name, name) == 0) { + if (info->offset < 0) + return -1; + *offset = info->offset; + return 0; + } + } + + return -1; +} +#endif