linux/tools/perf/arch/x86/util/dwarf-regs.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
   4 * Extracted from probe-finder.c
   5 *
   6 * Written by Masami Hiramatsu <mhiramat@redhat.com>
   7 */
   8
   9#include <stddef.h>
  10#include <errno.h> /* for EINVAL */
  11#include <string.h> /* for strcmp */
  12#include <linux/ptrace.h> /* for struct pt_regs */
  13#include <linux/kernel.h> /* for offsetof */
  14#include <dwarf-regs.h>
  15
  16/*
  17 * See arch/x86/kernel/ptrace.c.
  18 * Different from it:
  19 *
  20 *  - Since struct pt_regs is defined differently for user and kernel,
  21 *    but we want to use 'ax, bx' instead of 'rax, rbx' (which is struct
  22 *    field name of user's pt_regs), we make REG_OFFSET_NAME to accept
  23 *    both string name and reg field name.
  24 *
  25 *  - Since accessing x86_32's pt_regs from x86_64 building is difficult
  26 *    and vise versa, we simply fill offset with -1, so
  27 *    get_arch_regstr() still works but regs_query_register_offset()
  28 *    returns error.
  29 *    The only inconvenience caused by it now is that we are not allowed
  30 *    to generate BPF prologue for a x86_64 kernel if perf is built for
  31 *    x86_32. This is really a rare usecase.
  32 *
  33 *  - Order is different from kernel's ptrace.c for get_arch_regstr(). Use
  34 *    the order defined by dwarf.
  35 */
  36
  37struct pt_regs_offset {
  38        const char *name;
  39        int offset;
  40};
  41
  42#define REG_OFFSET_END {.name = NULL, .offset = 0}
  43
  44#ifdef __x86_64__
  45# define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
  46# define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = -1}
  47#else
  48# define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = -1}
  49# define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
  50#endif
  51
  52/* TODO: switching by dwarf address size */
  53#ifndef __x86_64__
  54static const struct pt_regs_offset x86_32_regoffset_table[] = {
  55        REG_OFFSET_NAME_32("%ax",       eax),
  56        REG_OFFSET_NAME_32("%cx",       ecx),
  57        REG_OFFSET_NAME_32("%dx",       edx),
  58        REG_OFFSET_NAME_32("%bx",       ebx),
  59        REG_OFFSET_NAME_32("$stack",    esp),   /* Stack address instead of %sp */
  60        REG_OFFSET_NAME_32("%bp",       ebp),
  61        REG_OFFSET_NAME_32("%si",       esi),
  62        REG_OFFSET_NAME_32("%di",       edi),
  63        REG_OFFSET_END,
  64};
  65
  66#define regoffset_table x86_32_regoffset_table
  67#else
  68static const struct pt_regs_offset x86_64_regoffset_table[] = {
  69        REG_OFFSET_NAME_64("%ax",       rax),
  70        REG_OFFSET_NAME_64("%dx",       rdx),
  71        REG_OFFSET_NAME_64("%cx",       rcx),
  72        REG_OFFSET_NAME_64("%bx",       rbx),
  73        REG_OFFSET_NAME_64("%si",       rsi),
  74        REG_OFFSET_NAME_64("%di",       rdi),
  75        REG_OFFSET_NAME_64("%bp",       rbp),
  76        REG_OFFSET_NAME_64("%sp",       rsp),
  77        REG_OFFSET_NAME_64("%r8",       r8),
  78        REG_OFFSET_NAME_64("%r9",       r9),
  79        REG_OFFSET_NAME_64("%r10",      r10),
  80        REG_OFFSET_NAME_64("%r11",      r11),
  81        REG_OFFSET_NAME_64("%r12",      r12),
  82        REG_OFFSET_NAME_64("%r13",      r13),
  83        REG_OFFSET_NAME_64("%r14",      r14),
  84        REG_OFFSET_NAME_64("%r15",      r15),
  85        REG_OFFSET_END,
  86};
  87
  88#define regoffset_table x86_64_regoffset_table
  89#endif
  90
  91/* Minus 1 for the ending REG_OFFSET_END */
  92#define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
  93
  94/* Return architecture dependent register string (for kprobe-tracer) */
  95const char *get_arch_regstr(unsigned int n)
  96{
  97        return (n < ARCH_MAX_REGS) ? regoffset_table[n].name : NULL;
  98}
  99
 100/* Reuse code from arch/x86/kernel/ptrace.c */
 101/**
 102 * regs_query_register_offset() - query register offset from its name
 103 * @name:       the name of a register
 104 *
 105 * regs_query_register_offset() returns the offset of a register in struct
 106 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
 107 */
 108int regs_query_register_offset(const char *name)
 109{
 110        const struct pt_regs_offset *roff;
 111        for (roff = regoffset_table; roff->name != NULL; roff++)
 112                if (!strcmp(roff->name, name))
 113                        return roff->offset;
 114        return -EINVAL;
 115}
 116