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