xref: /openbsd-src/sys/uvm/uvm_glue.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: uvm_glue.c,v 1.75 2019/06/21 09:39:49 visa Exp $	*/
2 /*	$NetBSD: uvm_glue.c,v 1.44 2001/02/06 19:54:44 eeh Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * Copyright (c) 1991, 1993, The Regents of the University of California.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * The Mach Operating System project at Carnegie-Mellon University.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)vm_glue.c	8.6 (Berkeley) 1/5/94
38  * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
39  *
40  *
41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  */
64 
65 /*
66  * uvm_glue.c: glue functions
67  */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/proc.h>
72 #include <sys/resourcevar.h>
73 #include <sys/buf.h>
74 #include <sys/user.h>
75 #ifdef SYSVSHM
76 #include <sys/shm.h>
77 #endif
78 #include <sys/sched.h>
79 
80 #include <uvm/uvm.h>
81 
82 /*
83  * uvm_kernacc: can the kernel access a region of memory
84  *
85  * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
86  */
87 boolean_t
88 uvm_kernacc(caddr_t addr, size_t len, int rw)
89 {
90 	boolean_t rv;
91 	vaddr_t saddr, eaddr;
92 	vm_prot_t prot = rw == B_READ ? PROT_READ : PROT_WRITE;
93 
94 	saddr = trunc_page((vaddr_t)addr);
95 	eaddr = round_page((vaddr_t)addr + len);
96 	vm_map_lock_read(kernel_map);
97 	rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
98 	vm_map_unlock_read(kernel_map);
99 
100 	return(rv);
101 }
102 
103 /*
104  * uvm_vslock: wire user memory for I/O
105  *
106  * - called from sys_sysctl
107  */
108 int
109 uvm_vslock(struct proc *p, caddr_t addr, size_t len, vm_prot_t access_type)
110 {
111 	struct vm_map *map = &p->p_vmspace->vm_map;
112 	vaddr_t start, end;
113 
114 	start = trunc_page((vaddr_t)addr);
115 	end = round_page((vaddr_t)addr + len);
116 	if (end <= start)
117 		return (EINVAL);
118 
119 	return uvm_fault_wire(map, start, end, access_type);
120 }
121 
122 /*
123  * uvm_vsunlock: unwire user memory wired by uvm_vslock()
124  *
125  * - called from sys_sysctl
126  */
127 void
128 uvm_vsunlock(struct proc *p, caddr_t addr, size_t len)
129 {
130 	vaddr_t start, end;
131 
132 	start = trunc_page((vaddr_t)addr);
133 	end = round_page((vaddr_t)addr + len);
134 	KASSERT(end > start);
135 
136 	uvm_fault_unwire(&p->p_vmspace->vm_map, start, end);
137 }
138 
139 /*
140  * uvm_vslock_device: wire user memory, make sure it's device reachable
141  *  and bounce if necessary.
142  *
143  * - called from physio
144  */
145 int
146 uvm_vslock_device(struct proc *p, void *addr, size_t len,
147     vm_prot_t access_type, void **retp)
148 {
149 	struct vm_map *map = &p->p_vmspace->vm_map;
150 	struct vm_page *pg;
151 	struct pglist pgl;
152 	int npages;
153 	vaddr_t start, end, off;
154 	vaddr_t sva, va;
155 	vsize_t sz;
156 	int error, mapv, i;
157 
158 	start = trunc_page((vaddr_t)addr);
159 	end = round_page((vaddr_t)addr + len);
160 	sz = end - start;
161 	off = (vaddr_t)addr - start;
162 	if (end <= start)
163 		return (EINVAL);
164 
165 	vm_map_lock_read(map);
166 retry:
167 	mapv = map->timestamp;
168 	vm_map_unlock_read(map);
169 
170 	if ((error = uvm_fault_wire(map, start, end, access_type)))
171 		return (error);
172 
173 	vm_map_lock_read(map);
174 	if (mapv != map->timestamp)
175 		goto retry;
176 
177 	npages = atop(sz);
178 	for (i = 0; i < npages; i++) {
179 		paddr_t pa;
180 
181 		if (!pmap_extract(map->pmap, start + ptoa(i), &pa)) {
182 			error = EFAULT;
183 			goto out_unwire;
184 		}
185 		if (!PADDR_IS_DMA_REACHABLE(pa))
186 			break;
187 	}
188 	if (i == npages) {
189 		*retp = NULL;
190 		return (0);
191 	}
192 
193 	if ((va = uvm_km_valloc(kernel_map, sz)) == 0) {
194 		error = ENOMEM;
195 		goto out_unwire;
196 	}
197 	sva = va;
198 
199 	TAILQ_INIT(&pgl);
200 	error = uvm_pglistalloc(npages * PAGE_SIZE, dma_constraint.ucr_low,
201 	    dma_constraint.ucr_high, 0, 0, &pgl, npages, UVM_PLA_WAITOK);
202 	if (error)
203 		goto out_unmap;
204 
205 	while ((pg = TAILQ_FIRST(&pgl)) != NULL) {
206 		TAILQ_REMOVE(&pgl, pg, pageq);
207 		pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg), PROT_READ | PROT_WRITE);
208 		va += PAGE_SIZE;
209 	}
210 	pmap_update(pmap_kernel());
211 	KASSERT(va == sva + sz);
212 	*retp = (void *)(sva + off);
213 
214 	if ((error = copyin(addr, *retp, len)) == 0)
215 		return 0;
216 
217 	uvm_km_pgremove_intrsafe(sva, sva + sz);
218 	pmap_kremove(sva, sz);
219 	pmap_update(pmap_kernel());
220 out_unmap:
221 	uvm_km_free(kernel_map, sva, sz);
222 out_unwire:
223 	uvm_fault_unwire_locked(map, start, end);
224 	vm_map_unlock_read(map);
225 	return (error);
226 }
227 
228 /*
229  * uvm_vsunlock_device: unwire user memory wired by uvm_vslock_device()
230  *
231  * - called from physio
232  */
233 void
234 uvm_vsunlock_device(struct proc *p, void *addr, size_t len, void *map)
235 {
236 	vaddr_t start, end;
237 	vaddr_t kva;
238 	vsize_t sz;
239 
240 	start = trunc_page((vaddr_t)addr);
241 	end = round_page((vaddr_t)addr + len);
242 	KASSERT(end > start);
243 	sz = end - start;
244 
245 	if (map)
246 		copyout(map, addr, len);
247 
248 	uvm_fault_unwire_locked(&p->p_vmspace->vm_map, start, end);
249 	vm_map_unlock_read(&p->p_vmspace->vm_map);
250 
251 	if (!map)
252 		return;
253 
254 	kva = trunc_page((vaddr_t)map);
255 	uvm_km_pgremove_intrsafe(kva, kva + sz);
256 	pmap_kremove(kva, sz);
257 	pmap_update(pmap_kernel());
258 	uvm_km_free(kernel_map, kva, sz);
259 }
260 
261 /*
262  * uvm_uarea_alloc: allocate the u-area for a new thread
263  */
264 vaddr_t
265 uvm_uarea_alloc(void)
266 {
267 	vaddr_t uaddr;
268 
269 	uaddr = uvm_km_kmemalloc_pla(kernel_map, uvm.kernel_object, USPACE,
270 	    USPACE_ALIGN, UVM_KMF_ZERO,
271 	    no_constraint.ucr_low, no_constraint.ucr_high,
272 	    0, 0, USPACE/PAGE_SIZE);
273 
274 	return (uaddr);
275 }
276 
277 /*
278  * uvm_uarea_free: free a dead thread's stack
279  *
280  * - the thread passed to us is a dead thread; we
281  *   are running on a different context now (the reaper).
282  */
283 void
284 uvm_uarea_free(struct proc *p)
285 {
286 	uvm_km_free(kernel_map, (vaddr_t)p->p_addr, USPACE);
287 	p->p_addr = NULL;
288 }
289 
290 /*
291  * uvm_exit: exit a virtual address space
292  */
293 void
294 uvm_exit(struct process *pr)
295 {
296 	uvmspace_free(pr->ps_vmspace);
297 	pr->ps_vmspace = NULL;
298 }
299 
300 /*
301  * uvm_init_limit: init per-process VM limits
302  *
303  * - called for process 0 and then inherited by all others.
304  */
305 void
306 uvm_init_limits(struct plimit *limit0)
307 {
308 	/*
309 	 * Set up the initial limits on process VM.  Set the maximum
310 	 * resident set size to be all of (reasonably) available memory.
311 	 * This causes any single, large process to start random page
312 	 * replacement once it fills memory.
313 	 */
314 	limit0->pl_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
315 	limit0->pl_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
316 	limit0->pl_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
317 	limit0->pl_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
318 	limit0->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
319 }
320 
321 #ifdef DEBUG
322 int	enableswap = 1;
323 int	swapdebug = 0;
324 #define	SDB_FOLLOW	1
325 #define SDB_SWAPIN	2
326 #define SDB_SWAPOUT	4
327 #endif
328 
329 
330 /*
331  * swapout_threads: find threads that can be swapped
332  *
333  * - called by the pagedaemon
334  * - try and swap at least one processs
335  * - processes that are sleeping or stopped for maxslp or more seconds
336  *   are swapped... otherwise the longest-sleeping or stopped process
337  *   is swapped, otherwise the longest resident process...
338  */
339 void
340 uvm_swapout_threads(void)
341 {
342 	struct process *pr;
343 	struct proc *p, *slpp;
344 	struct process *outpr;
345 	int outpri;
346 	int didswap = 0;
347 	extern int maxslp;
348 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
349 
350 #ifdef DEBUG
351 	if (!enableswap)
352 		return;
353 #endif
354 
355 	/*
356 	 * outpr/outpri  : stop/sleep process whose most active thread has
357 	 *	the largest sleeptime < maxslp
358 	 */
359 	outpr = NULL;
360 	outpri = 0;
361 	LIST_FOREACH(pr, &allprocess, ps_list) {
362 		if (pr->ps_flags & (PS_SYSTEM | PS_EXITING))
363 			continue;
364 
365 		/*
366 		 * slpp: the sleeping or stopped thread in pr with
367 		 * the smallest p_slptime
368 		 */
369 		slpp = NULL;
370 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
371 			switch (p->p_stat) {
372 			case SRUN:
373 			case SONPROC:
374 				goto next_process;
375 
376 			case SSLEEP:
377 			case SSTOP:
378 				if (slpp == NULL ||
379 				    slpp->p_slptime < p->p_slptime)
380 					slpp = p;
381 				continue;
382 			}
383 		}
384 
385 		if (slpp != NULL) {
386 			if (slpp->p_slptime >= maxslp) {
387 				pmap_collect(pr->ps_vmspace->vm_map.pmap);
388 				didswap++;
389 			} else if (slpp->p_slptime > outpri) {
390 				outpr = pr;
391 				outpri = slpp->p_slptime;
392 			}
393 		}
394 next_process:	;
395 	}
396 
397 	/*
398 	 * If we didn't get rid of any real duds, toss out the next most
399 	 * likely sleeping/stopped or running candidate.  We only do this
400 	 * if we are real low on memory since we don't gain much by doing
401 	 * it.
402 	 */
403 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE)) &&
404 	    outpr != NULL) {
405 #ifdef DEBUG
406 		if (swapdebug & SDB_SWAPOUT)
407 			printf("swapout_threads: no duds, try procpr %p\n",
408 			    outpr);
409 #endif
410 		pmap_collect(outpr->ps_vmspace->vm_map.pmap);
411 	}
412 }
413 
414 /*
415  * uvm_atopg: convert KVAs back to their page structures.
416  */
417 struct vm_page *
418 uvm_atopg(vaddr_t kva)
419 {
420 	struct vm_page *pg;
421 	paddr_t pa;
422 	boolean_t rv;
423 
424 	rv = pmap_extract(pmap_kernel(), kva, &pa);
425 	KASSERT(rv);
426 	pg = PHYS_TO_VM_PAGE(pa);
427 	KASSERT(pg != NULL);
428 	return (pg);
429 }
430 
431 void
432 uvm_pause(void)
433 {
434 	static unsigned int toggle;
435 	if (toggle++ > 128) {
436 		toggle = 0;
437 		KERNEL_UNLOCK();
438 		KERNEL_LOCK();
439 	}
440 	sched_pause(preempt);
441 }
442 
443 #ifndef SMALL_KERNEL
444 int
445 fill_vmmap(struct process *pr, struct kinfo_vmentry *kve,
446     size_t *lenp)
447 {
448 	struct vm_map *map;
449 
450 	if (pr != NULL)
451 		map = &pr->ps_vmspace->vm_map;
452 	else
453 		map = kernel_map;
454 	return uvm_map_fill_vmmap(map, kve, lenp);
455 }
456 #endif
457