1
2#include <errno.h>
3#include <libunwind.h>
4#include "perf_regs.h"
5#include "../../util/unwind.h"
6#include "../../util/debug.h"
7
8#ifdef HAVE_ARCH_X86_64_SUPPORT
9int libunwind__arch_reg_id(int regnum)
10{
11	int id;
12
13	switch (regnum) {
14	case UNW_X86_64_RAX:
15		id = PERF_REG_X86_AX;
16		break;
17	case UNW_X86_64_RDX:
18		id = PERF_REG_X86_DX;
19		break;
20	case UNW_X86_64_RCX:
21		id = PERF_REG_X86_CX;
22		break;
23	case UNW_X86_64_RBX:
24		id = PERF_REG_X86_BX;
25		break;
26	case UNW_X86_64_RSI:
27		id = PERF_REG_X86_SI;
28		break;
29	case UNW_X86_64_RDI:
30		id = PERF_REG_X86_DI;
31		break;
32	case UNW_X86_64_RBP:
33		id = PERF_REG_X86_BP;
34		break;
35	case UNW_X86_64_RSP:
36		id = PERF_REG_X86_SP;
37		break;
38	case UNW_X86_64_R8:
39		id = PERF_REG_X86_R8;
40		break;
41	case UNW_X86_64_R9:
42		id = PERF_REG_X86_R9;
43		break;
44	case UNW_X86_64_R10:
45		id = PERF_REG_X86_R10;
46		break;
47	case UNW_X86_64_R11:
48		id = PERF_REG_X86_R11;
49		break;
50	case UNW_X86_64_R12:
51		id = PERF_REG_X86_R12;
52		break;
53	case UNW_X86_64_R13:
54		id = PERF_REG_X86_R13;
55		break;
56	case UNW_X86_64_R14:
57		id = PERF_REG_X86_R14;
58		break;
59	case UNW_X86_64_R15:
60		id = PERF_REG_X86_R15;
61		break;
62	case UNW_X86_64_RIP:
63		id = PERF_REG_X86_IP;
64		break;
65	default:
66		pr_err("unwind: invalid reg id %d\n", regnum);
67		return -EINVAL;
68	}
69
70	return id;
71}
72#else
73int libunwind__arch_reg_id(int regnum)
74{
75	int id;
76
77	switch (regnum) {
78	case UNW_X86_EAX:
79		id = PERF_REG_X86_AX;
80		break;
81	case UNW_X86_EDX:
82		id = PERF_REG_X86_DX;
83		break;
84	case UNW_X86_ECX:
85		id = PERF_REG_X86_CX;
86		break;
87	case UNW_X86_EBX:
88		id = PERF_REG_X86_BX;
89		break;
90	case UNW_X86_ESI:
91		id = PERF_REG_X86_SI;
92		break;
93	case UNW_X86_EDI:
94		id = PERF_REG_X86_DI;
95		break;
96	case UNW_X86_EBP:
97		id = PERF_REG_X86_BP;
98		break;
99	case UNW_X86_ESP:
100		id = PERF_REG_X86_SP;
101		break;
102	case UNW_X86_EIP:
103		id = PERF_REG_X86_IP;
104		break;
105	default:
106		pr_err("unwind: invalid reg id %d\n", regnum);
107		return -EINVAL;
108	}
109
110	return id;
111}
112#endif /* HAVE_ARCH_X86_64_SUPPORT */
113