2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
32 #include <sys/syscallsubr.h>
34 #include <contrib/cloudabi/cloudabi_types_common.h>
36 #include <compat/cloudabi/cloudabi_proto.h>
38 /* Converts CloudABI's memory protection flags to FreeBSD's. */
40 convert_mprot(cloudabi_mprot_t in, int *out)
43 /* Unknown protection flags. */
44 if ((in & ~(CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE |
45 CLOUDABI_PROT_READ)) != 0)
47 /* W^X: Write and exec cannot be enabled at the same time. */
48 if ((in & (CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE)) ==
49 (CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE))
53 if (in & CLOUDABI_PROT_EXEC)
55 if (in & CLOUDABI_PROT_WRITE)
57 if (in & CLOUDABI_PROT_READ)
63 cloudabi_sys_mem_advise(struct thread *td,
64 struct cloudabi_sys_mem_advise_args *uap)
68 switch (uap->advice) {
69 case CLOUDABI_ADVICE_DONTNEED:
70 behav = MADV_DONTNEED;
72 case CLOUDABI_ADVICE_NORMAL:
75 case CLOUDABI_ADVICE_RANDOM:
78 case CLOUDABI_ADVICE_SEQUENTIAL:
79 behav = MADV_SEQUENTIAL;
81 case CLOUDABI_ADVICE_WILLNEED:
82 behav = MADV_WILLNEED;
88 return (kern_madvise(td, (uintptr_t)uap->mapping, uap->mapping_len,
93 cloudabi_sys_mem_map(struct thread *td, struct cloudabi_sys_mem_map_args *uap)
95 int error, flags, prot;
97 /* Translate flags. */
99 if (uap->flags & CLOUDABI_MAP_ANON)
101 if (uap->flags & CLOUDABI_MAP_FIXED)
103 if (uap->flags & CLOUDABI_MAP_PRIVATE)
104 flags |= MAP_PRIVATE;
105 if (uap->flags & CLOUDABI_MAP_SHARED)
108 /* Translate protection. */
109 error = convert_mprot(uap->prot, &prot);
113 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
118 cloudabi_sys_mem_protect(struct thread *td,
119 struct cloudabi_sys_mem_protect_args *uap)
123 /* Translate protection. */
124 error = convert_mprot(uap->prot, &prot);
128 return (kern_mprotect(td, (uintptr_t)uap->mapping, uap->mapping_len,
133 cloudabi_sys_mem_sync(struct thread *td, struct cloudabi_sys_mem_sync_args *uap)
138 switch (uap->flags & (CLOUDABI_MS_ASYNC | CLOUDABI_MS_SYNC)) {
139 case CLOUDABI_MS_ASYNC:
142 case CLOUDABI_MS_SYNC:
148 if ((uap->flags & CLOUDABI_MS_INVALIDATE) != 0)
149 flags |= MS_INVALIDATE;
151 return (kern_msync(td, (uintptr_t)uap->mapping, uap->mapping_len,
156 cloudabi_sys_mem_unmap(struct thread *td,
157 struct cloudabi_sys_mem_unmap_args *uap)
160 return (kern_munmap(td, (uintptr_t)uap->mapping, uap->mapping_len));