From 52648a99079d59b852a2389711c654c47abc0af4 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 21 Jan 2019 09:53:54 +0100 Subject: [PATCH] Fixing broken stuff --- Makefile.in | 2 +- kaleid/common/itoa.c | 18 ++++++++--- kaleid/common/rand.c | 4 +-- kaleid/common/sprintf.c | 4 +++ kaleid/common/string.c | 2 +- kaleid/include/common/kalcrt.h | 2 +- kaleid/include/common/kaldefs.h | 4 +++ kaleid/include/kalkern.h | 7 ++++ kaleid/include/kernel/kernbase.h | 1 - kaleid/kernel/proc/Makefile | 2 +- kaleid/kernel/proc/sched.c | 55 +++++++++++++++++++------------- 11 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Makefile.in b/Makefile.in index 3b7a9c6..e0deb10 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,7 +13,7 @@ CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" CC2NAME=gcc COPTIM=-O2 -CWARNS=-Wall -Wextra -Wshadow -Wpedantic +CWARNS=-Wall -Wextra -Wshadow // -Wpedantic CINCLUDES=-isystem./kaleid/include CFLAGS1=-std=gnu11 -nostdlib -ffreestanding -mcmodel=large diff --git a/kaleid/common/itoa.c b/kaleid/common/itoa.c index a5e3383..1144e26 100644 --- a/kaleid/common/itoa.c +++ b/kaleid/common/itoa.c @@ -12,8 +12,10 @@ // // Digits table for bases <=36 (unused) // +#if 0 static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +#endif // // Integer to string in any base between 2 and 36 (included) @@ -21,29 +23,35 @@ static const char digits[] = #if defined(_NEED_ITOA) -char *itoa(int i, char *str, int base) #define _IL_MIN INT_MIN #define _IL_MIN_STRING "-2147483648" +char *itoa(int i, char *str, int base) +{ + int rem; #elif defined(_NEED_LTOA) -char *ltoa(long i, char *str, int base) #define _IL_MIN LONG_MIN #define _IL_MIN_STRING "-9223372036854775808" +char *ltoa(long i, char *str, int base) +{ + long rem; #elif defined(_NEED_UTOA) char *utoa(uint i, char *str, int base) +{ + uint rem; #elif defined(_NEED_ULTOA) char *ultoa(ulong i, char *str, int base) +{ + ulong rem; #else #error "What am I supposed to declare?" #endif - -{ char *orig = str; #if defined(_NEED_ITOA) || defined(_NEED_LTOA) @@ -72,7 +80,7 @@ char *ultoa(ulong i, char *str, int base) // if (base < 2 || base > 36) { __set_errno(EINVAL); - *orig = '\0; + *orig = '\0'; goto leave; } diff --git a/kaleid/common/rand.c b/kaleid/common/rand.c index fe576ef..41ca5d9 100644 --- a/kaleid/common/rand.c +++ b/kaleid/common/rand.c @@ -20,8 +20,8 @@ static ulong next = 7756; // int rand(void) { - next = next * 1103515245 + 12345; - return (uint)(next / 65536) % INT_MAX; + next = next * 1103515245 + 12347; + return (uint)(next / 65536); } // diff --git a/kaleid/common/sprintf.c b/kaleid/common/sprintf.c index a8dd3ba..fdd48dc 100644 --- a/kaleid/common/sprintf.c +++ b/kaleid/common/sprintf.c @@ -12,6 +12,9 @@ // // Format str according to fmt using ellipsed arguments // +// BE CAREFUL when using this +// you need to know for sure an overflow won't happen +// int sprintf(char *str, const char *fmt, ...) { int ret; @@ -31,6 +34,7 @@ int vsprintf(char *str, const char *fmt, va_list ap) // // (v)sprintf() but with a size limit: no more than n bytes are written in str +// Always null-terminate str // int snprintf(char *str, size_t n, const char *fmt, ...) { diff --git a/kaleid/common/string.c b/kaleid/common/string.c index d7c391d..dd1c210 100644 --- a/kaleid/common/string.c +++ b/kaleid/common/string.c @@ -292,7 +292,7 @@ char *strncat(char *restrict dest, const char *restrict src, size_t n) // Always null-terminates, and returne TRUE or FALSE depending on whether // regular strcat() would have null-terminated this string, or not // -int *strnzcat(char *restrict dest, const char *restrict src, size_t n) +int strnzcat(char *restrict dest, const char *restrict src, size_t n) { size_t it, off = 0; diff --git a/kaleid/include/common/kalcrt.h b/kaleid/include/common/kalcrt.h index 165ba90..8b9a43e 100644 --- a/kaleid/include/common/kalcrt.h +++ b/kaleid/include/common/kalcrt.h @@ -147,7 +147,7 @@ int strnzcpy(char *restrict, const char *restrict, size_t); char *strcat (char *restrict, const char *restrict); char *strncat (char *restrict, const char *restrict, size_t); -int *strnzcat(char *restrict, const char *restrict, size_t); +int strnzcat(char *restrict, const char *restrict, size_t); char *strrev(char *restrict, const char *restrict); char *strrev2(char *); diff --git a/kaleid/include/common/kaldefs.h b/kaleid/include/common/kaldefs.h index cc43ee9..c2421d6 100644 --- a/kaleid/include/common/kaldefs.h +++ b/kaleid/include/common/kaldefs.h @@ -26,6 +26,10 @@ #define NULL 0L #endif +#ifndef INITOK +#define INITOK ((unsigned int)0xCAFEBABE) +#endif + //------------------------------------------// // Keywords // //------------------------------------------// diff --git a/kaleid/include/kalkern.h b/kaleid/include/kalkern.h index df15c7d..d1121eb 100644 --- a/kaleid/include/kalkern.h +++ b/kaleid/include/kalkern.h @@ -38,6 +38,13 @@ #include #endif +// not ready for kernel compilation +#ifndef _KALEID_KERNEL +#ifndef _KALKERN_SCHED_H +#include +#endif +#endif + //------------------------------------------// // End of header // //------------------------------------------// diff --git a/kaleid/include/kernel/kernbase.h b/kaleid/include/kernel/kernbase.h index 54307c7..ab2cb76 100644 --- a/kaleid/include/kernel/kernbase.h +++ b/kaleid/include/kernel/kernbase.h @@ -59,7 +59,6 @@ typedef enum { // Multiprocessor misc. // //------------------------------------------// - #ifndef INITOK #define INITOK ((unsigned int)0xCAFEBABE) #endif diff --git a/kaleid/kernel/proc/Makefile b/kaleid/kernel/proc/Makefile index 8001a5e..7c15976 100644 --- a/kaleid/kernel/proc/Makefile +++ b/kaleid/kernel/proc/Makefile @@ -1,4 +1,4 @@ sched-test: - gcc -O2 -masm=intel -I../../include ./sched.c + gcc -O2 -Wall -Wextra -Wshadow -std=gnu11 -masm=intel -I../../include ./sched.c diff --git a/kaleid/kernel/proc/sched.c b/kaleid/kernel/proc/sched.c index ad040b1..2a8dd9e 100644 --- a/kaleid/kernel/proc/sched.c +++ b/kaleid/kernel/proc/sched.c @@ -7,7 +7,7 @@ // Desc: Scheduling algorithm // //----------------------------------------------------------------------------// -#include +#include #ifndef _KALEID_KERNEL @@ -17,17 +17,18 @@ CREATE_PER_CPU(CurProc, Process_t *); // // For test purpose only // -int procslen = 9; +int procslen = 10; Process_t procs[] = { { 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, - { 4, 0, 0, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 4, 3, 3, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, { 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 9, 2, 2, 21, 21, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, }; #endif @@ -67,22 +68,22 @@ void SchedUnlock(void) { // The four priority classes of OS/2 // -CREATE_PER_CPU(IdlePrioProcs, ListHead_t *); -CREATE_PER_CPU(ReglPrioProcs, ListHead_t *); -CREATE_PER_CPU(ServPrioProcs, ListHead_t *); CREATE_PER_CPU(TimeCritProcs, ListHead_t *); +CREATE_PER_CPU(ServPrioProcs, ListHead_t *); +CREATE_PER_CPU(ReglPrioProcs, ListHead_t *); +CREATE_PER_CPU(IdlePrioProcs, ListHead_t *); const char *PrioClassesNames[] = { - "Idle priority class", - "Regular priority class", - "Server priority class", "Time-critical class", + "Server priority class", + "Regular priority class", + "Idle priority class", }; -enum { IDLE_PRIO_PROC = 0, - REGL_PRIO_PROC = 1, - SERV_PRIO_PROC = 2, - TIME_CRIT_PROC = 3, +enum { TIME_CRIT_PROC = 0, + SERV_PRIO_PROC = 1, + REGL_PRIO_PROC = 2, + IDLE_PRIO_PROC = 3, }; // @@ -111,8 +112,8 @@ Process_t *CompareProcs(Process_t *proc1, Process_t *proc2) { KalAssert(proc1 && proc2); - if (proc1->prioClass > proc2->prioClass) return proc1; - if (proc1->prioClass < proc2->prioClass) return proc2; + if (proc1->prioClass < proc2->prioClass) return proc1; + if (proc1->prioClass > proc2->prioClass) return proc2; if (proc1->prioLevel > proc2->prioLevel) return proc1; if (proc1->prioLevel < proc2->prioLevel) return proc2; @@ -126,7 +127,7 @@ Process_t *CompareProcs(Process_t *proc1, Process_t *proc2) static inline void SchedThisProcUnlocked(Process_t *proc) { - KalAssert(proc && proc->procState == STATE_RUNNABLE); + KalAssert(proc && proc->procState == STATE_RUNNABLE && !proc->schedNode); bool found = false; ListNode_t *iterNode = NULL; @@ -137,13 +138,15 @@ void SchedThisProcUnlocked(Process_t *proc) proc->schedNode = procNode; - //printdbg("Adding process %d to '%s'\n", proc->pid, PrioClassesNames[proc->prioClass]); - // // Find a process with lesser priority // for (iterNode = head->first; iterNode; iterNode = iterNode->next) { if (proc->prioLevel > GetNodeData(iterNode, Process_t *)->prioLevel) { + // Detect double insertions + KalAssert(proc->pid != GetNodeData(iterNode, Process_t *)->pid); + + // Add process to schedule AddNodeBefore(head, iterNode, procNode); found = true; break; @@ -198,9 +201,12 @@ void BlockCurProc(void) ListNode_t *procNode = GetCurProc()->schedNode; + KalAssert(procNode && "Blocking non-scheduled process"); + GetCurProc()->procState = STATE_BLOCKED; RemoveNode(procNode->head, procNode); + GetCurProc()->schedNode = NULL; SetCurProc(SelectSchedNext()); } @@ -208,6 +214,7 @@ static inline void ReSchedCurProc(void) { KalAssert(GetCurProc() && GetCurProc()->procState == STATE_RUNNING); + KalAssert(GetCurProc()->schedNode); // Restore default attributes, cancelling boosts GetCurProc()->prioClass = GetCurProc()->defPrioClass; @@ -301,7 +308,6 @@ leave: void InitSched(void) { int pid; - Process_t *proc; SchedLock(); @@ -345,7 +351,7 @@ void FiniSched(void) #ifndef _KALEID_KERNEL -#define PrintProc(proc) printdbg("{ %d, '%s', %d , %d}\n", (proc)->pid, \ +#define PrintProc(proc) printdbg("{ %d, '%s', %d , %lu}\n", (proc)->pid, \ PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice); // @@ -358,7 +364,7 @@ void PrintList(ListHead_t *head) Process_t *proc; ListNode_t *node = head->first; - printdbg("len: %d\n", head->length); + printdbg("len: %lu\n", head->length); while (node) { proc = GetNodeData(node, Process_t *); @@ -402,8 +408,8 @@ int main(void) } if (tick == 50) { - procs[2].procState = STATE_RUNNABLE; - SchedThisProc(&procs[2]); + procs[0].procState = STATE_RUNNABLE; + SchedThisProc(&procs[0]); } printf("Tick %d - Running: ", tick); @@ -418,6 +424,9 @@ int main(void) SchedOnTick(); + if (tick == 50) // already done + puts("Re-scheduling process 0"); + tick++; }