// -*- Mode: CFA -*- // // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // kernel.c -- // // Author : Thierry Delisle // Created On : Tue Jan 17 12:27:26 2016 // Last Modified By : Thierry Delisle // Last Modified On : -- // Update Count : 0 // //Header #include "kernel" //C Includes #include extern "C" { #include } //CFA Includes #include "libhdr.h" #include "threads" //Private includes #define __CFA_INVOKE_PRIVATE__ #include "invoke.h" cluster * systemCluster; processor * systemProcessor; thread_h * mainThread; void kernel_startup(void) __attribute__((constructor(101))); void kernel_shutdown(void) __attribute__((destructor(101))); void ?{}(processor * this, cluster * cltr) { this->ctx = NULL; this->cltr = cltr; this->terminated = false; } void ^?{}(processor * this) {} void ?{}(cluster * this) { ( &this->ready_queue ){}; } void ^?{}(cluster * this) {} //----------------------------------------------------------------------------- // Global state /*thread_local*/ processor * this_processor; coroutine * this_coroutine(void) { return this_processor->current_coroutine; } thread_h * this_thread(void) { return this_processor->current_thread; } //----------------------------------------------------------------------------- // Processor coroutine struct processorCtx_t { processor * proc; coroutine c; }; DECL_COROUTINE(processorCtx_t) void ?{}(processorCtx_t * this, processor * proc) { (&this->c){}; this->proc = proc; } void CtxInvokeProcessor(processor * proc) { processorCtx_t proc_cor_storage = {proc}; resume( &proc_cor_storage ); } //----------------------------------------------------------------------------- // Processor running routines void main(processorCtx_t * ctx); thread_h * nextThread(cluster * this); void runThread(processor * this, thread_h * dst); void spin(processor * this, unsigned int * spin_count); void main(processorCtx_t * ctx) { processor * this = ctx->proc; LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this); thread_h * readyThread = NULL; for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) { readyThread = nextThread( this->cltr ); if(readyThread) { runThread(this, readyThread); spin_count = 0; } else { spin(this, &spin_count); } } LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this); } void runThread(processor * this, thread_h * dst) { coroutine * proc_ctx = get_coroutine(this->ctx); coroutine * thrd_ctx = get_coroutine(dst); thrd_ctx->last = proc_ctx; // context switch to specified coroutine // Which is now the current_coroutine // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine); this->current_thread = dst; this->current_coroutine = thrd_ctx; CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context ); this->current_coroutine = proc_ctx; // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine); // when CtxSwitch returns we are back in the processor coroutine } void spin(processor * this, unsigned int * spin_count) { (*spin_count)++; } //----------------------------------------------------------------------------- // Scheduler routines void thread_schedule( thread_h * thrd ) { assertf( thrd->next == NULL, "Expected null got %p", thrd->next ); append( &systemProcessor->cltr->ready_queue, thrd ); } thread_h * nextThread(cluster * this) { return pop_head( &this->ready_queue ); } //----------------------------------------------------------------------------- // Kernel storage #define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)] KERNEL_STORAGE(processorCtx_t, systemProcessorCtx); KERNEL_STORAGE(cluster, systemCluster); KERNEL_STORAGE(processor, systemProcessor); KERNEL_STORAGE(thread_h, mainThread); KERNEL_STORAGE(machine_context_t, mainThread_context); //----------------------------------------------------------------------------- // Main thread construction struct mainThread_info_t { machine_context_t ctx; unsigned int size; // size of stack void *base; // base of stack void *storage; // pointer to stack void *limit; // stack grows towards stack limit void *context; // address of cfa_context_t void *top; // address of top of storage }; void ?{}( mainThread_info_t * this ) { CtxGet( &this->ctx ); this->base = this->ctx.FP; this->storage = this->ctx.SP; rlimit r; int ret = getrlimit( RLIMIT_STACK, &r); this->size = r.rlim_cur; this->limit = (void *)(((intptr_t)this->base) - this->size); this->context = &mainThread_context_storage; this->top = this->base; } void ?{}( coStack_t * this, mainThread_info_t * info) { this->size = info->size; this->storage = info->storage; this->limit = info->limit; this->base = info->base; this->context = info->context; this->top = info->top; this->userStack = true; } void ?{}( coroutine * this, mainThread_info_t * info) { (&this->stack){ info }; this->name = "Main Thread"; this->errno_ = 0; this->state = Inactive; this->notHalted = true; } void ?{}( thread_h * this, mainThread_info_t * info) { (&this->c){ info }; } //----------------------------------------------------------------------------- // Kernel boot procedures void kernel_startup(void) { // SKULLDUGGERY: the mainThread steals the process main thread // which will then be scheduled by the systemProcessor normally LIB_DEBUG_PRINTF("Kernel : Starting\n"); mainThread_info_t ctx; // Start by initializing the main thread mainThread = (thread_h *)&mainThread_storage; mainThread{ &ctx }; // Initialize the system cluster systemCluster = (cluster *)&systemCluster_storage; systemCluster{}; // Initialize the system processor systemProcessor = (processor *)&systemProcessor_storage; systemProcessor{ systemCluster }; // Initialize the system processor ctx // (the coroutine that contains the processing control flow) systemProcessor->ctx = (processorCtx_t *)&systemProcessorCtx_storage; systemProcessor->ctx{ systemProcessor }; // Add the main thread to the ready queue // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread thread_schedule(mainThread); //initialize the global state variables this_processor = systemProcessor; this_processor->current_thread = mainThread; this_processor->current_coroutine = &mainThread->c; // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that // mainThread is on the ready queue when this call is made. resume(systemProcessor->ctx); // THE SYSTEM IS NOW COMPLETELY RUNNING LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n"); } void kernel_shutdown(void) { LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n"); // SKULLDUGGERY: Notify the systemProcessor it needs to terminates. // When its coroutine terminates, it return control to the mainThread // which is currently here systemProcessor->terminated = true; suspend(); // THE SYSTEM IS NOW COMPLETELY STOPPED // Destroy the system processor and its context in reverse order of construction // These were manually constructed so we need manually destroy them ^(systemProcessor->ctx){}; ^(systemProcessor){}; // Final step, destroy the main thread since it is no longer needed // Since we provided a stack to this taxk it will not destroy anything ^(mainThread){}; LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n"); } //----------------------------------------------------------------------------- // Locks void ?{}( simple_lock * this ) { ( &this->blocked ){}; } void ^?{}( simple_lock * this ) { } void lock( simple_lock * this ) { append( &this->blocked, this_thread() ); suspend(); } void unlock( simple_lock * this ) { thread_h * it; while( it = pop_head( &this->blocked) ) { thread_schedule( it ); } } //----------------------------------------------------------------------------- // Queues void ?{}( simple_thread_list * this ) { this->head = NULL; this->tail = &this->head; } void append( simple_thread_list * this, thread_h * t ) { assert( t->next == NULL ); *this->tail = t; this->tail = &t->next; } thread_h * pop_head( simple_thread_list * this ) { thread_h * head = this->head; if( head ) { this->head = head->next; if( !head->next ) { this->tail = &this->head; } head->next = NULL; } return head; } // Local Variables: // // mode: c // // tab-width: 4 // // End: //