xref: /freebsd/sys/fs/procfs/procfs_mem.c (revision 4a0f765fbf09711e612e86fce8bb09ec43f482d9)
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.5 (Berkeley) 6/15/94
39  *
40  *	$Id$
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 <sys/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 
67 static int	procfs_rwmem __P((struct proc *p, struct uio *uio));
68 
69 static int
70 procfs_rwmem(p, uio)
71 	struct proc *p;
72 	struct uio *uio;
73 {
74 	int error;
75 	int writing;
76 	struct vmspace *vm;
77 	int fix_prot = 0;
78 	vm_map_t map;
79 	vm_object_t object = NULL;
80 	vm_offset_t pageno = 0;		/* page number */
81 
82 	/*
83 	 * if the vmspace is in the midst of being deallocated or the
84 	 * process is exiting, don't try to grab anything.  The page table
85 	 * usage in that process can be messed up.
86 	 */
87 	vm = p->p_vmspace;
88 	if ((p->p_flag & P_WEXIT) || (vm->vm_refcnt < 1))
89 		return EFAULT;
90 	++vm->vm_refcnt;
91 	/*
92 	 * The map we want...
93 	 */
94 	map = &vm->vm_map;
95 
96 	writing = uio->uio_rw == UIO_WRITE;
97 
98 	/*
99 	 * Only map in one page at a time.  We don't have to, but it
100 	 * makes things easier.  This way is trivial - right?
101 	 */
102 	do {
103 		vm_map_t tmap;
104 		vm_offset_t kva = 0;
105 		vm_offset_t uva;
106 		int page_offset;		/* offset into page */
107 		vm_map_entry_t out_entry;
108 		vm_prot_t out_prot;
109 		boolean_t wired, single_use;
110 		vm_pindex_t pindex;
111 		u_int len;
112 
113 		fix_prot = 0;
114 		object = NULL;
115 
116 		uva = (vm_offset_t) uio->uio_offset;
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 		if (uva >= VM_MAXUSER_ADDRESS) {
130 			if (writing || (uva >= (VM_MAXUSER_ADDRESS + UPAGES * PAGE_SIZE))) {
131 				error = 0;
132 				break;
133 			}
134 
135 			/* we are reading the "U area", force it into core */
136 			PHOLD(p);
137 
138 			/* sanity check */
139 			if (!(p->p_flag & P_INMEM)) {
140 				/* aiee! */
141 				PRELE(p);
142 				error = EFAULT;
143 				break;
144 			}
145 
146 			/* populate the ptrace/procfs area */
147 			p->p_addr->u_kproc.kp_proc = *p;
148 			fill_eproc (p, &p->p_addr->u_kproc.kp_eproc);
149 
150 			/* locate the in-core address */
151 			kva = (u_int)p->p_addr + uva - VM_MAXUSER_ADDRESS;
152 
153 			/* transfer it */
154 			error = uiomove((caddr_t)kva, len, uio);
155 
156 			/* let the pages go */
157 			PRELE(p);
158 
159 			continue;
160 		}
161 
162 		/*
163 		 * Check the permissions for the area we're interested
164 		 * in.
165 		 */
166 		if (writing) {
167 			fix_prot = !vm_map_check_protection(map, pageno,
168 					pageno + PAGE_SIZE, VM_PROT_WRITE);
169 
170 			if (fix_prot) {
171 				/*
172 				 * If the page is not writable, we make it so.
173 				 * XXX It is possible that a page may *not* be
174 				 * read/executable, if a process changes that!
175 				 * We will assume, for now, that a page is either
176 				 * VM_PROT_ALL, or VM_PROT_READ|VM_PROT_EXECUTE.
177 				 */
178 				error = vm_map_protect(map, pageno,
179 					pageno + PAGE_SIZE, VM_PROT_ALL, 0);
180 				if (error) {
181 					/*
182 					 * We don't have to undo something
183 					 * that didn't work, so we clear the
184 					 * flag.
185 					 */
186 					fix_prot = 0;
187 					break;
188 				}
189 			}
190 		}
191 
192 		/*
193 		 * Now we need to get the page.  out_entry, out_prot, wired,
194 		 * and single_use aren't used.  One would think the vm code
195 		 * would be a *bit* nicer...  We use tmap because
196 		 * vm_map_lookup() can change the map argument.
197 		 */
198 		tmap = map;
199 		error = vm_map_lookup(&tmap, pageno,
200 			      writing ? VM_PROT_WRITE : VM_PROT_READ,
201 			      &out_entry, &object, &pindex, &out_prot,
202 			      &wired, &single_use);
203 
204 		if (error) {
205 			/*
206 			 * Make sure that there is no residue in 'object' from
207 			 * an error return on vm_map_lookup.
208 			 */
209 			object = NULL;
210 			break;
211 		}
212 
213 		/*
214 		 * We're done with tmap now.
215 		 * But reference the object first, so that we won't loose
216 		 * it.
217 		 */
218 		vm_object_reference(object);
219 		vm_map_lookup_done(tmap, out_entry);
220 
221 		/*
222 		 * Fault the page in...
223 		 */
224 		if (writing && object->backing_object) {
225 			error = vm_fault(map, pageno,
226 				VM_PROT_WRITE, FALSE);
227 			if (error)
228 				break;
229 		}
230 
231 		/* Find space in kernel_map for the page we're interested in */
232 		error = vm_map_find(kernel_map, object,
233 				IDX_TO_OFF(pindex), &kva, PAGE_SIZE, 1,
234 				VM_PROT_ALL, VM_PROT_ALL, 0);
235 		if (error) {
236 			break;
237 		}
238 
239 		/*
240 		 * Mark the page we just found as pageable.
241 		 */
242 		error = vm_map_pageable(kernel_map, kva,
243 				kva + PAGE_SIZE, 0);
244 		if (error) {
245 			vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
246 			object = NULL;
247 			break;
248 		}
249 
250 		/*
251 		 * Now do the i/o move.
252 		 */
253 		error = uiomove((caddr_t)(kva + page_offset),
254 				len, uio);
255 
256 		/*
257 		 * vm_map_remove gets rid of the object reference, so
258 		 * we need to get rid of our 'object' pointer if there
259 		 * is subsequently an error.
260 		 */
261 		vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
262 		object = NULL;
263 
264 		/*
265 		 * Undo the protection 'damage'.
266 		 */
267 		if (fix_prot) {
268 			vm_map_protect(map, pageno, pageno + PAGE_SIZE,
269 				VM_PROT_READ|VM_PROT_EXECUTE, 0);
270 			fix_prot = 0;
271 		}
272 	} while (error == 0 && uio->uio_resid > 0);
273 
274 	if (object)
275 		vm_object_deallocate(object);
276 
277 	if (fix_prot)
278 		vm_map_protect(map, pageno, pageno + PAGE_SIZE,
279 				VM_PROT_READ|VM_PROT_EXECUTE, 0);
280 
281 	vmspace_free(vm);
282 	return (error);
283 }
284 
285 /*
286  * Copy data in and out of the target process.
287  * We do this by mapping the process's page into
288  * the kernel and then doing a uiomove direct
289  * from the kernel address space.
290  */
291 int
292 procfs_domem(curp, p, pfs, uio)
293 	struct proc *curp;
294 	struct proc *p;
295 	struct pfsnode *pfs;
296 	struct uio *uio;
297 {
298 
299 	if (uio->uio_resid == 0)
300 		return (0);
301 
302 	return (procfs_rwmem(p, uio));
303 }
304 
305 /*
306  * Given process (p), find the vnode from which
307  * it's text segment is being executed.
308  *
309  * It would be nice to grab this information from
310  * the VM system, however, there is no sure-fire
311  * way of doing that.  Instead, fork(), exec() and
312  * wait() all maintain the p_textvp field in the
313  * process proc structure which contains a held
314  * reference to the exec'ed vnode.
315  */
316 struct vnode *
317 procfs_findtextvp(p)
318 	struct proc *p;
319 {
320 
321 	return (p->p_textvp);
322 }
323