Path and compile musl libc into kernel

This commit is contained in:
Thomas Lovén 2022-01-09 22:51:55 +01:00
parent c95f256e1b
commit 8d53f5468e
8 changed files with 158 additions and 7 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
mittos.iso mittos.iso
sysroot/ sysroot/
external/
*.d *.d
*.o *.o

View File

@ -7,7 +7,7 @@ KERNELMAKE := TARGET=${TARGET} $(MAKE) -C src/kernel
DIST := $(BUILDROOT)/mittos.iso DIST := $(BUILDROOT)/mittos.iso
SYSROOT := $(BUILDROOT)/sysroot SYSROOT := $(BUILDROOT)/sysroot
SYS_ITEMS := $(SYSROOT)/kernel SYS_ITEMS := $(SYSROOT)/kernel $(SYSROOT)/usr/lib/libc.a
all: $(SYSROOT)/kernel all: $(SYSROOT)/kernel
@ -17,11 +17,14 @@ $(DIST): $(SYS_ITEMS)
$(BUILDROOT)/toolchain/setup-grub.sh $(BUILDROOT)/toolchain/setup-grub.sh
grub-mkrescue -o $@ $(SYSROOT) grub-mkrescue -o $@ $(SYSROOT)
$(SYSROOT)/kernel: FORCE $(SYSROOT)/kernel: $(SYSROOT)/usr/lib/libc.a FORCE
ifeq ($(shell make -sqC src/kernel || echo 1), 1) ifeq ($(shell make -sqC src/kernel || echo 1), 1)
$(KERNELMAKE) install $(KERNELMAKE) install
endif endif
$(SYSROOT)/usr/lib/libc.a:
toolchain/build-musl.sh
.PHONY: all dist sysroot FORCE .PHONY: all dist sysroot FORCE
clean: clean:

View File

@ -11,10 +11,11 @@ CFLAGS := -Wall -Wextra -pedantic -ffreestanding -mcmodel=large
CFLAGS += -ggdb -O0 CFLAGS += -ggdb -O0
ASFLAGS += -ggdb ASFLAGS += -ggdb
CPPFLAGS += -I include CPPFLAGS += -I include
LDFLAGS := -n -nostdlib -lgcc -T Link.ld LDFLAGS := -n -T Link.ld
LDLIBS := -nostdlib -lgcc -L/opt/sysroot/usr/lib -lc
kernel: $(OBJ) kernel: $(OBJ)
$(LINK.c) $^ -o $@ $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
%.o: %.S.py %.o: %.S.py
python3 $^ | $(COMPILE.S) $(DEPFLAGS) -x assembler-with-cpp - -o $@ python3 $^ | $(COMPILE.S) $(DEPFLAGS) -x assembler-with-cpp - -o $@

View File

@ -5,6 +5,7 @@
#include <debug.h> #include <debug.h>
#include <multiboot.h> #include <multiboot.h>
#include <interrupts.h> #include <interrupts.h>
#include <stdlib.h>
struct kernel_boot_data_st kernel_boot_data; struct kernel_boot_data_st kernel_boot_data;
@ -26,6 +27,10 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data)
debug_info("Boot complete\n"); debug_info("Boot complete\n");
debug_info("Mallocing\n");
uint64_t a = malloc(100);
debug_info("Malloced %x\n", a);
PANIC("End of kernel function!"); PANIC("End of kernel function!");
debug_info("Broke out of panic"); debug_info("Broke out of panic");

View File

@ -0,0 +1,50 @@
#include <debug.h>
long syscall0(long num)
{
long retval = 0;
PANIC("Unknown syscall: %d()\n", num);
return retval;
}
extern long syscall1(long num, long a1)
{
long retval = 0;
PANIC("Unknown syscall: %d(%x)\n", num, a1);
return retval;
}
extern long syscall2(long num, long a1, long a2)
{
long retval = 0;
PANIC("Unknown syscall: %d(%x, %x)\n", num, a1, a2);
return retval;
}
extern long syscall3(long num, long a1, long a2, long a3)
{
long retval = 0;
PANIC("Unknown syscall: %d(%x, %x, %x)\n", num, a1, a2, a3);
return retval;
}
extern long syscall4(long num, long a1, long a2, long a3, long a4)
{
long retval = 0;
PANIC("Unknown syscall: %d(%x, %x, %x, %x)\n", num, a1, a2, a3, a4);
return retval;
}
extern long syscall5(long num, long a1, long a2, long a3, long a4, long a5)
{
long retval = 0;
PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x)\n", num, a1, a2, a3, a4, a5);
return retval;
}
extern long syscall6(long num, long a1, long a2, long a3, long a4, long a5, long a6)
{
long retval = 0;
PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x, %x)\n", num, a1, a2, a3, a4, a5, a6);
return retval;
}

