diff --git a/README.md b/README.md index ce35d86..d0d301c 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,14 @@ Fully open-source operating system from scratch (WIP), released under the GNU GPL version 3.0 +<<<<<<< HEAD Branch boot : Development focused on the bootloader of OS/K +======= +Branch kaleid : Development focused on the Kaleid Kernel. +>>>>>>> kaleid For changelog, see src/ChangeLog. For structure of the sources, see src/project-tree.txt. -For code conventions, see src/project-style.txt. +For code conventions, see src/project-style.txt. diff --git a/src/ChangeLog b/src/ChangeLog index e95ff0b..27cc873 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -10,7 +10,7 @@ //----------------------------------------------------------------------------// 2018/10/?? - Started talking about making our own OS -2018/11/?? - Name decided & creation of gnu-os-k.eu +2018/11/?? - Name decided & creation of os-k.eu 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 diff --git a/src/Makefile b/src/Makefile index 6d51fb0..79c8836 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,13 +7,16 @@ # Desc: Project Makefile # #----------------------------------------------------------------------------# -CC_NAME="/opt/cross-cc/bin/x86_64-elf-gcc" -CWARNS=-Wall -Wextra -Werror +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=$(CC_NAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) +CC=$(CCNAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) +KCC=$(CC) -D_KALEID_KERNEL ASM=nasm ASMFLAGS= @@ -26,6 +29,7 @@ BOOTDIR=boot COMMDIR=kaleid/common KERNDIR=kaleid/kernel SYSTDIR=kaleid/system +LINXDIR=kaleid/linux all: bootloader @@ -52,76 +56,40 @@ testing: bootloader pseudo_kern # COMMON MAKEFILE COBJDIR=$(OBJDIR)/$(COMMDIR) +LOBJDIR=$(OBJDIR)/$(LINXDIR) -COMM_DEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \ +COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \ $(COMMDIR)/status.h -COBJECTS=$(COBJDIR)/lib/string.o +COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o -common: common.lib.string.c $(COMM_DEPS) +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 -common.lib.string.c: $(COMMDIR)/lib/string.c $(COMM_DEPS) - $(CC) -c $(COMMDIR)/lib/string.c -o $(OBJDIR)/$(COMMDIR)/lib/string.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) -KERN_DEPS=$(COMM_DEPS) $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h - -kernel: common kernel.io.terminal.c kernel.init.c kernel.io.ports.c - $(CC) -o $(BINDIR)/kaleid-kernel.elf \ - $(COBJECTS) $(KOBJDIR)/init.o \ - $(KOBJDIR)/io/terminal.o $(KOBJDIR)/io/ports.o - - -kernel.init.c: $(KERNDIR)/init.c $(KERN_DEPS) - $(CC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o - -kernel.io.ports.c: $(KERNDIR)/io/ports.c $(KERN_DEPS) - $(CC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o - -kernel.io.terminal.c: $(KERNDIR)/io/terminal.c $(KERN_DEPS) - $(CC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h +KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/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/boot/loader.s b/src/boot/loader.s index 6fd830b..5ec173a 100644 --- a/src/boot/loader.s +++ b/src/boot/loader.s @@ -58,7 +58,8 @@ GDT64: NULL_SELECTOR: ;; null selector within 64 bits dw GDT_LENGTH ; limit of GDT dw GDT64 ; linear address of GDT - dd 0x0 + dd 0x0 ; + CODE_SELECTOR: ;; 32-bit code selector (ring 0) dw 0x0FFFF ; Segment Limit db 0x0, 0x0, 0x0 ; Base Address @@ -117,7 +118,7 @@ GDT64: ; | | `---------- DPL !!! 0 for ring 0 ; | `----------- DPL (2/2) ; `------------ 1 if in physical memory, 0 if page fault - db 10101111b ; |7|6|5|4|3|2|1|0| + db 10101111b ; |7|6|5|4|3|2|1|0| ; | | | | | | | `----- Limit 16 ; | | | | | | `------ Limit 17 ; | | | | | `------- Limit 18 diff --git a/src/boot/loader64.inc b/src/boot/loader64.inc index 2e29af6..f6611fb 100644 --- a/src/boot/loader64.inc +++ b/src/boot/loader64.inc @@ -5,8 +5,9 @@ ; NeoX ; ; ; ; Desc: Kernel (second stage) Loader for OS/K INCLUDED FUNCTIONS ; -; (x86_64 architecture only) ; +; (x86_64 architecture only) ; ;=----------------------------------------------------------------------------=; + [BITS 64] clear: @@ -33,7 +34,7 @@ write: ; bl : color code ; ; esi : string address ; ;-----------------------------------------------------------------------; - mov edi, [NextTRAM] ;TRAM ADDRESS + mov edi, [NextTRAM] ; TRAM ADDRESS push rsi push rdi .pLoop: @@ -48,7 +49,7 @@ write: mov al, bl stosb ; color subpixel add qword [NextTRAM], 0x2 ; Cursor moving - add qword [VGA_X], 0x2 ; coord + 2 because 2 subpixels + add qword [VGA_X], 0x2 ; coord + 2 because 2 subpixels jmp .pLoop .pEnd: pop rdi @@ -72,3 +73,4 @@ write: .scroll: ; XXX ; jmp .pLoop + diff --git a/src/boot/mbr.inc b/src/boot/mbr.inc index 0e121e6..dc11db1 100644 --- a/src/boot/mbr.inc +++ b/src/boot/mbr.inc @@ -34,7 +34,7 @@ read_clusters: add ax, word [UserData] ; add the UserData xor ch, ch mov cl, byte [sectorsPerCluster] ; Sectors to read - call read_sectors ; Read the sectors + call read_sectors ; Read the sectors pop ax ; Current cluster number xor dx, dx ;; Calculate next sector for FAT16 (cluster * 2) diff --git a/src/boot/mbr.s b/src/boot/mbr.s index 3387242..f7a3066 100644 --- a/src/boot/mbr.s +++ b/src/boot/mbr.s @@ -79,8 +79,8 @@ go: ;; INIT STACK cli - mov ax, STACK_SEG ; Init the staaaaaack - mov ss, ax ; Continue init the staaaaaaaack + mov ax, STACK_SEG ; Init the stack + mov ss, ax ; Continue init the stack mov sp, STACK_OFF ; Ok man, the stack is in 4K :O sti mov bp, (0x7c0-STACK_SEG) << 4 ; Correct bp (the disk description table) @@ -88,7 +88,7 @@ go: ;; INITIALIZE BOOT DISK or dl, dl ; Verifying dl points actually to the boot drive jz load_root - mov byte [Bootdrv], dl ; Another soul (the disk) saved ! + mov byte [Bootdrv], dl ; Another soul (the disk) saved! mov ah, 0x08 int 0x13 ; int 0x13 : read drive parameters/geom jc load_root @@ -97,7 +97,7 @@ go: mov dl, dh ; Convert the maximum head number to a word with another vaudou magical trip xor dh, dh inc dx ; because head numbers start at zero - mov word [heads], dx ; Another soul (the heads number) saved ! + mov word [heads], dx ; Another soul (the heads number) saved! ;; LOAD THE ROOT DIRECTORY FROM DISK @@ -117,7 +117,7 @@ load_root: mov di, BUFFER_SEG ; Set the extra segment to the disk buffer mov es, di mov di, BUFFER_OFF ; Set es:di and load the root directory into the disk buffer - call read_sectors ; Read the sectoooooooors ! + call read_sectors ; Read the sectors ;; FIND THE SECOND STAGE LOADER mov di, BUFFER_OFF ; Set es:di to the disk buffer @@ -128,7 +128,7 @@ search_root: mov si, filename ; Load the filename mov cx, 11 ; Compare first 11 bytes rep cmpsb ; Compare si and di cx times - je load_fat ; We found the LOADEEEEEEEER!!! + je load_fat ; We found the loader add ax, 32 ; File entry offset mov di, BUFFER_OFF ; Point back to the start of the entry add di, ax ; Add the offset to point to the next entry @@ -150,14 +150,14 @@ load_fat: mov cx, ax ; Store in cx mov ax, word [reservedSectors] ; Convert the first fat on the disk mov di, BUFFER_OFF ; Set es:di and load the fat sectors into the disk buffer - call read_sectors ; Read the sectooooooooooors !!! + call read_sectors ; Read the sectors ;; LOAD THE CLUSTERS OF THE LOADER AND JUMP mov di, LOAD_SEG mov es, di ; Set es:bx to where the file will load mov di, LOAD_OFF pop ax ; File cluster restored - call read_clusters ; Read clusters from the file + call read_clusters ; Read clusters from the file mov dl, byte [Bootdrv] ; Pass the boot Bootdrv into dl jmp LOAD_SEG:LOAD_OFF ; Jump to the file loaded! diff --git a/src/boot/types.h b/src/boot/types.h deleted file mode 100644 index 71faf9b..0000000 --- a/src/boot/types.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _I386_TYPE_ -#define _I386_TYPE_ - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; -typedef unsigned char uchar; - -#endif diff --git a/src/kaleid/common/assert.h b/src/kaleid/common/assert.h index 5bf73eb..da80ace 100644 --- a/src/kaleid/common/assert.h +++ b/src/kaleid/common/assert.h @@ -7,28 +7,45 @@ // Desc: Assertions // //----------------------------------------------------------------------------// -#ifndef __kaleid_assert_h -#define __kaleid_assert_h +#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 -extern const char *panicstr; -extern void panic(const char *) __dead; - -#define assert(x) do{if(!(x))panic(#x);}while(0); - -#define assert_never_used(x) do{static bool __anu_##x = FALSE; \ - assert(__anu_##x == FALSE); __anu_##x = TRUE;}while(0); - -#define assert_used_once(x) do{static bool __anu_##x; assert(__anu_##x == TRUE);} while(0); - -#else // NDEBUG +#if !defined(_NO_DEBUG) +#define _NO_DEBUG 1 +#endif #define assert(x) -#define assert_never_used(x) -#define assert_used_once(x) - -#endif // NDEBUG + +#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 index 80dfe98..a1daef3 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/atomic.h @@ -7,14 +7,23 @@ // Desc: Atomic stuff // //----------------------------------------------------------------------------// -#ifndef __kaleid_atomic_h -#define __kaleid_atomic_h +#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 -#define cli() // XXX -#define sti() // XXX -#define hlt() // XXX +#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 index 4f85241..9c9dd6a 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -7,25 +7,39 @@ // Desc: Standard include file for both kernel/ and system/ // //----------------------------------------------------------------------------// +#ifndef _KALCOMM_COMMON_H +#define _KALCOMM_COMMON_H -#ifndef __kaleid_common_h -#define __kaleid_common_h +#if !defined(_OSK_SOURCE) && (defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM)) +#define _OSK_SOURCE 1 +#endif -#define true 1 -#define TRUE 1 -#define YES 1 +#if !defined(TRUE) && !defined(FALSE) +# define TRUE 1 +# define FALSE 0 +#endif -#define false 0 -#define FALSE 0 -#define NO 0 +#ifdef _OSK_SOURCE +# define YES 1 +# define NO 0 +#endif -#define NULL ((void*)0) +#ifndef NULL +# define NULL ((void*)0) +#endif -#define PACKED __attribute__((packed)) -#define __dead __attribute__((noreturn)) +#ifndef PACKED +# define PACKED __attribute__((packed)) +#endif -#define THROWS(...) -#define ERRS(...) +#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" diff --git a/src/kaleid/common/config.h b/src/kaleid/common/config.h index a31d87f..501be3d 100644 --- a/src/kaleid/common/config.h +++ b/src/kaleid/common/config.h @@ -13,18 +13,29 @@ // We may do a script that generates "config.h" for the user, asking about // the different configuration choices in the terminal. -#ifndef __kaleid_config_h -#define __kaleid_config_h +#ifndef _KALCOMM_CONFIG_H +#define _KALCOMM_CONFIG_H //------------------------------------------// // General configuration choices // //------------------------------------------// -// Enable multiprocessor support? -// Right now this has to be left to NO +// +// Enable/disable multiprocessor support +// #define MULTIPROCESSOR NO -// etc... +// +// 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 index a31d87f..501be3d 100644 --- a/src/kaleid/common/config.h.in +++ b/src/kaleid/common/config.h.in @@ -13,18 +13,29 @@ // We may do a script that generates "config.h" for the user, asking about // the different configuration choices in the terminal. -#ifndef __kaleid_config_h -#define __kaleid_config_h +#ifndef _KALCOMM_CONFIG_H +#define _KALCOMM_CONFIG_H //------------------------------------------// // General configuration choices // //------------------------------------------// -// Enable multiprocessor support? -// Right now this has to be left to NO +// +// Enable/disable multiprocessor support +// #define MULTIPROCESSOR NO -// etc... +// +// 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/convert.h b/src/kaleid/common/convert.h new file mode 100644 index 0000000..737f016 --- /dev/null +++ b/src/kaleid/common/convert.h @@ -0,0 +1,24 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Conversion utilities // +//----------------------------------------------------------------------------// + +#ifndef _KALCOMM_CONVERT_H +#define _KALCOMM_CONVERT_H + +#ifndef _KALCOMM_COMMON_H +#include "common/common.h" +#endif + +#ifndef _OSK_SOURCE +# define itoa _osk_itoa +# define atoi _osk_atoi +#endif + +char *itoa(int, char *, int); + +#endif diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/lib/convert.c new file mode 100644 index 0000000..0f76565 --- /dev/null +++ b/src/kaleid/common/lib/convert.c @@ -0,0 +1,52 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Conversion utilities // +//----------------------------------------------------------------------------// + +#include "common/convert.h" + +// +// Digits table for bases <=36 +// +static const char digits[36] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +// +// Integer to string in any base between 2 and 36 (included) +// +char *itoa(int i, char *str, int base) +{ + int neg = 0; + char *orig = str; + + if (base < 2 || base > 36) + return NULL; + + // deal with negatives + if (i < 0) { + neg = 1; + i = -i; + } + + // deal with zero separatly + if (i == 0) { + *str++ = '0'; + } + + // compute digits... in reverse order + while (i > 0) { + *str++ = digits[i % base]; + i /= base; + } + + if (neg) *str++ = '-'; + *str = '\0'; + + return reverse(orig); +} + + diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c new file mode 100644 index 0000000..3ebe6ee --- /dev/null +++ b/src/kaleid/common/lib/memory.c @@ -0,0 +1,11 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: mem*() functions // +//----------------------------------------------------------------------------// + +#include "common/memory.h" + diff --git a/src/kaleid/common/lib/sprintf.c b/src/kaleid/common/lib/sprintf.c new file mode 100644 index 0000000..e26ded9 --- /dev/null +++ b/src/kaleid/common/lib/sprintf.c @@ -0,0 +1,69 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: sprintf()-related functions // +//----------------------------------------------------------------------------// + +#include "common/string.h" + +// +// 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; +} + +int vsprintf(char *str, const char *fmt, va_list ap) +{ + return vsnprintf(str, SIZE_T_MAX, fmt, ap); +} + +// +// (v)sprintf() but with a size limit: no more than n bytes are written in str +// XXX null termination behavior? +// +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) { + if (*fmt != '%') { + *str++ = *fmt++; + ret++; + continue; + } + + switch (*fmt) { + case 'd': + default: + break; + } + } + + return ret; +} + + diff --git a/src/kaleid/common/lib/status.c b/src/kaleid/common/lib/status.c new file mode 100644 index 0000000..5e8115c --- /dev/null +++ b/src/kaleid/common/lib/status.c @@ -0,0 +1,31 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Implementation of describe_status() // +//----------------------------------------------------------------------------// + +#include "common/common.h" + +static const char *descriptions[] = { + [-SUCCESS] = "Success", + [-FAILED] = "Failed (no precision)", + [-NOT_PERMITTED] = "Operation not permitted", + [-ACCESS_DENIED] = "Access denied", + + [-BAD_ARGUMENT] = "Bad argument", + [-BAD_ARG_RANGE] = "Bad argument (not in range)", + [-BAD_ARG_NULL] = "Bad argument (null pointer)", +}; + +const char *describe_status(status_t status) +{ + (void)descriptions; + (void)status; + + // XXX + return ""; +} + diff --git a/src/kaleid/common/lib/string.c b/src/kaleid/common/lib/string.c index 7f1d30b..f81f60c 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/lib/string.c @@ -4,28 +4,64 @@ // Authors: spectral` // // NeoX // // // -// Desc: strlen() function // +// Desc: String-related functions // //----------------------------------------------------------------------------// #include "common/string.h" -// XXX several of these should be re-wrote in assembly +// TODO multibyte, assembly -size_t strlen(const char *s) +// +// 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; s; s++, i++); + for (i = 0; i < n && src[i]; i++) { + dest[i] = src[i]; + } - return i; + while (i < n) dest[i++] = 0; + return dest; } -/*size_t wcslen(const wchar_t *s); +// +// Reverses a string +// +char *reverse(char *str) { - int i; - - for (i = 0; s; s++, i++); - - return s; -}*/ + 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 new file mode 100644 index 0000000..a877e1a --- /dev/null +++ b/src/kaleid/common/memory.h @@ -0,0 +1,18 @@ +//----------------------------------------------------------------------------// +// 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 index c7a1147..82387da 100644 --- a/src/kaleid/common/status.h +++ b/src/kaleid/common/status.h @@ -7,9 +7,19 @@ // Desc: Values for status_t // //----------------------------------------------------------------------------// +#ifndef _KALCOMM_STATUS_H +#define _KALCOMM_STATUS_H -#ifndef __kaleid_status_h -#define __kaleid_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)) @@ -23,8 +33,9 @@ #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 (-6) // EAGAIN +#define TRY_AGAIN (-7) // EAGAIN #endif diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h index d18053a..0f8542d 100644 --- a/src/kaleid/common/string.h +++ b/src/kaleid/common/string.h @@ -8,12 +8,36 @@ //----------------------------------------------------------------------------// -#ifndef __kaleid_string_h -#define __kaleid_string_h +#ifndef _KALCOMM_STRING_H +#define _KALCOMM_STRING_H +#ifndef _KALCOMM_COMMON_H #include "common/common.h" +#endif -size_t strlen(const char *s); +#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 index a48d5b8..fb7d4ff 100644 --- a/src/kaleid/common/types.h +++ b/src/kaleid/common/types.h @@ -8,25 +8,34 @@ //----------------------------------------------------------------------------// -#ifndef __kaleid_types_h -#define __kaleid_types_h +#ifndef _KALCOMM_TYPES_H +#define _KALCOMM_TYPES_H -typedef _Bool bool; -typedef unsigned char uchar; -typedef unsigned short ushort; -typedef unsigned int uint; -typedef unsigned long ulong; -typedef long double ldouble; -typedef short status_t; -typedef short port_t; -typedef uint wchar_t; -typedef ulong size_t; -typedef long ssize_t; +#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 -// XXX -typedef int atomic_t; -typedef ulong pid_t; // etc... +typedef short port_t; +typedef short status_t; +// XXX limits #endif diff --git a/src/kaleid/kernel/init.c b/src/kaleid/kernel/init.c index 41e2efc..b6f268a 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -8,13 +8,15 @@ //----------------------------------------------------------------------------// #include "kernel/init.h" +#include "kernel/ke/panic.h" #include "kernel/io/terminal.h" -void _start(void) -{ - assert(strlen("test") == 4); - +// +// Entry point of kaleid-kernel.elf +// +void kstart(void) +{ kterm_init(); - kterm_print("Hello World!"); + panic("Goodbye World :("); } diff --git a/src/kaleid/kernel/init.h b/src/kaleid/kernel/init.h index c4677da..bb80788 100644 --- a/src/kaleid/kernel/init.h +++ b/src/kaleid/kernel/init.h @@ -7,11 +7,10 @@ // Desc: Include file for init.c // //----------------------------------------------------------------------------// -#ifndef __kaleid_kernel_init_h -#define __kaleid_kernel_init_h +#ifndef _KALKERN_INIT_H +#define _KALKERN_INIT_H #include "common/common.h" -#include "common/string.h" // kernel entry point void kstart(void); diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index 20d2908..b2adfbe 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/src/kaleid/kernel/io/ports.h @@ -7,15 +7,15 @@ // Desc: Ports I/O // //----------------------------------------------------------------------------// -#ifndef __kaleid_kernel_io_ports_h -#define __kaleid_kernel_io_ports_h +#ifndef _KALKERN_IO_PORTS_H +#define _KALKERN_IO_PORTS_H #include "common/common.h" #define outb(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) -uchar inb(port_t port); -ushort inw(port_t port); +uchar inb(port_t); +ushort inw(port_t); #endif diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index 7062819..503dfcd 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -9,139 +9,178 @@ #include "kernel/io/terminal.h" -// VGA buffer size -static const size_t kterm_width = 80; -static const size_t kterm_height = 25; - -// position in the buffer -static size_t current_row; -static size_t current_col; - -static uchar current_color; - -// each buffer entry is composed of two bytes -// one is the color code, the other the character -static ushort *const buffer = (ushort *)0xB8000; - +// +// VGA-related macros +// #define ComputeColorCode(fg, bg) ((fg) | (bg) << 4) -#define ComputeEntryOffset(x, y) ((y) * kterm_width + (x)) +#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_never_used(kterm_init); + assert(!kt_stdout && !_kt_vga.kt_init && "kterm_init() called twice"); + +#ifndef _NO_DEBUG + _kt_vga.kt_init = TRUE; +#endif - kterm_change_color(ComputeColorCode(KTERM_COLOR_LIGHT_GREY, KTERM_COLOR_BLACK)); - kterm_clear(); + // to be switched to VESA + kt_stdout = &_kt_vga; + ktclear(); } -void kterm_clear(void) +// +// Fills terminal with spaces +// XXX would '\0' work too? +// +status_t kterm_clear(struct kterm *kt) { - size_t x, y; + size_t i; - assert_used_once(kterm_init); + if (kt == NULL) + return BAD_ARG_NULL; - for (x = 0; x < kterm_width; x++) { - for (y = 0; y < kterm_height; y++) { - const size_t offset = ComputeEntryOffset(x,y); - buffer[offset] = ComputeEntry(' ', current_color); - } + 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; } - // go back to beginning - current_row = current_col = 0; + kt->kt_curr_x = kt->kt_curr_y = 0; - // XXX cursor update + kterm_unlock(kt); + + return SUCCESS; } -status_t kterm_change_color(uchar color) +// +// Change the color code +// +status_t kterm_change_color(struct kterm *kt, uchar color) { if (color > KTERM_COLOR_WHITE) return BAD_ARG_RANGE; - - current_color = color; + + if (kt == NULL) + return BAD_ARG_NULL; + + kterm_lock(kt); + kt->kt_color = color; + kterm_unlock(kt); return SUCCESS; } -void kterm_putchar(char ch) +// +// 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; - - assert_used_once(kterm_init); + size_t prev_row; // carriage return takes us back to the beginning of the line // no further test should be necessary - if (ch == '\r') { current_col = 0; return; } + 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') { current_col = kterm_width - 1; } + else if (ch == '\n') { kt->kt_curr_y = kt->kt_width - 1; } // tabulations account for 4 spaces else if (ch == '\t') { - prev = current_row; + prev_row = kt->kt_curr_y; // compiler will optimize this away - for (i = 0; i < 4; i++) { + for (i = 0; i < TABSIZE; i++) { // tabulations can't spread over two lines - if (current_row != prev) return; - - kterm_putchar(' '); + 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(current_col, current_row); - buffer[offset] = ComputeEntry(ch, current_color); + 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 (++current_col == kterm_width) { - current_col = 0; + if (++kt->kt_curr_x == kt->kt_width) { + kt->kt_curr_x = 0; - // end of buffer? - if (++current_row == kterm_height) { - current_row = 0; - // XXX save previous buffer(?) and clear + // line feed + end of buffer? + if (++kt->kt_curr_y == kt->kt_height) { + kt->kt_curr_y = 0; } } } -void kterm_print(const char *s) +// +// Writes a single character on the terminal (LOCKED version) +// +status_t kterm_putch(struct kterm *kt, char ch) { - while (*s) { - kterm_putchar(*s); - s++; + 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++); } -} - -void panic(const char *s) -{ - cli(); - - panicstr = s; - - kterm_clear(); - - kterm_print("panic! - "); - kterm_print(s); - - while (1) hlt(); + kterm_unlock(kt); - __builtin_unreachable(); + return SUCCESS; } - - - - - - - - - - - diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 1ba775a..5cb39bf 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -7,37 +7,58 @@ // Desc: Early terminal functions // //----------------------------------------------------------------------------// -#ifndef __kaleid_kernel_io_terminal_h -#define __kaleid_kernel_io_terminal_h +#ifndef _KALKERN_IO_KTERM_H +#define _KALKERN_IO_KTERM_H #include "common/common.h" // all available colors -enum { - KTERM_COLOR_BLACK, - KTERM_COLOR_BLUE, - KTERM_COLOR_GREEN, - KTERM_COLOR_CYAN, - KTERM_COLOR_RED, - KTERM_COLOR_MAGENTA, - KTERM_COLOR_BROWN, - KTERM_COLOR_LIGHT_GREY, - KTERM_COLOR_DARK_GREY, - KTERM_COLOR_LIGHT_BLUE, - KTERM_COLOR_LIGHT_GREEN, - KTERM_COLOR_LIGHT_CYAN, - KTERM_COLOR_LIGHT_RED, - KTERM_COLOR_LIGHT_MAGENTA, - KTERM_COLOR_LIGHT_BROWN, - KTERM_COLOR_WHITE +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 }; -void kterm_init(void); -void kterm_clear(void); -void kterm_putchar(char ch); -void kterm_print(const char *s); +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 + +}; -status_t kterm_change_color(uchar color) ERRS(BAD_ARG_RANGE); +// 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 new file mode 100644 index 0000000..04310c6 --- /dev/null +++ b/src/kaleid/kernel/ke/panic.c @@ -0,0 +1,59 @@ +//----------------------------------------------------------------------------// +// 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 new file mode 100644 index 0000000..403d728 --- /dev/null +++ b/src/kaleid/kernel/ke/panic.h @@ -0,0 +1,18 @@ +//----------------------------------------------------------------------------// +// 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 new file mode 100644 index 0000000..5c62036 --- /dev/null +++ b/src/kaleid/kernel/mm/malloc.c @@ -0,0 +1,12 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Memory allocation routines // +// Only exists to trigger Neox // +//----------------------------------------------------------------------------// + +#include "kernel/mm/malloc.h" + diff --git a/src/kaleid/kernel/mm/malloc.h b/src/kaleid/kernel/mm/malloc.h new file mode 100644 index 0000000..2669121 --- /dev/null +++ b/src/kaleid/kernel/mm/malloc.h @@ -0,0 +1,16 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Memory allocation routines // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_MM_MALLOC_H +#define _KALKERN_MM_MALLOC_H + +#include "common/common.h" + +#endif + diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c new file mode 100644 index 0000000..a1707c6 --- /dev/null +++ b/src/kaleid/linux/test-common.c @@ -0,0 +1,44 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Test file for common/ // +//----------------------------------------------------------------------------// + +#include +#include +#include + +#define KEEP_KALCOMM_TYPES_MINIMAL + +#include "common/common.h" +#include "common/string.h" + +int main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + + const char *test1 = "test string\n"; + char *test2 = malloc(strlen(test1)); + char *test3 = malloc(strlen(test1)); + + printf("1\n"); + + #undef strlen + assert(strlen("test string") == _osk_strlen("test string")); + + #undef strcpy + assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0); + + // tests done + printf("2\n"); + + free(test2); + free(test3); + + return 0; +} + diff --git a/src/kernel.ld b/src/kernel.ld new file mode 100644 index 0000000..86b66ea --- /dev/null +++ b/src/kernel.ld @@ -0,0 +1,49 @@ +ENTRY(kstart) +SECTIONS +{ + . = 0x4000; /* XXX 0x4000 is temporary */ + + .text : AT(ADDR(.text) - 0x4000) + { + _code = .; + *(.text) + *(.rodata*) + . = ALIGN(4096); + } + + .data : AT(ADDR(.data) - 0x4000) + { + _data = .; + *(.data) + . = ALIGN(4096); + } + + .eh_frame : AT(ADDR(.eh_frame) - 0x4000) + { + _ehframe = .; + *(.eh_frame) + . = ALIGN(4096); + } + + .bss : AT(ADDR(.bss) - 0x4000) + { + _bss = .; + *(.bss) + + /* + * You usually need to include generated COMMON symbols + * under kernel BSS section or use gcc's -fno-common + */ + + *(COMMON) + . = ALIGN(4096); + } + + _end = .; + + /DISCARD/ : + { + *(.comment) + } +} + diff --git a/src/making.sh b/src/making.sh deleted file mode 100644 index e69de29..0000000 diff --git a/src/project-tree.txt b/src/project-tree.txt index 1304d6a..7e342f8 100644 --- a/src/project-tree.txt +++ b/src/project-tree.txt @@ -7,6 +7,8 @@ // Desc: Project Tree // //----------------------------------------------------------------------------// +// XXX *not* up to date + src/ | + boot/ diff --git a/src/script.sh b/src/script.sh deleted file mode 100644 index 1ef10e1..0000000 --- a/src/script.sh +++ /dev/null @@ -1,4 +0,0 @@ -make -cp ../bin/bootloader.bin "~/Documents/GNU OSK/bin/bootloader.bin" -qemu-system-x86_64 -hda "~/Documents/GNU OSK/bin/bootloader.bin" -qemu-system-i386 -hda "~/Documents/GNU OSK/bin/bootloader.bin"