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