1/* GNU/Linux S/390 specific low level interface, for the in-process 2 agent library for GDB. 3 4 Copyright (C) 2016-2020 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" 22#include <sys/mman.h> 23#include "tracepoint.h" 24#include "linux-s390-tdesc.h" 25#include <elf.h> 26#ifdef HAVE_GETAUXVAL 27#include <sys/auxv.h> 28#endif 29 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) 38#define FT_PSWM 0x2c0 39#define FT_PSWM_U 0x2c0 40#define FT_PSWA 0x2c8 41#define FT_PSWA_L 0x2cc 42#define FT_FPC 0x2d0 43 44/* Mappings between registers collected by the jump pad and GDB's register 45 array layout used by regcache. 46 47 See linux-s390-low.c (s390_install_fast_tracepoint_jump_pad) for more 48 details. */ 49 50#ifndef __s390x__ 51 52/* Used for s390-linux32, s390-linux32v1, s390-linux32v2. */ 53 54static const int s390_linux32_ft_collect_regmap[] = { 55 /* 32-bit PSWA and PSWM. */ 56 FT_PSWM_U, FT_PSWA_L, 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), 62 /* ACRs */ 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 */ 73 -1, -1, -1, 74}; 75 76/* Used for s390-linux64, s390-linux64v1, s390-linux64v2, s390-vx-linux64. */ 77 78static const int s390_linux64_ft_collect_regmap[] = { 79 /* 32-bit PSWA and PSWM. */ 80 FT_PSWM_U, FT_PSWA_L, 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), 98 /* ACRs */ 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 */ 109 -1, -1, -1, 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), 120}; 121 122/* Used for s390-te-linux64, s390-tevx-linux64, and s390-gs-linux64. */ 123 124static 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), 144 /* ACRs */ 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 */ 155 -1, -1, -1, 156 /* TDB */ 157 -1, -1, -1, -1, 158 -1, -1, -1, -1, 159 -1, -1, -1, -1, 160 -1, -1, -1, -1, 161 -1, -1, -1, -1, 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), 172}; 173 174#else /* __s390x__ */ 175 176/* Used for s390x-linux64, s390x-linux64v1, s390x-linux64v2, s390x-vx-linux64. */ 177 178static const int s390x_ft_collect_regmap[] = { 179 /* 64-bit PSWA and PSWM. */ 180 FT_PSWM, FT_PSWA, 181 /* 64-bit GPRs. */ 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), 186 /* ACRs */ 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 */ 197 -1, -1, -1, 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), 208}; 209 210/* Used for s390x-te-linux64, s390x-tevx-linux64, and 211 s390x-gs-linux64. */ 212 213static const int s390x_te_ft_collect_regmap[] = { 214 /* 64-bit PSWA and PSWM. */ 215 FT_PSWM, FT_PSWA, 216 /* 64-bit GPRs. */ 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), 221 /* ACRs */ 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 */ 232 -1, -1, -1, 233 /* TDB */ 234 -1, -1, -1, -1, 235 -1, -1, -1, -1, 236 -1, -1, -1, -1, 237 -1, -1, -1, -1, 238 -1, -1, -1, -1, 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), 249}; 250 251#endif 252 253/* Initialized by get_ipa_tdesc according to the tdesc in use. */ 254 255static const int *s390_regmap; 256static int s390_regnum; 257 258/* Fill in REGCACHE with registers saved by the jump pad in BUF. */ 259 260void 261supply_fast_tracepoint_registers (struct regcache *regcache, 262 const unsigned char *buf) 263{ 264 int i; 265 for (i = 0; i < s390_regnum; i++) 266 if (s390_regmap[i] != -1) 267 supply_register (regcache, i, ((char *) buf) + s390_regmap[i]); 268} 269 270ULONGEST 271get_raw_reg (const unsigned char *raw_regs, int regnum) 272{ 273 int offset; 274 if (regnum >= s390_regnum) 275 return 0; 276 offset = s390_regmap[regnum]; 277 if (offset == -1) 278 return 0; 279 280 /* The regnums are variable, better to figure out size by FT offset. */ 281 282 /* 64-bit ones. */ 283 if (offset < FT_VR(16) 284#ifdef __s390x__ 285 || (offset >= FT_GPR(0) && offset < FT_ACR(0)) 286 || offset == FT_PSWM 287 || offset == FT_PSWA 288#endif 289 ) 290 return *(uint64_t *) (raw_regs + offset); 291 292 if (offset >= FT_ACR(0 && offset < FT_PSWM) 293 || offset == FT_FPC 294#ifndef __s390x__ 295 || (offset >= FT_GPR(0) && offset < FT_ACR(0)) 296 || offset == FT_PSWM_U 297 || offset == FT_PSWA_L 298#endif 299 ) 300 return *(uint32_t *) (raw_regs + offset); 301 302 /* This leaves 128-bit VX. No way to return them. */ 303 return 0; 304} 305 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. */ 308 309const struct target_desc * 310get_ipa_tdesc (int idx) 311{ 312#define SET_REGMAP(regmap, skip_last) \ 313 do { \ 314 s390_regmap = regmap; \ 315 s390_regnum = (sizeof regmap / sizeof regmap[0]) - skip_last; \ 316 } while(0) 317 switch (idx) 318 { 319#ifdef __s390x__ 320 case S390_TDESC_64: 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; 330 case S390_TDESC_TE: 331 SET_REGMAP(s390x_te_ft_collect_regmap, 32); 332 return tdesc_s390x_te_linux64; 333 case S390_TDESC_VX: 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; 339 case S390_TDESC_GS: 340 SET_REGMAP(s390x_te_ft_collect_regmap, 0); 341 return tdesc_s390x_gs_linux64; 342#else 343 case S390_TDESC_32: 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; 352 case S390_TDESC_64: 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; 361 case S390_TDESC_TE: 362 SET_REGMAP(s390_te_linux64_ft_collect_regmap, 32); 363 return tdesc_s390_te_linux64; 364 case S390_TDESC_VX: 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; 370 case S390_TDESC_GS: 371 SET_REGMAP(s390_te_linux64_ft_collect_regmap, 0); 372 return tdesc_s390_gs_linux64; 373#endif 374 default: 375 internal_error (__FILE__, __LINE__, 376 "unknown ipa tdesc index: %d", idx); 377#ifdef __s390x__ 378 return tdesc_s390x_linux64; 379#else 380 return tdesc_s390_linux32; 381#endif 382 } 383} 384 385/* Allocate buffer for the jump pads. On 31-bit, JG reaches everywhere, 386 so just allocate normally. On 64-bit, we have +/-4GiB of reach, and 387 the executable is usually mapped at 0x80000000 - aim for somewhere 388 below it. */ 389 390void * 391alloc_jump_pad_buffer (size_t size) 392{ 393#ifdef __s390x__ 394 uintptr_t addr; 395 uintptr_t exec_base = getauxval (AT_PHDR); 396 int pagesize; 397 void *res; 398 399 if (exec_base == 0) 400 exec_base = 0x80000000; 401 402 pagesize = sysconf (_SC_PAGE_SIZE); 403 if (pagesize == -1) 404 perror_with_name ("sysconf"); 405 406 addr = exec_base - size; 407 408 /* size should already be page-aligned, but this can't hurt. */ 409 addr &= ~(pagesize - 1); 410 411 /* Search for a free area. If we hit 0, we're out of luck. */ 412 for (; addr; addr -= pagesize) 413 { 414 /* No MAP_FIXED - we don't want to zap someone's mapping. */ 415 res = mmap ((void *) addr, size, 416 PROT_READ | PROT_WRITE | PROT_EXEC, 417 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 418 419 /* If we got what we wanted, return. */ 420 if ((uintptr_t) res == addr) 421 return res; 422 423 /* If we got a mapping, but at a wrong address, undo it. */ 424 if (res != MAP_FAILED) 425 munmap (res, size); 426 } 427 428 return NULL; 429#else 430 void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, 431 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 432 433 if (res == MAP_FAILED) 434 return NULL; 435 436 return res; 437#endif 438} 439 440void 441initialize_low_tracepoint (void) 442{ 443#ifdef __s390x__ 444 init_registers_s390x_linux64 (); 445 init_registers_s390x_linux64v1 (); 446 init_registers_s390x_linux64v2 (); 447 init_registers_s390x_te_linux64 (); 448 init_registers_s390x_vx_linux64 (); 449 init_registers_s390x_tevx_linux64 (); 450 init_registers_s390x_gs_linux64 (); 451#else 452 init_registers_s390_linux32 (); 453 init_registers_s390_linux32v1 (); 454 init_registers_s390_linux32v2 (); 455 init_registers_s390_linux64 (); 456 init_registers_s390_linux64v1 (); 457 init_registers_s390_linux64v2 (); 458 init_registers_s390_te_linux64 (); 459 init_registers_s390_vx_linux64 (); 460 init_registers_s390_tevx_linux64 (); 461 init_registers_s390_gs_linux64 (); 462#endif 463} 464