1 #include <elfutils/libdwfl.h>
2 #include "../../util/unwind-libdw.h"
3 #include "../../util/perf_regs.h"
4 #include "../../util/event.h"
5 
6 /* See backends/ppc_initreg.c and backends/ppc_regs.c in elfutils.  */
7 static const int special_regs[3][2] = {
8 	{ 65, PERF_REG_POWERPC_LINK },
9 	{ 101, PERF_REG_POWERPC_XER },
10 	{ 109, PERF_REG_POWERPC_CTR },
11 };
12 
13 bool libdw__arch_set_initial_registers(Dwfl_Thread *thread, void *arg)
14 {
15 	struct unwind_info *ui = arg;
16 	struct regs_dump *user_regs = &ui->sample->user_regs;
17 	Dwarf_Word dwarf_regs[32], dwarf_nip;
18 	size_t i;
19 
20 #define REG(r) ({						\
21 	Dwarf_Word val = 0;					\
22 	perf_reg_value(&val, user_regs, PERF_REG_POWERPC_##r);	\
23 	val;							\
24 })
25 
26 	dwarf_regs[0]  = REG(R0);
27 	dwarf_regs[1]  = REG(R1);
28 	dwarf_regs[2]  = REG(R2);
29 	dwarf_regs[3]  = REG(R3);
30 	dwarf_regs[4]  = REG(R4);
31 	dwarf_regs[5]  = REG(R5);
32 	dwarf_regs[6]  = REG(R6);
33 	dwarf_regs[7]  = REG(R7);
34 	dwarf_regs[8]  = REG(R8);
35 	dwarf_regs[9]  = REG(R9);
36 	dwarf_regs[10] = REG(R10);
37 	dwarf_regs[11] = REG(R11);
38 	dwarf_regs[12] = REG(R12);
39 	dwarf_regs[13] = REG(R13);
40 	dwarf_regs[14] = REG(R14);
41 	dwarf_regs[15] = REG(R15);
42 	dwarf_regs[16] = REG(R16);
43 	dwarf_regs[17] = REG(R17);
44 	dwarf_regs[18] = REG(R18);
45 	dwarf_regs[19] = REG(R19);
46 	dwarf_regs[20] = REG(R20);
47 	dwarf_regs[21] = REG(R21);
48 	dwarf_regs[22] = REG(R22);
49 	dwarf_regs[23] = REG(R23);
50 	dwarf_regs[24] = REG(R24);
51 	dwarf_regs[25] = REG(R25);
52 	dwarf_regs[26] = REG(R26);
53 	dwarf_regs[27] = REG(R27);
54 	dwarf_regs[28] = REG(R28);
55 	dwarf_regs[29] = REG(R29);
56 	dwarf_regs[30] = REG(R30);
57 	dwarf_regs[31] = REG(R31);
58 	if (!dwfl_thread_state_registers(thread, 0, 32, dwarf_regs))
59 		return false;
60 
61 	dwarf_nip = REG(NIP);
62 	dwfl_thread_state_register_pc(thread, dwarf_nip);
63 	for (i = 0; i < ARRAY_SIZE(special_regs); i++) {
64 		Dwarf_Word val = 0;
65 		perf_reg_value(&val, user_regs, special_regs[i][1]);
66 		if (!dwfl_thread_state_registers(thread,
67 						 special_regs[i][0], 1,
68 						 &val))
69 			return false;
70 	}
71 
72 	return true;
73 }
74