diff options
author | Christian Cunningham <cc@localhost> | 2022-03-10 20:01:25 -0800 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2022-03-10 20:01:25 -0800 |
commit | ad9e577e8b2f6431d48a6a64fd95aff432e48441 (patch) | |
tree | 598ade41d70d616cf0891855732c0957835cd465 /src | |
parent | 0d80865f669c2314c905f94a4117300a33338792 (diff) |
More Deterministic
Diffstat (limited to 'src')
-rw-r--r-- | src/drivers/uart.c | 1 | ||||
-rw-r--r-- | src/exceptions/svc.S | 18 | ||||
-rw-r--r-- | src/globals.c | 13 | ||||
-rw-r--r-- | src/lib/kmem.c | 53 | ||||
-rw-r--r-- | src/lib/strings.c | 8 | ||||
-rw-r--r-- | src/sys/schedule.S | 2 | ||||
-rw-r--r-- | src/sys/schedule.c | 67 | ||||
-rw-r--r-- | src/tests/test.S | 2 | ||||
-rw-r--r-- | src/util/lock.c | 8 | ||||
-rw-r--r-- | src/util/mutex.c | 52 | ||||
-rw-r--r-- | src/util/status.c | 10 |
11 files changed, 43 insertions, 191 deletions
diff --git a/src/drivers/uart.c b/src/drivers/uart.c index e4ae2d1..deaa84a 100644 --- a/src/drivers/uart.c +++ b/src/drivers/uart.c @@ -74,7 +74,6 @@ void uart_10(unsigned long val) { char* dptr = u32_to_str(val); uart_string(dptr); - kfree(dptr); } void uart_hexn(unsigned long c_val) diff --git a/src/exceptions/svc.S b/src/exceptions/svc.S index c23df82..1b0cc1f 100644 --- a/src/exceptions/svc.S +++ b/src/exceptions/svc.S @@ -33,22 +33,14 @@ svc_000001: // Get time svc_000002: // Run Schedule ldmfd sp!, {r0-r12,lr} b schedule -svc_000003: // Clean task stack +svc_000003: // Free Thread in Table ldr r3, =scheduler ldr r2, [r3, #0] // struct Thread* rthread - ldr r1, [r2, #8] // sp_base - cmp r1, #-1 - beq svc_exit - ldr r3, =stacks_table + ldr r1, [r2, #0x1c] // thread offset mov r0, #0 - strb r0, [r3, r1] - // Free the thread after freeing the stack - mov r0, r2 - bl kfree - ldr r3, =sched_stack_count - ldr r2, [r3] - sub r2, #1 - str r2, [r3] + ldr r2, =thread_table + add r2, r1 + str r0, [r2] b svc_exit svc_000004: // Lock Lock (usr_r0 = struct Lock*) ldr r3, =scheduler diff --git a/src/globals.c b/src/globals.c index e94580c..29760b6 100644 --- a/src/globals.c +++ b/src/globals.c @@ -1,6 +1,4 @@ #define GLOBALS_C -#include <lib/kmem.h> -#include <util/mutex.h> #include <sys/schedule.h> char* os_name = "Jobbed"; #ifndef VERSION @@ -9,10 +7,7 @@ char* os_info_v = "?"; char* os_info_v = VERSION; #endif -__attribute__((section(".bss.kmem"))) unsigned char kmem_begin[0x2000000]; -__attribute__((section(".bss"))) unsigned char kmem_lookup[0xD000]; __attribute__((section(".bss"))) unsigned long nextpid; -__attribute__((section(".bss"))) unsigned long sched_stack_count; __attribute__((section(".bss"))) unsigned long stimel; __attribute__((section(".bss"))) unsigned long stimeh; __attribute__((section(".bss"))) struct Scheduler scheduler; @@ -21,4 +16,10 @@ __attribute__((section(".bss"))) unsigned int gwidth; __attribute__((section(".bss"))) unsigned int gheight; __attribute__((section(".bss"))) unsigned int gpitch; __attribute__((section(".bss"))) unsigned int gisrgb; -__attribute__((section(".bss"))) unsigned char stacks_table[MAX_THREADS]; +// 0 - Free +// 1 - Ready +// 2 - Waiting for Mutex +// 3 - Waiting for Signal +// 4+ - Reserved +__attribute__((section(".bss.threadl"))) unsigned char thread_table[MAX_THREADS]; +__attribute__((section(".bss.threads"))) struct Thread threads[MAX_THREADS]; diff --git a/src/lib/kmem.c b/src/lib/kmem.c index 931d5e3..0419662 100644 --- a/src/lib/kmem.c +++ b/src/lib/kmem.c @@ -2,59 +2,6 @@ #include <drivers/uart.h> #include <lib/kmem.h> -void* kmalloc(unsigned int size) -{ - unsigned int sz = 1; - while (sz < size && sz < 0x1000) - sz *= 2; - unsigned long offset = 0x1000*(sz/2); - unsigned int exp = 0; - unsigned int tmp = sz; - while (tmp != 0) { - exp++; - tmp = tmp >> 1; - } - unsigned int i = 0; - while (i < 0x1000) { - if (kmem_lookup[0x1000*exp + i] == 0) { - kmem_lookup[0x1000*exp + i] = 1; - return (void*)kmem_begin + offset + i*sz; - } - i++; - } - return 0; -} - -void* kcalloc(unsigned int size) -{ - unsigned char* ptr = kmalloc(size); - if (ptr == 0) - return 0; - for(unsigned int i = 0; i < size; i++) - ptr[i] = 0; - return ptr; -} - -void kfree(void* ptr) -{ - if (!((unsigned long)kmem_begin <= (unsigned long)ptr && (unsigned long)ptr < (unsigned long)kmem_begin + 0x200000)) - return; - unsigned long size = 1; - while (!((unsigned long)kmem_begin + 0x1000*(size/2) <= (unsigned long)ptr && (unsigned long)ptr < (unsigned long)kmem_begin + 0x1000*size)) { - size *= 2; - } - void* base = (void*)((unsigned long)ptr - ((unsigned long)ptr % size)); - void* block_base = kmem_begin + 0x1000*(size/2); - unsigned int lookup_offset = (base - block_base)/size; - unsigned int exp = 0; - unsigned int tmp = size; - while (tmp != 0) { - exp++; - tmp = tmp >> 1; - } - kmem_lookup[0x1000*exp + lookup_offset] = 0; -} - void kmemshow32(void* data, unsigned long length) { unsigned long* ptr = data; diff --git a/src/lib/strings.c b/src/lib/strings.c index 53caaec..3155afa 100644 --- a/src/lib/strings.c +++ b/src/lib/strings.c @@ -36,7 +36,7 @@ unsigned char strcmpn(string_t a, string_t b, unsigned int n) char* zhex32_to_str(unsigned long value) { - char* data = kcalloc(9); + static char data[10]; char tmp = 0; char isz = -1; for (int i = 0; i < 8; i++) { @@ -53,7 +53,7 @@ char* zhex32_to_str(unsigned long value) char* hex32_to_str(unsigned long value) { - char* data = kcalloc(9); + static char data[10]; char tmp = 0; for (int i = 0; i < 8; i++) { tmp = (value >> 4*(8-i-1))&0xF; @@ -69,7 +69,7 @@ char* u32_to_str(unsigned long value) { unsigned long t = value; unsigned long c; - char* data = kcalloc(11); + static char data[12]; char* dptr = data + 9; for (int i = 0; i <= 10; i++) { c = t%10; @@ -91,7 +91,7 @@ char* s32_to_str(unsigned long value) t = -t; is_neg = 1; } - char* data = kcalloc(12); + static char data[13]; char* dptr = data + 10; for (int i = 0; i <= 10; i++) { c = t%10; diff --git a/src/sys/schedule.S b/src/sys/schedule.S index e44d12b..d7301fc 100644 --- a/src/sys/schedule.S +++ b/src/sys/schedule.S @@ -35,7 +35,7 @@ cleanup: mov r1, #0 1: str r1, [r0, #0] - // cleanup stack + // Free thread in table svc #3 // usrloop -> rthread ldr r3, =scheduler diff --git a/src/sys/schedule.c b/src/sys/schedule.c index b068963..8c65c6f 100644 --- a/src/sys/schedule.c +++ b/src/sys/schedule.c @@ -34,71 +34,54 @@ void init_scheduler(void) trb += 1; } } - sched_stack_count = 0; // Initialize nextpid nextpid = FIRST_AVAIL_PID; + + for (unsigned long i = 0; i < MAX_THREADS; i++) { + struct Thread* t = &threads[i]; + t->offset = i; + t->sp_base = 0x20000000 - STACK_SIZE*i; + } } -struct RStack get_stack(void) +struct Thread* get_available_thread(void) { - struct RStack r = {.sp = 0, .idx = -1}; - // Find an available stack - for (int i = 0; i < MAX_THREADS; i++) { - if (stacks_table[i] == 0) { - // Mark unavailable - stacks_table[i] = 1; - r.idx = i; - r.sp = (void*)0x20000000 - STACK_SIZE*i; - return r; - } + for(unsigned long i = 0; i < MAX_THREADS; i++) { + if (thread_table[i] == 0) + return &threads[i]; } - return r; + return 0; } void add_thread(void* pc, void* arg, unsigned char priority) { - //void* sp = get_stack(); - struct RStack r = get_stack(); - //struct Thread* thread = (struct Thread*)malloca(sizeof(struct Thread), 4); - struct Thread* thread = (struct Thread*)kmalloc(sizeof(struct Thread)); + struct Thread* thread = get_available_thread(); + thread_table[thread->offset] = 1; thread->pc = pc; - // Valid stack has been obtained for this thread - if (r.sp) { - thread->sp_base = r.idx; - unsigned long* argp = r.sp; - argp -= 13; - *argp = (unsigned long)arg; // Set r0 to the argument - argp -= 1; - *(unsigned long**)argp = (unsigned long*)cleanup; // Set lr to the cleanup function - thread->sp = (void*)argp; - thread->status = THREAD_READY; - sched_stack_count++; - } - // Couldn't allocate a proper stack - else { - thread->sp_base = r.idx; - thread->sp = r.sp; - thread->status = THREAD_SERROR; - } + unsigned long* argp = (void*)thread->sp_base; + argp -= 13; + *argp = (unsigned long)arg; // Set r0 to the argument + argp -= 1; + *(unsigned long**)argp = (unsigned long*)cleanup; // Set lr to the cleanup function + thread->sp = argp; + thread->status = THREAD_READY; thread->mptr = (void*)0; thread->pid = nextpid++; // Reset next pid on overflow if (nextpid < FIRST_AVAIL_PID) { nextpid = FIRST_AVAIL_PID; } - thread->priority = priority % PRIORITIES; + if (priority >= PRIORITIES) + thread->priority = PRIORITIES - 1; + else + thread->priority = priority; thread->old_priority = -1; thread->preempt = 0; // Add Thread* to scheduler's appropriate buffer struct ThreadQueues* tq = &scheduler.thread_queues[thread->priority]; struct ThreadRotBuffer* trb; // Add to stack error queue if stack was not obtained - if (thread->status == THREAD_SERROR) { - trb = &tq->serror; - } - else { - trb = &tq->ready; - } + trb = &tq->ready; trb->queue[trb->woffset++] = thread; trb->woffset %= TQUEUE_MAX; // Schedule if this was called in usermode diff --git a/src/tests/test.S b/src/tests/test.S index ebb901c..035f8f6 100644 --- a/src/tests/test.S +++ b/src/tests/test.S @@ -1,6 +1,6 @@ .section .text -btest: +a.btest: push {lr} pop {pc} diff --git a/src/util/lock.c b/src/util/lock.c index 6c7d07e..95f93af 100644 --- a/src/util/lock.c +++ b/src/util/lock.c @@ -1,6 +1,5 @@ #include <cpu.h> #include <cpu/atomic/swap.h> -#include <lib/kmem.h> #include <util/lock.h> // TODO: Improve locking for system @@ -25,10 +24,3 @@ void unlock(struct Lock* l) atm_release((unsigned long*)l); } } - -struct Lock* create_lock(void) -{ - struct Lock* l = (struct Lock*)kmalloc(sizeof(struct Lock)); - l->pid = 0; - return l; -} diff --git a/src/util/mutex.c b/src/util/mutex.c index 995ef62..e69de29 100644 --- a/src/util/mutex.c +++ b/src/util/mutex.c @@ -1,52 +0,0 @@ -#include <cpu/atomic/swap.h> -#include <globals.h> -#include <lib/kmem.h> -#include <sys/schedule.h> -#include <util/mutex.h> - -///unsigned char lock_mutex(struct Mutex* m, unsigned long pid) -///{ -/// if (m->pid == NULL_PID) { -/// // Use currently running thread's PID if no pid given -/// if (pid == 0) { -/// struct Thread* thread = scheduler.rthread; -/// atm_lock(thread->pid, &m->pid); -/// } else { -/// atm_lock(pid, &m->pid); -/// } -/// return 0; -/// } -/// struct Thread* thread = scheduler.rthread; -/// thread->status = THREAD_MWAIT; -/// thread->mptr = m; -/// return 1; -///} -/// -///// Eventually, there will need to be a hook into the scheduling mechanism -///// that checks the currently running process and check that against the -///// mutex's pid lock -///unsigned char release_mutex(struct Mutex* m, unsigned long pid) -///{ -/// // Use current thread's PID if no pid -/// if (pid == 0) { -/// struct Thread* thread = scheduler.rthread; -/// if (m->pid == thread->pid) { -/// atm_release(&m->pid); -/// return 0; -/// } -/// } -/// else if (m->pid == pid) { -/// atm_release(&m->pid); -/// return 0; -/// } -/// return 1; -///} - -struct Mutex* create_mutex(void* addr) -{ - // Ensure aligned to word - Important for Atomic Swap - struct Mutex* m = (struct Mutex*)kmalloc(sizeof(struct Mutex)); - m->addr = addr; - m->pid = 0; - return m; -} diff --git a/src/util/status.c b/src/util/status.c index 5aef915..828dfa3 100644 --- a/src/util/status.c +++ b/src/util/status.c @@ -64,11 +64,6 @@ void status(void) draw_cstring(9, 0, "v", 0x00FFFF); draw_cstring(0, 0, os_name, 0xFF0000); draw_cstring(10, 0, os_info_v, 0x00FFFF); - draw_cstring(16, 0, "# TStacks:", 0xFFDF00); - draw_string(27, 0, " "); - char* st_str = u32_to_str(sched_stack_count); - draw_cstring(27, 0, st_str, 0xFFDF00); - kfree(st_str); // GPU IRQ Statuses output_irq_status(); @@ -81,7 +76,6 @@ void status(void) char* frq_str = u32_to_str(frq); unsigned long fs_len = strlen(frq_str)+1; draw_string(8, 3, frq_str); - kfree(frq_str); draw_string(8+fs_len, 3, "kHz"); // Output the value unsigned long v = read_cntv_tval(); @@ -89,7 +83,6 @@ void status(void) unsigned long vs_len = strlen(v_str) + 1; draw_string(8+fs_len+4, 3, v_str); draw_string(8+fs_len+4 +vs_len, 3, " "); - kfree(v_str); draw_letter(8+fs_len+4 +vs_len+1, 3, '|'); draw_hex32(8+fs_len+7+vs_len, 3, v); @@ -99,12 +92,10 @@ void status(void) gwidth_str = u32_to_str(gwidth); unsigned long gs_len = strlen(gwidth_str) + 1; draw_string(6, 4, gwidth_str); - kfree(gwidth_str); draw_letter(6+gs_len-1, 4, 'x'); gwidth_str = u32_to_str(gheight); unsigned long gs_len1 = strlen(gwidth_str) + 1; draw_string(6+gs_len, 4, gwidth_str); - kfree(gwidth_str); if(gisrgb) draw_string(6+gs_len+gs_len1, 4, "RGB"); else @@ -143,5 +134,4 @@ void status(void) draw_string(19+14+18, 8, " "); char* t_str = u32_to_str(((unsigned long)tval)/1000000); draw_string(19+14+18, 8, t_str); - kfree(t_str); } |