xref: /original-bsd/sys/vm/vm_user.c (revision 3b6250d9)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)vm_user.c	7.4 (Berkeley) 05/04/92
11  *
12  *
13  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
14  * All rights reserved.
15  *
16  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
17  *
18  * Permission to use, copy, modify and distribute this software and
19  * its documentation is hereby granted, provided that both the copyright
20  * notice and this permission notice appear in all copies of the
21  * software, derivative works or modified versions, and any portions
22  * thereof, and that both notices appear in supporting documentation.
23  *
24  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
26  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27  *
28  * Carnegie Mellon requests users of this software to return to
29  *
30  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
31  *  School of Computer Science
32  *  Carnegie Mellon University
33  *  Pittsburgh PA 15213-3890
34  *
35  * any improvements or extensions that they make and grant Carnegie the
36  * rights to redistribute these changes.
37  */
38 
39 /*
40  *	User-exported virtual memory functions.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 
47 #include <vm/vm.h>
48 
49 simple_lock_data_t	vm_alloc_lock;	/* XXX */
50 
51 #ifdef MACHVMCOMPAT
52 /*
53  * BSD style syscall interfaces to MACH calls
54  * All return MACH return values.
55  */
56 /* ARGSUSED */
57 int
58 svm_allocate(p, uap, retval)
59 	struct proc *p;
60 	struct args {
61 		vm_map_t map;
62 		vm_offset_t *addr;
63 		vm_size_t size;
64 		boolean_t anywhere;
65 	} *uap;
66 	int *retval;
67 {
68 	vm_offset_t addr;
69 	int rv;
70 
71 	uap->map = p->p_map;		/* XXX */
72 
73 	if (copyin((caddr_t)uap->addr, (caddr_t)&addr, sizeof (addr)))
74 		rv = KERN_INVALID_ARGUMENT;
75 	else
76 		rv = vm_allocate(uap->map, &addr, uap->size, uap->anywhere);
77 	if (rv == KERN_SUCCESS) {
78 		if (copyout((caddr_t)&addr, (caddr_t)uap->addr, sizeof(addr)))
79 			rv = KERN_INVALID_ARGUMENT;
80 	}
81 	return((int)rv);
82 }
83 
84 /* ARGSUSED */
85 int
86 svm_deallocate(p, uap, retval)
87 	struct proc *p;
88 	struct args {
89 		vm_map_t map;
90 		vm_offset_t addr;
91 		vm_size_t size;
92 	} *uap;
93 	int *retval;
94 {
95 	int rv;
96 
97 	uap->map = p->p_map;		/* XXX */
98 	rv = vm_deallocate(uap->map, uap->addr, uap->size);
99 	return((int)rv);
100 }
101 
102 /* ARGSUSED */
103 int
104 svm_inherit(p, uap, retval)
105 	struct proc *p;
106 	struct args {
107 		vm_map_t map;
108 		vm_offset_t addr;
109 		vm_size_t size;
110 		vm_inherit_t inherit;
111 	} *uap;
112 	int *retval;
113 {
114 	int rv;
115 
116 	uap->map = p->p_map;		/* XXX */
117 	rv = vm_inherit(uap->map, uap->addr, uap->size, uap->inherit);
118 	return((int)rv);
119 }
120 
121 /* ARGSUSED */
122 int
123 svm_protect(p, uap, retval)
124 	struct proc *p;
125 	struct args {
126 		vm_map_t map;
127 		vm_offset_t addr;
128 		vm_size_t size;
129 		boolean_t setmax;
130 		vm_prot_t prot;
131 	} *uap;
132 	int *retval;
133 {
134 	int rv;
135 
136 	uap->map = p->p_map;		/* XXX */
137 	rv = vm_protect(uap->map, uap->addr, uap->size, uap->setmax, uap->prot);
138 	return((int)rv);
139 }
140 #endif
141 
142 /*
143  *	vm_allocate allocates "zero fill" memory in the specfied
144  *	map.
145  */
146 int
147 vm_allocate(map, addr, size, anywhere)
148 	register vm_map_t	map;
149 	register vm_offset_t	*addr;
150 	register vm_size_t	size;
151 	boolean_t		anywhere;
152 {
153 	int	result;
154 
155 	if (map == NULL)
156 		return(KERN_INVALID_ARGUMENT);
157 	if (size == 0) {
158 		*addr = 0;
159 		return(KERN_SUCCESS);
160 	}
161 
162 	if (anywhere)
163 		*addr = vm_map_min(map);
164 	else
165 		*addr = trunc_page(*addr);
166 	size = round_page(size);
167 
168 	result = vm_map_find(map, NULL, (vm_offset_t) 0, addr,
169 			size, anywhere);
170 
171 	return(result);
172 }
173 
174 /*
175  *	vm_deallocate deallocates the specified range of addresses in the
176  *	specified address map.
177  */
178 int
179 vm_deallocate(map, start, size)
180 	register vm_map_t	map;
181 	vm_offset_t		start;
182 	vm_size_t		size;
183 {
184 	if (map == NULL)
185 		return(KERN_INVALID_ARGUMENT);
186 
187 	if (size == (vm_offset_t) 0)
188 		return(KERN_SUCCESS);
189 
190 	return(vm_map_remove(map, trunc_page(start), round_page(start+size)));
191 }
192 
193 /*
194  *	vm_inherit sets the inheritence of the specified range in the
195  *	specified map.
196  */
197 int
198 vm_inherit(map, start, size, new_inheritance)
199 	register vm_map_t	map;
200 	vm_offset_t		start;
201 	vm_size_t		size;
202 	vm_inherit_t		new_inheritance;
203 {
204 	if (map == NULL)
205 		return(KERN_INVALID_ARGUMENT);
206 
207 	return(vm_map_inherit(map, trunc_page(start), round_page(start+size), new_inheritance));
208 }
209 
210 /*
211  *	vm_protect sets the protection of the specified range in the
212  *	specified map.
213  */
214 
215 int
216 vm_protect(map, start, size, set_maximum, new_protection)
217 	register vm_map_t	map;
218 	vm_offset_t		start;
219 	vm_size_t		size;
220 	boolean_t		set_maximum;
221 	vm_prot_t		new_protection;
222 {
223 	if (map == NULL)
224 		return(KERN_INVALID_ARGUMENT);
225 
226 	return(vm_map_protect(map, trunc_page(start), round_page(start+size), new_protection, set_maximum));
227 }
228