|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24:
25: /* $NetBSD: procfs_mem.c,v 1.7 1995/01/05 07:10:54 chopps Exp $ */
26:
27: /*
28: * Copyright (c) 1993 Jan-Simon Pendry
29: * Copyright (c) 1993 Sean Eric Fagan
30: * Copyright (c) 1993
31: * The Regents of the University of California. All rights reserved.
32: *
33: * This code is derived from software contributed to Berkeley by
34: * Jan-Simon Pendry and Sean Eric Fagan.
35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
44: * 3. All advertising materials mentioning features or use of this software
45: * must display the following acknowledgement:
46: * This product includes software developed by the University of
47: * California, Berkeley and its contributors.
48: * 4. Neither the name of the University nor the names of its contributors
49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: *
64: * @(#)procfs_mem.c 8.5 (Berkeley) 6/15/94
65: */
66:
67: /*
68: * This is a lightly hacked and merged version
69: * of sef's pread/pwrite functions
70: */
71:
72: #include <sys/param.h>
73: #include <sys/systm.h>
74: #include <sys/time.h>
75: #include <sys/kernel.h>
76: #include <sys/proc.h>
77: #include <sys/vnode.h>
78: #include <miscfs/procfs/procfs.h>
79: #include <vm/vm.h>
80: #include <vm/vm_kern.h>
81: #include <vm/vm_page.h>
82:
83: static int
84: procfs_rwmem(p, uio)
85: struct proc *p;
86: struct uio *uio;
87: {
88: int error;
89: int writing;
90:
91: writing = uio->uio_rw == UIO_WRITE;
92:
93: /*
94: * Only map in one page at a time. We don't have to, but it
95: * makes things easier. This way is trivial - right?
96: */
97: do {
98: vm_map_t map, tmap;
99: vm_object_t object;
100: vm_offset_t kva;
101: vm_offset_t uva;
102: int page_offset; /* offset into page */
103: vm_offset_t pageno; /* page number */
104: vm_map_entry_t out_entry;
105: vm_prot_t out_prot;
106: vm_page_t m;
107: boolean_t wired, single_use;
108: vm_offset_t off;
109: u_int len;
110: int fix_prot;
111:
112: uva = (vm_offset_t) uio->uio_offset;
113: if (uva > VM_MAXUSER_ADDRESS) {
114: error = 0;
115: break;
116: }
117:
118: /*
119: * Get the page number of this segment.
120: */
121: pageno = trunc_page(uva);
122: page_offset = uva - pageno;
123:
124: /*
125: * How many bytes to copy
126: */
127: len = min(PAGE_SIZE - page_offset, uio->uio_resid);
128:
129: /*
130: * The map we want...
131: */
132: map = &p->p_vmspace->vm_map;
133:
134: /*
135: * Check the permissions for the area we're interested
136: * in.
137: */
138: fix_prot = 0;
139: if (writing)
140: fix_prot = !vm_map_check_protection(map, pageno,
141: pageno + PAGE_SIZE, VM_PROT_WRITE);
142:
143: if (fix_prot) {
144: /*
145: * If the page is not writable, we make it so.
146: * XXX It is possible that a page may *not* be
147: * read/executable, if a process changes that!
148: * We will assume, for now, that a page is either
149: * VM_PROT_ALL, or VM_PROT_READ|VM_PROT_EXECUTE.
150: */
151: error = vm_map_protect(map, pageno,
152: pageno + PAGE_SIZE, VM_PROT_ALL, 0);
153: if (error)
154: break;
155: }
156:
157: /*
158: * Now we need to get the page. out_entry, out_prot, wired,
159: * and single_use aren't used. One would think the vm code
160: * would be a *bit* nicer... We use tmap because
161: * vm_map_lookup() can change the map argument.
162: */
163: tmap = map;
164: error = vm_map_lookup(&tmap, pageno,
165: writing ? VM_PROT_WRITE : VM_PROT_READ,
166: &out_entry, &object, &off, &out_prot,
167: &wired, &single_use);
168: /*
169: * We're done with tmap now.
170: */
171: if (!error)
172: vm_map_lookup_done(tmap, out_entry);
173:
174: /*
175: * Fault the page in...
176: */
177: if (!error && writing && object->shadow) {
178: m = vm_page_lookup(object, off);
179: if (m == 0 || (m->flags & PG_COPYONWRITE))
180: error = vm_fault(map, pageno,
181: VM_PROT_WRITE, FALSE);
182: }
183:
184: /* Find space in kernel_map for the page we're interested in */
185: if (!error) {
186: kva = VM_MIN_KERNEL_ADDRESS;
187: error = vm_map_find(kernel_map, object, off, &kva,
188: PAGE_SIZE, 1);
189: }
190:
191: if (!error) {
192: /*
193: * Neither vm_map_lookup() nor vm_map_find() appear
194: * to add a reference count to the object, so we do
195: * that here and now.
196: */
197: vm_object_reference(object);
198:
199: /*
200: * Mark the page we just found as pageable.
201: */
202: error = vm_map_pageable(kernel_map, kva,
203: kva + PAGE_SIZE, 0);
204:
205: /*
206: * Now do the i/o move.
207: */
208: if (!error)
209: error = uiomove(kva + page_offset, len, uio);
210:
211: vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
212: }
213: if (fix_prot)
214: vm_map_protect(map, pageno, pageno + PAGE_SIZE,
215: VM_PROT_READ|VM_PROT_EXECUTE, 0);
216: } while (error == 0 && uio->uio_resid > 0);
217:
218: return (error);
219: }
220:
221: /*
222: * Copy data in and out of the target process.
223: * We do this by mapping the process's page into
224: * the kernel and then doing a uiomove direct
225: * from the kernel address space.
226: */
227: int
228: procfs_domem(curp, p, pfs, uio)
229: struct proc *curp;
230: struct proc *p;
231: struct pfsnode *pfs;
232: struct uio *uio;
233: {
234:
235: if (uio->uio_resid == 0)
236: return (0);
237:
238: return (procfs_rwmem(p, uio));
239: }
240:
241: /*
242: * Given process (p), find the vnode from which
243: * it's text segment is being executed.
244: *
245: * It would be nice to grab this information from
246: * the VM system, however, there is no sure-fire
247: * way of doing that. Instead, fork(), exec() and
248: * wait() all maintain the p_textvp field in the
249: * process proc structure which contains a held
250: * reference to the exec'ed vnode.
251: */
252: struct vnode *
253: procfs_findtextvp(p)
254: struct proc *p;
255: {
256:
257: return (p->p_textvp);
258: }
259:
260:
261: #ifdef probably_never
262: /*
263: * Given process (p), find the vnode from which
264: * it's text segment is being mapped.
265: *
266: * (This is here, rather than in procfs_subr in order
267: * to keep all the VM related code in one place.)
268: */
269: struct vnode *
270: procfs_findtextvp(p)
271: struct proc *p;
272: {
273: int error;
274: vm_object_t object;
275: vm_offset_t pageno; /* page number */
276:
277: /* find a vnode pager for the user address space */
278:
279: for (pageno = VM_MIN_ADDRESS;
280: pageno < VM_MAXUSER_ADDRESS;
281: pageno += PAGE_SIZE) {
282: vm_map_t map;
283: vm_map_entry_t out_entry;
284: vm_prot_t out_prot;
285: boolean_t wired, single_use;
286: vm_offset_t off;
287:
288: map = &p->p_vmspace->vm_map;
289: error = vm_map_lookup(&map, pageno,
290: VM_PROT_READ,
291: &out_entry, &object, &off, &out_prot,
292: &wired, &single_use);
293:
294: if (!error) {
295: vm_pager_t pager;
296:
297: printf("procfs: found vm object\n");
298: vm_map_lookup_done(map, out_entry);
299: printf("procfs: vm object = %x\n", object);
300:
301: /*
302: * At this point, assuming no errors, object
303: * is the VM object mapping UVA (pageno).
304: * Ensure it has a vnode pager, then grab
305: * the vnode from that pager's handle.
306: */
307:
308: pager = object->pager;
309: printf("procfs: pager = %x\n", pager);
310: if (pager)
311: printf("procfs: found pager, type = %d\n", pager->pg_type);
312: if (pager && pager->pg_type == PG_VNODE) {
313: struct vnode *vp;
314:
315: vp = (struct vnode *) pager->pg_handle;
316: printf("procfs: vp = 0x%x\n", vp);
317: return (vp);
318: }
319: }
320: }
321:
322: printf("procfs: text object not found\n");
323: return (0);
324: }
325: #endif /* probably_never */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.