1 /* Target-dependent code for MItsubishi D10V, for GDB.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of GDB.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "gdb_string.h"
32 void d10v_frame_find_saved_regs PARAMS ((struct frame_info *fi, struct frame_saved_regs *fsr));
33 static void d10v_pop_dummy_frame PARAMS ((struct frame_info *fi));
35 /* Discard from the stack the innermost frame,
36 restoring all saved registers. */
41 struct frame_info *frame = get_current_frame ();
44 struct frame_saved_regs fsr;
47 fp = FRAME_FP (frame);
50 d10v_pop_dummy_frame(frame);
54 /* fill out fsr with the address of where each */
55 /* register was stored in the frame */
56 get_frame_saved_regs (frame, &fsr);
58 /* now update the current registers with the old values */
59 for (regnum = A0_REGNUM; regnum < A0_REGNUM+2 ; regnum++)
63 read_memory (fsr.regs[regnum], raw_buffer, 8);
64 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 8);
67 for (regnum = 0; regnum < SP_REGNUM; regnum++)
71 write_register (regnum, read_memory_unsigned_integer (fsr.regs[regnum], 2));
74 if (fsr.regs[PSW_REGNUM])
76 write_register (PSW_REGNUM, read_memory_unsigned_integer (fsr.regs[PSW_REGNUM], 2));
79 write_register (PC_REGNUM, read_register(13));
80 write_register (SP_REGNUM, fp + frame->size);
81 target_store_registers (-1);
82 flush_cached_frames ();
90 if ((op & 0x7E1F) == 0x6C1F)
94 if ((op & 0x7E3F) == 0x6E1F)
98 if ((op & 0x7FE1) == 0x01E1)
110 if ((op & 0x7E1F) == 0x681E)
114 if ((op & 0x7E3F) == 0x3A1E)
121 d10v_skip_prologue (pc)
125 unsigned short op1, op2;
126 CORE_ADDR func_addr, func_end;
127 struct symtab_and_line sal;
129 /* If we have line debugging information, then the end of the */
130 /* prologue should the first assembly instruction of the first source line */
131 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
133 sal = find_pc_line (func_addr, 0);
134 if ( sal.end && sal.end < func_end)
138 if (target_read_memory (pc, (char *)&op, 4))
139 return pc; /* Can't access it -- assume no prologue. */
143 op = (unsigned long)read_memory_integer (pc, 4);
144 if ((op & 0xC0000000) == 0xC0000000)
146 /* long instruction */
147 if ( ((op & 0x3FFF0000) != 0x01FF0000) && /* add3 sp,sp,n */
148 ((op & 0x3F0F0000) != 0x340F0000) && /* st rn, @(offset,sp) */
149 ((op & 0x3F1F0000) != 0x350F0000)) /* st2w rn, @(offset,sp) */
154 /* short instructions */
155 if ((op & 0xC0000000) == 0x80000000)
157 op2 = (op & 0x3FFF8000) >> 15;
162 op1 = (op & 0x3FFF8000) >> 15;
165 if (check_prologue(op1))
167 if (!check_prologue(op2))
169 /* if the previous opcode was really part of the prologue */
170 /* and not just a NOP, then we want to break after both instructions */
184 /* Given a GDB frame, determine the address of the calling function's frame.
185 This will be used to create a new GDB frame struct, and then
186 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
190 d10v_frame_chain (frame)
191 struct frame_info *frame;
193 struct frame_saved_regs fsr;
195 d10v_frame_find_saved_regs (frame, &fsr);
197 if (frame->return_pc == IMEM_START)
200 if (!fsr.regs[FP_REGNUM])
202 if (!fsr.regs[SP_REGNUM] || fsr.regs[SP_REGNUM] == STACK_START)
205 return fsr.regs[SP_REGNUM];
208 if (!read_memory_unsigned_integer(fsr.regs[FP_REGNUM],2))
211 return read_memory_unsigned_integer(fsr.regs[FP_REGNUM],2)| DMEM_START;
214 static int next_addr, uses_frame;
217 prologue_find_regs (op, fsr, addr)
219 struct frame_saved_regs *fsr;
225 if ((op & 0x7E1F) == 0x6C1F)
227 n = (op & 0x1E0) >> 5;
229 fsr->regs[n] = next_addr;
234 else if ((op & 0x7E3F) == 0x6E1F)
236 n = (op & 0x1E0) >> 5;
238 fsr->regs[n] = next_addr;
239 fsr->regs[n+1] = next_addr+2;
244 if ((op & 0x7FE1) == 0x01E1)
246 n = (op & 0x1E) >> 1;
265 if ((op & 0x7E1F) == 0x681E)
267 n = (op & 0x1E0) >> 5;
268 fsr->regs[n] = next_addr;
273 if ((op & 0x7E3F) == 0x3A1E)
275 n = (op & 0x1E0) >> 5;
276 fsr->regs[n] = next_addr;
277 fsr->regs[n+1] = next_addr+2;
284 /* Put here the code to store, into a struct frame_saved_regs, the
285 addresses of the saved registers of frame described by FRAME_INFO.
286 This includes special registers such as pc and fp saved in special
287 ways in the stack frame. sp is even more special: the address we
288 return for it IS the sp for the next frame. */
290 d10v_frame_find_saved_regs (fi, fsr)
291 struct frame_info *fi;
292 struct frame_saved_regs *fsr;
296 unsigned short op1, op2;
300 memset (fsr, 0, sizeof (*fsr));
303 pc = get_pc_function_start (fi->pc);
308 op = (unsigned long)read_memory_integer (pc, 4);
309 if ((op & 0xC0000000) == 0xC0000000)
311 /* long instruction */
312 if ((op & 0x3FFF0000) == 0x01FF0000)
315 short n = op & 0xFFFF;
318 else if ((op & 0x3F0F0000) == 0x340F0000)
320 /* st rn, @(offset,sp) */
321 short offset = op & 0xFFFF;
322 short n = (op >> 20) & 0xF;
323 fsr->regs[n] = next_addr + offset;
325 else if ((op & 0x3F1F0000) == 0x350F0000)
327 /* st2w rn, @(offset,sp) */
328 short offset = op & 0xFFFF;
329 short n = (op >> 20) & 0xF;
330 fsr->regs[n] = next_addr + offset;
331 fsr->regs[n+1] = next_addr + offset + 2;
338 /* short instructions */
339 if ((op & 0xC0000000) == 0x80000000)
341 op2 = (op & 0x3FFF8000) >> 15;
346 op1 = (op & 0x3FFF8000) >> 15;
349 if (!prologue_find_regs(op1,fsr,pc) || !prologue_find_regs(op2,fsr,pc))
355 fi->size = -next_addr;
358 fp = read_register(SP_REGNUM) | DMEM_START;
360 for (i=0; i<NUM_REGS-1; i++)
363 fsr->regs[i] = fp - (next_addr - fsr->regs[i]);
366 if (fsr->regs[LR_REGNUM])
367 fi->return_pc = ((read_memory_unsigned_integer(fsr->regs[LR_REGNUM],2) - 1) << 2) | IMEM_START;
369 fi->return_pc = ((read_register(LR_REGNUM) - 1) << 2) | IMEM_START;
371 /* th SP is not normally (ever?) saved, but check anyway */
372 if (!fsr->regs[SP_REGNUM])
374 /* if the FP was saved, that means the current FP is valid, */
375 /* otherwise, it isn't being used, so we use the SP instead */
377 fsr->regs[SP_REGNUM] = read_register(FP_REGNUM) + fi->size;
380 fsr->regs[SP_REGNUM] = fp + fi->size;
382 fsr->regs[FP_REGNUM] = 0;
388 d10v_init_extra_frame_info (fromleaf, fi)
390 struct frame_info *fi;
392 struct frame_saved_regs dummy;
394 if (fi->next && ((fi->pc & 0xffff) == 0))
395 fi->pc = fi->next->return_pc;
397 d10v_frame_find_saved_regs (fi, &dummy);
401 show_regs (args, from_tty)
405 long long num1, num2;
406 printf_filtered ("PC=%04x (0x%x) PSW=%04x RPT_S=%04x RPT_E=%04x RPT_C=%04x\n",
407 read_register (PC_REGNUM), (read_register (PC_REGNUM) << 2) + IMEM_START,
408 read_register (PSW_REGNUM),
412 printf_filtered ("R0-R7 %04x %04x %04x %04x %04x %04x %04x %04x\n",
421 printf_filtered ("R8-R15 %04x %04x %04x %04x %04x %04x %04x %04x\n",
430 printf_filtered ("IMAP0 %04x IMAP1 %04x DMAP %04x\n",
431 read_register (IMAP0_REGNUM),
432 read_register (IMAP1_REGNUM),
433 read_register (DMAP_REGNUM));
434 read_register_gen (A0_REGNUM, (char *)&num1);
435 read_register_gen (A0_REGNUM+1, (char *)&num2);
436 printf_filtered ("A0-A1 %010llx %010llx\n",num1, num2);
440 d10v_xlate_addr (addr)
446 imap = (int)read_register(IMAP0_REGNUM);
448 imap = (int)read_register(IMAP1_REGNUM);
451 return (CORE_ADDR)(addr + 0x1000000);
452 return (CORE_ADDR)(addr + (imap & 0xff)*0x20000);
460 int save_pid, retval;
462 save_pid = inferior_pid;
464 retval = (int)read_register (PC_REGNUM);
465 inferior_pid = save_pid;
466 return d10v_xlate_addr(retval << 2);
470 d10v_write_pc (val, pid)
476 save_pid = inferior_pid;
478 write_register (PC_REGNUM, (val & 0x3ffff) >> 2);
479 inferior_pid = save_pid;
485 return (read_register(SP_REGNUM) | DMEM_START);
492 write_register (SP_REGNUM, (LONGEST)(val & 0xffff));
496 d10v_fix_call_dummy (dummyname, start_sp, fun, nargs, args, type, gcc_p)
507 char buffer[MAX_REGISTER_RAW_SIZE];
508 struct frame_info *frame = get_current_frame ();
509 frame->dummy = start_sp;
510 start_sp |= DMEM_START;
513 for (regnum = 0; regnum < NUM_REGS; regnum++)
515 sp -= REGISTER_RAW_SIZE(regnum);
516 store_address (buffer, REGISTER_RAW_SIZE(regnum), read_register(regnum));
517 write_memory (sp, buffer, REGISTER_RAW_SIZE(regnum));
519 write_register (SP_REGNUM, (LONGEST)(sp & 0xffff));
520 /* now we need to load LR with the return address */
521 write_register (LR_REGNUM, (LONGEST)(d10v_call_dummy_address() & 0xffff) >> 2);
526 d10v_pop_dummy_frame (fi)
527 struct frame_info *fi;
529 CORE_ADDR sp = fi->dummy;
532 for (regnum = 0; regnum < NUM_REGS; regnum++)
534 sp -= REGISTER_RAW_SIZE(regnum);
535 write_register(regnum, read_memory_unsigned_integer (sp, REGISTER_RAW_SIZE(regnum)));
537 flush_cached_frames (); /* needed? */
542 d10v_push_arguments (nargs, args, sp, struct_return, struct_addr)
547 CORE_ADDR struct_addr;
549 int i, len, index=0, regnum=2;
550 char buffer[4], *contents;
554 /* Pass 1. Put all large args on stack */
555 for (i = 0; i < nargs; i++)
557 value_ptr arg = args[i];
558 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
559 len = TYPE_LENGTH (arg_type);
560 contents = VALUE_CONTENTS(arg);
561 val = extract_signed_integer (contents, len);
564 /* put on stack and pass pointers */
566 write_memory (sp, contents, len);
573 for (i = 0; i < nargs; i++)
575 value_ptr arg = args[i];
576 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
577 len = TYPE_LENGTH (arg_type);
578 contents = VALUE_CONTENTS(arg);
579 val = extract_signed_integer (contents, len);
582 /* use a pointer to previously saved data */
584 write_register (regnum++, ptrs[index++]);
587 /* no more registers available. put it on the stack */
589 store_address (buffer, 2, ptrs[index++]);
590 write_memory (sp, buffer, 2);
598 write_register (regnum++, val>>16);
599 write_register (regnum++, val & 0xffff);
604 store_address (buffer, len, val);
605 write_memory (sp, buffer, len);
613 /* pick an out-of-the-way place to set the return value */
614 /* for an inferior function call. The link register is set to this */
615 /* value and a momentary breakpoint is set there. When the breakpoint */
616 /* is hit, the dummy frame is popped and the previous environment is */
620 d10v_call_dummy_address ()
623 struct minimal_symbol *sym;
625 entry = entry_point_address ();
630 sym = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
632 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
635 return SYMBOL_VALUE_ADDRESS (sym);
638 /* Given a return value in `regbuf' with a type `valtype',
639 extract and copy its value into `valbuf'. */
642 d10v_extract_return_value (valtype, regbuf, valbuf)
643 struct type *valtype;
644 char regbuf[REGISTER_BYTES];
647 memcpy (valbuf, regbuf + REGISTER_BYTE (2), TYPE_LENGTH (valtype));
650 /* The following code implements access to, and display of, the D10V's
651 instruction trace buffer. The buffer consists of 64K or more
652 4-byte words of data, of which each words includes an 8-bit count,
653 and 8-bit segment number, and a 16-bit instruction address.
655 In theory, the trace buffer is continuously capturing instruction
656 data that the CPU presents on its "debug bus", but in practice, the
657 ROMified GDB stub only enables tracing when it continues or steps
658 the program, and stops tracing when the program stops; so it
659 actually works for GDB to read the buffer counter out of memory and
660 then read each trace word. The counter records where the tracing
661 stops, but there is no record of where it started, so we remember
662 the PC when we resumed and then search backwards in the trace
663 buffer for a word that includes that address. This is not perfect,
664 because you will miss trace data if the resumption PC is the target
665 of a branch. (The value of the buffer counter is semi-random, any
666 trace data from a previous program stop is gone.) */
668 /* The address of the last word recorded in the trace buffer. */
670 #define DBBC_ADDR (0xd80000)
672 /* The base of the trace buffer, at least for the "Board_0". */
674 #define TRACE_BUFFER_BASE (0xf40000)
676 static void trace_command PARAMS ((char *, int));
678 static void untrace_command PARAMS ((char *, int));
680 static void trace_info PARAMS ((char *, int));
682 static void tdisassemble_command PARAMS ((char *, int));
684 static void display_trace PARAMS ((int, int));
686 /* True when instruction traces are being collected. */
692 static CORE_ADDR last_pc;
694 static int trace_display;
696 struct trace_buffer {
703 trace_command (args, from_tty)
707 /* Clear the host-side trace buffer, allocating space if needed. */
709 if (trace_data.counts == NULL)
710 trace_data.counts = (short *) xmalloc (65536 * sizeof(short));
711 if (trace_data.addrs == NULL)
712 trace_data.addrs = (CORE_ADDR *) xmalloc (65536 * sizeof(CORE_ADDR));
716 printf_filtered ("Tracing is now on.\n");
720 untrace_command (args, from_tty)
726 printf_filtered ("Tracing is now off.\n");
730 trace_info (args, from_tty)
736 printf_filtered ("%d entries in trace buffer:\n", trace_data.size);
738 for (i = 0; i < trace_data.size; ++i)
740 printf_filtered ("%d: %d instruction%s at 0x%x\n",
741 i, trace_data.counts[i],
742 (trace_data.counts[i] == 1 ? "" : "s"),
743 trace_data.addrs[i]);
746 printf_filtered ("Tracing is currently %s.\n", (tracing ? "on" : "off"));
749 /* Print the instruction at address MEMADDR in debugged memory,
750 on STREAM. Returns length of the instruction, in bytes. */
753 print_insn (memaddr, stream)
757 /* If there's no disassembler, something is very wrong. */
758 if (tm_print_insn == NULL)
761 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
762 tm_print_insn_info.endian = BFD_ENDIAN_BIG;
764 tm_print_insn_info.endian = BFD_ENDIAN_LITTLE;
765 return (*tm_print_insn) (memaddr, &tm_print_insn_info);
769 d10v_eva_prepare_to_trace ()
774 last_pc = read_register (PC_REGNUM);
777 /* Collect trace data from the target board and format it into a form
778 more useful for display. */
781 d10v_eva_get_trace_data ()
783 int count, i, j, oldsize;
784 int trace_addr, trace_seg, trace_cnt, next_cnt;
785 unsigned int last_trace, trace_word, next_word;
786 unsigned int *tmpspace;
791 tmpspace = xmalloc (65536 * sizeof(unsigned int));
793 last_trace = read_memory_unsigned_integer (DBBC_ADDR, 2) << 2;
796 printf_filtered("Last pc is %x, is now %x\n",
797 last_pc, read_register (PC_REGNUM));
800 /* Collect buffer contents from the target, stopping when we reach
801 the word recorded when execution resumed. */
804 while (last_trace > 0)
808 read_memory_unsigned_integer (TRACE_BUFFER_BASE + last_trace, 4);
809 trace_addr = trace_word & 0xffff;
811 trace_seg = (trace_word >> 16) & 0xff;
812 trace_cnt = (trace_word >> 24) & 0xff;
813 printf_filtered("Trace word at %x is %x %x %x\n", last_trace,
814 trace_cnt, trace_seg, trace_addr);
817 /* Ignore an apparently nonsensical entry. */
818 if (trace_addr == 0xffd5)
820 tmpspace[count++] = trace_word;
821 if (trace_addr == last_pc)
827 /* Move the data to the host-side trace buffer, adjusting counts to
828 include the last instruction executed and transforming the address
829 into something that GDB likes. */
831 for (i = 0; i < count; ++i)
833 trace_word = tmpspace[i];
834 next_word = ((i == 0) ? 0 : tmpspace[i - 1]);
835 trace_addr = trace_word & 0xffff;
836 next_cnt = (next_word >> 24) & 0xff;
837 j = trace_data.size + count - i - 1;
838 trace_data.addrs[j] = (trace_addr << 2) + 0x1000000;
839 trace_data.counts[j] = next_cnt + 1;
842 oldsize = trace_data.size;
843 trace_data.size += count;
848 for (i = 0; i < trace_data.size; ++i)
850 printf_filtered("%d insns after %x\n",
851 trace_data.counts[i], trace_data.addrs[i]);
856 display_trace (oldsize, trace_data.size);
860 tdisassemble_command (arg, from_tty)
871 high = trace_data.size;
873 else if (!(space_index = (char *) strchr (arg, ' ')))
875 low = parse_and_eval_address (arg);
882 low = parse_and_eval_address (arg);
883 high = parse_and_eval_address (space_index + 1);
886 printf_filtered ("Dump of trace ");
887 printf_filtered ("from ");
888 print_address_numeric (low, 1, gdb_stdout);
889 printf_filtered (" to ");
890 print_address_numeric (high, 1, gdb_stdout);
891 printf_filtered (":\n");
893 display_trace (low, high);
895 printf_filtered ("End of trace dump.\n");
896 gdb_flush (gdb_stdout);
900 display_trace (low, high)
904 CORE_ADDR next_address;
906 for (i = low; i < high; ++i)
908 next_address = trace_data.addrs[i];
909 count = trace_data.counts[i];
913 print_address (next_address, gdb_stdout);
914 printf_filtered (":");
915 printf_filtered ("\t");
917 next_address = next_address + print_insn (next_address, gdb_stdout);
918 printf_filtered ("\n");
919 gdb_flush (gdb_stdout);
925 _initialize_d10v_tdep ()
927 tm_print_insn = print_insn_d10v;
929 add_com ("regs", class_vars, show_regs, "Print all registers");
931 add_com ("trace", class_support, trace_command,
932 "Enable tracing of instruction execution.");
934 add_com ("untrace", class_support, untrace_command,
935 "Disable tracing of instruction execution.");
937 add_com ("tdisassemble", class_vars, tdisassemble_command,
938 "Disassemble the trace buffer.\n\
939 Two optional arguments specify a range of trace buffer entries\n\
940 as reported by info trace (NOT addresses!).");
942 add_info ("trace", trace_info,
943 "Display info about the trace data buffer.");
945 add_show_from_set (add_set_cmd ("tracedisplay", no_class,
946 var_integer, (char *)&trace_display,
947 "Set automatic display of trace.\n", &setlist),