1 /* Native-dependent code for HP PA-RISC BSD's. 2 3 Copyright 2004, 2005 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 "obsd-nat.h" 32 #include "hppa-tdep.h" 33 #include "inf-ptrace.h" 34 35 static int 36 hppabsd_gregset_supplies_p (int regnum) 37 { 38 return (regnum >= HPPA_R0_REGNUM && regnum <= HPPA_CR27_REGNUM); 39 } 40 41 static int 42 hppabsd_fpregset_supplies_p (int regnum) 43 { 44 return (regnum >= HPPA_FP0_REGNUM && regnum < HPPA_FP0_REGNUM + 32 * 2); 45 } 46 47 /* Supply the general-purpose registers stored in GREGS to REGCACHE. */ 48 49 static void 50 hppabsd_supply_gregset (struct regcache *regcache, const void *gregs) 51 { 52 const char *regs = gregs; 53 int regnum; 54 55 for (regnum = HPPA_R1_REGNUM; regnum <= HPPA_R31_REGNUM; regnum++) 56 regcache_raw_supply (regcache, regnum, regs + regnum * 4); 57 58 regcache_raw_supply (regcache, HPPA_IPSW_REGNUM, regs); 59 regcache_raw_supply (regcache, HPPA_SAR_REGNUM, regs + 32 * 4); 60 regcache_raw_supply (regcache, HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4); 61 regcache_raw_supply (regcache, HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4); 62 regcache_raw_supply (regcache, HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4); 63 regcache_raw_supply (regcache, HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4); 64 regcache_raw_supply (regcache, HPPA_SR0_REGNUM, regs + 37 * 4); 65 regcache_raw_supply (regcache, HPPA_SR1_REGNUM, regs + 38 * 4); 66 regcache_raw_supply (regcache, HPPA_SR2_REGNUM, regs + 39 * 4); 67 regcache_raw_supply (regcache, HPPA_SR3_REGNUM, regs + 40 * 4); 68 regcache_raw_supply (regcache, HPPA_SR4_REGNUM, regs + 41 * 4); 69 regcache_raw_supply (regcache, HPPA_SR5_REGNUM, regs + 42 * 4); 70 regcache_raw_supply (regcache, HPPA_SR6_REGNUM, regs + 43 * 4); 71 regcache_raw_supply (regcache, HPPA_SR7_REGNUM, regs + 44 * 4); 72 regcache_raw_supply (regcache, HPPA_CR26_REGNUM, regs + 45 * 4); 73 regcache_raw_supply (regcache, HPPA_CR27_REGNUM, regs + 46 * 4); 74 } 75 76 /* Supply the floating-point registers stored in FPREGS to REGCACHE. */ 77 78 static void 79 hppabsd_supply_fpregset (struct regcache *regcache, const void *fpregs) 80 { 81 const char *regs = fpregs; 82 int regnum; 83 84 for (regnum = HPPA_FP0_REGNUM; regnum < HPPA_FP0_REGNUM + 32 * 2; regnum++) 85 regcache_raw_supply (regcache, regnum, 86 regs + (regnum - HPPA_FP0_REGNUM) * 4); 87 } 88 89 /* Collect the general-purpose registers from REGCACHE and store them 90 in GREGS. */ 91 92 static void 93 hppabsd_collect_gregset (const struct regcache *regcache, 94 void *gregs, int regnum) 95 { 96 char *regs = gregs; 97 int i; 98 99 for (i = HPPA_R1_REGNUM; i <= HPPA_R31_REGNUM; i++) 100 { 101 if (regnum == -1 || regnum == i) 102 regcache_raw_collect (regcache, i, regs + i * 4); 103 } 104 105 if (regnum == -1 || regnum == HPPA_IPSW_REGNUM) 106 regcache_raw_collect (regcache, HPPA_IPSW_REGNUM, regs); 107 if (regnum == -1 || regnum == HPPA_SAR_REGNUM) 108 regcache_raw_collect (regcache, HPPA_SAR_REGNUM, regs + 32 * 4); 109 if (regnum == -1 || regnum == HPPA_PCSQ_HEAD_REGNUM) 110 regcache_raw_collect (regcache, HPPA_PCSQ_HEAD_REGNUM, regs + 33 * 4); 111 if (regnum == -1 || regnum == HPPA_PCSQ_TAIL_REGNUM) 112 regcache_raw_collect (regcache, HPPA_PCSQ_TAIL_REGNUM, regs + 34 * 4); 113 if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM) 114 regcache_raw_collect (regcache, HPPA_PCOQ_HEAD_REGNUM, regs + 35 * 4); 115 if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM) 116 regcache_raw_collect (regcache, HPPA_PCOQ_TAIL_REGNUM, regs + 36 * 4); 117 if (regnum == -1 || regnum == HPPA_SR0_REGNUM) 118 regcache_raw_collect (regcache, HPPA_SR0_REGNUM, regs + 37 * 4); 119 if (regnum == -1 || regnum == HPPA_SR1_REGNUM) 120 regcache_raw_collect (regcache, HPPA_SR1_REGNUM, regs + 38 * 4); 121 if (regnum == -1 || regnum == HPPA_SR2_REGNUM) 122 regcache_raw_collect (regcache, HPPA_SR2_REGNUM, regs + 39 * 4); 123 if (regnum == -1 || regnum == HPPA_SR3_REGNUM) 124 regcache_raw_collect (regcache, HPPA_SR3_REGNUM, regs + 40 * 4); 125 if (regnum == -1 || regnum == HPPA_SR4_REGNUM) 126 regcache_raw_collect (regcache, HPPA_SR4_REGNUM, regs + 41 * 4); 127 if (regnum == -1 || regnum == HPPA_SR5_REGNUM) 128 regcache_raw_collect (regcache, HPPA_SR5_REGNUM, regs + 42 * 4); 129 if (regnum == -1 || regnum == HPPA_SR6_REGNUM) 130 regcache_raw_collect (regcache, HPPA_SR6_REGNUM, regs + 43 * 4); 131 if (regnum == -1 || regnum == HPPA_SR7_REGNUM) 132 regcache_raw_collect (regcache, HPPA_SR7_REGNUM, regs + 44 * 4); 133 if (regnum == -1 || regnum == HPPA_CR26_REGNUM) 134 regcache_raw_collect (regcache, HPPA_CR26_REGNUM, regs + 45 * 4); 135 if (regnum == -1 || regnum == HPPA_CR27_REGNUM) 136 regcache_raw_collect (regcache, HPPA_CR27_REGNUM, regs + 46 * 4); 137 } 138 139 /* Collect the floating-point registers from REGCACHE and store them 140 in FPREGS. */ 141 142 static void 143 hppabsd_collect_fpregset (struct regcache *regcache, 144 void *fpregs, int regnum) 145 { 146 char *regs = fpregs; 147 int i; 148 149 for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32 * 2; i++) 150 { 151 if (regnum == -1 || regnum == i) 152 regcache_raw_collect (regcache, i, regs + (i - HPPA_FP0_REGNUM) * 4); 153 } 154 } 155 156 157 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this 158 for all registers (including the floating-point registers). */ 159 160 static void 161 hppabsd_fetch_registers (int regnum) 162 { 163 struct regcache *regcache = current_regcache; 164 int pid; 165 166 /* Cater for systems like OpenBSD, that implement threads as 167 separate processes. */ 168 pid = ptid_get_lwp (inferior_ptid); 169 if (pid == 0) 170 pid = ptid_get_pid (inferior_ptid); 171 172 173 if (regnum == -1 || hppabsd_gregset_supplies_p (regnum)) 174 { 175 struct reg regs; 176 177 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) 178 perror_with_name (_("Couldn't get registers")); 179 180 hppabsd_supply_gregset (regcache, ®s); 181 } 182 183 if (regnum == -1 || hppabsd_fpregset_supplies_p (regnum)) 184 { 185 struct fpreg fpregs; 186 187 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) 188 perror_with_name (_("Couldn't get floating point status")); 189 190 hppabsd_supply_fpregset (current_regcache, &fpregs); 191 } 192 } 193 194 /* Store register REGNUM back into the inferior. If REGNUM is -1, do 195 this for all registers (including the floating-point registers). */ 196 197 static void 198 hppabsd_store_registers (int regnum) 199 { 200 int pid; 201 202 /* Cater for systems like OpenBSD, that implement threads as 203 separate processes. */ 204 pid = ptid_get_lwp (inferior_ptid); 205 if (pid == 0) 206 pid = ptid_get_pid (inferior_ptid); 207 208 if (regnum == -1 || hppabsd_gregset_supplies_p (regnum)) 209 { 210 struct reg regs; 211 212 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) 213 perror_with_name (_("Couldn't get registers")); 214 215 hppabsd_collect_gregset (current_regcache, ®s, regnum); 216 217 if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) 218 perror_with_name (_("Couldn't write registers")); 219 } 220 221 if (regnum == -1 || hppabsd_fpregset_supplies_p (regnum)) 222 { 223 struct fpreg fpregs; 224 225 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) 226 perror_with_name (_("Couldn't get floating point status")); 227 228 hppabsd_collect_fpregset (current_regcache, &fpregs, regnum); 229 230 if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) 231 perror_with_name (_("Couldn't write floating point status")); 232 } 233 } 234 235 /* Provide a prototype to silence -Wmissing-prototypes. */ 236 void _initialize_hppabsd_nat (void); 237 238 void 239 _initialize_hppabsd_nat (void) 240 { 241 struct target_ops *t; 242 243 /* Add in local overrides. */ 244 t = inf_ptrace_target (); 245 t->to_fetch_registers = hppabsd_fetch_registers; 246 t->to_store_registers = hppabsd_store_registers; 247 t->to_pid_to_str = obsd_pid_to_str; 248 t->to_find_new_threads = obsd_find_new_threads; 249 t->to_wait = obsd_wait; 250 add_target (t); 251 } 252