IPA: Add alloc_jump_pad_buffer target hook.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-aarch64-ipa.c
1 /* GNU/Linux/AArch64 specific low level interface, for the in-process
2 agent library for GDB.
3
4 Copyright (C) 2015-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 #ifdef HAVE_GETAUXVAL
25 #include <sys/auxv.h>
26 #endif
27
28 /* Defined in auto-generated file aarch64.c. */
29 void init_registers_aarch64 (void);
30 extern const struct target_desc *tdesc_aarch64;
31
32 /* Each register saved by the jump pad is in a 16 byte cell. */
33 #define FT_CR_SIZE 16
34
35 #define FT_CR_FPCR 0
36 #define FT_CR_FPSR 1
37 #define FT_CR_CPSR 2
38 #define FT_CR_PC 3
39 #define FT_CR_SP 4
40 #define FT_CR_X0 5
41 #define FT_CR_GPR(n) (FT_CR_X0 + (n))
42 #define FT_CR_FPR(n) (FT_CR_GPR (31) + (n))
43
44 /* Mapping between registers collected by the jump pad and GDB's register
45 array layout used by regcache.
46
47 See linux-aarch64-low.c (aarch64_install_fast_tracepoint_jump_pad) for
48 more details. */
49
50 static const int aarch64_ft_collect_regmap[] = {
51 FT_CR_GPR (0),
52 FT_CR_GPR (1),
53 FT_CR_GPR (2),
54 FT_CR_GPR (3),
55 FT_CR_GPR (4),
56 FT_CR_GPR (5),
57 FT_CR_GPR (6),
58 FT_CR_GPR (7),
59 FT_CR_GPR (8),
60 FT_CR_GPR (9),
61 FT_CR_GPR (10),
62 FT_CR_GPR (11),
63 FT_CR_GPR (12),
64 FT_CR_GPR (13),
65 FT_CR_GPR (14),
66 FT_CR_GPR (15),
67 FT_CR_GPR (16),
68 FT_CR_GPR (17),
69 FT_CR_GPR (18),
70 FT_CR_GPR (19),
71 FT_CR_GPR (20),
72 FT_CR_GPR (21),
73 FT_CR_GPR (22),
74 FT_CR_GPR (23),
75 FT_CR_GPR (24),
76 FT_CR_GPR (25),
77 FT_CR_GPR (26),
78 FT_CR_GPR (27),
79 FT_CR_GPR (28),
80 /* FP */
81 FT_CR_GPR (29),
82 /* LR */
83 FT_CR_GPR (30),
84 FT_CR_SP,
85 FT_CR_PC,
86 FT_CR_CPSR,
87 FT_CR_FPR (0),
88 FT_CR_FPR (1),
89 FT_CR_FPR (2),
90 FT_CR_FPR (3),
91 FT_CR_FPR (4),
92 FT_CR_FPR (5),
93 FT_CR_FPR (6),
94 FT_CR_FPR (7),
95 FT_CR_FPR (8),
96 FT_CR_FPR (9),
97 FT_CR_FPR (10),
98 FT_CR_FPR (11),
99 FT_CR_FPR (12),
100 FT_CR_FPR (13),
101 FT_CR_FPR (14),
102 FT_CR_FPR (15),
103 FT_CR_FPR (16),
104 FT_CR_FPR (17),
105 FT_CR_FPR (18),
106 FT_CR_FPR (19),
107 FT_CR_FPR (20),
108 FT_CR_FPR (21),
109 FT_CR_FPR (22),
110 FT_CR_FPR (23),
111 FT_CR_FPR (24),
112 FT_CR_FPR (25),
113 FT_CR_FPR (26),
114 FT_CR_FPR (27),
115 FT_CR_FPR (28),
116 FT_CR_FPR (29),
117 FT_CR_FPR (30),
118 FT_CR_FPR (31),
119 FT_CR_FPSR,
120 FT_CR_FPCR
121 };
122
123 #define AARCH64_NUM_FT_COLLECT_GREGS \
124 (sizeof (aarch64_ft_collect_regmap) / sizeof(aarch64_ft_collect_regmap[0]))
125
126 /* Fill in REGCACHE with registers saved by the jump pad in BUF. */
127
128 void
129 supply_fast_tracepoint_registers (struct regcache *regcache,
130 const unsigned char *buf)
131 {
132 int i;
133
134 for (i = 0; i < AARCH64_NUM_FT_COLLECT_GREGS; i++)
135 supply_register (regcache, i,
136 ((char *) buf)
137 + (aarch64_ft_collect_regmap[i] * FT_CR_SIZE));
138 }
139
140 ULONGEST
141 get_raw_reg (const unsigned char *raw_regs, int regnum)
142 {
143 if (regnum >= AARCH64_NUM_FT_COLLECT_GREGS)
144 return 0;
145
146 return *(ULONGEST *) (raw_regs
147 + aarch64_ft_collect_regmap[regnum] * FT_CR_SIZE);
148 }
149
150 /* Return target_desc to use for IPA, given the tdesc index passed by
151 gdbserver. Index is ignored, since we have only one tdesc
152 at the moment. */
153
154 const struct target_desc *
155 get_ipa_tdesc (int idx)
156 {
157 return tdesc_aarch64;
158 }
159
160 /* Allocate buffer for the jump pads. The branch instruction has a reach
161 of +/- 128MiB, and the executable is loaded at 0x400000 (4MiB).
162 To maximize the area of executable that can use tracepoints, try
163 allocating at 0x400000 - size initially, decreasing until we hit
164 a free area. */
165
166 void *
167 alloc_jump_pad_buffer (size_t size)
168 {
169 uintptr_t addr;
170 uintptr_t exec_base = getauxval (AT_PHDR);
171 int pagesize;
172 void *res;
173
174 if (exec_base == 0)
175 exec_base = 0x400000;
176
177 pagesize = sysconf (_SC_PAGE_SIZE);
178 if (pagesize == -1)
179 perror_with_name ("sysconf");
180
181 addr = exec_base - size;
182
183 /* size should already be page-aligned, but this can't hurt. */
184 addr &= ~(pagesize - 1);
185
186 /* Search for a free area. If we hit 0, we're out of luck. */
187 for (; addr; addr -= pagesize)
188 {
189 /* No MAP_FIXED - we don't want to zap someone's mapping. */
190 res = mmap ((void *) addr, size,
191 PROT_READ | PROT_WRITE | PROT_EXEC,
192 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
193
194 /* If we got what we wanted, return. */
195 if ((uintptr_t) res == addr)
196 return res;
197
198 /* If we got a mapping, but at a wrong address, undo it. */
199 if (res != MAP_FAILED)
200 munmap (res, size);
201 }
202
203 return NULL;
204 }
205
206 void
207 initialize_low_tracepoint (void)
208 {
209 init_registers_aarch64 ();
210 }
This page took 0.050873 seconds and 4 git commands to generate.