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