kcopy.c revision 1.2
11.2Smatt/* $NetBSD: kcopy.c,v 1.2 2011/01/18 01:02:52 matt 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.2Smatt 391.2Smatt__KERNEL_RCSID(0, "$NetBSD: kcopy.c,v 1.2 2011/01/18 01:02:52 matt Exp $"); 401.2Smatt 411.2Smatt#include <sys/param.h> 421.2Smatt#include <sys/cpu.h> 431.2Smatt 441.2Smatt#include <machine/pcb.h> 451.2Smatt 461.2Smatt/* 471.2Smatt * kcopy(const void *src, void *dst, size_t len); 481.2Smatt * 491.2Smatt * Copy len bytes from src to dst, aborting if we encounter a fatal 501.2Smatt * page fault. 511.2Smatt * 521.2Smatt * kcopy() _must_ save and restore the old fault handler since it is 531.2Smatt * called by uiomove(), which may be in the path of servicing a non-fatal 541.2Smatt * page fault. 551.2Smatt */ 561.2Smattint 571.2Smattkcopy(const void *src, void *dst, size_t len) 581.2Smatt{ 591.2Smatt struct pcb * const pcb = lwp_getpcb(curlwp); 601.2Smatt struct faultbuf * const saved_onfault = pcb->pcb_onfault; 611.2Smatt struct faultbuf env; 621.2Smatt 631.2Smatt const int rv = setfault(&env); 641.2Smatt if (__predict_true(rv == 0)) 651.2Smatt memcpy(dst, src, len); 661.2Smatt 671.2Smatt pcb->pcb_onfault = saved_onfault; 681.2Smatt return rv; 691.2Smatt} 70