1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // kernel.c -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Jun 20 17:21:23 2019 |
---|
13 | // Update Count : 25 |
---|
14 | // |
---|
15 | |
---|
16 | //C Includes |
---|
17 | #include <stddef.h> |
---|
18 | #include <errno.h> |
---|
19 | #include <string.h> |
---|
20 | extern "C" { |
---|
21 | #include <stdio.h> |
---|
22 | #include <fenv.h> |
---|
23 | #include <sys/resource.h> |
---|
24 | #include <signal.h> |
---|
25 | #include <unistd.h> |
---|
26 | } |
---|
27 | |
---|
28 | //CFA Includes |
---|
29 | #include "time.hfa" |
---|
30 | #include "kernel_private.hfa" |
---|
31 | #include "preemption.hfa" |
---|
32 | #include "startup.hfa" |
---|
33 | |
---|
34 | //Private includes |
---|
35 | #define __CFA_INVOKE_PRIVATE__ |
---|
36 | #include "invoke.h" |
---|
37 | |
---|
38 | //----------------------------------------------------------------------------- |
---|
39 | // Some assembly required |
---|
40 | #if defined( __i386 ) |
---|
41 | #define CtxGet( ctx ) \ |
---|
42 | __asm__ volatile ( \ |
---|
43 | "movl %%esp,%0\n"\ |
---|
44 | "movl %%ebp,%1\n"\ |
---|
45 | : "=rm" (ctx.SP),\ |
---|
46 | "=rm" (ctx.FP) \ |
---|
47 | ) |
---|
48 | |
---|
49 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
50 | // fcw : X87 FPU control word (preserved across function calls) |
---|
51 | #define __x87_store \ |
---|
52 | uint32_t __mxcr; \ |
---|
53 | uint16_t __fcw; \ |
---|
54 | __asm__ volatile ( \ |
---|
55 | "stmxcsr %0\n" \ |
---|
56 | "fnstcw %1\n" \ |
---|
57 | : "=m" (__mxcr),\ |
---|
58 | "=m" (__fcw) \ |
---|
59 | ) |
---|
60 | |
---|
61 | #define __x87_load \ |
---|
62 | __asm__ volatile ( \ |
---|
63 | "fldcw %1\n" \ |
---|
64 | "ldmxcsr %0\n" \ |
---|
65 | ::"m" (__mxcr),\ |
---|
66 | "m" (__fcw) \ |
---|
67 | ) |
---|
68 | |
---|
69 | #elif defined( __x86_64 ) |
---|
70 | #define CtxGet( ctx ) \ |
---|
71 | __asm__ volatile ( \ |
---|
72 | "movq %%rsp,%0\n"\ |
---|
73 | "movq %%rbp,%1\n"\ |
---|
74 | : "=rm" (ctx.SP),\ |
---|
75 | "=rm" (ctx.FP) \ |
---|
76 | ) |
---|
77 | |
---|
78 | #define __x87_store \ |
---|
79 | uint32_t __mxcr; \ |
---|
80 | uint16_t __fcw; \ |
---|
81 | __asm__ volatile ( \ |
---|
82 | "stmxcsr %0\n" \ |
---|
83 | "fnstcw %1\n" \ |
---|
84 | : "=m" (__mxcr),\ |
---|
85 | "=m" (__fcw) \ |
---|
86 | ) |
---|
87 | |
---|
88 | #define __x87_load \ |
---|
89 | __asm__ volatile ( \ |
---|
90 | "fldcw %1\n" \ |
---|
91 | "ldmxcsr %0\n" \ |
---|
92 | :: "m" (__mxcr),\ |
---|
93 | "m" (__fcw) \ |
---|
94 | ) |
---|
95 | |
---|
96 | |
---|
97 | #elif defined( __ARM_ARCH ) |
---|
98 | #define CtxGet( ctx ) __asm__ ( \ |
---|
99 | "mov %0,%%sp\n" \ |
---|
100 | "mov %1,%%r11\n" \ |
---|
101 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
102 | #else |
---|
103 | #error unknown hardware architecture |
---|
104 | #endif |
---|
105 | |
---|
106 | //----------------------------------------------------------------------------- |
---|
107 | //Start and stop routine for the kernel, declared first to make sure they run first |
---|
108 | static void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) )); |
---|
109 | static void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) )); |
---|
110 | |
---|
111 | //----------------------------------------------------------------------------- |
---|
112 | // Kernel storage |
---|
113 | KERNEL_STORAGE(cluster, mainCluster); |
---|
114 | KERNEL_STORAGE(processor, mainProcessor); |
---|
115 | KERNEL_STORAGE(thread_desc, mainThread); |
---|
116 | KERNEL_STORAGE(__stack_t, mainThreadCtx); |
---|
117 | |
---|
118 | cluster * mainCluster; |
---|
119 | processor * mainProcessor; |
---|
120 | thread_desc * mainThread; |
---|
121 | |
---|
122 | extern "C" { |
---|
123 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; |
---|
124 | } |
---|
125 | |
---|
126 | size_t __page_size = 0; |
---|
127 | |
---|
128 | //----------------------------------------------------------------------------- |
---|
129 | // Global state |
---|
130 | thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = { |
---|
131 | NULL, |
---|
132 | NULL, |
---|
133 | { 1, false, false } |
---|
134 | }; |
---|
135 | |
---|
136 | //----------------------------------------------------------------------------- |
---|
137 | // Struct to steal stack |
---|
138 | struct current_stack_info_t { |
---|
139 | __stack_t * storage; // pointer to stack object |
---|
140 | void *base; // base of stack |
---|
141 | void *limit; // stack grows towards stack limit |
---|
142 | void *context; // address of cfa_context_t |
---|
143 | }; |
---|
144 | |
---|
145 | void ?{}( current_stack_info_t & this ) { |
---|
146 | __stack_context_t ctx; |
---|
147 | CtxGet( ctx ); |
---|
148 | this.base = ctx.FP; |
---|
149 | |
---|
150 | rlimit r; |
---|
151 | getrlimit( RLIMIT_STACK, &r); |
---|
152 | size_t size = r.rlim_cur; |
---|
153 | |
---|
154 | this.limit = (void *)(((intptr_t)this.base) - size); |
---|
155 | this.context = &storage_mainThreadCtx; |
---|
156 | } |
---|
157 | |
---|
158 | //----------------------------------------------------------------------------- |
---|
159 | // Main thread construction |
---|
160 | |
---|
161 | void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) { |
---|
162 | stack.storage = info->storage; |
---|
163 | with(*stack.storage) { |
---|
164 | limit = info->limit; |
---|
165 | base = info->base; |
---|
166 | } |
---|
167 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage; |
---|
168 | *istorage |= 0x1; |
---|
169 | name = "Main Thread"; |
---|
170 | state = Start; |
---|
171 | starter = NULL; |
---|
172 | last = NULL; |
---|
173 | cancellation = NULL; |
---|
174 | } |
---|
175 | |
---|
176 | void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) { |
---|
177 | state = Start; |
---|
178 | self_cor{ info }; |
---|
179 | curr_cor = &self_cor; |
---|
180 | curr_cluster = mainCluster; |
---|
181 | self_mon.owner = &this; |
---|
182 | self_mon.recursion = 1; |
---|
183 | self_mon_p = &self_mon; |
---|
184 | next = NULL; |
---|
185 | |
---|
186 | node.next = NULL; |
---|
187 | node.prev = NULL; |
---|
188 | doregister(curr_cluster, this); |
---|
189 | |
---|
190 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
191 | } |
---|
192 | |
---|
193 | //----------------------------------------------------------------------------- |
---|
194 | // Processor coroutine |
---|
195 | void ?{}(processorCtx_t & this) { |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | // Construct the processor context of non-main processors |
---|
200 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) { |
---|
201 | (this.__cor){ info }; |
---|
202 | this.proc = proc; |
---|
203 | } |
---|
204 | |
---|
205 | static void start(processor * this); |
---|
206 | void ?{}(processor & this, const char * name, cluster & cltr) with( this ) { |
---|
207 | this.name = name; |
---|
208 | this.cltr = &cltr; |
---|
209 | terminated{ 0 }; |
---|
210 | do_terminate = false; |
---|
211 | preemption_alarm = NULL; |
---|
212 | pending_preemption = false; |
---|
213 | runner.proc = &this; |
---|
214 | |
---|
215 | idleLock{}; |
---|
216 | |
---|
217 | start( &this ); |
---|
218 | } |
---|
219 | |
---|
220 | void ^?{}(processor & this) with( this ){ |
---|
221 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) { |
---|
222 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this); |
---|
223 | |
---|
224 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED); |
---|
225 | wake( &this ); |
---|
226 | |
---|
227 | P( terminated ); |
---|
228 | verify( kernelTLS.this_processor != &this); |
---|
229 | } |
---|
230 | |
---|
231 | pthread_join( kernel_thread, NULL ); |
---|
232 | } |
---|
233 | |
---|
234 | void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) { |
---|
235 | this.name = name; |
---|
236 | this.preemption_rate = preemption_rate; |
---|
237 | ready_queue{}; |
---|
238 | ready_queue_lock{}; |
---|
239 | |
---|
240 | procs{ __get }; |
---|
241 | idles{ __get }; |
---|
242 | threads{ __get }; |
---|
243 | |
---|
244 | doregister(this); |
---|
245 | } |
---|
246 | |
---|
247 | void ^?{}(cluster & this) { |
---|
248 | unregister(this); |
---|
249 | } |
---|
250 | |
---|
251 | //============================================================================================= |
---|
252 | // Kernel Scheduling logic |
---|
253 | //============================================================================================= |
---|
254 | static void runThread(processor * this, thread_desc * dst); |
---|
255 | static void finishRunning(processor * this); |
---|
256 | static void halt(processor * this); |
---|
257 | |
---|
258 | //Main of the processor contexts |
---|
259 | void main(processorCtx_t & runner) { |
---|
260 | processor * this = runner.proc; |
---|
261 | verify(this); |
---|
262 | |
---|
263 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this); |
---|
264 | |
---|
265 | doregister(this->cltr, this); |
---|
266 | |
---|
267 | { |
---|
268 | // Setup preemption data |
---|
269 | preemption_scope scope = { this }; |
---|
270 | |
---|
271 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); |
---|
272 | |
---|
273 | thread_desc * readyThread = NULL; |
---|
274 | for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) |
---|
275 | { |
---|
276 | readyThread = nextThread( this->cltr ); |
---|
277 | |
---|
278 | if(readyThread) |
---|
279 | { |
---|
280 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
281 | |
---|
282 | runThread(this, readyThread); |
---|
283 | |
---|
284 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
285 | |
---|
286 | //Some actions need to be taken from the kernel |
---|
287 | finishRunning(this); |
---|
288 | |
---|
289 | spin_count = 0; |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | // spin(this, &spin_count); |
---|
294 | halt(this); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this); |
---|
299 | } |
---|
300 | |
---|
301 | unregister(this->cltr, this); |
---|
302 | |
---|
303 | V( this->terminated ); |
---|
304 | |
---|
305 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this); |
---|
306 | } |
---|
307 | |
---|
308 | static int * __volatile_errno() __attribute__((noinline)); |
---|
309 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
310 | |
---|
311 | // KERNEL ONLY |
---|
312 | // runThread runs a thread by context switching |
---|
313 | // from the processor coroutine to the target thread |
---|
314 | static void runThread(processor * this, thread_desc * thrd_dst) { |
---|
315 | coroutine_desc * proc_cor = get_coroutine(this->runner); |
---|
316 | |
---|
317 | // Reset the terminating actions here |
---|
318 | this->finish.action_code = No_Action; |
---|
319 | |
---|
320 | // Update global state |
---|
321 | kernelTLS.this_thread = thrd_dst; |
---|
322 | |
---|
323 | // set state of processor coroutine to inactive and the thread to active |
---|
324 | proc_cor->state = proc_cor->state == Halted ? Halted : Inactive; |
---|
325 | thrd_dst->state = Active; |
---|
326 | |
---|
327 | // set context switch to the thread that the processor is executing |
---|
328 | verify( thrd_dst->context.SP ); |
---|
329 | CtxSwitch( &proc_cor->context, &thrd_dst->context ); |
---|
330 | // when CtxSwitch returns we are back in the processor coroutine |
---|
331 | |
---|
332 | // set state of processor coroutine to active and the thread to inactive |
---|
333 | thrd_dst->state = thrd_dst->state == Halted ? Halted : Inactive; |
---|
334 | proc_cor->state = Active; |
---|
335 | } |
---|
336 | |
---|
337 | // KERNEL_ONLY |
---|
338 | static void returnToKernel() { |
---|
339 | coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner); |
---|
340 | thread_desc * thrd_src = kernelTLS.this_thread; |
---|
341 | |
---|
342 | // set state of current coroutine to inactive |
---|
343 | thrd_src->state = thrd_src->state == Halted ? Halted : Inactive; |
---|
344 | proc_cor->state = Active; |
---|
345 | int local_errno = *__volatile_errno(); |
---|
346 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
347 | __x87_store; |
---|
348 | #endif |
---|
349 | |
---|
350 | // set new coroutine that the processor is executing |
---|
351 | // and context switch to it |
---|
352 | verify( proc_cor->context.SP ); |
---|
353 | CtxSwitch( &thrd_src->context, &proc_cor->context ); |
---|
354 | |
---|
355 | // set state of new coroutine to active |
---|
356 | proc_cor->state = proc_cor->state == Halted ? Halted : Inactive; |
---|
357 | thrd_src->state = Active; |
---|
358 | |
---|
359 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
360 | __x87_load; |
---|
361 | #endif |
---|
362 | *__volatile_errno() = local_errno; |
---|
363 | } |
---|
364 | |
---|
365 | // KERNEL_ONLY |
---|
366 | // Once a thread has finished running, some of |
---|
367 | // its final actions must be executed from the kernel |
---|
368 | static void finishRunning(processor * this) with( this->finish ) { |
---|
369 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
370 | choose( action_code ) { |
---|
371 | case No_Action: |
---|
372 | break; |
---|
373 | case Release: |
---|
374 | unlock( *lock ); |
---|
375 | case Schedule: |
---|
376 | ScheduleThread( thrd ); |
---|
377 | case Release_Schedule: |
---|
378 | unlock( *lock ); |
---|
379 | ScheduleThread( thrd ); |
---|
380 | case Release_Multi: |
---|
381 | for(int i = 0; i < lock_count; i++) { |
---|
382 | unlock( *locks[i] ); |
---|
383 | } |
---|
384 | case Release_Multi_Schedule: |
---|
385 | for(int i = 0; i < lock_count; i++) { |
---|
386 | unlock( *locks[i] ); |
---|
387 | } |
---|
388 | for(int i = 0; i < thrd_count; i++) { |
---|
389 | ScheduleThread( thrds[i] ); |
---|
390 | } |
---|
391 | case Callback: |
---|
392 | callback(); |
---|
393 | default: |
---|
394 | abort("KERNEL ERROR: Unexpected action to run after thread"); |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | // KERNEL_ONLY |
---|
399 | // Context invoker for processors |
---|
400 | // This is the entry point for processors (kernel threads) |
---|
401 | // It effectively constructs a coroutine by stealing the pthread stack |
---|
402 | static void * CtxInvokeProcessor(void * arg) { |
---|
403 | processor * proc = (processor *) arg; |
---|
404 | kernelTLS.this_processor = proc; |
---|
405 | kernelTLS.this_thread = NULL; |
---|
406 | kernelTLS.preemption_state.[enabled, disable_count] = [false, 1]; |
---|
407 | // SKULLDUGGERY: We want to create a context for the processor coroutine |
---|
408 | // which is needed for the 2-step context switch. However, there is no reason |
---|
409 | // to waste the perfectly valid stack create by pthread. |
---|
410 | current_stack_info_t info; |
---|
411 | __stack_t ctx; |
---|
412 | info.storage = &ctx; |
---|
413 | (proc->runner){ proc, &info }; |
---|
414 | |
---|
415 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage); |
---|
416 | |
---|
417 | //Set global state |
---|
418 | kernelTLS.this_thread = NULL; |
---|
419 | |
---|
420 | //We now have a proper context from which to schedule threads |
---|
421 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx); |
---|
422 | |
---|
423 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't |
---|
424 | // resume it to start it like it normally would, it will just context switch |
---|
425 | // back to here. Instead directly call the main since we already are on the |
---|
426 | // appropriate stack. |
---|
427 | get_coroutine(proc->runner)->state = Active; |
---|
428 | main( proc->runner ); |
---|
429 | get_coroutine(proc->runner)->state = Halted; |
---|
430 | |
---|
431 | // Main routine of the core returned, the core is now fully terminated |
---|
432 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner); |
---|
433 | |
---|
434 | return NULL; |
---|
435 | } |
---|
436 | |
---|
437 | static void start(processor * this) { |
---|
438 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this); |
---|
439 | |
---|
440 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this ); |
---|
441 | |
---|
442 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this); |
---|
443 | } |
---|
444 | |
---|
445 | // KERNEL_ONLY |
---|
446 | void kernel_first_resume( processor * this ) { |
---|
447 | thread_desc * src = mainThread; |
---|
448 | coroutine_desc * dst = get_coroutine(this->runner); |
---|
449 | |
---|
450 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
451 | |
---|
452 | __stack_prepare( &dst->stack, 65000 ); |
---|
453 | CtxStart(&this->runner, CtxInvokeCoroutine); |
---|
454 | |
---|
455 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
456 | |
---|
457 | dst->last = &src->self_cor; |
---|
458 | dst->starter = dst->starter ? dst->starter : &src->self_cor; |
---|
459 | |
---|
460 | // set state of current coroutine to inactive |
---|
461 | src->state = src->state == Halted ? Halted : Inactive; |
---|
462 | |
---|
463 | // context switch to specified coroutine |
---|
464 | verify( dst->context.SP ); |
---|
465 | CtxSwitch( &src->context, &dst->context ); |
---|
466 | // when CtxSwitch returns we are back in the src coroutine |
---|
467 | |
---|
468 | // set state of new coroutine to active |
---|
469 | src->state = Active; |
---|
470 | |
---|
471 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
472 | } |
---|
473 | |
---|
474 | // KERNEL_ONLY |
---|
475 | void kernel_last_resume( processor * this ) { |
---|
476 | coroutine_desc * src = &mainThread->self_cor; |
---|
477 | coroutine_desc * dst = get_coroutine(this->runner); |
---|
478 | |
---|
479 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
480 | verify( dst->starter == src ); |
---|
481 | verify( dst->context.SP ); |
---|
482 | |
---|
483 | // context switch to the processor |
---|
484 | CtxSwitch( &src->context, &dst->context ); |
---|
485 | } |
---|
486 | |
---|
487 | //----------------------------------------------------------------------------- |
---|
488 | // Scheduler routines |
---|
489 | |
---|
490 | // KERNEL ONLY |
---|
491 | void ScheduleThread( thread_desc * thrd ) { |
---|
492 | verify( thrd ); |
---|
493 | verify( thrd->state != Halted ); |
---|
494 | |
---|
495 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
496 | |
---|
497 | verifyf( thrd->next == NULL, "Expected null got %p", thrd->next ); |
---|
498 | |
---|
499 | with( *thrd->curr_cluster ) { |
---|
500 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
501 | bool was_empty = !(ready_queue != 0); |
---|
502 | append( ready_queue, thrd ); |
---|
503 | unlock( ready_queue_lock ); |
---|
504 | |
---|
505 | if(was_empty) { |
---|
506 | lock (proc_list_lock __cfaabi_dbg_ctx2); |
---|
507 | if(idles) { |
---|
508 | wake_fast(idles.head); |
---|
509 | } |
---|
510 | unlock (proc_list_lock); |
---|
511 | } |
---|
512 | else if( struct processor * idle = idles.head ) { |
---|
513 | wake_fast(idle); |
---|
514 | } |
---|
515 | |
---|
516 | } |
---|
517 | |
---|
518 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
519 | } |
---|
520 | |
---|
521 | // KERNEL ONLY |
---|
522 | thread_desc * nextThread(cluster * this) with( *this ) { |
---|
523 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
524 | lock( ready_queue_lock __cfaabi_dbg_ctx2 ); |
---|
525 | thread_desc * head = pop_head( ready_queue ); |
---|
526 | unlock( ready_queue_lock ); |
---|
527 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
528 | return head; |
---|
529 | } |
---|
530 | |
---|
531 | void BlockInternal() { |
---|
532 | disable_interrupts(); |
---|
533 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
534 | returnToKernel(); |
---|
535 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
536 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
537 | } |
---|
538 | |
---|
539 | void BlockInternal( __spinlock_t * lock ) { |
---|
540 | disable_interrupts(); |
---|
541 | with( *kernelTLS.this_processor ) { |
---|
542 | finish.action_code = Release; |
---|
543 | finish.lock = lock; |
---|
544 | } |
---|
545 | |
---|
546 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
547 | returnToKernel(); |
---|
548 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
549 | |
---|
550 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
551 | } |
---|
552 | |
---|
553 | void BlockInternal( thread_desc * thrd ) { |
---|
554 | disable_interrupts(); |
---|
555 | with( * kernelTLS.this_processor ) { |
---|
556 | finish.action_code = Schedule; |
---|
557 | finish.thrd = thrd; |
---|
558 | } |
---|
559 | |
---|
560 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
561 | returnToKernel(); |
---|
562 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
563 | |
---|
564 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
565 | } |
---|
566 | |
---|
567 | void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) { |
---|
568 | assert(thrd); |
---|
569 | disable_interrupts(); |
---|
570 | with( * kernelTLS.this_processor ) { |
---|
571 | finish.action_code = Release_Schedule; |
---|
572 | finish.lock = lock; |
---|
573 | finish.thrd = thrd; |
---|
574 | } |
---|
575 | |
---|
576 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
577 | returnToKernel(); |
---|
578 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
579 | |
---|
580 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
581 | } |
---|
582 | |
---|
583 | void BlockInternal(__spinlock_t * locks [], unsigned short count) { |
---|
584 | disable_interrupts(); |
---|
585 | with( * kernelTLS.this_processor ) { |
---|
586 | finish.action_code = Release_Multi; |
---|
587 | finish.locks = locks; |
---|
588 | finish.lock_count = count; |
---|
589 | } |
---|
590 | |
---|
591 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
592 | returnToKernel(); |
---|
593 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
594 | |
---|
595 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
596 | } |
---|
597 | |
---|
598 | void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) { |
---|
599 | disable_interrupts(); |
---|
600 | with( *kernelTLS.this_processor ) { |
---|
601 | finish.action_code = Release_Multi_Schedule; |
---|
602 | finish.locks = locks; |
---|
603 | finish.lock_count = lock_count; |
---|
604 | finish.thrds = thrds; |
---|
605 | finish.thrd_count = thrd_count; |
---|
606 | } |
---|
607 | |
---|
608 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
609 | returnToKernel(); |
---|
610 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
611 | |
---|
612 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
613 | } |
---|
614 | |
---|
615 | void BlockInternal(__finish_callback_fptr_t callback) { |
---|
616 | disable_interrupts(); |
---|
617 | with( *kernelTLS.this_processor ) { |
---|
618 | finish.action_code = Callback; |
---|
619 | finish.callback = callback; |
---|
620 | } |
---|
621 | |
---|
622 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
623 | returnToKernel(); |
---|
624 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
625 | |
---|
626 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
627 | } |
---|
628 | |
---|
629 | // KERNEL ONLY |
---|
630 | void LeaveThread(__spinlock_t * lock, thread_desc * thrd) { |
---|
631 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
632 | with( * kernelTLS.this_processor ) { |
---|
633 | finish.action_code = thrd ? Release_Schedule : Release; |
---|
634 | finish.lock = lock; |
---|
635 | finish.thrd = thrd; |
---|
636 | } |
---|
637 | |
---|
638 | returnToKernel(); |
---|
639 | } |
---|
640 | |
---|
641 | //============================================================================================= |
---|
642 | // Kernel Setup logic |
---|
643 | //============================================================================================= |
---|
644 | //----------------------------------------------------------------------------- |
---|
645 | // Kernel boot procedures |
---|
646 | static void kernel_startup(void) { |
---|
647 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
648 | __cfaabi_dbg_print_safe("Kernel : Starting\n"); |
---|
649 | |
---|
650 | __page_size = sysconf( _SC_PAGESIZE ); |
---|
651 | |
---|
652 | __cfa_dbg_global_clusters.list{ __get }; |
---|
653 | __cfa_dbg_global_clusters.lock{}; |
---|
654 | |
---|
655 | // Initialize the main cluster |
---|
656 | mainCluster = (cluster *)&storage_mainCluster; |
---|
657 | (*mainCluster){"Main Cluster"}; |
---|
658 | |
---|
659 | __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n"); |
---|
660 | |
---|
661 | // Start by initializing the main thread |
---|
662 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
663 | // which will then be scheduled by the mainProcessor normally |
---|
664 | mainThread = (thread_desc *)&storage_mainThread; |
---|
665 | current_stack_info_t info; |
---|
666 | info.storage = (__stack_t*)&storage_mainThreadCtx; |
---|
667 | (*mainThread){ &info }; |
---|
668 | |
---|
669 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n"); |
---|
670 | |
---|
671 | |
---|
672 | |
---|
673 | // Construct the processor context of the main processor |
---|
674 | void ?{}(processorCtx_t & this, processor * proc) { |
---|
675 | (this.__cor){ "Processor" }; |
---|
676 | this.__cor.starter = NULL; |
---|
677 | this.proc = proc; |
---|
678 | } |
---|
679 | |
---|
680 | void ?{}(processor & this) with( this ) { |
---|
681 | name = "Main Processor"; |
---|
682 | cltr = mainCluster; |
---|
683 | terminated{ 0 }; |
---|
684 | do_terminate = false; |
---|
685 | preemption_alarm = NULL; |
---|
686 | pending_preemption = false; |
---|
687 | kernel_thread = pthread_self(); |
---|
688 | |
---|
689 | runner{ &this }; |
---|
690 | __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner); |
---|
691 | } |
---|
692 | |
---|
693 | // Initialize the main processor and the main processor ctx |
---|
694 | // (the coroutine that contains the processing control flow) |
---|
695 | mainProcessor = (processor *)&storage_mainProcessor; |
---|
696 | (*mainProcessor){}; |
---|
697 | |
---|
698 | //initialize the global state variables |
---|
699 | kernelTLS.this_processor = mainProcessor; |
---|
700 | kernelTLS.this_thread = mainThread; |
---|
701 | |
---|
702 | // Enable preemption |
---|
703 | kernel_start_preemption(); |
---|
704 | |
---|
705 | // Add the main thread to the ready queue |
---|
706 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread |
---|
707 | ScheduleThread(mainThread); |
---|
708 | |
---|
709 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX |
---|
710 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that |
---|
711 | // mainThread is on the ready queue when this call is made. |
---|
712 | kernel_first_resume( kernelTLS.this_processor ); |
---|
713 | |
---|
714 | |
---|
715 | |
---|
716 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
717 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n"); |
---|
718 | |
---|
719 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
720 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
721 | verify( TL_GET( preemption_state.enabled ) ); |
---|
722 | } |
---|
723 | |
---|
724 | static void kernel_shutdown(void) { |
---|
725 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
726 | |
---|
727 | verify( TL_GET( preemption_state.enabled ) ); |
---|
728 | disable_interrupts(); |
---|
729 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
730 | |
---|
731 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. |
---|
732 | // When its coroutine terminates, it return control to the mainThread |
---|
733 | // which is currently here |
---|
734 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE); |
---|
735 | kernel_last_resume( kernelTLS.this_processor ); |
---|
736 | mainThread->self_cor.state = Halted; |
---|
737 | |
---|
738 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
739 | |
---|
740 | // Disable preemption |
---|
741 | kernel_stop_preemption(); |
---|
742 | |
---|
743 | // Destroy the main processor and its context in reverse order of construction |
---|
744 | // These were manually constructed so we need manually destroy them |
---|
745 | ^(mainProcessor->runner){}; |
---|
746 | ^(mainProcessor){}; |
---|
747 | |
---|
748 | // Final step, destroy the main thread since it is no longer needed |
---|
749 | // Since we provided a stack to this taxk it will not destroy anything |
---|
750 | ^(mainThread){}; |
---|
751 | |
---|
752 | ^(__cfa_dbg_global_clusters.list){}; |
---|
753 | ^(__cfa_dbg_global_clusters.lock){}; |
---|
754 | |
---|
755 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n"); |
---|
756 | } |
---|
757 | |
---|
758 | //============================================================================================= |
---|
759 | // Kernel Quiescing |
---|
760 | //============================================================================================= |
---|
761 | static void halt(processor * this) with( *this ) { |
---|
762 | // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) ); |
---|
763 | |
---|
764 | with( *cltr ) { |
---|
765 | lock (proc_list_lock __cfaabi_dbg_ctx2); |
---|
766 | remove (procs, *this); |
---|
767 | push_front(idles, *this); |
---|
768 | unlock (proc_list_lock); |
---|
769 | } |
---|
770 | |
---|
771 | __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this); |
---|
772 | |
---|
773 | wait( idleLock ); |
---|
774 | |
---|
775 | __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this); |
---|
776 | |
---|
777 | with( *cltr ) { |
---|
778 | lock (proc_list_lock __cfaabi_dbg_ctx2); |
---|
779 | remove (idles, *this); |
---|
780 | push_front(procs, *this); |
---|
781 | unlock (proc_list_lock); |
---|
782 | } |
---|
783 | } |
---|
784 | |
---|
785 | //============================================================================================= |
---|
786 | // Unexpected Terminating logic |
---|
787 | //============================================================================================= |
---|
788 | static __spinlock_t kernel_abort_lock; |
---|
789 | static bool kernel_abort_called = false; |
---|
790 | |
---|
791 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
792 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
793 | // the globalAbort flag is true. |
---|
794 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
795 | |
---|
796 | // first task to abort ? |
---|
797 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
798 | unlock( kernel_abort_lock ); |
---|
799 | |
---|
800 | sigset_t mask; |
---|
801 | sigemptyset( &mask ); |
---|
802 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
803 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
804 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
805 | } |
---|
806 | else { |
---|
807 | kernel_abort_called = true; |
---|
808 | unlock( kernel_abort_lock ); |
---|
809 | } |
---|
810 | |
---|
811 | return kernelTLS.this_thread; |
---|
812 | } |
---|
813 | |
---|
814 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
815 | thread_desc * thrd = kernel_data; |
---|
816 | |
---|
817 | if(thrd) { |
---|
818 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
819 | __cfaabi_dbg_bits_write( abort_text, len ); |
---|
820 | |
---|
821 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
822 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
823 | __cfaabi_dbg_bits_write( abort_text, len ); |
---|
824 | } |
---|
825 | else { |
---|
826 | __cfaabi_dbg_bits_write( ".\n", 2 ); |
---|
827 | } |
---|
828 | } |
---|
829 | else { |
---|
830 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
831 | __cfaabi_dbg_bits_write( abort_text, len ); |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
836 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
837 | } |
---|
838 | |
---|
839 | static __spinlock_t kernel_debug_lock; |
---|
840 | |
---|
841 | extern "C" { |
---|
842 | void __cfaabi_dbg_bits_acquire() { |
---|
843 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
844 | } |
---|
845 | |
---|
846 | void __cfaabi_dbg_bits_release() { |
---|
847 | unlock( kernel_debug_lock ); |
---|
848 | } |
---|
849 | } |
---|
850 | |
---|
851 | //============================================================================================= |
---|
852 | // Kernel Utilities |
---|
853 | //============================================================================================= |
---|
854 | //----------------------------------------------------------------------------- |
---|
855 | // Locks |
---|
856 | void ?{}( semaphore & this, int count = 1 ) { |
---|
857 | (this.lock){}; |
---|
858 | this.count = count; |
---|
859 | (this.waiting){}; |
---|
860 | } |
---|
861 | void ^?{}(semaphore & this) {} |
---|
862 | |
---|
863 | void P(semaphore & this) with( this ){ |
---|
864 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
865 | count -= 1; |
---|
866 | if ( count < 0 ) { |
---|
867 | // queue current task |
---|
868 | append( waiting, kernelTLS.this_thread ); |
---|
869 | |
---|
870 | // atomically release spin lock and block |
---|
871 | BlockInternal( &lock ); |
---|
872 | } |
---|
873 | else { |
---|
874 | unlock( lock ); |
---|
875 | } |
---|
876 | } |
---|
877 | |
---|
878 | void V(semaphore & this) with( this ) { |
---|
879 | thread_desc * thrd = NULL; |
---|
880 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
881 | count += 1; |
---|
882 | if ( count <= 0 ) { |
---|
883 | // remove task at head of waiting list |
---|
884 | thrd = pop_head( waiting ); |
---|
885 | } |
---|
886 | |
---|
887 | unlock( lock ); |
---|
888 | |
---|
889 | // make new owner |
---|
890 | WakeThread( thrd ); |
---|
891 | } |
---|
892 | |
---|
893 | //----------------------------------------------------------------------------- |
---|
894 | // Global Queues |
---|
895 | void doregister( cluster & cltr ) { |
---|
896 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
897 | push_front( __cfa_dbg_global_clusters.list, cltr ); |
---|
898 | unlock ( __cfa_dbg_global_clusters.lock ); |
---|
899 | } |
---|
900 | |
---|
901 | void unregister( cluster & cltr ) { |
---|
902 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
903 | remove( __cfa_dbg_global_clusters.list, cltr ); |
---|
904 | unlock( __cfa_dbg_global_clusters.lock ); |
---|
905 | } |
---|
906 | |
---|
907 | void doregister( cluster * cltr, thread_desc & thrd ) { |
---|
908 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
909 | cltr->nthreads += 1; |
---|
910 | push_front(cltr->threads, thrd); |
---|
911 | unlock (cltr->thread_list_lock); |
---|
912 | } |
---|
913 | |
---|
914 | void unregister( cluster * cltr, thread_desc & thrd ) { |
---|
915 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
916 | remove(cltr->threads, thrd ); |
---|
917 | cltr->nthreads -= 1; |
---|
918 | unlock(cltr->thread_list_lock); |
---|
919 | } |
---|
920 | |
---|
921 | void doregister( cluster * cltr, processor * proc ) { |
---|
922 | lock (cltr->proc_list_lock __cfaabi_dbg_ctx2); |
---|
923 | cltr->nprocessors += 1; |
---|
924 | push_front(cltr->procs, *proc); |
---|
925 | unlock (cltr->proc_list_lock); |
---|
926 | } |
---|
927 | |
---|
928 | void unregister( cluster * cltr, processor * proc ) { |
---|
929 | lock (cltr->proc_list_lock __cfaabi_dbg_ctx2); |
---|
930 | remove(cltr->procs, *proc ); |
---|
931 | cltr->nprocessors -= 1; |
---|
932 | unlock(cltr->proc_list_lock); |
---|
933 | } |
---|
934 | |
---|
935 | //----------------------------------------------------------------------------- |
---|
936 | // Debug |
---|
937 | __cfaabi_dbg_debug_do( |
---|
938 | extern "C" { |
---|
939 | void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) { |
---|
940 | this.prev_name = prev_name; |
---|
941 | this.prev_thrd = kernelTLS.this_thread; |
---|
942 | } |
---|
943 | } |
---|
944 | ) |
---|
945 | // Local Variables: // |
---|
946 | // mode: c // |
---|
947 | // tab-width: 4 // |
---|
948 | // End: // |
---|