| 1 | /* Native-dependent code for OpenBSD/powerpc. |
| 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 "gdbcore.h" |
| 24 | #include "inferior.h" |
| 25 | #include "regcache.h" |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/ptrace.h> |
| 30 | #include <sys/signal.h> |
| 31 | #include <machine/frame.h> |
| 32 | #include <machine/pcb.h> |
| 33 | #include <machine/reg.h> |
| 34 | |
| 35 | #include "ppc-tdep.h" |
| 36 | #include "ppcobsd-tdep.h" |
| 37 | #include "inf-ptrace.h" |
| 38 | #include "bsd-kvm.h" |
| 39 | |
| 40 | /* OpenBSD/powerpc doesn't have PT_GETFPREGS/PT_SETFPREGS like |
| 41 | NetBSD/powerpc and FreeBSD/powerpc. */ |
| 42 | |
| 43 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
| 44 | for all registers. */ |
| 45 | |
| 46 | static void |
| 47 | ppcobsd_fetch_registers (int regnum) |
| 48 | { |
| 49 | struct reg regs; |
| 50 | |
| 51 | if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), |
| 52 | (PTRACE_TYPE_ARG3) ®s, 0) == -1) |
| 53 | perror_with_name (_("Couldn't get registers")); |
| 54 | |
| 55 | ppcobsd_supply_gregset (&ppcobsd_gregset, current_regcache, -1, |
| 56 | ®s, sizeof regs); |
| 57 | } |
| 58 | |
| 59 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do |
| 60 | this for all registers. */ |
| 61 | |
| 62 | static void |
| 63 | ppcobsd_store_registers (int regnum) |
| 64 | { |
| 65 | struct reg regs; |
| 66 | |
| 67 | if (ptrace (PT_GETREGS, PIDGET (inferior_ptid), |
| 68 | (PTRACE_TYPE_ARG3) ®s, 0) == -1) |
| 69 | perror_with_name (_("Couldn't get registers")); |
| 70 | |
| 71 | ppcobsd_collect_gregset (&ppcobsd_gregset, current_regcache, |
| 72 | regnum, ®s, sizeof regs); |
| 73 | |
| 74 | if (ptrace (PT_SETREGS, PIDGET (inferior_ptid), |
| 75 | (PTRACE_TYPE_ARG3) ®s, 0) == -1) |
| 76 | perror_with_name (_("Couldn't write registers")); |
| 77 | } |
| 78 | \f |
| 79 | |
| 80 | static int |
| 81 | ppcobsd_supply_pcb (struct regcache *regcache, struct pcb *pcb) |
| 82 | { |
| 83 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache)); |
| 84 | struct switchframe sf; |
| 85 | struct callframe cf; |
| 86 | int i, regnum; |
| 87 | |
| 88 | /* The following is true for OpenBSD 3.7: |
| 89 | |
| 90 | The pcb contains %r1 (the stack pointer) at the point of the |
| 91 | context switch in cpu_switch(). At that point we have a stack |
| 92 | frame as described by `struct switchframe', and below that a call |
| 93 | frame as described by `struct callframe'. From this information |
| 94 | we reconstruct the register state as it would look when we are in |
| 95 | cpu_switch(). */ |
| 96 | |
| 97 | /* The stack pointer shouldn't be zero. */ |
| 98 | if (pcb->pcb_sp == 0) |
| 99 | return 0; |
| 100 | |
| 101 | read_memory (pcb->pcb_sp, (gdb_byte *)&sf, sizeof sf); |
| 102 | regcache_raw_supply (regcache, SP_REGNUM, &sf.sp); |
| 103 | regcache_raw_supply (regcache, tdep->ppc_cr_regnum, &sf.cr); |
| 104 | regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 2, &sf.fixreg2); |
| 105 | for (i = 0, regnum = tdep->ppc_gp0_regnum + 13; i < 19; i++, regnum++) |
| 106 | regcache_raw_supply (regcache, regnum, &sf.fixreg[i]); |
| 107 | |
| 108 | read_memory (sf.sp, (gdb_byte *)&cf, sizeof cf); |
| 109 | regcache_raw_supply (regcache, PC_REGNUM, &cf.lr); |
| 110 | regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 30, &cf.r30); |
| 111 | regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + 31, &cf.r31); |
| 112 | |
| 113 | return 1; |
| 114 | } |
| 115 | \f |
| 116 | |
| 117 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
| 118 | void _initialize_ppcobsd_nat (void); |
| 119 | |
| 120 | void |
| 121 | _initialize_ppcobsd_nat (void) |
| 122 | { |
| 123 | struct target_ops *t; |
| 124 | |
| 125 | /* Add in local overrides. */ |
| 126 | t = inf_ptrace_target (); |
| 127 | t->to_fetch_registers = ppcobsd_fetch_registers; |
| 128 | t->to_store_registers = ppcobsd_store_registers; |
| 129 | add_target (t); |
| 130 | |
| 131 | /* General-purpose registers. */ |
| 132 | ppcobsd_reg_offsets.r0_offset = offsetof (struct reg, gpr); |
| 133 | ppcobsd_reg_offsets.pc_offset = offsetof (struct reg, pc); |
| 134 | ppcobsd_reg_offsets.ps_offset = offsetof (struct reg, ps); |
| 135 | ppcobsd_reg_offsets.cr_offset = offsetof (struct reg, cnd); |
| 136 | ppcobsd_reg_offsets.lr_offset = offsetof (struct reg, lr); |
| 137 | ppcobsd_reg_offsets.ctr_offset = offsetof (struct reg, cnt); |
| 138 | ppcobsd_reg_offsets.xer_offset = offsetof (struct reg, xer); |
| 139 | ppcobsd_reg_offsets.mq_offset = offsetof (struct reg, mq); |
| 140 | |
| 141 | /* Floating-point registers. */ |
| 142 | ppcobsd_reg_offsets.f0_offset = offsetof (struct reg, fpr); |
| 143 | ppcobsd_reg_offsets.fpscr_offset = -1; |
| 144 | |
| 145 | /* AltiVec registers. */ |
| 146 | ppcobsd_reg_offsets.vr0_offset = offsetof (struct vreg, vreg); |
| 147 | ppcobsd_reg_offsets.vscr_offset = offsetof (struct vreg, vscr); |
| 148 | ppcobsd_reg_offsets.vrsave_offset = offsetof (struct vreg, vrsave); |
| 149 | |
| 150 | /* Support debugging kernel virtual memory images. */ |
| 151 | bsd_kvm_add_target (ppcobsd_supply_pcb); |
| 152 | } |