xref: /openbsd/sys/uvm/uvm_extern.h (revision fc61954a)
1 /*	$OpenBSD: uvm_extern.h,v 1.139 2016/06/05 08:35:57 stefan Exp $	*/
2 /*	$NetBSD: uvm_extern.h,v 1.57 2001/03/09 01:02:12 chs Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 1991, 1992, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
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. Neither the name of the University nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  *	@(#)vm_extern.h	8.5 (Berkeley) 5/3/95
58  */
59 
60 #ifndef _UVM_UVM_EXTERN_H_
61 #define _UVM_UVM_EXTERN_H_
62 
63 typedef int vm_fault_t;
64 
65 typedef int vm_inherit_t;	/* XXX: inheritance codes */
66 typedef off_t voff_t;		/* XXX: offset within a uvm_object */
67 
68 union vm_map_object;
69 typedef union vm_map_object vm_map_object_t;
70 
71 struct vm_map_entry;
72 typedef struct vm_map_entry *vm_map_entry_t;
73 
74 struct vm_map;
75 typedef struct vm_map *vm_map_t;
76 
77 struct vm_page;
78 typedef struct vm_page  *vm_page_t;
79 
80 /*
81  * Bit assignments assigned by UVM_MAPFLAG() and extracted by
82  * UVM_{PROTECTION,INHERIT,MAXPROTECTION,ADVICE}():
83  * bits 0-2	protection
84  *  bit 3	 unused
85  * bits 4-5	inheritance
86  *  bits 6-7	 unused
87  * bits 8-10	max protection
88  *  bit 11	 unused
89  * bits 12-14	advice
90  *  bit 15	 unused
91  * bits 16-N	flags
92  */
93 
94 /* protections bits */
95 #define PROT_MASK	(PROT_READ | PROT_WRITE | PROT_EXEC)
96 
97 /* inherit codes */
98 #define MAP_INHERIT_MASK	0x3	/* inherit mask */
99 
100 typedef int		vm_prot_t;
101 
102 #define MADV_MASK	0x7	/* mask */
103 
104 /* mapping flags */
105 #define UVM_FLAG_FIXED   0x0010000 /* find space */
106 #define UVM_FLAG_OVERLAY 0x0020000 /* establish overlay */
107 #define UVM_FLAG_NOMERGE 0x0040000 /* don't merge map entries */
108 #define UVM_FLAG_COPYONW 0x0080000 /* set copy_on_write flag */
109 #define UVM_FLAG_TRYLOCK 0x0100000 /* fail if we can not lock map */
110 #define UVM_FLAG_HOLE    0x0200000 /* no backend */
111 #define UVM_FLAG_QUERY   0x0400000 /* do everything, except actual execution */
112 #define UVM_FLAG_NOFAULT 0x0800000 /* don't fault */
113 #define UVM_FLAG_UNMAP   0x1000000 /* unmap to make space */
114 
115 
116 /* macros to extract info */
117 #define UVM_PROTECTION(X)	((X) & PROT_MASK)
118 #define UVM_INHERIT(X)		(((X) >> 4) & MAP_INHERIT_MASK)
119 #define UVM_MAXPROTECTION(X)	(((X) >> 8) & PROT_MASK)
120 #define UVM_ADVICE(X)		(((X) >> 12) & MADV_MASK)
121 
122 #define UVM_MAPFLAG(prot, maxprot, inh, advice, flags) \
123 	((prot) | ((maxprot) << 8) | ((inh) << 4) | ((advice) << 12) | (flags))
124 
125 /* magic offset value */
126 #define UVM_UNKNOWN_OFFSET ((voff_t) -1)
127 				/* offset not known(obj) or don't care(!obj) */
128 
129 /*
130  * the following defines are for uvm_km_kmemalloc's flags
131  */
132 #define UVM_KMF_NOWAIT	0x1			/* matches M_NOWAIT */
133 #define UVM_KMF_VALLOC	0x2			/* allocate VA only */
134 #define UVM_KMF_CANFAIL	0x4			/* caller handles failure */
135 #define UVM_KMF_ZERO	0x08			/* zero pages */
136 #define UVM_KMF_TRYLOCK	UVM_FLAG_TRYLOCK	/* try locking only */
137 
138 /*
139  * flags for uvm_pagealloc()
140  */
141 #define UVM_PGA_USERESERVE	0x0001	/* ok to use reserve pages */
142 #define	UVM_PGA_ZERO		0x0002	/* returned page must be zeroed */
143 
144 /*
145  * flags for uvm_pglistalloc()
146  */
147 #define UVM_PLA_WAITOK		0x0001	/* may sleep */
148 #define UVM_PLA_NOWAIT		0x0002	/* can't sleep (need one of the two) */
149 #define UVM_PLA_ZERO		0x0004	/* zero all pages before returning */
150 #define UVM_PLA_TRYCONTIG	0x0008	/* try to allocate contig physmem */
151 #define UVM_PLA_FAILOK		0x0010	/* caller can handle failure */
152 
153 /*
154  * lockflags that control the locking behavior of various functions.
155  */
156 #define	UVM_LK_ENTER	0x00000001	/* map locked on entry */
157 #define	UVM_LK_EXIT	0x00000002	/* leave map locked on exit */
158 
159 /*
160  * flags to uvm_page_physload.
161  */
162 #define	PHYSLOAD_DEVICE	0x01	/* don't add to the page queue */
163 
164 #include <sys/queue.h>
165 #include <sys/tree.h>
166 #include <sys/mman.h>
167 
168 #ifdef _KERNEL
169 struct buf;
170 struct mount;
171 struct pglist;
172 struct vmspace;
173 struct pmap;
174 #endif
175 
176 #include <uvm/uvm_param.h>
177 
178 #include <uvm/uvm_pmap.h>
179 #include <uvm/uvm_object.h>
180 #include <uvm/uvm_page.h>
181 #include <uvm/uvm_map.h>
182 
183 #ifdef _KERNEL
184 #include <uvm/uvm_fault.h>
185 #include <uvm/uvm_pager.h>
186 #endif
187 
188 /*
189  * Shareable process virtual address space.
190  * May eventually be merged with vm_map.
191  * Several fields are temporary (text, data stuff).
192  */
193 struct vmspace {
194 	struct	vm_map vm_map;	/* VM address map */
195 	int	vm_refcnt;	/* number of references */
196 	caddr_t	vm_shm;		/* SYS5 shared memory private data XXX */
197 /* we copy from vm_startcopy to the end of the structure on fork */
198 #define vm_startcopy vm_rssize
199 	segsz_t vm_rssize; 	/* current resident set size in pages */
200 	segsz_t vm_swrss;	/* resident set size before last swap */
201 	segsz_t vm_tsize;	/* text size (pages) XXX */
202 	segsz_t vm_dsize;	/* data size (pages) XXX */
203 	segsz_t vm_dused;	/* data segment length (pages) XXX */
204 	segsz_t vm_ssize;	/* stack size (pages) */
205 	caddr_t	vm_taddr;	/* user virtual address of text XXX */
206 	caddr_t	vm_daddr;	/* user virtual address of data XXX */
207 	caddr_t vm_maxsaddr;	/* user VA at max stack growth */
208 	caddr_t vm_minsaddr;	/* user VA at top of stack */
209 };
210 
211 /*
212  * uvm_constraint_range's:
213  * MD code is allowed to setup constraint ranges for memory allocators, the
214  * primary use for this is to keep allocation for certain memory consumers
215  * such as mbuf pools withing address ranges that are reachable by devices
216  * that perform DMA.
217  *
218  * It is also to discourge memory allocations from being satisfied from ranges
219  * such as the ISA memory range, if they can be satisfied with allocation
220  * from other ranges.
221  *
222  * the MD ranges are defined in arch/ARCH/ARCH/machdep.c
223  */
224 struct uvm_constraint_range {
225 	paddr_t	ucr_low;
226 	paddr_t ucr_high;
227 };
228 
229 #ifdef _KERNEL
230 
231 #include <uvm/uvmexp.h>
232 extern struct uvmexp uvmexp;
233 
234 /* Constraint ranges, set by MD code. */
235 extern struct uvm_constraint_range  isa_constraint;
236 extern struct uvm_constraint_range  dma_constraint;
237 extern struct uvm_constraint_range  no_constraint;
238 extern struct uvm_constraint_range *uvm_md_constraints[];
239 
240 extern struct pool *uvm_aiobuf_pool;
241 
242 /*
243  * used to keep state while iterating over the map for a core dump.
244  */
245 struct uvm_coredump_state {
246 	void *cookie;		/* opaque for the caller */
247 	vaddr_t start;		/* start of region */
248 	vaddr_t realend;	/* real end of region */
249 	vaddr_t end;		/* virtual end of region */
250 	vm_prot_t prot;		/* protection of region */
251 	int flags;		/* flags; see below */
252 };
253 
254 #define	UVM_COREDUMP_STACK	0x01	/* region is user stack */
255 
256 /*
257  * the various kernel maps, owned by MD code
258  */
259 extern struct vm_map *exec_map;
260 extern struct vm_map *kernel_map;
261 extern struct vm_map *kmem_map;
262 extern struct vm_map *phys_map;
263 
264 /* base of kernel virtual memory */
265 extern vaddr_t vm_min_kernel_address;
266 
267 /* zalloc zeros memory, alloc does not */
268 #define uvm_km_zalloc(MAP,SIZE) uvm_km_alloc1(MAP,SIZE,0,TRUE)
269 #define uvm_km_alloc(MAP,SIZE)  uvm_km_alloc1(MAP,SIZE,0,FALSE)
270 
271 #define vm_resident_count(vm) (pmap_resident_count((vm)->vm_map.pmap))
272 
273 void			vmapbuf(struct buf *, vsize_t);
274 void			vunmapbuf(struct buf *, vsize_t);
275 void			cpu_fork(struct proc *, struct proc *, void *,
276 			    size_t, void (*)(void *), void *);
277 struct uvm_object	*uao_create(vsize_t, int);
278 void			uao_detach(struct uvm_object *);
279 void			uao_detach_locked(struct uvm_object *);
280 void			uao_reference(struct uvm_object *);
281 void			uao_reference_locked(struct uvm_object *);
282 int			uvm_fault(vm_map_t, vaddr_t, vm_fault_t, vm_prot_t);
283 
284 #if defined(KGDB)
285 void			uvm_chgkprot(caddr_t, size_t, int);
286 #endif
287 vaddr_t			uvm_uarea_alloc(void);
288 void			uvm_uarea_free(struct proc *);
289 void			uvm_exit(struct process *);
290 void			uvm_init_limits(struct proc *);
291 boolean_t		uvm_kernacc(caddr_t, size_t, int);
292 
293 int			uvm_vslock(struct proc *, caddr_t, size_t,
294 			    vm_prot_t);
295 void			uvm_vsunlock(struct proc *, caddr_t, size_t);
296 int			uvm_vslock_device(struct proc *, void *, size_t,
297 			    vm_prot_t, void **);
298 void			uvm_vsunlock_device(struct proc *, void *, size_t,
299 			    void *);
300 void			uvm_pause(void);
301 void			uvm_init(void);
302 int			uvm_io(vm_map_t, struct uio *, int);
303 
304 #define	UVM_IO_FIXPROT	0x01
305 
306 vaddr_t			uvm_km_alloc1(vm_map_t, vsize_t, vsize_t, boolean_t);
307 void			uvm_km_free(vm_map_t, vaddr_t, vsize_t);
308 void			uvm_km_free_wakeup(vm_map_t, vaddr_t, vsize_t);
309 vaddr_t			uvm_km_kmemalloc_pla(struct vm_map *,
310 			    struct uvm_object *, vsize_t, vsize_t, int,
311 			    paddr_t, paddr_t, paddr_t, paddr_t, int);
312 #define uvm_km_kmemalloc(map, obj, sz, flags)				\
313 	uvm_km_kmemalloc_pla(map, obj, sz, 0, flags, 0, (paddr_t)-1, 0, 0, 0)
314 vaddr_t			uvm_km_valloc(vm_map_t, vsize_t);
315 vaddr_t			uvm_km_valloc_try(vm_map_t, vsize_t);
316 vaddr_t			uvm_km_valloc_wait(vm_map_t, vsize_t);
317 vaddr_t			uvm_km_valloc_align(struct vm_map *, vsize_t,
318 			    vsize_t, int);
319 vaddr_t			uvm_km_valloc_prefer_wait(vm_map_t, vsize_t, voff_t);
320 struct vm_map		*uvm_km_suballoc(vm_map_t, vaddr_t *, vaddr_t *,
321 			    vsize_t, int, boolean_t, vm_map_t);
322 /*
323  * Allocation mode for virtual space.
324  *
325  *  kv_map - pointer to the pointer to the map we're allocating from.
326  *  kv_align - alignment.
327  *  kv_wait - wait for free space in the map if it's full. The default
328  *   allocators don't wait since running out of space in kernel_map and
329  *   kmem_map is usually fatal. Special maps like exec_map are specifically
330  *   limited, so waiting for space in them is necessary.
331  *  kv_singlepage - use the single page allocator.
332  *  kv_executable - map the physical pages with PROT_EXEC.
333  */
334 struct kmem_va_mode {
335 	struct vm_map **kv_map;
336 	vsize_t kv_align;
337 	char kv_wait;
338 	char kv_singlepage;
339 };
340 
341 /*
342  * Allocation mode for physical pages.
343  *
344  *  kp_constraint - allocation constraint for physical pages.
345  *  kp_object - if the pages should be allocated from an object.
346  *  kp_align - physical alignment of the first page in the allocation.
347  *  kp_boundary - boundary that the physical addresses can't cross if
348  *   the allocation is contiguous.
349  *  kp_nomem - don't allocate any backing pages.
350  *  kp_maxseg - maximal amount of contiguous segments.
351  *  kp_zero - zero the returned memory.
352  *  kp_pageable - allocate pageable memory.
353  */
354 struct kmem_pa_mode {
355 	struct uvm_constraint_range *kp_constraint;
356 	struct uvm_object **kp_object;
357 	paddr_t kp_align;
358 	paddr_t kp_boundary;
359 	int kp_maxseg;
360 	char kp_nomem;
361 	char kp_zero;
362 	char kp_pageable;
363 };
364 
365 /*
366  * Dynamic allocation parameters. Stuff that changes too often or too much
367  * to create separate va and pa modes for.
368  *
369  * kd_waitok - is it ok to sleep?
370  * kd_trylock - don't sleep on map locks.
371  * kd_prefer - offset to feed to PMAP_PREFER
372  * kd_slowdown - special parameter for the singlepage va allocator
373  *  that tells the caller to sleep if possible to let the singlepage
374  *  allocator catch up.
375  */
376 struct kmem_dyn_mode {
377 	voff_t kd_prefer;
378 	int *kd_slowdown;
379 	char kd_waitok;
380 	char kd_trylock;
381 };
382 
383 #define KMEM_DYN_INITIALIZER { UVM_UNKNOWN_OFFSET, NULL, 0, 0 }
384 
385 /*
386  * Notice that for kv_ waiting has a different meaning. It's only supposed
387  * to be used for very space constrained maps where waiting is a way
388  * to throttle some other operation.
389  * The exception is kv_page which needs to wait relatively often.
390  * All kv_ except kv_intrsafe will potentially sleep.
391  */
392 extern const struct kmem_va_mode kv_any;
393 extern const struct kmem_va_mode kv_intrsafe;
394 extern const struct kmem_va_mode kv_page;
395 
396 extern const struct kmem_pa_mode kp_dirty;
397 extern const struct kmem_pa_mode kp_zero;
398 extern const struct kmem_pa_mode kp_dma;
399 extern const struct kmem_pa_mode kp_dma_contig;
400 extern const struct kmem_pa_mode kp_dma_zero;
401 extern const struct kmem_pa_mode kp_pageable;
402 extern const struct kmem_pa_mode kp_none;
403 
404 extern const struct kmem_dyn_mode kd_waitok;
405 extern const struct kmem_dyn_mode kd_nowait;
406 extern const struct kmem_dyn_mode kd_trylock;
407 
408 void			*km_alloc(size_t, const struct kmem_va_mode *,
409 			    const struct kmem_pa_mode *,
410 			    const struct kmem_dyn_mode *);
411 void			km_free(void *, size_t, const struct kmem_va_mode *,
412 			    const struct kmem_pa_mode *);
413 int			uvm_map(vm_map_t, vaddr_t *, vsize_t,
414 			    struct uvm_object *, voff_t, vsize_t, unsigned int);
415 int			uvm_mapanon(vm_map_t, vaddr_t *, vsize_t, vsize_t, unsigned int);
416 int			uvm_map_pageable(vm_map_t, vaddr_t,
417 			    vaddr_t, boolean_t, int);
418 int			uvm_map_pageable_all(vm_map_t, int, vsize_t);
419 boolean_t		uvm_map_checkprot(vm_map_t, vaddr_t,
420 			    vaddr_t, vm_prot_t);
421 int			uvm_map_protect(vm_map_t, vaddr_t,
422 			    vaddr_t, vm_prot_t, boolean_t);
423 struct vmspace		*uvmspace_alloc(vaddr_t, vaddr_t,
424 			    boolean_t, boolean_t);
425 void			uvmspace_init(struct vmspace *, struct pmap *,
426 			    vaddr_t, vaddr_t, boolean_t, boolean_t);
427 void			uvmspace_exec(struct proc *, vaddr_t, vaddr_t);
428 struct vmspace		*uvmspace_fork(struct process *);
429 void			uvmspace_free(struct vmspace *);
430 struct vmspace		*uvmspace_share(struct process *);
431 int			uvm_share(vm_map_t, vaddr_t, vm_prot_t,
432 			    vm_map_t, vaddr_t, vsize_t);
433 void			uvm_meter(void);
434 int			uvm_sysctl(int *, u_int, void *, size_t *,
435 			    void *, size_t, struct proc *);
436 struct vm_page		*uvm_pagealloc(struct uvm_object *,
437 			    voff_t, struct vm_anon *, int);
438 vaddr_t			uvm_pagealloc_contig(vaddr_t, vaddr_t,
439 			    vaddr_t, vaddr_t);
440 int			uvm_pagealloc_multi(struct uvm_object *, voff_t,
441     			    vsize_t, int);
442 void			uvm_pagerealloc(struct vm_page *,
443 			    struct uvm_object *, voff_t);
444 int			uvm_pagerealloc_multi(struct uvm_object *, voff_t,
445 			    vsize_t, int, struct uvm_constraint_range *);
446 /* Actually, uvm_page_physload takes PF#s which need their own type */
447 void			uvm_page_physload(paddr_t, paddr_t, paddr_t,
448 			    paddr_t, int);
449 void			uvm_setpagesize(void);
450 void			uvm_shutdown(void);
451 void			uvm_aio_biodone(struct buf *);
452 void			uvm_aio_aiodone(struct buf *);
453 void			uvm_pageout(void *);
454 void			uvm_aiodone_daemon(void *);
455 void			uvm_wait(const char *);
456 int			uvm_pglistalloc(psize_t, paddr_t, paddr_t,
457 			    paddr_t, paddr_t, struct pglist *, int, int);
458 void			uvm_pglistfree(struct pglist *);
459 void			uvm_pmr_use_inc(paddr_t, paddr_t);
460 void			uvm_swap_init(void);
461 int			uvm_coredump_walkmap(struct proc *,
462 			    void *, int (*)(struct proc *, void *,
463 			    struct uvm_coredump_state *), void *);
464 void			uvm_grow(struct proc *, vaddr_t);
465 void			uvm_deallocate(vm_map_t, vaddr_t, vsize_t);
466 struct uvm_object	*uvn_attach(struct vnode *, vm_prot_t);
467 void			uvm_pagezero_thread(void *);
468 void			kmeminit_nkmempages(void);
469 void			kmeminit(void);
470 extern u_int		nkmempages;
471 
472 struct process;
473 struct kinfo_vmentry;
474 int			fill_vmmap(struct process *, struct kinfo_vmentry *,
475 			    size_t *);
476 
477 #endif /* _KERNEL */
478 
479 #endif /* _UVM_UVM_EXTERN_H_ */
480