source: trunk/hal/tsar_mips32/core/hal_special.c @ 619

Last change on this file since 619 was 619, checked in by alain, 5 years ago

1) Fix a bug in KSH : after the "load" command,

the [ksh] prompt is now printed after completion
of the loaded application.

2) Fix a bug in vmm_handle_cow() : the copy-on-write

use now a hal_remote_memcpy() to replicate the page content.


File size: 6.5 KB
Line 
1/*
2 * hal_special.c - implementation of Generic Special Register Access API for TSAR-MIPS32
3 *
4 * Author    Alain Greiner (2016,2017)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH..
9 *
10 * ALMOS-MKH. is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH. is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25#include <hal_kernel_types.h>
26#include <hal_special.h>
27#include <hal_exception.h>
28#include <core.h>
29#include <thread.h>
30
31/****  Forward declarations ****/
32
33struct thread_s;
34
35////////////////////////////////
36inline gid_t hal_get_gid( void )
37{
38        uint32_t proc_id;
39
40        asm volatile ("mfc0    %0,  $15, 1" : "=&r" (proc_id));
41
42        return (proc_id & 0x3FF);  // 4/4/2 format for TSAR
43}
44
45///////////////////////////////////
46inline reg_t hal_time_stamp( void )
47{
48    reg_t count;
49
50        asm volatile ("mfc0   %0,  $9" : "=&r" (count));
51
52    return count;
53}
54
55///////////////////////////////
56inline reg_t hal_get_sr( void )
57{
58    reg_t sr;
59
60        asm volatile ("mfc0    %0,    $12" : "=&r" (sr));
61
62        return sr;
63}
64
65///////////////////////////////
66uint64_t hal_get_cycles( void )
67{
68        uint64_t cycles;                // absolute time to be returned
69    uint32_t last_count;            // last registered cycles count
70    uint32_t current_count;         // current cycles count
71        uint32_t elapsed;
72
73    core_t * core = CURRENT_THREAD->core;
74
75    // get last registered time stamp
76        last_count = core->time_stamp;
77
78    // get current time stamp from hardware register
79        current_count = hal_time_stamp();
80
81        // compute number of elapsed cycles, taking into account 32 bits register wrap
82        if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count;
83        else                           elapsed = current_count - last_count;
84
85    // compute absolute time
86        cycles = core->cycles + elapsed;
87
88        // update core time
89        core->time_stamp = current_count;
90        core->cycles     = cycles;
91
92        hal_fence();
93
94        return cycles;
95}
96
97///////////////////////////////////////////////////////
98inline struct thread_s * hal_get_current_thread( void )
99{
100        void * thread_ptr;
101 
102        asm volatile ("mfc0    %0,  $4,  2" : "=&r" (thread_ptr));
103
104        return thread_ptr;
105}
106
107///////////////////////////////////////////////////////
108void hal_set_current_thread( struct thread_s * thread )
109{ 
110        asm volatile ("mtc0    %0,  $4,  2" : : "r" (thread));
111}
112
113///////////////////////////
114void hal_fpu_enable( void )
115{
116    // set CU1 bit (FPU enable) in c0_sr
117        asm volatile 
118        ( ".set noat                         \n"
119      "lui    $27,    0x2000             \n"
120      "mfc0   $1,     $12                \n"
121      "or     $27,    $1,    $27         \n"
122      "mtc0   $27,    $12                \n"
123      ".set at                           \n" );
124
125    // set CU1 bit in calling thread UZONE
126    uint32_t * uzone = CURRENT_THREAD->uzone_current;
127    uzone[34] |= 0x20000000;
128}
129
130////////////////////////////
131void hal_fpu_disable( void )
132{
133    // reset CU1 bit (FPU enable) in c0_sr
134        asm volatile 
135        ( ".set noat                         \n"
136      "lui    $27,    0xDFFF             \n"
137          "ori    $27,    $27,   0xFFFF      \n"
138      "mfc0   $1,     $12                \n"
139      "and    $27,    $1,    $27         \n"
140      "mtc0   $27,    $12                \n"
141          ".set at                           \n");
142
143    // reset CU1 bit in calling thread UZONE
144    uint32_t * uzone = CURRENT_THREAD->uzone_current;
145    uzone[34] &= 0xDFFFFFFF;
146}
147
148///////////////////////////
149uint32_t hal_get_sp( void )
150{
151        register uint32_t sp;
152 
153        asm volatile ("or    %0,   $0,   $29" : "=&r" (sp));
154 
155        return sp;
156}
157
158/////////////////////////////////////
159uint32_t hal_set_sp( void * new_val )
160{
161        register uint32_t sp;
162 
163        asm volatile
164        ( "or    %0,   $0,      $29   \n"
165          "or    $29,  $0,      %1    \n"
166          : "=&r" (sp) : "r" (new_val)  );
167 
168        return sp;
169}
170
171///////////////////////////
172uint32_t hal_get_ra( void )
173{
174        register uint32_t ra;
175 
176        asm volatile ("or    %0,   $0,   $31" : "=&r" (ra));
177 
178        return ra;
179}
180
181//////////////////////////////////
182uint32_t hal_get_bad_vaddr( void )
183{
184        register uint32_t bad_va;
185
186        asm volatile
187    ( "mfc0    %0,  $8  \n"
188      : "=&r" (bad_va) );
189
190        return bad_va;
191}
192
193////////////////////////////////////////////
194uint32_t hal_uncached_read( uint32_t * ptr )
195{
196        register uint32_t val;
197
198        asm volatile
199        ( "ll    %0,     (%1)  \n"
200      : "=&r"(val) : "r" (ptr) );
201
202        return val;
203}
204
205//////////////////////////////////////////
206void hal_invalid_dcache_line( void * ptr )
207{
208        asm volatile
209        ( "cache    %0,     (%1)              \n"
210          "sync                               \n"
211          : : "i" (0x11) , "r" (ptr) );
212}
213
214/////////////////////////////
215inline void hal_fence( void )
216{
217        asm volatile ("sync");
218}
219
220/////////////////////////////
221inline void hal_rdbar( void )
222{
223        asm volatile( "" ::: "memory" );
224}
225
226///////////////////////////
227void hal_core_sleep( void )
228{
229    thread_t * this = CURRENT_THREAD;
230
231    printk("\n*** thread[%x,%x] on core[%x,%d]/n"
232           "  sr = %X / sp = %X / ra = %X\n",
233           this->process->pid, this->trdid, local_cxy, this->core->lid, 
234           hal_get_sr(), hal_get_sp(), hal_get_ra() );
235
236        while( 1 ) asm volatile ("nop");
237}
238
239//////////////////////////////////////
240void hal_fixed_delay( uint32_t delay )
241{ 
242    asm volatile
243    ( ".set noreorder        \n"
244      "or    $27,  %0,  $0   \n"
245      "1:                    \n"
246      "addi  $27, $27,  -1   \n"
247      "nop                   \n"
248      "bne   $27,  $0,  1b   \n"
249      "nop                   \n"
250      ".set reorder          \n"
251      : : "r" (delay>>2) : "$27" );
252}
253
254//////////////////////////////////////////////////
255void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code,
256                       intptr_t * mmu_ins_bad_vaddr,
257                       intptr_t * mmu_dat_excp_code,
258                       intptr_t * mmu_dat_bad_vaddr )
259{
260    asm volatile
261    ( "mfc2   %0,    $11        \n"
262      "mfc2   %1,    $13        \n"
263      "mfc2   %2,    $12        \n"
264      "mfc2   %3,    $14        \n"
265      : "=&r"(*mmu_ins_excp_code),
266        "=&r"(*mmu_ins_bad_vaddr),
267        "=&r"(*mmu_dat_excp_code),
268        "=&r"(*mmu_dat_bad_vaddr) );
269}
270
Note: See TracBrowser for help on using the repository browser.