]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/cloudabi/cloudabi_vdso.c
Initial import from vendor-sys branch of openzfs
[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/param.h>
30 #include <sys/systm.h>
31 #include <sys/lock.h>
32 #include <sys/sysent.h>
33 #include <sys/rwlock.h>
34
35 #include <vm/vm.h>
36 #include <vm/pmap.h>
37 #include <vm/vm_extern.h>
38 #include <vm/vm_object.h>
39 #include <vm/vm_page.h>
40 #include <vm/vm_pager.h>
41
42 #include <compat/cloudabi/cloudabi_util.h>
43
44 void
45 cloudabi_vdso_init(struct sysentvec *sv, char *begin, char *end)
46 {
47         vm_page_t m;
48         vm_object_t obj;
49         vm_offset_t addr;
50         size_t i, pages, pages_length, vdso_length;
51
52         /* Determine the number of pages needed to store the vDSO. */
53         vdso_length = end - begin;
54         pages = howmany(vdso_length, PAGE_SIZE);
55         pages_length = pages * PAGE_SIZE;
56
57         /* Allocate a VM object and fill it with the vDSO. */
58         obj = vm_pager_allocate(OBJT_PHYS, 0, pages_length,
59             VM_PROT_DEFAULT, 0, NULL);
60         addr = kva_alloc(PAGE_SIZE);
61         for (i = 0; i < pages; ++i) {
62                 VM_OBJECT_WLOCK(obj);
63                 m = vm_page_grab(obj, i, VM_ALLOC_ZERO);
64                 VM_OBJECT_WUNLOCK(obj);
65                 vm_page_valid(m);
66                 vm_page_xunbusy(m);
67
68                 pmap_qenter(addr, &m, 1);
69                 memcpy((void *)addr, begin + i * PAGE_SIZE,
70                     MIN(vdso_length - i * PAGE_SIZE, PAGE_SIZE));
71                 pmap_qremove(addr, 1);
72         }
73         kva_free(addr, PAGE_SIZE);
74
75         /*
76          * Place the vDSO at the top of the address space. The user
77          * stack can start right below it.
78          */
79         sv->sv_shared_page_base = sv->sv_maxuser - pages_length;
80         sv->sv_shared_page_len = pages_length;
81         sv->sv_shared_page_obj = obj;
82         sv->sv_usrstack = sv->sv_shared_page_base;
83 }
84
85 void
86 cloudabi_vdso_destroy(struct sysentvec *sv)
87 {
88
89         vm_object_deallocate(sv->sv_shared_page_obj);
90 }