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