Big stuff

This commit is contained in:
Julian Barathieu 2018-12-29 23:51:00 +01:00
parent e76a30d1c5
commit 4ae060bd5d
10 changed files with 133 additions and 64 deletions

View File

@ -86,11 +86,12 @@ test-common:
KOBJDIR=$(OBJDIR)/$(KERNDIR) KOBJDIR=$(OBJDIR)/$(KERNDIR)
KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h 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 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/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/state.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o
kernel: common $(KERNSRCS) kernel: common $(KERNSRCS)
$(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o $(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)/ke/panic.c -o $(KOBJDIR)/ke/panic.o
$(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o $(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o
$(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o $(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o

View File

@ -19,9 +19,9 @@
#ifdef _KALEID_KERNEL #ifdef _KALEID_KERNEL
// only available in the kernel // only available in the kernel
#define cli() asm volatile ("cli") #define DosDisableInterrupts() asm volatile ("cli")
#define sti() asm volatile ("sti") #define DosEnableInterrupts() asm volatile ("sti")
#define hlt() asm volatile ("hlt") #define DosHaltCPU() asm volatile ("hlt")
#endif #endif

View File

@ -16,7 +16,9 @@
// //
void kstart(void) void kstart(void)
{ {
kterm_init(); DosSetKernState(KSTATE_INIT);
panic("Goodbye World :(");
DosInitTerms();
DosPanic("Goodbye World :(");
} }

View File

@ -10,12 +10,14 @@
#ifndef _KALKERN_IO_PORTS_H #ifndef _KALKERN_IO_PORTS_H
#define _KALKERN_IO_PORTS_H #define _KALKERN_IO_PORTS_H
#ifndef _KALCOMM_COMMON_H
#include "common/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); uchar DosReadByteFromPort(port_t);
ushort inw(port_t); ushort DosReadWordFromPort(port_t);
#endif #endif

View File

@ -19,7 +19,7 @@
// //
// VGA output // VGA output
// //
static struct kterm _kt_vga = { static terminal_t _vga_term = {
.kt_buffer = (ushort *)0xB8000, .kt_buffer = (ushort *)0xB8000,
.kt_width = 80, .kt_width = 80,
.kt_height = 25, .kt_height = 25,
@ -35,50 +35,50 @@ static struct kterm _kt_vga = {
// //
// Standard output terminal // Standard output terminal
// //
struct kterm *kt_stdout; terminal_t *stdout;
// //
// Initialize standard output // 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 #ifndef _NO_DEBUG
_kt_vga.kt_init = TRUE; _vga_term.kt_init = TRUE;
#endif #endif
// to be switched to VESA // to be switched to VESA
kt_stdout = &_kt_vga; stdout = &_vga_term;
ktclear(); DosClearTerm(stdout);
} }
// //
// Fills terminal with spaces // Fills terminal with spaces
// XXX would '\0' work too? // XXX would '\0' work too?
// //
status_t kterm_clear(struct kterm *kt) status_t DosClearTerm(terminal_t *kt)
{ {
size_t i; size_t i;
if (kt == NULL) if (kt == NULL)
return BAD_ARG_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 ushort filler = ComputeEntry(' ', kt->kt_color);
const size_t bufsize = kt->kt_width * kt->kt_height; const size_t bufsize = kt->kt_width * kt->kt_height;
for (i = 0; i < bufsize; i++) { for (i = 0; i < bufsize; i++) {
// XXX implement memset() // XXX implement memsetw()
kt->kt_buffer[i] = filler; kt->kt_buffer[i] = filler;
} }
kt->kt_curr_x = kt->kt_curr_y = 0; kt->kt_curr_x = kt->kt_curr_y = 0;
kterm_unlock(kt); DosUnlockTerm(kt);
return SUCCESS; return SUCCESS;
} }
@ -86,7 +86,7 @@ status_t kterm_clear(struct kterm *kt)
// //
// Change the color code // 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) if (color > KTERM_COLOR_WHITE)
return BAD_ARG_RANGE; return BAD_ARG_RANGE;
@ -94,9 +94,9 @@ status_t kterm_change_color(struct kterm *kt, uchar color)
if (kt == NULL) if (kt == NULL)
return BAD_ARG_NULL; return BAD_ARG_NULL;
kterm_lock(kt); DosLockTerm(kt);
kt->kt_color = color; kt->kt_color = color;
kterm_unlock(kt); DosUnlockTerm(kt);
return SUCCESS; return SUCCESS;
} }
@ -108,7 +108,7 @@ status_t kterm_change_color(struct kterm *kt, uchar color)
// - always use kterm_putch (LOCKED version) // - always use kterm_putch (LOCKED version)
// - doesn't check for NULL input // - 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; int i;
size_t prev_row; size_t prev_row;
@ -128,7 +128,7 @@ void kterm_putch_unlocked(struct kterm *kt, char ch)
for (i = 0; i < TABSIZE; i++) { for (i = 0; i < TABSIZE; i++) {
// tabulations can't spread over two lines // tabulations can't spread over two lines
if (kt->kt_curr_y == prev_row) { 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) // 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) if (kt == NULL)
return BAD_ARG_NULL; return BAD_ARG_NULL;
kterm_lock(kt); DosLockTerm(kt);
kterm_putch_unlocked(kt, ch); DosPutOnTerm_Unlocked(kt, ch);
kterm_unlock(kt); DosUnlockTerm(kt);
return SUCCESS; return SUCCESS;
} }
@ -169,16 +169,16 @@ status_t kterm_putch(struct kterm *kt, char ch)
// //
// Print string on terminal // 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) if (kt == NULL)
return BAD_ARG_NULL; return BAD_ARG_NULL;
kterm_lock(kt); DosLockTerm(kt);
while (*str) { while (*str) {
kterm_putch_unlocked(kt, *str++); DosPutOnTerm_Unlocked(kt, *str++);
} }
kterm_unlock(kt); DosUnlockTerm(kt);
return SUCCESS; return SUCCESS;
} }

View File

@ -10,10 +10,12 @@
#ifndef _KALKERN_IO_KTERM_H #ifndef _KALKERN_IO_KTERM_H
#define _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H
#ifndef _KALCOMM_COMMON_H
#include "common/common.h" #include "common/common.h"
#endif
// all available colors // all available colors
enum kterm_color { enum kterm_colors {
KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, KTERM_COLOR_BLACK, KTERM_COLOR_BLUE,
KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, KTERM_COLOR_GREEN, KTERM_COLOR_CYAN,
KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, KTERM_COLOR_RED, KTERM_COLOR_MAGENTA,
@ -24,7 +26,7 @@ enum kterm_color {
KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE
}; };
struct kterm { typedef struct kterm {
void *kt_lock; void *kt_lock;
ushort *kt_buffer; ushort *kt_buffer;
uchar kt_color; uchar kt_color;
@ -32,23 +34,23 @@ struct kterm {
size_t kt_height; size_t kt_height;
off_t kt_curr_x; off_t kt_curr_x;
off_t kt_curr_y; off_t kt_curr_y;
// XXX flags
#ifndef _NO_DEBUG #ifndef _NO_DEBUG
bool kt_init; bool kt_init;
#endif #endif
} terminal_t;
};
// current "standard" terminal // current "standard" terminal
extern struct kterm *kt_stdout; extern terminal_t *stdout;
void kterm_init(void); void DosInitTerms(void);
status_t kterm_clear(struct kterm *); status_t DosClearTerm(terminal_t *);
status_t kterm_putch(struct kterm *, char); status_t DosPutOnTerm(terminal_t *, char);
status_t kterm_print(struct kterm *, const char *); status_t DosPrintOnTerm(terminal_t *, const char *);
status_t kterm_change_color(struct kterm *, uchar); status_t DosChTermColor(terminal_t *, uchar);
#ifdef _UNLOCKED_IO #ifdef _UNLOCKED_IO
void kterm_putch_unlocked(struct kterm *, char); void DosPutOnTerm_Unlocked(terminal_t *, char);
#endif #endif
#define ktclear() kterm_clear(kt_stdout) #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 ktprint(s) kterm_print(kt_stdout, (s))
#define ktchcol(c) kterm_change_color(kt_stdout, (c)) #define ktchcol(c) kterm_change_color(kt_stdout, (c))
#define kterm_lock(kt) #define DosLockTerm(kt)
#define kterm_trylock(kt) #define DosTryLockTerm(kt)
#define kterm_unlock(kt) #define DosUnlockTerm(kt)
#endif #endif

View File

@ -8,6 +8,8 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "kernel/ke/panic.h" #include "kernel/ke/panic.h"
#include "kernel/ke/state.h"
#include "kernel/io/terminal.h"
// //
// Panic message // 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) noreturn void ___assert_handler(const char *msg, const char *file, int line, const char *func)
{ {
// not getting out of here // not getting out of here
cli(); DosDisableInterrupts();
(void)file; (void)line; (void)func; (void)file; (void)line; (void)func;
// XXX sprintf() to create a proper panicstr // XXX sprintf() to create a proper panicstr
panic(msg); DosPanic(msg);
} }
// //
// Your best boy panic() // 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) { if (str == NULL) {
str = "(no message given)"; str = "(no message given)";
@ -43,17 +47,17 @@ void panic(const char *str)
if (panicstr) { if (panicstr) {
// shouldn't be possible // shouldn't be possible
ktprint("double panic!\n"); DosPrintOnTerm(stdout, "double panic!\n");
hlt(); DosHaltCPU();
} }
panicstr = str; panicstr = str;
ktprint("panic! - "); DosPrintOnTerm(stdout, "panic! - ");
ktprint(str); DosPrintOnTerm(stdout, str);
while (TRUE) { while (TRUE) {
hlt(); DosHaltCPU();
} }
} }

View File

@ -10,9 +10,13 @@
#ifndef _KALKERN_KE_PANIC_H #ifndef _KALKERN_KE_PANIC_H
#define _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H
#include "kernel/io/terminal.h" #include "kernel/ke/state.h"
extern const char *panicstr; extern const char *__panicstr;
noreturn void panic(const char *); noreturn void DosPanic(const char *);
#define DosGetPanicStr() (__panicstr)
#define panic DosPanic
#endif #endif

View File

@ -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;

View File

@ -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