1 /* Target dependent code for the Motorola 68000 series.
2 Copyright (C) 1990, 1992, 1993, 1994, 1995, 1996, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "gdb_string.h"
31 #define P_LINKL_FP 0x480e
32 #define P_LINKW_FP 0x4e56
33 #define P_PEA_FP 0x4856
34 #define P_MOVL_SP_FP 0x2c4f
39 #define P_MOVML 0x48ef
40 #define P_FMOVM 0xf237
43 /* The only reason this is here is the tm-altos.h reference below. It
44 was moved back here from tm-m68k.h. FIXME? */
47 altos_skip_prologue (pc)
50 register int op = read_memory_integer (pc, 2);
52 pc += 4; /* Skip link #word */
53 else if (op == P_LINKL_FP)
54 pc += 6; /* Skip link #long */
55 /* Not sure why branches are here. */
56 /* From tm-isi.h, tm-altos.h */
57 else if (op == 0060000)
58 pc += 4; /* Skip bra #word */
59 else if (op == 00600377)
60 pc += 6; /* skip bra #long */
61 else if ((op & 0177400) == 0060000)
62 pc += 2; /* skip bra #char */
66 /* The only reason this is here is the tm-isi.h reference below. It
67 was moved back here from tm-m68k.h. FIXME? */
70 isi_skip_prologue (pc)
73 register int op = read_memory_integer (pc, 2);
75 pc += 4; /* Skip link #word */
76 else if (op == P_LINKL_FP)
77 pc += 6; /* Skip link #long */
78 /* Not sure why branches are here. */
79 /* From tm-isi.h, tm-altos.h */
80 else if (op == 0060000)
81 pc += 4; /* Skip bra #word */
82 else if (op == 00600377)
83 pc += 6; /* skip bra #long */
84 else if ((op & 0177400) == 0060000)
85 pc += 2; /* skip bra #char */
90 delta68_in_sigtramp (pc, name)
94 return strcmp (name, "_sigcode") == 0;
98 delta68_frame_args_address (frame_info)
99 struct frame_info * frame_info;
101 /* we assume here that the only frameless functions are the system calls
102 or other functions who do not put anything on the stack. */
103 if (frame_info->signal_handler_caller)
104 return frame_info->frame + 12;
105 else if (frameless_look_for_prologue (frame_info))
107 /* Check for an interrupted system call */
108 if (frame_info->next && frame_info->next->signal_handler_caller)
109 return frame_info->next->frame + 16;
111 return frame_info->frame + 4;
114 return frame_info->frame;
118 delta68_frame_saved_pc (frame_info)
119 struct frame_info * frame_info;
121 return read_memory_integer (delta68_frame_args_address (frame_info) + 4, 4);
124 /* Return number of args passed to a frame.
125 Can return -1, meaning no way to tell. */
128 isi_frame_num_args (fi)
129 struct frame_info *fi;
132 CORE_ADDR pc = FRAME_SAVED_PC (fi);
133 int insn = 0177777 & read_memory_integer (pc, 2);
135 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
136 val = read_memory_integer (pc + 2, 2);
137 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
138 || (insn & 0170777) == 0050117) /* addqw */
140 val = (insn >> 9) & 7;
144 else if (insn == 0157774) /* addal #WW, sp */
145 val = read_memory_integer (pc + 2, 4);
151 delta68_frame_num_args (fi)
152 struct frame_info *fi;
155 CORE_ADDR pc = FRAME_SAVED_PC (fi);
156 int insn = 0177777 & read_memory_integer (pc, 2);
158 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
159 val = read_memory_integer (pc + 2, 2);
160 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
161 || (insn & 0170777) == 0050117) /* addqw */
163 val = (insn >> 9) & 7;
167 else if (insn == 0157774) /* addal #WW, sp */
168 val = read_memory_integer (pc + 2, 4);
174 news_frame_num_args (fi)
175 struct frame_info *fi;
178 CORE_ADDR pc = FRAME_SAVED_PC (fi);
179 int insn = 0177777 & read_memory_integer (pc, 2);
181 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
182 val = read_memory_integer (pc + 2, 2);
183 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
184 || (insn & 0170777) == 0050117) /* addqw */
186 val = (insn >> 9) & 7;
190 else if (insn == 0157774) /* addal #WW, sp */
191 val = read_memory_integer (pc + 2, 4);
196 /* Push an empty stack frame, to record the current PC, etc. */
199 m68k_push_dummy_frame ()
201 register CORE_ADDR sp = read_register (SP_REGNUM);
205 sp = push_word (sp, read_register (PC_REGNUM));
206 sp = push_word (sp, read_register (FP_REGNUM));
207 write_register (FP_REGNUM, sp);
209 /* Always save the floating-point registers, whether they exist on
210 this target or not. */
211 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
213 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
214 sp = push_bytes (sp, raw_buffer, 12);
217 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
219 sp = push_word (sp, read_register (regnum));
221 sp = push_word (sp, read_register (PS_REGNUM));
222 write_register (SP_REGNUM, sp);
225 /* Discard from the stack the innermost frame,
226 restoring all saved registers. */
231 register struct frame_info *frame = get_current_frame ();
232 register CORE_ADDR fp;
234 struct frame_saved_regs fsr;
237 fp = FRAME_FP (frame);
238 get_frame_saved_regs (frame, &fsr);
239 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
241 if (fsr.regs[regnum])
243 read_memory (fsr.regs[regnum], raw_buffer, 12);
244 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
247 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
249 if (fsr.regs[regnum])
251 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
254 if (fsr.regs[PS_REGNUM])
256 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
258 write_register (FP_REGNUM, read_memory_integer (fp, 4));
259 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
260 write_register (SP_REGNUM, fp + 8);
261 flush_cached_frames ();
265 /* Given an ip value corresponding to the start of a function,
266 return the ip of the first instruction after the function
267 prologue. This is the generic m68k support. Machines which
268 require something different can override the SKIP_PROLOGUE
269 macro to point elsewhere.
271 Some instructions which typically may appear in a function
274 A link instruction, word form:
276 link.w %a6,&0 4e56 XXXX
278 A link instruction, long form:
280 link.l %fp,&F%1 480e XXXX XXXX
282 A movm instruction to preserve integer regs:
284 movm.l &M%1,(4,%sp) 48ef XXXX XXXX
286 A fmovm instruction to preserve float regs:
288 fmovm &FPM%1,(FPO%1,%sp) f237 XXXX XXXX XXXX XXXX
290 Some profiling setup code (FIXME, not recognized yet):
292 lea.l (.L3,%pc),%a1 43fb XXXX XXXX XXXX
293 bsr _mcount 61ff XXXX XXXX
298 m68k_skip_prologue (ip)
301 register CORE_ADDR limit;
302 struct symtab_and_line sal;
305 /* Find out if there is a known limit for the extent of the prologue.
306 If so, ensure we don't go past it. If not, assume "infinity". */
308 sal = find_pc_line (ip, 0);
309 limit = (sal.end) ? sal.end : (CORE_ADDR) ~ 0;
313 op = read_memory_integer (ip, 2);
316 if (op == P_LINKW_FP)
317 ip += 4; /* Skip link.w */
318 else if (op == P_PEA_FP)
319 ip += 2; /* Skip pea %fp */
320 else if (op == P_MOVL_SP_FP)
321 ip += 2; /* Skip move.l %sp, %fp */
322 else if (op == P_LINKL_FP)
323 ip += 6; /* Skip link.l */
324 else if (op == P_MOVML)
325 ip += 6; /* Skip movm.l */
326 else if (op == P_FMOVM)
327 ip += 10; /* Skip fmovm */
329 break; /* Found unknown code, bail out. */
335 m68k_find_saved_regs (frame_info, saved_regs)
336 struct frame_info *frame_info;
337 struct frame_saved_regs *saved_regs;
340 register int regmask;
341 register CORE_ADDR next_addr;
342 register CORE_ADDR pc;
344 /* First possible address for a pc in a call dummy for this frame. */
345 CORE_ADDR possible_call_dummy_start =
346 (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4 - 8 * 12;
349 memset (saved_regs, 0, sizeof (*saved_regs));
350 if ((frame_info)->pc >= possible_call_dummy_start
351 && (frame_info)->pc <= (frame_info)->frame)
354 /* It is a call dummy. We could just stop now, since we know
355 what the call dummy saves and where. But this code proceeds
356 to parse the "prologue" which is part of the call dummy.
357 This is needlessly complex and confusing. FIXME. */
359 next_addr = (frame_info)->frame;
360 pc = possible_call_dummy_start;
364 pc = get_pc_function_start ((frame_info)->pc);
366 nextinsn = read_memory_integer (pc, 2);
367 if (P_PEA_FP == nextinsn
368 && P_MOVL_SP_FP == read_memory_integer (pc + 2, 2))
372 next_addr = frame_info->frame;
375 else if (P_LINKL_FP == nextinsn)
377 /* Find the address above the saved
378 regs using the amount of storage from the link instruction. */
380 next_addr = (frame_info)->frame + read_memory_integer (pc + 2, 4);
383 else if (P_LINKW_FP == nextinsn)
385 /* Find the address above the saved
386 regs using the amount of storage from the link instruction. */
388 next_addr = (frame_info)->frame + read_memory_integer (pc + 2, 2);
394 /* If have an addal #-n, sp next, adjust next_addr. */
395 if ((0177777 & read_memory_integer (pc, 2)) == 0157774)
396 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
401 nextinsn = 0xffff & read_memory_integer (pc, 2);
402 regmask = read_memory_integer (pc + 2, 2);
403 /* fmovemx to -(sp) */
404 if (0xf227 == nextinsn && (regmask & 0xff00) == 0xe000)
406 /* Regmask's low bit is for register fp7, the first pushed */
407 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
409 saved_regs->regs[regnum] = (next_addr -= 12);
412 /* fmovemx to (fp + displacement) */
413 else if (0171056 == nextinsn && (regmask & 0xff00) == 0xf000)
415 register CORE_ADDR addr;
417 addr = (frame_info)->frame + read_memory_integer (pc + 4, 2);
418 /* Regmask's low bit is for register fp7, the first pushed */
419 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
422 saved_regs->regs[regnum] = addr;
428 else if (0044327 == nextinsn)
430 /* Regmask's low bit is for register 0, the first written */
431 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
434 saved_regs->regs[regnum] = next_addr;
439 /* moveml to (fp + displacement) */
440 else if (0044356 == nextinsn)
442 register CORE_ADDR addr;
444 addr = (frame_info)->frame + read_memory_integer (pc + 4, 2);
445 /* Regmask's low bit is for register 0, the first written */
446 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
449 saved_regs->regs[regnum] = addr;
454 /* moveml to -(sp) */
455 else if (0044347 == nextinsn)
457 /* Regmask's low bit is for register 15, the first pushed */
458 for (regnum = 16; --regnum >= 0; regmask >>= 1)
460 saved_regs->regs[regnum] = (next_addr -= 4);
464 else if (0x2f00 == (0xfff0 & nextinsn))
466 regnum = 0xf & nextinsn;
467 saved_regs->regs[regnum] = (next_addr -= 4);
470 /* fmovemx to index of sp */
471 else if (0xf236 == nextinsn && (regmask & 0xff00) == 0xf000)
473 /* Regmask's low bit is for register fp0, the first written */
474 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
477 saved_regs->regs[regnum] = next_addr;
482 /* clrw -(sp); movw ccr,-(sp) */
483 else if (0x4267 == nextinsn && 0x42e7 == regmask)
485 saved_regs->regs[PS_REGNUM] = (next_addr -= 4);
492 saved_regs->regs[SP_REGNUM] = (frame_info)->frame + 8;
493 saved_regs->regs[FP_REGNUM] = (frame_info)->frame;
494 saved_regs->regs[PC_REGNUM] = (frame_info)->frame + 4;
495 #ifdef SIG_SP_FP_OFFSET
496 /* Adjust saved SP_REGNUM for fake _sigtramp frames. */
497 if (frame_info->signal_handler_caller && frame_info->next)
498 saved_regs->regs[SP_REGNUM] = frame_info->next->frame + SIG_SP_FP_OFFSET;
503 #ifdef USE_PROC_FS /* Target dependent support for /proc */
505 #include <sys/procfs.h>
507 /* The /proc interface divides the target machine's register set up into
508 two different sets, the general register set (gregset) and the floating
509 point register set (fpregset). For each set, there is an ioctl to get
510 the current register set and another ioctl to set the current values.
512 The actual structure passed through the ioctl interface is, of course,
513 naturally machine dependent, and is different for each set of registers.
514 For the m68k for example, the general register set is typically defined
517 typedef int gregset_t[18];
523 and the floating point set by:
525 typedef struct fpregset {
529 int f_fpregs[8][3]; (8 regs, 96 bits each)
532 These routines provide the packing and unpacking of gregset_t and
533 fpregset_t formatted data.
537 /* Atari SVR4 has R_SR but not R_PS */
539 #if !defined (R_PS) && defined (R_SR)
543 /* Given a pointer to a general register set in /proc format (gregset_t *),
544 unpack the register contents and supply them as gdb's idea of the current
548 supply_gregset (gregsetp)
552 register greg_t *regp = (greg_t *) gregsetp;
554 for (regi = 0; regi < R_PC; regi++)
556 supply_register (regi, (char *) (regp + regi));
558 supply_register (PS_REGNUM, (char *) (regp + R_PS));
559 supply_register (PC_REGNUM, (char *) (regp + R_PC));
563 fill_gregset (gregsetp, regno)
568 register greg_t *regp = (greg_t *) gregsetp;
570 for (regi = 0; regi < R_PC; regi++)
572 if ((regno == -1) || (regno == regi))
574 *(regp + regi) = *(int *) ®isters[REGISTER_BYTE (regi)];
577 if ((regno == -1) || (regno == PS_REGNUM))
579 *(regp + R_PS) = *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)];
581 if ((regno == -1) || (regno == PC_REGNUM))
583 *(regp + R_PC) = *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
587 #if defined (FP0_REGNUM)
589 /* Given a pointer to a floating point register set in /proc format
590 (fpregset_t *), unpack the register contents and supply them as gdb's
591 idea of the current floating point register values. */
594 supply_fpregset (fpregsetp)
595 fpregset_t *fpregsetp;
600 for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
602 from = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
603 supply_register (regi, from);
605 supply_register (FPC_REGNUM, (char *) &(fpregsetp->f_pcr));
606 supply_register (FPS_REGNUM, (char *) &(fpregsetp->f_psr));
607 supply_register (FPI_REGNUM, (char *) &(fpregsetp->f_fpiaddr));
610 /* Given a pointer to a floating point register set in /proc format
611 (fpregset_t *), update the register specified by REGNO from gdb's idea
612 of the current floating point register set. If REGNO is -1, update
616 fill_fpregset (fpregsetp, regno)
617 fpregset_t *fpregsetp;
624 for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
626 if ((regno == -1) || (regno == regi))
628 from = (char *) ®isters[REGISTER_BYTE (regi)];
629 to = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
630 memcpy (to, from, REGISTER_RAW_SIZE (regi));
633 if ((regno == -1) || (regno == FPC_REGNUM))
635 fpregsetp->f_pcr = *(int *) ®isters[REGISTER_BYTE (FPC_REGNUM)];
637 if ((regno == -1) || (regno == FPS_REGNUM))
639 fpregsetp->f_psr = *(int *) ®isters[REGISTER_BYTE (FPS_REGNUM)];
641 if ((regno == -1) || (regno == FPI_REGNUM))
643 fpregsetp->f_fpiaddr = *(int *) ®isters[REGISTER_BYTE (FPI_REGNUM)];
647 #endif /* defined (FP0_REGNUM) */
649 #endif /* USE_PROC_FS */
651 #ifdef GET_LONGJMP_TARGET
652 /* Figure out where the longjmp will land. Slurp the args out of the stack.
653 We expect the first arg to be a pointer to the jmp_buf structure from which
654 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
655 This routine returns true on success. */
658 get_longjmp_target (pc)
661 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
662 CORE_ADDR sp, jb_addr;
664 sp = read_register (SP_REGNUM);
666 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
668 TARGET_PTR_BIT / TARGET_CHAR_BIT))
671 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
673 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
674 TARGET_PTR_BIT / TARGET_CHAR_BIT))
677 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
681 #endif /* GET_LONGJMP_TARGET */
683 /* Immediately after a function call, return the saved pc before the frame
684 is setup. For sun3's, we check for the common case of being inside of a
685 system call, and if so, we know that Sun pushes the call # on the stack
686 prior to doing the trap. */
689 m68k_saved_pc_after_call (frame)
690 struct frame_info *frame;
695 op = read_memory_integer (frame->pc - SYSCALL_TRAP_OFFSET, 2);
697 if (op == SYSCALL_TRAP)
698 return read_memory_integer (read_register (SP_REGNUM) + 4, 4);
700 #endif /* SYSCALL_TRAP */
701 return read_memory_integer (read_register (SP_REGNUM), 4);
706 _initialize_m68k_tdep ()
708 tm_print_insn = print_insn_m68k;