1 /* Target dependent code for the Motorola 68000 series.
2 Copyright (C) 1990, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
30 #define P_LINKL_FP 0x480e
31 #define P_LINKW_FP 0x4e56
32 #define P_PEA_FP 0x4856
33 #define P_MOVL_SP_FP 0x2c4f
38 #define P_MOVML 0x48ef
39 #define P_FMOVM 0xf237
42 /* The only reason this is here is the tm-altos.h reference below. It
43 was moved back here from tm-m68k.h. FIXME? */
46 altos_skip_prologue (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 (pc)
72 register int op = read_memory_integer (pc, 2);
74 pc += 4; /* Skip link #word */
75 else if (op == P_LINKL_FP)
76 pc += 6; /* Skip link #long */
77 /* Not sure why branches are here. */
78 /* From tm-isi.h, tm-altos.h */
79 else if (op == 0060000)
80 pc += 4; /* Skip bra #word */
81 else if (op == 00600377)
82 pc += 6; /* skip bra #long */
83 else if ((op & 0177400) == 0060000)
84 pc += 2; /* skip bra #char */
89 delta68_in_sigtramp (pc, name)
93 return strcmp (name, "_sigcode") == 0;
97 delta68_frame_args_address (frame_info)
98 struct frame_info * frame_info;
100 /* we assume here that the only frameless functions are the system calls
101 or other functions who do not put anything on the stack. */
102 if (frame_info->signal_handler_caller)
103 return frame_info->frame + 12;
104 else if (frameless_look_for_prologue (frame_info))
106 /* Check for an interrupted system call */
107 if (frame_info->next && frame_info->next->signal_handler_caller)
108 return frame_info->next->frame + 16;
110 return frame_info->frame + 4;
113 return frame_info->frame;
117 delta68_frame_saved_pc (frame_info)
118 struct frame_info * frame_info;
120 return read_memory_integer (delta68_frame_args_address (frame_info) + 4, 4);
123 /* Return number of args passed to a frame.
124 Can return -1, meaning no way to tell. */
127 isi_frame_num_args (fi)
128 struct frame_info *fi;
131 CORE_ADDR pc = FRAME_SAVED_PC (fi);
132 int insn = 0177777 & read_memory_integer (pc, 2);
134 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
135 val = read_memory_integer (pc + 2, 2);
136 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
137 || (insn & 0170777) == 0050117) /* addqw */
139 val = (insn >> 9) & 7;
143 else if (insn == 0157774) /* addal #WW, sp */
144 val = read_memory_integer (pc + 2, 4);
150 delta68_frame_num_args (fi)
151 struct frame_info *fi;
154 CORE_ADDR pc = FRAME_SAVED_PC (fi);
155 int insn = 0177777 & read_memory_integer (pc, 2);
157 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
158 val = read_memory_integer (pc + 2, 2);
159 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
160 || (insn & 0170777) == 0050117) /* addqw */
162 val = (insn >> 9) & 7;
166 else if (insn == 0157774) /* addal #WW, sp */
167 val = read_memory_integer (pc + 2, 4);
173 news_frame_num_args (fi)
174 struct frame_info *fi;
177 CORE_ADDR pc = FRAME_SAVED_PC (fi);
178 int insn = 0177777 & read_memory_integer (pc, 2);
180 if (insn == 0047757 || insn == 0157374) /* lea W(sp),sp or addaw #W,sp */
181 val = read_memory_integer (pc + 2, 2);
182 else if ((insn & 0170777) == 0050217 /* addql #N, sp */
183 || (insn & 0170777) == 0050117) /* addqw */
185 val = (insn >> 9) & 7;
189 else if (insn == 0157774) /* addal #WW, sp */
190 val = read_memory_integer (pc + 2, 4);
195 /* Push an empty stack frame, to record the current PC, etc. */
198 m68k_push_dummy_frame ()
200 register CORE_ADDR sp = read_register (SP_REGNUM);
204 sp = push_word (sp, read_register (PC_REGNUM));
205 sp = push_word (sp, read_register (FP_REGNUM));
206 write_register (FP_REGNUM, sp);
208 /* Always save the floating-point registers, whether they exist on
209 this target or not. */
210 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
212 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
213 sp = push_bytes (sp, raw_buffer, 12);
216 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
218 sp = push_word (sp, read_register (regnum));
220 sp = push_word (sp, read_register (PS_REGNUM));
221 write_register (SP_REGNUM, sp);
224 /* Discard from the stack the innermost frame,
225 restoring all saved registers. */
230 register struct frame_info *frame = get_current_frame ();
231 register CORE_ADDR fp;
233 struct frame_saved_regs fsr;
236 fp = FRAME_FP (frame);
237 get_frame_saved_regs (frame, &fsr);
238 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
240 if (fsr.regs[regnum])
242 read_memory (fsr.regs[regnum], raw_buffer, 12);
243 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
246 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
248 if (fsr.regs[regnum])
250 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
253 if (fsr.regs[PS_REGNUM])
255 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
257 write_register (FP_REGNUM, read_memory_integer (fp, 4));
258 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
259 write_register (SP_REGNUM, fp + 8);
260 flush_cached_frames ();
264 /* Given an ip value corresponding to the start of a function,
265 return the ip of the first instruction after the function
266 prologue. This is the generic m68k support. Machines which
267 require something different can override the SKIP_PROLOGUE
268 macro to point elsewhere.
270 Some instructions which typically may appear in a function
273 A link instruction, word form:
275 link.w %a6,&0 4e56 XXXX
277 A link instruction, long form:
279 link.l %fp,&F%1 480e XXXX XXXX
281 A movm instruction to preserve integer regs:
283 movm.l &M%1,(4,%sp) 48ef XXXX XXXX
285 A fmovm instruction to preserve float regs:
287 fmovm &FPM%1,(FPO%1,%sp) f237 XXXX XXXX XXXX XXXX
289 Some profiling setup code (FIXME, not recognized yet):
291 lea.l (.L3,%pc),%a1 43fb XXXX XXXX XXXX
292 bsr _mcount 61ff XXXX XXXX
297 m68k_skip_prologue (ip)
300 register CORE_ADDR limit;
301 struct symtab_and_line sal;
304 /* Find out if there is a known limit for the extent of the prologue.
305 If so, ensure we don't go past it. If not, assume "infinity". */
307 sal = find_pc_line (ip, 0);
308 limit = (sal.end) ? sal.end : (CORE_ADDR) ~ 0;
312 op = read_memory_integer (ip, 2);
315 if (op == P_LINKW_FP)
316 ip += 4; /* Skip link.w */
317 else if (op == P_PEA_FP)
318 ip += 2; /* Skip pea %fp */
319 else if (op == P_MOVL_SP_FP)
320 ip += 2; /* Skip move.l %sp, %fp */
321 else if (op == P_LINKL_FP)
322 ip += 6; /* Skip link.l */
323 else if (op == P_MOVML)
324 ip += 6; /* Skip movm.l */
325 else if (op == P_FMOVM)
326 ip += 10; /* Skip fmovm */
328 break; /* Found unknown code, bail out. */
334 m68k_find_saved_regs (frame_info, saved_regs)
335 struct frame_info *frame_info;
336 struct frame_saved_regs *saved_regs;
339 register int regmask;
340 register CORE_ADDR next_addr;
341 register CORE_ADDR pc;
343 /* First possible address for a pc in a call dummy for this frame. */
344 CORE_ADDR possible_call_dummy_start =
345 (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4 - 8 * 12;
348 memset (saved_regs, 0, sizeof (*saved_regs));
349 if ((frame_info)->pc >= possible_call_dummy_start
350 && (frame_info)->pc <= (frame_info)->frame)
353 /* It is a call dummy. We could just stop now, since we know
354 what the call dummy saves and where. But this code proceeds
355 to parse the "prologue" which is part of the call dummy.
356 This is needlessly complex and confusing. FIXME. */
358 next_addr = (frame_info)->frame;
359 pc = possible_call_dummy_start;
363 pc = get_pc_function_start ((frame_info)->pc);
365 nextinsn = read_memory_integer (pc, 2);
366 if (P_PEA_FP == nextinsn
367 && P_MOVL_SP_FP == read_memory_integer (pc + 2, 2))
371 next_addr = frame_info->frame;
374 else if (P_LINKL_FP == nextinsn)
376 /* Find the address above the saved
377 regs using the amount of storage from the link instruction. */
379 next_addr = (frame_info)->frame + read_memory_integer (pc + 2, 4);
382 else if (P_LINKW_FP == nextinsn)
384 /* Find the address above the saved
385 regs using the amount of storage from the link instruction. */
387 next_addr = (frame_info)->frame + read_memory_integer (pc + 2, 2);
393 /* If have an addal #-n, sp next, adjust next_addr. */
394 if ((0177777 & read_memory_integer (pc, 2)) == 0157774)
395 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
400 nextinsn = 0xffff & read_memory_integer (pc, 2);
401 regmask = read_memory_integer (pc + 2, 2);
402 /* fmovemx to -(sp) */
403 if (0xf227 == nextinsn && (regmask & 0xff00) == 0xe000)
405 /* Regmask's low bit is for register fp7, the first pushed */
406 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
408 saved_regs->regs[regnum] = (next_addr -= 12);
411 /* fmovemx to (fp + displacement) */
412 else if (0171056 == nextinsn && (regmask & 0xff00) == 0xf000)
414 register CORE_ADDR addr;
416 addr = (frame_info)->frame + read_memory_integer (pc + 4, 2);
417 /* Regmask's low bit is for register fp7, the first pushed */
418 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
421 saved_regs->regs[regnum] = addr;
427 else if (0044327 == nextinsn)
429 /* Regmask's low bit is for register 0, the first written */
430 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
433 saved_regs->regs[regnum] = next_addr;
438 /* moveml to (fp + displacement) */
439 else if (0044356 == nextinsn)
441 register CORE_ADDR addr;
443 addr = (frame_info)->frame + read_memory_integer (pc + 4, 2);
444 /* Regmask's low bit is for register 0, the first written */
445 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
448 saved_regs->regs[regnum] = addr;
453 /* moveml to -(sp) */
454 else if (0044347 == nextinsn)
456 /* Regmask's low bit is for register 15, the first pushed */
457 for (regnum = 16; --regnum >= 0; regmask >>= 1)
459 saved_regs->regs[regnum] = (next_addr -= 4);
463 else if (0x2f00 == (0xfff0 & nextinsn))
465 regnum = 0xf & nextinsn;
466 saved_regs->regs[regnum] = (next_addr -= 4);
469 /* fmovemx to index of sp */
470 else if (0xf236 == nextinsn && (regmask & 0xff00) == 0xf000)
472 /* Regmask's low bit is for register fp0, the first written */
473 for (regnum = FP0_REGNUM + 8; --regnum >= FP0_REGNUM; regmask >>= 1)
476 saved_regs->regs[regnum] = next_addr;
481 /* clrw -(sp); movw ccr,-(sp) */
482 else if (0x4267 == nextinsn && 0x42e7 == regmask)
484 saved_regs->regs[PS_REGNUM] = (next_addr -= 4);
491 saved_regs->regs[SP_REGNUM] = (frame_info)->frame + 8;
492 saved_regs->regs[FP_REGNUM] = (frame_info)->frame;
493 saved_regs->regs[PC_REGNUM] = (frame_info)->frame + 4;
494 #ifdef SIG_SP_FP_OFFSET
495 /* Adjust saved SP_REGNUM for fake _sigtramp frames. */
496 if (frame_info->signal_handler_caller && frame_info->next)
497 saved_regs->regs[SP_REGNUM] = frame_info->next->frame + SIG_SP_FP_OFFSET;
502 #ifdef USE_PROC_FS /* Target dependent support for /proc */
504 #include <sys/procfs.h>
506 /* The /proc interface divides the target machine's register set up into
507 two different sets, the general register set (gregset) and the floating
508 point register set (fpregset). For each set, there is an ioctl to get
509 the current register set and another ioctl to set the current values.
511 The actual structure passed through the ioctl interface is, of course,
512 naturally machine dependent, and is different for each set of registers.
513 For the m68k for example, the general register set is typically defined
516 typedef int gregset_t[18];
522 and the floating point set by:
524 typedef struct fpregset {
528 int f_fpregs[8][3]; (8 regs, 96 bits each)
531 These routines provide the packing and unpacking of gregset_t and
532 fpregset_t formatted data.
536 /* Atari SVR4 has R_SR but not R_PS */
538 #if !defined (R_PS) && defined (R_SR)
542 /* Given a pointer to a general register set in /proc format (gregset_t *),
543 unpack the register contents and supply them as gdb's idea of the current
547 supply_gregset (gregsetp)
551 register greg_t *regp = (greg_t *) gregsetp;
553 for (regi = 0; regi < R_PC; regi++)
555 supply_register (regi, (char *) (regp + regi));
557 supply_register (PS_REGNUM, (char *) (regp + R_PS));
558 supply_register (PC_REGNUM, (char *) (regp + R_PC));
562 fill_gregset (gregsetp, regno)
567 register greg_t *regp = (greg_t *) gregsetp;
569 for (regi = 0; regi < R_PC; regi++)
571 if ((regno == -1) || (regno == regi))
573 *(regp + regi) = *(int *) ®isters[REGISTER_BYTE (regi)];
576 if ((regno == -1) || (regno == PS_REGNUM))
578 *(regp + R_PS) = *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)];
580 if ((regno == -1) || (regno == PC_REGNUM))
582 *(regp + R_PC) = *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
586 #if defined (FP0_REGNUM)
588 /* Given a pointer to a floating point register set in /proc format
589 (fpregset_t *), unpack the register contents and supply them as gdb's
590 idea of the current floating point register values. */
593 supply_fpregset (fpregsetp)
594 fpregset_t *fpregsetp;
599 for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
601 from = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
602 supply_register (regi, from);
604 supply_register (FPC_REGNUM, (char *) &(fpregsetp->f_pcr));
605 supply_register (FPS_REGNUM, (char *) &(fpregsetp->f_psr));
606 supply_register (FPI_REGNUM, (char *) &(fpregsetp->f_fpiaddr));
609 /* Given a pointer to a floating point register set in /proc format
610 (fpregset_t *), update the register specified by REGNO from gdb's idea
611 of the current floating point register set. If REGNO is -1, update
615 fill_fpregset (fpregsetp, regno)
616 fpregset_t *fpregsetp;
623 for (regi = FP0_REGNUM; regi < FPC_REGNUM; regi++)
625 if ((regno == -1) || (regno == regi))
627 from = (char *) ®isters[REGISTER_BYTE (regi)];
628 to = (char *) &(fpregsetp->f_fpregs[regi - FP0_REGNUM][0]);
629 memcpy (to, from, REGISTER_RAW_SIZE (regi));
632 if ((regno == -1) || (regno == FPC_REGNUM))
634 fpregsetp->f_pcr = *(int *) ®isters[REGISTER_BYTE (FPC_REGNUM)];
636 if ((regno == -1) || (regno == FPS_REGNUM))
638 fpregsetp->f_psr = *(int *) ®isters[REGISTER_BYTE (FPS_REGNUM)];
640 if ((regno == -1) || (regno == FPI_REGNUM))
642 fpregsetp->f_fpiaddr = *(int *) ®isters[REGISTER_BYTE (FPI_REGNUM)];
646 #endif /* defined (FP0_REGNUM) */
648 #endif /* USE_PROC_FS */
650 #ifdef GET_LONGJMP_TARGET
651 /* Figure out where the longjmp will land. Slurp the args out of the stack.
652 We expect the first arg to be a pointer to the jmp_buf structure from which
653 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
654 This routine returns true on success. */
657 get_longjmp_target (pc)
660 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
661 CORE_ADDR sp, jb_addr;
663 sp = read_register (SP_REGNUM);
665 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
667 TARGET_PTR_BIT / TARGET_CHAR_BIT))
670 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
672 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
673 TARGET_PTR_BIT / TARGET_CHAR_BIT))
676 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
680 #endif /* GET_LONGJMP_TARGET */
682 /* Immediately after a function call, return the saved pc before the frame
683 is setup. For sun3's, we check for the common case of being inside of a
684 system call, and if so, we know that Sun pushes the call # on the stack
685 prior to doing the trap. */
688 m68k_saved_pc_after_call (frame)
689 struct frame_info *frame;
694 op = read_memory_integer (frame->pc - SYSCALL_TRAP_OFFSET, 2);
696 if (op == SYSCALL_TRAP)
697 return read_memory_integer (read_register (SP_REGNUM) + 4, 4);
699 #endif /* SYSCALL_TRAP */
700 return read_memory_integer (read_register (SP_REGNUM), 4);
705 _initialize_m68k_tdep ()
707 tm_print_insn = print_insn_m68k;