arm-get-next-pcs.c revision 1.1 1 1.1 christos /* Common code for ARM software single stepping support.
2 1.1 christos
3 1.1 christos Copyright (C) 1988-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "common-defs.h"
21 1.1 christos #include "gdb_vecs.h"
22 1.1 christos #include "common-regcache.h"
23 1.1 christos #include "arm.h"
24 1.1 christos #include "arm-get-next-pcs.h"
25 1.1 christos
26 1.1 christos /* See arm-get-next-pcs.h. */
27 1.1 christos
28 1.1 christos void
29 1.1 christos arm_get_next_pcs_ctor (struct arm_get_next_pcs *self,
30 1.1 christos struct arm_get_next_pcs_ops *ops,
31 1.1 christos int byte_order,
32 1.1 christos int byte_order_for_code,
33 1.1 christos int has_thumb2_breakpoint,
34 1.1 christos struct regcache *regcache)
35 1.1 christos {
36 1.1 christos self->ops = ops;
37 1.1 christos self->byte_order = byte_order;
38 1.1 christos self->byte_order_for_code = byte_order_for_code;
39 1.1 christos self->has_thumb2_breakpoint = has_thumb2_breakpoint;
40 1.1 christos self->regcache = regcache;
41 1.1 christos }
42 1.1 christos
43 1.1 christos /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
44 1.1 christos instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
45 1.1 christos is found, attempt to step through it. The end of the sequence address is
46 1.1 christos added to the next_pcs list. */
47 1.1 christos
48 1.1 christos static VEC (CORE_ADDR) *
49 1.1 christos thumb_deal_with_atomic_sequence_raw (struct arm_get_next_pcs *self)
50 1.1 christos {
51 1.1 christos int byte_order_for_code = self->byte_order_for_code;
52 1.1 christos CORE_ADDR breaks[2] = {-1, -1};
53 1.1 christos CORE_ADDR pc = regcache_read_pc (self->regcache);
54 1.1 christos CORE_ADDR loc = pc;
55 1.1 christos unsigned short insn1, insn2;
56 1.1 christos int insn_count;
57 1.1 christos int index;
58 1.1 christos int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
59 1.1 christos const int atomic_sequence_length = 16; /* Instruction sequence length. */
60 1.1 christos ULONGEST status, itstate;
61 1.1 christos VEC (CORE_ADDR) *next_pcs = NULL;
62 1.1 christos
63 1.1 christos /* We currently do not support atomic sequences within an IT block. */
64 1.1 christos status = regcache_raw_get_unsigned (self->regcache, ARM_PS_REGNUM);
65 1.1 christos itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
66 1.1 christos if (itstate & 0x0f)
67 1.1 christos return NULL;
68 1.1 christos
69 1.1 christos /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
70 1.1 christos insn1 = self->ops->read_mem_uint (loc, 2, byte_order_for_code);
71 1.1 christos
72 1.1 christos loc += 2;
73 1.1 christos if (thumb_insn_size (insn1) != 4)
74 1.1 christos return NULL;
75 1.1 christos
76 1.1 christos insn2 = self->ops->read_mem_uint (loc, 2, byte_order_for_code);
77 1.1 christos
78 1.1 christos loc += 2;
79 1.1 christos if (!((insn1 & 0xfff0) == 0xe850
80 1.1 christos || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
81 1.1 christos return NULL;
82 1.1 christos
83 1.1 christos /* Assume that no atomic sequence is longer than "atomic_sequence_length"
84 1.1 christos instructions. */
85 1.1 christos for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
86 1.1 christos {
87 1.1 christos insn1 = self->ops->read_mem_uint (loc, 2,byte_order_for_code);
88 1.1 christos loc += 2;
89 1.1 christos
90 1.1 christos if (thumb_insn_size (insn1) != 4)
91 1.1 christos {
92 1.1 christos /* Assume that there is at most one conditional branch in the
93 1.1 christos atomic sequence. If a conditional branch is found, put a
94 1.1 christos breakpoint in its destination address. */
95 1.1 christos if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
96 1.1 christos {
97 1.1 christos if (last_breakpoint > 0)
98 1.1 christos return NULL; /* More than one conditional branch found,
99 1.1 christos fallback to the standard code. */
100 1.1 christos
101 1.1 christos breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
102 1.1 christos last_breakpoint++;
103 1.1 christos }
104 1.1 christos
105 1.1 christos /* We do not support atomic sequences that use any *other*
106 1.1 christos instructions but conditional branches to change the PC.
107 1.1 christos Fall back to standard code to avoid losing control of
108 1.1 christos execution. */
109 1.1 christos else if (thumb_instruction_changes_pc (insn1))
110 1.1 christos return NULL;
111 1.1 christos }
112 1.1 christos else
113 1.1 christos {
114 1.1 christos insn2 = self->ops->read_mem_uint (loc, 2, byte_order_for_code);
115 1.1 christos
116 1.1 christos loc += 2;
117 1.1 christos
118 1.1 christos /* Assume that there is at most one conditional branch in the
119 1.1 christos atomic sequence. If a conditional branch is found, put a
120 1.1 christos breakpoint in its destination address. */
121 1.1 christos if ((insn1 & 0xf800) == 0xf000
122 1.1 christos && (insn2 & 0xd000) == 0x8000
123 1.1 christos && (insn1 & 0x0380) != 0x0380)
124 1.1 christos {
125 1.1 christos int sign, j1, j2, imm1, imm2;
126 1.1 christos unsigned int offset;
127 1.1 christos
128 1.1 christos sign = sbits (insn1, 10, 10);
129 1.1 christos imm1 = bits (insn1, 0, 5);
130 1.1 christos imm2 = bits (insn2, 0, 10);
131 1.1 christos j1 = bit (insn2, 13);
132 1.1 christos j2 = bit (insn2, 11);
133 1.1 christos
134 1.1 christos offset = (sign << 20) + (j2 << 19) + (j1 << 18);
135 1.1 christos offset += (imm1 << 12) + (imm2 << 1);
136 1.1 christos
137 1.1 christos if (last_breakpoint > 0)
138 1.1 christos return 0; /* More than one conditional branch found,
139 1.1 christos fallback to the standard code. */
140 1.1 christos
141 1.1 christos breaks[1] = loc + offset;
142 1.1 christos last_breakpoint++;
143 1.1 christos }
144 1.1 christos
145 1.1 christos /* We do not support atomic sequences that use any *other*
146 1.1 christos instructions but conditional branches to change the PC.
147 1.1 christos Fall back to standard code to avoid losing control of
148 1.1 christos execution. */
149 1.1 christos else if (thumb2_instruction_changes_pc (insn1, insn2))
150 1.1 christos return NULL;
151 1.1 christos
152 1.1 christos /* If we find a strex{,b,h,d}, we're done. */
153 1.1 christos if ((insn1 & 0xfff0) == 0xe840
154 1.1 christos || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
155 1.1 christos break;
156 1.1 christos }
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
160 1.1 christos if (insn_count == atomic_sequence_length)
161 1.1 christos return NULL;
162 1.1 christos
163 1.1 christos /* Insert a breakpoint right after the end of the atomic sequence. */
164 1.1 christos breaks[0] = loc;
165 1.1 christos
166 1.1 christos /* Check for duplicated breakpoints. Check also for a breakpoint
167 1.1 christos placed (branch instruction's destination) anywhere in sequence. */
168 1.1 christos if (last_breakpoint
169 1.1 christos && (breaks[1] == breaks[0]
170 1.1 christos || (breaks[1] >= pc && breaks[1] < loc)))
171 1.1 christos last_breakpoint = 0;
172 1.1 christos
173 1.1 christos /* Adds the breakpoints to the list to be inserted. */
174 1.1 christos for (index = 0; index <= last_breakpoint; index++)
175 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, MAKE_THUMB_ADDR (breaks[index]));
176 1.1 christos
177 1.1 christos return next_pcs;
178 1.1 christos }
179 1.1 christos
180 1.1 christos /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
181 1.1 christos instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
182 1.1 christos is found, attempt to step through it. The end of the sequence address is
183 1.1 christos added to the next_pcs list. */
184 1.1 christos
185 1.1 christos static VEC (CORE_ADDR) *
186 1.1 christos arm_deal_with_atomic_sequence_raw (struct arm_get_next_pcs *self)
187 1.1 christos {
188 1.1 christos int byte_order_for_code = self->byte_order_for_code;
189 1.1 christos CORE_ADDR breaks[2] = {-1, -1};
190 1.1 christos CORE_ADDR pc = regcache_read_pc (self->regcache);
191 1.1 christos CORE_ADDR loc = pc;
192 1.1 christos unsigned int insn;
193 1.1 christos int insn_count;
194 1.1 christos int index;
195 1.1 christos int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
196 1.1 christos const int atomic_sequence_length = 16; /* Instruction sequence length. */
197 1.1 christos VEC (CORE_ADDR) *next_pcs = NULL;
198 1.1 christos
199 1.1 christos /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
200 1.1 christos Note that we do not currently support conditionally executed atomic
201 1.1 christos instructions. */
202 1.1 christos insn = self->ops->read_mem_uint (loc, 4, byte_order_for_code);
203 1.1 christos
204 1.1 christos loc += 4;
205 1.1 christos if ((insn & 0xff9000f0) != 0xe1900090)
206 1.1 christos return NULL;
207 1.1 christos
208 1.1 christos /* Assume that no atomic sequence is longer than "atomic_sequence_length"
209 1.1 christos instructions. */
210 1.1 christos for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
211 1.1 christos {
212 1.1 christos insn = self->ops->read_mem_uint (loc, 4, byte_order_for_code);
213 1.1 christos
214 1.1 christos loc += 4;
215 1.1 christos
216 1.1 christos /* Assume that there is at most one conditional branch in the atomic
217 1.1 christos sequence. If a conditional branch is found, put a breakpoint in
218 1.1 christos its destination address. */
219 1.1 christos if (bits (insn, 24, 27) == 0xa)
220 1.1 christos {
221 1.1 christos if (last_breakpoint > 0)
222 1.1 christos return NULL; /* More than one conditional branch found, fallback
223 1.1 christos to the standard single-step code. */
224 1.1 christos
225 1.1 christos breaks[1] = BranchDest (loc - 4, insn);
226 1.1 christos last_breakpoint++;
227 1.1 christos }
228 1.1 christos
229 1.1 christos /* We do not support atomic sequences that use any *other* instructions
230 1.1 christos but conditional branches to change the PC. Fall back to standard
231 1.1 christos code to avoid losing control of execution. */
232 1.1 christos else if (arm_instruction_changes_pc (insn))
233 1.1 christos return NULL;
234 1.1 christos
235 1.1 christos /* If we find a strex{,b,h,d}, we're done. */
236 1.1 christos if ((insn & 0xff9000f0) == 0xe1800090)
237 1.1 christos break;
238 1.1 christos }
239 1.1 christos
240 1.1 christos /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
241 1.1 christos if (insn_count == atomic_sequence_length)
242 1.1 christos return NULL;
243 1.1 christos
244 1.1 christos /* Insert a breakpoint right after the end of the atomic sequence. */
245 1.1 christos breaks[0] = loc;
246 1.1 christos
247 1.1 christos /* Check for duplicated breakpoints. Check also for a breakpoint
248 1.1 christos placed (branch instruction's destination) anywhere in sequence. */
249 1.1 christos if (last_breakpoint
250 1.1 christos && (breaks[1] == breaks[0]
251 1.1 christos || (breaks[1] >= pc && breaks[1] < loc)))
252 1.1 christos last_breakpoint = 0;
253 1.1 christos
254 1.1 christos /* Adds the breakpoints to the list to be inserted. */
255 1.1 christos for (index = 0; index <= last_breakpoint; index++)
256 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, breaks[index]);
257 1.1 christos
258 1.1 christos return next_pcs;
259 1.1 christos }
260 1.1 christos
261 1.1 christos /* Find the next possible PCs for thumb mode. */
262 1.1 christos
263 1.1 christos static VEC (CORE_ADDR) *
264 1.1 christos thumb_get_next_pcs_raw (struct arm_get_next_pcs *self)
265 1.1 christos {
266 1.1 christos int byte_order = self->byte_order;
267 1.1 christos int byte_order_for_code = self->byte_order_for_code;
268 1.1 christos CORE_ADDR pc = regcache_read_pc (self->regcache);
269 1.1 christos unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
270 1.1 christos unsigned short inst1;
271 1.1 christos CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
272 1.1 christos unsigned long offset;
273 1.1 christos ULONGEST status, itstate;
274 1.1 christos struct regcache *regcache = self->regcache;
275 1.1 christos VEC (CORE_ADDR) * next_pcs = NULL;
276 1.1 christos
277 1.1 christos nextpc = MAKE_THUMB_ADDR (nextpc);
278 1.1 christos pc_val = MAKE_THUMB_ADDR (pc_val);
279 1.1 christos
280 1.1 christos inst1 = self->ops->read_mem_uint (pc, 2, byte_order_for_code);
281 1.1 christos
282 1.1 christos /* Thumb-2 conditional execution support. There are eight bits in
283 1.1 christos the CPSR which describe conditional execution state. Once
284 1.1 christos reconstructed (they're in a funny order), the low five bits
285 1.1 christos describe the low bit of the condition for each instruction and
286 1.1 christos how many instructions remain. The high three bits describe the
287 1.1 christos base condition. One of the low four bits will be set if an IT
288 1.1 christos block is active. These bits read as zero on earlier
289 1.1 christos processors. */
290 1.1 christos status = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
291 1.1 christos itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
292 1.1 christos
293 1.1 christos /* If-Then handling. On GNU/Linux, where this routine is used, we
294 1.1 christos use an undefined instruction as a breakpoint. Unlike BKPT, IT
295 1.1 christos can disable execution of the undefined instruction. So we might
296 1.1 christos miss the breakpoint if we set it on a skipped conditional
297 1.1 christos instruction. Because conditional instructions can change the
298 1.1 christos flags, affecting the execution of further instructions, we may
299 1.1 christos need to set two breakpoints. */
300 1.1 christos
301 1.1 christos if (self->has_thumb2_breakpoint)
302 1.1 christos {
303 1.1 christos if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
304 1.1 christos {
305 1.1 christos /* An IT instruction. Because this instruction does not
306 1.1 christos modify the flags, we can accurately predict the next
307 1.1 christos executed instruction. */
308 1.1 christos itstate = inst1 & 0x00ff;
309 1.1 christos pc += thumb_insn_size (inst1);
310 1.1 christos
311 1.1 christos while (itstate != 0 && ! condition_true (itstate >> 4, status))
312 1.1 christos {
313 1.1 christos inst1 = self->ops->read_mem_uint (pc, 2,byte_order_for_code);
314 1.1 christos pc += thumb_insn_size (inst1);
315 1.1 christos itstate = thumb_advance_itstate (itstate);
316 1.1 christos }
317 1.1 christos
318 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, MAKE_THUMB_ADDR (pc));
319 1.1 christos return next_pcs;
320 1.1 christos }
321 1.1 christos else if (itstate != 0)
322 1.1 christos {
323 1.1 christos /* We are in a conditional block. Check the condition. */
324 1.1 christos if (! condition_true (itstate >> 4, status))
325 1.1 christos {
326 1.1 christos /* Advance to the next executed instruction. */
327 1.1 christos pc += thumb_insn_size (inst1);
328 1.1 christos itstate = thumb_advance_itstate (itstate);
329 1.1 christos
330 1.1 christos while (itstate != 0 && ! condition_true (itstate >> 4, status))
331 1.1 christos {
332 1.1 christos inst1 = self->ops->read_mem_uint (pc, 2, byte_order_for_code);
333 1.1 christos
334 1.1 christos pc += thumb_insn_size (inst1);
335 1.1 christos itstate = thumb_advance_itstate (itstate);
336 1.1 christos }
337 1.1 christos
338 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, MAKE_THUMB_ADDR (pc));
339 1.1 christos return next_pcs;
340 1.1 christos }
341 1.1 christos else if ((itstate & 0x0f) == 0x08)
342 1.1 christos {
343 1.1 christos /* This is the last instruction of the conditional
344 1.1 christos block, and it is executed. We can handle it normally
345 1.1 christos because the following instruction is not conditional,
346 1.1 christos and we must handle it normally because it is
347 1.1 christos permitted to branch. Fall through. */
348 1.1 christos }
349 1.1 christos else
350 1.1 christos {
351 1.1 christos int cond_negated;
352 1.1 christos
353 1.1 christos /* There are conditional instructions after this one.
354 1.1 christos If this instruction modifies the flags, then we can
355 1.1 christos not predict what the next executed instruction will
356 1.1 christos be. Fortunately, this instruction is architecturally
357 1.1 christos forbidden to branch; we know it will fall through.
358 1.1 christos Start by skipping past it. */
359 1.1 christos pc += thumb_insn_size (inst1);
360 1.1 christos itstate = thumb_advance_itstate (itstate);
361 1.1 christos
362 1.1 christos /* Set a breakpoint on the following instruction. */
363 1.1 christos gdb_assert ((itstate & 0x0f) != 0);
364 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, MAKE_THUMB_ADDR (pc));
365 1.1 christos
366 1.1 christos cond_negated = (itstate >> 4) & 1;
367 1.1 christos
368 1.1 christos /* Skip all following instructions with the same
369 1.1 christos condition. If there is a later instruction in the IT
370 1.1 christos block with the opposite condition, set the other
371 1.1 christos breakpoint there. If not, then set a breakpoint on
372 1.1 christos the instruction after the IT block. */
373 1.1 christos do
374 1.1 christos {
375 1.1 christos inst1 = self->ops->read_mem_uint (pc, 2, byte_order_for_code);
376 1.1 christos pc += thumb_insn_size (inst1);
377 1.1 christos itstate = thumb_advance_itstate (itstate);
378 1.1 christos }
379 1.1 christos while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
380 1.1 christos
381 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, MAKE_THUMB_ADDR (pc));
382 1.1 christos
383 1.1 christos return next_pcs;
384 1.1 christos }
385 1.1 christos }
386 1.1 christos }
387 1.1 christos else if (itstate & 0x0f)
388 1.1 christos {
389 1.1 christos /* We are in a conditional block. Check the condition. */
390 1.1 christos int cond = itstate >> 4;
391 1.1 christos
392 1.1 christos if (! condition_true (cond, status))
393 1.1 christos {
394 1.1 christos /* Advance to the next instruction. All the 32-bit
395 1.1 christos instructions share a common prefix. */
396 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs,
397 1.1 christos MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1)));
398 1.1 christos }
399 1.1 christos
400 1.1 christos return next_pcs;
401 1.1 christos
402 1.1 christos /* Otherwise, handle the instruction normally. */
403 1.1 christos }
404 1.1 christos
405 1.1 christos if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
406 1.1 christos {
407 1.1 christos CORE_ADDR sp;
408 1.1 christos
409 1.1 christos /* Fetch the saved PC from the stack. It's stored above
410 1.1 christos all of the other registers. */
411 1.1 christos offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
412 1.1 christos sp = regcache_raw_get_unsigned (regcache, ARM_SP_REGNUM);
413 1.1 christos nextpc = self->ops->read_mem_uint (sp + offset, 4, byte_order);
414 1.1 christos }
415 1.1 christos else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
416 1.1 christos {
417 1.1 christos unsigned long cond = bits (inst1, 8, 11);
418 1.1 christos if (cond == 0x0f) /* 0x0f = SWI */
419 1.1 christos {
420 1.1 christos nextpc = self->ops->syscall_next_pc (self);
421 1.1 christos }
422 1.1 christos else if (cond != 0x0f && condition_true (cond, status))
423 1.1 christos nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
424 1.1 christos }
425 1.1 christos else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
426 1.1 christos {
427 1.1 christos nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
428 1.1 christos }
429 1.1 christos else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
430 1.1 christos {
431 1.1 christos unsigned short inst2;
432 1.1 christos inst2 = self->ops->read_mem_uint (pc + 2, 2, byte_order_for_code);
433 1.1 christos
434 1.1 christos /* Default to the next instruction. */
435 1.1 christos nextpc = pc + 4;
436 1.1 christos nextpc = MAKE_THUMB_ADDR (nextpc);
437 1.1 christos
438 1.1 christos if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
439 1.1 christos {
440 1.1 christos /* Branches and miscellaneous control instructions. */
441 1.1 christos
442 1.1 christos if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
443 1.1 christos {
444 1.1 christos /* B, BL, BLX. */
445 1.1 christos int j1, j2, imm1, imm2;
446 1.1 christos
447 1.1 christos imm1 = sbits (inst1, 0, 10);
448 1.1 christos imm2 = bits (inst2, 0, 10);
449 1.1 christos j1 = bit (inst2, 13);
450 1.1 christos j2 = bit (inst2, 11);
451 1.1 christos
452 1.1 christos offset = ((imm1 << 12) + (imm2 << 1));
453 1.1 christos offset ^= ((!j2) << 22) | ((!j1) << 23);
454 1.1 christos
455 1.1 christos nextpc = pc_val + offset;
456 1.1 christos /* For BLX make sure to clear the low bits. */
457 1.1 christos if (bit (inst2, 12) == 0)
458 1.1 christos nextpc = nextpc & 0xfffffffc;
459 1.1 christos }
460 1.1 christos else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
461 1.1 christos {
462 1.1 christos /* SUBS PC, LR, #imm8. */
463 1.1 christos nextpc = regcache_raw_get_unsigned (regcache, ARM_LR_REGNUM);
464 1.1 christos nextpc -= inst2 & 0x00ff;
465 1.1 christos }
466 1.1 christos else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
467 1.1 christos {
468 1.1 christos /* Conditional branch. */
469 1.1 christos if (condition_true (bits (inst1, 6, 9), status))
470 1.1 christos {
471 1.1 christos int sign, j1, j2, imm1, imm2;
472 1.1 christos
473 1.1 christos sign = sbits (inst1, 10, 10);
474 1.1 christos imm1 = bits (inst1, 0, 5);
475 1.1 christos imm2 = bits (inst2, 0, 10);
476 1.1 christos j1 = bit (inst2, 13);
477 1.1 christos j2 = bit (inst2, 11);
478 1.1 christos
479 1.1 christos offset = (sign << 20) + (j2 << 19) + (j1 << 18);
480 1.1 christos offset += (imm1 << 12) + (imm2 << 1);
481 1.1 christos
482 1.1 christos nextpc = pc_val + offset;
483 1.1 christos }
484 1.1 christos }
485 1.1 christos }
486 1.1 christos else if ((inst1 & 0xfe50) == 0xe810)
487 1.1 christos {
488 1.1 christos /* Load multiple or RFE. */
489 1.1 christos int rn, offset, load_pc = 1;
490 1.1 christos
491 1.1 christos rn = bits (inst1, 0, 3);
492 1.1 christos if (bit (inst1, 7) && !bit (inst1, 8))
493 1.1 christos {
494 1.1 christos /* LDMIA or POP */
495 1.1 christos if (!bit (inst2, 15))
496 1.1 christos load_pc = 0;
497 1.1 christos offset = bitcount (inst2) * 4 - 4;
498 1.1 christos }
499 1.1 christos else if (!bit (inst1, 7) && bit (inst1, 8))
500 1.1 christos {
501 1.1 christos /* LDMDB */
502 1.1 christos if (!bit (inst2, 15))
503 1.1 christos load_pc = 0;
504 1.1 christos offset = -4;
505 1.1 christos }
506 1.1 christos else if (bit (inst1, 7) && bit (inst1, 8))
507 1.1 christos {
508 1.1 christos /* RFEIA */
509 1.1 christos offset = 0;
510 1.1 christos }
511 1.1 christos else if (!bit (inst1, 7) && !bit (inst1, 8))
512 1.1 christos {
513 1.1 christos /* RFEDB */
514 1.1 christos offset = -8;
515 1.1 christos }
516 1.1 christos else
517 1.1 christos load_pc = 0;
518 1.1 christos
519 1.1 christos if (load_pc)
520 1.1 christos {
521 1.1 christos CORE_ADDR addr = regcache_raw_get_unsigned (regcache, rn);
522 1.1 christos nextpc = self->ops->read_mem_uint (addr + offset, 4, byte_order);
523 1.1 christos }
524 1.1 christos }
525 1.1 christos else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
526 1.1 christos {
527 1.1 christos /* MOV PC or MOVS PC. */
528 1.1 christos nextpc = regcache_raw_get_unsigned (regcache, bits (inst2, 0, 3));
529 1.1 christos nextpc = MAKE_THUMB_ADDR (nextpc);
530 1.1 christos }
531 1.1 christos else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
532 1.1 christos {
533 1.1 christos /* LDR PC. */
534 1.1 christos CORE_ADDR base;
535 1.1 christos int rn, load_pc = 1;
536 1.1 christos
537 1.1 christos rn = bits (inst1, 0, 3);
538 1.1 christos base = regcache_raw_get_unsigned (regcache, rn);
539 1.1 christos if (rn == ARM_PC_REGNUM)
540 1.1 christos {
541 1.1 christos base = (base + 4) & ~(CORE_ADDR) 0x3;
542 1.1 christos if (bit (inst1, 7))
543 1.1 christos base += bits (inst2, 0, 11);
544 1.1 christos else
545 1.1 christos base -= bits (inst2, 0, 11);
546 1.1 christos }
547 1.1 christos else if (bit (inst1, 7))
548 1.1 christos base += bits (inst2, 0, 11);
549 1.1 christos else if (bit (inst2, 11))
550 1.1 christos {
551 1.1 christos if (bit (inst2, 10))
552 1.1 christos {
553 1.1 christos if (bit (inst2, 9))
554 1.1 christos base += bits (inst2, 0, 7);
555 1.1 christos else
556 1.1 christos base -= bits (inst2, 0, 7);
557 1.1 christos }
558 1.1 christos }
559 1.1 christos else if ((inst2 & 0x0fc0) == 0x0000)
560 1.1 christos {
561 1.1 christos int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
562 1.1 christos base += regcache_raw_get_unsigned (regcache, rm) << shift;
563 1.1 christos }
564 1.1 christos else
565 1.1 christos /* Reserved. */
566 1.1 christos load_pc = 0;
567 1.1 christos
568 1.1 christos if (load_pc)
569 1.1 christos nextpc
570 1.1 christos = self->ops->read_mem_uint (base, 4, byte_order);
571 1.1 christos }
572 1.1 christos else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
573 1.1 christos {
574 1.1 christos /* TBB. */
575 1.1 christos CORE_ADDR tbl_reg, table, offset, length;
576 1.1 christos
577 1.1 christos tbl_reg = bits (inst1, 0, 3);
578 1.1 christos if (tbl_reg == 0x0f)
579 1.1 christos table = pc + 4; /* Regcache copy of PC isn't right yet. */
580 1.1 christos else
581 1.1 christos table = regcache_raw_get_unsigned (regcache, tbl_reg);
582 1.1 christos
583 1.1 christos offset = regcache_raw_get_unsigned (regcache, bits (inst2, 0, 3));
584 1.1 christos length = 2 * self->ops->read_mem_uint (table + offset, 1, byte_order);
585 1.1 christos nextpc = pc_val + length;
586 1.1 christos }
587 1.1 christos else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
588 1.1 christos {
589 1.1 christos /* TBH. */
590 1.1 christos CORE_ADDR tbl_reg, table, offset, length;
591 1.1 christos
592 1.1 christos tbl_reg = bits (inst1, 0, 3);
593 1.1 christos if (tbl_reg == 0x0f)
594 1.1 christos table = pc + 4; /* Regcache copy of PC isn't right yet. */
595 1.1 christos else
596 1.1 christos table = regcache_raw_get_unsigned (regcache, tbl_reg);
597 1.1 christos
598 1.1 christos offset = 2 * regcache_raw_get_unsigned (regcache, bits (inst2, 0, 3));
599 1.1 christos length = 2 * self->ops->read_mem_uint (table + offset, 2, byte_order);
600 1.1 christos nextpc = pc_val + length;
601 1.1 christos }
602 1.1 christos }
603 1.1 christos else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
604 1.1 christos {
605 1.1 christos if (bits (inst1, 3, 6) == 0x0f)
606 1.1 christos nextpc = UNMAKE_THUMB_ADDR (pc_val);
607 1.1 christos else
608 1.1 christos nextpc = regcache_raw_get_unsigned (regcache, bits (inst1, 3, 6));
609 1.1 christos }
610 1.1 christos else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
611 1.1 christos {
612 1.1 christos if (bits (inst1, 3, 6) == 0x0f)
613 1.1 christos nextpc = pc_val;
614 1.1 christos else
615 1.1 christos nextpc = regcache_raw_get_unsigned (regcache, bits (inst1, 3, 6));
616 1.1 christos
617 1.1 christos nextpc = MAKE_THUMB_ADDR (nextpc);
618 1.1 christos }
619 1.1 christos else if ((inst1 & 0xf500) == 0xb100)
620 1.1 christos {
621 1.1 christos /* CBNZ or CBZ. */
622 1.1 christos int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
623 1.1 christos ULONGEST reg = regcache_raw_get_unsigned (regcache, bits (inst1, 0, 2));
624 1.1 christos
625 1.1 christos if (bit (inst1, 11) && reg != 0)
626 1.1 christos nextpc = pc_val + imm;
627 1.1 christos else if (!bit (inst1, 11) && reg == 0)
628 1.1 christos nextpc = pc_val + imm;
629 1.1 christos }
630 1.1 christos
631 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, nextpc);
632 1.1 christos
633 1.1 christos return next_pcs;
634 1.1 christos }
635 1.1 christos
636 1.1 christos /* Get the raw next possible addresses. PC in next_pcs is the current program
637 1.1 christos counter, which is assumed to be executing in ARM mode.
638 1.1 christos
639 1.1 christos The values returned have the execution state of the next instruction
640 1.1 christos encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
641 1.1 christos in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
642 1.1 christos address in GDB and arm_addr_bits_remove in GDBServer. */
643 1.1 christos
644 1.1 christos static VEC (CORE_ADDR) *
645 1.1 christos arm_get_next_pcs_raw (struct arm_get_next_pcs *self)
646 1.1 christos {
647 1.1 christos int byte_order = self->byte_order;
648 1.1 christos int byte_order_for_code = self->byte_order_for_code;
649 1.1 christos unsigned long pc_val;
650 1.1 christos unsigned long this_instr = 0;
651 1.1 christos unsigned long status;
652 1.1 christos CORE_ADDR nextpc;
653 1.1 christos struct regcache *regcache = self->regcache;
654 1.1 christos CORE_ADDR pc = regcache_read_pc (self->regcache);
655 1.1 christos VEC (CORE_ADDR) *next_pcs = NULL;
656 1.1 christos
657 1.1 christos pc_val = (unsigned long) pc;
658 1.1 christos this_instr = self->ops->read_mem_uint (pc, 4, byte_order_for_code);
659 1.1 christos
660 1.1 christos status = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
661 1.1 christos nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
662 1.1 christos
663 1.1 christos if (bits (this_instr, 28, 31) == INST_NV)
664 1.1 christos switch (bits (this_instr, 24, 27))
665 1.1 christos {
666 1.1 christos case 0xa:
667 1.1 christos case 0xb:
668 1.1 christos {
669 1.1 christos /* Branch with Link and change to Thumb. */
670 1.1 christos nextpc = BranchDest (pc, this_instr);
671 1.1 christos nextpc |= bit (this_instr, 24) << 1;
672 1.1 christos nextpc = MAKE_THUMB_ADDR (nextpc);
673 1.1 christos break;
674 1.1 christos }
675 1.1 christos case 0xc:
676 1.1 christos case 0xd:
677 1.1 christos case 0xe:
678 1.1 christos /* Coprocessor register transfer. */
679 1.1 christos if (bits (this_instr, 12, 15) == 15)
680 1.1 christos error (_("Invalid update to pc in instruction"));
681 1.1 christos break;
682 1.1 christos }
683 1.1 christos else if (condition_true (bits (this_instr, 28, 31), status))
684 1.1 christos {
685 1.1 christos switch (bits (this_instr, 24, 27))
686 1.1 christos {
687 1.1 christos case 0x0:
688 1.1 christos case 0x1: /* data processing */
689 1.1 christos case 0x2:
690 1.1 christos case 0x3:
691 1.1 christos {
692 1.1 christos unsigned long operand1, operand2, result = 0;
693 1.1 christos unsigned long rn;
694 1.1 christos int c;
695 1.1 christos
696 1.1 christos if (bits (this_instr, 12, 15) != 15)
697 1.1 christos break;
698 1.1 christos
699 1.1 christos if (bits (this_instr, 22, 25) == 0
700 1.1 christos && bits (this_instr, 4, 7) == 9) /* multiply */
701 1.1 christos error (_("Invalid update to pc in instruction"));
702 1.1 christos
703 1.1 christos /* BX <reg>, BLX <reg> */
704 1.1 christos if (bits (this_instr, 4, 27) == 0x12fff1
705 1.1 christos || bits (this_instr, 4, 27) == 0x12fff3)
706 1.1 christos {
707 1.1 christos rn = bits (this_instr, 0, 3);
708 1.1 christos nextpc = ((rn == ARM_PC_REGNUM)
709 1.1 christos ? (pc_val + 8)
710 1.1 christos : regcache_raw_get_unsigned (regcache, rn));
711 1.1 christos
712 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, nextpc);
713 1.1 christos return next_pcs;
714 1.1 christos }
715 1.1 christos
716 1.1 christos /* Multiply into PC. */
717 1.1 christos c = (status & FLAG_C) ? 1 : 0;
718 1.1 christos rn = bits (this_instr, 16, 19);
719 1.1 christos operand1 = ((rn == ARM_PC_REGNUM)
720 1.1 christos ? (pc_val + 8)
721 1.1 christos : regcache_raw_get_unsigned (regcache, rn));
722 1.1 christos
723 1.1 christos if (bit (this_instr, 25))
724 1.1 christos {
725 1.1 christos unsigned long immval = bits (this_instr, 0, 7);
726 1.1 christos unsigned long rotate = 2 * bits (this_instr, 8, 11);
727 1.1 christos operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
728 1.1 christos & 0xffffffff;
729 1.1 christos }
730 1.1 christos else /* operand 2 is a shifted register. */
731 1.1 christos operand2 = shifted_reg_val (regcache, this_instr, c,
732 1.1 christos pc_val, status);
733 1.1 christos
734 1.1 christos switch (bits (this_instr, 21, 24))
735 1.1 christos {
736 1.1 christos case 0x0: /*and */
737 1.1 christos result = operand1 & operand2;
738 1.1 christos break;
739 1.1 christos
740 1.1 christos case 0x1: /*eor */
741 1.1 christos result = operand1 ^ operand2;
742 1.1 christos break;
743 1.1 christos
744 1.1 christos case 0x2: /*sub */
745 1.1 christos result = operand1 - operand2;
746 1.1 christos break;
747 1.1 christos
748 1.1 christos case 0x3: /*rsb */
749 1.1 christos result = operand2 - operand1;
750 1.1 christos break;
751 1.1 christos
752 1.1 christos case 0x4: /*add */
753 1.1 christos result = operand1 + operand2;
754 1.1 christos break;
755 1.1 christos
756 1.1 christos case 0x5: /*adc */
757 1.1 christos result = operand1 + operand2 + c;
758 1.1 christos break;
759 1.1 christos
760 1.1 christos case 0x6: /*sbc */
761 1.1 christos result = operand1 - operand2 + c;
762 1.1 christos break;
763 1.1 christos
764 1.1 christos case 0x7: /*rsc */
765 1.1 christos result = operand2 - operand1 + c;
766 1.1 christos break;
767 1.1 christos
768 1.1 christos case 0x8:
769 1.1 christos case 0x9:
770 1.1 christos case 0xa:
771 1.1 christos case 0xb: /* tst, teq, cmp, cmn */
772 1.1 christos result = (unsigned long) nextpc;
773 1.1 christos break;
774 1.1 christos
775 1.1 christos case 0xc: /*orr */
776 1.1 christos result = operand1 | operand2;
777 1.1 christos break;
778 1.1 christos
779 1.1 christos case 0xd: /*mov */
780 1.1 christos /* Always step into a function. */
781 1.1 christos result = operand2;
782 1.1 christos break;
783 1.1 christos
784 1.1 christos case 0xe: /*bic */
785 1.1 christos result = operand1 & ~operand2;
786 1.1 christos break;
787 1.1 christos
788 1.1 christos case 0xf: /*mvn */
789 1.1 christos result = ~operand2;
790 1.1 christos break;
791 1.1 christos }
792 1.1 christos nextpc = self->ops->addr_bits_remove (self, result);
793 1.1 christos break;
794 1.1 christos }
795 1.1 christos
796 1.1 christos case 0x4:
797 1.1 christos case 0x5: /* data transfer */
798 1.1 christos case 0x6:
799 1.1 christos case 0x7:
800 1.1 christos if (bits (this_instr, 25, 27) == 0x3 && bit (this_instr, 4) == 1)
801 1.1 christos {
802 1.1 christos /* Media instructions and architecturally undefined
803 1.1 christos instructions. */
804 1.1 christos break;
805 1.1 christos }
806 1.1 christos
807 1.1 christos if (bit (this_instr, 20))
808 1.1 christos {
809 1.1 christos /* load */
810 1.1 christos if (bits (this_instr, 12, 15) == 15)
811 1.1 christos {
812 1.1 christos /* rd == pc */
813 1.1 christos unsigned long rn;
814 1.1 christos unsigned long base;
815 1.1 christos
816 1.1 christos if (bit (this_instr, 22))
817 1.1 christos error (_("Invalid update to pc in instruction"));
818 1.1 christos
819 1.1 christos /* byte write to PC */
820 1.1 christos rn = bits (this_instr, 16, 19);
821 1.1 christos base = ((rn == ARM_PC_REGNUM)
822 1.1 christos ? (pc_val + 8)
823 1.1 christos : regcache_raw_get_unsigned (regcache, rn));
824 1.1 christos
825 1.1 christos if (bit (this_instr, 24))
826 1.1 christos {
827 1.1 christos /* pre-indexed */
828 1.1 christos int c = (status & FLAG_C) ? 1 : 0;
829 1.1 christos unsigned long offset =
830 1.1 christos (bit (this_instr, 25)
831 1.1 christos ? shifted_reg_val (regcache, this_instr, c,
832 1.1 christos pc_val, status)
833 1.1 christos : bits (this_instr, 0, 11));
834 1.1 christos
835 1.1 christos if (bit (this_instr, 23))
836 1.1 christos base += offset;
837 1.1 christos else
838 1.1 christos base -= offset;
839 1.1 christos }
840 1.1 christos nextpc
841 1.1 christos = (CORE_ADDR) self->ops->read_mem_uint ((CORE_ADDR) base,
842 1.1 christos 4, byte_order);
843 1.1 christos }
844 1.1 christos }
845 1.1 christos break;
846 1.1 christos
847 1.1 christos case 0x8:
848 1.1 christos case 0x9: /* block transfer */
849 1.1 christos if (bit (this_instr, 20))
850 1.1 christos {
851 1.1 christos /* LDM */
852 1.1 christos if (bit (this_instr, 15))
853 1.1 christos {
854 1.1 christos /* loading pc */
855 1.1 christos int offset = 0;
856 1.1 christos CORE_ADDR rn_val_offset = 0;
857 1.1 christos unsigned long rn_val
858 1.1 christos = regcache_raw_get_unsigned (regcache,
859 1.1 christos bits (this_instr, 16, 19));
860 1.1 christos
861 1.1 christos if (bit (this_instr, 23))
862 1.1 christos {
863 1.1 christos /* up */
864 1.1 christos unsigned long reglist = bits (this_instr, 0, 14);
865 1.1 christos offset = bitcount (reglist) * 4;
866 1.1 christos if (bit (this_instr, 24)) /* pre */
867 1.1 christos offset += 4;
868 1.1 christos }
869 1.1 christos else if (bit (this_instr, 24))
870 1.1 christos offset = -4;
871 1.1 christos
872 1.1 christos rn_val_offset = rn_val + offset;
873 1.1 christos nextpc = (CORE_ADDR) self->ops->read_mem_uint (rn_val_offset,
874 1.1 christos 4, byte_order);
875 1.1 christos }
876 1.1 christos }
877 1.1 christos break;
878 1.1 christos
879 1.1 christos case 0xb: /* branch & link */
880 1.1 christos case 0xa: /* branch */
881 1.1 christos {
882 1.1 christos nextpc = BranchDest (pc, this_instr);
883 1.1 christos break;
884 1.1 christos }
885 1.1 christos
886 1.1 christos case 0xc:
887 1.1 christos case 0xd:
888 1.1 christos case 0xe: /* coproc ops */
889 1.1 christos break;
890 1.1 christos case 0xf: /* SWI */
891 1.1 christos {
892 1.1 christos nextpc = self->ops->syscall_next_pc (self);
893 1.1 christos }
894 1.1 christos break;
895 1.1 christos
896 1.1 christos default:
897 1.1 christos error (_("Bad bit-field extraction"));
898 1.1 christos return next_pcs;
899 1.1 christos }
900 1.1 christos }
901 1.1 christos
902 1.1 christos VEC_safe_push (CORE_ADDR, next_pcs, nextpc);
903 1.1 christos return next_pcs;
904 1.1 christos }
905 1.1 christos
906 1.1 christos /* See arm-get-next-pcs.h. */
907 1.1 christos
908 1.1 christos VEC (CORE_ADDR) *
909 1.1 christos arm_get_next_pcs (struct arm_get_next_pcs *self)
910 1.1 christos {
911 1.1 christos VEC (CORE_ADDR) *next_pcs = NULL;
912 1.1 christos
913 1.1 christos if (self->ops->is_thumb (self))
914 1.1 christos {
915 1.1 christos next_pcs = thumb_deal_with_atomic_sequence_raw (self);
916 1.1 christos if (next_pcs == NULL)
917 1.1 christos next_pcs = thumb_get_next_pcs_raw (self);
918 1.1 christos }
919 1.1 christos else
920 1.1 christos {
921 1.1 christos next_pcs = arm_deal_with_atomic_sequence_raw (self);
922 1.1 christos if (next_pcs == NULL)
923 1.1 christos next_pcs = arm_get_next_pcs_raw (self);
924 1.1 christos }
925 1.1 christos
926 1.1 christos if (self->ops->fixup != NULL)
927 1.1 christos {
928 1.1 christos CORE_ADDR nextpc;
929 1.1 christos int i;
930 1.1 christos
931 1.1 christos for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, nextpc); i++)
932 1.1 christos {
933 1.1 christos nextpc = self->ops->fixup (self, nextpc);
934 1.1 christos VEC_replace (CORE_ADDR, next_pcs, i, nextpc);
935 1.1 christos }
936 1.1 christos }
937 1.1 christos return next_pcs;
938 1.1 christos }
939