From fdcf18e988bfaec933380c59e21ce57c6a4c810c Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 25 Dec 2018 19:40:32 +0100 Subject: [PATCH 01/22] Cleaned up some things --- src/kaleid/common/lib/convert.c | 2 +- src/kaleid/common/lib/sprintf.c | 10 ++-------- src/kaleid/kernel/mm/malloc.c | 12 ++++++++++++ src/kaleid/kernel/mm/malloc.h | 16 ++++++++++++++++ src/kaleid/linux/test-common.c | 1 - src/making.sh | 0 src/project-tree.txt | 2 ++ src/script.sh | 4 ---- 8 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 src/kaleid/kernel/mm/malloc.c create mode 100644 src/kaleid/kernel/mm/malloc.h delete mode 100644 src/making.sh delete mode 100644 src/script.sh diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/lib/convert.c index 2731900..0f76565 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/lib/convert.c @@ -37,7 +37,7 @@ char *itoa(int i, char *str, int base) *str++ = '0'; } - // compute digits... in reverse order (XXX?) + // compute digits... in reverse order while (i > 0) { *str++ = digits[i % base]; i /= base; diff --git a/src/kaleid/common/lib/sprintf.c b/src/kaleid/common/lib/sprintf.c index 1785a01..e26ded9 100644 --- a/src/kaleid/common/lib/sprintf.c +++ b/src/kaleid/common/lib/sprintf.c @@ -24,17 +24,14 @@ int sprintf(char *str, const char *fmt, ...) return ret; } -// -// Format str according to fmt, using the va_list ap -// int vsprintf(char *str, const char *fmt, va_list ap) { return vsnprintf(str, SIZE_T_MAX, fmt, ap); } // -// sprintf() but with a size limit: no more than n bytes are written in str -// XXX null termination behavior? +// (v)sprintf() but with a size limit: no more than n bytes are written in str +// XXX null termination behavior? // int snprintf(char *str, size_t n, const char *fmt, ...) { @@ -48,9 +45,6 @@ int snprintf(char *str, size_t n, const char *fmt, ...) return ret; } -// -// snprintf() but arguments -// int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) { int ret = 0; diff --git a/src/kaleid/kernel/mm/malloc.c b/src/kaleid/kernel/mm/malloc.c new file mode 100644 index 0000000..5c62036 --- /dev/null +++ b/src/kaleid/kernel/mm/malloc.c @@ -0,0 +1,12 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Memory allocation routines // +// Only exists to trigger Neox // +//----------------------------------------------------------------------------// + +#include "kernel/mm/malloc.h" + diff --git a/src/kaleid/kernel/mm/malloc.h b/src/kaleid/kernel/mm/malloc.h new file mode 100644 index 0000000..2669121 --- /dev/null +++ b/src/kaleid/kernel/mm/malloc.h @@ -0,0 +1,16 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Memory allocation routines // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_MM_MALLOC_H +#define _KALKERN_MM_MALLOC_H + +#include "common/common.h" + +#endif + diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index a8c746e..a1707c6 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -33,7 +33,6 @@ int main(int argc, char *argv[]) #undef strcpy assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0); - // tests done printf("2\n"); diff --git a/src/making.sh b/src/making.sh deleted file mode 100644 index e69de29..0000000 diff --git a/src/project-tree.txt b/src/project-tree.txt index 1304d6a..7e342f8 100644 --- a/src/project-tree.txt +++ b/src/project-tree.txt @@ -7,6 +7,8 @@ // Desc: Project Tree // //----------------------------------------------------------------------------// +// XXX *not* up to date + src/ | + boot/ diff --git a/src/script.sh b/src/script.sh deleted file mode 100644 index 1ef10e1..0000000 --- a/src/script.sh +++ /dev/null @@ -1,4 +0,0 @@ -make -cp ../bin/bootloader.bin "~/Documents/GNU OSK/bin/bootloader.bin" -qemu-system-x86_64 -hda "~/Documents/GNU OSK/bin/bootloader.bin" -qemu-system-i386 -hda "~/Documents/GNU OSK/bin/bootloader.bin" From 974bbba2210db85413a0b0c79d1287b76a8f6044 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Thu, 27 Dec 2018 15:04:31 +0100 Subject: [PATCH 02/22] Completed project tree and folder descriptions --- src/kaleid/common/assert.h | 8 +-- src/kaleid/common/atomic.h | 2 +- src/kaleid/common/common.h | 7 +- src/kaleid/common/folder.desc | 40 +++++++++++ src/kaleid/common/memory.h | 2 +- src/kaleid/common/status.h | 2 +- src/kaleid/common/string.h | 6 +- src/kaleid/common/types.h | 2 +- src/kaleid/{common => kernel}/config.h | 0 src/kaleid/{common => kernel}/config.h.in | 0 src/kaleid/kernel/folder.desc | 18 ++--- src/kaleid/kernel/io/terminal.c | 2 +- src/kaleid/kernel/mm/malloc.h | 1 + src/kaleid/linux/test-common.c | 2 + src/project-tree.txt | 83 ++++++++++++++++------- 15 files changed, 126 insertions(+), 49 deletions(-) rename src/kaleid/{common => kernel}/config.h (100%) rename src/kaleid/{common => kernel}/config.h.in (100%) diff --git a/src/kaleid/common/assert.h b/src/kaleid/common/assert.h index da80ace..8c18ad6 100644 --- a/src/kaleid/common/assert.h +++ b/src/kaleid/common/assert.h @@ -11,7 +11,7 @@ #define _KALCOMM_ASSERT_H #ifndef _KALCOMM_COMMON_H -#error "don't include common/types.h without common/common.h" +# error "don't include common/types.h without common/common.h" #endif #ifdef _OSK_SOURCE @@ -26,11 +26,11 @@ noreturn void ___assert_handler(const char *, const char *, int, const char *); #else // not debugging #if !defined(NDEBUG) -#define NDEBUG 1 +# define NDEBUG 1 #endif #if !defined(_NO_DEBUG) -#define _NO_DEBUG 1 +# define _NO_DEBUG 1 #endif #define assert(x) @@ -40,7 +40,7 @@ noreturn void ___assert_handler(const char *, const char *, int, const char *); #else // !defined(_OSK_SOURCE) #if defined(_NO_DEBUG) && !defined(NDEBUG) -#define NDEBUG 1 +# define NDEBUG 1 #endif #include diff --git a/src/kaleid/common/atomic.h b/src/kaleid/common/atomic.h index a1daef3..f520339 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/atomic.h @@ -11,7 +11,7 @@ #define _KALCOMM_ATOMIC_H #ifndef _KALCOMM_COMMON_H -#error "don't include common/types.h without common/common.h" +# error "don't include common/types.h without common/common.h" #endif // atomic_t defined in common/types.h diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index 9c9dd6a..52c3b44 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -11,7 +11,7 @@ #define _KALCOMM_COMMON_H #if !defined(_OSK_SOURCE) && (defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM)) -#define _OSK_SOURCE 1 +# define _OSK_SOURCE 1 #endif #if !defined(TRUE) && !defined(FALSE) @@ -41,8 +41,11 @@ # define unlikely(x) __builtin_expect((x), 0) #endif +#ifdef _KALEID_KERNEL +# include "kernel/config.h" +#endif + #include "common/types.h" -#include "common/config.h" #include "common/atomic.h" #include "common/status.h" #include "common/assert.h" diff --git a/src/kaleid/common/folder.desc b/src/kaleid/common/folder.desc index 8b13789..651f5a9 100644 --- a/src/kaleid/common/folder.desc +++ b/src/kaleid/common/folder.desc @@ -1 +1,41 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Folder description - "kaleid/common" // +//----------------------------------------------------------------------------// + +This is the folder containing the sources for Kaleid's C runtime library, linked both to the kernel +and to the system processes that run outiside the kernel. It can also be compiled for Linux as +(very basic) C library, for test purposes. + +This folder contains the following files: + - common.h + This file is to be included by every source file using the library, and it includes in turn + the following files: + - assert.h + Defines the macro "assert()". Currently any program wanting to use this macro has to + implement its own "___assert_handler(const char *cond, const char *file, int line, const char *func)" + but an (overridable) default handler will be furnished in the future. + - atomic.h + Support for atomic operations. When compiled by the kernel, also furnishes macros for + enabling/disabling interrupts. This will change in the future. + - status.h + Defines the different values of the "status_t" type used throughout Kaleid and related + utilitary functions. + - types.h + Provides the elementary types (size_t, etc) used throught Kaleid. + + - convert.h + Contains the declaration of conversion utilities (e.g. itoa()) + It is included by string.h + + - memory.h + Contains the declaration of the mem*() family of utilities (e.g. memcpy()) + + - string.h + Contains the declaration of various string-related utilities (including the sprintf() family) + Includes convert.h diff --git a/src/kaleid/common/memory.h b/src/kaleid/common/memory.h index a877e1a..a628141 100644 --- a/src/kaleid/common/memory.h +++ b/src/kaleid/common/memory.h @@ -11,7 +11,7 @@ #define _KALCOMM_MEMORY_H #ifndef _KALCOMM_COMMON_H -#include "common/common.h" +# include "common/common.h" #endif #endif diff --git a/src/kaleid/common/status.h b/src/kaleid/common/status.h index 82387da..898c62a 100644 --- a/src/kaleid/common/status.h +++ b/src/kaleid/common/status.h @@ -11,7 +11,7 @@ #define _KALCOMM_STATUS_H #ifndef _KALCOMM_COMMON_H -#error "don't include common/types.h without common/common.h" +# error "don't include common/types.h without common/common.h" #endif #ifndef _OSK_SOURCE diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h index 0f8542d..be51281 100644 --- a/src/kaleid/common/string.h +++ b/src/kaleid/common/string.h @@ -12,7 +12,11 @@ #define _KALCOMM_STRING_H #ifndef _KALCOMM_COMMON_H -#include "common/common.h" +# include "common/common.h" +#endif + +#ifndef _KALCOMM_CONVERT_H +# include "common/convert.h" #endif #ifndef _OSK_SOURCE diff --git a/src/kaleid/common/types.h b/src/kaleid/common/types.h index fb7d4ff..739efa9 100644 --- a/src/kaleid/common/types.h +++ b/src/kaleid/common/types.h @@ -12,7 +12,7 @@ #define _KALCOMM_TYPES_H #ifndef _KALCOMM_COMMON_H -#error "don't include common/types.h without common/common.h" +# error "don't include common/types.h without common/common.h" #endif #ifndef KEEP_KALCOMM_TYPES_MINIMAL diff --git a/src/kaleid/common/config.h b/src/kaleid/kernel/config.h similarity index 100% rename from src/kaleid/common/config.h rename to src/kaleid/kernel/config.h diff --git a/src/kaleid/common/config.h.in b/src/kaleid/kernel/config.h.in similarity index 100% rename from src/kaleid/common/config.h.in rename to src/kaleid/kernel/config.h.in diff --git a/src/kaleid/kernel/folder.desc b/src/kaleid/kernel/folder.desc index f035c37..1b37b3a 100644 --- a/src/kaleid/kernel/folder.desc +++ b/src/kaleid/kernel/folder.desc @@ -7,38 +7,30 @@ // Desc: Folder description - "kaleid/kernel" // //----------------------------------------------------------------------------// -This is the folder containing the source of Kaleid's kernel. +This folder contains the source of Kaleid's kernel component. -It contains the following files: +This contains the following files: - init.c The file containing the entry point of Kaleid, the kstart() function called from the bootloader (see ../../boot). -This folder also has the following subfolders -(for more information on a particular subfolder, see {name}/folder.desc) +This folder also has the following subfolders: - mm/ This folder contains all files related to memory management. - fs/ - This folder contains Kaleid's virtual filesystem, as well as one + This folder will contain Kaleid's virtual filesystem, as well as one subfolder for each FS supported by Kaleid (e.g. FAT filesystem). - io/ I/O folder. (XXX) - ps/ - This folder contains Kaleid's process manager and scheduler, as well as the + This folder will contain Kaleid's process manager and scheduler, as well as the implementation of the related syscalls and functions. - ex/ This folder contains the exec()-related functions and syscalls, as well as one subfolder per executable format supported by Kaleid (e.g. ELF executable) - - se/ - Security folder. (XXX) - - - sys/ - Syscall folder. (XXX) - - diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index 503dfcd..0fd46ac 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -167,7 +167,7 @@ status_t kterm_putch(struct kterm *kt, char ch) // -// Print string on kterminal +// Print string on terminal // status_t kterm_print(struct kterm *kt, const char *str) { diff --git a/src/kaleid/kernel/mm/malloc.h b/src/kaleid/kernel/mm/malloc.h index 2669121..9d63aba 100644 --- a/src/kaleid/kernel/mm/malloc.h +++ b/src/kaleid/kernel/mm/malloc.h @@ -5,6 +5,7 @@ // NeoX // // // // Desc: Memory allocation routines // +// Only exists to trigger Neox // //----------------------------------------------------------------------------// #ifndef _KALKERN_MM_MALLOC_H diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index a1707c6..ee2df6f 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -33,6 +33,8 @@ int main(int argc, char *argv[]) #undef strcpy assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0); + // XXX test itoa() and v?sn?printf() + // tests done printf("2\n"); diff --git a/src/project-tree.txt b/src/project-tree.txt index 7e342f8..f102785 100644 --- a/src/project-tree.txt +++ b/src/project-tree.txt @@ -7,9 +7,14 @@ // Desc: Project Tree // //----------------------------------------------------------------------------// -// XXX *not* up to date - src/ + | + x COPYING + x CONTACT + x ChangeLog + | + - Makefile + - kernel.ld | + boot/ | | @@ -22,7 +27,7 @@ src/ | - loader16.inc | - loader64.inc | | - | - types.h + | 0 | + kaleid/ | | @@ -30,31 +35,61 @@ src/ | | | | | x folder.desc | | | + | | - config.h + | | - config.h.in + | | | | | - init.c | | - init.h | | | | | + io/ - | | | - | | - ports.c - | | - ports.h - | | - terminal.c - | | - terminal.h + | | | | + | | | - ports.c + | | | - ports.h + | | | | + | | | - terminal.c + | | | - terminal.h + | | | | + | | | 0 + | | | + | | + ke/ + | | | | + | | | - panic.c + | | | - panic.h + | | | | + | | | 0 + | | | + | | 0 | | | + common/ - | | - | x folder.desc - | | - | - assert.h - | - atomic.h - | - common.h - | - config.h - | - config.h.in - | - status.h - | - string.h - | - types.h - | | - | + lib/ - | | - | - string.c - | + | | | + | | x folder.desc + | | | + | | - common.h + | | - assert.h + | | - atomic.h + | | - status.h + | | - types.h + | | | + | | - string.h + | | - memory.h + | | - convert.h + | | | + | | + lib/ + | | | | + | | | - status.c + | | | | + | | | - string.c + | | | - memory.c + | | | - convert.c + | | | - sprintf.c + | | | | + | | | 0 + | | 0 + | | + | + linux/ + | | | + | | - test-common.c + | | | + | | 0 + | 0 0 From 6675f7eb78054f1105af00285601cfe595789120 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 28 Dec 2018 18:52:55 +0100 Subject: [PATCH 03/22] Stuff --- src/Makefile | 15 +++++--- src/kaleid/common/lib/convert.c | 2 +- src/kaleid/common/lib/memory.c | 68 +++++++++++++++++++++++++++++++++ src/kaleid/common/memory.h | 10 +++++ src/kaleid/common/types.h | 9 +++-- src/kaleid/linux/test-common.c | 40 +++++++++++-------- 6 files changed, 119 insertions(+), 25 deletions(-) diff --git a/src/Makefile b/src/Makefile index 79c8836..cf8c31f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,12 +10,13 @@ CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" CC2NAME=gcc # compiler for testing CLDSCR=-T kernel.ld -CWARNS= -pedantic -Wall -Wextra -Werror +COPTIM=-O2 #-fbuiltin-memset +CWARNS=-pedantic -Wall -Wextra -Werror CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 CINCLUDES=-I./kaleid CDEFINES= -CC=$(CCNAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) +CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) KCC=$(CC) -D_KALEID_KERNEL ASM=nasm @@ -58,20 +59,24 @@ testing: bootloader pseudo_kern COBJDIR=$(OBJDIR)/$(COMMDIR) LOBJDIR=$(OBJDIR)/$(LINXDIR) -COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \ +COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(KERNDIR)/config.h \ $(COMMDIR)/status.h -COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o +COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o $(COBJDIR)/lib/convert.o $(COBJDIR)/lib/memory.o common: $(COMMDEPS) $(COMMDIR)/lib/string.c $(COMMDIR)/lib/status.c $(KCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o $(KCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o + $(KCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o + $(KCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o -CCC=$(CC2NAME) $(CWARNS) $(CDEFINES) $(CINCLUDES) +CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CDEFINES) $(CINCLUDES) common-test: $(CCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o $(CCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o + $(CCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o + $(CCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/lib/convert.c index 0f76565..284f61f 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/lib/convert.c @@ -7,7 +7,7 @@ // Desc: Conversion utilities // //----------------------------------------------------------------------------// -#include "common/convert.h" +#include "common/string.h" // // Digits table for bases <=36 diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 3ebe6ee..547ab79 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -9,3 +9,71 @@ #include "common/memory.h" +// to be moved +#define QWORD_SIZE 8 +#define QWORD_ALIGN 8 + +// +// Set "qwords"-many aligned qwords starting from ptr to val +// +static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +{ + size_t n; + ullong *uptr = (ullong *)ptr; + + // aligned memory write + for (n = 0; n < qwords; n++) { + *(uptr + n) = uval; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to val +// This is most certainely overkill, but I enjoyed doing this +// Alignment stuff barely matters on modern processors +// This may actually be slower than the naive way +// +void *memset(void *ptr, int val, size_t bytes) +{ + uchar *uptr = (uchar *)ptr; + const size_t qwords = bytes/QWORD_SIZE; + + // get rid of everything after the first byte + val = val & 0xFF; + + // deal with bytes before start of the first aligned qword + while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // move qword by qword + if (qwords) { + const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) + | ((ullong)val << 40) | ((ullong)val << 32) + | ((ullong)val << 24) | ((ullong)val << 16) + | ((ullong)val << 8) | ((ullong)val); + + memsetq(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE; + bytes %= QWORD_SIZE; + } + + // deal with what's left + while (bytes--) { + *uptr++ = (uchar)val; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + return memset(ptr, 0, bytes); +} + diff --git a/src/kaleid/common/memory.h b/src/kaleid/common/memory.h index a628141..31f6a6b 100644 --- a/src/kaleid/common/memory.h +++ b/src/kaleid/common/memory.h @@ -14,5 +14,15 @@ # include "common/common.h" #endif +#ifndef _OSK_SOURCE +# define memcpy _osk_memcpy +# define memset _osk_memset +# define memcmp _osk_memcmp +# define memzero _osk_memzero +#endif + +void *memset(void *, int, size_t); +void *memzero(void *, size_t); + #endif diff --git a/src/kaleid/common/types.h b/src/kaleid/common/types.h index 739efa9..bf74580 100644 --- a/src/kaleid/common/types.h +++ b/src/kaleid/common/types.h @@ -15,8 +15,6 @@ # error "don't include common/types.h without common/common.h" #endif -#ifndef KEEP_KALCOMM_TYPES_MINIMAL -typedef _Bool bool; typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; @@ -24,6 +22,11 @@ typedef unsigned long ulong; typedef long long llong; typedef unsigned long long ullong; typedef long double ldouble; +typedef short port_t; +typedef short status_t; + +#ifndef KEEP_KALCOMM_TYPES_MINIMAL +typedef _Bool bool; typedef uint wchar_t; typedef ullong size_t; typedef llong ssize_t; @@ -33,8 +36,6 @@ typedef ulong pid_t; typedef void *va_list; #endif -typedef short port_t; -typedef short status_t; // XXX limits #endif diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index ee2df6f..bef4749 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -9,35 +9,45 @@ #include #include -#include #define KEEP_KALCOMM_TYPES_MINIMAL #include "common/common.h" #include "common/string.h" +#include "common/memory.h" int main(int argc, char *argv[]) { (void)argc; (void)argv; + assert(sizeof(char) == 1); + assert(sizeof(short) == 2); + assert(sizeof(int) == 4); + assert(sizeof(long) == 8); + assert(sizeof(long long) == 8); + + // please don't mind how weird this file is + // I remove tests of "simple" functions after + // I'm done with testing that function + // so a lot of test variables remain in case + // I need them for a future test + const char *test1 = "test string\n"; - char *test2 = malloc(strlen(test1)); - char *test3 = malloc(strlen(test1)); + const size_t size1 = strlen(test1); + char *test2 = malloc(size1); + char *test3 = malloc(size1); + + memset(test2, 'x', size1); + + size_t it; + for (it = 0; it < size1; it++) { + char c = *(test2 + it); + printf("%c", (c ? c : '-')); + } - printf("1\n"); + puts(""); - #undef strlen - assert(strlen("test string") == _osk_strlen("test string")); - - #undef strcpy - assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0); - - // XXX test itoa() and v?sn?printf() - - // tests done - printf("2\n"); - free(test2); free(test3); From 9cca8f9ef6c41d65dac781788db5c5e3a43a8fe1 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 28 Dec 2018 19:15:22 +0100 Subject: [PATCH 04/22] Stuff --- src/Makefile | 6 +++--- src/kaleid/common/lib/memory.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Makefile b/src/Makefile index cf8c31f..eaa9884 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,9 +8,9 @@ #----------------------------------------------------------------------------# CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" -CC2NAME=gcc # compiler for testing +CC2NAME=gcc +COPTIM=-O2 CLDSCR=-T kernel.ld -COPTIM=-O2 #-fbuiltin-memset CWARNS=-pedantic -Wall -Wextra -Werror CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 CINCLUDES=-I./kaleid @@ -70,7 +70,7 @@ common: $(COMMDEPS) $(COMMDIR)/lib/string.c $(COMMDIR)/lib/status.c $(KCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o $(KCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o -CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CDEFINES) $(CINCLUDES) +CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) common-test: $(CCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 547ab79..70c4dbd 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -35,9 +35,9 @@ static inline void *memsetq(void *ptr, ullong uval, size_t qwords) // Alignment stuff barely matters on modern processors // This may actually be slower than the naive way // -void *memset(void *ptr, int val, size_t bytes) +void *memset(void *ptr, register int val, register size_t bytes) { - uchar *uptr = (uchar *)ptr; + register uchar *uptr = (uchar *)ptr; const size_t qwords = bytes/QWORD_SIZE; // get rid of everything after the first byte From e22d8ede123cfefd45fdc49383708b780ac1cccc Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 28 Dec 2018 20:49:43 +0100 Subject: [PATCH 05/22] Removed stuff --- src/kaleid/common/lib/memory.c | 60 ++++---------------------- src/kaleid/common/sub/memory.c | 78 ++++++++++++++++++++++++++++++++++ src/kaleid/linux/test-common.c | 31 ++++++-------- 3 files changed, 101 insertions(+), 68 deletions(-) create mode 100644 src/kaleid/common/sub/memory.c diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 70c4dbd..8d85d9c 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -9,62 +9,15 @@ #include "common/memory.h" -// to be moved -#define QWORD_SIZE 8 -#define QWORD_ALIGN 8 - -// -// Set "qwords"-many aligned qwords starting from ptr to val -// -static inline void *memsetq(void *ptr, ullong uval, size_t qwords) -{ - size_t n; - ullong *uptr = (ullong *)ptr; - - // aligned memory write - for (n = 0; n < qwords; n++) { - *(uptr + n) = uval; - } - - return ptr; -} - // // Set "bytes"-many bytes starting from ptr to val -// This is most certainely overkill, but I enjoyed doing this -// Alignment stuff barely matters on modern processors -// This may actually be slower than the naive way // void *memset(void *ptr, register int val, register size_t bytes) { - register uchar *uptr = (uchar *)ptr; - const size_t qwords = bytes/QWORD_SIZE; + uchar uval = val & 0xFF; + uchar *uptr = (uchar *)ptr; - // get rid of everything after the first byte - val = val & 0xFF; - - // deal with bytes before start of the first aligned qword - while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { - *uptr++ = (uchar)val; - } - - // move qword by qword - if (qwords) { - const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) - | ((ullong)val << 40) | ((ullong)val << 32) - | ((ullong)val << 24) | ((ullong)val << 16) - | ((ullong)val << 8) | ((ullong)val); - - memsetq(uptr, uval, qwords); - - uptr += qwords * QWORD_SIZE; - bytes %= QWORD_SIZE; - } - - // deal with what's left - while (bytes--) { - *uptr++ = (uchar)val; - } + while (bytes--) *uptr++ = uval; return ptr; } @@ -74,6 +27,11 @@ void *memset(void *ptr, register int val, register size_t bytes) // void *memzero(void *ptr, size_t bytes) { - return memset(ptr, 0, bytes); + uchar *uptr = (uchar *)ptr; + + while (bytes--) *uptr++ = 0; + + return ptr; } + diff --git a/src/kaleid/common/sub/memory.c b/src/kaleid/common/sub/memory.c new file mode 100644 index 0000000..8a4585a --- /dev/null +++ b/src/kaleid/common/sub/memory.c @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: mem*() functions - sub-optimal version // +//----------------------------------------------------------------------------// + +#include "common/memory.h" + +// to be moved +#define QWORD_SIZE 8 +#define QWORD_ALIGN 8 + +// +// Set "qwords"-many aligned qwords starting from ptr to val +// +static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +{ + size_t n; + ullong *uptr = (ullong *)ptr; + + // aligned memory write + for (n = 0; n < qwords; n++) { + *(uptr + n) = uval; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to val +// This is most certainely overkill, but I enjoyed doing this +// Alignment stuff barely matters on modern processors +// +void *memset(void *ptr, register int val, register size_t bytes) +{ + register uchar *uptr = (uchar *)ptr; + const size_t qwords = bytes/QWORD_SIZE; + + // get rid of everything after the first byte + val = val & 0xFF; + + // deal with bytes before start of the first aligned qword + while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // move qword by qword + if (qwords) { + const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) + | ((ullong)val << 40) | ((ullong)val << 32) + | ((ullong)val << 24) | ((ullong)val << 16) + | ((ullong)val << 8) | ((ullong)val); + + memsetq(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE; + bytes %= QWORD_SIZE; + } + + // deal with what's left + while (bytes--) { + *uptr++ = (uchar)val; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + return memset(ptr, 0, bytes); +} + diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index bef4749..127d07b 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -21,35 +21,32 @@ int main(int argc, char *argv[]) (void)argc; (void)argv; - assert(sizeof(char) == 1); - assert(sizeof(short) == 2); - assert(sizeof(int) == 4); - assert(sizeof(long) == 8); - assert(sizeof(long long) == 8); - // please don't mind how weird this file is // I remove tests of "simple" functions after // I'm done with testing that function // so a lot of test variables remain in case // I need them for a future test - const char *test1 = "test string\n"; - const size_t size1 = strlen(test1); - char *test2 = malloc(size1); - char *test3 = malloc(size1); + //const char *test1 = "test string\n"; + //const size_t size1 = strlen(test1); + //char *test2 = malloc(size1); + //char *test3 = malloc(size1); - memset(test2, 'x', size1); + //const size_t sizex = 10000000; + //void *xxx = malloc(sizex); + //printf("%p\n",xxx); + //memset(xxx, 'x', sizex); - size_t it; - for (it = 0; it < size1; it++) { - char c = *(test2 + it); + /*size_t it; + for (it = 0; it < sizex; it++) { + char c = *(xxx + it); printf("%c", (c ? c : '-')); } - puts(""); + puts("");*/ - free(test2); - free(test3); + //free(test2); + //free(test3); return 0; } From 6f4a616eb663ddc71192d890c93e3699d66c908e Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 28 Dec 2018 20:50:28 +0100 Subject: [PATCH 06/22] Removed stuff --- src/kaleid/common/lib/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 8d85d9c..6ef23ce 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -12,7 +12,7 @@ // // Set "bytes"-many bytes starting from ptr to val // -void *memset(void *ptr, register int val, register size_t bytes) +void *memset(void *ptr, int val, size_t bytes) { uchar uval = val & 0xFF; uchar *uptr = (uchar *)ptr; From b6ea3c20fe613652e60051d36e7c5ae035b5faab Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 28 Dec 2018 21:20:59 +0100 Subject: [PATCH 07/22] Stuff --- src/Makefile | 2 +- src/boot/folder.desc | 16 ++++++++-------- src/kaleid/common/folder.desc | 16 ++++++++-------- src/kaleid/common/lib/string.c | 23 ++++++++++++++++++----- src/kaleid/common/sub/memory.c | 9 ++++++--- src/kaleid/kernel/folder.desc | 16 ++++++++-------- src/kaleid/linux/test-common.c | 18 ++++++++++++++++-- 7 files changed, 65 insertions(+), 35 deletions(-) diff --git a/src/Makefile b/src/Makefile index eaa9884..cb8b590 100644 --- a/src/Makefile +++ b/src/Makefile @@ -72,7 +72,7 @@ common: $(COMMDEPS) $(COMMDIR)/lib/string.c $(COMMDIR)/lib/status.c CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) -common-test: +test-common: $(CCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o $(CCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o $(CCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o diff --git a/src/boot/folder.desc b/src/boot/folder.desc index d6b3cab..6322bc3 100644 --- a/src/boot/folder.desc +++ b/src/boot/folder.desc @@ -1,11 +1,11 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Folder description - "boot" // -//----------------------------------------------------------------------------// +--------------------------------------------------------------------- + GNU GPL OS/K + + Authors: spectral` + NeoX + + Desc: Folder description - "boot" +--------------------------------------------------------------------- This folder contains the source for OS/K's bootloader. diff --git a/src/kaleid/common/folder.desc b/src/kaleid/common/folder.desc index 651f5a9..ab5d8cc 100644 --- a/src/kaleid/common/folder.desc +++ b/src/kaleid/common/folder.desc @@ -1,11 +1,11 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Folder description - "kaleid/common" // -//----------------------------------------------------------------------------// +--------------------------------------------------------------------- + GNU GPL OS/K + + Authors: spectral` + NeoX + + Desc: Folder description - "kaleid/common" +--------------------------------------------------------------------- This is the folder containing the sources for Kaleid's C runtime library, linked both to the kernel and to the system processes that run outiside the kernel. It can also be compiled for Linux as diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/lib/string.c index f81f60c..89cf625 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/lib/string.c @@ -38,18 +38,31 @@ char *strcpy(char *dest, const char *src) // char *strncpy(char *dest, const char *src, size_t n) { - size_t i; + size_t it; - for (i = 0; i < n && src[i]; i++) { - dest[i] = src[i]; + for (it = 0; it < n && src[it]; i++) { + dest[it] = src[i]; } - while (i < n) dest[i++] = 0; + while (it < n) dest[it++] = 0; return dest; } // -// Reverses a string +// Reverses the string src, putting the result into dest +// +char *strrev(char *dest, const char *src) +{ + size_t m = 0, n = strlen(src); + + dest[n--] = '\0'; + + while (n) dest[m++] = src[n--]; +} + +// +// Reverses a string, modifying it +// Returns a point to said string // char *reverse(char *str) { diff --git a/src/kaleid/common/sub/memory.c b/src/kaleid/common/sub/memory.c index 8a4585a..06407b9 100644 --- a/src/kaleid/common/sub/memory.c +++ b/src/kaleid/common/sub/memory.c @@ -4,9 +4,12 @@ // Authors: spectral` // // NeoX // // // -// Desc: mem*() functions - sub-optimal version // +// Desc: mem*() functions // //----------------------------------------------------------------------------// +// XXX to be improved before being brought back +// to be tested with more alignment sizes + #include "common/memory.h" // to be moved @@ -34,9 +37,9 @@ static inline void *memsetq(void *ptr, ullong uval, size_t qwords) // This is most certainely overkill, but I enjoyed doing this // Alignment stuff barely matters on modern processors // -void *memset(void *ptr, register int val, register size_t bytes) +void *memset(void *ptr, int val, size_t bytes) { - register uchar *uptr = (uchar *)ptr; + uchar *uptr = (uchar *)ptr; const size_t qwords = bytes/QWORD_SIZE; // get rid of everything after the first byte diff --git a/src/kaleid/kernel/folder.desc b/src/kaleid/kernel/folder.desc index 1b37b3a..039b5af 100644 --- a/src/kaleid/kernel/folder.desc +++ b/src/kaleid/kernel/folder.desc @@ -1,11 +1,11 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Folder description - "kaleid/kernel" // -//----------------------------------------------------------------------------// +--------------------------------------------------------------------- + GNU GPL OS/K + + Authors: spectral` + NeoX + + Desc: Folder description - "kaleid/kernel" +--------------------------------------------------------------------- This folder contains the source of Kaleid's kernel component. diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index 127d07b..d9b3aec 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -7,9 +7,8 @@ // Desc: Test file for common/ // //----------------------------------------------------------------------------// -#include #include - +#include #define KEEP_KALCOMM_TYPES_MINIMAL #include "common/common.h" @@ -36,6 +35,21 @@ int main(int argc, char *argv[]) //void *xxx = malloc(sizex); //printf("%p\n",xxx); //memset(xxx, 'x', sizex); + + const char *str = "ceci est un string de test\n"; + char *str2 = malloc((strlen(str) + 3) * sizeof(char)); + + strcpy(str2, str); + + size_t s = strlen(str2); + + printf(reverse(reverse(str2))); + + str2[s] = '\n'; + str2[s+1] = '\0'; + printf(reverse(reverse(str2))); + + free(str2); /*size_t it; for (it = 0; it < sizex; it++) { From e76a30d1c523d6d0a53b4469119a6ac935e14797 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 28 Dec 2018 22:16:22 +0100 Subject: [PATCH 08/22] Stuff --- src/kaleid/common/lib/string.c | 11 +++++++---- src/kaleid/common/string.h | 18 ++++++++++-------- src/kaleid/common/sub/memory.c | 2 -- src/kaleid/linux/test-common.c | 16 ++++++++++------ 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/lib/string.c index 89cf625..464ff2e 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/lib/string.c @@ -40,8 +40,8 @@ char *strncpy(char *dest, const char *src, size_t n) { size_t it; - for (it = 0; it < n && src[it]; i++) { - dest[it] = src[i]; + for (it = 0; it < n && src[it]; it++) { + dest[it] = src[it]; } while (it < n) dest[it++] = 0; @@ -53,11 +53,14 @@ char *strncpy(char *dest, const char *src, size_t n) // char *strrev(char *dest, const char *src) { - size_t m = 0, n = strlen(src); + char *orig = dest; + size_t n = strlen(src); dest[n--] = '\0'; - while (n) dest[m++] = src[n--]; + while ((*dest++ = src[n--])); + + return orig; } // diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h index be51281..61056be 100644 --- a/src/kaleid/common/string.h +++ b/src/kaleid/common/string.h @@ -21,21 +21,23 @@ #ifndef _OSK_SOURCE -# define strlen _osk_strlen -# define strcpy _osk_strcpy -# define strncpy _osk_strncpy -# define reverse _osk_reverse +# define strlen _osk_strlen +# define strcpy _osk_strcpy +# define strncpy _osk_strncpy +# define strrev _osk_strrev +# define reverse _osk_reverse -# define sprintf _osk_sprintf -# define snprintf _osk_snprintf -# define vsprintf _osk_vsprintf -# define vsnprintf _osk_vsnprintf +# define sprintf _osk_sprintf +# define snprintf _osk_snprintf +# define vsprintf _osk_vsprintf +# define vsnprintf _osk_vsnprintf #endif size_t strlen(const char *); char *strcpy(char *, const char *); char *strncpy(char *, const char *, size_t); +char *strrev(char *dest, const char *src); char *reverse(char *); int sprintf(char *, const char *, ...); diff --git a/src/kaleid/common/sub/memory.c b/src/kaleid/common/sub/memory.c index 06407b9..aaecb6b 100644 --- a/src/kaleid/common/sub/memory.c +++ b/src/kaleid/common/sub/memory.c @@ -34,8 +34,6 @@ static inline void *memsetq(void *ptr, ullong uval, size_t qwords) // // Set "bytes"-many bytes starting from ptr to val -// This is most certainely overkill, but I enjoyed doing this -// Alignment stuff barely matters on modern processors // void *memset(void *ptr, int val, size_t bytes) { diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index d9b3aec..edae890 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -36,20 +36,24 @@ int main(int argc, char *argv[]) //printf("%p\n",xxx); //memset(xxx, 'x', sizex); - const char *str = "ceci est un string de test\n"; + const char *str = "ceci est un string de test!"; char *str2 = malloc((strlen(str) + 3) * sizeof(char)); + char *str3 = malloc((strlen(str) + 3) * sizeof(char)); strcpy(str2, str); - size_t s = strlen(str2); + //size_t s = strlen(str2); - printf(reverse(reverse(str2))); + strrev(str3,str2); + printf("%lu - %s\n", strlen(str3), str3); - str2[s] = '\n'; - str2[s+1] = '\0'; - printf(reverse(reverse(str2))); + //str2[s] = '\n'; + //str2[s+1] = '\0'; + strrev(str2,str3); + printf("%lu - %s\n", strlen(str2), str2); free(str2); + free(str3); /*size_t it; for (it = 0; it < sizex; it++) { From 4ae060bd5dc42d4acda921c60e391ee926b5e0c7 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Sat, 29 Dec 2018 23:51:00 +0100 Subject: [PATCH 09/22] Big stuff --- src/Makefile | 5 ++-- src/kaleid/common/atomic.h | 6 ++-- src/kaleid/kernel/init.c | 8 +++-- src/kaleid/kernel/io/ports.h | 8 +++-- src/kaleid/kernel/io/terminal.c | 52 ++++++++++++++++----------------- src/kaleid/kernel/io/terminal.h | 30 ++++++++++--------- src/kaleid/kernel/ke/panic.c | 24 ++++++++------- src/kaleid/kernel/ke/panic.h | 10 +++++-- src/kaleid/kernel/ke/state.c | 16 ++++++++++ src/kaleid/kernel/ke/state.h | 38 ++++++++++++++++++++++++ 10 files changed, 133 insertions(+), 64 deletions(-) create mode 100644 src/kaleid/kernel/ke/state.c create mode 100644 src/kaleid/kernel/ke/state.h diff --git a/src/Makefile b/src/Makefile index cb8b590..4988716 100644 --- a/src/Makefile +++ b/src/Makefile @@ -86,11 +86,12 @@ test-common: KOBJDIR=$(OBJDIR)/$(KERNDIR) KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h -KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/panic.c $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c -KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o +KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/state.c $(KERNDIR)/ke/panic.c $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c +KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/state.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o kernel: common $(KERNSRCS) $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o + $(KCC) -c $(KERNDIR)/ke/state.c -o $(KOBJDIR)/ke/state.o $(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o $(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o diff --git a/src/kaleid/common/atomic.h b/src/kaleid/common/atomic.h index f520339..56a4afb 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/atomic.h @@ -19,9 +19,9 @@ #ifdef _KALEID_KERNEL // only available in the kernel -#define cli() asm volatile ("cli") -#define sti() asm volatile ("sti") -#define hlt() asm volatile ("hlt") +#define DosDisableInterrupts() asm volatile ("cli") +#define DosEnableInterrupts() asm volatile ("sti") +#define DosHaltCPU() asm volatile ("hlt") #endif diff --git a/src/kaleid/kernel/init.c b/src/kaleid/kernel/init.c index b6f268a..aa794fe 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -15,8 +15,10 @@ // Entry point of kaleid-kernel.elf // void kstart(void) -{ - kterm_init(); - panic("Goodbye World :("); +{ + DosSetKernState(KSTATE_INIT); + + DosInitTerms(); + DosPanic("Goodbye World :("); } diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index b2adfbe..8df790d 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/src/kaleid/kernel/io/ports.h @@ -10,12 +10,14 @@ #ifndef _KALKERN_IO_PORTS_H #define _KALKERN_IO_PORTS_H +#ifndef _KALCOMM_COMMON_H #include "common/common.h" +#endif -#define outb(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) +#define DosWriteByteOnPort(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) -uchar inb(port_t); -ushort inw(port_t); +uchar DosReadByteFromPort(port_t); +ushort DosReadWordFromPort(port_t); #endif diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index 0fd46ac..e172daf 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -19,7 +19,7 @@ // // VGA output // -static struct kterm _kt_vga = { +static terminal_t _vga_term = { .kt_buffer = (ushort *)0xB8000, .kt_width = 80, .kt_height = 25, @@ -35,50 +35,50 @@ static struct kterm _kt_vga = { // // Standard output terminal // -struct kterm *kt_stdout; +terminal_t *stdout; // // Initialize standard output // -void kterm_init(void) +void DosInitTerms(void) { - assert(!kt_stdout && !_kt_vga.kt_init && "kterm_init() called twice"); - + assert(!stdout && !_vga_term.kt_init && "DosInitTerms() called twice"); + #ifndef _NO_DEBUG - _kt_vga.kt_init = TRUE; + _vga_term.kt_init = TRUE; #endif // to be switched to VESA - kt_stdout = &_kt_vga; - ktclear(); + stdout = &_vga_term; + DosClearTerm(stdout); } // // Fills terminal with spaces // XXX would '\0' work too? // -status_t kterm_clear(struct kterm *kt) +status_t DosClearTerm(terminal_t *kt) { size_t i; if (kt == NULL) return BAD_ARG_NULL; - assert(kt->kt_init && "kterm_clear called before initialization"); + assert(kt->kt_init && "DosClearTerm called before initialization"); - kterm_lock(kt); + DosLockTerm(kt); const ushort filler = ComputeEntry(' ', kt->kt_color); const size_t bufsize = kt->kt_width * kt->kt_height; for (i = 0; i < bufsize; i++) { - // XXX implement memset() + // XXX implement memsetw() kt->kt_buffer[i] = filler; } kt->kt_curr_x = kt->kt_curr_y = 0; - kterm_unlock(kt); + DosUnlockTerm(kt); return SUCCESS; } @@ -86,7 +86,7 @@ status_t kterm_clear(struct kterm *kt) // // Change the color code // -status_t kterm_change_color(struct kterm *kt, uchar color) +status_t DosChTermColor(terminal_t *kt, uchar color) { if (color > KTERM_COLOR_WHITE) return BAD_ARG_RANGE; @@ -94,9 +94,9 @@ status_t kterm_change_color(struct kterm *kt, uchar color) if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); + DosLockTerm(kt); kt->kt_color = color; - kterm_unlock(kt); + DosUnlockTerm(kt); return SUCCESS; } @@ -108,7 +108,7 @@ status_t kterm_change_color(struct kterm *kt, uchar color) // - always use kterm_putch (LOCKED version) // - doesn't check for NULL input // -void kterm_putch_unlocked(struct kterm *kt, char ch) +void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) { int i; size_t prev_row; @@ -128,7 +128,7 @@ void kterm_putch_unlocked(struct kterm *kt, char ch) for (i = 0; i < TABSIZE; i++) { // tabulations can't spread over two lines if (kt->kt_curr_y == prev_row) { - kterm_putch_unlocked(kt, ' '); + DosPutOnTerm_Unlocked(kt, ' '); } } } @@ -153,14 +153,14 @@ void kterm_putch_unlocked(struct kterm *kt, char ch) // // Writes a single character on the terminal (LOCKED version) // -status_t kterm_putch(struct kterm *kt, char ch) +status_t DosPutOnTerm(terminal_t *kt, char ch) { if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); - kterm_putch_unlocked(kt, ch); - kterm_unlock(kt); + DosLockTerm(kt); + DosPutOnTerm_Unlocked(kt, ch); + DosUnlockTerm(kt); return SUCCESS; } @@ -169,16 +169,16 @@ status_t kterm_putch(struct kterm *kt, char ch) // // Print string on terminal // -status_t kterm_print(struct kterm *kt, const char *str) +status_t DosPrintOnTerm(terminal_t *kt, const char *str) { if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); + DosLockTerm(kt); while (*str) { - kterm_putch_unlocked(kt, *str++); + DosPutOnTerm_Unlocked(kt, *str++); } - kterm_unlock(kt); + DosUnlockTerm(kt); return SUCCESS; } diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 5cb39bf..229be43 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -10,10 +10,12 @@ #ifndef _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H +#ifndef _KALCOMM_COMMON_H #include "common/common.h" +#endif // all available colors -enum kterm_color { +enum kterm_colors { KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, @@ -24,7 +26,7 @@ enum kterm_color { KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE }; -struct kterm { +typedef struct kterm { void *kt_lock; ushort *kt_buffer; uchar kt_color; @@ -32,23 +34,23 @@ struct kterm { size_t kt_height; off_t kt_curr_x; off_t kt_curr_y; + // XXX flags #ifndef _NO_DEBUG bool kt_init; #endif - -}; +} terminal_t; // current "standard" terminal -extern struct kterm *kt_stdout; +extern terminal_t *stdout; -void kterm_init(void); -status_t kterm_clear(struct kterm *); -status_t kterm_putch(struct kterm *, char); -status_t kterm_print(struct kterm *, const char *); -status_t kterm_change_color(struct kterm *, uchar); +void DosInitTerms(void); +status_t DosClearTerm(terminal_t *); +status_t DosPutOnTerm(terminal_t *, char); +status_t DosPrintOnTerm(terminal_t *, const char *); +status_t DosChTermColor(terminal_t *, uchar); #ifdef _UNLOCKED_IO -void kterm_putch_unlocked(struct kterm *, char); +void DosPutOnTerm_Unlocked(terminal_t *, char); #endif #define ktclear() kterm_clear(kt_stdout) @@ -56,9 +58,9 @@ void kterm_putch_unlocked(struct kterm *, char); #define ktprint(s) kterm_print(kt_stdout, (s)) #define ktchcol(c) kterm_change_color(kt_stdout, (c)) -#define kterm_lock(kt) -#define kterm_trylock(kt) -#define kterm_unlock(kt) +#define DosLockTerm(kt) +#define DosTryLockTerm(kt) +#define DosUnlockTerm(kt) #endif diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 04310c6..38f8f9d 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -8,6 +8,8 @@ //----------------------------------------------------------------------------// #include "kernel/ke/panic.h" +#include "kernel/ke/state.h" +#include "kernel/io/terminal.h" // // Panic message @@ -20,22 +22,24 @@ const char *panicstr = NULL; noreturn void ___assert_handler(const char *msg, const char *file, int line, const char *func) { // not getting out of here - cli(); + DosDisableInterrupts(); (void)file; (void)line; (void)func; // XXX sprintf() to create a proper panicstr - panic(msg); + DosPanic(msg); } // // Your best boy panic() // -void panic(const char *str) +void DosPanic(const char *str) { - cli(); + DosDisableInterrupts(); - ktclear(); + DosSetKernState(KSTATE_PANIC); + + DosClearTerm(stdout); if (str == NULL) { str = "(no message given)"; @@ -43,17 +47,17 @@ void panic(const char *str) if (panicstr) { // shouldn't be possible - ktprint("double panic!\n"); - hlt(); + DosPrintOnTerm(stdout, "double panic!\n"); + DosHaltCPU(); } panicstr = str; - ktprint("panic! - "); - ktprint(str); + DosPrintOnTerm(stdout, "panic! - "); + DosPrintOnTerm(stdout, str); while (TRUE) { - hlt(); + DosHaltCPU(); } } diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index 403d728..f73387a 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -10,9 +10,13 @@ #ifndef _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H -#include "kernel/io/terminal.h" +#include "kernel/ke/state.h" -extern const char *panicstr; -noreturn void panic(const char *); +extern const char *__panicstr; +noreturn void DosPanic(const char *); + +#define DosGetPanicStr() (__panicstr) + +#define panic DosPanic #endif diff --git a/src/kaleid/kernel/ke/state.c b/src/kaleid/kernel/ke/state.c new file mode 100644 index 0000000..e8a79fe --- /dev/null +++ b/src/kaleid/kernel/ke/state.c @@ -0,0 +1,16 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Current kernel state // +//----------------------------------------------------------------------------// + +#include "kernel/ke/state.h" + +// +// Current kernel state +// +uchar __kstate; + diff --git a/src/kaleid/kernel/ke/state.h b/src/kaleid/kernel/ke/state.h new file mode 100644 index 0000000..ba6bf8d --- /dev/null +++ b/src/kaleid/kernel/ke/state.h @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Current kernel state // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_KE_STATE_H +#define _KALKERN_KE_STATE_H + +#ifndef _KALCOMM_COMMON_H +#include "common/common.h" +#endif + +// XXX improve this +enum kernel_states { + // a process is running in kernel mode + KSTATE_PROCESS, + + // the kernel is not running a process + KSTATE_KERNEL, + + // the kernel is panicking + KSTATE_PANIC, + + // the kernel is booting + KSTATE_INIT, +}; + +extern uchar __kstate; + +#define DosGetKernState() (__kstate) +#define DosSetKernState(x) (__kstate = (x)) + +#endif + From d4be0f4fd5052553e5c6a9c66444ba10884c1804 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Sun, 30 Dec 2018 20:13:56 +0100 Subject: [PATCH 10/22] More stuff --- src/Makefile | 2 +- src/kaleid/common/assert.h | 2 +- src/kaleid/common/atomic.h | 2 +- src/kaleid/common/common.h | 10 +++++----- src/kaleid/common/convert.h | 2 +- src/kaleid/common/lib/convert.c | 2 +- src/kaleid/common/lib/memory.c | 2 +- src/kaleid/common/lib/sprintf.c | 2 +- src/kaleid/common/lib/status.c | 2 +- src/kaleid/common/lib/string.c | 2 +- src/kaleid/common/memory.h | 2 +- src/kaleid/common/status.h | 2 +- src/kaleid/common/string.h | 4 ++-- src/kaleid/common/sub/memory.c | 2 +- src/kaleid/common/types.h | 2 +- src/kaleid/kernel/init.c | 15 +++++++++++---- src/kaleid/kernel/init.h | 4 ++-- src/kaleid/kernel/io/ports.c | 2 +- src/kaleid/kernel/io/ports.h | 2 +- src/kaleid/kernel/io/terminal.c | 12 ++++++++++-- src/kaleid/kernel/io/terminal.h | 12 +++++++----- src/kaleid/kernel/ke/panic.c | 6 +++--- src/kaleid/kernel/ke/panic.h | 2 +- src/kaleid/kernel/ke/state.c | 2 +- src/kaleid/kernel/ke/state.h | 2 +- src/kaleid/kernel/mm/malloc.c | 2 +- src/kaleid/kernel/mm/malloc.h | 2 +- src/kaleid/linux/test-common.c | 6 +++--- src/kernel.ld | 2 +- 29 files changed, 64 insertions(+), 47 deletions(-) diff --git a/src/Makefile b/src/Makefile index 4988716..533f55f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,7 +13,7 @@ COPTIM=-O2 CLDSCR=-T kernel.ld CWARNS=-pedantic -Wall -Wextra -Werror CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -CINCLUDES=-I./kaleid +CINCLUDES=-isystem. CDEFINES= CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) diff --git a/src/kaleid/common/assert.h b/src/kaleid/common/assert.h index 8c18ad6..10b663d 100644 --- a/src/kaleid/common/assert.h +++ b/src/kaleid/common/assert.h @@ -11,7 +11,7 @@ #define _KALCOMM_ASSERT_H #ifndef _KALCOMM_COMMON_H -# error "don't include common/types.h without common/common.h" +# error "don't include kaleid/common/types.h without kaleid/common/common.h" #endif #ifdef _OSK_SOURCE diff --git a/src/kaleid/common/atomic.h b/src/kaleid/common/atomic.h index 56a4afb..569ecdb 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/atomic.h @@ -11,7 +11,7 @@ #define _KALCOMM_ATOMIC_H #ifndef _KALCOMM_COMMON_H -# error "don't include common/types.h without common/common.h" +# error "don't include kaleid/common/types.h without kaleid/common/common.h" #endif // atomic_t defined in common/types.h diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index 52c3b44..5b4dc38 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -42,13 +42,13 @@ #endif #ifdef _KALEID_KERNEL -# include "kernel/config.h" +# include #endif -#include "common/types.h" -#include "common/atomic.h" -#include "common/status.h" -#include "common/assert.h" +#include +#include +#include +#include #endif diff --git a/src/kaleid/common/convert.h b/src/kaleid/common/convert.h index 737f016..915a071 100644 --- a/src/kaleid/common/convert.h +++ b/src/kaleid/common/convert.h @@ -11,7 +11,7 @@ #define _KALCOMM_CONVERT_H #ifndef _KALCOMM_COMMON_H -#include "common/common.h" +#include #endif #ifndef _OSK_SOURCE diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/lib/convert.c index 284f61f..49b8529 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/lib/convert.c @@ -7,7 +7,7 @@ // Desc: Conversion utilities // //----------------------------------------------------------------------------// -#include "common/string.h" +#include // // Digits table for bases <=36 diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 6ef23ce..7f7c05d 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -7,7 +7,7 @@ // Desc: mem*() functions // //----------------------------------------------------------------------------// -#include "common/memory.h" +#include // // Set "bytes"-many bytes starting from ptr to val diff --git a/src/kaleid/common/lib/sprintf.c b/src/kaleid/common/lib/sprintf.c index e26ded9..b1b86c6 100644 --- a/src/kaleid/common/lib/sprintf.c +++ b/src/kaleid/common/lib/sprintf.c @@ -7,7 +7,7 @@ // Desc: sprintf()-related functions // //----------------------------------------------------------------------------// -#include "common/string.h" +#include // // Format str according to fmt using ellipsed arguments diff --git a/src/kaleid/common/lib/status.c b/src/kaleid/common/lib/status.c index 5e8115c..254e1aa 100644 --- a/src/kaleid/common/lib/status.c +++ b/src/kaleid/common/lib/status.c @@ -7,7 +7,7 @@ // Desc: Implementation of describe_status() // //----------------------------------------------------------------------------// -#include "common/common.h" +#include static const char *descriptions[] = { [-SUCCESS] = "Success", diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/lib/string.c index 464ff2e..b5178b9 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/lib/string.c @@ -7,7 +7,7 @@ // Desc: String-related functions // //----------------------------------------------------------------------------// -#include "common/string.h" +#include // TODO multibyte, assembly diff --git a/src/kaleid/common/memory.h b/src/kaleid/common/memory.h index 31f6a6b..a0f8366 100644 --- a/src/kaleid/common/memory.h +++ b/src/kaleid/common/memory.h @@ -11,7 +11,7 @@ #define _KALCOMM_MEMORY_H #ifndef _KALCOMM_COMMON_H -# include "common/common.h" +# include #endif #ifndef _OSK_SOURCE diff --git a/src/kaleid/common/status.h b/src/kaleid/common/status.h index 898c62a..7d674ff 100644 --- a/src/kaleid/common/status.h +++ b/src/kaleid/common/status.h @@ -11,7 +11,7 @@ #define _KALCOMM_STATUS_H #ifndef _KALCOMM_COMMON_H -# error "don't include common/types.h without common/common.h" +# error "don't include kaleid/common/types.h without kaleid/common/common.h" #endif #ifndef _OSK_SOURCE diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h index 61056be..5440138 100644 --- a/src/kaleid/common/string.h +++ b/src/kaleid/common/string.h @@ -12,11 +12,11 @@ #define _KALCOMM_STRING_H #ifndef _KALCOMM_COMMON_H -# include "common/common.h" +# include #endif #ifndef _KALCOMM_CONVERT_H -# include "common/convert.h" +# include #endif #ifndef _OSK_SOURCE diff --git a/src/kaleid/common/sub/memory.c b/src/kaleid/common/sub/memory.c index aaecb6b..a12a9c6 100644 --- a/src/kaleid/common/sub/memory.c +++ b/src/kaleid/common/sub/memory.c @@ -10,7 +10,7 @@ // XXX to be improved before being brought back // to be tested with more alignment sizes -#include "common/memory.h" +#include // to be moved #define QWORD_SIZE 8 diff --git a/src/kaleid/common/types.h b/src/kaleid/common/types.h index bf74580..910ceac 100644 --- a/src/kaleid/common/types.h +++ b/src/kaleid/common/types.h @@ -12,7 +12,7 @@ #define _KALCOMM_TYPES_H #ifndef _KALCOMM_COMMON_H -# error "don't include common/types.h without common/common.h" +# error "don't include kaleid/common/types.h without kaleid/common/common.h" #endif typedef unsigned char uchar; diff --git a/src/kaleid/kernel/init.c b/src/kaleid/kernel/init.c index aa794fe..5483a22 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -7,18 +7,25 @@ // Desc: Kernel entry point // //----------------------------------------------------------------------------// -#include "kernel/init.h" -#include "kernel/ke/panic.h" -#include "kernel/io/terminal.h" +#include +#include +#include // // Entry point of kaleid-kernel.elf // -void kstart(void) +void DosStartKern(void) { + // we're not ready to deal with interrupts + DosDisableInterrupts(); + + // booting! DosSetKernState(KSTATE_INIT); + // kernel terminals DosInitTerms(); + + // we're out DosPanic("Goodbye World :("); } diff --git a/src/kaleid/kernel/init.h b/src/kaleid/kernel/init.h index bb80788..37e831c 100644 --- a/src/kaleid/kernel/init.h +++ b/src/kaleid/kernel/init.h @@ -10,10 +10,10 @@ #ifndef _KALKERN_INIT_H #define _KALKERN_INIT_H -#include "common/common.h" +#include // kernel entry point -void kstart(void); +void DosStartKern(void); #endif diff --git a/src/kaleid/kernel/io/ports.c b/src/kaleid/kernel/io/ports.c index fa824a0..4f5c8fe 100644 --- a/src/kaleid/kernel/io/ports.c +++ b/src/kaleid/kernel/io/ports.c @@ -7,7 +7,7 @@ // Desc: Ports I/O // //----------------------------------------------------------------------------// -#include "kernel/io/ports.h" +#include diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index 8df790d..a92ddb6 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/src/kaleid/kernel/io/ports.h @@ -11,7 +11,7 @@ #define _KALKERN_IO_PORTS_H #ifndef _KALCOMM_COMMON_H -#include "common/common.h" +#include #endif #define DosWriteByteOnPort(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index e172daf..79da9b4 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -7,7 +7,7 @@ // Desc: Early terminal functions // //----------------------------------------------------------------------------// -#include "kernel/io/terminal.h" +#include // // VGA-related macros @@ -17,7 +17,7 @@ #define ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8) // -// VGA output +// VGA output // static terminal_t _vga_term = { .kt_buffer = (ushort *)0xB8000, @@ -37,6 +37,13 @@ static terminal_t _vga_term = { // terminal_t *stdout; +#ifndef _NO_DEBUG +// +// Debugging terminal +// +terminal_t *stddbg; +#endif + // // Initialize standard output // @@ -46,6 +53,7 @@ void DosInitTerms(void) #ifndef _NO_DEBUG _vga_term.kt_init = TRUE; + stddbg = &_vga_term; #endif // to be switched to VESA diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 229be43..63327fb 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -11,7 +11,7 @@ #define _KALKERN_IO_KTERM_H #ifndef _KALCOMM_COMMON_H -#include "common/common.h" +#include #endif // all available colors @@ -53,10 +53,12 @@ status_t DosChTermColor(terminal_t *, uchar); void DosPutOnTerm_Unlocked(terminal_t *, char); #endif -#define ktclear() kterm_clear(kt_stdout) -#define ktputch(c) kterm_putch(kt_stdout, (c)) -#define ktprint(s) kterm_print(kt_stdout, (s)) -#define ktchcol(c) kterm_change_color(kt_stdout, (c)) +#ifndef _NO_DEBUG +extern terminal_t *stddbg; +# define DebugLog(...) DosPutOnTerm(stddbg, __VA_ARGS__) +#else +# define DebugLog(...) +#endif #define DosLockTerm(kt) #define DosTryLockTerm(kt) diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 38f8f9d..028729c 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -7,9 +7,9 @@ // Desc: How NOT to panic 101 // //----------------------------------------------------------------------------// -#include "kernel/ke/panic.h" -#include "kernel/ke/state.h" -#include "kernel/io/terminal.h" +#include +#include +#include // // Panic message diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index f73387a..4b7883a 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -10,7 +10,7 @@ #ifndef _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H -#include "kernel/ke/state.h" +#include extern const char *__panicstr; noreturn void DosPanic(const char *); diff --git a/src/kaleid/kernel/ke/state.c b/src/kaleid/kernel/ke/state.c index e8a79fe..fae844d 100644 --- a/src/kaleid/kernel/ke/state.c +++ b/src/kaleid/kernel/ke/state.c @@ -7,7 +7,7 @@ // Desc: Current kernel state // //----------------------------------------------------------------------------// -#include "kernel/ke/state.h" +#include // // Current kernel state diff --git a/src/kaleid/kernel/ke/state.h b/src/kaleid/kernel/ke/state.h index ba6bf8d..7ca09a9 100644 --- a/src/kaleid/kernel/ke/state.h +++ b/src/kaleid/kernel/ke/state.h @@ -11,7 +11,7 @@ #define _KALKERN_KE_STATE_H #ifndef _KALCOMM_COMMON_H -#include "common/common.h" +#include #endif // XXX improve this diff --git a/src/kaleid/kernel/mm/malloc.c b/src/kaleid/kernel/mm/malloc.c index 5c62036..1ffa3c1 100644 --- a/src/kaleid/kernel/mm/malloc.c +++ b/src/kaleid/kernel/mm/malloc.c @@ -8,5 +8,5 @@ // Only exists to trigger Neox // //----------------------------------------------------------------------------// -#include "kernel/mm/malloc.h" +#include diff --git a/src/kaleid/kernel/mm/malloc.h b/src/kaleid/kernel/mm/malloc.h index 9d63aba..7d27929 100644 --- a/src/kaleid/kernel/mm/malloc.h +++ b/src/kaleid/kernel/mm/malloc.h @@ -11,7 +11,7 @@ #ifndef _KALKERN_MM_MALLOC_H #define _KALKERN_MM_MALLOC_H -#include "common/common.h" +#include #endif diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index edae890..b23e00a 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -11,9 +11,9 @@ #include #define KEEP_KALCOMM_TYPES_MINIMAL -#include "common/common.h" -#include "common/string.h" -#include "common/memory.h" +#include +#include +#include int main(int argc, char *argv[]) { diff --git a/src/kernel.ld b/src/kernel.ld index 86b66ea..d8fa35a 100644 --- a/src/kernel.ld +++ b/src/kernel.ld @@ -1,4 +1,4 @@ -ENTRY(kstart) +ENTRY(DosStartKern) SECTIONS { . = 0x4000; /* XXX 0x4000 is temporary */ From 31702521b10eb50e442e8b0ce48aba257acf8470 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Sun, 30 Dec 2018 22:21:19 +0100 Subject: [PATCH 11/22] Still more stuff --- src/kaleid/common/assert.h | 3 +- src/kaleid/common/common.h | 4 + src/kaleid/kernel/io/terminal.c | 128 ++++++++++++++++++-------------- src/kaleid/kernel/io/terminal.h | 51 +++++++------ src/kaleid/kernel/ke/lock.c | 13 ++++ src/kaleid/kernel/ke/lock.h | 96 ++++++++++++++++++++++++ src/kaleid/kernel/ke/panic.c | 18 +++-- src/kaleid/kernel/ke/panic.h | 7 +- 8 files changed, 226 insertions(+), 94 deletions(-) create mode 100644 src/kaleid/kernel/ke/lock.c create mode 100644 src/kaleid/kernel/ke/lock.h diff --git a/src/kaleid/common/assert.h b/src/kaleid/common/assert.h index 10b663d..f01060b 100644 --- a/src/kaleid/common/assert.h +++ b/src/kaleid/common/assert.h @@ -21,7 +21,8 @@ // uses panic() in kernel, abort() in system noreturn void ___assert_handler(const char *, const char *, int, const char *); -#define assert(x) do{if(unlikely(!(x)))___assert_handler(#x, __FILE__, __LINE__, __func__);}while(0); +#define DosAssert(x) do{if(unlikely(!(x)))___assert_handler(#x, __FILE__, __LINE__, __func__);}while(0); +#define assert DosAssert #else // not debugging diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index 5b4dc38..bc7a36f 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -41,6 +41,10 @@ # define unlikely(x) __builtin_expect((x), 0) #endif +#ifndef INITOK +# define INITOK 0xCAFEBABE +#endif + #ifdef _KALEID_KERNEL # include #endif diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index 79da9b4..b05ab48 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -7,6 +7,7 @@ // Desc: Early terminal functions // //----------------------------------------------------------------------------// +#define _UNLOCKED_IO #include // @@ -26,10 +27,8 @@ static terminal_t _vga_term = { .kt_curr_x = 0, .kt_curr_y = 0, .kt_color = ComputeColorCode(KTERM_COLOR_LGREY, KTERM_COLOR_BLACK), - .kt_lock = NULL, -#ifndef _NO_DEBUG + .kt_lock = INITLOCK(KLOCK_MUTEX), .kt_init = FALSE, -#endif }; // @@ -37,24 +36,20 @@ static terminal_t _vga_term = { // terminal_t *stdout; -#ifndef _NO_DEBUG // // Debugging terminal // terminal_t *stddbg; -#endif // // Initialize standard output // void DosInitTerms(void) { - assert(!stdout && !_vga_term.kt_init && "DosInitTerms() called twice"); + DosAssert(!stdout && _vga_term.kt_init != INITOK && "DosInitTerms() called twice"); -#ifndef _NO_DEBUG - _vga_term.kt_init = TRUE; + _vga_term.kt_init = INITOK; stddbg = &_vga_term; -#endif // to be switched to VESA stdout = &_vga_term; @@ -63,29 +58,16 @@ void DosInitTerms(void) // // Fills terminal with spaces -// XXX would '\0' work too? // status_t DosClearTerm(terminal_t *kt) { - size_t i; - if (kt == NULL) return BAD_ARG_NULL; - assert(kt->kt_init && "DosClearTerm called before initialization"); + DosAssert(kt->kt_init == INITOK); DosLockTerm(kt); - - const ushort filler = ComputeEntry(' ', kt->kt_color); - const size_t bufsize = kt->kt_width * kt->kt_height; - - for (i = 0; i < bufsize; i++) { - // XXX implement memsetw() - kt->kt_buffer[i] = filler; - } - - kt->kt_curr_x = kt->kt_curr_y = 0; - + DosClearTerm_Unlocked(kt); DosUnlockTerm(kt); return SUCCESS; @@ -102,20 +84,78 @@ status_t DosChTermColor(terminal_t *kt, uchar color) if (kt == NULL) return BAD_ARG_NULL; - DosLockTerm(kt); + DosLockTerm(kt); kt->kt_color = color; DosUnlockTerm(kt); return SUCCESS; } +// +// Writes a single character on the terminal +// +status_t DosPutOnTerm(terminal_t *kt, char ch) +{ + if (kt == NULL) + return BAD_ARG_NULL; + + DosAssert(kt->kt_init == INITOK); + + DosLockTerm(kt); + DosPutOnTerm_Unlocked(kt, ch); + DosUnlockTerm(kt); + + return SUCCESS; +} + +// +// Print string on terminal +// +status_t DosPrintOnTerm(terminal_t *kt, const char *str) +{ + if (kt == NULL) + return BAD_ARG_NULL; + + DosAssert(kt->kt_init == INITOK); + + DosLockTerm(kt); + while (*str) { + DosPutOnTerm_Unlocked(kt, *str++); + } + DosUnlockTerm(kt); + + return SUCCESS; +} + +//----------------------------------------------------------// +// UNLOCKED VERSIONS // +// // +// Direct use is highly deprecated // +// Useful in rare instances // +//----------------------------------------------------------// + +// +// Fills terminal with spaces +// XXX would '\0' work too? +// +void DosClearTerm_Unlocked(terminal_t *kt) +{ + size_t i; + + const ushort filler = ComputeEntry(' ', kt->kt_color); + const size_t bufsize = kt->kt_width * kt->kt_height; + + for (i = 0; i < bufsize; i++) { + // XXX implement memsetw() + kt->kt_buffer[i] = filler; + } + + kt->kt_curr_x = kt->kt_curr_y = 0; +} + // // Writes a single character on the terminal (UNLOCKED version) // -// DEPRECATED: -// - always use kterm_putch (LOCKED version) -// - doesn't check for NULL input -// void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) { int i; @@ -158,37 +198,15 @@ void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) } } -// -// Writes a single character on the terminal (LOCKED version) // -status_t DosPutOnTerm(terminal_t *kt, char ch) +// Print string on terminal (UNLOCKED version) +// +void DosPrintOnTerm_Unlocked(terminal_t *kt, const char *str) { - if (kt == NULL) - return BAD_ARG_NULL; - - DosLockTerm(kt); - DosPutOnTerm_Unlocked(kt, ch); - DosUnlockTerm(kt); - - return SUCCESS; -} - - -// -// Print string on terminal -// -status_t DosPrintOnTerm(terminal_t *kt, const char *str) -{ - if (kt == NULL) - return BAD_ARG_NULL; - - DosLockTerm(kt); while (*str) { DosPutOnTerm_Unlocked(kt, *str++); } - DosUnlockTerm(kt); - - return SUCCESS; } + diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 63327fb..a209ead 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -10,12 +10,10 @@ #ifndef _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H -#ifndef _KALCOMM_COMMON_H -#include -#endif +#include // all available colors -enum kterm_colors { +enum terminal_colors { KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, @@ -26,43 +24,44 @@ enum kterm_colors { KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE }; -typedef struct kterm { - void *kt_lock; - ushort *kt_buffer; - uchar kt_color; - size_t kt_width; - size_t kt_height; - off_t kt_curr_x; - off_t kt_curr_y; +typedef struct { + lock_t kt_lock; + ushort *kt_buffer; + uchar kt_color; + size_t kt_width; + size_t kt_height; + off_t kt_curr_x; + off_t kt_curr_y; + uint kt_init; // XXX flags -#ifndef _NO_DEBUG - bool kt_init; -#endif } terminal_t; // current "standard" terminal extern terminal_t *stdout; -void DosInitTerms(void); -status_t DosClearTerm(terminal_t *); -status_t DosPutOnTerm(terminal_t *, char); -status_t DosPrintOnTerm(terminal_t *, const char *); -status_t DosChTermColor(terminal_t *, uchar); +void DosInitTerms(void); +status_t DosClearTerm(terminal_t *); +status_t DosPutOnTerm(terminal_t *, char); +status_t DosPrintOnTerm(terminal_t *, const char *); +status_t DosChTermColor(terminal_t *, uchar); -#ifdef _UNLOCKED_IO -void DosPutOnTerm_Unlocked(terminal_t *, char); +#ifdef _UNLOCKED_IO +void DosClearTerm_Unlocked(terminal_t *); +void DosPutOnTerm_Unlocked(terminal_t *, char); +void DosPrintOnTerm_Unlocked(terminal_t *kt, const char *str); +#define DosChTermColor_Unlocked(kt, col) ((kt)->kt_color = col) #endif #ifndef _NO_DEBUG extern terminal_t *stddbg; -# define DebugLog(...) DosPutOnTerm(stddbg, __VA_ARGS__) +# define DebugLog(...) DosPutOnTerm(stddbg, __VA_ARGS__) #else # define DebugLog(...) #endif -#define DosLockTerm(kt) -#define DosTryLockTerm(kt) -#define DosUnlockTerm(kt) +#define DosLockTerm(kt) DosAquireLock(&kt->kt_lock) +#define DosUnlockTerm(kt) DosReleaseLock(&kt->kt_lock) +#define DosTryLockTerm(kt) DosAttemptLock(&kt->kt_lock) #endif diff --git a/src/kaleid/kernel/ke/lock.c b/src/kaleid/kernel/ke/lock.c new file mode 100644 index 0000000..544bf93 --- /dev/null +++ b/src/kaleid/kernel/ke/lock.c @@ -0,0 +1,13 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Locks // +//----------------------------------------------------------------------------// + +#include + +// nothing to do here + diff --git a/src/kaleid/kernel/ke/lock.h b/src/kaleid/kernel/ke/lock.h new file mode 100644 index 0000000..79202d4 --- /dev/null +++ b/src/kaleid/kernel/ke/lock.h @@ -0,0 +1,96 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Locks // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_KE_LOCK_H +#define _KALKERN_KE_LOCK_H + +#ifndef _KALCOMM_COMMON_H +#include +#endif + +enum lock_type { + // + // Mutex-type lock + // WARNING: DosLock() panics when used on a mutex while not running a process + // + KLOCK_MUTEX, + + // + // Spinlock-type lock + // Turns into a Mutex-type lock when MULTIPROCESSOR is off + // + KLOCK_SPINLOCK, + +}; + +typedef struct { + uchar lk_type; // lock type? + uint lk_lock; // is locked? + void *lk_owner; // unused + void *lk_waiting; // unused + uint lk_init; // unused if _NO_DEBUG +} lock_t; + +// +// Initialize a lock +// +#define DosInitLock(lk, type) \ + do { \ + (lk)->lk_type = (type); \ + (lk)->lk_lock = FALSE; \ + (lk)->lk_owner = NULL; \ + (lk)->lk_waiting = NULL; \ + (lk)->lk_init = INITOK; \ + } while (FALSE); + +// +// Alternative way to initalize a lock +// +#define INITLOCK(type) { (type), FALSE, NULL, NULL, INITOK } + +// +// Does nothing +// +#define DosDestroyLock(lk) + +// +// Aquires the lock +// Panics on double aquisition since that should never happen +// until we have at least a basic scheduler +// +#define DosAquireLock(lk) \ + do { \ + DosDisableInterrupts(); \ + DosAssert((lk)->lk_init == INITOK); \ + if ((lk)->lk_lock++) \ + DosPanic("DosAquireLock on an already locked object"); \ + DosEnableInterrupts(); \ + } while (FALSE); + +// +// Releases an already aquired lock +// Panics if the lock was never aquired (this will change) +// +#define DosReleaseLock(lk) \ + do { \ + DosDisableInterrupts(); \ + DosAssert((lk)->lk_init == INITOK); \ + if ((lk)->lk_lock++) \ + DosPanic("DosReleased on an unlocked object"); \ + DosEnableInterrupts(); \ + } while (FALSE); + +// +// Tries to aquire lock +// Doesn't work at all for obvious reasons +// +#define DosAttemptLock(lk) ((lk)->lk_lock++) + +#endif + diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 028729c..e319fe6 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -9,12 +9,14 @@ #include #include + +#define _UNLOCKED_IO #include // // Panic message // -const char *panicstr = NULL; +const char *__panicmsg = NULL; // // Failed assert() handler @@ -39,22 +41,22 @@ void DosPanic(const char *str) DosSetKernState(KSTATE_PANIC); - DosClearTerm(stdout); + DosClearTerm_Unlocked(stdout); if (str == NULL) { str = "(no message given)"; } - if (panicstr) { - // shouldn't be possible - DosPrintOnTerm(stdout, "double panic!\n"); + if (DosGetPanicStr()) { + DosPrintOnTerm_Unlocked(stdout, "double panic!\n"); DosHaltCPU(); } - panicstr = str; + DosSetPanicStr(str); - DosPrintOnTerm(stdout, "panic! - "); - DosPrintOnTerm(stdout, str); + // we cannot lock anything when panicking + DosPrintOnTerm_Unlocked(stdout, "panic! - "); + DosPrintOnTerm_Unlocked(stdout, str); while (TRUE) { DosHaltCPU(); diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index 4b7883a..91cc78e 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -12,11 +12,10 @@ #include -extern const char *__panicstr; noreturn void DosPanic(const char *); -#define DosGetPanicStr() (__panicstr) - -#define panic DosPanic +extern const char *__panicmsg; +#define DosGetPanicStr() (__panicmsg) +#define DosSetPanicStr(str) (__panicmsg = (str)) #endif From 680ca2a52cbb2caf2f302121da7c07880bc08857 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Sun, 30 Dec 2018 22:42:19 +0100 Subject: [PATCH 12/22] Minor stuff --- src/kaleid/common/common.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index bc7a36f..45943dd 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -15,17 +15,17 @@ #endif #if !defined(TRUE) && !defined(FALSE) -# define TRUE 1 -# define FALSE 0 +# define TRUE (1) +# define FALSE (0) #endif #ifdef _OSK_SOURCE -# define YES 1 -# define NO 0 +# define YES (1) +# define NO (0) #endif #ifndef NULL -# define NULL ((void*)0) +# define NULL ((void *)0) #endif #ifndef PACKED @@ -42,7 +42,7 @@ #endif #ifndef INITOK -# define INITOK 0xCAFEBABE +# define INITOK ((unsigned int)0xCAFEBABE) #endif #ifdef _KALEID_KERNEL From 15a7ad873f530017e7605055c4c399de398cc857 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 31 Dec 2018 10:49:08 +0100 Subject: [PATCH 13/22] Common stuff --- src/Makefile | 24 ++-- src/kaleid/common/assert.h | 52 -------- src/kaleid/common/common.h | 128 +++++++++++++++++-- src/kaleid/common/convert.h | 24 ---- src/kaleid/common/folder.desc | 52 ++++---- src/kaleid/common/lib/convert.c | 2 +- src/kaleid/common/lib/memory.c | 60 +++++++-- src/kaleid/common/{atomic.h => lib/memsub.c} | 42 +++--- src/kaleid/common/lib/sprintf.c | 2 +- src/kaleid/common/lib/string.c | 4 +- src/kaleid/common/memory.h | 28 ---- src/kaleid/common/status.h | 41 ------ src/kaleid/common/stdlib.h | 75 +++++++++++ src/kaleid/common/string.h | 49 ------- src/kaleid/common/sub/memory.c | 79 ------------ src/kaleid/common/types.h | 44 ------- src/kaleid/kernel/folder.desc | 7 +- src/kaleid/kernel/init.c | 1 + src/kaleid/kernel/init.h | 2 + src/kaleid/kernel/io/terminal.c | 19 ++- src/kaleid/kernel/io/terminal.h | 6 +- src/kaleid/kernel/ke/lock.h | 8 +- src/kaleid/kernel/ke/panic.c | 5 +- src/kaleid/kernel/ke/panic.h | 5 +- src/kaleid/kernel/ke/state.h | 1 - 25 files changed, 346 insertions(+), 414 deletions(-) delete mode 100644 src/kaleid/common/assert.h delete mode 100644 src/kaleid/common/convert.h rename src/kaleid/common/{atomic.h => lib/memsub.c} (53%) delete mode 100644 src/kaleid/common/memory.h delete mode 100644 src/kaleid/common/status.h create mode 100644 src/kaleid/common/stdlib.h delete mode 100644 src/kaleid/common/string.h delete mode 100644 src/kaleid/common/sub/memory.c delete mode 100644 src/kaleid/common/types.h diff --git a/src/Makefile b/src/Makefile index 533f55f..d4e80b8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,10 +12,13 @@ CC2NAME=gcc COPTIM=-O2 CLDSCR=-T kernel.ld CWARNS=-pedantic -Wall -Wextra -Werror -CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 CINCLUDES=-isystem. CDEFINES= +CFLAGS1=-nostdlib -ffreestanding -mcmodel=large +CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CFLAGS=$(CFLAGS1) $(CFLAGS2) + CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) KCC=$(CC) -D_KALEID_KERNEL @@ -32,7 +35,7 @@ KERNDIR=kaleid/kernel SYSTDIR=kaleid/system LINXDIR=kaleid/linux -all: bootloader +all: bootloader kernel boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc $(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin @@ -59,10 +62,10 @@ testing: bootloader pseudo_kern COBJDIR=$(OBJDIR)/$(COMMDIR) LOBJDIR=$(OBJDIR)/$(LINXDIR) -COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(KERNDIR)/config.h \ - $(COMMDIR)/status.h +COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/stdlib.h $(KERNDIR)/config.h -COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o $(COBJDIR)/lib/convert.o $(COBJDIR)/lib/memory.o +COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o \ + $(COBJDIR)/lib/convert.o $(COBJDIR)/lib/memory.o common: $(COMMDEPS) $(COMMDIR)/lib/string.c $(COMMDIR)/lib/status.c $(KCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o @@ -85,9 +88,14 @@ test-common: KOBJDIR=$(OBJDIR)/$(KERNDIR) -KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h -KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/state.c $(KERNDIR)/ke/panic.c $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c -KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/state.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o +KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h \ + $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h + +KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/state.c $(KERNDIR)/ke/panic.c \ + $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c + +KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/state.o $(KOBJDIR)/ke/panic.o \ + $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o kernel: common $(KERNSRCS) $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o diff --git a/src/kaleid/common/assert.h b/src/kaleid/common/assert.h deleted file mode 100644 index f01060b..0000000 --- a/src/kaleid/common/assert.h +++ /dev/null @@ -1,52 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Assertions // -//----------------------------------------------------------------------------// - -#ifndef _KALCOMM_ASSERT_H -#define _KALCOMM_ASSERT_H - -#ifndef _KALCOMM_COMMON_H -# error "don't include kaleid/common/types.h without kaleid/common/common.h" -#endif - -#ifdef _OSK_SOURCE - -#if !defined(_NO_DEBUG) && !defined(NDEBUG) - -// uses panic() in kernel, abort() in system -noreturn void ___assert_handler(const char *, const char *, int, const char *); - -#define DosAssert(x) do{if(unlikely(!(x)))___assert_handler(#x, __FILE__, __LINE__, __func__);}while(0); -#define assert DosAssert - -#else // not debugging - -#if !defined(NDEBUG) -# define NDEBUG 1 -#endif - -#if !defined(_NO_DEBUG) -# define _NO_DEBUG 1 -#endif - -#define assert(x) - -#endif - -#else // !defined(_OSK_SOURCE) - -#if defined(_NO_DEBUG) && !defined(NDEBUG) -# define NDEBUG 1 -#endif - -#include - -#endif - -#endif - diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index 45943dd..121a50f 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -10,8 +10,14 @@ #ifndef _KALCOMM_COMMON_H #define _KALCOMM_COMMON_H -#if !defined(_OSK_SOURCE) && (defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM)) -# define _OSK_SOURCE 1 +//------------------------------------------// +// PREPROCESSOR CONSTANTS // +//------------------------------------------// + +#if !defined(_OSK_SOURCE) +# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) +# define _OSK_SOURCE 1 +# endif #endif #if !defined(TRUE) && !defined(FALSE) @@ -37,8 +43,8 @@ #endif #if !defined(likely) && !defined(unlikely) -# define likely(x) __builtin_expect((x), 1) -# define unlikely(x) __builtin_expect((x), 0) +# define likely(x) (__builtin_expect((x), 1)) +# define unlikely(x) (__builtin_expect((x), 0)) #endif #ifndef INITOK @@ -49,10 +55,116 @@ # include #endif -#include -#include -#include -#include +//------------------------------------------// +// COMMON TYPES // +//------------------------------------------// + +typedef unsigned char uchar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef long long llong; +typedef unsigned long long ullong; +typedef long double ldouble; +typedef short port_t; +typedef short status_t; + +#ifndef KEEP_KALCOMM_TYPES_MINIMAL +typedef _Bool bool; +typedef uint wchar_t; +typedef ullong size_t; +typedef llong ssize_t; +typedef size_t off_t; +typedef int atomic_t; +typedef ulong pid_t; +typedef void *va_list; +#endif + +// XXX limits + +//------------------------------------------// +// VALUES FOR "status_t" // +//------------------------------------------// + +#ifndef _OSK_SOURCE +# define describe_status _osk_describe_status +#endif + +// see in common/lib/status.c for status messages +const char *describe_status(status_t); + +#define STATUS_FAILED(x) ((x) < 0)) +#define STATUS_SUCCESS(x) (!STATUS_FAILED(x)) + +#define SUCCESS (0) // everything went fine + +#define FAILED (-1) +#define ERROR FAILED // something went wrong +#define NOT_PERMITTED (-2) +#define ACCESS_DENIED (-3) + +#define BAD_ARGUMENT (-4) // invalid arguments +#define BAD_ARG_RANGE (-5) // arguments out of range +#define BAD_ARG_NULL (-6) // unexpected NULL argument + +#define TRY_AGAIN (-7) // EAGAIN + +//------------------------------------------// +// INLINE ASM MACROS // +//------------------------------------------// + + +#ifdef _KALEID_KERNEL +# define DosDisableInterrupts() asm volatile ("cli") +# define DosEnableInterrupts() asm volatile ("sti") +# define DosHaltCPU() asm volatile ("hlt") +#endif + +//------------------------------------------// +// assert()/DosAssert() SUPPORT // +//------------------------------------------// + +#ifdef _OSK_SOURCE + +#if !defined(_NO_DEBUG) && !defined(NDEBUG) + +// uses panic() in kernel, abort() in system +noreturn void ___assert_handler(const char *, const char *, int, const char *); + +#define DosAssert(x) \ + do { \ + if unlikely(!(x)) \ + ___assert_handler(#x, __FILE__, __LINE__, __func__); \ + } while (FALSE); +#define assert DosAssert + +#else // not debugging + +#if !defined(NDEBUG) +# define NDEBUG 1 +#endif + +#if !defined(_NO_DEBUG) +# define _NO_DEBUG 1 +#endif + +#define assert(x) + +#endif + +#else // !defined(_OSK_SOURCE) + +#if defined(_NO_DEBUG) && !defined(NDEBUG) +# define NDEBUG 1 +#endif + +#include + +#endif + +//------------------------------------------// +// END OF "kaleid/common/common.h" // +//------------------------------------------// #endif diff --git a/src/kaleid/common/convert.h b/src/kaleid/common/convert.h deleted file mode 100644 index 915a071..0000000 --- a/src/kaleid/common/convert.h +++ /dev/null @@ -1,24 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Conversion utilities // -//----------------------------------------------------------------------------// - -#ifndef _KALCOMM_CONVERT_H -#define _KALCOMM_CONVERT_H - -#ifndef _KALCOMM_COMMON_H -#include -#endif - -#ifndef _OSK_SOURCE -# define itoa _osk_itoa -# define atoi _osk_atoi -#endif - -char *itoa(int, char *, int); - -#endif diff --git a/src/kaleid/common/folder.desc b/src/kaleid/common/folder.desc index ab5d8cc..124f60c 100644 --- a/src/kaleid/common/folder.desc +++ b/src/kaleid/common/folder.desc @@ -7,35 +7,31 @@ Desc: Folder description - "kaleid/common" --------------------------------------------------------------------- -This is the folder containing the sources for Kaleid's C runtime library, linked both to the kernel -and to the system processes that run outiside the kernel. It can also be compiled for Linux as -(very basic) C library, for test purposes. +This is the folder containing the sources for Kaleid's C runtime library, linked +both to the kernel and to the system processes that run outiside the kernel. +It can also be compiled for Linux as a very basic C library, for test purposes. This folder contains the following files: - common.h - This file is to be included by every source file using the library, and it includes in turn - the following files: - - assert.h - Defines the macro "assert()". Currently any program wanting to use this macro has to - implement its own "___assert_handler(const char *cond, const char *file, int line, const char *func)" - but an (overridable) default handler will be furnished in the future. - - atomic.h - Support for atomic operations. When compiled by the kernel, also furnishes macros for - enabling/disabling interrupts. This will change in the future. - - status.h - Defines the different values of the "status_t" type used throughout Kaleid and related - utilitary functions. - - types.h - Provides the elementary types (size_t, etc) used throught Kaleid. - - - convert.h - Contains the declaration of conversion utilities (e.g. itoa()) - It is included by string.h - - - memory.h - Contains the declaration of the mem*() family of utilities (e.g. memcpy()) - - - string.h - Contains the declaration of various string-related utilities (including the sprintf() family) - Includes convert.h + This file is to be included by every source file using the library. + It: + - Provides the elementary types (e.g. size_t) used through Kaleid. + + - Defines the different values of the "status_t" type used + throughout Kaleid and related utilitary functions. + + - Defines the macro "assert()". Currently any program wanting to + use this macro has to implement its own handler: + noreturn void ___assert_handler(const char *cond, + const char *file, + int line, + const char *func) + but an overridable default handler will be furnished later. + + - stlib.h + The include file for most of functions of the common library, notably + - Memory management utilities (e.g. memset()) + - String manipulation utilities (e.g. strcpy()) + - Type conversion utilities (e.g. itoa()) + diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/lib/convert.c index 49b8529..9a9f037 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/lib/convert.c @@ -7,7 +7,7 @@ // Desc: Conversion utilities // //----------------------------------------------------------------------------// -#include +#include // // Digits table for bases <=36 diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 7f7c05d..7d40011 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -7,17 +7,64 @@ // Desc: mem*() functions // //----------------------------------------------------------------------------// -#include +// XXX to be improved before being brought back +// to be tested with more alignment sizes + +#include + +// to be moved +#define QWORD_SIZE 8 +#define QWORD_ALIGN 8 + +// +// Set "qwords"-many aligned qwords starting from ptr to val +// +static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +{ + size_t n; + ullong *uptr = (ullong *)ptr; + + // aligned memory write + for (n = 0; n < qwords; n++) { + *(uptr + n) = uval; + } + + return ptr; +} // // Set "bytes"-many bytes starting from ptr to val // void *memset(void *ptr, int val, size_t bytes) { - uchar uval = val & 0xFF; uchar *uptr = (uchar *)ptr; + const size_t qwords = bytes/QWORD_SIZE; - while (bytes--) *uptr++ = uval; + // get rid of everything after the first byte + val = val & 0xFF; + + // deal with bytes before start of the first aligned qword + while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // move qword by qword + if (qwords) { + const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) + | ((ullong)val << 40) | ((ullong)val << 32) + | ((ullong)val << 24) | ((ullong)val << 16) + | ((ullong)val << 8) | ((ullong)val); + + memsetq(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE; + bytes %= QWORD_SIZE; + } + + // deal with what's left + while (bytes--) { + *uptr++ = (uchar)val; + } return ptr; } @@ -27,11 +74,6 @@ void *memset(void *ptr, int val, size_t bytes) // void *memzero(void *ptr, size_t bytes) { - uchar *uptr = (uchar *)ptr; - - while (bytes--) *uptr++ = 0; - - return ptr; + return memset(ptr, 0, bytes); } - diff --git a/src/kaleid/common/atomic.h b/src/kaleid/common/lib/memsub.c similarity index 53% rename from src/kaleid/common/atomic.h rename to src/kaleid/common/lib/memsub.c index 569ecdb..8b25667 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/lib/memsub.c @@ -4,26 +4,34 @@ // Authors: spectral` // // NeoX // // // -// Desc: Atomic stuff // +// Desc: mem*() functions, suboptimal edition // //----------------------------------------------------------------------------// -#ifndef _KALCOMM_ATOMIC_H -#define _KALCOMM_ATOMIC_H +#include -#ifndef _KALCOMM_COMMON_H -# error "don't include kaleid/common/types.h without kaleid/common/common.h" -#endif +// +// Set "bytes"-many bytes starting from ptr to val +// +void *memset(void *ptr, int val, size_t bytes) +{ + uchar uval = val & 0xFF; + uchar *uptr = (uchar *)ptr; + + while (bytes--) *uptr++ = uval; + + return ptr; +} -// atomic_t defined in common/types.h +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + uchar *uptr = (uchar *)ptr; + + while (bytes--) *uptr++ = 0; + + return ptr; +} -#ifdef _KALEID_KERNEL - -// only available in the kernel -#define DosDisableInterrupts() asm volatile ("cli") -#define DosEnableInterrupts() asm volatile ("sti") -#define DosHaltCPU() asm volatile ("hlt") - -#endif - -#endif diff --git a/src/kaleid/common/lib/sprintf.c b/src/kaleid/common/lib/sprintf.c index b1b86c6..c8cbfa8 100644 --- a/src/kaleid/common/lib/sprintf.c +++ b/src/kaleid/common/lib/sprintf.c @@ -7,7 +7,7 @@ // Desc: sprintf()-related functions // //----------------------------------------------------------------------------// -#include +#include // // Format str according to fmt using ellipsed arguments diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/lib/string.c index b5178b9..4bab621 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/lib/string.c @@ -7,9 +7,7 @@ // Desc: String-related functions // //----------------------------------------------------------------------------// -#include - -// TODO multibyte, assembly +#include // // Returns str's length diff --git a/src/kaleid/common/memory.h b/src/kaleid/common/memory.h deleted file mode 100644 index a0f8366..0000000 --- a/src/kaleid/common/memory.h +++ /dev/null @@ -1,28 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: mem*() functions // -//----------------------------------------------------------------------------// - -#ifndef _KALCOMM_MEMORY_H -#define _KALCOMM_MEMORY_H - -#ifndef _KALCOMM_COMMON_H -# include -#endif - -#ifndef _OSK_SOURCE -# define memcpy _osk_memcpy -# define memset _osk_memset -# define memcmp _osk_memcmp -# define memzero _osk_memzero -#endif - -void *memset(void *, int, size_t); -void *memzero(void *, size_t); - -#endif - diff --git a/src/kaleid/common/status.h b/src/kaleid/common/status.h deleted file mode 100644 index 7d674ff..0000000 --- a/src/kaleid/common/status.h +++ /dev/null @@ -1,41 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Values for status_t // -//----------------------------------------------------------------------------// - -#ifndef _KALCOMM_STATUS_H -#define _KALCOMM_STATUS_H - -#ifndef _KALCOMM_COMMON_H -# error "don't include kaleid/common/types.h without kaleid/common/common.h" -#endif - -#ifndef _OSK_SOURCE -# define describe_status _osk_describe_status -#endif - -// see in common/lib/status.c for status messages -const char *describe_status(status_t); - -#define STATUS_FAILED(x) ((x) < 0)) -#define STATUS_SUCCESS(x) (!STATUS_FAILED(x)) - -#define SUCCESS (0) // everything went fine - -#define FAILED (-1) -#define ERROR FAILED // something went wrong, can't be more precise -#define NOT_PERMITTED (-2) -#define ACCESS_DENIED (-3) - -#define BAD_ARGUMENT (-4) // invalid arguments, can't be more precise -#define BAD_ARG_RANGE (-5) // arguments out of range -#define BAD_ARG_NULL (-6) // unexpected NULL argument - -#define TRY_AGAIN (-7) // EAGAIN - -#endif - diff --git a/src/kaleid/common/stdlib.h b/src/kaleid/common/stdlib.h new file mode 100644 index 0000000..e267a4e --- /dev/null +++ b/src/kaleid/common/stdlib.h @@ -0,0 +1,75 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid string library // +//----------------------------------------------------------------------------// + + +#ifndef _KALCOMM_STRING_H +#define _KALCOMM_STRING_H + +#ifndef _KALCOMM_COMMON_H +# include +#endif + +//------------------------------------------// +// Memory management utilitaries // +//------------------------------------------// + +#ifndef _OSK_SOURCE +# define memcpy _osk_memcpy +# define memset _osk_memset +# define memcmp _osk_memcmp +# define memzero _osk_memzero +#endif + +void *memset(void *, int, size_t); +void *memzero(void *, size_t); + +//------------------------------------------// +// String manipulation utilitaries // +//------------------------------------------// + +#ifndef _OSK_SOURCE +# define strlen _osk_strlen +# define strcpy _osk_strcpy +# define strncpy _osk_strncpy +# define strrev _osk_strrev +# define reverse _osk_reverse +# define sprintf _osk_sprintf +# define snprintf _osk_snprintf +# define vsprintf _osk_vsprintf +# define vsnprintf _osk_vsnprintf +#endif + +size_t strlen(const char *); +char *strcpy(char *, const char *); +char *strncpy(char *, const char *, size_t); +char *strrev(char *dest, const char *src); +char *reverse(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 // +//------------------------------------------// + +#ifndef _OSK_SOURCE +# define itoa _osk_itoa +# define atoi _osk_atoi +#endif + +char *itoa(int, char *, int); + +//------------------------------------------// +// End of kaleid/common/stdlib.h // +//------------------------------------------// + +#endif + diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h deleted file mode 100644 index 5440138..0000000 --- a/src/kaleid/common/string.h +++ /dev/null @@ -1,49 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Kaleid string library // -//----------------------------------------------------------------------------// - - -#ifndef _KALCOMM_STRING_H -#define _KALCOMM_STRING_H - -#ifndef _KALCOMM_COMMON_H -# include -#endif - -#ifndef _KALCOMM_CONVERT_H -# include -#endif - -#ifndef _OSK_SOURCE - -# define strlen _osk_strlen -# define strcpy _osk_strcpy -# define strncpy _osk_strncpy -# define strrev _osk_strrev -# define reverse _osk_reverse - -# define sprintf _osk_sprintf -# define snprintf _osk_snprintf -# define vsprintf _osk_vsprintf -# define vsnprintf _osk_vsnprintf - -#endif - -size_t strlen(const char *); -char *strcpy(char *, const char *); -char *strncpy(char *, const char *, size_t); -char *strrev(char *dest, const char *src); -char *reverse(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); - -#endif - diff --git a/src/kaleid/common/sub/memory.c b/src/kaleid/common/sub/memory.c deleted file mode 100644 index a12a9c6..0000000 --- a/src/kaleid/common/sub/memory.c +++ /dev/null @@ -1,79 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: mem*() functions // -//----------------------------------------------------------------------------// - -// XXX to be improved before being brought back -// to be tested with more alignment sizes - -#include - -// to be moved -#define QWORD_SIZE 8 -#define QWORD_ALIGN 8 - -// -// Set "qwords"-many aligned qwords starting from ptr to val -// -static inline void *memsetq(void *ptr, ullong uval, size_t qwords) -{ - size_t n; - ullong *uptr = (ullong *)ptr; - - // aligned memory write - for (n = 0; n < qwords; n++) { - *(uptr + n) = uval; - } - - return ptr; -} - -// -// Set "bytes"-many bytes starting from ptr to val -// -void *memset(void *ptr, int val, size_t bytes) -{ - uchar *uptr = (uchar *)ptr; - const size_t qwords = bytes/QWORD_SIZE; - - // get rid of everything after the first byte - val = val & 0xFF; - - // deal with bytes before start of the first aligned qword - while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { - *uptr++ = (uchar)val; - } - - // move qword by qword - if (qwords) { - const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) - | ((ullong)val << 40) | ((ullong)val << 32) - | ((ullong)val << 24) | ((ullong)val << 16) - | ((ullong)val << 8) | ((ullong)val); - - memsetq(uptr, uval, qwords); - - uptr += qwords * QWORD_SIZE; - bytes %= QWORD_SIZE; - } - - // deal with what's left - while (bytes--) { - *uptr++ = (uchar)val; - } - - return ptr; -} - -// -// Set "bytes"-many bytes starting from ptr to 0 -// -void *memzero(void *ptr, size_t bytes) -{ - return memset(ptr, 0, bytes); -} - diff --git a/src/kaleid/common/types.h b/src/kaleid/common/types.h deleted file mode 100644 index 910ceac..0000000 --- a/src/kaleid/common/types.h +++ /dev/null @@ -1,44 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Essential types for Kaleid // -//----------------------------------------------------------------------------// - - -#ifndef _KALCOMM_TYPES_H -#define _KALCOMM_TYPES_H - -#ifndef _KALCOMM_COMMON_H -# error "don't include kaleid/common/types.h without kaleid/common/common.h" -#endif - -typedef unsigned char uchar; -typedef unsigned short ushort; -typedef unsigned int uint; -typedef unsigned long ulong; -typedef long long llong; -typedef unsigned long long ullong; -typedef long double ldouble; -typedef short port_t; -typedef short status_t; - -#ifndef KEEP_KALCOMM_TYPES_MINIMAL -typedef _Bool bool; -typedef uint wchar_t; -typedef ullong size_t; -typedef llong ssize_t; -typedef size_t off_t; -typedef int atomic_t; -typedef ulong pid_t; -typedef void *va_list; -#endif - -// XXX limits - -#endif - - - diff --git a/src/kaleid/kernel/folder.desc b/src/kaleid/kernel/folder.desc index 039b5af..e85aabc 100644 --- a/src/kaleid/kernel/folder.desc +++ b/src/kaleid/kernel/folder.desc @@ -26,11 +26,12 @@ This folder also has the following subfolders: I/O folder. (XXX) - ps/ - This folder will contain Kaleid's process manager and scheduler, as well as the - implementation of the related syscalls and functions. + This folder will contain Kaleid's process manager and scheduler, as well + as the implementation of the related syscalls and functions. - ex/ This folder contains the exec()-related functions and syscalls, as well - as one subfolder per executable format supported by Kaleid (e.g. ELF executable) + as one subfolder per executable format supported by Kaleid + (e.g. ELF executable) diff --git a/src/kaleid/kernel/init.c b/src/kaleid/kernel/init.c index 5483a22..e4d0450 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -8,6 +8,7 @@ //----------------------------------------------------------------------------// #include +#include #include #include diff --git a/src/kaleid/kernel/init.h b/src/kaleid/kernel/init.h index 37e831c..40351c2 100644 --- a/src/kaleid/kernel/init.h +++ b/src/kaleid/kernel/init.h @@ -10,7 +10,9 @@ #ifndef _KALKERN_INIT_H #define _KALKERN_INIT_H +#ifndef _KALCOMM_COMMON_H #include +#endif // kernel entry point void DosStartKern(void); diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index b05ab48..ce3ebd8 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -14,7 +14,7 @@ // VGA-related macros // #define ComputeColorCode(fg, bg) ((fg) | (bg) << 4) -#define ComputeEntryOffset(kt, x, y) ((y) * kt->kt_width + (x)) +#define ComputeOffset(kt, x, y) ((y) * kt->kt_width + (x)) #define ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8) // @@ -46,7 +46,7 @@ terminal_t *stddbg; // void DosInitTerms(void) { - DosAssert(!stdout && _vga_term.kt_init != INITOK && "DosInitTerms() called twice"); + DosAssert(!stdout && _vga_term.kt_init != INITOK); _vga_term.kt_init = INITOK; stddbg = &_vga_term; @@ -57,7 +57,7 @@ void DosInitTerms(void) } // -// Fills terminal with spaces +// Fill terminal with spaces // status_t DosClearTerm(terminal_t *kt) { @@ -92,7 +92,7 @@ status_t DosChTermColor(terminal_t *kt, uchar color) } // -// Writes a single character on the terminal +// Write a single character on the terminal // status_t DosPutOnTerm(terminal_t *kt, char ch) { @@ -135,7 +135,7 @@ status_t DosPrintOnTerm(terminal_t *kt, const char *str) //----------------------------------------------------------// // -// Fills terminal with spaces +// Fill terminal with spaces (UNLOCKED version) // XXX would '\0' work too? // void DosClearTerm_Unlocked(terminal_t *kt) @@ -154,7 +154,7 @@ void DosClearTerm_Unlocked(terminal_t *kt) } // -// Writes a single character on the terminal (UNLOCKED version) +// Write a single character on the terminal (UNLOCKED version) // void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) { @@ -180,10 +180,9 @@ void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) } } } - - // XXX check whether we were given a writable character + else { - const size_t offset = ComputeEntryOffset(kt, kt->kt_curr_x, kt->kt_curr_y); + const size_t offset = ComputeOffset(kt, kt->kt_curr_x, kt->kt_curr_y); kt->kt_buffer[offset] = ComputeEntry(ch, kt->kt_color); } @@ -208,5 +207,3 @@ void DosPrintOnTerm_Unlocked(terminal_t *kt, const char *str) } } - - diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index a209ead..0826453 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -39,6 +39,9 @@ typedef struct { // current "standard" terminal extern terminal_t *stdout; +// current debugging terminal +extern terminal_t *stddbg; + void DosInitTerms(void); status_t DosClearTerm(terminal_t *); status_t DosPutOnTerm(terminal_t *, char); @@ -48,12 +51,11 @@ status_t DosChTermColor(terminal_t *, uchar); #ifdef _UNLOCKED_IO void DosClearTerm_Unlocked(terminal_t *); void DosPutOnTerm_Unlocked(terminal_t *, char); -void DosPrintOnTerm_Unlocked(terminal_t *kt, const char *str); +void DosPrintOnTerm_Unlocked(terminal_t *, const char *); #define DosChTermColor_Unlocked(kt, col) ((kt)->kt_color = col) #endif #ifndef _NO_DEBUG -extern terminal_t *stddbg; # define DebugLog(...) DosPutOnTerm(stddbg, __VA_ARGS__) #else # define DebugLog(...) diff --git a/src/kaleid/kernel/ke/lock.h b/src/kaleid/kernel/ke/lock.h index 79202d4..1174c26 100644 --- a/src/kaleid/kernel/ke/lock.h +++ b/src/kaleid/kernel/ke/lock.h @@ -17,7 +17,9 @@ enum lock_type { // // Mutex-type lock - // WARNING: DosLock() panics when used on a mutex while not running a process + // + // WARNING + // DosLock() panics when used on a mutex while not running a process // KLOCK_MUTEX, @@ -57,7 +59,7 @@ typedef struct { // // Does nothing // -#define DosDestroyLock(lk) +#define DosDestroyLock(lk) ((lk)->lk_init = FALSE) // // Aquires the lock @@ -82,7 +84,7 @@ typedef struct { DosDisableInterrupts(); \ DosAssert((lk)->lk_init == INITOK); \ if ((lk)->lk_lock++) \ - DosPanic("DosReleased on an unlocked object"); \ + DosPanic("DosReleaseLock on an unlocked object"); \ DosEnableInterrupts(); \ } while (FALSE); diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index e319fe6..633cbf7 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -21,7 +21,10 @@ const char *__panicmsg = NULL; // // 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) { // not getting out of here DosDisableInterrupts(); diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index 91cc78e..27a7753 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -10,9 +10,12 @@ #ifndef _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H -#include +#ifndef _KALCOMM_COMMON_H +#include +#endif noreturn void DosPanic(const char *); +noreturn void DosCrashSystem(void); extern const char *__panicmsg; #define DosGetPanicStr() (__panicmsg) diff --git a/src/kaleid/kernel/ke/state.h b/src/kaleid/kernel/ke/state.h index 7ca09a9..fe8e6b9 100644 --- a/src/kaleid/kernel/ke/state.h +++ b/src/kaleid/kernel/ke/state.h @@ -30,7 +30,6 @@ enum kernel_states { }; extern uchar __kstate; - #define DosGetKernState() (__kstate) #define DosSetKernState(x) (__kstate = (x)) From 529055e9fe0c0a4165a425635749f8275655243f Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 31 Dec 2018 15:08:56 +0100 Subject: [PATCH 14/22] Still more stuff --- src/kaleid/common/common.h | 16 +++++- src/kaleid/common/folder.desc | 8 +-- src/kaleid/common/lib/memory.c | 89 ++++++++++++++++++++++++++++------ src/kaleid/common/stdlib.h | 10 ++-- src/kaleid/kernel/ke/panic.c | 2 +- src/kaleid/linux/test-common.c | 51 ++++++++++--------- 6 files changed, 128 insertions(+), 48 deletions(-) diff --git a/src/kaleid/common/common.h b/src/kaleid/common/common.h index 121a50f..db57a20 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -47,6 +47,10 @@ # define unlikely(x) (__builtin_expect((x), 0)) #endif +#ifndef alignof +# define alignof _Alignof +#endif + #ifndef INITOK # define INITOK ((unsigned int)0xCAFEBABE) #endif @@ -82,6 +86,14 @@ typedef void *va_list; // XXX limits +#define WORD_SIZE sizeof(ushort) +#define DWORD_SIZE sizeof(uint) +#define QWORD_SIZE sizeof(ulong) + +#define WORD_ALIGN alignof(ushort) +#define DWORD_ALIGN alignof(uint) +#define QWORD_ALIGN alignof(ulong) + //------------------------------------------// // VALUES FOR "status_t" // //------------------------------------------// @@ -129,12 +141,12 @@ const char *describe_status(status_t); #if !defined(_NO_DEBUG) && !defined(NDEBUG) // uses panic() in kernel, abort() in system -noreturn void ___assert_handler(const char *, const char *, int, const char *); +noreturn void _assert_handler(const char *, const char *, int, const char *); #define DosAssert(x) \ do { \ if unlikely(!(x)) \ - ___assert_handler(#x, __FILE__, __LINE__, __func__); \ + _assert_handler(#x, __FILE__, __LINE__, __func__); \ } while (FALSE); #define assert DosAssert diff --git a/src/kaleid/common/folder.desc b/src/kaleid/common/folder.desc index 124f60c..eaf5c24 100644 --- a/src/kaleid/common/folder.desc +++ b/src/kaleid/common/folder.desc @@ -22,10 +22,10 @@ This folder contains the following files: - Defines the macro "assert()". Currently any program wanting to use this macro has to implement its own handler: - noreturn void ___assert_handler(const char *cond, - const char *file, - int line, - const char *func) + noreturn void _assert_handler(const char *cond, + const char *file, + int line, + const char *func) but an overridable default handler will be furnished later. - stlib.h diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 7d40011..196cdcc 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -7,22 +7,19 @@ // Desc: mem*() functions // //----------------------------------------------------------------------------// -// XXX to be improved before being brought back -// to be tested with more alignment sizes - #include -// to be moved -#define QWORD_SIZE 8 -#define QWORD_ALIGN 8 +//------------------------------------------// +// memset() family // +//------------------------------------------// // // Set "qwords"-many aligned qwords starting from ptr to val // -static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +static inline void *_memset_internal(void *ptr, ulong uval, size_t qwords) { size_t n; - ullong *uptr = (ullong *)ptr; + ulong *uptr = (ulong *)ptr; // aligned memory write for (n = 0; n < qwords; n++) { @@ -44,18 +41,18 @@ void *memset(void *ptr, int val, size_t bytes) val = val & 0xFF; // deal with bytes before start of the first aligned qword - while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { + while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { *uptr++ = (uchar)val; } // move qword by qword if (qwords) { - const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) - | ((ullong)val << 40) | ((ullong)val << 32) - | ((ullong)val << 24) | ((ullong)val << 16) - | ((ullong)val << 8) | ((ullong)val); + 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); - memsetq(uptr, uval, qwords); + _memset_internal(uptr, uval, qwords); uptr += qwords * QWORD_SIZE; bytes %= QWORD_SIZE; @@ -69,11 +66,75 @@ void *memset(void *ptr, int val, size_t bytes) return ptr; } +// +// Set "words"-many words starting from ptr to val +// +void *memsetw(void *ptr, int val, size_t words) +{ + ushort *uptr = (ushort *)ptr; + + // get rid of everything after the first word + val = val & 0xFFFF; + + // can we do this an aligned way? + if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { + // no, we can't align ourselves + while (words--) { + // do it the hard way + *uptr++ = (ushort)val; + } + // too bad '-' + return uptr; + } + + // deal with words before start of the first aligned qword + while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) { + *uptr++ = (ushort)val; + } + + const size_t qwords = (words * WORD_SIZE)/QWORD_SIZE; + + // move qword by qword + if (qwords) { + const ulong uval = ((ulong)val << 48) | ((ulong)val << 32) + | ((ulong)val << 16) | ((ulong)val); + + _memset_internal(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE / WORD_SIZE; + words %= QWORD_SIZE / WORD_SIZE; + } + + // deal with what's left + while (words--) { + *uptr++ = (ushort)val; + } + + return ptr; +} + +// +// Set "qwords"-many qwords starting from ptr to val +// +void *memsetq(void *ptr, long val, size_t qwords) +{ + return _memset_internal(ptr, (ulong)val, qwords); +} + // // Set "bytes"-many bytes starting from ptr to 0 // void *memzero(void *ptr, size_t bytes) { + // is direct aligned access possible? (is "unlikely" good here?) + if unlikely (bytes % QWORD_SIZE && (ulong)ptr % QWORD_ALIGN) { + return _memset_internal(ptr, (ulong)0, bytes/QWORD_SIZE); + } + + if unlikely (bytes % WORD_SIZE && (ulong)ptr % WORD_ALIGN) { + return memsetw(ptr, (int)0, bytes/WORD_SIZE); + } + return memset(ptr, 0, bytes); } diff --git a/src/kaleid/common/stdlib.h b/src/kaleid/common/stdlib.h index e267a4e..b4af1f3 100644 --- a/src/kaleid/common/stdlib.h +++ b/src/kaleid/common/stdlib.h @@ -8,8 +8,8 @@ //----------------------------------------------------------------------------// -#ifndef _KALCOMM_STRING_H -#define _KALCOMM_STRING_H +#ifndef _KALCOMM_STDLIB_H +#define _KALCOMM_STDLIB_H #ifndef _KALCOMM_COMMON_H # include @@ -21,12 +21,16 @@ #ifndef _OSK_SOURCE # define memcpy _osk_memcpy -# define memset _osk_memset # define memcmp _osk_memcmp # define memzero _osk_memzero #endif +#define memsetb memset 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 *memzero(void *, size_t); //------------------------------------------// diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 633cbf7..aaad786 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -21,7 +21,7 @@ const char *__panicmsg = NULL; // // Failed assert() handler // -noreturn void ___assert_handler(const char *msg, +noreturn void _assert_handler(const char *msg, const char *file, int line, const char *func) diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index b23e00a..cbce8cb 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -9,11 +9,9 @@ #include #include -#define KEEP_KALCOMM_TYPES_MINIMAL -#include -#include -#include +#define KEEP_KALCOMM_TYPES_MINIMAL +#include int main(int argc, char *argv[]) { @@ -31,37 +29,42 @@ int main(int argc, char *argv[]) //char *test2 = malloc(size1); //char *test3 = malloc(size1); - //const size_t sizex = 10000000; - //void *xxx = malloc(sizex); - //printf("%p\n",xxx); - //memset(xxx, 'x', sizex); + const size_t sizex = 130; + short *xxx = (short *)malloc(sizex * sizeof(short)); + //printf("%ld\n",(ulong)xxx%8); + //memzero(xxx, sizex); + memsetw(xxx, 300, sizex); - const char *str = "ceci est un string de test!"; - char *str2 = malloc((strlen(str) + 3) * sizeof(char)); - char *str3 = malloc((strlen(str) + 3) * sizeof(char)); + size_t it; + for (it = 0; it < sizex; it++) { + short s = *(xxx + it); + printf("%hd", s); + } + + puts(""); + + //free((void *)xxx); + + //const char *str = "ceci est un string de test!"; + //char *str2 = malloc((strlen(str) + 3) * sizeof(char)); + //char *str3 = malloc((strlen(str) + 3) * sizeof(char)); - strcpy(str2, str); + //strcpy(str2, str); //size_t s = strlen(str2); - strrev(str3,str2); - printf("%lu - %s\n", strlen(str3), str3); + //strrev(str3,str2); + //printf("%lu - %s\n", strlen(str3), str3); //str2[s] = '\n'; //str2[s+1] = '\0'; - strrev(str2,str3); - printf("%lu - %s\n", strlen(str2), str2); + //strrev(str2,str3); + //printf("%lu - %s\n", strlen(str2), str2); - free(str2); - free(str3); + //free(str2); + //free(str3); - /*size_t it; - for (it = 0; it < sizex; it++) { - char c = *(xxx + it); - printf("%c", (c ? c : '-')); - } - puts("");*/ //free(test2); //free(test3); From ec0045a566aabf6a5a37e0f8c4322b77136ddb6a Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 1 Jan 2019 13:09:57 +0100 Subject: [PATCH 15/22] Big stuff! Happy new year! --- src/Makefile | 51 +++++---- src/kaleid/common/{lib => }/convert.c | 2 +- src/kaleid/common/{lib => }/memory.c | 2 +- src/kaleid/common/{lib => }/memsub.c | 2 +- src/kaleid/common/{ => old}/common.h | 17 ++- src/kaleid/common/{ => old}/folder.desc | 0 src/kaleid/common/{ => old}/stdlib.h | 25 +++-- src/kaleid/common/{lib => }/sprintf.c | 2 +- src/kaleid/common/{lib => }/status.c | 2 +- src/kaleid/common/{lib => }/string.c | 23 +++- .../{linux => common/test}/test-common.c | 5 +- src/kaleid/include/kalassrt.h | 82 ++++++++++++++ src/kaleid/include/kalcrt.h | 102 +++++++++++++++++ src/kaleid/include/kaldefs.h | 105 ++++++++++++++++++ src/kaleid/include/kaleid.h | 51 +++++++++ src/kaleid/include/kalkern | 1 + src/kaleid/include/kalkern.h | 37 ++++++ src/kaleid/include/kalmask.h | 46 ++++++++ src/kaleid/include/kaltypes.h | 74 ++++++++++++ src/kaleid/kernel/config.h | 4 +- src/kaleid/kernel/init.c | 18 +-- src/kaleid/kernel/init.h | 6 +- src/kaleid/kernel/io/ports.c | 2 +- src/kaleid/kernel/io/ports.h | 10 +- src/kaleid/kernel/io/terminal.c | 54 ++++----- src/kaleid/kernel/io/terminal.h | 30 ++--- src/kaleid/kernel/ke/lock.c | 2 +- src/kaleid/kernel/ke/lock.h | 30 ++--- src/kaleid/kernel/ke/panic.c | 38 +++---- src/kaleid/kernel/ke/panic.h | 12 +- src/kaleid/kernel/ke/state.c | 2 +- src/kaleid/kernel/ke/state.h | 8 +- src/kaleid/kernel/mm/malloc.c | 12 -- src/kaleid/kernel/mm/malloc.h | 17 --- src/kernel.ld | 2 +- src/project-tree.txt | 52 +++++---- 36 files changed, 718 insertions(+), 210 deletions(-) rename src/kaleid/common/{lib => }/convert.c (97%) rename src/kaleid/common/{lib => }/memory.c (99%) rename src/kaleid/common/{lib => }/memsub.c (96%) rename src/kaleid/common/{ => old}/common.h (92%) rename src/kaleid/common/{ => old}/folder.desc (100%) rename src/kaleid/common/{ => old}/stdlib.h (83%) rename src/kaleid/common/{lib => }/sprintf.c (98%) rename src/kaleid/common/{lib => }/status.c (97%) rename src/kaleid/common/{lib => }/string.c (79%) rename src/kaleid/{linux => common/test}/test-common.c (95%) create mode 100644 src/kaleid/include/kalassrt.h create mode 100644 src/kaleid/include/kalcrt.h create mode 100644 src/kaleid/include/kaldefs.h create mode 100644 src/kaleid/include/kaleid.h create mode 120000 src/kaleid/include/kalkern create mode 100644 src/kaleid/include/kalkern.h create mode 100644 src/kaleid/include/kalmask.h create mode 100644 src/kaleid/include/kaltypes.h delete mode 100644 src/kaleid/kernel/mm/malloc.c delete mode 100644 src/kaleid/kernel/mm/malloc.h diff --git a/src/Makefile b/src/Makefile index d4e80b8..91253d1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,7 +12,7 @@ CC2NAME=gcc COPTIM=-O2 CLDSCR=-T kernel.ld CWARNS=-pedantic -Wall -Wextra -Werror -CINCLUDES=-isystem. +CINCLUDES=-isystem./kaleid/include CDEFINES= CFLAGS1=-nostdlib -ffreestanding -mcmodel=large @@ -20,7 +20,7 @@ CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2 CFLAGS=$(CFLAGS1) $(CFLAGS2) CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) -KCC=$(CC) -D_KALEID_KERNEL +KCC=$(CC) -D_OSK_SOURCE -D_KALEID_KERNEL ASM=nasm ASMFLAGS= @@ -33,7 +33,8 @@ BOOTDIR=boot COMMDIR=kaleid/common KERNDIR=kaleid/kernel SYSTDIR=kaleid/system -LINXDIR=kaleid/linux +LINXDIR=kaleid/common/test +INCDIR=kaleid/include all: bootloader kernel @@ -62,24 +63,29 @@ testing: bootloader pseudo_kern COBJDIR=$(OBJDIR)/$(COMMDIR) LOBJDIR=$(OBJDIR)/$(LINXDIR) -COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/stdlib.h $(KERNDIR)/config.h +#COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/stdlib.h $(KERNDIR)/config.h +COMMDEPS=$(INCDIR)/kaleid.h $(INCDIR)/kaldefs.h $(INCDIR)/kaltypes.h \ + $(INCDIR)/kalmask.h $(INCDIR)/kalmask.h $(INCDIR)/kalassrt.h -COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o \ - $(COBJDIR)/lib/convert.o $(COBJDIR)/lib/memory.o +COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c \ + $(COMMDIR)/convert.c $(COMMDIR)/memory.c -common: $(COMMDEPS) $(COMMDIR)/lib/string.c $(COMMDIR)/lib/status.c - $(KCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o - $(KCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o - $(KCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o - $(KCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o +COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o \ + $(COBJDIR)/convert.o $(COBJDIR)/memory.o + +common: $(COMMDEPS) $(COMMSRCS) + $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o + $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o + $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o + $(KCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) -test-common: - $(CCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o - $(CCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o - $(CCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o - $(CCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o +test-common: $(COMMSRCS) + $(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o + $(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o + $(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o + $(CCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf @@ -88,17 +94,18 @@ test-common: KOBJDIR=$(OBJDIR)/$(KERNDIR) -KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h \ - $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h +KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/ke/lock.h \ + $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h $(KERNDIR)/ke/state.h -KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/state.c $(KERNDIR)/ke/panic.c \ - $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c +KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/io/terminal.c $(KERNDIR)/ke/lock.c \ + $(KERNDIR)/io/ports.c $(KERNDIR)/ke/panic.c $(KERNDIR)/ke/state.c -KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/state.o $(KOBJDIR)/ke/panic.o \ - $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o +KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/io/terminal.o $(KOBJDIR)/ke/lock.o \ + $(KOBJDIR)/io/ports.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/ke/state.o kernel: common $(KERNSRCS) $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o + $(KCC) -c $(KERNDIR)/ke/lock.c -o $(KOBJDIR)/ke/lock.o $(KCC) -c $(KERNDIR)/ke/state.c -o $(KOBJDIR)/ke/state.o $(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/convert.c similarity index 97% rename from src/kaleid/common/lib/convert.c rename to src/kaleid/common/convert.c index 9a9f037..61e63bf 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/convert.c @@ -7,7 +7,7 @@ // Desc: Conversion utilities // //----------------------------------------------------------------------------// -#include +#include // // Digits table for bases <=36 diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/memory.c similarity index 99% rename from src/kaleid/common/lib/memory.c rename to src/kaleid/common/memory.c index 196cdcc..d9097eb 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/memory.c @@ -7,7 +7,7 @@ // Desc: mem*() functions // //----------------------------------------------------------------------------// -#include +#include //------------------------------------------// // memset() family // diff --git a/src/kaleid/common/lib/memsub.c b/src/kaleid/common/memsub.c similarity index 96% rename from src/kaleid/common/lib/memsub.c rename to src/kaleid/common/memsub.c index 8b25667..247c4d3 100644 --- a/src/kaleid/common/lib/memsub.c +++ b/src/kaleid/common/memsub.c @@ -7,7 +7,7 @@ // Desc: mem*() functions, suboptimal edition // //----------------------------------------------------------------------------// -#include +#include // // Set "bytes"-many bytes starting from ptr to val diff --git a/src/kaleid/common/common.h b/src/kaleid/common/old/common.h similarity index 92% rename from src/kaleid/common/common.h rename to src/kaleid/common/old/common.h index db57a20..1c9c0d6 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/old/common.h @@ -55,6 +55,10 @@ # define INITOK ((unsigned int)0xCAFEBABE) #endif +#ifndef KALAPI +# define KALAPI +#endif + #ifdef _KALEID_KERNEL # include #endif @@ -86,13 +90,6 @@ typedef void *va_list; // XXX limits -#define WORD_SIZE sizeof(ushort) -#define DWORD_SIZE sizeof(uint) -#define QWORD_SIZE sizeof(ulong) - -#define WORD_ALIGN alignof(ushort) -#define DWORD_ALIGN alignof(uint) -#define QWORD_ALIGN alignof(ulong) //------------------------------------------// // VALUES FOR "status_t" // @@ -127,9 +124,9 @@ const char *describe_status(status_t); #ifdef _KALEID_KERNEL -# define DosDisableInterrupts() asm volatile ("cli") -# define DosEnableInterrupts() asm volatile ("sti") -# define DosHaltCPU() asm volatile ("hlt") +# define DisableInterrupts() asm volatile ("cli") +# define EnableInterrupts() asm volatile ("sti") +# define HaltCPU() asm volatile ("hlt") #endif //------------------------------------------// diff --git a/src/kaleid/common/folder.desc b/src/kaleid/common/old/folder.desc similarity index 100% rename from src/kaleid/common/folder.desc rename to src/kaleid/common/old/folder.desc diff --git a/src/kaleid/common/stdlib.h b/src/kaleid/common/old/stdlib.h similarity index 83% rename from src/kaleid/common/stdlib.h rename to src/kaleid/common/old/stdlib.h index b4af1f3..5f8e96c 100644 --- a/src/kaleid/common/stdlib.h +++ b/src/kaleid/common/old/stdlib.h @@ -4,15 +4,24 @@ // Authors: spectral` // // NeoX // // // -// Desc: Kaleid string library // +// Desc: Kaleid C runtime library // //----------------------------------------------------------------------------// - #ifndef _KALCOMM_STDLIB_H #define _KALCOMM_STDLIB_H -#ifndef _KALCOMM_COMMON_H -# include +//------------------------------------------// +// Typedefs // +//------------------------------------------// + +#ifndef __size_t +#define __size_t +typedef unsigned long size_t; +#endif + +#ifndef __va_list +#define __va_list +typedef __builtin_va_start va_list; #endif //------------------------------------------// @@ -25,8 +34,10 @@ # define memzero _osk_memzero #endif -#define memsetb memset -void *memset(void *, int, size_t); +#ifndef memset +# define memset memsetb +#endif +void *memsetb(void *, int, size_t); void *memsetw(void *, int, size_t); void *memsetd(void *, int, size_t); void *memsetq(void *, long, size_t); @@ -72,7 +83,7 @@ int vsnprintf(char *, size_t, const char *, va_list); char *itoa(int, char *, int); //------------------------------------------// -// End of kaleid/common/stdlib.h // +// End of header file // //------------------------------------------// #endif diff --git a/src/kaleid/common/lib/sprintf.c b/src/kaleid/common/sprintf.c similarity index 98% rename from src/kaleid/common/lib/sprintf.c rename to src/kaleid/common/sprintf.c index c8cbfa8..8a4e1f5 100644 --- a/src/kaleid/common/lib/sprintf.c +++ b/src/kaleid/common/sprintf.c @@ -7,7 +7,7 @@ // Desc: sprintf()-related functions // //----------------------------------------------------------------------------// -#include +#include // // Format str according to fmt using ellipsed arguments diff --git a/src/kaleid/common/lib/status.c b/src/kaleid/common/status.c similarity index 97% rename from src/kaleid/common/lib/status.c rename to src/kaleid/common/status.c index 254e1aa..c5139fe 100644 --- a/src/kaleid/common/lib/status.c +++ b/src/kaleid/common/status.c @@ -7,7 +7,7 @@ // Desc: Implementation of describe_status() // //----------------------------------------------------------------------------// -#include +#include static const char *descriptions[] = { [-SUCCESS] = "Success", diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/string.c similarity index 79% rename from src/kaleid/common/lib/string.c rename to src/kaleid/common/string.c index 4bab621..81b4847 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/string.c @@ -7,7 +7,7 @@ // Desc: String-related functions // //----------------------------------------------------------------------------// -#include +#include // // Returns str's length @@ -32,7 +32,7 @@ char *strcpy(char *dest, const char *src) } // -// strcpy() but safer +// strcpy() but always writes n bytes // char *strncpy(char *dest, const char *src, size_t n) { @@ -46,6 +46,25 @@ char *strncpy(char *dest, const char *src, size_t n) return dest; } +// +// strncpy() but safer - always null-terminate, +// returns boolean indicating whether copy was complete +// XXX find a better name +// XXX actually implement this +// +size_t xstrcnpy(char *dest, const char *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 TRUE; +} + // // Reverses the string src, putting the result into dest // diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/common/test/test-common.c similarity index 95% rename from src/kaleid/linux/test-common.c rename to src/kaleid/common/test/test-common.c index cbce8cb..c63735b 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/common/test/test-common.c @@ -10,8 +10,7 @@ #include #include -#define KEEP_KALCOMM_TYPES_MINIMAL -#include +#include int main(int argc, char *argv[]) { @@ -43,7 +42,7 @@ int main(int argc, char *argv[]) puts(""); - //free((void *)xxx); + free((void *)xxx); //const char *str = "ceci est un string de test!"; //char *str2 = malloc((strlen(str) + 3) * sizeof(char)); diff --git a/src/kaleid/include/kalassrt.h b/src/kaleid/include/kalassrt.h new file mode 100644 index 0000000..3a345f4 --- /dev/null +++ b/src/kaleid/include/kalassrt.h @@ -0,0 +1,82 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid assert() support // +//----------------------------------------------------------------------------// + +#ifndef _KALASSRT_H +#define _KALASSRT_H + +//------------------------------------------// +// Useful macros // +//------------------------------------------// + +#ifndef noreturn +#define noreturn __attribute__((noreturn)) +#endif + +#ifndef unlikely(x) +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +//------------------------------------------// +// When debugging // +//------------------------------------------// + +#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(assert) + +// +// Failed assert handler +// +noreturn void _assert_handler(const char *, const char *, int, const char *); + +#define assert(x) \ + do { \ + if unlikely(!(x)) \ + _assert_handler(#x, __FILE__, __LINE__, __func__); \ + } while (0); + +// +// Aliases +// + +#ifndef Assert +#define Assert assert +#endif + +#ifndef DosAssert +#define DosAssert assert +#endif + +#ifndef KalAssert +#define KalAssert assert +#endif + +//------------------------------------------// +// When not debugging // +//------------------------------------------// + +#else + +#if !defined(NDEBUG) +# define NDEBUG 1 +#endif + +#if !defined(_NO_DEBUG) +# define _NO_DEBUG 1 +#endif + +#ifndef assert +#define assert(x) +#endif + +#endif + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kalcrt.h b/src/kaleid/include/kalcrt.h new file mode 100644 index 0000000..6c1fcd6 --- /dev/null +++ b/src/kaleid/include/kalcrt.h @@ -0,0 +1,102 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C runtime library // +//----------------------------------------------------------------------------// + +#ifndef _KALCRT_H +#define _KALCRT_H + +//------------------------------------------// +// Typedefs // +//------------------------------------------// + +#ifndef __size_t +#define __size_t +typedef unsigned long size_t; +#endif + +#ifndef __va_list +#define __va_list +typedef __builtin_va_list va_list; +#endif + +#ifndef __status_t +#define __status_t +typedef signed long status_t; +#endif + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef _NO_MASK +#define _NO_MASK +#endif + +//------------------------------------------// +// va_list utilities // +//------------------------------------------// + +#ifndef va_start +#define va_start __builtin_va_start +#endif + +#ifndef va_next +#define va_next __builtin_va_next +#endif + +#ifndef va_end +#define va_end __builtin_va_end +#endif + +//------------------------------------------// +// Memory management utilities // +//------------------------------------------// + +#if !defined(memset) && !defined(_KALMASK_H) +# define memset memsetb +#endif +void *memsetb(void *, int, size_t); +void *memsetw(void *, int, size_t); +void *memsetd(void *, int, size_t); +void *memsetq(void *, long, size_t); + +void *memzero(void *, size_t); + +//------------------------------------------// +// String manipulation utilities // +//------------------------------------------// + +size_t strlen(const char *); +char *strcpy(char *, const char *); +char *strncpy(char *, const char *, size_t); +char *strrev(char *, const char *); +char *reverse(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); + +//------------------------------------------// +// Diverse utilities // +//------------------------------------------// + +const char *describe_status(status_t) _NO_MASK; + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif + diff --git a/src/kaleid/include/kaldefs.h b/src/kaleid/include/kaldefs.h new file mode 100644 index 0000000..d8dedb3 --- /dev/null +++ b/src/kaleid/include/kaldefs.h @@ -0,0 +1,105 @@ +//----------------------------------------------------------------------------// +// 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 ((void *)0) +#endif + +#ifndef INITOK +#define INITOK ((unsigned int)0xCAFEBABE) +#endif + +#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 + +//------------------------------------------// +// Keywords and attributes // +//------------------------------------------// + +#ifndef PACKED +#define PACKED __attribute__((packed)) +#endif + +#ifndef noreturn +#define noreturn __attribute__((noreturn)) +#endif + +#ifndef alignof +#define alignof _Alignof +#endif + +#ifndef likely +#define likely(x) (__builtin_expect((x), 1)) +#endif + +#ifndef unlikely(x) +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +//------------------------------------------// +// API specific macros // +//------------------------------------------// + +#ifndef KALAPI +# define KALAPI +#endif + +//------------------------------------------// +// Values for APIRET // +//------------------------------------------// + +#define STATUS_FAILED(x) ((x) < 0)) +#define STATUS_SUCCESS(x) (!STATUS_FAILED(x)) + +#define SUCCESS (0) // everything went fine + +#define FAILED (-1) +#define ERROR FAILED // something went wrong +#define NOT_PERMITTED (-2) +#define ACCESS_DENIED (-3) + +#define BAD_ARGUMENT (-4) // invalid arguments +#define BAD_ARG_RANGE (-5) // arguments out of range +#define BAD_ARG_NULL (-6) // unexpected NULL argument + +#define TRY_AGAIN (-7) // EAGAIN + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kaleid.h b/src/kaleid/include/kaleid.h new file mode 100644 index 0000000..80f9b7e --- /dev/null +++ b/src/kaleid/include/kaleid.h @@ -0,0 +1,51 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid API main include file // +//----------------------------------------------------------------------------// + +#ifndef _KALEID_H +#define _KALEID_H + +//------------------------------------------// +// Building for OS/K // +//------------------------------------------// + +#if !defined(_OSK_SOURCE) +# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) +# define _OSK_SOURCE 1 +# endif +#endif + +//------------------------------------------// +// Include common part of API // +//------------------------------------------// + +#ifndef _KALDEFS_H +#include +#endif + +#ifndef _KALTYPES_H +#include +#endif + +#ifndef _KALASSRT_H +#include +#endif + +#if defined(_KALMASK_NEEDED) && !defined(_KALMASK_H) +#include +#endif + +#ifndef _KALCRT_H +#include +#endif + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kalkern b/src/kaleid/include/kalkern new file mode 120000 index 0000000..195d388 --- /dev/null +++ b/src/kaleid/include/kalkern @@ -0,0 +1 @@ +../kernel \ No newline at end of file diff --git a/src/kaleid/include/kalkern.h b/src/kaleid/include/kalkern.h new file mode 100644 index 0000000..40f0625 --- /dev/null +++ b/src/kaleid/include/kalkern.h @@ -0,0 +1,37 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid Kernel main include file // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_H +#define _KALKERN_H + +//------------------------------------------// +// Dependencies // +//------------------------------------------// + +#ifndef _KALEID_H +#include +#endif + +#ifndef _KALKERN_CONFIG_H +#include +#endif + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#define DisableInterrupts() asm volatile ("cli") +#define EnableInterrupts() asm volatile ("sti") +#define HaltCPU() asm volatile ("hlt") + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kalmask.h b/src/kaleid/include/kalmask.h new file mode 100644 index 0000000..d54d761 --- /dev/null +++ b/src/kaleid/include/kalmask.h @@ -0,0 +1,46 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Masks for the functions in the KCRL // +//----------------------------------------------------------------------------// + +#ifndef _KALMASK_H +#define _KALMASK_H + +//------------------------------------------// +// Not building for OS/K // +//------------------------------------------// +#ifndef _OSK_SOURCE + +//------------------------------------------// + +# define memcpy _osk_memcpy +# define memcmp _osk_memcmp +# define memzero _osk_memzero + +//------------------------------------------// + +# define strlen _osk_strlen +# define strcpy _osk_strcpy +# define strncpy _osk_strncpy +# define strrev _osk_strrev +# define reverse _osk_reverse +# define sprintf _osk_sprintf +# define snprintf _osk_snprintf +# define vsprintf _osk_vsprintf +# define vsnprintf _osk_vsnprintf + +//------------------------------------------// + +# define itoa _osk_itoa +# define atoi _osk_atoi + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif +#endif diff --git a/src/kaleid/include/kaltypes.h b/src/kaleid/include/kaltypes.h new file mode 100644 index 0000000..8f8294a --- /dev/null +++ b/src/kaleid/include/kaltypes.h @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------------// +// 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 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 + +//------------------------------------------// +// Other standard integer types // +//------------------------------------------// + +#ifndef __size_t +#define __size_t +typedef unsigned long size_t; +#endif + +#ifndef __ssize_t +#define __ssize_t +typedef signed long ssize_t; +#endif + +#ifndef __wchar_t +#define __wchar_t +typedef signed int wchar_t; +#endif + +#ifndef __off_t +#define __off_t +typedef unsigned long off_t; +#endif + +//------------------------------------------// +// Special types // +//------------------------------------------// + +#ifndef __va_list +#define __va_list +typedef __builtin_va_list va_list; +#endif + +#ifndef __status_t +#define __status_t +typedef signed long status_t; +#endif + +//------------------------------------------// +// Kaleid system types // +//------------------------------------------// + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/kernel/config.h b/src/kaleid/kernel/config.h index 501be3d..6c43ff8 100644 --- a/src/kaleid/kernel/config.h +++ b/src/kaleid/kernel/config.h @@ -23,12 +23,12 @@ // // Enable/disable multiprocessor support // -#define MULTIPROCESSOR NO +#define MULTIPROCESSOR FALSE // // Enable/disable preemptivity // -#define PREEMPTIVE YES +#define PREEMPTIVE TRUE // // Size of a tabulation in spaces diff --git a/src/kaleid/kernel/init.c b/src/kaleid/kernel/init.c index e4d0450..3a7d1cd 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -7,26 +7,26 @@ // Desc: Kernel entry point // //----------------------------------------------------------------------------// -#include -#include -#include -#include +#include +#include +#include +#include // // Entry point of kaleid-kernel.elf // -void DosStartKern(void) +void StartKern(void) { // we're not ready to deal with interrupts - DosDisableInterrupts(); + DisableInterrupts(); // booting! - DosSetKernState(KSTATE_INIT); + SetKernState(KSTATE_INIT); // kernel terminals - DosInitTerms(); + InitTerms(); // we're out - DosPanic("Goodbye World :("); + StartPanic("Goodbye World :("); } diff --git a/src/kaleid/kernel/init.h b/src/kaleid/kernel/init.h index 40351c2..db03c33 100644 --- a/src/kaleid/kernel/init.h +++ b/src/kaleid/kernel/init.h @@ -10,12 +10,12 @@ #ifndef _KALKERN_INIT_H #define _KALKERN_INIT_H -#ifndef _KALCOMM_COMMON_H -#include +#ifndef _KALKERN_H +#include #endif // kernel entry point -void DosStartKern(void); +void StartKern(void); #endif diff --git a/src/kaleid/kernel/io/ports.c b/src/kaleid/kernel/io/ports.c index 4f5c8fe..3f44417 100644 --- a/src/kaleid/kernel/io/ports.c +++ b/src/kaleid/kernel/io/ports.c @@ -7,7 +7,7 @@ // Desc: Ports I/O // //----------------------------------------------------------------------------// -#include +#include diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index a92ddb6..10799a3 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/src/kaleid/kernel/io/ports.h @@ -10,14 +10,14 @@ #ifndef _KALKERN_IO_PORTS_H #define _KALKERN_IO_PORTS_H -#ifndef _KALCOMM_COMMON_H -#include +#ifndef _KALKERN_H +#include #endif -#define DosWriteByteOnPort(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) +#define WriteByteOnPort(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) -uchar DosReadByteFromPort(port_t); -ushort DosReadWordFromPort(port_t); +uchar ReadByteFromPort(port_t); +ushort ReadWordFromPort(port_t); #endif diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index ce3ebd8..fe09e25 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -8,7 +8,7 @@ //----------------------------------------------------------------------------// #define _UNLOCKED_IO -#include +#include // // VGA-related macros @@ -44,31 +44,31 @@ terminal_t *stddbg; // // Initialize standard output // -void DosInitTerms(void) +void InitTerms(void) { - DosAssert(!stdout && _vga_term.kt_init != INITOK); + Assert(!stdout && _vga_term.kt_init != INITOK); _vga_term.kt_init = INITOK; stddbg = &_vga_term; // to be switched to VESA stdout = &_vga_term; - DosClearTerm(stdout); + ClearTerm(stdout); } // // Fill terminal with spaces // -status_t DosClearTerm(terminal_t *kt) +status_t ClearTerm(terminal_t *kt) { if (kt == NULL) return BAD_ARG_NULL; - DosAssert(kt->kt_init == INITOK); + Assert(kt->kt_init == INITOK); - DosLockTerm(kt); - DosClearTerm_Unlocked(kt); - DosUnlockTerm(kt); + LockTerm(kt); + ClearTerm_Unlocked(kt); + UnlockTerm(kt); return SUCCESS; } @@ -76,7 +76,7 @@ status_t DosClearTerm(terminal_t *kt) // // Change the color code // -status_t DosChTermColor(terminal_t *kt, uchar color) +status_t ChTermColor(terminal_t *kt, uchar color) { if (color > KTERM_COLOR_WHITE) return BAD_ARG_RANGE; @@ -84,9 +84,9 @@ status_t DosChTermColor(terminal_t *kt, uchar color) if (kt == NULL) return BAD_ARG_NULL; - DosLockTerm(kt); + LockTerm(kt); kt->kt_color = color; - DosUnlockTerm(kt); + UnlockTerm(kt); return SUCCESS; } @@ -94,16 +94,16 @@ status_t DosChTermColor(terminal_t *kt, uchar color) // // Write a single character on the terminal // -status_t DosPutOnTerm(terminal_t *kt, char ch) +status_t PutOnTerm(terminal_t *kt, char ch) { if (kt == NULL) return BAD_ARG_NULL; - DosAssert(kt->kt_init == INITOK); + Assert(kt->kt_init == INITOK); - DosLockTerm(kt); - DosPutOnTerm_Unlocked(kt, ch); - DosUnlockTerm(kt); + LockTerm(kt); + PutOnTerm_Unlocked(kt, ch); + UnlockTerm(kt); return SUCCESS; } @@ -111,18 +111,18 @@ status_t DosPutOnTerm(terminal_t *kt, char ch) // // Print string on terminal // -status_t DosPrintOnTerm(terminal_t *kt, const char *str) +status_t PrintOnTerm(terminal_t *kt, const char *str) { if (kt == NULL) return BAD_ARG_NULL; - DosAssert(kt->kt_init == INITOK); + Assert(kt->kt_init == INITOK); - DosLockTerm(kt); + LockTerm(kt); while (*str) { - DosPutOnTerm_Unlocked(kt, *str++); + PutOnTerm_Unlocked(kt, *str++); } - DosUnlockTerm(kt); + UnlockTerm(kt); return SUCCESS; } @@ -138,7 +138,7 @@ status_t DosPrintOnTerm(terminal_t *kt, const char *str) // Fill terminal with spaces (UNLOCKED version) // XXX would '\0' work too? // -void DosClearTerm_Unlocked(terminal_t *kt) +void ClearTerm_Unlocked(terminal_t *kt) { size_t i; @@ -156,7 +156,7 @@ void DosClearTerm_Unlocked(terminal_t *kt) // // Write a single character on the terminal (UNLOCKED version) // -void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) +void PutOnTerm_Unlocked(terminal_t *kt, char ch) { int i; size_t prev_row; @@ -176,7 +176,7 @@ void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) for (i = 0; i < TABSIZE; i++) { // tabulations can't spread over two lines if (kt->kt_curr_y == prev_row) { - DosPutOnTerm_Unlocked(kt, ' '); + PutOnTerm_Unlocked(kt, ' '); } } } @@ -200,10 +200,10 @@ void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) // // Print string on terminal (UNLOCKED version) // -void DosPrintOnTerm_Unlocked(terminal_t *kt, const char *str) +void PrintOnTerm_Unlocked(terminal_t *kt, const char *str) { while (*str) { - DosPutOnTerm_Unlocked(kt, *str++); + PutOnTerm_Unlocked(kt, *str++); } } diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 0826453..d7cc988 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -10,7 +10,7 @@ #ifndef _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H -#include +#include // all available colors enum terminal_colors { @@ -42,28 +42,28 @@ extern terminal_t *stdout; // current debugging terminal extern terminal_t *stddbg; -void DosInitTerms(void); -status_t DosClearTerm(terminal_t *); -status_t DosPutOnTerm(terminal_t *, char); -status_t DosPrintOnTerm(terminal_t *, const char *); -status_t DosChTermColor(terminal_t *, uchar); +void InitTerms(void); +status_t ClearTerm(terminal_t *); +status_t PutOnTerm(terminal_t *, char); +status_t PrintOnTerm(terminal_t *, const char *); +status_t ChTermColor(terminal_t *, uchar); -#ifdef _UNLOCKED_IO -void DosClearTerm_Unlocked(terminal_t *); -void DosPutOnTerm_Unlocked(terminal_t *, char); -void DosPrintOnTerm_Unlocked(terminal_t *, const char *); -#define DosChTermColor_Unlocked(kt, col) ((kt)->kt_color = col) +#if defined(_UNLOCKED_IO) +void ClearTerm_Unlocked(terminal_t *); +void PutOnTerm_Unlocked(terminal_t *, char); +void PrintOnTerm_Unlocked(terminal_t *, const char *); +#define ChTermColor_Unlocked(kt, col) ((kt)->kt_color = col) #endif #ifndef _NO_DEBUG -# define DebugLog(...) DosPutOnTerm(stddbg, __VA_ARGS__) +# define DebugLog(...) PrintOnTerm(stddbg, __VA_ARGS__) #else # define DebugLog(...) #endif -#define DosLockTerm(kt) DosAquireLock(&kt->kt_lock) -#define DosUnlockTerm(kt) DosReleaseLock(&kt->kt_lock) -#define DosTryLockTerm(kt) DosAttemptLock(&kt->kt_lock) +#define LockTerm(kt) AquireLock(&kt->kt_lock) +#define UnlockTerm(kt) ReleaseLock(&kt->kt_lock) +#define TryLockTerm(kt) AttemptLock(&kt->kt_lock) #endif diff --git a/src/kaleid/kernel/ke/lock.c b/src/kaleid/kernel/ke/lock.c index 544bf93..465b76f 100644 --- a/src/kaleid/kernel/ke/lock.c +++ b/src/kaleid/kernel/ke/lock.c @@ -7,7 +7,7 @@ // Desc: Locks // //----------------------------------------------------------------------------// -#include +#include // nothing to do here diff --git a/src/kaleid/kernel/ke/lock.h b/src/kaleid/kernel/ke/lock.h index 1174c26..40482ec 100644 --- a/src/kaleid/kernel/ke/lock.h +++ b/src/kaleid/kernel/ke/lock.h @@ -10,8 +10,8 @@ #ifndef _KALKERN_KE_LOCK_H #define _KALKERN_KE_LOCK_H -#ifndef _KALCOMM_COMMON_H -#include +#ifndef _KALKERN_H +#include #endif enum lock_type { @@ -42,7 +42,7 @@ typedef struct { // // Initialize a lock // -#define DosInitLock(lk, type) \ +#define InitLock(lk, type) \ do { \ (lk)->lk_type = (type); \ (lk)->lk_lock = FALSE; \ @@ -59,40 +59,40 @@ typedef struct { // // Does nothing // -#define DosDestroyLock(lk) ((lk)->lk_init = FALSE) +#define DestroyLock(lk) ((lk)->lk_init = FALSE) // // Aquires the lock // Panics on double aquisition since that should never happen // until we have at least a basic scheduler // -#define DosAquireLock(lk) \ +#define AquireLock(lk) \ do { \ - DosDisableInterrupts(); \ - DosAssert((lk)->lk_init == INITOK); \ + DisableInterrupts(); \ + Assert((lk)->lk_init == INITOK); \ if ((lk)->lk_lock++) \ - DosPanic("DosAquireLock on an already locked object"); \ - DosEnableInterrupts(); \ + StartPanic("DosAquireLock on an already locked object"); \ + EnableInterrupts(); \ } while (FALSE); // // Releases an already aquired lock // Panics if the lock was never aquired (this will change) // -#define DosReleaseLock(lk) \ +#define ReleaseLock(lk) \ do { \ - DosDisableInterrupts(); \ - DosAssert((lk)->lk_init == INITOK); \ + DisableInterrupts(); \ + Assert((lk)->lk_init == INITOK); \ if ((lk)->lk_lock++) \ - DosPanic("DosReleaseLock on an unlocked object"); \ - DosEnableInterrupts(); \ + StartPanic("DosReleaseLock on an unlocked object"); \ + EnableInterrupts(); \ } while (FALSE); // // Tries to aquire lock // Doesn't work at all for obvious reasons // -#define DosAttemptLock(lk) ((lk)->lk_lock++) +#define AttemptLock(lk) ((lk)->lk_lock++) #endif diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index aaad786..888ac7b 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -7,11 +7,11 @@ // Desc: How NOT to panic 101 // //----------------------------------------------------------------------------// -#include -#include +#include +#include #define _UNLOCKED_IO -#include +#include // // Panic message @@ -22,47 +22,47 @@ const char *__panicmsg = NULL; // Failed assert() handler // noreturn void _assert_handler(const char *msg, - const char *file, - int line, - const char *func) + const char *file, + int line, + const char *func) { // not getting out of here - DosDisableInterrupts(); + DisableInterrupts(); (void)file; (void)line; (void)func; // XXX sprintf() to create a proper panicstr - DosPanic(msg); + StartPanic(msg); } // // Your best boy panic() // -void DosPanic(const char *str) +void StartPanic(const char *str) { - DosDisableInterrupts(); + DisableInterrupts(); - DosSetKernState(KSTATE_PANIC); + SetKernState(KSTATE_PANIC); - DosClearTerm_Unlocked(stdout); + ClearTerm_Unlocked(stdout); if (str == NULL) { str = "(no message given)"; } - if (DosGetPanicStr()) { - DosPrintOnTerm_Unlocked(stdout, "double panic!\n"); - DosHaltCPU(); + if (GetPanicStr()) { + PrintOnTerm_Unlocked(stdout, "double panic!\n"); + HaltCPU(); } - DosSetPanicStr(str); + SetPanicStr(str); // we cannot lock anything when panicking - DosPrintOnTerm_Unlocked(stdout, "panic! - "); - DosPrintOnTerm_Unlocked(stdout, str); + PrintOnTerm_Unlocked(stdout, "panic! - "); + PrintOnTerm_Unlocked(stdout, str); while (TRUE) { - DosHaltCPU(); + HaltCPU(); } } diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index 27a7753..372848e 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -10,15 +10,15 @@ #ifndef _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H -#ifndef _KALCOMM_COMMON_H -#include +#ifndef _KALKERN_H +#include #endif -noreturn void DosPanic(const char *); -noreturn void DosCrashSystem(void); +noreturn void StartPanic(const char *); +noreturn void CrashSystem(void); extern const char *__panicmsg; -#define DosGetPanicStr() (__panicmsg) -#define DosSetPanicStr(str) (__panicmsg = (str)) +#define GetPanicStr() (__panicmsg) +#define SetPanicStr(str) (__panicmsg = (str)) #endif diff --git a/src/kaleid/kernel/ke/state.c b/src/kaleid/kernel/ke/state.c index fae844d..4d65f89 100644 --- a/src/kaleid/kernel/ke/state.c +++ b/src/kaleid/kernel/ke/state.c @@ -7,7 +7,7 @@ // Desc: Current kernel state // //----------------------------------------------------------------------------// -#include +#include // // Current kernel state diff --git a/src/kaleid/kernel/ke/state.h b/src/kaleid/kernel/ke/state.h index fe8e6b9..6f5de07 100644 --- a/src/kaleid/kernel/ke/state.h +++ b/src/kaleid/kernel/ke/state.h @@ -10,8 +10,8 @@ #ifndef _KALKERN_KE_STATE_H #define _KALKERN_KE_STATE_H -#ifndef _KALCOMM_COMMON_H -#include +#ifndef _KALKERN_H +#include #endif // XXX improve this @@ -30,8 +30,8 @@ enum kernel_states { }; extern uchar __kstate; -#define DosGetKernState() (__kstate) -#define DosSetKernState(x) (__kstate = (x)) +#define GetKernState() (__kstate) +#define SetKernState(x) (__kstate = (x)) #endif diff --git a/src/kaleid/kernel/mm/malloc.c b/src/kaleid/kernel/mm/malloc.c deleted file mode 100644 index 1ffa3c1..0000000 --- a/src/kaleid/kernel/mm/malloc.c +++ /dev/null @@ -1,12 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Memory allocation routines // -// Only exists to trigger Neox // -//----------------------------------------------------------------------------// - -#include - diff --git a/src/kaleid/kernel/mm/malloc.h b/src/kaleid/kernel/mm/malloc.h deleted file mode 100644 index 7d27929..0000000 --- a/src/kaleid/kernel/mm/malloc.h +++ /dev/null @@ -1,17 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Memory allocation routines // -// Only exists to trigger Neox // -//----------------------------------------------------------------------------// - -#ifndef _KALKERN_MM_MALLOC_H -#define _KALKERN_MM_MALLOC_H - -#include - -#endif - diff --git a/src/kernel.ld b/src/kernel.ld index d8fa35a..5768560 100644 --- a/src/kernel.ld +++ b/src/kernel.ld @@ -1,4 +1,4 @@ -ENTRY(DosStartKern) +ENTRY(StartKern) SECTIONS { . = 0x4000; /* XXX 0x4000 is temporary */ diff --git a/src/project-tree.txt b/src/project-tree.txt index f102785..e0e3f4d 100644 --- a/src/project-tree.txt +++ b/src/project-tree.txt @@ -31,6 +31,22 @@ src/ | + kaleid/ | | + | + include/ + | | | + | | - kaleid.h + | | - kaldefs.h + | | - kaltypes.h + | | - kalassrt.h + | | - kalmask.h + | | - kalcrt.h + | | | + | | - kalkern.h + | | | + | | ~ kalkern/ -> ../kernel/ + | | | + | | 0 + | | + | | | + kernel/ | | | | | x folder.desc @@ -53,43 +69,33 @@ src/ | | | | | + ke/ | | | | + | | | - lock.c + | | | - lock.h + | | | | | | | - panic.c | | | - panic.h | | | | + | | | - state.c + | | | - state.h + | | | | | | | 0 | | | | | 0 | | | + common/ | | | - | | x folder.desc + | | - status.c | | | - | | - common.h - | | - assert.h - | | - atomic.h - | | - status.h - | | - types.h + | | - string.c + | | - memory.c + | | - convert.c + | | - sprintf.c | | | - | | - string.h - | | - memory.h - | | - convert.h - | | | - | | + lib/ + | | + test/ | | | | - | | | - status.c - | | | | - | | | - string.c - | | | - memory.c - | | | - convert.c - | | | - sprintf.c + | | | - test-common.c | | | | | | | 0 | | 0 - | | - | + linux/ - | | | - | | - test-common.c - | | | - | | 0 | 0 0 From bea3e8a927ddb3cb2497e688b097eabac23f49f3 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 1 Jan 2019 17:11:30 +0100 Subject: [PATCH 16/22] Stuff, again --- src/Makefile | 20 +++-- src/kaleid/common/arith.c | 53 +++++++++++++ src/kaleid/common/memory.c | 16 +++- src/kaleid/common/rand.c | 1 + src/kaleid/common/test/test-common.c | 4 +- src/kaleid/common/test/test-file0.c | 23 ++++++ src/kaleid/include/kalassrt.h | 32 ++++---- src/kaleid/include/kalcrt.h | 107 +++++++++++++++++++++++++-- src/kaleid/include/kaldefs.h | 4 +- src/kaleid/include/kaleid.h | 10 ++- src/kaleid/include/kalmask.h | 77 ++++++++++++++----- src/kaleid/kernel/io/terminal.c | 19 ++--- src/kaleid/kernel/io/terminal.h | 8 +- src/kaleid/kernel/ke/panic.c | 8 +- 14 files changed, 305 insertions(+), 77 deletions(-) create mode 100644 src/kaleid/common/arith.c create mode 100644 src/kaleid/common/rand.c create mode 100644 src/kaleid/common/test/test-file0.c diff --git a/src/Makefile b/src/Makefile index 91253d1..5d173d2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -67,21 +67,25 @@ LOBJDIR=$(OBJDIR)/$(LINXDIR) COMMDEPS=$(INCDIR)/kaleid.h $(INCDIR)/kaldefs.h $(INCDIR)/kaltypes.h \ $(INCDIR)/kalmask.h $(INCDIR)/kalmask.h $(INCDIR)/kalassrt.h -COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c \ - $(COMMDIR)/convert.c $(COMMDIR)/memory.c +COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \ + $(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c -COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o \ - $(COBJDIR)/convert.o $(COBJDIR)/memory.o +COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \ + $(COBJDIR)/convert.o $(COBJDIR)/memory.o $(COBJDIR)/arith.o common: $(COMMDEPS) $(COMMSRCS) - $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o - $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o - $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o + $(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o + $(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o + $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o + $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o + $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o $(KCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) -test-common: $(COMMSRCS) +tests: $(COMMSRCS) + $(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o + $(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o $(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o $(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o $(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o diff --git a/src/kaleid/common/arith.c b/src/kaleid/common/arith.c new file mode 100644 index 0000000..1d3c740 --- /dev/null +++ b/src/kaleid/common/arith.c @@ -0,0 +1,53 @@ +//----------------------------------------------------------------------------// +// 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); +} + +int _osk_min(int x, int y) +{ + return min(x, y); +} + +long _osk_lmin(long x, long y) +{ + return lmin(x, y); +} + +int _osk_max(int x, int y) +{ + return max(x, y); +} + +long _osk_lmax(long x, long y) +{ + return lmax(x, y); +} + +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/src/kaleid/common/memory.c b/src/kaleid/common/memory.c index d9097eb..81c1785 100644 --- a/src/kaleid/common/memory.c +++ b/src/kaleid/common/memory.c @@ -23,7 +23,7 @@ static inline void *_memset_internal(void *ptr, ulong uval, size_t qwords) // aligned memory write for (n = 0; n < qwords; n++) { - *(uptr + n) = uval; + *uptr++ = uval; } return ptr; @@ -54,7 +54,7 @@ void *memset(void *ptr, int val, size_t bytes) _memset_internal(uptr, uval, qwords); - uptr += qwords * QWORD_SIZE; + uptr = (uchar *) ( (ulong)uptr / (qwords * QWORD_SIZE) ); bytes %= QWORD_SIZE; } @@ -113,6 +113,18 @@ 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; + + return ptr; +} + // // Set "qwords"-many qwords starting from ptr to val // diff --git a/src/kaleid/common/rand.c b/src/kaleid/common/rand.c new file mode 100644 index 0000000..b413bbf --- /dev/null +++ b/src/kaleid/common/rand.c @@ -0,0 +1 @@ +int rand(void) { /*STUB*/ return 0; } diff --git a/src/kaleid/common/test/test-common.c b/src/kaleid/common/test/test-common.c index c63735b..a528a2e 100644 --- a/src/kaleid/common/test/test-common.c +++ b/src/kaleid/common/test/test-common.c @@ -10,7 +10,7 @@ #include #include -#include +void *_osk_memsetw(void *, int, long); int main(int argc, char *argv[]) { @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) short *xxx = (short *)malloc(sizex * sizeof(short)); //printf("%ld\n",(ulong)xxx%8); //memzero(xxx, sizex); - memsetw(xxx, 300, sizex); + _osk_memsetw(xxx, 300, sizex); size_t it; for (it = 0; it < sizex; it++) { diff --git a/src/kaleid/common/test/test-file0.c b/src/kaleid/common/test/test-file0.c new file mode 100644 index 0000000..0cff677 --- /dev/null +++ b/src/kaleid/common/test/test-file0.c @@ -0,0 +1,23 @@ +// random test file + +#include + +int main(int argc, char *argv[]) +{ + long x[4]; + long *ptr = &x[0]; + + printf("%p\n", ptr); + + ptr++; + printf("%p\n", ptr); + + ptr = ptr + 1; + printf("%p\n", ptr); + + ptr = (long *)((long)ptr + 1); + printf("%p\n", ptr); + + return 0; +} + diff --git a/src/kaleid/include/kalassrt.h b/src/kaleid/include/kalassrt.h index 3a345f4..0e1d103 100644 --- a/src/kaleid/include/kalassrt.h +++ b/src/kaleid/include/kalassrt.h @@ -11,11 +11,11 @@ #define _KALASSRT_H //------------------------------------------// -// Useful macros // +// Macros // //------------------------------------------// #ifndef noreturn -#define noreturn __attribute__((noreturn)) +#define noreturn __attribute__((__noreturn__)) #endif #ifndef unlikely(x) @@ -39,22 +39,6 @@ noreturn void _assert_handler(const char *, const char *, int, const char *); _assert_handler(#x, __FILE__, __LINE__, __func__); \ } while (0); -// -// Aliases -// - -#ifndef Assert -#define Assert assert -#endif - -#ifndef DosAssert -#define DosAssert assert -#endif - -#ifndef KalAssert -#define KalAssert assert -#endif - //------------------------------------------// // When not debugging // //------------------------------------------// @@ -75,6 +59,18 @@ noreturn void _assert_handler(const char *, const char *, int, const char *); #endif +//------------------------------------------// +// Aliases for assert() // +//------------------------------------------// + +#ifndef Assert +#define Assert assert +#endif + +#ifndef KalAssert +#define KalAssert assert +#endif + //------------------------------------------// // End of // //------------------------------------------// diff --git a/src/kaleid/include/kalcrt.h b/src/kaleid/include/kalcrt.h index 6c1fcd6..33791a8 100644 --- a/src/kaleid/include/kalcrt.h +++ b/src/kaleid/include/kalcrt.h @@ -19,14 +19,24 @@ typedef unsigned long size_t; #endif +#ifndef __status_t +#define __status_t +typedef signed long status_t; +#endif + #ifndef __va_list #define __va_list typedef __builtin_va_list va_list; #endif -#ifndef __status_t -#define __status_t -typedef signed long status_t; +#ifndef __div_t +#define __div_t +typedef struct { int quot, rem; } div_t; +#endif + +#ifndef __ldiv_t +#define __ldiv_t +typedef struct { long quot, rem; } ldiv_t; #endif //------------------------------------------// @@ -45,6 +55,10 @@ typedef signed long status_t; #define va_start __builtin_va_start #endif +#ifndef va_arg +#define va_arg __builtin_va_arg +#endif + #ifndef va_next #define va_next __builtin_va_next #endif @@ -57,15 +71,29 @@ typedef signed long status_t; // Memory management utilities // //------------------------------------------// -#if !defined(memset) && !defined(_KALMASK_H) -# define memset memsetb +#ifndef memset +#define memset memsetb #endif + +#ifndef memchr +#define memchr memchrb +#endif + void *memsetb(void *, int, size_t); void *memsetw(void *, int, size_t); void *memsetd(void *, int, size_t); void *memsetq(void *, long, size_t); +void *memchrb(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 *memcpy(void *, const void *, 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 // @@ -86,7 +114,24 @@ int vsnprintf(char *, size_t, const char *, va_list); // Type conversion utilities // //------------------------------------------// -char *itoa(int, char *, int); +int *atoi(const char *); +long *atol(const char *); + +char *itoa(int, char *, int); +char *ltoa(long, char *, int); + +char *utoa(unsigned int, char *, int); +char *ultoa(unsigned long, char *, int); + +long strtol(const char *, char **, int); +unsigned long strtoul(const char *, char **, int); + +//------------------------------------------// +// RNG utilities // +//------------------------------------------// + +int rand(void); +void srand(unsigned int); //------------------------------------------// // Diverse utilities // @@ -94,6 +139,56 @@ char *itoa(int, char *, int); const char *describe_status(status_t) _NO_MASK; +//------------------------------------------// +// Arithmetical macros // +//------------------------------------------// + +#ifndef abs +#define abs(x) ((x) < 0 ? -x : x) +#endif + +#ifndef labs +#define labs(x) ((x) < 0 ? -x : x) +#endif + +#ifndef min +#define min(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef lmin +#define lmin(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef max +#define max(x,y) ((x) < (y) ? (x) : (y)) +#endif + +#ifndef lmax +#define lmax(x,y) ((x) < (y) ? (x) : (y)) +#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 // //------------------------------------------// diff --git a/src/kaleid/include/kaldefs.h b/src/kaleid/include/kaldefs.h index d8dedb3..b17c1cc 100644 --- a/src/kaleid/include/kaldefs.h +++ b/src/kaleid/include/kaldefs.h @@ -51,11 +51,11 @@ //------------------------------------------// #ifndef PACKED -#define PACKED __attribute__((packed)) +#define PACKED __attribute__((__packed__)) #endif #ifndef noreturn -#define noreturn __attribute__((noreturn)) +#define noreturn __attribute__((__noreturn__)) #endif #ifndef alignof diff --git a/src/kaleid/include/kaleid.h b/src/kaleid/include/kaleid.h index 80f9b7e..64de82f 100644 --- a/src/kaleid/include/kaleid.h +++ b/src/kaleid/include/kaleid.h @@ -20,6 +20,12 @@ # endif #endif +#ifndef _OSK_SOURCE +# ifndef _KALMASK_H +# include +# endif +#endif + //------------------------------------------// // Include common part of API // //------------------------------------------// @@ -36,10 +42,6 @@ #include #endif -#if defined(_KALMASK_NEEDED) && !defined(_KALMASK_H) -#include -#endif - #ifndef _KALCRT_H #include #endif diff --git a/src/kaleid/include/kalmask.h b/src/kaleid/include/kalmask.h index d54d761..078163a 100644 --- a/src/kaleid/include/kalmask.h +++ b/src/kaleid/include/kalmask.h @@ -11,36 +11,77 @@ #define _KALMASK_H //------------------------------------------// -// Not building for OS/K // -//------------------------------------------// -#ifndef _OSK_SOURCE + +#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 memchrb _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 memcpy _osk_memcpy -# define memcmp _osk_memcmp -# define memzero _osk_memzero +#define strlen _osk_strlen +#define strcpy _osk_strcpy +#define strncpy _osk_strncpy +#define strrev _osk_strrev +#define reverse _osk_reverse + +#define sprintf _osk_sprintf +#define snprintf _osk_snprintf +#define vsprintf _osk_vsprintf +#define vsnprintf _osk_vsnprintf //------------------------------------------// -# define strlen _osk_strlen -# define strcpy _osk_strcpy -# define strncpy _osk_strncpy -# define strrev _osk_strrev -# define reverse _osk_reverse -# define sprintf _osk_sprintf -# define snprintf _osk_snprintf -# define vsprintf _osk_vsprintf -# define vsnprintf _osk_vsnprintf +#define atoi _osk_atoi +#define atol _osk_atol + +#define itoa _osk_itoa +#define ltoa _osk_ltoa + +#define utoa _osk_utoa +#define ultoa _osk_ultoa + +#define strtol _osk_strtol +#define strtoul _osk_strtoul //------------------------------------------// -# define itoa _osk_itoa -# define atoi _osk_atoi +#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 //------------------------------------------// // End of // //------------------------------------------// #endif -#endif diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index fe09e25..dbf5963 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -67,7 +67,7 @@ status_t ClearTerm(terminal_t *kt) Assert(kt->kt_init == INITOK); LockTerm(kt); - ClearTerm_Unlocked(kt); + ClearTermUnlocked(kt); UnlockTerm(kt); return SUCCESS; @@ -102,7 +102,7 @@ status_t PutOnTerm(terminal_t *kt, char ch) Assert(kt->kt_init == INITOK); LockTerm(kt); - PutOnTerm_Unlocked(kt, ch); + PutOnTermUnlocked(kt, ch); UnlockTerm(kt); return SUCCESS; @@ -120,7 +120,7 @@ status_t PrintOnTerm(terminal_t *kt, const char *str) LockTerm(kt); while (*str) { - PutOnTerm_Unlocked(kt, *str++); + PutOnTermUnlocked(kt, *str++); } UnlockTerm(kt); @@ -138,7 +138,7 @@ status_t PrintOnTerm(terminal_t *kt, const char *str) // Fill terminal with spaces (UNLOCKED version) // XXX would '\0' work too? // -void ClearTerm_Unlocked(terminal_t *kt) +void ClearTermUnlocked(terminal_t *kt) { size_t i; @@ -156,7 +156,7 @@ void ClearTerm_Unlocked(terminal_t *kt) // // Write a single character on the terminal (UNLOCKED version) // -void PutOnTerm_Unlocked(terminal_t *kt, char ch) +void PutOnTermUnlocked(terminal_t *kt, char ch) { int i; size_t prev_row; @@ -169,19 +169,20 @@ void PutOnTerm_Unlocked(terminal_t *kt, char ch) // later in this function we actually do the line feed else if (ch == '\n') { kt->kt_curr_y = kt->kt_width - 1; } - // tabulations account for 4 spaces + // tabulations account for "TABSIZE" spaces else if (ch == '\t') { prev_row = kt->kt_curr_y; // compiler will optimize this away for (i = 0; i < TABSIZE; i++) { // tabulations can't spread over two lines if (kt->kt_curr_y == prev_row) { - PutOnTerm_Unlocked(kt, ' '); + PutOnTermUnlocked(kt, ' '); } } } else { + // actually write on the buffer const size_t offset = ComputeOffset(kt, kt->kt_curr_x, kt->kt_curr_y); kt->kt_buffer[offset] = ComputeEntry(ch, kt->kt_color); } @@ -200,10 +201,10 @@ void PutOnTerm_Unlocked(terminal_t *kt, char ch) // // Print string on terminal (UNLOCKED version) // -void PrintOnTerm_Unlocked(terminal_t *kt, const char *str) +void PrintOnTermUnlocked(terminal_t *kt, const char *str) { while (*str) { - PutOnTerm_Unlocked(kt, *str++); + PutOnTermUnlocked(kt, *str++); } } diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index d7cc988..42f8f90 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -49,10 +49,10 @@ status_t PrintOnTerm(terminal_t *, const char *); status_t ChTermColor(terminal_t *, uchar); #if defined(_UNLOCKED_IO) -void ClearTerm_Unlocked(terminal_t *); -void PutOnTerm_Unlocked(terminal_t *, char); -void PrintOnTerm_Unlocked(terminal_t *, const char *); -#define ChTermColor_Unlocked(kt, col) ((kt)->kt_color = col) +void ClearTermUnlocked(terminal_t *); +void PutOnTermUnlocked(terminal_t *, char); +void PrintOnTermUnlocked(terminal_t *, const char *); +#define ChTermColorUnlocked(kt, col) ((kt)->kt_color = col) #endif #ifndef _NO_DEBUG diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 888ac7b..72fa83e 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -44,22 +44,22 @@ void StartPanic(const char *str) SetKernState(KSTATE_PANIC); - ClearTerm_Unlocked(stdout); + ClearTermUnlocked(stdout); if (str == NULL) { str = "(no message given)"; } if (GetPanicStr()) { - PrintOnTerm_Unlocked(stdout, "double panic!\n"); + PrintOnTermUnlocked(stdout, "double panic!\n"); HaltCPU(); } SetPanicStr(str); // we cannot lock anything when panicking - PrintOnTerm_Unlocked(stdout, "panic! - "); - PrintOnTerm_Unlocked(stdout, str); + PrintOnTermUnlocked(stdout, "panic! - "); + PrintOnTermUnlocked(stdout, str); while (TRUE) { HaltCPU(); From 5c84ba8b68a175b8b9d11a70e69fe05d860a5c78 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 1 Jan 2019 17:37:58 +0100 Subject: [PATCH 17/22] Convert stuff --- src/Makefile | 21 +++++++++++++---- src/kaleid/common/convert.c | 34 ++++++++++++++++++++++++++-- src/kaleid/common/test/test-common.c | 14 ++++++++++-- src/kaleid/include/kalassrt.h | 8 +++---- src/kaleid/include/kaleid.h | 2 +- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/Makefile b/src/Makefile index 5d173d2..e87a049 100644 --- a/src/Makefile +++ b/src/Makefile @@ -71,25 +71,36 @@ COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \ $(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \ - $(COBJDIR)/convert.o $(COBJDIR)/memory.o $(COBJDIR)/arith.o + $(COBJDIR)/memory.o $(COBJDIR)/arith.o \ + $(COBJDIR)/itoa.o $(COBJDIR)/ltoa.o $(COBJDIR)/utoa.o $(COBJDIR)/ultoa.o -common: $(COMMDEPS) $(COMMSRCS) +comm-convert: + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o + +common: $(COMMDEPS) $(COMMSRCS) comm-convert $(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o $(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o - $(KCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) -tests: $(COMMSRCS) +tests-comm-convert: + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o + +tests: $(COMMSRCS) tests-comm-convert $(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o $(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o $(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o $(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o $(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o - $(CCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf diff --git a/src/kaleid/common/convert.c b/src/kaleid/common/convert.c index 61e63bf..19c9bbe 100644 --- a/src/kaleid/common/convert.c +++ b/src/kaleid/common/convert.c @@ -15,24 +15,51 @@ static const char digits[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +#if defined(_NEED_UTOA) || defined(_NEED_ULTOA) +#define _S unsigned +#else +#define _S +#endif + +#if defined (_NEED_ITOA) || defined(_NEED_UTOA) +#define _T int +#else +#define _T long +#endif + // // Integer to string in any base between 2 and 36 (included) // +#if defined(_NEED_ITOA) char *itoa(int i, char *str, int base) +#elif defined(_NEED_LTOA) +char *ltoa(long i, char *str, int base) +#elif defined(_NEED_UTOA) +char *utoa(uint i, char *str, int base) +#elif defined(_NEED_ULTOA) +char *ultoa(ulong i, char *str, int base) +#else +#error "What am I supposed to declare?" +#endif { +#if defined(_NEED_ITOA) || defined(_NEED_LTOA) int neg = 0; +#endif + char *orig = str; if (base < 2 || base > 36) return NULL; - + +#if defined(_NEED_ITOA) || defined(_NEED_LTOA) // deal with negatives if (i < 0) { neg = 1; i = -i; } +#endif - // deal with zero separatly + // deal with zero separately if (i == 0) { *str++ = '0'; } @@ -43,7 +70,10 @@ char *itoa(int i, char *str, int base) i /= base; } +#if defined(_NEED_ITOA) || defined(_NEED_LTOA) if (neg) *str++ = '-'; +#endif + *str = '\0'; return reverse(orig); diff --git a/src/kaleid/common/test/test-common.c b/src/kaleid/common/test/test-common.c index a528a2e..e4df713 100644 --- a/src/kaleid/common/test/test-common.c +++ b/src/kaleid/common/test/test-common.c @@ -11,6 +11,10 @@ #include void *_osk_memsetw(void *, int, long); +char *_osk_itoa(int, char *, int); +char *_osk_ltoa(long, char *, int); +char *_osk_utoa(unsigned int, char *, int); +char *_osk_ultoa(unsigned long, char *, int); int main(int argc, char *argv[]) { @@ -27,7 +31,8 @@ int main(int argc, char *argv[]) //const size_t size1 = strlen(test1); //char *test2 = malloc(size1); //char *test3 = malloc(size1); - + +#if 0 const size_t sizex = 130; short *xxx = (short *)malloc(sizex * sizeof(short)); //printf("%ld\n",(ulong)xxx%8); @@ -39,10 +44,15 @@ int main(int argc, char *argv[]) short s = *(xxx + it); printf("%hd", s); } + free((void *)xxx); +#endif + + char buf[256]; + + puts(_osk_ultoa(5000000000, buf, 10)); puts(""); - free((void *)xxx); //const char *str = "ceci est un string de test!"; //char *str2 = malloc((strlen(str) + 3) * sizeof(char)); diff --git a/src/kaleid/include/kalassrt.h b/src/kaleid/include/kalassrt.h index 0e1d103..3c99b2d 100644 --- a/src/kaleid/include/kalassrt.h +++ b/src/kaleid/include/kalassrt.h @@ -45,12 +45,12 @@ noreturn void _assert_handler(const char *, const char *, int, const char *); #else -#if !defined(NDEBUG) -# define NDEBUG 1 +#ifndef NDEBUG +#define NDEBUG 1 #endif -#if !defined(_NO_DEBUG) -# define _NO_DEBUG 1 +#ifndef _NO_DEBUG +#define _NO_DEBUG 1 #endif #ifndef assert diff --git a/src/kaleid/include/kaleid.h b/src/kaleid/include/kaleid.h index 64de82f..a46f317 100644 --- a/src/kaleid/include/kaleid.h +++ b/src/kaleid/include/kaleid.h @@ -20,7 +20,7 @@ # endif #endif -#ifndef _OSK_SOURCE +#if !defined(_OSK_SOURCE) # ifndef _KALMASK_H # include # endif From 452771f8baa8045d670ed7def35036c396384701 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 1 Jan 2019 17:40:48 +0100 Subject: [PATCH 18/22] Most minor stuff so far --- src/kaleid/include/kalcrt.h | 54 ++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/kaleid/include/kalcrt.h b/src/kaleid/include/kalcrt.h index 33791a8..bc46aa1 100644 --- a/src/kaleid/include/kalcrt.h +++ b/src/kaleid/include/kalcrt.h @@ -79,49 +79,49 @@ typedef struct { long quot, rem; } ldiv_t; #define memchr memchrb #endif -void *memsetb(void *, int, size_t); -void *memsetw(void *, int, size_t); -void *memsetd(void *, int, size_t); -void *memsetq(void *, long, size_t); +void *memsetb(void *, int, size_t); +void *memsetw(void *, int, size_t); +void *memsetd(void *, int, size_t); +void *memsetq(void *, long, size_t); -void *memchrb(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 *memchrb(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 *memcpy(void *, const void *, size_t); -void *memmove(void *, const void *, size_t); +void *memcpy(void *, const void *, size_t); +void *memmove(void *, const void *, size_t); -void *memzero(void *, size_t); -int memcmp(const 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 *); -char *strcpy(char *, const char *); -char *strncpy(char *, const char *, size_t); -char *strrev(char *, const char *); -char *reverse(char *); +size_t strlen(const char *); +char *strcpy(char *, const char *); +char *strncpy(char *, const char *, size_t); +char *strrev(char *, const char *); +char *reverse(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); +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 // //------------------------------------------// -int *atoi(const char *); -long *atol(const char *); +int *atoi(const char *); +long *atol(const char *); -char *itoa(int, char *, int); -char *ltoa(long, char *, int); +char *itoa(int, char *, int); +char *ltoa(long, char *, int); -char *utoa(unsigned int, char *, int); -char *ultoa(unsigned long, char *, int); +char *utoa(unsigned int, char *, int); +char *ultoa(unsigned long, char *, int); long strtol(const char *, char **, int); unsigned long strtoul(const char *, char **, int); From b04ead6130b7d207a2f1f8ea9776ccc59d6115d8 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Tue, 1 Jan 2019 20:46:06 +0100 Subject: [PATCH 19/22] Limits stuff --- src/kaleid/common/memory.c | 1 + src/kaleid/common/rand.c | 36 +++++++++- src/kaleid/common/test/test-file0.c | 5 ++ src/kaleid/include/kaldefs.h | 26 ++------ src/kaleid/include/kallims.h | 100 ++++++++++++++++++++++++++++ src/kaleid/include/kaltypes.h | 20 ++++-- src/kaleid/kernel/io/terminal.h | 8 +-- src/kaleid/kernel/ke/lock.h | 2 +- 8 files changed, 167 insertions(+), 31 deletions(-) create mode 100644 src/kaleid/include/kallims.h diff --git a/src/kaleid/common/memory.c b/src/kaleid/common/memory.c index 81c1785..2ccf2e8 100644 --- a/src/kaleid/common/memory.c +++ b/src/kaleid/common/memory.c @@ -8,6 +8,7 @@ //----------------------------------------------------------------------------// #include +#include //------------------------------------------// // memset() family // diff --git a/src/kaleid/common/rand.c b/src/kaleid/common/rand.c index b413bbf..5569792 100644 --- a/src/kaleid/common/rand.c +++ b/src/kaleid/common/rand.c @@ -1 +1,35 @@ -int rand(void) { /*STUB*/ return 0; } +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: RNG related functions // +//----------------------------------------------------------------------------// + +#include +#include + +// +// Seed value +// +static ulong next = 7756; + +// +// Returns a pseudo-random integer +// To be improved +// +int rand(void) +{ + next = next * 1103515245 + 12345; + return (uint)(next / 65536) % INT_MAX; +} + +// +// (Re)Set the random seed +// +void srand(uint seed) +{ + next = (ulong)seed; +} + diff --git a/src/kaleid/common/test/test-file0.c b/src/kaleid/common/test/test-file0.c index 0cff677..59ba1cc 100644 --- a/src/kaleid/common/test/test-file0.c +++ b/src/kaleid/common/test/test-file0.c @@ -1,12 +1,17 @@ // random test file #include +#include int main(int argc, char *argv[]) { long x[4]; long *ptr = &x[0]; + printf("%d\n", CHAR_MIN); + + printf("%hhd\n", (1 << 7)); + printf("%p\n", ptr); ptr++; diff --git a/src/kaleid/include/kaldefs.h b/src/kaleid/include/kaldefs.h index b17c1cc..7341804 100644 --- a/src/kaleid/include/kaldefs.h +++ b/src/kaleid/include/kaldefs.h @@ -30,26 +30,14 @@ #define INITOK ((unsigned int)0xCAFEBABE) #endif -#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 - //------------------------------------------// // Keywords and attributes // //------------------------------------------// +#ifndef alignof +#define alignof _Alignof +#endif + #ifndef PACKED #define PACKED __attribute__((__packed__)) #endif @@ -58,10 +46,6 @@ #define noreturn __attribute__((__noreturn__)) #endif -#ifndef alignof -#define alignof _Alignof -#endif - #ifndef likely #define likely(x) (__builtin_expect((x), 1)) #endif @@ -79,7 +63,7 @@ #endif //------------------------------------------// -// Values for APIRET // +// Values for status_t // //------------------------------------------// #define STATUS_FAILED(x) ((x) < 0)) diff --git a/src/kaleid/include/kallims.h b/src/kaleid/include/kallims.h new file mode 100644 index 0000000..57ba2fa --- /dev/null +++ b/src/kaleid/include/kallims.h @@ -0,0 +1,100 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid type limits definitions // +//----------------------------------------------------------------------------// + +#ifndef _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 + +//------------------------------------------// +// 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 UCHAR_MAX +# else +# define CHAR_MIN SCHAR_MIN +# define CHAR_MAX 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 // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kaltypes.h b/src/kaleid/include/kaltypes.h index 8f8294a..6814644 100644 --- a/src/kaleid/include/kaltypes.h +++ b/src/kaleid/include/kaltypes.h @@ -17,6 +17,7 @@ #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; @@ -58,15 +59,26 @@ typedef unsigned long off_t; typedef __builtin_va_list va_list; #endif +#ifndef __div_t +#define __div_t +typedef struct { int quot, rem; } div_t; +#endif + +#ifndef __ldiv_t +#define __ldiv_t +typedef struct { long quot, rem; } ldiv_t; +#endif + + +//------------------------------------------// +// Kaleid-specific types // +//------------------------------------------// + #ifndef __status_t #define __status_t typedef signed long status_t; #endif -//------------------------------------------// -// Kaleid system types // -//------------------------------------------// - //------------------------------------------// // End of // //------------------------------------------// diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 42f8f90..df42066 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -52,7 +52,7 @@ status_t ChTermColor(terminal_t *, uchar); void ClearTermUnlocked(terminal_t *); void PutOnTermUnlocked(terminal_t *, char); void PrintOnTermUnlocked(terminal_t *, const char *); -#define ChTermColorUnlocked(kt, col) ((kt)->kt_color = col) +#define ChTermColorUnlocked(kt, col) ((kt)->kt_color = (col)) #endif #ifndef _NO_DEBUG @@ -61,9 +61,9 @@ void PrintOnTermUnlocked(terminal_t *, const char *); # define DebugLog(...) #endif -#define LockTerm(kt) AquireLock(&kt->kt_lock) -#define UnlockTerm(kt) ReleaseLock(&kt->kt_lock) -#define TryLockTerm(kt) AttemptLock(&kt->kt_lock) +#define LockTerm(kt) AquireLock(&(kt)->kt_lock) +#define UnlockTerm(kt) ReleaseLock(&(kt)->kt_lock) +#define TryLockTerm(kt) AttemptLock(&(kt)->kt_lock) #endif diff --git a/src/kaleid/kernel/ke/lock.h b/src/kaleid/kernel/ke/lock.h index 40482ec..6841025 100644 --- a/src/kaleid/kernel/ke/lock.h +++ b/src/kaleid/kernel/ke/lock.h @@ -19,7 +19,7 @@ enum lock_type { // Mutex-type lock // // WARNING - // DosLock() panics when used on a mutex while not running a process + // AquireLock() panics when used on a mutex while not running a process // KLOCK_MUTEX, From 5938aa9cfb2e5390d38058b8b370d9319228860d Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Wed, 2 Jan 2019 14:51:40 +0100 Subject: [PATCH 20/22] Better mem*() stuff --- src/kaleid/common/convert.c | 13 ---- src/kaleid/common/memory.c | 137 +++++++++++++++++++++++------------ src/kaleid/common/memsub.c | 37 ---------- src/kaleid/include/kallims.h | 16 ++++ 4 files changed, 105 insertions(+), 98 deletions(-) delete mode 100644 src/kaleid/common/memsub.c diff --git a/src/kaleid/common/convert.c b/src/kaleid/common/convert.c index 19c9bbe..c18765c 100644 --- a/src/kaleid/common/convert.c +++ b/src/kaleid/common/convert.c @@ -14,19 +14,6 @@ // static const char digits[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - -#if defined(_NEED_UTOA) || defined(_NEED_ULTOA) -#define _S unsigned -#else -#define _S -#endif - -#if defined (_NEED_ITOA) || defined(_NEED_UTOA) -#define _T int -#else -#define _T long -#endif - // // Integer to string in any base between 2 and 36 (included) // diff --git a/src/kaleid/common/memory.c b/src/kaleid/common/memory.c index 2ccf2e8..c88bd43 100644 --- a/src/kaleid/common/memory.c +++ b/src/kaleid/common/memory.c @@ -14,49 +14,34 @@ // memset() family // //------------------------------------------// -// -// Set "qwords"-many aligned qwords starting from ptr to val -// -static inline void *_memset_internal(void *ptr, ulong uval, size_t qwords) -{ - size_t n; - ulong *uptr = (ulong *)ptr; - - // aligned memory write - for (n = 0; n < qwords; n++) { - *uptr++ = uval; - } - - return ptr; -} - // // Set "bytes"-many bytes starting from ptr to val // -void *memset(void *ptr, int val, size_t bytes) +void *memsetb(void *ptr, int val, size_t bytes) { uchar *uptr = (uchar *)ptr; - const size_t qwords = bytes/QWORD_SIZE; - - // get rid of everything after the first byte - val = val & 0xFF; // deal with bytes before start of the first aligned qword while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { *uptr++ = (uchar)val; } - // move qword by qword - if (qwords) { + // we're qword-aligned now + 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); - _memset_internal(uptr, uval, qwords); + ulong *uqptr = (ulong *)ptr; - uptr = (uchar *) ( (ulong)uptr / (qwords * QWORD_SIZE) ); - bytes %= QWORD_SIZE; + // move qword by qword + while (bytes > QWORD_SIZE) { + *uqptr++ = uval; + bytes -= QWORD_SIZE; + } + + uptr = (uchar *)(ulong)uqptr; } // deal with what's left @@ -74,10 +59,7 @@ void *memsetw(void *ptr, int val, size_t words) { ushort *uptr = (ushort *)ptr; - // get rid of everything after the first word - val = val & 0xFFFF; - - // can we do this an aligned way? + // can't we do this an aligned way? if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { // no, we can't align ourselves while (words--) { @@ -93,17 +75,20 @@ void *memsetw(void *ptr, int val, size_t words) *uptr++ = (ushort)val; } - const size_t qwords = (words * WORD_SIZE)/QWORD_SIZE; - - // move qword by qword - if (qwords) { + // we're aligned for sure + if (words > QWORDS_TO_WORDS(1)) { const ulong uval = ((ulong)val << 48) | ((ulong)val << 32) | ((ulong)val << 16) | ((ulong)val); - _memset_internal(uptr, uval, qwords); + ulong *uqptr = (ulong *)uptr; + + // move qword by qword + while (words > QWORDS_TO_WORDS(1)) { + words -= QWORDS_TO_WORDS(1); + *uqptr++ = uval; + } - uptr += qwords * QWORD_SIZE / WORD_SIZE; - words %= QWORD_SIZE / WORD_SIZE; + uptr = (ushort *)(ulong)uqptr; } // deal with what's left @@ -131,23 +116,79 @@ void *memsetd(void *ptr, int val, size_t dwords) // void *memsetq(void *ptr, long val, size_t qwords) { - return _memset_internal(ptr, (ulong)val, qwords); + ulong *uptr = (ulong *)ptr; + + while (qwords--) *uptr++ = (ulong)val; + + return ptr; } // // Set "bytes"-many bytes starting from ptr to 0 +// +// WARNING +// Assume "bytes" is large, for small sizes +// use memset(ptr, 0, bytes) directly // void *memzero(void *ptr, size_t bytes) { - // is direct aligned access possible? (is "unlikely" good here?) - if unlikely (bytes % QWORD_SIZE && (ulong)ptr % QWORD_ALIGN) { - return _memset_internal(ptr, (ulong)0, bytes/QWORD_SIZE); - } - - if unlikely (bytes % WORD_SIZE && (ulong)ptr % WORD_ALIGN) { - return memsetw(ptr, (int)0, bytes/WORD_SIZE); - } - - return memset(ptr, 0, 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 *dst, const void *src, size_t bytes) +{ + const ulong *usrc = (const ulong *)src; + ulong *udst = (ulong *)dst; + + if unlikely (bytes == 0) return dst; + + // can we align them both at once? + if unlikely ((ulong)src % WORD_ALIGN == 1 + && (ulong)dst % WORD_ALIGN == 1) { + const uchar *ubsrc = (const uchar *)usrc; + uchar *ubdst = (uchar *)udst; + + *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; + + // 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 what's left + while (bytes--) { + *ubdst ++ = *ubsrc++; + } + + return dst; } diff --git a/src/kaleid/common/memsub.c b/src/kaleid/common/memsub.c deleted file mode 100644 index 247c4d3..0000000 --- a/src/kaleid/common/memsub.c +++ /dev/null @@ -1,37 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: mem*() functions, suboptimal edition // -//----------------------------------------------------------------------------// - -#include - -// -// Set "bytes"-many bytes starting from ptr to val -// -void *memset(void *ptr, int val, size_t bytes) -{ - uchar uval = val & 0xFF; - uchar *uptr = (uchar *)ptr; - - while (bytes--) *uptr++ = uval; - - return ptr; -} - -// -// Set "bytes"-many bytes starting from ptr to 0 -// -void *memzero(void *ptr, size_t bytes) -{ - uchar *uptr = (uchar *)ptr; - - while (bytes--) *uptr++ = 0; - - return ptr; -} - - diff --git a/src/kaleid/include/kallims.h b/src/kaleid/include/kallims.h index 57ba2fa..ac5b158 100644 --- a/src/kaleid/include/kallims.h +++ b/src/kaleid/include/kallims.h @@ -41,6 +41,22 @@ # 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 // //------------------------------------------// From 7dc4f77a8a57f64fa1a417e8dfbd912234cc5448 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Wed, 2 Jan 2019 16:27:12 +0100 Subject: [PATCH 21/22] Evil stuff --- src/Makefile | 134 +++++-------------------------------- src/Makefile.in | 107 ++++++++++++++++++++++++++++++ src/idttool.py | 14 ++++ src/kaleid/Makefile.old | 142 ++++++++++++++++++++++++++++++++++++++++ src/preproc.h | 33 ++++++++++ 5 files changed, 311 insertions(+), 119 deletions(-) create mode 100644 src/Makefile.in create mode 100644 src/idttool.py create mode 100644 src/kaleid/Makefile.old create mode 100644 src/preproc.h diff --git a/src/Makefile b/src/Makefile index e87a049..5a4d5c5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,125 +7,21 @@ # Desc: Project Makefile # #----------------------------------------------------------------------------# -CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" -CC2NAME=gcc -COPTIM=-O2 -CLDSCR=-T kernel.ld -CWARNS=-pedantic -Wall -Wextra -Werror -CINCLUDES=-isystem./kaleid/include -CDEFINES= +kernel: + cpp ./Makefile.in > Makefile.out + python ./idttool.py + make kernel -f Makefile.out.2 + rm Makefile.out Makefile.out.2 -CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2 -CFLAGS=$(CFLAGS1) $(CFLAGS2) +kernel-asm: + cpp -D_TO_ASM ./Makefile.in > Makefile.out + python ./idttool.py + make kernel -f Makefile.out.2 + rm Makefile.out Makefile.out.2 -CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) -KCC=$(CC) -D_OSK_SOURCE -D_KALEID_KERNEL - -ASM=nasm -ASMFLAGS= -BOOTFLAGS=-f bin - -BINDIR=../bin -OBJDIR=../obj - -BOOTDIR=boot -COMMDIR=kaleid/common -KERNDIR=kaleid/kernel -SYSTDIR=kaleid/system -LINXDIR=kaleid/common/test -INCDIR=kaleid/include - -all: bootloader kernel - -boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc - $(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin - -boot.loader.s: $(BOOTDIR)/loader.s - $(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.s -o $(OBJDIR)/boot/loader.bin - -bootloader: boot.mbr.s boot.loader.s - cp $(OBJDIR)/boot/mbr.bin $(BINDIR)/mbr.bin - cp $(OBJDIR)/boot/loader.bin $(BINDIR)/loader.bin - -#----------------------------------------------------------------------------# -# TESTING MAKEFILE - -pseudo_kern: - $(ASM) $(BOOTFLAGS) $(BOOTDIR)/pseudo_kernel.s -o $(OBJDIR)/boot/pkernel.bin - -testing: bootloader pseudo_kern - cat $(BINDIR)/bootloader.bin $(OBJDIR)/boot/pkernel.bin > $(BINDIR)/boot.bin - -#----------------------------------------------------------------------------# -# COMMON MAKEFILE - -COBJDIR=$(OBJDIR)/$(COMMDIR) -LOBJDIR=$(OBJDIR)/$(LINXDIR) - -#COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/stdlib.h $(KERNDIR)/config.h -COMMDEPS=$(INCDIR)/kaleid.h $(INCDIR)/kaldefs.h $(INCDIR)/kaltypes.h \ - $(INCDIR)/kalmask.h $(INCDIR)/kalmask.h $(INCDIR)/kalassrt.h - -COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \ - $(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c - -COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \ - $(COBJDIR)/memory.o $(COBJDIR)/arith.o \ - $(COBJDIR)/itoa.o $(COBJDIR)/ltoa.o $(COBJDIR)/utoa.o $(COBJDIR)/ultoa.o - -comm-convert: - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o - -common: $(COMMDEPS) $(COMMSRCS) comm-convert - $(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o - $(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o - $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o - $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o - $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o - -CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) - -tests-comm-convert: - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o - -tests: $(COMMSRCS) tests-comm-convert - $(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o - $(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o - $(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o - $(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o - $(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o - $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o - $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf - -#----------------------------------------------------------------------------# -# KERNEL MAKEFILE - -KOBJDIR=$(OBJDIR)/$(KERNDIR) - -KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/ke/lock.h \ - $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h $(KERNDIR)/ke/state.h - -KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/io/terminal.c $(KERNDIR)/ke/lock.c \ - $(KERNDIR)/io/ports.c $(KERNDIR)/ke/panic.c $(KERNDIR)/ke/state.c - -KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/io/terminal.o $(KOBJDIR)/ke/lock.o \ - $(KOBJDIR)/io/ports.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/ke/state.o - -kernel: common $(KERNSRCS) - $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o - $(KCC) -c $(KERNDIR)/ke/lock.c -o $(KOBJDIR)/ke/lock.o - $(KCC) -c $(KERNDIR)/ke/state.c -o $(KOBJDIR)/ke/state.o - $(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o - $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o - $(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o - $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/kaleid-kernel.elf - -#----------------------------------------------------------------------------# +tests: + cpp -D_TESTS ./Makefile.in > Makefile.out + python ./idttool.py + make tests -f Makefile.out.2 + rm Makefile.out Makefile.out.2 diff --git a/src/Makefile.in b/src/Makefile.in new file mode 100644 index 0000000..12bdd68 --- /dev/null +++ b/src/Makefile.in @@ -0,0 +1,107 @@ +//----------------------------------------------------------------------------# +// GNU GPL OS/K # +// # +// Authors: spectral` # +// NeoX # +// # +// Desc: Project Makefile # +//----------------------------------------------------------------------------# + +// The madman's Makefile +#include "preproc.h" + +CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" +CC2NAME=gcc +COPTIM=-O2 +CLDSCR=-T kernel.ld +CWARNS=-pedantic -Wall -Wextra -Werror +CINCLUDES=-isystem./kaleid/include +CDEFINES= + +CFLAGS1=-nostdlib -ffreestanding -mcmodel=large +CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CFLAGS=$(CFLAGS1) $(CFLAGS2) $(SFLAG) + +CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) + +ASM=nasm +ASMFLAGS= +BOOTFLAGS=-f bin + +BINDIR=../bin +OBJDIR=../obj + +BOOTDIR=boot +COMMDIR=kaleid/common +KERNDIR=kaleid/kernel +SYSTDIR=kaleid/system +LINXDIR=kaleid/common/test +INCDIR=kaleid/include + +all: bootloader kernel + +boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin + +boot.loader.s: $(BOOTDIR)/loader.s + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.s -o $(OBJDIR)/boot/loader.bin + +bootloader: boot.mbr.s boot.loader.s + cp $(OBJDIR)/boot/mbr.bin $(BINDIR)/mbr.bin + cp $(OBJDIR)/boot/loader.bin $(BINDIR)/loader.bin + +//----------------------------------------------------------------------------# +// TESTING MAKEFILE + +pseudo_kern: + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/pseudo_kernel.s -o $(OBJDIR)/boot/pkernel.bin + +testing: bootloader pseudo_kern + cat $(BINDIR)/bootloader.bin $(OBJDIR)/boot/pkernel.bin > $(BINDIR)/boot.bin + +//----------------------------------------------------------------------------# +// COMMON MAKEFILE + +COBJDIR=$(OBJDIR)/$(COMMDIR) +LOBJDIR=$(OBJDIR)/$(LINXDIR) + +COMMOBJS=COBJ5(string,status,rand,memory,arith) COBJ4(itoa,ltoa,utoa,ultoa) + +TCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) +KCC=$(CC) -D_OSK_SOURCE -D_KALEID_KERNEL + +comm-convert: + COMPILE_CONVRT(itoa) -D_NEED_ITOA + COMPILE_CONVRT(ltoa) -D_NEED_LTOA + COMPILE_CONVRT(utoa) -D_NEED_UTOA + COMPILE_CONVRT(ultoa) -D_NEED_ULTOA + +common: comm-convert + COMPILE_COMMON(rand) + COMPILE_COMMON(arith) + COMPILE_COMMON(string) + COMPILE_COMMON(status) + COMPILE_COMMON(memory) + +tests: common + $(TCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o + $(TCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf + +//----------------------------------------------------------------------------# +// KERNEL MAKEFILE + +KOBJDIR=$(OBJDIR)/$(KERNDIR) + +KERNOBJS=KOBJ5(init,ke/lock,ke/panic,ke/state,io/ports) KOBJ1(io/terminal) + +kernel: common + COMPILE_KERNEL(init) + COMPILE_KERNEL(ke/lock) + COMPILE_KERNEL(ke/state) + COMPILE_KERNEL(ke/panic) + COMPILE_KERNEL(io/ports) + COMPILE_KERNEL(io/terminal) + LINK_KERNEL(kaleid-kernel.elf) + +//----------------------------------------------------------------------------# + diff --git a/src/idttool.py b/src/idttool.py new file mode 100644 index 0000000..90dfa0e --- /dev/null +++ b/src/idttool.py @@ -0,0 +1,14 @@ +# don't mind this file + +f1 = open("Makefile.out", "r+") +f2 = open("Makefile.out.2", "w+") + +fl = f1.readlines() +for ln in fl: + if ln[0] == ' ' and ln[1] != ' ': + f2.write('\t') + f2.write(ln) + +f1.close() +f2.close() + diff --git a/src/kaleid/Makefile.old b/src/kaleid/Makefile.old new file mode 100644 index 0000000..d3ce7ff --- /dev/null +++ b/src/kaleid/Makefile.old @@ -0,0 +1,142 @@ +#----------------------------------------------------------------------------# +# GNU GPL OS/K # +# # +# Authors: spectral` # +# NeoX # +# # +# Desc: Project Makefile # +#----------------------------------------------------------------------------# + +CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" +CC2NAME=gcc +COPTIM=-O2 +CLDSCR=-T kernel.ld +CWARNS=-pedantic -Wall -Wextra -Werror +CINCLUDES=-isystem./kaleid/include +CDEFINES= + +# set to -S to produce assembly +# (will be stored in the .o) +# (will also generate errors, but don't mind them) +SFLAG=-S + +CFLAGS1=-nostdlib -ffreestanding -mcmodel=large +CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CFLAGS=$(CFLAGS1) $(CFLAGS2) $(SFLAG) + +CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) +KCC=$(CC) -D_OSK_SOURCE -D_KALEID_KERNEL + +ASM=nasm +ASMFLAGS= +BOOTFLAGS=-f bin + +BINDIR=../bin +OBJDIR=../obj + +BOOTDIR=boot +COMMDIR=kaleid/common +KERNDIR=kaleid/kernel +SYSTDIR=kaleid/system +LINXDIR=kaleid/common/test +INCDIR=kaleid/include + +all: bootloader kernel + +boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin + +boot.loader.s: $(BOOTDIR)/loader.s + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.s -o $(OBJDIR)/boot/loader.bin + +bootloader: boot.mbr.s boot.loader.s + cp $(OBJDIR)/boot/mbr.bin $(BINDIR)/mbr.bin + cp $(OBJDIR)/boot/loader.bin $(BINDIR)/loader.bin + +#----------------------------------------------------------------------------# +# TESTING MAKEFILE + +pseudo_kern: + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/pseudo_kernel.s -o $(OBJDIR)/boot/pkernel.bin + +testing: bootloader pseudo_kern + cat $(BINDIR)/bootloader.bin $(OBJDIR)/boot/pkernel.bin > $(BINDIR)/boot.bin + +#----------------------------------------------------------------------------# +# COMMON MAKEFILE + +COBJDIR=$(OBJDIR)/$(COMMDIR) +LOBJDIR=$(OBJDIR)/$(LINXDIR) + +#COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/stdlib.h $(KERNDIR)/config.h +COMMDEPS=$(INCDIR)/kaleid.h $(INCDIR)/kaldefs.h $(INCDIR)/kaltypes.h \ + $(INCDIR)/kalmask.h $(INCDIR)/kalmask.h $(INCDIR)/kalassrt.h + +COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \ + $(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c + +COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \ + $(COBJDIR)/memory.o $(COBJDIR)/arith.o \ + $(COBJDIR)/itoa.o $(COBJDIR)/ltoa.o $(COBJDIR)/utoa.o $(COBJDIR)/ultoa.o + +comm-convert: + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o + $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o + +common: $(COMMDEPS) $(COMMSRCS) comm-convert + $(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o + $(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o + $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o + $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o + $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o + +CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) + +tests-comm-convert: + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o + $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o + +tests: $(COMMSRCS) tests-comm-convert + $(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o + $(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o + $(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o + $(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o + $(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o + $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o + $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf + +#----------------------------------------------------------------------------# +# KERNEL MAKEFILE + +KOBJDIR=$(OBJDIR)/$(KERNDIR) + +KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/ke/lock.h \ + $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h $(KERNDIR)/ke/state.h + +KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/io/terminal.c $(KERNDIR)/ke/lock.c \ + $(KERNDIR)/io/ports.c $(KERNDIR)/ke/panic.c $(KERNDIR)/ke/state.c + +KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/io/terminal.o $(KOBJDIR)/ke/lock.o \ + $(KOBJDIR)/io/ports.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/ke/state.o + +kernel: common $(KERNSRCS) + $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o + $(KCC) -c $(KERNDIR)/ke/lock.c -o $(KOBJDIR)/ke/lock.o + $(KCC) -c $(KERNDIR)/ke/state.c -o $(KOBJDIR)/ke/state.o + $(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o + $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o + $(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o + $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/kaleid-kernel.elf + +#----------------------------------------------------------------------------# + +.PHONY: clean + +clean: + +#----------------------------------------------------------------------------# + diff --git a/src/preproc.h b/src/preproc.h new file mode 100644 index 0000000..265f9ee --- /dev/null +++ b/src/preproc.h @@ -0,0 +1,33 @@ +// be careful with this file + +#ifdef _TESTS +# define CCC TCC +#else +# define CCC KCC +#endif + +#ifdef _TO_ASM +# define _CSPREF -S +# define _OUTFIX S +# define LINK_KERNEL(out) +#else +# define _CSPREF -c +# define _OUTFIX o +# define LINK_KERNEL(out) $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/out +#endif + +#define COMPILE_CONVRT(file) $(CCC) _CSPREF $(COMMDIR)/convert.c -o $(COBJDIR)/file._OUTFIX +#define COMPILE_COMMON(file) $(CCC) _CSPREF $(COMMDIR)/file.c -o $(COBJDIR)/file._OUTFIX +#define COMPILE_KERNEL(file) $(KCC) _CSPREF $(KERNDIR)/file.c -o $(KOBJDIR)/file._OUTFIX + +#define COBJ1(x1) $(COBJDIR)/x1.o +#define COBJ2(x1,x2) $(COBJDIR)/x1.o $(COBJDIR)/x2.o +#define COBJ3(x1,x2,x3) $(COBJDIR)/x1.o $(COBJDIR)/x2.o $(COBJDIR)/x3 +#define COBJ4(x1,x2,x3,x4) $(COBJDIR)/x1.o $(COBJDIR)/x2.o $(COBJDIR)/x3.o $(COBJDIR)/x4.o +#define COBJ5(x1,x2,x3,x4,x5) $(COBJDIR)/x1.o $(COBJDIR)/x2.o $(COBJDIR)/x3.o $(COBJDIR)/x4.o $(COBJDIR)/x5.o + +#define KOBJ1(x1) $(KOBJDIR)/x1.o +#define KOBJ2(x1,x2) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o +#define KOBJ3(x1,x2,x3) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o $(KOBJDIR)/x3 +#define KOBJ4(x1,x2,x3,x4) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o $(KOBJDIR)/x3.o $(KOBJDIR)/x4.o +#define KOBJ5(x1,x2,x3,x4,x5) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o $(KOBJDIR)/x3.o $(KOBJDIR)/x4.o $(KOBJDIR)/x5.o From c501d2196e1bed3f10f3c45d512090d9aa0d50d3 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Wed, 2 Jan 2019 16:28:13 +0100 Subject: [PATCH 22/22] Oops stuff --- src/kaleid/Makefile.old | 142 ---------------------------------------- 1 file changed, 142 deletions(-) delete mode 100644 src/kaleid/Makefile.old diff --git a/src/kaleid/Makefile.old b/src/kaleid/Makefile.old deleted file mode 100644 index d3ce7ff..0000000 --- a/src/kaleid/Makefile.old +++ /dev/null @@ -1,142 +0,0 @@ -#----------------------------------------------------------------------------# -# GNU GPL OS/K # -# # -# Authors: spectral` # -# NeoX # -# # -# Desc: Project Makefile # -#----------------------------------------------------------------------------# - -CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" -CC2NAME=gcc -COPTIM=-O2 -CLDSCR=-T kernel.ld -CWARNS=-pedantic -Wall -Wextra -Werror -CINCLUDES=-isystem./kaleid/include -CDEFINES= - -# set to -S to produce assembly -# (will be stored in the .o) -# (will also generate errors, but don't mind them) -SFLAG=-S - -CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2 -CFLAGS=$(CFLAGS1) $(CFLAGS2) $(SFLAG) - -CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) -KCC=$(CC) -D_OSK_SOURCE -D_KALEID_KERNEL - -ASM=nasm -ASMFLAGS= -BOOTFLAGS=-f bin - -BINDIR=../bin -OBJDIR=../obj - -BOOTDIR=boot -COMMDIR=kaleid/common -KERNDIR=kaleid/kernel -SYSTDIR=kaleid/system -LINXDIR=kaleid/common/test -INCDIR=kaleid/include - -all: bootloader kernel - -boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc - $(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin - -boot.loader.s: $(BOOTDIR)/loader.s - $(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.s -o $(OBJDIR)/boot/loader.bin - -bootloader: boot.mbr.s boot.loader.s - cp $(OBJDIR)/boot/mbr.bin $(BINDIR)/mbr.bin - cp $(OBJDIR)/boot/loader.bin $(BINDIR)/loader.bin - -#----------------------------------------------------------------------------# -# TESTING MAKEFILE - -pseudo_kern: - $(ASM) $(BOOTFLAGS) $(BOOTDIR)/pseudo_kernel.s -o $(OBJDIR)/boot/pkernel.bin - -testing: bootloader pseudo_kern - cat $(BINDIR)/bootloader.bin $(OBJDIR)/boot/pkernel.bin > $(BINDIR)/boot.bin - -#----------------------------------------------------------------------------# -# COMMON MAKEFILE - -COBJDIR=$(OBJDIR)/$(COMMDIR) -LOBJDIR=$(OBJDIR)/$(LINXDIR) - -#COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/stdlib.h $(KERNDIR)/config.h -COMMDEPS=$(INCDIR)/kaleid.h $(INCDIR)/kaldefs.h $(INCDIR)/kaltypes.h \ - $(INCDIR)/kalmask.h $(INCDIR)/kalmask.h $(INCDIR)/kalassrt.h - -COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \ - $(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c - -COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \ - $(COBJDIR)/memory.o $(COBJDIR)/arith.o \ - $(COBJDIR)/itoa.o $(COBJDIR)/ltoa.o $(COBJDIR)/utoa.o $(COBJDIR)/ultoa.o - -comm-convert: - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o - $(KCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o - -common: $(COMMDEPS) $(COMMSRCS) comm-convert - $(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o - $(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o - $(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o - $(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o - $(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o - -CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) - -tests-comm-convert: - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ITOA -o $(COBJDIR)/itoa.o - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_LTOA -o $(COBJDIR)/ltoa.o - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_UTOA -o $(COBJDIR)/utoa.o - $(CCC) -c $(COMMDIR)/convert.c -D_NEED_ULTOA -o $(COBJDIR)/ultoa.o - -tests: $(COMMSRCS) tests-comm-convert - $(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o - $(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o - $(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o - $(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o - $(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o - $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o - $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf - -#----------------------------------------------------------------------------# -# KERNEL MAKEFILE - -KOBJDIR=$(OBJDIR)/$(KERNDIR) - -KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/ke/lock.h \ - $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h $(KERNDIR)/ke/state.h - -KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/io/terminal.c $(KERNDIR)/ke/lock.c \ - $(KERNDIR)/io/ports.c $(KERNDIR)/ke/panic.c $(KERNDIR)/ke/state.c - -KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/io/terminal.o $(KOBJDIR)/ke/lock.o \ - $(KOBJDIR)/io/ports.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/ke/state.o - -kernel: common $(KERNSRCS) - $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o - $(KCC) -c $(KERNDIR)/ke/lock.c -o $(KOBJDIR)/ke/lock.o - $(KCC) -c $(KERNDIR)/ke/state.c -o $(KOBJDIR)/ke/state.o - $(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o - $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o - $(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o - $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/kaleid-kernel.elf - -#----------------------------------------------------------------------------# - -.PHONY: clean - -clean: - -#----------------------------------------------------------------------------# -