1 /*-
2 * Copyright (c) 2015 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/mman.h>
31 #include <sys/sysproto.h>
32
33 #include <compat/cloudabi/cloudabi_proto.h>
34 #include <compat/cloudabi/cloudabi_syscalldefs.h>
35
36 /* Converts CloudABI's memory protection flags to FreeBSD's. */
37 static int
convert_mprot(cloudabi_mprot_t in)38 convert_mprot(cloudabi_mprot_t in)
39 {
40 int out;
41
42 out = 0;
43 if (in & CLOUDABI_PROT_EXEC)
44 out |= PROT_EXEC;
45 if (in & CLOUDABI_PROT_WRITE)
46 out |= PROT_WRITE;
47 if (in & CLOUDABI_PROT_READ)
48 out |= PROT_READ;
49 return (out);
50 }
51
52 int
cloudabi_sys_mem_advise(struct thread * td,struct cloudabi_sys_mem_advise_args * uap)53 cloudabi_sys_mem_advise(struct thread *td,
54 struct cloudabi_sys_mem_advise_args *uap)
55 {
56 struct madvise_args madvise_args = {
57 .addr = uap->addr,
58 .len = uap->len
59 };
60
61 switch (uap->advice) {
62 case CLOUDABI_ADVICE_DONTNEED:
63 madvise_args.behav = MADV_DONTNEED;
64 break;
65 case CLOUDABI_ADVICE_NORMAL:
66 madvise_args.behav = MADV_NORMAL;
67 break;
68 case CLOUDABI_ADVICE_RANDOM:
69 madvise_args.behav = MADV_RANDOM;
70 break;
71 case CLOUDABI_ADVICE_SEQUENTIAL:
72 madvise_args.behav = MADV_SEQUENTIAL;
73 break;
74 case CLOUDABI_ADVICE_WILLNEED:
75 madvise_args.behav = MADV_WILLNEED;
76 break;
77 default:
78 return (EINVAL);
79 }
80
81 return (sys_madvise(td, &madvise_args));
82 }
83
84 int
cloudabi_sys_mem_lock(struct thread * td,struct cloudabi_sys_mem_lock_args * uap)85 cloudabi_sys_mem_lock(struct thread *td, struct cloudabi_sys_mem_lock_args *uap)
86 {
87 struct mlock_args mlock_args = {
88 .addr = uap->addr,
89 .len = uap->len
90 };
91
92 return (sys_mlock(td, &mlock_args));
93 }
94
95 int
cloudabi_sys_mem_map(struct thread * td,struct cloudabi_sys_mem_map_args * uap)96 cloudabi_sys_mem_map(struct thread *td, struct cloudabi_sys_mem_map_args *uap)
97 {
98 struct mmap_args mmap_args = {
99 .addr = uap->addr,
100 .len = uap->len,
101 .prot = convert_mprot(uap->prot),
102 .fd = uap->fd,
103 .pos = uap->off
104 };
105
106 /* Translate flags. */
107 if (uap->flags & CLOUDABI_MAP_ANON)
108 mmap_args.flags |= MAP_ANON;
109 if (uap->flags & CLOUDABI_MAP_FIXED)
110 mmap_args.flags |= MAP_FIXED;
111 if (uap->flags & CLOUDABI_MAP_PRIVATE)
112 mmap_args.flags |= MAP_PRIVATE;
113 if (uap->flags & CLOUDABI_MAP_SHARED)
114 mmap_args.flags |= MAP_SHARED;
115
116 return (sys_mmap(td, &mmap_args));
117 }
118
119 int
cloudabi_sys_mem_protect(struct thread * td,struct cloudabi_sys_mem_protect_args * uap)120 cloudabi_sys_mem_protect(struct thread *td,
121 struct cloudabi_sys_mem_protect_args *uap)
122 {
123 struct mprotect_args mprotect_args = {
124 .addr = uap->addr,
125 .len = uap->len,
126 .prot = convert_mprot(uap->prot),
127 };
128
129 return (sys_mprotect(td, &mprotect_args));
130 }
131
132 int
cloudabi_sys_mem_sync(struct thread * td,struct cloudabi_sys_mem_sync_args * uap)133 cloudabi_sys_mem_sync(struct thread *td, struct cloudabi_sys_mem_sync_args *uap)
134 {
135 struct msync_args msync_args = {
136 .addr = uap->addr,
137 .len = uap->len,
138 };
139
140 /* Convert flags. */
141 switch (uap->flags & (CLOUDABI_MS_ASYNC | CLOUDABI_MS_SYNC)) {
142 case CLOUDABI_MS_ASYNC:
143 msync_args.flags |= MS_ASYNC;
144 break;
145 case CLOUDABI_MS_SYNC:
146 msync_args.flags |= MS_SYNC;
147 break;
148 default:
149 return (EINVAL);
150 }
151 if ((uap->flags & CLOUDABI_MS_INVALIDATE) != 0)
152 msync_args.flags |= MS_INVALIDATE;
153
154 return (sys_msync(td, &msync_args));
155 }
156
157 int
cloudabi_sys_mem_unlock(struct thread * td,struct cloudabi_sys_mem_unlock_args * uap)158 cloudabi_sys_mem_unlock(struct thread *td,
159 struct cloudabi_sys_mem_unlock_args *uap)
160 {
161 struct munlock_args munlock_args = {
162 .addr = uap->addr,
163 .len = uap->len
164 };
165
166 return (sys_munlock(td, &munlock_args));
167 }
168
169 int
cloudabi_sys_mem_unmap(struct thread * td,struct cloudabi_sys_mem_unmap_args * uap)170 cloudabi_sys_mem_unmap(struct thread *td,
171 struct cloudabi_sys_mem_unmap_args *uap)
172 {
173 struct munmap_args munmap_args = {
174 .addr = uap->addr,
175 .len = uap->len
176 };
177
178 return (sys_munmap(td, &munmap_args));
179 }
180