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_PCOQ_TAIL_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_SAR_REGNUM, regs); 59 regcache_raw_supply (regcache, HPPA_PCOQ_HEAD_REGNUM, regs + 32 * 4); 60 regcache_raw_supply (regcache, HPPA_PCOQ_TAIL_REGNUM, regs + 33 * 4); 61 } 62 63 /* Supply the floating-point registers stored in FPREGS to REGCACHE. */ 64 65 static void 66 hppabsd_supply_fpregset (struct regcache *regcache, const void *fpregs) 67 { 68 const char *regs = fpregs; 69 int regnum; 70 71 for (regnum = HPPA_FP0_REGNUM; regnum < HPPA_FP0_REGNUM + 32 * 2; regnum++) 72 regcache_raw_supply (regcache, regnum, 73 regs + (regnum - HPPA_FP0_REGNUM) * 4); 74 } 75 76 /* Collect the general-purpose registers from REGCACHE and store them 77 in GREGS. */ 78 79 static void 80 hppabsd_collect_gregset (const struct regcache *regcache, 81 void *gregs, int regnum) 82 { 83 char *regs = gregs; 84 int i; 85 86 for (i = HPPA_R1_REGNUM; i <= HPPA_R31_REGNUM; i++) 87 { 88 if (regnum == -1 || regnum == i) 89 regcache_raw_collect (regcache, i, regs + i * 4); 90 } 91 92 if (regnum == -1 || regnum == HPPA_SAR_REGNUM) 93 regcache_raw_collect (regcache, HPPA_SAR_REGNUM, regs); 94 if (regnum == -1 || regnum == HPPA_PCOQ_HEAD_REGNUM) 95 regcache_raw_collect (regcache, HPPA_PCOQ_HEAD_REGNUM, regs + 32 * 4); 96 if (regnum == -1 || regnum == HPPA_PCOQ_TAIL_REGNUM) 97 regcache_raw_collect (regcache, HPPA_PCOQ_TAIL_REGNUM, regs + 33 * 4); 98 } 99 100 /* Collect the floating-point registers from REGCACHE and store them 101 in FPREGS. */ 102 103 static void 104 hppabsd_collect_fpregset (struct regcache *regcache, 105 void *fpregs, int regnum) 106 { 107 char *regs = fpregs; 108 int i; 109 110 for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32 * 2; i++) 111 { 112 if (regnum == -1 || regnum == i) 113 regcache_raw_collect (regcache, i, regs + (i - HPPA_FP0_REGNUM) * 4); 114 } 115 } 116 117 118 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this 119 for all registers (including the floating-point registers). */ 120 121 static void 122 hppabsd_fetch_registers (int regnum) 123 { 124 struct regcache *regcache = current_regcache; 125 int pid; 126 127 /* Cater for systems like OpenBSD, that implement threads as 128 separate processes. */ 129 pid = ptid_get_lwp (inferior_ptid); 130 if (pid == 0) 131 pid = ptid_get_pid (inferior_ptid); 132 133 134 if (regnum == -1 || hppabsd_gregset_supplies_p (regnum)) 135 { 136 struct reg regs; 137 138 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) 139 perror_with_name (_("Couldn't get registers")); 140 141 hppabsd_supply_gregset (regcache, ®s); 142 } 143 144 if (regnum == -1 || hppabsd_fpregset_supplies_p (regnum)) 145 { 146 struct fpreg fpregs; 147 148 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) 149 perror_with_name (_("Couldn't get floating point status")); 150 151 hppabsd_supply_fpregset (current_regcache, &fpregs); 152 } 153 } 154 155 /* Store register REGNUM back into the inferior. If REGNUM is -1, do 156 this for all registers (including the floating-point registers). */ 157 158 static void 159 hppabsd_store_registers (int regnum) 160 { 161 int pid; 162 163 /* Cater for systems like OpenBSD, that implement threads as 164 separate processes. */ 165 pid = ptid_get_lwp (inferior_ptid); 166 if (pid == 0) 167 pid = ptid_get_pid (inferior_ptid); 168 169 if (regnum == -1 || hppabsd_gregset_supplies_p (regnum)) 170 { 171 struct reg regs; 172 173 if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) 174 perror_with_name (_("Couldn't get registers")); 175 176 hppabsd_collect_gregset (current_regcache, ®s, regnum); 177 178 if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) 179 perror_with_name (_("Couldn't write registers")); 180 } 181 182 if (regnum == -1 || hppabsd_fpregset_supplies_p (regnum)) 183 { 184 struct fpreg fpregs; 185 186 if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) 187 perror_with_name (_("Couldn't get floating point status")); 188 189 hppabsd_collect_fpregset (current_regcache, &fpregs, regnum); 190 191 if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) 192 perror_with_name (_("Couldn't write floating point status")); 193 } 194 } 195 196 /* Provide a prototype to silence -Wmissing-prototypes. */ 197 void _initialize_hppabsd_nat (void); 198 199 void 200 _initialize_hppabsd_nat (void) 201 { 202 struct target_ops *t; 203 204 /* Add in local overrides. */ 205 t = inf_ptrace_target (); 206 t->to_fetch_registers = hppabsd_fetch_registers; 207 t->to_store_registers = hppabsd_store_registers; 208 t->to_pid_to_str = obsd_pid_to_str; 209 t->to_find_new_threads = obsd_find_new_threads; 210 t->to_wait = obsd_wait; 211 add_target (t); 212 } 213