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