diff --git a/.gitignore b/.gitignore index c6127b3..3e8b80f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,15 @@ # Prerequisites *.d +# Test files +test-*.c + # Object files *.o *.ko *.obj *.elf +*.S # Linker output *.ilk diff --git a/LICENSE b/COPYING similarity index 100% rename from LICENSE rename to COPYING diff --git a/src/ChangeLog b/ChangeLog similarity index 91% rename from src/ChangeLog rename to ChangeLog index 27cc873..b019064 100644 --- a/src/ChangeLog +++ b/ChangeLog @@ -14,6 +14,6 @@ 2018/12/06 - Actually started project, began MBR, decided directories organization, created this file and others 2018/12/08 - MBR actually supports Long Mode Compatibility Verification - Added A20 line Enabling to MBR -2018/12/21 - Boot is now in two stages. First stage is 512 MBR code that loads second stage loader from FAT16. +2018/12/21 - Boot is now in two stages. First stage is 512B MBR code that loads second stage loader from FAT16. That second stage loader enables A20, switches into long mode and write colored text =D diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..71e74a6 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +#----------------------------------------------------------------------------# +# GNU GPL OS/K # +# # +# Authors: spectral` # +# NeoX # +# # +# Desc: Project Makefile # +#----------------------------------------------------------------------------# + +kernel: + cpp ./Makefile.in > Makefile.out + python ./build/idttool.py + make kernel -f Makefile.out.2 + rm Makefile.out Makefile.out.2 + +kernel-asm: + cpp -D_TO_ASM ./Makefile.in > Makefile.out + python ./build/idttool.py + make kernel -f Makefile.out.2 + rm Makefile.out Makefile.out.2 + +tests: + cpp -D_TESTS ./Makefile.in > Makefile.out + python ./build/idttool.py + make tests -f Makefile.out.2 + rm Makefile.out Makefile.out.2 + diff --git a/Makefile.in b/Makefile.in new file mode 100644 index 0000000..5c26235 --- /dev/null +++ b/Makefile.in @@ -0,0 +1,107 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Project Makefile // +//----------------------------------------------------------------------------// + +// The madman's Makefile +#include "build/preproc.h" + +CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" +CC2NAME=gcc +COPTIM=-O2 +CWARNS=-Wall -Wextra -Wshadow -Wpedantic +CINCLUDES=-isystem./kaleid/include + +CFLAGS1=-std=gnu11 -nostdlib -ffreestanding -mcmodel=large +CFLAGS2=-m64 -masm=intel -mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CFLAGS=$(CFLAGS1) $(CFLAGS2) $(SFLAG) + +CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) + +ASM=nasm +ASMFLAGS= +BOOTFLAGS=-f bin + +BINDIR=./build/bin +OBJDIR=./build/obj + +BOOTDIR=boot +COMMDIR=kaleid/common +KERNDIR=kaleid/kernel +SYSTDIR=kaleid/system +LINXDIR=kaleid/common/test + +all: bootloader kernel + +boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.asm -o $(OBJDIR)/boot/mbr.bin + +boot.loader.s: $(BOOTDIR)/loader.s + $(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.asm -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=COBJ6(string, status, rand, memory, arith, strtol) COBJ4(itoa, ltoa, utoa, ultoa) COBJ4(atoi, atol, atou, atoul) + +TCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES) +KCC=$(CC) -T ./build/kernel.ld -D_OSK_SOURCE -D_KALEID_KERNEL + +comm-convert: + COMPILE_CONVRT1(itoa) -D_NEED_ITOA + COMPILE_CONVRT1(ltoa) -D_NEED_LTOA + COMPILE_CONVRT1(utoa) -D_NEED_UTOA + COMPILE_CONVRT1(ultoa) -D_NEED_ULTOA + COMPILE_CONVRT2(atoi) -D_NEED_ATOI + COMPILE_CONVRT2(atol) -D_NEED_ATOL + COMPILE_CONVRT2(atou) -D_NEED_ATOU + COMPILE_CONVRT2(atoul) -D_NEED_ATOUL + +common: comm-convert + COMPILE_COMMON(rand) + COMPILE_COMMON(arith) + COMPILE_COMMON(string) + COMPILE_COMMON(status) + COMPILE_COMMON(memory) + COMPILE_COMMON(strtol) + +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=KOBJ4(init/init, init/table, ke/panic, ke/terminal) + +kernel: common + COMPILE_KERNEL(init/init) + COMPILE_KERNEL(init/table) + COMPILE_KERNEL(ke/panic) + COMPILE_KERNEL(ke/terminal) + LINK_KERNEL(kaleid-kernel.elf) + +//----------------------------------------------------------------------------# + diff --git a/ProjectTree b/ProjectTree new file mode 100644 index 0000000..90c5627 --- /dev/null +++ b/ProjectTree @@ -0,0 +1,107 @@ +#------------------------------------------------------------------------------# +# GNU GPL OS/K # +# # +# Authors: spectral` # +# NeoX # +# # +# Desc: Project Tree # +#------------------------------------------------------------------------------# + +src/ + | + x COPYING + x README.md + x ChangeLog + | + - Makefile + - Makefile.in + | + + boot/ + | | + | x folder.desc + | | + | - mbr.asm + | - mbr.inc + | | + | - loader.asm + | - loader16.inc + | - loader64.inc + | | + | 0 + | + + kaleid/ + | | + | + include/ + | | | + | | - kaleid.h + | | - kalkern.h + | | | + | | + common/ + | | | | + | | | - kaldefs.h + | | | - kaltypes.h + | | | - kalerror.h + | | | - kalassrt.h + | | | - kallims.h + | | | - kalmask.h + | | | - kalcrt.h + | | | | + | | | 0 + | | | + | | + kernel/ + | | | | + | | | - kernbase.h + | | | - kernlocks.h + | | | - kernterm.h + | | | | + | | | 0 + | | 0 + | | + | | + | + kernel/ + | | | + | | - kernel.ld + | | | + | | + init/ + | | | | + | | | - init.c + | | | | + | | | 0 + | | | + | | + ke/ + | | | | + | | | - panic.c + | | | - table.c + | | | - terminal.c + | | | | + | | | 0 + | | 0 + | | + | + common/ + | | | + | | - status.c + | | | + | | - arith.c + | | - rand.c + | | | + | | - atoi.c + | | - itoa.c + | | - strtol.c + | | | + | | - memory.c + | | - string.c + | | - sprintf.c + | | | + | | 0 + | 0 + | + + build/ + | | + | - preproc.h + | - iddtool.h + | | + | + bin/ + | + obj/ + | | + | 0 + 0 diff --git a/README.md b/README.md deleted file mode 100644 index dbcf818..0000000 --- a/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# GNU-GPL OS/K (OS on Kaleid) - -Fully open-source operating system from scratch (WIP), released under the GNU GPL version 3.0 - -Branch boot : Development focused on the bootloader of OS/K - -For changelog, see src/ChangeLog. - -For structure of the sources, see src/project-tree.txt. - -For code conventions, see src/project-style.txt. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..1c449a7 --- /dev/null +++ b/Readme.md @@ -0,0 +1,12 @@ +# GNU-GPL OS/K (OS on Kaleid) + +Fully open-source operating system from scratch (WIP), released under the GNU GPL version 3.0 + +Branch kaleid : Development focused on the Kaleid Kernel. + +For changelog, see ChangeLog. + +For structure of the sources, see src/project-tree.txt. + +Note that every file within OS/K is written using spaces for tabulation, with each +tabulation being 4 spaces long. diff --git a/boot/folder.desc b/boot/folder.desc new file mode 100644 index 0000000..6322bc3 --- /dev/null +++ b/boot/folder.desc @@ -0,0 +1,23 @@ +--------------------------------------------------------------------- + GNU GPL OS/K + + Authors: spectral` + NeoX + + Desc: Folder description - "boot" +--------------------------------------------------------------------- + + +This folder contains the source for OS/K's bootloader. +OS/K being intended to only run on x86-64 systems, we have not divided +this folder into one sub-folder per architecture. + +It is divided in two parts each of exactly 512 bytes: + - mbr.s (and its auxiliary file mbr.inc) + This is our Master Boot Record (MBR). It switches to long mode + (64 bits mode) then loads the second half of the bootloader. + The MBR must be placed precisely on the first sector of the hard drive. + + - loader.s + This is the Kernel Loader. XXX + diff --git a/src/boot/loader.s b/boot/loader.asm similarity index 100% rename from src/boot/loader.s rename to boot/loader.asm diff --git a/src/boot/loader16.inc b/boot/loader16.inc similarity index 100% rename from src/boot/loader16.inc rename to boot/loader16.inc diff --git a/src/boot/loader64.inc b/boot/loader64.inc similarity index 100% rename from src/boot/loader64.inc rename to boot/loader64.inc diff --git a/src/boot/mbr.s b/boot/mbr.asm similarity index 100% rename from src/boot/mbr.s rename to boot/mbr.asm diff --git a/src/boot/mbr.inc b/boot/mbr.inc similarity index 100% rename from src/boot/mbr.inc rename to boot/mbr.inc diff --git a/bin/disk.img b/build/bin/disk.img similarity index 100% rename from bin/disk.img rename to build/bin/disk.img diff --git a/bin/loader.bin b/build/bin/loader.bin similarity index 100% rename from bin/loader.bin rename to build/bin/loader.bin diff --git a/bin/mbr.bin b/build/bin/mbr.bin similarity index 100% rename from bin/mbr.bin rename to build/bin/mbr.bin diff --git a/build/idttool.py b/build/idttool.py new file mode 100644 index 0000000..90dfa0e --- /dev/null +++ b/build/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/kernel.ld b/build/kernel.ld similarity index 72% rename from src/kernel.ld rename to build/kernel.ld index 86b66ea..e96beb8 100644 --- a/src/kernel.ld +++ b/build/kernel.ld @@ -1,9 +1,9 @@ -ENTRY(kstart) +ENTRY(StartKern) SECTIONS { - . = 0x4000; /* XXX 0x4000 is temporary */ + . = 1M; - .text : AT(ADDR(.text) - 0x4000) + .text : AT(ADDR(.text) - 1M) { _code = .; *(.text) @@ -11,21 +11,21 @@ SECTIONS . = ALIGN(4096); } - .data : AT(ADDR(.data) - 0x4000) + .data : AT(ADDR(.data) - 1M) { _data = .; *(.data) . = ALIGN(4096); } - .eh_frame : AT(ADDR(.eh_frame) - 0x4000) + .eh_frame : AT(ADDR(.eh_frame) - 1M) { _ehframe = .; *(.eh_frame) . = ALIGN(4096); } - .bss : AT(ADDR(.bss) - 0x4000) + .bss : AT(ADDR(.bss) - 1M) { _bss = .; *(.bss) diff --git a/obj/boot/loader.bin b/build/obj/boot/loader.bin similarity index 100% rename from obj/boot/loader.bin rename to build/obj/boot/loader.bin diff --git a/obj/boot/mbr.bin b/build/obj/boot/mbr.bin similarity index 100% rename from obj/boot/mbr.bin rename to build/obj/boot/mbr.bin diff --git a/build/obj/kaleid/common/.placeholder b/build/obj/kaleid/common/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build/obj/kaleid/common/test/.paceholder b/build/obj/kaleid/common/test/.paceholder new file mode 100644 index 0000000..e69de29 diff --git a/build/obj/kaleid/kernel/init/.placeholder b/build/obj/kaleid/kernel/init/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build/obj/kaleid/kernel/io/.placeholder b/build/obj/kaleid/kernel/io/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build/obj/kaleid/kernel/ke/.placeholder b/build/obj/kaleid/kernel/ke/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/build/preproc.h b/build/preproc.h new file mode 100644 index 0000000..f690c6e --- /dev/null +++ b/build/preproc.h @@ -0,0 +1,42 @@ +// 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_CONVRT1(file) $(CCC) _CSPREF $(COMMDIR)/itoa.c -o $(COBJDIR)/file._OUTFIX +#define COMPILE_CONVRT2(file) $(CCC) _CSPREF $(COMMDIR)/atoi.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) COBJ1(x1) $(COBJDIR)/x2.o +#define COBJ3(x1,x2,x3) COBJ2(x1,x2) $(COBJDIR)/x3.o +#define COBJ4(x1,x2,x3,x4) COBJ3(x1,x2,x3) $(COBJDIR)/x4.o +#define COBJ5(x1,x2,x3,x4,x5) COBJ4(x1,x2,x3,x4) $(COBJDIR)/x5.o +#define COBJ6(x1,x2,x3,x4,x5,x6) COBJ5(x1,x2,x3,x4,x5) $(COBJDIR)/x6.o +#define COBJ7(x1,x2,x3,x4,x5,x6,x7) COBJ6(x1,x2,x3,x4,x5,x6) $(COBJDIR)/x7.o +#define COBJ8(x1,x2,x3,x4,x5,x6,x7,x8) COBJ7(x1,x2,x3,x4,x5,x6,x7) $(COBJDIR)/x8.o + +#define KOBJ1(x1) $(KOBJDIR)/x1.o +#define KOBJ2(x1,x2) KOBJ1(x1) $(KOBJDIR)/x2.o +#define KOBJ3(x1,x2,x3) KOBJ2(x1,x2) $(KOBJDIR)/x3.o +#define KOBJ4(x1,x2,x3,x4) KOBJ3(x1,x2,x3) $(KOBJDIR)/x4.o +#define KOBJ5(x1,x2,x3,x4,x5) KOBJ4(x1,x2,x3,x4) $(KOBJDIR)/x5.o +#define KOBJ6(x1,x2,x3,x4,x5,x6) KOBJ5(x1,x2,x3,x4,x5) $(KOBJDIR)/x6.o +#define KOBJ7(x1,x2,x3,x4,x5,x6,x7) KOBJ6(x1,x2,x3,x4,x5,x6) $(KOBJDIR)/x7.o +#define KOBJ8(x1,x2,x3,x4,x5,x6,x7,x8) KOBJ7(x1,x2,x3,x4,x5,x6,x7) $(KOBJDIR)/x8.o + diff --git a/src/kaleid/kernel/io/ports.h b/kaleid/common/arith.c similarity index 63% rename from src/kaleid/kernel/io/ports.h rename to kaleid/common/arith.c index b2adfbe..5ac8338 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/kaleid/common/arith.c @@ -4,18 +4,30 @@ // Authors: spectral` // // NeoX // // // -// Desc: Ports I/O // +// Desc: Arithmetical functions // //----------------------------------------------------------------------------// -#ifndef _KALKERN_IO_PORTS_H -#define _KALKERN_IO_PORTS_H +// do not mask anything +#define _KALMASK_H +#include -#include "common/common.h" +int _osk_abs(int x) +{ + return abs(x); +} -#define outb(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) +long _osk_labs(long x) +{ + return labs(x); +} -uchar inb(port_t); -ushort inw(port_t); +div_t _osk_div(int x, int y) +{ + return div(x, y); +} -#endif +ldiv_t _osk_ldiv(long x, long y) +{ + return ldiv(x, y); +} diff --git a/kaleid/common/atoi.c b/kaleid/common/atoi.c new file mode 100644 index 0000000..f5a22a6 --- /dev/null +++ b/kaleid/common/atoi.c @@ -0,0 +1,37 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Conversion utilities - atoi family // +//----------------------------------------------------------------------------// + +#include + +// +// String to integer +// Do not change errno +// +#define _ATOI_IMPL(_Name, _Type, _Func) \ + _Type _Name(const char *str) { \ + error_t old = errno; \ + _Type ret = (_Type)_Func(str, NULL, 0); \ + errno = old; \ + return ret; \ + } + + +// ISO C does not allow extra ‘;’ outside of a function +#if defined(_NEED_ATOI) +_ATOI_IMPL(atoi, int, strtol) +#elif defined(_NEED_ATOL) +_ATOI_IMPL(atol, long, strtol) +#elif defined(_NEED_ATOU) +_ATOI_IMPL(atou, uint, strtoul) +#elif defined(_NEED_ATOUL) +_ATOI_IMPL(atoul, ulong, strtoul) +#else +#error "What am I supposed to declare?" +#endif + diff --git a/src/kaleid/common/lib/convert.c b/kaleid/common/itoa.c similarity index 50% rename from src/kaleid/common/lib/convert.c rename to kaleid/common/itoa.c index 0f76565..e24a41a 100644 --- a/src/kaleid/common/lib/convert.c +++ b/kaleid/common/itoa.c @@ -4,49 +4,78 @@ // Authors: spectral` // // NeoX // // // -// Desc: Conversion utilities // +// Desc: Conversion utilities - itoa family // //----------------------------------------------------------------------------// -#include "common/convert.h" +#include // -// Digits table for bases <=36 +// Digits table for bases <=36 // -static const char digits[36] = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - +static const char digits[] = + "0123456789abcdefghijklmnopqrstuvwxyz"; // -// Integer to string in any base between 2 and 36 (included) +// 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; + // + // Only handle base 2 -> 36 + // if (base < 2 || base > 36) return NULL; - - // deal with negatives + +#if defined(_NEED_ITOA) || defined(_NEED_LTOA) + // + // Deal with negatives + // if (i < 0) { neg = 1; i = -i; } - - // deal with zero separatly +#endif + + // + // Deal with zero separately + // if (i == 0) { *str++ = '0'; } - - // compute digits... in reverse order + + // + // 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); + // + // Reverse the string + // + return strrev2(orig); } diff --git a/kaleid/common/memory.c b/kaleid/common/memory.c new file mode 100644 index 0000000..3497a38 --- /dev/null +++ b/kaleid/common/memory.c @@ -0,0 +1,245 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: mem*() functions // +//----------------------------------------------------------------------------// + +#include + +//------------------------------------------// +// memset() family // +//------------------------------------------// + +// +// Set "bytes"-many bytes starting from ptr to val +// +void *memset(void *ptr, int val, size_t bytes) +{ + uchar *uptr = (uchar *)ptr; + + // + // Deal with bytes before start of the first aligned qword + // + while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // + // At this point we're qword-aligned + // + if (bytes > QWORD_SIZE) { + const ulong uval = ((ulong)val << 56) | ((ulong)val << 48) + | ((ulong)val << 40) | ((ulong)val << 32) + | ((ulong)val << 24) | ((ulong)val << 16) + | ((ulong)val << 8) | ((ulong)val); + + ulong *uqptr = (ulong *)ptr; + + // + // Moving fast, qword by qword + // + while (bytes > QWORD_SIZE) { + *uqptr++ = uval; + bytes -= QWORD_SIZE; + } + + uptr = (uchar *)(ulong)uqptr; + } + + // + // Deal with the few remaining bytes + // + while (bytes--) *uptr++ = (uchar)val; + + return ptr; +} + +// +// Set "words"-many words starting from ptr to val +// +void *memsetw(void *ptr, int val, size_t words) +{ + ushort *uptr = (ushort *)ptr; + + // + // Check whether we can we do this an aligned way + // + if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { + // + // We can't, so we write word by word all the way up + // + while (words--) *uptr++ = (ushort)val; + return uptr; + } + + while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) { + *uptr++ = (ushort)val; + } + + if (words > QWORDS_TO_WORDS(1)) { + const ulong uval = ((ulong)val << 48) | ((ulong)val << 32) + | ((ulong)val << 16) | ((ulong)val); + + ulong *uqptr = (ulong *)uptr; + + while (words > QWORDS_TO_WORDS(1)) { + words -= QWORDS_TO_WORDS(1); + *uqptr++ = uval; + } + + uptr = (ushort *)(ulong)uqptr; + } + + while (words--) *uptr++ = (ushort)val; + + return ptr; +} + +// +// Set "dwords"-many dwords starting from ptr to val +// XXX unimplemented +// +void *memsetd(void *ptr, int val, size_t dwords) +{ + (void)val; + (void)dwords; + + return ptr; +} + +// +// Set "qwords"-many qwords starting from ptr to val +// +void *memsetq(void *ptr, long val, size_t qwords) +{ + ulong *uptr = (ulong *)ptr; + + // + // There's no need to check for alignment + // + while (qwords--) *uptr++ = (ulong)val; + + return ptr; +} + +//------------------------------------------// +// Other mem*() functions // +//------------------------------------------// + +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + return memsetb(ptr, 0, bytes); +} + +// +// Copy "bytes"-many bytes of src to dst +// Does not deal with overlapping blocks (memmove's job) +// +void *memcpy(void *restrict dst, const void *restrict src, size_t bytes) +{ + const ulong *usrc = (const ulong *)src; + ulong *udst = (ulong *)dst; + + if unlikely (bytes == 0) return dst; + + // + // Can align both src and dst at once at once? + // + if unlikely ((ulong)src % WORD_ALIGN == 1 + && (ulong)dst % WORD_ALIGN == 1) { + const uchar *ubsrc = (const uchar *)usrc; + uchar *ubdst = (uchar *)udst; + + // + // Yes we can, we're guaranteed to be word-aligned now + // + *ubdst++ = *ubsrc++; + bytes--; + + udst = (ulong *)ubdst; + usrc = (ulong *)ubsrc; + } + + const ushort *uwsrc = (const ushort *)usrc; + ushort *uwdst = (ushort *)udst; + + // + // Align either dst or src for qword access + // + while ((ulong)dst % QWORD_ALIGN > 0 + && (ulong)src % QWORD_ALIGN > 0 + && bytes > WORD_SIZE) { + + *uwdst++ = *uwsrc++; + bytes -= WORD_SIZE; + } + + udst = (ulong *)uwdst; + usrc = (ulong *)uwsrc; + + // + // This should be most of the job + // + while (bytes > QWORD_SIZE) { + *udst++ = *usrc++; + bytes -= QWORD_SIZE; + } + + const uchar *ubsrc = (const uchar *)usrc; + ushort *ubdst = (ushort *)udst; + + // + // Deal with the few bytes left + // + while (bytes--) *ubdst ++ = *ubsrc++; + + return dst; +} + +// +// Move memory from src to dest, even if they overlap +// +void *memmove(void *dst, const void *src, size_t bytes) +{ + const uchar *usrc = src; + uchar *udst = dst; + + // + // Can we use memcpy() safely? + // + if (udst < usrc) { + return memcpy(dst, src, bytes); + } + + // + // No, so we go backwards + // + uchar *usrc_end = (uchar *)usrc + bytes - 1; + uchar *udst_end = udst + bytes - 1; + while (bytes--) *udst_end-- = *usrc_end--; + + return dst; +} + +// +// Compare memory areas +// +int memcmp(const void *ptr1, const void *ptr2, size_t bytes) +{ + const uchar *uptr1 = ptr1; + const uchar *uptr2 = ptr2; + + while (bytes--) { + if (*uptr1++ != *uptr2++) { + return uptr1[-1] < uptr2[-1] ? -1 : 1; + } + } + + return 0; +} diff --git a/kaleid/common/rand.c b/kaleid/common/rand.c new file mode 100644 index 0000000..fe576ef --- /dev/null +++ b/kaleid/common/rand.c @@ -0,0 +1,34 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: RNG related functions // +//----------------------------------------------------------------------------// + +#include + +// +// Seed value +// +static ulong next = 7756; + +// +// Returns a pseudo-random integer +// To be improved +// +int rand(void) +{ + next = next * 1103515245 + 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/lib/sprintf.c b/kaleid/common/sprintf.c similarity index 77% rename from src/kaleid/common/lib/sprintf.c rename to kaleid/common/sprintf.c index e26ded9..a8dd3ba 100644 --- a/src/kaleid/common/lib/sprintf.c +++ b/kaleid/common/sprintf.c @@ -7,20 +7,20 @@ // Desc: sprintf()-related functions // //----------------------------------------------------------------------------// -#include "common/string.h" +#include // -// Format str according to fmt using ellipsed arguments +// Format str according to fmt using ellipsed arguments // int sprintf(char *str, const char *fmt, ...) { int ret; va_list ap; - + va_start(ap); ret = vsnprintf(str, SIZE_T_MAX, fmt, ap); va_end(ap); - + return ret; } @@ -30,39 +30,45 @@ int vsprintf(char *str, const char *fmt, va_list ap) } // -// (v)sprintf() but with a size limit: no more than n bytes are written in str -// XXX null termination behavior? +// (v)sprintf() but with a size limit: no more than n bytes are written in str // int snprintf(char *str, size_t n, const char *fmt, ...) { int ret; va_list ap; - + va_start(ap); ret = vsnprintf(str, n, fmt, ap) va_end(ap); - + return ret; } int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) { int ret = 0; - - while (*fmt && ret < n) { + + // + // Go throught the format string + // + while (*fmt) { if (*fmt != '%') { - *str++ = *fmt++; - ret++; + // + // Even if we don't have any more room we still increase ret + // + if (ret++ < n) { + *str++ = *fmt++; + } continue; } - + switch (*fmt) { case 'd': default: break; } } - + return ret; } diff --git a/src/kaleid/common/lib/status.c b/kaleid/common/status.c similarity index 96% rename from src/kaleid/common/lib/status.c rename to kaleid/common/status.c index 5e8115c..457e741 100644 --- a/src/kaleid/common/lib/status.c +++ b/kaleid/common/status.c @@ -7,8 +7,11 @@ // Desc: Implementation of describe_status() // //----------------------------------------------------------------------------// -#include "common/common.h" +#include +error_t errno = 0; + +/* static const char *descriptions[] = { [-SUCCESS] = "Success", [-FAILED] = "Failed (no precision)", @@ -28,4 +31,4 @@ const char *describe_status(status_t status) // XXX return ""; } - +*/ diff --git a/kaleid/common/string.c b/kaleid/common/string.c new file mode 100644 index 0000000..fafaf89 --- /dev/null +++ b/kaleid/common/string.c @@ -0,0 +1,301 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: String-related functions // +//----------------------------------------------------------------------------// + + +#include + +// +// Compare two strings +// +int strcmp(const char *str1, const char *str2) +{ + while (*str1 == *str2 && *str2) str1++, str2++; + + return *(uchar *)str1 - *(uchar *)str2; +} + +// +// Compare at most n bytes of two strings +// +int strncmp(const char *str1, const char *str2, size_t n) +{ + size_t it = 0; + + while (*str1 == *str2 && *str2 && it < n) str1++, str2++, it++; + + return *(uchar *)str1 - *(uchar *)str2; +} + +// +// Return str's length +// +size_t strlen(const char *str) +{ + const char *base = str; + + while (*str) str++; + + return str - base; +} + +// +// Return a pointer to the first occurence of ch in str, +// or str's null-terminator if none is found +// +char *strchrnul(const char *str, int ch) +{ + while ((*str && *str != (char)ch)) str++; + + return (char *)str; +} + +// +// Return a pointer to the first occurence of ch in str, +// NULL if none is found +// +char *strchr(const char *str, int ch) +{ + while ((*str && *str != (char)ch)) str++; + + return *str ? (char *)str : NULL; +} + +// +// Return a point to the last occurence of ch in str, +// NULL if none is found +// +char *strrchr(const char *str, int ch) +{ + char *ptr = NULL; + + while (*str) { + if (*str == ch) { + ptr = (char *)str; + } + str++; + } + + return ptr; +} + +// +// Return the length of the longest inital segment of str +// that only contains characters in acc +// +size_t strspn(const char *str, const char *acc) +{ + const char *ptr = str; + + while (*ptr && strchr(acc, *ptr) != NULL) ptr++; + + return ptr - str; +} + +// +// Return the length of the longest initial segment of str +// that does not contain any character in rej +// +size_t strcspn(const char *str, const char *rej) +{ + const char *ptr = str; + + while (*ptr && strchr(rej, *ptr) == NULL) ptr++; + + return ptr - str; +} + +// +// Return the first occurence in str of any byte in acc +// +char *strpbrk(const char *str, const char *acc) +{ + str += strcspn(str, acc); + + return *str ? (char *)str : NULL; +} + +// +// Return the first occurence of the substring needle +// in the string haystack, NULL if none is found +// Null-terminators aren't compared +// +char *strstr(const char *haystack, const char *needle) +{ + const size_t needle_size = strlen(needle); + + // + // Moves haystack to first occurence of the needle's first byte + // + while ((haystack = strchr(haystack, *needle)) != NULL) { + if (strncmp(haystack, needle, needle_size) == 0) { + return (char *)haystack; + } + } + + return NULL; +} + +// +// Tokenize a string, using saveptr as a savestate +// We let a segmentation fault happen if *saveptr == NULL +// +char *strtok_r(char *restrict str, const char *restrict delim, char **restrict saveptr) +{ + assert(*saveptr != NULL); + + if (str == NULL) str = *saveptr; + + // + // Skip initial segments composed only of delimiters + // + str += strspn(str, delim); + + // + // If str is empty, store it in saveptr so that next call + // still finds an empty strings and returns NULL + // + if (*str == 0) { + *saveptr = str; + return NULL; + } + + char *ptr = str, *tok_end = strpbrk(str, delim); + + // + // If we found the last token, set *saveptr to a str's null-terminator + // Otherwise, null-terminate token and save next byte + // + + if (tok_end == NULL) { + while (*ptr) ptr++; + *saveptr = ptr; + } + + else { + *tok_end = 0; + *saveptr = tok_end + 1; + } + + return str; +} + +// +// Tokenize a string in a very thread-unsafe way +// +char *strtok(char *restrict str, const char *restrict delim) +{ + static char *saveptr = NULL; + + KalAssert(FALSE); + + if (str) saveptr = str; + + return strtok_r(str, delim, &saveptr); +} + +// +// Copy the string src into dest +// +char *strcpy(char *restrict dest, const char *restrict src) +{ + char *base = dest; + + while ((*dest++ = *src++)); + + return base; +} + +// +// strcpy() but always writes n bytes +// Will not null-terminate for strings longer than n bytes +// +char *strncpy(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it; + + for (it = 0; it < n && src[it]; it++) { + dest[it] = src[it]; + } + + while (it < n) dest[it++] = 0; + + return dest; +} + +// +// Copies at most n-1 bytes from src to dest, then fills +// the rest with 0; dest[n] is guanranteed to be '\0' +// +// Returns TRUE if dest would have been null-terminated +// by ordinary strncpy(), and FALSE otherwise +// +int xstrncpy(char *restrict dest, const char *restrict src, size_t n) +{ + size_t it; + + for (it = 0; it < n - 1 && src[it]; it++) { + dest[it] = src[it]; + } + + // + // Was the copy complete? + // + if (it == n) { + if (dest[n] == 0) { + return TRUE; + } + + dest[n] = 0; + return FALSE; + } + + while (it < n) dest[it++] = 0; + + return TRUE; +} + +// +// XXX strcat family +// +char *strcat (char *restrict, const char *restrict); +char *strncat (char *restrict, const char *restrict, size_t); +int *xstrncat(char *restrict, const char *restrict, size_t); + +// +// Reverses the string src, putting the result into dest +// +char *strrev(char *restrict dest, const char *restrict src) +{ + char *orig = dest; + size_t n = strlen(src); + + dest[n--] = '\0'; + + while ((*dest++ = src[n--])); + + return orig; +} + +// +// Reverses a string, modifying it +// +char *strrev2(char *str) +{ + char ch, *orig = str; + size_t n = strlen(str); + char *temp = str + n - 1; + + while (temp > str) { + ch = *temp; + *temp-- = *str; + *str++ = ch; + } + + return orig; +} diff --git a/src/kaleid/kernel/init.h b/kaleid/common/strtol.c similarity index 62% rename from src/kaleid/kernel/init.h rename to kaleid/common/strtol.c index bb80788..2e1aad6 100644 --- a/src/kaleid/kernel/init.h +++ b/kaleid/common/strtol.c @@ -4,16 +4,24 @@ // Authors: spectral` // // NeoX // // // -// Desc: Include file for init.c // +// Desc: strto(u)l functions // //----------------------------------------------------------------------------// -#ifndef _KALKERN_INIT_H -#define _KALKERN_INIT_H +#include -#include "common/common.h" +long strtol(const char *str, char **endp, int base) { + (void)str; + (void)endp; + (void)base; + errno = ENOSYS; + return 0; +} -// kernel entry point -void kstart(void); - -#endif +ulong strtoul(const char *str, char **endp, int base) { + (void)str; + (void)endp; + (void)base; + errno = ENOSYS; + return 0; +} diff --git a/kaleid/include/common/kalassrt.h b/kaleid/include/common/kalassrt.h new file mode 100644 index 0000000..cf4ddbb --- /dev/null +++ b/kaleid/include/common/kalassrt.h @@ -0,0 +1,108 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid assert() support // +//----------------------------------------------------------------------------// + +#ifndef _KALASSRT_H +#define _KALASSRT_H + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef noreturn +#define noreturn __attribute__((__noreturn__)) +#endif + +#ifndef unlikely +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +#ifndef static_assert +#define static_assert _Static_assert +#endif + +//------------------------------------------// +// API compatibility checks // +//------------------------------------------// + +#define _SA_MSG "Incompatible type sizes" +static_assert(sizeof(char) == 1, _SA_MSG); +static_assert(sizeof(short) == 2, _SA_MSG); +static_assert(sizeof(int) == 4, _SA_MSG); +static_assert(sizeof(long) == 8, _SA_MSG); +static_assert(sizeof(void *) == 8, _SA_MSG); +#undef _SA_MSG + +//------------------------------------------// +// When debugging // +//------------------------------------------// + +#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(assert) + +#ifdef _OSK_SOURCE + +// +// Failed assert handler +// +noreturn void _assert_handler(const char *, const char *, int, const char *); + +// +// Checks whether (x) holds, if not call _assert_handler +// +#define assert(x) \ + do { \ + if unlikely (!(x)) \ + _assert_handler(#x, __FILE__, __LINE__, __func__); \ + } while (0); + +#else + +// +// When not building for OS/K, use the system's assert +// +#include + +#endif + +//------------------------------------------// +// When not debugging // +//------------------------------------------// + +#else + +#ifndef NDEBUG +#define NDEBUG 1 +#endif + +#ifndef _NO_DEBUG +#define _NO_DEBUG 1 +#endif + +#ifndef assert +#define assert(x) ((void)0) +#endif + +#endif + +//------------------------------------------// +// Aliases for assert() // +//------------------------------------------// + +#ifndef Assert +#define Assert assert +#endif + +#ifndef KalAssert +#define KalAssert assert +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/common/kalcrt.h b/kaleid/include/common/kalcrt.h new file mode 100644 index 0000000..75e34e1 --- /dev/null +++ b/kaleid/include/common/kalcrt.h @@ -0,0 +1,225 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C runtime library // +//----------------------------------------------------------------------------// + +#ifndef _KALCRT_H +#define _KALCRT_H + +//------------------------------------------// +// Typedefs // +//------------------------------------------// + +#ifndef __error_t_defined +#define __error_t_defined +typedef int error_t; +#endif + +#ifndef __size_t_defined +#define __size_t_defined +typedef unsigned long size_t; +#endif + +#ifndef __va_list_defined +#define __va_list_defined +typedef __builtin_va_list va_list; +#endif + +#ifndef __div_t_defined +#define __div_t_defined +typedef struct { int quot, rem; } div_t; +#endif + +#ifndef __ldiv_t_defined +#define __ldiv_t_defined +typedef struct { long quot, rem; } ldiv_t; +#endif + +//------------------------------------------// +// Global variables // +//------------------------------------------// + +extern error_t errno; + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef _NO_MASK +#define _NO_MASK +#endif + +//------------------------------------------// +// va_list utilities // +//------------------------------------------// + +#ifndef va_start +#define va_start __builtin_va_start +#endif + +#ifndef va_arg +#define va_arg __builtin_va_arg +#endif + +#ifndef va_copy +#define va_copy __builtin_va_copy +#endif + +#ifndef va_end +#define va_end __builtin_va_end +#endif + +//------------------------------------------// +// Memory management utilities // +//------------------------------------------// + +#ifndef memsetb +#define memsetb memset +#endif + +#ifndef memchrb +#define memchrb memchr +#endif + +void *memset(void *, int, size_t); +void *memsetw(void *, int, size_t); +void *memsetd(void *, int, size_t); +void *memsetq(void *, long, size_t); + +void *memchr(const void *, int, size_t); +void *memchrw(const void *, int, size_t); +void *memchrd(const void *, int, size_t); +void *memchrq(const void *, long, size_t); + +void *memrchr(const void *, int, size_t); + +void *memcpy(void *restrict, const void *restrict, size_t); +void *memmove(void *, const void *, size_t); + +void *memzero(void *, size_t); +int memcmp(const void *, const void *, size_t); + +//------------------------------------------// +// String manipulation utilities // +//------------------------------------------// + +size_t strlen(const char *); +size_t strspn(const char *, const char *); +size_t strcspn(const char *, const char *); + +int strcmp(const char *, const char *); +int strncmp(const char *, const char *, size_t); + +char *strchr(const char *, int); +char *strrchr(const char *, int); +char *strchrnul(const char *, int); + +char *strstr(const char *, const char *); +char *strpbrk(const char *, const char *); + +char *strtok(char *restrict, const char *restrict); +char *strtok_r(char *restrict, const char *restrict, char **restrict); + +char *strcpy (char *restrict, const char *restrict); +char *strncpy (char *restrict, const char *restrict, size_t); +int xstrncpy(char *restrict, const char *restrict, size_t); + +char *strcat (char *restrict, const char *restrict); +char *strncat (char *restrict, const char *restrict, size_t); +int *xstrncat(char *restrict, const char *restrict, size_t); + +char *strrev(char *restrict, const char *restrict); +char *strrev2(char *); + +int sprintf(char *, const char *, ...); +int snprintf(char *, size_t, const char *, ...); +int vsprintf(char *, const char *, va_list); +int vsnprintf(char *, size_t, const char *, va_list); + +//------------------------------------------// +// Type conversion utilities // +//------------------------------------------// + +char *itoa(int, char *, int); +char *ltoa(long, char *, int); +char *utoa(unsigned int, char *, int); +char *ultoa(unsigned long, char *, int); + +int atoi(const char *); +long atol(const char *); +unsigned int atou(const char *); +unsigned long atoul(const char *); + +long strtol (const char *restrict, char **restrict, int); +unsigned long strtoul(const char *restrict, char **restrict, int); + +//------------------------------------------// +// RNG utilities // +//------------------------------------------// + +int rand(void); +void srand(unsigned int); + +//------------------------------------------// +// Time utilities // +//------------------------------------------// + +//------------------------------------------// +// Diverse utilities // +//------------------------------------------// + +char *strerror(int); +char *strsignal(int); + +//------------------------------------------// +// Arithmetical macros // +//------------------------------------------// + +#ifndef __abs +#define __abs +static inline int abs(int x) +{ + return x < 0 ? -x : x; +} +#endif + +#ifndef __labs +#define __labs +static inline long labs(long x) +{ + return x < 0 ? -x : x; +} +#endif + +#ifndef __div +#define __div +static inline div_t div(int __x, int __y) +{ + div_t __res; + __res.quot = __x/__y; + __res.rem = __x%__y; + return __res; +} +#endif + +#ifndef __ldiv +#define __ldiv +static inline ldiv_t ldiv(long __x, long __y) +{ + ldiv_t __res; + __res.quot = __x/__y; + __res.rem = __x%__y; + return __res; +} +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/common/kaldefs.h b/kaleid/include/common/kaldefs.h new file mode 100644 index 0000000..9dbf1be --- /dev/null +++ b/kaleid/include/common/kaldefs.h @@ -0,0 +1,92 @@ +//----------------------------------------------------------------------------// +// 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 // +//------------------------------------------// + +#ifndef __alignof_is_defined +#define __alignof_is_defined +#define alignof _Alignof +#endif + +#ifndef __alignas_is_defined +#define __alignas_is_defined +#define alignas _Alignas +#endif + +#ifndef __bool_true_false_are_defined +#define __bool_true_false_are_defined +# define bool _Bool +# define true 1 +# define false 0 +# ifndef TRUE +# define TRUE 1 +# endif +# ifndef FALSE +# define FALSE 0 +# endif +#endif + +//------------------------------------------// +// Attributes and macros // +//------------------------------------------// + +#ifndef PACKED +#define PACKED __attribute__((__packed__)) +#endif + +#ifndef noreturn +#define noreturn __attribute__((__noreturn__)) +#endif + +#ifndef likely +#define likely(x) (__builtin_expect((x), 1)) +#endif + +#ifndef unlikely +#define unlikely(x) (__builtin_expect((x), 0)) +#endif + +//------------------------------------------// +// API specific macros // +//------------------------------------------// + +#ifndef KALAPI +# define KALAPI +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/common/kalerror.h b/kaleid/include/common/kalerror.h new file mode 100644 index 0000000..ce8b2c4 --- /dev/null +++ b/kaleid/include/common/kalerror.h @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Values for errno_t and errno // +//----------------------------------------------------------------------------// + +#ifndef _KALERROR_H +#define _KALERROR_H + +//------------------------------------------// +// Preprocessor constants // +//------------------------------------------// + +// Everything went fine +#define EOK 0 + +// Operation not permitted +#define EPERM 1 + +// No such file or directory +#define ENOENT 2 + +// No such process +#define ESRCH 3 + +// Syscall interrupted (e.g. by signal) +#define EINTR 4 + +// I/0 error +#define EIO 5 + +// No such device or address +#define ENXIO 6 + +// Argument list too long +#define E2BIG 7 + +// Not an executable format +#define ENOEXEC 8 + +// Bad file number +#define EBADF 9 + +// Invalid argument +#define EINVAL 22 + +// Functionality not implemented +#define ENOSYS 38 + +// Component crashed +#define ECRASH 500 + +// System is panicking +#define EPANIC 600 + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/common/kallims.h b/kaleid/include/common/kallims.h new file mode 100644 index 0000000..9afd35d --- /dev/null +++ b/kaleid/include/common/kallims.h @@ -0,0 +1,117 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid type limits definitions // +//----------------------------------------------------------------------------// + +#ifndef _KALLIMS_H +#define _KALLIMS_H + +//------------------------------------------// +// Data sizes blocks // +//------------------------------------------// + +#ifndef DATA_SIZE_BLOCK +#define DATA_SIZE_BLOCK +# define BYTE_SIZE sizeof(char) +# define WORD_SIZE sizeof(short) +# define DWORD_SIZE sizeof(int) +# define QWORD_SIZE sizeof(long) +#endif + +#ifndef DATA_ALIGN_BLOCK +#define DATA_ALIGN_BLOCK +# define BYTE_ALIGN alignof(char) +# define WORD_ALIGN alignof(short) +# define DWORD_ALIGN alignof(int) +# define QWORD_ALIGN alignof(long) +#endif + +#ifndef DATA_BITS_BLOCK +#define DATA_BITS_BLOCK +# define BYTE_BIT 8 +# define CHAR_BIT (BYTE_SIZE * BYTE_BIT) +# define WORD_BIT (WORD_SIZE * BYTE_BIT) +# define DWORD_BIT (DWORD_SIZE * BYTE_BIT) +# define QWORD_BIT (QWORD_SIZE * BYTE_BIT) +# define SHORT_BIT WORD_BIT +# define INT_BIT DWORD_BIT +# define LONG_BIT QWORD_BIT +#endif + +#ifndef DATA_SHIFTS_BLOCK +#define DATA_SHIFTS_BLOCK +# define BYTES_TO_WORDS(B) ((B) >> 1) +# define BYTES_TO_DWORDS(B) ((B) >> 2) +# define BYTES_TO_QWORDS(B) ((B) >> 3) +# define WORDS_TO_BYTES(W) ((W) << 1) +# define WORDS_TO_DWORDS(W) ((W) >> 1) +# define WORDS_TO_QWORDS(W) ((W) >> 2) +# define DWORDS_TO_BYTES(D) ((D) << 2) +# define DWORDS_TO_WORDS(D) ((D) << 1) +# define DWORDS_TO_QWORDS(D) ((D) >> 1) +# define QWORDS_TO_BYTES(Q) ((Q) << 3) +# define QWORDS_TO_WORDS(Q) ((Q) << 2) +# define QWORDS_TO_DWORDS(Q) ((Q) << 1) +#endif + +//------------------------------------------// +// Numeric data limits // +//------------------------------------------// + +#ifndef DATA_MAX_LIMITS_BLOCK +#define DATA_MAX_LIMITS_BLOCK +# define SCHAR_MAX ((signed char) 0x7F) +# define SHRT_MAX ((short) 0x7FFF) +# define INT_MAX ((int) 0x7FFFFFFF) +# define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFF) +# define UCHAR_MAX ((unsigned char) 0xFF +# define USHRT_MAX ((unsigned short) 0xFFFF) +# define UINT_MAX ((unsigned int) 0xFFFFFFFF) +# define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFF) +#endif + +#ifndef DATA_MIN_LIMITS_BLOCK +#define DATA_MIN_LIMITS_BLOCK +# define SCHAR_MIN ((signed char) -SCHAR_MAX - 1) +# define SHRT_MIN ((short) -SHRT_MAX - 1) +# define INT_MIN ((int) -INT_MAX - 1) +# define LONG_MIN ((long) -LONG_MAX - 1L) +#endif + +#ifndef DATA_CHAR_LIMITS_BLOCK +#define DATA_CHAR_LIMITS_BLOCK +# ifdef __CHAR_UNSIGNED__ +# define CHAR_MIN ((char)0) +# define CHAR_MAX ((char)UCHAR_MAX) +# else +# define CHAR_MIN ((char)SCHAR_MIN) +# define CHAR_MAX ((char)SCHAR_MAX) +# endif +#endif + +#ifndef DATA_SPTYPES_LIMITS_BLOCK +#define DATA_SPTYPES_LIMITS_BLOCK +# define SSIZE_T_MIN LONG_MIN +# define SSIZE_T_MAX LONG_MAX +# define SIZE_T_MAX ULONG_MAX +#endif + +#ifdef NEED_MORE_USELESS_DATA +# define UCHAR_MIN ((unsigned char)0) +# define USHRT_MIN ((unsigned short)0) +# define UINT_MIN ((unsigned int)0) +# define ULONG_MIN ((unsigned long)0) +# ifdef STILL_NEED_MORE_USELESS_DATA +# error "Not enough useless data!" +# endif +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/common/kallist.h b/kaleid/include/common/kallist.h new file mode 100644 index 0000000..96c5f9d --- /dev/null +++ b/kaleid/include/common/kallist.h @@ -0,0 +1,271 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Doubly linked lists implementation // +//----------------------------------------------------------------------------// + +#ifndef _KALLIST_H +#define _KALLIST_H + +#ifdef _KALEID_KERNEL +#error "kallist.h - Not ready for kernel compilation" +#endif + +#ifndef _KALASSRT_H +#include +#endif + +#ifndef _KALKERN_LOCKS_H +#include +#endif + +// +// XXX ¯\_(ツ)_/¯ +// +void *malloc(long); +void free(void *); + +#define AllocMemory malloc +#define FreeMemory free + +//------------------------------------------// +// Data structures // +//------------------------------------------// + +typedef struct sListHead_t { + Lock_t *lock; + unsigned long length; + struct sListNode_t *first; + struct sListNode_t *last; +} ListHead_t; + +typedef struct sListNode_t { + void *data; + ListHead_t *head; + struct sListNode_t *prev; + struct sListNode_t *next; +} ListNode_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +// +// Create a list head with an extern lock +// +static inline ListHead_t +*CreateListHeadWithLock(Lock_t *lock) +{ + ListHead_t *head = AllocMemory(sizeof(ListHead_t)); + + if (head == NULL) return NULL; + + head->first = head->last = NULL; + head->length = 0; + + head->lock = lock; + + return head; +} + +// +// Create a liste head +// +static inline ListHead_t +*CreateListHead(void) +{ + return CreateListHeadWithLock(NULL); +} + +// +// Create a node +// +static inline ListNode_t +*CreateNode(void *data) +{ + ListNode_t *node = AllocMemory(sizeof(ListNode_t)); + + if (node == NULL) return NULL; + + node->data = data; + node->head = NULL; + node->prev = node->next = NULL; + + return node; +} + +// +// Prepend node at beginning of list +// +static inline ListHead_t +*PrependNode(ListHead_t *head, ListNode_t *node) +{ + KalAssert(head && node); + + node->head = head; + node->prev = NULL; + + if (head->length > 0) { + node->next = head->first; + head->first->prev = node; + head->first = node; + } + + else { + head->first = node; + head->last = node; + node->next = NULL; + } + + head->length++; + + return head; +} + +// +// Append node at end of list +// +static inline ListHead_t +*AppendNode(ListHead_t *head, ListNode_t *node) +{ + KalAssert(head && node); + + node->head = head; + node->next = NULL; + + if (head->length > 0) { + node->prev = head->last; + head->last->next = node; + head->last = node; + } + + else { + head->first = node; + head->last = node; + node->prev = NULL; + } + + head->length++; + + return head; +} + +// +// Insert node2 before node1 +// +static inline ListHead_t +*AddNodeBefore(ListHead_t *head, ListNode_t *node1, ListNode_t *node2) +{ + KalAssert(head && node1 && node2 && node1->head == head); + + if (head->first == node1) { + return PrependNode(head, node2); + } + + node2->head = head; + node2->next = node1; + node2->prev = node1->prev; + + // node1->prev does exist + // or node1 would be first + node1->prev->next = node2; + node1->prev = node2; + + head->length++; + + return head; +} + +// +// Insert node2 after node1 +// +static inline ListHead_t +*AddNodeAfter(ListHead_t *head, ListNode_t *node1, ListNode_t *node2) +{ + KalAssert(head && node1 && node2 && node1->head == head); + + if (head->last == node1) { + return AppendNode(head, node2); + } + + node2->head = head; + node2->prev = node1; + node2->next = node1->next; + + node1->next->prev = node2; + node1->next = node2; + + head->length++; + + return head; +} + +// +// Remove node of list +// +static inline ListHead_t +*RemoveNode(ListHead_t *head, ListNode_t *node) +{ + KalAssert(head && node && head->length > 0 && node->head == head); + + if (head->length == 1) { + head->first = head->last = NULL; + goto leave; + } + + if (head->first == node) { + head->first = node->next; + node->next->prev = NULL; + } + + else if (head->last == node) { + head->last = node->prev; + node->prev->next = NULL; + } + + else { + node->prev->next = node->next; + node->next->prev = node->prev; + } + +leave: + head->length--; + FreeMemory(node); + + return head; +} + +// +// Free a node +// +static inline void +DestroyNode(ListNode_t *node) +{ + KalAssert(node); + FreeMemory(node); +} + +// +// Free a list head +// +static inline void +DestroyListHead(ListHead_t *head) +{ + KalAssert(head); + FreeMemory(head); +} + +// +// Access a node's data +// +#define GetNodeData(node, type) ((type)(node)->data) + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/common/kalmask.h b/kaleid/include/common/kalmask.h new file mode 100644 index 0000000..0295bd0 --- /dev/null +++ b/kaleid/include/common/kalmask.h @@ -0,0 +1,114 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Masks for the functions in the KCRL // +//----------------------------------------------------------------------------// + +#ifndef _KALMASK_H +#define _KALMASK_H + +//------------------------------------------// + +#define memset _osk_memsetb +#define memchr _osk_memchrb + +#define memsetb _osk_memsetb +#define memsetw _osk_memsetw +#define memsetd _osk_memsetd +#define memsetq _osk_memsetq + +#define memchr _osk_memchrb +#define memchrw _osk_memchrw +#define memchrd _osk_memchrd +#define memchrq _osk_memchrq + +#define memcpy _osk_memcpy +#define memmove _osk_memmove + +#define memcmp _osk_memcmp +#define memzero _osk_memzero + +//------------------------------------------// + +#define strlen _osk_strlen +#define strspn _osk_strspn +#define strcspn _osk_strcspn + +#define strcmp _osk_strcmp +#define strncmp _osk_strncmp + +#define strchr _osk_strchr +#define strrchr _osk_strrchr + +#define strstr _osk_strstr +#define strpbrk _osk_strpbrk + +#define strtok _osk_strtok +#define strtok_r _osk_strtok_r + +#define strcpy _osk_strcpy +#define strncpy _osk_strncpy +#define xstrncpy _osk_xstrncpy + +#define strcat _osk_strcat +#define strncat _osk_strncat +#define xstrncat _osk_xstrncat + +#define strrev _osk_strrev +#define strrev2 _osk_strrev2 + +#define sprintf _osk_sprintf +#define snprintf _osk_snprintf +#define vsprintf _osk_vsprintf +#define vsnprintf _osk_vsnprintf + +//------------------------------------------// + + +#define itoa _osk_itoa +#define ltoa _osk_ltoa +#define utoa _osk_utoa +#define ultoa _osk_ultoa + +#define atoi _osk_atoi +#define atol _osk_atol +#define atou _osk_atou +#define atoul _osk_atoul + +#define strtol _osk_strtol +#define strtoul _osk_strtoul + +//------------------------------------------// + +#define rand _osk_rand +#define srand _osk_srand + +//------------------------------------------// + +#define abs _osk_abs +#define labs _osk_labs + +#define min _osk_min +#define lmin _osk_lmin + +#define max _osk_max +#define lmax _osk_lmax + +#define __div +#define __ldiv + +#define div _osk_div +#define ldiv _osk_ldiv + +//------------------------------------------// + +#define strerror _osk_strerror + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/common/kaltypes.h b/kaleid/include/common/kaltypes.h new file mode 100644 index 0000000..6823ddb --- /dev/null +++ b/kaleid/include/common/kaltypes.h @@ -0,0 +1,120 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid C common types // +//----------------------------------------------------------------------------// + +#ifndef _KALTYPES_H +#define _KALTYPES_H + +//------------------------------------------// +// Basic integer types aliases // +//------------------------------------------// + +#ifndef __base_types_aliases +#define __base_types_aliases +typedef unsigned char uchar; +typedef signed char schar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef signed long long llong; +typedef unsigned long long ullong; +typedef long double ldouble; +#endif + +//------------------------------------------// +// Miscellaneous types // +//------------------------------------------// + +#ifndef __size_t_defined +#define __size_t_defined +typedef unsigned long size_t; +#endif + +#ifndef __ssize_t_defined +#define __ssize_t_defined +typedef signed long ssize_t; +#endif + +#ifndef __wchar_t_defined +#define __wchar_t_defined +typedef signed int wchar_t; +#endif + +#ifndef __off_t_defined +#define __off_t_defined +typedef unsigned long off_t; +#endif + +//------------------------------------------// +// Standard fixed-width integer types // +//------------------------------------------// + +#ifndef __ptrdiff_t_defined +#define __ptrdiff_t_defined +typedef signed long ptrdiff_t; +#endif + +#ifndef __intptr_t_defined +#define __intptr_t_defined +typedef signed long intptr_t; +#endif + +#ifndef __uintptr_t_defined +#define __uintptr_t_defined +typedef unsigned long uintptr_t; +#endif + +#ifndef __intmax_t_defined +#define __intmax_t_defined +typedef signed long intmax_t; +#endif + +#ifndef __uintmax_t_defined +#define __uintmax_t_defined +typedef unsigned long uintmax_t; +#endif + +//------------------------------------------// +// Special types // +//------------------------------------------// + +#ifndef __va_list_defined +#define __va_list_defined +typedef __builtin_va_list va_list; +#endif + +#ifndef __div_t_defined +#define __div_t_defined +typedef struct { int quot, rem; } div_t; +#endif + +#ifndef __ldiv_t_defined +#define __ldiv_t_defined +typedef struct { long quot, rem; } ldiv_t; +#endif + + +//------------------------------------------// +// Kaleid-specific types // +//------------------------------------------// + +#ifndef __error_t_defined +#define __error_t_defined +typedef int error_t; +#endif + +#ifndef __port_t_defined +#define __port_t_defined +typedef ushort port_t; +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/kaleid.h b/kaleid/include/kaleid.h new file mode 100644 index 0000000..da81fce --- /dev/null +++ b/kaleid/include/kaleid.h @@ -0,0 +1,95 @@ +//----------------------------------------------------------------------------// +// 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 + +//------------------------------------------// +// Building in C++ // +//------------------------------------------// + +#ifdef __cplusplus__ +extern "C" { +#endif + +//------------------------------------------// +// Include common part of API // +//------------------------------------------// + +#ifndef _KALDEFS_H +#include +#endif + +#ifndef _KALERROR_H +#include +#endif + +#ifndef _KALTYPES_H +#include +#endif + +#ifndef _KALLIMS_H +#include +#endif + +#ifndef _KALASSRT_H +#include +#endif + +#ifndef _KALCRT_H +#include +#endif + +//------------------------------------------// +// Include kernel headers // +//------------------------------------------// + +#ifdef _KALEID_KERNEL + +#ifndef _KALKERN_H +#include +#endif + +#else + +#ifndef _KALKERN_LOCKS_H +#include +#endif + +#endif + +//------------------------------------------// +// Building in C++ // +//------------------------------------------// + +#ifdef __cplusplus__ +} +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/kalkern.h b/kaleid/include/kalkern.h new file mode 100644 index 0000000..df15c7d --- /dev/null +++ b/kaleid/include/kalkern.h @@ -0,0 +1,45 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid Kernel main include file // +//----------------------------------------------------------------------------// + +//------------------------------------------// +// Dependencies // +//------------------------------------------// + +#ifndef _KALEID_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_H +#define _KALKERN_H + +//------------------------------------------// +// Kernel headers // +//------------------------------------------// + +#ifndef _KALKERN_BASE_H +#include +#endif + +#ifndef _KALKERN_LOCKS_H +#include +#endif + +#ifndef _KALKERN_TERM_H +#include +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/kernel/kernbase.h b/kaleid/include/kernel/kernbase.h new file mode 100644 index 0000000..171c048 --- /dev/null +++ b/kaleid/include/kernel/kernbase.h @@ -0,0 +1,205 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Kaleid Kernel base types and functionalities // +//----------------------------------------------------------------------------// + +//------------------------------------------// +// Dependencies // +//------------------------------------------// + +#ifndef _KALEID_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_BASE_H +#define _KALKERN_BASE_H + +//------------------------------------------// +// Elementary types // +//------------------------------------------// + +typedef struct sLock_t volatile Lock_t; +typedef struct sThread_t Thread_t; +typedef struct sProcess_t Process_t; +typedef struct sTerminal_t Terminal_t; +typedef struct sListHead_t ListHead_t; +typedef struct sListNode_t ListNode_t; + +//------------------------------------------// +// Values for __kstate // +//------------------------------------------// + +// +// Current state of the kernel +// +typedef enum { + // the kernel is booting + KSTATE_INIT, + + // the kernel is not running a process + KSTATE_KERNEL, + + // a process is running in kernel mode + KSTATE_PROCESS, + + // the kernel is panicking + KSTATE_PANIC, + +} KernelState_t; + +//------------------------------------------// +// Multiprocessor misc. // +//------------------------------------------// + +#ifndef NCPU +#define NCPU 4 +#endif + +#define GetCurCPU() 0 + +// +// Declare an (extern) CPU-local variable +// +#define __DECLARE_PER_CPU(_X, _Tp, _Qual) \ + _Qual _Tp __ ## _X [NCPU]; \ + static inline _Tp Get ## _X (void) \ + { return __ ## _X [GetCurCPU()]; } \ + static inline void _Set ## _X (_Tp _Y) \ + { (__ ## _X [GetCurCPU()] = _Y); } + +#define DECLARE_PER_CPU(_X, _Tp) \ + __DECLARE_PER_CPU(_X, _Tp, extern) + +#define LOCAL_DEC_PER_CPU(_X, _Tp) \ + __DECLARE_PER_CPU(_X, _Tp, static) + +// +// Actually creates a CPU-local variable +// +#define CREATE_PER_CPU(_X, _Tp) \ + _Tp __ ## _X [NCPU] = { (_Tp) 0 } + + +//------------------------------------------// +// Global constants // +//------------------------------------------// + +// XXX +DECLARE_PER_CPU(PanicStr, const char *); + +DECLARE_PER_CPU(KernState, KernelState_t); + +DECLARE_PER_CPU(_StdOut, Terminal_t *); +DECLARE_PER_CPU(_StdDbg, Terminal_t *); + +DECLARE_PER_CPU(CurProc, Process_t *); +DECLARE_PER_CPU(CurThread, Thread_t *); + +//------------------------------------------// +// Macros for manipulating said // +// global constants // +//------------------------------------------// + +#define SetPanicStr(str) \ + do { \ + SetKernState(KSTATE_PANIC); \ + _SetPanicStr(str); \ + } while (0) + +#define SetKernState(x) \ + do { \ + _SetKernState(x); \ + } while (0) + +#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : NULL) +#define SetStdOut(tm) \ + do { \ + if (GetCurProc() == NULL) \ + _Set_StdOut(tm); \ + } while (0); + +#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : NULL) +#define SetStdDbg(tm) \ + do { \ + if (GetCurProc() == NULL) \ + _Set_StdDbg(tm); \ + } while (0) + +//------------------------------------------// +// Other Macros // +//------------------------------------------// + +// +// Size of a tabulation in spaces +// Default: 4 spaces/tab +// +#define KTABSIZE 4 + +// +// Disable IRQs +// +#define DisableIRQs() asm volatile ("cli") + +// +// Enable IRQs +// +#define EnableIRQs() asm volatile ("sti") + +// +// Pause CPU until next interuption +// !!! Enables IRQs !!! +// +#define PauseCPU() asm volatile("sti\n\thlt") + +// +// Halt the CPU indefinitely +// +#define HaltCPU() do { asm volatile ("hlt"); } while (1) + +//------------------------------------------// +// Some base functions // +//------------------------------------------// + +noreturn void StartPanic(const char *); +noreturn void CrashSystem(void); + +//------------------------------------------// +// Useful I/O inlines // +//------------------------------------------// + +static inline +void WriteByteOnPort(port_t port, port_t val) +{ + asm volatile ("out %0, %1" : : "dN" (port), "a" (val)); +} + +static inline +uchar ReadByteFromPort(port_t port) +{ + errno = ENOSYS; + (void)port; + return 0; +} + +static inline +ushort ReadWordFromPort(port_t port) +{ + errno = ENOSYS; + (void)port; + return 0; +} + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/kernel/kernlocks.h b/kaleid/include/kernel/kernlocks.h new file mode 100644 index 0000000..f3dd7e2 --- /dev/null +++ b/kaleid/include/kernel/kernlocks.h @@ -0,0 +1,170 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Spinlocks and mutexes // +//----------------------------------------------------------------------------// + +//------------------------------------------// +// Dependencies // +//------------------------------------------// + +#ifdef _KALEID_KERNEL + +#ifndef _KALKERN_BASE_H +#include "kernbase.h" +#endif + +#else + +#ifndef _KALEID_H +#include +#endif + +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_LOCKS_H +#define _KALKERN_LOCKS_H + +//------------------------------------------// +// Types // +//------------------------------------------// + +typedef enum eLockType_t { + // + // Mutex-type lock + // + // WARNING + // AquireLock() panics when used on a mutex while not running a process + // + KLOCK_MUTEX, + + // + // Spinlock-type lock + // + KLOCK_SPINLOCK, + +} LockType_t; + +// +// "volatile" may not be actually needed +// +typedef struct sLock_t { + unsigned int initDone; // initialized? + int locked; // is locked? + LockType_t type; // lock type? +#ifdef _KALEID_KERNEL + Process_t *ownerProc; // unused + Process_t *waitingProc; // unused +#endif +} volatile Lock_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +// +// Linux syscall... +// +#ifndef _KALEID_KERNEL +int sched_yield(void); +#endif + +// +// Initialize a lock +// +static inline +void InitLock(Lock_t *lock, LockType_t type) +{ + lock->type = type; + lock->locked = FALSE; + lock->initDone = INITOK; +#ifdef _KALEID_KERNEL + lock->ownerProc = NULL; + lock->waitingProc = NULL; +#endif +} + +// +// Alternative way to initalize a lock +// +#ifdef _KALEID_KERNEL +# define INITLOCK(type) { INITOK, FALSE, (type), NULL, NULL } +#else +# define INITLOCK(type) { INITOK, FALSE, (type) } +#endif + +// +// Destroy a lock +// +static inline +void DestroyLock(Lock_t *lock) +{ + KalAssert(lock->initDone); + + __sync_synchronize(); + lock->initDone = 0; +} + +// +// Aquire the lock +// Panic on double aquisition since that should never happen +// until we have at least a basic scheduler +// +static inline +void AquireLock(Lock_t *lock) +{ + KalAssert(lock->initDone == INITOK); + + while (!__sync_bool_compare_and_swap(&lock->locked, 0, 1)) { +#ifdef _KALEID_KERNEL + StartPanic("AquireLock on an already locked object"); +#else + if likely (lock->type == KLOCK_SPINLOCK) continue; + else sched_yield(); +#endif + } + __sync_synchronize(); +} + +// +// Release an already aquired lock +// Panic if the lock was never aquired +// +static inline +void ReleaseLock(Lock_t *lock) +{ +#ifdef _KALEID_KERNEL + KalAssert(lock->ownerProc == GetCurProc()); +#endif + + __sync_synchronize(); + lock->locked = 0; +} + +// +// Tries to aquire lock +// +static inline +bool AttemptLock(Lock_t *lock) +{ + KalAssert(lock->initDone == INITOK); + + bool retval = __sync_bool_compare_and_swap(&lock->locked, 0, 1); + + __sync_synchronize(); + + return retval; +} + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/kaleid/include/kernel/kernsched.h b/kaleid/include/kernel/kernsched.h new file mode 100644 index 0000000..1026d53 --- /dev/null +++ b/kaleid/include/kernel/kernsched.h @@ -0,0 +1,102 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Scheduler header // +//----------------------------------------------------------------------------// + +//------------------------------------------// +// Dependencies // +//------------------------------------------// + +#ifndef _KALKERN_BASE_H +#include "kernbase.h" +#endif + +#ifndef _KALLIST_H +#include +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_SCHED_H +#define _KALKERN_SCHED_H + +//------------------------------------------// +// Preprocessor // +//------------------------------------------// + +#define printdbg printf +#define _STR(x) #x +#define _XSTR(x) _STR(x) + +// +// States for a process +// +#define STATE_RUNNING 0 +#define STATE_RUNNABLE 1 +#define STATE_BLOCKED 2 + +// +// Time in ticks a process should be run +// +#define DEF_PROC_TSLICE 5 // 20 ticks +#define TCR_PROC_TSLICE 20000 // 20000 ticks (time critical) + +//------------------------------------------// +// List heads // +//------------------------------------------// + +DECLARE_PER_CPU(IdlePrioProcs, ListHead_t *); +DECLARE_PER_CPU(ReglPrioProcs, ListHead_t *); +DECLARE_PER_CPU(ServPrioProcs, ListHead_t *); +DECLARE_PER_CPU(TimeCritProcs, ListHead_t *); + +//------------------------------------------// +// Data types // +//------------------------------------------// + +// +// A process +// +typedef struct sProcess_t{ + + // Identifier + int pid; + + // Current priority class + int prioClass; + + // Default priority class (without boosts) + int defPrioClass; + + // Current priority level + int prioLevel; + + // Default priority level + int defPrioLevel; + + // Current state + int procState; + + // Remaining time running + ulong timeSlice; + + // Default time-slice + ulong defTimeSlice; + + // Scheduler internals + ListNode_t *schedNode; + +} Process_t; + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif + diff --git a/kaleid/include/kernel/kernterm.h b/kaleid/include/kernel/kernterm.h new file mode 100644 index 0000000..b5c32b6 --- /dev/null +++ b/kaleid/include/kernel/kernterm.h @@ -0,0 +1,95 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Terminal functions // +//----------------------------------------------------------------------------// + +//------------------------------------------// +// Dependencies // +//------------------------------------------// + +#ifndef _KALKERN_BASE_H +#include "kernbase.h" +#endif + +//------------------------------------------// +// Start of header // +//------------------------------------------// + +#ifndef _KALKERN_TERM_H +#define _KALKERN_TERM_H + +//------------------------------------------// +// Types // +//------------------------------------------// + +// +// The VGA colors +// +typedef enum { + KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, + KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, + KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, + KTERM_COLOR_BROWN, KTERM_COLOR_LGREY, + KTERM_COLOR_DARK_GREY, KTERM_COLOR_LBLUE, + KTERM_COLOR_LGREEN, KTERM_COLOR_LCYAN, + KTERM_COLOR_LRED, KTERM_COLOR_LMAGENTA, + KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE +} TermColor_t; + +// +// Terminal structure, right now VGA and output only +// +typedef struct sTerminal_t { + + uint initDone; + Lock_t lock; + + const char *name; + const char *type; + + void *data; + + size_t width; + size_t height; + off_t currentX; + off_t currentY; + + uint tabSize; + TermColor_t fgColor; + TermColor_t bgColor; + + error_t (*ClearTermUnlocked)(Terminal_t *); + error_t (*PutOnTermUnlocked)(Terminal_t *, char); + error_t (*PrintOnTermUnlocked)(Terminal_t *, const char *); + +} Terminal_t; + +//------------------------------------------// +// Functions // +//------------------------------------------// + +void InitTerms(void); +error_t ClearTerm(Terminal_t *); +error_t PutOnTerm(Terminal_t *, char); +error_t PrintOnTerm(Terminal_t *, const char *); +error_t ChTermColor(Terminal_t *, TermColor_t, TermColor_t); + +//------------------------------------------// +// Macros // +//------------------------------------------// + +#ifndef _NO_DEBUG +# define DebugLog(...) PrintOnTerm(GetStdDbg(), __VA_ARGS__) +#else +# define DebugLog(...) +#endif + +//------------------------------------------// +// End of header // +//------------------------------------------// + +#endif diff --git a/src/kaleid/kernel/init.c b/kaleid/kernel/init/init.c similarity index 68% rename from src/kaleid/kernel/init.c rename to kaleid/kernel/init/init.c index b6f268a..0887f9c 100644 --- a/src/kaleid/kernel/init.c +++ b/kaleid/kernel/init/init.c @@ -7,16 +7,23 @@ // Desc: Kernel entry point // //----------------------------------------------------------------------------// -#include "kernel/init.h" -#include "kernel/ke/panic.h" -#include "kernel/io/terminal.h" +#include // -// Entry point of kaleid-kernel.elf +// Entry point of kaleid-kernel.elf // -void kstart(void) -{ - kterm_init(); - panic("Goodbye World :("); +noreturn void StartKern(void) +{ + // we're not ready to deal with interrupts + DisableIRQs(); + + // booting! + SetKernState(KSTATE_INIT); + + // kernel terminals + InitTerms(); + + // we're out + StartPanic("Goodbye World :("); } diff --git a/src/kaleid/common/convert.h b/kaleid/kernel/init/table.c similarity index 63% rename from src/kaleid/common/convert.h rename to kaleid/kernel/init/table.c index 737f016..4b98d25 100644 --- a/src/kaleid/common/convert.h +++ b/kaleid/kernel/init/table.c @@ -4,21 +4,18 @@ // Authors: spectral` // // NeoX // // // -// Desc: Conversion utilities // +// Desc: Global constants // //----------------------------------------------------------------------------// -#ifndef _KALCOMM_CONVERT_H -#define _KALCOMM_CONVERT_H +#include -#ifndef _KALCOMM_COMMON_H -#include "common/common.h" -#endif +CREATE_PER_CPU(PanicStr, const char *); -#ifndef _OSK_SOURCE -# define itoa _osk_itoa -# define atoi _osk_atoi -#endif +CREATE_PER_CPU(KernState, KernelState_t); -char *itoa(int, char *, int); +CREATE_PER_CPU(_StdOut, Terminal_t *); +CREATE_PER_CPU(_StdDbg, Terminal_t *); + +CREATE_PER_CPU(CurProc, Process_t *); +CREATE_PER_CPU(CurThread, Thread_t *); -#endif diff --git a/kaleid/kernel/ke/panic.c b/kaleid/kernel/ke/panic.c new file mode 100644 index 0000000..72fe089 --- /dev/null +++ b/kaleid/kernel/ke/panic.c @@ -0,0 +1,69 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: How NOT to panic 101 // +//----------------------------------------------------------------------------// + +#define _UNLOCKED_IO +#include + +// +// Failed assert() handler +// +noreturn void _assert_handler(const char *msg, + const char *file, + int line, + const char *func) +{ + DisableIRQs(); + + (void)file; (void)line; (void)func; + + // XXX sprintf() to create a proper panicstr + StartPanic(msg); +} + +// +// Your best boy panic() +// This is CPU local... +// +noreturn void StartPanic(const char *str) +{ + DisableIRQs(); + + SetKernState(KSTATE_PANIC); + + if (GetCurProc()) __CurProc[GetCurCPU()] = NULL; + if (GetStdOut() == NULL) CrashSystem(); + + GetStdOut()->ClearTermUnlocked(GetStdOut()); + + if (str == NULL) { + str = "(no message given)"; + } + + if (GetPanicStr()) { + GetStdOut()->PrintOnTermUnlocked(GetStdOut(), "double panic!\n"); + HaltCPU(); + } + + SetPanicStr(str); + + GetStdOut()->PrintOnTermUnlocked(GetStdOut(), "PANIC! - "); + GetStdOut()->PrintOnTermUnlocked(GetStdOut(), str); + + HaltCPU(); +} + +// +// Oh well +// +noreturn void CrashSystem(void) +{ + DisableIRQs(); + HaltCPU(); +} + diff --git a/kaleid/kernel/ke/terminal.c b/kaleid/kernel/ke/terminal.c new file mode 100644 index 0000000..1d7439b --- /dev/null +++ b/kaleid/kernel/ke/terminal.c @@ -0,0 +1,236 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Early terminal functions // +//----------------------------------------------------------------------------// + +#include + +static Terminal_t vgaTerm; + +// +// Initialize standard output +// +void InitTerms(void) +{ + KalAssert(!GetStdOut() && vgaTerm.initDone != INITOK); + + vgaTerm.initDone = INITOK; + + SetStdDbg(&vgaTerm); + SetStdOut(&vgaTerm); + + ClearTerm(GetStdOut()); +} + +// +// Fill terminal with spaces +// +error_t ClearTerm(Terminal_t *term) +{ + error_t retcode; + + if (term == NULL) return EINVAL; + KalAssert(term->initDone == INITOK); + + AquireLock(&term->lock); + retcode = term->ClearTermUnlocked(term); + ReleaseLock(&term->lock); + + return retcode; +} + +// +// Change the color code +// +error_t ChTermColor(Terminal_t *term, TermColor_t fgColor, TermColor_t bgColor) +{ + if (fgColor > KTERM_COLOR_WHITE || bgColor > KTERM_COLOR_WHITE) + return EINVAL; + + if (term == NULL) + return EINVAL; + + AquireLock(&term->lock); + + term->fgColor = fgColor; + term->bgColor = bgColor; + + ReleaseLock(&term->lock); + + return EOK; +} + +// +// Write a single character on the terminal +// +error_t PutOnTerm(Terminal_t *term, char ch) +{ + error_t retcode; + + if (term == NULL) return EINVAL; + KalAssert(term->initDone == INITOK); + + AquireLock(&term->lock); + retcode = term->PutOnTermUnlocked(term, ch); + ReleaseLock(&term->lock); + + return retcode; +} + +// +// Print string on terminal +// +error_t PrintOnTerm(Terminal_t *term, const char *str) +{ + error_t retcode = EOK; + + if (term == NULL) return EINVAL; + KalAssert(term->initDone == INITOK); + + AquireLock(&term->lock); + while (*str && retcode == EOK) { + retcode = term->PutOnTermUnlocked(term, *str++); + } + ReleaseLock(&term->lock); + + return retcode; +} + +//----------------------------------------------------------// +// Internal functions for VGA terminals // +// These DO NOT check input correctness // +//----------------------------------------------------------// + +// +// VGA-related macros +// +#define VGA_ComputeColorCode(fg, bg) ((fg) | (bg) << 4) +#define VGA_ComputeOffset(term, x, y) ((y) * (term)->width + (x)) +#define VGA_ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8) + +// +// Fill terminal with '\0' +// +error_t VGA_ClearTermUnlocked(Terminal_t *term) +{ + const uchar color = VGA_ComputeColorCode(term->fgColor, term->bgColor); + const ushort filler = VGA_ComputeEntry('\0', color); + const size_t bufsize = term->width * term->height; + + // Fill the buffer + memsetw((ushort *)term->data, filler, bufsize); + + // XXX update cursor too + term->currentX = term->currentY = 0; + + return EOK; +} + +// +// Write a single character on the terminal +// +error_t VGA_PutOnTermUnlocked(Terminal_t *term, char ch) +{ + uint i; + size_t prevY; + + if (ch == '\r') { + term->currentX = 0; + return EOK; + } + + // + // Line feed first takes us to the very end of the line + // Later in this function we actually do the line feed + // + else if (ch == '\n') { + term->currentY = term->width - 1; + } + + // + // Tabulations account for "term->tabSize" spaces + // + else if (ch == '\t') { + prevY = term->currentY; + for (i = 0; i < term->tabSize; i++) { + // + // Make sure tabulations can't spread over two lines + // + if (term->currentY == prevY) { + VGA_PutOnTermUnlocked(term, ' '); + } + } + } + + else { + ushort *buffer = (ushort *)term->data; + const size_t offset = VGA_ComputeOffset(term, term->currentY, term->currentY); + buffer[offset] = VGA_ComputeEntry(ch, VGA_ComputeColorCode(term->fgColor, term->bgColor)); + } + + // + // Did we reach the end of line? + // + if (++term->currentX == term->width) { + term->currentX = 0; + + // + // Did we reach the buffer's end? + // + if (++term->currentY == term->height) { + // + // XXX scroll up + // + term->currentY = 0; + } + } + + // + // Nothing can go wrong + // + return EOK; +} + +// +// Print string on terminal +// +error_t VGA_PrintOnTermUnlocked(Terminal_t *term, const char *str) +{ + error_t retcode = EOK; + + while (*str && retcode == EOK) { + retcode = term->PutOnTermUnlocked(term, *str++); + } + + return retcode; +} + +// +// VGA output +// +static Terminal_t vgaTerm = { + .initDone = FALSE, + .lock = INITLOCK(KLOCK_MUTEX), + + .name = "VGA Output Terminal", + .type = "VGA", + + .data = (void *)0xB8000, + .width = 80, + .height = 25, + .currentX = 0, + .currentY = 0, + + .tabSize = KTABSIZE, + .fgColor = KTERM_COLOR_LGREY, + .bgColor = KTERM_COLOR_BLACK, + + .ClearTermUnlocked = VGA_ClearTermUnlocked, + .PutOnTermUnlocked = VGA_PutOnTermUnlocked, + .PrintOnTermUnlocked = VGA_PrintOnTermUnlocked, +}; + diff --git a/kaleid/kernel/proc/Makefile b/kaleid/kernel/proc/Makefile new file mode 100644 index 0000000..8001a5e --- /dev/null +++ b/kaleid/kernel/proc/Makefile @@ -0,0 +1,4 @@ + +sched-test: + gcc -O2 -masm=intel -I../../include ./sched.c + diff --git a/kaleid/kernel/proc/sched.c b/kaleid/kernel/proc/sched.c new file mode 100644 index 0000000..b81ec45 --- /dev/null +++ b/kaleid/kernel/proc/sched.c @@ -0,0 +1,409 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Scheduling algorithm // +//----------------------------------------------------------------------------// + +#include + +#ifndef _KALEID_KERNEL + +#include +CREATE_PER_CPU(CurProc, Process_t *); + +// +// For test purpose only +// +int procslen = 9; +Process_t procs[] = { + { 0, 0, 0, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 1, 2, 2, 16, 16, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 2, 3, 3, 31, 31, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 3, 2, 2, 1, 1, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 4, 0, 0, 5, 5, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 5, 0, 0, 30, 30, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 6, 1, 1, 19, 19, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 7, 1, 1, 0, 0, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, + { 8, 3, 3, 12, 12, STATE_RUNNABLE, DEF_PROC_TSLICE, DEF_PROC_TSLICE, NULL }, +}; + +#endif + +// +// Set current process +// TODO Select thread, context switch +// +static inline +void SetCurProc(Process_t *proc) +{ + _SetCurProc(proc); + if (GetCurProc() != NULL) { + GetCurProc()->procState = STATE_RUNNING; + } +} + +// +// (Un)Lock priority class list heads +// + +static inline +void SchedLock(void) { +#ifdef _KALEID_KERNEL + DisableIRQs(); +#endif +} + +static inline +void SchedUnlock(void) { +#ifdef _KALEID_KERNEL + EnableIRQs(); +#endif +} + +// +// The four priority classes of OS/2 +// +CREATE_PER_CPU(IdlePrioProcs, ListHead_t *); +CREATE_PER_CPU(ReglPrioProcs, ListHead_t *); +CREATE_PER_CPU(ServPrioProcs, ListHead_t *); +CREATE_PER_CPU(TimeCritProcs, ListHead_t *); + +char *PrioClassesNames[] = { + "Idle priority class", + "Regular priority class", + "Server priority class", + "Time-critical class", +}; + +enum { IDLE_PRIO_PROC = 0, + REGL_PRIO_PROC = 1, + SERV_PRIO_PROC = 2, + TIME_CRIT_PROC = 3, +}; + +// +// Get priority class list head +// +static inline +ListHead_t *GetPrioClassHead(int prioClass) +{ + switch (prioClass) { + case TIME_CRIT_PROC: return GetTimeCritProcs(); + case SERV_PRIO_PROC: return GetServPrioProcs(); + case REGL_PRIO_PROC: return GetReglPrioProcs(); + case IDLE_PRIO_PROC: return GetIdlePrioProcs(); + default: KalAssert(FALSE && "Unknown priority class"); + } + + return NULL; +} + +// +// Determine which process is going to run first +// Return NULL for "equal" processes +// +static inline +Process_t *CompareProcs(Process_t *proc1, Process_t *proc2) +{ + KalAssert(proc1 && proc2); + + if (proc1->prioClass > proc2->prioClass) return proc1; + if (proc1->prioClass < proc2->prioClass) return proc2; + + if (proc1->prioLevel > proc2->prioLevel) return proc1; + if (proc1->prioLevel < proc2->prioLevel) return proc2; + + return NULL; // same class and level +} + +// +// Add process to schedule lists (unlocked) +// +static inline +void SchedThisProcUnlocked(Process_t *proc) +{ + KalAssert(proc && proc->procState == STATE_RUNNABLE); + + bool found = false; + ListNode_t *iterNode = NULL; + ListNode_t *procNode = CreateNode(proc); + ListHead_t *head = GetPrioClassHead(proc->prioClass); + + KalAssert(procNode && head); + + proc->schedNode = procNode; + + //printdbg("Adding process %d to '%s'\n", proc->pid, PrioClassesNames[proc->prioClass]); + + // + // Find a process with lesser priority + // + for (iterNode = head->first; iterNode; iterNode = iterNode->next) { + if (proc->prioLevel > GetNodeData(iterNode, Process_t *)->prioLevel) { + AddNodeBefore(head, iterNode, procNode); + found = true; + break; + } + } + + // + // Didn't find any process with lesser priority + // + if (found == false) { + AppendNode(head, procNode); + } +} + +// +// Add process to schedule lists +// +void SchedThisProc(Process_t *proc) +{ + SchedLock(); + + SchedThisProcUnlocked(proc); + + SchedUnlock(); +} + +// +// Selects process to schedule next +// +// WARNING +// Does not call SchedLock()/SchedUnlock() +// +static inline +Process_t *SelectSchedNext(void) +{ + if (GetTimeCritProcs()->length > 0) return GetNodeData(GetTimeCritProcs()->first, Process_t *); + if (GetServPrioProcs()->length > 0) return GetNodeData(GetServPrioProcs()->first, Process_t *); + if (GetReglPrioProcs()->length > 0) return GetNodeData(GetReglPrioProcs()->first, Process_t *); + if (GetIdlePrioProcs()->length > 0) return GetNodeData(GetIdlePrioProcs()->first, Process_t *); + + return NULL; +} + +// +// Remove running process from schedule lists +// and schedule next runnable process +// +static inline +void BlockCurProc(void) +{ + KalAssert(GetCurProc() && GetCurProc()->procState == STATE_RUNNING); + + ListNode_t *procNode = GetCurProc()->schedNode; + + GetCurProc()->procState = STATE_BLOCKED; + RemoveNode(procNode->head, procNode); + + SetCurProc(SelectSchedNext()); +} + +// +// Should we schedule another process? +// Called at each tick +// +void SchedOnTick(void) +{ + Process_t *procNext; + Process_t *winner; + + SchedLock(); + + // + // We're either idle or running something + // + KalAssert(GetCurProc() == NULL || GetCurProc()->procState == STATE_RUNNING); + + // + // Has current process spent his timeslice? + // (To be handled in CPU decisions function) + // + if (GetCurProc() != NULL) { + if (GetCurProc()->timeSlice <= 1) { + + // Restore default attributes, cancelling boosts + GetCurProc()->prioClass = GetCurProc()->defPrioClass; + GetCurProc()->prioLevel = GetCurProc()->defPrioLevel; + GetCurProc()->timeSlice = GetCurProc()->defTimeSlice; + GetCurProc()->procState = STATE_RUNNABLE; + + // Remove from list + RemoveNode(GetCurProc()->schedNode->head, GetCurProc()->schedNode); + + // Schedule again, with default attributes now + SchedThisProcUnlocked(GetCurProc()); + + // Mark as idle + SetCurProc(NULL); + } + + // + // Otherwise, make him lose a tick + // + else { + GetCurProc()->timeSlice--; + } + } + + // + // Are we idle, or scheduling next process? + // + if (GetCurProc() == NULL) { + SetCurProc(SelectSchedNext()); + goto leave; + } + + // + // Is there a higher priority process that is runnable? + // + procNext = SelectSchedNext(); + winner = CompareProcs(GetCurProc(), procNext); + + // + // Yes, procNext should preempt current process + // + if (winner == procNext) { + GetCurProc()->procState = STATE_RUNNABLE; + SchedThisProcUnlocked(GetCurProc()); + SetCurProc(procNext); + } + + // + // Current process won't be preempted and has time remaining + // +leave: + SchedUnlock(); +} + +#define PrintProc(proc) printdbg("{ %d, '%s', %d , %d}\n", (proc)->pid, \ + PrioClassesNames[(proc)->prioClass], (proc)->prioLevel, (proc)->timeSlice); + +// +// Print out process list +// +void PrintList(ListHead_t *head) +{ + KalAssert(head); + + Process_t *proc; + ListNode_t *node = head->first; + + printdbg("len: %d\n", head->length); + + while (node) { + proc = GetNodeData(node, Process_t *); + + PrintProc(proc); + + node = node->next; + } + + puts(""); +} + +// +// Initialize scheduler +// +void InitSched(void) +{ + int pid; + Process_t *proc; + + SchedLock(); + + _SetTimeCritProcs(CreateListHead()); + _SetServPrioProcs(CreateListHead()); + _SetReglPrioProcs(CreateListHead()); + _SetIdlePrioProcs(CreateListHead()); + +#ifndef _KALEID_KERNEL + for (pid = 0; pid < procslen; pid++) { + if (procs[pid].procState == STATE_RUNNABLE) { + SchedThisProcUnlocked(&procs[pid]); + } + } +#endif + + SchedUnlock(); +} + +// +// Shutdown scheduler +// +void FiniSched(void) +{ + KalAssert(GetIdlePrioProcs() && GetReglPrioProcs() && GetServPrioProcs() && GetTimeCritProcs()); + + SchedLock(); + + while (GetIdlePrioProcs()->length > 0) RemoveNode(GetIdlePrioProcs(), GetIdlePrioProcs()->first); + while (GetReglPrioProcs()->length > 0) RemoveNode(GetReglPrioProcs(), GetReglPrioProcs()->first); + while (GetServPrioProcs()->length > 0) RemoveNode(GetServPrioProcs(), GetServPrioProcs()->first); + while (GetTimeCritProcs()->length > 0) RemoveNode(GetTimeCritProcs(), GetTimeCritProcs()->first); + + DestroyListHead(GetIdlePrioProcs()); _SetIdlePrioProcs(NULL); + DestroyListHead(GetReglPrioProcs()); _SetReglPrioProcs(NULL); + DestroyListHead(GetServPrioProcs()); _SetServPrioProcs(NULL); + DestroyListHead(GetTimeCritProcs()); _SetTimeCritProcs(NULL); + + SchedUnlock(); +} + +#ifndef _KALEID_KERNEL +int main(void) +{ + InitSched(); + + puts("---------------"); + + puts("Time Critical:"); + PrintList(GetTimeCritProcs()); + + puts("Server:"); + PrintList(GetServPrioProcs()); + + puts("Regular:"); + PrintList(GetReglPrioProcs()); + + puts("Idle:"); + PrintList(GetIdlePrioProcs()); + + puts("---------------"); + + getchar(); + + int tick = 0; + + while (tick < 20) { + printf("Tick %d - Running: ", tick); + + if (GetCurProc() == NULL) { + puts("IDLE"); + } + + else { + PrintProc(GetCurProc()); + } + + if (tick == 9 || tick == 14) { + puts("Blocking current process"); + BlockCurProc(); + } + + SchedOnTick(); + + //puts("\n---------------"); + tick++; + } + + FiniSched(); + + return 0; +} +#endif + diff --git a/src/CONTACT b/src/CONTACT deleted file mode 100644 index dd6d86a..0000000 --- a/src/CONTACT +++ /dev/null @@ -1 +0,0 @@ -XXX diff --git a/src/COPYING b/src/COPYING deleted file mode 100644 index 53d1f3d..0000000 --- a/src/COPYING +++ /dev/null @@ -1,675 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. - diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index 79c8836..0000000 --- a/src/Makefile +++ /dev/null @@ -1,95 +0,0 @@ -#----------------------------------------------------------------------------# -# GNU GPL OS/K # -# # -# Authors: spectral` # -# NeoX # -# # -# 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= - -CC=$(CCNAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) -KCC=$(CC) -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/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 - -#----------------------------------------------------------------------------# - 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/atomic.h b/src/kaleid/common/atomic.h deleted file mode 100644 index a1daef3..0000000 --- a/src/kaleid/common/atomic.h +++ /dev/null @@ -1,29 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Atomic stuff // -//----------------------------------------------------------------------------// - -#ifndef _KALCOMM_ATOMIC_H -#define _KALCOMM_ATOMIC_H - -#ifndef _KALCOMM_COMMON_H -#error "don't include common/types.h without common/common.h" -#endif - -// atomic_t defined in common/types.h - -#ifdef _KALEID_KERNEL - -// only available in the kernel -#define cli() asm volatile ("cli") -#define sti() asm volatile ("sti") -#define hlt() asm volatile ("hlt") - -#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/config.h b/src/kaleid/common/config.h deleted file mode 100644 index 501be3d..0000000 --- a/src/kaleid/common/config.h +++ /dev/null @@ -1,42 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Default configuration file // -//----------------------------------------------------------------------------// - -// XXX update the tree etc - -// This file is to be copied to "config.h" and edited before compilation -// We may do a script that generates "config.h" for the user, asking about -// the different configuration choices in the terminal. - -#ifndef _KALCOMM_CONFIG_H -#define _KALCOMM_CONFIG_H - -//------------------------------------------// -// General configuration choices // -//------------------------------------------// - -// -// Enable/disable multiprocessor support -// -#define MULTIPROCESSOR NO - -// -// Enable/disable preemptivity -// -#define PREEMPTIVE YES - -// -// Size of a tabulation in spaces -// Default: 4 spaces/tab -// -#define TABSIZE 4 - -// this file is to be progressively filled - -#endif - diff --git a/src/kaleid/common/config.h.in b/src/kaleid/common/config.h.in deleted file mode 100644 index 501be3d..0000000 --- a/src/kaleid/common/config.h.in +++ /dev/null @@ -1,42 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Default configuration file // -//----------------------------------------------------------------------------// - -// XXX update the tree etc - -// This file is to be copied to "config.h" and edited before compilation -// We may do a script that generates "config.h" for the user, asking about -// the different configuration choices in the terminal. - -#ifndef _KALCOMM_CONFIG_H -#define _KALCOMM_CONFIG_H - -//------------------------------------------// -// General configuration choices // -//------------------------------------------// - -// -// Enable/disable multiprocessor support -// -#define MULTIPROCESSOR NO - -// -// Enable/disable preemptivity -// -#define PREEMPTIVE YES - -// -// Size of a tabulation in spaces -// Default: 4 spaces/tab -// -#define TABSIZE 4 - -// this file is to be progressively filled - -#endif - 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/lib/memory.c b/src/kaleid/common/lib/memory.c deleted file mode 100644 index 3ebe6ee..0000000 --- a/src/kaleid/common/lib/memory.c +++ /dev/null @@ -1,11 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: mem*() functions // -//----------------------------------------------------------------------------// - -#include "common/memory.h" - diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/lib/string.c deleted file mode 100644 index f81f60c..0000000 --- a/src/kaleid/common/lib/string.c +++ /dev/null @@ -1,67 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: String-related functions // -//----------------------------------------------------------------------------// - -#include "common/string.h" - -// TODO multibyte, assembly - -// -// Returns str's length -// -size_t strlen(const char *str) -{ - const char *base = str; - - while (*str++); - - return (str - base - 1); -} - -// -// Copy the string src into dest -// -char *strcpy(char *dest, const char *src) -{ - char *base = dest; - while ((*dest++ = *src++)); - return base; -} - -// -// strcpy() but safer -// -char *strncpy(char *dest, const char *src, size_t n) -{ - size_t i; - - for (i = 0; i < n && src[i]; i++) { - dest[i] = src[i]; - } - - while (i < n) dest[i++] = 0; - return dest; -} - -// -// Reverses a string -// -char *reverse(char *str) -{ - char ch, *orig = str; - size_t n = strlen(str); - char *temp = str + n - 1; - - while (temp > str) { - ch = *temp; - *temp-- = *str; - *str++ = ch; - } - - return orig; -} diff --git a/src/kaleid/common/memory.h b/src/kaleid/common/memory.h deleted file mode 100644 index a877e1a..0000000 --- a/src/kaleid/common/memory.h +++ /dev/null @@ -1,18 +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 "common/common.h" -#endif - -#endif - 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/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/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/kernel/folder.desc b/src/kaleid/kernel/folder.desc deleted file mode 100644 index f035c37..0000000 --- a/src/kaleid/kernel/folder.desc +++ /dev/null @@ -1,44 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Folder description - "kaleid/kernel" // -//----------------------------------------------------------------------------// - -This is the folder containing the source of Kaleid's kernel. - -It 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) - - mm/ - This folder contains all files related to memory management. - - - fs/ - This folder contains 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. - - - 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/ports.c b/src/kaleid/kernel/io/ports.c deleted file mode 100644 index fa824a0..0000000 --- a/src/kaleid/kernel/io/ports.c +++ /dev/null @@ -1,13 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Ports I/O // -//----------------------------------------------------------------------------// - -#include "kernel/io/ports.h" - - - diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c deleted file mode 100644 index 503dfcd..0000000 --- a/src/kaleid/kernel/io/terminal.c +++ /dev/null @@ -1,186 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Early terminal functions // -//----------------------------------------------------------------------------// - -#include "kernel/io/terminal.h" - -// -// VGA-related macros -// -#define ComputeColorCode(fg, bg) ((fg) | (bg) << 4) -#define ComputeEntryOffset(kt, x, y) ((y) * kt->kt_width + (x)) -#define ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8) - -// -// VGA output -// -static struct kterm _kt_vga = { - .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_init = FALSE, -#endif -}; - -// -// Standard output terminal -// -struct kterm *kt_stdout; - -// -// Initialize standard output -// -void kterm_init(void) -{ - assert(!kt_stdout && !_kt_vga.kt_init && "kterm_init() called twice"); - -#ifndef _NO_DEBUG - _kt_vga.kt_init = TRUE; -#endif - - // to be switched to VESA - kt_stdout = &_kt_vga; - ktclear(); -} - -// -// Fills terminal with spaces -// XXX would '\0' work too? -// -status_t kterm_clear(struct kterm *kt) -{ - size_t i; - - if (kt == NULL) - return BAD_ARG_NULL; - - assert(kt->kt_init && "kterm_clear called before initialization"); - - 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); - - return SUCCESS; -} - -// -// Change the color code -// -status_t kterm_change_color(struct kterm *kt, uchar color) -{ - if (color > KTERM_COLOR_WHITE) - return BAD_ARG_RANGE; - - if (kt == NULL) - return BAD_ARG_NULL; - - kterm_lock(kt); - kt->kt_color = color; - kterm_unlock(kt); - - return SUCCESS; -} - -// -// Writes a single character on the terminal (UNLOCKED version) -// -// DEPRECATED: -// - always use kterm_putch (LOCKED version) -// - doesn't check for NULL input -// -void kterm_putch_unlocked(struct kterm *kt, char ch) -{ - int i; - size_t prev_row; - - // carriage return takes us back to the beginning of the line - // no further test should be necessary - if (ch == '\r') { kt->kt_curr_x = 0; return; } - - // line feed first takes us to the very end of the line - // later in this function we actually do the line feed - else if (ch == '\n') { kt->kt_curr_y = kt->kt_width - 1; } - - // tabulations account for 4 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, ' '); - } - } - } - - // XXX check whether we were given a writable character - else { - const size_t offset = ComputeEntryOffset(kt, kt->kt_curr_x, kt->kt_curr_y); - kt->kt_buffer[offset] = ComputeEntry(ch, kt->kt_color); - } - - // end of line? - if (++kt->kt_curr_x == kt->kt_width) { - kt->kt_curr_x = 0; - - // line feed + end of buffer? - if (++kt->kt_curr_y == kt->kt_height) { - kt->kt_curr_y = 0; - } - } -} - -// -// Writes a single character on the terminal (LOCKED version) -// -status_t kterm_putch(struct kterm *kt, char ch) -{ - 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++); - } - kterm_unlock(kt); - - return SUCCESS; -} - - diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h deleted file mode 100644 index 5cb39bf..0000000 --- a/src/kaleid/kernel/io/terminal.h +++ /dev/null @@ -1,64 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Early terminal functions // -//----------------------------------------------------------------------------// - -#ifndef _KALKERN_IO_KTERM_H -#define _KALKERN_IO_KTERM_H - -#include "common/common.h" - -// all available colors -enum kterm_color { - KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, - KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, - KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, - KTERM_COLOR_BROWN, KTERM_COLOR_LGREY, - KTERM_COLOR_DARK_GREY, KTERM_COLOR_LBLUE, - KTERM_COLOR_LGREEN, KTERM_COLOR_LCYAN, - KTERM_COLOR_LRED, KTERM_COLOR_LMAGENTA, - KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE -}; - -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 - -}; - -// current "standard" terminal -extern struct kterm *kt_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); - -#ifdef _UNLOCKED_IO -void kterm_putch_unlocked(struct kterm *, 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)) - -#define kterm_lock(kt) -#define kterm_trylock(kt) -#define kterm_unlock(kt) - -#endif - diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c deleted file mode 100644 index 04310c6..0000000 --- a/src/kaleid/kernel/ke/panic.c +++ /dev/null @@ -1,59 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: How NOT to panic 101 // -//----------------------------------------------------------------------------// - -#include "kernel/ke/panic.h" - -// -// Panic message -// -const char *panicstr = NULL; - -// -// Failed assert() handler -// -noreturn void ___assert_handler(const char *msg, const char *file, int line, const char *func) -{ - // not getting out of here - cli(); - - (void)file; (void)line; (void)func; - - // XXX sprintf() to create a proper panicstr - panic(msg); -} - -// -// Your best boy panic() -// -void panic(const char *str) -{ - cli(); - - ktclear(); - - if (str == NULL) { - str = "(no message given)"; - } - - if (panicstr) { - // shouldn't be possible - ktprint("double panic!\n"); - hlt(); - } - - panicstr = str; - - ktprint("panic! - "); - ktprint(str); - - while (TRUE) { - hlt(); - } -} - diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h deleted file mode 100644 index 403d728..0000000 --- a/src/kaleid/kernel/ke/panic.h +++ /dev/null @@ -1,18 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Looking to create some panic? Look no further! // -//----------------------------------------------------------------------------// - -#ifndef _KALKERN_KE_PANIC_H -#define _KALKERN_KE_PANIC_H - -#include "kernel/io/terminal.h" - -extern const char *panicstr; -noreturn void panic(const char *); - -#endif diff --git a/src/kaleid/kernel/mm/malloc.c b/src/kaleid/kernel/mm/malloc.c deleted file mode 100644 index 5c62036..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 "kernel/mm/malloc.h" - diff --git a/src/kaleid/kernel/mm/malloc.h b/src/kaleid/kernel/mm/malloc.h deleted file mode 100644 index 2669121..0000000 --- a/src/kaleid/kernel/mm/malloc.h +++ /dev/null @@ -1,16 +0,0 @@ -//----------------------------------------------------------------------------// -// 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 deleted file mode 100644 index a1707c6..0000000 --- a/src/kaleid/linux/test-common.c +++ /dev/null @@ -1,44 +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/project-style.txt b/src/project-style.txt deleted file mode 100644 index 4ed65ad..0000000 --- a/src/project-style.txt +++ /dev/null @@ -1,19 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Notes on the coding style and // -// conventions used throughout OS/K // -//----------------------------------------------------------------------------// - -To keep naming conventions and code style, we have decided to create this file. -How the code looks isn't the most important, so don't worry too much about this; -making the code conform to this file can be done later. - -Every file within OS/K is written using spaces for tabulation, with each -tabulation being 4 spaces long. - -(naming conventions here) - diff --git a/src/project-tree.txt b/src/project-tree.txt deleted file mode 100644 index 7e342f8..0000000 --- a/src/project-tree.txt +++ /dev/null @@ -1,60 +0,0 @@ -//----------------------------------------------------------------------------// -// GNU GPL OS/K // -// // -// Authors: spectral` // -// NeoX // -// // -// Desc: Project Tree // -//----------------------------------------------------------------------------// - -// XXX *not* up to date - -src/ - | - + boot/ - | | - | x folder.desc - | | - | - mbr.s - | - mbr.inc - | | - | - loader.s - | - loader16.inc - | - loader64.inc - | | - | - types.h - | - + kaleid/ - | | - | + kernel/ - | | | - | | x folder.desc - | | | - | | - init.c - | | - init.h - | | | - | | + io/ - | | | - | | - ports.c - | | - ports.h - | | - terminal.c - | | - terminal.h - | | - | + common/ - | | - | x folder.desc - | | - | - assert.h - | - atomic.h - | - common.h - | - config.h - | - config.h.in - | - status.h - | - string.h - | - types.h - | | - | + lib/ - | | - | - string.c - | - 0