source: trunk/hal/tsar_mips32/core/hal_uspace.c @ 469

Last change on this file since 469 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 9.6 KB
Line 
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 <errno.h>
26#include <vmm.h>
27#include <hal_kernel_types.h>
28#include <hal_uspace.h>
29#include <hal_irqmask.h>
30
31#include <printk.h>
32#include <thread.h>
33
34///////////////////////////////////////////
35void hal_copy_from_uspace( void     * k_dst,
36                           void     * u_src,
37                           uint32_t   size ) 
38{
39    uint32_t save_sr;
40        uint32_t i;
41        uint32_t wsize;                        // number of words
42    uint32_t src = (uint32_t)u_src;
43    uint32_t dst = (uint32_t)k_dst;
44
45        if( (dst & 0x3) || (src & 0x3) ) wsize = 0;          // do it all in bytes
46    else                             wsize = size >> 2;
47
48    hal_disable_irq( &save_sr );
49
50
51        for( i = 0 ; i < wsize ; i++ )          // transfer one word per iteration
52        {
53        asm volatile(
54        "mfc2   $15,   $1           \n"   /* save   MMU_MODE                */
55        "ori    $14,   $0,  0x7     \n" 
56        "mtc2   $14,   $1                       \n"   /* MMU_MODE <= DTLB ON            */
57        "lw         $13,   0(%0)        \n"   /* read data from user space      */
58        "mtc2   $15,   $1                       \n"   /* restore MMU_MODE               */
59            "sw     $13,   0(%1)        \n"   /* store data to kernel space     */
60        : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" );
61
62        src += 4;
63        dst += 4;
64    }
65
66        for( i = wsize << 2 ; i < size ; i++ )  // transfer one byte per iteration
67        {
68        asm volatile(
69        "mfc2   $15,   $1           \n"   /* save   MMU_MODE                */
70        "ori    $14,   $0,  0x7     \n" 
71        "mtc2   $14,   $1                       \n"   /* MMU_MODE <= DTLB ON            */
72        "lb         $13,   0(%0)        \n"   /* read data from user space      */
73        "mtc2   $15,   $1                       \n"   /* restore MMU_MODE               */
74            "sb     $13,   0(%1)        \n"   /* store data to kernel space     */
75        : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" );
76
77        src += 1;
78        dst += 1;
79    }
80
81    hal_restore_irq( save_sr );
82
83}  // end hal_copy_from_uspace()
84
85///////////////////////////////////////////
86void hal_copy_to_uspace( void     * u_dst,
87                         void     * k_src,
88                         uint32_t   size )
89{
90    uint32_t save_sr;
91        uint32_t i;
92        uint32_t wsize;                   // number of words if aligned
93    uint32_t src = (uint32_t)k_src;
94    uint32_t dst = (uint32_t)u_dst;
95
96        if( (dst & 0x3) || (src & 0x3) ) wsize = 0;          // not aligned
97    else                             wsize = size >> 2;
98
99    hal_disable_irq( &save_sr );
100
101        for( i = 0 ; i < wsize ; i++ )          // transfer one word per iteration
102        {
103        asm volatile(
104        "mfc2   $15,   $1           \n"   /* save   MMU_MODE                */
105        "lw         $13,   0(%0)        \n"   /* read data from kernel space    */
106        "ori    $14,   $0,  0x7     \n" 
107        "mtc2   $14,   $1                       \n"   /* MMU_MODE <= DTLB ON            */
108            "sw     $13,   0(%1)        \n"   /* store data to user space       */
109        "mtc2   $15,   $1                       \n"   /* restore MMU_MODE               */
110        : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" );
111
112        src += 4;
113        dst += 4;
114    }
115
116        for( i = wsize << 2 ; i < size ; i++ )  // transfer one byte per iteration
117        {
118        asm volatile(
119        "mfc2   $15,   $1           \n"   /* save   MMU_MODE                */
120        "lw         $13,   0(%0)        \n"   /* read data from kernel space    */
121        "ori    $14,   $0,  0x7     \n" 
122        "mtc2   $14,   $1                       \n"   /* MMU_MODE <= DTLB ON            */
123            "sb     $13,   0(%1)        \n"   /* store data to user space       */
124        "mtc2   $15,   $1                       \n"   /* restore MMU_MODE               */
125        : : "r"( src ) , "r"( dst ) : "$13","$14","$15", "memory" );
126
127        src += 1;
128        dst += 1;
129    }
130
131    hal_restore_irq( save_sr );
132
133}  // end hal_copy_to_uspace()
134
135//////////////////////////////////////////////
136void hal_strcpy_from_uspace( char     * k_dst,
137                             char     * u_src,
138                             uint32_t   size )
139{
140    uint32_t save_sr;
141    uint32_t src = (uint32_t)u_src;
142    uint32_t dst = (uint32_t)k_dst;
143
144    hal_disable_irq( &save_sr );
145
146    // loop on characters while ( (character != NUL) and (count < size ) )
147    asm volatile(
148        ".set noreorder             \n"
149        "move   $11,   %0           \n"   /* $11 <= count == size           */
150        "move   $12,   %1           \n"   /* $12 <= u_src                   */
151        "move   $13,   %2           \n"   /* $13 <= k_dst                   */
152        "mfc2   $15,   $1           \n"   /* $15 <= mode DTLB and ITLB off  */
153        "ori    $14,   $15,  0x4    \n"   /* $14 <= mode DTLB on            */
154        "1:                         \n"
155        "mtc2   $14,   $1                       \n"   /* MMU_MODE <= DTLB ON            */
156        "lb     $10,   0($12)       \n"   /* read char from user space      */
157        "mtc2   $15,   $1                       \n"   /* restore DTLB and ITLB off      */
158            "sb     $10,   0($13)       \n"   /* store char to kernel space     */
159        "beq    $10,   $0,   2f     \n"   /* exit if char = 0               */
160        "addi   $11,   $11, -1      \n"   /* decrement count                */
161        "addi   $12,   $12,  1      \n"   /* increment u_src pointer        */
162        "beq    $11,   $0,   2f     \n"   /* exit if count == 0             */
163        "addi   $13,   $13,  1      \n"   /* increment k_src pointer        */
164        "j                   1b     \n"   /* jump to next iteration         */
165        "2:                         \n"
166        "nop                        \n"
167        ".set reorder               \n"
168        : 
169        : "r"(size),"r"(src),"r"(dst)
170        : "$10","$11","$12","$13","$14","$15" );
171       
172    hal_restore_irq( save_sr ); 
173
174} // hal_strcpy_from_uspace()
175
176////////////////////////////////////////////
177void hal_strcpy_to_uspace( char     * u_dst,
178                           char     * k_src,
179                           uint32_t   size )
180{
181    uint32_t save_sr;
182    uint32_t src = (uint32_t)k_src;
183    uint32_t dst = (uint32_t)u_dst;
184
185    hal_disable_irq( &save_sr );
186
187    // loop on characters while ( (character != NUL) and (count < size) )
188    asm volatile(
189        ".set noreorder             \n"
190        "move   $11,   %0           \n"   /* $11 <= count == size           */
191        "move   $12,   %1           \n"   /* $12 <= k_src                   */
192        "move   $13,   %2           \n"   /* $13 <= u_dst                   */
193        "mfc2   $15,   $1           \n"   /* $15 <= mode DTLB and ITLB off  */
194        "ori    $14,   $15,  0x4    \n"   /* $14 <= mode DTLB on            */
195        "1:                         \n"
196        "lb     $10,   0($12)       \n"   /* read char from kernel space    */
197        "mtc2   $14,   $1                       \n"   /* MMU_MODE <= DTLB ON            */
198            "sb     $10,   0($13)       \n"   /* store char to user space       */
199        "mtc2   $15,   $1                       \n"   /* restore DTLB and ITLB off      */
200        "beq    $10,   $0,   2f     \n"   /* exit if char == 0              */
201        "addi   $11,   $11, -1      \n"   /* decrement count                */
202        "addi   $12,   $12,  1      \n"   /* increment k_src pointer        */
203        "beq    $11,   $0,   2f     \n"   /* exit if count == size          */
204        "addi   $13,   $13,  1      \n"   /* increment u_src pointer        */
205        "j                   1b     \n"   /* jump to next iteration         */
206        "2:                         \n"
207        "nop                        \n"
208        ".set reorder               \n"
209        :
210        : "r"(size),"r"(src),"r"(dst)
211        : "$10","$11","$12","$13","$14","$15" );
212       
213    hal_restore_irq( save_sr ); 
214
215} // hal_strcpy_to_uspace()
216
217///////////////////////////////////////////////
218uint32_t hal_strlen_from_uspace( char * u_str )
219{
220    uint32_t save_sr;
221    uint32_t count = 0;
222    uint32_t str   = (uint32_t)u_str;
223
224    hal_disable_irq( &save_sr ); 
225
226        asm volatile(
227        ".set noreorder             \n"
228        "move   $13,   %1           \n"   /* $13 <= str                     */
229        "mfc2   $15,   $1           \n"   /* $15 <= DTLB and ITLB off       */
230        "ori    $14,   $15,  0x4    \n"   /* $14 <= mode DTLB on            */
231        "1:                         \n"
232        "mtc2   $14,   $1                       \n"   /* set DTLB on                    */
233        "lb         $12,   0($13)       \n"   /* read char from user space      */
234        "mtc2   $15,   $1                       \n"   /* set DTLB off                   */
235        "addi   $13,   $13,  1      \n"   /* increment address              */
236        "bne    $12,   $0,   1b     \n"   /* loop until NUL found           */
237        "addi   %0,    %0,   1      \n"   /* increment count                */
238        ".set reorder               \n"
239        : "+r"(count) 
240        : "r"(str) 
241        : "$12","$13","$14","$15" );
242
243    hal_restore_irq( save_sr );
244
245    return count;
246}
247
Note: See TracBrowser for help on using the repository browser.