Commit | Line | Data |
---|---|---|
bb903df0 PL |
1 | /* GNU/Linux/AArch64 specific low level interface, for the in-process |
2 | agent library for GDB. | |
3 | ||
61baf725 | 4 | Copyright (C) 2015-2017 Free Software Foundation, Inc. |
bb903df0 PL |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "server.h" | |
a13c4696 | 22 | #include <sys/mman.h> |
bb903df0 | 23 | #include "tracepoint.h" |
d0a9981f | 24 | #include <elf.h> |
a13c4696 MK |
25 | #ifdef HAVE_GETAUXVAL |
26 | #include <sys/auxv.h> | |
27 | #endif | |
bb903df0 PL |
28 | |
29 | /* Defined in auto-generated file aarch64.c. */ | |
30 | void init_registers_aarch64 (void); | |
31 | extern const struct target_desc *tdesc_aarch64; | |
32 | ||
33 | /* Each register saved by the jump pad is in a 16 byte cell. */ | |
34 | #define FT_CR_SIZE 16 | |
35 | ||
36 | #define FT_CR_FPCR 0 | |
37 | #define FT_CR_FPSR 1 | |
38 | #define FT_CR_CPSR 2 | |
39 | #define FT_CR_PC 3 | |
40 | #define FT_CR_SP 4 | |
41 | #define FT_CR_X0 5 | |
42 | #define FT_CR_GPR(n) (FT_CR_X0 + (n)) | |
43 | #define FT_CR_FPR(n) (FT_CR_GPR (31) + (n)) | |
44 | ||
45 | /* Mapping between registers collected by the jump pad and GDB's register | |
46 | array layout used by regcache. | |
47 | ||
48 | See linux-aarch64-low.c (aarch64_install_fast_tracepoint_jump_pad) for | |
49 | more details. */ | |
50 | ||
51 | static const int aarch64_ft_collect_regmap[] = { | |
52 | FT_CR_GPR (0), | |
53 | FT_CR_GPR (1), | |
54 | FT_CR_GPR (2), | |
55 | FT_CR_GPR (3), | |
56 | FT_CR_GPR (4), | |
57 | FT_CR_GPR (5), | |
58 | FT_CR_GPR (6), | |
59 | FT_CR_GPR (7), | |
60 | FT_CR_GPR (8), | |
61 | FT_CR_GPR (9), | |
62 | FT_CR_GPR (10), | |
63 | FT_CR_GPR (11), | |
64 | FT_CR_GPR (12), | |
65 | FT_CR_GPR (13), | |
66 | FT_CR_GPR (14), | |
67 | FT_CR_GPR (15), | |
68 | FT_CR_GPR (16), | |
69 | FT_CR_GPR (17), | |
70 | FT_CR_GPR (18), | |
71 | FT_CR_GPR (19), | |
72 | FT_CR_GPR (20), | |
73 | FT_CR_GPR (21), | |
74 | FT_CR_GPR (22), | |
75 | FT_CR_GPR (23), | |
76 | FT_CR_GPR (24), | |
77 | FT_CR_GPR (25), | |
78 | FT_CR_GPR (26), | |
79 | FT_CR_GPR (27), | |
80 | FT_CR_GPR (28), | |
81 | /* FP */ | |
82 | FT_CR_GPR (29), | |
83 | /* LR */ | |
84 | FT_CR_GPR (30), | |
85 | FT_CR_SP, | |
86 | FT_CR_PC, | |
87 | FT_CR_CPSR, | |
88 | FT_CR_FPR (0), | |
89 | FT_CR_FPR (1), | |
90 | FT_CR_FPR (2), | |
91 | FT_CR_FPR (3), | |
92 | FT_CR_FPR (4), | |
93 | FT_CR_FPR (5), | |
94 | FT_CR_FPR (6), | |
95 | FT_CR_FPR (7), | |
96 | FT_CR_FPR (8), | |
97 | FT_CR_FPR (9), | |
98 | FT_CR_FPR (10), | |
99 | FT_CR_FPR (11), | |
100 | FT_CR_FPR (12), | |
101 | FT_CR_FPR (13), | |
102 | FT_CR_FPR (14), | |
103 | FT_CR_FPR (15), | |
104 | FT_CR_FPR (16), | |
105 | FT_CR_FPR (17), | |
106 | FT_CR_FPR (18), | |
107 | FT_CR_FPR (19), | |
108 | FT_CR_FPR (20), | |
109 | FT_CR_FPR (21), | |
110 | FT_CR_FPR (22), | |
111 | FT_CR_FPR (23), | |
112 | FT_CR_FPR (24), | |
113 | FT_CR_FPR (25), | |
114 | FT_CR_FPR (26), | |
115 | FT_CR_FPR (27), | |
116 | FT_CR_FPR (28), | |
117 | FT_CR_FPR (29), | |
118 | FT_CR_FPR (30), | |
119 | FT_CR_FPR (31), | |
120 | FT_CR_FPSR, | |
121 | FT_CR_FPCR | |
122 | }; | |
123 | ||
124 | #define AARCH64_NUM_FT_COLLECT_GREGS \ | |
125 | (sizeof (aarch64_ft_collect_regmap) / sizeof(aarch64_ft_collect_regmap[0])) | |
126 | ||
127 | /* Fill in REGCACHE with registers saved by the jump pad in BUF. */ | |
128 | ||
129 | void | |
130 | supply_fast_tracepoint_registers (struct regcache *regcache, | |
131 | const unsigned char *buf) | |
132 | { | |
133 | int i; | |
134 | ||
135 | for (i = 0; i < AARCH64_NUM_FT_COLLECT_GREGS; i++) | |
136 | supply_register (regcache, i, | |
137 | ((char *) buf) | |
138 | + (aarch64_ft_collect_regmap[i] * FT_CR_SIZE)); | |
139 | } | |
140 | ||
1cda1512 MK |
141 | ULONGEST |
142 | get_raw_reg (const unsigned char *raw_regs, int regnum) | |
bb903df0 PL |
143 | { |
144 | if (regnum >= AARCH64_NUM_FT_COLLECT_GREGS) | |
145 | return 0; | |
146 | ||
147 | return *(ULONGEST *) (raw_regs | |
148 | + aarch64_ft_collect_regmap[regnum] * FT_CR_SIZE); | |
149 | } | |
150 | ||
ae91f625 MK |
151 | /* Return target_desc to use for IPA, given the tdesc index passed by |
152 | gdbserver. Index is ignored, since we have only one tdesc | |
153 | at the moment. */ | |
154 | ||
155 | const struct target_desc * | |
156 | get_ipa_tdesc (int idx) | |
157 | { | |
158 | return tdesc_aarch64; | |
159 | } | |
160 | ||
a13c4696 MK |
161 | /* Allocate buffer for the jump pads. The branch instruction has a reach |
162 | of +/- 128MiB, and the executable is loaded at 0x400000 (4MiB). | |
163 | To maximize the area of executable that can use tracepoints, try | |
164 | allocating at 0x400000 - size initially, decreasing until we hit | |
165 | a free area. */ | |
166 | ||
167 | void * | |
168 | alloc_jump_pad_buffer (size_t size) | |
169 | { | |
170 | uintptr_t addr; | |
171 | uintptr_t exec_base = getauxval (AT_PHDR); | |
172 | int pagesize; | |
173 | void *res; | |
174 | ||
175 | if (exec_base == 0) | |
176 | exec_base = 0x400000; | |
177 | ||
178 | pagesize = sysconf (_SC_PAGE_SIZE); | |
179 | if (pagesize == -1) | |
180 | perror_with_name ("sysconf"); | |
181 | ||
182 | addr = exec_base - size; | |
183 | ||
184 | /* size should already be page-aligned, but this can't hurt. */ | |
185 | addr &= ~(pagesize - 1); | |
186 | ||
187 | /* Search for a free area. If we hit 0, we're out of luck. */ | |
188 | for (; addr; addr -= pagesize) | |
189 | { | |
190 | /* No MAP_FIXED - we don't want to zap someone's mapping. */ | |
191 | res = mmap ((void *) addr, size, | |
192 | PROT_READ | PROT_WRITE | PROT_EXEC, | |
193 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
194 | ||
195 | /* If we got what we wanted, return. */ | |
196 | if ((uintptr_t) res == addr) | |
197 | return res; | |
198 | ||
199 | /* If we got a mapping, but at a wrong address, undo it. */ | |
200 | if (res != MAP_FAILED) | |
201 | munmap (res, size); | |
202 | } | |
203 | ||
204 | return NULL; | |
205 | } | |
206 | ||
bb903df0 PL |
207 | void |
208 | initialize_low_tracepoint (void) | |
209 | { | |
210 | init_registers_aarch64 (); | |
bb903df0 | 211 | } |