copyoutstr.c revision 1.1
11.1Ssimonb/*	$NetBSD: copyoutstr.c,v 1.1 2001/06/13 06:01:48 simonb Exp $	*/
21.1Ssimonb
31.1Ssimonb/*
41.1Ssimonb * Copyright 2001 Wasabi Systems, Inc.
51.1Ssimonb * All rights reserved.
61.1Ssimonb *
71.1Ssimonb * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
81.1Ssimonb *
91.1Ssimonb * Redistribution and use in source and binary forms, with or without
101.1Ssimonb * modification, are permitted provided that the following conditions
111.1Ssimonb * are met:
121.1Ssimonb * 1. Redistributions of source code must retain the above copyright
131.1Ssimonb *    notice, this list of conditions and the following disclaimer.
141.1Ssimonb * 2. Redistributions in binary form must reproduce the above copyright
151.1Ssimonb *    notice, this list of conditions and the following disclaimer in the
161.1Ssimonb *    documentation and/or other materials provided with the distribution.
171.1Ssimonb * 3. All advertising materials mentioning features or use of this software
181.1Ssimonb *    must display the following acknowledgement:
191.1Ssimonb *      This product includes software developed for the NetBSD Project by
201.1Ssimonb *      Wasabi Systems, Inc.
211.1Ssimonb * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.1Ssimonb *    or promote products derived from this software without specific prior
231.1Ssimonb *    written permission.
241.1Ssimonb *
251.1Ssimonb * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.1Ssimonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.1Ssimonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.1Ssimonb * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.1Ssimonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.1Ssimonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.1Ssimonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.1Ssimonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.1Ssimonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.1Ssimonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.1Ssimonb * POSSIBILITY OF SUCH DAMAGE.
361.1Ssimonb */
371.1Ssimonb
381.1Ssimonb#include <sys/param.h>
391.1Ssimonb#include <uvm/uvm_extern.h>
401.1Ssimonb#include <machine/pcb.h>
411.1Ssimonb
421.1Ssimonbint setfault(faultbuf);		/* defined in locore.S */
431.1Ssimonb
441.1Ssimonbint
451.1Ssimonbcopyoutstr(const void *kaddr, void *udaddr, size_t len, size_t *done)
461.1Ssimonb{
471.1Ssimonb	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
481.1Ssimonb	int msr, pid, tmp, ctx;
491.1Ssimonb	faultbuf env;
501.1Ssimonb
511.1Ssimonb	if (setfault(env)) {
521.1Ssimonb		curpcb->pcb_onfault = 0;
531.1Ssimonb		/* XXXX -- len may be lost on a fault */
541.1Ssimonb		if (done)
551.1Ssimonb			*done = len;
561.1Ssimonb		return EFAULT;
571.1Ssimonb	}
581.1Ssimonb
591.1Ssimonb	if (!(ctx = pm->pm_ctx)) {
601.1Ssimonb		/* No context -- assign it one */
611.1Ssimonb		ctx_alloc(pm);
621.1Ssimonb		ctx = pm->pm_ctx;
631.1Ssimonb	}
641.1Ssimonb
651.1Ssimonb	if (len) {
661.1Ssimonb		asm volatile("mtctr %3;"		/* Set up counter */
671.1Ssimonb			"mfmsr %0;"			/* Save MSR */
681.1Ssimonb			"li %1,0x20; "
691.1Ssimonb			"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
701.1Ssimonb			"mfpid %1;"			/* Save old PID */
711.1Ssimonb			"sync; isync;"
721.1Ssimonb
731.1Ssimonb			"li %3,0;"			/* Clear len */
741.1Ssimonb
751.1Ssimonb			"1:"
761.1Ssimonb			"mtpid %1;sync;"
771.1Ssimonb			"lbz %2,0(%6); addi %6,%6,1;"	/* Store kernel byte */
781.1Ssimonb			"sync; isync;"
791.1Ssimonb			"mtpid %4; sync;"		/* Load user ctx */
801.1Ssimonb			"stb %2,0(%5);  dcbf 0,%5; addi %5,%5,1;"	/* Load byte */
811.1Ssimonb			"sync; isync;"
821.1Ssimonb			"addi %3,%3,1;"			/* Inc len */
831.1Ssimonb			"or. %2,%2,%2;"
841.1Ssimonb			"bdnzf 2,1b;"			/*
851.1Ssimonb							 * while(ctr-- && !zero)
861.1Ssimonb							 */
871.1Ssimonb
881.1Ssimonb			"2: mtpid %1; mtmsr %0;"	/* Restore PID, MSR */
891.1Ssimonb			"sync; isync;"
901.1Ssimonb			: "=&r" (msr), "=&r" (pid), "=&r" (tmp), "+r" (len)
911.1Ssimonb			: "r" (ctx), "r" (udaddr), "r" (kaddr));
921.1Ssimonb	}
931.1Ssimonb	curpcb->pcb_onfault = 0;
941.1Ssimonb	if (done)
951.1Ssimonb		*done = len;
961.1Ssimonb	return 0;
971.1Ssimonb}
98