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, Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
29 /* Push an empty stack frame, to record the current PC, etc. */
32 m68k_push_dummy_frame ()
34 register CORE_ADDR sp = read_register (SP_REGNUM);
38 sp = push_word (sp, read_register (PC_REGNUM));
39 sp = push_word (sp, read_register (FP_REGNUM));
40 write_register (FP_REGNUM, sp);
42 /* Always save the floating-point registers, whether they exist on
43 this target or not. */
44 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--)
46 read_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
47 sp = push_bytes (sp, raw_buffer, 12);
50 for (regnum = FP_REGNUM - 1; regnum >= 0; regnum--)
52 sp = push_word (sp, read_register (regnum));
54 sp = push_word (sp, read_register (PS_REGNUM));
55 write_register (SP_REGNUM, sp);
58 /* Discard from the stack the innermost frame,
59 restoring all saved registers. */
64 register struct frame_info *frame = get_current_frame ();
65 register CORE_ADDR fp;
67 struct frame_saved_regs fsr;
70 fp = FRAME_FP (frame);
71 get_frame_saved_regs (frame, &fsr);
72 for (regnum = FP0_REGNUM + 7 ; regnum >= FP0_REGNUM ; regnum--)
76 read_memory (fsr.regs[regnum], raw_buffer, 12);
77 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 12);
80 for (regnum = FP_REGNUM - 1 ; regnum >= 0 ; regnum--)
84 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
87 if (fsr.regs[PS_REGNUM])
89 write_register (PS_REGNUM, read_memory_integer (fsr.regs[PS_REGNUM], 4));
91 write_register (FP_REGNUM, read_memory_integer (fp, 4));
92 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
93 write_register (SP_REGNUM, fp + 8);
94 flush_cached_frames ();
98 /* Given an ip value corresponding to the start of a function,
99 return the ip of the first instruction after the function
100 prologue. This is the generic m68k support. Machines which
101 require something different can override the SKIP_PROLOGUE
102 macro to point elsewhere.
104 Some instructions which typically may appear in a function
107 A link instruction, word form:
109 link.w %a6,&0 4e56 XXXX
111 A link instruction, long form:
113 link.l %fp,&F%1 480e XXXX XXXX
115 A movm instruction to preserve integer regs:
117 movm.l &M%1,(4,%sp) 48ef XXXX XXXX
119 A fmovm instruction to preserve float regs:
121 fmovm &FPM%1,(FPO%1,%sp) f237 XXXX XXXX XXXX XXXX
123 Some profiling setup code (FIXME, not recognized yet):
125 lea.l (.L3,%pc),%a1 43fb XXXX XXXX XXXX
126 bsr _mcount 61ff XXXX XXXX
130 #define P_LINK_L 0x480e
131 #define P_LINK_W 0x4e56
132 #define P_MOV_L 0x207c
135 #define P_LEA_L 0x43fb
136 #define P_MOVM_L 0x48ef
137 #define P_FMOVM 0xf237
138 #define P_TRAP 0x4e40
141 m68k_skip_prologue (ip)
144 register CORE_ADDR limit;
145 struct symtab_and_line sal;
148 /* Find out if there is a known limit for the extent of the prologue.
149 If so, ensure we don't go past it. If not, assume "infinity". */
151 sal = find_pc_line (ip, 0);
152 limit = (sal.end) ? sal.end : (CORE_ADDR) ~0;
156 op = read_memory_integer (ip, 2);
161 ip += 4; /* Skip link.w */
163 else if (op == 0x4856)
164 ip += 2; /* Skip pea %fp */
165 else if (op == 0x2c4f)
166 ip += 2; /* Skip move.l %sp, %fp */
167 else if (op == P_LINK_L)
169 ip += 6; /* Skip link.l */
171 else if (op == P_MOVM_L)
173 ip += 6; /* Skip movm.l */
175 else if (op == P_FMOVM)
177 ip += 10; /* Skip fmovm */
181 break; /* Found unknown code, bail out. */
188 m68k_find_saved_regs (frame_info, saved_regs)
189 struct frame_info *frame_info;
190 struct frame_saved_regs *saved_regs;
193 register int regmask;
194 register CORE_ADDR next_addr;
195 register CORE_ADDR pc;
197 /* First possible address for a pc in a call dummy for this frame. */
198 CORE_ADDR possible_call_dummy_start =
199 (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM*4 - 4 - 8*12;
202 memset (saved_regs, 0, sizeof (*saved_regs));
203 if ((frame_info)->pc >= possible_call_dummy_start
204 && (frame_info)->pc <= (frame_info)->frame)
207 /* It is a call dummy. We could just stop now, since we know
208 what the call dummy saves and where. But this code proceeds
209 to parse the "prologue" which is part of the call dummy.
210 This is needlessly complex and confusing. FIXME. */
212 next_addr = (frame_info)->frame;
213 pc = possible_call_dummy_start;
217 pc = get_pc_function_start ((frame_info)->pc);
219 if (0x4856 == read_memory_integer (pc, 2)
220 && 0x2c4f == read_memory_integer (pc + 2, 2))
227 next_addr = frame_info->frame;
229 else if (044016 == read_memory_integer (pc, 2))
231 /* Find the address above the saved
232 regs using the amount of storage from the link instruction. */
233 next_addr = (frame_info)->frame + read_memory_integer (pc += 2, 4), pc+=4;
234 else if (047126 == read_memory_integer (pc, 2))
236 /* Find the address above the saved
237 regs using the amount of storage from the link instruction. */
238 next_addr = (frame_info)->frame + read_memory_integer (pc += 2, 2), pc+=2;
241 /* If have an addal #-n, sp next, adjust next_addr. */
242 if ((0177777 & read_memory_integer (pc, 2)) == 0157774)
243 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
245 regmask = read_memory_integer (pc + 2, 2);
247 /* Here can come an fmovem. Check for it. */
248 nextinsn = 0xffff & read_memory_integer (pc, 2);
249 if (0xf227 == nextinsn
250 && (regmask & 0xff00) == 0xe000)
251 { pc += 4; /* Regmask's low bit is for register fp7, the first pushed */
252 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--, regmask >>= 1)
254 saved_regs->regs[regnum] = (next_addr -= 12);
255 regmask = read_memory_integer (pc + 2, 2); }
257 /* next should be a moveml to (sp) or -(sp) or a movl r,-(sp) */
258 if (0044327 == read_memory_integer (pc, 2))
259 { pc += 4; /* Regmask's low bit is for register 0, the first written */
260 for (regnum = 0; regnum < 16; regnum++, regmask >>= 1)
262 saved_regs->regs[regnum] = (next_addr += 4) - 4; }
263 else if (0044347 == read_memory_integer (pc, 2))
265 pc += 4; /* Regmask's low bit is for register 15, the first pushed */
266 for (regnum = 15; regnum >= 0; regnum--, regmask >>= 1)
268 saved_regs->regs[regnum] = (next_addr -= 4);
270 else if (0x2f00 == (0xfff0 & read_memory_integer (pc, 2)))
272 regnum = 0xf & read_memory_integer (pc, 2); pc += 2;
273 saved_regs->regs[regnum] = (next_addr -= 4);
274 /* gcc, at least, may use a pair of movel instructions when saving
275 exactly 2 registers. */
276 if (0x2f00 == (0xfff0 & read_memory_integer (pc, 2)))
278 regnum = 0xf & read_memory_integer (pc, 2);
280 saved_regs->regs[regnum] = (next_addr -= 4);
284 /* fmovemx to index of sp may follow. */
285 regmask = read_memory_integer (pc + 2, 2);
286 nextinsn = 0xffff & read_memory_integer (pc, 2);
287 if (0xf236 == nextinsn
288 && (regmask & 0xff00) == 0xf000)
289 { pc += 10; /* Regmask's low bit is for register fp0, the first written */
290 for (regnum = FP0_REGNUM + 7; regnum >= FP0_REGNUM; regnum--, regmask >>= 1)
292 saved_regs->regs[regnum] = (next_addr += 12) - 12;
293 regmask = read_memory_integer (pc + 2, 2); }
295 /* clrw -(sp); movw ccr,-(sp) may follow. */
296 if (0x426742e7 == read_memory_integer (pc, 4))
297 saved_regs->regs[PS_REGNUM] = (next_addr -= 4);
299 saved_regs->regs[SP_REGNUM] = (frame_info)->frame + 8;
300 saved_regs->regs[FP_REGNUM] = (frame_info)->frame;
301 saved_regs->regs[PC_REGNUM] = (frame_info)->frame + 4;
302 #ifdef SIG_SP_FP_OFFSET
303 /* Adjust saved SP_REGNUM for fake _sigtramp frames. */
304 if (frame_info->signal_handler_caller && frame_info->next)
305 saved_regs->regs[SP_REGNUM] = frame_info->next->frame + SIG_SP_FP_OFFSET;
310 #ifdef USE_PROC_FS /* Target dependent support for /proc */
312 #include <sys/procfs.h>
314 /* The /proc interface divides the target machine's register set up into
315 two different sets, the general register set (gregset) and the floating
316 point register set (fpregset). For each set, there is an ioctl to get
317 the current register set and another ioctl to set the current values.
319 The actual structure passed through the ioctl interface is, of course,
320 naturally machine dependent, and is different for each set of registers.
321 For the m68k for example, the general register set is typically defined
324 typedef int gregset_t[18];
330 and the floating point set by:
332 typedef struct fpregset {
336 int f_fpregs[8][3]; (8 regs, 96 bits each)
339 These routines provide the packing and unpacking of gregset_t and
340 fpregset_t formatted data.
344 /* Atari SVR4 has R_SR but not R_PS */
346 #if !defined (R_PS) && defined (R_SR)
350 /* Given a pointer to a general register set in /proc format (gregset_t *),
351 unpack the register contents and supply them as gdb's idea of the current
355 supply_gregset (gregsetp)
359 register greg_t *regp = (greg_t *) gregsetp;
361 for (regi = 0 ; regi < R_PC ; regi++)
363 supply_register (regi, (char *) (regp + regi));
365 supply_register (PS_REGNUM, (char *) (regp + R_PS));
366 supply_register (PC_REGNUM, (char *) (regp + R_PC));
370 fill_gregset (gregsetp, regno)
375 register greg_t *regp = (greg_t *) gregsetp;
377 for (regi = 0 ; regi < R_PC ; regi++)
379 if ((regno == -1) || (regno == regi))
381 *(regp + regi) = *(int *) ®isters[REGISTER_BYTE (regi)];
384 if ((regno == -1) || (regno == PS_REGNUM))
386 *(regp + R_PS) = *(int *) ®isters[REGISTER_BYTE (PS_REGNUM)];
388 if ((regno == -1) || (regno == PC_REGNUM))
390 *(regp + R_PC) = *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
394 #if defined (FP0_REGNUM)
396 /* Given a pointer to a floating point register set in /proc format
397 (fpregset_t *), unpack the register contents and supply them as gdb's
398 idea of the current floating point register values. */
401 supply_fpregset (fpregsetp)
402 fpregset_t *fpregsetp;
407 for (regi = FP0_REGNUM ; regi < FPC_REGNUM ; regi++)
409 from = (char *) &(fpregsetp -> f_fpregs[regi-FP0_REGNUM][0]);
410 supply_register (regi, from);
412 supply_register (FPC_REGNUM, (char *) &(fpregsetp -> f_pcr));
413 supply_register (FPS_REGNUM, (char *) &(fpregsetp -> f_psr));
414 supply_register (FPI_REGNUM, (char *) &(fpregsetp -> f_fpiaddr));
417 /* Given a pointer to a floating point register set in /proc format
418 (fpregset_t *), update the register specified by REGNO from gdb's idea
419 of the current floating point register set. If REGNO is -1, update
423 fill_fpregset (fpregsetp, regno)
424 fpregset_t *fpregsetp;
431 for (regi = FP0_REGNUM ; regi < FPC_REGNUM ; regi++)
433 if ((regno == -1) || (regno == regi))
435 from = (char *) ®isters[REGISTER_BYTE (regi)];
436 to = (char *) &(fpregsetp -> f_fpregs[regi-FP0_REGNUM][0]);
437 memcpy (to, from, REGISTER_RAW_SIZE (regi));
440 if ((regno == -1) || (regno == FPC_REGNUM))
442 fpregsetp -> f_pcr = *(int *) ®isters[REGISTER_BYTE (FPC_REGNUM)];
444 if ((regno == -1) || (regno == FPS_REGNUM))
446 fpregsetp -> f_psr = *(int *) ®isters[REGISTER_BYTE (FPS_REGNUM)];
448 if ((regno == -1) || (regno == FPI_REGNUM))
450 fpregsetp -> f_fpiaddr = *(int *) ®isters[REGISTER_BYTE (FPI_REGNUM)];
454 #endif /* defined (FP0_REGNUM) */
456 #endif /* USE_PROC_FS */
458 #ifdef GET_LONGJMP_TARGET
459 /* Figure out where the longjmp will land. Slurp the args out of the stack.
460 We expect the first arg to be a pointer to the jmp_buf structure from which
461 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
462 This routine returns true on success. */
465 get_longjmp_target(pc)
468 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
469 CORE_ADDR sp, jb_addr;
471 sp = read_register(SP_REGNUM);
473 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
475 TARGET_PTR_BIT / TARGET_CHAR_BIT))
478 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
480 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
481 TARGET_PTR_BIT / TARGET_CHAR_BIT))
484 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
488 #endif /* GET_LONGJMP_TARGET */
490 /* Immediately after a function call, return the saved pc before the frame
491 is setup. For sun3's, we check for the common case of being inside of a
492 system call, and if so, we know that Sun pushes the call # on the stack
493 prior to doing the trap. */
496 m68k_saved_pc_after_call(frame)
497 struct frame_info *frame;
502 op = read_memory_integer (frame->pc - SYSCALL_TRAP_OFFSET, 2);
504 if (op == SYSCALL_TRAP)
505 return read_memory_integer (read_register (SP_REGNUM) + 4, 4);
507 #endif /* SYSCALL_TRAP */
508 return read_memory_integer (read_register (SP_REGNUM), 4);
512 _initialize_m68k_tdep ()
514 tm_print_insn = print_insn_m68k;