1*16031f7dSrin /* $NetBSD: kcopy.c,v 1.3 2020/07/06 09:34:16 rin Exp $ */
2b8ea2c8cSmatt /*-
3b8ea2c8cSmatt * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4b8ea2c8cSmatt * All rights reserved.
5b8ea2c8cSmatt *
6b8ea2c8cSmatt * This code is derived from software contributed to The NetBSD Foundation
7b8ea2c8cSmatt * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8b8ea2c8cSmatt * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9b8ea2c8cSmatt *
10b8ea2c8cSmatt * This material is based upon work supported by the Defense Advanced Research
11b8ea2c8cSmatt * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12b8ea2c8cSmatt * Contract No. N66001-09-C-2073.
13b8ea2c8cSmatt * Approved for Public Release, Distribution Unlimited
14b8ea2c8cSmatt *
15b8ea2c8cSmatt * Redistribution and use in source and binary forms, with or without
16b8ea2c8cSmatt * modification, are permitted provided that the following conditions
17b8ea2c8cSmatt * are met:
18b8ea2c8cSmatt * 1. Redistributions of source code must retain the above copyright
19b8ea2c8cSmatt * notice, this list of conditions and the following disclaimer.
20b8ea2c8cSmatt * 2. Redistributions in binary form must reproduce the above copyright
21b8ea2c8cSmatt * notice, this list of conditions and the following disclaimer in the
22b8ea2c8cSmatt * documentation and/or other materials provided with the distribution.
23b8ea2c8cSmatt *
24b8ea2c8cSmatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25b8ea2c8cSmatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26b8ea2c8cSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27b8ea2c8cSmatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28b8ea2c8cSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b8ea2c8cSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b8ea2c8cSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b8ea2c8cSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b8ea2c8cSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b8ea2c8cSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b8ea2c8cSmatt * POSSIBILITY OF SUCH DAMAGE.
35b8ea2c8cSmatt */
36b8ea2c8cSmatt
37b8ea2c8cSmatt #include <sys/cdefs.h>
38*16031f7dSrin __KERNEL_RCSID(0, "$NetBSD: kcopy.c,v 1.3 2020/07/06 09:34:16 rin Exp $");
39b8ea2c8cSmatt
40b8ea2c8cSmatt #include <sys/param.h>
41b8ea2c8cSmatt #include <sys/cpu.h>
42b8ea2c8cSmatt
43b8ea2c8cSmatt #include <machine/pcb.h>
44b8ea2c8cSmatt
45b8ea2c8cSmatt /*
46b8ea2c8cSmatt * kcopy(const void *src, void *dst, size_t len);
47b8ea2c8cSmatt *
48b8ea2c8cSmatt * Copy len bytes from src to dst, aborting if we encounter a fatal
49b8ea2c8cSmatt * page fault.
50b8ea2c8cSmatt *
51b8ea2c8cSmatt * kcopy() _must_ save and restore the old fault handler since it is
52b8ea2c8cSmatt * called by uiomove(), which may be in the path of servicing a non-fatal
53b8ea2c8cSmatt * page fault.
54b8ea2c8cSmatt */
55b8ea2c8cSmatt int
kcopy(const void * src,void * dst,size_t len)56b8ea2c8cSmatt kcopy(const void *src, void *dst, size_t len)
57b8ea2c8cSmatt {
58b8ea2c8cSmatt struct pcb * const pcb = lwp_getpcb(curlwp);
59b8ea2c8cSmatt struct faultbuf * const saved_onfault = pcb->pcb_onfault;
60b8ea2c8cSmatt struct faultbuf env;
61b8ea2c8cSmatt
62b8ea2c8cSmatt const int rv = setfault(&env);
63b8ea2c8cSmatt if (__predict_true(rv == 0))
64b8ea2c8cSmatt memcpy(dst, src, len);
65b8ea2c8cSmatt
66b8ea2c8cSmatt pcb->pcb_onfault = saved_onfault;
67b8ea2c8cSmatt return rv;
68b8ea2c8cSmatt }
69