diff --git a/src/Makefile b/src/Makefile index 79c8836..5a4d5c5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,89 +7,21 @@ # Desc: Project Makefile # #----------------------------------------------------------------------------# -CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" -CC2NAME=gcc # compiler for testing -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 -CDEFINES= +kernel: + cpp ./Makefile.in > Makefile.out + python ./idttool.py + make kernel -f Makefile.out.2 + rm Makefile.out Makefile.out.2 -CC=$(CCNAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) -KCC=$(CC) -D_KALEID_KERNEL +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 -ASM=nasm -ASMFLAGS= -BOOTFLAGS=-f bin - -BINDIR=../bin -OBJDIR=../obj - -BOOTDIR=boot -COMMDIR=kaleid/common -KERNDIR=kaleid/kernel -SYSTDIR=kaleid/system -LINXDIR=kaleid/linux - -all: bootloader - -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)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \ - $(COMMDIR)/status.h - -COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.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 - -CCC=$(CC2NAME) $(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 $(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)/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 - -kernel: common $(KERNSRCS) - $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.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/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/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/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/assert.h b/src/kaleid/common/assert.h deleted file mode 100644 index da80ace..0000000 --- a/src/kaleid/common/assert.h +++ /dev/null @@ -1,51 +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 common/types.h without 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 assert(x) do{if(unlikely(!(x)))___assert_handler(#x, __FILE__, __LINE__, __func__);}while(0); - -#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 deleted file mode 100644 index 9c9dd6a..0000000 --- a/src/kaleid/common/common.h +++ /dev/null @@ -1,51 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Standard include file for both kernel/ and system/ // -//----------------------------------------------------------------------------// - -#ifndef _KALCOMM_COMMON_H -#define _KALCOMM_COMMON_H - -#if !defined(_OSK_SOURCE) && (defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM)) -#define _OSK_SOURCE 1 -#endif - -#if !defined(TRUE) && !defined(FALSE) -# define TRUE 1 -# define FALSE 0 -#endif - -#ifdef _OSK_SOURCE -# define YES 1 -# define NO 0 -#endif - -#ifndef NULL -# define NULL ((void*)0) -#endif - -#ifndef PACKED -# define PACKED __attribute__((packed)) -#endif - -#ifndef noreturn -# define noreturn __attribute__((noreturn)) -#endif - -#if !defined(likely) && !defined(unlikely) -# define likely(x) __builtin_expect((x), 1) -# define unlikely(x) __builtin_expect((x), 0) -#endif - -#include "common/types.h" -#include "common/config.h" -#include "common/atomic.h" -#include "common/status.h" -#include "common/assert.h" - -#endif - diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/convert.c similarity index 70% rename from src/kaleid/common/lib/convert.c rename to src/kaleid/common/convert.c index 2731900..c18765c 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/convert.c @@ -7,43 +7,60 @@ // Desc: Conversion utilities // //----------------------------------------------------------------------------// -#include "common/convert.h" +#include // // Digits table for bases <=36 // static const char digits[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - // // 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'; } - // compute digits... in reverse order (XXX?) + // compute digits... in reverse order while (i > 0) { *str++ = digits[i % 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/folder.desc b/src/kaleid/common/folder.desc deleted file mode 100644 index 8b13789..0000000 --- a/src/kaleid/common/folder.desc +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/kaleid/common/memory.c b/src/kaleid/common/memory.c new file mode 100644 index 0000000..c88bd43 --- /dev/null +++ b/src/kaleid/common/memory.c @@ -0,0 +1,194 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: mem*() functions // +//----------------------------------------------------------------------------// + +#include +#include + +//------------------------------------------// +// memset() family // +//------------------------------------------// + +// +// Set "bytes"-many bytes starting from ptr to val +// +void *memsetb(void *ptr, int val, size_t bytes) +{ + uchar *uptr = (uchar *)ptr; + + // deal with bytes before start of the first aligned qword + while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // 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); + + ulong *uqptr = (ulong *)ptr; + + // move qword by qword + while (bytes > QWORD_SIZE) { + *uqptr++ = uval; + bytes -= QWORD_SIZE; + } + + uptr = (uchar *)(ulong)uqptr; + } + + // deal with what's left + while (bytes--) { + *uptr++ = (uchar)val; + } + + return ptr; +} + +// +// Set "words"-many words starting from ptr to val +// +void *memsetw(void *ptr, int val, size_t words) +{ + ushort *uptr = (ushort *)ptr; + + // can't 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; + } + + // 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); + + ulong *uqptr = (ulong *)uptr; + + // move qword by qword + while (words > QWORDS_TO_WORDS(1)) { + words -= QWORDS_TO_WORDS(1); + *uqptr++ = uval; + } + + uptr = (ushort *)(ulong)uqptr; + } + + // deal with what's left + while (words--) { + *uptr++ = (ushort)val; + } + + return ptr; +} + +// +// Set "dwords"-many dwords starting from ptr to val +// XXX unimplemented +// +void *memsetd(void *ptr, int val, size_t dwords) +{ + (void)val; + (void)dwords; + + return ptr; +} + +// +// Set "qwords"-many qwords starting from ptr to val +// +void *memsetq(void *ptr, long val, size_t qwords) +{ + ulong *uptr = (ulong *)ptr; + + 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) +{ + 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/old/common.h b/src/kaleid/common/old/common.h new file mode 100644 index 0000000..1c9c0d6 --- /dev/null +++ b/src/kaleid/common/old/common.h @@ -0,0 +1,179 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Standard include file for both kernel/ and system/ // +//----------------------------------------------------------------------------// + +#ifndef _KALCOMM_COMMON_H +#define _KALCOMM_COMMON_H + +//------------------------------------------// +// PREPROCESSOR CONSTANTS // +//------------------------------------------// + +#if !defined(_OSK_SOURCE) +# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM) +# define _OSK_SOURCE 1 +# endif +#endif + +#if !defined(TRUE) && !defined(FALSE) +# define TRUE (1) +# define FALSE (0) +#endif + +#ifdef _OSK_SOURCE +# define YES (1) +# define NO (0) +#endif + +#ifndef NULL +# define NULL ((void *)0) +#endif + +#ifndef PACKED +# define PACKED __attribute__((packed)) +#endif + +#ifndef noreturn +# define noreturn __attribute__((noreturn)) +#endif + +#if !defined(likely) && !defined(unlikely) +# define likely(x) (__builtin_expect((x), 1)) +# define unlikely(x) (__builtin_expect((x), 0)) +#endif + +#ifndef alignof +# define alignof _Alignof +#endif + +#ifndef INITOK +# define INITOK ((unsigned int)0xCAFEBABE) +#endif + +#ifndef KALAPI +# define KALAPI +#endif + +#ifdef _KALEID_KERNEL +# include +#endif + +//------------------------------------------// +// 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 DisableInterrupts() asm volatile ("cli") +# define EnableInterrupts() asm volatile ("sti") +# define HaltCPU() 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/old/folder.desc b/src/kaleid/common/old/folder.desc new file mode 100644 index 0000000..eaf5c24 --- /dev/null +++ b/src/kaleid/common/old/folder.desc @@ -0,0 +1,37 @@ +--------------------------------------------------------------------- + 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 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. + 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/old/stdlib.h b/src/kaleid/common/old/stdlib.h new file mode 100644 index 0000000..5f8e96c --- /dev/null +++ b/src/kaleid/common/old/stdlib.h @@ -0,0 +1,90 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C runtime library // +//----------------------------------------------------------------------------// + +#ifndef _KALCOMM_STDLIB_H +#define _KALCOMM_STDLIB_H + +//------------------------------------------// +// 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 + +//------------------------------------------// +// Memory management utilitaries // +//------------------------------------------// + +#ifndef _OSK_SOURCE +# define memcpy _osk_memcpy +# define memcmp _osk_memcmp +# define memzero _osk_memzero +#endif + +#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); + +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 header file // +//------------------------------------------// + +#endif + diff --git a/src/kaleid/common/convert.h b/src/kaleid/common/rand.c similarity index 59% rename from src/kaleid/common/convert.h rename to src/kaleid/common/rand.c index 737f016..5569792 100644 --- a/src/kaleid/common/convert.h +++ b/src/kaleid/common/rand.c @@ -4,21 +4,32 @@ // Authors: spectral` // // NeoX // // // -// Desc: Conversion utilities // +// Desc: RNG related functions // //----------------------------------------------------------------------------// -#ifndef _KALCOMM_CONVERT_H -#define _KALCOMM_CONVERT_H +#include +#include -#ifndef _KALCOMM_COMMON_H -#include "common/common.h" -#endif +// +// Seed value +// +static ulong next = 7756; -#ifndef _OSK_SOURCE -# define itoa _osk_itoa -# define atoi _osk_atoi -#endif +// +// Returns a pseudo-random integer +// To be improved +// +int rand(void) +{ + next = next * 1103515245 + 12345; + return (uint)(next / 65536) % INT_MAX; +} -char *itoa(int, char *, int); +// +// (Re)Set the random seed +// +void srand(uint seed) +{ + next = (ulong)seed; +} -#endif diff --git a/src/kaleid/common/lib/sprintf.c b/src/kaleid/common/sprintf.c similarity index 86% rename from src/kaleid/common/lib/sprintf.c rename to src/kaleid/common/sprintf.c index 1785a01..8a4e1f5 100644 --- a/src/kaleid/common/lib/sprintf.c +++ b/src/kaleid/common/sprintf.c @@ -7,7 +7,7 @@ // Desc: sprintf()-related functions // //----------------------------------------------------------------------------// -#include "common/string.h" +#include // // Format str according to fmt using ellipsed arguments @@ -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/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 5e8115c..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 "common/common.h" +#include static const char *descriptions[] = { [-SUCCESS] = "Success", diff --git a/src/kaleid/common/status.h b/src/kaleid/common/status.h deleted file mode 100644 index 82387da..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 common/types.h without 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/lib/string.c b/src/kaleid/common/string.c similarity index 59% rename from src/kaleid/common/lib/string.c rename to src/kaleid/common/string.c index f81f60c..81b4847 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/string.c @@ -7,9 +7,7 @@ // Desc: String-related functions // //----------------------------------------------------------------------------// -#include "common/string.h" - -// TODO multibyte, assembly +#include // // Returns str's length @@ -34,22 +32,57 @@ 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) { - 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]; it++) { + dest[it] = src[it]; } - while (i < n) dest[i++] = 0; + while (it < n) dest[it++] = 0; return dest; } // -// Reverses a string +// 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 +// +char *strrev(char *dest, const char *src) +{ + char *orig = dest; + size_t n = strlen(src); + + dest[n--] = '\0'; + + while ((*dest++ = src[n--])); + + return orig; +} + +// +// Reverses a string, modifying it +// Returns a point to said string // char *reverse(char *str) { diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h deleted file mode 100644 index 0f8542d..0000000 --- a/src/kaleid/common/string.h +++ /dev/null @@ -1,43 +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 "common/common.h" -#endif - -#ifndef _OSK_SOURCE - -# define strlen _osk_strlen -# define strcpy _osk_strcpy -# define strncpy _osk_strncpy -# 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 *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/test/test-common.c b/src/kaleid/common/test/test-common.c new file mode 100644 index 0000000..e4df713 --- /dev/null +++ b/src/kaleid/common/test/test-common.c @@ -0,0 +1,83 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Test file for common/ // +//----------------------------------------------------------------------------// + +#include +#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[]) +{ + (void)argc; + (void)argv; + + // 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); + +#if 0 + const size_t sizex = 130; + short *xxx = (short *)malloc(sizex * sizeof(short)); + //printf("%ld\n",(ulong)xxx%8); + //memzero(xxx, sizex); + _osk_memsetw(xxx, 300, sizex); + + size_t it; + for (it = 0; it < sizex; it++) { + short s = *(xxx + it); + printf("%hd", s); + } + free((void *)xxx); +#endif + + char buf[256]; + + puts(_osk_ultoa(5000000000, buf, 10)); + + puts(""); + + + //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); + + //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); + + //free(str2); + //free(str3); + + + + //free(test2); + //free(test3); + + return 0; +} + diff --git a/src/kaleid/common/test/test-file0.c b/src/kaleid/common/test/test-file0.c new file mode 100644 index 0000000..59ba1cc --- /dev/null +++ b/src/kaleid/common/test/test-file0.c @@ -0,0 +1,28 @@ +// 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++; + 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/common/types.h b/src/kaleid/common/types.h deleted file mode 100644 index fb7d4ff..0000000 --- a/src/kaleid/common/types.h +++ /dev/null @@ -1,43 +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 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; -typedef unsigned long ulong; -typedef long long llong; -typedef unsigned long long ullong; -typedef long double ldouble; -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 - -typedef short port_t; -typedef short status_t; -// XXX limits - -#endif - - - diff --git a/src/kaleid/include/kalassrt.h b/src/kaleid/include/kalassrt.h new file mode 100644 index 0000000..3c99b2d --- /dev/null +++ b/src/kaleid/include/kalassrt.h @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid assert() support // +//----------------------------------------------------------------------------// + +#ifndef _KALASSRT_H +#define _KALASSRT_H + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef noreturn +#define noreturn __attribute__((__noreturn__)) +#endif + +#ifndef unlikely(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); + +//------------------------------------------// +// When not debugging // +//------------------------------------------// + +#else + +#ifndef NDEBUG +#define NDEBUG 1 +#endif + +#ifndef _NO_DEBUG +#define _NO_DEBUG 1 +#endif + +#ifndef assert +#define assert(x) +#endif + +#endif + +//------------------------------------------// +// Aliases for assert() // +//------------------------------------------// + +#ifndef Assert +#define Assert assert +#endif + +#ifndef KalAssert +#define KalAssert assert +#endif + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kalcrt.h b/src/kaleid/include/kalcrt.h new file mode 100644 index 0000000..bc46aa1 --- /dev/null +++ b/src/kaleid/include/kalcrt.h @@ -0,0 +1,197 @@ +//----------------------------------------------------------------------------// +// 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 __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 __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 + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef _NO_MASK +#define _NO_MASK +#endif + +//------------------------------------------// +// va_list utilities // +//------------------------------------------// + +#ifndef va_start +#define va_start __builtin_va_start +#endif + +#ifndef va_arg +#define va_arg __builtin_va_arg +#endif + +#ifndef va_next +#define va_next __builtin_va_next +#endif + +#ifndef va_end +#define va_end __builtin_va_end +#endif + +//------------------------------------------// +// Memory management utilities // +//------------------------------------------// + +#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 // +//------------------------------------------// + +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 // +//------------------------------------------// + +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 // +//------------------------------------------// + +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 // +//------------------------------------------// + +#endif + diff --git a/src/kaleid/include/kaldefs.h b/src/kaleid/include/kaldefs.h new file mode 100644 index 0000000..7341804 --- /dev/null +++ b/src/kaleid/include/kaldefs.h @@ -0,0 +1,89 @@ +//----------------------------------------------------------------------------// +// 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 + +//------------------------------------------// +// Keywords and attributes // +//------------------------------------------// + +#ifndef alignof +#define alignof _Alignof +#endif + +#ifndef PACKED +#define PACKED __attribute__((__packed__)) +#endif + +#ifndef noreturn +#define noreturn __attribute__((__noreturn__)) +#endif + +#ifndef likely +#define likely(x) (__builtin_expect((x), 1)) +#endif + +#ifndef unlikely(x) +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +//------------------------------------------// +// API specific macros // +//------------------------------------------// + +#ifndef KALAPI +# define KALAPI +#endif + +//------------------------------------------// +// Values for 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 + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/include/kaleid.h b/src/kaleid/include/kaleid.h new file mode 100644 index 0000000..a46f317 --- /dev/null +++ b/src/kaleid/include/kaleid.h @@ -0,0 +1,53 @@ +//----------------------------------------------------------------------------// +// 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 + +#if !defined(_OSK_SOURCE) +# ifndef _KALMASK_H +# include +# endif +#endif + +//------------------------------------------// +// Include common part of API // +//------------------------------------------// + +#ifndef _KALDEFS_H +#include +#endif + +#ifndef _KALTYPES_H +#include +#endif + +#ifndef _KALASSRT_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/kallims.h b/src/kaleid/include/kallims.h new file mode 100644 index 0000000..ac5b158 --- /dev/null +++ b/src/kaleid/include/kallims.h @@ -0,0 +1,116 @@ +//----------------------------------------------------------------------------// +// 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 + +#ifndef DATA_SHIFTS_BLOCK +#define DATA_SHIFTS_BLOCK +# define BYTES_TO_WORDS(B) ((B) >> 1) +# define BYTES_TO_DWORDS(B) ((B) >> 2) +# define BYTES_TO_QWORDS(B) ((B) >> 3) +# define WORDS_TO_BYTES(W) ((W) << 1) +# define WORDS_TO_DWORDS(W) ((W) >> 1) +# define WORDS_TO_QWORDS(W) ((W) >> 2) +# define DWORDS_TO_BYTES(D) ((D) << 2) +# define DWORDS_TO_WORDS(D) ((D) << 1) +# define DWORDS_TO_QWORDS(D) ((D) >> 1) +# define QWORDS_TO_BYTES(Q) ((Q) << 3) +# define QWORDS_TO_WORDS(Q) ((Q) << 2) +# define QWORDS_TO_DWORDS(Q) ((Q) << 1) +#endif + +//------------------------------------------// +// Numeric data limits // +//------------------------------------------// + +#ifndef DATA_MAX_LIMITS_BLOCK +#define DATA_MAX_LIMITS_BLOCK +# define SCHAR_MAX ((signed char) 0x7F) +# define SHRT_MAX ((short) 0x7FFF) +# define INT_MAX ((int) 0x7FFFFFFF) +# define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFF) +# define UCHAR_MAX ((unsigned char) 0xFF +# define USHRT_MAX ((unsigned short) 0xFFFF) +# define UINT_MAX ((unsigned int) 0xFFFFFFFF) +# define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFF) +#endif + +#ifndef DATA_MIN_LIMITS_BLOCK +#define DATA_MIN_LIMITS_BLOCK +# define SCHAR_MIN ((signed char) -SCHAR_MAX - 1) +# define SHRT_MIN ((short) -SHRT_MAX - 1) +# define INT_MIN ((int) -INT_MAX - 1) +# define LONG_MIN ((long) -LONG_MAX - 1L) +#endif + +#ifndef DATA_CHAR_LIMITS_BLOCK +#define DATA_CHAR_LIMITS_BLOCK +# ifdef __CHAR_UNSIGNED__ +# define CHAR_MIN ((char)0) +# define CHAR_MAX 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/kalmask.h b/src/kaleid/include/kalmask.h new file mode 100644 index 0000000..078163a --- /dev/null +++ b/src/kaleid/include/kalmask.h @@ -0,0 +1,87 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Masks for the functions in the KCRL // +//----------------------------------------------------------------------------// + +#ifndef _KALMASK_H +#define _KALMASK_H + +//------------------------------------------// + +#define memset _osk_memsetb +#define memchr _osk_memchrb + +#define memsetb _osk_memsetb +#define memsetw _osk_memsetw +#define memsetd _osk_memsetd +#define memsetq _osk_memsetq + +#define 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 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 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 diff --git a/src/kaleid/include/kaltypes.h b/src/kaleid/include/kaltypes.h new file mode 100644 index 0000000..6814644 --- /dev/null +++ b/src/kaleid/include/kaltypes.h @@ -0,0 +1,86 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C common types // +//----------------------------------------------------------------------------// + +#ifndef _KALTYPES_H +#define _KALTYPES_H + +//------------------------------------------// +// Basic integer types aliases // +//------------------------------------------// + +#ifndef __base_types_aliases +#define __base_types_aliases +typedef unsigned char uchar; +typedef signed char schar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef signed long long llong; +typedef unsigned long long ullong; +typedef long double ldouble; +#endif + +//------------------------------------------// +// 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 __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 + +//------------------------------------------// +// End of // +//------------------------------------------// + +#endif diff --git a/src/kaleid/common/config.h b/src/kaleid/kernel/config.h similarity index 96% rename from src/kaleid/common/config.h rename to src/kaleid/kernel/config.h index 501be3d..6c43ff8 100644 --- a/src/kaleid/common/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/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..e85aabc 100644 --- a/src/kaleid/kernel/folder.desc +++ b/src/kaleid/kernel/folder.desc @@ -1,44 +1,37 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Folder description - "kaleid/kernel" // -//----------------------------------------------------------------------------// +--------------------------------------------------------------------- + GNU GPL OS/K -This is the folder containing the source of Kaleid's kernel. + Authors: spectral` + NeoX -It contains the following files: + Desc: Folder description - "kaleid/kernel" +--------------------------------------------------------------------- + +This folder contains the source of Kaleid's kernel component. + +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 - 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) - - - se/ - Security folder. (XXX) - - - sys/ - Syscall folder. (XXX) - + 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 b6f268a..3a7d1cd 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -7,16 +7,26 @@ // Desc: Kernel entry point // //----------------------------------------------------------------------------// -#include "kernel/init.h" -#include "kernel/ke/panic.h" -#include "kernel/io/terminal.h" +#include +#include +#include +#include // // Entry point of kaleid-kernel.elf // -void kstart(void) -{ - kterm_init(); - panic("Goodbye World :("); +void StartKern(void) +{ + // we're not ready to deal with interrupts + DisableInterrupts(); + + // booting! + SetKernState(KSTATE_INIT); + + // kernel terminals + InitTerms(); + + // we're out + StartPanic("Goodbye World :("); } diff --git a/src/kaleid/kernel/init.h b/src/kaleid/kernel/init.h index bb80788..db03c33 100644 --- a/src/kaleid/kernel/init.h +++ b/src/kaleid/kernel/init.h @@ -10,10 +10,12 @@ #ifndef _KALKERN_INIT_H #define _KALKERN_INIT_H -#include "common/common.h" +#ifndef _KALKERN_H +#include +#endif // kernel entry point -void kstart(void); +void StartKern(void); #endif diff --git a/src/kaleid/kernel/io/ports.c b/src/kaleid/kernel/io/ports.c index fa824a0..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 "kernel/io/ports.h" +#include diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index b2adfbe..10799a3 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 -#include "common/common.h" +#ifndef _KALKERN_H +#include +#endif -#define outb(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 inb(port_t); -ushort inw(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 503dfcd..dbf5963 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -7,78 +7,68 @@ // Desc: Early terminal functions // //----------------------------------------------------------------------------// -#include "kernel/io/terminal.h" +#define _UNLOCKED_IO +#include // // 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) // -// VGA output +// VGA output // -static struct kterm _kt_vga = { +static terminal_t _vga_term = { .kt_buffer = (ushort *)0xB8000, .kt_width = 80, .kt_height = 25, .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 }; // // Standard output terminal // -struct kterm *kt_stdout; +terminal_t *stdout; + +// +// Debugging terminal +// +terminal_t *stddbg; // // Initialize standard output // -void kterm_init(void) +void InitTerms(void) { - assert(!kt_stdout && !_kt_vga.kt_init && "kterm_init() called twice"); - -#ifndef _NO_DEBUG - _kt_vga.kt_init = TRUE; -#endif + Assert(!stdout && _vga_term.kt_init != INITOK); + + _vga_term.kt_init = INITOK; + stddbg = &_vga_term; // to be switched to VESA - kt_stdout = &_kt_vga; - ktclear(); + stdout = &_vga_term; + ClearTerm(stdout); } // -// Fills terminal with spaces -// XXX would '\0' work too? +// Fill terminal with spaces // -status_t kterm_clear(struct kterm *kt) +status_t ClearTerm(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 == INITOK); - kterm_lock(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() - kt->kt_buffer[i] = filler; - } - - kt->kt_curr_x = kt->kt_curr_y = 0; - - kterm_unlock(kt); + LockTerm(kt); + ClearTermUnlocked(kt); + UnlockTerm(kt); return SUCCESS; } @@ -86,7 +76,7 @@ status_t kterm_clear(struct kterm *kt) // // Change the color code // -status_t kterm_change_color(struct kterm *kt, uchar color) +status_t ChTermColor(terminal_t *kt, uchar color) { if (color > KTERM_COLOR_WHITE) return BAD_ARG_RANGE; @@ -94,21 +84,79 @@ status_t kterm_change_color(struct kterm *kt, uchar color) if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); + LockTerm(kt); kt->kt_color = color; - kterm_unlock(kt); + UnlockTerm(kt); return SUCCESS; } +// +// Write a single character on the terminal // -// Writes a single character on the terminal (UNLOCKED version) +status_t PutOnTerm(terminal_t *kt, char ch) +{ + if (kt == NULL) + return BAD_ARG_NULL; + + Assert(kt->kt_init == INITOK); + + LockTerm(kt); + PutOnTermUnlocked(kt, ch); + UnlockTerm(kt); + + return SUCCESS; +} + // -// DEPRECATED: -// - always use kterm_putch (LOCKED version) -// - doesn't check for NULL input +// Print string on terminal // -void kterm_putch_unlocked(struct kterm *kt, char ch) +status_t PrintOnTerm(terminal_t *kt, const char *str) +{ + if (kt == NULL) + return BAD_ARG_NULL; + + Assert(kt->kt_init == INITOK); + + LockTerm(kt); + while (*str) { + PutOnTermUnlocked(kt, *str++); + } + UnlockTerm(kt); + + return SUCCESS; +} + +//----------------------------------------------------------// +// UNLOCKED VERSIONS // +// // +// Direct use is highly deprecated // +// Useful in rare instances // +//----------------------------------------------------------// + +// +// Fill terminal with spaces (UNLOCKED version) +// XXX would '\0' work too? +// +void ClearTermUnlocked(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; +} + +// +// Write a single character on the terminal (UNLOCKED version) +// +void PutOnTermUnlocked(terminal_t *kt, char ch) { int i; size_t prev_row; @@ -121,21 +169,21 @@ void kterm_putch_unlocked(struct kterm *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) { - kterm_putch_unlocked(kt, ' '); + PutOnTermUnlocked(kt, ' '); } } } - - // XXX check whether we were given a writable character + else { - const size_t offset = ComputeEntryOffset(kt, kt->kt_curr_x, kt->kt_curr_y); + // 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); } @@ -150,37 +198,13 @@ 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) +// Print string on terminal (UNLOCKED version) +// +void PrintOnTermUnlocked(terminal_t *kt, const char *str) { - if (kt == NULL) - return BAD_ARG_NULL; - - kterm_lock(kt); - kterm_putch_unlocked(kt, ch); - kterm_unlock(kt); - - return SUCCESS; -} - - -// -// Print string on kterminal -// -status_t kterm_print(struct kterm *kt, const char *str) -{ - if (kt == NULL) - return BAD_ARG_NULL; - - kterm_lock(kt); while (*str) { - kterm_putch_unlocked(kt, *str++); + PutOnTermUnlocked(kt, *str++); } - kterm_unlock(kt); - - return SUCCESS; } - diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 5cb39bf..df42066 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -10,10 +10,10 @@ #ifndef _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H -#include "common/common.h" +#include // all available colors -enum kterm_color { +enum terminal_colors { KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, @@ -24,41 +24,46 @@ enum kterm_color { KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE }; -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; -#ifndef _NO_DEBUG - bool kt_init; -#endif - -}; +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 +} 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); +// current debugging terminal +extern terminal_t *stddbg; -#ifdef _UNLOCKED_IO -void kterm_putch_unlocked(struct kterm *, char); +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); + +#if defined(_UNLOCKED_IO) +void ClearTermUnlocked(terminal_t *); +void PutOnTermUnlocked(terminal_t *, char); +void PrintOnTermUnlocked(terminal_t *, const char *); +#define ChTermColorUnlocked(kt, col) ((kt)->kt_color = (col)) #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 +# define DebugLog(...) PrintOnTerm(stddbg, __VA_ARGS__) +#else +# define DebugLog(...) +#endif -#define kterm_lock(kt) -#define kterm_trylock(kt) -#define kterm_unlock(kt) +#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/common/lib/memory.c b/src/kaleid/kernel/ke/lock.c similarity index 83% rename from src/kaleid/common/lib/memory.c rename to src/kaleid/kernel/ke/lock.c index 3ebe6ee..465b76f 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/kernel/ke/lock.c @@ -4,8 +4,10 @@ // Authors: spectral` // // NeoX // // // -// Desc: mem*() functions // +// Desc: Locks // //----------------------------------------------------------------------------// -#include "common/memory.h" +#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..6841025 --- /dev/null +++ b/src/kaleid/kernel/ke/lock.h @@ -0,0 +1,98 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Locks // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_KE_LOCK_H +#define _KALKERN_KE_LOCK_H + +#ifndef _KALKERN_H +#include +#endif + +enum lock_type { + // + // Mutex-type lock + // + // WARNING + // AquireLock() 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 InitLock(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 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 AquireLock(lk) \ + do { \ + DisableInterrupts(); \ + Assert((lk)->lk_init == INITOK); \ + if ((lk)->lk_lock++) \ + 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 ReleaseLock(lk) \ + do { \ + DisableInterrupts(); \ + Assert((lk)->lk_init == INITOK); \ + if ((lk)->lk_lock++) \ + StartPanic("DosReleaseLock on an unlocked object"); \ + EnableInterrupts(); \ + } while (FALSE); + +// +// Tries to aquire lock +// Doesn't work at all for obvious reasons +// +#define AttemptLock(lk) ((lk)->lk_lock++) + +#endif + diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 04310c6..72fa83e 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -7,53 +7,62 @@ // Desc: How NOT to panic 101 // //----------------------------------------------------------------------------// -#include "kernel/ke/panic.h" +#include +#include + +#define _UNLOCKED_IO +#include // // Panic message // -const char *panicstr = NULL; +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 - cli(); + DisableInterrupts(); (void)file; (void)line; (void)func; // XXX sprintf() to create a proper panicstr - panic(msg); + StartPanic(msg); } // // Your best boy panic() // -void panic(const char *str) +void StartPanic(const char *str) { - cli(); + DisableInterrupts(); - ktclear(); + SetKernState(KSTATE_PANIC); + + ClearTermUnlocked(stdout); if (str == NULL) { str = "(no message given)"; } - if (panicstr) { - // shouldn't be possible - ktprint("double panic!\n"); - hlt(); + if (GetPanicStr()) { + PrintOnTermUnlocked(stdout, "double panic!\n"); + HaltCPU(); } - panicstr = str; + SetPanicStr(str); - ktprint("panic! - "); - ktprint(str); + // we cannot lock anything when panicking + PrintOnTermUnlocked(stdout, "panic! - "); + PrintOnTermUnlocked(stdout, str); while (TRUE) { - hlt(); + HaltCPU(); } } diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index 403d728..372848e 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -10,9 +10,15 @@ #ifndef _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H -#include "kernel/io/terminal.h" +#ifndef _KALKERN_H +#include +#endif -extern const char *panicstr; -noreturn void panic(const char *); +noreturn void StartPanic(const char *); +noreturn void CrashSystem(void); + +extern const char *__panicmsg; +#define GetPanicStr() (__panicmsg) +#define SetPanicStr(str) (__panicmsg = (str)) #endif diff --git a/src/kaleid/common/memory.h b/src/kaleid/kernel/ke/state.c similarity index 76% rename from src/kaleid/common/memory.h rename to src/kaleid/kernel/ke/state.c index a877e1a..4d65f89 100644 --- a/src/kaleid/common/memory.h +++ b/src/kaleid/kernel/ke/state.c @@ -4,15 +4,13 @@ // Authors: spectral` // // NeoX // // // -// Desc: mem*() functions // +// Desc: Current kernel state // //----------------------------------------------------------------------------// -#ifndef _KALCOMM_MEMORY_H -#define _KALCOMM_MEMORY_H +#include -#ifndef _KALCOMM_COMMON_H -#include "common/common.h" -#endif - -#endif +// +// Current kernel state +// +uchar __kstate; diff --git a/src/kaleid/common/atomic.h b/src/kaleid/kernel/ke/state.h similarity index 53% rename from src/kaleid/common/atomic.h rename to src/kaleid/kernel/ke/state.h index a1daef3..6f5de07 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/kernel/ke/state.h @@ -4,26 +4,34 @@ // Authors: spectral` // // NeoX // // // -// Desc: Atomic stuff // +// Desc: Current kernel state // //----------------------------------------------------------------------------// -#ifndef _KALCOMM_ATOMIC_H -#define _KALCOMM_ATOMIC_H +#ifndef _KALKERN_KE_STATE_H +#define _KALKERN_KE_STATE_H -#ifndef _KALCOMM_COMMON_H -#error "don't include common/types.h without common/common.h" +#ifndef _KALKERN_H +#include #endif -// atomic_t defined in common/types.h +// XXX improve this +enum kernel_states { + // a process is running in kernel mode + KSTATE_PROCESS, -#ifdef _KALEID_KERNEL + // the kernel is not running a process + KSTATE_KERNEL, -// only available in the kernel -#define cli() asm volatile ("cli") -#define sti() asm volatile ("sti") -#define hlt() asm volatile ("hlt") - -#endif + // the kernel is panicking + KSTATE_PANIC, + + // the kernel is booting + KSTATE_INIT, +}; + +extern uchar __kstate; +#define GetKernState() (__kstate) +#define SetKernState(x) (__kstate = (x)) #endif diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c deleted file mode 100644 index a8c746e..0000000 --- a/src/kaleid/linux/test-common.c +++ /dev/null @@ -1,45 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Test file for common/ // -//----------------------------------------------------------------------------// - -#include -#include -#include - -#define KEEP_KALCOMM_TYPES_MINIMAL - -#include "common/common.h" -#include "common/string.h" - -int main(int argc, char *argv[]) -{ - (void)argc; - (void)argv; - - const char *test1 = "test string\n"; - char *test2 = malloc(strlen(test1)); - char *test3 = malloc(strlen(test1)); - - printf("1\n"); - - #undef strlen - assert(strlen("test string") == _osk_strlen("test string")); - - #undef strcpy - assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0); - - - // tests done - printf("2\n"); - - free(test2); - free(test3); - - return 0; -} - diff --git a/src/kernel.ld b/src/kernel.ld index 86b66ea..5768560 100644 --- a/src/kernel.ld +++ b/src/kernel.ld @@ -1,4 +1,4 @@ -ENTRY(kstart) +ENTRY(StartKern) SECTIONS { . = 0x4000; /* XXX 0x4000 is temporary */ diff --git a/src/making.sh b/src/making.sh deleted file mode 100644 index e69de29..0000000 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 diff --git a/src/project-tree.txt b/src/project-tree.txt index 1304d6a..e0e3f4d 100644 --- a/src/project-tree.txt +++ b/src/project-tree.txt @@ -8,6 +8,13 @@ //----------------------------------------------------------------------------// src/ + | + x COPYING + x CONTACT + x ChangeLog + | + - Makefile + - kernel.ld | + boot/ | | @@ -20,39 +27,75 @@ src/ | - loader16.inc | - loader64.inc | | - | - types.h + | 0 | + kaleid/ | | + | + include/ + | | | + | | - kaleid.h + | | - kaldefs.h + | | - kaltypes.h + | | - kalassrt.h + | | - kalmask.h + | | - kalcrt.h + | | | + | | - kalkern.h + | | | + | | ~ kalkern/ -> ../kernel/ + | | | + | | 0 + | | + | | | + kernel/ | | | | | 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/ + | | | | + | | | - lock.c + | | | - lock.h + | | | | + | | | - panic.c + | | | - panic.h + | | | | + | | | - state.c + | | | - state.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 - | + | | | + | | - status.c + | | | + | | - string.c + | | - memory.c + | | - convert.c + | | - sprintf.c + | | | + | | + test/ + | | | | + | | | - test-common.c + | | | | + | | | 0 + | | 0 + | 0 0 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"