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: stable/11/sys/compat/cloudabi/cloudabi_mem.c 324250 2017-10-04 07:35:01Z ed $");
28
29#include <sys/param.h>
30#include <sys/mman.h>
31#include <sys/proc.h>
32#include <sys/syscallsubr.h>
33
34#include <contrib/cloudabi/cloudabi_types_common.h>
35
36#include <compat/cloudabi/cloudabi_proto.h>
37
38/* Converts CloudABI's memory protection flags to FreeBSD's. */
39static int
40convert_mprot(cloudabi_mprot_t in, int *out)
41{
42
43	/* Unknown protection flags. */
44	if ((in & ~(CLOUDABI_PROT_EXEC | CLOUDABI_PROT_WRITE |
45	    CLOUDABI_PROT_READ)) != 0)
46		return (ENOTSUP);
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))
50		return (ENOTSUP);
51
52	*out = 0;
53	if (in & CLOUDABI_PROT_EXEC)
54		*out |= PROT_EXEC;
55	if (in & CLOUDABI_PROT_WRITE)
56		*out |= PROT_WRITE;
57	if (in & CLOUDABI_PROT_READ)
58		*out |= PROT_READ;
59	return (0);
60}
61
62int
63cloudabi_sys_mem_advise(struct thread *td,
64    struct cloudabi_sys_mem_advise_args *uap)
65{
66	int behav;
67
68	switch (uap->advice) {
69	case CLOUDABI_ADVICE_DONTNEED:
70		behav = MADV_DONTNEED;
71		break;
72	case CLOUDABI_ADVICE_NORMAL:
73		behav = MADV_NORMAL;
74		break;
75	case CLOUDABI_ADVICE_RANDOM:
76		behav = MADV_RANDOM;
77		break;
78	case CLOUDABI_ADVICE_SEQUENTIAL:
79		behav = MADV_SEQUENTIAL;
80		break;
81	case CLOUDABI_ADVICE_WILLNEED:
82		behav = MADV_WILLNEED;
83		break;
84	default:
85		return (EINVAL);
86	}
87
88	return (kern_madvise(td, (uintptr_t)uap->mapping, uap->mapping_len,
89	    behav));
90}
91
92int
93cloudabi_sys_mem_map(struct thread *td, struct cloudabi_sys_mem_map_args *uap)
94{
95	int error, flags, prot;
96
97	/* Translate flags. */
98	flags = 0;
99	if (uap->flags & CLOUDABI_MAP_ANON)
100		flags |= MAP_ANON;
101	if (uap->flags & CLOUDABI_MAP_FIXED)
102		flags |= MAP_FIXED;
103	if (uap->flags & CLOUDABI_MAP_PRIVATE)
104		flags |= MAP_PRIVATE;
105	if (uap->flags & CLOUDABI_MAP_SHARED)
106		flags |= MAP_SHARED;
107
108	/* Translate protection. */
109	error = convert_mprot(uap->prot, &prot);
110	if (error != 0)
111		return (error);
112
113	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
114	    uap->fd, uap->off));
115}
116
117int
118cloudabi_sys_mem_protect(struct thread *td,
119    struct cloudabi_sys_mem_protect_args *uap)
120{
121	int error, prot;
122
123	/* Translate protection. */
124	error = convert_mprot(uap->prot, &prot);
125	if (error != 0)
126		return (error);
127
128	return (kern_mprotect(td, (uintptr_t)uap->mapping, uap->mapping_len,
129	    prot));
130}
131
132int
133cloudabi_sys_mem_sync(struct thread *td, struct cloudabi_sys_mem_sync_args *uap)
134{
135	int flags;
136
137	/* Convert flags. */
138	switch (uap->flags & (CLOUDABI_MS_ASYNC | CLOUDABI_MS_SYNC)) {
139	case CLOUDABI_MS_ASYNC:
140		flags = MS_ASYNC;
141		break;
142	case CLOUDABI_MS_SYNC:
143		flags = MS_SYNC;
144		break;
145	default:
146		return (EINVAL);
147	}
148	if ((uap->flags & CLOUDABI_MS_INVALIDATE) != 0)
149		flags |= MS_INVALIDATE;
150
151	return (kern_msync(td, (uintptr_t)uap->mapping, uap->mapping_len,
152	    flags));
153}
154
155int
156cloudabi_sys_mem_unmap(struct thread *td,
157    struct cloudabi_sys_mem_unmap_args *uap)
158{
159
160	return (kern_munmap(td, (uintptr_t)uap->mapping, uap->mapping_len));
161}
162