nv_kern_netbsd.c revision 1.3 1 /* $NetBSD: nv_kern_netbsd.c,v 1.3 2018/09/22 17:13:30 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.3 2018/09/22 17:13:30 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 int
65 nvlist_copyin(const nvlist_ref_t *nref, nvlist_t **nvlp, size_t lim)
66 {
67 const size_t len = nref->len;
68 nvlist_t *nvl;
69 void *buf;
70 int error;
71
72 if (len >= lim) {
73 return E2BIG;
74 }
75 buf = kmem_alloc(len, KM_SLEEP);
76 error = copyin(nref->buf, buf, len);
77 if (error) {
78 kmem_free(buf, len);
79 return error;
80 }
81 nvl = nvlist_unpack(buf, len, nref->flags);
82 kmem_free(buf, len);
83 if (nvl == NULL) {
84 return EINVAL;
85 }
86 *nvlp = nvl;
87 return 0;
88 }
89
90 int
91 nvlist_copyout(nvlist_ref_t *nref, const nvlist_t *nvl)
92 {
93 struct proc *p = curproc;
94 void *buf, *uaddr;
95 size_t len, rlen;
96 int error;
97
98 buf = nvlist_pack(nvl, &len);
99 if (buf == NULL) {
100 return ENOMEM;
101 }
102
103 /*
104 * Map the user page(s).
105 *
106 * Note: nvlist_recv_ioctl() will unmap it.
107 */
108 uaddr = NULL;
109 rlen = round_page(len);
110 error = uvm_mmap_anon(p, &uaddr, rlen);
111 if (error) {
112 goto err;
113 }
114 error = copyout(buf, uaddr, len);
115 if (error) {
116 uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)uaddr,
117 (vaddr_t)uaddr + len);
118 goto err;
119 }
120 nref->flags = nvlist_error(nvl);
121 nref->buf = uaddr;
122 nref->len = len;
123 err:
124 free(buf, M_TEMP);
125 return error;
126 }
127
128 #else
129
130 int
131 nvlist_xfer_ioctl(int fd, unsigned long cmd, const nvlist_t *nvl,
132 nvlist_t **nvlp)
133 {
134 nvlist_ref_t nref;
135 void *buf = NULL;
136
137 memset(&nref, 0, sizeof(nvlist_ref_t));
138
139 if (nvl) {
140 /*
141 * Sending: serialize the name-value list.
142 */
143 buf = nvlist_pack(nvl, &nref.len);
144 if (buf == NULL) {
145 errno = ENOMEM;
146 return -1;
147 }
148 nref.buf = buf;
149 nref.flags = nvlist_flags(nvl);
150 }
151
152 /*
153 * Exchange the nvlist reference data.
154 */
155 if (ioctl(fd, cmd, &nref) == -1) {
156 free(buf);
157 return -1;
158 }
159 free(buf);
160
161 if (nvlp) {
162 nvlist_t *retnvl;
163
164 /*
165 * Receiving: unserialize the nvlist.
166 *
167 * Note: pages are mapped by nvlist_kern_copyout() for us.
168 */
169 if (nref.buf == NULL || nref.len == 0) {
170 errno = EIO;
171 return -1;
172 }
173 retnvl = nvlist_unpack(nref.buf, nref.len, nref.flags);
174 munmap(nref.buf, nref.len);
175 if (retnvl == NULL) {
176 errno = EIO;
177 return -1;
178 }
179 *nvlp = retnvl;
180 }
181 return 0;
182 }
183
184 int
185 nvlist_send_ioctl(int fd, unsigned long cmd, const nvlist_t *nvl)
186 {
187 return nvlist_xfer_ioctl(fd, cmd, nvl, NULL);
188 }
189
190 int
191 nvlist_recv_ioctl(int fd, unsigned long cmd, nvlist_t **nvlp)
192 {
193 return nvlist_xfer_ioctl(fd, cmd, NULL, nvlp);
194 }
195 #endif
196 #endif
197
198 void *
199 nv_calloc(size_t n, size_t s)
200 {
201 const size_t len = n * s;
202 void *buf = nv_malloc(len);
203 if (buf == NULL)
204 return NULL;
205 memset(buf, 0, len);
206 return buf;
207 }
208
209 char *
210 nv_strdup(const char *s1)
211 {
212 size_t len = strlen(s1) + 1;
213 char *s2;
214
215 s2 = nv_malloc(len);
216 if (s2) {
217 memcpy(s2, s1, len);
218 s2[len] = '\0';
219 }
220 return s2;
221 }
222
223 #ifdef _STANDALONE
224
225 void *
226 nv_malloc(size_t len)
227 {
228 return alloc(len);
229 }
230
231 void
232 nv_free(void *buf)
233 {
234 if (buf == NULL)
235 return;
236 unsigned int *olen = (void *)((char *)buf - sizeof(unsigned int));
237 dealloc(buf, *olen);
238 }
239
240 void *
241 nv_realloc(void *buf, size_t len)
242 {
243 if (buf == NULL)
244 return alloc(len);
245
246 unsigned int *olen = (void *)((char *)buf - sizeof(unsigned int));
247 if (*olen < len)
248 return buf;
249
250 void *nbuf = alloc(len);
251 memcpy(nbuf, buf, *olen);
252 dealloc(buf, *olen);
253 return nbuf;
254 }
255 #endif
256