View File

@ -0,0 +1,47 @@
#define __SYSCALL_LL_E(x) (x)
#define __SYSCALL_LL_O(x) (x)
extern long syscall0(long);
extern long syscall1(long, long);
extern long syscall2(long, long, long);
extern long syscall3(long, long, long, long);
extern long syscall4(long, long, long, long, long);
extern long syscall5(long, long, long, long, long, long);
extern long syscall6(long, long, long, long, long, long, long);
static __inline long __syscall0(long n)
{
return syscall0(n);
}
static __inline long __syscall1(long n, long a1)
{
return syscall1(n, a1);
}
static __inline long __syscall2(long n, long a1, long a2)
{
return syscall2(n, a1, a2);
}
static __inline long __syscall3(long n, long a1, long a2, long a3)
{
return syscall3(n, a1, a2, a3);
}
static __inline long __syscall4(long n, long a1, long a2, long a3, long a4)
{
return syscall4(n, a1, a2, a3, a4);
}
static __inline long __syscall5(long n, long a1, long a2, long a3, long a4, long a5)
{
return syscall5(n, a1, a2, a3, a4, a5);
}
static __inline long __syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6)
{
return syscall6(n, a1, a2, a3, a4, a5, a6);
}
#define VDSO_USEFUL
#define VDSO_CGT_SYM "__vdso_clock_gettime"
#define VDSO_CGT_VER "LINUX_2.6"
#define VDSO_GETCPU_SYM "__vdso_getcpu"
#define VDSO_GETCPU_VER "LINUX_2.6"
#define IPC_64 0

26
toolchain/build-musl.sh Executable file
View File

@ -0,0 +1,26 @@
#!/bin/sh -e
target=x86_64-elf
SYSROOT=/opt/sysroot
# Pull or update musl libc
mkdir -p /opt/external && cd /opt/external
[ -d musl ] || git clone --depth=1 git://git.musl-libc.org/musl
cd musl
git reset --hard
git pull
[ -d /opt/src/patch-musl ] && rsync -a /opt/src/patch-musl/ /opt/external/musl/
mkdir -p /opt/external/build-musl && cd /opt/external/build-musl
rm -r *
../musl/configure \
--target=${target} \
--prefix=${SYSROOT}/usr \
--disable-shared \
--enable-debug \
CFLAGS="-O0 -mcmodel=large"
make
make install-headers
make install

View File

@ -5,14 +5,31 @@ apk add gmp-dev mpfr-dev mpc1-dev
apk add make apk add make
apk add grub-bios xorriso apk add grub-bios xorriso
apk add python3 apk add python3 git rsync
rm -rf /var/cache/apk/* rm -rf /var/cache/apk/*
target=x86_64-elf target=x86_64-elf
SYSROOT=/opt/sysroot
binutils=binutils-2.37 binutils=binutils-2.37
gcc=gcc-11.2.0 gcc=gcc-11.2.0
mkdir -p /opt/external
cd /opt/external
[ -d musl ] || git clone --depth=1 git://git.musl-libc.org/musl
cd musl
git reset --hard
git pull
cat > config.mak << EOF
ARCH=x86_64
prefix=${SYSROOT}/usr
includedir=\$(prefix)/include
EOF
make install-headers
# Build binutils
cd /opt cd /opt
wget http://ftp.gnu.org/gnu/binutils/${binutils}.tar.gz wget http://ftp.gnu.org/gnu/binutils/${binutils}.tar.gz
tar -xf ${binutils}.tar.gz tar -xf ${binutils}.tar.gz
@ -21,11 +38,12 @@ mkdir binutils-build && cd binutils-build
--target=${target} \ --target=${target} \
--disable-nls \ --disable-nls \
--disable-werror \ --disable-werror \
--with-sysroot \ --with-sysroot=${SYSROOT}
make -j 4 make -j 4
make install make install
# Build gcc and libgcc
cd /opt cd /opt
wget http://ftp.gnu.org/gnu/gcc/${gcc}/${gcc}.tar.gz wget http://ftp.gnu.org/gnu/gcc/${gcc}/${gcc}.tar.gz
tar -xf ${gcc}.tar.gz tar -xf ${gcc}.tar.gz
@ -34,7 +52,7 @@ mkdir gcc-build && cd gcc-build
--target=${target} \ --target=${target} \
--disable-nls \ --disable-nls \
--enable-languages=c \ --enable-languages=c \
--without-headers \ --with-sysroot=${SYSROOT}
make all-gcc all-target-libgcc -j 4 make all-gcc all-target-libgcc -j 4
make install-gcc install-target-libgcc make install-gcc install-target-libgcc