Compare commits

..

3 Commits

24 changed files with 188 additions and 478 deletions

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
/* Originally from https://wiki.osdev.org/Bare_Bones */
@ -9,36 +9,20 @@
.section .text
/* arch_setgdt */
.global __arch_setgdt
#ifndef __clang__
.type __arch_setgdt, @function
#endif
__arch_setgdt:
#if 0
lea gdt_descriptor, %rcx
#else
mov (gdt_descriptor), %rcx
#endif
/* set the offset of the GDT */
mov %rsi, 0x2(%rcx)
/* set the size of the GDT */
shl $0x3, %rdi
dec %rdi
mov %di, (%rcx)
/* load the GDT */
lgdt (%rcx)
/* apply the GDT */
push $0x8
lea gdt_flush, %rax
push %rax
retf
gdt_flush:
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
ret
@ -47,4 +31,4 @@ gdt_flush:
.align 16
gdt_descriptor:
.skip 2 /* size */
.skip 8 /* offset */
.skip 4 /* offset */

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
@ -9,4 +9,9 @@
# include "../i386/gdt.h"
#endif /* !UKERNEL_ARCH_AMD64_GDT_H */
/* public */
/* prototypes */
int _arch_setgdt64(GDT const * gdt, size_t count);
#endif /* !UKERNEL_ARCH_I386_GDT_H */

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2020 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
/* Originally from https://wiki.osdev.org/Bare_Bones */
@ -13,31 +13,20 @@
.type __arch_setgdt, @function
#endif
__arch_setgdt:
#if 1
lea gdt_descriptor, %ecx
#else
mov (gdt_descriptor), %ecx
#endif
/* set the offset of the GDT */
mov 0x4(%esp), %eax
mov %eax, 0x2(%ecx)
/* set the size of the GDT */
mov 0x8(%esp), %eax
shl $0x3, %eax
dec %eax
mov %ax, (%ecx)
/* load the GDT */
lgdt (gdt_descriptor)
/* apply the GDT */
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
ljmp $0x8, $gdt_flush
gdt_flush:
lgdt (%ecx)
ret

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
@ -13,79 +13,15 @@
/* public */
/* types */
typedef struct _GDT GDT;
typedef struct _GDTTable
typedef struct _GDT
{
vaddr_t base;
size_t size;
unsigned int prot;
} GDTTable;
#pragma pack(1)
typedef struct _TSS
{
uint16_t link;
uint16_t __padding0;
uint32_t esp0;
uint16_t ss0;
uint16_t __padding1;
uint32_t esp1;
uint16_t ss1;
uint16_t __padding2;
uint32_t esp2;
uint16_t ss2;
uint16_t __padding3;
uint32_t cr3;
uint32_t eip;
uint32_t eflags;
uint32_t eax;
uint32_t ecx;
uint32_t edx;
uint32_t ebx;
uint32_t esp;
uint32_t ebp;
uint32_t esi;
uint32_t edi;
uint16_t es;
uint16_t __padding4;
uint16_t cs;
uint16_t __padding5;
uint16_t ss;
uint16_t __padding6;
uint16_t ds;
uint16_t __padding7;
uint16_t fs;
uint16_t __padding8;
uint16_t gs;
uint16_t __padding9;
uint16_t ldtr;
uint16_t __padding10;
uint16_t __padding11;
uint16_t iopb;
uint32_t ssp;
} TSS;
#pragma pack()
/* constants */
# define GDT_PROT_READ 0x1
# define GDT_PROT_WRITE 0x2
# define GDT_PROT_EXEC 0x4
# define GDT_SYSTEM_TYPE_LDT 0x2
# define GDT_SYSTEM_TYPE_TSS 0x9
vaddr_t limit;
uint8_t type;
} GDT;
/* prototypes */
GDT * gdt_init(void);
int gdt_init_table(GDTTable const * table, size_t table_cnt, size_t tss_cnt);
/* useful */
int gdt_append(GDT * gdt, vaddr_t base, size_t size, unsigned int prot);
int gdt_append_system(GDT * gdt, void * base, size_t size, unsigned int type);
void gdt_apply(GDT * gdt);
int _arch_setgdt(GDT const * gdt, size_t count);
#endif /* !UKERNEL_ARCH_I386_GDT_H */

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2020 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
/* Originally from https://wiki.osdev.org/Bare_Bones */
@ -129,9 +129,6 @@ _start:
push $0x0
2:
/* enable interrupts */
sti
/* start the kernel */
call main
mov %ebp, %esp

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2020 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
/* Originally from https://wiki.osdev.org/Bare_Bones */
@ -125,9 +125,6 @@ _start:
push $0x0
2:
/* enable interrupts */
sti
/* start the loader */
call main
mov %ebp, %esp

