procfs_mem.c revision 1.20 1 /* $NetBSD: procfs_mem.c,v 1.20 1999/02/25 21:54:53 is Exp $ */
2
3 /*
4 * Copyright (c) 1993 Jan-Simon Pendry
5 * Copyright (c) 1993 Sean Eric Fagan
6 * Copyright (c) 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Jan-Simon Pendry and Sean Eric Fagan.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)procfs_mem.c 8.5 (Berkeley) 6/15/94
41 */
42
43 /*
44 * This is a lightly hacked and merged version
45 * of sef's pread/pwrite functions
46 */
47
48 #include "opt_uvm.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/time.h>
53 #include <sys/kernel.h>
54 #include <sys/proc.h>
55 #include <sys/vnode.h>
56 #include <miscfs/procfs/procfs.h>
57 #include <vm/vm.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_page.h>
60
61 #if defined(UVM)
62 #include <uvm/uvm_extern.h>
63 #endif
64
65 #define ISSET(t, f) ((t) & (f))
66
67 #if !defined(UVM)
68 static int procfs_rwmem __P((struct proc *, struct uio *));
69
70 static int
71 procfs_rwmem(p, uio)
72 struct proc *p;
73 struct uio *uio;
74 {
75 int error;
76 int writing;
77
78 writing = uio->uio_rw == UIO_WRITE;
79
80 /*
81 * Only map in one page at a time. We don't have to, but it
82 * makes things easier. This way is trivial - right?
83 */
84 do {
85 vm_map_t map, tmap;
86 vm_object_t object;
87 vaddr_t kva;
88 vaddr_t uva;
89 int page_offset; /* offset into page */
90 vaddr_t pageno; /* page number */
91 vm_map_entry_t out_entry;
92 vm_prot_t out_prot;
93 vm_page_t m;
94 boolean_t wired, single_use;
95 vaddr_t off;
96 u_int len;
97 int fix_prot;
98
99 uva = (vaddr_t) uio->uio_offset;
100 if (uva > VM_MAXUSER_ADDRESS) {
101 error = 0;
102 break;
103 }
104
105 /*
106 * Get the page number of this segment.
107 */
108 pageno = trunc_page(uva);
109 page_offset = uva - pageno;
110
111 /*
112 * How many bytes to copy
113 */
114 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
115
116 /*
117 * The map we want...
118 */
119 map = &p->p_vmspace->vm_map;
120
121 /*
122 * Check the permissions for the area we're interested
123 * in.
124 */
125 fix_prot = 0;
126 if (writing)
127 fix_prot = !vm_map_check_protection(map, pageno,
128 pageno + PAGE_SIZE, VM_PROT_WRITE);
129
130 if (fix_prot) {
131 /*
132 * If the page is not writable, we make it so.
133 * XXX It is possible that a page may *not* be
134 * read/executable, if a process changes that!
135 * We will assume, for now, that a page is either
136 * VM_PROT_ALL, or VM_PROT_READ|VM_PROT_EXECUTE.
137 */
138 error = vm_map_protect(map, pageno,
139 pageno + PAGE_SIZE, VM_PROT_ALL, 0);
140 if (error)
141 break;
142 }
143
144 /*
145 * Now we need to get the page. out_entry, out_prot, wired,
146 * and single_use aren't used. One would think the vm code
147 * would be a *bit* nicer... We use tmap because
148 * vm_map_lookup() can change the map argument.
149 */
150 tmap = map;
151 error = vm_map_lookup(&tmap, pageno,
152 writing ? VM_PROT_WRITE : VM_PROT_READ,
153 &out_entry, &object, &off, &out_prot,
154 &wired, &single_use);
155 /*
156 * We're done with tmap now.
157 */
158 if (!error)
159 vm_map_lookup_done(tmap, out_entry);
160
161 /*
162 * Fault the page in...
163 */
164 if (!error && writing && object->shadow) {
165 m = vm_page_lookup(object, off);
166 if (m == 0 || (m->flags & PG_COPYONWRITE))
167 error = vm_fault(map, pageno,
168 VM_PROT_WRITE, FALSE);
169 }
170
171 /* Find space in kernel_map for the page we're interested in */
172 if (!error) {
173 kva = VM_MIN_KERNEL_ADDRESS;
174 error = vm_map_find(kernel_map, object, off, &kva,
175 PAGE_SIZE, 1);
176 }
177
178 if (!error) {
179 /*
180 * Neither vm_map_lookup() nor vm_map_find() appear
181 * to add a reference count to the object, so we do
182 * that here and now.
183 */
184 vm_object_reference(object);
185
186 /*
187 * Mark the page we just found as pageable.
188 */
189 error = vm_map_pageable(kernel_map, kva,
190 kva + PAGE_SIZE, 0);
191
192 /*
193 * Now do the i/o move.
194 */
195 if (!error)
196 error = uiomove((caddr_t) (kva + page_offset),
197 len, uio);
198
199 vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
200 }
201 if (fix_prot)
202 vm_map_protect(map, pageno, pageno + PAGE_SIZE,
203 VM_PROT_READ|VM_PROT_EXECUTE, 0);
204 } while (error == 0 && uio->uio_resid > 0);
205
206 return (error);
207 }
208 #endif
209
210 /*
211 * Copy data in and out of the target process.
212 * We do this by mapping the process's page into
213 * the kernel and then doing a uiomove direct
214 * from the kernel address space.
215 */
216 int
217 procfs_domem(curp, p, pfs, uio)
218 struct proc *curp; /* tracer */
219 struct proc *p; /* traced */
220 struct pfsnode *pfs;
221 struct uio *uio;
222 {
223 int error;
224
225 size_t len;
226 vaddr_t addr;
227
228 len = uio->uio_resid;
229
230 if (len == 0)
231 return (0);
232
233 addr = uio->uio_offset;
234
235 if ((error = procfs_checkioperm(curp, p)) != 0)
236 return (error);
237
238 #if defined(UVM)
239 /* XXXCDC: how should locking work here? */
240 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
241 return(EFAULT);
242 PHOLD(p);
243 p->p_vmspace->vm_refcnt++; /* XXX */
244 error = uvm_io(&p->p_vmspace->vm_map, uio);
245 PRELE(p);
246 uvmspace_free(p->p_vmspace);
247
248 #ifdef PMAP_NEED_PROCWR
249 if (uio->uio_rw == UIO_WRITE)
250 pmap_procwr(p, addr, len);
251 #endif
252
253 #else
254 PHOLD(p);
255 error = procfs_rwmem(p, uio);
256 PRELE(p);
257 #endif
258 return (error);
259 }
260
261 /*
262 * Given process (p), find the vnode from which
263 * it's text segment is being executed.
264 *
265 * It would be nice to grab this information from
266 * the VM system, however, there is no sure-fire
267 * way of doing that. Instead, fork(), exec() and
268 * wait() all maintain the p_textvp field in the
269 * process proc structure which contains a held
270 * reference to the exec'ed vnode.
271 */
272 struct vnode *
273 procfs_findtextvp(p)
274 struct proc *p;
275 {
276
277 return (p->p_textvp);
278 }
279
280 /*
281 * Ensure that a process has permission to perform I/O on another.
282 * Arguments:
283 * p The process wishing to do the I/O (the tracer).
284 * t The process who's memory/registers will be read/written.
285 */
286 int
287 procfs_checkioperm(p, t)
288 struct proc *p, *t;
289 {
290 int error;
291
292 /*
293 * You cannot attach to a processes mem/regs if:
294 *
295 * (1) it's not owned by you, or is set-id on exec
296 * (unless you're root), or...
297 */
298 if ((t->p_cred->p_ruid != p->p_cred->p_ruid ||
299 ISSET(t->p_flag, P_SUGID)) &&
300 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
301 return (error);
302
303 /*
304 * (2) ...it's init, which controls the security level
305 * of the entire system, and the system was not
306 * compiled with permanetly insecure mode turned on.
307 */
308 if (t == initproc && securelevel > -1)
309 return (EPERM);
310
311 return (0);
312 }
313
314 #ifdef probably_never
315 /*
316 * Given process (p), find the vnode from which
317 * it's text segment is being mapped.
318 *
319 * (This is here, rather than in procfs_subr in order
320 * to keep all the VM related code in one place.)
321 */
322 struct vnode *
323 procfs_findtextvp(p)
324 struct proc *p;
325 {
326 int error;
327 vm_object_t object;
328 vaddr_t pageno; /* page number */
329
330 /* find a vnode pager for the user address space */
331
332 for (pageno = VM_MIN_ADDRESS;
333 pageno < VM_MAXUSER_ADDRESS;
334 pageno += PAGE_SIZE) {
335 vm_map_t map;
336 vm_map_entry_t out_entry;
337 vm_prot_t out_prot;
338 boolean_t wired, single_use;
339 vaddr_t off;
340
341 map = &p->p_vmspace->vm_map;
342 error = vm_map_lookup(&map, pageno,
343 VM_PROT_READ,
344 &out_entry, &object, &off, &out_prot,
345 &wired, &single_use);
346
347 if (!error) {
348 vm_pager_t pager;
349
350 printf("procfs: found vm object\n");
351 vm_map_lookup_done(map, out_entry);
352 printf("procfs: vm object = %p\n", object);
353
354 /*
355 * At this point, assuming no errors, object
356 * is the VM object mapping UVA (pageno).
357 * Ensure it has a vnode pager, then grab
358 * the vnode from that pager's handle.
359 */
360
361 pager = object->pager;
362 printf("procfs: pager = %p\n", pager);
363 if (pager)
364 printf("procfs: found pager, type = %d\n",
365 pager->pg_type);
366 if (pager && pager->pg_type == PG_VNODE) {
367 struct vnode *vp;
368
369 vp = (struct vnode *) pager->pg_handle;
370 printf("procfs: vp = %p\n", vp);
371 return (vp);
372 }
373 }
374 }
375
376 printf("procfs: text object not found\n");
377 return (0);
378 }
379 #endif /* probably_never */
380