1 /* GNU/Linux S/390 specific low level interface, for the in-process
4 Copyright (C) 2016-2023 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
23 #include "tracepoint.h"
24 #include "linux-s390-tdesc.h"
30 #define FT_FPR(x) (0x000 + (x) * 0x10)
31 #define FT_VR(x) (0x000 + (x) * 0x10)
32 #define FT_VR_L(x) (0x008 + (x) * 0x10)
33 #define FT_GPR(x) (0x200 + (x) * 8)
34 #define FT_GPR_U(x) (0x200 + (x) * 8)
35 #define FT_GPR_L(x) (0x204 + (x) * 8)
36 #define FT_GPR(x) (0x200 + (x) * 8)
37 #define FT_ACR(x) (0x280 + (x) * 4)
39 #define FT_PSWM_U 0x2c0
41 #define FT_PSWA_L 0x2cc
44 /* Mappings between registers collected by the jump pad and GDB's register
45 array layout used by regcache.
47 See linux-s390-low.c (s390_install_fast_tracepoint_jump_pad) for more
52 /* Used for s390-linux32, s390-linux32v1, s390-linux32v2. */
54 static const int s390_linux32_ft_collect_regmap
[] = {
55 /* 32-bit PSWA and PSWM. */
57 /* 32-bit GPRs (mapped to lower halves of 64-bit slots). */
58 FT_GPR_L (0), FT_GPR_L (1), FT_GPR_L (2), FT_GPR_L (3),
59 FT_GPR_L (4), FT_GPR_L (5), FT_GPR_L (6), FT_GPR_L (7),
60 FT_GPR_L (8), FT_GPR_L (9), FT_GPR_L (10), FT_GPR_L (11),
61 FT_GPR_L (12), FT_GPR_L (13), FT_GPR_L (14), FT_GPR_L (15),
63 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
64 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
65 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
66 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
67 /* FPRs (mapped to upper halves of 128-bit VR slots). */
68 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
69 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
70 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
71 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
72 /* orig_r2, last_break, system_call */
76 /* Used for s390-linux64, s390-linux64v1, s390-linux64v2, s390-vx-linux64. */
78 static const int s390_linux64_ft_collect_regmap
[] = {
79 /* 32-bit PSWA and PSWM. */
81 /* 32-bit halves of 64-bit GPRs. */
82 FT_GPR_U (0), FT_GPR_L (0),
83 FT_GPR_U (1), FT_GPR_L (1),
84 FT_GPR_U (2), FT_GPR_L (2),
85 FT_GPR_U (3), FT_GPR_L (3),
86 FT_GPR_U (4), FT_GPR_L (4),
87 FT_GPR_U (5), FT_GPR_L (5),
88 FT_GPR_U (6), FT_GPR_L (6),
89 FT_GPR_U (7), FT_GPR_L (7),
90 FT_GPR_U (8), FT_GPR_L (8),
91 FT_GPR_U (9), FT_GPR_L (9),
92 FT_GPR_U (10), FT_GPR_L (10),
93 FT_GPR_U (11), FT_GPR_L (11),
94 FT_GPR_U (12), FT_GPR_L (12),
95 FT_GPR_U (13), FT_GPR_L (13),
96 FT_GPR_U (14), FT_GPR_L (14),
97 FT_GPR_U (15), FT_GPR_L (15),
99 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
100 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
101 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
102 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
103 /* FPRs (mapped to upper halves of 128-bit VR slots). */
104 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
105 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
106 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
107 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
108 /* orig_r2, last_break, system_call */
110 /* Lower halves of 128-bit VRs. */
111 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
112 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
113 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
114 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
115 /* And the next 16 VRs. */
116 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
117 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
118 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
119 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
122 /* Used for s390-te-linux64, s390-tevx-linux64, and s390-gs-linux64. */
124 static const int s390_te_linux64_ft_collect_regmap
[] = {
125 /* 32-bit PSWA and PSWM. */
126 FT_PSWM_U
, FT_PSWA_L
,
127 /* 32-bit halves of 64-bit GPRs. */
128 FT_GPR_U (0), FT_GPR_L (0),
129 FT_GPR_U (1), FT_GPR_L (1),
130 FT_GPR_U (2), FT_GPR_L (2),
131 FT_GPR_U (3), FT_GPR_L (3),
132 FT_GPR_U (4), FT_GPR_L (4),
133 FT_GPR_U (5), FT_GPR_L (5),
134 FT_GPR_U (6), FT_GPR_L (6),
135 FT_GPR_U (7), FT_GPR_L (7),
136 FT_GPR_U (8), FT_GPR_L (8),
137 FT_GPR_U (9), FT_GPR_L (9),
138 FT_GPR_U (10), FT_GPR_L (10),
139 FT_GPR_U (11), FT_GPR_L (11),
140 FT_GPR_U (12), FT_GPR_L (12),
141 FT_GPR_U (13), FT_GPR_L (13),
142 FT_GPR_U (14), FT_GPR_L (14),
143 FT_GPR_U (15), FT_GPR_L (15),
145 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
146 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
147 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
148 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
149 /* FPRs (mapped to upper halves of 128-bit VR slots). */
150 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
151 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
152 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
153 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
154 /* orig_r2, last_break, system_call */
162 /* Lower halves of 128-bit VRs. */
163 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
164 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
165 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
166 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
167 /* And the next 16 VRs. */
168 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
169 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
170 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
171 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
174 #else /* __s390x__ */
176 /* Used for s390x-linux64, s390x-linux64v1, s390x-linux64v2, s390x-vx-linux64. */
178 static const int s390x_ft_collect_regmap
[] = {
179 /* 64-bit PSWA and PSWM. */
182 FT_GPR (0), FT_GPR (1), FT_GPR (2), FT_GPR (3),
183 FT_GPR (4), FT_GPR (5), FT_GPR (6), FT_GPR (7),
184 FT_GPR (8), FT_GPR (9), FT_GPR (10), FT_GPR (11),
185 FT_GPR (12), FT_GPR (13), FT_GPR (14), FT_GPR (15),
187 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
188 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
189 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
190 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
191 /* FPRs (mapped to upper halves of 128-bit VR slots). */
192 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
193 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
194 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
195 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
196 /* orig_r2, last_break, system_call */
198 /* Lower halves of 128-bit VRs. */
199 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
200 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
201 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
202 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
203 /* And the next 16 VRs. */
204 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
205 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
206 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
207 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
210 /* Used for s390x-te-linux64, s390x-tevx-linux64, and
213 static const int s390x_te_ft_collect_regmap
[] = {
214 /* 64-bit PSWA and PSWM. */
217 FT_GPR (0), FT_GPR (1), FT_GPR (2), FT_GPR (3),
218 FT_GPR (4), FT_GPR (5), FT_GPR (6), FT_GPR (7),
219 FT_GPR (8), FT_GPR (9), FT_GPR (10), FT_GPR (11),
220 FT_GPR (12), FT_GPR (13), FT_GPR (14), FT_GPR (15),
222 FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
223 FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
224 FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
225 FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
226 /* FPRs (mapped to upper halves of 128-bit VR slots). */
227 FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
228 FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
229 FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
230 FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
231 /* orig_r2, last_break, system_call */
239 /* Lower halves of 128-bit VRs. */
240 FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
241 FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
242 FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
243 FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
244 /* And the next 16 VRs. */
245 FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
246 FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
247 FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
248 FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
253 /* Initialized by get_ipa_tdesc according to the tdesc in use. */
255 static const int *s390_regmap
;
256 static int s390_regnum
;
258 /* Fill in REGCACHE with registers saved by the jump pad in BUF. */
261 supply_fast_tracepoint_registers (struct regcache
*regcache
,
262 const unsigned char *buf
)
265 for (i
= 0; i
< s390_regnum
; i
++)
266 if (s390_regmap
[i
] != -1)
267 supply_register (regcache
, i
, ((char *) buf
) + s390_regmap
[i
]);
271 get_raw_reg (const unsigned char *raw_regs
, int regnum
)
274 if (regnum
>= s390_regnum
)
276 offset
= s390_regmap
[regnum
];
280 /* The regnums are variable, better to figure out size by FT offset. */
283 if (offset
< FT_VR(16)
285 || (offset
>= FT_GPR(0) && offset
< FT_ACR(0))
290 return *(uint64_t *) (raw_regs
+ offset
);
292 if (offset
>= FT_ACR(0 && offset
< FT_PSWM
)
295 || (offset
>= FT_GPR(0) && offset
< FT_ACR(0))
296 || offset
== FT_PSWM_U
297 || offset
== FT_PSWA_L
300 return *(uint32_t *) (raw_regs
+ offset
);
302 /* This leaves 128-bit VX. No way to return them. */
306 /* Return target_desc to use for IPA, given the tdesc index passed by
307 gdbserver. For s390, it also sets s390_regmap and s390_regnum. */
309 const struct target_desc
*
310 get_ipa_tdesc (int idx
)
312 #define SET_REGMAP(regmap, skip_last) \
314 s390_regmap = regmap; \
315 s390_regnum = (sizeof regmap / sizeof regmap[0]) - skip_last; \
321 /* Subtract number of VX regs. */
322 SET_REGMAP(s390x_ft_collect_regmap
, 32);
323 return tdesc_s390x_linux64
;
324 case S390_TDESC_64V1
:
325 SET_REGMAP(s390x_ft_collect_regmap
, 32);
326 return tdesc_s390x_linux64v1
;
327 case S390_TDESC_64V2
:
328 SET_REGMAP(s390x_ft_collect_regmap
, 32);
329 return tdesc_s390x_linux64v2
;
331 SET_REGMAP(s390x_te_ft_collect_regmap
, 32);
332 return tdesc_s390x_te_linux64
;
334 SET_REGMAP(s390x_ft_collect_regmap
, 0);
335 return tdesc_s390x_vx_linux64
;
336 case S390_TDESC_TEVX
:
337 SET_REGMAP(s390x_te_ft_collect_regmap
, 0);
338 return tdesc_s390x_tevx_linux64
;
340 SET_REGMAP(s390x_te_ft_collect_regmap
, 0);
341 return tdesc_s390x_gs_linux64
;
344 SET_REGMAP(s390_linux32_ft_collect_regmap
, 0);
345 return tdesc_s390_linux32
;
346 case S390_TDESC_32V1
:
347 SET_REGMAP(s390_linux32_ft_collect_regmap
, 0);
348 return tdesc_s390_linux32v1
;
349 case S390_TDESC_32V2
:
350 SET_REGMAP(s390_linux32_ft_collect_regmap
, 0);
351 return tdesc_s390_linux32v2
;
353 SET_REGMAP(s390_linux64_ft_collect_regmap
, 32);
354 return tdesc_s390_linux64
;
355 case S390_TDESC_64V1
:
356 SET_REGMAP(s390_linux64_ft_collect_regmap
, 32);
357 return tdesc_s390_linux64v1
;
358 case S390_TDESC_64V2
:
359 SET_REGMAP(s390_linux64_ft_collect_regmap
, 32);
360 return tdesc_s390_linux64v2
;
362 SET_REGMAP(s390_te_linux64_ft_collect_regmap
, 32);
363 return tdesc_s390_te_linux64
;
365 SET_REGMAP(s390_linux64_ft_collect_regmap
, 0);
366 return tdesc_s390_vx_linux64
;
367 case S390_TDESC_TEVX
:
368 SET_REGMAP(s390_te_linux64_ft_collect_regmap
, 0);
369 return tdesc_s390_tevx_linux64
;
371 SET_REGMAP(s390_te_linux64_ft_collect_regmap
, 0);
372 return tdesc_s390_gs_linux64
;
375 internal_error ("unknown ipa tdesc index: %d", idx
);
377 return tdesc_s390x_linux64
;
379 return tdesc_s390_linux32
;
384 /* Allocate buffer for the jump pads. On 31-bit, JG reaches everywhere,
385 so just allocate normally. On 64-bit, we have +/-4GiB of reach, and
386 the executable is usually mapped at 0x80000000 - aim for somewhere
390 alloc_jump_pad_buffer (size_t size
)
394 uintptr_t exec_base
= getauxval (AT_PHDR
);
399 exec_base
= 0x80000000;
401 pagesize
= sysconf (_SC_PAGE_SIZE
);
403 perror_with_name ("sysconf");
405 addr
= exec_base
- size
;
407 /* size should already be page-aligned, but this can't hurt. */
408 addr
&= ~(pagesize
- 1);
410 /* Search for a free area. If we hit 0, we're out of luck. */
411 for (; addr
; addr
-= pagesize
)
413 /* No MAP_FIXED - we don't want to zap someone's mapping. */
414 res
= mmap ((void *) addr
, size
,
415 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
416 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
418 /* If we got what we wanted, return. */
419 if ((uintptr_t) res
== addr
)
422 /* If we got a mapping, but at a wrong address, undo it. */
423 if (res
!= MAP_FAILED
)
429 void *res
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
430 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
432 if (res
== MAP_FAILED
)
440 initialize_low_tracepoint (void)
443 init_registers_s390x_linux64 ();
444 init_registers_s390x_linux64v1 ();
445 init_registers_s390x_linux64v2 ();
446 init_registers_s390x_te_linux64 ();
447 init_registers_s390x_vx_linux64 ();
448 init_registers_s390x_tevx_linux64 ();
449 init_registers_s390x_gs_linux64 ();
451 init_registers_s390_linux32 ();
452 init_registers_s390_linux32v1 ();
453 init_registers_s390_linux32v2 ();
454 init_registers_s390_linux64 ();
455 init_registers_s390_linux64v1 ();
456 init_registers_s390_linux64v2 ();
457 init_registers_s390_te_linux64 ();
458 init_registers_s390_vx_linux64 ();
459 init_registers_s390_tevx_linux64 ();
460 init_registers_s390_gs_linux64 ();