copyoutstr.c revision 1.11
11.11Srin/*	$NetBSD: copyoutstr.c,v 1.11 2020/03/05 01:10:57 rin 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.4Slukem
381.4Slukem#include <sys/cdefs.h>
391.11Srin__KERNEL_RCSID(0, "$NetBSD: copyoutstr.c,v 1.11 2020/03/05 01:10:57 rin Exp $");
401.1Ssimonb
411.1Ssimonb#include <sys/param.h>
421.1Ssimonb#include <uvm/uvm_extern.h>
431.1Ssimonb#include <machine/pcb.h>
441.1Ssimonb
451.1Ssimonbint
461.1Ssimonbcopyoutstr(const void *kaddr, void *udaddr, size_t len, size_t *done)
471.1Ssimonb{
481.1Ssimonb	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
491.11Srin	size_t resid;
501.11Srin	int rv, msr, pid, data, ctx;
511.3Smatt	struct faultbuf env;
521.1Ssimonb
531.10Srin	if (__predict_false(len == 0)) {
541.10Srin		if (done)
551.10Srin			*done = 0;
561.10Srin		return 0;
571.10Srin	}
581.10Srin
591.9Schs	if ((rv = setfault(&env))) {
601.9Schs		curpcb->pcb_onfault = NULL;
611.1Ssimonb		if (done)
621.10Srin			*done = 0;
631.9Schs		return rv;
641.1Ssimonb	}
651.1Ssimonb
661.1Ssimonb	if (!(ctx = pm->pm_ctx)) {
671.1Ssimonb		/* No context -- assign it one */
681.1Ssimonb		ctx_alloc(pm);
691.1Ssimonb		ctx = pm->pm_ctx;
701.1Ssimonb	}
711.1Ssimonb
721.11Srin	resid = len;
731.10Srin	__asm volatile(
741.10Srin		"mtctr %3;"			/* Set up counter */
751.10Srin		"mfmsr %0;"			/* Save MSR */
761.10Srin		"li %1,0x20;"
771.10Srin		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
781.10Srin		"mfpid %1;"			/* Save old PID */
791.10Srin		"sync; isync;"
801.10Srin
811.10Srin		"1:"
821.10Srin		"mtpid %1;sync;"
831.10Srin		"lbz %2,0(%6); addi %6,%6,1;"	/* Store kernel byte */
841.10Srin		"sync; isync;"
851.10Srin		"mtpid %4; sync;"		/* Load user ctx */
861.10Srin		"stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;"
871.10Srin						/* Load byte */
881.10Srin		"sync; isync;"
891.10Srin		"or. %2,%2,%2;"
901.10Srin		"bdnzf 2,1b;"			/* while(ctr-- && !zero) */
911.10Srin
921.10Srin		"mtpid %1; mtmsr %0;"		/* Restore PID, MSR */
931.10Srin		"sync; isync;"
941.11Srin		"mfctr %3;"			/* Restore resid */
951.11Srin		: "=&r" (msr), "=&r" (pid), "=&r" (data), "+r" (resid)
961.10Srin		: "r" (ctx), "b" (udaddr), "b" (kaddr));
971.10Srin
981.9Schs	curpcb->pcb_onfault = NULL;
991.1Ssimonb	if (done)
1001.11Srin		*done = len - resid;
1011.11Srin	if (resid == 0 && (char)data != '\0')
1021.11Srin		return ENAMETOOLONG;
1031.11Srin	else
1041.11Srin		return 0;
1051.1Ssimonb}
106