1 | /* |
---|
2 | * scheduler.c - Core scheduler implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016) |
---|
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 | #include <kernel_config.h> |
---|
25 | #include <hal_types.h> |
---|
26 | #include <hal_switch.h> |
---|
27 | #include <hal_irqmask.h> |
---|
28 | #include <hal_context.h> |
---|
29 | #include <printk.h> |
---|
30 | #include <list.h> |
---|
31 | #include <core.h> |
---|
32 | #include <thread.h> |
---|
33 | #include <chdev.h> |
---|
34 | #include <scheduler.h> |
---|
35 | |
---|
36 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
37 | // Extern global variables |
---|
38 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
39 | |
---|
40 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c file |
---|
41 | extern uint32_t switch_save_sr[]; // allocated in kernel_init.c file |
---|
42 | |
---|
43 | //////////////////////////////// |
---|
44 | void sched_init( core_t * core ) |
---|
45 | { |
---|
46 | scheduler_t * sched = &core->scheduler; |
---|
47 | |
---|
48 | sched->u_threads_nr = 0; |
---|
49 | sched->k_threads_nr = 0; |
---|
50 | |
---|
51 | sched->current = CURRENT_THREAD; |
---|
52 | sched->idle = NULL; // initialized in kernel_init() |
---|
53 | sched->u_last = NULL; // initialized in sched_register_thread() |
---|
54 | sched->k_last = NULL; // initialized in sched_register_thread() |
---|
55 | |
---|
56 | // initialise threads lists |
---|
57 | list_root_init( &sched->u_root ); |
---|
58 | list_root_init( &sched->k_root ); |
---|
59 | |
---|
60 | sched->req_ack_pending = false; // no pending request |
---|
61 | |
---|
62 | } // end sched_init() |
---|
63 | |
---|
64 | //////////////////////////////////////////// |
---|
65 | void sched_register_thread( core_t * core, |
---|
66 | thread_t * thread ) |
---|
67 | { |
---|
68 | scheduler_t * sched = &core->scheduler; |
---|
69 | thread_type_t type = thread->type; |
---|
70 | |
---|
71 | // take lock protecting sheduler lists |
---|
72 | spinlock_lock( &sched->lock ); |
---|
73 | |
---|
74 | if( type == THREAD_USER ) |
---|
75 | { |
---|
76 | list_add_last( &sched->u_root , &thread->sched_list ); |
---|
77 | sched->u_threads_nr++; |
---|
78 | if( sched->u_last == NULL ) sched->u_last = &thread->sched_list; |
---|
79 | } |
---|
80 | else // kernel thread |
---|
81 | { |
---|
82 | list_add_last( &sched->k_root , &thread->sched_list ); |
---|
83 | sched->k_threads_nr++; |
---|
84 | if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; |
---|
85 | } |
---|
86 | |
---|
87 | // release lock |
---|
88 | spinlock_unlock( &sched->lock ); |
---|
89 | |
---|
90 | } // end sched_register_thread() |
---|
91 | |
---|
92 | ////////////////////////////////////////////// |
---|
93 | thread_t * sched_select( scheduler_t * sched ) |
---|
94 | { |
---|
95 | thread_t * thread; |
---|
96 | list_entry_t * current; |
---|
97 | list_entry_t * last; |
---|
98 | |
---|
99 | // take lock protecting sheduler lists |
---|
100 | spinlock_lock( &sched->lock ); |
---|
101 | |
---|
102 | // first loop : scan the kernel threads list if not empty |
---|
103 | if( list_is_empty( &sched->k_root ) == false ) |
---|
104 | { |
---|
105 | last = sched->k_last; |
---|
106 | current = sched->k_last; |
---|
107 | do |
---|
108 | { |
---|
109 | // get next entry in kernel list |
---|
110 | current = list_next( &sched->k_root , current ); |
---|
111 | |
---|
112 | // skip the root that does not contain a thread |
---|
113 | if( current == NULL ) current = sched->k_root.next; |
---|
114 | |
---|
115 | // get thread pointer for this entry |
---|
116 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
---|
117 | |
---|
118 | // analyse kernel thread type |
---|
119 | switch( thread->type ) |
---|
120 | { |
---|
121 | case THREAD_IDLE: // skip IDLE thread |
---|
122 | break; |
---|
123 | |
---|
124 | case THREAD_RPC: // RPC thread if non blocked and FIFO non-empty |
---|
125 | if( (thread->blocked == 0) && |
---|
126 | (local_fifo_is_empty( &LOCAL_CLUSTER->rpc_fifo ) == 0) ) |
---|
127 | { |
---|
128 | spinlock_unlock( &sched->lock ); |
---|
129 | return thread; |
---|
130 | } |
---|
131 | break; |
---|
132 | |
---|
133 | default: // DEV thread if non blocked and waiting queue non empty |
---|
134 | if( (thread->blocked == 0) && |
---|
135 | (xlist_is_empty( XPTR( local_cxy , &thread->chdev->wait_root)) == 0) ) |
---|
136 | { |
---|
137 | spinlock_unlock( &sched->lock ); |
---|
138 | return thread; |
---|
139 | } |
---|
140 | break; |
---|
141 | } // end switch type |
---|
142 | } |
---|
143 | while( current != last ); |
---|
144 | } |
---|
145 | |
---|
146 | // second loop : scan the user threads list if not empty |
---|
147 | if( list_is_empty( &sched->u_root ) == false ) |
---|
148 | { |
---|
149 | last = sched->u_last; |
---|
150 | current = sched->u_last; |
---|
151 | do |
---|
152 | { |
---|
153 | // get next entry in user list |
---|
154 | current = list_next( &sched->u_root , current ); |
---|
155 | |
---|
156 | // skip the root that does not contain a thread |
---|
157 | if( current == NULL ) current = sched->u_root.next; |
---|
158 | |
---|
159 | // get thread pointer for this entry |
---|
160 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
---|
161 | |
---|
162 | // return thread if runnable |
---|
163 | if( thread->blocked == 0 ) |
---|
164 | { |
---|
165 | spinlock_unlock( &sched->lock ); |
---|
166 | return thread; |
---|
167 | } |
---|
168 | } |
---|
169 | while( current != last ); |
---|
170 | } |
---|
171 | |
---|
172 | // third : return idle thread if no runnable thread |
---|
173 | spinlock_unlock( &sched->lock ); |
---|
174 | return sched->idle; |
---|
175 | |
---|
176 | } // end sched_select() |
---|
177 | |
---|
178 | /////////////////////////////////////////// |
---|
179 | void sched_handle_requests( core_t * core ) |
---|
180 | { |
---|
181 | list_entry_t * iter; |
---|
182 | thread_t * thread; |
---|
183 | |
---|
184 | // printk("\n@@@ %s : current thread %x enter at cycle %d\n", |
---|
185 | // __FUNCTION__ , CURRENT_THREAD , hal_time_stamp() ); |
---|
186 | |
---|
187 | scheduler_t * sched = &core->scheduler; |
---|
188 | |
---|
189 | // take lock protecting threads lists |
---|
190 | spinlock_lock( &sched->lock ); |
---|
191 | |
---|
192 | // scan all user threads |
---|
193 | LIST_FOREACH( &sched->u_root , iter ) |
---|
194 | { |
---|
195 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
196 | |
---|
197 | // handle REQ_ACK |
---|
198 | if( thread->flags & THREAD_FLAG_REQ_ACK ) |
---|
199 | { |
---|
200 | // check thread blocked |
---|
201 | assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , |
---|
202 | __FUNCTION__ , "thread not blocked" ); |
---|
203 | |
---|
204 | // decrement response counter |
---|
205 | hal_atomic_add( thread->ack_rsp_count , -1 ); |
---|
206 | |
---|
207 | // reset REQ_ACK in thread descriptor |
---|
208 | thread_reset_req_ack( thread ); |
---|
209 | } |
---|
210 | |
---|
211 | // handle REQ_DELETE |
---|
212 | if( thread->flags & THREAD_FLAG_REQ_DELETE ) |
---|
213 | { |
---|
214 | |
---|
215 | sched_dmsg("\n[DBG] %s : current thread %x delete thread %x at cycle %d\n", |
---|
216 | __FUNCTION__ , CURRENT_THREAD , thread , hal_time_stamp() ); |
---|
217 | |
---|
218 | // release FPU if required |
---|
219 | if( thread->core->fpu_owner == thread ) thread->core->fpu_owner = NULL; |
---|
220 | |
---|
221 | // detach thread from parent if attached |
---|
222 | if( (thread->flags & THREAD_FLAG_DETACHED) == 0 ) |
---|
223 | thread_child_parent_unlink( thread->parent , XPTR( local_cxy , thread ) ); |
---|
224 | |
---|
225 | // detach thread from process |
---|
226 | process_remove_thread( thread ); |
---|
227 | |
---|
228 | // remove thread from scheduler |
---|
229 | list_unlink( &thread->sched_list ); |
---|
230 | sched->u_threads_nr--; |
---|
231 | if( sched->u_threads_nr == 0 ) sched->u_last = NULL; |
---|
232 | |
---|
233 | // release memory allocated to thread |
---|
234 | thread_destroy( thread ); |
---|
235 | |
---|
236 | // destroy process descriptor if no more threads |
---|
237 | if (thread->process->th_nr == 0) process_destroy( thread->process ); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | // release lock |
---|
242 | spinlock_unlock( &sched->lock ); |
---|
243 | |
---|
244 | // printk("\n@@@ %s : current thread %x exit at cycle %d\n", |
---|
245 | // __FUNCTION__ , CURRENT_THREAD , hal_time_stamp() ); |
---|
246 | |
---|
247 | } // end sched_handle_requests() |
---|
248 | |
---|
249 | //////////////////////////////// |
---|
250 | void sched_yield( char * cause ) |
---|
251 | { |
---|
252 | thread_t * next; |
---|
253 | thread_t * current = CURRENT_THREAD; |
---|
254 | core_t * core = current->core; |
---|
255 | scheduler_t * sched = &core->scheduler; |
---|
256 | |
---|
257 | #if( CONFIG_SCHED_DEBUG & 0x1 ) |
---|
258 | if( hal_time_stamp() > CONFIG_SCHED_DEBUG ) sched_display( core->lid ); |
---|
259 | #endif |
---|
260 | |
---|
261 | // delay the yield if current thread has locks |
---|
262 | if( (current->local_locks != 0) || (current->remote_locks != 0) ) |
---|
263 | { |
---|
264 | current->flags |= THREAD_FLAG_SCHED; |
---|
265 | return; |
---|
266 | } |
---|
267 | |
---|
268 | // enter critical section / save SR in current thread context |
---|
269 | hal_disable_irq( ¤t->save_sr ); |
---|
270 | |
---|
271 | // loop on threads to select next thread |
---|
272 | next = sched_select( sched ); |
---|
273 | |
---|
274 | // check next thread attached to same core as the calling thread |
---|
275 | assert( (next->core == current->core), __FUNCTION__ , |
---|
276 | "next core != current core\n"); |
---|
277 | |
---|
278 | // check next thread not blocked when type != IDLE |
---|
279 | assert( (next->blocked == 0) || (next->type = THREAD_IDLE) , __FUNCTION__ , |
---|
280 | "next thread %x (%s) is blocked on core[%x,%d]\n", |
---|
281 | next->trdid , thread_type_str(next->type) , local_cxy , core->lid ); |
---|
282 | |
---|
283 | // switch contexts and update scheduler state if next != current |
---|
284 | if( next != current ) |
---|
285 | { |
---|
286 | |
---|
287 | sched_dmsg("\n[DBG] %s : core[%x,%d] / cause = %s\n" |
---|
288 | " thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n", |
---|
289 | __FUNCTION__, local_cxy, core->lid, cause, |
---|
290 | current, thread_type_str(current->type), current->process->pid, current->trdid, |
---|
291 | next , thread_type_str(next->type) , next->process->pid , next->trdid, |
---|
292 | (uint32_t)hal_get_cycles() ); |
---|
293 | |
---|
294 | // update scheduler |
---|
295 | sched->current = next; |
---|
296 | if( next->type == THREAD_USER ) sched->u_last = &next->sched_list; |
---|
297 | else sched->k_last = &next->sched_list; |
---|
298 | |
---|
299 | // handle FPU ownership |
---|
300 | if( next->type == THREAD_USER ) |
---|
301 | { |
---|
302 | if( next == current->core->fpu_owner ) hal_fpu_enable(); |
---|
303 | else hal_fpu_disable(); |
---|
304 | } |
---|
305 | |
---|
306 | // switch CPU from calling thread context to new thread context |
---|
307 | hal_do_cpu_switch( current->cpu_context, next->cpu_context ); |
---|
308 | } |
---|
309 | else |
---|
310 | { |
---|
311 | |
---|
312 | sched_dmsg("\n[DBG] %s : core[%x,%d] / cause = %s\n" |
---|
313 | " thread %x (%s) (%x,%x) continue / cycle %d\n", |
---|
314 | __FUNCTION__, local_cxy, core->lid, cause, |
---|
315 | current, thread_type_str(current->type), current->process->pid, current->trdid, |
---|
316 | (uint32_t)hal_get_cycles() ); |
---|
317 | |
---|
318 | } |
---|
319 | |
---|
320 | // handle pending requests for all threads executing on this core. |
---|
321 | sched_handle_requests( core ); |
---|
322 | |
---|
323 | // exit critical section / restore SR from next thread context |
---|
324 | hal_restore_irq( next->save_sr ); |
---|
325 | |
---|
326 | } // end sched_yield() |
---|
327 | |
---|
328 | |
---|
329 | /////////////////////////////// |
---|
330 | void sched_display( lid_t lid ) |
---|
331 | { |
---|
332 | list_entry_t * iter; |
---|
333 | thread_t * thread; |
---|
334 | uint32_t save_sr; |
---|
335 | |
---|
336 | if( lid >= LOCAL_CLUSTER->cores_nr ) |
---|
337 | { |
---|
338 | printk("\n[ERROR] in %s : illegal local index %d in cluster %x\n", |
---|
339 | __FUNCTION__ , lid , local_cxy ); |
---|
340 | return; |
---|
341 | } |
---|
342 | |
---|
343 | core_t * core = &LOCAL_CLUSTER->core_tbl[lid]; |
---|
344 | scheduler_t * sched = &core->scheduler; |
---|
345 | |
---|
346 | // get pointers on TXT0 chdev |
---|
347 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
348 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
349 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
350 | |
---|
351 | // get extended pointer on remote TXT0 chdev lock |
---|
352 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
353 | |
---|
354 | // get TXT0 lock in busy waiting mode |
---|
355 | remote_spinlock_lock_busy( lock_xp , &save_sr ); |
---|
356 | |
---|
357 | nolock_printk("\n***** scheduler state for core[%x,%d] at cycle %d\n" |
---|
358 | "kernel_threads = %d / user_threads = %d / current = (%x,%x)\n", |
---|
359 | local_cxy , core->lid, hal_time_stamp(), |
---|
360 | sched->k_threads_nr, sched->u_threads_nr, |
---|
361 | sched->current->process->pid , sched->current->trdid ); |
---|
362 | |
---|
363 | // display kernel threads |
---|
364 | LIST_FOREACH( &sched->k_root , iter ) |
---|
365 | { |
---|
366 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
367 | if (thread->type == THREAD_DEV) |
---|
368 | { |
---|
369 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n", |
---|
370 | thread_type_str( thread->type ), thread->process->pid, thread->trdid, |
---|
371 | thread, thread->blocked, thread->flags, thread->chdev->name ); |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X \n", |
---|
376 | thread_type_str( thread->type ), thread->process->pid, thread->trdid, |
---|
377 | thread, thread->blocked, thread->flags ); |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | // display user threads |
---|
382 | LIST_FOREACH( &sched->u_root , iter ) |
---|
383 | { |
---|
384 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
---|
385 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n", |
---|
386 | thread_type_str( thread->type ), thread->process->pid, thread->trdid, |
---|
387 | thread, thread->blocked, thread->flags ); |
---|
388 | } |
---|
389 | |
---|
390 | // release TXT0 lock |
---|
391 | remote_spinlock_unlock_busy( lock_xp , save_sr ); |
---|
392 | |
---|
393 | } // end sched_display() |
---|
394 | |
---|