From 4dad68706db8fae1b60b9bb88ad0ea6c789cdfcf Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 17 Jan 2019 15:24:54 +0100 Subject: [PATCH 01/14] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 0775db9..8e765d1 100644 --- a/Readme.md +++ b/Readme.md @@ -2,7 +2,7 @@ ### Fully open-source operating system from scratch (WIP), released under the GNU GPL version 3.0 -#### Boot Branch +#### Master Branch For the project plan, see [OS/K Project](https://github.com/orgs/os-k-team/projects/1) From 52648a99079d59b852a2389711c654c47abc0af4 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 21 Jan 2019 09:53:54 +0100 Subject: [PATCH 02/14] 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++; } From 61429bb60db37f66087770f94e9595a4cb182f7e Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 21 Jan 2019 11:07:11 +0100 Subject: [PATCH 03/14] (Hopefuly) definitive headers structure --- Makefile.in | 4 +- kaleid/common/arith.c | 33 --- kaleid/common/atoi.c | 37 ---- kaleid/common/itoa.c | 125 ----------- kaleid/common/memory.c | 245 --------------------- kaleid/common/rand.c | 34 --- kaleid/common/sprintf.c | 79 ------- kaleid/common/status.c | 34 --- kaleid/common/string.c | 351 ------------------------------ kaleid/common/strtol.c | 27 --- kaleid/include/common/kalassrt.h | 108 --------- kaleid/include/common/kalcrt.h | 242 -------------------- kaleid/include/common/kaldefs.h | 100 --------- kaleid/include/common/kalerror.h | 63 ------ kaleid/include/common/kallims.h | 117 ---------- kaleid/include/common/kallist.h | 271 ----------------------- kaleid/include/common/kalmask.h | 114 ---------- kaleid/include/common/kaltypes.h | 120 ---------- kaleid/include/kaleid.h | 71 +----- kaleid/include/kalkern.h | 22 +- kaleid/include/kernel/kernbase.h | 203 ----------------- kaleid/include/kernel/kernlocks.h | 178 --------------- kaleid/include/kernel/kernsched.h | 115 ---------- kaleid/include/kernel/kernterm.h | 95 -------- kaleid/kernel/ke/panic.c | 9 +- 25 files changed, 20 insertions(+), 2777 deletions(-) delete mode 100644 kaleid/common/arith.c delete mode 100644 kaleid/common/atoi.c delete mode 100644 kaleid/common/itoa.c delete mode 100644 kaleid/common/memory.c delete mode 100644 kaleid/common/rand.c delete mode 100644 kaleid/common/sprintf.c delete mode 100644 kaleid/common/status.c delete mode 100644 kaleid/common/string.c delete mode 100644 kaleid/common/strtol.c delete mode 100644 kaleid/include/common/kalassrt.h delete mode 100644 kaleid/include/common/kalcrt.h delete mode 100644 kaleid/include/common/kaldefs.h delete mode 100644 kaleid/include/common/kalerror.h delete mode 100644 kaleid/include/common/kallims.h delete mode 100644 kaleid/include/common/kallist.h delete mode 100644 kaleid/include/common/kalmask.h delete mode 100644 kaleid/include/common/kaltypes.h delete mode 100644 kaleid/include/kernel/kernbase.h delete mode 100644 kaleid/include/kernel/kernlocks.h delete mode 100644 kaleid/include/kernel/kernsched.h delete mode 100644 kaleid/include/kernel/kernterm.h diff --git a/Makefile.in b/Makefile.in index e0deb10..0fd3267 100644 --- a/Makefile.in +++ b/Makefile.in @@ -26,10 +26,10 @@ BINDIR=./build/bin OBJDIR=./build/obj BOOTDIR=boot -COMMDIR=kaleid/common +COMMDIR=kaleid/crtlib KERNDIR=kaleid/kernel SYSTDIR=kaleid/system -LINXDIR=kaleid/common/test +LINXDIR=$(COMMDIR)/test //----------------------------------------------------------------------------# // TESTING MAKEFILE diff --git a/kaleid/common/arith.c b/kaleid/common/arith.c deleted file mode 100644 index 5ac8338..0000000 --- a/kaleid/common/arith.c +++ /dev/null @@ -1,33 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Arithmetical functions // -//----------------------------------------------------------------------------// - -// do not mask anything -#define _KALMASK_H -#include - -int _osk_abs(int x) -{ - return abs(x); -} - -long _osk_labs(long x) -{ - return labs(x); -} - -div_t _osk_div(int x, int y) -{ - return div(x, y); -} - -ldiv_t _osk_ldiv(long x, long y) -{ - return ldiv(x, y); -} - diff --git a/kaleid/common/atoi.c b/kaleid/common/atoi.c deleted file mode 100644 index 0ea016c..0000000 --- a/kaleid/common/atoi.c +++ /dev/null @@ -1,37 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Conversion utilities - atoi family // -//----------------------------------------------------------------------------// - -#include - -// -// String to integer -// Do not change errno -// -#define _ATOI_IMPL(_Name, _Type, _Func) \ - _Type _Name(const char *str) { \ - __get_errno(old); \ - _Type ret = (_Type)_Func(str, NULL, 0); \ - __set_errno(old); \ - return ret; \ - } - - -// ISO C does not allow extra ‘;’ outside of a function -#if defined(_NEED_ATOI) -_ATOI_IMPL(atoi, int, strtol) -#elif defined(_NEED_ATOL) -_ATOI_IMPL(atol, long, strtol) -#elif defined(_NEED_ATOU) -_ATOI_IMPL(atou, uint, strtoul) -#elif defined(_NEED_ATOUL) -_ATOI_IMPL(atoul, ulong, strtoul) -#else -#error "What am I supposed to declare?" -#endif - diff --git a/kaleid/common/itoa.c b/kaleid/common/itoa.c deleted file mode 100644 index 1144e26..0000000 --- a/kaleid/common/itoa.c +++ /dev/null @@ -1,125 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Conversion utilities - itoa family // -//----------------------------------------------------------------------------// - -#include - -// -// 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) -// - -#if defined(_NEED_ITOA) - -#define _IL_MIN INT_MIN -#define _IL_MIN_STRING "-2147483648" -char *itoa(int i, char *str, int base) -{ - int rem; - -#elif defined(_NEED_LTOA) - -#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) - // - // Deal with negatives - // - int neg = 0; - if (i < 0 && base == 10) { - // - // Handle INT_MIN and LONG_MIN... - // - if (__builtin_expect(i == _IL_MIN, 0)) { - strcpy(orig, _IL_MIN_STRING); - goto leave; - } - - else { - neg = 1; - i = -i; - } - } -#endif - - // - // Only handle base 2 -> 36 - // - if (base < 2 || base > 36) { - __set_errno(EINVAL); - *orig = '\0'; - goto leave; - } - - // - // Deal with zero separately - // - if (i == 0) { - *str++ = '0'; - *str = '\0'; - goto leave; - } - - // - // Compute digits... in reverse order - // - while (i > 0) { - rem = i % base; - *str++ = (rem > 9) - ? (rem - 10) + 'a' - : rem + '0'; - i /= base; - } - -#if defined(_NEED_ITOA) || defined(_NEED_LTOA) - if (neg) *str++ = '-'; -#endif - - *str = '\0'; - - // - // Reverse the string - // - orig = strrev2(orig); - - // - // End of conversion - // -leave: - return orig; -} - - diff --git a/kaleid/common/memory.c b/kaleid/common/memory.c deleted file mode 100644 index 20a9dd2..0000000 --- a/kaleid/common/memory.c +++ /dev/null @@ -1,245 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: mem*() functions // -//----------------------------------------------------------------------------// - -#include - -//------------------------------------------// -// memset() family // -//------------------------------------------// - -// -// Set "bytes"-many bytes starting from ptr to val -// -void *memset(void *ptr, int val, size_t bytes) -{ - uchar *uptr = (uchar *)ptr; - - // - // Deal with bytes before start of the first aligned qword - // - while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { - *uptr++ = (uchar)val; - } - - // - // At this point we're qword-aligned - // - if (bytes > QWORD_SIZE) { - const ulong uval = ((ulong)val << 56) | ((ulong)val << 48) - | ((ulong)val << 40) | ((ulong)val << 32) - | ((ulong)val << 24) | ((ulong)val << 16) - | ((ulong)val << 8) | ((ulong)val); - - ulong *uqptr = (ulong *)ptr; - - // - // Moving fast, qword by qword - // - while (bytes > QWORD_SIZE) { - *uqptr++ = uval; - bytes -= QWORD_SIZE; - } - - uptr = (uchar *)(ulong)uqptr; - } - - // - // Deal with the few remaining bytes - // - while (bytes--) *uptr++ = (uchar)val; - - return ptr; -} - -// -// Set "words"-many words starting from ptr to val -// -void *memsetw(void *ptr, int val, size_t words) -{ - ushort *uptr = (ushort *)ptr; - - // - // Check whether we can we do this an aligned way - // - if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { - // - // We can't, so we write word by word all the way up - // - while (words--) *uptr++ = (ushort)val; - return uptr; - } - - while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) { - *uptr++ = (ushort)val; - } - - if (words > QWORDS_TO_WORDS(1)) { - const ulong uval = ((ulong)val << 48) | ((ulong)val << 32) - | ((ulong)val << 16) | ((ulong)val); - - ulong *uqptr = (ulong *)uptr; - - while (words > QWORDS_TO_WORDS(1)) { - words -= QWORDS_TO_WORDS(1); - *uqptr++ = uval; - } - - uptr = (ushort *)(ulong)uqptr; - } - - while (words--) *uptr++ = (ushort)val; - - return ptr; -} - -// -// Set "dwords"-many dwords starting from ptr to val -// XXX unimplemented -// -void *memsetd(void *ptr, int val, size_t dwords) -{ - (void)val; - (void)dwords; - - return ptr; -} - -// -// Set "qwords"-many qwords starting from ptr to val -// -void *memsetq(void *ptr, long val, size_t qwords) -{ - ulong *uptr = (ulong *)ptr; - - // - // There's no need to check for alignment - // - while (qwords--) *uptr++ = (ulong)val; - - return ptr; -} - -//------------------------------------------// -// Other mem*() functions // -//------------------------------------------// - -// -// Set "bytes"-many bytes starting from ptr to 0 -// -void *memzero(void *ptr, size_t bytes) -{ - return memsetb(ptr, 0, bytes); -} - -// -// Copy "bytes"-many bytes of src to dst -// Does not deal with overlapping blocks (memmove's job) -// -void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) -{ - const ulong *usrc = (const ulong *)src; - ulong *udst = (ulong *)dst; - - if unlikely (bytes == 0) return dst; - - // - // Can align both src and dst at once at once? - // - if unlikely ((ulong)src % WORD_ALIGN == 1 - && (ulong)dst % WORD_ALIGN == 1) { - const uchar *ubsrc = (const uchar *)usrc; - uchar *ubdst = (uchar *)udst; - - // - // Yes we can, we're guaranteed to be word-aligned now - // - *ubdst++ = *ubsrc++; - bytes--; - - udst = (ulong *)ubdst; - usrc = (ulong *)ubsrc; - } - - const ushort *uwsrc = (const ushort *)usrc; - ushort *uwdst = (ushort *)udst; - - // - // Align either dst or src for qword access - // - while ((ulong)dst % QWORD_ALIGN > 0 - && (ulong)src % QWORD_ALIGN > 0 - && bytes > WORD_SIZE) { - - *uwdst++ = *uwsrc++; - bytes -= WORD_SIZE; - } - - udst = (ulong *)uwdst; - usrc = (ulong *)uwsrc; - - // - // This should be most of the job - // - while (bytes > QWORD_SIZE) { - *udst++ = *usrc++; - bytes -= QWORD_SIZE; - } - - const uchar *ubsrc = (const uchar *)usrc; - ushort *ubdst = (ushort *)udst; - - // - // Deal with the few bytes left - // - while (bytes--) *ubdst ++ = *ubsrc++; - - return dst; -} - -// -// Move memory from src to dest, even if they overlap -// -void *memmove(void *dst, const void *src, size_t bytes) -{ - const uchar *usrc = src; - uchar *udst = dst; - - // - // Can we use memcpy() safely? - // - if (udst < usrc) { - return memcpy(dst, src, bytes); - } - - // - // No, so we go backwards - // - uchar *usrc_end = (uchar *)usrc + bytes - 1; - uchar *udst_end = udst + bytes - 1; - while (bytes--) *udst_end-- = *usrc_end--; - - return dst; -} - -// -// Compare memory areas -// -int memcmp(const void *ptr1, const void *ptr2, size_t bytes) -{ - const uchar *uptr1 = ptr1; - const uchar *uptr2 = ptr2; - - while (bytes--) { - if (*uptr1++ != *uptr2++) { - return uptr1[-1] < uptr2[-1] ? -1 : 1; - } - } - - return 0; -} diff --git a/kaleid/common/rand.c b/kaleid/common/rand.c deleted file mode 100644 index 41ca5d9..0000000 --- a/kaleid/common/rand.c +++ /dev/null @@ -1,34 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: RNG related functions // -//----------------------------------------------------------------------------// - -#include - -// -// Seed value -// -static ulong next = 7756; - -// -// Returns a pseudo-random integer -// To be improved -// -int rand(void) -{ - next = next * 1103515245 + 12347; - return (uint)(next / 65536); -} - -// -// (Re)Set the random seed -// -void srand(uint seed) -{ - next = (ulong)seed; -} - diff --git a/kaleid/common/sprintf.c b/kaleid/common/sprintf.c deleted file mode 100644 index fdd48dc..0000000 --- a/kaleid/common/sprintf.c +++ /dev/null @@ -1,79 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: sprintf()-related functions // -//----------------------------------------------------------------------------// - -#include - -// -// 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; - va_list ap; - - va_start(ap); - ret = vsnprintf(str, SIZE_T_MAX, fmt, ap); - va_end(ap); - - return ret; -} - -int vsprintf(char *str, const char *fmt, va_list ap) -{ - return vsnprintf(str, SIZE_T_MAX, fmt, 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, ...) -{ - int ret; - va_list ap; - - va_start(ap); - ret = vsnprintf(str, n, fmt, ap) - va_end(ap); - - return ret; -} - -int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) -{ - int ret = 0; - - // - // Go throught the format string - // - while (*fmt) { - if (*fmt != '%') { - // - // Even if we don't have any more room we still increase ret - // - if (ret++ < n) { - *str++ = *fmt++; - } - continue; - } - - switch (*fmt) { - case 'd': - default: - break; - } - } - - return ret; -} - - diff --git a/kaleid/common/status.c b/kaleid/common/status.c deleted file mode 100644 index 2866833..0000000 --- a/kaleid/common/status.c +++ /dev/null @@ -1,34 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Implementation of describe_status() // -//----------------------------------------------------------------------------// - -#include - -error_t __errno = 0; - -/* -static const char *descriptions[] = { - [-SUCCESS] = "Success", - [-FAILED] = "Failed (no precision)", - [-NOT_PERMITTED] = "Operation not permitted", - [-ACCESS_DENIED] = "Access denied", - - [-BAD_ARGUMENT] = "Bad argument", - [-BAD_ARG_RANGE] = "Bad argument (not in range)", - [-BAD_ARG_NULL] = "Bad argument (null pointer)", -}; - -const char *describe_status(status_t status) -{ - (void)descriptions; - (void)status; - - // XXX - return ""; -} -*/ diff --git a/kaleid/common/string.c b/kaleid/common/string.c deleted file mode 100644 index dd1c210..0000000 --- a/kaleid/common/string.c +++ /dev/null @@ -1,351 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: String-related functions // -//----------------------------------------------------------------------------// - - -#include - -// -// Compare two strings -// -int strcmp(const char *str1, const char *str2) -{ - while (*str1 == *str2 && *str2) str1++, str2++; - - return *(uchar *)str1 - *(uchar *)str2; -} - -// -// Compare at most n bytes of two strings -// -int strncmp(const char *str1, const char *str2, size_t n) -{ - size_t it = 0; - - while (*str1 == *str2 && *str2 && it < n) str1++, str2++, it++; - - return *(uchar *)str1 - *(uchar *)str2; -} - -// -// Return str's length -// -size_t strlen(const char *str) -{ - const char *base = str; - - while (*str) str++; - - return str - base; -} - -// -// Return a pointer to the first occurence of ch in str, -// or str's null-terminator if none is found -// -char *strchrnul(const char *str, int ch) -{ - while ((*str && *str != (char)ch)) str++; - - return (char *)str; -} - -// -// Return a pointer to the first occurence of ch in str, -// NULL if none is found -// -char *strchr(const char *str, int ch) -{ - while ((*str && *str != (char)ch)) str++; - - return *str ? (char *)str : NULL; -} - -// -// Return a point to the last occurence of ch in str, -// NULL if none is found -// -char *strrchr(const char *str, int ch) -{ - char *ptr = NULL; - - while (*str) { - if (*str == ch) { - ptr = (char *)str; - } - str++; - } - - return ptr; -} - -// -// Return the length of the longest inital segment of str -// that only contains characters in acc -// -size_t strspn(const char *str, const char *acc) -{ - const char *ptr = str; - - while (*ptr && strchr(acc, *ptr) != NULL) ptr++; - - return ptr - str; -} - -// -// Return the length of the longest initial segment of str -// that does not contain any character in rej -// -size_t strcspn(const char *str, const char *rej) -{ - const char *ptr = str; - - while (*ptr && strchr(rej, *ptr) == NULL) ptr++; - - return ptr - str; -} - -// -// Return the first occurence in str of any byte in acc -// -char *strpbrk(const char *str, const char *acc) -{ - str += strcspn(str, acc); - - return *str ? (char *)str : NULL; -} - -// -// Return the first occurence of the substring needle -// in the string haystack, NULL if none is found -// Null-terminators aren't compared -// -char *strstr(const char *haystack, const char *needle) -{ - const size_t needle_size = strlen(needle); - - // - // Moves haystack to first occurence of the needle's first byte - // - while ((haystack = strchr(haystack, *needle)) != NULL) { - if (strncmp(haystack, needle, needle_size) == 0) { - return (char *)haystack; - } - } - - return NULL; -} - -// -// Tokenize a string, using saveptr as a savestate -// We let a segmentation fault happen if *saveptr == NULL -// -char *strtok_r(char *restrict str, const char *restrict delim, char **restrict saveptr) -{ - assert(*saveptr != NULL); - - if (str == NULL) str = *saveptr; - - // - // Skip initial segments composed only of delimiters - // - str += strspn(str, delim); - - // - // If str is empty, store it in saveptr so that next call - // still finds an empty strings and returns NULL - // - if (*str == 0) { - *saveptr = str; - return NULL; - } - - char *ptr = str, *tok_end = strpbrk(str, delim); - - // - // If we found the last token, set *saveptr to a str's null-terminator - // Otherwise, null-terminate token and save next byte - // - - if (tok_end == NULL) { - while (*ptr) ptr++; - *saveptr = ptr; - } - - else { - *tok_end = 0; - *saveptr = tok_end + 1; - } - - return str; -} - -// -// Tokenize a string in a very thread-unsafe way -// -char *strtok(char *restrict str, const char *restrict delim) -{ - static char *saveptr = NULL; - - KalAssert(FALSE); - - if (str) saveptr = str; - - return strtok_r(str, delim, &saveptr); -} - -// -// Copy the string src into dest -// -char *strcpy(char *restrict dest, const char *restrict src) -{ - char *base = dest; - - while ((*dest++ = *src++)); - - return base; -} - -// -// strcpy() but always writes n bytes -// Will not null-terminate for strings longer than n bytes -// -char *strncpy(char *restrict dest, const char *restrict src, size_t n) -{ - size_t it; - - for (it = 0; it < n && src[it]; it++) { - dest[it] = src[it]; - } - - while (it < n) dest[it++] = 0; - - return dest; -} - -// -// Copies at most n-1 bytes from src to dest, then fills -// the rest with 0; dest[n] is guanranteed to be '\0' -// -// Returns TRUE if dest would have been null-terminated -// by ordinary strncpy(), and FALSE otherwise -// -int strnzcpy(char *restrict dest, const char *restrict src, size_t n) -{ - size_t it; - - for (it = 0; it < n - 1 && src[it]; it++) { - dest[it] = src[it]; - } - - // Was the copy complete? - if (it == n) { - if (dest[n] == 0) { - return TRUE; - } - - dest[n] = 0; - return FALSE; - } - - while (it < n) dest[it++] = 0; - - return TRUE; -} - -// -// Appends a copy of src at the end of dest -// -char *strcat(char *restrict dest, const char *restrict src) -{ - char *base = dest; - while (*dest) dest++; - while ((*dest++ = *src++)); - return base; -} - -// -// Appends a copy of at most n bytes of src at the end of dest -// -char *strncat(char *restrict dest, const char *restrict src, size_t n) -{ - size_t it, off = 0; - - while (dest[off]) off++; - - for (it = 0; it < n && src[it]; it++) { - dest[it+off] = src[it]; - } - - while (it++ < n) dest[it+off] = 0; - - return dest; -} - -// -// Appends a copy of at most n bytes of src at the end of dest -// 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) -{ - size_t it, off = 0; - - while (dest[off]) off++; - - for (it = 0; it < n - 1 && src[it]; it++) { - dest[it+off] = src[it]; - } - - // Was the copy complete? - if (it == n) { - if (dest[n+off] == 0) { - return TRUE; - } - - dest[n+off] = 0; - return FALSE; - } - - while (it++ < n) dest[it+off] = 0; - - return TRUE; -} - -// -// Reverses the string src, putting the result into dest -// -char *strrev(char *restrict dest, const char *restrict src) -{ - char *orig = dest; - size_t n = strlen(src); - - dest[n--] = '\0'; - - while ((*dest++ = src[n--])); - - return orig; -} - -// -// Reverses a string, modifying it -// -char *strrev2(char *str) -{ - char ch, *orig = str; - size_t n = strlen(str); - char *temp = str + n - 1; - - while (temp > str) { - ch = *temp; - *temp-- = *str; - *str++ = ch; - } - - return orig; -} diff --git a/kaleid/common/strtol.c b/kaleid/common/strtol.c deleted file mode 100644 index 7636822..0000000 --- a/kaleid/common/strtol.c +++ /dev/null @@ -1,27 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: strto(u)l functions // -//----------------------------------------------------------------------------// - -#include - -long strtol(const char *str, char **endp, int base) { - (void)str; - (void)endp; - (void)base; - __set_errno(ENOSYS); - return 0; -} - -ulong strtoul(const char *str, char **endp, int base) { - (void)str; - (void)endp; - (void)base; - __set_errno(ENOSYS); - return 0; -} - diff --git a/kaleid/include/common/kalassrt.h b/kaleid/include/common/kalassrt.h deleted file mode 100644 index cf4ddbb..0000000 --- a/kaleid/include/common/kalassrt.h +++ /dev/null @@ -1,108 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid assert() support // -//----------------------------------------------------------------------------// - -#ifndef _KALASSRT_H -#define _KALASSRT_H - -//------------------------------------------// -// Macros // -//------------------------------------------// - -#ifndef noreturn -#define noreturn __attribute__((__noreturn__)) -#endif - -#ifndef unlikely -#define unlikely(x) (__builtin_expect((x), 0)) -#endif - -#ifndef static_assert -#define static_assert _Static_assert -#endif - -//------------------------------------------// -// API compatibility checks // -//------------------------------------------// - -#define _SA_MSG "Incompatible type sizes" -static_assert(sizeof(char) == 1, _SA_MSG); -static_assert(sizeof(short) == 2, _SA_MSG); -static_assert(sizeof(int) == 4, _SA_MSG); -static_assert(sizeof(long) == 8, _SA_MSG); -static_assert(sizeof(void *) == 8, _SA_MSG); -#undef _SA_MSG - -//------------------------------------------// -// When debugging // -//------------------------------------------// - -#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(assert) - -#ifdef _OSK_SOURCE - -// -// Failed assert handler -// -noreturn void _assert_handler(const char *, const char *, int, const char *); - -// -// Checks whether (x) holds, if not call _assert_handler -// -#define assert(x) \ - do { \ - if unlikely (!(x)) \ - _assert_handler(#x, __FILE__, __LINE__, __func__); \ - } while (0); - -#else - -// -// When not building for OS/K, use the system's assert -// -#include - -#endif - -//------------------------------------------// -// When not debugging // -//------------------------------------------// - -#else - -#ifndef NDEBUG -#define NDEBUG 1 -#endif - -#ifndef _NO_DEBUG -#define _NO_DEBUG 1 -#endif - -#ifndef assert -#define assert(x) ((void)0) -#endif - -#endif - -//------------------------------------------// -// Aliases for assert() // -//------------------------------------------// - -#ifndef Assert -#define Assert assert -#endif - -#ifndef KalAssert -#define KalAssert assert -#endif - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/common/kalcrt.h b/kaleid/include/common/kalcrt.h deleted file mode 100644 index 8b9a43e..0000000 --- a/kaleid/include/common/kalcrt.h +++ /dev/null @@ -1,242 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid C runtime library // -//----------------------------------------------------------------------------// - -#ifndef _KALCRT_H -#define _KALCRT_H - -//------------------------------------------// -// Typedefs // -//------------------------------------------// - -#ifndef __error_t_defined -#define __error_t_defined -typedef int error_t; -#endif - -#ifndef __size_t_defined -#define __size_t_defined -typedef unsigned long size_t; -#endif - -#ifndef __va_list_defined -#define __va_list_defined -typedef __builtin_va_list va_list; -#endif - -#ifndef __div_t_defined -#define __div_t_defined -typedef struct { int quot, rem; } div_t; -#endif - -#ifndef __ldiv_t_defined -#define __ldiv_t_defined -typedef struct { long quot, rem; } ldiv_t; -#endif - -//------------------------------------------// -// Global variables // -//------------------------------------------// - -#ifndef _KALEID_KERNEL - -extern error_t __errno; - -#ifndef errno -#define errno __errno -#endif - -#define __get_errno(x) error_t x = errno; -#define __set_errno(x) (errno = (x)) - -#else - -#define errno -#define __get_errno(x) -#define __set_errno(x) - -#endif - -//------------------------------------------// -// Macros // -//------------------------------------------// - -#ifndef _NO_MASK -#define _NO_MASK -#endif - -//------------------------------------------// -// va_list utilities // -//------------------------------------------// - -#ifndef va_start -#define va_start __builtin_va_start -#endif - -#ifndef va_arg -#define va_arg __builtin_va_arg -#endif - -#ifndef va_copy -#define va_copy __builtin_va_copy -#endif - -#ifndef va_end -#define va_end __builtin_va_end -#endif - -//------------------------------------------// -// Memory management utilities // -//------------------------------------------// - -#ifndef memsetb -#define memsetb memset -#endif - -#ifndef memchrb -#define memchrb memchr -#endif - -void *memset(void *, int, size_t); -void *memsetw(void *, int, size_t); -void *memsetd(void *, int, size_t); -void *memsetq(void *, long, size_t); - -void *memchr(const void *, int, size_t); -void *memchrw(const void *, int, size_t); -void *memchrd(const void *, int, size_t); -void *memchrq(const void *, long, size_t); - -void *memrchr(const void *, int, size_t); - -void *memcpy(void *restrict, const void *restrict, size_t); -void *memmove(void *, const void *, size_t); - -void *memzero(void *, size_t); -int memcmp(const void *, const void *, size_t); - -//------------------------------------------// -// String manipulation utilities // -//------------------------------------------// - -size_t strlen(const char *); -size_t strspn(const char *, const char *); -size_t strcspn(const char *, const char *); - -int strcmp(const char *, const char *); -int strncmp(const char *, const char *, size_t); - -char *strchr(const char *, int); -char *strrchr(const char *, int); -char *strchrnul(const char *, int); - -char *strstr(const char *, const char *); -char *strpbrk(const char *, const char *); - -char *strtok(char *restrict, const char *restrict); -char *strtok_r(char *restrict, const char *restrict, char **restrict); - -char *strcpy (char *restrict, const char *restrict); -char *strncpy (char *restrict, const char *restrict, size_t); -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); - -char *strrev(char *restrict, const char *restrict); -char *strrev2(char *); - -int sprintf(char *, const char *, ...); -int snprintf(char *, size_t, const char *, ...); -int vsprintf(char *, const char *, va_list); -int vsnprintf(char *, size_t, const char *, va_list); - -//------------------------------------------// -// Type conversion utilities // -//------------------------------------------// - -char *itoa(int, char *, int); -char *ltoa(long, char *, int); -char *utoa(unsigned int, char *, int); -char *ultoa(unsigned long, char *, int); - -int atoi(const char *); -long atol(const char *); -unsigned int atou(const char *); -unsigned long atoul(const char *); - -long strtol (const char *restrict, char **restrict, int); -unsigned long strtoul(const char *restrict, char **restrict, int); - -//------------------------------------------// -// RNG utilities // -//------------------------------------------// - -int rand(void); -void srand(unsigned int); - -//------------------------------------------// -// Time utilities // -//------------------------------------------// - -//------------------------------------------// -// Diverse utilities // -//------------------------------------------// - -char *strerror(int); -char *strsignal(int); - -//------------------------------------------// -// Arithmetical macros // -//------------------------------------------// - -#ifndef __abs -#define __abs -static inline int abs(int x) -{ - return x < 0 ? -x : x; -} -#endif - -#ifndef __labs -#define __labs -static inline long labs(long x) -{ - return x < 0 ? -x : x; -} -#endif - -#ifndef __div -#define __div -static inline div_t div(int __x, int __y) -{ - div_t __res; - __res.quot = __x/__y; - __res.rem = __x%__y; - return __res; -} -#endif - -#ifndef __ldiv -#define __ldiv -static inline ldiv_t ldiv(long __x, long __y) -{ - ldiv_t __res; - __res.quot = __x/__y; - __res.rem = __x%__y; - return __res; -} -#endif - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif - diff --git a/kaleid/include/common/kaldefs.h b/kaleid/include/common/kaldefs.h deleted file mode 100644 index c2421d6..0000000 --- a/kaleid/include/common/kaldefs.h +++ /dev/null @@ -1,100 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid general preprocessor constants // -//----------------------------------------------------------------------------// - -#ifndef _KALDEFS_H -#define _KALDEFS_H - -//------------------------------------------// -// Actual constants // -//------------------------------------------// - -#ifndef TRUE -#define TRUE 1 -#endif - -#ifndef FALSE -#define FALSE 0 -#endif - -#ifndef NULL -#define NULL 0L -#endif - -#ifndef INITOK -#define INITOK ((unsigned int)0xCAFEBABE) -#endif - -//------------------------------------------// -// Keywords // -//------------------------------------------// - -#ifndef __alignof_is_defined -#define __alignof_is_defined -#define alignof _Alignof -#endif - -#ifndef __alignas_is_defined -#define __alignas_is_defined -#define alignas _Alignas -#endif - -#ifndef __bool_true_false_are_defined -#define __bool_true_false_are_defined -# define bool _Bool -# define true 1 -# define false 0 -# ifndef TRUE -# define TRUE 1 -# endif -# ifndef FALSE -# define FALSE 0 -# endif -#endif - -//------------------------------------------// -// Attributes and macros // -//------------------------------------------// - -#ifndef _PACKED -#define _PACKED __attribute__((__packed__)) -#endif - -#ifndef noreturn -#define noreturn __attribute__((__noreturn__)) -#endif - -#ifndef likely -#define likely(x) (__builtin_expect((x), 1)) -#endif - -#ifndef unlikely -#define unlikely(x) (__builtin_expect((x), 0)) -#endif - -#ifndef _STR -#define _STR(x) #x -#endif - -#ifndef _XSTR -#define _XSTR(x) _STR(x) -#endif - -//------------------------------------------// -// API specific macros // -//------------------------------------------// - -#ifndef KALAPI -# define KALAPI -#endif - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/common/kalerror.h b/kaleid/include/common/kalerror.h deleted file mode 100644 index b29c660..0000000 --- a/kaleid/include/common/kalerror.h +++ /dev/null @@ -1,63 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Values for errno_t and errno // -//----------------------------------------------------------------------------// - -#ifndef _KALERROR_H -#define _KALERROR_H - -//------------------------------------------// -// "errno" values // -//------------------------------------------// - -// Everything went fine -#define EOK 0 - -// Operation not permitted -#define EPERM 1 - -// No such file or directory -#define ENOENT 2 - -// No such process -#define ESRCH 3 - -// Syscall interrupted (e.g. by signal) -#define EINTR 4 - -// I/0 error -#define EIO 5 - -// No such device or address -#define ENXIO 6 - -// Argument list too long -#define E2BIG 7 - -// Not an executable format -#define ENOEXEC 8 - -// Bad file number -#define EBADF 9 - -// Invalid argument -#define EINVAL 22 - -// Functionality not implemented -#define ENOSYS 38 - -// Component crashed -#define ECRASH 500 - -// System is panicking -#define EPANIC 600 - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/common/kallims.h b/kaleid/include/common/kallims.h deleted file mode 100644 index 9afd35d..0000000 --- a/kaleid/include/common/kallims.h +++ /dev/null @@ -1,117 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid type limits definitions // -//----------------------------------------------------------------------------// - -#ifndef _KALLIMS_H -#define _KALLIMS_H - -//------------------------------------------// -// Data sizes blocks // -//------------------------------------------// - -#ifndef DATA_SIZE_BLOCK -#define DATA_SIZE_BLOCK -# define BYTE_SIZE sizeof(char) -# define WORD_SIZE sizeof(short) -# define DWORD_SIZE sizeof(int) -# define QWORD_SIZE sizeof(long) -#endif - -#ifndef DATA_ALIGN_BLOCK -#define DATA_ALIGN_BLOCK -# define BYTE_ALIGN alignof(char) -# define WORD_ALIGN alignof(short) -# define DWORD_ALIGN alignof(int) -# define QWORD_ALIGN alignof(long) -#endif - -#ifndef DATA_BITS_BLOCK -#define DATA_BITS_BLOCK -# define BYTE_BIT 8 -# define CHAR_BIT (BYTE_SIZE * BYTE_BIT) -# define WORD_BIT (WORD_SIZE * BYTE_BIT) -# define DWORD_BIT (DWORD_SIZE * BYTE_BIT) -# define QWORD_BIT (QWORD_SIZE * BYTE_BIT) -# define SHORT_BIT WORD_BIT -# define INT_BIT DWORD_BIT -# define LONG_BIT QWORD_BIT -#endif - -#ifndef DATA_SHIFTS_BLOCK -#define DATA_SHIFTS_BLOCK -# define BYTES_TO_WORDS(B) ((B) >> 1) -# define BYTES_TO_DWORDS(B) ((B) >> 2) -# define BYTES_TO_QWORDS(B) ((B) >> 3) -# define WORDS_TO_BYTES(W) ((W) << 1) -# define WORDS_TO_DWORDS(W) ((W) >> 1) -# define WORDS_TO_QWORDS(W) ((W) >> 2) -# define DWORDS_TO_BYTES(D) ((D) << 2) -# define DWORDS_TO_WORDS(D) ((D) << 1) -# define DWORDS_TO_QWORDS(D) ((D) >> 1) -# define QWORDS_TO_BYTES(Q) ((Q) << 3) -# define QWORDS_TO_WORDS(Q) ((Q) << 2) -# define QWORDS_TO_DWORDS(Q) ((Q) << 1) -#endif - -//------------------------------------------// -// Numeric data limits // -//------------------------------------------// - -#ifndef DATA_MAX_LIMITS_BLOCK -#define DATA_MAX_LIMITS_BLOCK -# define SCHAR_MAX ((signed char) 0x7F) -# define SHRT_MAX ((short) 0x7FFF) -# define INT_MAX ((int) 0x7FFFFFFF) -# define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFF) -# define UCHAR_MAX ((unsigned char) 0xFF -# define USHRT_MAX ((unsigned short) 0xFFFF) -# define UINT_MAX ((unsigned int) 0xFFFFFFFF) -# define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFF) -#endif - -#ifndef DATA_MIN_LIMITS_BLOCK -#define DATA_MIN_LIMITS_BLOCK -# define SCHAR_MIN ((signed char) -SCHAR_MAX - 1) -# define SHRT_MIN ((short) -SHRT_MAX - 1) -# define INT_MIN ((int) -INT_MAX - 1) -# define LONG_MIN ((long) -LONG_MAX - 1L) -#endif - -#ifndef DATA_CHAR_LIMITS_BLOCK -#define DATA_CHAR_LIMITS_BLOCK -# ifdef __CHAR_UNSIGNED__ -# define CHAR_MIN ((char)0) -# define CHAR_MAX ((char)UCHAR_MAX) -# else -# define CHAR_MIN ((char)SCHAR_MIN) -# define CHAR_MAX ((char)SCHAR_MAX) -# endif -#endif - -#ifndef DATA_SPTYPES_LIMITS_BLOCK -#define DATA_SPTYPES_LIMITS_BLOCK -# define SSIZE_T_MIN LONG_MIN -# define SSIZE_T_MAX LONG_MAX -# define SIZE_T_MAX ULONG_MAX -#endif - -#ifdef NEED_MORE_USELESS_DATA -# define UCHAR_MIN ((unsigned char)0) -# define USHRT_MIN ((unsigned short)0) -# define UINT_MIN ((unsigned int)0) -# define ULONG_MIN ((unsigned long)0) -# ifdef STILL_NEED_MORE_USELESS_DATA -# error "Not enough useless data!" -# endif -#endif - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/common/kallist.h b/kaleid/include/common/kallist.h deleted file mode 100644 index 8257b64..0000000 --- a/kaleid/include/common/kallist.h +++ /dev/null @@ -1,271 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Doubly linked lists implementation // -//----------------------------------------------------------------------------// - -#ifndef _KALLIST_H -#define _KALLIST_H - -#ifdef _KALEID_KERNEL -#error "kallist.h - Not ready for kernel compilation" -#endif - -#ifndef _KALASSRT_H -#include -#endif - -#ifndef _KALKERN_LOCKS_H -#include -#endif - -// -// XXX ¯\_(ツ)_/¯ -// -void *malloc(long); -void free(void *); - -#define AllocMemory malloc -#define FreeMemory free - -//------------------------------------------// -// Data structures // -//------------------------------------------// - -typedef struct sListHead_t { - Lock_t *lock; - unsigned long length; - struct sListNode_t *first; - struct sListNode_t *last; -} ListHead_t; - -typedef struct sListNode_t { - void *data; - ListHead_t *head; - struct sListNode_t *prev; - struct sListNode_t *next; -} ListNode_t; - -//------------------------------------------// -// Functions // -//------------------------------------------// - -// -// Create a list head with an extern lock -// -static inline ListHead_t -*CreateListHeadWithLock(Lock_t *lock) -{ - ListHead_t *head = AllocMemory(sizeof(ListHead_t)); - - if (head == NULL) return NULL; - - head->first = head->last = NULL; - head->length = 0; - - head->lock = lock; - - return head; -} - -// -// Create a liste head -// -static inline ListHead_t -*CreateListHead(void) -{ - return CreateListHeadWithLock(NULL); -} - -// -// Create a node -// -static inline ListNode_t -*CreateNode(void *data) -{ - ListNode_t *node = AllocMemory(sizeof(ListNode_t)); - - if (node == NULL) return NULL; - - node->data = data; - node->head = NULL; - node->prev = node->next = NULL; - - return node; -} - -// -// Prepend node at beginning of list -// -static inline ListHead_t -*PrependNode(ListHead_t *head, ListNode_t *node) -{ - KalAssert(head && node); - - node->head = head; - node->prev = NULL; - - if (head->length > 0) { - node->next = head->first; - head->first->prev = node; - head->first = node; - } - - else { - head->first = node; - head->last = node; - node->next = NULL; - } - - head->length++; - - return head; -} - -// -// Append node at end of list -// -static inline ListHead_t -*AppendNode(ListHead_t *head, ListNode_t *node) -{ - KalAssert(head && node); - - node->head = head; - node->next = NULL; - - if (head->length > 0) { - node->prev = head->last; - head->last->next = node; - head->last = node; - } - - else { - head->first = node; - head->last = node; - node->prev = NULL; - } - - head->length++; - - return head; -} - -// -// Insert node2 before node1 -// -static inline ListHead_t -*AddNodeBefore(ListHead_t *head, ListNode_t *node1, ListNode_t *node2) -{ - KalAssert(head && node1 && node2 && node1->head == head); - - if (head->first == node1) { - return PrependNode(head, node2); - } - - node2->head = head; - node2->next = node1; - node2->prev = node1->prev; - - // node1->prev does exist - // or node1 would be first - node1->prev->next = node2; - node1->prev = node2; - - head->length++; - - return head; -} - -// -// Insert node2 after node1 -// -static inline ListHead_t -*AddNodeAfter(ListHead_t *head, ListNode_t *node1, ListNode_t *node2) -{ - KalAssert(head && node1 && node2 && node1->head == head); - - if (head->last == node1) { - return AppendNode(head, node2); - } - - node2->head = head; - node2->prev = node1; - node2->next = node1->next; - - node1->next->prev = node2; - node1->next = node2; - - head->length++; - - return head; -} - -// -// Remove node of list (and frees it) -// -static inline ListHead_t -*RemoveNode(ListHead_t *head, ListNode_t *node) -{ - KalAssert(head && node && head->length > 0 && node->head == head); - - if (head->length == 1) { - head->first = head->last = NULL; - goto leave; - } - - if (head->first == node) { - head->first = node->next; - node->next->prev = NULL; - } - - else if (head->last == node) { - head->last = node->prev; - node->prev->next = NULL; - } - - else { - node->prev->next = node->next; - node->next->prev = node->prev; - } - -leave: - head->length--; - FreeMemory(node); - - return head; -} - -// -// Free a node -// -static inline void -DestroyNode(ListNode_t *node) -{ - KalAssert(node); - FreeMemory(node); -} - -// -// Free a list head -// -static inline void -DestroyListHead(ListHead_t *head) -{ - KalAssert(head); - FreeMemory(head); -} - -// -// Access a node's data -// -#define GetNodeData(node, type) ((type)(node)->data) - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif - diff --git a/kaleid/include/common/kalmask.h b/kaleid/include/common/kalmask.h deleted file mode 100644 index c185354..0000000 --- a/kaleid/include/common/kalmask.h +++ /dev/null @@ -1,114 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Masks for the functions in the KCRL // -//----------------------------------------------------------------------------// - -#ifndef _KALMASK_H -#define _KALMASK_H - -//------------------------------------------// - -#define memset _osk_memsetb -#define memchr _osk_memchrb - -#define memsetb _osk_memsetb -#define memsetw _osk_memsetw -#define memsetd _osk_memsetd -#define memsetq _osk_memsetq - -#define memchr _osk_memchrb -#define memchrw _osk_memchrw -#define memchrd _osk_memchrd -#define memchrq _osk_memchrq - -#define memcpy _osk_memcpy -#define memmove _osk_memmove - -#define memcmp _osk_memcmp -#define memzero _osk_memzero - -//------------------------------------------// - -#define strlen _osk_strlen -#define strspn _osk_strspn -#define strcspn _osk_strcspn - -#define strcmp _osk_strcmp -#define strncmp _osk_strncmp - -#define strchr _osk_strchr -#define strrchr _osk_strrchr - -#define strstr _osk_strstr -#define strpbrk _osk_strpbrk - -#define strtok _osk_strtok -#define strtok_r _osk_strtok_r - -#define strcpy _osk_strcpy -#define strncpy _osk_strncpy -#define strnzcpy _osk_strnzcpy - -#define strcat _osk_strcat -#define strncat _osk_strncat -#define strnzcat _osk_strnzcat - -#define strrev _osk_strrev -#define strrev2 _osk_strrev2 - -#define sprintf _osk_sprintf -#define snprintf _osk_snprintf -#define vsprintf _osk_vsprintf -#define vsnprintf _osk_vsnprintf - -//------------------------------------------// - - -#define itoa _osk_itoa -#define ltoa _osk_ltoa -#define utoa _osk_utoa -#define ultoa _osk_ultoa - -#define atoi _osk_atoi -#define atol _osk_atol -#define atou _osk_atou -#define atoul _osk_atoul - -#define strtol _osk_strtol -#define strtoul _osk_strtoul - -//------------------------------------------// - -#define rand _osk_rand -#define srand _osk_srand - -//------------------------------------------// - -#define abs _osk_abs -#define labs _osk_labs - -#define min _osk_min -#define lmin _osk_lmin - -#define max _osk_max -#define lmax _osk_lmax - -#define __div -#define __ldiv - -#define div _osk_div -#define ldiv _osk_ldiv - -//------------------------------------------// - -#define strerror _osk_strerror - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/common/kaltypes.h b/kaleid/include/common/kaltypes.h deleted file mode 100644 index 6823ddb..0000000 --- a/kaleid/include/common/kaltypes.h +++ /dev/null @@ -1,120 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid C common types // -//----------------------------------------------------------------------------// - -#ifndef _KALTYPES_H -#define _KALTYPES_H - -//------------------------------------------// -// Basic integer types aliases // -//------------------------------------------// - -#ifndef __base_types_aliases -#define __base_types_aliases -typedef unsigned char uchar; -typedef signed char schar; -typedef unsigned short ushort; -typedef unsigned int uint; -typedef unsigned long ulong; -typedef signed long long llong; -typedef unsigned long long ullong; -typedef long double ldouble; -#endif - -//------------------------------------------// -// Miscellaneous types // -//------------------------------------------// - -#ifndef __size_t_defined -#define __size_t_defined -typedef unsigned long size_t; -#endif - -#ifndef __ssize_t_defined -#define __ssize_t_defined -typedef signed long ssize_t; -#endif - -#ifndef __wchar_t_defined -#define __wchar_t_defined -typedef signed int wchar_t; -#endif - -#ifndef __off_t_defined -#define __off_t_defined -typedef unsigned long off_t; -#endif - -//------------------------------------------// -// Standard fixed-width integer types // -//------------------------------------------// - -#ifndef __ptrdiff_t_defined -#define __ptrdiff_t_defined -typedef signed long ptrdiff_t; -#endif - -#ifndef __intptr_t_defined -#define __intptr_t_defined -typedef signed long intptr_t; -#endif - -#ifndef __uintptr_t_defined -#define __uintptr_t_defined -typedef unsigned long uintptr_t; -#endif - -#ifndef __intmax_t_defined -#define __intmax_t_defined -typedef signed long intmax_t; -#endif - -#ifndef __uintmax_t_defined -#define __uintmax_t_defined -typedef unsigned long uintmax_t; -#endif - -//------------------------------------------// -// Special types // -//------------------------------------------// - -#ifndef __va_list_defined -#define __va_list_defined -typedef __builtin_va_list va_list; -#endif - -#ifndef __div_t_defined -#define __div_t_defined -typedef struct { int quot, rem; } div_t; -#endif - -#ifndef __ldiv_t_defined -#define __ldiv_t_defined -typedef struct { long quot, rem; } ldiv_t; -#endif - - -//------------------------------------------// -// Kaleid-specific types // -//------------------------------------------// - -#ifndef __error_t_defined -#define __error_t_defined -typedef int error_t; -#endif - -#ifndef __port_t_defined -#define __port_t_defined -typedef ushort port_t; -#endif - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/kaleid.h b/kaleid/include/kaleid.h index da81fce..239b222 100644 --- a/kaleid/include/kaleid.h +++ b/kaleid/include/kaleid.h @@ -11,83 +11,22 @@ #define _KALEID_H //------------------------------------------// -// Building for OS/K // +// Include all Kaleid headers // //------------------------------------------// -#if !defined(_OSK_SOURCE) -# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) -# define _OSK_SOURCE 1 -# endif +#ifndef _KALBASE_H +#include #endif -#if !defined(_OSK_SOURCE) -# ifndef _KALMASK_H -# include -# endif +#ifndef _KALEXT_H +#include #endif -//------------------------------------------// -// Building in C++ // -//------------------------------------------// - -#ifdef __cplusplus__ -extern "C" { -#endif - -//------------------------------------------// -// Include common part of API // -//------------------------------------------// - -#ifndef _KALDEFS_H -#include -#endif - -#ifndef _KALERROR_H -#include -#endif - -#ifndef _KALTYPES_H -#include -#endif - -#ifndef _KALLIMS_H -#include -#endif - -#ifndef _KALASSRT_H -#include -#endif - -#ifndef _KALCRT_H -#include -#endif - -//------------------------------------------// -// Include kernel headers // -//------------------------------------------// - #ifdef _KALEID_KERNEL - #ifndef _KALKERN_H #include #endif - -#else - -#ifndef _KALKERN_LOCKS_H -#include #endif - -#endif - -//------------------------------------------// -// Building in C++ // -//------------------------------------------// - -#ifdef __cplusplus__ -} -#endif - //------------------------------------------// // End of header // //------------------------------------------// diff --git a/kaleid/include/kalkern.h b/kaleid/include/kalkern.h index d1121eb..82c84c6 100644 --- a/kaleid/include/kalkern.h +++ b/kaleid/include/kalkern.h @@ -7,12 +7,12 @@ // Desc: Kaleid Kernel main include file // //----------------------------------------------------------------------------// -//------------------------------------------// -// Dependencies // -//------------------------------------------// +#ifndef _KALBASE_H +#include +#endif -#ifndef _KALEID_H -#include +#ifndef _KALEXT_H +#include #endif //------------------------------------------// @@ -27,21 +27,17 @@ //------------------------------------------// #ifndef _KALKERN_BASE_H -#include +#include #endif -#ifndef _KALKERN_LOCKS_H -#include -#endif - -#ifndef _KALKERN_TERM_H -#include +#ifndef _KALKERN_TERMINAL_H +#include #endif // not ready for kernel compilation #ifndef _KALEID_KERNEL #ifndef _KALKERN_SCHED_H -#include +#include #endif #endif diff --git a/kaleid/include/kernel/kernbase.h b/kaleid/include/kernel/kernbase.h deleted file mode 100644 index ab2cb76..0000000 --- a/kaleid/include/kernel/kernbase.h +++ /dev/null @@ -1,203 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid Kernel base types and functionalities // -//----------------------------------------------------------------------------// - -//------------------------------------------// -// Dependencies // -//------------------------------------------// - -#ifndef _KALEID_H -#include -#endif - -//------------------------------------------// -// Start of header // -//------------------------------------------// - -#ifndef _KALKERN_BASE_H -#define _KALKERN_BASE_H - -//------------------------------------------// -// Elementary types // -//------------------------------------------// - -typedef struct sLock_t volatile Lock_t; -typedef struct sThread_t Thread_t; -typedef struct sProcess_t Process_t; -typedef struct sTerminal_t Terminal_t; -typedef struct sListHead_t ListHead_t; -typedef struct sListNode_t ListNode_t; - -//------------------------------------------// -// Values for __kstate // -//------------------------------------------// - -// -// Current state of the kernel -// -typedef enum { - // the kernel is booting - KSTATE_INIT, - - // the kernel is not running a process - KSTATE_KERNEL, - - // a process is running in kernel mode - KSTATE_PROCESS, - - // the kernel is panicking - KSTATE_PANIC, - -} KernelState_t; - -//------------------------------------------// -// Multiprocessor misc. // -//------------------------------------------// - -#ifndef INITOK -#define INITOK ((unsigned int)0xCAFEBABE) -#endif - -#ifndef NCPU -#define NCPU 4 -#endif - -#define GetCurCPU() 0 - -// -// Declare an (extern) CPU-local variable -// -#define __DECLARE_PER_CPU(_X, _Tp, _Qual) \ - _Qual _Tp __ ## _X [NCPU]; \ - static inline _Tp Get ## _X (void) \ - { return __ ## _X [GetCurCPU()]; } \ - static inline void _Set ## _X (_Tp _Y) \ - { (__ ## _X [GetCurCPU()] = _Y); } - -#define DECLARE_PER_CPU(_X, _Tp) \ - __DECLARE_PER_CPU(_X, _Tp, extern) - -#define LOCAL_DEC_PER_CPU(_X, _Tp) \ - __DECLARE_PER_CPU(_X, _Tp, static) - -// -// Actually creates a CPU-local variable -// -#define CREATE_PER_CPU(_X, _Tp) \ - _Tp __ ## _X [NCPU] = { (_Tp) 0 } - - -//------------------------------------------// -// Global constants // -//------------------------------------------// - -// XXX -DECLARE_PER_CPU(PanicStr, const char *); - -DECLARE_PER_CPU(KernState, KernelState_t); - -DECLARE_PER_CPU(_StdOut, Terminal_t *); -DECLARE_PER_CPU(_StdDbg, Terminal_t *); - -DECLARE_PER_CPU(CurProc, Process_t *); -DECLARE_PER_CPU(CurThread, Thread_t *); - -//------------------------------------------// -// Macros for manipulating said // -// global constants // -//------------------------------------------// - -#define SetKernState(x) \ - do { \ - _SetKernState(x); \ - } while (0) - -#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : NULL) -#define SetStdOut(tm) \ - do { \ - if (GetCurProc() == NULL) \ - _Set_StdOut(tm); \ - } while (0); - -#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : NULL) -#define SetStdDbg(tm) \ - do { \ - if (GetCurProc() == NULL) \ - _Set_StdDbg(tm); \ - } while (0) - -//------------------------------------------// -// Other Macros // -//------------------------------------------// - -// -// Size of a tabulation in spaces -// Default: 4 spaces/tab -// -#define KTABSIZE 4 - -// -// Disable IRQs -// -#define DisableIRQs() asm volatile ("cli") - -// -// Enable IRQs -// -#define EnableIRQs() asm volatile ("sti") - -// -// Pause CPU until next interuption -// !!! Enables IRQs !!! -// -#define PauseCPU() asm volatile("sti\n\thlt") - -// -// Halt the CPU indefinitely -// -#define HaltCPU() do { asm volatile ("hlt"); } while (1) - -//------------------------------------------// -// Some base functions // -//------------------------------------------// - -noreturn void StartPanic(const char *); -noreturn void CrashSystem(void); - -//------------------------------------------// -// Useful I/O inlines // -//------------------------------------------// - -static inline -void WriteByteOnPort(port_t port, port_t val) -{ - asm volatile ("out %0, %1" : : "dN" (port), "a" (val)); -} - -static inline -uchar ReadByteFromPort(port_t port) -{ - KalAssert(FALSE && ENOSYS); - (void)port; - return 0; -} - -static inline -ushort ReadWordFromPort(port_t port) -{ - KalAssert(FALSE && ENOSYS); - (void)port; - return 0; -} - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif - diff --git a/kaleid/include/kernel/kernlocks.h b/kaleid/include/kernel/kernlocks.h deleted file mode 100644 index 6d32224..0000000 --- a/kaleid/include/kernel/kernlocks.h +++ /dev/null @@ -1,178 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Spinlocks and mutexes // -//----------------------------------------------------------------------------// - -//------------------------------------------// -// Dependencies // -//------------------------------------------// - -#ifdef _KALEID_KERNEL - -#ifndef _KALKERN_BASE_H -#include "kernbase.h" -#endif - -#else - -#ifndef _KALEID_H -#include -#endif - -#endif - -//------------------------------------------// -// Start of header // -//------------------------------------------// - -#ifndef _KALKERN_LOCKS_H -#define _KALKERN_LOCKS_H - -//------------------------------------------// -// Types // -//------------------------------------------// - -typedef enum eLockType_t { - // - // Mutex-type lock - // - // WARNING - // AquireLock() panics when used on a mutex while not running a process - // - KLOCK_MUTEX, - - // - // Spinlock-type lock - // - KLOCK_SPINLOCK, - -} LockType_t; - -// -// "volatile" may not be actually needed -// -typedef struct sLock_t { - unsigned int initDone; // initialized? - int locked; // is locked? - LockType_t type; // lock type? -#ifdef _KALEID_KERNEL - Process_t *ownerProc; // unused - Process_t *waitingProc; // unused -#endif -} volatile Lock_t; - -//------------------------------------------// -// Functions // -//------------------------------------------// - -// -// Linux syscall vs unimplemented syscall... -// -#ifndef _KALEID_KERNEL -#ifdef _OSK_SOURCE -int KalYieldCPU(void), -#else -int sched_yield(void); -#endif -#endif - -// -// Initialize a lock -// -static inline -void InitLock(Lock_t *lock, LockType_t type) -{ - lock->type = type; - lock->locked = FALSE; - lock->initDone = INITOK; -#ifdef _KALEID_KERNEL - lock->ownerProc = NULL; - lock->waitingProc = NULL; -#endif -} - -// -// Alternative way to initalize a lock -// -#ifdef _KALEID_KERNEL -# define INITLOCK(type) { INITOK, FALSE, (type), NULL, NULL } -#else -# define INITLOCK(type) { INITOK, FALSE, (type) } -#endif - -// -// Destroy a lock -// -static inline -void DestroyLock(Lock_t *lock) -{ - KalAssert(lock->initDone); - - __sync_synchronize(); - lock->initDone = 0; -} - -// -// Aquire the lock -// Panic on double aquisition since that should never happen -// until we have at least a basic scheduler -// -static inline -void AquireLock(Lock_t *lock) -{ - KalAssert(lock->initDone == INITOK); - - while (!__sync_bool_compare_and_swap(&lock->locked, 0, 1)) { -#ifdef _KALEID_KERNEL - StartPanic("AquireLock on an already locked object"); -#else - if likely (lock->type == KLOCK_SPINLOCK) continue; -#ifdef _OSK_SOURCE - else KalYieldCPU(); -#else - else sched_yield(); -#endif -#endif - } - __sync_synchronize(); -} - -// -// Release an already aquired lock -// Panic if the lock was never aquired -// -static inline -void ReleaseLock(Lock_t *lock) -{ -#ifdef _KALEID_KERNEL - KalAssert(lock->ownerProc == GetCurProc()); -#endif - - __sync_synchronize(); - lock->locked = 0; -} - -// -// Tries to aquire lock -// -static inline -bool AttemptLock(Lock_t *lock) -{ - KalAssert(lock->initDone == INITOK); - - bool retval = __sync_bool_compare_and_swap(&lock->locked, 0, 1); - - __sync_synchronize(); - - return retval; -} - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/include/kernel/kernsched.h b/kaleid/include/kernel/kernsched.h deleted file mode 100644 index d99f8de..0000000 --- a/kaleid/include/kernel/kernsched.h +++ /dev/null @@ -1,115 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Scheduler header // -//----------------------------------------------------------------------------// - -//------------------------------------------// -// Dependencies // -//------------------------------------------// - -#ifndef _KALKERN_BASE_H -#include "kernbase.h" -#endif - -#ifndef _KALLIST_H -#include -#endif - -//------------------------------------------// -// Start of header // -//------------------------------------------// - -#ifndef _KALKERN_SCHED_H -#define _KALKERN_SCHED_H - -//------------------------------------------// -// Preprocessor // -//------------------------------------------// - -// -// Debug stuff -// -#define printdbg printf - -// -// States for a process -// -#define STATE_RUNNING 0 -#define STATE_RUNNABLE 1 -#define STATE_BLOCKED 2 - -// -// Time in ticks a process should be run -// -#define DEF_PROC_TSLICE 5 // 20 ticks -#define TCR_PROC_TSLICE 20000 // 20000 ticks (time critical) - -//------------------------------------------// -// List heads // -//------------------------------------------// - -DECLARE_PER_CPU(IdlePrioProcs, ListHead_t *); -DECLARE_PER_CPU(ReglPrioProcs, ListHead_t *); -DECLARE_PER_CPU(ServPrioProcs, ListHead_t *); -DECLARE_PER_CPU(TimeCritProcs, ListHead_t *); - -extern const char *PrioClassesNames[]; - -//------------------------------------------// -// Data types // -//------------------------------------------// - -// -// A process -// -typedef struct sProcess_t { - - // Identifier - int pid; - - // Current priority class - int prioClass; - - // Default priority class (without boosts) - int defPrioClass; - - // Current priority level - int prioLevel; - - // Default priority level - int defPrioLevel; - - // Current state - int procState; - - // Remaining time running - ulong timeSlice; - - // Default time-slice - ulong defTimeSlice; - - // Scheduler internals - ListNode_t *schedNode; - -} Process_t; - -//------------------------------------------// -// Functions // -//------------------------------------------// - -void SchedInit(void); -void SchedFini(void); - -void SchedThisProc(Process_t *); -void SchedOnTick(void); - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif - diff --git a/kaleid/include/kernel/kernterm.h b/kaleid/include/kernel/kernterm.h deleted file mode 100644 index b5c32b6..0000000 --- a/kaleid/include/kernel/kernterm.h +++ /dev/null @@ -1,95 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Terminal functions // -//----------------------------------------------------------------------------// - -//------------------------------------------// -// Dependencies // -//------------------------------------------// - -#ifndef _KALKERN_BASE_H -#include "kernbase.h" -#endif - -//------------------------------------------// -// Start of header // -//------------------------------------------// - -#ifndef _KALKERN_TERM_H -#define _KALKERN_TERM_H - -//------------------------------------------// -// Types // -//------------------------------------------// - -// -// The VGA colors -// -typedef enum { - KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, - KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, - KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, - KTERM_COLOR_BROWN, KTERM_COLOR_LGREY, - KTERM_COLOR_DARK_GREY, KTERM_COLOR_LBLUE, - KTERM_COLOR_LGREEN, KTERM_COLOR_LCYAN, - KTERM_COLOR_LRED, KTERM_COLOR_LMAGENTA, - KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE -} TermColor_t; - -// -// Terminal structure, right now VGA and output only -// -typedef struct sTerminal_t { - - uint initDone; - Lock_t lock; - - const char *name; - const char *type; - - void *data; - - size_t width; - size_t height; - off_t currentX; - off_t currentY; - - uint tabSize; - TermColor_t fgColor; - TermColor_t bgColor; - - error_t (*ClearTermUnlocked)(Terminal_t *); - error_t (*PutOnTermUnlocked)(Terminal_t *, char); - error_t (*PrintOnTermUnlocked)(Terminal_t *, const char *); - -} Terminal_t; - -//------------------------------------------// -// Functions // -//------------------------------------------// - -void InitTerms(void); -error_t ClearTerm(Terminal_t *); -error_t PutOnTerm(Terminal_t *, char); -error_t PrintOnTerm(Terminal_t *, const char *); -error_t ChTermColor(Terminal_t *, TermColor_t, TermColor_t); - -//------------------------------------------// -// Macros // -//------------------------------------------// - -#ifndef _NO_DEBUG -# define DebugLog(...) PrintOnTerm(GetStdDbg(), __VA_ARGS__) -#else -# define DebugLog(...) -#endif - -//------------------------------------------// -// End of header // -//------------------------------------------// - -#endif diff --git a/kaleid/kernel/ke/panic.c b/kaleid/kernel/ke/panic.c index 79c9a89..7b2bf6e 100644 --- a/kaleid/kernel/ke/panic.c +++ b/kaleid/kernel/ke/panic.c @@ -7,16 +7,15 @@ // Desc: How NOT to panic 101 // //----------------------------------------------------------------------------// -#define _UNLOCKED_IO #include // // Failed assert() handler // -noreturn void _assert_handler(const char *msg, - const char *file, - int line, - const char *func) +noreturn void __assert_handler(const char *msg, + const char *file, + int line, + const char *func) { DisableIRQs(); From 27e6b9f69f8e58af51f43190b2cf2e2b436f027b Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 21 Jan 2019 14:58:04 +0100 Subject: [PATCH 04/14] Fixing very broken stuff... --- Makefile.in | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile.in b/Makefile.in index 0fd3267..eadc905 100644 --- a/Makefile.in +++ b/Makefile.in @@ -46,7 +46,7 @@ testing: bootloader pseudo_kern COBJDIR=$(OBJDIR)/$(COMMDIR) LOBJDIR=$(OBJDIR)/$(LINXDIR) -COMMOBJS=COBJ6(string, status, rand, memory, arith, strtol) COBJ4(itoa, ltoa, utoa, ultoa) COBJ4(atoi, atol, atou, atoul) +COMMOBJS=COBJ6(string, status, rand, memory, arith, strtol) COBJ4(itoa, ltoa, utoa, ultoa) COBJ4(atoi, atol, atou, atoul) COBJ2(../extras/prog, ../extras/argv) TCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) KCC=$(CC) -T ./build/kernel.ld -D_OSK_SOURCE -D_KALEID_KERNEL @@ -68,6 +68,8 @@ common: comm-convert COMPILE_COMMON(status) COMPILE_COMMON(memory) COMPILE_COMMON(strtol) + COMPILE_COMMON(../extras/prog) + COMPILE_COMMON(../extras/argv) tests: common $(TCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o From b7fa7a30d0c988ff6e0b10f5565f5a7be6f72bc4 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 21 Jan 2019 15:00:04 +0100 Subject: [PATCH 05/14] Fixing more broken stuff --- build/obj/kaleid/crtlib/.placeholder | 0 build/obj/kaleid/crtlib/test/.paceholder | 0 build/obj/kaleid/extras/.placeholder | 0 kaleid/crtlib/arith.c | 33 +++ kaleid/crtlib/atoi.c | 37 +++ kaleid/crtlib/itoa.c | 125 +++++++++ kaleid/crtlib/memory.c | 245 +++++++++++++++++ kaleid/crtlib/rand.c | 34 +++ kaleid/crtlib/sprintf.c | 79 ++++++ kaleid/crtlib/status.c | 34 +++ kaleid/crtlib/string.c | 330 +++++++++++++++++++++++ kaleid/crtlib/strtol.c | 27 ++ kaleid/extras/argv.c | 23 ++ kaleid/extras/prog.c | 48 ++++ kaleid/include/base/assert.h | 117 ++++++++ kaleid/include/base/bdefs.h | 100 +++++++ kaleid/include/base/crtlib.h | 242 +++++++++++++++++ kaleid/include/base/errno.h | 63 +++++ kaleid/include/base/limits.h | 117 ++++++++ kaleid/include/base/masks.h | 119 ++++++++ kaleid/include/base/types.h | 120 +++++++++ kaleid/include/extras/argv.h | 126 +++++++++ kaleid/include/extras/list.h | 273 +++++++++++++++++++ kaleid/include/extras/locks.h | 170 ++++++++++++ kaleid/include/extras/prog.h | 42 +++ kaleid/include/kalbase.h | 61 +++++ kaleid/include/kalext.h | 48 ++++ kaleid/include/kernel/base.h | 199 ++++++++++++++ kaleid/include/kernel/sched.h | 107 ++++++++ kaleid/include/kernel/terminal.h | 91 +++++++ 30 files changed, 3010 insertions(+) create mode 100644 build/obj/kaleid/crtlib/.placeholder create mode 100644 build/obj/kaleid/crtlib/test/.paceholder create mode 100644 build/obj/kaleid/extras/.placeholder create mode 100644 kaleid/crtlib/arith.c create mode 100644 kaleid/crtlib/atoi.c create mode 100644 kaleid/crtlib/itoa.c create mode 100644 kaleid/crtlib/memory.c create mode 100644 kaleid/crtlib/rand.c create mode 100644 kaleid/crtlib/sprintf.c create mode 100644 kaleid/crtlib/status.c create mode 100644 kaleid/crtlib/string.c create mode 100644 kaleid/crtlib/strtol.c create mode 100644 kaleid/extras/argv.c create mode 100644 kaleid/extras/prog.c create mode 100644 kaleid/include/base/assert.h create mode 100644 kaleid/include/base/bdefs.h create mode 100644 kaleid/include/base/crtlib.h create mode 100644 kaleid/include/base/errno.h create mode 100644 kaleid/include/base/limits.h create mode 100644 kaleid/include/base/masks.h create mode 100644 kaleid/include/base/types.h create mode 100644 kaleid/include/extras/argv.h create mode 100644 kaleid/include/extras/list.h create mode 100644 kaleid/include/extras/locks.h create mode 100644 kaleid/include/extras/prog.h create mode 100644 kaleid/include/kalbase.h create mode 100644 kaleid/include/kalext.h create mode 100644 kaleid/include/kernel/base.h create mode 100644 kaleid/include/kernel/sched.h create mode 100644 kaleid/include/kernel/terminal.h diff --git a/build/obj/kaleid/crtlib/.placeholder b/build/obj/kaleid/crtlib/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build/obj/kaleid/crtlib/test/.paceholder b/build/obj/kaleid/crtlib/test/.paceholder new file mode 100644 index 0000000..e69de29 diff --git a/build/obj/kaleid/extras/.placeholder b/build/obj/kaleid/extras/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/kaleid/crtlib/arith.c b/kaleid/crtlib/arith.c new file mode 100644 index 0000000..68e2575 --- /dev/null +++ b/kaleid/crtlib/arith.c @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Arithmetical functions // +//----------------------------------------------------------------------------// + +// do not mask anything +#define _KALEID_UNMASKED +#include + +int _osk_abs(int x) +{ + return abs(x); +} + +long _osk_labs(long x) +{ + return labs(x); +} + +div_t _osk_div(int x, int y) +{ + return div(x, y); +} + +ldiv_t _osk_ldiv(long x, long y) +{ + return ldiv(x, y); +} + diff --git a/kaleid/crtlib/atoi.c b/kaleid/crtlib/atoi.c new file mode 100644 index 0000000..eaf42b6 --- /dev/null +++ b/kaleid/crtlib/atoi.c @@ -0,0 +1,37 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Conversion utilities - atoi family // +//----------------------------------------------------------------------------// + +#include + +// +// String to integer +// Do not change errno +// +#define _ATOI_IMPL(_Name, _Type, _Func) \ + _Type _Name(const char *str) { \ + __get_errno(old); \ + _Type ret = (_Type)_Func(str, NULL, 0); \ + __set_errno(old); \ + return ret; \ + } + + +// ISO C does not allow extra ‘;’ outside of a function +#if defined(_NEED_ATOI) +_ATOI_IMPL(atoi, int, strtol) +#elif defined(_NEED_ATOL) +_ATOI_IMPL(atol, long, strtol) +#elif defined(_NEED_ATOU) +_ATOI_IMPL(atou, uint, strtoul) +#elif defined(_NEED_ATOUL) +_ATOI_IMPL(atoul, ulong, strtoul) +#else +#error "What am I supposed to declare?" +#endif + diff --git a/kaleid/crtlib/itoa.c b/kaleid/crtlib/itoa.c new file mode 100644 index 0000000..b393a57 --- /dev/null +++ b/kaleid/crtlib/itoa.c @@ -0,0 +1,125 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Conversion utilities - itoa family // +//----------------------------------------------------------------------------// + +#include + +// +// 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) +// + +#if defined(_NEED_ITOA) + +#define _IL_MIN INT_MIN +#define _IL_MIN_STRING "-2147483648" +char *itoa(int i, char *str, int base) +{ + int rem; + +#elif defined(_NEED_LTOA) + +#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) + // + // Deal with negatives + // + int neg = 0; + if (i < 0 && base == 10) { + // + // Handle INT_MIN and LONG_MIN... + // + if (__builtin_expect(i == _IL_MIN, 0)) { + strcpy(orig, _IL_MIN_STRING); + goto leave; + } + + else { + neg = 1; + i = -i; + } + } +#endif + + // + // Only handle base 2 -> 36 + // + if (base < 2 || base > 36) { + __set_errno(EINVAL); + *orig = '\0'; + goto leave; + } + + // + // Deal with zero separately + // + if (i == 0) { + *str++ = '0'; + *str = '\0'; + goto leave; + } + + // + // Compute digits... in reverse order + // + while (i > 0) { + rem = i % base; + *str++ = (rem > 9) + ? (rem - 10) + 'a' + : rem + '0'; + i /= base; + } + +#if defined(_NEED_ITOA) || defined(_NEED_LTOA) + if (neg) *str++ = '-'; +#endif + + *str = '\0'; + + // + // Reverse the string + // + orig = strrev2(orig); + + // + // End of conversion + // +leave: + return orig; +} + + diff --git a/kaleid/crtlib/memory.c b/kaleid/crtlib/memory.c new file mode 100644 index 0000000..925ae9f --- /dev/null +++ b/kaleid/crtlib/memory.c @@ -0,0 +1,245 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: mem*() functions // +//----------------------------------------------------------------------------// + +#include + +//------------------------------------------// +// memset() family // +//------------------------------------------// + +// +// Set "bytes"-many bytes starting from ptr to val +// +void *memset(void *ptr, int val, size_t bytes) +{ + uchar *uptr = (uchar *)ptr; + + // + // Deal with bytes before start of the first aligned qword + // + while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // + // At this point we're qword-aligned + // + if (bytes > QWORD_SIZE) { + const ulong uval = ((ulong)val << 56) | ((ulong)val << 48) + | ((ulong)val << 40) | ((ulong)val << 32) + | ((ulong)val << 24) | ((ulong)val << 16) + | ((ulong)val << 8) | ((ulong)val); + + ulong *uqptr = (ulong *)ptr; + + // + // Moving fast, qword by qword + // + while (bytes > QWORD_SIZE) { + *uqptr++ = uval; + bytes -= QWORD_SIZE; + } + + uptr = (uchar *)(ulong)uqptr; + } + + // + // Deal with the few remaining bytes + // + while (bytes--) *uptr++ = (uchar)val; + + return ptr; +} + +// +// Set "words"-many words starting from ptr to val +// +void *memsetw(void *ptr, int val, size_t words) +{ + ushort *uptr = (ushort *)ptr; + + // + // Check whether we can we do this an aligned way + // + if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { + // + // We can't, so we write word by word all the way up + // + while (words--) *uptr++ = (ushort)val; + return uptr; + } + + while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) { + *uptr++ = (ushort)val; + } + + if (words > QWORDS_TO_WORDS(1)) { + const ulong uval = ((ulong)val << 48) | ((ulong)val << 32) + | ((ulong)val << 16) | ((ulong)val); + + ulong *uqptr = (ulong *)uptr; + + while (words > QWORDS_TO_WORDS(1)) { + words -= QWORDS_TO_WORDS(1); + *uqptr++ = uval; + } + + uptr = (ushort *)(ulong)uqptr; + } + + while (words--) *uptr++ = (ushort)val; + + return ptr; +} + +// +// Set "dwords"-many dwords starting from ptr to val +// XXX unimplemented +// +void *memsetd(void *ptr, int val, size_t dwords) +{ + (void)val; + (void)dwords; + + return ptr; +} + +// +// Set "qwords"-many qwords starting from ptr to val +// +void *memsetq(void *ptr, long val, size_t qwords) +{ + ulong *uptr = (ulong *)ptr; + + // + // There's no need to check for alignment + // + while (qwords--) *uptr++ = (ulong)val; + + return ptr; +} + +//------------------------------------------// +// Other mem*() functions // +//------------------------------------------// + +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + return memsetb(ptr, 0, bytes); +} + +// +// Copy "bytes"-many bytes of src to dst +// Does not deal with overlapping blocks (memmove's job) +// +void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) +{ + const ulong *usrc = (const ulong *)src; + ulong *udst = (ulong *)dst; + + if unlikely (bytes == 0) return dst; + + // + // Can align both src and dst at once at once? + // + if unlikely ((ulong)src % WORD_ALIGN == 1 + && (ulong)dst % WORD_ALIGN == 1) { + const uchar *ubsrc = (const uchar *)usrc; + uchar *ubdst = (uchar *)udst; + + // + // Yes we can, we're guaranteed to be word-aligned now + // + *ubdst++ = *ubsrc++; + bytes--; + + udst = (ulong *)ubdst; + usrc = (ulong *)ubsrc; + } + + const ushort *uwsrc = (const ushort *)usrc; + ushort *uwdst = (ushort *)udst; + + // + // Align either dst or src for qword access + // + while ((ulong)dst % QWORD_ALIGN > 0 + && (ulong)src % QWORD_ALIGN > 0 + && bytes > WORD_SIZE) { + + *uwdst++ = *uwsrc++; + bytes -= WORD_SIZE; + } + + udst = (ulong *)uwdst; + usrc = (ulong *)uwsrc; + + // + // This should be most of the job + // + while (bytes > QWORD_SIZE) { + *udst++ = *usrc++; + bytes -= QWORD_SIZE; + } + + const uchar *ubsrc = (const uchar *)usrc; + ushort *ubdst = (ushort *)udst; + + // + // Deal with the few bytes left + // + while (bytes--) *ubdst ++ = *ubsrc++; + + return dst; +} + +// +// Move memory from src to dest, even if they overlap +// +void *memmove(void *dst, const void *src, size_t bytes) +{ + const uchar *usrc = src; + uchar *udst = dst; + + // + // Can we use memcpy() safely? + // + if (udst < usrc) { + return memcpy(dst, src, bytes); + } + + // + // No, so we go backwards + // + uchar *usrc_end = (uchar *)usrc + bytes - 1; + uchar *udst_end = udst + bytes - 1; + while (bytes--) *udst_end-- = *usrc_end--; + + return dst; +} + +// +// Compare memory areas +// +int memcmp(const void *ptr1, const void *ptr2, size_t bytes) +{ + const uchar *uptr1 = ptr1; + const uchar *uptr2 = ptr2; + + while (bytes--) { + if (*uptr1++ != *uptr2++) { + return uptr1[-1] < uptr2[-1] ? -1 : 1; + } + } + + return 0; +} diff --git a/kaleid/crtlib/rand.c b/kaleid/crtlib/rand.c new file mode 100644 index 0000000..186311f --- /dev/null +++ b/kaleid/crtlib/rand.c @@ -0,0 +1,34 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: RNG related functions // +//----------------------------------------------------------------------------// + +#include + +// +// Seed value +// +static ulong next = 7756; + +// +// Returns a pseudo-random integer +// To be improved +// +int rand(void) +{ + next = next * 1103515245 + 12347; + return (uint)(next / 65536); +} + +// +// (Re)Set the random seed +// +void srand(uint seed) +{ + next = (ulong)seed; +} + diff --git a/kaleid/crtlib/sprintf.c b/kaleid/crtlib/sprintf.c new file mode 100644 index 0000000..f0f6b15 --- /dev/null +++ b/kaleid/crtlib/sprintf.c @@ -0,0 +1,79 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: sprintf()-related functions // +//----------------------------------------------------------------------------// + +#include + +// +// 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; + va_list ap; + + va_start(ap); + ret = vsnprintf(str, SIZE_T_MAX, fmt, ap); + va_end(ap); + + return ret; +} + +int vsprintf(char *str, const char *fmt, va_list ap) +{ + return vsnprintf(str, SIZE_T_MAX, fmt, 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, ...) +{ + int ret; + va_list ap; + + va_start(ap); + ret = vsnprintf(str, n, fmt, ap) + va_end(ap); + + return ret; +} + +int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) +{ + int ret = 0; + + // + // Go throught the format string + // + while (*fmt) { + if (*fmt != '%') { + // + // Even if we don't have any more room we still increase ret + // + if (ret++ < n) { + *str++ = *fmt++; + } + continue; + } + + switch (*fmt) { + case 'd': + default: + break; + } + } + + return ret; +} + + diff --git a/kaleid/crtlib/status.c b/kaleid/crtlib/status.c new file mode 100644 index 0000000..6fc3c38 --- /dev/null +++ b/kaleid/crtlib/status.c @@ -0,0 +1,34 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Implementation of describe_status() // +//----------------------------------------------------------------------------// + +#include + +error_t __errno = 0; + +/* +static const char *descriptions[] = { + [-SUCCESS] = "Success", + [-FAILED] = "Failed (no precision)", + [-NOT_PERMITTED] = "Operation not permitted", + [-ACCESS_DENIED] = "Access denied", + + [-BAD_ARGUMENT] = "Bad argument", + [-BAD_ARG_RANGE] = "Bad argument (not in range)", + [-BAD_ARG_NULL] = "Bad argument (null pointer)", +}; + +const char *describe_status(status_t status) +{ + (void)descriptions; + (void)status; + + // XXX + return ""; +} +*/ diff --git a/kaleid/crtlib/string.c b/kaleid/crtlib/string.c new file mode 100644 index 0000000..895d031 --- /dev/null +++ b/kaleid/crtlib/string.c @@ -0,0 +1,330 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: String-related functions // +//----------------------------------------------------------------------------// + +#include + +// +// Compare two strings +// +int strcmp(const char *str1, const char *str2) +{ + while (*str1 == *str2 && *str2) str1++, str2++; + + return *(uchar *)str1 - *(uchar *)str2; +} + +// +// Compare at most n bytes of two strings +// +int strncmp(const char *str1, const char *str2, size_t n) +{ + size_t it = 0; + + while (*str1 == *str2 && *str2 && it < n) str1++, str2++, it++; + + return *(uchar *)str1 - *(uchar *)str2; +} + +// +// Return str's length +// +size_t strlen(const char *str) +{ + const char *base = str; + + while (*str) str++; + + return str - base; +} + +// +// Return a pointer to the first occurence of ch in str, +// or str's null-terminator if none is found +// +char *strchrnul(const char *str, int ch) +{ + while ((*str && *str != (char)ch)) str++; + + return (char *)str; +} + +// +// Return a pointer to the first occurence of ch in str, +// NULL if none is found +// +char *strchr(const char *str, int ch) +{ + while ((*str && *str != (char)ch)) str++; + + return *str ? (char *)str : NULL; +} + +// +// Return a point to the last occurence of ch in str, +// NULL if none is found +// +char *strrchr(const char *str, int ch) +{ + char *ptr = NULL; + + while (*str) { + if (*str == ch) { + ptr = (char *)str; + } + str++; + } + + return ptr; +} + +// +// Return the length of the longest inital segment of str +// that only contains characters in acc +// +size_t strspn(const char *str, const char *acc) +{ + const char *ptr = str; + + while (*ptr && strchr(acc, *ptr) != NULL) ptr++; + + return ptr - str; +} + +// +// Return the length of the longest initial segment of str +// that does not contain any character in rej +// +size_t strcspn(const char *str, const char *rej) +{ + const char *ptr = str; + + while (*ptr && strchr(rej, *ptr) == NULL) ptr++; + + return ptr - str; +} + +// +// Return the first occurence in str of any byte in acc +// +char *strpbrk(const char *str, const char *acc) +{ + str += strcspn(str, acc); + + return *str ? (char *)str : NULL; +} + +// +// Return the first occurence of the substring needle +// in the string haystack, NULL if none is found +// Null-terminators aren't compared +// +char *strstr(const char *haystack, const char *needle) +{ + const size_t needle_size = strlen(needle); + + // + // Moves haystack to first occurence of the needle's first byte + // + while ((haystack = strchr(haystack, *needle)) != NULL) { + if (strncmp(haystack, needle, needle_size) == 0) { + return (char *)haystack; + } + } + + return NULL; +} + +// +// Tokenize a string, using saveptr as a savestate +// We let a segmentation fault happen if *saveptr == NULL +// +char *strtok_r(char *restrict str, const char *restrict delim, char **restrict saveptr) +{ + assert(*saveptr != NULL); + + if (str == NULL) str = *saveptr; + + // + // Skip initial segments composed only of delimiters + // + str += strspn(str, delim); + + // + // If str is empty, store it in saveptr so that next call + // still finds an empty strings and returns NULL + // + if (*str == 0) { + *saveptr = str; + return NULL; + } + + char *ptr = str, *tok_end = strpbrk(str, delim); + + // + // If we found the last token, set *saveptr to a str's null-terminator + // Otherwise, null-terminate token and save next byte + // + + if (tok_end == NULL) { + while (*ptr) ptr++; + *saveptr = ptr; + } + + else { + *tok_end = 0; + *saveptr = tok_end + 1; + } + + return str; +} + +// +// Tokenize a string in a very thread-unsafe way +// +char *strtok(char *restrict str, const char *restrict delim) +{ + static char *saveptr = NULL; + + KalAssert(FALSE); + + if (str) saveptr = str; + + return strtok_r(str, delim, &saveptr); +} + +// +// Copy the string src into dest +// +char *strcpy(char *restrict dest, const char *restrict src) +{ + char *base = dest; + + while ((*dest++ = *src++)); + + return base; +} + +// +// strcpy() but always writes n bytes +// Will not null-terminate for strings longer than n bytes +// +char *strncpy(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it; + + for (it = 0; it < n && src[it]; it++) { + dest[it] = src[it]; + } + + while (it < n) dest[it++] = 0; + + return dest; +} + +// +// Copies at most n-1 bytes from src to dest +// Always null-terminates dest, but doesn't fill +// dest's contents past the null-terminator +// +// Returns the number of bytes written +// +size_t strnzcpy(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it; + + for (it = 0; it < n - 1 && src[it]; it++) { + dest[it] = src[it]; + } + + dest[it++] = 0; + + return it; +} + +// +// Appends a copy of src at the end of dest +// +char *strcat(char *restrict dest, const char *restrict src) +{ + char *base = dest; + while (*dest) dest++; + while ((*dest++ = *src++)); + return base; +} + +// +// Appends a copy of at most n bytes of src at the end of dest +// +char *strncat(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it, off = 0; + + while (dest[off]) off++; + + for (it = 0; it < n && src[it]; it++) { + dest[it+off] = src[it]; + } + + while (it++ < n) dest[it+off] = 0; + + return dest; +} + +// +// Appends a copy of at most n bytes of src at the end of dest +// Always null-terminates, and returne TRUE or FALSE depending on whether +// regular strcat() would have null-terminated this string, or not +// +size_t strnzcat(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it, off = 0; + + while (dest[off]) off++; + + for (it = 0; it < n - 1 && src[it]; it++) { + dest[it+off] = src[it]; + } + + dest[it+off] = 0; + + return it+1; +} + +// +// Reverses the string src, putting the result into dest +// +char *strrev(char *restrict dest, const char *restrict src) +{ + char *orig = dest; + size_t n = strlen(src); + + dest[n--] = '\0'; + + while ((*dest++ = src[n--])); + + return orig; +} + +// +// Reverses a string, modifying it +// +char *strrev2(char *str) +{ + char ch, *orig = str; + size_t n = strlen(str); + char *temp = str + n - 1; + + while (temp > str) { + ch = *temp; + *temp-- = *str; + *str++ = ch; + } + + return orig; +} diff --git a/kaleid/crtlib/strtol.c b/kaleid/crtlib/strtol.c new file mode 100644 index 0000000..87a132f --- /dev/null +++ b/kaleid/crtlib/strtol.c @@ -0,0 +1,27 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: strto(u)l functions // +//----------------------------------------------------------------------------// + +#include + +long strtol(const char *str, char **endp, int base) { + (void)str; + (void)endp; + (void)base; + __set_errno(ENOSYS); + return 0; +} + +ulong strtoul(const char *str, char **endp, int base) { + (void)str; + (void)endp; + (void)base; + __set_errno(ENOSYS); + return 0; +} + diff --git a/kaleid/extras/argv.c b/kaleid/extras/argv.c new file mode 100644 index 0000000..e4e2a49 --- /dev/null +++ b/kaleid/extras/argv.c @@ -0,0 +1,23 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Command line parsing utilities // +//----------------------------------------------------------------------------// + +#include +#include +#include +#if 0 +error_t KalCmdLineToArgV(const char *cmdLine, + int argcMax, + int *argcPtr, + const char **argv); + +error_t KalArgVToCmdLine(const char *cmdLine, + size_t lengthMax, + int argc, + const char **argv); +#endif diff --git a/kaleid/extras/prog.c b/kaleid/extras/prog.c new file mode 100644 index 0000000..e3591d0 --- /dev/null +++ b/kaleid/extras/prog.c @@ -0,0 +1,48 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Program utilities // +//----------------------------------------------------------------------------// + +#include +#include + +#ifdef _KALEID_KERNEL + +const char *__progname = "kaleid-kernel"; +const char *__progvers = "alpha-0.0.1"; + +#else + +const char *__progname = "kaleid-test"; +const char *__progvers = "(n/a)"; + +#endif + +const char *KalGetProgName(void) +{ + return __progname; +} + +const char *KalGetProgVersion(void) +{ + return __progvers; +} + +bool KalSetProgVers(const char *vers) +{ + (void)vers; + __set_errno(ENOSYS); + return false; +} + +bool KalSetProgName(const char *name) +{ + (void)name; + __set_errno(ENOSYS); + return false; +} + diff --git a/kaleid/include/base/assert.h b/kaleid/include/base/assert.h new file mode 100644 index 0000000..e5d5608 --- /dev/null +++ b/kaleid/include/base/assert.h @@ -0,0 +1,117 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid assert() support // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_ASSERT_H +#define _KALBASE_ASSERT_H + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef noreturn +#define noreturn __attribute__((__noreturn__)) +#endif + +#ifndef unlikely +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +#ifndef static_assert +#define static_assert _Static_assert +#endif + +//------------------------------------------// +// API compatibility checks // +//------------------------------------------// + +#define _SA_MSG "Incompatible type sizes" +static_assert(sizeof(char) == 1, _SA_MSG); +static_assert(sizeof(short) == 2, _SA_MSG); +static_assert(sizeof(int) == 4, _SA_MSG); +static_assert(sizeof(long) == 8, _SA_MSG); +static_assert(sizeof(void *) == 8, _SA_MSG); +#undef _SA_MSG + +//------------------------------------------// +// Assert core // +//------------------------------------------// + +// +// Failed assert handler +// +noreturn void __assert_handler(const char *, const char *, int, const char *); + +// +// Unconditional assert +// +#define KalAlwaysAssert(x) \ + do { \ + if unlikely (!(x)) \ + __assert_handler(#x, __FILE__, __LINE__, __func__); \ + } while (0) + +//------------------------------------------// +// When debugging // +//------------------------------------------// + +#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(KalAssert) + +// +// Check whether (x) holds, if not call __assert_handler +// +#define KalAssert KalAlwaysAssert + +#ifndef _OSK_SOURCE + +// +// When not building for OS/K, use the system's assert +// +#include + +#undef KalAwaysAssert +#define KalAlwaysAssert assert + +#endif + +//------------------------------------------// +// When not debugging // +//------------------------------------------// + +#else + +#ifndef NDEBUG +#define NDEBUG 1 +#endif + +#ifndef _NO_DEBUG +#define _NO_DEBUG 1 +#endif + +#ifndef KalAssert +#define KalAssert(x) ((void)0) +#endif + +#endif + +//------------------------------------------// +// Aliases and extensions // +//------------------------------------------// + +#ifndef assert +#define assert KalAssert +#endif + +#define KalAssertEx(x,m) KalAssert(x && m) +#define KalAlwaysAssertEx(x,m) KalAlwaysAssert(x && m) + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/base/bdefs.h b/kaleid/include/base/bdefs.h new file mode 100644 index 0000000..1c1ef13 --- /dev/null +++ b/kaleid/include/base/bdefs.h @@ -0,0 +1,100 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid general preprocessor constants // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_BDEFS_H +#define _KALDEFS_BDEFS_H + +//------------------------------------------// +// Actual constants // +//------------------------------------------// + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef NULL +#define NULL 0L +#endif + +#ifndef INITOK +#define INITOK ((unsigned int)0xCAFEBABE) +#endif + +//------------------------------------------// +// Keywords // +//------------------------------------------// + +#ifndef __alignof_is_defined +#define __alignof_is_defined +#define alignof _Alignof +#endif + +#ifndef __alignas_is_defined +#define __alignas_is_defined +#define alignas _Alignas +#endif + +#ifndef __bool_true_false_are_defined +#define __bool_true_false_are_defined +# define bool _Bool +# define true 1 +# define false 0 +# ifndef TRUE +# define TRUE 1 +# endif +# ifndef FALSE +# define FALSE 0 +# endif +#endif + +//------------------------------------------// +// Attributes and macros // +//------------------------------------------// + +#ifndef _PACKED +#define _PACKED __attribute__((__packed__)) +#endif + +#ifndef noreturn +#define noreturn __attribute__((__noreturn__)) +#endif + +#ifndef likely +#define likely(x) (__builtin_expect((x), 1)) +#endif + +#ifndef unlikely +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +#ifndef _STR +#define _STR(x) #x +#endif + +#ifndef _XSTR +#define _XSTR(x) _STR(x) +#endif + +//------------------------------------------// +// API specific macros // +//------------------------------------------// + +#ifndef KALAPI +# define KALAPI +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/base/crtlib.h b/kaleid/include/base/crtlib.h new file mode 100644 index 0000000..e4e7767 --- /dev/null +++ b/kaleid/include/base/crtlib.h @@ -0,0 +1,242 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C runtime library // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_CRTLIB_H +#define _KALBASE_CRTLIB_H + +//------------------------------------------// +// Typedefs // +//------------------------------------------// + +#ifndef __error_t_defined +#define __error_t_defined +typedef int error_t; +#endif + +#ifndef __size_t_defined +#define __size_t_defined +typedef unsigned long size_t; +#endif + +#ifndef __va_list_defined +#define __va_list_defined +typedef __builtin_va_list va_list; +#endif + +#ifndef __div_t_defined +#define __div_t_defined +typedef struct { int quot, rem; } div_t; +#endif + +#ifndef __ldiv_t_defined +#define __ldiv_t_defined +typedef struct { long quot, rem; } ldiv_t; +#endif + +//------------------------------------------// +// Global variables // +//------------------------------------------// + +#ifndef _KALEID_KERNEL + +extern error_t __errno; + +#ifndef errno +#define errno __errno +#endif + +#define __get_errno(x) error_t x = errno; +#define __set_errno(x) (errno = (x)) + +#else + +#define errno +#define __get_errno(x) +#define __set_errno(x) + +#endif + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef _NO_MASK +#define _NO_MASK +#endif + +//------------------------------------------// +// va_list utilities // +//------------------------------------------// + +#ifndef va_start +#define va_start __builtin_va_start +#endif + +#ifndef va_arg +#define va_arg __builtin_va_arg +#endif + +#ifndef va_copy +#define va_copy __builtin_va_copy +#endif + +#ifndef va_end +#define va_end __builtin_va_end +#endif + +//------------------------------------------// +// Memory management utilities // +//------------------------------------------// + +#ifndef memsetb +#define memsetb memset +#endif + +#ifndef memchrb +#define memchrb memchr +#endif + +void *memset(void *, int, size_t); +void *memsetw(void *, int, size_t); +void *memsetd(void *, int, size_t); +void *memsetq(void *, long, size_t); + +void *memchr(const void *, int, size_t); +void *memchrw(const void *, int, size_t); +void *memchrd(const void *, int, size_t); +void *memchrq(const void *, long, size_t); + +void *memrchr(const void *, int, size_t); + +void *memcpy(void *restrict, const void *restrict, size_t); +void *memmove(void *, const void *, size_t); + +void *memzero(void *, size_t); +int memcmp(const void *, const void *, size_t); + +//------------------------------------------// +// String manipulation utilities // +//------------------------------------------// + +size_t strlen(const char *); +size_t strspn(const char *, const char *); +size_t strcspn(const char *, const char *); + +int strcmp(const char *, const char *); +int strncmp(const char *, const char *, size_t); + +char *strchr(const char *, int); +char *strrchr(const char *, int); +char *strchrnul(const char *, int); + +char *strstr(const char *, const char *); +char *strpbrk(const char *, const char *); + +char *strtok(char *restrict, const char *restrict); +char *strtok_r(char *restrict, const char *restrict, char **restrict); + +char *strcpy (char *restrict, const char *restrict); +char *strncpy (char *restrict, const char *restrict, size_t); +size_t strnzcpy(char *restrict, const char *restrict, size_t); + +char *strcat (char *restrict, const char *restrict); +char *strncat (char *restrict, const char *restrict, size_t); +size_t strnzcat(char *restrict, const char *restrict, size_t); + +char *strrev(char *restrict, const char *restrict); +char *strrev2(char *); + +int sprintf(char *, const char *, ...); +int snprintf(char *, size_t, const char *, ...); +int vsprintf(char *, const char *, va_list); +int vsnprintf(char *, size_t, const char *, va_list); + +//------------------------------------------// +// Type conversion utilities // +//------------------------------------------// + +char *itoa(int, char *, int); +char *ltoa(long, char *, int); +char *utoa(unsigned int, char *, int); +char *ultoa(unsigned long, char *, int); + +int atoi(const char *); +long atol(const char *); +unsigned int atou(const char *); +unsigned long atoul(const char *); + +long strtol (const char *restrict, char **restrict, int); +unsigned long strtoul(const char *restrict, char **restrict, int); + +//------------------------------------------// +// RNG utilities // +//------------------------------------------// + +int rand(void); +void srand(unsigned int); + +//------------------------------------------// +// Time utilities // +//------------------------------------------// + +//------------------------------------------// +// Diverse utilities // +//------------------------------------------// + +char *strerror(int); +char *strsignal(int); + +//------------------------------------------// +// Arithmetical macros // +//------------------------------------------// + +#ifndef __abs +#define __abs +static inline int abs(int __x) +{ + return __x < 0 ? -__x : __x; +} +#endif + +#ifndef __labs +#define __labs +static inline long labs(long __x) +{ + return __x < 0 ? -__x : __x; +} +#endif + +#ifndef __div +#define __div +static inline div_t div(int __x, int __y) +{ + div_t __res; + __res.quot = __x/__y; + __res.rem = __x%__y; + return __res; +} +#endif + +#ifndef __ldiv +#define __ldiv +static inline ldiv_t ldiv(long __x, long __y) +{ + ldiv_t __res; + __res.quot = __x/__y; + __res.rem = __x%__y; + return __res; +} +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/base/errno.h b/kaleid/include/base/errno.h new file mode 100644 index 0000000..c4476e3 --- /dev/null +++ b/kaleid/include/base/errno.h @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Values for errno_t and errno // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_ERRNO_H +#define _KALBASE_ERRNO_H + +//------------------------------------------// +// "errno" values // +//------------------------------------------// + +// Everything went fine +#define EOK 0 + +// Operation not permitted +#define EPERM 1 + +// No such file or directory +#define ENOENT 2 + +// No such process +#define ESRCH 3 + +// Syscall interrupted (e.g. by signal) +#define EINTR 4 + +// I/0 error +#define EIO 5 + +// No such device or address +#define ENXIO 6 + +// Argument list too long +#define E2BIG 7 + +// Not an executable format +#define ENOEXEC 8 + +// Bad file number +#define EBADF 9 + +// Invalid argument +#define EINVAL 22 + +// Functionality not implemented +#define ENOSYS 38 + +// Component crashed +#define ECRASH 500 + +// System is panicking +#define EPANIC 600 + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/base/limits.h b/kaleid/include/base/limits.h new file mode 100644 index 0000000..affe0fa --- /dev/null +++ b/kaleid/include/base/limits.h @@ -0,0 +1,117 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid type limits definitions // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_LIMITS_H +#define _KALBASE_LIMITS_H + +//------------------------------------------// +// Data sizes blocks // +//------------------------------------------// + +#ifndef DATA_SIZE_BLOCK +#define DATA_SIZE_BLOCK +# define BYTE_SIZE sizeof(char) +# define WORD_SIZE sizeof(short) +# define DWORD_SIZE sizeof(int) +# define QWORD_SIZE sizeof(long) +#endif + +#ifndef DATA_ALIGN_BLOCK +#define DATA_ALIGN_BLOCK +# define BYTE_ALIGN alignof(char) +# define WORD_ALIGN alignof(short) +# define DWORD_ALIGN alignof(int) +# define QWORD_ALIGN alignof(long) +#endif + +#ifndef DATA_BITS_BLOCK +#define DATA_BITS_BLOCK +# define BYTE_BIT 8 +# define CHAR_BIT (BYTE_SIZE * BYTE_BIT) +# define WORD_BIT (WORD_SIZE * BYTE_BIT) +# define DWORD_BIT (DWORD_SIZE * BYTE_BIT) +# define QWORD_BIT (QWORD_SIZE * BYTE_BIT) +# define SHORT_BIT WORD_BIT +# define INT_BIT DWORD_BIT +# define LONG_BIT QWORD_BIT +#endif + +#ifndef DATA_SHIFTS_BLOCK +#define DATA_SHIFTS_BLOCK +# define BYTES_TO_WORDS(B) ((B) >> 1) +# define BYTES_TO_DWORDS(B) ((B) >> 2) +# define BYTES_TO_QWORDS(B) ((B) >> 3) +# define WORDS_TO_BYTES(W) ((W) << 1) +# define WORDS_TO_DWORDS(W) ((W) >> 1) +# define WORDS_TO_QWORDS(W) ((W) >> 2) +# define DWORDS_TO_BYTES(D) ((D) << 2) +# define DWORDS_TO_WORDS(D) ((D) << 1) +# define DWORDS_TO_QWORDS(D) ((D) >> 1) +# define QWORDS_TO_BYTES(Q) ((Q) << 3) +# define QWORDS_TO_WORDS(Q) ((Q) << 2) +# define QWORDS_TO_DWORDS(Q) ((Q) << 1) +#endif + +//------------------------------------------// +// Numeric data limits // +//------------------------------------------// + +#ifndef DATA_MAX_LIMITS_BLOCK +#define DATA_MAX_LIMITS_BLOCK +# define SCHAR_MAX ((signed char) 0x7F) +# define SHRT_MAX ((short) 0x7FFF) +# define INT_MAX ((int) 0x7FFFFFFF) +# define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFF) +# define UCHAR_MAX ((unsigned char) 0xFF +# define USHRT_MAX ((unsigned short) 0xFFFF) +# define UINT_MAX ((unsigned int) 0xFFFFFFFF) +# define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFF) +#endif + +#ifndef DATA_MIN_LIMITS_BLOCK +#define DATA_MIN_LIMITS_BLOCK +# define SCHAR_MIN ((signed char) -SCHAR_MAX - 1) +# define SHRT_MIN ((short) -SHRT_MAX - 1) +# define INT_MIN ((int) -INT_MAX - 1) +# define LONG_MIN ((long) -LONG_MAX - 1L) +#endif + +#ifndef DATA_CHAR_LIMITS_BLOCK +#define DATA_CHAR_LIMITS_BLOCK +# ifdef __CHAR_UNSIGNED__ +# define CHAR_MIN ((char)0) +# define CHAR_MAX ((char)UCHAR_MAX) +# else +# define CHAR_MIN ((char)SCHAR_MIN) +# define CHAR_MAX ((char)SCHAR_MAX) +# endif +#endif + +#ifndef DATA_SPTYPES_LIMITS_BLOCK +#define DATA_SPTYPES_LIMITS_BLOCK +# define SSIZE_T_MIN LONG_MIN +# define SSIZE_T_MAX LONG_MAX +# define SIZE_T_MAX ULONG_MAX +#endif + +#ifdef NEED_MORE_USELESS_DATA +# define UCHAR_MIN ((unsigned char)0) +# define USHRT_MIN ((unsigned short)0) +# define UINT_MIN ((unsigned int)0) +# define ULONG_MIN ((unsigned long)0) +# ifdef STILL_NEED_MORE_USELESS_DATA +# error "Not enough useless data!" +# endif +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/base/masks.h b/kaleid/include/base/masks.h new file mode 100644 index 0000000..60e861e --- /dev/null +++ b/kaleid/include/base/masks.h @@ -0,0 +1,119 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Masks for the functions in the KCRL // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_MASKS_H +#define _KALBASE_MASKS_H + +//------------------------------------------// + +#define div_t _osk_div_t +#define ldiv_t _osk_ldiv_t + +//------------------------------------------// + +#define memset _osk_memsetb +#define memchr _osk_memchrb + +#define memsetb _osk_memsetb +#define memsetw _osk_memsetw +#define memsetd _osk_memsetd +#define memsetq _osk_memsetq + +#define memchr _osk_memchrb +#define memchrw _osk_memchrw +#define memchrd _osk_memchrd +#define memchrq _osk_memchrq + +#define memcpy _osk_memcpy +#define memmove _osk_memmove + +#define memcmp _osk_memcmp +#define memzero _osk_memzero + +//------------------------------------------// + +#define strlen _osk_strlen +#define strspn _osk_strspn +#define strcspn _osk_strcspn + +#define strcmp _osk_strcmp +#define strncmp _osk_strncmp + +#define strchr _osk_strchr +#define strrchr _osk_strrchr + +#define strstr _osk_strstr +#define strpbrk _osk_strpbrk + +#define strtok _osk_strtok +#define strtok_r _osk_strtok_r + +#define strcpy _osk_strcpy +#define strncpy _osk_strncpy +#define strnzcpy _osk_strnzcpy + +#define strcat _osk_strcat +#define strncat _osk_strncat +#define strnzcat _osk_strnzcat + +#define strrev _osk_strrev +#define strrev2 _osk_strrev2 + +#define sprintf _osk_sprintf +#define snprintf _osk_snprintf +#define vsprintf _osk_vsprintf +#define vsnprintf _osk_vsnprintf + +//------------------------------------------// + + +#define itoa _osk_itoa +#define ltoa _osk_ltoa +#define utoa _osk_utoa +#define ultoa _osk_ultoa + +#define atoi _osk_atoi +#define atol _osk_atol +#define atou _osk_atou +#define atoul _osk_atoul + +#define strtol _osk_strtol +#define strtoul _osk_strtoul + +//------------------------------------------// + +#define rand _osk_rand +#define srand _osk_srand + +//------------------------------------------// + +#define abs _osk_abs +#define labs _osk_labs + +#define min _osk_min +#define lmin _osk_lmin + +#define max _osk_max +#define lmax _osk_lmax + +#define __div +#define __ldiv + +#define div _osk_div +#define ldiv _osk_ldiv + +//------------------------------------------// + +#define strerror _osk_strerror + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/base/types.h b/kaleid/include/base/types.h new file mode 100644 index 0000000..115319a --- /dev/null +++ b/kaleid/include/base/types.h @@ -0,0 +1,120 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C common types // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_TYPES_H +#define _KALBASE_TYPES_H + +//------------------------------------------// +// Basic integer types aliases // +//------------------------------------------// + +#ifndef __base_types_aliases +#define __base_types_aliases +typedef unsigned char uchar; +typedef signed char schar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef signed long long llong; +typedef unsigned long long ullong; +typedef long double ldouble; +#endif + +//------------------------------------------// +// Miscellaneous types // +//------------------------------------------// + +#ifndef __size_t_defined +#define __size_t_defined +typedef unsigned long size_t; +#endif + +#ifndef __ssize_t_defined +#define __ssize_t_defined +typedef signed long ssize_t; +#endif + +#ifndef __wchar_t_defined +#define __wchar_t_defined +typedef signed int wchar_t; +#endif + +#ifndef __off_t_defined +#define __off_t_defined +typedef unsigned long off_t; +#endif + +//------------------------------------------// +// Standard fixed-width integer types // +//------------------------------------------// + +#ifndef __ptrdiff_t_defined +#define __ptrdiff_t_defined +typedef signed long ptrdiff_t; +#endif + +#ifndef __intptr_t_defined +#define __intptr_t_defined +typedef signed long intptr_t; +#endif + +#ifndef __uintptr_t_defined +#define __uintptr_t_defined +typedef unsigned long uintptr_t; +#endif + +#ifndef __intmax_t_defined +#define __intmax_t_defined +typedef signed long intmax_t; +#endif + +#ifndef __uintmax_t_defined +#define __uintmax_t_defined +typedef unsigned long uintmax_t; +#endif + +//------------------------------------------// +// Special types // +//------------------------------------------// + +#ifndef __va_list_defined +#define __va_list_defined +typedef __builtin_va_list va_list; +#endif + +#ifndef __div_t_defined +#define __div_t_defined +typedef struct { int quot, rem; } div_t; +#endif + +#ifndef __ldiv_t_defined +#define __ldiv_t_defined +typedef struct { long quot, rem; } ldiv_t; +#endif + + +//------------------------------------------// +// Kaleid-specific types // +//------------------------------------------// + +#ifndef __error_t_defined +#define __error_t_defined +typedef int error_t; +#endif + +#ifndef __port_t_defined +#define __port_t_defined +typedef ushort port_t; +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/extras/argv.h b/kaleid/include/extras/argv.h new file mode 100644 index 0000000..352c532 --- /dev/null +++ b/kaleid/include/extras/argv.h @@ -0,0 +1,126 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Command line parsing utilities // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALEXTRAS_ARGV_H +#define _KALEXTRAS_ARGV_H + +//------------------------------------------// +// Types // +//------------------------------------------// + +// +// Option types +// +typedef enum { + // + // A flag option, without any more parameters + // + CMDOPT_FLAG, + + // + // An option that expects a parameter + // + CMDOPT_PARAM, + +} CmdOptType_t; + +// +// An option for a command, e.g. "-o file" in "cc -o file" +// +typedef struct { + // + // The option's name, e.g. "help" for "--help" + // May be 0, but only if letter is not zero + // + const char *longName; + + // + // The option's letter, e.g. 'h' for '-h' + // + int letter; + + // + // The option's group, for sorting during --help + // Must be positive and < 256, or option won't shop up + // during help texts + // + int group; + + // + // The option's type, see above + // + CmdOptType_t type; + + // + // Address of the variable to put the parameter into + // Should be an int point for flag arguments, string + // pointer for parameter arguments + // + void *param; + + // + // The option's help text + // If this is 0, this option is hidden + // + const char *helpText; + +} CmdOption_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +int KalComputeArgC(const char *argv[]); + +size_t KalComputeArgVSize(const char *argv[]); + +error_t KalCmdLineToArgV(const char *cmdLine, + int *argcPtr, + const char *argv[]); + +error_t KalArgVToCmdLine(const char *cmdLine, + size_t lengthMax, + int argc, + const char *argv[]); + +error_t KalParseCmdLine(const char *cmdLine, + CmdOption_t *options); + +error_t KalParseArgV(int argc, + const char *argv[], + CmdOption_t *options); + +// +// The "Ex" variants reacts to "--help" and "--version" by themselves +// + +error_t KalParseCmdLineEx(const char *cmdLine, + CmdOption_t *options, + const char *progDesc, + const char *groupDescs[]); + +error_t KalParseArgVEx(int argc, + const char *argv[], + CmdOption_t *options, + const char *progDesc, + const char *groupDescs[]); + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/extras/list.h b/kaleid/include/extras/list.h new file mode 100644 index 0000000..d9f5bd6 --- /dev/null +++ b/kaleid/include/extras/list.h @@ -0,0 +1,273 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Doubly linked lists implementation // +//----------------------------------------------------------------------------// + +#ifdef _KALEID_KERNEL +#error "extra/list.h - Not ready for kernel compilation" +#endif + +#ifndef _KALBASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALEXTRAS_LIST_H +#define _KALEXTRAS_LIST_H + +// +// XXX ¯\_(ツ)_/¯ +// +#ifndef _STDLIB_H +void *malloc(long); +void free(void *); +#endif + +#define AllocMemory malloc +#define FreeMemory free + +//------------------------------------------// +// Data structures // +//------------------------------------------// + +typedef struct sListHead_t { + Lock_t *lock; + unsigned long length; + struct sListNode_t *first; + struct sListNode_t *last; +} ListHead_t; + +typedef struct sListNode_t { + void *data; + ListHead_t *head; + struct sListNode_t *prev; + struct sListNode_t *next; +} ListNode_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +// +// Create a list head with an extern lock +// +static inline ListHead_t +*CreateListHeadWithLock(Lock_t *lock) +{ + ListHead_t *head = AllocMemory(sizeof(ListHead_t)); + + if (head == NULL) return NULL; + + head->first = head->last = NULL; + head->length = 0; + + head->lock = lock; + + return head; +} + +// +// Create a liste head +// +static inline ListHead_t +*CreateListHead(void) +{ + return CreateListHeadWithLock(NULL); +} + +// +// Create a node +// +static inline ListNode_t +*CreateNode(void *data) +{ + ListNode_t *node = AllocMemory(sizeof(ListNode_t)); + + if (node == NULL) return NULL; + + node->data = data; + node->head = NULL; + node->prev = node->next = NULL; + + return node; +} + +// +// Prepend node at beginning of list +// +static inline ListHead_t +*PrependNode(ListHead_t *head, ListNode_t *node) +{ + KalAssert(head && node); + + node->head = head; + node->prev = NULL; + + if (head->length > 0) { + node->next = head->first; + head->first->prev = node; + head->first = node; + } + + else { + head->first = node; + head->last = node; + node->next = NULL; + } + + head->length++; + + return head; +} + +// +// Append node at end of list +// +static inline ListHead_t +*AppendNode(ListHead_t *head, ListNode_t *node) +{ + KalAssert(head && node); + + node->head = head; + node->next = NULL; + + if (head->length > 0) { + node->prev = head->last; + head->last->next = node; + head->last = node; + } + + else { + head->first = node; + head->last = node; + node->prev = NULL; + } + + head->length++; + + return head; +} + +// +// Insert node2 before node1 +// +static inline ListHead_t +*AddNodeBefore(ListHead_t *head, ListNode_t *node1, ListNode_t *node2) +{ + KalAssert(head && node1 && node2 && node1->head == head); + + if (head->first == node1) { + return PrependNode(head, node2); + } + + node2->head = head; + node2->next = node1; + node2->prev = node1->prev; + + // node1->prev does exist + // or node1 would be first + node1->prev->next = node2; + node1->prev = node2; + + head->length++; + + return head; +} + +// +// Insert node2 after node1 +// +static inline ListHead_t +*AddNodeAfter(ListHead_t *head, ListNode_t *node1, ListNode_t *node2) +{ + KalAssert(head && node1 && node2 && node1->head == head); + + if (head->last == node1) { + return AppendNode(head, node2); + } + + node2->head = head; + node2->prev = node1; + node2->next = node1->next; + + node1->next->prev = node2; + node1->next = node2; + + head->length++; + + return head; +} + +// +// Remove node of list (and frees it) +// +static inline ListHead_t +*RemoveNode(ListHead_t *head, ListNode_t *node) +{ + KalAssert(head && node && head->length > 0 && node->head == head); + + if (head->length == 1) { + head->first = head->last = NULL; + goto leave; + } + + if (head->first == node) { + head->first = node->next; + node->next->prev = NULL; + } + + else if (head->last == node) { + head->last = node->prev; + node->prev->next = NULL; + } + + else { + node->prev->next = node->next; + node->next->prev = node->prev; + } + +leave: + head->length--; + FreeMemory(node); + + return head; +} + +// +// Free a node +// +static inline void +DestroyNode(ListNode_t *node) +{ + KalAssert(node); + FreeMemory(node); +} + +// +// Free a list head +// +static inline void +DestroyListHead(ListHead_t *head) +{ + KalAssert(head); + FreeMemory(head); +} + +// +// Access a node's data +// +#define GetNodeData(node, type) ((type)(node)->data) + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/extras/locks.h b/kaleid/include/extras/locks.h new file mode 100644 index 0000000..4ca5220 --- /dev/null +++ b/kaleid/include/extras/locks.h @@ -0,0 +1,170 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Spinlocks and mutexes // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#include +#endif + +#ifdef _KALEID_KERNEL +#ifndef _KALKERN_BASE_H +#include +#endif +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALEXTRAS_LOCKS_H +#define _KALEXTRAS_LOCKS_H + +//------------------------------------------// +// Types // +//------------------------------------------// + +typedef enum eLockType_t { + // + // Mutex-type lock + // + // WARNING + // AquireLock() panics when used on a mutex while not running a process + // + KLOCK_MUTEX, + + // + // Spinlock-type lock + // + KLOCK_SPINLOCK, + +} LockType_t; + +// +// "volatile" may not be actually needed +// +typedef struct sLock_t { + unsigned int initDone; // initialized? + int locked; // is locked? + LockType_t type; // lock type? +#ifdef _KALEID_KERNEL + Thread_t *ownerThread; // unused + Thread_t *waitingThread; // unused +#endif +} volatile Lock_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +// +// Linux syscall vs unimplemented syscall... +// +#ifndef _KALEID_KERNEL +#ifdef _OSK_SOURCE +int KalYieldCPU(void), +#else +int sched_yield(void); +#endif +#endif + +// +// Initialize a lock +// +static inline +void InitLock(Lock_t *lock, LockType_t type) +{ + lock->type = type; + lock->locked = FALSE; + lock->initDone = INITOK; +#ifdef _KALEID_KERNEL + lock->ownerThread = NULL; + lock->waitingThread = NULL; +#endif +} + +// +// Alternative way to initalize a lock +// +#ifdef _KALEID_KERNEL +# define INITLOCK(type) { INITOK, FALSE, (type), NULL, NULL } +#else +# define INITLOCK(type) { INITOK, FALSE, (type) } +#endif + +// +// Destroy a lock +// +static inline +void DestroyLock(Lock_t *lock) +{ + KalAssert(lock->initDone); + + __sync_synchronize(); + lock->initDone = 0; +} + +// +// Aquire the lock +// Panic on double aquisition since that should never happen +// until we have at least a basic scheduler +// +static inline +void AquireLock(Lock_t *lock) +{ + KalAssert(lock->initDone == INITOK); + + while (!__sync_bool_compare_and_swap(&lock->locked, 0, 1)) { +#ifdef _KALEID_KERNEL + StartPanic("AquireLock on an already locked object"); +#else + if likely (lock->type == KLOCK_SPINLOCK) continue; +#ifdef _OSK_SOURCE + else KalYieldCPU(); +#else + else sched_yield(); +#endif +#endif + } + __sync_synchronize(); +} + +// +// Release an already aquired lock +// Panic if the lock was never aquired +// +static inline +void ReleaseLock(Lock_t *lock) +{ +#ifdef _KALEID_KERNEL + KalAssert(lock->ownerThread == GetCurThread()); +#endif + + __sync_synchronize(); + lock->locked = 0; +} + +// +// Tries to aquire lock +// +static inline +bool AttemptLock(Lock_t *lock) +{ + KalAssert(lock->initDone == INITOK); + + bool retval = __sync_bool_compare_and_swap(&lock->locked, 0, 1); + + __sync_synchronize(); + + return retval; +} + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/extras/prog.h b/kaleid/include/extras/prog.h new file mode 100644 index 0000000..bae5b1a --- /dev/null +++ b/kaleid/include/extras/prog.h @@ -0,0 +1,42 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Program utilities // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALEXTRAS_PROG_H +#define _KALEXTRAS_PROG_H + +//------------------------------------------// +// Constants // +//------------------------------------------// + +extern const char *__progname; +extern const char *__progvers; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +const char *KalGetProgName(void); +const char *KalGetProgVersion(void); + +bool KalSetProgVers(const char *); +bool KalSetProgName(const char *); + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/kalbase.h b/kaleid/include/kalbase.h new file mode 100644 index 0000000..618061a --- /dev/null +++ b/kaleid/include/kalbase.h @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid API base minimal include file // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#define _KALBASE_H + +//------------------------------------------// +// Building for OS/K // +//------------------------------------------// + +#if !defined(_OSK_SOURCE) +# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) +# define _OSK_SOURCE 1 +# endif +#endif + +#if !defined(_OSK_SOURCE) && !defined(_KALEID_UNMASKED) +#ifndef _KALBASE_MASKS_H +#include +#endif +#endif + +//------------------------------------------// +// Include common part of API // +//------------------------------------------// + +#ifndef _KALBASE_BDEFS_H +#include +#endif + +#ifndef _KALBASE_ERRNO_H +#include +#endif + +#ifndef _KALBASE_TYPES_H +#include +#endif + +#ifndef _KALBASE_LIMITS_H +#include +#endif + +#ifndef _KALBASE_ASSERT_H +#include +#endif + +#ifndef _KALBASE_CRTLIB_H +#include +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/kalext.h b/kaleid/include/kalext.h new file mode 100644 index 0000000..5dcc19f --- /dev/null +++ b/kaleid/include/kalext.h @@ -0,0 +1,48 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid extras main include file // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALEXT_H +#define _KALEXT_H + +//------------------------------------------// +// Extra headers // +//------------------------------------------// + +#ifndef _KALEXTRAS_LOCKS_H +#include +#endif + +#ifndef _KALEXTRAS_PROG_H +#include +#endif + +#ifndef _KALEXTRAS_ARGV_H +#include +#endif + +#ifndef _KALEID_KERNEL +#ifndef _KALEXTRAS_LIST_H +#include +#endif +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/kernel/base.h b/kaleid/include/kernel/base.h new file mode 100644 index 0000000..6ac5134 --- /dev/null +++ b/kaleid/include/kernel/base.h @@ -0,0 +1,199 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid Kernel base types and functionalities // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_BASE_H +#define _KALKERN_BASE_H + +//------------------------------------------// +// Elementary types // +//------------------------------------------// + +typedef struct sLock_t volatile Lock_t; +typedef struct sThread_t Thread_t; +typedef struct sProcess_t Process_t; +typedef struct sTerminal_t Terminal_t; +typedef struct sListHead_t ListHead_t; +typedef struct sListNode_t ListNode_t; + +//------------------------------------------// +// Values for __kstate // +//------------------------------------------// + +// +// Current state of the kernel +// +typedef enum { + // the kernel is booting + KSTATE_INIT, + + // the kernel is not running a process + KSTATE_KERNEL, + + // a process is running in kernel mode + KSTATE_PROCESS, + + // the kernel is panicking + KSTATE_PANIC, + +} KernelState_t; + +//------------------------------------------// +// Multiprocessor misc. // +//------------------------------------------// + +#ifndef INITOK +#define INITOK ((unsigned int)0xCAFEBABE) +#endif + +#ifndef NCPUS +#define NCPUS 4 +#endif + +#define GetCurCPU() 0 + +// +// Declare an (extern) CPU-local variable +// +#define __DECLARE_PER_CPU(_X, _Tp, _Qual) \ + _Qual _Tp __ ## _X [NCPUS]; \ + static inline _Tp Get ## _X (void) \ + { return __ ## _X [GetCurCPU()]; } \ + static inline void _Set ## _X (_Tp _Y) \ + { (__ ## _X [GetCurCPU()] = _Y); } + +#define DECLARE_PER_CPU(_X, _Tp) \ + __DECLARE_PER_CPU(_X, _Tp, extern) + +#define LOCAL_DEC_PER_CPU(_X, _Tp) \ + __DECLARE_PER_CPU(_X, _Tp, static) + +// +// Actually creates a CPU-local variable +// +#define CREATE_PER_CPU(_X, _Tp) \ + _Tp __ ## _X [NCPUS] = { (_Tp) 0 } + + +//------------------------------------------// +// Global constants // +//------------------------------------------// + +// XXX +DECLARE_PER_CPU(PanicStr, const char *); + +DECLARE_PER_CPU(KernState, KernelState_t); + +DECLARE_PER_CPU(_StdOut, Terminal_t *); +DECLARE_PER_CPU(_StdDbg, Terminal_t *); + +DECLARE_PER_CPU(CurProc, Process_t *); +DECLARE_PER_CPU(CurThread, Thread_t *); + +//------------------------------------------// +// Macros for manipulating said // +// global constants // +//------------------------------------------// + +#define SetKernState(x) \ + do { \ + _SetKernState(x); \ + } while (0) + +#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : NULL) +#define SetStdOut(tm) \ + do { \ + if (GetCurProc() == NULL) \ + _Set_StdOut(tm); \ + } while (0); + +#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : NULL) +#define SetStdDbg(tm) \ + do { \ + if (GetCurProc() == NULL) \ + _Set_StdDbg(tm); \ + } while (0) + +//------------------------------------------// +// Other Macros // +//------------------------------------------// + +// +// Size of a tabulation in spaces +// Default: 4 spaces/tab +// +#define KTABSIZE 4 + +// +// Disable IRQs +// +#define DisableIRQs() asm volatile ("cli") + +// +// Enable IRQs +// +#define EnableIRQs() asm volatile ("sti") + +// +// Pause CPU until next interuption +// !!! Enables IRQs !!! +// +#define PauseCPU() asm volatile("sti\n\thlt") + +// +// Halt the CPU indefinitely +// +#define HaltCPU() do { asm volatile ("hlt"); } while (1) + +//------------------------------------------// +// Some base functions // +//------------------------------------------// + +noreturn void StartPanic(const char *); +noreturn void CrashSystem(void); + +//------------------------------------------// +// Useful I/O inlines // +//------------------------------------------// + +static inline +void WriteByteOnPort(port_t port, port_t val) +{ + asm volatile ("out %0, %1" : : "dN" (port), "a" (val)); +} + +static inline +uchar ReadByteFromPort(port_t port) +{ + KalAssert(FALSE && ENOSYS); + (void)port; + return 0; +} + +static inline +ushort ReadWordFromPort(port_t port) +{ + KalAssert(FALSE && ENOSYS); + (void)port; + return 0; +} + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/kernel/sched.h b/kaleid/include/kernel/sched.h new file mode 100644 index 0000000..596e08f --- /dev/null +++ b/kaleid/include/kernel/sched.h @@ -0,0 +1,107 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Scheduler header // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_BASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_SCHED_H +#define _KALKERN_SCHED_H + +//------------------------------------------// +// Preprocessor // +//------------------------------------------// + +// +// Debug stuff +// +#define printdbg printf + +// +// States for a process +// +#define STATE_RUNNING 0 +#define STATE_RUNNABLE 1 +#define STATE_BLOCKED 2 + +// +// Time in ticks a process should be run +// +#define DEF_PROC_TSLICE 5 // 20 ticks +#define TCR_PROC_TSLICE 20000 // 20000 ticks (time critical) + +//------------------------------------------// +// List heads // +//------------------------------------------// + +DECLARE_PER_CPU(IdlePrioProcs, ListHead_t *); +DECLARE_PER_CPU(ReglPrioProcs, ListHead_t *); +DECLARE_PER_CPU(ServPrioProcs, ListHead_t *); +DECLARE_PER_CPU(TimeCritProcs, ListHead_t *); + +extern const char *PrioClassesNames[]; + +//------------------------------------------// +// Data types // +//------------------------------------------// + +// +// A process +// +typedef struct sProcess_t { + + // Identifier + int pid; + + // Current priority class + int prioClass; + + // Default priority class (without boosts) + int defPrioClass; + + // Current priority level + int prioLevel; + + // Default priority level + int defPrioLevel; + + // Current state + int procState; + + // Remaining time running + ulong timeSlice; + + // Default time-slice + ulong defTimeSlice; + + // Scheduler internals + ListNode_t *schedNode; + +} Process_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +void SchedInit(void); +void SchedFini(void); + +void SchedThisProc(Process_t *); +void SchedOnTick(void); + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/kernel/terminal.h b/kaleid/include/kernel/terminal.h new file mode 100644 index 0000000..1cde180 --- /dev/null +++ b/kaleid/include/kernel/terminal.h @@ -0,0 +1,91 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Terminal functions // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_BASE_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_TERMINAL_H +#define _KALKERN_TERMINAL_H + +//------------------------------------------// +// Types // +//------------------------------------------// + +// +// The VGA colors +// +typedef enum { + KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, + KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, + KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, + KTERM_COLOR_BROWN, KTERM_COLOR_LGREY, + KTERM_COLOR_DARK_GREY, KTERM_COLOR_LBLUE, + KTERM_COLOR_LGREEN, KTERM_COLOR_LCYAN, + KTERM_COLOR_LRED, KTERM_COLOR_LMAGENTA, + KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE +} TermColor_t; + +// +// Terminal structure, right now VGA and output only +// +typedef struct sTerminal_t { + + uint initDone; + Lock_t lock; + + const char *name; + const char *type; + + void *data; + + size_t width; + size_t height; + off_t currentX; + off_t currentY; + + uint tabSize; + TermColor_t fgColor; + TermColor_t bgColor; + + error_t (*ClearTermUnlocked)(Terminal_t *); + error_t (*PutOnTermUnlocked)(Terminal_t *, char); + error_t (*PrintOnTermUnlocked)(Terminal_t *, const char *); + +} Terminal_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +void InitTerms(void); +error_t ClearTerm(Terminal_t *); +error_t PutOnTerm(Terminal_t *, char); +error_t PrintOnTerm(Terminal_t *, const char *); +error_t ChTermColor(Terminal_t *, TermColor_t, TermColor_t); + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef _NO_DEBUG +# define DebugLog(...) PrintOnTerm(GetStdDbg(), __VA_ARGS__) +#else +# define DebugLog(...) +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif From 3ccfcbef549756c510b8109d88032486d74a068b Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 2 Feb 2019 19:08:15 +0100 Subject: [PATCH 06/14] Set theme jekyll-theme-slate --- _config.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 _config.yml diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..c741881 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-slate \ No newline at end of file From ba4370d9123674360e6b75fc26b580298f3dd3b0 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 2 Feb 2019 19:08:38 +0100 Subject: [PATCH 07/14] Create CNAME --- CNAME | 1 + 1 file changed, 1 insertion(+) create mode 100644 CNAME diff --git a/CNAME b/CNAME new file mode 100644 index 0000000..2821c3b --- /dev/null +++ b/CNAME @@ -0,0 +1 @@ +www.os-k.eu \ No newline at end of file From 74d4ca5749ef07a55e742ad1f7d6f236e96b3c81 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Wed, 6 Feb 2019 14:07:38 +0100 Subject: [PATCH 08/14] Lots of different stuff --- Makefile.in | 27 +- build/bin/comm-test | Bin 0 -> 19576 bytes build/obj/kaleid/test/.placeholder | 0 build/preproc.h | 4 +- kaleid/crtlib/atoi.c | 2 - kaleid/crtlib/itoa.c | 18 +- kaleid/crtlib/memory.c | 68 ++--- kaleid/crtlib/sprintf.c | 4 - kaleid/crtlib/string.c | 33 +-- kaleid/extras/argv.c | 199 ++++++++++++++- kaleid/extras/prog.c | 2 +- kaleid/include/base/assert.h | 12 +- kaleid/include/base/bdefs.h | 16 -- kaleid/include/base/crtlib.h | 28 +-- kaleid/include/base/errno.h | 10 +- kaleid/include/base/limits.h | 113 +++------ kaleid/include/base/masks.h | 17 +- kaleid/include/base/types.h | 12 - kaleid/include/extras/argv.h | 294 +++++++++++++++++----- kaleid/include/extras/list.h | 25 +- kaleid/include/extras/locks.h | 25 +- kaleid/include/extras/malloc.h | 33 +++ kaleid/include/extras/prog.h | 10 - kaleid/include/kalbase.h | 10 +- kaleid/include/kaleid.h | 3 +- kaleid/include/kalext.h | 4 - kaleid/include/kalkern.h | 4 - kaleid/include/kernel/base.h | 127 ++++++---- kaleid/include/kernel/sched.h | 54 ---- kaleid/include/kernel/terminal.h | 10 - kaleid/kernel/init/table.c | 2 + kaleid/kernel/io/cursor.c | 0 kaleid/kernel/io/term.c | 105 ++++++++ kaleid/kernel/{ke/terminal.c => io/vga.c} | 121 ++------- kaleid/kernel/proc/Makefile | 4 +- kaleid/kernel/proc/sched.c | 62 ++--- 36 files changed, 806 insertions(+), 652 deletions(-) create mode 100755 build/bin/comm-test create mode 100644 build/obj/kaleid/test/.placeholder create mode 100644 kaleid/include/extras/malloc.h create mode 100644 kaleid/kernel/io/cursor.c create mode 100644 kaleid/kernel/io/term.c rename kaleid/kernel/{ke/terminal.c => io/vga.c} (66%) diff --git a/Makefile.in b/Makefile.in index eadc905..8fa91e9 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,3 +1,4 @@ +// -*- Mode: Makefile -*- //----------------------------------------------------------------------------// // GNU GPL OS/K // // // @@ -13,23 +14,23 @@ CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" CC2NAME=gcc COPTIM=-O2 -CWARNS=-Wall -Wextra -Wshadow // -Wpedantic -CINCLUDES=-isystem./kaleid/include +CWARNS=-Wall -Wextra // -Wshadow -Wpedantic +CINCLUDES=-Ikaleid/include -CFLAGS1=-std=gnu11 -nostdlib -ffreestanding -mcmodel=large -CFLAGS2=-m64 -masm=intel -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -CFLAGS=$(CFLAGS1) $(CFLAGS2) $(SFLAG) +CFLAGS1=-nostdlib -ffreestanding -mcmodel=large // -std=gnu11 +CFLAGS2=_ASMTYPE -mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CFLAGS=$(CFLAGS1) $(CFLAGS2) CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) -BINDIR=./build/bin -OBJDIR=./build/obj +BINDIR=build/bin +OBJDIR=build/obj BOOTDIR=boot COMMDIR=kaleid/crtlib KERNDIR=kaleid/kernel SYSTDIR=kaleid/system -LINXDIR=$(COMMDIR)/test +LINXDIR=kaleid/test //----------------------------------------------------------------------------# // TESTING MAKEFILE @@ -49,7 +50,7 @@ LOBJDIR=$(OBJDIR)/$(LINXDIR) COMMOBJS=COBJ6(string, status, rand, memory, arith, strtol) COBJ4(itoa, ltoa, utoa, ultoa) COBJ4(atoi, atol, atou, atoul) COBJ2(../extras/prog, ../extras/argv) TCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) -KCC=$(CC) -T ./build/kernel.ld -D_OSK_SOURCE -D_KALEID_KERNEL +KCC=$(CC) -D_KALEID_KERNEL comm-convert: COMPILE_CONVRT1(itoa) -D_NEED_ITOA @@ -73,20 +74,22 @@ common: comm-convert tests: common $(TCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o - $(TCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf + $(TCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/comm-test //----------------------------------------------------------------------------# // KERNEL MAKEFILE KOBJDIR=$(OBJDIR)/$(KERNDIR) -KERNOBJS=KOBJ4(init/init, init/table, ke/panic, ke/terminal) +KERNOBJS=KOBJ6(init/init, init/table, ke/panic, io/term, io/cursor, io/vga) kernel: common COMPILE_KERNEL(init/init) COMPILE_KERNEL(init/table) COMPILE_KERNEL(ke/panic) - COMPILE_KERNEL(ke/terminal) + COMPILE_KERNEL(io/cursor) + COMPILE_KERNEL(io/term) + COMPILE_KERNEL(io/vga) LINK_KERNEL(kaleid-kernel.elf) //----------------------------------------------------------------------------# diff --git a/build/bin/comm-test b/build/bin/comm-test new file mode 100755 index 0000000000000000000000000000000000000000..ea0cd8f3fffe1bc41dfe9cc46c27bbf04fbc8c1d GIT binary patch literal 19576 zcmeHPeRNaDm4A}IWN;)03^5-mN`SYFarF4jr}l_V)Zt_dky_PuTM}RcQ534muZ3@@|(-cxAOX?^GgA5zo5K&X}P~~*Xq*c{`w++V{@>h zsKdUxXmx3ETTAf@W;f|4-c8lpSxv%^R^+J7dHACkpz>er-#M#y%U23b`;UF))jys3 z{P*X+&_ev#_#+);SNanCz%7JPs`~RU)gRZie_hL6`?~{>^;%dWHM6F`=T3t=r@;fj zt@zWYPXn;(LFB(Q4ZaHatyA!S6xb}uEcNh!D&#yfL2s1iNpgvmAtK%+^hX`Jf3@WC zv<6ysH+!3Wxb(KQ`2vck!Q1GUS_6&EN`us<1nTw#nuC56*L8S34UNrSf8&1QCMBLW z#S4ihZ)3CMan)}2)cXRy-HmOEFHpO=!r#*DtM%^k6Zh_>mS)cFkvvuHo~@j(!cWz< zY2s4~Z*sdfRCrbtuP7!5AQ{9b0(r^--DaipcEDNiPk4zc`yBHz$0^lp36-y%sbktwL6M2?z`Z;Oq5 z47H-ICAQc@eGT2VGcNV`qq6!ZIsEo`O|3hcap!E3xuFlE9HM?|#gG+7;&dq>vGhJN|@RV5$Fp61GOG-aB+USDY4AlRhf?Q(eCZo1kIeM*jeVBP1D z8~NJRczi^L36Jz=^Z+nixVAmF+=fPn*Gs{fqmP19Yd7l820s}yN-E<+kBpY1)N(k> za(J%&NN{1~L@;yA8i^?jp~94B)qlBmrPl%OH_Qv#{w zN&Y4Mq}`sReF14-OxiQMNpF6#-Xwkh&3)FOYcFeLOtD!G-`!mvi7N{n_6cR_q`t+| z>9fTWe!al`M$ICNA9tzaWRDB=JE+4hx(dfN=Pz_1JaWcvX8q)}A00RM%)O2paMyX6B$#Iz+6Lu|^O zaoJKiz};zyJPiz)xs~WoYo+~&5|Gt%V>5U=29!EE+>8DwgAamdNy?@6%4#pUdxMBS zl3hr$J4lxNJ)qo^Ea!rbO3Psbb>Im|Q(35t0#==YcuwkHOJ^GfoU9tPpwq^Oqu&6Z z^%~sZ#w>Np3^SsuzRbwF=ZRoqe(4LaWaQ~9@pw0QzNhiDYdp^go*xrW6L?bM2TqmM zE0gi6jq7U6m`4ne!LF)!@JvcPV+2z_+zxkS+6Kue%h+OQL_QrI9aH8yyV6Z$q+E6^ zIGTtDj`TZCajAVYIk)%ivEL@we;O{&RH~t>+BP^glaCK%OS{r7n&5G&Eck9D)}L;X zlx4DdG%=sB`Cx4(;!9@p1#!Ti&~&^%OPie#9h3E8KG9sD%whdbc8Bgn_waf@%pZs{ zO@)%nS&%chFU`C2=bTPjG!1)=D5{fchq61?8=$p zE;|xD#YQ*`HI8Krc;;lm%7MyiCod@Ra6HBljmf=~iRp zOi^FxZt|b97PCXD|K!5+aVAiuaaRcpR5Nx0z`Zz^W>y*w8Cw8M&ieK_pnzw3-?1jH^2OC#CUE3Z z4!mlwD?CD@&m^n&POwJ2H~MkXuU>YkL!&-irmj!QFJh|gq{kv$!O-tc8tOpksh2Un z*P{KO|os_QBlI)XaVHA83FPJpjLExnieiX!;}da{ojDB?w~dtFwCum+QR zhf7?&Z&}^hN9F7xm<$_+8oE(Wy4HRNwLg9Ddt=|G7TaPpoW9!){hKHz(fQCHP2beV zO{2M!{H#t4WPE#(Rk{s^(0Xr>&sej?eZ2H2^%Tuc=m(6i^R|nlbFpmV<=XuA@$!pU z-*o-}N-*i;-gPibX$=1*OR1N`SB#b?SUg1c|J^p|Qa^AVAKMbGU0|ekIxIgZH8nWY zQW{-GciHja8P}yZM~q*876FlEPfo8!y3O!YZdq}^q~$@`@?c{ z|0PX-?^qhf`n53z}?F)~jV#_pYQcM;D>;OTTp+`q5f)KBlg6jI%tmqbmKg9in+b_`IN%+bPr+UodKHeGY+7S=_u@aui>d!0!ld(S&?f>ILqia4O zU$FK>@4Fhla_ipw@Prhs=ua!@uP}h;hb8-tLg+f`JPTh?YL6P=8F?- zrFrR`GrBd=Z6_=n&~1*c8uXi^pK7}_df+mP+y2aw{*BZ%CfS`tqnKA77Dv>*#DM!;-DTX-)5aQ^o#l};LDdnuY*50HH!P8VgRCR`ssTE2ONP$6SD`i= zYTaty5gTqa{K-lAp6k;S7vnJPi873v$QvYK>HHT|&gywF2by?Qa_2>PApK4Nl44ZN z4?W^|IN`sww=2%Iw<_h1E+e&{>J&DNeIE0CrTQ~R_=3S<{h6Xvs=sB64H|#s#xtW$ zs9GEMumgVY1FO^ehNZI;b(Pykf(Nl^+hVAUKA<@{6P8+oU-$Q6_HYRp=Hfd7VT`0tzKM|sXQ$n)-RO*FRu$I*dJbw z?c>Jg-Nkj%vNmtKuT=?HZ&_!p-tKlw8y)U#mDCAHo|d-9us0k~S{~EqJprj`g{^c= zseR?@QoB@CwqnJ~m1|a%tXyqhRk~))Dtn1tDrjErE#ws!DZVy^DZx)_TZ)U9`#O|> zw{5vMu)7^?l89PwyUn^xT+}tyyBnK*LjE4Fzd}Q5TO5JiJA8GO9nw;N(7Lp32Jsjc zq}@gDR@mqni^u7`M+s;RXgMhDFCPM>_gTYm_a5NZGx2x`v=#JO&~DH}pfS*+pyQy! zpw{;w2O0v+#b)dfXaT78EFO(OdqD38?H-TETR~%>U7+KjJ)kw19$zFJbO=;BkIg~Q z5U7-n{DpWtAG8Lv1oRN73_1k56Lc7~12pzNHWom;{~C`U1nmKh5e|A1bO>~u=*4*4 zl!3i>+`z4%<)C)Z8qgZhR?t15AO&>GMl&<@ZL2HZi= zZqQ*+3DZddjYuSV??lP4f2(BZ$Tcj;$ue~tvT_Muh`&Rl@i@JE5ov0EiM>Z6{-F{0 znwZVG8_oGumYI7^A!+^myOyn7ax1|k@5J9p@bM-P%eH68NWdxEpvm#>`p5ji=1C^H2Qs!1FOi78CvegBkt_;3dHI?ffEe892S; z6N&V^0G!62SAd{|zY2T=IDgf|N(rZNo{KT~Nrq@Yguf5G19*oH&%xLo2EIjyF9Dv9 zv8!*-THrf@->l=Ocfosrvk8ZnQN4c7e?rF}0bYPP;O9F0tH6hWn|1j2fu97vNQb}1 z`Sta_&H2yk`2Pl+=9+anJP&iu2=HPZeg~hEa&)*8_yqWG)Zq_vev1xQIDfVde-ijO z^xUAszX_b$lZ;c$zYKgB_;KQ|H0OqmM$Zhh$vyLa{K_-$#qTD1pGt}N-vxg+=F+za za5DY?c&cVTz&KO*slOItj-_wM@^zSf>If2Uk zA5#A0hm^nY56Y=sukmtGCm=RH-Ue<$yY%?qfJ?xW{YrZBFqcojK0SU1@NwXaNik1L zK%Kxzznw7H{e;nB&JU-#%+}rJeE8V`FOdKJ;Io47b;KR@*OSEeM4H2F4X39S8D^OC zA-utCa-q|x9bW}M-J@uV7m57%ec&B`j>nq`WPYwTmm424=T?|aPC&mx`9a9}sCFH> zhnjmmUH8Cs4_x=abq`$kz;zE?_rP@zT=&2~(*rTSC=Mkrj?o}I+Oj05cvhqR8A@VJ zpEqC2&(lcaJIe(;FP;@RnDz$*PG{)q3`^-Z{qcg$+hL7YK+ zFYEq@28i!u#doorDk|=@7C3#o8okZd)x|4|ONwmvkJkHkl?82u8em;fVp~(P%C=fx zr#_#CXtj$DO)5;2jEV2+lVB+g|0z@Qrzhesl|Lg9cd2-0B0f{`EJ^fZDsGZQ|EJ>F zl8CERd`2SvQt_FR7>B8Nj+CqG2P#j)Xr+BreM-Z3<26&_7D^coE&!>vZH0dTue0QBJ zlqO+x^6{UF&yxzK#OF&jQ{rNFo76(%I9)o27jLP_G7U~{G+>s}+jTo|Bl?B*v6B<+ z*%DqNCL&(AmgD;Qdn<5*G}(SJ&v+Srt)IKNo|sPm7a9Mx{q3!EgS5#+OW^|NjdA=rsCQ zOoNvJH(*?k^L`Qc&FSKHE90Lf)g*7;p-*-){MvX%(Ukq-2fu`QK&%HSM~Plk8>Sf7 zPjG&54ol;fBzg%?CtdXO5AE?ArWo%>fYW)ILwo^omMa?KcrN#YxIGMWyc@>^D2e<0 zY2a3dr2Z_%Lc@x4H~RS`6AG!`VLl+l`sGHB57A2wB!PcK;C!HncM{7u-pv;*Vo$jg zcrNPI&sTSGez72WT&qUf$nhTD;bPrF?=PvHF>dd1&i^3C4{=~bW ze!KQQ($4rb-sIoUay`R0YN&XZ@C?U8yk3e+N zL>0%M5jcZqJbHO81~5=N$GHOWj`jxN z)E@nKoX7ch^7e~&fwyt|5brPcZUc4P#c{DtYv%QqbKI)y-z^-^<+vD+UXF)!m*<30SrYB8_()4;9#I+=Wl^AGFv^l)6_eiQb7hvVHmewuIxFEk8{Qoq7wSJrl_u^31>O+{S3bGX;Xp?4Y>4psw%d6+^%i49*;D6 z2GpZ8dFtpCD2{h}>RUX!{Vlt^eowv95@_>ygB=odwfcREufBM7*_stoGtrsTMvpfT z@b2^Ynw7vlsUd*#s-F5_Q`0_(B;`EdR8m+IA0T-=HCrodYaj4z++JN#>)KN7@f5Ep zDWy86Dp<7=M_;{4unooqyv_9>&G>pqYVtL;1ok244KylyK-p(Yeq1yvEndQvmPQad zRZBVTOQR|4Shb3d-)`LM*j(wUtaie2nis`&iY@h$$N50DW3#IQTuCMD{4F3HjrLT^ zT$}9NDtR`!w`_2@JzF+z+*VoZsda2{SE7c=FRk!p6+gpU*A`?x)=K^LjqU&VEHBeR zPSFJ5lKs9wOG0;@SJAG6@QCE`)q52X5(@emLYHupUjxZ+30b8DCynvh)h3@(0}ocy zxn%7ma(Q|3F<_<+6*slC<8U!4WUXc`qwmO|JNY#86vC73M-SrvL=&1>`}L=rr_g}v z>zY~vL*r#x4_XX%O}P4$h< zo?x4A(${T1WpAR=)?IZdA<9Yg1oS_$0~#zIJlB?mIWI z`vQUH$xdzFpU@6xF}ppx+S-y$1rB8^rqG73=GY1GNo#kIFDCn$vW+e99}kVj#8oiS zQ2ID<%5&pfhK{K7{%=lcYg;RI4349VQO3C#&aasOonN2w06NplVwY^^$KhGE=v;Ch zX0f2+(>@X0#!knJmh7i!N@1yJ2{yz1N$2i4FKGsze9C_6s6*fKa6ogK6EUdu`YuT- zZrj(Scz1zfC@{K5T;WW=FVHF#H@7IhV#fwok>cIWbGw^^#k+!y_(vKV>!o7K?eVtl zk&5g0HAA09F-%wqg);S5at8BBalq&Ik^sMI^(#^_i?(817VmCBR-sw6n5J|vwXnIN z*tdsI;(O|$MC3G4Z8p~=#P!GOz=mqPO^tPELyJP{fzU)#jHr}~=^ll9g}871FDdQx z81RQ>KTBetQBeK%7V#!0y3f{BKFq>E#U09?Xx7-i##jQx`38eqWv*`A*k5@rsXx+F671YB(3+bG)c&d z{d_^i{ykV&();gGptM&m>KFT}g7W2*B-$_d1bqzhwD&IZv>v4-sFmfk_Y2AG=diRN z7bGI&V_aTPVZW$fZ-0c#Z{P~W`vgHtI8O59Grjy%z^GkfJ5cQV3M$@HfQ2RAJdu@D zTqVnk{b4~x{X&2*6Y=b#C2jw2vVO6TEU0L&kk{wGhJxh!#dD0HNBBBi$cuVK{u?@Z zu`ewse;g3(tgjLoT>Lwi7yW1D`#XZ3NoK~izW(p&?<5U#|B3h(^@@IY7P86pi+%l}8#I*m!z%D6+9T#~8egLA8Ylm~Tch6+6CZ~~y@K~fTudeZEvgJD KM<<}mO8*1Z5frNc literal 0 HcmV?d00001 diff --git a/build/obj/kaleid/test/.placeholder b/build/obj/kaleid/test/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build/preproc.h b/build/preproc.h index f690c6e..746bd2c 100644 --- a/build/preproc.h +++ b/build/preproc.h @@ -9,11 +9,13 @@ #ifdef _TO_ASM # define _CSPREF -S # define _OUTFIX S +# define _ASMTYPE -masm=intel # define LINK_KERNEL(out) #else # define _CSPREF -c # define _OUTFIX o -# define LINK_KERNEL(out) $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/out +# define _ASMTYPE +# define LINK_KERNEL(out) $(KCC) -T ./build/kernel.ld $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/out #endif #define COMPILE_CONVRT1(file) $(CCC) _CSPREF $(COMMDIR)/itoa.c -o $(COBJDIR)/file._OUTFIX diff --git a/kaleid/crtlib/atoi.c b/kaleid/crtlib/atoi.c index eaf42b6..7066c55 100644 --- a/kaleid/crtlib/atoi.c +++ b/kaleid/crtlib/atoi.c @@ -9,10 +9,8 @@ #include -// // String to integer // Do not change errno -// #define _ATOI_IMPL(_Name, _Type, _Func) \ _Type _Name(const char *str) { \ __get_errno(old); \ diff --git a/kaleid/crtlib/itoa.c b/kaleid/crtlib/itoa.c index b393a57..67572e1 100644 --- a/kaleid/crtlib/itoa.c +++ b/kaleid/crtlib/itoa.c @@ -9,9 +9,7 @@ #include -// // Digits table for bases <=36 (unused) -// #if 0 static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; @@ -20,7 +18,6 @@ static const char digits[] = // // Integer to string in any base between 2 and 36 (included) // - #if defined(_NEED_ITOA) #define _IL_MIN INT_MIN @@ -55,14 +52,11 @@ char *ultoa(ulong i, char *str, int base) char *orig = str; #if defined(_NEED_ITOA) || defined(_NEED_LTOA) - // // Deal with negatives - // int neg = 0; if (i < 0 && base == 10) { - // + // Handle INT_MIN and LONG_MIN... - // if (__builtin_expect(i == _IL_MIN, 0)) { strcpy(orig, _IL_MIN_STRING); goto leave; @@ -75,27 +69,21 @@ char *ultoa(ulong i, char *str, int base) } #endif - // // Only handle base 2 -> 36 - // if (base < 2 || base > 36) { __set_errno(EINVAL); *orig = '\0'; goto leave; } - // // Deal with zero separately - // if (i == 0) { *str++ = '0'; *str = '\0'; goto leave; } - // // Compute digits... in reverse order - // while (i > 0) { rem = i % base; *str++ = (rem > 9) @@ -110,14 +98,10 @@ char *ultoa(ulong i, char *str, int base) *str = '\0'; - // // Reverse the string - // orig = strrev2(orig); - // // End of conversion - // leave: return orig; } diff --git a/kaleid/crtlib/memory.c b/kaleid/crtlib/memory.c index 925ae9f..ee31348 100644 --- a/kaleid/crtlib/memory.c +++ b/kaleid/crtlib/memory.c @@ -20,17 +20,13 @@ void *memset(void *ptr, int val, size_t bytes) { uchar *uptr = (uchar *)ptr; - // // Deal with bytes before start of the first aligned qword - // - while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { + while (((ulong)uptr % alignof(QWORD)) > 0 && bytes--) { *uptr++ = (uchar)val; } - // // At this point we're qword-aligned - // - if (bytes > QWORD_SIZE) { + if (bytes > sizeof(QWORD)) { const ulong uval = ((ulong)val << 56) | ((ulong)val << 48) | ((ulong)val << 40) | ((ulong)val << 32) | ((ulong)val << 24) | ((ulong)val << 16) @@ -38,20 +34,16 @@ void *memset(void *ptr, int val, size_t bytes) ulong *uqptr = (ulong *)ptr; - // // Moving fast, qword by qword - // - while (bytes > QWORD_SIZE) { + while (bytes > sizeof(QWORD)) { + bytes -= sizeof(QWORD); *uqptr++ = uval; - bytes -= QWORD_SIZE; } uptr = (uchar *)(ulong)uqptr; } - // // Deal with the few remaining bytes - // while (bytes--) *uptr++ = (uchar)val; return ptr; @@ -64,18 +56,14 @@ void *memsetw(void *ptr, int val, size_t words) { ushort *uptr = (ushort *)ptr; - // - // Check whether we can we do this an aligned way - // - if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { - // + // Check whether we can we do this a word-aligned way + if unlikely (((ulong)uptr % alignof(WORD)) > 0) { // We can't, so we write word by word all the way up - // while (words--) *uptr++ = (ushort)val; return uptr; } - while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) { + while (((ulong)uptr % alignof(QWORD)) > 0 && words--) { *uptr++ = (ushort)val; } @@ -98,28 +86,22 @@ void *memsetw(void *ptr, int val, size_t words) return ptr; } -// // Set "dwords"-many dwords starting from ptr to val // XXX unimplemented -// void *memsetd(void *ptr, int val, size_t dwords) { (void)val; (void)dwords; - + __set_errno(ENOSYS); return ptr; } -// // Set "qwords"-many qwords starting from ptr to val -// void *memsetq(void *ptr, long val, size_t qwords) { ulong *uptr = (ulong *)ptr; - // // There's no need to check for alignment - // while (qwords--) *uptr++ = (ulong)val; return ptr; @@ -148,17 +130,13 @@ void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) if unlikely (bytes == 0) return dst; - // // Can align both src and dst at once at once? - // - if unlikely ((ulong)src % WORD_ALIGN == 1 - && (ulong)dst % WORD_ALIGN == 1) { + if unlikely ((ulong)src % alignof(WORD) == 1 + && (ulong)dst % alignof(WORD) == 1) { const uchar *ubsrc = (const uchar *)usrc; uchar *ubdst = (uchar *)udst; - // - // Yes we can, we're guaranteed to be word-aligned now - // + // Yes we can, we're guaranteed to be word-aligned after that *ubdst++ = *ubsrc++; bytes--; @@ -169,34 +147,28 @@ void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) const ushort *uwsrc = (const ushort *)usrc; ushort *uwdst = (ushort *)udst; - // // Align either dst or src for qword access - // - while ((ulong)dst % QWORD_ALIGN > 0 - && (ulong)src % QWORD_ALIGN > 0 - && bytes > WORD_SIZE) { + while ((ulong)dst % alignof(QWORD) > 0 + && (ulong)src % alignof(QWORD) > 0 + && bytes > sizeof(WORD)) { + bytes -= sizeof(WORD); *uwdst++ = *uwsrc++; - bytes -= WORD_SIZE; } udst = (ulong *)uwdst; usrc = (ulong *)uwsrc; - // - // This should be most of the job - // - while (bytes > QWORD_SIZE) { + // Copy fast + while (bytes > sizeof(QWORD)) { + bytes -= sizeof(QWORD); *udst++ = *usrc++; - bytes -= QWORD_SIZE; } const uchar *ubsrc = (const uchar *)usrc; ushort *ubdst = (ushort *)udst; - // // Deal with the few bytes left - // while (bytes--) *ubdst ++ = *ubsrc++; return dst; @@ -210,16 +182,12 @@ void *memmove(void *dst, const void *src, size_t bytes) const uchar *usrc = src; uchar *udst = dst; - // // Can we use memcpy() safely? - // if (udst < usrc) { return memcpy(dst, src, bytes); } - // // No, so we go backwards - // uchar *usrc_end = (uchar *)usrc + bytes - 1; uchar *udst_end = udst + bytes - 1; while (bytes--) *udst_end-- = *usrc_end--; diff --git a/kaleid/crtlib/sprintf.c b/kaleid/crtlib/sprintf.c index f0f6b15..c3ad3f1 100644 --- a/kaleid/crtlib/sprintf.c +++ b/kaleid/crtlib/sprintf.c @@ -52,14 +52,10 @@ int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) { int ret = 0; - // // Go throught the format string - // while (*fmt) { if (*fmt != '%') { - // // Even if we don't have any more room we still increase ret - // if (ret++ < n) { *str++ = *fmt++; } diff --git a/kaleid/crtlib/string.c b/kaleid/crtlib/string.c index 895d031..d9d6776 100644 --- a/kaleid/crtlib/string.c +++ b/kaleid/crtlib/string.c @@ -128,9 +128,7 @@ char *strstr(const char *haystack, const char *needle) { const size_t needle_size = strlen(needle); - // // Moves haystack to first occurence of the needle's first byte - // while ((haystack = strchr(haystack, *needle)) != NULL) { if (strncmp(haystack, needle, needle_size) == 0) { return (char *)haystack; @@ -150,15 +148,11 @@ char *strtok_r(char *restrict str, const char *restrict delim, char **restrict s if (str == NULL) str = *saveptr; - // // Skip initial segments composed only of delimiters - // str += strspn(str, delim); - // // If str is empty, store it in saveptr so that next call // still finds an empty strings and returns NULL - // if (*str == 0) { *saveptr = str; return NULL; @@ -191,6 +185,7 @@ char *strtok(char *restrict str, const char *restrict delim) { static char *saveptr = NULL; + // Avoid this function if possible KalAssert(FALSE); if (str) saveptr = str; @@ -232,19 +227,22 @@ char *strncpy(char *restrict dest, const char *restrict src, size_t n) // Always null-terminates dest, but doesn't fill // dest's contents past the null-terminator // -// Returns the number of bytes written +// Returns the number of bytes NOT written, not counting null-terminators // size_t strnzcpy(char *restrict dest, const char *restrict src, size_t n) { - size_t it; + size_t it, loss; for (it = 0; it < n - 1 && src[it]; it++) { dest[it] = src[it]; } - dest[it++] = 0; + dest[it] = 0; - return it; + // Compute how many bytes were not copied + for (loss = it; src[loss]; loss++); + + return loss - it; } // @@ -277,13 +275,15 @@ char *strncat(char *restrict dest, const char *restrict src, size_t n) } // -// Appends a copy of at most n bytes of src at the end of dest -// Always null-terminates, and returne TRUE or FALSE depending on whether -// regular strcat() would have null-terminated this string, or not +// Appends at most n-1 bytes from src to dest +// Always null-terminates dest, but doesn't fill +// dest's contents past the null-terminator +// +// Returns the number of bytes NOT written, not counting null-terminators // size_t strnzcat(char *restrict dest, const char *restrict src, size_t n) { - size_t it, off = 0; + size_t it, loss, off = 0; while (dest[off]) off++; @@ -293,7 +293,10 @@ size_t strnzcat(char *restrict dest, const char *restrict src, size_t n) dest[it+off] = 0; - return it+1; + // Compute how many bytes were not copied + for (loss = it; src[loss+off]; loss++); + + return loss - it; } // diff --git a/kaleid/extras/argv.c b/kaleid/extras/argv.c index e4e2a49..d9920da 100644 --- a/kaleid/extras/argv.c +++ b/kaleid/extras/argv.c @@ -10,14 +10,193 @@ #include #include #include -#if 0 -error_t KalCmdLineToArgV(const char *cmdLine, - int argcMax, - int *argcPtr, - const char **argv); +#include -error_t KalArgVToCmdLine(const char *cmdLine, - size_t lengthMax, - int argc, - const char **argv); -#endif +// +// Computes argument count, the least N such +// that argv[N] == NULL +// +int KalComputeArgCount(const char **argv) +{ + int argc = 0; + + while (argv[argc]) argc++; + + return argc; +} + +// +// Computes the total size of argv, including +// the null-terminators +// +size_t KalComputeArgVecSize(const char *argv[]) +{ + size_t len; + + for (len = 0; *argv; len += strlen(*argv) + 1, argv++); + + return len; +} + +// +// Converts command line to an argument vector +// +// This function assumes that argv[0] is a pointer +// to a ARG_MAX-wide buffer, which will be filled +// with strings in succession; the address of the nth +// string will be stored in argv[n-1] +// +// Technically ARG_MAX is the maximum number of bytes +// in both the buffer *and* argv, i.e. (argc + 1) * sizeof(char *) +// bytes are reserved for the argv[i] pointers, so in fact less than +// ARG_MAX bytes are available +// +// That available space, however, remains strictly higher than 4KB, +// which is the POSIX minimum; in case of doubt, safely use 4KB +// +// TODO long escape sequences +// get program command line if cmdLine == NULL +// +error_t KalCmdLineToArgVecEx(const char *cmdLine, + int *argcPtr, + char **argv, + bool doEscaping) +{ + int argc; + char quotes = 0; + bool started = false; + bool escaping = false; + size_t written = 0; + error_t retcode = EOK; + + KalAlwaysAssert(argv && *argv && cmdLine); + + // An ARG_MAX-wide buffer + char *buffer = *argv; + + // Null-terminate current argv slot + // and save the start of next string + // Macro'd to avoid copypasting code +#define NULLTERM_AND_SAVE \ + *buffer = 0; \ + argv[++argc] = buffer++ + 1; \ + written += sizeof(char *) + 1; \ + + // Is character a blank character? + // To be replaced by ctype stuff once + // that's implemented +#define ISBLANK(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') + + // Both " and ' are valid quotes chars + // XXX aren't there more? +#define ISQUOTE(c) ((c) == '\'' || (c) == '"') + + + // Go through the command line + for (argc = 0; *cmdLine; cmdLine++) { + + // Make sure we don't go beyond ARG_MAX bytes + if (written >= ARG_MAX - (1 + sizeof(char *))) { + + // All we have left is one byte for the null-terminator of the current slot + // and sizeof(char *) bytes for the NULL at the end of argv + *buffer = 0; + + // Did we write anything in this slot? + if (started) { + argc++; + } + + // We're done, get out of here + retcode = ENOMEM; + break; + } + + // Switch to next argv slot + if (ISBLANK(*cmdLine) && !quotes && !escaping) { + + // Has slot even started? + if (started) { + started = false; + NULLTERM_AND_SAVE; + } + + continue; + } + + // Escaping next character + if (*cmdLine == '\\' && !escaping && doEscaping) { + escaping = true; + continue; + } + + // Deal with escape sequences + if (escaping) { + if (*cmdLine == 'n') *buffer++ = '\n'; + else if (*cmdLine == 'r') *buffer++ = '\r'; + else if (*cmdLine == 't') *buffer++ = '\t'; + else if (*cmdLine == 'f') *buffer++ = '\f'; + else if (*cmdLine == 'v') *buffer++ = '\v'; + + written++; + started = true; + escaping = false; + continue; + } + + // Deal with quotes + if (ISQUOTE(*cmdLine) && !escaping) { + + // Quoted text always fills a whole slot + // Note that this is the only way an empty + // string can be put in a slot + if (!quotes && !started) { + quotes = *cmdLine; + started = true; + + continue; + } + + // End a quote block + if (quotes == *cmdLine && ISBLANK(cmdLine[1])) { + quotes = 0; + started = false; + + NULLTERM_AND_SAVE; + continue; + } + + // Quotes were either preceeded by unquoted non-blank text + // or couldn't close quoted text block because succeeded + // by text; we consider this " to be escaped and fall through + } + + // Just a regular character, or it is being escaped + written++; + started = true; + escaping = false; + *buffer++ = *cmdLine; + } + + // Ensures that argv[argc] == NULL + argv[++argc] = NULL; + + // Update *argcPtr, but we don't mind if + // NULL was passed for argcPtr + if (argcPtr) { + *argcPtr = argc; + } + + return retcode; +} + +#undef ISQUOTE +#undef ISBLANK +#undef NULLTERM_AND_SAVE + +error_t KalCmdLineToArgVec(const char *cmdLine, + int *argcPtr, + char **argv) +{ + return KalCmdLineToArgVecEx(cmdLine, argcPtr, argv, false); +} diff --git a/kaleid/extras/prog.c b/kaleid/extras/prog.c index e3591d0..b0ce13d 100644 --- a/kaleid/extras/prog.c +++ b/kaleid/extras/prog.c @@ -13,7 +13,7 @@ #ifdef _KALEID_KERNEL const char *__progname = "kaleid-kernel"; -const char *__progvers = "alpha-0.0.1"; +const char *__progvers = "pre-pre-alpha-0.0.1"; #else diff --git a/kaleid/include/base/assert.h b/kaleid/include/base/assert.h index e5d5608..65d00da 100644 --- a/kaleid/include/base/assert.h +++ b/kaleid/include/base/assert.h @@ -10,8 +10,6 @@ #ifndef _KALBASE_ASSERT_H #define _KALBASE_ASSERT_H -//------------------------------------------// -// Macros // //------------------------------------------// #ifndef noreturn @@ -42,14 +40,10 @@ static_assert(sizeof(void *) == 8, _SA_MSG); // Assert core // //------------------------------------------// -// // Failed assert handler -// noreturn void __assert_handler(const char *, const char *, int, const char *); -// // Unconditional assert -// #define KalAlwaysAssert(x) \ do { \ if unlikely (!(x)) \ @@ -69,12 +63,10 @@ noreturn void __assert_handler(const char *, const char *, int, const char *); #ifndef _OSK_SOURCE -// // When not building for OS/K, use the system's assert -// #include -#undef KalAwaysAssert +#undef KalAlwaysAssert #define KalAlwaysAssert assert #endif @@ -110,8 +102,6 @@ noreturn void __assert_handler(const char *, const char *, int, const char *); #define KalAssertEx(x,m) KalAssert(x && m) #define KalAlwaysAssertEx(x,m) KalAlwaysAssert(x && m) -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/base/bdefs.h b/kaleid/include/base/bdefs.h index 1c1ef13..9b3c4d4 100644 --- a/kaleid/include/base/bdefs.h +++ b/kaleid/include/base/bdefs.h @@ -10,8 +10,6 @@ #ifndef _KALBASE_BDEFS_H #define _KALDEFS_BDEFS_H -//------------------------------------------// -// Actual constants // //------------------------------------------// #ifndef TRUE @@ -30,8 +28,6 @@ #define INITOK ((unsigned int)0xCAFEBABE) #endif -//------------------------------------------// -// Keywords // //------------------------------------------// #ifndef __alignof_is_defined @@ -57,8 +53,6 @@ # endif #endif -//------------------------------------------// -// Attributes and macros // //------------------------------------------// #ifndef _PACKED @@ -85,16 +79,6 @@ #define _XSTR(x) _STR(x) #endif -//------------------------------------------// -// API specific macros // -//------------------------------------------// - -#ifndef KALAPI -# define KALAPI -#endif - -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/base/crtlib.h b/kaleid/include/base/crtlib.h index e4e7767..47b269d 100644 --- a/kaleid/include/base/crtlib.h +++ b/kaleid/include/base/crtlib.h @@ -10,8 +10,6 @@ #ifndef _KALBASE_CRTLIB_H #define _KALBASE_CRTLIB_H -//------------------------------------------// -// Typedefs // //------------------------------------------// #ifndef __error_t_defined @@ -39,8 +37,6 @@ typedef struct { int quot, rem; } div_t; typedef struct { long quot, rem; } ldiv_t; #endif -//------------------------------------------// -// Global variables // //------------------------------------------// #ifndef _KALEID_KERNEL @@ -62,16 +58,12 @@ extern error_t __errno; #endif -//------------------------------------------// -// Macros // //------------------------------------------// #ifndef _NO_MASK #define _NO_MASK #endif -//------------------------------------------// -// va_list utilities // //------------------------------------------// #ifndef va_start @@ -90,8 +82,6 @@ extern error_t __errno; #define va_end __builtin_va_end #endif -//------------------------------------------// -// Memory management utilities // //------------------------------------------// #ifndef memsetb @@ -107,10 +97,12 @@ void *memsetw(void *, int, size_t); void *memsetd(void *, int, size_t); void *memsetq(void *, long, size_t); +/* void *memchr(const void *, int, size_t); void *memchrw(const void *, int, size_t); void *memchrd(const void *, int, size_t); void *memchrq(const void *, long, size_t); +*/ void *memrchr(const void *, int, size_t); @@ -120,8 +112,6 @@ void *memmove(void *, const void *, size_t); void *memzero(void *, size_t); int memcmp(const void *, const void *, size_t); -//------------------------------------------// -// String manipulation utilities // //------------------------------------------// size_t strlen(const char *); @@ -157,8 +147,6 @@ int snprintf(char *, size_t, const char *, ...); int vsprintf(char *, const char *, va_list); int vsnprintf(char *, size_t, const char *, va_list); -//------------------------------------------// -// Type conversion utilities // //------------------------------------------// char *itoa(int, char *, int); @@ -174,26 +162,16 @@ unsigned long atoul(const char *); long strtol (const char *restrict, char **restrict, int); unsigned long strtoul(const char *restrict, char **restrict, int); -//------------------------------------------// -// RNG utilities // //------------------------------------------// int rand(void); void srand(unsigned int); -//------------------------------------------// -// Time utilities // -//------------------------------------------// - -//------------------------------------------// -// Diverse utilities // //------------------------------------------// char *strerror(int); char *strsignal(int); -//------------------------------------------// -// Arithmetical macros // //------------------------------------------// #ifndef __abs @@ -234,8 +212,6 @@ static inline ldiv_t ldiv(long __x, long __y) } #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/base/errno.h b/kaleid/include/base/errno.h index c4476e3..f77c8f8 100644 --- a/kaleid/include/base/errno.h +++ b/kaleid/include/base/errno.h @@ -10,8 +10,6 @@ #ifndef _KALBASE_ERRNO_H #define _KALBASE_ERRNO_H -//------------------------------------------// -// "errno" values // //------------------------------------------// // Everything went fine @@ -44,6 +42,12 @@ // Bad file number #define EBADF 9 +// Try again +#define EAGAIN 11 + +// Out of memory +#define ENOMEM 12 + // Invalid argument #define EINVAL 22 @@ -56,8 +60,6 @@ // System is panicking #define EPANIC 600 -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/base/limits.h b/kaleid/include/base/limits.h index affe0fa..56a03ee 100644 --- a/kaleid/include/base/limits.h +++ b/kaleid/include/base/limits.h @@ -10,95 +10,58 @@ #ifndef _KALBASE_LIMITS_H #define _KALBASE_LIMITS_H -//------------------------------------------// -// Data sizes blocks // //------------------------------------------// -#ifndef DATA_SIZE_BLOCK -#define DATA_SIZE_BLOCK -# define BYTE_SIZE sizeof(char) -# define WORD_SIZE sizeof(short) -# define DWORD_SIZE sizeof(int) -# define QWORD_SIZE sizeof(long) -#endif +#define BYTE unsigned char +#define WORD unsigned short +#define DWORD unsigned int +#define QWORD unsigned long -#ifndef DATA_ALIGN_BLOCK -#define DATA_ALIGN_BLOCK -# define BYTE_ALIGN alignof(char) -# define WORD_ALIGN alignof(short) -# define DWORD_ALIGN alignof(int) -# define QWORD_ALIGN alignof(long) -#endif +#define CHAR_BIT 8 +#define BITS_IN(T) (sizeof(T) * CHAR_BIT) -#ifndef DATA_BITS_BLOCK -#define DATA_BITS_BLOCK -# define BYTE_BIT 8 -# define CHAR_BIT (BYTE_SIZE * BYTE_BIT) -# define WORD_BIT (WORD_SIZE * BYTE_BIT) -# define DWORD_BIT (DWORD_SIZE * BYTE_BIT) -# define QWORD_BIT (QWORD_SIZE * BYTE_BIT) -# define SHORT_BIT WORD_BIT -# define INT_BIT DWORD_BIT -# define LONG_BIT QWORD_BIT -#endif - -#ifndef DATA_SHIFTS_BLOCK -#define DATA_SHIFTS_BLOCK -# define BYTES_TO_WORDS(B) ((B) >> 1) -# define BYTES_TO_DWORDS(B) ((B) >> 2) -# define BYTES_TO_QWORDS(B) ((B) >> 3) -# define WORDS_TO_BYTES(W) ((W) << 1) -# define WORDS_TO_DWORDS(W) ((W) >> 1) -# define WORDS_TO_QWORDS(W) ((W) >> 2) -# define DWORDS_TO_BYTES(D) ((D) << 2) -# define DWORDS_TO_WORDS(D) ((D) << 1) -# define DWORDS_TO_QWORDS(D) ((D) >> 1) -# define QWORDS_TO_BYTES(Q) ((Q) << 3) -# define QWORDS_TO_WORDS(Q) ((Q) << 2) -# define QWORDS_TO_DWORDS(Q) ((Q) << 1) -#endif +/* XXX find a generic way */ +#define BYTES_TO_WORDS(B) ((B) >> 1) +#define BYTES_TO_DWORDS(B) ((B) >> 2) +#define BYTES_TO_QWORDS(B) ((B) >> 3) +#define WORDS_TO_BYTES(W) ((W) << 1) +#define WORDS_TO_DWORDS(W) ((W) >> 1) +#define WORDS_TO_QWORDS(W) ((W) >> 2) +#define DWORDS_TO_BYTES(D) ((D) << 2) +#define DWORDS_TO_WORDS(D) ((D) << 1) +#define DWORDS_TO_QWORDS(D) ((D) >> 1) +#define QWORDS_TO_BYTES(Q) ((Q) << 3) +#define QWORDS_TO_WORDS(Q) ((Q) << 2) +#define QWORDS_TO_DWORDS(Q) ((Q) << 1) -//------------------------------------------// -// Numeric data limits // //------------------------------------------// -#ifndef DATA_MAX_LIMITS_BLOCK -#define DATA_MAX_LIMITS_BLOCK -# define SCHAR_MAX ((signed char) 0x7F) -# define SHRT_MAX ((short) 0x7FFF) -# define INT_MAX ((int) 0x7FFFFFFF) -# define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFF) -# define UCHAR_MAX ((unsigned char) 0xFF -# define USHRT_MAX ((unsigned short) 0xFFFF) -# define UINT_MAX ((unsigned int) 0xFFFFFFFF) -# define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFF) -#endif +/* U/L suffixes on hex numbers look odd */ +#define SCHAR_MAX ((signed char) 0x7F) +#define SHRT_MAX ((short) 0x7FFF) +#define INT_MAX ((int) 0x7FFFFFFF) +#define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFFL) +#define UCHAR_MAX ((unsigned char) 0xFFU) +#define USHRT_MAX ((unsigned short) 0xFFFFU) +#define UINT_MAX ((unsigned int) 0xFFFFFFFFU) +#define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFFUL) -#ifndef DATA_MIN_LIMITS_BLOCK -#define DATA_MIN_LIMITS_BLOCK -# define SCHAR_MIN ((signed char) -SCHAR_MAX - 1) -# define SHRT_MIN ((short) -SHRT_MAX - 1) -# define INT_MIN ((int) -INT_MAX - 1) -# define LONG_MIN ((long) -LONG_MAX - 1L) -#endif +#define SCHAR_MIN ((signed char) -SCHAR_MAX - 1) +#define SHRT_MIN ((short) -SHRT_MAX - 1) +#define INT_MIN ((int) -INT_MAX - 1) +#define LONG_MIN ((long) -LONG_MAX - 1L) -#ifndef DATA_CHAR_LIMITS_BLOCK -#define DATA_CHAR_LIMITS_BLOCK -# ifdef __CHAR_UNSIGNED__ +#ifdef __CHAR_UNSIGNED__ # define CHAR_MIN ((char)0) # define CHAR_MAX ((char)UCHAR_MAX) -# else +#else # define CHAR_MIN ((char)SCHAR_MIN) # define CHAR_MAX ((char)SCHAR_MAX) -# endif #endif -#ifndef DATA_SPTYPES_LIMITS_BLOCK -#define DATA_SPTYPES_LIMITS_BLOCK -# define SSIZE_T_MIN LONG_MIN -# define SSIZE_T_MAX LONG_MAX -# define SIZE_T_MAX ULONG_MAX -#endif +#define SSIZE_T_MIN LONG_MIN +#define SSIZE_T_MAX LONG_MAX +#define SIZE_T_MAX ULONG_MAX #ifdef NEED_MORE_USELESS_DATA # define UCHAR_MIN ((unsigned char)0) @@ -110,8 +73,6 @@ # endif #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/base/masks.h b/kaleid/include/base/masks.h index 60e861e..868df80 100644 --- a/kaleid/include/base/masks.h +++ b/kaleid/include/base/masks.h @@ -38,26 +38,40 @@ //------------------------------------------// +#undef strspn +#undef strcspn #define strlen _osk_strlen #define strspn _osk_strspn #define strcspn _osk_strcspn +#undef strcmp +#undef strncmp #define strcmp _osk_strcmp #define strncmp _osk_strncmp +#undef strchr +#undef strrchr #define strchr _osk_strchr #define strrchr _osk_strrchr +#undef strstr +#undef strpbrk #define strstr _osk_strstr #define strpbrk _osk_strpbrk +#undef strtok +#undef strtok_r #define strtok _osk_strtok #define strtok_r _osk_strtok_r +#undef strcpy +#undef strncpy #define strcpy _osk_strcpy #define strncpy _osk_strncpy #define strnzcpy _osk_strnzcpy +#undef strcat +#undef strncat #define strcat _osk_strcat #define strncat _osk_strncat #define strnzcat _osk_strnzcat @@ -72,7 +86,6 @@ //------------------------------------------// - #define itoa _osk_itoa #define ltoa _osk_ltoa #define utoa _osk_utoa @@ -112,8 +125,6 @@ #define strerror _osk_strerror -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/base/types.h b/kaleid/include/base/types.h index 115319a..7e40d34 100644 --- a/kaleid/include/base/types.h +++ b/kaleid/include/base/types.h @@ -10,8 +10,6 @@ #ifndef _KALBASE_TYPES_H #define _KALBASE_TYPES_H -//------------------------------------------// -// Basic integer types aliases // //------------------------------------------// #ifndef __base_types_aliases @@ -26,8 +24,6 @@ typedef unsigned long long ullong; typedef long double ldouble; #endif -//------------------------------------------// -// Miscellaneous types // //------------------------------------------// #ifndef __size_t_defined @@ -50,8 +46,6 @@ typedef signed int wchar_t; typedef unsigned long off_t; #endif -//------------------------------------------// -// Standard fixed-width integer types // //------------------------------------------// #ifndef __ptrdiff_t_defined @@ -79,8 +73,6 @@ typedef signed long intmax_t; typedef unsigned long uintmax_t; #endif -//------------------------------------------// -// Special types // //------------------------------------------// #ifndef __va_list_defined @@ -99,8 +91,6 @@ typedef struct { long quot, rem; } ldiv_t; #endif -//------------------------------------------// -// Kaleid-specific types // //------------------------------------------// #ifndef __error_t_defined @@ -113,8 +103,6 @@ typedef int error_t; typedef ushort port_t; #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/extras/argv.h b/kaleid/include/extras/argv.h index 352c532..2a1a22e 100644 --- a/kaleid/include/extras/argv.h +++ b/kaleid/include/extras/argv.h @@ -11,116 +11,272 @@ #include #endif -//------------------------------------------// -// Start of header // -//------------------------------------------// +#ifdef _KALEID_KERNEL +#define FILE FILE +typedef void* FILE; +#else +#include +#endif #ifndef _KALEXTRAS_ARGV_H #define _KALEXTRAS_ARGV_H //------------------------------------------// -// Types // + +// +// Maximal total size of argv, including the pointers, +// the pointed-to strings and their null-terminators +// This is a 32-bit integer +// +#define ARG_MAX (1 << 16) // 16 KB + //------------------------------------------// // -// Option types +// Flags for options // typedef enum { - // - // A flag option, without any more parameters - // - CMDOPT_FLAG, - // - // An option that expects a parameter - // - CMDOPT_PARAM, + // The option parameter is optional + KALOPT_OPTIONAL = (1 << 0), -} CmdOptType_t; + // Do not show option in any help message + KALOPT_HIDDEN = (1 << 1), + + // This option is an alias for the previous one + KALOPT_ALIAS = (1 << 2), + + // This isn't an option but a docstring + KALOPT_DOCSTR = (1 << 3), + + // Only show in long help messages + KALOPT_LONGDOC = (1 << 4), + +} CmdOptionFlag_t; + +// +// Flags for KalParse(CmdLine|ArgV) +// +typedef enum { + + // Don't exit on errors= + KALOPT_NO_EXIT = (1 << 0), + + // Don't react to --help + KALOPT_NO_HELP = (1 << 1), + + // Don't react to --version + KALOPT_NO_VERSION = (1 << 2), + + // Alias -h for --help and -V for --version + KALOPT_ALIASES = (1 << 3), + + // Don't print any error message + // Implies KALOPT_NO_EXIT + KALOPT_NO_ERRORS = (1 << 4) | KALOPT_NO_EXIT, + + // Use KalGetProgName() instead of argv[0] + // as program name + KALOPT_USE_ARGV0 = (1 << 5), + + // Call argument parser for non-options + // Non-options arguments will be indicated by + // a key of 0, the argument passed as a parameter + KALOPT_PARSE_ALL = (1 << 6), + + // Call argument parser with options and non-options in the + // order they were found, instead of parsing options first + KALOPT_IN_ORDER = (1 << 7) | KALOPT_PARSE_ALL, + + // Stay silent all along + KALOPT_SILENT = KALOPT_NO_EXIT | KALOPT_NO_ERRORS + | KALOPT_NO_HELP | KALOPT_NO_VERSION, + +} CmdParserFlags_t; + +// +// Return values for the command parser +// These flags can be combined and are applied in order +// +// "Continue" and "Break" actions do not prevent +// later actions from applying +// +typedef enum { + + // Continue parsing new options + KALOPT_CONTINUE = 0, + + // Stop parsing further options + KALOPT_BREAK = (1 << 0), + + // Show help/version message (by default, continue parsing) + KALOPT_SHOWHELP = (1 << 1), + KALOPT_SHOWVERS = (1 << 2), + +} CmdParserReturn_t; + +//------------------------------------------// // // An option for a command, e.g. "-o file" in "cc -o file" // typedef struct { - // + // The option's name, e.g. "help" for "--help" // May be 0, but only if letter is not zero - // const char *longName; - // // The option's letter, e.g. 'h' for '-h' - // int letter; - // + // Address of the variable to put the parameter into + const char *param; + + // Option flags, see above + CmdOptionFlag_t flags; + + // The option's help text + // If this is 0, this option is hidden + const char *helpText; + // The option's group, for sorting during --help // Must be positive and < 256, or option won't shop up // during help texts - // int group; - // - // The option's type, see above - // - CmdOptType_t type; - - // - // Address of the variable to put the parameter into - // Should be an int point for flag arguments, string - // pointer for parameter arguments - // - void *param; - - // - // The option's help text - // If this is 0, this option is hidden - // - const char *helpText; - } CmdOption_t; -//------------------------------------------// -// Functions // -//------------------------------------------// +// +// Program help/documentation strings; any can be 0 +// +// Help messages are printed in this format: +// (letting "this" be a CmdDocStrings_t* variable) +// +// Usage: (this->usage) +// (this->header) +// +// (this->groups[0]) +// -o, --option-name option description +// ... +// ... +// +// (this->bottom) +// +// XXX progname/version +// +typedef struct { -int KalComputeArgC(const char *argv[]); + const char *usage; + const char *header; + const char *bottom; -size_t KalComputeArgVSize(const char *argv[]); + // Groups documentation + // groups[n] should be either 0 or contain the + // description of the option group n + const char **groups; -error_t KalCmdLineToArgV(const char *cmdLine, - int *argcPtr, - const char *argv[]); - -error_t KalArgVToCmdLine(const char *cmdLine, - size_t lengthMax, - int argc, - const char *argv[]); - -error_t KalParseCmdLine(const char *cmdLine, - CmdOption_t *options); - -error_t KalParseArgV(int argc, - const char *argv[], - CmdOption_t *options); +} CmdDocStrings_t; // -// The "Ex" variants reacts to "--help" and "--version" by themselves +// The state variable passed to the parser containing useful infos // +typedef struct { -error_t KalParseCmdLineEx(const char *cmdLine, - CmdOption_t *options, - const char *progDesc, - const char *groupDescs[]); + // Option we're currently parsing + const CmdOption_t *option; -error_t KalParseArgVEx(int argc, - const char *argv[], - CmdOption_t *options, - const char *progDesc, - const char *groupDescs[]); + // Index (in argv) of the option we're parsing + int argvIndex; + + // Flags passed to KalParse(CmdLine|ArgV) + CmdParserFlags_t flags; + + // Has help/version messages been displayed already? + bool shownHelp; + bool shownVersion; + + // Standard output streams + FILE *outStream; + FILE *errStream; + + // Private, internal data; do not touch + void *priv; + +} CmdParserState_t; + +// +// The argument parser function +// +typedef CmdParserReturn_t (*CmdParser_t)(int key, + const char *param, + CmdParserState_t *state); //------------------------------------------// -// End of header // + +// +// Misc. simple functions +// +int KalComputeArgCount(const char **argv); +size_t KalComputeArgVecSize(const char **argv); + +// +// Command line to argument vector utility +// +error_t KalCmdLineToArgVecEx(const char *cmdLine, + int *argcPtr, + char **argv, + bool doEscaping); + +// +// KalCmdLineToArgVecEx but doEscaping = false +// +error_t KalCmdLineToArgVec(const char *cmdLine, + int *argcPtr, + char **argv); +/* + +// +// Argument vector to command line utility +// +error_t KalArgVecToCmdLineEx(char *cmdLine, + size_t lengthMax, + int argc, + const char **argv, + bool doUnEscaping); + +// +// KalArgVecToCmdLineEx but doUnEscapign = false +// +error_t KalArgVecToCmdLine(char *cmdLine, + size_t lengthMax, + int argc, + const char **argv); +// +// Command line parser; only takes an argument vector +// The argv argument *will* be modified and all parsed +// options and arguments will be removed, except argv[0] +// which is guanranteed to be left +// +error_t KalParseArgVecEx(int argc, + char **argv, + const CmdOption_t *options, + const CmdDocStrings_t *docStrings, + CmdParserFlags_t *flags, + CmdParser_t *parser, + FILE *outStream, + FILE *errStream); +// +// KalParseArgVecEx(argc, argv, options, docString, stdin, stdout, parser, NULL) +// +error_t KalParseArgVec(int argc, + char **argv, + const CmdOption_t *options, + const CmdDocStrings_t *docStrings, + CmdParserFlags_t *flags, + CmdParser_t *parser); + +*/ + //------------------------------------------// #endif diff --git a/kaleid/include/extras/list.h b/kaleid/include/extras/list.h index d9f5bd6..d394ddf 100644 --- a/kaleid/include/extras/list.h +++ b/kaleid/include/extras/list.h @@ -15,26 +15,13 @@ #include #endif -//------------------------------------------// -// Start of header // -//------------------------------------------// +#ifndef _KALEXTRAS_MALLOC_H +#include +#endif #ifndef _KALEXTRAS_LIST_H #define _KALEXTRAS_LIST_H -// -// XXX ¯\_(ツ)_/¯ -// -#ifndef _STDLIB_H -void *malloc(long); -void free(void *); -#endif - -#define AllocMemory malloc -#define FreeMemory free - -//------------------------------------------// -// Data structures // //------------------------------------------// typedef struct sListHead_t { @@ -51,8 +38,6 @@ typedef struct sListNode_t { struct sListNode_t *next; } ListNode_t; -//------------------------------------------// -// Functions // //------------------------------------------// // @@ -74,7 +59,7 @@ static inline ListHead_t } // -// Create a liste head +// Create a list head // static inline ListHead_t *CreateListHead(void) @@ -265,8 +250,6 @@ DestroyListHead(ListHead_t *head) // #define GetNodeData(node, type) ((type)(node)->data) -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/extras/locks.h b/kaleid/include/extras/locks.h index 4ca5220..e2333e3 100644 --- a/kaleid/include/extras/locks.h +++ b/kaleid/include/extras/locks.h @@ -17,48 +17,35 @@ #endif #endif -//------------------------------------------// -// Start of header // -//------------------------------------------// - #ifndef _KALEXTRAS_LOCKS_H #define _KALEXTRAS_LOCKS_H -//------------------------------------------// -// Types // //------------------------------------------// typedef enum eLockType_t { - // + // Mutex-type lock // // WARNING // AquireLock() panics when used on a mutex while not running a process - // KLOCK_MUTEX, - // // Spinlock-type lock - // KLOCK_SPINLOCK, } LockType_t; -// // "volatile" may not be actually needed -// typedef struct sLock_t { unsigned int initDone; // initialized? - int locked; // is locked? LockType_t type; // lock type? + volatile int locked; // is locked? #ifdef _KALEID_KERNEL Thread_t *ownerThread; // unused Thread_t *waitingThread; // unused #endif -} volatile Lock_t; +} Lock_t; -//------------------------------------------// -// Functions // //------------------------------------------// // @@ -124,9 +111,9 @@ void AquireLock(Lock_t *lock) #else if likely (lock->type == KLOCK_SPINLOCK) continue; #ifdef _OSK_SOURCE - else KalYieldCPU(); + else (void)KalYieldCPU(); #else - else sched_yield(); + else (void)sched_yield(); #endif #endif } @@ -163,8 +150,6 @@ bool AttemptLock(Lock_t *lock) return retval; } -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/extras/malloc.h b/kaleid/include/extras/malloc.h new file mode 100644 index 0000000..65728cd --- /dev/null +++ b/kaleid/include/extras/malloc.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Memory allocation utilities // +//----------------------------------------------------------------------------// + +#ifndef _KALBASE_H +#include +#endif + +#ifndef _KALEXTRAS_MALLOC_H +#define _KALEXTRAS_MALLOC_H + +//------------------------------------------// + +// +// ¯\_(ツ)_/¯ +// +#ifndef _STDLIB_H +void *malloc(unsigned long); +void free(void *); +#endif + +#define AllocMemory malloc +#define FreeMemory free + +//------------------------------------------// + +#endif + diff --git a/kaleid/include/extras/prog.h b/kaleid/include/extras/prog.h index bae5b1a..57a008b 100644 --- a/kaleid/include/extras/prog.h +++ b/kaleid/include/extras/prog.h @@ -11,22 +11,14 @@ #include #endif -//------------------------------------------// -// Start of header // -//------------------------------------------// - #ifndef _KALEXTRAS_PROG_H #define _KALEXTRAS_PROG_H -//------------------------------------------// -// Constants // //------------------------------------------// extern const char *__progname; extern const char *__progvers; -//------------------------------------------// -// Functions // //------------------------------------------// const char *KalGetProgName(void); @@ -35,8 +27,6 @@ const char *KalGetProgVersion(void); bool KalSetProgVers(const char *); bool KalSetProgName(const char *); -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/kalbase.h b/kaleid/include/kalbase.h index 618061a..8743f05 100644 --- a/kaleid/include/kalbase.h +++ b/kaleid/include/kalbase.h @@ -14,10 +14,10 @@ // Building for OS/K // //------------------------------------------// -#if !defined(_OSK_SOURCE) -# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) -# define _OSK_SOURCE 1 -# endif +#ifndef _OSK_SOURCE +#if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) +#define _OSK_SOURCE 1 +#endif #endif #if !defined(_OSK_SOURCE) && !defined(_KALEID_UNMASKED) @@ -54,8 +54,6 @@ #include #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/kaleid.h b/kaleid/include/kaleid.h index 239b222..36401a3 100644 --- a/kaleid/include/kaleid.h +++ b/kaleid/include/kaleid.h @@ -27,8 +27,7 @@ #include #endif #endif -//------------------------------------------// -// End of header // + //------------------------------------------// #endif diff --git a/kaleid/include/kalext.h b/kaleid/include/kalext.h index 5dcc19f..a4c5b4f 100644 --- a/kaleid/include/kalext.h +++ b/kaleid/include/kalext.h @@ -11,8 +11,6 @@ #include #endif -//------------------------------------------// -// Start of header // //------------------------------------------// #ifndef _KALEXT_H @@ -40,8 +38,6 @@ #endif #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/kalkern.h b/kaleid/include/kalkern.h index 82c84c6..2020436 100644 --- a/kaleid/include/kalkern.h +++ b/kaleid/include/kalkern.h @@ -15,8 +15,6 @@ #include #endif -//------------------------------------------// -// Start of header // //------------------------------------------// #ifndef _KALKERN_H @@ -41,8 +39,6 @@ #endif #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/kernel/base.h b/kaleid/include/kernel/base.h index 6ac5134..dbf03bd 100644 --- a/kaleid/include/kernel/base.h +++ b/kaleid/include/kernel/base.h @@ -11,18 +11,14 @@ #include #endif -//------------------------------------------// -// Start of header // //------------------------------------------// #ifndef _KALKERN_BASE_H #define _KALKERN_BASE_H -//------------------------------------------// -// Elementary types // //------------------------------------------// -typedef struct sLock_t volatile Lock_t; +typedef struct sLock_t Lock_t; typedef struct sThread_t Thread_t; typedef struct sProcess_t Process_t; typedef struct sTerminal_t Terminal_t; @@ -30,23 +26,23 @@ typedef struct sListHead_t ListHead_t; typedef struct sListNode_t ListNode_t; //------------------------------------------// -// Values for __kstate // -//------------------------------------------// + +/* XXX */ // // Current state of the kernel // typedef enum { - // the kernel is booting + // The kernel is booting KSTATE_INIT, - // the kernel is not running a process + // The kernel is not running a process KSTATE_KERNEL, - // a process is running in kernel mode + // A process is running in kernel mode KSTATE_PROCESS, - // the kernel is panicking + // The kernel is panicking KSTATE_PANIC, } KernelState_t; @@ -88,48 +84,50 @@ typedef enum { _Tp __ ## _X [NCPUS] = { (_Tp) 0 } -//------------------------------------------// -// Global constants // //------------------------------------------// // XXX -DECLARE_PER_CPU(PanicStr, const char *); +DECLARE_PER_CPU(PanicStr, const char *); -DECLARE_PER_CPU(KernState, KernelState_t); +DECLARE_PER_CPU(KernState, KernelState_t); -DECLARE_PER_CPU(_StdOut, Terminal_t *); -DECLARE_PER_CPU(_StdDbg, Terminal_t *); +DECLARE_PER_CPU(_StdOut, Terminal_t *); +DECLARE_PER_CPU(_StdDbg, Terminal_t *); -DECLARE_PER_CPU(CurProc, Process_t *); -DECLARE_PER_CPU(CurThread, Thread_t *); +DECLARE_PER_CPU(CurProc, Process_t *); +DECLARE_PER_CPU(CurThread, Thread_t *); + +DECLARE_PER_CPU(ReSchedFlag, bool); +DECLARE_PER_CPU(PreemptCount, ulong); -//------------------------------------------// -// Macros for manipulating said // -// global constants // //------------------------------------------// -#define SetKernState(x) \ - do { \ - _SetKernState(x); \ - } while (0) +#define SetKernState(x) _SetKernState(x); -#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : NULL) -#define SetStdOut(tm) \ - do { \ - if (GetCurProc() == NULL) \ - _Set_StdOut(tm); \ - } while (0); - -#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : NULL) -#define SetStdDbg(tm) \ - do { \ - if (GetCurProc() == NULL) \ - _Set_StdDbg(tm); \ - } while (0) +// +// StdOut/StdDbg manipulation +// +#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : GetCurProc()->stdOut) +#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : GetCurProc()->stdDbg) +#define SetStdOut(tm) do { if (GetCurProc() == NULL) _Set_StdOut(tm); \ + else GetCurProc()->stdOut = (tm); } while (0) +#define SetStdDbg(tm) do { if (GetCurProc() == NULL) _Set_StdDbg(tm); \ + else GetCurProc()->stdDbg = (tm); } while (0) +// +// Re-scheduling and preemption +// XXX XXX XXX atomic operations +// +#define SetReSchedFlag(x) _SetReSchedFlag(x) +#define DisablePreemption() _SetPreemptCount(GetPreemptCount()+1) +#define EnablePreemption() do { KalAssert(GetPreemptCount() > 0); \ + _SetPreemptCount(GetPreemptCount()-1); } while(0) //------------------------------------------// -// Other Macros // -//------------------------------------------// + +// +// Value of the preemption count indicating that preemption is activated +// +#define PREEMPT_ON 0 // // Size of a tabulation in spaces @@ -159,7 +157,44 @@ DECLARE_PER_CPU(CurThread, Thread_t *); #define HaltCPU() do { asm volatile ("hlt"); } while (1) //------------------------------------------// -// Some base functions // + +// +// A process +// +typedef struct sProcess_t { + + // Identifier + int pid; + + // Current priority class + int prioClass; + + // Default priority class (without boosts) + int defPrioClass; + + // Current priority level + int prioLevel; + + // Default priority level + int defPrioLevel; + + // Current state + int procState; + + // Remaining time running + ulong timeSlice; + + // Default time-slice + ulong defTimeSlice; + + // Scheduler internals + ListNode_t *schedNode; + + // Standard output/debug + Terminal_t *stdOut, *stdDbg; + +} Process_t; + //------------------------------------------// noreturn void StartPanic(const char *); @@ -169,10 +204,13 @@ noreturn void CrashSystem(void); // Useful I/O inlines // //------------------------------------------// + static inline void WriteByteOnPort(port_t port, port_t val) { - asm volatile ("out %0, %1" : : "dN" (port), "a" (val)); + KalAssert(FALSE && ENOSYS); + (void)port; + (void)val; } static inline @@ -191,9 +229,6 @@ ushort ReadWordFromPort(port_t port) return 0; } -//------------------------------------------// -// End of header // //------------------------------------------// #endif - diff --git a/kaleid/include/kernel/sched.h b/kaleid/include/kernel/sched.h index 596e08f..5dc1023 100644 --- a/kaleid/include/kernel/sched.h +++ b/kaleid/include/kernel/sched.h @@ -11,37 +11,25 @@ #include #endif -//------------------------------------------// -// Start of header // //------------------------------------------// #ifndef _KALKERN_SCHED_H #define _KALKERN_SCHED_H -//------------------------------------------// -// Preprocessor // //------------------------------------------// -// // Debug stuff -// #define printdbg printf -// // States for a process -// #define STATE_RUNNING 0 #define STATE_RUNNABLE 1 #define STATE_BLOCKED 2 -// // Time in ticks a process should be run -// #define DEF_PROC_TSLICE 5 // 20 ticks #define TCR_PROC_TSLICE 20000 // 20000 ticks (time critical) -//------------------------------------------// -// List heads // //------------------------------------------// DECLARE_PER_CPU(IdlePrioProcs, ListHead_t *); @@ -51,46 +39,6 @@ DECLARE_PER_CPU(TimeCritProcs, ListHead_t *); extern const char *PrioClassesNames[]; -//------------------------------------------// -// Data types // -//------------------------------------------// - -// -// A process -// -typedef struct sProcess_t { - - // Identifier - int pid; - - // Current priority class - int prioClass; - - // Default priority class (without boosts) - int defPrioClass; - - // Current priority level - int prioLevel; - - // Default priority level - int defPrioLevel; - - // Current state - int procState; - - // Remaining time running - ulong timeSlice; - - // Default time-slice - ulong defTimeSlice; - - // Scheduler internals - ListNode_t *schedNode; - -} Process_t; - -//------------------------------------------// -// Functions // //------------------------------------------// void SchedInit(void); @@ -99,8 +47,6 @@ void SchedFini(void); void SchedThisProc(Process_t *); void SchedOnTick(void); -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/include/kernel/terminal.h b/kaleid/include/kernel/terminal.h index 1cde180..0cb08f1 100644 --- a/kaleid/include/kernel/terminal.h +++ b/kaleid/include/kernel/terminal.h @@ -11,15 +11,11 @@ #include #endif -//------------------------------------------// -// Start of header // //------------------------------------------// #ifndef _KALKERN_TERMINAL_H #define _KALKERN_TERMINAL_H -//------------------------------------------// -// Types // //------------------------------------------// // @@ -64,8 +60,6 @@ typedef struct sTerminal_t { } Terminal_t; -//------------------------------------------// -// Functions // //------------------------------------------// void InitTerms(void); @@ -74,8 +68,6 @@ error_t PutOnTerm(Terminal_t *, char); error_t PrintOnTerm(Terminal_t *, const char *); error_t ChTermColor(Terminal_t *, TermColor_t, TermColor_t); -//------------------------------------------// -// Macros // //------------------------------------------// #ifndef _NO_DEBUG @@ -84,8 +76,6 @@ error_t ChTermColor(Terminal_t *, TermColor_t, TermColor_t); # define DebugLog(...) #endif -//------------------------------------------// -// End of header // //------------------------------------------// #endif diff --git a/kaleid/kernel/init/table.c b/kaleid/kernel/init/table.c index 4b98d25..e610a42 100644 --- a/kaleid/kernel/init/table.c +++ b/kaleid/kernel/init/table.c @@ -19,3 +19,5 @@ CREATE_PER_CPU(_StdDbg, Terminal_t *); CREATE_PER_CPU(CurProc, Process_t *); CREATE_PER_CPU(CurThread, Thread_t *); +CREATE_PER_CPU(ReSchedFlag, bool); + diff --git a/kaleid/kernel/io/cursor.c b/kaleid/kernel/io/cursor.c new file mode 100644 index 0000000..e69de29 diff --git a/kaleid/kernel/io/term.c b/kaleid/kernel/io/term.c new file mode 100644 index 0000000..98d3827 --- /dev/null +++ b/kaleid/kernel/io/term.c @@ -0,0 +1,105 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Early terminal functions // +//----------------------------------------------------------------------------// + +#include + +extern void VGA_Init(void); +extern Terminal_t VGA_Terminal; + +// +// Initialize standard output +// +void InitTerms(void) +{ + KalAssert(!GetStdOut() && !GetStdDbg()); + + VGA_Init(); + + // vgaTerm.initDone = INITOK; + + SetStdDbg(&VGA_Terminal); + SetStdOut(&VGA_Terminal); + + ClearTerm(GetStdOut()); +} + +// +// Fill terminal with spaces +// +error_t ClearTerm(Terminal_t *term) +{ + error_t retcode; + + if (term == NULL) return EINVAL; + KalAssert(term->initDone == INITOK); + + AquireLock(&term->lock); + retcode = term->ClearTermUnlocked(term); + ReleaseLock(&term->lock); + + return retcode; +} + +// +// Change the color code +// +error_t ChTermColor(Terminal_t *term, TermColor_t fgColor, TermColor_t bgColor) +{ + if (fgColor > KTERM_COLOR_WHITE || bgColor > KTERM_COLOR_WHITE) + return EINVAL; + + if (term == NULL) + return EINVAL; + + AquireLock(&term->lock); + + term->fgColor = fgColor; + term->bgColor = bgColor; + + ReleaseLock(&term->lock); + + return EOK; +} + +// +// Write a single character on the terminal +// +error_t PutOnTerm(Terminal_t *term, char ch) +{ + error_t retcode; + + if (term == NULL) return EINVAL; + KalAssert(term->initDone == INITOK); + + AquireLock(&term->lock); + retcode = term->PutOnTermUnlocked(term, ch); + ReleaseLock(&term->lock); + + return retcode; +} + +// +// Print string on terminal +// +error_t PrintOnTerm(Terminal_t *term, const char *str) +{ + error_t retcode = EOK; + + if (term == NULL) return EINVAL; + KalAssert(term->initDone == INITOK); + + AquireLock(&term->lock); + while (*str && retcode == EOK) { + retcode = term->PutOnTermUnlocked(term, *str++); + } + ReleaseLock(&term->lock); + + return retcode; +} + diff --git a/kaleid/kernel/ke/terminal.c b/kaleid/kernel/io/vga.c similarity index 66% rename from kaleid/kernel/ke/terminal.c rename to kaleid/kernel/io/vga.c index 1d7439b..f26ef65 100644 --- a/kaleid/kernel/ke/terminal.c +++ b/kaleid/kernel/io/vga.c @@ -4,102 +4,11 @@ // Authors: spectral` // // NeoX // // // -// Desc: Early terminal functions // +// Desc: VGA terminal functions // //----------------------------------------------------------------------------// #include -static Terminal_t vgaTerm; - -// -// Initialize standard output -// -void InitTerms(void) -{ - KalAssert(!GetStdOut() && vgaTerm.initDone != INITOK); - - vgaTerm.initDone = INITOK; - - SetStdDbg(&vgaTerm); - SetStdOut(&vgaTerm); - - ClearTerm(GetStdOut()); -} - -// -// Fill terminal with spaces -// -error_t ClearTerm(Terminal_t *term) -{ - error_t retcode; - - if (term == NULL) return EINVAL; - KalAssert(term->initDone == INITOK); - - AquireLock(&term->lock); - retcode = term->ClearTermUnlocked(term); - ReleaseLock(&term->lock); - - return retcode; -} - -// -// Change the color code -// -error_t ChTermColor(Terminal_t *term, TermColor_t fgColor, TermColor_t bgColor) -{ - if (fgColor > KTERM_COLOR_WHITE || bgColor > KTERM_COLOR_WHITE) - return EINVAL; - - if (term == NULL) - return EINVAL; - - AquireLock(&term->lock); - - term->fgColor = fgColor; - term->bgColor = bgColor; - - ReleaseLock(&term->lock); - - return EOK; -} - -// -// Write a single character on the terminal -// -error_t PutOnTerm(Terminal_t *term, char ch) -{ - error_t retcode; - - if (term == NULL) return EINVAL; - KalAssert(term->initDone == INITOK); - - AquireLock(&term->lock); - retcode = term->PutOnTermUnlocked(term, ch); - ReleaseLock(&term->lock); - - return retcode; -} - -// -// Print string on terminal -// -error_t PrintOnTerm(Terminal_t *term, const char *str) -{ - error_t retcode = EOK; - - if (term == NULL) return EINVAL; - KalAssert(term->initDone == INITOK); - - AquireLock(&term->lock); - while (*str && retcode == EOK) { - retcode = term->PutOnTermUnlocked(term, *str++); - } - ReleaseLock(&term->lock); - - return retcode; -} - //----------------------------------------------------------// // Internal functions for VGA terminals // // These DO NOT check input correctness // @@ -143,23 +52,17 @@ error_t VGA_PutOnTermUnlocked(Terminal_t *term, char ch) return EOK; } - // // Line feed first takes us to the very end of the line // Later in this function we actually do the line feed - // else if (ch == '\n') { term->currentY = term->width - 1; } - // // Tabulations account for "term->tabSize" spaces - // else if (ch == '\t') { prevY = term->currentY; for (i = 0; i < term->tabSize; i++) { - // // Make sure tabulations can't spread over two lines - // if (term->currentY == prevY) { VGA_PutOnTermUnlocked(term, ' '); } @@ -172,26 +75,18 @@ error_t VGA_PutOnTermUnlocked(Terminal_t *term, char ch) buffer[offset] = VGA_ComputeEntry(ch, VGA_ComputeColorCode(term->fgColor, term->bgColor)); } - // // Did we reach the end of line? - // if (++term->currentX == term->width) { term->currentX = 0; - // // Did we reach the buffer's end? - // if (++term->currentY == term->height) { - // // XXX scroll up - // term->currentY = 0; } } - // // Nothing can go wrong - // return EOK; } @@ -211,8 +106,9 @@ error_t VGA_PrintOnTermUnlocked(Terminal_t *term, const char *str) // // VGA output +// XXX custom sizes // -static Terminal_t vgaTerm = { +Terminal_t VGA_Terminal = { .initDone = FALSE, .lock = INITLOCK(KLOCK_MUTEX), @@ -234,3 +130,14 @@ static Terminal_t vgaTerm = { .PrintOnTermUnlocked = VGA_PrintOnTermUnlocked, }; + +// +// Initialize VGA output +// +void VGA_Init(void) +{ + KalAssert(VGA_Terminal.initDone != INITOK); + + VGA_Terminal.initDone = INITOK; +} + diff --git a/kaleid/kernel/proc/Makefile b/kaleid/kernel/proc/Makefile index 7c15976..c83ea97 100644 --- a/kaleid/kernel/proc/Makefile +++ b/kaleid/kernel/proc/Makefile @@ -1,4 +1,6 @@ sched-test: - gcc -O2 -Wall -Wextra -Wshadow -std=gnu11 -masm=intel -I../../include ./sched.c + gcc -O2 -Wall -Wextra -I../../include ./sched.c +clean: + rm a.out diff --git a/kaleid/kernel/proc/sched.c b/kaleid/kernel/proc/sched.c index 2a8dd9e..46b75f6 100644 --- a/kaleid/kernel/proc/sched.c +++ b/kaleid/kernel/proc/sched.c @@ -12,6 +12,7 @@ #ifndef _KALEID_KERNEL #include + CREATE_PER_CPU(CurProc, Process_t *); // @@ -19,16 +20,16 @@ CREATE_PER_CPU(CurProc, Process_t *); // 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, 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 }, + { 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 4, 3, 3, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, + { 9, 2, 2, 21, 21, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL, NULL, NULL }, }; #endif @@ -138,9 +139,7 @@ void SchedThisProcUnlocked(Process_t *proc) proc->schedNode = procNode; - // // 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 @@ -153,9 +152,7 @@ void SchedThisProcUnlocked(Process_t *proc) } } - // // Didn't find any process with lesser priority - // if (found == false) { AppendNode(head, procNode); } @@ -240,15 +237,11 @@ void SchedOnTick(void) Process_t *procNext, *winner, *previous = GetCurProc(); - // // We're either idle or running something - // KalAssert(GetCurProc() == NULL || GetCurProc()->procState == STATE_RUNNING); - // // Have the current process spent its timeslice? // (To be handled in CPU decisions function) - // if (GetCurProc() != NULL) { if (GetCurProc()->timeSlice <= 1) { // Re-schedule @@ -258,42 +251,36 @@ void SchedOnTick(void) _SetCurProc(NULL); } - // // Otherwise, make it lose a tick - // else { GetCurProc()->timeSlice--; } } - // // Are we idle, or scheduling next process? - // if (GetCurProc() == NULL) { SetCurProc(SelectSchedNext()); goto leave; } - // - // Is there a higher priority process that is runnable? - // - procNext = SelectSchedNext(); - winner = CompareProcs(GetCurProc(), procNext); + // Is preemption on and a re-schedule is needed? + if (GetPreemptCount() == PREEMPT_ON && GetReSchedFlag()) { - // - // Yes, procNext should preempt current process - // - if (winner == procNext) { - // Re-schedule - ReSchedCurProc(); + // Is there a higher priority process that is runnable? + procNext = SelectSchedNext(); + winner = CompareProcs(GetCurProc(), procNext); - // Switch to procNext - SetCurProc(procNext); + // Yes, procNext should preempt current process + if (winner == procNext) { + // Re-schedule + ReSchedCurProc(); + + // Switch to procNext + SetCurProc(procNext); + } } - // // Current process won't be preempted and has time remaining - // leave: SchedUnlock(); @@ -436,4 +423,3 @@ int main(void) } #endif - From 5f305161f5ab144a20b625c098312459d3b3b92e Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Feb 2019 15:34:39 +0100 Subject: [PATCH 09/14] Legal stuff --- .gitignore | 4 + ChangeLog | 33 ++++-- ProjectTree | 211 +++++++++++++++++++------------------- boot/folder.desc | 33 ++++-- boot/loader/cpu/cpu.asm | 23 ++++- boot/loader/cpu/cpuid.asm | 23 ++++- boot/loader/elf/elf.c | 26 ++++- boot/loader/fs/fat.asm | 21 +++- boot/loader/io/ata.asm | 21 +++- boot/loader/io/lmmem.asm | 21 +++- boot/loader/io/lmterm.asm | 22 +++- boot/loader/io/rmmem.asm | 21 +++- boot/loader/io/rmterm.asm | 21 +++- boot/loader/loader.asm | 22 +++- boot/mbr/mbr.asm | 23 ++++- boot/mbr/mbr.inc | 24 ++++- 16 files changed, 383 insertions(+), 166 deletions(-) diff --git a/.gitignore b/.gitignore index 7840e6a..42f9498 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,10 @@ test-*.c *.elf *.S +# CNAME STUFF +*.yml +CNAME + # Linker output *.ilk diff --git a/ChangeLog b/ChangeLog index b019064..9410f1e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,13 +1,26 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: ChangeLog #1 // -// From: 2018/12/06 // -// UpTo: current // -//----------------------------------------------------------------------------// +#=----------------------------------------------------------------------------=# +# GNU GPL OS/K # +# # +# Desc: Project ChangeLog # +# # +# # +# Copyright © 2018-2019 The OS/K Team # +# # +# This file is part of OS/K. # +# # +# OS/K is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OS/K is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY# without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OS/K. If not, see . # +#=----------------------------------------------------------------------------=# 2018/10/?? - Started talking about making our own OS 2018/11/?? - Name decided & creation of os-k.eu diff --git a/ProjectTree b/ProjectTree index 965face..586b9d7 100644 --- a/ProjectTree +++ b/ProjectTree @@ -1,107 +1,110 @@ -#------------------------------------------------------------------------------# -# GNU GPL OS/K # +#=----------------------------------------------------------------------------=# +# GNU GPL OS/K # # # -# Authors: spectral` # -# NeoX # +# Desc: Project Tree # # # -# Desc: Project Tree # -#------------------------------------------------------------------------------# +# # +# Copyright © 2018-2019 The OS/K Team # +# # +# This file is part of OS/K. # +# # +# OS/K is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OS/K is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY# without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OS/K. If not, see . # +#=----------------------------------------------------------------------------=# -src/ - | - x COPYING - x README.md - x ChangeLog - | - - Makefile - - Makefile.in - | - + boot/ - | | - | x folder.desc - | | - | - mbr.asm - | - mbr.inc - | | - | - loader.asm - | - loader16.inc - | - loader64.inc - | | - | 0 - | - + kaleid/ - | | - | + include/ - | | | - | | - kaleid.h - | | - kalkern.h - | | | - | | + common/ - | | | | - | | | - kaldefs.h - | | | - kaltypes.h - | | | - kalerror.h - | | | - kalassrt.h - | | | - kallims.h - | | | - kalmask.h - | | | - kalcrt.h - | | | | - | | | 0 - | | | - | | + kernel/ - | | | | - | | | - kernbase.h - | | | - kernlocks.h - | | | - kernterm.h - | | | | - | | | 0 - | | 0 - | | - | | - | + kernel/ - | | | - | | + init/ - | | | | - | | | - init.c - | | | | - | | | 0 - | | | - | | + ke/ - | | | | - | | | - panic.c - | | | - table.c - | | | - terminal.c - | | | | - | | | 0 - | | 0 - | | - | + common/ - | | | - | | - status.c - | | | - | | - arith.c - | | - rand.c - | | | - | | - atoi.c - | | - itoa.c - | | - strtol.c - | | | - | | - memory.c - | | - string.c - | | - sprintf.c - | | | - | | 0 - | 0 - | - + build/ - | | - | - preproc.h - | - iddtool.h - | | - | - kernel.ld - | | - | + bin/ - | + obj/ - | | - | 0 - 0 + + +. +├── boot +│   ├── loader +│   │   ├── cpu +│   │   │   ├── cpu.asm +│   │   │   └── cpuid.asm +│   │   ├── elf +│   │   │   └── elf.c +│   │   ├── fs +│   │   │   └── fat.asm +│   │   ├── io +│   │   │   ├── ata.asm +│   │   │   ├── lmmem.asm +│   │   │   ├── lmterm.asm +│   │   │   ├── rmmem.asm +│   │   │   └── rmterm.asm +│   │   └── loader.asm +│   ├── mbr +│   │   ├── mbr.asm +│   │   └── mbr.inc +│   └── folder.desc +├── build +│   ├── idttool.py +│   ├── kernel.ld +│   └── preproc.h +├── kaleid +│   ├── crtlib +│   │   ├── arith.c +│   │   ├── atoi.c +│   │   ├── itoa.c +│   │   ├── memory.c +│   │   ├── rand.c +│   │   ├── sprintf.c +│   │   ├── status.c +│   │   ├── string.c +│   │   └── strtol.c +│   ├── extras +│   │   ├── argv.c +│   │   └── prog.c +│   ├── include +│   │   ├── base +│   │   │   ├── assert.h +│   │   │   ├── bdefs.h +│   │   │   ├── crtlib.h +│   │   │   ├── errno.h +│   │   │   ├── limits.h +│   │   │   ├── masks.h +│   │   │   └── types.h +│   │   ├── extras +│   │   │   ├── argv.h +│   │   │   ├── list.h +│   │   │   ├── locks.h +│   │   │   ├── malloc.h +│   │   │   └── prog.h +│   │   ├── kernel +│   │   │   ├── base.h +│   │   │   ├── sched.h +│   │   │   └── terminal.h +│   │   ├── kalbase.h +│   │   ├── kaleid.h +│   │   ├── kalext.h +│   │   └── kalkern.h +│   └── kernel +│   ├── init +│   │   ├── init.c +│   │   └── table.c +│   ├── io +│   │   ├── cursor.c +│   │   ├── term.c +│   │   └── vga.c +│   ├── ke +│   │   └── panic.c +│   └── proc +│   ├── Makefile +│   └── sched.c +├── AUTHORS +├── ChangeLog +├── COPYING +├── Makefile +├── Makefile.in +├── ProjectTree +└── Readme.md + +20 directories, 61 files diff --git a/boot/folder.desc b/boot/folder.desc index 6322bc3..fc76abf 100644 --- a/boot/folder.desc +++ b/boot/folder.desc @@ -1,11 +1,26 @@ ---------------------------------------------------------------------- - GNU GPL OS/K - - Authors: spectral` - NeoX - - Desc: Folder description - "boot" ---------------------------------------------------------------------- +#=----------------------------------------------------------------------------=# +# GNU GPL OS/K # +# # +# Desc: Folder description - "boot" # +# # +# # +# Copyright © 2018-2019 The OS/K Team # +# # +# This file is part of OS/K. # +# # +# OS/K is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OS/K is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY# without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OS/K. If not, see . # +#=----------------------------------------------------------------------------=# This folder contains the source for OS/K's bootloader. @@ -19,5 +34,5 @@ It is divided in two parts each of exactly 512 bytes: The MBR must be placed precisely on the first sector of the hard drive. - loader.s - This is the Kernel Loader. XXX + This is the Kernel Loader. It switches to long mode and makes stuff. diff --git a/boot/loader/cpu/cpu.asm b/boot/loader/cpu/cpu.asm index e9b5274..b875758 100644 --- a/boot/loader/cpu/cpu.asm +++ b/boot/loader/cpu/cpu.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic longmode CPU functions ; -; (x86_64 architecture only) ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 64] diff --git a/boot/loader/cpu/cpuid.asm b/boot/loader/cpu/cpuid.asm index ac82eff..89c1bde 100644 --- a/boot/loader/cpu/cpuid.asm +++ b/boot/loader/cpu/cpuid.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic realmode CPU Detection ; -; (x86_64 architecture only) ; +; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 16] diff --git a/boot/loader/elf/elf.c b/boot/loader/elf/elf.c index c30d834..fe48cde 100644 --- a/boot/loader/elf/elf.c +++ b/boot/loader/elf/elf.c @@ -1,10 +1,26 @@ -//----------------------------------------------------------------------------// +//=--------------------------------------------------------------------------=// // GNU GPL OS/K // // // -// Authors: spectral` // -// NeoX // -// // // Desc: ELF64 Parser and Loader // -//----------------------------------------------------------------------------// +// (x86_64 architecture only) // +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// (at your option) any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//=--------------------------------------------------------------------------=// int stub; diff --git a/boot/loader/fs/fat.asm b/boot/loader/fs/fat.asm index d9bca9b..978d7ed 100644 --- a/boot/loader/fs/fat.asm +++ b/boot/loader/fs/fat.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic File Allocation Table Long mode Driver ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 64] diff --git a/boot/loader/io/ata.asm b/boot/loader/io/ata.asm index f075b7d..c37cfc6 100644 --- a/boot/loader/io/ata.asm +++ b/boot/loader/io/ata.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic Read Only ATA Long mode Driver ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 64] diff --git a/boot/loader/io/lmmem.asm b/boot/loader/io/lmmem.asm index a82330e..e77e4c2 100644 --- a/boot/loader/io/lmmem.asm +++ b/boot/loader/io/lmmem.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic Memory Long mode Functions ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 64] diff --git a/boot/loader/io/lmterm.asm b/boot/loader/io/lmterm.asm index 427ae89..c5805d8 100644 --- a/boot/loader/io/lmterm.asm +++ b/boot/loader/io/lmterm.asm @@ -1,13 +1,29 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic Colored VGA Terminal Long mode Driver ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; + ;;VIDEO %define TRAM 0x0B8000 ; [T]ext[RAM] %define VRAM 0x0A0000 ; [V]ideo[RAM] diff --git a/boot/loader/io/rmmem.asm b/boot/loader/io/rmmem.asm index 2348083..865b9a9 100644 --- a/boot/loader/io/rmmem.asm +++ b/boot/loader/io/rmmem.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic Memory Realmode Driver ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 16] diff --git a/boot/loader/io/rmterm.asm b/boot/loader/io/rmterm.asm index 62f1b8a..275ff71 100644 --- a/boot/loader/io/rmterm.asm +++ b/boot/loader/io/rmterm.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Basic realmode terminal functions ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; [BITS 16] diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index c67f038..6734aec 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; ; GNU GPL OS/K ; ; ; -; Authors: spectral` ; -; NeoX ; -; ; ; Desc: Kernel (second stage) Loader for OS/K ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; %define DEBUG @@ -242,4 +257,3 @@ main64: jmp Die -times 20 db 0 diff --git a/boot/mbr/mbr.asm b/boot/mbr/mbr.asm index f94c713..1a08f74 100644 --- a/boot/mbr/mbr.asm +++ b/boot/mbr/mbr.asm @@ -1,11 +1,26 @@ ;=----------------------------------------------------------------------------=; -; GNU GPL OS/K ; -; ; -; Authors: spectral` ; -; NeoX ; +; GNU GPL OS/K ; ; ; ; Desc: Bootsector for OS/K ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; ;; BOOT "SEGMENT" diff --git a/boot/mbr/mbr.inc b/boot/mbr/mbr.inc index a5f396d..b006cce 100644 --- a/boot/mbr/mbr.inc +++ b/boot/mbr/mbr.inc @@ -1,12 +1,28 @@ ;=----------------------------------------------------------------------------=; -; GNU GPL OS/K ; -; ; -; Authors: spectral` ; -; NeoX ; +; GNU GPL OS/K ; ; ; ; Desc: Bootsector for OS/K INCLUDED FUNCTIONS ; ; (x86_64 architecture only) ; +; ; +; ; +; Copyright © 2018-2019 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; + [BITS 16] read_clusters: From d318d6998f0a066d4a6e7d91e431593f23cb6f62 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Feb 2019 15:41:24 +0100 Subject: [PATCH 10/14] Legal stuff --- AUTHORS | 41 +++++++++++++++++++++++++++++++++++++++++ Makefile | 31 +++++++++++++++++++++++-------- Makefile.in | 26 +++++++++++++++++++++----- 3 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 AUTHORS diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..3c74434 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,41 @@ +#=----------------------------------------------------------------------------=# +# GNU GPL OS/K # +# # +# Desc: Project Authors File # +# # +# # +# Copyright © 2018-2019 The OS/K Team # +# # +# This file is part of OS/K. # +# # +# OS/K is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OS/K is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY# without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OS/K. If not, see . # +#=----------------------------------------------------------------------------=# + +The OS/K development team (named in the sources The OS/K Team) is composed of +all contributors of the project. Its representatives are the owners of the OS/K +project repository, and own the copyright in it. + + Founders and owner of the OS/K project repository + + Julian Barathieu (julianb0) + Adrien Bourmault (NeoX95) + + Main contributors + + Julian Barathieu (julianb0) + Adrien Bourmault (NeoX95) + + Other contributors + + (void) diff --git a/Makefile b/Makefile index 6458c3c..79e3b32 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,26 @@ -#----------------------------------------------------------------------------# -# GNU GPL OS/K # -# # -# Authors: spectral` # -# NeoX # -# # -# Desc: Project Makefile # -#----------------------------------------------------------------------------# +#=----------------------------------------------------------------------------=# +# GNU GPL OS/K # +# # +# Desc: Project Makefile # +# # +# # +# Copyright © 2018-2019 The OS/K Team # +# # +# This file is part of OS/K. # +# # +# OS/K is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OS/K is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY# without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OS/K. If not, see . # +#=----------------------------------------------------------------------------=# kernel: cpp ./Makefile.in > build/Makefile.out diff --git a/Makefile.in b/Makefile.in index 8fa91e9..e965c0b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,12 +1,28 @@ // -*- Mode: Makefile -*- -//----------------------------------------------------------------------------// + +//=--------------------------------------------------------------------------=// // GNU GPL OS/K // // // -// Authors: spectral` // -// NeoX // -// // // Desc: Project Makefile // -//----------------------------------------------------------------------------// +// // +// // +// Copyright © 2018-2019 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//=--------------------------------------------------------------------------=// // The madman's Makefile #include "build/preproc.h" From 95377dc5cbbf0427f2556bdf61407d6696a10de5 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Feb 2019 21:44:57 +0100 Subject: [PATCH 11/14] Legal Stuff --- Readme.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 8e765d1..cd0cbb1 100644 --- a/Readme.md +++ b/Readme.md @@ -1,8 +1,8 @@ -# GNU-GPL OS/K (OS on Kaleid) +# GNU GPL OS/K (OS on Kaleid) -### Fully open-source operating system from scratch (WIP), released under the GNU GPL version 3.0 +### Fully open-source operating system from scratch (WIP) -#### Master Branch +#### Master Branch For the project plan, see [OS/K Project](https://github.com/orgs/os-k-team/projects/1) @@ -11,4 +11,7 @@ For changelog, see [ChangeLog](https://github.com/os-k-team/GNU-GPL-OS-K/blob/ma For structure of the sources, see [ProjectTree](https://github.com/os-k-team/GNU-GPL-OS-K/blob/master/ProjectTree) Note that every file within OS/K is written using spaces for tabulation, with each -tabulation being 4 spaces long. +tabulation being 4 spaces long. + +| ![FSF Logo](https://www.os-k.eu/GPLLOGO.PNG) | This program is free software, released under the terms of the GNU GPL version 3 or later as published by the Free Software Foundation | +|----------------------------------------------|----------------------------------------------------------------------| From 3f92f587721c293ab2116604ba0ab3eb41d59d35 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Feb 2019 21:47:59 +0100 Subject: [PATCH 12/14] Update Readme.md --- Readme.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index cd0cbb1..aee3890 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,8 @@ # GNU GPL OS/K (OS on Kaleid) +| ![FSF Logo](https://www.os-k.eu/GPLLOGO.PNG) | This program is free software, released under the terms of the GNU GPL version 3 or later as published by the Free Software Foundation | +|----------------------------------------------|----------------------------------------------------------------------| + ### Fully open-source operating system from scratch (WIP) #### Master Branch @@ -13,5 +16,4 @@ For structure of the sources, see [ProjectTree](https://github.com/os-k-team/GNU Note that every file within OS/K is written using spaces for tabulation, with each tabulation being 4 spaces long. -| ![FSF Logo](https://www.os-k.eu/GPLLOGO.PNG) | This program is free software, released under the terms of the GNU GPL version 3 or later as published by the Free Software Foundation | -|----------------------------------------------|----------------------------------------------------------------------| + From 8dbf5b9fb57c92ef015bf3c82af637268ad93244 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Feb 2019 21:51:00 +0100 Subject: [PATCH 13/14] Legal Stuff --- Readme.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index aee3890..0319d97 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,6 @@ # GNU GPL OS/K (OS on Kaleid) -| ![FSF Logo](https://www.os-k.eu/GPLLOGO.PNG) | This program is free software, released under the terms of the GNU GPL version 3 or later as published by the Free Software Foundation | +| ![FSF Logo](https://www.os-k.eu/GPLLOGO.PNG) | This program is free software, released under the terms of the [GNU GPL](COPYING) version 3 or later as published by the Free Software Foundation | |----------------------------------------------|----------------------------------------------------------------------| ### Fully open-source operating system from scratch (WIP) @@ -9,11 +9,9 @@ For the project plan, see [OS/K Project](https://github.com/orgs/os-k-team/projects/1) -For changelog, see [ChangeLog](https://github.com/os-k-team/GNU-GPL-OS-K/blob/master/ChangeLog) +For changelog, see [ChangeLog](ChangeLog) -For structure of the sources, see [ProjectTree](https://github.com/os-k-team/GNU-GPL-OS-K/blob/master/ProjectTree) +For structure of the sources, see [ProjectTree](ProjectTree) Note that every file within OS/K is written using spaces for tabulation, with each tabulation being 4 spaces long. - - From 4442e1e65814ca02e3a47c482df7f2ff77f354e4 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 6 Feb 2019 21:52:46 +0100 Subject: [PATCH 14/14] Horrible Legal Stuff --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 0319d97..251d36e 100644 --- a/Readme.md +++ b/Readme.md @@ -3,7 +3,7 @@ | ![FSF Logo](https://www.os-k.eu/GPLLOGO.PNG) | This program is free software, released under the terms of the [GNU GPL](COPYING) version 3 or later as published by the Free Software Foundation | |----------------------------------------------|----------------------------------------------------------------------| -### Fully open-source operating system from scratch (WIP) +### Fully free operating system from scratch (WIP) #### Master Branch