From e6b4866ab01150368aaa6bc98ac74f21518ef538 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 12 Nov 2022 21:04:47 +0200 Subject: [PATCH] Initial commit. We have a booting kernel --- .gitignore | 3 + build.sh | 7 +++ clean.sh | 11 ++++ config.sh | 28 ++++++++++ default-host.sh | 2 + headers.sh | 9 +++ iso.sh | 15 +++++ kernel/.gitignore | 3 + kernel/Makefile | 83 ++++++++++++++++++++++++++++ kernel/arch/i386/boot.s | 38 +++++++++++++ kernel/arch/i386/crti.s | 16 ++++++ kernel/arch/i386/crtn.s | 10 ++++ kernel/arch/i386/linker.ld | 28 ++++++++++ kernel/arch/i386/make.config | 8 +++ kernel/arch/i386/tty.c | 103 +++++++++++++++++++++++++++++++++++ kernel/arch/i386/vga.h | 33 +++++++++++ kernel/include/kernel/tty.h | 8 +++ kernel/kernel/kernel.c | 9 +++ libc/.gitignore | 3 + libc/Makefile | 93 +++++++++++++++++++++++++++++++ libc/arch/i386/make.config | 8 +++ libc/include/stdio.h | 17 ++++++ libc/include/stdlib.h | 14 +++++ libc/include/string.h | 18 ++++++ libc/include/sys/cdefs.h | 3 + libc/stdio/printf.c | 9 +++ libc/stdio/putchar.c | 15 +++++ libc/stdio/puts.c | 6 ++ libc/stdlib/abort.c | 15 +++++ libc/string/memcmp.c | 17 ++++++ libc/string/memcpy.c | 10 ++++ libc/string/memmove.c | 20 +++++++ libc/string/memset.c | 9 +++ libc/string/strlen.c | 9 +++ qemu.sh | 5 ++ target-triplet-to-arch.sh | 6 ++ 36 files changed, 691 insertions(+) create mode 100644 .gitignore create mode 100755 build.sh create mode 100755 clean.sh create mode 100644 config.sh create mode 100755 default-host.sh create mode 100755 headers.sh create mode 100755 iso.sh create mode 100644 kernel/.gitignore create mode 100644 kernel/Makefile create mode 100644 kernel/arch/i386/boot.s create mode 100644 kernel/arch/i386/crti.s create mode 100644 kernel/arch/i386/crtn.s create mode 100644 kernel/arch/i386/linker.ld create mode 100644 kernel/arch/i386/make.config create mode 100644 kernel/arch/i386/tty.c create mode 100644 kernel/arch/i386/vga.h create mode 100644 kernel/include/kernel/tty.h create mode 100644 kernel/kernel/kernel.c create mode 100644 libc/.gitignore create mode 100644 libc/Makefile create mode 100644 libc/arch/i386/make.config create mode 100644 libc/include/stdio.h create mode 100644 libc/include/stdlib.h create mode 100644 libc/include/string.h create mode 100644 libc/include/sys/cdefs.h create mode 100644 libc/stdio/printf.c create mode 100644 libc/stdio/putchar.c create mode 100644 libc/stdio/puts.c create mode 100644 libc/stdlib/abort.c create mode 100644 libc/string/memcmp.c create mode 100644 libc/string/memcpy.c create mode 100644 libc/string/memmove.c create mode 100644 libc/string/memset.c create mode 100644 libc/string/strlen.c create mode 100755 qemu.sh create mode 100755 target-triplet-to-arch.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..47bd505c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.iso +isodir +sysroot diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..e19f5fa2 --- /dev/null +++ b/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e +. ./headers.sh + +for PROJECT in $PROJECTS; do + (cd $PROJECT && DESTDIR="$SYSROOT" $MAKE install) +done diff --git a/clean.sh b/clean.sh new file mode 100755 index 00000000..5d057e96 --- /dev/null +++ b/clean.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -e +. ./config.sh + +for PROJECT in $PROJECTS; do + (cd $PROJECT && $MAKE clean) +done + +rm -rf sysroot +rm -rf isodir +rm -rf banan-os.iso diff --git a/config.sh b/config.sh new file mode 100644 index 00000000..b182b712 --- /dev/null +++ b/config.sh @@ -0,0 +1,28 @@ +SYSTEM_HEADER_PROJECTS="libc kernel" +PROJECTS="libc kernel" + +export MAKE=${MAKE:-make} +export HOST=${HOST:-$(./default-host.sh)} + +export AR=${HOST}-ar +export AS=${HOST}-as +export CC=${HOST}-gcc + +export PREFIX=/usr +export EXEC_PREFIX=$PREFIX +export BOOTDIR=/boot +export LIBDIR=$EXEC_PREFIX/lib +export INCLUDEDIR=$PREFIX/include + +export CFLAGS='-O2 -g' +export CPPFLAGS='' + +# Configure the cross-compiler to use the desired system root. +export SYSROOT="$(pwd)/sysroot" +export CC="$CC --sysroot=$SYSROOT" + +# Work around that the -elf gcc targets doesn't have a system include directory +# because it was configured with --without-headers rather than --with-sysroot. +if echo "$HOST" | grep -Eq -- '-elf($|-)'; then + export CC="$CC -isystem=$INCLUDEDIR" +fi diff --git a/default-host.sh b/default-host.sh new file mode 100755 index 00000000..7e0f7955 --- /dev/null +++ b/default-host.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo i686-elf diff --git a/headers.sh b/headers.sh new file mode 100755 index 00000000..050e1958 --- /dev/null +++ b/headers.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -e +. ./config.sh + +mkdir -p "$SYSROOT" + +for PROJECT in $SYSTEM_HEADER_PROJECTS; do + (cd $PROJECT && DESTDIR="$SYSROOT" $MAKE install-headers) +done diff --git a/iso.sh b/iso.sh new file mode 100755 index 00000000..b597040e --- /dev/null +++ b/iso.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -e +. ./build.sh + +mkdir -p isodir +mkdir -p isodir/boot +mkdir -p isodir/boot/grub + +cp sysroot/boot/banan-os.kernel isodir/boot/banan-os.kernel +cat > isodir/boot/grub/grub.cfg << EOF +menuentry "banan-os" { + multiboot /boot/banan-os.kernel +} +EOF +grub-mkrescue -o banan-os.iso isodir diff --git a/kernel/.gitignore b/kernel/.gitignore new file mode 100644 index 00000000..9290cdf5 --- /dev/null +++ b/kernel/.gitignore @@ -0,0 +1,3 @@ +*.d +*.kernel +*.o diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 00000000..07b40703 --- /dev/null +++ b/kernel/Makefile @@ -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) \ No newline at end of file diff --git a/kernel/arch/i386/boot.s b/kernel/arch/i386/boot.s new file mode 100644 index 00000000..2595b997 --- /dev/null +++ b/kernel/arch/i386/boot.s @@ -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 diff --git a/kernel/arch/i386/crti.s b/kernel/arch/i386/crti.s new file mode 100644 index 00000000..972b9735 --- /dev/null +++ b/kernel/arch/i386/crti.s @@ -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. */ \ No newline at end of file diff --git a/kernel/arch/i386/crtn.s b/kernel/arch/i386/crtn.s new file mode 100644 index 00000000..5da1e23c --- /dev/null +++ b/kernel/arch/i386/crtn.s @@ -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 \ No newline at end of file diff --git a/kernel/arch/i386/linker.ld b/kernel/arch/i386/linker.ld new file mode 100644 index 00000000..c6c65728 --- /dev/null +++ b/kernel/arch/i386/linker.ld @@ -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) + } +} diff --git a/kernel/arch/i386/make.config b/kernel/arch/i386/make.config new file mode 100644 index 00000000..a4cbb930 --- /dev/null +++ b/kernel/arch/i386/make.config @@ -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 \ diff --git a/kernel/arch/i386/tty.c b/kernel/arch/i386/tty.c new file mode 100644 index 00000000..1b8d5767 --- /dev/null +++ b/kernel/arch/i386/tty.c @@ -0,0 +1,103 @@ +#include + +#include "vga.h" + +#include +#include +#include + + +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); +} \ No newline at end of file diff --git a/kernel/arch/i386/vga.h b/kernel/arch/i386/vga.h new file mode 100644 index 00000000..14c7cd22 --- /dev/null +++ b/kernel/arch/i386/vga.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +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; +} diff --git a/kernel/include/kernel/tty.h b/kernel/include/kernel/tty.h new file mode 100644 index 00000000..835e33df --- /dev/null +++ b/kernel/include/kernel/tty.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +void terminal_initialize(void); +void terminal_putchar(char c); +void terminal_write(const char* data, size_t size); +void terminal_writestring(const char* data); \ No newline at end of file diff --git a/kernel/kernel/kernel.c b/kernel/kernel/kernel.c new file mode 100644 index 00000000..9b7585d4 --- /dev/null +++ b/kernel/kernel/kernel.c @@ -0,0 +1,9 @@ +#include + +#include + +void kernel_main() +{ + terminal_initialize(); + printf("Hello from the kernel!\n"); +} \ No newline at end of file diff --git a/libc/.gitignore b/libc/.gitignore new file mode 100644 index 00000000..268e0748 --- /dev/null +++ b/libc/.gitignore @@ -0,0 +1,3 @@ +*.a +*.d +*.o diff --git a/libc/Makefile b/libc/Makefile new file mode 100644 index 00000000..8d41f743 --- /dev/null +++ b/libc/Makefile @@ -0,0 +1,93 @@ +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) +INCLUDEDIR?=$(PREFIX)/include +LIBDIR?=$(EXEC_PREFIX)/lib + +CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra +CPPFLAGS:=$(CPPFLAGS) -D__is_libc -Iinclude +LIBK_CFLAGS:=$(CFLAGS) +LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk + +ARCHDIR=arch/$(HOSTARCH) + +include $(ARCHDIR)/make.config + +CFLAGS:=$(CFLAGS) $(ARCH_CFLAGS) +CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS) +LIBK_CFLAGS:=$(LIBK_CFLAGS) $(KERNEL_ARCH_CFLAGS) +LIBK_CPPFLAGS:=$(LIBK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS) + +FREEOBJS=\ +$(ARCH_FREEOBJS) \ +stdio/printf.o \ +stdio/putchar.o \ +stdio/puts.o \ +stdlib/abort.o \ +string/memcmp.o \ +string/memcpy.o \ +string/memmove.o \ +string/memset.o \ +string/strlen.o \ + +HOSTEDOBJS=\ +$(ARCH_HOSTEDOBJS) \ + +OBJS=\ +$(FREEOBJS) \ +$(HOSTEDOBJS) \ + +LIBK_OBJS=$(FREEOBJS:.o=.libk.o) + +#BINARIES=libc.a libk.a # Not ready for libc yet. +BINARIES=libk.a + +.PHONY: all clean install install-headers install-libs +.SUFFIXES: .o .libk.o .c .S + +all: $(BINARIES) + +libc.a: $(OBJS) + $(AR) rcs $@ $(OBJS) + +libk.a: $(LIBK_OBJS) + $(AR) rcs $@ $(LIBK_OBJS) + +.c.o: + $(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS) + +.S.o: + $(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + +.c.libk.o: + $(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + +.S.libk.o: + $(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + +clean: + rm -f $(BINARIES) *.a + rm -f $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o + rm -f $(OBJS:.o=.d) $(LIBK_OBJS:.o=.d) *.d */*.d */*/*.d + +install: install-headers install-libs + +install-headers: + mkdir -p $(DESTDIR)$(INCLUDEDIR) + cp -R --preserve=timestamps include/. $(DESTDIR)$(INCLUDEDIR)/. + +install-libs: $(BINARIES) + mkdir -p $(DESTDIR)$(LIBDIR) + cp $(BINARIES) $(DESTDIR)$(LIBDIR) + +-include $(OBJS:.o=.d) +-include $(LIBK_OBJS:.o=.d) \ No newline at end of file diff --git a/libc/arch/i386/make.config b/libc/arch/i386/make.config new file mode 100644 index 00000000..24f8c15e --- /dev/null +++ b/libc/arch/i386/make.config @@ -0,0 +1,8 @@ +ARCH_CFLAGS= +ARCH_CPPFLAGS= +KERNEL_ARCH_CFLAGS= +KERNEL_ARCH_CPPFLAGS= + +ARCH_FREEOBJS=\ + +ARCH_HOSTEDOBJS=\ diff --git a/libc/include/stdio.h b/libc/include/stdio.h new file mode 100644 index 00000000..c8482f8b --- /dev/null +++ b/libc/include/stdio.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#define EOF (-1) + +#ifdef __cplusplus +extern "C" { +#endif + +int printf(const char* __restrict, ...); +int putchar(int); +int puts(const char*); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h new file mode 100644 index 00000000..61c8a92b --- /dev/null +++ b/libc/include/stdlib.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((__noreturn__)) +void abort(void); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/libc/include/string.h b/libc/include/string.h new file mode 100644 index 00000000..5b228b5a --- /dev/null +++ b/libc/include/string.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int memcmp(const void*, const void*, size_t); +void* memcpy(void* __restrict, const void* __restrict, size_t); +void* memmove(void*, const void*, size_t); +void* memset(void*, int, size_t); +size_t strlen(const char*); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h new file mode 100644 index 00000000..368603d4 --- /dev/null +++ b/libc/include/sys/cdefs.h @@ -0,0 +1,3 @@ +#pragma once + +#define __banan_libc 1 \ No newline at end of file diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c new file mode 100644 index 00000000..e3f0ef2c --- /dev/null +++ b/libc/stdio/printf.c @@ -0,0 +1,9 @@ +#include + +int printf(const char* restrict fmt, ...) +{ + int len = 0; + while (fmt[len]) + putchar(fmt[len++]); + return len; +} \ No newline at end of file diff --git a/libc/stdio/putchar.c b/libc/stdio/putchar.c new file mode 100644 index 00000000..71152d75 --- /dev/null +++ b/libc/stdio/putchar.c @@ -0,0 +1,15 @@ +#include + +#if defined(__is_libk) +#include +#endif + +int putchar(int c) +{ +#if defined(__is_libk) + char ch = (char)c; + terminal_write(&ch, sizeof(ch)); +#else +#endif + return c; +} \ No newline at end of file diff --git a/libc/stdio/puts.c b/libc/stdio/puts.c new file mode 100644 index 00000000..ba661b88 --- /dev/null +++ b/libc/stdio/puts.c @@ -0,0 +1,6 @@ +#include + +int puts(const char* str) +{ + return printf("%s\n", str); +} \ No newline at end of file diff --git a/libc/stdlib/abort.c b/libc/stdlib/abort.c new file mode 100644 index 00000000..354132ef --- /dev/null +++ b/libc/stdlib/abort.c @@ -0,0 +1,15 @@ +#include +#include + +__attribute__((__noreturn__)) +void abort(void) +{ +#if defined(__is_libk) + printf("Kernel panic: abort()\n"); + asm volatile("hlt"); +#else + printf("abort()\n"); +#endif + while (1); + __builtin_unreachable(); +} \ No newline at end of file diff --git a/libc/string/memcmp.c b/libc/string/memcmp.c new file mode 100644 index 00000000..7617a848 --- /dev/null +++ b/libc/string/memcmp.c @@ -0,0 +1,17 @@ +#include + +int memcmp(const void* s1, const void* s2, size_t n) +{ + const unsigned char* a = s1; + const unsigned char* b = s2; + + while (n--) + { + if (*a != *b) + return *a - *b; + a++; + b++; + } + + return 0; +} diff --git a/libc/string/memcpy.c b/libc/string/memcpy.c new file mode 100644 index 00000000..c03360d3 --- /dev/null +++ b/libc/string/memcpy.c @@ -0,0 +1,10 @@ +#include + +void* memcpy(void* restrict destp, const void* restrict srcp, size_t n) +{ + unsigned char* dest = (unsigned char*)destp; + const unsigned char* src = (const unsigned char*)srcp; + for (size_t i = 0; i < n; i++) + dest[i] = src[i]; + return destp; +} \ No newline at end of file diff --git a/libc/string/memmove.c b/libc/string/memmove.c new file mode 100644 index 00000000..d4e14350 --- /dev/null +++ b/libc/string/memmove.c @@ -0,0 +1,20 @@ +#include + +void* memmove(void* destp, const void* srcp, size_t n) +{ + unsigned char* dest = (unsigned char*)destp; + const unsigned char* src = (const unsigned char*)srcp; + + if (dest < src) + { + for (size_t i = 0; i < n; i++) + dest[i] = src[i]; + } + else + { + for (size_t i = 1; i <= n; i++) + dest[n - i] = src[n - i]; + } + + return destp; +} diff --git a/libc/string/memset.c b/libc/string/memset.c new file mode 100644 index 00000000..cbad0b3f --- /dev/null +++ b/libc/string/memset.c @@ -0,0 +1,9 @@ +#include + +void* memset(void* s, int c, size_t n) +{ + unsigned char* p = (unsigned char*)s; + for (size_t i = 0; i < n; i++) + p[i] = c; + return s; +} \ No newline at end of file diff --git a/libc/string/strlen.c b/libc/string/strlen.c new file mode 100644 index 00000000..22b301dc --- /dev/null +++ b/libc/string/strlen.c @@ -0,0 +1,9 @@ +#include + +size_t strlen(const char* str) +{ + size_t len = 0; + while (str[len]) + len++; + return len; +} \ No newline at end of file diff --git a/qemu.sh b/qemu.sh new file mode 100755 index 00000000..c86b5c45 --- /dev/null +++ b/qemu.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -e +. ./iso.sh + +qemu-system-$(./target-triplet-to-arch.sh $HOST) -cdrom banan-os.iso diff --git a/target-triplet-to-arch.sh b/target-triplet-to-arch.sh new file mode 100755 index 00000000..35572857 --- /dev/null +++ b/target-triplet-to-arch.sh @@ -0,0 +1,6 @@ +#!/bin/sh +if echo "$1" | grep -Eq 'i[[:digit:]]86-'; then + echo i386 +else + echo "$1" | grep -Eo '^[[:alnum:]_]*' +fi