From 88b31f5f9219b5e11dda5b6fa14795be2e27e962 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Tue, 24 Dec 2019 23:44:04 +0900 Subject: [PATCH] Add simple processes for vga and keyboard handling --- kernel/Makefile | 4 +- kernel/core/main.c | 40 +++++++---------- kernel/kbd/Makefile | 16 +++++++ kernel/kbd/main.c | 7 +++ kernel/vga/Makefile | 16 +++++++ kernel/vga/main.c | 106 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 163 insertions(+), 26 deletions(-) create mode 100644 kernel/kbd/Makefile create mode 100644 kernel/kbd/main.c create mode 100644 kernel/vga/Makefile create mode 100644 kernel/vga/main.c diff --git a/kernel/Makefile b/kernel/Makefile index 318c03d..40b959c 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,5 +1,5 @@ -OBJECTS = arch/arch.o core/core.o io/io.o klibc/klibc.a -DEPS = arch core io klibc +OBJECTS = arch/arch.o core/core.o io/io.o vga/vga.o kbd/kbd.o klibc/klibc.a +DEPS = arch core io vga kbd klibc LDFLAGS = --oformat=elf32-i386 -b elf32-i386 -m elf_i386 CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding PHONY = $(DEPS) clean diff --git a/kernel/core/main.c b/kernel/core/main.c index 45dbb3d..96a016b 100644 --- a/kernel/core/main.c +++ b/kernel/core/main.c @@ -30,8 +30,11 @@ void cpu_debug(void); int arch_init(void*); void sched_tick(void); +extern void io_main(void); extern void _stdio(void); extern void _net(void); +extern int vga_main(int, char**); +extern int kbd_main(int, char**); void _print_mptbl(void *tbl) { @@ -101,6 +104,7 @@ static void _init(void) { pid_t pid; int err; + int i; /* * instead of something like /sbin/init, this process will behave more like the process daemon @@ -108,52 +112,40 @@ static void _init(void) * operation, and restart them in case they should die. */ + pid_t vgapid; + pid = fork(); if(!pid) { /* this is the child */ - err = execfve(_stdio, NULL, NULL); + err = execfve(vga_main, NULL, NULL); if(err < 0) { - /* FIXME: Print an error message */ - for(;;); } } else if(pid < 0) { - /* FIXME: Print an error message */ - for(;;); + /* FIXME: Handle error */ } + vgapid = pid; + pid = fork(); if(!pid) { - /* this is the child */ - err = execfve(_net, NULL, NULL); + err = execfve(kbd_main, NULL, NULL); if(err < 0) { - /* FIXME: Print an error message */ for(;;); } } else if(pid < 0) { - /* FIXME: Print an error message */ - for(;;); + /* FIXME: Handle error */ } -#if 0 - - err = process_create(&stdio, PID_STDIO, 3, (void*)_stdio); - - if(err < 0) { - dbg_printf("Failed to start stdio process [err=0x%08x]\n", err); - } - - err = process_create(&net, PID_NET, 3, (void*)_net); - - if(err < 0) { - dbg_printf("Failed to start NET process [err=0x%08x]\n", err); - } + struct cxmsg msg; + char *hw = "Hello, world\n"; -#endif /* 0 */ + memcpy(msg.cm_data, hw, 13); + cxsendrecv(vgapid, &msg); wait(NULL); diff --git a/kernel/kbd/Makefile b/kernel/kbd/Makefile new file mode 100644 index 0000000..27233ca --- /dev/null +++ b/kernel/kbd/Makefile @@ -0,0 +1,16 @@ +OBJECTS = main.o +OUTPUT = kbd.o +INCLUDES = -I../include -I../../include -I../.. +CFLAGS += $(INCLUDES) +ASFLAGS = $(CFLAGS) +PHONY = clean + +all: $(OUTPUT) + +$(OUTPUT): $(OBJECTS) + ld -r $(LDFLAGS) -o $@ $^ + +clean: + rm -f $(OBJECTS) $(OUTPUT) + +.PHONY: $(PHONY) diff --git a/kernel/kbd/main.c b/kernel/kbd/main.c new file mode 100644 index 0000000..408dedd --- /dev/null +++ b/kernel/kbd/main.c @@ -0,0 +1,7 @@ +int kbd_main(int argc, char *argv[]) +{ + /* FIXME: Handle interrupts, IPC messages */ + for(;;); + + return(0); +} diff --git a/kernel/vga/Makefile b/kernel/vga/Makefile new file mode 100644 index 0000000..55fff93 --- /dev/null +++ b/kernel/vga/Makefile @@ -0,0 +1,16 @@ +OBJECTS = main.o +OUTPUT = vga.o +INCLUDES = -I../include -I../../include -I../.. +CFLAGS += $(INCLUDES) +ASFLAGS = $(CFLAGS) +PHONY = clean + +all: $(OUTPUT) + +$(OUTPUT): $(OBJECTS) + ld -r $(LDFLAGS) -o $@ $^ + +clean: + rm -f $(OBJECTS) $(OUTPUT) + +.PHONY: $(PHONY) diff --git a/kernel/vga/main.c b/kernel/vga/main.c new file mode 100644 index 0000000..9b4d162 --- /dev/null +++ b/kernel/vga/main.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include + +enum vgacolor { + BLACK = 0, + BLUE, + GREEN, + CYAN, + RED, + MAGENTA, + BROWN, + LGRAY, + GRAY, + LBLUE, + LGREEN, + LCYAN, + LRED, + LMAGENTA, + YELLOW, + WHITE +}; + +#define FG(c) ((unsigned short)(((c) & 0xf) << 8)) +#define BG(c) ((unsigned short)(((c) & 0xf) << 12)) + +#define VGA_BUFFER_ADDR ((void*)0xb8000) +#define VGA_BUFFER_COLS 80 +#define VGA_BUFFER_ROWS 24 +#define VGA_ROW_BYTES (VGA_BUFFER_COLS << 1) +#define VGA_SCROLL_SIZE (VGA_BUFFER_COLS * (VGA_BUFFER_ROWS - 1)) +#define VGA_SCROLL_BYTES (VGA_BUFFER_SIZE << 1) +#define VGA_BUFFER_SIZE (VGA_BUFFER_COLS * VGA_BUFFER_ROWS) +#define VGA_BUFFER_BYTES (VGA_BUFFER_SIZE << 1) + +struct context { + unsigned short *buffer; + long offset; + unsigned short attrs; +}; + +static void _putchar(struct context *ctx, int c) +{ + if(c == '\n') { + ctx->offset += VGA_BUFFER_COLS - (ctx->offset % VGA_BUFFER_COLS); + } else { + if(ctx->offset >= VGA_BUFFER_SIZE) { + ctx->offset = VGA_SCROLL_SIZE; + memcpy((void*)ctx->buffer, (void*)ctx->buffer + VGA_ROW_BYTES, VGA_SCROLL_BYTES); + memset((void*)ctx->buffer + VGA_SCROLL_BYTES, 0, VGA_ROW_BYTES); + } + + ctx->buffer[ctx->offset++] = ctx->attrs | (c & 0xff); + } + + return; +} + +static int _puts(struct context *ctx, const char *s) +{ + int i; + + for(i = 0; s[i]; i++) { + _putchar(ctx, s[i]); + } + + return(i); +} + +int vga_main(int argc, char *argv[]) +{ + struct context ctx; + + ctx.buffer = mmap(VGA_BUFFER_ADDR, 0x1000, PROT_READ | PROT_WRITE, MAP_PHYS, 0, 0); + + if(!ctx.buffer) { + /* nothing to do if we can't get access to the VGA buffer */ + return(1); + } + + ctx.offset = VGA_BUFFER_SIZE; + ctx.attrs = FG(LGRAY) | BG(BLACK); + memset(ctx.buffer, 0, VGA_BUFFER_SIZE); + + while(1) { + struct cxmsg msg; + int err; + + err = cxrecv(PID_ANY, &msg); + + /* FIXME: Validate message */ + /* FIXME: Only accept messages from IO process */ + + if(err > 0) { + err = _puts(&ctx, (const char*)msg.cm_data); + } + + msg.cm_retval = err; + cxsend(msg.cm_src, &msg); + } + + munmap(ctx.buffer, 0x1000); + + return(0); +} -- 2.47.3