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