source: soft/giet_vm/giet_kernel/ctx_handler.c @ 440

Last change on this file since 440 was 440, checked in by alain, 10 years ago

Introducing dynamic allocation of peripheral channels (NIC, TTY, CMA, TIM)
Intoducing a kernel function for all system calls: No more direct call
to the peripheral drivers.

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////
2// File     : ctx_handler.c
3// Date     : 01/04/2012
4// Authors  : alain greiner & joel porquet
5// Copyright (c) UPMC-LIP6
6//////////////////////////////////////////////////////////////////////////////////
7
8#include <ctx_handler.h>
9#include <giet_config.h>
10#include <utils.h>
11#include <kernel_utils.h>
12#include <xcu_driver.h>
13
14////////// defined in giet_kernel/switch.s file /////////
15extern void _task_switch(unsigned int *, unsigned int *);
16
17//////////////////
18void _ctx_switch() 
19{
20    unsigned int gpid       = _get_procid();
21    unsigned int cluster_xy = gpid >> P_WIDTH;
22    unsigned int lpid       = gpid & ((1<<P_WIDTH)-1);
23
24    // get scheduler address
25    static_scheduler_t* psched = (static_scheduler_t*)_get_sched();
26
27    // get number of tasks allocated to scheduler
28    unsigned int tasks = psched->tasks;
29
30    // get current task index
31    unsigned int curr_task_id = psched->current;
32
33    // select the next task using a round-robin policy
34    unsigned int next_task_id;
35    unsigned int tid;
36    unsigned int found = 0;
37
38    for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) 
39    {
40        next_task_id = tid % tasks;
41        // test if the task is runable
42        if ( psched->context[next_task_id][CTX_RUN_ID] ) 
43        {
44            found = 1;
45            break;
46        }
47    }
48
49    // launch "idle" task if no runable task
50    if (found == 0) 
51    {
52        next_task_id = IDLE_TASK_INDEX;
53    }
54
55#if GIET_DEBUG_SWITCH
56unsigned int x = cluster_xy >> Y_WIDTH;
57unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1);
58
59_printf("\n[TASK SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n",
60        curr_task_id, next_task_id, x, y , lpid, _get_proctime() );
61#endif
62
63    if (curr_task_id != next_task_id)  // actual task switch required
64    {
65        unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]);
66        unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]);
67
68        // reset timer counter.
69        // In all clusters, the first NB_PROCS_MAX
70        // timers are system timers (TICK)
71
72#if USE_XCU
73        _xcu_timer_reset_cpt( cluster_xy, lpid );
74#else
75        _timer_reset_cpt( cluster_xy, lpid); 
76#endif
77
78        // set current task index
79        psched->current = next_task_id;
80
81        // makes context switch
82        _task_switch(curr_ctx_vaddr, next_ctx_vaddr);
83    }
84} //end _ctx_switch()
85
86/////////////////
87void _idle_task() 
88{
89    unsigned int gpid       = _get_procid();
90    unsigned int cluster_xy = gpid >> P_WIDTH;
91    unsigned int x          = cluster_xy >> Y_WIDTH;
92    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
93    unsigned int p          = gpid & ((1<<P_WIDTH)-1);
94
95    while(1)
96    {
97        // initialize counter
98        unsigned int count = GIET_IDLE_TASK_PERIOD;
99
100        // decounting loop
101        asm volatile(
102                "move   $3,   %0              \n"
103                "_idle_task_loop:             \n"
104                "addi   $3,   $3,   -1        \n"
105                "bnez   $3,   _idle_task_loop \n"
106                "nop                          \n"
107                :
108                : "r"(count)
109                : "$3" ); 
110
111        // warning message
112        _puts("\n[GIET WARNING] Processor[");
113        _putd( x );
114        _puts(",");
115        _putd( y );
116        _puts(",");
117        _putd( p );
118        _puts("] still idle at cycle ");
119        _putd( _get_proctime() );
120        _puts("\n");
121    }
122} // end ctx_idle()
123
124
125////////////////
126void _ctx_eret() 
127{
128    asm volatile("eret");
129}
130
131
132// Local Variables:
133// tab-width: 4
134// c-basic-offset: 4
135// c-file-offsets:((innamespace . 0)(inline-open . 0))
136// indent-tabs-mode: nil
137// End:
138// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
139
Note: See TracBrowser for help on using the repository browser.