11.3Srin/*	$NetBSD: kcopy.c,v 1.3 2020/07/06 09:34:16 rin Exp $	*/
21.2Smatt/*-
31.2Smatt * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
41.2Smatt * All rights reserved.
51.2Smatt *
61.2Smatt * This code is derived from software contributed to The NetBSD Foundation
71.2Smatt * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
81.2Smatt * Agency and which was developed by Matt Thomas of 3am Software Foundry.
91.2Smatt *
101.2Smatt * This material is based upon work supported by the Defense Advanced Research
111.2Smatt * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
121.2Smatt * Contract No. N66001-09-C-2073.
131.2Smatt * Approved for Public Release, Distribution Unlimited
141.2Smatt *
151.2Smatt * Redistribution and use in source and binary forms, with or without
161.2Smatt * modification, are permitted provided that the following conditions
171.2Smatt * are met:
181.2Smatt * 1. Redistributions of source code must retain the above copyright
191.2Smatt *    notice, this list of conditions and the following disclaimer.
201.2Smatt * 2. Redistributions in binary form must reproduce the above copyright
211.2Smatt *    notice, this list of conditions and the following disclaimer in the
221.2Smatt *    documentation and/or other materials provided with the distribution.
231.2Smatt *
241.2Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
251.2Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
261.2Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
271.2Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
281.2Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
291.2Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
301.2Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
311.2Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
321.2Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
331.2Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
341.2Smatt * POSSIBILITY OF SUCH DAMAGE.
351.2Smatt */
361.2Smatt
371.2Smatt#include <sys/cdefs.h>
381.3Srin__KERNEL_RCSID(0, "$NetBSD: kcopy.c,v 1.3 2020/07/06 09:34:16 rin Exp $");
391.2Smatt
401.2Smatt#include <sys/param.h>
411.2Smatt#include <sys/cpu.h>
421.2Smatt
431.2Smatt#include <machine/pcb.h>
441.2Smatt
451.2Smatt/*
461.2Smatt * kcopy(const void *src, void *dst, size_t len);
471.2Smatt *
481.2Smatt * Copy len bytes from src to dst, aborting if we encounter a fatal
491.2Smatt * page fault.
501.2Smatt *
511.2Smatt * kcopy() _must_ save and restore the old fault handler since it is
521.2Smatt * called by uiomove(), which may be in the path of servicing a non-fatal
531.2Smatt * page fault.
541.2Smatt */
551.2Smattint
561.2Smattkcopy(const void *src, void *dst, size_t len)
571.2Smatt{
581.2Smatt	struct pcb * const pcb = lwp_getpcb(curlwp);
591.2Smatt	struct faultbuf * const saved_onfault = pcb->pcb_onfault;
601.2Smatt	struct faultbuf env;
611.2Smatt
621.2Smatt	const int rv = setfault(&env);
631.2Smatt	if (__predict_true(rv == 0))
641.2Smatt		memcpy(dst, src, len);
651.2Smatt
661.2Smatt	pcb->pcb_onfault = saved_onfault;
671.2Smatt	return rv;
681.2Smatt}
69