8489c6e2abb2bea611752fbaa4952cdc02ed7444
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-ppc-ipa.c
1 /* GNU/Linux/PowerPC 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"
22 #include <sys/mman.h>
23 #include "tracepoint.h"
24 #include "linux-ppc-tdesc.h"
25 #ifdef HAVE_GETAUXVAL
26 #include <sys/auxv.h>
27 #endif
28
29 /* These macros define the position of registers in the buffer collected
30 by the fast tracepoint jump pad. */
31 #define FT_CR_R0 0
32 #define FT_CR_CR 32
33 #define FT_CR_XER 33
34 #define FT_CR_LR 34
35 #define FT_CR_CTR 35
36 #define FT_CR_PC 36
37 #define FT_CR_GPR(n) (FT_CR_R0 + (n))
38
39 static const int ppc_ft_collect_regmap[] = {
40 /* GPRs */
41 FT_CR_GPR (0), FT_CR_GPR (1), FT_CR_GPR (2),
42 FT_CR_GPR (3), FT_CR_GPR (4), FT_CR_GPR (5),
43 FT_CR_GPR (6), FT_CR_GPR (7), FT_CR_GPR (8),
44 FT_CR_GPR (9), FT_CR_GPR (10), FT_CR_GPR (11),
45 FT_CR_GPR (12), FT_CR_GPR (13), FT_CR_GPR (14),
46 FT_CR_GPR (15), FT_CR_GPR (16), FT_CR_GPR (17),
47 FT_CR_GPR (18), FT_CR_GPR (19), FT_CR_GPR (20),
48 FT_CR_GPR (21), FT_CR_GPR (22), FT_CR_GPR (23),
49 FT_CR_GPR (24), FT_CR_GPR (25), FT_CR_GPR (26),
50 FT_CR_GPR (27), FT_CR_GPR (28), FT_CR_GPR (29),
51 FT_CR_GPR (30), FT_CR_GPR (31),
52 /* FPRs - not collected. */
53 -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1,
57 FT_CR_PC, /* PC */
58 -1, /* MSR */
59 FT_CR_CR, /* CR */
60 FT_CR_LR, /* LR */
61 FT_CR_CTR, /* CTR */
62 FT_CR_XER, /* XER */
63 -1, /* FPSCR */
64 };
65
66 #define PPC_NUM_FT_COLLECT_GREGS \
67 (sizeof (ppc_ft_collect_regmap) / sizeof(ppc_ft_collect_regmap[0]))
68
69 /* Supply registers collected by the fast tracepoint jump pad.
70 BUF is the second argument we pass to gdb_collect in jump pad. */
71
72 void
73 supply_fast_tracepoint_registers (struct regcache *regcache,
74 const unsigned char *buf)
75 {
76 int i;
77
78 for (i = 0; i < PPC_NUM_FT_COLLECT_GREGS; i++)
79 {
80 if (ppc_ft_collect_regmap[i] == -1)
81 continue;
82 supply_register (regcache, i,
83 ((char *) buf)
84 + ppc_ft_collect_regmap[i] * sizeof (long));
85 }
86 }
87
88 /* Return the value of register REGNUM. RAW_REGS is collected buffer
89 by jump pad. This function is called by emit_reg. */
90
91 ULONGEST
92 get_raw_reg (const unsigned char *raw_regs, int regnum)
93 {
94 if (regnum >= PPC_NUM_FT_COLLECT_GREGS)
95 return 0;
96 if (ppc_ft_collect_regmap[regnum] == -1)
97 return 0;
98
99 return *(unsigned long *) (raw_regs
100 + ppc_ft_collect_regmap[regnum] * sizeof (long));
101 }
102
103 /* Allocate buffer for the jump pads. The branch instruction has a reach
104 of +/- 32MiB, and the executable is loaded at 0x10000000 (256MiB).
105
106 64-bit: To maximize the area of executable that can use tracepoints,
107 try allocating at 0x10000000 - size initially, decreasing until we hit
108 a free area.
109
110 32-bit: ld.so loads dynamic libraries right below the executable, so
111 we cannot depend on that area (dynamic libraries can be quite large).
112 Instead, aim right after the executable - at sbrk(0). This will
113 cause future brk to fail, and malloc will fallback to mmap. */
114
115 void *
116 alloc_jump_pad_buffer (size_t size)
117 {
118 #ifdef __powerpc64__
119 uintptr_t addr;
120 uintptr_t exec_base = getauxval (AT_PHDR);
121 int pagesize;
122 void *res;
123
124 if (exec_base == 0)
125 exec_base = 0x10000000;
126
127 pagesize = sysconf (_SC_PAGE_SIZE);
128 if (pagesize == -1)
129 perror_with_name ("sysconf");
130
131 addr = exec_base - size;
132
133 /* size should already be page-aligned, but this can't hurt. */
134 addr &= ~(pagesize - 1);
135
136 /* Search for a free area. If we hit 0, we're out of luck. */
137 for (; addr; addr -= pagesize)
138 {
139 /* No MAP_FIXED - we don't want to zap someone's mapping. */
140 res = mmap ((void *) addr, size,
141 PROT_READ | PROT_WRITE | PROT_EXEC,
142 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
143
144 /* If we got what we wanted, return. */
145 if ((uintptr_t) res == addr)
146 return res;
147
148 /* If we got a mapping, but at a wrong address, undo it. */
149 if (res != MAP_FAILED)
150 munmap (res, size);
151 }
152
153 return NULL;
154 #else
155 void *target = sbrk (0);
156 void *res = mmap (target, size, PROT_READ | PROT_WRITE | PROT_EXEC,
157 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
158
159 if (res == target)
160 return res;
161
162 if (res != MAP_FAILED)
163 munmap (res, size);
164
165 return NULL;
166 #endif
167 }
168
169 /* Return target_desc to use for IPA, given the tdesc index passed by
170 gdbserver. */
171
172 const struct target_desc *
173 get_ipa_tdesc (int idx)
174 {
175 switch (idx)
176 {
177 #ifdef __powerpc64__
178 case PPC_TDESC_BASE:
179 return tdesc_powerpc_64l;
180 case PPC_TDESC_ALTIVEC:
181 return tdesc_powerpc_altivec64l;
182 case PPC_TDESC_CELL:
183 return tdesc_powerpc_cell64l;
184 case PPC_TDESC_VSX:
185 return tdesc_powerpc_vsx64l;
186 case PPC_TDESC_ISA205:
187 return tdesc_powerpc_isa205_64l;
188 case PPC_TDESC_ISA205_ALTIVEC:
189 return tdesc_powerpc_isa205_altivec64l;
190 case PPC_TDESC_ISA205_VSX:
191 return tdesc_powerpc_isa205_vsx64l;
192 #else
193 case PPC_TDESC_BASE:
194 return tdesc_powerpc_32l;
195 case PPC_TDESC_ALTIVEC:
196 return tdesc_powerpc_altivec32l;
197 case PPC_TDESC_CELL:
198 return tdesc_powerpc_cell32l;
199 case PPC_TDESC_VSX:
200 return tdesc_powerpc_vsx32l;
201 case PPC_TDESC_ISA205:
202 return tdesc_powerpc_isa205_32l;
203 case PPC_TDESC_ISA205_ALTIVEC:
204 return tdesc_powerpc_isa205_altivec32l;
205 case PPC_TDESC_ISA205_VSX:
206 return tdesc_powerpc_isa205_vsx32l;
207 case PPC_TDESC_E500:
208 return tdesc_powerpc_e500l;
209 #endif
210 default:
211 internal_error (__FILE__, __LINE__,
212 "unknown ipa tdesc index: %d", idx);
213 #ifdef __powerpc64__
214 return tdesc_powerpc_64l;
215 #else
216 return tdesc_powerpc_32l;
217 #endif
218 }
219 }
220
221
222 /* Initialize ipa_tdesc and others. */
223
224 void
225 initialize_low_tracepoint (void)
226 {
227 #ifdef __powerpc64__
228 init_registers_powerpc_64l ();
229 init_registers_powerpc_altivec64l ();
230 init_registers_powerpc_cell64l ();
231 init_registers_powerpc_vsx64l ();
232 init_registers_powerpc_isa205_64l ();
233 init_registers_powerpc_isa205_altivec64l ();
234 init_registers_powerpc_isa205_vsx64l ();
235 #else
236 init_registers_powerpc_32l ();
237 init_registers_powerpc_altivec32l ();
238 init_registers_powerpc_cell32l ();
239 init_registers_powerpc_vsx32l ();
240 init_registers_powerpc_isa205_32l ();
241 init_registers_powerpc_isa205_altivec32l ();
242 init_registers_powerpc_isa205_vsx32l ();
243 init_registers_powerpc_e500l ();
244 #endif
245 }
This page took 0.034013 seconds and 3 git commands to generate.