From 003bd3be3c160abc597f4f9b01c987b0ba62ab22 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 24 Dec 2018 22:38:14 +0100 Subject: [PATCH] x --- src/ChangeLog | 2 +- src/Makefile | 60 ++++++++------------------------- src/boot/loader64.inc | 7 ++-- src/kaleid/common/assert.h | 16 ++++++--- src/kaleid/common/atomic.h | 5 +++ src/kaleid/common/common.h | 2 +- src/kaleid/common/config.h | 17 ++++++++-- src/kaleid/common/config.h.in | 17 ++++++++-- src/kaleid/common/lib/status.c | 31 +++++++++++++++++ src/kaleid/common/lib/string.c | 29 ++++++++++++---- src/kaleid/common/status.h | 3 ++ src/kaleid/common/string.h | 4 ++- src/kaleid/common/types.h | 5 +-- src/kaleid/kernel/init.h | 1 + src/kaleid/kernel/io/ports.h | 4 +-- src/kaleid/kernel/io/terminal.c | 38 ++------------------- src/kaleid/kernel/io/terminal.h | 26 +++++++------- src/kaleid/kernel/ke/panic.c | 59 ++++++++++++++++++++++++++++++++ src/kaleid/kernel/ke/panic.h | 18 ++++++++++ 19 files changed, 221 insertions(+), 123 deletions(-) create mode 100644 src/kaleid/common/lib/status.c create mode 100644 src/kaleid/kernel/ke/panic.c create mode 100644 src/kaleid/kernel/ke/panic.h 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 a4a69e5..4c61f15 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,11 +8,11 @@ #----------------------------------------------------------------------------# CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" -CWARNS=-Wall -Wextra -Werror -pedantic CLDSCR=-T kernel.ld +CWARNS= -pedantic -Wall -Wextra -Werror CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CDEFINES=-D_KALEID_KERNEL CINCLUDES=-I./kaleid -CDEFINES= CC=$(CCNAME) $(CWARNS) $(CLDSCR) $(CFLAGS) $(CDEFINES) $(CINCLUDES) @@ -57,23 +57,26 @@ COBJDIR=$(OBJDIR)/$(COMMDIR) COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \ $(COMMDIR)/status.h -COBJECTS=$(COBJDIR)/lib/string.o +COBJECTS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o -common: common.lib.string.c $(COMMDEPS) +common: common.lib.string.c common.lib.status.c common.lib.string.c: $(COMMDIR)/lib/string.c $(COMMDEPS) - $(CC) -c $(COMMDIR)/lib/string.c -o $(OBJDIR)/$(COMMDIR)/lib/string.o + $(CC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o + +common.lib.status.c: $(COMMDIR)/lib/status.c $(COMMDEPS) + $(CC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o #----------------------------------------------------------------------------# # KERNEL MAKEFILE KOBJDIR=$(OBJDIR)/$(KERNDIR) -KERNDEPS=$(COMMDEPS) $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h +KERNDEPS=$(COMMDEPS) $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h -kernel: common kernel.io.terminal.c kernel.init.c kernel.io.ports.c +kernel: common kernel.io.terminal.c kernel.init.c kernel.io.ports.c kernel.ke.panic.c $(CC) -o $(BINDIR)/kaleid-kernel.elf \ - $(COBJECTS) $(KOBJDIR)/init.o \ + $(COBJECTS) $(KOBJDIR)/init.o $(KOBJDIR)/ke/panic.o \ $(KOBJDIR)/io/terminal.o $(KOBJDIR)/io/ports.o @@ -85,44 +88,9 @@ kernel.io.ports.c: $(KERNDIR)/io/ports.c $(KERNDEPS) kernel.io.terminal.c: $(KERNDIR)/io/terminal.c $(KERNDEPS) $(CC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +kernel.ke.panic.c: $(KERNDIR)/ke/panic.c $(KERNDEPS) + $(CC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o +#----------------------------------------------------------------------------# diff --git a/src/boot/loader64.inc b/src/boot/loader64.inc index 10406b3..f6611fb 100644 --- a/src/boot/loader64.inc +++ b/src/boot/loader64.inc @@ -5,7 +5,7 @@ ; NeoX ; ; ; ; Desc: Kernel (second stage) Loader for OS/K INCLUDED FUNCTIONS ; -; (x86_64 architecture only) ; +; (x86_64 architecture only) ; ;=----------------------------------------------------------------------------=; [BITS 64] @@ -34,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: @@ -49,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 @@ -73,3 +73,4 @@ write: .scroll: ; XXX ; jmp .pLoop + diff --git a/src/kaleid/common/assert.h b/src/kaleid/common/assert.h index 5e109d3..98aadcc 100644 --- a/src/kaleid/common/assert.h +++ b/src/kaleid/common/assert.h @@ -10,17 +10,19 @@ #ifndef _KALCOMM_ASSERT_H #define _KALCOMM_ASSERT_H -extern const char *panicstr; -extern void panic(const char *) __dead; +#if !defined(_NO_DEBUG) && !defined(NDEBUG) -#if !defined(_NO_DEBUG) +// uses panic() in kernel, abort() in system +noreturn void ___assert_handler(const char *, const char *, int, const char *); -#define assert(x) do{if(!(x))panic(#x);}while(0); +#define assert(x) do{if(!(x))___assert_handler(#x, __FILE__, __LINE__, __func__);}while(0); +/* #define assert_never_used(x) do{static bool __##x##_init = FALSE; \ assert(__##x##_init == FALSE); __##x##_init = TRUE;}while(0); #define assert_used_once(x) do{static bool __##x##_init; assert(__##x##_init == TRUE);} while(0); +*/ #else // not debugging @@ -28,9 +30,13 @@ extern void panic(const char *) __dead; #define NDEBUG 1 #endif +#if !defined(_NO_DEBUG) +#define _NO_DEBUG 1 +#endif + #define assert(x) -#define assert_never_used(x) #define assert_used_once(x) +#define assert_never_used(x) #endif diff --git a/src/kaleid/common/atomic.h b/src/kaleid/common/atomic.h index f8d9e72..bb4bf04 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/atomic.h @@ -12,9 +12,14 @@ // 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 index 6107b21..9c06922 100644 --- a/src/kaleid/common/common.h +++ b/src/kaleid/common/common.h @@ -22,7 +22,7 @@ #define NULL ((void*)0) #define PACKED __attribute__((packed)) -#define __dead __attribute__((noreturn)) +#define noreturn __attribute__((noreturn)) #define THROWS(...) #define ERRS(...) diff --git a/src/kaleid/common/config.h b/src/kaleid/common/config.h index 2374b66..501be3d 100644 --- a/src/kaleid/common/config.h +++ b/src/kaleid/common/config.h @@ -20,11 +20,22 @@ // 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 2374b66..501be3d 100644 --- a/src/kaleid/common/config.h.in +++ b/src/kaleid/common/config.h.in @@ -20,11 +20,22 @@ // 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/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 f7093fe..083f269 100644 --- a/src/kaleid/common/lib/string.c +++ b/src/kaleid/common/lib/string.c @@ -4,21 +4,38 @@ // Authors: spectral` // // NeoX // // // -// Desc: strlen() function // +// Desc: String-related functions // //----------------------------------------------------------------------------// #include "common/string.h" -// -// Return str's length // TODO multibyte, assembly -// + size_t strlen(const char *str) +{ + const char *base = str; + + while (str) str++; + + return (str - base); +} + +char *strcpy(char *dest, const char *src) +{ + char *base = dest; + while ((*dest++ = *src++)); + return base; +} + +char *strncpy(char *dest, const char *src, size_t n) { size_t i; - for (i = 0; str; str++, i++); + for (i = 0; i < n && src[i]; i++) { + dest[i] = src[i]; + } - return i; + while (i < n) dest[i++] = 0; + return dest; } diff --git a/src/kaleid/common/status.h b/src/kaleid/common/status.h index 72e29a1..ee9516e 100644 --- a/src/kaleid/common/status.h +++ b/src/kaleid/common/status.h @@ -14,6 +14,9 @@ #define STATUS_FAILED(x) ((x) < 0)) #define STATUS_SUCCESS(x) (!STATUS_FAILED(x)) +// see in common/lib/status.c for status messages +const char *describe_status(status_t); + #define SUCCESS (0) // everything went fine #define FAILED (-1) diff --git a/src/kaleid/common/string.h b/src/kaleid/common/string.h index 2605063..bf5fdf3 100644 --- a/src/kaleid/common/string.h +++ b/src/kaleid/common/string.h @@ -13,7 +13,9 @@ #include "common/common.h" -size_t strlen(const char *s); +size_t strlen(const char *); +char *strcpy(char *, const char *); +char *strncpy(char *, const char *, size_t); #endif diff --git a/src/kaleid/common/types.h b/src/kaleid/common/types.h index 57c73bf..abe48a5 100644 --- a/src/kaleid/common/types.h +++ b/src/kaleid/common/types.h @@ -22,9 +22,10 @@ typedef short port_t; typedef uint wchar_t; typedef ulong size_t; typedef long ssize_t; +typedef size_t off_t; -typedef int atomic_t; -typedef ulong pid_t; +typedef int atomic_t; +typedef ulong pid_t; #endif diff --git a/src/kaleid/kernel/init.h b/src/kaleid/kernel/init.h index 2b85b67..8238842 100644 --- a/src/kaleid/kernel/init.h +++ b/src/kaleid/kernel/init.h @@ -12,6 +12,7 @@ #include "common/common.h" #include "common/string.h" +#include "kernel/ke/panic.h" // kernel entry point void kstart(void); diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index cd4c5cc..b2adfbe 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/src/kaleid/kernel/io/ports.h @@ -14,8 +14,8 @@ #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 38285a3..503dfcd 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -125,7 +125,7 @@ void kterm_putch_unlocked(struct kterm *kt, char ch) else if (ch == '\t') { 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 (kt->kt_curr_y == prev_row) { kterm_putch_unlocked(kt, ' '); @@ -176,45 +176,11 @@ status_t kterm_print(struct kterm *kt, const char *str) kterm_lock(kt); while (*str) { - kterm_putch_unlocked(kt, *str); - s++; + kterm_putch_unlocked(kt, *str++); } kterm_unlock(kt); return SUCCESS; } -// -// Panic message -// -const char *panicstr = NULL; - -// -// Your best boy panic() -// -void panic(const char *s) -{ - cli(); - - ktclear(); - - if (s == NULL) { - s = "(no message given)"; - } - - if (panicstr) { - // shouldn't be possible - ktprint("double panic!\n"); - hlt(); - } - - panicstr = s; - - ktprint("panic! - "); - ktprint(s); - - while (TRUE) { - hlt(); - } -} diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 25ad1cd..5cb39bf 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -25,17 +25,15 @@ enum kterm_color { }; struct kterm { - // flags? - - void *kt_lock; - ushort *kt_buffer; - - uchar kt_color; - size_t kt_width, kt_height; - size_t kt_curr_x, kt_curr_y; - + 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; + bool kt_init; #endif }; @@ -50,13 +48,13 @@ 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 ch); +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 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) 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