procfs_mem.c revision 13627
1/* 2 * Copyright (c) 1993 Jan-Simon Pendry 3 * Copyright (c) 1993 Sean Eric Fagan 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry and Sean Eric Fagan. 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)procfs_mem.c 8.4 (Berkeley) 1/21/94 39 * 40 * $Id: procfs_mem.c,v 1.16 1996/01/24 18:41:06 peter Exp $ 41 */ 42 43/* 44 * This is a lightly hacked and merged version 45 * of sef's pread/pwrite functions 46 */ 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/time.h> 51#include <sys/kernel.h> 52#include <sys/proc.h> 53#include <sys/vnode.h> 54#include <miscfs/procfs/procfs.h> 55#include <vm/vm.h> 56#include <vm/vm_param.h> 57#include <vm/vm_prot.h> 58#include <vm/lock.h> 59#include <vm/pmap.h> 60#include <vm/vm_map.h> 61#include <vm/vm_kern.h> 62#include <vm/vm_object.h> 63#include <vm/vm_page.h> 64#include <vm/vm_extern.h> 65#include <sys/user.h> 66 67static int procfs_rwmem __P((struct proc *p, struct uio *uio)); 68 69static int 70procfs_rwmem(p, uio) 71 struct proc *p; 72 struct uio *uio; 73{ 74 int error; 75 int writing; 76 77 writing = uio->uio_rw == UIO_WRITE; 78 79 /* 80 * Only map in one page at a time. We don't have to, but it 81 * makes things easier. This way is trivial - right? 82 */ 83 do { 84 vm_map_t map, tmap; 85 vm_object_t object; 86 vm_offset_t kva = 0; 87 vm_offset_t uva; 88 int page_offset; /* offset into page */ 89 vm_offset_t pageno; /* page number */ 90 vm_map_entry_t out_entry; 91 vm_prot_t out_prot; 92 vm_page_t m; 93 boolean_t wired, single_use; 94 vm_pindex_t pindex; 95 u_int len; 96 int fix_prot; 97 98 uva = (vm_offset_t) uio->uio_offset; 99 100 /* 101 * Get the page number of this segment. 102 */ 103 pageno = trunc_page(uva); 104 page_offset = uva - pageno; 105 106 /* 107 * How many bytes to copy 108 */ 109 len = min(PAGE_SIZE - page_offset, uio->uio_resid); 110 111 if (uva >= VM_MAXUSER_ADDRESS) { 112 if (writing || (uva >= (VM_MAXUSER_ADDRESS + UPAGES * PAGE_SIZE))) { 113 error = 0; 114 break; 115 } 116 117 /* we are reading the "U area", force it into core */ 118 PHOLD(p); 119 120 /* sanity check */ 121 if (!(p->p_flag & P_INMEM)) { 122 /* aiee! */ 123 error = EFAULT; 124 break; 125 } 126 127 /* populate the ptrace/procfs area */ 128 p->p_addr->u_kproc.kp_proc = *p; 129 fill_eproc (p, &p->p_addr->u_kproc.kp_eproc); 130 131 /* locate the in-core address */ 132 kva = (u_int)p->p_addr + uva - VM_MAXUSER_ADDRESS; 133 134 /* transfer it */ 135 error = uiomove((caddr_t)kva, len, uio); 136 137 /* let the pages go */ 138 PRELE(p); 139 140 continue; 141 } 142 143 144 /* 145 * The map we want... 146 */ 147 map = &p->p_vmspace->vm_map; 148 149 /* 150 * Check the permissions for the area we're interested 151 * in. 152 */ 153 fix_prot = 0; 154 if (writing) 155 fix_prot = !vm_map_check_protection(map, pageno, 156 pageno + PAGE_SIZE, VM_PROT_WRITE); 157 158 if (fix_prot) { 159 /* 160 * If the page is not writable, we make it so. 161 * XXX It is possible that a page may *not* be 162 * read/executable, if a process changes that! 163 * We will assume, for now, that a page is either 164 * VM_PROT_ALL, or VM_PROT_READ|VM_PROT_EXECUTE. 165 */ 166 error = vm_map_protect(map, pageno, 167 pageno + PAGE_SIZE, VM_PROT_ALL, 0); 168 if (error) 169 break; 170 } 171 172 /* 173 * Now we need to get the page. out_entry, out_prot, wired, 174 * and single_use aren't used. One would think the vm code 175 * would be a *bit* nicer... We use tmap because 176 * vm_map_lookup() can change the map argument. 177 */ 178 tmap = map; 179 error = vm_map_lookup(&tmap, pageno, 180 writing ? VM_PROT_WRITE : VM_PROT_READ, 181 &out_entry, &object, &pindex, &out_prot, 182 &wired, &single_use); 183 /* 184 * We're done with tmap now. 185 */ 186 if (!error) 187 vm_map_lookup_done(tmap, out_entry); 188 189 /* 190 * Fault the page in... 191 */ 192 if (!error && writing && object->backing_object) { 193 m = vm_page_lookup(object, pindex); 194 if (m == 0) 195 error = vm_fault(map, pageno, 196 VM_PROT_WRITE, FALSE); 197 } 198 199 /* Find space in kernel_map for the page we're interested in */ 200 if (!error) 201 error = vm_map_find(kernel_map, object, 202 IDX_TO_OFF(pindex), &kva, PAGE_SIZE, 1, 203 VM_PROT_ALL, VM_PROT_ALL, 0); 204 205 if (!error) { 206 /* 207 * Neither vm_map_lookup() nor vm_map_find() appear 208 * to add a reference count to the object, so we do 209 * that here and now. 210 */ 211 vm_object_reference(object); 212 213 /* 214 * Mark the page we just found as pageable. 215 */ 216 error = vm_map_pageable(kernel_map, kva, 217 kva + PAGE_SIZE, 0); 218 219 /* 220 * Now do the i/o move. 221 */ 222 if (!error) 223 error = uiomove((caddr_t)(kva + page_offset), 224 len, uio); 225 226 vm_map_remove(kernel_map, kva, kva + PAGE_SIZE); 227 } 228 if (fix_prot) 229 vm_map_protect(map, pageno, pageno + PAGE_SIZE, 230 VM_PROT_READ|VM_PROT_EXECUTE, 0); 231 } while (error == 0 && uio->uio_resid > 0); 232 233 return (error); 234} 235 236/* 237 * Copy data in and out of the target process. 238 * We do this by mapping the process's page into 239 * the kernel and then doing a uiomove direct 240 * from the kernel address space. 241 */ 242int 243procfs_domem(curp, p, pfs, uio) 244 struct proc *curp; 245 struct proc *p; 246 struct pfsnode *pfs; 247 struct uio *uio; 248{ 249 int error; 250 251 if (uio->uio_resid == 0) 252 return (0); 253 254 error = procfs_rwmem(p, uio); 255 256 return (error); 257} 258 259/* 260 * Given process (p), find the vnode from which 261 * it's text segment is being executed. 262 * 263 * It would be nice to grab this information from 264 * the VM system, however, there is no sure-fire 265 * way of doing that. Instead, fork(), exec() and 266 * wait() all maintain the p_textvp field in the 267 * process proc structure which contains a held 268 * reference to the exec'ed vnode. 269 */ 270struct vnode * 271procfs_findtextvp(p) 272 struct proc *p; 273{ 274 return (p->p_textvp); 275} 276