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