]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/procfs/procfs_mem.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: procfs_mem.c,v 1.7 1995/01/05 07:10:54 chopps Exp $ */
25 * Copyright (c) 1993 Jan-Simon Pendry
26 * Copyright (c) 1993 Sean Eric Fagan
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Jan-Simon Pendry and Sean Eric Fagan.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)procfs_mem.c 8.5 (Berkeley) 6/15/94
65 * This is a lightly hacked and merged version
66 * of sef's pread/pwrite functions
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/kernel.h>
74 #include <sys/vnode.h>
75 #include <miscfs/procfs/procfs.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_page.h>
88 writing
= uio
->uio_rw
== UIO_WRITE
;
91 * Only map in one page at a time. We don't have to, but it
92 * makes things easier. This way is trivial - right?
99 int page_offset
; /* offset into page */
100 vm_offset_t pageno
; /* page number */
101 vm_map_entry_t out_entry
;
104 boolean_t wired
, single_use
;
109 uva
= (vm_offset_t
) uio
->uio_offset
;
110 if (uva
> VM_MAXUSER_ADDRESS
) {
116 * Get the page number of this segment.
118 pageno
= trunc_page(uva
);
119 page_offset
= uva
- pageno
;
122 * How many bytes to copy
124 len
= min(PAGE_SIZE
- page_offset
, uio
->uio_resid
);
129 map
= &p
->p_vmspace
->vm_map
;
132 * Check the permissions for the area we're interested
137 fix_prot
= !vm_map_check_protection(map
, pageno
,
138 pageno
+ PAGE_SIZE
, VM_PROT_WRITE
);
142 * If the page is not writable, we make it so.
143 * XXX It is possible that a page may *not* be
144 * read/executable, if a process changes that!
145 * We will assume, for now, that a page is either
146 * VM_PROT_ALL, or VM_PROT_READ|VM_PROT_EXECUTE.
148 error
= vm_map_protect(map
, pageno
,
149 pageno
+ PAGE_SIZE
, VM_PROT_ALL
, 0);
155 * Now we need to get the page. out_entry, out_prot, wired,
156 * and single_use aren't used. One would think the vm code
157 * would be a *bit* nicer... We use tmap because
158 * vm_map_lookup() can change the map argument.
161 error
= vm_map_lookup(&tmap
, pageno
,
162 writing
? VM_PROT_WRITE
: VM_PROT_READ
,
163 &out_entry
, &object
, &off
, &out_prot
,
164 &wired
, &single_use
);
166 * We're done with tmap now.
169 vm_map_lookup_done(tmap
, out_entry
);
172 * Fault the page in...
174 if (!error
&& writing
&& object
->shadow
) {
175 m
= vm_page_lookup(object
, off
);
176 if (m
== 0 || (m
->flags
& PG_COPYONWRITE
))
177 error
= vm_fault(map
, pageno
,
178 VM_PROT_WRITE
, FALSE
);
181 /* Find space in kernel_map for the page we're interested in */
183 kva
= VM_MIN_KERNEL_ADDRESS
;
184 error
= vm_map_find(kernel_map
, object
, off
, &kva
,
190 * Neither vm_map_lookup() nor vm_map_find() appear
191 * to add a reference count to the object, so we do
194 vm_object_reference(object
);
197 * Mark the page we just found as pageable.
199 error
= vm_map_pageable(kernel_map
, kva
,
203 * Now do the i/o move.
206 error
= uiomove(kva
+ page_offset
, len
, uio
);
208 vm_map_remove(kernel_map
, kva
, kva
+ PAGE_SIZE
);
211 vm_map_protect(map
, pageno
, pageno
+ PAGE_SIZE
,
212 VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
213 } while (error
== 0 && uio
->uio_resid
> 0);
219 * Copy data in and out of the target process.
220 * We do this by mapping the process's page into
221 * the kernel and then doing a uiomove direct
222 * from the kernel address space.
225 procfs_domem(curp
, p
, pfs
, uio
)
232 if (uio
->uio_resid
== 0)
235 return (procfs_rwmem(p
, uio
));
239 * Given process (p), find the vnode from which
240 * it's text segment is being executed.
242 * It would be nice to grab this information from
243 * the VM system, however, there is no sure-fire
244 * way of doing that. Instead, fork(), exec() and
245 * wait() all maintain the p_textvp field in the
246 * process proc structure which contains a held
247 * reference to the exec'ed vnode.
254 return (p
->p_textvp
);
258 #ifdef probably_never
260 * Given process (p), find the vnode from which
261 * it's text segment is being mapped.
263 * (This is here, rather than in procfs_subr in order
264 * to keep all the VM related code in one place.)
272 vm_offset_t pageno
; /* page number */
274 /* find a vnode pager for the user address space */
276 for (pageno
= VM_MIN_ADDRESS
;
277 pageno
< VM_MAXUSER_ADDRESS
;
278 pageno
+= PAGE_SIZE
) {
280 vm_map_entry_t out_entry
;
282 boolean_t wired
, single_use
;
285 map
= &p
->p_vmspace
->vm_map
;
286 error
= vm_map_lookup(&map
, pageno
,
288 &out_entry
, &object
, &off
, &out_prot
,
289 &wired
, &single_use
);
294 printf("procfs: found vm object\n");
295 vm_map_lookup_done(map
, out_entry
);
296 printf("procfs: vm object = %x\n", object
);
299 * At this point, assuming no errors, object
300 * is the VM object mapping UVA (pageno).
301 * Ensure it has a vnode pager, then grab
302 * the vnode from that pager's handle.
305 pager
= object
->pager
;
306 printf("procfs: pager = %x\n", pager
);
308 printf("procfs: found pager, type = %d\n", pager
->pg_type
);
309 if (pager
&& pager
->pg_type
== PG_VNODE
) {
312 vp
= (struct vnode
*) pager
->pg_handle
;
313 printf("procfs: vp = 0x%x\n", vp
);
319 printf("procfs: text object not found\n");
322 #endif /* probably_never */