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

Last change on this file since 714 was 714, checked in by alain, 9 years ago

Introduce the _sys_fbf_size() function.
Modify the _sys_fbf_alloc() / _sys_fbf_release() functions
to allow all threads of a given vspace to access the frame buffer.
(replace the lock by a multi-threads allocator.

  • Property svn:executable set to *
File size: 7.1 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 <sys_handler.h>
10#include <giet_config.h>
11#include <fat32.h>
12#include <hard_config.h>
13#include <utils.h>
14#include <tty0.h>
15#include <xcu_driver.h>
16#include <bdv_driver.h>
17
18/////////////////////////////////////////////////////////////////////////////////
19//     Extern variables and functions
20/////////////////////////////////////////////////////////////////////////////////
21
22// defined in giet_kernel/switch.s file
23extern void _thread_switch( thread_context_t* , thread_context_t* );
24
25// allocated in boot.c or kernel_init.c files
26extern static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX];
27
28// allocated in kernel_init.c file
29extern fat_desc_t  _fat;
30
31//////////////////////////////////////////////////////////////////
32// This function is called by the _ctx_switch() function.
33// It desactivates a thread that received a KILL signal.
34// We must release all ressources allocated to the thread
35// before the actual desactivation, that uses NORUN_MASK_THREAD.
36//////////////////////////////////////////////////////////////////
37static void _ctx_kill_thread( unsigned int x,
38                              unsigned int y,
39                              unsigned int p,
40                              unsigned int ltid )
41{
42    // get scheduler address
43    static_scheduler_t* psched = _schedulers[x][y][p];
44
45    // pretend the thread to kill is the currently scheduled thread
46    // (required by the _sys_***_release() calls)
47    unsigned int cur_thread = psched->current;
48    psched->current = ltid;
49
50    // release BDV lock if taken and reset BDV peripheral
51    if ( psched->context[ltid].slot[CTX_LOCKS_ID] & LOCKS_MASK_BDV ) 
52    {
53        _bdv_set_register( BLOCK_DEVICE_STATUS , 0 );
54        _spin_lock_release( &_bdv_lock );
55    }
56
57    // release FAT lock if taken
58    if ( psched->context[ltid].slot[CTX_LOCKS_ID] & LOCKS_MASK_FAT ) 
59    {
60       
61        _spin_lock_release( &_fat.fat_lock );
62    }
63
64    // release FBF lock if taken
65    if ( psched->context[ltid].slot[CTX_LOCKS_ID] & LOCKS_MASK_FBF ) 
66    {
67        _sys_fbf_release();
68    }
69
70    // release private TTY terminal if required
71    if ( psched->context[ltid].slot[CTX_TTY_ID] < NB_TTY_CHANNELS ) 
72        _sys_tty_release(); 
73
74    // release private TIM channel if required
75
76    if ( psched->context[ltid].slot[CTX_TIM_ID] < NB_TIM_CHANNELS )
77    {
78        _sys_tim_release();
79    }
80
81    // release private NIC_RX and CMA_RX channels if required
82    if ( psched->context[ltid].slot[CTX_NIC_RX_ID] < NB_NIC_CHANNELS )
83    {
84        _sys_nic_release( 1 );
85    }
86
87    // release private NIC_TX and CMA_TX channels if required
88    if ( psched->context[ltid].slot[CTX_NIC_TX_ID] < NB_NIC_CHANNELS )
89    {
90        _sys_nic_release( 0 );
91    }
92
93    // release private FBF_CMA channel if required
94    if ( psched->context[ltid].slot[CTX_CMA_FB_ID] < NB_CMA_CHANNELS )
95    {
96        _sys_fbf_cma_release();
97    }
98
99    // restore scheduled thread index
100    psched->current = cur_thread;
101
102    // set NORUN_MASK_THREAD bit to desactivate the target thread
103    psched->context[ltid].slot[CTX_NORUN_ID] = NORUN_MASK_THREAD;
104
105} // end _ctx_kill_thread()
106
107
108//////////////////
109void _ctx_switch() 
110{
111    unsigned int gpid       = _get_procid();
112    unsigned int cluster_xy = gpid >> P_WIDTH;
113    unsigned int x          = cluster_xy >> Y_WIDTH;
114    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
115    unsigned int p          = gpid & ((1<<P_WIDTH)-1);
116
117    unsigned int ltid;     // index for loops on threads in scheduler
118
119    // get calling thread scheduler address
120    static_scheduler_t* psched = (static_scheduler_t*)_get_sched();
121
122    // get number of threads allocated to scheduler
123    unsigned int threads = psched->threads;
124
125    // get current thread ltid
126    unsigned int curr_thread_id = psched->current;
127
128    // first loop on threads: handle all pending KILL signals
129    for ( ltid = 0 ; ltid < threads ; ltid++ )
130    {
131        if ( psched->context[ltid].slot[CTX_SIGS_ID] & SIGS_MASK_KILL )
132        {
133            // acknowledge KILL signal
134            _atomic_and( &psched->context[ltid].slot[CTX_SIGS_ID], ~SIGS_MASK_KILL );
135
136            // desactivate the killed thread
137            _ctx_kill_thread( x , y , p , ltid );
138        }
139    }
140
141    // second loop: select next thread using a round-robin policy
142    unsigned int next_thread_id;
143    unsigned int found = 0;
144    for ( ltid = curr_thread_id + 1 ; ltid < (curr_thread_id + 1 + threads) ; ltid++ ) 
145    {
146        next_thread_id = ltid % threads;
147
148        // test if the thread is runable
149        if ( psched->context[next_thread_id].slot[CTX_NORUN_ID] == 0 )
150        {
151            found = 1;
152            break;
153        }
154    }
155
156    // launch idle_thread if no runable thread
157    if ( found == 0 ) next_thread_id = IDLE_THREAD_INDEX;
158
159    if ( curr_thread_id != next_thread_id )  // actual thread switch required
160    {
161
162#if GIET_DEBUG_SWITCH
163unsigned int x = cluster_xy >> Y_WIDTH;
164unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1);
165if ( (_get_proctime() > GIET_DEBUG_SWITCH) && (x == 0) && (y == 0) && (p == 0) )
166_printf("\n[DEBUG SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n",
167        curr_thread_id, next_thread_id, x, y , p, _get_proctime() );
168#endif
169
170        thread_context_t* curr_ctx_vaddr = &(psched->context[curr_thread_id]);
171        thread_context_t* next_ctx_vaddr = &(psched->context[next_thread_id]);
172
173        // reset TICK timer counter.
174        _xcu_timer_reset_cpt( cluster_xy, p );
175
176        // set current thread index
177        psched->current = next_thread_id;
178
179        // makes context switch
180        _thread_switch( curr_ctx_vaddr , next_ctx_vaddr );
181    }
182} //end _ctx_switch()
183
184
185///////////////////
186void _idle_thread() 
187{
188    unsigned int gpid       = _get_procid();
189    unsigned int cluster_xy = gpid >> P_WIDTH;
190    unsigned int x          = cluster_xy >> Y_WIDTH;
191    unsigned int y          = cluster_xy & ((1<<Y_WIDTH)-1);
192    unsigned int p          = gpid & ((1<<P_WIDTH)-1);
193
194    while(1)
195    {
196        // initialize counter
197        unsigned int count = GIET_IDLE_THREAD_PERIOD;
198
199        // decounting loop
200        asm volatile(
201                "move   $3,   %0              \n"
202                "_idle_thread_loop:             \n"
203                "addi   $3,   $3,   -1        \n"
204                "bnez   $3,   _idle_thread_loop \n"
205                "nop                          \n"
206                :
207                : "r"(count)
208                : "$3" ); 
209
210        // warning message
211        _printf("\n[GIET WARNING] Processor[%d,%d,%d] still idle at cycle %d",
212                x , y , p , _get_proctime() );
213    }
214} // end ctx_idle()
215
216
217////////////////
218void _ctx_eret() 
219{
220    asm volatile("eret");
221}
222
223
224// Local Variables:
225// tab-width: 4
226// c-basic-offset: 4
227// c-file-offsets:((innamespace . 0)(inline-open . 0))
228// indent-tabs-mode: nil
229// End:
230// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
231
Note: See TracBrowser for help on using the repository browser.