Share fork_inferior et al with gdbserver
[external/binutils.git] / gdb / gdbserver / linux-s390-ipa.c
1 /* GNU/Linux S/390 specific low level interface, for the in-process
2    agent library for GDB.
3
4    Copyright (C) 2016-2017 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
54 static 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
78 static 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.  */
123
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),
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
178 static 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.  */
211
212 static const int s390x_te_ft_collect_regmap[] = {
213   /* 64-bit PSWA and PSWM.  */
214   FT_PSWM, FT_PSWA,
215   /* 64-bit GPRs.  */
216   FT_GPR (0), FT_GPR (1), FT_GPR (2), FT_GPR (3),
217   FT_GPR (4), FT_GPR (5), FT_GPR (6), FT_GPR (7),
218   FT_GPR (8), FT_GPR (9), FT_GPR (10), FT_GPR (11),
219   FT_GPR (12), FT_GPR (13), FT_GPR (14), FT_GPR (15),
220   /* ACRs */
221   FT_ACR (0), FT_ACR (1), FT_ACR (2), FT_ACR (3),
222   FT_ACR (4), FT_ACR (5), FT_ACR (6), FT_ACR (7),
223   FT_ACR (8), FT_ACR (9), FT_ACR (10), FT_ACR (11),
224   FT_ACR (12), FT_ACR (13), FT_ACR (14), FT_ACR (15),
225   /* FPRs (mapped to upper halves of 128-bit VR slots).  */
226   FT_FPR (0), FT_FPR (1), FT_FPR (2), FT_FPR (3),
227   FT_FPR (4), FT_FPR (5), FT_FPR (6), FT_FPR (7),
228   FT_FPR (8), FT_FPR (9), FT_FPR (10), FT_FPR (11),
229   FT_FPR (12), FT_FPR (13), FT_FPR (14), FT_FPR (15),
230   /* orig_r2, last_break, system_call */
231   -1, -1, -1,
232   /* TDB */
233   -1, -1, -1, -1,
234   -1, -1, -1, -1,
235   -1, -1, -1, -1,
236   -1, -1, -1, -1,
237   -1, -1, -1, -1,
238   /* Lower halves of 128-bit VRs. */
239   FT_VR_L (0), FT_VR_L (1), FT_VR_L (2), FT_VR_L (3),
240   FT_VR_L (4), FT_VR_L (5), FT_VR_L (6), FT_VR_L (7),
241   FT_VR_L (8), FT_VR_L (9), FT_VR_L (10), FT_VR_L (11),
242   FT_VR_L (12), FT_VR_L (13), FT_VR_L (14), FT_VR_L (15),
243   /* And the next 16 VRs.  */
244   FT_VR (16), FT_VR (17), FT_VR (18), FT_VR (19),
245   FT_VR (20), FT_VR (21), FT_VR (22), FT_VR (23),
246   FT_VR (24), FT_VR (25), FT_VR (26), FT_VR (27),
247   FT_VR (28), FT_VR (29), FT_VR (30), FT_VR (31),
248 };
249
250 #endif
251
252 /* Initialized by get_ipa_tdesc according to the tdesc in use.  */
253
254 static const int *s390_regmap;
255 static int s390_regnum;
256
257 /* Fill in REGCACHE with registers saved by the jump pad in BUF.  */
258
259 void
260 supply_fast_tracepoint_registers (struct regcache *regcache,
261                                   const unsigned char *buf)
262 {
263   int i;
264   for (i = 0; i < s390_regnum; i++)
265     if (s390_regmap[i] != -1)
266       supply_register (regcache, i, ((char *) buf) + s390_regmap[i]);
267 }
268
269 ULONGEST
270 get_raw_reg (const unsigned char *raw_regs, int regnum)
271 {
272   int offset;
273   if (regnum >= s390_regnum)
274     return 0;
275   offset = s390_regmap[regnum];
276   if (offset == -1)
277     return 0;
278
279   /* The regnums are variable, better to figure out size by FT offset.  */
280
281   /* 64-bit ones.  */
282   if (offset < FT_VR(16)
283 #ifdef __s390x__
284       || (offset >= FT_GPR(0) && offset < FT_ACR(0))
285       || offset == FT_PSWM
286       || offset == FT_PSWA
287 #endif
288      )
289     return *(uint64_t *) (raw_regs + offset);
290
291   if (offset >= FT_ACR(0 && offset < FT_PSWM)
292       || offset == FT_FPC
293 #ifndef __s390x__
294       || (offset >= FT_GPR(0) && offset < FT_ACR(0))
295       || offset == FT_PSWM_U
296       || offset == FT_PSWA_L
297 #endif
298      )
299     return *(uint32_t *) (raw_regs + offset);
300
301   /* This leaves 128-bit VX.  No way to return them.  */
302   return 0;
303 }
304
305 /* Return target_desc to use for IPA, given the tdesc index passed by
306    gdbserver.  For s390, it also sets s390_regmap and s390_regnum.  */
307
308 const struct target_desc *
309 get_ipa_tdesc (int idx)
310 {
311 #define SET_REGMAP(regmap, skip_last) \
312         do { \
313           s390_regmap = regmap; \
314           s390_regnum = (sizeof regmap / sizeof regmap[0]) - skip_last; \
315         } while(0)
316   switch (idx)
317     {
318 #ifdef __s390x__
319     case S390_TDESC_64:
320       /* Subtract number of VX regs.  */
321       SET_REGMAP(s390x_ft_collect_regmap, 32);
322       return tdesc_s390x_linux64;
323     case S390_TDESC_64V1:
324       SET_REGMAP(s390x_ft_collect_regmap, 32);
325       return tdesc_s390x_linux64v1;
326     case S390_TDESC_64V2:
327       SET_REGMAP(s390x_ft_collect_regmap, 32);
328       return tdesc_s390x_linux64v2;
329     case S390_TDESC_TE:
330       SET_REGMAP(s390x_te_ft_collect_regmap, 32);
331       return tdesc_s390x_te_linux64;
332     case S390_TDESC_VX:
333       SET_REGMAP(s390x_ft_collect_regmap, 0);
334       return tdesc_s390x_vx_linux64;
335     case S390_TDESC_TEVX:
336       SET_REGMAP(s390x_te_ft_collect_regmap, 0);
337       return tdesc_s390x_tevx_linux64;
338 #else
339     case S390_TDESC_32:
340       SET_REGMAP(s390_linux32_ft_collect_regmap, 0);
341       return tdesc_s390_linux32;
342     case S390_TDESC_32V1:
343       SET_REGMAP(s390_linux32_ft_collect_regmap, 0);
344       return tdesc_s390_linux32v1;
345     case S390_TDESC_32V2:
346       SET_REGMAP(s390_linux32_ft_collect_regmap, 0);
347       return tdesc_s390_linux32v2;
348     case S390_TDESC_64:
349       SET_REGMAP(s390_linux64_ft_collect_regmap, 32);
350       return tdesc_s390_linux64;
351     case S390_TDESC_64V1:
352       SET_REGMAP(s390_linux64_ft_collect_regmap, 32);
353       return tdesc_s390_linux64v1;
354     case S390_TDESC_64V2:
355       SET_REGMAP(s390_linux64_ft_collect_regmap, 32);
356       return tdesc_s390_linux64v2;
357     case S390_TDESC_TE:
358       SET_REGMAP(s390_te_linux64_ft_collect_regmap, 32);
359       return tdesc_s390_te_linux64;
360     case S390_TDESC_VX:
361       SET_REGMAP(s390_linux64_ft_collect_regmap, 0);
362       return tdesc_s390_vx_linux64;
363     case S390_TDESC_TEVX:
364       SET_REGMAP(s390_te_linux64_ft_collect_regmap, 0);
365       return tdesc_s390_tevx_linux64;
366 #endif
367     default:
368       internal_error (__FILE__, __LINE__,
369                       "unknown ipa tdesc index: %d", idx);
370 #ifdef __s390x__
371       return tdesc_s390x_linux64;
372 #else
373       return tdesc_s390_linux32;
374 #endif
375     }
376 }
377
378 /* Allocate buffer for the jump pads.  On 31-bit, JG reaches everywhere,
379    so just allocate normally.  On 64-bit, we have +/-4GiB of reach, and
380    the executable is usually mapped at 0x80000000 - aim for somewhere
381    below it.  */
382
383 void *
384 alloc_jump_pad_buffer (size_t size)
385 {
386 #ifdef __s390x__
387   uintptr_t addr;
388   uintptr_t exec_base = getauxval (AT_PHDR);
389   int pagesize;
390   void *res;
391
392   if (exec_base == 0)
393     exec_base = 0x80000000;
394
395   pagesize = sysconf (_SC_PAGE_SIZE);
396   if (pagesize == -1)
397     perror_with_name ("sysconf");
398
399   addr = exec_base - size;
400
401   /* size should already be page-aligned, but this can't hurt.  */
402   addr &= ~(pagesize - 1);
403
404   /* Search for a free area.  If we hit 0, we're out of luck.  */
405   for (; addr; addr -= pagesize)
406     {
407       /* No MAP_FIXED - we don't want to zap someone's mapping.  */
408       res = mmap ((void *) addr, size,
409                   PROT_READ | PROT_WRITE | PROT_EXEC,
410                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
411
412       /* If we got what we wanted, return.  */
413       if ((uintptr_t) res == addr)
414         return res;
415
416       /* If we got a mapping, but at a wrong address, undo it.  */
417       if (res != MAP_FAILED)
418         munmap (res, size);
419     }
420
421   return NULL;
422 #else
423   void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
424                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
425
426   if (res == MAP_FAILED)
427     return NULL;
428
429   return res;
430 #endif
431 }
432
433 void
434 initialize_low_tracepoint (void)
435 {
436 #ifdef __s390x__
437   init_registers_s390x_linux64 ();
438   init_registers_s390x_linux64v1 ();
439   init_registers_s390x_linux64v2 ();
440   init_registers_s390x_te_linux64 ();
441   init_registers_s390x_vx_linux64 ();
442   init_registers_s390x_tevx_linux64 ();
443 #else
444   init_registers_s390_linux32 ();
445   init_registers_s390_linux32v1 ();
446   init_registers_s390_linux32v2 ();
447   init_registers_s390_linux64 ();
448   init_registers_s390_linux64v1 ();
449   init_registers_s390_linux64v2 ();
450   init_registers_s390_te_linux64 ();
451   init_registers_s390_vx_linux64 ();
452   init_registers_s390_tevx_linux64 ();
453 #endif
454 }