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