1 /* Native-dependent code for OpenBSD/mips64.
2 
3    Copyright 2004 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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.  */
21 
22 #include "defs.h"
23 #include "inferior.h"
24 #include "regcache.h"
25 #include "target.h"
26 
27 #include <sys/types.h>
28 #include <sys/ptrace.h>
29 #include <machine/reg.h>
30 
31 #include "mips-tdep.h"
32 #include "inf-ptrace.h"
33 #include "obsd-nat.h"
34 
35 /* Shorthand for some register numbers used below.  */
36 #define MIPS_PC_REGNUM	MIPS_EMBED_PC_REGNUM
37 #define MIPS_FP0_REGNUM	MIPS_EMBED_FP0_REGNUM
38 #define MIPS_FSR_REGNUM MIPS_EMBED_FP0_REGNUM + 32
39 
40 /* Supply the general-purpose registers stored in GREGS to REGCACHE.  */
41 
42 static void
43 mips64obsd_supply_gregset (struct regcache *regcache, const void *gregs)
44 {
45   const char *regs = gregs;
46   int regnum;
47 
48   for (regnum = MIPS_ZERO_REGNUM; regnum <= MIPS_PC_REGNUM; regnum++)
49     regcache_raw_supply (regcache, regnum, regs + regnum * 8);
50 
51   for (regnum = MIPS_FP0_REGNUM; regnum <= MIPS_FSR_REGNUM; regnum++)
52     regcache_raw_supply (regcache, regnum, regs + (regnum + 2) * 8);
53 }
54 
55 /* Collect the general-purpose registers from REGCACHE and store them
56    in GREGS.  */
57 
58 static void
59 mips64obsd_collect_gregset (const struct regcache *regcache,
60 			    void *gregs, int regnum)
61 {
62   char *regs = gregs;
63   int i;
64 
65   for (i = MIPS_ZERO_REGNUM; i <= MIPS_PC_REGNUM; i++)
66     {
67       if (regnum == -1 || regnum == i)
68 	regcache_raw_collect (regcache, i, regs + i * 8);
69     }
70 
71   for (i = MIPS_FP0_REGNUM; i <= MIPS_FSR_REGNUM; i++)
72     {
73       if (regnum == -1 || regnum == i)
74 	regcache_raw_collect (regcache, i, regs + (i + 2) * 8);
75     }
76 }
77 
78 
79 /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
80    for all registers.  */
81 
82 static void
83 mips64obsd_fetch_inferior_registers (int regnum)
84 {
85   struct reg regs;
86   int pid;
87 
88   /* Cater for systems like OpenBSD, that implement threads as
89      separate processes.  */
90   pid = ptid_get_lwp (inferior_ptid);
91   if (pid == 0)
92     pid = ptid_get_pid (inferior_ptid);
93 
94   if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
95     perror_with_name ("Couldn't get registers");
96 
97   mips64obsd_supply_gregset (current_regcache, &regs);
98 }
99 
100 /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
101    this for all registers.  */
102 
103 static void
104 mips64obsd_store_inferior_registers (int regnum)
105 {
106   struct reg regs;
107   int pid;
108 
109   /* Cater for systems like OpenBSD, that implement threads as
110      separate processes.  */
111   pid = ptid_get_lwp (inferior_ptid);
112   if (pid == 0)
113     pid = ptid_get_pid (inferior_ptid);
114 
115   if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
116     perror_with_name ("Couldn't get registers");
117 
118   mips64obsd_collect_gregset (current_regcache, &regs, regnum);
119 
120   if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
121     perror_with_name ("Couldn't write registers");
122 }
123 
124 
125 /* Provide a prototype to silence -Wmissing-prototypes.  */
126 void _initialize_mips64obsd_nat (void);
127 
128 void
129 _initialize_mips64obsd_nat (void)
130 {
131   struct target_ops *t;
132 
133   t = inf_ptrace_target ();
134   t->to_fetch_registers = mips64obsd_fetch_inferior_registers;
135   t->to_store_registers = mips64obsd_store_inferior_registers;
136   t->to_pid_to_str = obsd_pid_to_str;
137   t->to_find_new_threads = obsd_find_new_threads;
138   add_target (t);
139 }
140