Add two more vmspace_*() system calls to read and write a vmspace. These
[dragonfly.git] / sys / vm / vm_vmspace.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vm/vm_vmspace.c,v 1.3 2006/10/10 15:43:16 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/kern_syscall.h>
42 #include <sys/mman.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/sysctl.h>
46 #include <sys/vkernel.h>
47
48 #include <vm/vm_extern.h>
49 #include <vm/pmap.h>
50
51 static struct vmspace_entry *vkernel_find_vmspace(struct vkernel *vk, void *id);
52
53 static MALLOC_DEFINE(M_VKERNEL, "vkernel", "VKernel structures");
54
55 /*
56  * vmspace_create (void *id, int type, void *data)
57  *
58  * Create a VMSPACE under the control of the caller with the specified id.
59  * An id of NULL cannot be used.  The type and data fields must currently
60  * be 0.
61  *
62  * The vmspace starts out completely empty.  Memory may be mapped into the
63  * VMSPACE with vmspace_mmap() and MAP_VPAGETABLE section(s) controlled
64  * with vmspace_mcontrol().
65  */
66 int
67 sys_vmspace_create(struct vmspace_create_args *uap)
68 {
69         struct vkernel *vk;
70         struct vmspace_entry *ve;
71
72         if (vkernel_enable == 0)
73                 return (EOPNOTSUPP);
74
75         /*
76          * Create a virtual kernel side-structure for the process if one
77          * does not exist.
78          */
79         if ((vk = curproc->p_vkernel) == NULL) {
80                 vk = kmalloc(sizeof(*vk), M_VKERNEL, M_WAITOK|M_ZERO);
81                 vk->vk_refs = 1;
82                 RB_INIT(&vk->vk_root);
83                 curproc->p_vkernel = vk;
84         }
85
86         /*
87          * Create a new VMSPACE
88          */
89         if (vkernel_find_vmspace(vk, uap->id))
90                 return (EEXIST);
91         ve = kmalloc(sizeof(struct vmspace_entry), M_VKERNEL, M_WAITOK|M_ZERO);
92         ve->vmspace = vmspace_alloc(VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
93         ve->id = uap->id;
94         pmap_pinit2(vmspace_pmap(ve->vmspace));
95         RB_INSERT(vmspace_rb_tree, &vk->vk_root, ve);
96         return (0);
97 }
98
99 /*
100  * vmspace_destroy (void *id)
101  *
102  * Destroy a VMSPACE.
103  */
104 int
105 sys_vmspace_destroy(struct vmspace_destroy_args *uap)
106 {
107         struct vkernel *vk;
108         struct vmspace_entry *ve;
109
110         if ((vk = curproc->p_vkernel) == NULL)
111                 return (EINVAL);
112         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
113                 return (ENOENT);
114         /* XXX check if active */
115         RB_REMOVE(vmspace_rb_tree, &vk->vk_root, ve);
116         vmspace_free(ve->vmspace);
117         kfree(ve, M_VKERNEL);
118         return(0);
119 }
120
121 /*
122  * vmspace_ctl (void *id, int cmd, void *ctx, int ctx_bytes, int timeout_us)
123  *
124  * Transfer control to a VMSPACE.  Control is returned after the specified
125  * number of microseconds or if a page fault, signal, trap, or system call
126  * occurs.  The context is updated as appropriate.
127  */
128 int
129 sys_vmspace_ctl(struct vmspace_ctl_args *uap)
130 {
131         struct vkernel *vk;
132         struct vmspace_entry *ve;
133
134         if ((vk = curproc->p_vkernel) == NULL)
135                 return (EINVAL);
136         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
137                 return (ENOENT);
138         return(EINVAL);
139 }
140
141 /*
142  * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
143  *
144  * map memory within a VMSPACE.  This function is just like a normal mmap()
145  * but operates on the vmspace's memory map.  Most callers use this to create
146  * a MAP_VPAGETABLE mapping.
147  */
148 int
149 sys_vmspace_mmap(struct vmspace_mmap_args *uap)
150 {
151         struct vkernel *vk;
152         struct vmspace_entry *ve;
153         int error;
154
155         if ((vk = curproc->p_vkernel) == NULL)
156                 return (EINVAL);
157         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
158                 return (ENOENT);
159         error = kern_mmap(ve->vmspace, uap->addr, uap->len,
160                           uap->prot, uap->flags,
161                           uap->fd, uap->offset, &uap->sysmsg_resultp);
162         return (error);
163 }
164
165 /*
166  * vmspace_munmap(id, addr, len)
167  *
168  * unmap memory within a VMSPACE.
169  */
170 int
171 sys_vmspace_munmap(struct vmspace_munmap_args *uap)
172 {
173         struct vkernel *vk;
174         struct vmspace_entry *ve;
175         vm_offset_t addr;
176         vm_size_t size, pageoff;
177         vm_map_t map;
178
179         if ((vk = curproc->p_vkernel) == NULL)
180                 return (EINVAL);
181         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
182                 return (ENOENT);
183
184         /*
185          * Copied from sys_munmap()
186          */
187         addr = (vm_offset_t)uap->addr;
188         size = uap->len;
189
190         pageoff = (addr & PAGE_MASK);
191         addr -= pageoff;
192         size += pageoff;
193         size = (vm_size_t)round_page(size);
194         if (addr + size < addr)
195                 return (EINVAL);
196         if (size == 0)
197                 return (0);
198
199         if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
200                 return (EINVAL);
201 #ifndef i386
202         if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
203                 return (EINVAL);
204 #endif
205         map = &ve->vmspace->vm_map;
206         if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
207                 return (EINVAL);
208         vm_map_remove(map, addr, addr + size);
209         return (0);
210 }
211
212 /* 
213  * vmspace_pread(id, buf, nbyte, flags, offset)
214  *
215  * Read data from a vmspace.  The number of bytes read is returned or
216  * -1 if an unrecoverable error occured.  If the number of bytes read is
217  * less then the request size, a page fault occured in the VMSPACE which
218  * the caller must resolve in order to proceed.
219  */
220 int
221 sys_vmspace_pread(struct vmspace_pread_args *uap)
222 {
223         struct vkernel *vk;
224         struct vmspace_entry *ve;
225
226         if ((vk = curproc->p_vkernel) == NULL)
227                 return (EINVAL);
228         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
229                 return (ENOENT);
230         return (EINVAL);
231 }
232
233 /*
234  * vmspace_pwrite(id, buf, nbyte, flags, offset)
235  *
236  * Write data to a vmspace.  The number of bytes written is returned or
237  * -1 if an unrecoverable error occured.  If the number of bytes written is
238  * less then the request size, a page fault occured in the VMSPACE which
239  * the caller must resolve in order to proceed.
240  */
241 int
242 sys_vmspace_pwrite(struct vmspace_pwrite_args *uap)
243 {
244         struct vkernel *vk;
245         struct vmspace_entry *ve;
246
247         if ((vk = curproc->p_vkernel) == NULL)
248                 return (EINVAL);
249         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
250                 return (ENOENT);
251         return (EINVAL);
252 }
253
254 /*
255  * vmspace_mcontrol(id, addr, len, behav, value)
256  *
257  * madvise/mcontrol support for a vmspace.
258  */
259 int
260 sys_vmspace_mcontrol(struct vmspace_mcontrol_args *uap)
261 {
262         struct vkernel *vk;
263         struct vmspace_entry *ve;
264         vm_offset_t start, end;
265
266         if ((vk = curproc->p_vkernel) == NULL)
267                 return (EINVAL);
268         if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
269                 return (ENOENT);
270
271         /*
272          * This code is basically copied from sys_mcontrol()
273          */
274         if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
275                 return (EINVAL);
276
277         if (VM_MAXUSER_ADDRESS > 0 &&
278                 ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
279                 return (EINVAL);
280 #ifndef i386
281         if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
282                 return (EINVAL);
283 #endif
284         if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
285                 return (EINVAL);
286
287         start = trunc_page((vm_offset_t) uap->addr);
288         end = round_page((vm_offset_t) uap->addr + uap->len);
289
290         return (vm_map_madvise(&ve->vmspace->vm_map, start, end,
291                                 uap->behav, uap->value));
292 }
293
294 /*
295  * Red black tree functions
296  */
297 static int rb_vmspace_compare(struct vmspace_entry *, struct vmspace_entry *);
298 RB_GENERATE(vmspace_rb_tree, vmspace_entry, rb_entry, rb_vmspace_compare);
299    
300 /* a->start is address, and the only field has to be initialized */
301 static int
302 rb_vmspace_compare(struct vmspace_entry *a, struct vmspace_entry *b)
303 {
304         if ((char *)a->id < (char *)b->id)
305                 return(-1);
306         else if ((char *)a->id > (char *)b->id)
307                 return(1);
308         return(0);
309 }
310
311 static
312 int
313 rb_vmspace_delete(struct vmspace_entry *ve, void *data)
314 {
315         struct vkernel *vk = data;
316
317         RB_REMOVE(vmspace_rb_tree, &vk->vk_root, ve);
318         vmspace_free(ve->vmspace);
319         kfree(ve, M_VKERNEL);
320         return(0);
321 }
322
323 static
324 struct vmspace_entry *
325 vkernel_find_vmspace(struct vkernel *vk, void *id)
326 {
327         struct vmspace_entry *ve;
328         struct vmspace_entry key;
329
330         key.id = id;
331         ve = RB_FIND(vmspace_rb_tree, &vk->vk_root, &key);
332         return (ve);
333 }
334
335 /*
336  * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
337  * a vkernel process.
338  */
339 void
340 vkernel_hold(struct vkernel *vk)
341 {
342         ++vk->vk_refs;
343 }
344
345 void
346 vkernel_drop(struct vkernel *vk)
347 {
348         KKASSERT(vk->vk_refs > 0);
349         if (--vk->vk_refs == 0) {
350                 RB_SCAN(vmspace_rb_tree, &vk->vk_root, NULL,
351                         rb_vmspace_delete, vk);
352                 kfree(vk, M_VKERNEL);
353         }
354 }
355