xref: /freebsd/sys/fs/procfs/procfs_mem.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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.18 1996/06/11 23:52:27 dyson 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 
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 			vm_page_t m;
226 			m = vm_page_lookup(object, pindex);
227 			if (m == 0) {
228 				error = vm_fault(map, pageno,
229 					VM_PROT_WRITE, FALSE);
230 				break;
231 			}
232 		}
233 
234 		/* Find space in kernel_map for the page we're interested in */
235 		error = vm_map_find(kernel_map, object,
236 				IDX_TO_OFF(pindex), &kva, PAGE_SIZE, 1,
237 				VM_PROT_ALL, VM_PROT_ALL, 0);
238 		if (error) {
239 			break;
240 		}
241 
242 		/*
243 		 * Mark the page we just found as pageable.
244 		 */
245 		error = vm_map_pageable(kernel_map, kva,
246 				kva + PAGE_SIZE, 0);
247 		if (error) {
248 			vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
249 			object = NULL;
250 			break;
251 		}
252 
253 		/*
254 		 * Now do the i/o move.
255 		 */
256 		error = uiomove((caddr_t)(kva + page_offset),
257 				len, uio);
258 
259 		/*
260 		 * vm_map_remove gets rid of the object reference, so
261 		 * we need to get rid of our 'object' pointer if there
262 		 * is subsequently an error.
263 		 */
264 		vm_map_remove(kernel_map, kva, kva + PAGE_SIZE);
265 		object = NULL;
266 
267 		/*
268 		 * Undo the protection 'damage'.
269 		 */
270 		if (fix_prot) {
271 			vm_map_protect(map, pageno, pageno + PAGE_SIZE,
272 				VM_PROT_READ|VM_PROT_EXECUTE, 0);
273 			fix_prot = 0;
274 		}
275 	} while (error == 0 && uio->uio_resid > 0);
276 
277 	if (object)
278 		vm_object_deallocate(object);
279 
280 	if (fix_prot)
281 		vm_map_protect(map, pageno, pageno + PAGE_SIZE,
282 				VM_PROT_READ|VM_PROT_EXECUTE, 0);
283 
284 	vmspace_free(vm);
285 	return (error);
286 }
287 
288 /*
289  * Copy data in and out of the target process.
290  * We do this by mapping the process's page into
291  * the kernel and then doing a uiomove direct
292  * from the kernel address space.
293  */
294 int
295 procfs_domem(curp, p, pfs, uio)
296 	struct proc *curp;
297 	struct proc *p;
298 	struct pfsnode *pfs;
299 	struct uio *uio;
300 {
301 	int error;
302 
303 	if (uio->uio_resid == 0)
304 		return (0);
305 
306 	error = procfs_rwmem(p, uio);
307 
308 	return (error);
309 }
310 
311 /*
312  * Given process (p), find the vnode from which
313  * it's text segment is being executed.
314  *
315  * It would be nice to grab this information from
316  * the VM system, however, there is no sure-fire
317  * way of doing that.  Instead, fork(), exec() and
318  * wait() all maintain the p_textvp field in the
319  * process proc structure which contains a held
320  * reference to the exec'ed vnode.
321  */
322 struct vnode *
323 procfs_findtextvp(p)
324 	struct proc *p;
325 {
326 	return (p->p_textvp);
327 }
328