xref: /openbsd-src/sys/uvm/uvm_glue.c (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 /*	$OpenBSD: uvm_glue.c,v 1.80 2021/03/26 13:40:05 mpi 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 	struct vmspace *vm = pr->ps_vmspace;
297 
298 	pr->ps_vmspace = NULL;
299 	uvmspace_free(vm);
300 }
301 
302 /*
303  * uvm_init_limit: init per-process VM limits
304  *
305  * - called for process 0 and then inherited by all others.
306  */
307 void
308 uvm_init_limits(struct plimit *limit0)
309 {
310 	/*
311 	 * Set up the initial limits on process VM.  Set the maximum
312 	 * resident set size to be all of (reasonably) available memory.
313 	 * This causes any single, large process to start random page
314 	 * replacement once it fills memory.
315 	 */
316 	limit0->pl_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
317 	limit0->pl_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
318 	limit0->pl_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
319 	limit0->pl_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
320 	limit0->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
321 }
322 
323 #ifdef DEBUG
324 int	enableswap = 1;
325 int	swapdebug = 0;
326 #define	SDB_FOLLOW	1
327 #define SDB_SWAPIN	2
328 #define SDB_SWAPOUT	4
329 #endif
330 
331 
332 /*
333  * swapout_threads: find threads that can be swapped
334  *
335  * - called by the pagedaemon
336  * - try and swap at least one process
337  * - processes that are sleeping or stopped for maxslp or more seconds
338  *   are swapped... otherwise the longest-sleeping or stopped process
339  *   is swapped, otherwise the longest resident process...
340  */
341 void
342 uvm_swapout_threads(void)
343 {
344 	struct process *pr;
345 	struct proc *p, *slpp;
346 	struct process *outpr;
347 	int outpri;
348 	int didswap = 0;
349 	extern int maxslp;
350 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
351 
352 #ifdef DEBUG
353 	if (!enableswap)
354 		return;
355 #endif
356 
357 	/*
358 	 * outpr/outpri  : stop/sleep process whose most active thread has
359 	 *	the largest sleeptime < maxslp
360 	 */
361 	outpr = NULL;
362 	outpri = 0;
363 	LIST_FOREACH(pr, &allprocess, ps_list) {
364 		if (pr->ps_flags & (PS_SYSTEM | PS_EXITING))
365 			continue;
366 
367 		/*
368 		 * slpp: the sleeping or stopped thread in pr with
369 		 * the smallest p_slptime
370 		 */
371 		slpp = NULL;
372 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
373 			switch (p->p_stat) {
374 			case SRUN:
375 			case SONPROC:
376 				goto next_process;
377 
378 			case SSLEEP:
379 			case SSTOP:
380 				if (slpp == NULL ||
381 				    slpp->p_slptime < p->p_slptime)
382 					slpp = p;
383 				continue;
384 			}
385 		}
386 
387 		if (slpp != NULL) {
388 			if (slpp->p_slptime >= maxslp) {
389 				pmap_collect(pr->ps_vmspace->vm_map.pmap);
390 				didswap++;
391 			} else if (slpp->p_slptime > outpri) {
392 				outpr = pr;
393 				outpri = slpp->p_slptime;
394 			}
395 		}
396 next_process:	;
397 	}
398 
399 	/*
400 	 * If we didn't get rid of any real duds, toss out the next most
401 	 * likely sleeping/stopped or running candidate.  We only do this
402 	 * if we are real low on memory since we don't gain much by doing
403 	 * it.
404 	 */
405 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE)) &&
406 	    outpr != NULL) {
407 #ifdef DEBUG
408 		if (swapdebug & SDB_SWAPOUT)
409 			printf("swapout_threads: no duds, try procpr %p\n",
410 			    outpr);
411 #endif
412 		pmap_collect(outpr->ps_vmspace->vm_map.pmap);
413 	}
414 }
415 
416 /*
417  * uvm_atopg: convert KVAs back to their page structures.
418  */
419 struct vm_page *
420 uvm_atopg(vaddr_t kva)
421 {
422 	struct vm_page *pg;
423 	paddr_t pa;
424 	boolean_t rv;
425 
426 	rv = pmap_extract(pmap_kernel(), kva, &pa);
427 	KASSERT(rv);
428 	pg = PHYS_TO_VM_PAGE(pa);
429 	KASSERT(pg != NULL);
430 	return (pg);
431 }
432 
433 void
434 uvm_pause(void)
435 {
436 	static unsigned int toggle;
437 	if (toggle++ > 128) {
438 		toggle = 0;
439 		KERNEL_UNLOCK();
440 		KERNEL_LOCK();
441 	}
442 	sched_pause(preempt);
443 }
444 
445 #ifndef SMALL_KERNEL
446 int
447 fill_vmmap(struct process *pr, struct kinfo_vmentry *kve,
448     size_t *lenp)
449 {
450 	struct vm_map *map;
451 
452 	if (pr != NULL)
453 		map = &pr->ps_vmspace->vm_map;
454 	else
455 		map = kernel_map;
456 	return uvm_map_fill_vmmap(map, kve, lenp);
457 }
458 #endif
459