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