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
libc/.gitignore vendored Normal file
View File

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

93
libc/Makefile Normal file
View File

@@ -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)

View File

@@ -0,0 +1,8 @@
ARCH_CFLAGS=
ARCH_CPPFLAGS=
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
ARCH_FREEOBJS=\
ARCH_HOSTEDOBJS=\

17
libc/include/stdio.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <sys/cdefs.h>
#define EOF (-1)
#ifdef __cplusplus
extern "C" {
#endif
int printf(const char* __restrict, ...);
int putchar(int);
int puts(const char*);
#ifdef __cplusplus
}
#endif

14
libc/include/stdlib.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <sys/cdefs.h>
#ifdef __cplusplus
extern "C" {
#endif
__attribute__((__noreturn__))
void abort(void);
#ifdef __cplusplus
}
#endif

18
libc/include/string.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <stddef.h>
#include <sys/cdefs.h>
#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

3
libc/include/sys/cdefs.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
#define __banan_libc 1

9
libc/stdio/printf.c Normal file
View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int printf(const char* restrict fmt, ...)
{
int len = 0;
while (fmt[len])
putchar(fmt[len++]);
return len;
}

15
libc/stdio/putchar.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdio.h>
#if defined(__is_libk)
#include <kernel/tty.h>
#endif
int putchar(int c)
{
#if defined(__is_libk)
char ch = (char)c;
terminal_write(&ch, sizeof(ch));
#else
#endif
return c;
}

6
libc/stdio/puts.c Normal file
View File

@@ -0,0 +1,6 @@
#include <stdio.h>
int puts(const char* str)
{
return printf("%s\n", str);
}

15
libc/stdlib/abort.c Normal file
View File

@@ -0,0 +1,15 @@
#include <stdlib.h>
#include <stdio.h>
__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();
}

17
libc/string/memcmp.c Normal file
View File

@@ -0,0 +1,17 @@
#include <string.h>
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;
}

10
libc/string/memcpy.c Normal file
View File

@@ -0,0 +1,10 @@
#include <string.h>
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;
}

20
libc/string/memmove.c Normal file
View File

@@ -0,0 +1,20 @@
#include <string.h>
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;
}

9
libc/string/memset.c Normal file
View File

@@ -0,0 +1,9 @@
#include <string.h>
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;
}

9
libc/string/strlen.c Normal file
View File

@@ -0,0 +1,9 @@
#include <string.h>
size_t strlen(const char* str)
{
size_t len = 0;
while (str[len])
len++;
return len;
}