]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/cloudabi/cloudabi_vdso.c
file: update to 5.34
[FreeBSD/FreeBSD.git] / sys / compat / cloudabi / cloudabi_vdso.c
1 /*-
2  * Copyright (c) 2016 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/types.h>
30 #include <sys/lock.h>
31 #include <sys/sysent.h>
32 #include <sys/rwlock.h>
33
34 #include <vm/vm.h>
35 #include <vm/pmap.h>
36 #include <vm/vm_extern.h>
37 #include <vm/vm_object.h>
38 #include <vm/vm_page.h>
39 #include <vm/vm_pager.h>
40
41 #include <compat/cloudabi/cloudabi_util.h>
42
43 void
44 cloudabi_vdso_init(struct sysentvec *sv, char *begin, char *end)
45 {
46         vm_page_t m;
47         vm_object_t obj;
48         vm_offset_t addr;
49         size_t i, pages, pages_length, vdso_length;
50
51         /* Determine the number of pages needed to store the vDSO. */
52         vdso_length = end - begin;
53         pages = howmany(vdso_length, PAGE_SIZE);
54         pages_length = pages * PAGE_SIZE;
55
56         /* Allocate a VM object and fill it with the vDSO. */
57         obj = vm_pager_allocate(OBJT_PHYS, 0, pages_length,
58             VM_PROT_DEFAULT, 0, NULL);
59         addr = kva_alloc(PAGE_SIZE);
60         for (i = 0; i < pages; ++i) {
61                 VM_OBJECT_WLOCK(obj);
62                 m = vm_page_grab(obj, i, VM_ALLOC_NOBUSY | VM_ALLOC_ZERO);
63                 m->valid = VM_PAGE_BITS_ALL;
64                 VM_OBJECT_WUNLOCK(obj);
65
66                 pmap_qenter(addr, &m, 1);
67                 memcpy((void *)addr, begin + i * PAGE_SIZE,
68                     MIN(vdso_length - i * PAGE_SIZE, PAGE_SIZE));
69                 pmap_qremove(addr, 1);
70         }
71         kva_free(addr, PAGE_SIZE);
72
73         /*
74          * Place the vDSO at the top of the address space. The user
75          * stack can start right below it.
76          */
77         sv->sv_shared_page_base = sv->sv_maxuser - pages_length;
78         sv->sv_shared_page_len = pages_length;
79         sv->sv_shared_page_obj = obj;
80         sv->sv_usrstack = sv->sv_shared_page_base;
81 }
82
83 void
84 cloudabi_vdso_destroy(struct sysentvec *sv)
85 {
86
87         vm_object_deallocate(sv->sv_shared_page_obj);
88 }