copyoutstr.c revision 1.16
11.16Srin/* $NetBSD: copyoutstr.c,v 1.16 2022/10/03 23:32:27 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.16Srin__KERNEL_RCSID(0, "$NetBSD: copyoutstr.c,v 1.16 2022/10/03 23:32:27 rin Exp $"); 401.1Ssimonb 411.1Ssimonb#include <sys/param.h> 421.1Ssimonb#include <uvm/uvm_extern.h> 431.15Srin#include <powerpc/ibm4xx/spr.h> 441.1Ssimonb#include <machine/pcb.h> 451.1Ssimonb 461.1Ssimonbint 471.1Ssimonbcopyoutstr(const void *kaddr, void *udaddr, size_t len, size_t *done) 481.1Ssimonb{ 491.1Ssimonb struct pmap *pm = curproc->p_vmspace->vm_map.pmap; 501.11Srin size_t resid; 511.11Srin int rv, msr, pid, data, ctx; 521.3Smatt struct faultbuf env; 531.1Ssimonb 541.10Srin if (__predict_false(len == 0)) { 551.10Srin if (done) 561.10Srin *done = 0; 571.10Srin return 0; 581.10Srin } 591.10Srin 601.9Schs if ((rv = setfault(&env))) { 611.9Schs curpcb->pcb_onfault = NULL; 621.1Ssimonb if (done) 631.10Srin *done = 0; 641.9Schs return rv; 651.1Ssimonb } 661.1Ssimonb 671.1Ssimonb if (!(ctx = pm->pm_ctx)) { 681.1Ssimonb /* No context -- assign it one */ 691.1Ssimonb ctx_alloc(pm); 701.1Ssimonb ctx = pm->pm_ctx; 711.1Ssimonb } 721.1Ssimonb 731.11Srin resid = len; 741.10Srin __asm volatile( 751.16Srin "mtctr %[resid];" /* Set up counter */ 761.16Srin 771.16Srin "mfmsr %[msr];" /* Save MSR */ 781.16Srin 791.16Srin "li %[pid],0x20;" /* Disable IMMU */ 801.16Srin "andc %[pid],%[msr],%[pid];" 811.16Srin "mtmsr %[pid];" 821.14Srin "isync;" 831.10Srin 841.16Srin MFPID(%[pid]) /* Save old PID */ 851.16Srin 861.16Srin "1:" MTPID(%[pid]) 871.15Srin "isync;" 881.16Srin 891.16Srin "lbz %[data],0(%[kaddr]);" /* Load kernel byte */ 901.16Srin "addi %[kaddr],%[kaddr],1;" 911.14Srin "sync;" 921.16Srin 931.16Srin MTPID(%[ctx]) /* Load user ctx */ 941.15Srin "isync;" 951.16Srin 961.16Srin "stb %[data],0(%[udaddr]);" /* Store byte */ 971.16Srin "dcbst 0,%[udaddr];" 981.16Srin "addi %[udaddr],%[udaddr],1;" 991.16Srin 1001.16Srin "or. %[data],%[data],%[data];" 1011.14Srin "sync;" 1021.16Srin "bdnzf eq,1b;" /* while(ctr-- && !zero) */ 1031.10Srin 1041.16Srin MTPID(%[pid]) /* Restore PID, MSR */ 1051.16Srin "mtmsr %[msr];" 1061.14Srin "isync;" 1071.16Srin 1081.16Srin "mfctr %[resid];" /* Restore resid */ 1091.16Srin 1101.16Srin : [msr] "=&r" (msr), [pid] "=&r" (pid), [data] "=&r" (data), 1111.16Srin [resid] "+r" (resid) 1121.16Srin : [ctx] "r" (ctx), [udaddr] "b" (udaddr), [kaddr] "b" (kaddr)); 1131.10Srin 1141.9Schs curpcb->pcb_onfault = NULL; 1151.1Ssimonb if (done) 1161.11Srin *done = len - resid; 1171.11Srin if (resid == 0 && (char)data != '\0') 1181.11Srin return ENAMETOOLONG; 1191.11Srin else 1201.11Srin return 0; 1211.1Ssimonb} 122