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 : Mon Apr 9 16:11:46 2018
|
---|
13 | // Update Count : 24
|
---|
14 | //
|
---|
15 |
|
---|
16 | //C Includes
|
---|
17 | #include <stddef.h>
|
---|
18 | extern "C" {
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <fenv.h>
|
---|
21 | #include <sys/resource.h>
|
---|
22 | #include <signal.h>
|
---|
23 | #include <unistd.h>
|
---|
24 | }
|
---|
25 |
|
---|
26 | //CFA Includes
|
---|
27 | #include "time"
|
---|
28 | #include "kernel_private.h"
|
---|
29 | #include "preemption.h"
|
---|
30 | #include "startup.h"
|
---|
31 |
|
---|
32 | //Private includes
|
---|
33 | #define __CFA_INVOKE_PRIVATE__
|
---|
34 | #include "invoke.h"
|
---|
35 |
|
---|
36 | //Start and stop routine for the kernel, declared first to make sure they run first
|
---|
37 | void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
---|
38 | void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
---|
39 |
|
---|
40 | //-----------------------------------------------------------------------------
|
---|
41 | // Kernel storage
|
---|
42 | KERNEL_STORAGE(cluster, mainCluster);
|
---|
43 | KERNEL_STORAGE(processor, mainProcessor);
|
---|
44 | KERNEL_STORAGE(thread_desc, mainThread);
|
---|
45 | KERNEL_STORAGE(machine_context_t, mainThreadCtx);
|
---|
46 |
|
---|
47 | cluster * mainCluster;
|
---|
48 | processor * mainProcessor;
|
---|
49 | thread_desc * mainThread;
|
---|
50 |
|
---|
51 | //-----------------------------------------------------------------------------
|
---|
52 | // Global state
|
---|
53 |
|
---|
54 | // volatile thread_local bool preemption_in_progress = 0;
|
---|
55 | // volatile thread_local bool preemption_enabled = false;
|
---|
56 | // volatile thread_local unsigned short disable_preempt_count = 1;
|
---|
57 |
|
---|
58 | thread_local struct KernelThreadData kernelThreadData = {
|
---|
59 | NULL,
|
---|
60 | NULL,
|
---|
61 | NULL,
|
---|
62 | { 1, false, false }
|
---|
63 | };
|
---|
64 |
|
---|
65 | //-----------------------------------------------------------------------------
|
---|
66 | // Struct to steal stack
|
---|
67 | struct current_stack_info_t {
|
---|
68 | machine_context_t ctx;
|
---|
69 | unsigned int size; // size of stack
|
---|
70 | void *base; // base of stack
|
---|
71 | void *storage; // pointer to stack
|
---|
72 | void *limit; // stack grows towards stack limit
|
---|
73 | void *context; // address of cfa_context_t
|
---|
74 | void *top; // address of top of storage
|
---|
75 | };
|
---|
76 |
|
---|
77 | void ?{}( current_stack_info_t & this ) {
|
---|
78 | CtxGet( this.ctx );
|
---|
79 | this.base = this.ctx.FP;
|
---|
80 | this.storage = this.ctx.SP;
|
---|
81 |
|
---|
82 | rlimit r;
|
---|
83 | getrlimit( RLIMIT_STACK, &r);
|
---|
84 | this.size = r.rlim_cur;
|
---|
85 |
|
---|
86 | this.limit = (void *)(((intptr_t)this.base) - this.size);
|
---|
87 | this.context = &storage_mainThreadCtx;
|
---|
88 | this.top = this.base;
|
---|
89 | }
|
---|
90 |
|
---|
91 | //-----------------------------------------------------------------------------
|
---|
92 | // Main thread construction
|
---|
93 | void ?{}( coStack_t & this, current_stack_info_t * info) with( this ) {
|
---|
94 | size = info->size;
|
---|
95 | storage = info->storage;
|
---|
96 | limit = info->limit;
|
---|
97 | base = info->base;
|
---|
98 | context = info->context;
|
---|
99 | top = info->top;
|
---|
100 | userStack = true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
|
---|
104 | stack{ info };
|
---|
105 | name = "Main Thread";
|
---|
106 | errno_ = 0;
|
---|
107 | state = Start;
|
---|
108 | starter = NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
|
---|
112 | self_cor{ info };
|
---|
113 | curr_cor = &self_cor;
|
---|
114 | curr_cluster = mainCluster;
|
---|
115 | self_mon.owner = &this;
|
---|
116 | self_mon.recursion = 1;
|
---|
117 | self_mon_p = &self_mon;
|
---|
118 | next = NULL;
|
---|
119 | __cfaabi_dbg_debug_do(
|
---|
120 | dbg_next = NULL;
|
---|
121 | dbg_prev = NULL;
|
---|
122 | __cfaabi_dbg_thread_register(&this);
|
---|
123 | )
|
---|
124 |
|
---|
125 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
126 | }
|
---|
127 |
|
---|
128 | //-----------------------------------------------------------------------------
|
---|
129 | // Processor coroutine
|
---|
130 | void ?{}(processorCtx_t & this) {
|
---|
131 |
|
---|
132 | }
|
---|
133 |
|
---|
134 | // Construct the processor context of non-main processors
|
---|
135 | void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
---|
136 | (this.__cor){ info };
|
---|
137 | this.proc = proc;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
|
---|
141 | this.name = name;
|
---|
142 | this.cltr = &cltr;
|
---|
143 | terminated{ 0 };
|
---|
144 | do_terminate = false;
|
---|
145 | preemption_alarm = NULL;
|
---|
146 | pending_preemption = false;
|
---|
147 | runner.proc = &this;
|
---|
148 |
|
---|
149 | start( &this );
|
---|
150 | }
|
---|
151 |
|
---|
152 | void ^?{}(processor & this) with( this ){
|
---|
153 | if( ! do_terminate ) {
|
---|
154 | __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
|
---|
155 | terminate(&this);
|
---|
156 | verify(this.do_terminate);
|
---|
157 | verify(TL_GET( this_processor ) != &this);
|
---|
158 | P( terminated );
|
---|
159 | verify(TL_GET( this_processor ) != &this);
|
---|
160 | pthread_join( kernel_thread, NULL );
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
|
---|
165 | this.name = name;
|
---|
166 | this.preemption_rate = preemption_rate;
|
---|
167 | ready_queue{};
|
---|
168 | ready_queue_lock{};
|
---|
169 | }
|
---|
170 |
|
---|
171 | void ^?{}(cluster & this) {
|
---|
172 |
|
---|
173 | }
|
---|
174 |
|
---|
175 | //=============================================================================================
|
---|
176 | // Kernel Scheduling logic
|
---|
177 | //=============================================================================================
|
---|
178 | //Main of the processor contexts
|
---|
179 | void main(processorCtx_t & runner) {
|
---|
180 | processor * this = runner.proc;
|
---|
181 | verify(this);
|
---|
182 |
|
---|
183 | __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
|
---|
184 |
|
---|
185 | {
|
---|
186 | // Setup preemption data
|
---|
187 | preemption_scope scope = { this };
|
---|
188 |
|
---|
189 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
---|
190 |
|
---|
191 | thread_desc * readyThread = NULL;
|
---|
192 | for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
|
---|
193 | {
|
---|
194 | readyThread = nextThread( this->cltr );
|
---|
195 |
|
---|
196 | if(readyThread)
|
---|
197 | {
|
---|
198 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
199 |
|
---|
200 | runThread(this, readyThread);
|
---|
201 |
|
---|
202 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
203 |
|
---|
204 | //Some actions need to be taken from the kernel
|
---|
205 | finishRunning(this);
|
---|
206 |
|
---|
207 | spin_count = 0;
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | spin(this, &spin_count);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
|
---|
216 | }
|
---|
217 |
|
---|
218 | V( this->terminated );
|
---|
219 |
|
---|
220 | __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
|
---|
221 | }
|
---|
222 |
|
---|
223 | // runThread runs a thread by context switching
|
---|
224 | // from the processor coroutine to the target thread
|
---|
225 | void runThread(processor * this, thread_desc * dst) {
|
---|
226 | assert(dst->curr_cor);
|
---|
227 | coroutine_desc * proc_cor = get_coroutine(this->runner);
|
---|
228 | coroutine_desc * thrd_cor = dst->curr_cor;
|
---|
229 |
|
---|
230 | //Reset the terminating actions here
|
---|
231 | this->finish.action_code = No_Action;
|
---|
232 |
|
---|
233 | //Update global state
|
---|
234 | TL_SET( this_thread, dst );
|
---|
235 |
|
---|
236 | // Context Switch to the thread
|
---|
237 | ThreadCtxSwitch(proc_cor, thrd_cor);
|
---|
238 | // when ThreadCtxSwitch returns we are back in the processor coroutine
|
---|
239 | }
|
---|
240 |
|
---|
241 | void returnToKernel() {
|
---|
242 | coroutine_desc * proc_cor = get_coroutine(TL_GET( this_processor )->runner);
|
---|
243 | coroutine_desc * thrd_cor = TL_GET( this_thread )->curr_cor = TL_GET( this_coroutine );
|
---|
244 | ThreadCtxSwitch(thrd_cor, proc_cor);
|
---|
245 | }
|
---|
246 |
|
---|
247 | // Once a thread has finished running, some of
|
---|
248 | // its final actions must be executed from the kernel
|
---|
249 | void finishRunning(processor * this) with( this->finish ) {
|
---|
250 | if( action_code == Release ) {
|
---|
251 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
252 | unlock( *lock );
|
---|
253 | }
|
---|
254 | else if( action_code == Schedule ) {
|
---|
255 | ScheduleThread( thrd );
|
---|
256 | }
|
---|
257 | else if( action_code == Release_Schedule ) {
|
---|
258 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
259 | unlock( *lock );
|
---|
260 | ScheduleThread( thrd );
|
---|
261 | }
|
---|
262 | else if( action_code == Release_Multi ) {
|
---|
263 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
264 | for(int i = 0; i < lock_count; i++) {
|
---|
265 | unlock( *locks[i] );
|
---|
266 | }
|
---|
267 | }
|
---|
268 | else if( action_code == Release_Multi_Schedule ) {
|
---|
269 | for(int i = 0; i < lock_count; i++) {
|
---|
270 | unlock( *locks[i] );
|
---|
271 | }
|
---|
272 | for(int i = 0; i < thrd_count; i++) {
|
---|
273 | ScheduleThread( thrds[i] );
|
---|
274 | }
|
---|
275 | }
|
---|
276 | else {
|
---|
277 | assert(action_code == No_Action);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | // Handles spinning logic
|
---|
282 | // TODO : find some strategy to put cores to sleep after some time
|
---|
283 | void spin(processor * this, unsigned int * spin_count) {
|
---|
284 | (*spin_count)++;
|
---|
285 | }
|
---|
286 |
|
---|
287 | // Context invoker for processors
|
---|
288 | // This is the entry point for processors (kernel threads)
|
---|
289 | // It effectively constructs a coroutine by stealing the pthread stack
|
---|
290 | void * CtxInvokeProcessor(void * arg) {
|
---|
291 | processor * proc = (processor *) arg;
|
---|
292 | TL_SET( this_processor, proc );
|
---|
293 | TL_SET( this_coroutine, NULL );
|
---|
294 | TL_SET( this_thread, NULL );
|
---|
295 | TL_GET( preemption_state ).[enabled, disable_count] = [false, 1];
|
---|
296 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
---|
297 | // which is needed for the 2-step context switch. However, there is no reason
|
---|
298 | // to waste the perfectly valid stack create by pthread.
|
---|
299 | current_stack_info_t info;
|
---|
300 | machine_context_t ctx;
|
---|
301 | info.context = &ctx;
|
---|
302 | (proc->runner){ proc, &info };
|
---|
303 |
|
---|
304 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
|
---|
305 |
|
---|
306 | //Set global state
|
---|
307 | TL_SET( this_coroutine, get_coroutine(proc->runner) );
|
---|
308 | TL_SET( this_thread, NULL );
|
---|
309 |
|
---|
310 | //We now have a proper context from which to schedule threads
|
---|
311 | __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
|
---|
312 |
|
---|
313 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
---|
314 | // resume it to start it like it normally would, it will just context switch
|
---|
315 | // back to here. Instead directly call the main since we already are on the
|
---|
316 | // appropriate stack.
|
---|
317 | get_coroutine(proc->runner)->state = Active;
|
---|
318 | main( proc->runner );
|
---|
319 | get_coroutine(proc->runner)->state = Halted;
|
---|
320 |
|
---|
321 | // Main routine of the core returned, the core is now fully terminated
|
---|
322 | __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
|
---|
323 |
|
---|
324 | return NULL;
|
---|
325 | }
|
---|
326 |
|
---|
327 | void start(processor * this) {
|
---|
328 | __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
|
---|
329 |
|
---|
330 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
|
---|
331 |
|
---|
332 | __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
|
---|
333 | }
|
---|
334 |
|
---|
335 | void kernel_first_resume(processor * this) {
|
---|
336 | coroutine_desc * src = TL_GET( this_coroutine );
|
---|
337 | coroutine_desc * dst = get_coroutine(this->runner);
|
---|
338 |
|
---|
339 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
340 |
|
---|
341 | create_stack(&dst->stack, dst->stack.size);
|
---|
342 | CtxStart(&this->runner, CtxInvokeCoroutine);
|
---|
343 |
|
---|
344 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
345 |
|
---|
346 | dst->last = src;
|
---|
347 | dst->starter = dst->starter ? dst->starter : src;
|
---|
348 |
|
---|
349 | // set state of current coroutine to inactive
|
---|
350 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
351 |
|
---|
352 | // set new coroutine that task is executing
|
---|
353 | TL_SET( this_coroutine, dst );
|
---|
354 |
|
---|
355 | // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch.
|
---|
356 | // Therefore, when first creating a coroutine, interrupts are enable before calling the main.
|
---|
357 | // This is consistent with thread creation. However, when creating the main processor coroutine,
|
---|
358 | // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will
|
---|
359 | // stay disabled.
|
---|
360 | disable_interrupts();
|
---|
361 |
|
---|
362 | // context switch to specified coroutine
|
---|
363 | assert( src->stack.context );
|
---|
364 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
365 | // when CtxSwitch returns we are back in the src coroutine
|
---|
366 |
|
---|
367 | // set state of new coroutine to active
|
---|
368 | src->state = Active;
|
---|
369 |
|
---|
370 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
371 | }
|
---|
372 |
|
---|
373 | //-----------------------------------------------------------------------------
|
---|
374 | // Scheduler routines
|
---|
375 | void ScheduleThread( thread_desc * thrd ) {
|
---|
376 | // if( ! thrd ) return;
|
---|
377 | verify( thrd );
|
---|
378 | verify( thrd->self_cor.state != Halted );
|
---|
379 |
|
---|
380 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
381 |
|
---|
382 | verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
|
---|
383 |
|
---|
384 | with( *thrd->curr_cluster ) {
|
---|
385 | lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
|
---|
386 | append( ready_queue, thrd );
|
---|
387 | unlock( ready_queue_lock );
|
---|
388 | }
|
---|
389 |
|
---|
390 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
391 | }
|
---|
392 |
|
---|
393 | thread_desc * nextThread(cluster * this) with( *this ) {
|
---|
394 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
395 | lock( ready_queue_lock __cfaabi_dbg_ctx2 );
|
---|
396 | thread_desc * head = pop_head( ready_queue );
|
---|
397 | unlock( ready_queue_lock );
|
---|
398 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
399 | return head;
|
---|
400 | }
|
---|
401 |
|
---|
402 | void BlockInternal() {
|
---|
403 | disable_interrupts();
|
---|
404 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
405 | returnToKernel();
|
---|
406 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
407 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
408 | }
|
---|
409 |
|
---|
410 | void BlockInternal( __spinlock_t * lock ) {
|
---|
411 | disable_interrupts();
|
---|
412 | with( *TL_GET( this_processor ) ) {
|
---|
413 | finish.action_code = Release;
|
---|
414 | finish.lock = lock;
|
---|
415 | }
|
---|
416 |
|
---|
417 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
418 | returnToKernel();
|
---|
419 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
420 |
|
---|
421 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
422 | }
|
---|
423 |
|
---|
424 | void BlockInternal( thread_desc * thrd ) {
|
---|
425 | disable_interrupts();
|
---|
426 | with( *TL_GET( this_processor ) ) {
|
---|
427 | finish.action_code = Schedule;
|
---|
428 | finish.thrd = thrd;
|
---|
429 | }
|
---|
430 |
|
---|
431 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
432 | returnToKernel();
|
---|
433 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
434 |
|
---|
435 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
436 | }
|
---|
437 |
|
---|
438 | void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
|
---|
439 | assert(thrd);
|
---|
440 | disable_interrupts();
|
---|
441 | with( *TL_GET( this_processor ) ) {
|
---|
442 | finish.action_code = Release_Schedule;
|
---|
443 | finish.lock = lock;
|
---|
444 | finish.thrd = thrd;
|
---|
445 | }
|
---|
446 |
|
---|
447 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
448 | returnToKernel();
|
---|
449 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
450 |
|
---|
451 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
452 | }
|
---|
453 |
|
---|
454 | void BlockInternal(__spinlock_t * locks [], unsigned short count) {
|
---|
455 | disable_interrupts();
|
---|
456 | with( *TL_GET( this_processor ) ) {
|
---|
457 | finish.action_code = Release_Multi;
|
---|
458 | finish.locks = locks;
|
---|
459 | finish.lock_count = count;
|
---|
460 | }
|
---|
461 |
|
---|
462 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
463 | returnToKernel();
|
---|
464 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
465 |
|
---|
466 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
467 | }
|
---|
468 |
|
---|
469 | void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
|
---|
470 | disable_interrupts();
|
---|
471 | with( *TL_GET( this_processor ) ) {
|
---|
472 | finish.action_code = Release_Multi_Schedule;
|
---|
473 | finish.locks = locks;
|
---|
474 | finish.lock_count = lock_count;
|
---|
475 | finish.thrds = thrds;
|
---|
476 | finish.thrd_count = thrd_count;
|
---|
477 | }
|
---|
478 |
|
---|
479 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
480 | returnToKernel();
|
---|
481 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
482 |
|
---|
483 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
484 | }
|
---|
485 |
|
---|
486 | void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
|
---|
487 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
488 | with( *TL_GET( this_processor ) ) {
|
---|
489 | finish.action_code = thrd ? Release_Schedule : Release;
|
---|
490 | finish.lock = lock;
|
---|
491 | finish.thrd = thrd;
|
---|
492 | }
|
---|
493 |
|
---|
494 | returnToKernel();
|
---|
495 | }
|
---|
496 |
|
---|
497 | //=============================================================================================
|
---|
498 | // Kernel Setup logic
|
---|
499 | //=============================================================================================
|
---|
500 | //-----------------------------------------------------------------------------
|
---|
501 | // Kernel boot procedures
|
---|
502 | void kernel_startup(void) {
|
---|
503 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
504 | __cfaabi_dbg_print_safe("Kernel : Starting\n");
|
---|
505 |
|
---|
506 | // Initialize the main cluster
|
---|
507 | mainCluster = (cluster *)&storage_mainCluster;
|
---|
508 | (*mainCluster){"Main Cluster"};
|
---|
509 |
|
---|
510 | __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
|
---|
511 |
|
---|
512 | // Start by initializing the main thread
|
---|
513 | // SKULLDUGGERY: the mainThread steals the process main thread
|
---|
514 | // which will then be scheduled by the mainProcessor normally
|
---|
515 | mainThread = (thread_desc *)&storage_mainThread;
|
---|
516 | current_stack_info_t info;
|
---|
517 | (*mainThread){ &info };
|
---|
518 |
|
---|
519 | __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
|
---|
520 |
|
---|
521 |
|
---|
522 |
|
---|
523 | // Construct the processor context of the main processor
|
---|
524 | void ?{}(processorCtx_t & this, processor * proc) {
|
---|
525 | (this.__cor){ "Processor" };
|
---|
526 | this.__cor.starter = NULL;
|
---|
527 | this.proc = proc;
|
---|
528 | }
|
---|
529 |
|
---|
530 | void ?{}(processor & this) with( this ) {
|
---|
531 | name = "Main Processor";
|
---|
532 | cltr = mainCluster;
|
---|
533 | terminated{ 0 };
|
---|
534 | do_terminate = false;
|
---|
535 | preemption_alarm = NULL;
|
---|
536 | pending_preemption = false;
|
---|
537 | kernel_thread = pthread_self();
|
---|
538 |
|
---|
539 | runner{ &this };
|
---|
540 | __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
|
---|
541 | }
|
---|
542 |
|
---|
543 | // Initialize the main processor and the main processor ctx
|
---|
544 | // (the coroutine that contains the processing control flow)
|
---|
545 | mainProcessor = (processor *)&storage_mainProcessor;
|
---|
546 | (*mainProcessor){};
|
---|
547 |
|
---|
548 | //initialize the global state variables
|
---|
549 | TL_SET( this_processor, mainProcessor );
|
---|
550 | TL_SET( this_thread, mainThread );
|
---|
551 | TL_SET( this_coroutine, &mainThread->self_cor );
|
---|
552 |
|
---|
553 | // Enable preemption
|
---|
554 | kernel_start_preemption();
|
---|
555 |
|
---|
556 | // Add the main thread to the ready queue
|
---|
557 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
---|
558 | ScheduleThread(mainThread);
|
---|
559 |
|
---|
560 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
---|
561 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
|
---|
562 | // mainThread is on the ready queue when this call is made.
|
---|
563 | kernel_first_resume( TL_GET( this_processor ) );
|
---|
564 |
|
---|
565 |
|
---|
566 |
|
---|
567 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
---|
568 | __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
|
---|
569 |
|
---|
570 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
571 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
572 | verify( TL_GET( preemption_state ).enabled );
|
---|
573 | }
|
---|
574 |
|
---|
575 | void kernel_shutdown(void) {
|
---|
576 | __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
|
---|
577 |
|
---|
578 | verify( TL_GET( preemption_state ).enabled );
|
---|
579 | disable_interrupts();
|
---|
580 | verify( ! TL_GET( preemption_state ).enabled );
|
---|
581 |
|
---|
582 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
---|
583 | // When its coroutine terminates, it return control to the mainThread
|
---|
584 | // which is currently here
|
---|
585 | mainProcessor->do_terminate = true;
|
---|
586 | returnToKernel();
|
---|
587 |
|
---|
588 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
---|
589 |
|
---|
590 | // Disable preemption
|
---|
591 | kernel_stop_preemption();
|
---|
592 |
|
---|
593 | // Destroy the main processor and its context in reverse order of construction
|
---|
594 | // These were manually constructed so we need manually destroy them
|
---|
595 | ^(mainProcessor->runner){};
|
---|
596 | ^(mainProcessor){};
|
---|
597 |
|
---|
598 | // Final step, destroy the main thread since it is no longer needed
|
---|
599 | // Since we provided a stack to this taxk it will not destroy anything
|
---|
600 | ^(mainThread){};
|
---|
601 |
|
---|
602 | __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
|
---|
603 | }
|
---|
604 |
|
---|
605 | //=============================================================================================
|
---|
606 | // Unexpected Terminating logic
|
---|
607 | //=============================================================================================
|
---|
608 |
|
---|
609 |
|
---|
610 | static __spinlock_t kernel_abort_lock;
|
---|
611 | static __spinlock_t kernel_debug_lock;
|
---|
612 | static bool kernel_abort_called = false;
|
---|
613 |
|
---|
614 | void * kernel_abort (void) __attribute__ ((__nothrow__)) {
|
---|
615 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when
|
---|
616 | // the globalAbort flag is true.
|
---|
617 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
|
---|
618 |
|
---|
619 | // first task to abort ?
|
---|
620 | if ( ! kernel_abort_called ) { // not first task to abort ?
|
---|
621 | kernel_abort_called = true;
|
---|
622 | unlock( kernel_abort_lock );
|
---|
623 | }
|
---|
624 | else {
|
---|
625 | unlock( kernel_abort_lock );
|
---|
626 |
|
---|
627 | sigset_t mask;
|
---|
628 | sigemptyset( &mask );
|
---|
629 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals
|
---|
630 | sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals
|
---|
631 | sigsuspend( &mask ); // block the processor to prevent further damage during abort
|
---|
632 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
|
---|
633 | }
|
---|
634 |
|
---|
635 | return TL_GET( this_thread );
|
---|
636 | }
|
---|
637 |
|
---|
638 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
|
---|
639 | thread_desc * thrd = kernel_data;
|
---|
640 |
|
---|
641 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd );
|
---|
642 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
643 |
|
---|
644 | if ( get_coroutine(thrd) != TL_GET( this_coroutine ) ) {
|
---|
645 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", TL_GET( this_coroutine )->name, TL_GET( this_coroutine ) );
|
---|
646 | __cfaabi_dbg_bits_write( abort_text, len );
|
---|
647 | }
|
---|
648 | else {
|
---|
649 | __cfaabi_dbg_bits_write( ".\n", 2 );
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
---|
654 | return get_coroutine(TL_GET( this_thread )) == get_coroutine(mainThread) ? 4 : 2;
|
---|
655 | }
|
---|
656 |
|
---|
657 | extern "C" {
|
---|
658 | void __cfaabi_dbg_bits_acquire() {
|
---|
659 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
---|
660 | }
|
---|
661 |
|
---|
662 | void __cfaabi_dbg_bits_release() {
|
---|
663 | unlock( kernel_debug_lock );
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | //=============================================================================================
|
---|
668 | // Kernel Utilities
|
---|
669 | //=============================================================================================
|
---|
670 | //-----------------------------------------------------------------------------
|
---|
671 | // Locks
|
---|
672 | void ?{}( semaphore & this, int count = 1 ) {
|
---|
673 | (this.lock){};
|
---|
674 | this.count = count;
|
---|
675 | (this.waiting){};
|
---|
676 | }
|
---|
677 | void ^?{}(semaphore & this) {}
|
---|
678 |
|
---|
679 | void P(semaphore & this) with( this ){
|
---|
680 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
681 | count -= 1;
|
---|
682 | if ( count < 0 ) {
|
---|
683 | // queue current task
|
---|
684 | append( waiting, (thread_desc *)TL_GET( this_thread ) );
|
---|
685 |
|
---|
686 | // atomically release spin lock and block
|
---|
687 | BlockInternal( &lock );
|
---|
688 | }
|
---|
689 | else {
|
---|
690 | unlock( lock );
|
---|
691 | }
|
---|
692 | }
|
---|
693 |
|
---|
694 | void V(semaphore & this) with( this ) {
|
---|
695 | thread_desc * thrd = NULL;
|
---|
696 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
697 | count += 1;
|
---|
698 | if ( count <= 0 ) {
|
---|
699 | // remove task at head of waiting list
|
---|
700 | thrd = pop_head( waiting );
|
---|
701 | }
|
---|
702 |
|
---|
703 | unlock( lock );
|
---|
704 |
|
---|
705 | // make new owner
|
---|
706 | WakeThread( thrd );
|
---|
707 | }
|
---|
708 |
|
---|
709 | //-----------------------------------------------------------------------------
|
---|
710 | // Debug
|
---|
711 | __cfaabi_dbg_debug_do(
|
---|
712 | struct {
|
---|
713 | thread_desc * tail;
|
---|
714 | } __cfaabi_dbg_thread_list = { NULL };
|
---|
715 |
|
---|
716 | void __cfaabi_dbg_thread_register( thread_desc * thrd ) {
|
---|
717 | if( !__cfaabi_dbg_thread_list.tail ) {
|
---|
718 | __cfaabi_dbg_thread_list.tail = thrd;
|
---|
719 | return;
|
---|
720 | }
|
---|
721 | __cfaabi_dbg_thread_list.tail->dbg_next = thrd;
|
---|
722 | thrd->dbg_prev = __cfaabi_dbg_thread_list.tail;
|
---|
723 | __cfaabi_dbg_thread_list.tail = thrd;
|
---|
724 | }
|
---|
725 |
|
---|
726 | void __cfaabi_dbg_thread_unregister( thread_desc * thrd ) {
|
---|
727 | thread_desc * prev = thrd->dbg_prev;
|
---|
728 | thread_desc * next = thrd->dbg_next;
|
---|
729 |
|
---|
730 | if( next ) { next->dbg_prev = prev; }
|
---|
731 | else {
|
---|
732 | assert( __cfaabi_dbg_thread_list.tail == thrd );
|
---|
733 | __cfaabi_dbg_thread_list.tail = prev;
|
---|
734 | }
|
---|
735 |
|
---|
736 | if( prev ) { prev->dbg_next = next; }
|
---|
737 |
|
---|
738 | thrd->dbg_prev = NULL;
|
---|
739 | thrd->dbg_next = NULL;
|
---|
740 | }
|
---|
741 |
|
---|
742 | void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
|
---|
743 | this.prev_name = prev_name;
|
---|
744 | this.prev_thrd = TL_GET( this_thread );
|
---|
745 | }
|
---|
746 | )
|
---|
747 | // Local Variables: //
|
---|
748 | // mode: c //
|
---|
749 | // tab-width: 4 //
|
---|
750 | // End: //
|
---|