xref: /openbsd-src/gnu/usr.bin/binutils/gdb/sparcnbsd-nat.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* Functions specific to running gdb native on a SPARC running NetBSD
2    Copyright 1989, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
3 
4 This file is part of GDB.
5 
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.
10 
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.
15 
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.  */
19 
20 #include <sys/types.h>
21 #include <sys/ptrace.h>
22 #include <machine/reg.h>
23 #include <machine/frame.h>
24 #include <machine/pcb.h>
25 
26 #include "defs.h"
27 #include "inferior.h"
28 #include "target.h"
29 #include "gdbcore.h"
30 
31 /* We don't store all registers immediately when requested, since they
32    get sent over in large chunks anyway.  Instead, we accumulate most
33    of the changes and send them over once.  "deferred_stores" keeps
34    track of which sets of registers we have locally-changed copies of,
35    so we only need send the groups that have changed.  */
36 
37 #define	INT_REGS	1
38 #define	STACK_REGS	2
39 #define	FP_REGS		4
40 
41 /* Fetch one or more registers from the inferior.  REGNO == -1 to get
42    them all.  We actually fetch more than requested, when convenient,
43    marking them as valid so we won't fetch them again.  */
44 
45 void
46 fetch_inferior_registers (regno)
47      int regno;
48 {
49   struct reg inferior_registers;
50   struct fpreg inferior_fp_registers;
51   int save_g0;
52   int i;
53 
54   /* We should never be called with deferred stores, because a prerequisite
55      for writing regs is to have fetched them all (PREPARE_TO_STORE), sigh.  */
56   if (deferred_stores) abort();
57 
58   DO_DEFERRED_STORES;
59 
60   /* Global and Out regs are fetched directly, as well as the control
61      registers.  If we're getting one of the in or local regs,
62      and the stack pointer has not yet been fetched,
63      we have to do that first, since they're found in memory relative
64      to the stack pointer.  */
65   if (regno < O7_REGNUM  /* including -1 */
66       || regno >= Y_REGNUM
67       || (!register_valid[SP_REGNUM] && regno < I7_REGNUM))
68     {
69       if (0 != ptrace (PT_GETREGS, inferior_pid,
70 		       (PTRACE_ARG3_TYPE) &inferior_registers, 0))
71 	perror("ptrace_getregs");
72 
73       /* Copy them (in order shown in reg.h) */
74       memcpy (&registers[REGISTER_BYTE (G0_REGNUM)],
75 	      &inferior_registers.r_global[0],
76 	      sizeof(inferior_registers.r_global));
77       memcpy (&registers[REGISTER_BYTE (O0_REGNUM)],
78 	      &inferior_registers.r_out[0],
79 	      sizeof(inferior_registers.r_out));
80       *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] =
81 	inferior_registers.r_psr;
82       *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] =
83 	inferior_registers.r_pc;
84       *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)] =
85 	inferior_registers.r_npc;
86       *(int *)&registers[REGISTER_BYTE (Y_REGNUM)] =
87 	inferior_registers.r_y;
88 
89       /*
90        * Note that the G0 slot actually carries the
91        * value of the %wim register, and G0 is zero.
92        */
93       *(int *)&registers[REGISTER_BYTE(WIM_REGNUM)] =
94         *(int *)&registers[REGISTER_BYTE(G0_REGNUM)];
95       *(int *)&registers[REGISTER_BYTE(G0_REGNUM)] = 0;
96 
97       /* Mark what is valid (not the %i regs). */
98       for (i = G0_REGNUM; i <= O7_REGNUM; i++)
99 	register_valid[i] = 1;
100       register_valid[PS_REGNUM] = 1;
101       register_valid[PC_REGNUM] = 1;
102       register_valid[NPC_REGNUM] = 1;
103       register_valid[Y_REGNUM] = 1;
104       register_valid[WIM_REGNUM] = 1;
105 
106       /* If we don't set these valid, read_register_bytes() rereads
107 	 all the regs every time it is called!  FIXME.  */
108       register_valid[TBR_REGNUM] = 1;	/* Not true yet, FIXME */
109       register_valid[CPS_REGNUM] = 1;	/* Not true yet, FIXME */
110     }
111 
112   /* Floating point registers */
113   if (regno == -1 || regno == FPS_REGNUM ||
114       (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31))
115     {
116       if (0 != ptrace (PT_GETFPREGS, inferior_pid,
117 		       (PTRACE_ARG3_TYPE) &inferior_fp_registers,
118 		       0))
119 	perror("ptrace_getfpregs");
120       memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)],
121 	      &inferior_fp_registers.fr_regs[0],
122 	      sizeof (inferior_fp_registers.fr_regs));
123       memcpy (&registers[REGISTER_BYTE (FPS_REGNUM)],
124 	      &inferior_fp_registers.fr_fsr,
125 	      sizeof (inferior_fp_registers.fr_fsr));
126       for (i = FP0_REGNUM; i <= FP0_REGNUM+31; i++)
127 	register_valid[i] = 1;
128       register_valid[FPS_REGNUM] = 1;
129     }
130 
131   /* These regs are saved on the stack by the kernel.  Only read them
132      all (16 ptrace calls!) if we really need them.  */
133   if (regno == -1)
134     {
135       target_xfer_memory (*(CORE_ADDR*)&registers[REGISTER_BYTE (SP_REGNUM)],
136 		          &registers[REGISTER_BYTE (L0_REGNUM)],
137 			  16*REGISTER_RAW_SIZE (L0_REGNUM), 0);
138       for (i = L0_REGNUM; i <= I7_REGNUM; i++)
139 	register_valid[i] = 1;
140     }
141   else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
142     {
143       CORE_ADDR sp = *(CORE_ADDR*)&registers[REGISTER_BYTE (SP_REGNUM)];
144       i = REGISTER_BYTE (regno);
145       if (register_valid[regno])
146 	printf_unfiltered("register %d valid and read\n", regno);
147       target_xfer_memory (sp + i - REGISTER_BYTE (L0_REGNUM),
148 			  &registers[i], REGISTER_RAW_SIZE (regno), 0);
149       register_valid[regno] = 1;
150     }
151 }
152 
153 /* Store our register values back into the inferior.
154    If REGNO is -1, do this for all registers.
155    Otherwise, REGNO specifies which register (so we can save time).  */
156 
157 void
158 store_inferior_registers (regno)
159      int regno;
160 {
161   struct reg inferior_registers;
162   struct fpreg inferior_fp_registers;
163   int wanna_store = INT_REGS + STACK_REGS + FP_REGS;
164   int save_g0;
165 
166   /* First decide which pieces of machine-state we need to modify.
167      Default for regno == -1 case is all pieces.  */
168   if (regno >= 0)
169     if (FP0_REGNUM <= regno && regno < FP0_REGNUM + 32)
170       {
171 	wanna_store = FP_REGS;
172       }
173     else
174       {
175 	if (regno == SP_REGNUM)
176 	  wanna_store = INT_REGS + STACK_REGS;
177 	else if (regno < L0_REGNUM || regno > I7_REGNUM)
178 	  wanna_store = INT_REGS;
179 	else if (regno == FPS_REGNUM)
180 	  wanna_store = FP_REGS;
181 	else
182 	  wanna_store = STACK_REGS;
183       }
184 
185   /* See if we're forcing the stores to happen now, or deferring. */
186   if (regno == -2)
187     {
188       wanna_store = deferred_stores;
189       deferred_stores = 0;
190     }
191   else
192     {
193       if (wanna_store == STACK_REGS)
194 	{
195 	  /* Fall through and just store one stack reg.  If we deferred
196 	     it, we'd have to store them all, or remember more info.  */
197 	}
198       else
199 	{
200 	  deferred_stores |= wanna_store;
201 	  return;
202 	}
203     }
204 
205   if (wanna_store & STACK_REGS)
206     {
207       CORE_ADDR sp = *(CORE_ADDR *)&registers[REGISTER_BYTE (SP_REGNUM)];
208 
209       if (regno < 0 || regno == SP_REGNUM)
210 	{
211 	  if (!register_valid[L0_REGNUM+5]) abort();
212 	  target_xfer_memory (sp,
213 			      &registers[REGISTER_BYTE (L0_REGNUM)],
214 			      16*REGISTER_RAW_SIZE (L0_REGNUM), 1);
215 	}
216       else
217 	{
218 	  if (!register_valid[regno]) abort();
219 	  target_xfer_memory ((sp + REGISTER_BYTE (regno) -
220 			       REGISTER_BYTE (L0_REGNUM)),
221 			      &registers[REGISTER_BYTE (regno)],
222 			      REGISTER_RAW_SIZE (regno), 1);
223 	}
224 
225     }
226 
227   if (wanna_store & INT_REGS)
228     {
229       if (!register_valid[G1_REGNUM]) abort();
230 
231       /* The G0 slot really holds %wim (leave it alone). */
232       save_g0 = inferior_registers.r_global[0];
233       memcpy (&inferior_registers.r_global[0],
234 	      &registers[REGISTER_BYTE (G0_REGNUM)],
235 	      sizeof(inferior_registers.r_global));
236       inferior_registers.r_global[0] = save_g0;
237       memcpy (&inferior_registers.r_out[0],
238 	      &registers[REGISTER_BYTE (O0_REGNUM)],
239 	      sizeof(inferior_registers.r_out));
240 
241       inferior_registers.r_psr =
242 	*(int *)&registers[REGISTER_BYTE (PS_REGNUM)];
243       inferior_registers.r_pc =
244 	*(int *)&registers[REGISTER_BYTE (PC_REGNUM)];
245       inferior_registers.r_npc =
246 	*(int *)&registers[REGISTER_BYTE (NPC_REGNUM)];
247       inferior_registers.r_y =
248 	*(int *)&registers[REGISTER_BYTE (Y_REGNUM)];
249 
250       if (0 != ptrace (PT_SETREGS, inferior_pid,
251 		       (PTRACE_ARG3_TYPE) &inferior_registers, 0))
252 	perror("ptrace_setregs");
253     }
254 
255   if (wanna_store & FP_REGS)
256     {
257       if (!register_valid[FP0_REGNUM+9]) abort();
258       memcpy (&inferior_fp_registers.fr_regs[0],
259 	      &registers[REGISTER_BYTE (FP0_REGNUM)],
260 	      sizeof(inferior_fp_registers.fr_regs));
261       memcpy (&inferior_fp_registers.fr_fsr,
262 	      &registers[REGISTER_BYTE (FPS_REGNUM)],
263 	      sizeof(inferior_fp_registers.fr_fsr));
264       if (0 !=
265 	 ptrace (PT_SETFPREGS, inferior_pid,
266 		 (PTRACE_ARG3_TYPE) &inferior_fp_registers, 0))
267 	 perror("ptrace_setfpregs");
268     }
269 }
270 
271 
272 static void
273 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
274      char *core_reg_sect;
275      unsigned core_reg_size;
276      int which;
277      unsigned int reg_addr;	/* Unused in this version */
278 {
279   struct md_coredump *core_reg;
280   struct trapframe *tf;
281   struct fpstate *fs;
282 
283   core_reg = (struct md_coredump *)core_reg_sect;
284   tf = &core_reg->md_tf;
285   fs = &core_reg->md_fpstate;
286 
287   /* We get everything from the .reg section. */
288   if (which != 0)
289     return;
290 
291   if (core_reg_size < sizeof(*core_reg)) {
292     fprintf_unfiltered (gdb_stderr, "Couldn't read regs from core file\n");
293     return;
294   }
295 
296   /* Integer registers */
297   memcpy(&registers[REGISTER_BYTE (G0_REGNUM)],
298 	 &tf->tf_global[0], sizeof(tf->tf_global));
299   memcpy(&registers[REGISTER_BYTE (O0_REGNUM)],
300 	 &tf->tf_out[0], sizeof(tf->tf_out));
301   *(int *)&registers[REGISTER_BYTE (PS_REGNUM)]  = tf->tf_psr;
302   *(int *)&registers[REGISTER_BYTE (PC_REGNUM)]  = tf->tf_pc;
303   *(int *)&registers[REGISTER_BYTE (NPC_REGNUM)] = tf->tf_npc;
304   *(int *)&registers[REGISTER_BYTE (Y_REGNUM)]   = tf->tf_y;
305 
306   /* Clear out the G0 slot (see reg.h) */
307   *(int *)&registers[REGISTER_BYTE(G0_REGNUM)] = 0;
308 
309   /* My best guess at where to get the locals and input
310      registers is exactly where they usually are, right above
311      the stack pointer.  If the core dump was caused by a bus error
312      from blowing away the stack pointer (as is possible) then this
313      won't work, but it's worth the try. */
314   {
315     int sp;
316 
317     sp = *(int *)&registers[REGISTER_BYTE (SP_REGNUM)];
318     if (0 != target_read_memory (sp, &registers[REGISTER_BYTE (L0_REGNUM)],
319 				 16 * REGISTER_RAW_SIZE (L0_REGNUM)))
320       {
321 	/* fprintf_unfiltered so user can still use gdb */
322 	fprintf_unfiltered (gdb_stderr,
323 		"Couldn't read input and local registers from core file\n");
324       }
325   }
326 
327   /* Floating point registers */
328   memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)],
329 	  &fs->fs_regs[0], sizeof (fs->fs_regs));
330   memcpy (&registers[REGISTER_BYTE (FPS_REGNUM)],
331 	  &fs->fs_fsr,	sizeof (fs->fs_fsr));
332 
333   registers_fetched ();
334 }
335 
336 /* Register that we are able to handle sparcnbsd core file formats.
337    FIXME: is this really bfd_target_unknown_flavour? */
338 
339 static struct core_fns nat_core_fns =
340 {
341   bfd_target_unknown_flavour,
342   fetch_core_registers,
343   NULL
344 };
345 
346 void
347 _initialize_sparcnbsd_nat ()
348 {
349   add_core_fns (&nat_core_fns);
350 }
351 
352 
353 /*
354  * kernel_u_size() is not helpful on NetBSD because
355  * the "u" struct is NOT in the core dump file.
356  */
357 
358 #ifdef	FETCH_KCORE_REGISTERS
359 /*
360  * Get registers from a kernel crash dump or live kernel.
361  * Called by kcore-nbsd.c:get_kcore_registers().
362  */
363 void
364 fetch_kcore_registers (pcb)
365      struct pcb *pcb;
366 {
367   struct rwindow win;
368   int i;
369   u_long sp;
370 
371   /* We only do integer registers */
372   sp = pcb->pcb_sp;
373 
374   supply_register(SP_REGNUM, (char *)&pcb->pcb_sp);
375   supply_register(PC_REGNUM, (char *)&pcb->pcb_pc);
376   supply_register(O7_REGNUM, (char *)&pcb->pcb_pc);
377   supply_register(PS_REGNUM, (char *)&pcb->pcb_psr);
378   supply_register(WIM_REGNUM, (char *)&pcb->pcb_wim);
379   /*
380    * Read last register window saved on stack.
381    */
382   if (target_read_memory(sp, (char *)&win, sizeof win)) {
383     printf("cannot read register window at sp=%x\n", pcb->pcb_sp);
384     bzero((char *)&win, sizeof win);
385   }
386   for (i = 0; i < sizeof(win.rw_local); ++i)
387     supply_register(i + L0_REGNUM, (char *)&win.rw_local[i]);
388   for (i = 0; i < sizeof(win.rw_in); ++i)
389     supply_register(i + I0_REGNUM, (char *)&win.rw_in[i]);
390   /*
391    * read the globals & outs saved on the stack (for a trap frame).
392    */
393   sp += 92 + 12; /* XXX - MINFRAME + R_Y */
394   for (i = 1; i < 14; ++i) {
395     u_long val;
396 
397     if (target_read_memory(sp + i*4, (char *)&val, sizeof val) == 0)
398       supply_register(i, (char *)&val);
399   }
400 #if 0
401   if (kvread(pcb.pcb_cpctxp, &cps) == 0)
402     supply_register(CPS_REGNUM, (char *)&cps);
403 #endif
404 
405   /* The kernel does not use the FPU, so ignore it. */
406   registers_fetched ();
407 }
408 #endif	/* FETCH_KCORE_REGISTERS */
409