1 /* $NetBSD: process_machdep.c,v 1.26 2017/01/09 12:24:38 martin Exp $ */ 2 3 /* 4 * Copyright (c) 1993 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel 35 */ 36 37 /* 38 * Copyright (c) 1993 Jan-Simon Pendry 39 * All rights reserved. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * Jan-Simon Pendry. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * from: Id: procfs_i386.c,v 4.1 1993/12/17 10:47:45 jsp Rel 73 */ 74 75 /* 76 * This file may seem a bit stylized, but that so that it's easier to port. 77 * Functions to be implemented here are: 78 * 79 * process_read_regs(proc, regs) 80 * Get the current user-visible register set from the process 81 * and copy it into the regs structure (<machine/reg.h>). 82 * The process is stopped at the time read_regs is called. 83 * 84 * process_write_regs(proc, regs) 85 * Update the current register set from the passed in regs 86 * structure. Take care to avoid clobbering special CPU 87 * registers or privileged bits in the PSL. 88 * The process is stopped at the time write_regs is called. 89 * 90 * process_sstep(proc) 91 * Arrange for the process to trap after executing a single instruction. 92 * 93 * process_set_pc(proc) 94 * Set the process's program counter. 95 */ 96 97 #include <sys/cdefs.h> 98 __KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.26 2017/01/09 12:24:38 martin Exp $"); 99 100 #include <sys/param.h> 101 #include <sys/systm.h> 102 #include <sys/time.h> 103 #include <sys/kernel.h> 104 #include <sys/proc.h> 105 #include <sys/vnode.h> 106 #include <machine/psl.h> 107 #include <machine/reg.h> 108 #include <machine/frame.h> 109 #include <sys/ptrace.h> 110 111 /* Unfortunately we need to convert v9 trapframe to v8 regs */ 112 int 113 process_read_regs(struct lwp *l, struct reg *regs) 114 { 115 struct trapframe64* tf = l->l_md.md_tf; 116 struct reg32* regp = (struct reg32*)regs; 117 int i; 118 119 #ifdef __arch64__ 120 if (!(curproc->p_flag & PK_32)) { 121 /* 64-bit mode -- copy out regs */ 122 regs->r_tstate = tf->tf_tstate; 123 regs->r_pc = tf->tf_pc; 124 regs->r_npc = tf->tf_npc; 125 regs->r_y = tf->tf_y; 126 for (i = 0; i < 8; i++) { 127 regs->r_global[i] = tf->tf_global[i]; 128 regs->r_out[i] = tf->tf_out[i]; 129 } 130 return (0); 131 } 132 #endif 133 /* 32-bit mode -- copy out & convert 32-bit regs */ 134 regp->r_psr = TSTATECCR_TO_PSR(tf->tf_tstate); 135 regp->r_pc = tf->tf_pc; 136 regp->r_npc = tf->tf_npc; 137 regp->r_y = tf->tf_y; 138 for (i = 0; i < 8; i++) { 139 regp->r_global[i] = tf->tf_global[i]; 140 regp->r_out[i] = tf->tf_out[i]; 141 } 142 /* We should also write out the ins and locals. See signal stuff */ 143 return (0); 144 } 145 146 int 147 process_write_regs(struct lwp *l, const struct reg *regs) 148 { 149 struct trapframe64* tf = l->l_md.md_tf; 150 const struct reg32* regp = (const struct reg32*)regs; 151 int i; 152 153 #ifdef __arch64__ 154 if (!(curproc->p_flag & PK_32)) { 155 /* 64-bit mode -- copy in regs */ 156 tf->tf_pc = regs->r_pc; 157 tf->tf_npc = regs->r_npc; 158 tf->tf_y = regs->r_y; 159 for (i = 0; i < 8; i++) { 160 tf->tf_global[i] = regs->r_global[i]; 161 tf->tf_out[i] = regs->r_out[i]; 162 } 163 /* We should also read in the ins and locals. See signal stuff */ 164 tf->tf_tstate = (tf->tf_tstate & ~TSTATE_CCR) | 165 (regs->r_tstate & TSTATE_CCR); 166 return (0); 167 } 168 #endif 169 /* 32-bit mode -- copy in & convert 32-bit regs */ 170 tf->tf_pc = regp->r_pc; 171 tf->tf_npc = regp->r_npc; 172 tf->tf_y = regp->r_y; 173 for (i = 0; i < 8; i++) { 174 tf->tf_global[i] = regp->r_global[i]; 175 tf->tf_out[i] = regp->r_out[i]; 176 } 177 /* We should also read in the ins and locals. See signal stuff */ 178 tf->tf_tstate = (int64_t)(tf->tf_tstate & ~TSTATE_CCR) | 179 PSRCC_TO_TSTATE(regp->r_psr); 180 return (0); 181 } 182 183 int 184 process_sstep(struct lwp *l, int sstep) 185 { 186 187 if (sstep) 188 return EINVAL; 189 return (0); 190 } 191 192 int 193 process_set_pc(struct lwp *l, void *addr) 194 { 195 196 l->l_md.md_tf->tf_pc = (vaddr_t)addr; 197 l->l_md.md_tf->tf_npc = (vaddr_t)addr + 4; 198 return (0); 199 } 200 201 extern const struct fpstate64 initfpstate; 202 203 int 204 process_read_fpregs(struct lwp *l, struct fpreg *regs, size_t *sz) 205 { 206 const struct fpstate64 *fs; 207 208 if ((fs = l->l_md.md_fpstate) == NULL) 209 fs = &initfpstate; 210 #ifdef __arch64__ 211 if (!(curproc->p_flag & PK_32)) { 212 /* 64-bit mode -- copy out fregs */ 213 *regs = fs->fs_reg; 214 return 0; 215 } 216 #endif 217 218 struct fpreg32 *regp = (struct fpreg32 *)regs; 219 220 /* 32-bit mode -- copy out & convert 32-bit fregs */ 221 for (size_t i = 0; i < __arraycount(regp->fr_regs); i++) 222 regp->fr_regs[i] = fs->fs_regs[i]; 223 regp->fr_fsr = fs->fs_fsr; 224 225 return 0; 226 } 227 228 int 229 process_write_fpregs(struct lwp *l, const struct fpreg *regs, size_t sz) 230 { 231 struct fpstate64 *fs; 232 233 if ((fs = l->l_md.md_fpstate) == NULL) { 234 fs = pool_cache_get(fpstate_cache, PR_WAITOK); 235 memcpy(fs, &initfpstate, sizeof *fs); 236 l->l_md.md_fpstate = fs; 237 } else 238 fs->fs_qsize = 0; 239 240 #ifdef __arch64__ 241 if (!(curproc->p_flag & PK_32)) { 242 /* 64-bit mode -- copy in fregs */ 243 fs->fs_reg = *regs; 244 return 0; 245 } 246 #endif 247 const struct fpreg32 *regp = (const struct fpreg32 *)regs; 248 249 /* 32-bit mode -- copy in & convert 32-bit fregs */ 250 for (size_t i = 0; i < __arraycount(regp->fr_regs); i++) 251 fs->fs_regs[i] = regp->fr_regs[i]; 252 fs->fs_fsr = regp->fr_fsr; 253 254 return 0; 255 } 256