386BSD 0.1 development
[unix-history] / usr / src / sys.386bsd / vm / vm_user.c
CommitLineData
b688fc87
WJ
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 * 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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vm_user.c 7.3 (Berkeley) 4/21/91
37 *
38 *
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
41 *
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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 * User-exported virtual memory functions.
67 */
68
69#include "param.h"
70#include "systm.h"
71#include "proc.h"
72
73#include "vm.h"
74#include "vm_page.h"
75
76simple_lock_data_t vm_alloc_lock; /* XXX */
77
78#ifdef MACHVMCOMPAT
79/*
80 * BSD style syscall interfaces to MACH calls
81 * All return MACH return values.
82 */
83/* ARGSUSED */
84svm_allocate(p, uap, retval)
85 struct proc *p;
86 struct args {
87 vm_map_t map;
88 vm_offset_t *addr;
89 vm_size_t size;
90 boolean_t anywhere;
91 } *uap;
92 int *retval;
93{
94 vm_offset_t addr;
95 int rv;
96
97 uap->map = p->p_map; /* XXX */
98
99 if (copyin((caddr_t)uap->addr, (caddr_t)&addr, sizeof (addr)))
100 rv = KERN_INVALID_ARGUMENT;
101 else
102 rv = vm_allocate(uap->map, &addr, uap->size, uap->anywhere);
103 if (rv == KERN_SUCCESS) {
104 if (copyout((caddr_t)&addr, (caddr_t)uap->addr, sizeof(addr)))
105 rv = KERN_INVALID_ARGUMENT;
106 }
107 return((int)rv);
108}
109
110/* ARGSUSED */
111svm_deallocate(p, uap, retval)
112 struct proc *p;
113 struct args {
114 vm_map_t map;
115 vm_offset_t addr;
116 vm_size_t size;
117 } *uap;
118 int *retval;
119{
120 int rv;
121
122 uap->map = p->p_map; /* XXX */
123 rv = vm_deallocate(uap->map, uap->addr, uap->size);
124 return((int)rv);
125}
126
127/* ARGSUSED */
128svm_inherit(p, uap, retval)
129 struct proc *p;
130 struct args {
131 vm_map_t map;
132 vm_offset_t addr;
133 vm_size_t size;
134 vm_inherit_t inherit;
135 } *uap;
136 int *retval;
137{
138 int rv;
139
140 uap->map = p->p_map; /* XXX */
141 rv = vm_inherit(uap->map, uap->addr, uap->size, uap->inherit);
142 return((int)rv);
143}
144
145/* ARGSUSED */
146svm_protect(p, uap, retval)
147 struct proc *p;
148 struct args {
149 vm_map_t map;
150 vm_offset_t addr;
151 vm_size_t size;
152 boolean_t setmax;
153 vm_prot_t prot;
154 } *uap;
155 int *retval;
156{
157 int rv;
158
159 uap->map = p->p_map; /* XXX */
160 rv = vm_protect(uap->map, uap->addr, uap->size, uap->setmax, uap->prot);
161 return((int)rv);
162}
163#endif
164
165/*
166 * vm_allocate allocates "zero fill" memory in the specfied
167 * map.
168 */
169vm_allocate(map, addr, size, anywhere)
170 register vm_map_t map;
171 register vm_offset_t *addr;
172 register vm_size_t size;
173 boolean_t anywhere;
174{
175 int result;
176
177 if (map == NULL)
178 return(KERN_INVALID_ARGUMENT);
179 if (size == 0) {
180 *addr = 0;
181 return(KERN_SUCCESS);
182 }
183
184 if (anywhere)
185 *addr = vm_map_min(map);
186 else
187 *addr = trunc_page(*addr);
188 size = round_page(size);
189
190 result = vm_map_find(map, NULL, (vm_offset_t) 0, addr,
191 size, anywhere);
192
193 return(result);
194}
195
196/*
197 * vm_deallocate deallocates the specified range of addresses in the
198 * specified address map.
199 */
200vm_deallocate(map, start, size)
201 register vm_map_t map;
202 vm_offset_t start;
203 vm_size_t size;
204{
205 if (map == NULL)
206 return(KERN_INVALID_ARGUMENT);
207
208 if (size == (vm_offset_t) 0)
209 return(KERN_SUCCESS);
210
211 return(vm_map_remove(map, trunc_page(start), round_page(start+size)));
212}
213
214/*
215 * vm_inherit sets the inheritence of the specified range in the
216 * specified map.
217 */
218vm_inherit(map, start, size, new_inheritance)
219 register vm_map_t map;
220 vm_offset_t start;
221 vm_size_t size;
222 vm_inherit_t new_inheritance;
223{
224 if (map == NULL)
225 return(KERN_INVALID_ARGUMENT);
226
227 return(vm_map_inherit(map, trunc_page(start), round_page(start+size), new_inheritance));
228}
229
230/*
231 * vm_protect sets the protection of the specified range in the
232 * specified map.
233 */
234
235vm_protect(map, start, size, set_maximum, new_protection)
236 register vm_map_t map;
237 vm_offset_t start;
238 vm_size_t size;
239 boolean_t set_maximum;
240 vm_prot_t new_protection;
241{
242 if (map == NULL)
243 return(KERN_INVALID_ARGUMENT);
244
245 return(vm_map_protect(map, trunc_page(start), round_page(start+size), new_protection, set_maximum));
246}