b718e4c88833ab2085866c262f0422d5a55118ac
[deliverable/binutils-gdb.git] / gdb / sparc-tdep.c
1 /* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This code is for the sparc cpu.
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include <stdio.h>
23 #include "defs.h"
24 #include "param.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "obstack.h"
28 #include "signame.h"
29 #include "target.h"
30 #include "ieee-float.h"
31
32 #include <sys/ptrace.h>
33
34 #include "gdbcore.h"
35
36 /* From infrun.c */
37 extern int stop_after_trap;
38
39 typedef enum
40 {
41 Error, not_branch, bicc, bicca, ba, baa, ticc, ta,
42 } branch_type;
43
44 /* Simulate single-step ptrace call for sun4. Code written by Gary
45 Beihl (beihl@mcc.com). */
46
47 /* npc4 and next_pc describe the situation at the time that the
48 step-breakpoint was set, not necessary the current value of NPC_REGNUM. */
49 static CORE_ADDR next_pc, npc4, target;
50 static int brknpc4, brktrg;
51 typedef char binsn_quantum[BREAKPOINT_MAX];
52 static binsn_quantum break_mem[3];
53
54 /* Non-zero if we just simulated a single-step ptrace call. This is
55 needed because we cannot remove the breakpoints in the inferior
56 process until after the `wait' in `wait_for_inferior'. Used for
57 sun4. */
58
59 int one_stepped;
60
61 /* single_step() is called just before we want to resume the inferior,
62 if we want to single-step it but there is no hardware or kernel single-step
63 support (as on all SPARCs). We find all the possible targets of the
64 coming instruction and breakpoint them.
65
66 single_step is also called just after the inferior stops. If we had
67 set up a simulated single-step, we undo our damage. */
68
69 void
70 single_step ()
71 {
72 branch_type br, isannulled();
73 CORE_ADDR pc;
74 long pc_instruction;
75
76 if (!one_stepped)
77 {
78 /* Always set breakpoint for NPC. */
79 next_pc = read_register (NPC_REGNUM);
80 npc4 = next_pc + 4; /* branch not taken */
81
82 target_insert_breakpoint (next_pc, break_mem[0]);
83 /* printf ("set break at %x\n",next_pc); */
84
85 pc = read_register (PC_REGNUM);
86 pc_instruction = read_memory_integer (pc, sizeof(pc_instruction));
87 br = isannulled (pc_instruction, pc, &target);
88 brknpc4 = brktrg = 0;
89
90 if (br == bicca)
91 {
92 /* Conditional annulled branch will either end up at
93 npc (if taken) or at npc+4 (if not taken).
94 Trap npc+4. */
95 brknpc4 = 1;
96 target_insert_breakpoint (npc4, break_mem[1]);
97 }
98 else if (br == baa && target != next_pc)
99 {
100 /* Unconditional annulled branch will always end up at
101 the target. */
102 brktrg = 1;
103 target_insert_breakpoint (target, break_mem[2]);
104 }
105
106 /* We are ready to let it go */
107 one_stepped = 1;
108 return;
109 }
110 else
111 {
112 /* Remove breakpoints */
113 target_remove_breakpoint (next_pc, break_mem[0]);
114
115 if (brknpc4)
116 target_remove_breakpoint (npc4, break_mem[1]);
117
118 if (brktrg)
119 target_remove_breakpoint (target, break_mem[2]);
120
121 one_stepped = 0;
122 }
123 }
124 \f
125 CORE_ADDR
126 sparc_frame_chain (thisframe)
127 FRAME thisframe;
128 {
129 CORE_ADDR retval;
130 int err;
131 err = target_read_memory
132 ((CORE_ADDR)&(((struct rwindow *)(thisframe->frame))->rw_in[6]),
133 &retval,
134 sizeof (CORE_ADDR));
135 if (err)
136 return 0;
137 return retval;
138 }
139
140 CORE_ADDR
141 sparc_extract_struct_value_address (regbuf)
142 char regbuf[REGISTER_BYTES];
143 {
144 CORE_ADDR retval;
145 read_memory (((int *)(regbuf))[SP_REGNUM]+(16*4),
146 &retval,
147 sizeof (CORE_ADDR));
148 return retval;
149 }
150
151 /*
152 * Find the pc saved in frame FRAME.
153 */
154 CORE_ADDR
155 frame_saved_pc (frame)
156 FRAME frame;
157 {
158 CORE_ADDR prev_pc;
159
160 /* If it's at the bottom, the return value's stored in i7/rp */
161 if (get_current_frame () == frame)
162 read_memory ((CORE_ADDR)&((struct rwindow *)
163 (read_register (SP_REGNUM)))->rw_in[7],
164 &prev_pc, sizeof (CORE_ADDR));
165 else
166 /* Wouldn't this always work? */
167 read_memory ((CORE_ADDR)&((struct rwindow *)(frame->bottom))->rw_in[7],
168 &prev_pc,
169 sizeof (CORE_ADDR));
170
171 return PC_ADJUST (prev_pc);
172 }
173
174 /*
175 * Since an individual frame in the frame cache is defined by two
176 * arguments (a frame pointer and a stack pointer), we need two
177 * arguments to get info for an arbitrary stack frame. This routine
178 * takes two arguments and makes the cached frames look as if these
179 * two arguments defined a frame on the cache. This allows the rest
180 * of info frame to extract the important arguments without
181 * difficulty.
182 */
183 FRAME
184 setup_arbitrary_frame (frame, stack)
185 FRAME_ADDR frame, stack;
186 {
187 FRAME fid = create_new_frame (frame, 0);
188
189 if (!fid)
190 fatal ("internal: create_new_frame returned invalid frame id");
191
192 fid->bottom = stack;
193 fid->pc = FRAME_SAVED_PC (fid);
194 return fid;
195 }
196
197 /* This code was written by Gary Beihl (beihl@mcc.com).
198 It was modified by Michael Tiemann (tiemann@corto.inria.fr). */
199
200 /*
201 * This routine appears to be passed a size by which to increase the
202 * stack. It then executes a save instruction in the inferior to
203 * increase the stack by this amount. Only the register window system
204 * should be affected by this; the program counter & etc. will not be.
205 *
206 * This instructions used for this purpose are:
207 *
208 * sethi %hi(0x0),g1 *
209 * add g1,0x1ee0,g1 *
210 * save sp,g1,sp
211 * sethi %hi(0x0),g1 *
212 * add g1,0x1ee0,g1 *
213 * t g0,0x1,o0
214 * sethi %hi(0x0),g0 (nop)
215 *
216 * I presume that these set g1 to be the negative of the size, do a
217 * save (putting the stack pointer at sp - size) and restore the
218 * original contents of g1. A * indicates that the actual value of
219 * the instruction is modified below.
220 */
221 static int save_insn_opcodes[] = {
222 0x03000000, 0x82007ee0, 0x9de38001, 0x03000000,
223 0x82007ee0, 0x91d02001, 0x01000000 };
224
225 /* Neither do_save_insn or do_restore_insn save stack configuration
226 (current_frame, etc),
227 since the stack is in an indeterminate state through the call to
228 each of them. That responsibility of the routine which calls them. */
229
230 static void
231 do_save_insn (size)
232 int size;
233 {
234 int g1 = read_register (G1_REGNUM);
235 CORE_ADDR sp = read_register (SP_REGNUM);
236 CORE_ADDR pc = read_register (PC_REGNUM);
237 CORE_ADDR npc = read_register (NPC_REGNUM);
238 CORE_ADDR fake_pc = sp - sizeof (save_insn_opcodes);
239 struct inferior_status inf_status;
240
241 save_inferior_status (&inf_status, 0); /* Don't restore stack info */
242 /*
243 * See above.
244 */
245 save_insn_opcodes[0] = 0x03000000 | ((-size >> 10) & 0x3fffff);
246 save_insn_opcodes[1] = 0x82006000 | (-size & 0x3ff);
247 save_insn_opcodes[3] = 0x03000000 | ((g1 >> 10) & 0x3fffff);
248 save_insn_opcodes[4] = 0x82006000 | (g1 & 0x3ff);
249 write_memory (fake_pc, (char *)save_insn_opcodes, sizeof (save_insn_opcodes));
250
251 clear_proceed_status ();
252 stop_after_trap = 1;
253 proceed (fake_pc, 0, 0);
254
255 write_register (PC_REGNUM, pc);
256 write_register (NPC_REGNUM, npc);
257 restore_inferior_status (&inf_status);
258 }
259
260 /*
261 * This routine takes a program counter value. It restores the
262 * register window system to the frame above the current one.
263 * THIS ROUTINE CLOBBERS PC AND NPC IN THE TARGET!
264 */
265
266 /* The following insns translate to:
267
268 restore %g0,%g0,%g0
269 t %g0,1
270 sethi %hi(0),%g0 */
271
272 static int restore_insn_opcodes[] = { 0x81e80000, 0x91d02001, 0x01000000 };
273
274 static void
275 do_restore_insn ()
276 {
277 CORE_ADDR sp = read_register (SP_REGNUM);
278 CORE_ADDR fake_pc = sp - sizeof (restore_insn_opcodes);
279 struct inferior_status inf_status;
280
281 save_inferior_status (&inf_status, 0); /* Don't restore stack info */
282
283 write_memory (fake_pc, (char *)restore_insn_opcodes,
284 sizeof (restore_insn_opcodes));
285
286 clear_proceed_status ();
287 stop_after_trap = 1;
288 proceed (fake_pc, 0, 0);
289
290 restore_inferior_status (&inf_status);
291 }
292
293 /* Given a pc value, skip it forward past the function prologue by
294 disassembling instructions that appear to be a prologue.
295
296 If FRAMELESS_P is set, we are only testing to see if the function
297 is frameless. This allows a quicker answer.
298
299 This routine should be more specific in its actions; making sure
300 that it uses the same register in the initial prologue section. */
301 CORE_ADDR
302 skip_prologue (start_pc, frameless_p)
303 CORE_ADDR start_pc;
304 int frameless_p;
305 {
306 union
307 {
308 unsigned long int code;
309 struct
310 {
311 unsigned int op:2;
312 unsigned int rd:5;
313 unsigned int op2:3;
314 unsigned int imm22:22;
315 } sethi;
316 struct
317 {
318 unsigned int op:2;
319 unsigned int rd:5;
320 unsigned int op3:6;
321 unsigned int rs1:5;
322 unsigned int i:1;
323 unsigned int simm13:13;
324 } add;
325 int i;
326 } x;
327 int dest = -1;
328 CORE_ADDR pc = start_pc;
329
330 x.i = read_memory_integer (pc, 4);
331
332 /* Recognize the `sethi' insn and record its destination. */
333 if (x.sethi.op == 0 && x.sethi.op2 == 4)
334 {
335 dest = x.sethi.rd;
336 pc += 4;
337 x.i = read_memory_integer (pc, 4);
338 }
339
340 /* Recognize an add immediate value to register to either %g1 or
341 the destination register recorded above. Actually, this might
342 well recognize several different arithmetic operations.
343 It doesn't check that rs1 == rd because in theory "sub %g0, 5, %g1"
344 followed by "save %sp, %g1, %sp" is a valid prologue (Not that
345 I imagine any compiler really does that, however). */
346 if (x.add.op == 2 && x.add.i && (x.add.rd == 1 || x.add.rd == dest))
347 {
348 pc += 4;
349 x.i = read_memory_integer (pc, 4);
350 }
351
352 /* This recognizes any SAVE insn. But why do the XOR and then
353 the compare? That's identical to comparing against 60 (as long
354 as there isn't any sign extension). */
355 if (x.add.op == 2 && (x.add.op3 ^ 32) == 28)
356 {
357 pc += 4;
358 if (frameless_p) /* If the save is all we care about, */
359 return pc; /* return before doing more work */
360 x.i = read_memory_integer (pc, 4);
361 }
362 else
363 {
364 /* Without a save instruction, it's not a prologue. */
365 return start_pc;
366 }
367
368 /* Now we need to recognize stores into the frame from the input
369 registers. This recognizes all non alternate stores of input
370 register, into a location offset from the frame pointer. */
371 while (x.add.op == 3
372 && (x.add.op3 & 0x3c) == 4 /* Store, non-alternate. */
373 && (x.add.rd & 0x18) == 0x18 /* Input register. */
374 && x.add.i /* Immediate mode. */
375 && x.add.rs1 == 30 /* Off of frame pointer. */
376 /* Into reserved stack space. */
377 && x.add.simm13 >= 0x44
378 && x.add.simm13 < 0x5b)
379 {
380 pc += 4;
381 x.i = read_memory_integer (pc, 4);
382 }
383 return pc;
384 }
385
386 /* Check instruction at ADDR to see if it is an annulled branch.
387 All other instructions will go to NPC or will trap.
388 Set *TARGET if we find a canidate branch; set to zero if not. */
389
390 branch_type
391 isannulled (instruction, addr, target)
392 long instruction;
393 CORE_ADDR addr, *target;
394 {
395 branch_type val = not_branch;
396 long int offset; /* Must be signed for sign-extend. */
397 union
398 {
399 unsigned long int code;
400 struct
401 {
402 unsigned int op:2;
403 unsigned int a:1;
404 unsigned int cond:4;
405 unsigned int op2:3;
406 unsigned int disp22:22;
407 } b;
408 } insn;
409
410 *target = 0;
411 insn.code = instruction;
412
413 if (insn.b.op == 0
414 && (insn.b.op2 == 2 || insn.b.op2 == 6 || insn.b.op2 == 7))
415 {
416 if (insn.b.cond == 8)
417 val = insn.b.a ? baa : ba;
418 else
419 val = insn.b.a ? bicca : bicc;
420 offset = 4 * ((int) (insn.b.disp22 << 10) >> 10);
421 *target = addr + offset;
422 }
423
424 return val;
425 }
426
427 /* sparc_frame_find_saved_regs ()
428
429 Stores, into a struct frame_saved_regs,
430 the addresses of the saved registers of frame described by FRAME_INFO.
431 This includes special registers such as pc and fp saved in special
432 ways in the stack frame. sp is even more special:
433 the address we return for it IS the sp for the next frame.
434
435 Note that on register window machines, we are currently making the
436 assumption that window registers are being saved somewhere in the
437 frame in which they are being used. If they are stored in an
438 inferior frame, find_saved_register will break.
439
440 On the Sun 4, the only time all registers are saved is when
441 a dummy frame is involved. Otherwise, the only saved registers
442 are the LOCAL and IN registers which are saved as a result
443 of the "save/restore" opcodes. This condition is determined
444 by address rather than by value.
445
446 The "pc" is not stored in a frame on the SPARC. (What is stored
447 is a return address minus 8.) sparc_pop_frame knows how to
448 deal with that. Other routines might or might not.
449
450 See tm-sparc.h (PUSH_FRAME and friends) for CRITICAL information
451 about how this works. */
452
453 void
454 sparc_frame_find_saved_regs (fi, saved_regs_addr)
455 struct frame_info *fi;
456 struct frame_saved_regs *saved_regs_addr;
457 {
458 register int regnum;
459 FRAME_ADDR frame = read_register (FP_REGNUM);
460 FRAME fid = FRAME_INFO_ID (fi);
461
462 if (!fid)
463 fatal ("Bad frame info struct in FRAME_FIND_SAVED_REGS");
464
465 bzero (saved_regs_addr, sizeof (*saved_regs_addr));
466
467 /* Old test.
468 if (fi->pc >= frame - CALL_DUMMY_LENGTH - 0x140
469 && fi->pc <= frame) */
470
471 if (fi->pc >= (fi->bottom ? fi->bottom :
472 read_register (SP_REGNUM))
473 && fi->pc <= FRAME_FP(fi))
474 {
475 /* Dummy frame. All but the window regs are in there somewhere. */
476 for (regnum = G1_REGNUM; regnum < G1_REGNUM+7; regnum++)
477 saved_regs_addr->regs[regnum] =
478 frame + (regnum - G0_REGNUM) * 4 - 0xa0;
479 for (regnum = I0_REGNUM; regnum < I0_REGNUM+8; regnum++)
480 saved_regs_addr->regs[regnum] =
481 frame + (regnum - I0_REGNUM) * 4 - 0xc0;
482 for (regnum = FP0_REGNUM; regnum < FP0_REGNUM + 32; regnum++)
483 saved_regs_addr->regs[regnum] =
484 frame + (regnum - FP0_REGNUM) * 4 - 0x80;
485 for (regnum = Y_REGNUM; regnum < NUM_REGS; regnum++)
486 saved_regs_addr->regs[regnum] =
487 frame + (regnum - Y_REGNUM) * 4 - 0xe0;
488 frame = fi->bottom ?
489 fi->bottom : read_register (SP_REGNUM);
490 }
491 else
492 {
493 /* Normal frame. Just Local and In registers */
494 frame = fi->bottom ?
495 fi->bottom : read_register (SP_REGNUM);
496 for (regnum = L0_REGNUM; regnum < L0_REGNUM+16; regnum++)
497 saved_regs_addr->regs[regnum] = frame + (regnum-L0_REGNUM) * 4;
498 }
499 if (fi->next)
500 {
501 /* Pull off either the next frame pointer or the stack pointer */
502 FRAME_ADDR next_next_frame =
503 (fi->next->bottom ?
504 fi->next->bottom :
505 read_register (SP_REGNUM));
506 for (regnum = O0_REGNUM; regnum < O0_REGNUM+8; regnum++)
507 saved_regs_addr->regs[regnum] = next_next_frame + regnum * 4;
508 }
509 /* Otherwise, whatever we would get from ptrace(GETREGS) is accurate */
510 saved_regs_addr->regs[SP_REGNUM] = FRAME_FP (fi);
511 }
512
513 /* Push an empty stack frame, and record in it the current PC, regs, etc.
514
515 Note that the write's are of registers in the context of the newly
516 pushed frame. Thus the the fp*'s, the g*'s, the i*'s, and
517 the randoms, of the new frame, are being saved. The locals and outs
518 are new; they don't need to be saved. The i's and l's of
519 the last frame were saved by the do_save_insn in the register
520 file (now on the stack, since a context switch happended imm after).
521
522 The return pointer register %i7 does not have
523 the pc saved into it (return from this frame will be accomplished
524 by a POP_FRAME). In fact, we must leave it unclobbered, since we
525 must preserve it in the calling routine except across call instructions. */
526
527 /* Definitely see tm-sparc.h for more doc of the frame format here. */
528
529 void
530 sparc_push_dummy_frame ()
531 {
532 CORE_ADDR fp;
533 char register_temp[REGISTER_BYTES];
534
535 do_save_insn (0x140); /* FIXME where does this value come from? */
536 fp = read_register (FP_REGNUM);
537
538 read_register_bytes (REGISTER_BYTE (FP0_REGNUM), register_temp, 32 * 4);
539 write_memory (fp - 0x80, register_temp, 32 * 4);
540
541 read_register_bytes (REGISTER_BYTE (G0_REGNUM), register_temp, 8 * 4);
542 write_memory (fp - 0xa0, register_temp, 8 * 4);
543
544 read_register_bytes (REGISTER_BYTE (I0_REGNUM), register_temp, 8 * 4);
545 write_memory (fp - 0xc0, register_temp, 8 * 4);
546
547 /* Y, PS, WIM, TBR, PC, NPC, FPS, CPS regs */
548 read_register_bytes (REGISTER_BYTE (Y_REGNUM), register_temp, 8 * 4);
549 write_memory (fp - 0xe0, register_temp, 8 * 4);
550 }
551
552 /* Discard from the stack the innermost frame, restoring all saved registers.
553
554 Note that the values stored in fsr by get_frame_saved_regs are *in
555 the context of the called frame*. What this means is that the i
556 regs of fsr must be restored into the o regs of the (calling) frame that
557 we pop into. We don't care about the output regs of the calling frame,
558 since unless it's a dummy frame, it won't have any output regs in it.
559
560 We never have to bother with %l (local) regs, since the called routine's
561 locals get tossed, and the calling routine's locals are already saved
562 on its stack. */
563
564 /* Definitely see tm-sparc.h for more doc of the frame format here. */
565
566 void
567 sparc_pop_frame ()
568 {
569 register FRAME frame = get_current_frame ();
570 register CORE_ADDR pc;
571 struct frame_saved_regs fsr;
572 struct frame_info *fi;
573 char raw_buffer[REGISTER_BYTES];
574
575 fi = get_frame_info (frame);
576 get_frame_saved_regs (fi, &fsr);
577 do_restore_insn ();
578 if (fsr.regs[FP0_REGNUM])
579 {
580 read_memory (fsr.regs[FP0_REGNUM], raw_buffer, 32 * 4);
581 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), raw_buffer, 32 * 4);
582 }
583 if (fsr.regs[G1_REGNUM])
584 {
585 read_memory (fsr.regs[G1_REGNUM], raw_buffer, 7 * 4);
586 write_register_bytes (REGISTER_BYTE (G1_REGNUM), raw_buffer, 7 * 4);
587 }
588 if (fsr.regs[I0_REGNUM])
589 {
590 read_memory (fsr.regs[I0_REGNUM], raw_buffer, 8 * 4);
591 write_register_bytes (REGISTER_BYTE (O0_REGNUM), raw_buffer, 8 * 4);
592 }
593 if (fsr.regs[PS_REGNUM])
594 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
595 if (fsr.regs[Y_REGNUM])
596 write_register (Y_REGNUM, read_memory_integer (fsr.regs[Y_REGNUM], 4));
597 if (fsr.regs[PC_REGNUM])
598 {
599 /* Explicitly specified PC (and maybe NPC) -- just restore them. */
600 write_register (PC_REGNUM, read_memory_integer (fsr.regs[PC_REGNUM], 4));
601 if (fsr.regs[NPC_REGNUM])
602 write_register (NPC_REGNUM,
603 read_memory_integer (fsr.regs[NPC_REGNUM], 4));
604 }
605 else if (fsr.regs[I7_REGNUM])
606 {
607 /* Return address in %i7 -- adjust it, then restore PC and NPC from it */
608 pc = PC_ADJUST (read_memory_integer (fsr.regs[I7_REGNUM], 4));
609 write_register (PC_REGNUM, pc);
610 write_register (NPC_REGNUM, pc + 4);
611 }
612 flush_cached_frames ();
613 set_current_frame ( create_new_frame (read_register (FP_REGNUM),
614 read_pc ()));
615 }
616
617 /* Structure of SPARC extended floating point numbers.
618 This information is not currently used by GDB, since no current SPARC
619 implementations support extended float. */
620
621 const struct ext_format ext_format_sparc[] = {
622 /* tot sbyte smask expbyte manbyte */
623 { 16, 0, 0x80, 0,1, 4,8 }, /* sparc */
624 };
This page took 0.042353 seconds and 4 git commands to generate.