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