| [8118303] | 1 | // -*- Mode: CFA -*-
|
|---|
| 2 | //
|
|---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
|---|
| 4 | //
|
|---|
| 5 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 6 | // file "LICENCE" distributed with Cforall.
|
|---|
| 7 | //
|
|---|
| 8 | // kernel.c --
|
|---|
| 9 | //
|
|---|
| 10 | // Author : Thierry Delisle
|
|---|
| 11 | // Created On : Tue Jan 17 12:27:26 2016
|
|---|
| 12 | // Last Modified By : Thierry Delisle
|
|---|
| 13 | // Last Modified On : --
|
|---|
| 14 | // Update Count : 0
|
|---|
| 15 | //
|
|---|
| 16 |
|
|---|
| 17 | //Header
|
|---|
| 18 | #include "kernel"
|
|---|
| 19 |
|
|---|
| 20 | //C Includes
|
|---|
| [c84e80a] | 21 | #include <stddef.h>
|
|---|
| [eb2e723] | 22 | extern "C" {
|
|---|
| 23 | #include <sys/resource.h>
|
|---|
| 24 | }
|
|---|
| [8118303] | 25 |
|
|---|
| 26 | //CFA Includes
|
|---|
| 27 | #include "libhdr.h"
|
|---|
| 28 | #include "threads"
|
|---|
| 29 |
|
|---|
| 30 | //Private includes
|
|---|
| 31 | #define __CFA_INVOKE_PRIVATE__
|
|---|
| 32 | #include "invoke.h"
|
|---|
| 33 |
|
|---|
| [eb2e723] | 34 | processor * systemProcessor;
|
|---|
| 35 | thread_h * mainThread;
|
|---|
| 36 |
|
|---|
| 37 | void kernel_startup(void) __attribute__((constructor(101)));
|
|---|
| 38 | void kernel_shutdown(void) __attribute__((destructor(101)));
|
|---|
| [8118303] | 39 |
|
|---|
| [c84e80a] | 40 | void ?{}(processor * this) {
|
|---|
| [eb2e723] | 41 | this->ctx = NULL;
|
|---|
| [c84e80a] | 42 | this->thread_index = 0;
|
|---|
| 43 | this->thread_count = 10;
|
|---|
| 44 | this->terminated = false;
|
|---|
| 45 |
|
|---|
| 46 | for(int i = 0; i < 10; i++) {
|
|---|
| 47 | this->threads[i] = NULL;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | LIB_DEBUG_PRINTF("Processor : ctor for core %p (core spots %d)\n", this, this->thread_count);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void ^?{}(processor * this) {
|
|---|
| 54 |
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | //-----------------------------------------------------------------------------
|
|---|
| 58 | // Processor coroutine
|
|---|
| [eb2e723] | 59 | struct processorCtx_t {
|
|---|
| [c84e80a] | 60 | processor * proc;
|
|---|
| 61 | coroutine c;
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| [eb2e723] | 64 | DECL_COROUTINE(processorCtx_t)
|
|---|
| [c84e80a] | 65 |
|
|---|
| [eb2e723] | 66 | void ?{}(processorCtx_t * this, processor * proc) {
|
|---|
| 67 | (&this->c){};
|
|---|
| [c84e80a] | 68 | this->proc = proc;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void CtxInvokeProcessor(processor * proc) {
|
|---|
| [eb2e723] | 72 | processorCtx_t proc_cor_storage = {proc};
|
|---|
| [c84e80a] | 73 | resume( &proc_cor_storage );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | //-----------------------------------------------------------------------------
|
|---|
| 77 | // Processor running routines
|
|---|
| [eb2e723] | 78 | void main(processorCtx_t * ctx);
|
|---|
| [c84e80a] | 79 | thread_h * nextThread(processor * this);
|
|---|
| 80 | void runThread(processor * this, thread_h * dst);
|
|---|
| 81 | void spin(processor * this, unsigned int * spin_count);
|
|---|
| [8118303] | 82 |
|
|---|
| [eb2e723] | 83 | void main(processorCtx_t * ctx) {
|
|---|
| 84 | processor * this = ctx->proc;
|
|---|
| 85 | LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
|
|---|
| [8118303] | 86 |
|
|---|
| [c84e80a] | 87 | thread_h * readyThread = NULL;
|
|---|
| 88 | for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) {
|
|---|
| 89 |
|
|---|
| 90 | readyThread = nextThread(this);
|
|---|
| [8118303] | 91 |
|
|---|
| [c84e80a] | 92 | if(readyThread) {
|
|---|
| 93 | runThread(this, readyThread);
|
|---|
| 94 | spin_count = 0;
|
|---|
| 95 | } else {
|
|---|
| 96 | spin(this, &spin_count);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| [8118303] | 99 |
|
|---|
| [c84e80a] | 100 | LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | thread_h * nextThread(processor * this) {
|
|---|
| 104 | for(int i = 0; i < this->thread_count; i++) {
|
|---|
| 105 | this->thread_index = (this->thread_index + 1) % this->thread_count;
|
|---|
| 106 |
|
|---|
| 107 | thread_h * thrd = this->threads[this->thread_index];
|
|---|
| 108 | if(thrd) return thrd;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return NULL;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | void runThread(processor * this, thread_h * dst) {
|
|---|
| [eb2e723] | 115 | coroutine * proc_ctx = get_coroutine(this->ctx);
|
|---|
| [c84e80a] | 116 | coroutine * thrd_ctx = get_coroutine(dst);
|
|---|
| 117 | thrd_ctx->last = proc_ctx;
|
|---|
| 118 |
|
|---|
| 119 | // context switch to specified coroutine
|
|---|
| 120 | // Which is now the current_coroutine
|
|---|
| [8f49a54] | 121 | // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
|
|---|
| [c84e80a] | 122 | current_coroutine = thrd_ctx;
|
|---|
| 123 | CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context );
|
|---|
| 124 | current_coroutine = proc_ctx;
|
|---|
| [8f49a54] | 125 | // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
|
|---|
| [c84e80a] | 126 |
|
|---|
| 127 | // when CtxSwitch returns we are back in the processor coroutine
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void spin(processor * this, unsigned int * spin_count) {
|
|---|
| 131 | (*spin_count)++;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | //-----------------------------------------------------------------------------
|
|---|
| 135 | // Kernel runner (Temporary)
|
|---|
| 136 |
|
|---|
| [eb2e723] | 137 | void scheduler_add( thread_h * thrd ) {
|
|---|
| [c84e80a] | 138 | for(int i = 0; i < systemProcessor->thread_count; i++) {
|
|---|
| 139 | if(systemProcessor->threads[i] == NULL) {
|
|---|
| 140 | systemProcessor->threads[i] = thrd;
|
|---|
| 141 | return;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| [8f49a54] | 144 | assertf(false, "Scheduler full");
|
|---|
| [c84e80a] | 145 | }
|
|---|
| 146 |
|
|---|
| [eb2e723] | 147 | void scheduler_remove( thread_h * thrd ) {
|
|---|
| [c84e80a] | 148 | for(int i = 0; i < systemProcessor->thread_count; i++) {
|
|---|
| 149 | if(systemProcessor->threads[i] == thrd) {
|
|---|
| 150 | systemProcessor->threads[i] = NULL;
|
|---|
| 151 | return;
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| [8f49a54] | 154 | assertf(false, "Trying to unschedule unkown thread");
|
|---|
| [c84e80a] | 155 | }
|
|---|
| 156 |
|
|---|
| [eb2e723] | 157 | //-----------------------------------------------------------------------------
|
|---|
| 158 | // Kernel storage
|
|---|
| 159 | #define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
|
|---|
| 160 |
|
|---|
| 161 | KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
|
|---|
| 162 | KERNEL_STORAGE(processor, systemProcessor);
|
|---|
| 163 | KERNEL_STORAGE(thread_h, mainThread);
|
|---|
| 164 | KERNEL_STORAGE(machine_context_t, mainThread_context);
|
|---|
| 165 |
|
|---|
| 166 | //-----------------------------------------------------------------------------
|
|---|
| 167 | // Main thread construction
|
|---|
| 168 | struct mainThread_info_t {
|
|---|
| 169 | machine_context_t ctx;
|
|---|
| 170 | unsigned int size; // size of stack
|
|---|
| 171 | void *base; // base of stack
|
|---|
| 172 | void *storage; // pointer to stack
|
|---|
| 173 | void *limit; // stack grows towards stack limit
|
|---|
| 174 | void *context; // address of cfa_context_t
|
|---|
| 175 | void *top; // address of top of storage
|
|---|
| 176 | };
|
|---|
| 177 |
|
|---|
| 178 | void ?{}( mainThread_info_t * this ) {
|
|---|
| 179 | CtxGet( &this->ctx );
|
|---|
| 180 | this->base = this->ctx.FP;
|
|---|
| 181 | this->storage = this->ctx.SP;
|
|---|
| 182 |
|
|---|
| 183 | rlimit r;
|
|---|
| 184 | int ret = getrlimit( RLIMIT_STACK, &r);
|
|---|
| 185 | this->size = r.rlim_cur;
|
|---|
| 186 |
|
|---|
| 187 | this->limit = (void *)(((intptr_t)this->base) - this->size);
|
|---|
| 188 | this->context = &mainThread_context_storage;
|
|---|
| 189 | this->top = this->base;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | void ?{}( coStack_t * this, mainThread_info_t * info) {
|
|---|
| 193 | this->size = info->size;
|
|---|
| 194 | this->storage = info->storage;
|
|---|
| 195 | this->limit = info->limit;
|
|---|
| 196 | this->base = info->base;
|
|---|
| 197 | this->context = info->context;
|
|---|
| 198 | this->top = info->top;
|
|---|
| 199 | this->userStack = true;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | void ?{}( coroutine * this, mainThread_info_t * info) {
|
|---|
| 203 | (&this->stack){ info };
|
|---|
| 204 | this->name = "Main Thread";
|
|---|
| 205 | this->errno_ = 0;
|
|---|
| 206 | this->state = Inactive;
|
|---|
| 207 | this->notHalted = true;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void ?{}( thread_h * this, mainThread_info_t * info) {
|
|---|
| 211 | (&this->c){ info };
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | //-----------------------------------------------------------------------------
|
|---|
| 215 | // Kernel boot procedures
|
|---|
| 216 | void kernel_startup(void) {
|
|---|
| 217 |
|
|---|
| 218 | // SKULLDUGGERY: the mainThread steals the process main thread
|
|---|
| 219 | // which will then be scheduled by the systemProcessor normally
|
|---|
| 220 | LIB_DEBUG_PRINTF("Kernel : Starting\n");
|
|---|
| 221 |
|
|---|
| 222 | mainThread_info_t ctx;
|
|---|
| [8f49a54] | 223 | // LIB_DEBUG_PRINTF("Kernel : base : %p\n", ctx.base );
|
|---|
| 224 | // LIB_DEBUG_PRINTF("Kernel : top : %p\n", ctx.top );
|
|---|
| 225 | // LIB_DEBUG_PRINTF("Kernel : limit : %p\n", ctx.limit );
|
|---|
| 226 | // LIB_DEBUG_PRINTF("Kernel : size : %x\n", ctx.size );
|
|---|
| 227 | // LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage );
|
|---|
| 228 | // LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context );
|
|---|
| [eb2e723] | 229 |
|
|---|
| 230 | // Start by initializing the main thread
|
|---|
| 231 | mainThread = (thread_h *)&mainThread_storage;
|
|---|
| 232 | mainThread{ &ctx };
|
|---|
| 233 |
|
|---|
| 234 | // // Initialize the system processor
|
|---|
| 235 | systemProcessor = (processor *)&systemProcessor_storage;
|
|---|
| 236 | systemProcessor{};
|
|---|
| 237 |
|
|---|
| 238 | // Initialize the system processor ctx
|
|---|
| 239 | // (the coroutine that contains the processing control flow)
|
|---|
| 240 | systemProcessor->ctx = (processorCtx_t *)&systemProcessorCtx_storage;
|
|---|
| 241 | systemProcessor->ctx{ systemProcessor };
|
|---|
| 242 |
|
|---|
| [dcb42b8] | 243 | // Add the main thread to the ready queue
|
|---|
| 244 | // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
|
|---|
| [eb2e723] | 245 | scheduler_add(mainThread);
|
|---|
| 246 |
|
|---|
| [dcb42b8] | 247 | //initialize the global state variables
|
|---|
| [eb2e723] | 248 | current_coroutine = &mainThread->c;
|
|---|
| 249 |
|
|---|
| [dcb42b8] | 250 | // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
|
|---|
| 251 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
|
|---|
| 252 | // mainThread is on the ready queue when this call is made.
|
|---|
| [eb2e723] | 253 | resume(systemProcessor->ctx);
|
|---|
| 254 |
|
|---|
| [dcb42b8] | 255 |
|
|---|
| 256 |
|
|---|
| 257 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| [eb2e723] | 261 | LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| [dcb42b8] | 264 | void kernel_shutdown(void) {
|
|---|
| 265 | LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n");
|
|---|
| [eb2e723] | 266 |
|
|---|
| [dcb42b8] | 267 | // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
|
|---|
| 268 | // When its coroutine terminates, it return control to the mainThread
|
|---|
| 269 | // which is currently here
|
|---|
| [8f49a54] | 270 | systemProcessor->terminated = true;
|
|---|
| [eb2e723] | 271 | suspend();
|
|---|
| 272 |
|
|---|
| [dcb42b8] | 273 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
|---|
| [eb2e723] | 274 |
|
|---|
| [dcb42b8] | 275 | // Destroy the system processor and its context in reverse order of construction
|
|---|
| 276 | // These were manually constructed so we need manually destroy them
|
|---|
| [eb2e723] | 277 | ^(systemProcessor->ctx){};
|
|---|
| 278 | ^(systemProcessor){};
|
|---|
| 279 |
|
|---|
| [dcb42b8] | 280 | // Final step, destroy the main thread since it is no longer needed
|
|---|
| 281 | // Since we provided a stack to this taxk it will not destroy anything
|
|---|
| [eb2e723] | 282 | ^(mainThread){};
|
|---|
| 283 |
|
|---|
| 284 | LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");
|
|---|
| [8118303] | 285 | }
|
|---|
| 286 |
|
|---|
| 287 | // Local Variables: //
|
|---|
| 288 | // mode: c //
|
|---|
| 289 | // tab-width: 4 //
|
|---|
| 290 | // End: //
|
|---|