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

Last change on this file since 411 was 407, checked in by alain, 6 years ago

First implementation of fork/exec.

File size: 5.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_types.h>
26#include <hal_special.h>
27#include <core.h>
28#include <thread.h>
29
30/****  Forward declarations ****/
31
32struct thread_s;
33
34//////////////////////////
35inline gid_t hal_get_gid()
36{
37        uint32_t proc_id;
38
39        asm volatile ("mfc0    %0,  $15, 1" : "=&r" (proc_id));
40
41        return (proc_id & 0x3FF);  // 4/4/2 format for TSAR
42}
43
44/////////////////////////////
45inline reg_t hal_time_stamp()
46{
47    reg_t count;
48
49        asm volatile ("mfc0   %0,  $9" : "=&r" (count));
50
51    return count;
52}
53
54/////////////////////////
55inline reg_t hal_get_sr()
56{
57    reg_t sr;
58
59        asm volatile ("mfc0    %0,    $12" : "=&r" (sr));
60
61        return sr;
62}
63
64/////////////////////////
65uint64_t hal_get_cycles()
66{
67        uint64_t cycles;                // absolute time to be returned
68    uint32_t last_count;            // last registered cycles count
69    uint32_t current_count;         // current cycles count
70        uint32_t elapsed;
71
72    core_t * core = CURRENT_THREAD->core;
73
74    // get last registered time stamp
75        last_count = core->time_stamp;
76
77    // get current time stamp from hardware register
78        current_count = hal_time_stamp();
79
80        // compute number of elapsed cycles, taking into account 32 bits register wrap
81        if(current_count < last_count) elapsed = (0xFFFFFFFF - last_count) + current_count;
82        else                           elapsed = current_count - last_count;
83
84    // compute absolute time
85        cycles = core->cycles + elapsed;
86
87        // update core time
88        core->time_stamp = current_count;
89        core->cycles     = cycles;
90
91        hal_fence();
92
93        return cycles;
94}
95
96/////////////////////////////////////////////////
97inline struct thread_s * hal_get_current_thread()
98{
99        void * thread_ptr;
100 
101        asm volatile ("mfc0    %0,  $4,  2" : "=&r" (thread_ptr));
102
103        return thread_ptr;
104}
105
106///////////////////////////////////////////////////////
107void hal_set_current_thread( struct thread_s * thread )
108{ 
109        asm volatile ("mtc0    %0,  $4,  2" : : "r" (thread));
110}
111
112/////////////////////
113void hal_fpu_enable()
114{
115        asm volatile 
116        ( ".set noat                         \n"
117      "lui    $27,    0x2000             \n"
118      "mfc0   $1,     $12                \n"
119      "or     $27,    $1,    $27         \n"
120      "mtc0   $27,    $12                \n"
121      ".set at                           \n" );
122}
123
124//////////////////////
125void hal_fpu_disable()
126{
127        asm volatile 
128        ( ".set noat                         \n"
129      "lui    $27,    0xDFFF             \n"
130          "ori    $27,    $27,   0xFFFF      \n"
131      "mfc0   $1,     $12                \n"
132      "and    $27,    $1,    $27         \n"
133      "mtc0   $27,    $12                \n"
134          ".set at                           \n");
135}
136
137////////////////////////
138uint32_t hal_get_stack()
139{
140        register uint32_t sp;
141 
142        asm volatile ("or    %0,   $0,   $29" : "=&r" (sp));
143 
144        return sp;
145}
146
147////////////////////////////////////////
148uint32_t hal_set_stack( void * new_val )
149{
150        register uint32_t sp;
151 
152        asm volatile
153        ( "or    %0,   $0,      $29   \n"
154          "or    $29,  $0,      %1    \n"
155          : "=&r" (sp) : "r" (new_val)  );
156 
157        return sp;
158}
159
160////////////////////////////
161uint32_t hal_get_bad_vaddr()
162{
163        register uint32_t bad_va;
164
165        asm volatile
166    ( "mfc0    %0,  $8  \n"
167      : "=&r" (bad_va) );
168
169        return bad_va;
170}
171
172////////////////////////////////////////////
173uint32_t hal_uncached_read( uint32_t * ptr )
174{
175        register uint32_t val;
176
177        asm volatile
178        ( "ll    %0,     (%1)  \n"
179      : "=&r"(val) : "r" (ptr) );
180
181        return val;
182}
183
184//////////////////////////////////////////
185void hal_invalid_dcache_line( void * ptr )
186{
187        asm volatile
188        ( "cache    %0,     (%1)              \n"
189          "sync                               \n"
190          : : "i" (0x11) , "r" (ptr) );
191}
192
193////////////////
194void hal_fence()
195{
196        asm volatile ("sync");
197}
198
199////////////////
200void hal_rdbar()
201{
202        asm volatile( "" ::: "memory" );
203}
204
205/////////////////////
206void hal_core_sleep()
207{
208        asm volatile ("wait");
209}
210
211//////////////////////////////////////
212void hal_fixed_delay( uint32_t delay )
213{ 
214    asm volatile
215    ( ".set noreorder        \n"
216      "or    $27,  %0,  $0   \n"
217      "1:                    \n"
218      "addi  $27, $27,  -1   \n"
219      "nop                   \n"
220      "bne   $27,  $0,  1b   \n"
221      "nop                   \n"
222      ".set reorder          \n"
223      : : "r" (delay>>2) : "$27" );
224}
225
226//////////////////////////////////////////////////
227void hal_get_mmu_excp( intptr_t * mmu_ins_excp_code,
228                       intptr_t * mmu_ins_bad_vaddr,
229                       intptr_t * mmu_dat_excp_code,
230                       intptr_t * mmu_dat_bad_vaddr )
231{
232    asm volatile
233    ( "mfc2   %0,    $11        \n"
234      "mfc2   %1,    $13        \n"
235      "mfc2   %2,    $12        \n"
236      "mfc2   %3,    $14        \n"
237      : "=&r"(*mmu_ins_excp_code),
238        "=&r"(*mmu_ins_bad_vaddr),
239        "=&r"(*mmu_dat_excp_code),
240        "=&r"(*mmu_dat_bad_vaddr) );
241}
242
Note: See TracBrowser for help on using the repository browser.