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