IPA: Add alloc_jump_pad_buffer target hook.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-s390-ipa.c
CommitLineData
abd9baf9
MK
1/* GNU/Linux S/390 specific low level interface, for the in-process
2 agent library for GDB.
3
4 Copyright (C) 2016 Free Software Foundation, Inc.
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>
abd9baf9
MK
23#include "tracepoint.h"
24#include "linux-s390-tdesc.h"
a13c4696
MK
25#ifdef HAVE_GETAUXVAL
26#include <sys/auxv.h>
27#endif
abd9baf9
MK
28
29#define FT_FPR(x) (0x000 + (x) * 0x10)
30#define FT_VR(x) (0x000 + (x) * 0x10)
31#define FT_VR_L(x) (0x008 + (x) * 0x10)
32#define FT_GPR(x) (0x200 + (x) * 8)
33#define FT_GPR_U(x) (0x200 + (x) * 8)
34#define FT_GPR_L(x) (0x204 + (x) * 8)
35#define FT_GPR(x) (0x200 + (x) * 8)
36#define FT_ACR(x) (0x280 + (x) * 4)
37#define FT_PSWM 0x2c0
38#define FT_PSWM_U 0x2c0
39#define FT_PSWA 0x2c8
40#define FT_PSWA_L 0x2cc
41#define FT_FPC 0x2d0
42
43/* Mappings between registers collected by the jump pad and GDB's register
44 array layout used by regcache.
45
46 See linux-s390-low.c (s390_install_fast_tracepoint_jump_pad) for more
47 details. */
48
49#ifndef __s390x__
50
51/* Used for s390-linux32, s390-linux32v1, s390-linux32v2. */
52
53static const int s390_linux32_ft_collect_regmap[] = {
54 /* 32-bit PSWA and PSWM. */
55 FT_PSWM_U, FT_PSWA_L,
56 /* 32-bit GPRs (mapped to lower halves of 64-bit slots). */
57 FT_GPR_L (0), FT_GPR_L (1), FT_GPR_L (2), FT_GPR_L (3),
58 FT_GPR_L (4), FT_GPR_L (5), FT_GPR_L (6), FT_GPR_L (7),
59 FT_GPR_L (8), FT_GPR_L (9), FT_GPR_L (10), FT_GPR_L (11),
60 FT_GPR_L (12), FT_GPR_L (13), FT_GPR_L (14), FT_GPR_L (15),
61 /* ACRs */
62 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
63 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
64 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
65 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
66 /* FPRs (mapped to upper halves of 128-bit VR slots). */
67 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
68 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
69 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
70 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
71 /* orig_r2, last_break, system_call */
72 -1, -1, -1,
73};
74
75/* Used for s390-linux64, s390-linux64v1, s390-linux64v2, s390-vx-linux64. */
76
77static const int s390_linux64_ft_collect_regmap[] = {
78 /* 32-bit PSWA and PSWM. */
79 FT_PSWM_U, FT_PSWA_L,
80 /* 32-bit halves of 64-bit GPRs. */
81 FT_GPR_U (0), FT_GPR_L (0),
82 FT_GPR_U (1), FT_GPR_L (1),
83 FT_GPR_U (2), FT_GPR_L (2),
84 FT_GPR_U (3), FT_GPR_L (3),
85 FT_GPR_U (4), FT_GPR_L (4),
86 FT_GPR_U (5), FT_GPR_L (5),
87 FT_GPR_U (6), FT_GPR_L (6),
88 FT_GPR_U (7), FT_GPR_L (7),
89 FT_GPR_U (8), FT_GPR_L (8),
90 FT_GPR_U (9), FT_GPR_L (9),
91 FT_GPR_U (10), FT_GPR_L (10),
92 FT_GPR_U (11), FT_GPR_L (11),
93 FT_GPR_U (12), FT_GPR_L (12),
94 FT_GPR_U (13), FT_GPR_L (13),
95 FT_GPR_U (14), FT_GPR_L (14),
96 FT_GPR_U (15), FT_GPR_L (15),
97 /* ACRs */
98 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
99 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
100 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
101 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
102 /* FPRs (mapped to upper halves of 128-bit VR slots). */
103 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
104 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
105 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
106 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
107 /* orig_r2, last_break, system_call */
108 -1, -1, -1,
109 /* Lower halves of 128-bit VRs. */
110 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
111 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
112 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
113 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
114 /* And the next 16 VRs. */
115 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
116 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
117 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
118 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
119};
120
121/* Used for s390-te-linux64, s390-tevx-linux64. */
122
123static const int s390_te_linux64_ft_collect_regmap[] = {
124 /* 32-bit PSWA and PSWM. */
125 FT_PSWM_U, FT_PSWA_L,
126 /* 32-bit halves of 64-bit GPRs. */
127 FT_GPR_U (0), FT_GPR_L (0),
128 FT_GPR_U (1), FT_GPR_L (1),
129 FT_GPR_U (2), FT_GPR_L (2),
130 FT_GPR_U (3), FT_GPR_L (3),
131 FT_GPR_U (4), FT_GPR_L (4),
132 FT_GPR_U (5), FT_GPR_L (5),
133 FT_GPR_U (6), FT_GPR_L (6),
134 FT_GPR_U (7), FT_GPR_L (7),
135 FT_GPR_U (8), FT_GPR_L (8),
136 FT_GPR_U (9), FT_GPR_L (9),
137 FT_GPR_U (10), FT_GPR_L (10),
138 FT_GPR_U (11), FT_GPR_L (11),
139 FT_GPR_U (12), FT_GPR_L (12),
140 FT_GPR_U (13), FT_GPR_L (13),
141 FT_GPR_U (14), FT_GPR_L (14),
142 FT_GPR_U (15), FT_GPR_L (15),
143 /* ACRs */
144 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
145 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
146 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
147 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
148 /* FPRs (mapped to upper halves of 128-bit VR slots). */
149 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
150 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
151 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
152 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
153 /* orig_r2, last_break, system_call */
154 -1, -1, -1,
155 /* TDB */
156 -1, -1, -1, -1,
157 -1, -1, -1, -1,
158 -1, -1, -1, -1,
159 -1, -1, -1, -1,
160 -1, -1, -1, -1,
161 /* Lower halves of 128-bit VRs. */
162 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
163 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
164 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
165 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
166 /* And the next 16 VRs. */
167 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
168 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
169 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
170 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
171};
172
173#else /* __s390x__ */
174
175/* Used for s390x-linux64, s390x-linux64v1, s390x-linux64v2, s390x-vx-linux64. */
176
177static const int s390x_ft_collect_regmap[] = {
178 /* 64-bit PSWA and PSWM. */
179 FT_PSWM, FT_PSWA,
180 /* 64-bit GPRs. */
181 FT_GPR (0), FT_GPR (1), FT_GPR (2), FT_GPR (3),
182 FT_GPR (4), FT_GPR (5), FT_GPR (6), FT_GPR (7),
183 FT_GPR (8), FT_GPR (9), FT_GPR (10), FT_GPR (11),
184 FT_GPR (12), FT_GPR (13), FT_GPR (14), FT_GPR (15),
185 /* ACRs */
186 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
187 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
188 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
189 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
190 /* FPRs (mapped to upper halves of 128-bit VR slots). */
191 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
192 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
193 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
194 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
195 /* orig_r2, last_break, system_call */
196 -1, -1, -1,
197 /* Lower halves of 128-bit VRs. */
198 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
199 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
200 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
201 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
202 /* And the next 16 VRs. */
203 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
204 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
205 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
206 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
207};
208
209/* Used for s390x-te-linux64, s390x-tevx-linux64. */
210
211static const int s390x_te_ft_collect_regmap[] = {
212 /* 64-bit PSWA and PSWM. */
213 FT_PSWM, FT_PSWA,
214 /* 64-bit GPRs. */
215 FT_GPR (0), FT_GPR (1), FT_GPR (2), FT_GPR (3),
216 FT_GPR (4), FT_GPR (5), FT_GPR (6), FT_GPR (7),
217 FT_GPR (8), FT_GPR (9), FT_GPR (10), FT_GPR (11),
218 FT_GPR (12), FT_GPR (13), FT_GPR (14), FT_GPR (15),
219 /* ACRs */
220 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
221 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
222 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
223 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
224 /* FPRs (mapped to upper halves of 128-bit VR slots). */
225 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
226 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
227 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
228 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
229 /* orig_r2, last_break, system_call */
230 -1, -1, -1,
231 /* TDB */
232 -1, -1, -1, -1,
233 -1, -1, -1, -1,
234 -1, -1, -1, -1,
235 -1, -1, -1, -1,
236 -1, -1, -1, -1,
237 /* Lower halves of 128-bit VRs. */
238 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
239 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
240 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
241 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
242 /* And the next 16 VRs. */
243 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
244 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
245 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
246 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
247};
248
249#endif
250
251/* Initialized by get_ipa_tdesc according to the tdesc in use. */
252
253static const int *s390_regmap;
254static int s390_regnum;
255
256/* Fill in REGCACHE with registers saved by the jump pad in BUF. */
257
258void
259supply_fast_tracepoint_registers (struct regcache *regcache,
260 const unsigned char *buf)
261{
262 int i;
263 for (i = 0; i < s390_regnum; i++)
264 if (s390_regmap[i] != -1)
265 supply_register (regcache, i, ((char *) buf) + s390_regmap[i]);
266}
267
1cda1512
MK
268ULONGEST
269get_raw_reg (const unsigned char *raw_regs, int regnum)
abd9baf9
MK
270{
271 int offset;
272 if (regnum >= s390_regnum)
273 return 0;
274 offset = s390_regmap[regnum];
275 if (offset == -1)
276 return 0;
277
278 /* The regnums are variable, better to figure out size by FT offset. */
279
280 /* 64-bit ones. */
281 if (offset < FT_VR(16)
282#ifdef __s390x__
283 || (offset >= FT_GPR(0) && offset < FT_ACR(0))
284 || offset == FT_PSWM
285 || offset == FT_PSWA
286#endif
287 )
288 return *(uint64_t *) (raw_regs + offset);
289
290 if (offset >= FT_ACR(0 && offset < FT_PSWM)
291 || offset == FT_FPC
292#ifndef __s390x__
293 || (offset >= FT_GPR(0) && offset < FT_ACR(0))
294 || offset == FT_PSWM_U
295 || offset == FT_PSWA_L
296#endif
297 )
298 return *(uint32_t *) (raw_regs + offset);
299
300 /* This leaves 128-bit VX. No way to return them. */
301 return 0;
302}
303
304/* Return target_desc to use for IPA, given the tdesc index passed by
305 gdbserver. For s390, it also sets s390_regmap and s390_regnum. */
306
307const struct target_desc *
308get_ipa_tdesc (int idx)
309{
310#define SET_REGMAP(regmap, skip_last) \
311 do { \
312 s390_regmap = regmap; \
313 s390_regnum = (sizeof regmap / sizeof regmap[0]) - skip_last; \
314 } while(0)
315 switch (idx)
316 {
317#ifdef __s390x__
318 case S390_TDESC_64:
319 /* Subtract number of VX regs. */
320 SET_REGMAP(s390x_ft_collect_regmap, 32);
321 return tdesc_s390x_linux64;
322 case S390_TDESC_64V1:
323 SET_REGMAP(s390x_ft_collect_regmap, 32);
324 return tdesc_s390x_linux64v1;
325 case S390_TDESC_64V2:
326 SET_REGMAP(s390x_ft_collect_regmap, 32);
327 return tdesc_s390x_linux64v2;
328 case S390_TDESC_TE:
329 SET_REGMAP(s390x_te_ft_collect_regmap, 32);
330 return tdesc_s390x_te_linux64;
331 case S390_TDESC_VX:
332 SET_REGMAP(s390x_ft_collect_regmap, 0);
333 return tdesc_s390x_vx_linux64;
334 case S390_TDESC_TEVX:
335 SET_REGMAP(s390x_te_ft_collect_regmap, 0);
336 return tdesc_s390x_tevx_linux64;
337#else
338 case S390_TDESC_32:
339 SET_REGMAP(s390_linux32_ft_collect_regmap, 0);
340 return tdesc_s390_linux32;
341 case S390_TDESC_32V1:
342 SET_REGMAP(s390_linux32_ft_collect_regmap, 0);
343 return tdesc_s390_linux32v1;
344 case S390_TDESC_32V2:
345 SET_REGMAP(s390_linux32_ft_collect_regmap, 0);
346 return tdesc_s390_linux32v2;
347 case S390_TDESC_64:
348 SET_REGMAP(s390_linux64_ft_collect_regmap, 32);
349 return tdesc_s390_linux64;
350 case S390_TDESC_64V1:
351 SET_REGMAP(s390_linux64_ft_collect_regmap, 32);
352 return tdesc_s390_linux64v1;
353 case S390_TDESC_64V2:
354 SET_REGMAP(s390_linux64_ft_collect_regmap, 32);
355 return tdesc_s390_linux64v2;
356 case S390_TDESC_TE:
357 SET_REGMAP(s390_te_linux64_ft_collect_regmap, 32);
358 return tdesc_s390_te_linux64;
359 case S390_TDESC_VX:
360 SET_REGMAP(s390_linux64_ft_collect_regmap, 0);
361 return tdesc_s390_vx_linux64;
362 case S390_TDESC_TEVX:
363 SET_REGMAP(s390_te_linux64_ft_collect_regmap, 0);
364 return tdesc_s390_tevx_linux64;
365#endif
366 default:
367 internal_error (__FILE__, __LINE__,
368 "unknown ipa tdesc index: %d", idx);
369#ifdef __s390x__
370 return tdesc_s390x_linux64;
371#else
372 return tdesc_s390_linux32;
373#endif
374 }
375}
376
a13c4696
MK
377/* Allocate buffer for the jump pads. On 31-bit, JG reaches everywhere,
378 so just allocate normally. On 64-bit, we have +/-4GiB of reach, and
379 the executable is usually mapped at 0x80000000 - aim for somewhere
380 below it. */
381
382void *
383alloc_jump_pad_buffer (size_t size)
384{
385#ifdef __s390x__
386 uintptr_t addr;
387 uintptr_t exec_base = getauxval (AT_PHDR);
388 int pagesize;
389 void *res;
390
391 if (exec_base == 0)
392 exec_base = 0x80000000;
393
394 pagesize = sysconf (_SC_PAGE_SIZE);
395 if (pagesize == -1)
396 perror_with_name ("sysconf");
397
398 addr = exec_base - size;
399
400 /* size should already be page-aligned, but this can't hurt. */
401 addr &= ~(pagesize - 1);
402
403 /* Search for a free area. If we hit 0, we're out of luck. */
404 for (; addr; addr -= pagesize)
405 {
406 /* No MAP_FIXED - we don't want to zap someone's mapping. */
407 res = mmap ((void *) addr, size,
408 PROT_READ | PROT_WRITE | PROT_EXEC,
409 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
410
411 /* If we got what we wanted, return. */
412 if ((uintptr_t) res == addr)
413 return res;
414
415 /* If we got a mapping, but at a wrong address, undo it. */
416 if (res != MAP_FAILED)
417 munmap (res, size);
418 }
419
420 return NULL;
421#else
422 void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
423 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
424
425 if (res == MAP_FAILED)
426 return NULL;
427
428 return res;
429#endif
430}
431
abd9baf9
MK
432void
433initialize_low_tracepoint (void)
434{
435#ifdef __s390x__
436 init_registers_s390x_linux64 ();
437 init_registers_s390x_linux64v1 ();
438 init_registers_s390x_linux64v2 ();
439 init_registers_s390x_te_linux64 ();
440 init_registers_s390x_vx_linux64 ();
441 init_registers_s390x_tevx_linux64 ();
442#else
443 init_registers_s390_linux32 ();
444 init_registers_s390_linux32v1 ();
445 init_registers_s390_linux32v2 ();
446 init_registers_s390_linux64 ();
447 init_registers_s390_linux64v1 ();
448 init_registers_s390_linux64v2 ();
449 init_registers_s390_te_linux64 ();
450 init_registers_s390_vx_linux64 ();
451 init_registers_s390_tevx_linux64 ();
452#endif
453}
This page took 0.065708 seconds and 4 git commands to generate.