1 /* $NetBSD: copy.c,v 1.5 2011/08/27 17:57:14 reinoud Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: copy.c,v 1.5 2011/08/27 17:57:14 reinoud Exp $"); 31 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 35 /* XXX until strnlen(3) has been added to the kernel, we *could* panic on it */ 36 #define strnlen(str, maxlen) min(strlen((str)), maxlen) 37 38 int 39 copyin(const void *uaddr, void *kaddr, size_t len) 40 { 41 aprint_debug("copyin uaddr %p, kaddr %p, len %d\n", uaddr, kaddr, (int) len); 42 memcpy(kaddr, uaddr, len); 43 return 0; 44 } 45 46 int 47 copyout(const void *kaddr, void *uaddr, size_t len) 48 { 49 aprint_debug("copyout kaddr %p, uaddr %p, len %d\n", kaddr, uaddr, (int) len); 50 memcpy(uaddr, kaddr, len); 51 return 0; 52 } 53 54 int 55 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) 56 { 57 len = min(strnlen(uaddr, len), len) + 1; 58 strncpy(kaddr, uaddr, len); 59 if (done) 60 *done = len; 61 return 0; 62 } 63 64 int 65 copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) 66 { 67 len = min(strnlen(kaddr, len), len) + 1; 68 strncpy(uaddr, kaddr, len); 69 if (done) 70 *done = len; 71 return 0; 72 } 73 74 int 75 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) 76 { 77 len = min(strnlen(kfaddr, len), len) + 1; 78 strncpy(kdaddr, kfaddr, len); 79 if (done) 80 *done = len; 81 return 0; 82 } 83 84 int 85 kcopy(const void *src, void *dst, size_t len) 86 { 87 memcpy(dst, src, len); 88 #ifdef DEBUG 89 if (memcmp(dst, src, len) != 0) 90 panic("kcopy not finished correctly\n"); 91 #endif 92 return 0; 93 } 94 95 int 96 fuswintr(const void *base) 97 { 98 return *(const short *)base; 99 } 100 101 int 102 suswintr(void *base, short c) 103 { 104 *(short *)base = c; 105 return 0; 106 } 107 108 int 109 subyte(void *base, int c) 110 { 111 *(char *)base = c; 112 return 0; 113 } 114 115 int 116 suword(void *base, long c) 117 { 118 *(long *)base = c; 119 return 0; 120 } 121