Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / vm / vm_vmspace.c
CommitLineData
133aabc4
MD
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 *
086c1d7e 34 * $DragonFly: src/sys/vm/vm_vmspace.c,v 1.6 2006/12/23 00:41:31 swildner Exp $
133aabc4
MD
35 */
36
37#include <sys/param.h>
38#include <sys/kernel.h>
39#include <sys/systm.h>
40#include <sys/sysproto.h>
021a4ed4 41#include <sys/kern_syscall.h>
d3313941
MD
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>
4a22e893
MD
47#include <sys/vmspace.h>
48#include <sys/spinlock2.h>
d3313941
MD
49
50#include <vm/vm_extern.h>
51#include <vm/pmap.h>
52
4a22e893
MD
53static struct vmspace_entry *vkernel_find_vmspace(struct vkernel_common *vc,
54 void *id);
55static void vmspace_entry_delete(struct vmspace_entry *ve,
56 struct vkernel_common *vc);
d3313941
MD
57
58static MALLOC_DEFINE(M_VKERNEL, "vkernel", "VKernel structures");
133aabc4
MD
59
60/*
d3313941 61 * vmspace_create (void *id, int type, void *data)
133aabc4 62 *
d3313941
MD
63 * Create a VMSPACE under the control of the caller with the specified id.
64 * An id of NULL cannot be used. The type and data fields must currently
65 * be 0.
133aabc4 66 *
d3313941
MD
67 * The vmspace starts out completely empty. Memory may be mapped into the
68 * VMSPACE with vmspace_mmap() and MAP_VPAGETABLE section(s) controlled
69 * with vmspace_mcontrol().
70 */
71int
72sys_vmspace_create(struct vmspace_create_args *uap)
73{
4a22e893 74 struct vkernel_common *vc;
d3313941 75 struct vmspace_entry *ve;
4a22e893 76 struct vkernel *vk;
d3313941
MD
77
78 if (vkernel_enable == 0)
79 return (EOPNOTSUPP);
80
81 /*
82 * Create a virtual kernel side-structure for the process if one
83 * does not exist.
84 */
85 if ((vk = curproc->p_vkernel) == NULL) {
86 vk = kmalloc(sizeof(*vk), M_VKERNEL, M_WAITOK|M_ZERO);
4a22e893
MD
87 vc = kmalloc(sizeof(*vc), M_VKERNEL, M_WAITOK|M_ZERO);
88 vc->vc_refs = 1;
89 spin_init(&vc->vc_spin);
90 RB_INIT(&vc->vc_root);
91 vk->vk_common = vc;
d3313941
MD
92 curproc->p_vkernel = vk;
93 }
4a22e893 94 vc = vk->vk_common;
d3313941
MD
95
96 /*
97 * Create a new VMSPACE
98 */
4a22e893 99 if (vkernel_find_vmspace(vc, uap->id))
d3313941
MD
100 return (EEXIST);
101 ve = kmalloc(sizeof(struct vmspace_entry), M_VKERNEL, M_WAITOK|M_ZERO);
88181b08 102 ve->vmspace = vmspace_alloc(VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
d3313941
MD
103 ve->id = uap->id;
104 pmap_pinit2(vmspace_pmap(ve->vmspace));
4a22e893 105 RB_INSERT(vmspace_rb_tree, &vc->vc_root, ve);
d3313941
MD
106 return (0);
107}
108
109/*
110 * vmspace_destroy (void *id)
133aabc4 111 *
d3313941 112 * Destroy a VMSPACE.
133aabc4
MD
113 */
114int
d3313941 115sys_vmspace_destroy(struct vmspace_destroy_args *uap)
133aabc4 116{
4a22e893 117 struct vkernel_common *vc;
d3313941 118 struct vmspace_entry *ve;
4a22e893 119 struct vkernel *vk;
d3313941
MD
120
121 if ((vk = curproc->p_vkernel) == NULL)
122 return (EINVAL);
4a22e893
MD
123 vc = vk->vk_common;
124 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
d3313941 125 return (ENOENT);
4a22e893
MD
126 if (ve->refs)
127 return (EBUSY);
128 vmspace_entry_delete(ve, vc);
d3313941 129 return(0);
133aabc4
MD
130}
131
132/*
d3313941 133 * vmspace_ctl (void *id, int cmd, void *ctx, int ctx_bytes, int timeout_us)
133aabc4 134 *
d3313941
MD
135 * Transfer control to a VMSPACE. Control is returned after the specified
136 * number of microseconds or if a page fault, signal, trap, or system call
021a4ed4 137 * occurs. The context is updated as appropriate.
133aabc4
MD
138 */
139int
d3313941 140sys_vmspace_ctl(struct vmspace_ctl_args *uap)
133aabc4 141{
4a22e893 142 struct vkernel_common *vc;
d3313941 143 struct vmspace_entry *ve;
4a22e893
MD
144 struct vkernel *vk;
145 struct proc *p;
146 int framesz;
147 int error;
d3313941
MD
148
149 if ((vk = curproc->p_vkernel) == NULL)
150 return (EINVAL);
4a22e893
MD
151 vc = vk->vk_common;
152 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
d3313941 153 return (ENOENT);
4a22e893
MD
154
155 switch(uap->cmd) {
156 case VMSPACE_CTL_RUN:
157 /*
158 * Save the caller's register context, swap VM spaces, and
159 * install the passed register context. Return with
160 * EJUSTRETURN so the syscall code doesn't adjust the context.
161 */
162 p = curproc;
163 ++ve->refs;
164 framesz = sizeof(struct trapframe);
165 vk->vk_current = ve;
166 vk->vk_save_vmspace = p->p_vmspace;
167 vk->vk_user_frame = uap->ctx;
168 bcopy(uap->sysmsg_frame, &vk->vk_save_frame, framesz);
169 error = copyin(uap->ctx, uap->sysmsg_frame, framesz);
170 if (error == 0)
171 error = cpu_sanitize_frame(uap->sysmsg_frame);
172 if (error) {
173 bcopy(&vk->vk_save_frame, uap->sysmsg_frame, framesz);
174 vk->vk_current = NULL;
175 vk->vk_save_vmspace = NULL;
176 --ve->refs;
177 } else {
178 pmap_deactivate(p);
179 p->p_vmspace = ve->vmspace;
180 pmap_activate(p);
181 error = EJUSTRETURN;
182 }
183 break;
184 default:
185 error = EOPNOTSUPP;
186 break;
187 }
188 return(error);
133aabc4
MD
189}
190
191/*
d3313941 192 * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
133aabc4 193 *
d3313941
MD
194 * map memory within a VMSPACE. This function is just like a normal mmap()
195 * but operates on the vmspace's memory map. Most callers use this to create
196 * a MAP_VPAGETABLE mapping.
133aabc4
MD
197 */
198int
d3313941 199sys_vmspace_mmap(struct vmspace_mmap_args *uap)
133aabc4 200{
4a22e893 201 struct vkernel_common *vc;
d3313941 202 struct vmspace_entry *ve;
4a22e893 203 struct vkernel *vk;
021a4ed4 204 int error;
d3313941
MD
205
206 if ((vk = curproc->p_vkernel) == NULL)
207 return (EINVAL);
4a22e893
MD
208 vc = vk->vk_common;
209 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
d3313941 210 return (ENOENT);
021a4ed4
MD
211 error = kern_mmap(ve->vmspace, uap->addr, uap->len,
212 uap->prot, uap->flags,
213 uap->fd, uap->offset, &uap->sysmsg_resultp);
214 return (error);
133aabc4
MD
215}
216
217/*
d3313941 218 * vmspace_munmap(id, addr, len)
133aabc4 219 *
d3313941 220 * unmap memory within a VMSPACE.
133aabc4
MD
221 */
222int
d3313941 223sys_vmspace_munmap(struct vmspace_munmap_args *uap)
133aabc4 224{
4a22e893 225 struct vkernel_common *vc;
d3313941 226 struct vmspace_entry *ve;
4a22e893 227 struct vkernel *vk;
021a4ed4
MD
228 vm_offset_t addr;
229 vm_size_t size, pageoff;
230 vm_map_t map;
d3313941
MD
231
232 if ((vk = curproc->p_vkernel) == NULL)
233 return (EINVAL);
4a22e893
MD
234 vc = vk->vk_common;
235 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
d3313941 236 return (ENOENT);
021a4ed4
MD
237
238 /*
239 * Copied from sys_munmap()
240 */
241 addr = (vm_offset_t)uap->addr;
242 size = uap->len;
243
244 pageoff = (addr & PAGE_MASK);
245 addr -= pageoff;
246 size += pageoff;
247 size = (vm_size_t)round_page(size);
248 if (addr + size < addr)
249 return (EINVAL);
250 if (size == 0)
251 return (0);
252
88181b08 253 if (VM_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS)
021a4ed4 254 return (EINVAL);
88181b08 255 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
021a4ed4 256 return (EINVAL);
021a4ed4
MD
257 map = &ve->vmspace->vm_map;
258 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
259 return (EINVAL);
260 vm_map_remove(map, addr, addr + size);
261 return (0);
262}
263
264/*
265 * vmspace_pread(id, buf, nbyte, flags, offset)
266 *
267 * Read data from a vmspace. The number of bytes read is returned or
268 * -1 if an unrecoverable error occured. If the number of bytes read is
269 * less then the request size, a page fault occured in the VMSPACE which
270 * the caller must resolve in order to proceed.
271 */
272int
273sys_vmspace_pread(struct vmspace_pread_args *uap)
274{
4a22e893 275 struct vkernel_common *vc;
021a4ed4 276 struct vmspace_entry *ve;
4a22e893 277 struct vkernel *vk;
021a4ed4
MD
278
279 if ((vk = curproc->p_vkernel) == NULL)
280 return (EINVAL);
4a22e893
MD
281 vc = vk->vk_common;
282 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
021a4ed4
MD
283 return (ENOENT);
284 return (EINVAL);
285}
286
287/*
288 * vmspace_pwrite(id, buf, nbyte, flags, offset)
289 *
290 * Write data to a vmspace. The number of bytes written is returned or
291 * -1 if an unrecoverable error occured. If the number of bytes written is
292 * less then the request size, a page fault occured in the VMSPACE which
293 * the caller must resolve in order to proceed.
294 */
295int
296sys_vmspace_pwrite(struct vmspace_pwrite_args *uap)
297{
4a22e893 298 struct vkernel_common *vc;
021a4ed4 299 struct vmspace_entry *ve;
4a22e893 300 struct vkernel *vk;
021a4ed4
MD
301
302 if ((vk = curproc->p_vkernel) == NULL)
303 return (EINVAL);
4a22e893
MD
304 vc = vk->vk_common;
305 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
021a4ed4
MD
306 return (ENOENT);
307 return (EINVAL);
133aabc4
MD
308}
309
310/*
d3313941 311 * vmspace_mcontrol(id, addr, len, behav, value)
133aabc4 312 *
d3313941 313 * madvise/mcontrol support for a vmspace.
133aabc4
MD
314 */
315int
d3313941
MD
316sys_vmspace_mcontrol(struct vmspace_mcontrol_args *uap)
317{
4a22e893 318 struct vkernel_common *vc;
d3313941 319 struct vmspace_entry *ve;
4a22e893 320 struct vkernel *vk;
d3313941
MD
321 vm_offset_t start, end;
322
323 if ((vk = curproc->p_vkernel) == NULL)
324 return (EINVAL);
4a22e893
MD
325 vc = vk->vk_common;
326 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
d3313941
MD
327 return (ENOENT);
328
329 /*
330 * This code is basically copied from sys_mcontrol()
331 */
332 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
333 return (EINVAL);
334
88181b08
MD
335 if (VM_MAX_USER_ADDRESS > 0 &&
336 ((vm_offset_t) uap->addr + uap->len) > VM_MAX_USER_ADDRESS)
d3313941 337 return (EINVAL);
88181b08 338 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
d3313941 339 return (EINVAL);
d3313941
MD
340 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
341 return (EINVAL);
342
343 start = trunc_page((vm_offset_t) uap->addr);
344 end = round_page((vm_offset_t) uap->addr + uap->len);
345
346 return (vm_map_madvise(&ve->vmspace->vm_map, start, end,
347 uap->behav, uap->value));
348}
349
350/*
351 * Red black tree functions
352 */
353static int rb_vmspace_compare(struct vmspace_entry *, struct vmspace_entry *);
354RB_GENERATE(vmspace_rb_tree, vmspace_entry, rb_entry, rb_vmspace_compare);
355
356/* a->start is address, and the only field has to be initialized */
357static int
358rb_vmspace_compare(struct vmspace_entry *a, struct vmspace_entry *b)
359{
360 if ((char *)a->id < (char *)b->id)
361 return(-1);
362 else if ((char *)a->id > (char *)b->id)
363 return(1);
364 return(0);
365}
366
367static
368int
369rb_vmspace_delete(struct vmspace_entry *ve, void *data)
370{
4a22e893
MD
371 struct vkernel_common *vc = data;
372
373 KKASSERT(ve->refs == 0);
374 vmspace_entry_delete(ve, vc);
375 return(0);
376}
377
378/*
379 * Remove a vmspace_entry from the RB tree and destroy it. We have to clean
380 * up the pmap, the vm_map, then destroy the vmspace.
381 */
382static
383void
384vmspace_entry_delete(struct vmspace_entry *ve, struct vkernel_common *vc)
385{
386 RB_REMOVE(vmspace_rb_tree, &vc->vc_root, ve);
d3313941 387
4a22e893 388 pmap_remove_pages(vmspace_pmap(ve->vmspace),
88181b08 389 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
4a22e893 390 vm_map_remove(&ve->vmspace->vm_map,
88181b08 391 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
d3313941
MD
392 vmspace_free(ve->vmspace);
393 kfree(ve, M_VKERNEL);
d3313941
MD
394}
395
4a22e893 396
d3313941
MD
397static
398struct vmspace_entry *
4a22e893 399vkernel_find_vmspace(struct vkernel_common *vc, void *id)
d3313941
MD
400{
401 struct vmspace_entry *ve;
402 struct vmspace_entry key;
403
404 key.id = id;
4a22e893 405 ve = RB_FIND(vmspace_rb_tree, &vc->vc_root, &key);
d3313941
MD
406 return (ve);
407}
408
409/*
410 * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
411 * a vkernel process.
412 */
413void
4a22e893 414vkernel_inherit(struct proc *p1, struct proc *p2)
d3313941 415{
4a22e893
MD
416 struct vkernel_common *vc;
417 struct vkernel *vk;
418
419 vk = p1->p_vkernel;
420 vc = vk->vk_common;
421 KKASSERT(vc->vc_refs > 0);
422 atomic_add_int(&vc->vc_refs, 1);
423 vk = kmalloc(sizeof(*vk), M_VKERNEL, M_WAITOK|M_ZERO);
424 p2->p_vkernel = vk;
425 vk->vk_common = vc;
d3313941
MD
426}
427
428void
4a22e893 429vkernel_exit(struct proc *p)
133aabc4 430{
4a22e893
MD
431 struct vkernel_common *vc;
432 struct vmspace_entry *ve;
433 struct vkernel *vk;
434 int freeme = 0;
435
436 vk = p->p_vkernel;
437 p->p_vkernel = NULL;
438 vc = vk->vk_common;
439 vk->vk_common = NULL;
440
441 /*
442 * Restore the original VM context if we are killed while running
443 * a different one.
444 */
445 if ((ve = vk->vk_current) != NULL) {
086c1d7e 446 kprintf("killed with active VC\n");
4a22e893
MD
447 vk->vk_current = NULL;
448 pmap_deactivate(p);
449 p->p_vmspace = vk->vk_save_vmspace;
450 pmap_activate(p);
451 vk->vk_save_vmspace = NULL;
452 KKASSERT(ve->refs > 0);
453 --ve->refs;
d3313941 454 }
4a22e893
MD
455
456 /*
457 * Dereference the common area
458 */
459 KKASSERT(vc->vc_refs > 0);
460 spin_lock_wr(&vc->vc_spin);
461 if (--vc->vc_refs == 0)
462 freeme = 1;
463 spin_unlock_wr(&vc->vc_spin);
464
465 if (freeme) {
466 RB_SCAN(vmspace_rb_tree, &vc->vc_root, NULL,
467 rb_vmspace_delete, vc);
468 kfree(vc, M_VKERNEL);
469 }
470 kfree(vk, M_VKERNEL);
471}
472
473/*
474 * A VM space under virtual kernel control trapped out or made a system call
475 * or otherwise needs to return control to the virtual kernel context.
476 */
477int
478vkernel_trap(struct proc *p, struct trapframe *frame)
479{
480 struct vmspace_entry *ve;
481 struct vkernel *vk;
482 int error;
483
086c1d7e 484 kprintf("trap for vkernel type %d wm=%d\n",
4a22e893
MD
485 frame->tf_trapno & 0x7FFFFFFF,
486 ((frame->tf_trapno & 0x80000000) ? 1 : 0));
487
488 /*
489 * Which vmspace entry was running?
490 */
491 vk = p->p_vkernel;
492 ve = vk->vk_current;
493 vk->vk_current = NULL;
494 KKASSERT(ve != NULL);
495
496 /*
497 * Switch the process context back to the virtual kernel's VM space.
498 */
499 pmap_deactivate(p);
500 p->p_vmspace = vk->vk_save_vmspace;
501 pmap_activate(p);
502 vk->vk_save_vmspace = NULL;
503 KKASSERT(ve->refs > 0);
504 --ve->refs;
505
506 /*
507 * Copy the trapframe to the virtual kernel's userspace, then
508 * restore virtual kernel's original syscall trap frame so we
509 * can 'return' from the system call that ran the custom VM space.
510 */
511 error = copyout(frame, vk->vk_user_frame, sizeof(*frame));
512 bcopy(&vk->vk_save_frame, frame, sizeof(*frame));
513 return(error);
133aabc4
MD
514}
515