source: soft/giet_vm/sys/common.c @ 244

Last change on this file since 244 was 240, checked in by joannou, 11 years ago

Bug fix for scheduler handling :

  • In boot_init.c, changed left shifts of 10 to left shifts of 12 to support 4K schedulers instead of 1K schedulers
  • In common.c, updated _get_sched() function to return the content of CP0_SCHED register
File size: 15.1 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File     : common.c
3// Date     : 01/04/2012
4// Author   : alain greiner and joel porquet
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The common.c and common.h files are part of the GIET nano-kernel.
8// They contains various utilities functions.
9///////////////////////////////////////////////////////////////////////////////////
10
11#include <sys_handler.h>
12#include <common.h>
13#include <ctx_handler.h>
14#include <drivers.h>
15#include <hwr_mapping.h>
16#include <stdarg.h>
17
18///////////////////////////////////////////////////////////////////////////////////
19//    Global variables
20///////////////////////////////////////////////////////////////////////////////////
21
22// SR save (used by _it_mask() / it_restore()
23unsigned int _status_register_save[NB_CLUSTERS*NB_PROCS_MAX];
24
25///////////////////////////////////////////////////////////////////////////////////
26//       _get_sched()
27// Access CP0 and returns a pointer (virtual address) on the calling
28// processor scheduler.
29///////////////////////////////////////////////////////////////////////////////////
30static_scheduler_t* _get_sched() 
31{
32    unsigned int vaddr;
33    asm volatile(
34            "mfc0    %0,   $22    \n" 
35            : "=r"(vaddr) );
36    return (static_scheduler_t*)vaddr;
37}
38
39///////////////////////////////////////////////////////////////////////////////////
40//       _get_ptpr()
41// Access CP2 and returns PTPR register.
42///////////////////////////////////////////////////////////////////////////////////
43inline unsigned int _get_ptpr() 
44{
45    unsigned int ret;
46    asm volatile(
47            "mfc2    %0,        $0" 
48            : "=r"(ret));
49    return ret;
50}
51
52///////////////////////////////////////////////////////////////////////////////////
53//       _get_epc()
54// Access CP0 and returns EPC register.
55///////////////////////////////////////////////////////////////////////////////////
56inline unsigned int _get_epc() 
57{
58    unsigned int ret;
59    asm volatile("mfc0    %0,        $14" 
60            : "=r"(ret));
61    return ret;
62}
63
64///////////////////////////////////////////////////////////////////////////////////
65//       _get_bar()
66// Access CP0 and returns BAR register.
67///////////////////////////////////////////////////////////////////////////////////
68inline unsigned int _get_bvar() 
69{
70    unsigned int ret;
71    asm volatile(
72            "mfc0    %0,        $8" 
73            : "=r"(ret));
74    return ret;
75}
76
77///////////////////////////////////////////////////////////////////////////////////
78//       _get_cr()
79// Access CP0 and returns CR register.
80///////////////////////////////////////////////////////////////////////////////////
81inline unsigned int _get_cause() 
82{
83    unsigned int ret;
84    asm volatile("mfc0    %0,        $13" 
85            : "=r"(ret));
86    return ret;
87}
88
89///////////////////////////////////////////////////////////////////////////////////
90//       _get_sr()
91// Access CP0 and returns SR register.
92///////////////////////////////////////////////////////////////////////////////////
93inline unsigned int _get_sr() 
94{
95    unsigned int ret;
96    asm volatile(
97            "mfc0    %0,        $12" 
98            : "=r"(ret));
99    return ret;
100}
101
102///////////////////////////////////////////////////////////////////////////////////
103//    _it_mask()
104// Access CP0 and mask IRQs
105// This function uses a global _status_register_save array
106// This function is NOT USED and NOT TESTED
107///////////////////////////////////////////////////////////////////////////////////
108inline void _it_mask() 
109{
110    unsigned int sr_value;
111    unsigned int proc_id;
112    asm volatile(
113            "li      $3,        0xFFFFFFFE    \n"
114            "mfc0    %0,        $12           \n"
115            "and     $3,        $3, %0        \n"
116            "mtc0    $3,        $12           \n"
117            "mfc0    %1,        $15, 1        \n"
118            : "=r"(sr_value), "=r"(proc_id)
119            : 
120            : "$3");
121    _status_register_save[proc_id] = sr_value;
122}
123
124///////////////////////////////////////////////////////////////////////////////////
125//    _it_restore()
126// Access CP0 and enable IRQs
127// This function uses a global _status_register_save array
128// This function is NOT USED and NOT TESTED
129///////////////////////////////////////////////////////////////////////////////////
130inline void _it_restore() 
131{
132    unsigned int proc_id;
133    // get the processor id to index the _status_register_save table
134    asm volatile("mfc0 %0, $15, 1" : "=r" (proc_id));
135    // restore the saved value into the status register
136    asm volatile("mtc0  %0, $12" : : "r" (_status_register_save[proc_id]));
137}
138
139///////////////////////////////////////////////////////////////////////////////////
140//    _it_disable()
141// Access CP0 and disables IRQs
142///////////////////////////////////////////////////////////////////////////////////
143inline void _it_disable() 
144{
145    asm volatile(
146            "li      $3,        0xFFFFFFFE    \n"
147            "mfc0    $4,        $12           \n"
148            "and     $3,        $3, $4        \n"
149            "mtc0    $3,        $12           \n"
150            ::: "$3", "$4");
151}
152
153///////////////////////////////////////////////////////////////////////////////////
154//    _it_enable()
155// Access CP0 and enables IRQs
156///////////////////////////////////////////////////////////////////////////////////
157inline void _it_enable() 
158{
159    asm volatile(
160            "li      $3,        0x00000001    \n"
161            "mfc0    $4,        $12           \n"
162            "or      $3,        $3, $4        \n"
163            "mtc0    $3,        $12           \n"
164            ::: "$3", "$4");
165}
166
167////////////////////////////////////////////////////////////////////////////
168//    _get_lock()
169// Takes a lock with an ll/sc atomic access.
170// A pseudo random delay is introduced before retry in case of miss
171// (delay average value = 100 cycles)
172////////////////////////////////////////////////////////////////////////////
173inline void _get_lock(unsigned int * plock) 
174{
175    register unsigned int delay = ( _proctime() ^ _procid() << 4) & 0xFF;
176
177    asm volatile (
178            "_lock_llsc:             \n"
179            "ll   $2,    0(%0)       \n" /* $2 <= _ioc_lock current value */
180            "bnez $2,    _lock_delay \n" /* delay if _ioc_lock already taken */
181            "li   $3,    1           \n" /* $3 <= argument for sc */
182            "sc   $3,    0(%0)       \n" /* try to set _ioc_lock */
183            "bnez $3,    _lock_ok    \n" /* exit if atomic */
184            "_lock_delay:            \n"
185            "move $4,    %1          \n" /* $4 <= delay */
186            "_lock_loop:             \n"
187            "addi $4,    $4,    -1   \n" /* $4 <= $4 - 1 */
188            "beqz $4,    _lock_loop  \n" /* test end delay */
189            "j           _lock_llsc  \n" /* retry */
190            "_lock_ok:               \n"
191            :
192            :"r"(plock), "r"(delay)
193            :"$2", "$3", "$4");
194}
195
196////////////////////////////////////////////////////////////////////////////
197// _release_lock()
198////////////////////////////////////////////////////////////////////////////
199inline void _release_lock(unsigned int * plock) 
200{
201    asm volatile (
202            "sync\n" /* necessary because of the consistency model in tsar */
203            );
204    *plock = 0;
205}
206
207////////////////////////////////////////////////////////////////////////////
208//    _puts()
209// display a string on TTY0 / used for system code debug and log
210////////////////////////////////////////////////////////////////////////////
211void _puts(char * buffer) 
212{
213    unsigned int * tty_address = (unsigned int *) &seg_tty_base;
214    unsigned int n;
215
216    for (n = 0; n < 100; n++) 
217    {
218        if (buffer[n] == 0)  break; 
219        tty_address[TTY_WRITE] = (unsigned int) buffer[n];
220    }
221}
222
223////////////////////////////////////////////////////////////////////////////
224//    _putx()
225// display a 32 bits unsigned int as an hexadecimal string on TTY0
226////////////////////////////////////////////////////////////////////////////
227void _putx(unsigned int val) 
228{
229    static const char HexaTab[] = "0123456789ABCDEF";
230    char buf[11];
231    unsigned int c;
232
233    buf[0] = '0';
234    buf[1] = 'x';
235    buf[10] = 0;
236
237    for (c = 0; c < 8; c++) 
238    { 
239        buf[9 - c] = HexaTab[val & 0xF];
240        val = val >> 4;
241    }
242    _puts(buf);
243}
244
245////////////////////////////////////////////////////////////////////////////
246//    _putl()
247// display a 64 bits unsigned long as an hexadecimal string on TTY0
248////////////////////////////////////////////////////////////////////////////
249void _putl(paddr_t val) 
250{
251    static const char HexaTab[] = "0123456789ABCDEF";
252    char buf[19];
253    unsigned int c;
254
255    buf[0] = '0';
256    buf[1] = 'x';
257    buf[18] = 0;
258
259    for (c = 0; c < 16; c++) 
260    { 
261        buf[17 - c] = HexaTab[(unsigned int)val & 0xF];
262        val = val >> 4;
263    }
264    _puts(buf);
265}
266
267////////////////////////////////////////////////////////////////////////////
268//    _putd()
269// display an int (decimal) on TTY0 / used for system code debug and log
270////////////////////////////////////////////////////////////////////////////
271void _putd(unsigned int val) {
272    static const char DecTab[] = "0123456789";
273    char buf[11];
274    unsigned int i;
275    unsigned int first;
276
277    buf[10] = 0;
278
279    for (i = 0; i < 10; i++) {
280        if ((val != 0) || (i == 0)) {
281            buf[9 - i] = DecTab[val % 10];
282            first = 9 - i;
283        }
284        else {
285            break;
286        }
287        val /= 10;
288    }
289    _puts(&buf[first]);
290}
291
292////////////////////////////////////////////////////////////////////////////
293//    _strncmp()
294// compare two strings s1 & s2 (no more than n characters)
295////////////////////////////////////////////////////////////////////////////
296unsigned int _strncmp(const char * s1, const char * s2, unsigned int n) {
297    unsigned int i;
298    for (i = 0; i < n; i++) {
299        if (s1[i] != s2[i]) {
300            return 1;
301        }
302        if (s1[i] == 0) {
303            break;
304        }
305    }
306    return 0;
307}
308
309////////////////////////////////////////////////////////////////////////////
310//        _dcache_buf_invalidate()
311// Invalidate all data cache lines corresponding to a memory
312// buffer (identified by an address and a size).
313////////////////////////////////////////////////////////////////////////////
314void _dcache_buf_invalidate(const void * buffer, unsigned int size) {
315    unsigned int i;
316    unsigned int tmp;
317    unsigned int line_size;
318
319    // compute data cache line size based on config register (bits 12:10)
320    asm volatile("mfc0 %0, $16, 1" : "=r" (tmp));
321    tmp = ((tmp >> 10) & 0x7);
322    line_size = 2 << tmp;
323
324    // iterate on cache lines
325    for (i = 0; i < size; i += line_size) {
326        asm volatile(
327                " cache %0, %1"
328                :
329                :"i" (0x11), "R" (*((unsigned char *) buffer + i))
330                );
331    }
332}
333
334/////////////////////////////////////////////////////////////////////////////
335//      _get_task_slot()
336// This function returns the content of a context slot
337// for the task identified by the ltid argument (local index).
338/////////////////////////////////////////////////////////////////////////////
339unsigned int _get_task_slot( unsigned int ltid,
340                             unsigned int slot )
341{
342    static_scheduler_t* psched  = _get_sched();
343    return psched->context[ltid][slot];
344}
345
346/////////////////////////////////////////////////////////////////////////////
347//      _set_task_slot()
348// This function updates the content of a context slot
349// for the task identified by the ltid argument (local index).
350/////////////////////////////////////////////////////////////////////////////
351void _set_task_slot( unsigned int ltid,
352                     unsigned int slot,
353                     unsigned int value )
354{
355    static_scheduler_t* psched  = _get_sched();
356    psched->context[ltid][slot] = value;
357}
358
359/////////////////////////////////////////////////////////////////////////////
360//      _get_context_slot()
361// This function returns the content of a context slot
362// for the running task (defined by the scheduler current field).
363/////////////////////////////////////////////////////////////////////////////
364unsigned int _get_context_slot( unsigned int slot )
365{
366    static_scheduler_t* psched  = _get_sched();
367    unsigned int        task_id = psched->current;
368    return psched->context[task_id][slot];
369}
370
371/////////////////////////////////////////////////////////////////////////////
372//      _set_context_slot()
373// This function updates the content of a context slot for the running task.
374/////////////////////////////////////////////////////////////////////////////
375void _set_context_slot( unsigned int slot,
376                       unsigned int value )
377{
378    static_scheduler_t* psched  = _get_sched();
379    unsigned int        task_id = psched->current;
380    psched->context[task_id][slot] = value;
381}
382
383/////////////////////////////////////////////////////////////////////////////
384//      access functions to mapping_info data structure
385/////////////////////////////////////////////////////////////////////////////
386mapping_cluster_t * _get_cluster_base(mapping_header_t * header) 
387{
388    return (mapping_cluster_t *) ((char *) header +
389            MAPPING_HEADER_SIZE);
390}
391/////////////////////////////////////////////////////////////////////////////
392mapping_pseg_t * _get_pseg_base(mapping_header_t * header) 
393{
394    return (mapping_pseg_t *) ((char *) header +
395            MAPPING_HEADER_SIZE +
396            MAPPING_CLUSTER_SIZE * header->clusters);
397}
398/////////////////////////////////////////////////////////////////////////////
399mapping_vspace_t * _get_vspace_base(mapping_header_t * header) 
400{
401    return (mapping_vspace_t *)  ((char *) header +
402            MAPPING_HEADER_SIZE +
403            MAPPING_CLUSTER_SIZE * header->clusters +
404            MAPPING_PSEG_SIZE * header->psegs);
405}
406
407
408/////////////////////////////////////////////////////////////////////////////
409mapping_vseg_t * _get_vseg_base(mapping_header_t * header)
410{
411    return (mapping_vseg_t *) ((char *) header +
412            MAPPING_HEADER_SIZE +
413            MAPPING_CLUSTER_SIZE * header->clusters +
414            MAPPING_PSEG_SIZE * header->psegs +
415            MAPPING_VSPACE_SIZE * header->vspaces);
416}
417
418
419/////////////////////////////////////////////////////////////////////////////
420mapping_vobj_t * _get_vobj_base(mapping_header_t * header) 
421{
422    return (mapping_vobj_t *) ((char *) header +
423            MAPPING_HEADER_SIZE +
424            MAPPING_CLUSTER_SIZE * header->clusters +
425            MAPPING_PSEG_SIZE * header->psegs +
426            MAPPING_VSPACE_SIZE * header->vspaces +
427            MAPPING_VSEG_SIZE * header->vsegs );
428}
429
430
431/////////////////////////////////////////////////////////////////////////////
432mapping_task_t * _get_task_base(mapping_header_t * header) 
433{
434    return (mapping_task_t *) ((char *) header +
435            MAPPING_HEADER_SIZE +
436            MAPPING_CLUSTER_SIZE * header->clusters +
437            MAPPING_PSEG_SIZE * header->psegs +
438            MAPPING_VSPACE_SIZE * header->vspaces +
439            MAPPING_VOBJ_SIZE * header->vobjs +
440            MAPPING_VSEG_SIZE * header->vsegs);
441}
442
443
444// Local Variables:
445// tab-width: 4
446// c-basic-offset: 4
447// c-file-offsets:((innamespace . 0)(inline-open . 0))
448// indent-tabs-mode: nil
449// End:
450// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
451
Note: See TracBrowser for help on using the repository browser.