From dcf2dc2e675cb0d6fda1524a64bf346c47caa187 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Fri, 23 Mar 2018 14:02:31 +0100 Subject: [PATCH] Build a userland version of the kernel for tests --- include/unistd.h | 7 ++++ project.conf | 2 +- tools/arch/i386/start.S | 87 +++++++++++++++++++++++++++++++++++++++++ tools/console.c | 47 ++++++++++++++++++++++ tools/main.c | 1 + tools/project.conf | 18 +++++++++ tools/start.S | 20 ++++++++++ 7 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 tools/arch/i386/start.S create mode 100644 tools/console.c create mode 100644 tools/main.c create mode 100644 tools/project.conf create mode 100644 tools/start.S diff --git a/include/unistd.h b/include/unistd.h index 5dde47f..902648b 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -11,6 +11,13 @@ # include +/* constants */ +/* file streams */ +# define STDIN_FILENO 0 +# define STDOUT_FILENO 1 +# define STDERR_FILENO 2 + + /* prototypes */ void _exit(int status); diff --git a/project.conf b/project.conf index 2aae04a..60cfb1e 100644 --- a/project.conf +++ b/project.conf @@ -1,5 +1,5 @@ package=uKernel version=0.0.0 -subdirs=include,src,tests +subdirs=include,src,tools,tests dist=COPYING,Makefile,README.md diff --git a/tools/arch/i386/start.S b/tools/arch/i386/start.S new file mode 100644 index 0000000..6208fee --- /dev/null +++ b/tools/arch/i386/start.S @@ -0,0 +1,87 @@ +/* $Id$ */ +/* Copyright (c) 2018 Pierre Pronchery */ +/* This file is part of DeforaOS uKernel */ + + +.section .text +/* exit */ +.global _exit +.type _exit, @function +_exit: + mov $0x1, %eax + int $0x80 + ret + + +/* start */ +_start: + /* reset the stack */ + xor %ebp, %ebp + + /* setup the environment */ + mov (%esp), %eax /* argc */ + mov %esp, %ebx /* argv */ + add $0x4, %ebx + mov %eax, %ecx /* envp */ + shl $2, %ecx + add %ebx, %ecx +#if defined(__ELF__) + mov %ecx, %edx /* auxv */ +auxv: + cmpl $0x0, (%edx) + jz auxv_done + add $0x4, %edx + jmp auxv +auxv_done: + add $0x4, %edx +#endif + push %ebp + push %esp + mov %esp, %ebp + push %ecx + push %ebx + push %eax +#if defined(__ELF__) + push %edx +#endif + +#if defined(__SSP__) + /* initialize SSP */ + call __stack_chk_setup +#endif + + /* run the userland kernel */ + call main + + /* exit the userland kernel */ + mov %ebp, %esp + push %eax + call exit + + hlt + + +/* write */ +.global _write +.type _write, @function +_write: + mov $0x4, %eax + int $0x80 + ret + + +#if defined(__NetBSD__) +/* NetBSD emulation */ +# define __NetBSD_Version__ 600000000 + +.section ".note.netbsd.ident", "a" + .p2align 2 + + .long 7 + .long 4 + .long 1 + .ascii "NetBSD\0\0" + .long __NetBSD_Version__ + .previous + .p2align 2 +#endif diff --git a/tools/console.c b/tools/console.c new file mode 100644 index 0000000..1be84fc --- /dev/null +++ b/tools/console.c @@ -0,0 +1,47 @@ +/* $Id$ */ +/* Copyright (c) 2018 Pierre Pronchery */ +/* This file is part of DeforaOS uKernel */ + + + +#include +#include +#include "drivers/console.h" + + +/* types */ +struct _Console +{ + int fd; +}; + + +/* prototypes */ +int _write(int fd, char const * buf, size_t len); + + +/* variables */ +static Console _console; + + +/* functions */ +/* console_init */ +Console * console_init(void) +{ + _console.fd = STDOUT_FILENO; + return &_console; +} + + +/* useful */ +/* console_clear */ +void console_clear(Console * console) +{ +} + + +/* console_print */ +void console_print(Console * console, char const * text, size_t len) +{ + _write(console->fd, text, len); +} diff --git a/tools/main.c b/tools/main.c new file mode 100644 index 0000000..dae73a9 --- /dev/null +++ b/tools/main.c @@ -0,0 +1 @@ +#include "../src/main.c" diff --git a/tools/project.conf b/tools/project.conf new file mode 100644 index 0000000..41aef20 --- /dev/null +++ b/tools/project.conf @@ -0,0 +1,18 @@ +targets=start.o,uKernel +cppflags_force=-nostdinc -isystem ../include -I ../src +cflags_force=-ffreestanding -m32 +cflags=-W -Wall -g -O2 -fstack-protector +as=$(CC) +asflags_force=$(CFLAGSF) $(CFLAGS) -c +ldflags_force=-nostdlib -static -m32 +dist=Makefile + +[start.o] +type=object +sources=start.S + +[uKernel] +type=binary +sources=console.c,main.c +ldflags=$(OBJDIR)start.o $(OBJDIR)../src/lib/libuKernel.a `$(CC) -m32 -print-libgcc-file-name` +depends=$(OBJDIR)start.o,$(OBJDIR)../src/lib/libuKernel.a diff --git a/tools/start.S b/tools/start.S new file mode 100644 index 0000000..4857d4c --- /dev/null +++ b/tools/start.S @@ -0,0 +1,20 @@ +/* $Id$ */ +/* Copyright (c) 2018 Pierre Pronchery */ +/* This file is part of DeforaOS uKernel */ + + + +.text + + +/* _start */ +.global _start +.type _start, @function + + +/* check for supported architectures */ +#if defined(__amd64__) || defined(__i386__) +# include "arch/i386/start.S" +#else +# warning Unsupported architecture +#endif