Re-use a lot more code for the userland kernel

This commit is contained in:
Pierre Pronchery 2018-08-03 04:21:00 +02:00
parent 76a58935c3
commit eaab8021e2
18 changed files with 115 additions and 256 deletions

View File

@ -13,7 +13,9 @@
/* public */
/* variables */
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
extern const ukBusInterface tty_bus;
#elif defined(__amd64__) || defined(__i386__)
extern const ukBusInterface cmos_bus;
extern const ukBusInterface ioport_bus;
extern const ukBusInterface vga_bus;
@ -25,7 +27,9 @@ extern const ukBusInterface vga_bus;
ukBus * bus_init(ukBus * parent, char const * name, ...)
{
const ukBusInterface * drivers[] = {
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
&tty_bus
#elif defined(__amd64__) || defined(__i386__)
&cmos_bus,
&ioport_bus,
&vga_bus

View File

@ -18,7 +18,9 @@ static ukClock * _clock = NULL;
/* public */
/* variables */
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
extern const ukClockInterface sys_clock;
#elif defined(__amd64__) || defined(__i386__)
extern const ukClockInterface cmos_clock;
#endif
@ -28,7 +30,9 @@ extern const ukClockInterface cmos_clock;
ukClock * clock_init(ukBus * bus, char const * name, ...)
{
const ukClockInterface * drivers[] = {
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
&sys_clock
#elif defined(__amd64__) || defined(__i386__)
&cmos_clock
#endif
};

View File

@ -39,7 +39,10 @@ static size_t _console_buf_cnt = 0;
/* public */
/* variables */
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
extern const ukConsoleInterface stdio_console;
extern const ukConsoleInterface uart_console;
#elif defined(__amd64__) || defined(__i386__)
extern const ukConsoleInterface uart_console;
#endif
@ -49,8 +52,11 @@ extern const ukConsoleInterface uart_console;
ukConsole * console_init(ukBus * bus, char const * name, ...)
{
const ukConsoleInterface * drivers[] = {
#if defined(__amd64__) || defined(__i386__)
&uart_console,
#if defined(__user__)
&stdio_console,
&uart_console
#elif defined(__amd64__) || defined(__i386__)
&uart_console
#endif
};
va_list ap;

View File

@ -18,7 +18,8 @@ static ukDisplay * _display = NULL;
/* public */
/* variables */
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
#elif defined(__amd64__) || defined(__i386__)
extern const ukDisplayInterface vesa_display;
extern const ukDisplayInterface vga_display;
#endif
@ -29,7 +30,8 @@ extern const ukDisplayInterface vga_display;
ukDisplay * display_init(ukBus * bus, char const * name, ...)
{
const ukDisplayInterface * drivers[] = {
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
#elif defined(__amd64__) || defined(__i386__)
&vesa_display,
&vga_display
#endif

View File

@ -18,7 +18,8 @@ static ukPIC * _pic = NULL;
/* public */
/* variables */
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
#elif defined(__amd64__) || defined(__i386__)
extern const ukPICInterface i8259a_pic;
#endif
@ -28,7 +29,8 @@ extern const ukPICInterface i8259a_pic;
ukPIC * pic_init(ukBus * bus, char const * name, ...)
{
const ukPICInterface * drivers[] = {
#if defined(__amd64__) || defined(__i386__)
#if defined(__user__)
#elif defined(__amd64__) || defined(__i386__)
&i8259a_pic
#endif
};

View File

@ -47,11 +47,16 @@ struct _ukPlatform
/* constants */
static const ukPlatformDevice _devices[] =
{
#if defined(__user__)
{ UKDT_BUS, "tty", NULL },
{ UKDT_CLOCK, "sys", NULL }
#elif defined(__amd64__) || defined(__i386__)
{ UKDT_BUS, "ioport", NULL },
{ UKDT_BUS, "vga", "ioport"},
{ UKDT_BUS, "cmos", "ioport"},
{ UKDT_CLOCK, "cmos", "cmos" },
{ UKDT_PIC, "i8259a", "ioport"}
#endif
};
static char const * _driver_types[UKDT_COUNT] =
@ -253,25 +258,31 @@ int platform_start(ukPlatform * platform)
sizeof(_devices) / sizeof(*_devices));
/* setup the GDT */
#if defined(__amd64__)
#if !defined(__user__)
# if defined(__amd64__)
if(_arch_setgdt64(_gdt_4gb, sizeof(_gdt_4gb) / sizeof(*_gdt_4gb)) != 0)
#else
# elif defined(__i386__)
if(_arch_setgdt(_gdt_4gb, sizeof(_gdt_4gb) / sizeof(*_gdt_4gb)) != 0)
#endif
# endif
{
puts("Could not setup the GDT");
return 2;
}
#endif
/* load the modules */
boot_module_foreach(platform->boot, _start_module_load, platform);
#if !defined(__user__)
/* setup the IDT */
# if defined(__amd64__) || defined(__i386__)
if(_arch_setidt(_idt, sizeof(_idt) / sizeof(*_idt)) != 0)
# endif
{
puts("Could not setup the IDT");
return 2;
}
#endif
return 0;
}

View File

@ -34,6 +34,9 @@ ssize_t read(int fildes, void * buf, size_t count)
errno = EBADF;
return -1;
}
if(_fildes[fildes] == NULL
&& (_fildes[fildes] = console_get_default()) == NULL)
return -1;
if(count == 0)
return 0;
errno = ENOSYS;
@ -70,6 +73,9 @@ ssize_t write(int fildes, const void * buf, size_t count)
errno = EINVAL;
return -1;
}
if(_fildes[fildes] == NULL
&& (_fildes[fildes] = console_get_default()) == NULL)
return -1;
if((size_t)fildes < sizeof(_fildes) / sizeof(*_fildes))
console = _fildes[fildes];
else

5
tools/boot.c Normal file
View File

@ -0,0 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include "kernel/boot.c"

View File

@ -2,53 +2,5 @@
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "drivers/bus.h"
#include "drivers/bus/tty.c"
/* bus_init */
ukBus * bus_init(ukBus * parent, char const * name, ...)
{
const ukBusInterface * drivers[] = {
&tty_bus
};
ukBus * bus;
va_list ap;
size_t i;
if((bus = malloc(sizeof(*bus))) == NULL)
return NULL;
va_start(ap, name);
for(i = 0; i < sizeof(drivers) / sizeof(*drivers); i++)
if(strncmp(drivers[i]->name, name,
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
bus->interface = drivers[i];
bus->driver = drivers[i]->init(parent, ap);
break;
}
va_end(ap);
if(bus == NULL)
{
if(i == sizeof(drivers) / sizeof(*drivers))
errno = ENODEV;
free(bus);
return NULL;
}
return bus;
}
/* accessors */
/* bus_get_name */
char const * bus_get_name(ukBus * bus)
{
return bus->interface->name;
}
#define __user__
#include "drivers/bus.c"

View File

@ -2,80 +2,5 @@
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "drivers/clock.h"
/* private */
/* variables */
static ukClock * _clock = NULL;
/* public */
/* variables */
extern const ukClockInterface sys_clock;
/* functions */
/* clock_init */
ukClock * clock_init(ukBus * bus, char const * name, ...)
{
const ukClockInterface * drivers[] = {
&sys_clock
};
ukClock * clock;
va_list ap;
size_t i;
if((clock = malloc(sizeof(*clock))) == NULL)
return NULL;
va_start(ap, name);
for(i = 0; i < sizeof(drivers) / sizeof(*drivers); i++)
if(strncmp(drivers[i]->name, name,
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
printf("%s%s%s\n", name, (bus != NULL) ? " at " : "",
(bus != NULL) ? bus_get_name(bus) : "");
clock->interface = drivers[i];
clock->driver = drivers[i]->init(bus, ap);
break;
}
va_end(ap);
if(clock == NULL)
{
errno = ENODEV;
return NULL;
}
else if(_clock == NULL)
_clock = clock;
return clock;
}
/* FIXME code duplication with src/drivers/clock.c */
/* accessors */
/* clock_get_default */
ukClock * clock_get_default(void)
{
if(_clock == NULL)
errno = ENODEV;
return _clock;
}
/* clock_get_time */
int clock_get_time(ukClock * clock, time_t * time)
{
if(clock->interface->get_time == NULL)
{
errno = ENOTSUP;
return -1;
}
return clock->interface->get_time(clock, time);
}
#define __user__
#include "drivers/clock.c"

View File

@ -2,100 +2,5 @@
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "drivers/console.h"
/* private */
/* variables */
static ukConsole _console = { NULL, NULL };
static char _console_buf[1024];
static size_t _console_buf_cnt = 0;
/* public */
/* variables */
extern const ukConsoleInterface stdio_console;
/* functions */
/* console_init */
ukConsole * console_init(ukBus * bus, char const * name, ...)
{
const ukConsoleInterface * drivers[] = {
&stdio_console
};
va_list ap;
size_t i;
if(_console.driver != NULL)
return &_console;
va_start(ap, name);
for(i = 0; i < sizeof(drivers) / sizeof(*drivers); i++)
if(strncmp(drivers[i]->name, name,
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
printf("%s%s%s\n", name, (bus != NULL) ? " at " : "",
(bus != NULL) ? bus_get_name(bus) : "");
_console.interface = drivers[i];
_console.driver = drivers[i]->init(bus, ap);
break;
}
va_end(ap);
if(_console.driver == NULL)
{
errno = ENODEV;
return NULL;
}
_console.interface->print(&_console, _console_buf, _console_buf_cnt);
return &_console;
}
/* FIXME code duplication with src/drivers/console.c */
/* accessors */
/* console_get_default */
ukConsole * console_get_default(void)
{
if(_console.driver == NULL)
{
errno = ENODEV;
return NULL;
}
return &_console;
}
/* helpers */
/* console_clear */
void console_clear(ukConsole * console)
{
if(console == NULL
&& (console = console_get_default()) == NULL)
return;
if(console->interface->clear != NULL)
console->interface->clear(console);
}
/* console_print */
void console_print(ukConsole * console, char const * str, size_t len)
{
size_t s;
if(console == NULL && (console = console_get_default()) == NULL)
{
s = sizeof(_console_buf) - _console_buf_cnt;
s = (s > len) ? len : s;
strncpy(&_console_buf[_console_buf_cnt], str, s);
_console_buf_cnt += s;
}
else if(console->interface->print != NULL)
console->interface->print(console, str, len);
}
#define __user__
#include "drivers/console.c"

6
tools/display.c Normal file
View File

@ -0,0 +1,6 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#define __user__
#include "drivers/display.c"

View File

@ -5,22 +5,17 @@
#include <stddef.h>
#include "drivers/bus.h"
#include "drivers/clock.h"
#include "drivers/console.h"
#define TOOLS_BUS "tty"
#define TOOLS_CLOCK "sys"
#define TOOLS_CONSOLE "stdio"
#include <kernel/platform.h>
/* public */
/* functions */
/* init */
int _init(void)
{
ukBus * bus;
bus = bus_init(NULL, TOOLS_BUS);
console_init(bus, TOOLS_CONSOLE);
clock_init(NULL, TOOLS_CLOCK);
boot_init(boot, "user", NULL, 0, 0);
boot_set_console(boot, "tty", "stdio");
platform_init(platform, boot);
platform_start(platform);
return 0;
}

View File

@ -2,4 +2,4 @@
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include "../src/kernel/main.c"
#include "kernel/main.c"

6
tools/multiboot.c Normal file
View File

@ -0,0 +1,6 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#define __user__
#include "drivers/boot/multiboot.c"

6
tools/pic.c Normal file
View File

@ -0,0 +1,6 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#define __user__
#include "drivers/pic.c"

6
tools/platform.c Normal file
View File

@ -0,0 +1,6 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#define __user__
#include "../src/kernel/platform.c"

View File

@ -22,19 +22,37 @@ sources=start.S
[uKernel]
type=binary
sources=bus.c,clock.c,console.c,init.c,main.c
ldflags=$(OBJDIR)crtbegin.o $(OBJDIR)crtend.o $(OBJDIR)start.o $(OBJDIR)../src/lib/libuKernel.a $(OBJDIR)../src/drivers/clock/sys.o $(OBJDIR)../src/drivers/console/stdio.o `$(CC) -print-libgcc-file-name`
depends=$(OBJDIR)crtbegin.o,$(OBJDIR)crtend.o,$(OBJDIR)start.o,$(OBJDIR)../src/lib/libuKernel.a,$(OBJDIR)../src/drivers/clock/sys.o,$(OBJDIR)../src/drivers/console/stdio.o
sources=boot.c,bus.c,clock.c,console.c,display.c,init.c,main.c,multiboot.c,pic.c,platform.c
ldflags=$(OBJDIR)crtbegin.o $(OBJDIR)crtend.o $(OBJDIR)start.o $(OBJDIR)../src/lib/libuKernel.a $(OBJDIR)../src/drivers/bus/tty.o $(OBJDIR)../src/drivers/clock/sys.o $(OBJDIR)../src/drivers/console/stdio.o $(OBJDIR)../src/drivers/console/uart.o `$(CC) -print-libgcc-file-name`
depends=$(OBJDIR)crtbegin.o,$(OBJDIR)crtend.o,$(OBJDIR)start.o,$(OBJDIR)../src/lib/libuKernel.a,$(OBJDIR)../src/drivers/bus/tty.o,$(OBJDIR)../src/drivers/clock/sys.o,$(OBJDIR)../src/drivers/console/stdio.o,$(OBJDIR)../src/drivers/console/uart.o
#sources
[boot.c]
depends=../src/kernel/boot.c
[bus.c]
depends=../src/drivers/bus.c,../src/drivers/bus/tty.c
depends=../src/drivers/bus.h,../src/drivers/bus.c
[clock.c]
depends=../src/drivers/clock.h,../src/drivers/clock.c
[console.c]
depends=../src/drivers/console.h
depends=../src/drivers/console.h,../src/drivers/console.c
[display.c]
depends=../src/drivers/display.h,../src/drivers/display.c
[main.c]
depends=../src/kernel/main.c
[multiboot.c]
depends=../src/drivers/boot/multiboot.c
[pic.c]
depends=../src/drivers/pic.h,../src/drivers/pic.c
[platform.c]
depends=../src/kernel/platform.c
[start.S]
depends=arch/start.S,arch/amd64/start.S,arch/i386/start.S