nv_kern_netbsd.c revision 1.4 1 /* $NetBSD: nv_kern_netbsd.c,v 1.4 2018/09/23 19:07:10 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: nv_kern_netbsd.c,v 1.4 2018/09/23 19:07:10 rmind Exp $");
34
35 #if !defined(_KERNEL) && !defined(_STANDALONE)
36 #include <sys/mman.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #endif
42 #ifdef _KERNEL
43 #include <sys/param.h>
44 #include <sys/lwp.h>
45 #include <sys/kmem.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <uvm/uvm_extern.h>
49 #endif
50 #ifdef _STANDALONE
51 /* XXX */
52 extern void *alloc(unsigned int);
53 extern void dealloc(void *, unsigned int);
54 // #include "stand.h"
55 #else
56 #include <sys/ioctl.h>
57 #endif
58 #include "nv.h"
59 #include "nv_impl.h"
60
61 #ifndef _STANDALONE
62 #ifdef _KERNEL
63
64 void
65 nv_free(void *buf)
66 {
67 if (!buf) {
68 return;
69 }
70 free(buf, M_NVLIST);
71 }
72
73 int
74 nvlist_copyin(const nvlist_ref_t *nref, nvlist_t **nvlp, size_t lim)
75 {
76 const size_t len = nref->len;
77 nvlist_t *nvl;
78 void *buf;
79 int error;
80
81 if (len >= lim) {
82 return E2BIG;
83 }
84 buf = kmem_alloc(len, KM_SLEEP);
85 error = copyin(nref->buf, buf, len);
86 if (error) {
87 kmem_free(buf, len);
88 return error;
89 }
90 nvl = nvlist_unpack(buf, len, nref->flags);
91 kmem_free(buf, len);
92 if (nvl == NULL) {
93 return EINVAL;
94 }
95 *nvlp = nvl;
96 return 0;
97 }
98
99 int
100 nvlist_copyout(nvlist_ref_t *nref, const nvlist_t *nvl)
101 {
102 struct proc *p = curproc;
103 void *buf, *uaddr;
104 size_t len, rlen;
105 int error;
106
107 buf = nvlist_pack(nvl, &len);
108 if (buf == NULL) {
109 return ENOMEM;
110 }
111
112 /*
113 * Map the user page(s).
114 *
115 * Note: nvlist_recv_ioctl() will unmap it.
116 */
117 uaddr = NULL;
118 rlen = round_page(len);
119 error = uvm_mmap_anon(p, &uaddr, rlen);
120 if (error) {
121 goto err;
122 }
123 error = copyout(buf, uaddr, len);
124 if (error) {
125 uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)uaddr,
126 (vaddr_t)uaddr + len);
127 goto err;
128 }
129 nref->flags = nvlist_error(nvl);
130 nref->buf = uaddr;
131 nref->len = len;
132 err:
133 free(buf, M_TEMP);
134 return error;
135 }
136
137 #else
138
139 int
140 nvlist_xfer_ioctl(int fd, unsigned long cmd, const nvlist_t *nvl,
141 nvlist_t **nvlp)
142 {
143 nvlist_ref_t nref;
144 void *buf = NULL;
145
146 memset(&nref, 0, sizeof(nvlist_ref_t));
147
148 if (nvl) {
149 /*
150 * Sending: serialize the name-value list.
151 */
152 buf = nvlist_pack(nvl, &nref.len);
153 if (buf == NULL) {
154 errno = ENOMEM;
155 return -1;
156 }
157 nref.buf = buf;
158 nref.flags = nvlist_flags(nvl);
159 }
160
161 /*
162 * Exchange the nvlist reference data.
163 */
164 if (ioctl(fd, cmd, &nref) == -1) {
165 free(buf);
166 return -1;
167 }
168 free(buf);
169
170 if (nvlp) {
171 nvlist_t *retnvl;
172
173 /*
174 * Receiving: unserialize the nvlist.
175 *
176 * Note: pages are mapped by nvlist_kern_copyout() for us.
177 */
178 if (nref.buf == NULL || nref.len == 0) {
179 errno = EIO;
180 return -1;
181 }
182 retnvl = nvlist_unpack(nref.buf, nref.len, nref.flags);
183 munmap(nref.buf, nref.len);
184 if (retnvl == NULL) {
185 errno = EIO;
186 return -1;
187 }
188 *nvlp = retnvl;
189 }
190 return 0;
191 }
192
193 int
194 nvlist_send_ioctl(int fd, unsigned long cmd, const nvlist_t *nvl)
195 {
196 return nvlist_xfer_ioctl(fd, cmd, nvl, NULL);
197 }
198
199 int
200 nvlist_recv_ioctl(int fd, unsigned long cmd, nvlist_t **nvlp)
201 {
202 return nvlist_xfer_ioctl(fd, cmd, NULL, nvlp);
203 }
204 #endif
205 #endif
206
207 void *
208 nv_calloc(size_t n, size_t s)
209 {
210 const size_t len = n * s;
211 void *buf = nv_malloc(len);
212 if (buf == NULL)
213 return NULL;
214 memset(buf, 0, len);
215 return buf;
216 }
217
218 char *
219 nv_strdup(const char *s1)
220 {
221 size_t len = strlen(s1) + 1;
222 char *s2;
223
224 s2 = nv_malloc(len);
225 if (s2) {
226 memcpy(s2, s1, len);
227 s2[len] = '\0';
228 }
229 return s2;
230 }
231
232 #ifdef _STANDALONE
233
234 void *
235 nv_malloc(size_t len)
236 {
237 return alloc(len);
238 }
239
240 void
241 nv_free(void *buf)
242 {
243 if (buf == NULL)
244 return;
245 unsigned int *olen = (void *)((char *)buf - sizeof(unsigned int));
246 dealloc(buf, *olen);
247 }
248
249 void *
250 nv_realloc(void *buf, size_t len)
251 {
252 if (buf == NULL)
253 return alloc(len);
254
255 unsigned int *olen = (void *)((char *)buf - sizeof(unsigned int));
256 if (*olen < len)
257 return buf;
258
259 void *nbuf = alloc(len);
260 memcpy(nbuf, buf, *olen);
261 dealloc(buf, *olen);
262 return nbuf;
263 }
264 #endif
265