Initial commit. We have a booting kernel

This commit is contained in:
Bananymous
2022-11-12 21:04:47 +02:00
commit e6b4866ab0
36 changed files with 691 additions and 0 deletions

3
kernel/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.d
*.kernel
*.o

83
kernel/Makefile Normal file
View File

@@ -0,0 +1,83 @@
DEFAULT_HOST!=../default-host.sh
HOST?=DEFAULT_HOST
HOSTARCH!=../target-triplet-to-arch.sh $(HOST)
CFLAGS?=-O2 -g
CPPFLAGS?=
LDFLAGS?=
LIBS?=
DESTDIR?=
PREFIX?=/usr/local
EXEC_PREFIX?=$(PREFIX)
BOOTDIR?=$(EXEC_PREFIX)/boot
INCLUDEDIR?=$(PREFIX)/include
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra
CPPFLAGS:=$(CPPFLAGS) -D__is_kernel -Iinclude
LDFLAGS:=$(LDFLAGS)
LIBS:=$(LIBS) -nostdlib -lk -lgcc
ARCHDIR=arch/$(HOSTARCH)
include $(ARCHDIR)/make.config
CFLAGS:=$(CFLAGS) $(KERNEL_ARCH_CFLAGS)
CPPFLAGS:=$(CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
LDFLAGS:=$(LDFLAGS) $(KERNEL_ARCH_LDFLAGS)
LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS)
KERNEL_OBJS=\
$(KERNEL_ARCH_OBJS) \
kernel/kernel.o \
OBJS=\
$(ARCHDIR)/crti.o \
$(ARCHDIR)/crtbegin.o \
$(KERNEL_OBJS) \
$(ARCHDIR)/crtend.o \
$(ARCHDIR)/crtn.o \
LINK_LIST=\
$(LDFLAGS) \
$(ARCHDIR)/crti.o \
$(ARCHDIR)/crtbegin.o \
$(KERNEL_OBJS) \
$(LIBS) \
$(ARCHDIR)/crtend.o \
$(ARCHDIR)/crtn.o \
.PHONY: all clean install install-headers install-kernel
.SUFFIXES: .o .c .cpp .S
all: banan-os.kernel
banan-os.kernel: $(OBJS) $(ARCHDIR)/linker.ld
$(CC) -T $(ARCHDIR)/linker.ld -o $@ $(CFLAGS) $(LINK_LIST)
grub-file --is-x86-multiboot banan-os.kernel
$(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
OBJ=`$(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@
.c.o:
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
.S.o:
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
clean:
rm -f banan-os.kernel
rm -f $(OBJS) *.o */*.o */*/*.o
rm -f $(OBJS:.o=.d) *.d */*.d */*/*.d
install: install-headers install-kernel
install-headers:
mkdir -p $(DESTDIR)$(INCLUDEDIR)
cp -R --preserve=timestamps include/. $(DESTDIR)$(INCLUDEDIR)/.
install-kernel: banan-os.kernel
mkdir -p $(DESTDIR)$(BOOTDIR)
cp banan-os.kernel $(DESTDIR)$(BOOTDIR)
-include $(OBJS:.o=.d)

38
kernel/arch/i386/boot.s Normal file
View File

@@ -0,0 +1,38 @@
/* Declare constants for the multiboot header. */
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
.set MEMINFO, 1<<1 /* provide memory map */
.set MB_FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */
.set MB_MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
.set MB_CHECKSUM, -(MB_MAGIC + MB_FLAGS) /* checksum of above, to prove we are multiboot */
/* Multiboot header */
.section .multiboot
.align 4
.long MB_MAGIC
.long MB_FLAGS
.long MB_CHECKSUM
/* Create stack */
.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:
/* Entrypoint */
.section .text
.global _start
.type _start, @function
_start:
/* Setup stack */
mov $stack_top, %esp
/* Call into C code */
call kernel_main
/* Hang if kernel_main returns */
cli
1: hlt
jmp 1b
.size _start, . - _start

16
kernel/arch/i386/crti.s Normal file
View File

@@ -0,0 +1,16 @@
/* x86 crti.s */
.section .init
.global _init
.type _init, @function
_init:
push %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
.section .fini
.global _fini
.type _fini, @function
_fini:
push %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

10
kernel/arch/i386/crtn.s Normal file
View File

@@ -0,0 +1,10 @@
/* x86 crtn.s */
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
popl %ebp
ret
.section .fini
/* gcc will nicely put the contents of crtend.o's .fini section here. */
popl %ebp
ret

View File

@@ -0,0 +1,28 @@
ENTRY(_start)
SECTIONS
{
. = 1M;
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}

View File

@@ -0,0 +1,8 @@
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
KERNEL_ARCH_LDFLAGS=
KERNEL_ARCH_LIBS=
KERNEL_ARCH_OBJS=\
$(ARCHDIR)/boot.o \
$(ARCHDIR)/tty.o \

103
kernel/arch/i386/tty.c Normal file
View File

@@ -0,0 +1,103 @@
#include <kernel/tty.h>
#include "vga.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static uint16_t* const VGA_MEMORY = (uint16_t*)0xB8000;
static size_t terminal_row;
static size_t terminal_col;
static uint8_t terminal_color;
static uint16_t* terminal_buffer;
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_clear(void)
{
for (size_t y = 0; y < VGA_HEIGHT; y++)
for (size_t x = 0; x < VGA_WIDTH; x++)
terminal_putentryat(' ', terminal_color, x, y);
}
void terminal_initialize(void)
{
terminal_row = 0;
terminal_col = 0;
terminal_color = vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
terminal_buffer = VGA_MEMORY;
terminal_clear();
}
void terminal_setcolor(uint8_t color)
{
terminal_color = color;
}
void terminal_scroll_line(size_t line)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
const size_t index = line * VGA_WIDTH + x;
terminal_buffer[index - VGA_WIDTH] = terminal_buffer[index];
}
}
void terminal_clear_line(size_t line)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
terminal_putentryat(' ', terminal_color, x, line);
}
void terminal_putchar(char c)
{
if (c == '\n')
{
terminal_col = 0;
terminal_row++;
}
else
{
terminal_putentryat(c, terminal_color, terminal_col, terminal_row);
terminal_col++;
}
if (terminal_col == VGA_WIDTH)
{
terminal_col = 0;
terminal_row++;
}
if (terminal_row == VGA_HEIGHT)
{
for (size_t line = 1; line < VGA_HEIGHT; line++)
terminal_scroll_line(line);
terminal_clear_line(VGA_HEIGHT - 1);
terminal_col = 0;
terminal_row = VGA_HEIGHT - 1;
}
}
void terminal_write(const char* data, size_t size)
{
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char* data)
{
size_t len = 0;
while (data[len])
len++;
terminal_write(data, len);
}

33
kernel/arch/i386/vga.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
enum vga_color
{
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15,
};
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t) uc | (uint16_t) color << 8;
}

View File

@@ -0,0 +1,8 @@
#pragma once
#include <stddef.h>
void terminal_initialize(void);
void terminal_putchar(char c);
void terminal_write(const char* data, size_t size);
void terminal_writestring(const char* data);

9
kernel/kernel/kernel.c Normal file
View File

@@ -0,0 +1,9 @@
#include <kernel/tty.h>
#include <stdio.h>
void kernel_main()
{
terminal_initialize();
printf("Hello from the kernel!\n");
}