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