1*ca08b3e7Smaxv /* $NetBSD: copy.c,v 1.12 2020/06/30 16:20:02 maxv Exp $ */
238365ca3Sjmcneill
338365ca3Sjmcneill /*-
438365ca3Sjmcneill * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
538365ca3Sjmcneill * All rights reserved.
638365ca3Sjmcneill *
738365ca3Sjmcneill * Redistribution and use in source and binary forms, with or without
838365ca3Sjmcneill * modification, are permitted provided that the following conditions
938365ca3Sjmcneill * are met:
1038365ca3Sjmcneill * 1. Redistributions of source code must retain the above copyright
1138365ca3Sjmcneill * notice, this list of conditions and the following disclaimer.
1238365ca3Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1338365ca3Sjmcneill * notice, this list of conditions and the following disclaimer in the
1438365ca3Sjmcneill * documentation and/or other materials provided with the distribution.
1538365ca3Sjmcneill *
1638365ca3Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1738365ca3Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1838365ca3Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1938365ca3Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2038365ca3Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2138365ca3Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2238365ca3Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2338365ca3Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2438365ca3Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2538365ca3Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2638365ca3Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
2738365ca3Sjmcneill */
2838365ca3Sjmcneill
2938365ca3Sjmcneill #include <sys/cdefs.h>
30*ca08b3e7Smaxv __KERNEL_RCSID(0, "$NetBSD: copy.c,v 1.12 2020/06/30 16:20:02 maxv Exp $");
3191bfaeb6Sthorpej
3291bfaeb6Sthorpej #define __UFETCHSTORE_PRIVATE
3391bfaeb6Sthorpej #define __UCAS_PRIVATE
3438365ca3Sjmcneill
3538365ca3Sjmcneill #include <sys/types.h>
3638365ca3Sjmcneill #include <sys/systm.h>
37cd980be6Sreinoud #include <machine/thunk.h>
3883485a13Sreinoud
3983485a13Sreinoud /* XXX until strnlen(3) has been added to the kernel, we *could* panic on it */
40d1579b2dSriastradh #define strnlen(str, maxlen) uimin(strlen((str)), maxlen)
4138365ca3Sjmcneill
4238365ca3Sjmcneill int
copyin(const void * uaddr,void * kaddr,size_t len)4338365ca3Sjmcneill copyin(const void *uaddr, void *kaddr, size_t len)
4438365ca3Sjmcneill {
45cd980be6Sreinoud // thunk_printf("copyin uaddr %p, kaddr %p, len %d\n", uaddr, kaddr, (int) len);
4638365ca3Sjmcneill memcpy(kaddr, uaddr, len);
4738365ca3Sjmcneill return 0;
4838365ca3Sjmcneill }
4938365ca3Sjmcneill
5038365ca3Sjmcneill int
copyout(const void * kaddr,void * uaddr,size_t len)5138365ca3Sjmcneill copyout(const void *kaddr, void *uaddr, size_t len)
5238365ca3Sjmcneill {
53cd980be6Sreinoud // thunk_printf("copyout kaddr %p, uaddr %p, len %d\n", kaddr, uaddr, (int) len);
5438365ca3Sjmcneill memcpy(uaddr, kaddr, len);
5538365ca3Sjmcneill return 0;
5638365ca3Sjmcneill }
5738365ca3Sjmcneill
5838365ca3Sjmcneill int
copyinstr(const void * uaddr,void * kaddr,size_t len,size_t * done)5938365ca3Sjmcneill copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
6038365ca3Sjmcneill {
61d1579b2dSriastradh len = uimin(strnlen(uaddr, len), len) + 1;
6238365ca3Sjmcneill strncpy(kaddr, uaddr, len);
6338365ca3Sjmcneill if (done)
6483485a13Sreinoud *done = len;
6538365ca3Sjmcneill return 0;
6638365ca3Sjmcneill }
6738365ca3Sjmcneill
6838365ca3Sjmcneill int
copyoutstr(const void * kaddr,void * uaddr,size_t len,size_t * done)6938365ca3Sjmcneill copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
7038365ca3Sjmcneill {
71d1579b2dSriastradh len = uimin(strnlen(kaddr, len), len) + 1;
7238365ca3Sjmcneill strncpy(uaddr, kaddr, len);
7338365ca3Sjmcneill if (done)
7483485a13Sreinoud *done = len;
7538365ca3Sjmcneill return 0;
7638365ca3Sjmcneill }
7738365ca3Sjmcneill
7838365ca3Sjmcneill int
kcopy(const void * src,void * dst,size_t len)7938365ca3Sjmcneill kcopy(const void *src, void *dst, size_t len)
8038365ca3Sjmcneill {
8138365ca3Sjmcneill memcpy(dst, src, len);
8218218b2dSreinoud #ifdef DEBUG
8318218b2dSreinoud if (memcmp(dst, src, len) != 0)
8418218b2dSreinoud panic("kcopy not finished correctly\n");
8518218b2dSreinoud #endif
8638365ca3Sjmcneill return 0;
8738365ca3Sjmcneill }
8838365ca3Sjmcneill
8938365ca3Sjmcneill int
_ufetch_8(const uint8_t * uaddr,uint8_t * valp)9091bfaeb6Sthorpej _ufetch_8(const uint8_t *uaddr, uint8_t *valp)
9191bfaeb6Sthorpej {
9291bfaeb6Sthorpej *valp = *uaddr;
9394d0f933Smsaitoh return 0;
9438365ca3Sjmcneill }
9538365ca3Sjmcneill
9638365ca3Sjmcneill int
_ufetch_16(const uint16_t * uaddr,uint16_t * valp)9791bfaeb6Sthorpej _ufetch_16(const uint16_t *uaddr, uint16_t *valp)
9838365ca3Sjmcneill {
9991bfaeb6Sthorpej *valp = *uaddr;
10094d0f933Smsaitoh return 0;
10138365ca3Sjmcneill }
10238365ca3Sjmcneill
10338365ca3Sjmcneill int
_ufetch_32(const uint32_t * uaddr,uint32_t * valp)10491bfaeb6Sthorpej _ufetch_32(const uint32_t *uaddr, uint32_t *valp)
10538365ca3Sjmcneill {
10691bfaeb6Sthorpej *valp = *uaddr;
10794d0f933Smsaitoh return 0;
10891bfaeb6Sthorpej }
10991bfaeb6Sthorpej
11091bfaeb6Sthorpej #ifdef _LP64
11191bfaeb6Sthorpej int
_ufetch_64(const uint64_t * uaddr,uint64_t * valp)11291bfaeb6Sthorpej _ufetch_64(const uint64_t *uaddr, uint64_t *valp)
11391bfaeb6Sthorpej {
11491bfaeb6Sthorpej *valp = *uaddr;
11594d0f933Smsaitoh return 0;
11691bfaeb6Sthorpej }
11791bfaeb6Sthorpej #endif /* _LP64 */
11891bfaeb6Sthorpej
11991bfaeb6Sthorpej int
_ustore_8(uint8_t * uaddr,uint8_t val)12091bfaeb6Sthorpej _ustore_8(uint8_t *uaddr, uint8_t val)
12191bfaeb6Sthorpej {
12291bfaeb6Sthorpej *uaddr = val;
12394d0f933Smsaitoh return 0;
12438365ca3Sjmcneill }
12538365ca3Sjmcneill
12638365ca3Sjmcneill int
_ustore_16(uint16_t * uaddr,uint16_t val)12791bfaeb6Sthorpej _ustore_16(uint16_t *uaddr, uint16_t val)
12838365ca3Sjmcneill {
12991bfaeb6Sthorpej *uaddr = val;
13094d0f933Smsaitoh return 0;
13138365ca3Sjmcneill }
13291bfaeb6Sthorpej
13391bfaeb6Sthorpej int
_ustore_32(uint32_t * uaddr,uint32_t val)13491bfaeb6Sthorpej _ustore_32(uint32_t *uaddr, uint32_t val)
13591bfaeb6Sthorpej {
13691bfaeb6Sthorpej *uaddr = val;
13794d0f933Smsaitoh return 0;
13891bfaeb6Sthorpej }
13991bfaeb6Sthorpej
14091bfaeb6Sthorpej #ifdef _LP64
14191bfaeb6Sthorpej int
_ustore_64(uint64_t * uaddr,uint64_t val)14291bfaeb6Sthorpej _ustore_64(uint64_t *uaddr, uint64_t val)
14391bfaeb6Sthorpej {
14491bfaeb6Sthorpej *uaddr = val;
14594d0f933Smsaitoh return 0;
14691bfaeb6Sthorpej }
14791bfaeb6Sthorpej #endif /* _LP64 */
148