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