View File

@ -11,6 +11,8 @@
# include <stdio.h>
# include <string.h>
# include <elf.h>
# include "arch/amd64/gdt.h"
# include "arch/i386/gdt.h"
# include "drivers/boot/multiboot.h"
# ifndef MAX

View File

@ -4,8 +4,8 @@
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <kernel/drivers/bus.h>
@ -37,7 +37,7 @@ ukBus * bus_init(ukBus * parent, char const * name)
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
fprintf(stderr, "%s bus%s%s%s\n", name,
syslog(LOG_KERN | LOG_NOTICE, "%s bus%s%s%s", name,
(parent != NULL) ? " at " : "",
(parent != NULL) ? parent->name : "",
(parent != NULL) ? " bus" : "");

View File

@ -1,11 +1,11 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <kernel/drivers/clock.h>
@ -39,7 +39,7 @@ ukClock * clock_init(ukBus * bus, char const * name)
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
fprintf(stderr, "%s clock%s%s%s\n", name,
syslog(LOG_KERN | LOG_NOTICE, "%s clock%s%s%s", name,
(bus != NULL) ? " at " : "",
(bus != NULL) ? bus->name : "",
(bus != NULL) ? " bus" : "");

View File

@ -1,10 +1,11 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <kernel/drivers/clock.h>
#include "cmos.h"

View File

@ -1,11 +1,11 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <kernel/drivers/console.h>
@ -43,7 +43,7 @@ ukConsole * console_init(ukBus * bus, char const * name)
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
fprintf(stderr, "%s console%s%s%s\n", name,
syslog(LOG_KERN | LOG_NOTICE, "%s console%s%s%s", name,
(bus != NULL) ? " at " : "",
(bus != NULL) ? bus->name : "",
(bus != NULL) ? " bus" : "");

View File

@ -1,11 +1,11 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <kernel/drivers/display.h>
@ -41,7 +41,7 @@ ukDisplay * display_init(ukBus * bus, char const * name)
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
fprintf(stderr, "%s display%s%s%s\n", name,
syslog(LOG_KERN | LOG_NOTICE, "%s display%s%s%s", name,
(bus != NULL) ? " at " : "",
(bus != NULL) ? bus->name : "",
(bus != NULL) ? " bus" : "");

View File

@ -1,11 +1,11 @@
/* $Id$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <kernel/drivers/pic.h>
@ -39,7 +39,7 @@ ukPIC * pic_init(ukBus * bus, char const * name)
strlen(drivers[i]->name)) == 0
&& drivers[i]->init != NULL)
{
fprintf(stderr, "%s pic%s%s%s\n", name,
syslog(LOG_KERN | LOG_NOTICE, "%s pic%s%s%s", name,
(bus != NULL) ? " at " : "",
(bus != NULL) ? bus->name : "",
(bus != NULL) ? " bus" : "");

View File

@ -5,7 +5,7 @@
#if defined(__amd64__) || defined(__i386__)
# include <stdio.h>
# include <syslog.h>
# include <kernel/drivers/bus.h>
# include <kernel/drivers/clock.h>
# include <kernel/drivers/console.h>
@ -27,10 +27,12 @@
/* private */
/* constants */
/* GDT: 4GB flat memory setup */
static const GDTTable _gdt_4gb[2] =
static const GDT _gdt_4gb[4] =
{
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_EXEC },
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_WRITE }
{ 0x00000000, 0x00000000, 0x00 },
{ 0x00000000, 0xffffffff, 0x9a },
{ 0x00000000, 0xffffffff, 0x92 },
{ 0x00000000, 0x00000000, 0x89 }
};
static const IDT _idt[] =
@ -54,6 +56,9 @@ int multiboot(const ukMultibootInfo * mi)
/* initialize the heap */
multiboot_heap_reset(mi);
/* initialize logging */
openlog("uKernel", LOG_ODELAY, LOG_KERN);
/* initialize the buses */
ioportbus = bus_init(NULL, "ioport");
vgabus = bus_init(ioportbus, "vga");
@ -78,33 +83,32 @@ int multiboot(const ukMultibootInfo * mi)
clock_init(cmosbus, "cmos");
/* report information on the boot process */
puts("DeforaOS Multiboot");
syslog(LOG_KERN | LOG_NOTICE, "%s", "DeforaOS Multiboot");
if(mi->loader_name != NULL)
printf("Loader: %s\n", mi->loader_name);
syslog(LOG_KERN | LOG_INFO, "Loader: %s", mi->loader_name);
if(mi->cmdline != NULL)
printf("Command line: %s\n", mi->cmdline);
printf("%u MB memory available\n",
syslog(LOG_KERN | LOG_INFO, "Command line: %s", mi->cmdline);
syslog(LOG_KERN | LOG_INFO, "%u MB memory available",
(mi->mem_upper - mi->mem_lower) / 1024);
printf("Booted from %#x\n", mi->boot_device_drive);
syslog(LOG_KERN | LOG_INFO, "Booted from %#x", mi->boot_device_drive);
/* setup the GDT */
#if defined(__amd64__)
if(_arch_setgdt64(_gdt_4gb, sizeof(_gdt_4gb) / sizeof(*_gdt_4gb)) != 0)
#else
if(gdt_init_table((const GDTTable *)&_gdt_4gb,
sizeof(_gdt_4gb) / sizeof(*_gdt_4gb), 1) != 0)
if(_arch_setgdt(_gdt_4gb, sizeof(_gdt_4gb) / sizeof(*_gdt_4gb)) != 0)
#endif
{
puts("Could not setup the GDT");
syslog(LOG_KERN | LOG_EMERG, "%s", "Could not setup the GDT");
return 4;
}
/* load the modules */
if(!(mi->flags & BOOT_MULTIBOOT_INFO_HAS_MODS))
puts("No modules provided");
syslog(LOG_KERN | LOG_INFO, "%s", "No modules provided");
else
{
puts("Loading modules...");
syslog(LOG_KERN | LOG_INFO, "%s", "Loading modules...");
for(i = 0; i < mi->mods_count; i++)
{
mod = &mi->mods_addr[i];
@ -115,7 +119,7 @@ int multiboot(const ukMultibootInfo * mi)
/* setup the IDT */
if(_arch_setidt(_idt, sizeof(_idt) / sizeof(*_idt)) != 0)
{
puts("Could not setup the IDT");
syslog(LOG_KERN | LOG_EMERG, "%s", "Could not setup the IDT");
return 5;
}

@ -1 +1 @@
Subproject commit 1ff21ed155f9fc8773b415fb828a82cccdea0a98
Subproject commit 505cd30fd7bed917c46cb38a0551a1e0847cb0f5

View File

@ -13,4 +13,4 @@ dist=Makefile,libc/src/chacha/chacha.c,libc/src/chacha/ecrypt-config.h,libc/src/
#targets
[libk]
type=library
sources=libc/src/ctype.c,libc/src/dirent.c,libc/src/errno.c,libc/src/fcntl.c,libc/src/pwd.c,libc/src/signal.c,libc/src/ssp/ssp.c,libc/src/ssp.c,libc/src/stdio.c,libc/src/stdlib.c,libc/src/string.c,libc/src/syscalls.S,libc/src/sys/ioctl.c,libc/src/sys/mman.c,libc/src/sys/stat.c,libc/src/sys/sysctl.c,libc/src/sys/time.c,libc/src/sys/wait.c,libc/src/termios.c,libc/src/time.c,libc/src/unistd.c,sys/mman.c,sys/time.c,unistd.c
sources=libc/src/ctype.c,libc/src/dirent.c,libc/src/errno.c,libc/src/fcntl.c,libc/src/pwd.c,libc/src/signal.c,libc/src/ssp/ssp.c,libc/src/ssp.c,libc/src/stdio.c,libc/src/stdlib.c,libc/src/string.c,libc/src/syscalls.S,libc/src/sys/ioctl.c,libc/src/sys/mman.c,libc/src/sys/stat.c,libc/src/sys/sysctl.c,libc/src/syslog.c,libc/src/sys/time.c,libc/src/sys/wait.c,libc/src/termios.c,libc/src/time.c,libc/src/unistd.c,sys/mman.c,sys/time.c,unistd.c

View File

@ -1,11 +1,10 @@
/* $Id$ */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#if defined(__amd64__) || defined(__i386__)
# include <limits.h>
# include <stdint.h>
# include <string.h>
# include <errno.h>
@ -13,239 +12,101 @@
# include "arch/i386/gdt.h"
/* constants */
# define GDT_ENTRIES_MAX 8192
# define GDT_LIMIT_MAX 0x000fffff
/* access */
# define GDT_ACCESS_SET 0x01
# define GDT_ACCESS_PROT_RW 0x02
# define GDT_ACCESS_PROT_X 0x08
# define GDT_ACCESS_SEGMENT 0x10
# define GDT_ACCESS_RING(level) ((level) << 5)
# define GDT_ACCESS_PRESENT 0x80
/* flags */
# define GDT_FLAG_LONG_MODE 0x2 /* 64-bit code segment */
# define GDT_FLAG_PROTECTED_MODE 0x4 /* 32-bit protected mode
code segment */
# define GDT_FLAG_PAGE_GRANULARITY 0x8 /* 4 KB page size */
/* types */
#pragma pack(1)
typedef struct _GDTEntry
{
uint8_t limit0;
uint8_t limit1;
uint8_t base0;
uint8_t base1;
uint8_t base2;
uint8_t access;
unsigned int limit2:4;
unsigned int flags:4;
uint8_t base3;
} GDTEntry;
#pragma pack()
#pragma pack(1)
typedef struct _LDT
{
uint8_t limit0;
uint8_t limit1;
uint8_t base0;
uint8_t base1;
uint8_t base2;
uint8_t access;
unsigned int limit2:4;
unsigned int flags:4;
uint8_t base3;
} LDT;
#pragma pack()
struct _GDT
{
GDTEntry entries[GDT_ENTRIES_MAX];
size_t entries_cnt;
};
/* prototypes */
extern void __arch_setgdt(GDTEntry const * entries, size_t count);
static int _gdt_append_entry(GDT * gdt, vaddr_t base, uint32_t limit,
uint8_t access, uint8_t flags);
extern void __arch_setgdt(uint8_t * buf, size_t count);
/* variables */
static GDT _gdt;
static TSS _tss;
static uint8_t _buf[65536];
/* functions */
/* gdt_init */
GDT * gdt_init(void)
/* arch_setgdt */
int _arch_setgdt(GDT const * gdt, size_t count)
{
memset(&_gdt.entries[0], 0, sizeof(_gdt.entries[0]));
_gdt.entries_cnt = 1;
return &_gdt;
}
/* gdt_init_table */
int gdt_init_table(GDTTable const * table, size_t table_cnt, size_t tss_cnt)
{
int ret = 0;
GDT * gdt;
uint8_t * buf = _buf;
size_t i;
GDT g;
gdt = gdt_init();
for(i = 0; i < table_cnt; i++)
if((ret = gdt_append(gdt, table[i].base, table[i].size,
table[i].prot)) != 0)
return ret;
/* set the TSS */
if(tss_cnt > 1)
memset(&_buf, 0, sizeof(_buf));
/* check for errors */
if(count == 0 || count > sizeof(_buf) / sizeof(*gdt))
{
errno = ENOSYS;
errno = ERANGE;
return -1;
}
else if(tss_cnt == 1 && (ret = gdt_append_system(gdt, &_tss,
sizeof(_tss), GDT_SYSTEM_TYPE_TSS))
!= 0)
return ret;
gdt_apply(gdt);
for(i = 0; i < count; i++)
{
g = gdt[i];
buf = &_buf[sizeof(g) * i];
if(g.limit > 65536)
{
/* make sure the limit can be encoded */
if((g.limit & 0xfff) != 0xfff)
return -1;
g.limit = g.limit >> 12;
buf[6] = 0xc0;
}
else
buf[6] = 0x40;
//encode the limit
buf[0] = g.limit & 0xff;
buf[1] = (g.limit >> 8) & 0xff;
buf[6] |= (g.limit >> 16) & 0xf;
//encode the base
buf[2] = g.base & 0xff;
buf[3] = (g.base >> 8) & 0xff;
buf[4] = (g.base >> 16) & 0xff;
buf[7] = (g.base >> 24) & 0xff;
//encode the type
buf[5] = g.type;
}
__arch_setgdt(_buf, count);
return 0;
}
/* useful */
/* gdt_append */
int gdt_append(GDT * gdt, vaddr_t base, size_t size, unsigned int prot)
/* arch_setgdt64 */
int _arch_setgdt64(GDT const * gdt, size_t count)
{
uint32_t limit;
uint8_t access = GDT_ACCESS_SEGMENT | GDT_ACCESS_PRESENT
| GDT_ACCESS_RING(0);
uint8_t flags = GDT_FLAG_PROTECTED_MODE;
uint8_t * buf;
size_t i;
GDT g;
memset(&_buf, 0, sizeof(_buf));
/* check for errors */
if(size == 0)
if(count == 0 || count > sizeof(_buf) / sizeof(*gdt))
{
errno = ERANGE;
return -1;
}
if(size > ULONG_MAX || ULONG_MAX - size < base)
for(i = 0; i < count; i++)
{
errno = ENOMEM;
return -1;
g = gdt[i];
buf = &_buf[sizeof(g) * i];
if(g.limit > 65536)
{
/* make sure the limit can be encoded */
if((g.limit & 0xfff) != 0xfff)
return -1;
g.limit = g.limit >> 12;
buf[6] = 0xa0;
}
else
buf[6] = 0x20;
//encode the limit
buf[0] = g.limit & 0xff;
buf[1] = (g.limit >> 8) & 0xff;
buf[6] |= (g.limit >> 16) & 0xf;
//encode the base
buf[2] = g.base & 0xff;
buf[3] = (g.base >> 8) & 0xff;
buf[4] = (g.base >> 16) & 0xff;
buf[7] = (g.base >> 24) & 0xff;
//encode the type
buf[5] = g.type;
}
if(prot != GDT_PROT_READ
&& prot != (GDT_PROT_READ | GDT_PROT_WRITE)
&& prot != (GDT_PROT_READ | GDT_PROT_EXEC))
{
errno = EPERM;
return -1;
}
/* limit */
if(size - 1 > GDT_LIMIT_MAX)
{
limit = (size & 0xfff) == 0
? (size >> 12) - 1
: (((size | 0xfff) + 1) >> 12) - 1;
flags |= GDT_FLAG_PAGE_GRANULARITY;
}
else
limit = size - 1;
/* access */
if(prot == (GDT_PROT_READ | GDT_PROT_EXEC))
/* code segment */
access |= GDT_ACCESS_PROT_RW | GDT_ACCESS_PROT_X;
else if(prot == (GDT_PROT_READ | GDT_PROT_WRITE))
/* data segment (read/write) */
access |= GDT_ACCESS_PROT_RW;
else if(prot == GDT_PROT_READ)
/* data segment (read-only) */
access |= GDT_ACCESS_SET;
return _gdt_append_entry(gdt, base, limit, access, flags);
}
/* gdt_append_system */
static int _append_system_ldt(GDT * gdt, void * base, size_t size);
static int _append_system_tss(GDT * gdt, void * base, size_t size);
int gdt_append_system(GDT * gdt, void * base, size_t size, unsigned int type)
{
switch(type)
{
case GDT_SYSTEM_TYPE_LDT:
return _append_system_ldt(gdt, base, size);
case GDT_SYSTEM_TYPE_TSS:
return _append_system_tss(gdt, base, size);
default:
errno = ENOSYS;
return -1;
}
}
static int _append_system_ldt(GDT * gdt, void * base, size_t size)
{
uint32_t access = GDT_ACCESS_PRESENT | GDT_SYSTEM_TYPE_LDT;
uint8_t flags = GDT_FLAG_PROTECTED_MODE;
if(size != sizeof(LDT))
{
errno = ERANGE;
return -1;
}
return _gdt_append_entry(gdt, (vaddr_t)base, size - 1, access, flags);
}
static int _append_system_tss(GDT * gdt, void * base, size_t size)
{
uint32_t access = GDT_ACCESS_PRESENT | GDT_SYSTEM_TYPE_TSS;
uint8_t flags = GDT_FLAG_PROTECTED_MODE;
if(size != sizeof(TSS))
{
errno = ERANGE;
return -1;
}
return _gdt_append_entry(gdt, (vaddr_t)base, size - 1, access, flags);
}
/* gdt_apply */
void gdt_apply(GDT * gdt)
{
__arch_setgdt(gdt->entries, gdt->entries_cnt);
}
/* private */
/* gdt_append_entry */
static int _gdt_append_entry(GDT * gdt, vaddr_t base, uint32_t limit,
uint8_t access, uint8_t flags)
{
GDTEntry * entry;
/* check for errors */
if(gdt->entries_cnt >= sizeof(gdt->entries) / sizeof(*gdt->entries))
{
errno = ENOMEM;
return -1;
}
entry = &gdt->entries[gdt->entries_cnt];
entry->base0 = base & 0xff;
entry->base1 = (base & 0xff00) >> 8;
entry->base2 = (base & 0xff0000) >> 16;
entry->base3 = (base & 0xff000000) >> 24;
entry->limit0 = limit & 0xff;
entry->limit1 = (limit & 0xff00) >> 8;
entry->limit2 = (limit & 0xf0000) >> 16;
entry->access = access;
entry->flags = flags;
gdt->entries_cnt++;
__arch_setgdt(_buf, count);
return 0;
}
#endif

View File

@ -1,5 +1,5 @@
/* $Id$ */
/* Copyright (c) 2018-2019 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
@ -26,6 +26,7 @@
#include "lib/libc/src/sys/ioctl.c"
#include "lib/libc/src/sys/stat.c"
#include "lib/libc/src/sys/sysctl.c"
#include "lib/libc/src/syslog.c"
#include "lib/libc/src/sys/time.c"
#include "lib/libc/src/sys/wait.c"
#include "lib/libc/src/termios.c"

View File

@ -1,10 +1,13 @@
/* $Id$ */
/* Copyright (c) 2018-2019 Pierre Pronchery <khorben@defora.org> */
/* Copyright (c) 2018-2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/* public */
@ -13,15 +16,23 @@
int main(int argc, char * argv[])
{
int i;
char buf[1024];
size_t size = sizeof(buf) - 1;
puts("Failed to boot DeforaOS");
printf("Command line:");
syslog(LOG_KERN | LOG_EMERG, "Failed to boot DeforaOS");
for(i = 0; i < argc; i++)
printf(" \"%s\"", argv[i]);
printf("\n");
{
strncat(buf, " ", 1);
size -= MIN(size, 1);
strncat(buf, argv[i], size);
size -= MIN(size, strlen(argv[i]));
}
buf[sizeof(buf) - 1] = '\0';
syslog(LOG_KERN | LOG_INFO, "Command line: %s", buf);
#ifdef DEBUG
if(argv[i] != NULL)
puts("uLoader: argv is not terminated properly");
syslog(LOG_KERN | LOG_DEBUG, "%s",
"uLoader: argv is not terminated properly");
#endif
return 0;
}

View File

@ -5,8 +5,8 @@
#if defined(__i386__)
# include <stdio.h>
# include <string.h>
# include <syslog.h>
# include <kernel/drivers/bus.h>
# include <kernel/drivers/console.h>
# include <kernel/drivers/clock.h>
@ -29,10 +29,12 @@
/* private */
/* constants */
/* GDT: 4GB flat memory setup */
static const GDTTable _gdt_4gb[2] =
static const GDT _gdt_4gb[4] =
{
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_EXEC },
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_WRITE }
{ 0x00000000, 0x00000000, 0x00 },
{ 0x00000000, 0xffffffff, 0x9a },
{ 0x00000000, 0xffffffff, 0x92 },
{ 0x00000000, 0x00000000, 0x89 }
};
@ -55,6 +57,9 @@ int multiboot(const ukMultibootInfo * mi)
/* initialize the heap */
multiboot_heap_reset(mi);
/* initialize logging */
openlog("uLoader", LOG_ODELAY, LOG_KERN);
/* initialize the buses */
ioportbus = bus_init(NULL, "ioport");
vgabus = bus_init(ioportbus, "vga");
@ -76,39 +81,38 @@ int multiboot(const ukMultibootInfo * mi)
clock_init(cmosbus, clock);
/* report information on the boot process */
puts("DeforaOS Multiboot");
syslog(LOG_KERN | LOG_NOTICE, "%s", "DeforaOS Multiboot");
if(mi->loader_name != NULL)
printf("Loader: %s\n", mi->loader_name);
syslog(LOG_KERN | LOG_INFO, "Loader: %s", mi->loader_name);
if(mi->cmdline != NULL)
printf("Command line: %s\n", mi->cmdline);
printf("%u MB memory available\n",
syslog(LOG_KERN | LOG_INFO, "Command line: %s", mi->cmdline);
syslog(LOG_KERN | LOG_INFO, "%u MB memory available",
(mi->mem_upper - mi->mem_lower) / 1024);
printf("Booted from %#x\n", mi->boot_device_drive);
syslog(LOG_KERN | LOG_INFO, "Booted from %#x", mi->boot_device_drive);
/* setup the GDT */
if(gdt_init_table((const GDTTable *)&_gdt_4gb,
sizeof(_gdt_4gb) / sizeof(*_gdt_4gb), 1) != 0)
if(_arch_setgdt(_gdt_4gb, sizeof(_gdt_4gb) / sizeof(*_gdt_4gb)) != 0)
{
puts("Could not setup the GDT");
syslog(LOG_KERN | LOG_EMERG, "%s", "Could not setup the GDT");
return 4;
}
/* load the kernel */
if(!(mi->flags & BOOT_MULTIBOOT_INFO_HAS_MODS))
{
puts("No modules provided");
syslog(LOG_KERN | LOG_NOTICE, "%s", "No modules provided");
return 6;
}
if(mi->mods_count == 0)
{
puts("No kernel provided");
syslog(LOG_KERN | LOG_ERR, "%s", "No kernel provided");
return 7;
}
mod = &mi->mods_addr[0];
printf("Loading kernel: %s\n", mod->cmdline);
syslog(LOG_KERN | LOG_NOTICE, "Loading kernel: %s", mod->cmdline);
if(multiboot_load_module(mod, &elfclass, &entrypoint) != 0)
{
puts("Could not load the kernel");
syslog(LOG_KERN | LOG_EMERG, "%s", "Could not load the kernel");
return 8;
}
@ -121,20 +125,25 @@ int multiboot(const ukMultibootInfo * mi)
/* hand control over to the kernel */
#ifdef DEBUG
printf("Jumping into the kernel at %#x (%u, %u, %#x)\n", entrypoint,
mi->elfshdr_num, mi->elfshdr_size, mi->elfshdr_addr);
syslog(LOG_KERN | LOG_DEBUG,
"Jumping into the kernel at %#x (%u, %u, %#x)",
entrypoint, mi->elfshdr_num, mi->elfshdr_size,
mi->elfshdr_addr);
#endif
switch(elfclass)
{
case ELFCLASS32:
puts("Detected 32-bit kernel");
syslog(LOG_KERN | LOG_INFO, "%s",
"Detected 32-bit kernel");
return multiboot_boot_kernel32(&kmi, entrypoint);
case ELFCLASS64:
puts("Detected 64-bit kernel");
syslog(LOG_KERN | LOG_INFO, "%s",
"Detected 64-bit kernel");
return multiboot_boot_kernel64(&kmi, entrypoint);
}
puts("Unsupported ELF class for the kernel");
syslog(LOG_KERN | LOG_EMERG, "%s",
"Unsupported ELF class for the kernel");
return 7;
}
#endif

View File

@ -85,7 +85,7 @@ depends=$(OBJDIR)../lib/libk.a
depends=../drivers/boot/multiboot.h,../../include/kernel/drivers/bus.h,../../include/kernel/drivers/console.h
[start.S]
depends=../arch/i386/gdt.S,../arch/i386/intr.S,../arch/i386/loader.S,../arch/i386/multiboot.S
depends=../arch/i386/intr.S,../arch/i386/loader.S,../arch/i386/multiboot.S
[syscalls.S]
depends=$(OBJDIR)../lib/libc/src/syscalls.o

1
tests/.gitignore vendored
View File

@ -1,5 +1,4 @@
/clint.log
/distcheck.log
/fixme.log
/gdt
/grub.log

View File

@ -1,76 +0,0 @@
/* $Id$ */
/* Copyright (c) 2025 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
#include <stdio.h>
#ifndef vaddr_t
# define vaddr_t unsigned long
#endif
#include "../src/loader/gdt.c"
#if defined(__amd64__) || defined(__i386__)
/* variables */
static TSS tss;
/* functions */
/* __arch_setgdt */
void __arch_setgdt(GDTEntry const * entries, size_t count)
{
size_t i;
GDTEntry const * entry;
printf("sizeof(*entries) => %zu\n", sizeof(*entries));
for(i = 0; i < count; i++)
{
entry = &entries[i];
printf("base=0x%02x%02x%02x%02x ",
entry->base3, entry->base2,
entry->base1, entry->base0);
printf("limit=0x%x%02x%02x ",
entry->limit2, entry->limit1, entry->limit0);
printf("access=0x%02x ", entry->access);
printf("flags=0x%0x\n", entry->flags);
}
printf("count=0x%zx => size=0x%zx\n", count, (count << 3) - 1);
}
#endif
/* main */
int main(int argc, char * argv[])
{
#if defined(__amd64__) || defined(__i386__)
GDT * gdt;
(void) argc;
(void) argv;
printf("sizeof(GDTEntry) = %zu (expected: %u)\n\n",
sizeof(GDTEntry), 8);
/* flat 4 GB */
printf("Flat 4 GB:\n");
gdt = gdt_init();
gdt_append(gdt, 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_EXEC);
gdt_append(gdt, 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_WRITE);
gdt_append_system(gdt, &tss, sizeof(tss), GDT_SYSTEM_TYPE_TSS);
gdt_apply(gdt);
/* 4 MB code + 4 MB data (read-write) + 4 MB data (read-only) */
printf("\n4 MB (rx) + 4 MB (rw) + 4 MB (ro):\n");
gdt = gdt_init();
gdt_append(gdt, 0x00400000, 0x00400000, GDT_PROT_READ | GDT_PROT_EXEC);
gdt_append(gdt, 0x00800000, 0x00400000, GDT_PROT_READ | GDT_PROT_WRITE);
gdt_append(gdt, 0x00c00000, 0x00400000, GDT_PROT_READ);
gdt_append_system(gdt, &tss, sizeof(tss), GDT_SYSTEM_TYPE_TSS);
gdt_apply(gdt);
#endif
(void) argc;
(void) argv;
return 0;
}

View File

@ -1,6 +1,4 @@
targets=clint.log,distcheck.log,fixme.log,gdt,grub.log
cppflags_force=-I../src
cflags_force=-W -Wall -g -O2
targets=clint.log,distcheck.log,fixme.log,grub.log
dist=Makefile,clint.sh,distcheck.sh,fixme.sh,grub.sh
#targets
@ -22,16 +20,8 @@ script=./fixme.sh
depends=fixme.sh
enabled=0
[gdt]
type=binary
sources=gdt.c
[grub.log]
type=script
script=./grub.sh
depends=$(OBJDIR)../src/kernel/uKernel.bin,grub.sh
enabled=0
#sources
[gdt.c]
depends=../src/loader/gdt.c,../src/arch/amd64/gdt.h,../src/arch/i386/gdt.h