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 (CORE_ADDR pc)
49 register int op = read_memory_integer (pc, 2);
51 pc += 4; /* Skip link #word */
52 else if (op == P_LINKL_FP)
53 pc += 6; /* Skip link #long */
54 /* Not sure why branches are here. */
55 /* From tm-isi.h, tm-altos.h */
56 else if (op == 0060000)
57 pc += 4; /* Skip bra #word */
58 else if (op == 00600377)
59 pc += 6; /* skip bra #long */
60 else if ((op & 0177400) == 0060000)
61 pc += 2; /* skip bra #char */
65 /* The only reason this is here is the tm-isi.h reference below. It
66 was moved back here from tm-m68k.h. FIXME? */
69 isi_skip_prologue (CORE_ADDR pc)
71 register int op = read_memory_integer (pc, 2);
73 pc += 4; /* Skip link #word */
74 else if (op == P_LINKL_FP)
75 pc += 6; /* Skip link #long */
76 /* Not sure why branches are here. */
77 /* From tm-isi.h, tm-altos.h */
78 else if (op == 0060000)
79 pc += 4; /* Skip bra #word */
80 else if (op == 00600377)
81 pc += 6; /* skip bra #long */
82 else if ((op & 0177400) == 0060000)
83 pc += 2; /* skip bra #char */
88 delta68_in_sigtramp (CORE_ADDR pc, char *name)
91 return strcmp (name, "_sigcode") == 0;
97 delta68_frame_args_address (struct frame_info *frame_info)
99 /* we assume here that the only frameless functions are the system calls
100 or other functions who do not put anything on the stack. */
101 if (frame_info->signal_handler_caller)
102 return frame_info->frame + 12;
103 else if (frameless_look_for_prologue (frame_info))
105 /* Check for an interrupted system call */
106 if (frame_info->next && frame_info->next->signal_handler_caller)
107 return frame_info->next->frame + 16;
109 return frame_info->frame + 4;
112 return frame_info->frame;
116 delta68_frame_saved_pc (struct frame_info *frame_info)
118 return read_memory_integer (delta68_frame_args_address (frame_info) + 4, 4);
121 /* Return number of args passed to a frame.
122 Can return -1, meaning no way to tell. */
125 isi_frame_num_args (struct frame_info *fi)
128 CORE_ADDR pc = FRAME_SAVED_PC (fi);
129 int insn = 0177777 & read_memory_integer (pc, 2);
131 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
132 val = read_memory_integer (pc + 2, 2);
133 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
134 || (insn & 0170777) == 0050117) /* addqw */
136 val = (insn >> 9) & 7;
140 else if (insn == 0157774) /* addal #WW, sp */
141 val = read_memory_integer (pc + 2, 4);
147 delta68_frame_num_args (struct frame_info *fi)
150 CORE_ADDR pc = FRAME_SAVED_PC (fi);
151 int insn = 0177777 & read_memory_integer (pc, 2);
153 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
154 val = read_memory_integer (pc + 2, 2);
155 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
156 || (insn & 0170777) == 0050117) /* addqw */
158 val = (insn >> 9) & 7;
162 else if (insn == 0157774) /* addal #WW, sp */
163 val = read_memory_integer (pc + 2, 4);
169 news_frame_num_args (struct frame_info *fi)
172 CORE_ADDR pc = FRAME_SAVED_PC (fi);
173 int insn = 0177777 & read_memory_integer (pc, 2);
175 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
176 val = read_memory_integer (pc + 2, 2);
177 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
178 || (insn & 0170777) == 0050117) /* addqw */
180 val = (insn >> 9) & 7;
184 else if (insn == 0157774) /* addal #WW, sp */
185 val = read_memory_integer (pc + 2, 4);
190 /* Push an empty stack frame, to record the current PC, etc. */
193 m68k_push_dummy_frame (void)
195 register CORE_ADDR sp = read_register (SP_REGNUM);
199 sp = push_word (sp, read_register (PC_REGNUM));
200 sp = push_word (sp, read_register (FP_REGNUM));
201 write_register (FP_REGNUM, sp);
203 /* Always save the floating-point registers, whether they exist on
204 this target or not. */
205 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
207 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
208 sp = push_bytes (sp, raw_buffer, 12);
211 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
213 sp = push_word (sp, read_register (regnum));
215 sp = push_word (sp, read_register (PS_REGNUM));
216 write_register (SP_REGNUM, sp);
219 /* Discard from the stack the innermost frame,
220 restoring all saved registers. */
223 m68k_pop_frame (void)
225 register struct frame_info *frame = get_current_frame ();
226 register CORE_ADDR fp;
228 struct frame_saved_regs fsr;
231 fp = FRAME_FP (frame);
232 get_frame_saved_regs (frame, &fsr);
233 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
235 if (fsr.regs[regnum])
237 read_memory (fsr.regs[regnum], raw_buffer, 12);
238 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
241 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
243 if (fsr.regs[regnum])
245 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
248 if (fsr.regs[PS_REGNUM])
250 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
252 write_register (FP_REGNUM, read_memory_integer (fp, 4));
253 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
254 write_register (SP_REGNUM, fp + 8);
255 flush_cached_frames ();
259 /* Given an ip value corresponding to the start of a function,
260 return the ip of the first instruction after the function
261 prologue. This is the generic m68k support. Machines which
262 require something different can override the SKIP_PROLOGUE
263 macro to point elsewhere.
265 Some instructions which typically may appear in a function
268 A link instruction, word form:
270 link.w %a6,&0 4e56 XXXX
272 A link instruction, long form:
274 link.l %fp,&F%1 480e XXXX XXXX
276 A movm instruction to preserve integer regs:
278 movm.l &M%1,(4,%sp) 48ef XXXX XXXX
280 A fmovm instruction to preserve float regs:
282 fmovm &FPM%1,(FPO%1,%sp) f237 XXXX XXXX XXXX XXXX
284 Some profiling setup code (FIXME, not recognized yet):
286 lea.l (.L3,%pc),%a1 43fb XXXX XXXX XXXX
287 bsr _mcount 61ff XXXX XXXX
292 m68k_skip_prologue (CORE_ADDR ip)
294 register CORE_ADDR limit;
295 struct symtab_and_line sal;
298 /* Find out if there is a known limit for the extent of the prologue.
299 If so, ensure we don't go past it. If not, assume "infinity". */
301 sal = find_pc_line (ip, 0);
302 limit = (sal.end) ? sal.end : (CORE_ADDR) ~ 0;
306 op = read_memory_integer (ip, 2);
309 if (op == P_LINKW_FP)
310 ip += 4; /* Skip link.w */
311 else if (op == P_PEA_FP)
312 ip += 2; /* Skip pea %fp */
313 else if (op == P_MOVL_SP_FP)
314 ip += 2; /* Skip move.l %sp, %fp */
315 else if (op == P_LINKL_FP)
316 ip += 6; /* Skip link.l */
317 else if (op == P_MOVML)
318 ip += 6; /* Skip movm.l */
319 else if (op == P_FMOVM)
320 ip += 10; /* Skip fmovm */
322 break; /* Found unknown code, bail out. */
328 m68k_find_saved_regs (struct frame_info *frame_info,
329 struct frame_saved_regs *saved_regs)
332 register int regmask;
333 register CORE_ADDR next_addr;
334 register CORE_ADDR pc;
336 /* First possible address for a pc in a call dummy for this frame. */
337 CORE_ADDR possible_call_dummy_start =
338 (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4 - 8 * 12;
341 memset (saved_regs, 0, sizeof (*saved_regs));
342 if ((frame_info)->pc >= possible_call_dummy_start
343 && (frame_info)->pc <= (frame_info)->frame)
346 /* It is a call dummy. We could just stop now, since we know
347 what the call dummy saves and where. But this code proceeds
348 to parse the "prologue" which is part of the call dummy.
349 This is needlessly complex and confusing. FIXME. */
351 next_addr = (frame_info)->frame;
352 pc = possible_call_dummy_start;
356 pc = get_pc_function_start ((frame_info)->pc);
358 nextinsn = read_memory_integer (pc, 2);
359 if (P_PEA_FP == nextinsn
360 && P_MOVL_SP_FP == read_memory_integer (pc + 2, 2))
364 next_addr = frame_info->frame;
367 else if (P_LINKL_FP == nextinsn)
369 /* Find the address above the saved
370 regs using the amount of storage from the link instruction. */
372 next_addr = (frame_info)->frame + read_memory_integer (pc + 2, 4);
375 else if (P_LINKW_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, 2);
386 /* If have an addal #-n, sp next, adjust next_addr. */
387 if ((0177777 & read_memory_integer (pc, 2)) == 0157774)
388 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
393 nextinsn = 0xffff & read_memory_integer (pc, 2);
394 regmask = read_memory_integer (pc + 2, 2);
395 /* fmovemx to -(sp) */
396 if (0xf227 == nextinsn && (regmask & 0xff00) == 0xe000)
398 /* Regmask's low bit is for register fp7, the first pushed */
399 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
401 saved_regs->regs[regnum] = (next_addr -= 12);
404 /* fmovemx to (fp + displacement) */
405 else if (0171056 == nextinsn && (regmask & 0xff00) == 0xf000)
407 register CORE_ADDR addr;
409 addr = (frame_info)->frame + read_memory_integer (pc + 4, 2);
410 /* Regmask's low bit is for register fp7, the first pushed */
411 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
414 saved_regs->regs[regnum] = addr;
420 else if (0044327 == nextinsn)
422 /* Regmask's low bit is for register 0, the first written */
423 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
426 saved_regs->regs[regnum] = next_addr;
431 /* moveml to (fp + displacement) */
432 else if (0044356 == nextinsn)
434 register CORE_ADDR addr;
436 addr = (frame_info)->frame + read_memory_integer (pc + 4, 2);
437 /* Regmask's low bit is for register 0, the first written */
438 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
441 saved_regs->regs[regnum] = addr;
446 /* moveml to -(sp) */
447 else if (0044347 == nextinsn)
449 /* Regmask's low bit is for register 15, the first pushed */
450 for (regnum = 16; --regnum >= 0; regmask >>= 1)
452 saved_regs->regs[regnum] = (next_addr -= 4);
456 else if (0x2f00 == (0xfff0 & nextinsn))
458 regnum = 0xf & nextinsn;
459 saved_regs->regs[regnum] = (next_addr -= 4);
462 /* fmovemx to index of sp */
463 else if (0xf236 == nextinsn && (regmask & 0xff00) == 0xf000)
465 /* Regmask's low bit is for register fp0, the first written */
466 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
469 saved_regs->regs[regnum] = next_addr;
474 /* clrw -(sp); movw ccr,-(sp) */
475 else if (0x4267 == nextinsn && 0x42e7 == regmask)
477 saved_regs->regs[PS_REGNUM] = (next_addr -= 4);
484 saved_regs->regs[SP_REGNUM] = (frame_info)->frame + 8;
485 saved_regs->regs[FP_REGNUM] = (frame_info)->frame;
486 saved_regs->regs[PC_REGNUM] = (frame_info)->frame + 4;
487 #ifdef SIG_SP_FP_OFFSET
488 /* Adjust saved SP_REGNUM for fake _sigtramp frames. */
489 if (frame_info->signal_handler_caller && frame_info->next)
490 saved_regs->regs[SP_REGNUM] = frame_info->next->frame + SIG_SP_FP_OFFSET;
495 #ifdef USE_PROC_FS /* Target dependent support for /proc */
497 #include <sys/procfs.h>
499 /* Prototypes for supply_gregset etc. */
502 /* The /proc interface divides the target machine's register set up into
503 two different sets, the general register set (gregset) and the floating
504 point register set (fpregset). For each set, there is an ioctl to get
505 the current register set and another ioctl to set the current values.
507 The actual structure passed through the ioctl interface is, of course,
508 naturally machine dependent, and is different for each set of registers.
509 For the m68k for example, the general register set is typically defined
512 typedef int gregset_t[18];
518 and the floating point set by:
520 typedef struct fpregset {
524 int f_fpregs[8][3]; (8 regs, 96 bits each)
527 These routines provide the packing and unpacking of gregset_t and
528 fpregset_t formatted data.
532 /* Atari SVR4 has R_SR but not R_PS */
534 #if !defined (R_PS) && defined (R_SR)
538 /* Given a pointer to a general register set in /proc format (gregset_t *),
539 unpack the register contents and supply them as gdb's idea of the current
543 supply_gregset (gregset_t *gregsetp)
546 register greg_t *regp = (greg_t *) gregsetp;
548 for (regi = 0; regi < R_PC; regi++)
550 supply_register (regi, (char *) (regp + regi));
552 supply_register (PS_REGNUM, (char *) (regp + R_PS));
553 supply_register (PC_REGNUM, (char *) (regp + R_PC));
557 fill_gregset (gregset_t *gregsetp, int regno)
560 register greg_t *regp = (greg_t *) gregsetp;
562 for (regi = 0; regi < R_PC; regi++)
564 if ((regno == -1) || (regno == regi))
566 *(regp + regi) = *(int *) ®isters[REGISTER_BYTE (regi)];
569 if ((regno == -1) || (regno == PS_REGNUM))
571 *(regp + R_PS) = *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)];
573 if ((regno == -1) || (regno == PC_REGNUM))
575 *(regp + R_PC) = *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
579 #if defined (FP0_REGNUM)
581 /* Given a pointer to a floating point register set in /proc format
582 (fpregset_t *), unpack the register contents and supply them as gdb's
583 idea of the current floating point register values. */
586 supply_fpregset (fpregset_t *fpregsetp)
591 for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
593 from = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
594 supply_register (regi, from);
596 supply_register (FPC_REGNUM, (char *) &(fpregsetp->f_pcr));
597 supply_register (FPS_REGNUM, (char *) &(fpregsetp->f_psr));
598 supply_register (FPI_REGNUM, (char *) &(fpregsetp->f_fpiaddr));
601 /* Given a pointer to a floating point register set in /proc format
602 (fpregset_t *), update the register specified by REGNO from gdb's idea
603 of the current floating point register set. If REGNO is -1, update
607 fill_fpregset (fpregset_t *fpregsetp, int regno)
613 for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
615 if ((regno == -1) || (regno == regi))
617 from = (char *) ®isters[REGISTER_BYTE (regi)];
618 to = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
619 memcpy (to, from, REGISTER_RAW_SIZE (regi));
622 if ((regno == -1) || (regno == FPC_REGNUM))
624 fpregsetp->f_pcr = *(int *) ®isters[REGISTER_BYTE (FPC_REGNUM)];
626 if ((regno == -1) || (regno == FPS_REGNUM))
628 fpregsetp->f_psr = *(int *) ®isters[REGISTER_BYTE (FPS_REGNUM)];
630 if ((regno == -1) || (regno == FPI_REGNUM))
632 fpregsetp->f_fpiaddr = *(int *) ®isters[REGISTER_BYTE (FPI_REGNUM)];
636 #endif /* defined (FP0_REGNUM) */
638 #endif /* USE_PROC_FS */
640 /* Figure out where the longjmp will land. Slurp the args out of the stack.
641 We expect the first arg to be a pointer to the jmp_buf structure from which
642 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
643 This routine returns true on success. */
645 /* NOTE: cagney/2000-11-08: For this function to be fully multi-arched
646 the macro's JB_PC and JB_ELEMENT_SIZE would need to be moved into
647 the ``struct gdbarch_tdep'' object and then set on a target ISA/ABI
651 m68k_get_longjmp_target (CORE_ADDR *pc)
653 #if defined (JB_PC) && defined (JB_ELEMENT_SIZE)
655 CORE_ADDR sp, jb_addr;
657 buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
658 sp = read_register (SP_REGNUM);
660 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
662 TARGET_PTR_BIT / TARGET_CHAR_BIT))
665 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
667 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
668 TARGET_PTR_BIT / TARGET_CHAR_BIT))
671 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
675 internal_error ("m68k_get_longjmp_target: not implemented");
680 /* Immediately after a function call, return the saved pc before the frame
681 is setup. For sun3's, we check for the common case of being inside of a
682 system call, and if so, we know that Sun pushes the call # on the stack
683 prior to doing the trap. */
686 m68k_saved_pc_after_call (struct frame_info *frame)
691 op = read_memory_integer (frame->pc - SYSCALL_TRAP_OFFSET, 2);
693 if (op == SYSCALL_TRAP)
694 return read_memory_integer (read_register (SP_REGNUM) + 4, 4);
696 #endif /* SYSCALL_TRAP */
697 return read_memory_integer (read_register (SP_REGNUM), 4);
702 _initialize_m68k_tdep (void)
704 tm_print_insn = print_insn_m68k;