18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
48c2ecf20Sopenharmony_ci * Extracted from probe-finder.c
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Written by Masami Hiramatsu <mhiramat@redhat.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <stddef.h>
108c2ecf20Sopenharmony_ci#include <errno.h> /* for EINVAL */
118c2ecf20Sopenharmony_ci#include <string.h> /* for strcmp */
128c2ecf20Sopenharmony_ci#include <linux/ptrace.h> /* for struct pt_regs */
138c2ecf20Sopenharmony_ci#include <linux/kernel.h> /* for offsetof */
148c2ecf20Sopenharmony_ci#include <dwarf-regs.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * See arch/x86/kernel/ptrace.c.
188c2ecf20Sopenharmony_ci * Different from it:
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *  - Since struct pt_regs is defined differently for user and kernel,
218c2ecf20Sopenharmony_ci *    but we want to use 'ax, bx' instead of 'rax, rbx' (which is struct
228c2ecf20Sopenharmony_ci *    field name of user's pt_regs), we make REG_OFFSET_NAME to accept
238c2ecf20Sopenharmony_ci *    both string name and reg field name.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *  - Since accessing x86_32's pt_regs from x86_64 building is difficult
268c2ecf20Sopenharmony_ci *    and vise versa, we simply fill offset with -1, so
278c2ecf20Sopenharmony_ci *    get_arch_regstr() still works but regs_query_register_offset()
288c2ecf20Sopenharmony_ci *    returns error.
298c2ecf20Sopenharmony_ci *    The only inconvenience caused by it now is that we are not allowed
308c2ecf20Sopenharmony_ci *    to generate BPF prologue for a x86_64 kernel if perf is built for
318c2ecf20Sopenharmony_ci *    x86_32. This is really a rare usecase.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci *  - Order is different from kernel's ptrace.c for get_arch_regstr(). Use
348c2ecf20Sopenharmony_ci *    the order defined by dwarf.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct pt_regs_offset {
388c2ecf20Sopenharmony_ci	const char *name;
398c2ecf20Sopenharmony_ci	int offset;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define REG_OFFSET_END {.name = NULL, .offset = 0}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#ifdef __x86_64__
458c2ecf20Sopenharmony_ci# define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
468c2ecf20Sopenharmony_ci# define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = -1}
478c2ecf20Sopenharmony_ci#else
488c2ecf20Sopenharmony_ci# define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = -1}
498c2ecf20Sopenharmony_ci# define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* TODO: switching by dwarf address size */
538c2ecf20Sopenharmony_ci#ifndef __x86_64__
548c2ecf20Sopenharmony_cistatic const struct pt_regs_offset x86_32_regoffset_table[] = {
558c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%ax",	eax),
568c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%cx",	ecx),
578c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%dx",	edx),
588c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%bx",	ebx),
598c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("$stack",	esp),	/* Stack address instead of %sp */
608c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%bp",	ebp),
618c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%si",	esi),
628c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_32("%di",	edi),
638c2ecf20Sopenharmony_ci	REG_OFFSET_END,
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define regoffset_table x86_32_regoffset_table
678c2ecf20Sopenharmony_ci#else
688c2ecf20Sopenharmony_cistatic const struct pt_regs_offset x86_64_regoffset_table[] = {
698c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%ax",	rax),
708c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%dx",	rdx),
718c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%cx",	rcx),
728c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%bx",	rbx),
738c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%si",	rsi),
748c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%di",	rdi),
758c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%bp",	rbp),
768c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%sp",	rsp),
778c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r8",	r8),
788c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r9",	r9),
798c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r10",	r10),
808c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r11",	r11),
818c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r12",	r12),
828c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r13",	r13),
838c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r14",	r14),
848c2ecf20Sopenharmony_ci	REG_OFFSET_NAME_64("%r15",	r15),
858c2ecf20Sopenharmony_ci	REG_OFFSET_END,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define regoffset_table x86_64_regoffset_table
898c2ecf20Sopenharmony_ci#endif
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* Minus 1 for the ending REG_OFFSET_END */
928c2ecf20Sopenharmony_ci#define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* Return architecture dependent register string (for kprobe-tracer) */
958c2ecf20Sopenharmony_ciconst char *get_arch_regstr(unsigned int n)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	return (n < ARCH_MAX_REGS) ? regoffset_table[n].name : NULL;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* Reuse code from arch/x86/kernel/ptrace.c */
1018c2ecf20Sopenharmony_ci/**
1028c2ecf20Sopenharmony_ci * regs_query_register_offset() - query register offset from its name
1038c2ecf20Sopenharmony_ci * @name:	the name of a register
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * regs_query_register_offset() returns the offset of a register in struct
1068c2ecf20Sopenharmony_ci * pt_regs from its name. If the name is invalid, this returns -EINVAL;
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ciint regs_query_register_offset(const char *name)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	const struct pt_regs_offset *roff;
1118c2ecf20Sopenharmony_ci	for (roff = regoffset_table; roff->name != NULL; roff++)
1128c2ecf20Sopenharmony_ci		if (!strcmp(roff->name, name))
1138c2ecf20Sopenharmony_ci			return roff->offset;
1148c2ecf20Sopenharmony_ci	return -EINVAL;
1158c2ecf20Sopenharmony_ci}
116