[1] | 1 | /* |
---|
| 2 | * hal_uspace.c - implementation of Generic User Space Access API for MIPS32 |
---|
| 3 | * |
---|
| 4 | * Author Mohamed Karaoui (2015) |
---|
| 5 | * Alain Greiner (2016) |
---|
| 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH.. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH. is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-MKH. is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include <hal_types.h> |
---|
| 26 | #include <hal_uspace.h> |
---|
| 27 | #include <hal_irqmask.h> |
---|
| 28 | |
---|
| 29 | /////////////////////////////////////////// |
---|
| 30 | void hal_copy_from_uspace( void * k_dst, |
---|
| 31 | void * u_src, |
---|
| 32 | uint32_t size ) |
---|
| 33 | { |
---|
| 34 | uint32_t save_sr; |
---|
| 35 | uint32_t i; |
---|
| 36 | uint32_t wsize; // number of words |
---|
| 37 | uint32_t src = (uint32_t)u_src; |
---|
| 38 | uint32_t dst = (uint32_t)k_dst; |
---|
| 39 | |
---|
| 40 | if( (dst & 0x3) || (src & 0x3) ) wsize = 0; // do it all in bytes |
---|
| 41 | else wsize = size >> 2; |
---|
| 42 | |
---|
| 43 | hal_disable_irq( &save_sr ); |
---|
| 44 | |
---|
| 45 | for( i = 0 ; i < wsize ; i++ ) // transfer one word per iteration |
---|
| 46 | { |
---|
| 47 | asm volatile( |
---|
| 48 | "mfc2 $15, $1 \n" /* save MMU_MODE */ |
---|
| 49 | "ori $14, $0, 0x7 \n" |
---|
| 50 | "mtc2 $14, $1 \n" /* MMU_MODE <= DTLB ON */ |
---|
| 51 | "lw $13, 0(%0) \n" /* read data from user space */ |
---|
| 52 | "mtc2 $14, $1 \n" /* restore MMU_MODE */ |
---|
| 53 | "sw $13, 0(%1) \n" /* store data to kernel space */ |
---|
| 54 | : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" ); |
---|
| 55 | |
---|
| 56 | src += 4; |
---|
| 57 | dst += 4; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | for( i = wsize << 2 ; i < size ; i++ ) // transfer one byte per iteration |
---|
| 61 | { |
---|
| 62 | asm volatile( |
---|
| 63 | "mfc2 $15, $1 \n" /* save MMU_MODE */ |
---|
| 64 | "ori $14, $0, 0x7 \n" |
---|
| 65 | "mtc2 $14, $1 \n" /* MMU_MODE <= DTLB ON */ |
---|
| 66 | "lb $13, 0(%0) \n" /* read data from user space */ |
---|
| 67 | "mtc2 $14, $1 \n" /* restore MMU_MODE */ |
---|
| 68 | "sb $13, 0(%1) \n" /* store data to kernel space */ |
---|
| 69 | : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" ); |
---|
| 70 | |
---|
| 71 | src += 1; |
---|
| 72 | dst += 1; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | hal_restore_irq( save_sr ); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /////////////////////////////////////////// |
---|
| 79 | void hal_copy_to_uspace( void * u_dst, |
---|
| 80 | void * k_src, |
---|
| 81 | uint32_t size ) |
---|
| 82 | { |
---|
| 83 | uint32_t save_sr; |
---|
| 84 | uint32_t i; |
---|
| 85 | uint32_t wsize; // number of words |
---|
| 86 | uint32_t src = (uint32_t)k_src; |
---|
| 87 | uint32_t dst = (uint32_t)u_dst; |
---|
| 88 | |
---|
| 89 | if( (dst & 0x3) || (src & 0x3) ) wsize = 0; // do it all in bytes |
---|
| 90 | else wsize = size >> 2; |
---|
| 91 | |
---|
| 92 | hal_disable_irq( &save_sr ); |
---|
| 93 | |
---|
| 94 | for( i = 0 ; i < wsize ; i++ ) // transfer one word per iteration |
---|
| 95 | { |
---|
| 96 | asm volatile( |
---|
| 97 | "mfc2 $15, $1 \n" /* save MMU_MODE */ |
---|
| 98 | "lw $13, 0(%0) \n" /* read data from kernel space */ |
---|
| 99 | "ori $14, $0, 0x7 \n" |
---|
| 100 | "mtc2 $14, $1 \n" /* MMU_MODE <= DTLB ON */ |
---|
| 101 | "sw $13, 0(%1) \n" /* store data to user space */ |
---|
| 102 | "mtc2 $14, $1 \n" /* restore MMU_MODE */ |
---|
| 103 | : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" ); |
---|
| 104 | |
---|
| 105 | src += 4; |
---|
| 106 | dst += 4; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | for( i = wsize << 2 ; i < size ; i++ ) // transfer one byte per iteration |
---|
| 110 | { |
---|
| 111 | asm volatile( |
---|
| 112 | "mfc2 $15, $1 \n" /* save MMU_MODE */ |
---|
| 113 | "lw $13, 0(%0) \n" /* read data from kernel space */ |
---|
| 114 | "ori $14, $0, 0x7 \n" |
---|
| 115 | "mtc2 $14, $1 \n" /* MMU_MODE <= DTLB ON */ |
---|
| 116 | "sb $13, 0(%1) \n" /* store data to user space */ |
---|
| 117 | "mtc2 $14, $1 \n" /* restore MMU_MODE */ |
---|
| 118 | : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" ); |
---|
| 119 | |
---|
| 120 | src += 1; |
---|
| 121 | dst += 1; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | hal_restore_irq( save_sr ); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /////////////////////////////////////////////// |
---|
| 128 | uint32_t hal_strlen_from_uspace( char * u_str ) |
---|
| 129 | { |
---|
| 130 | uint32_t save_sr; |
---|
| 131 | uint32_t str = (uint32_t)u_str; |
---|
| 132 | uint32_t count = 0; |
---|
| 133 | |
---|
| 134 | hal_disable_irq( &save_sr ); |
---|
| 135 | |
---|
| 136 | asm volatile( |
---|
| 137 | "ori $15, %0, 0 \n" /* $15 <= count */ |
---|
| 138 | "ori $13, %1, 0 \n" /* $13 <= str */ |
---|
| 139 | |
---|
| 140 | "mfc2 $15, $1 \n" /* save MMU_MODE */ |
---|
| 141 | "ori $14, $0, 0x7 \n" /* $14 <= mode DTLB on */ |
---|
| 142 | "mtc2 $14, $1 \n" /* MMU_MODE <= DTLB ON */ |
---|
| 143 | |
---|
| 144 | "1: \n" |
---|
| 145 | "lb $13, 0(%0) \n" /* read data from kernel space */ |
---|
| 146 | "addi $13, $13, 1 \n" /* increment address */ |
---|
| 147 | "bne $13, $0, 1b \n" /* loop until NUL found */ |
---|
| 148 | "addi $15, $15, 1 \n" /* increment counter */ |
---|
| 149 | |
---|
| 150 | "mtc2 $14, $1 \n" /* restore MMU_MODE */ |
---|
| 151 | : "+r"(count) : "r"(str) : "$13","$14","$15" ); |
---|
| 152 | |
---|
| 153 | hal_restore_irq( save_sr ); |
---|
| 154 | |
---|
| 155 | return count; |
---|
| 156 | } |
---|
| 157 | |
---|