From 4ae060bd5dc42d4acda921c60e391ee926b5e0c7 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Sat, 29 Dec 2018 23:51:00 +0100 Subject: [PATCH] Big stuff --- src/Makefile | 5 ++-- src/kaleid/common/atomic.h | 6 ++-- src/kaleid/kernel/init.c | 8 +++-- src/kaleid/kernel/io/ports.h | 8 +++-- src/kaleid/kernel/io/terminal.c | 52 ++++++++++++++++----------------- src/kaleid/kernel/io/terminal.h | 30 ++++++++++--------- src/kaleid/kernel/ke/panic.c | 24 ++++++++------- src/kaleid/kernel/ke/panic.h | 10 +++++-- src/kaleid/kernel/ke/state.c | 16 ++++++++++ src/kaleid/kernel/ke/state.h | 38 ++++++++++++++++++++++++ 10 files changed, 133 insertions(+), 64 deletions(-) create mode 100644 src/kaleid/kernel/ke/state.c create mode 100644 src/kaleid/kernel/ke/state.h diff --git a/src/Makefile b/src/Makefile index cb8b590..4988716 100644 --- a/src/Makefile +++ b/src/Makefile @@ -86,11 +86,12 @@ test-common: KOBJDIR=$(OBJDIR)/$(KERNDIR) KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h -KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/panic.c $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c -KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o +KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/state.c $(KERNDIR)/ke/panic.c $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c +KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/state.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o kernel: common $(KERNSRCS) $(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o + $(KCC) -c $(KERNDIR)/ke/state.c -o $(KOBJDIR)/ke/state.o $(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o $(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o diff --git a/src/kaleid/common/atomic.h b/src/kaleid/common/atomic.h index f520339..56a4afb 100644 --- a/src/kaleid/common/atomic.h +++ b/src/kaleid/common/atomic.h @@ -19,9 +19,9 @@ #ifdef _KALEID_KERNEL // only available in the kernel -#define cli() asm volatile ("cli") -#define sti() asm volatile ("sti") -#define hlt() asm volatile ("hlt") +#define DosDisableInterrupts() asm volatile ("cli") +#define DosEnableInterrupts() asm volatile ("sti") +#define DosHaltCPU() asm volatile ("hlt") #endif diff --git a/src/kaleid/kernel/init.c b/src/kaleid/kernel/init.c index b6f268a..aa794fe 100644 --- a/src/kaleid/kernel/init.c +++ b/src/kaleid/kernel/init.c @@ -15,8 +15,10 @@ // Entry point of kaleid-kernel.elf // void kstart(void) -{ - kterm_init(); - panic("Goodbye World :("); +{ + DosSetKernState(KSTATE_INIT); + + DosInitTerms(); + DosPanic("Goodbye World :("); } diff --git a/src/kaleid/kernel/io/ports.h b/src/kaleid/kernel/io/ports.h index b2adfbe..8df790d 100644 --- a/src/kaleid/kernel/io/ports.h +++ b/src/kaleid/kernel/io/ports.h @@ -10,12 +10,14 @@ #ifndef _KALKERN_IO_PORTS_H #define _KALKERN_IO_PORTS_H +#ifndef _KALCOMM_COMMON_H #include "common/common.h" +#endif -#define outb(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) +#define DosWriteByteOnPort(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) -uchar inb(port_t); -ushort inw(port_t); +uchar DosReadByteFromPort(port_t); +ushort DosReadWordFromPort(port_t); #endif diff --git a/src/kaleid/kernel/io/terminal.c b/src/kaleid/kernel/io/terminal.c index 0fd46ac..e172daf 100644 --- a/src/kaleid/kernel/io/terminal.c +++ b/src/kaleid/kernel/io/terminal.c @@ -19,7 +19,7 @@ // // VGA output // -static struct kterm _kt_vga = { +static terminal_t _vga_term = { .kt_buffer = (ushort *)0xB8000, .kt_width = 80, .kt_height = 25, @@ -35,50 +35,50 @@ static struct kterm _kt_vga = { // // Standard output terminal // -struct kterm *kt_stdout; +terminal_t *stdout; // // Initialize standard output // -void kterm_init(void) +void DosInitTerms(void) { - assert(!kt_stdout && !_kt_vga.kt_init && "kterm_init() called twice"); - + assert(!stdout && !_vga_term.kt_init && "DosInitTerms() called twice"); + #ifndef _NO_DEBUG - _kt_vga.kt_init = TRUE; + _vga_term.kt_init = TRUE; #endif // to be switched to VESA - kt_stdout = &_kt_vga; - ktclear(); + stdout = &_vga_term; + DosClearTerm(stdout); } // // Fills terminal with spaces // XXX would '\0' work too? // -status_t kterm_clear(struct kterm *kt) +status_t DosClearTerm(terminal_t *kt) { size_t i; if (kt == NULL) return BAD_ARG_NULL; - assert(kt->kt_init && "kterm_clear called before initialization"); + assert(kt->kt_init && "DosClearTerm called before initialization"); - kterm_lock(kt); + DosLockTerm(kt); const ushort filler = ComputeEntry(' ', kt->kt_color); const size_t bufsize = kt->kt_width * kt->kt_height; for (i = 0; i < bufsize; i++) { - // XXX implement memset() + // XXX implement memsetw() kt->kt_buffer[i] = filler; } kt->kt_curr_x = kt->kt_curr_y = 0; - kterm_unlock(kt); + DosUnlockTerm(kt); return SUCCESS; } @@ -86,7 +86,7 @@ status_t kterm_clear(struct kterm *kt) // // Change the color code // -status_t kterm_change_color(struct kterm *kt, uchar color) +status_t DosChTermColor(terminal_t *kt, uchar color) { if (color > KTERM_COLOR_WHITE) return BAD_ARG_RANGE; @@ -94,9 +94,9 @@ status_t kterm_change_color(struct kterm *kt, uchar color) if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); + DosLockTerm(kt); kt->kt_color = color; - kterm_unlock(kt); + DosUnlockTerm(kt); return SUCCESS; } @@ -108,7 +108,7 @@ status_t kterm_change_color(struct kterm *kt, uchar color) // - always use kterm_putch (LOCKED version) // - doesn't check for NULL input // -void kterm_putch_unlocked(struct kterm *kt, char ch) +void DosPutOnTerm_Unlocked(terminal_t *kt, char ch) { int i; size_t prev_row; @@ -128,7 +128,7 @@ void kterm_putch_unlocked(struct kterm *kt, char ch) for (i = 0; i < TABSIZE; i++) { // tabulations can't spread over two lines if (kt->kt_curr_y == prev_row) { - kterm_putch_unlocked(kt, ' '); + DosPutOnTerm_Unlocked(kt, ' '); } } } @@ -153,14 +153,14 @@ void kterm_putch_unlocked(struct kterm *kt, char ch) // // Writes a single character on the terminal (LOCKED version) // -status_t kterm_putch(struct kterm *kt, char ch) +status_t DosPutOnTerm(terminal_t *kt, char ch) { if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); - kterm_putch_unlocked(kt, ch); - kterm_unlock(kt); + DosLockTerm(kt); + DosPutOnTerm_Unlocked(kt, ch); + DosUnlockTerm(kt); return SUCCESS; } @@ -169,16 +169,16 @@ status_t kterm_putch(struct kterm *kt, char ch) // // Print string on terminal // -status_t kterm_print(struct kterm *kt, const char *str) +status_t DosPrintOnTerm(terminal_t *kt, const char *str) { if (kt == NULL) return BAD_ARG_NULL; - kterm_lock(kt); + DosLockTerm(kt); while (*str) { - kterm_putch_unlocked(kt, *str++); + DosPutOnTerm_Unlocked(kt, *str++); } - kterm_unlock(kt); + DosUnlockTerm(kt); return SUCCESS; } diff --git a/src/kaleid/kernel/io/terminal.h b/src/kaleid/kernel/io/terminal.h index 5cb39bf..229be43 100644 --- a/src/kaleid/kernel/io/terminal.h +++ b/src/kaleid/kernel/io/terminal.h @@ -10,10 +10,12 @@ #ifndef _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H +#ifndef _KALCOMM_COMMON_H #include "common/common.h" +#endif // all available colors -enum kterm_color { +enum kterm_colors { KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, @@ -24,7 +26,7 @@ enum kterm_color { KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE }; -struct kterm { +typedef struct kterm { void *kt_lock; ushort *kt_buffer; uchar kt_color; @@ -32,23 +34,23 @@ struct kterm { size_t kt_height; off_t kt_curr_x; off_t kt_curr_y; + // XXX flags #ifndef _NO_DEBUG bool kt_init; #endif - -}; +} terminal_t; // current "standard" terminal -extern struct kterm *kt_stdout; +extern terminal_t *stdout; -void kterm_init(void); -status_t kterm_clear(struct kterm *); -status_t kterm_putch(struct kterm *, char); -status_t kterm_print(struct kterm *, const char *); -status_t kterm_change_color(struct kterm *, uchar); +void DosInitTerms(void); +status_t DosClearTerm(terminal_t *); +status_t DosPutOnTerm(terminal_t *, char); +status_t DosPrintOnTerm(terminal_t *, const char *); +status_t DosChTermColor(terminal_t *, uchar); #ifdef _UNLOCKED_IO -void kterm_putch_unlocked(struct kterm *, char); +void DosPutOnTerm_Unlocked(terminal_t *, char); #endif #define ktclear() kterm_clear(kt_stdout) @@ -56,9 +58,9 @@ void kterm_putch_unlocked(struct kterm *, char); #define ktprint(s) kterm_print(kt_stdout, (s)) #define ktchcol(c) kterm_change_color(kt_stdout, (c)) -#define kterm_lock(kt) -#define kterm_trylock(kt) -#define kterm_unlock(kt) +#define DosLockTerm(kt) +#define DosTryLockTerm(kt) +#define DosUnlockTerm(kt) #endif diff --git a/src/kaleid/kernel/ke/panic.c b/src/kaleid/kernel/ke/panic.c index 04310c6..38f8f9d 100644 --- a/src/kaleid/kernel/ke/panic.c +++ b/src/kaleid/kernel/ke/panic.c @@ -8,6 +8,8 @@ //----------------------------------------------------------------------------// #include "kernel/ke/panic.h" +#include "kernel/ke/state.h" +#include "kernel/io/terminal.h" // // Panic message @@ -20,22 +22,24 @@ const char *panicstr = NULL; noreturn void ___assert_handler(const char *msg, const char *file, int line, const char *func) { // not getting out of here - cli(); + DosDisableInterrupts(); (void)file; (void)line; (void)func; // XXX sprintf() to create a proper panicstr - panic(msg); + DosPanic(msg); } // // Your best boy panic() // -void panic(const char *str) +void DosPanic(const char *str) { - cli(); + DosDisableInterrupts(); - ktclear(); + DosSetKernState(KSTATE_PANIC); + + DosClearTerm(stdout); if (str == NULL) { str = "(no message given)"; @@ -43,17 +47,17 @@ void panic(const char *str) if (panicstr) { // shouldn't be possible - ktprint("double panic!\n"); - hlt(); + DosPrintOnTerm(stdout, "double panic!\n"); + DosHaltCPU(); } panicstr = str; - ktprint("panic! - "); - ktprint(str); + DosPrintOnTerm(stdout, "panic! - "); + DosPrintOnTerm(stdout, str); while (TRUE) { - hlt(); + DosHaltCPU(); } } diff --git a/src/kaleid/kernel/ke/panic.h b/src/kaleid/kernel/ke/panic.h index 403d728..f73387a 100644 --- a/src/kaleid/kernel/ke/panic.h +++ b/src/kaleid/kernel/ke/panic.h @@ -10,9 +10,13 @@ #ifndef _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H -#include "kernel/io/terminal.h" +#include "kernel/ke/state.h" -extern const char *panicstr; -noreturn void panic(const char *); +extern const char *__panicstr; +noreturn void DosPanic(const char *); + +#define DosGetPanicStr() (__panicstr) + +#define panic DosPanic #endif diff --git a/src/kaleid/kernel/ke/state.c b/src/kaleid/kernel/ke/state.c new file mode 100644 index 0000000..e8a79fe --- /dev/null +++ b/src/kaleid/kernel/ke/state.c @@ -0,0 +1,16 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Current kernel state // +//----------------------------------------------------------------------------// + +#include "kernel/ke/state.h" + +// +// Current kernel state +// +uchar __kstate; + diff --git a/src/kaleid/kernel/ke/state.h b/src/kaleid/kernel/ke/state.h new file mode 100644 index 0000000..ba6bf8d --- /dev/null +++ b/src/kaleid/kernel/ke/state.h @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------------// +// GNU GPL OS/K // +// // +// Authors: spectral` // +// NeoX // +// // +// Desc: Current kernel state // +//----------------------------------------------------------------------------// + +#ifndef _KALKERN_KE_STATE_H +#define _KALKERN_KE_STATE_H + +#ifndef _KALCOMM_COMMON_H +#include "common/common.h" +#endif + +// XXX improve this +enum kernel_states { + // a process is running in kernel mode + KSTATE_PROCESS, + + // the kernel is not running a process + KSTATE_KERNEL, + + // the kernel is panicking + KSTATE_PANIC, + + // the kernel is booting + KSTATE_INIT, +}; + +extern uchar __kstate; + +#define DosGetKernState() (__kstate) +#define DosSetKernState(x) (__kstate = (x)) + +#endif +