diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-01-06 23:39:12 +0200 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-01-07 14:05:06 +0200 |
| commit | 9cc125fb3cfdc2c8ae7153b5ae0fe705980835fb (patch) | |
| tree | a1974f9513458acab0bd8d09758259b24b6d7d8c | |
| parent | f0eba93472e0afecc57180fc0d638eeef8e6f3c6 (diff) | |
| download | kmi-9cc125fb3cfdc2c8ae7153b5ae0fe705980835fb.tar.gz kmi-9cc125fb3cfdc2c8ae7153b5ae0fe705980835fb.zip | |
Allow building with clang
+ Release mode is sort of broken, as lto doesn't work with linker
relaxation and turning the relaxation off causes issues with the jump
from init to kernel (as I've currently implemented it, could probably
be fixed with a manual jump from assembly, I'll add it to my TODO
list)
| -rw-r--r-- | Makefile | 46 | ||||
| -rw-r--r-- | arch/riscv/init/start.S | 5 | ||||
| -rw-r--r-- | arch/riscv/source.mk | 10 | ||||
| -rw-r--r-- | arch/riscv64/conf/apos.its (renamed from arch/riscv/conf/apos.its) | 0 | ||||
| -rw-r--r-- | arch/riscv64/conf/boot.cmd (renamed from arch/riscv/conf/boot.cmd) | 0 | ||||
| -rwxr-xr-x | arch/riscv64/conf/init (renamed from arch/riscv/conf/init) | bin | 968 -> 968 bytes | |||
| -rw-r--r-- | arch/riscv64/conf/init-link.S (renamed from arch/riscv/conf/init-link.S) | 2 | ||||
| -rw-r--r-- | arch/riscv64/conf/initrd (renamed from arch/riscv/conf/initrd) | bin | 1536 -> 1536 bytes | |||
| -rw-r--r-- | arch/riscv64/conf/kernel-link.S (renamed from arch/riscv/conf/kernel-link.S) | 2 | ||||
| -rwxr-xr-x | arch/riscv64/conf/mkimage.sh (renamed from arch/riscv/conf/mkimage.sh) | 4 | ||||
| -rwxr-xr-x | arch/riscv64/conf/rmimage.sh (renamed from arch/riscv/conf/rmimage.sh) | 0 | ||||
| -rw-r--r-- | arch/riscv64/conf/rootfs.fd (renamed from arch/riscv/conf/rootfs.fd) | 0 | ||||
| -rw-r--r-- | arch/riscv64/conf/s.S (renamed from arch/riscv/conf/s.S) | 0 | ||||
| -rw-r--r-- | arch/riscv64/config.h (renamed from arch/riscv/config.h) | 0 | ||||
| -rw-r--r-- | arch/riscv64/include/csr.h (renamed from arch/riscv/include/csr.h) | 8 | ||||
| -rw-r--r-- | arch/riscv64/include/lock.h (renamed from arch/riscv/include/lock.h) | 0 | ||||
| -rw-r--r-- | arch/riscv64/include/pages.h (renamed from arch/riscv/include/pages.h) | 0 | ||||
| -rw-r--r-- | arch/riscv64/include/sbi.h (renamed from arch/riscv/include/sbi.h) | 0 | ||||
| -rw-r--r-- | arch/riscv64/include/tcb.h (renamed from arch/riscv/include/tcb.h) | 0 | ||||
| -rw-r--r-- | arch/riscv64/include/vmem.h (renamed from arch/riscv/include/vmem.h) | 0 | ||||
| -rw-r--r-- | arch/riscv64/init/init.c (renamed from arch/riscv/init/init.c) | 4 | ||||
| -rw-r--r-- | arch/riscv64/init/start.S | 12 | ||||
| -rw-r--r-- | arch/riscv64/ipc.txt (renamed from arch/riscv/ipc.txt) | 0 | ||||
| -rw-r--r-- | arch/riscv64/kernel/cpu.c (renamed from arch/riscv/kernel/cpu.c) | 0 | ||||
| -rw-r--r-- | arch/riscv64/kernel/irq.c (renamed from arch/riscv/kernel/irq.c) | 0 | ||||
| -rw-r--r-- | arch/riscv64/kernel/main.c (renamed from arch/riscv/kernel/main.c) | 0 | ||||
| -rw-r--r-- | arch/riscv64/kernel/pmem.c (renamed from arch/riscv/kernel/pmem.c) | 0 | ||||
| -rw-r--r-- | arch/riscv64/kernel/proc.c (renamed from arch/riscv/kernel/proc.c) | 4 | ||||
| -rw-r--r-- | arch/riscv64/kernel/vmem.c (renamed from arch/riscv/kernel/vmem.c) | 4 | ||||
| -rw-r--r-- | arch/riscv64/mm.txt (renamed from arch/riscv/mm.txt) | 0 | ||||
| -rw-r--r-- | arch/riscv64/proc.txt (renamed from arch/riscv/proc.txt) | 0 | ||||
| -rw-r--r-- | arch/riscv64/source.mk | 17 | ||||
| -rw-r--r-- | common/bytes.c | 3 | ||||
| -rw-r--r-- | common/initrd.c | 2 | ||||
| -rw-r--r-- | include/apos/elf.h | 14 |
35 files changed, 82 insertions, 55 deletions
@@ -1,25 +1,30 @@ DO != echo > deps.mk # this could be done better -DEBUGFLAGS != [ $(RELEASE) ] && echo "-flto -O2" || echo "-O0 -ggdb3 -DDEBUG" -CFLAGS = -fno-pie -ffreestanding -nostdlib -std=c17 -Wall -Wextra -LINTFLAGS = -fsyntax-only +DEBUGFLAGS != [ $(RELEASE) ] \ + && echo "-flto -O2" \ + || echo "-O0 -ggdb3 -DDEBUG" + +CFLAGS = -ffreestanding -nostdlib -std=c17 -Wall -Wextra DEPFLAGS = -MT $@ -MMD -MP -MF $@.d +LINTFLAGS = -fsyntax-only +PREPROCESS = -E +LINKFLAGS = all: apos.bin # default values, overwrite if/when needed -ARCH ?= riscv -CROSS_COMPILE ?= riscv64-unknown-elf- +ARCH ?= riscv64 +CROSS_COMPILE ?= $(ARCH)-unknown-elf -# Common programs -CC := gcc -AR := ar -CPP := cpp -OBJCOPY ?= objcopy +OBJCOPY != [ $(LLVM) ] \ + && echo llvm-objcopy \ + || echo $(CROSS_COMPILE)-objcopy + +COMPILER != [ $(LLVM) ] \ + && echo clang --target="$(CROSS_COMPILE)" \ + || echo $(CROSS_COMPILE)-gcc -# This makes sure .bss is loaded into the binary -OBJCOPY_FLAGS ?= -Obinary --set-section-flags .bss=alloc,load,contents KERNEL_SOURCES != echo common/*.c common/uapi/*.c lib/*.c CLEANUP := build deps.mk kernel.* init.* apos.bin @@ -31,16 +36,19 @@ include arch/$(ARCH)/source.mk INCLUDE_FLAGS := -I include -I arch/$(ARCH)/include\ -include config.h -include arch/$(ARCH)/config.h -COMPILE = $(CROSS_COMPILE)$(CC) $(DEBUGFLAGS)\ +# This makes sure .bss is loaded into the binary +OBJCOPY_FLAGS ?= -Obinary --set-section-flags .bss=alloc,load,contents + +COMPILE = $(COMPILER) $(DEBUGFLAGS)\ $(CFLAGS) $(ARCH_FLAGS) $(DEPFLAGS) $(INCLUDE_FLAGS) -LINT = $(CROSS_COMPILE)$(CC) $(DEBUGFLAGS)\ +LINT = $(COMPILER) $(DEBUGFLAGS)\ $(CFLAGS) $(ARCH_FLAGS) $(LINTFLAGS) $(INCLUDE_FLAGS) -GENELF = $(CROSS_COMPILE)$(CC) $(DEBUGFLAGS)\ - $(CFLAGS) $(ARCH_FLAGS) $(INCLUDE_FLAGS) +GENELF = $(COMPILER) $(DEBUGFLAGS)\ + $(CFLAGS) $(ARCH_FLAGS) $(LINKFLAGS) $(INCLUDE_FLAGS) -GENLINK = $(CROSS_COMPILE)$(CPP) $(DEPFLAGS) $(INCLUDE_FLAGS) +GENLINK = $(COMPILER) $(PREPROCESS) $(DEPFLAGS) $(INCLUDE_FLAGS) STRIPLINK = sed -n '/^[^\#]/p' KERN_SIZE = wc -c kernel.bin | cut -d ' ' -f 1 KERN_INFO = sed "s/<KERNEL_SIZE>/$$($(KERN_SIZE))/" @@ -66,10 +74,10 @@ kernel.elf: $(KERNEL_OBJECTS) $(KERNEL_LD) $(GENELF) -T $(KERNEL_LD) $(KERNEL_OBJECTS) -o $@ init.bin: init.elf - $(CROSS_COMPILE)$(OBJCOPY) $(OBJCOPY_FLAGS) $< $@ + $(OBJCOPY) $(OBJCOPY_FLAGS) $< $@ kernel.bin: kernel.elf - $(CROSS_COMPILE)$(OBJCOPY) $(OBJCOPY_FLAGS) $< $@ + $(OBJCOPY) $(OBJCOPY_FLAGS) $< $@ apos.bin: init.bin kernel.bin cat init.bin kernel.bin > apos.bin diff --git a/arch/riscv/init/start.S b/arch/riscv/init/start.S deleted file mode 100644 index f9bfb92..0000000 --- a/arch/riscv/init/start.S +++ /dev/null @@ -1,5 +0,0 @@ -.section .init.start -.global _start -_start: -li sp, PM_STACK_TOP -jal init diff --git a/arch/riscv/source.mk b/arch/riscv/source.mk deleted file mode 100644 index e50880c..0000000 --- a/arch/riscv/source.mk +++ /dev/null @@ -1,10 +0,0 @@ -KERNEL_LOCAL != echo arch/riscv/kernel/*.[cS] -KERNEL_SOURCES += $(KERNEL_LOCAL) -INIT_SOURCES += arch/riscv/init/*.[cS] - -CLEANUP_CMD := ./arch/riscv/conf/rmimage.sh - -ARCH_FLAGS := -mcmodel=medany - -run: - ./arch/riscv/conf/mkimage.sh diff --git a/arch/riscv/conf/apos.its b/arch/riscv64/conf/apos.its index da48860..da48860 100644 --- a/arch/riscv/conf/apos.its +++ b/arch/riscv64/conf/apos.its diff --git a/arch/riscv/conf/boot.cmd b/arch/riscv64/conf/boot.cmd index a7517fd..a7517fd 100644 --- a/arch/riscv/conf/boot.cmd +++ b/arch/riscv64/conf/boot.cmd diff --git a/arch/riscv/conf/init b/arch/riscv64/conf/init Binary files differindex f05254f..f05254f 100755 --- a/arch/riscv/conf/init +++ b/arch/riscv64/conf/init diff --git a/arch/riscv/conf/init-link.S b/arch/riscv64/conf/init-link.S index ce53e5e..baabcc6 100644 --- a/arch/riscv/conf/init-link.S +++ b/arch/riscv64/conf/init-link.S @@ -23,7 +23,7 @@ SECTIONS { } .bss : { - *(.bss*) *(COMMON) + *(.sbss*) *(.bss*) *(COMMON) } __init_end = . ; diff --git a/arch/riscv/conf/initrd b/arch/riscv64/conf/initrd Binary files differindex 90344c1..90344c1 100644 --- a/arch/riscv/conf/initrd +++ b/arch/riscv64/conf/initrd diff --git a/arch/riscv/conf/kernel-link.S b/arch/riscv64/conf/kernel-link.S index d3b1223..c6a910a 100644 --- a/arch/riscv/conf/kernel-link.S +++ b/arch/riscv64/conf/kernel-link.S @@ -19,7 +19,7 @@ SECTIONS { } .bss : { - *(.bss*) *(COMMON) + *(.sbss*) *(.bss*) *(COMMON) } __kernel_end = .; diff --git a/arch/riscv/conf/mkimage.sh b/arch/riscv64/conf/mkimage.sh index 5a22d01..d097248 100755 --- a/arch/riscv/conf/mkimage.sh +++ b/arch/riscv64/conf/mkimage.sh @@ -16,9 +16,9 @@ mount -o loop,offset=$((${SSIZE}*${OFFSET})) rootfs.img fs # not entirely pleased with this solution, although eventually I should probably # move `run` out of the kernel repo and into some `aposos` repo with a runtime # and proper initrd etc. so this is good enough for now -../u-boot-apos/tools/mkimage -f arch/riscv/conf/apos.its fs/apos.itb +../u-boot-apos/tools/mkimage -f arch/riscv64/conf/apos.its fs/apos.itb ../u-boot-apos/tools/mkimage -A riscv -O apos -T script \ - -d arch/riscv/conf/boot.cmd fs/boot.scr + -d arch/riscv64/conf/boot.cmd fs/boot.scr umount -l fs diff --git a/arch/riscv/conf/rmimage.sh b/arch/riscv64/conf/rmimage.sh index 8d6457f..8d6457f 100755 --- a/arch/riscv/conf/rmimage.sh +++ b/arch/riscv64/conf/rmimage.sh diff --git a/arch/riscv/conf/rootfs.fd b/arch/riscv64/conf/rootfs.fd index 59fb5f8..59fb5f8 100644 --- a/arch/riscv/conf/rootfs.fd +++ b/arch/riscv64/conf/rootfs.fd diff --git a/arch/riscv/conf/s.S b/arch/riscv64/conf/s.S index 80a3a11..80a3a11 100644 --- a/arch/riscv/conf/s.S +++ b/arch/riscv64/conf/s.S diff --git a/arch/riscv/config.h b/arch/riscv64/config.h index 0ef22fa..0ef22fa 100644 --- a/arch/riscv/config.h +++ b/arch/riscv64/config.h diff --git a/arch/riscv/include/csr.h b/arch/riscv64/include/csr.h index b309f0b..52db9c1 100644 --- a/arch/riscv/include/csr.h +++ b/arch/riscv64/include/csr.h @@ -49,15 +49,15 @@ #endif #define csr_read(csr, res)\ - __asm__ ("csrr %0, " __ASM_STR(csr) : "=r" (res) : : "memory") + __asm__ volatile ("csrr %0, " __ASM_STR(csr) : "=r" (res) : : "memory") #define csr_write(csr, val)\ - __asm__ ("csrw " __ASM_STR(csr) ", %0" : : "rK" (val) : "memory") + __asm__ volatile ("csrw " __ASM_STR(csr) ", %0" : : "r" (val) : "memory") #define csr_set(csr, val)\ - __asm__ ("csrs " __ASM_STR(csr) ", %0" : : "rK" (val) : "memory") + __asm__ volatile ("csrs " __ASM_STR(csr) ", %0" : : "r" (val) : "memory") #define csr_clear(csr, val)\ - __asm__ ("csrc " __ASM_STR(csr) ", %0" : : "rK" (val) : "memory") + __asm__ volatile ("csrc " __ASM_STR(csr) ", %0" : : "r" (val) : "memory") #endif /* APOS_CSR_H */ diff --git a/arch/riscv/include/lock.h b/arch/riscv64/include/lock.h index 4f71344..4f71344 100644 --- a/arch/riscv/include/lock.h +++ b/arch/riscv64/include/lock.h diff --git a/arch/riscv/include/pages.h b/arch/riscv64/include/pages.h index 362499a..362499a 100644 --- a/arch/riscv/include/pages.h +++ b/arch/riscv64/include/pages.h diff --git a/arch/riscv/include/sbi.h b/arch/riscv64/include/sbi.h index 609849d..609849d 100644 --- a/arch/riscv/include/sbi.h +++ b/arch/riscv64/include/sbi.h diff --git a/arch/riscv/include/tcb.h b/arch/riscv64/include/tcb.h index d1e0717..d1e0717 100644 --- a/arch/riscv/include/tcb.h +++ b/arch/riscv64/include/tcb.h diff --git a/arch/riscv/include/vmem.h b/arch/riscv64/include/vmem.h index 228b24b..228b24b 100644 --- a/arch/riscv/include/vmem.h +++ b/arch/riscv64/include/vmem.h diff --git a/arch/riscv/init/init.c b/arch/riscv64/init/init.c index 23dcadd..7aecfb5 100644 --- a/arch/riscv/init/init.c +++ b/arch/riscv64/init/init.c @@ -60,11 +60,13 @@ void move_kernel() void init(void *fdt) { extern char *__init_end; + extern void jump_to_kernel(void *k, void *fdt); void (*kernel_main)(void *fdt) = (void (*)(void *))(VM_KERN); init_bootmem(); move_kernel(); __va_reg(sp); __va_reg(fp); __va_reg(gp); - kernel_main(__va(fdt)); + jump_to_kernel((void *)VM_KERN, __va(fdt)); + //kernel_main(__va(fdt)); } diff --git a/arch/riscv64/init/start.S b/arch/riscv64/init/start.S new file mode 100644 index 0000000..7842946 --- /dev/null +++ b/arch/riscv64/init/start.S @@ -0,0 +1,12 @@ +.section .init.start +.global _start +_start: +li sp, PM_STACK_TOP +call init + +.section .text +.global jump_to_kernel +jump_to_kernel: +mv a2, a0 // store kernel addr +mv a0, a1 // move fdt pointer to first argument +jr a2 // jump to kernel diff --git a/arch/riscv/ipc.txt b/arch/riscv64/ipc.txt index 21d758e..21d758e 100644 --- a/arch/riscv/ipc.txt +++ b/arch/riscv64/ipc.txt diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv64/kernel/cpu.c index 6ae4ee7..6ae4ee7 100644 --- a/arch/riscv/kernel/cpu.c +++ b/arch/riscv64/kernel/cpu.c diff --git a/arch/riscv/kernel/irq.c b/arch/riscv64/kernel/irq.c index f844dd1..f844dd1 100644 --- a/arch/riscv/kernel/irq.c +++ b/arch/riscv64/kernel/irq.c diff --git a/arch/riscv/kernel/main.c b/arch/riscv64/kernel/main.c index 9ce256e..9ce256e 100644 --- a/arch/riscv/kernel/main.c +++ b/arch/riscv64/kernel/main.c diff --git a/arch/riscv/kernel/pmem.c b/arch/riscv64/kernel/pmem.c index 45b8eeb..45b8eeb 100644 --- a/arch/riscv/kernel/pmem.c +++ b/arch/riscv64/kernel/pmem.c diff --git a/arch/riscv/kernel/proc.c b/arch/riscv64/kernel/proc.c index 6120c2d..b696912 100644 --- a/arch/riscv/kernel/proc.c +++ b/arch/riscv64/kernel/proc.c @@ -5,8 +5,8 @@ void jump_to_userspace(struct tcb *t, int argc, char **argv) { csr_write(CSR_SEPC, t->entry); - __asm__("mv sp, %0\n" : "=r" (t->proc_stack) :: "memory"); - __asm__("sret\n" ::: "memory"); + __asm__ volatile ("mv sp, %0\n" : "=r" (t->proc_stack) :: "memory"); + __asm__ volatile ("sret\n" ::: "memory"); } void return_to_userspace(struct tcb *t) diff --git a/arch/riscv/kernel/vmem.c b/arch/riscv64/kernel/vmem.c index 1f1f44a..bc1a74e 100644 --- a/arch/riscv/kernel/vmem.c +++ b/arch/riscv64/kernel/vmem.c @@ -125,12 +125,12 @@ void unmap_vmem(struct vm_branch *branch, vm_t vaddr) void flush_tlb() { - __asm__("sfence.vma %0\n" :: "rk" (cpu_id()) : "memory"); + __asm__ volatile ("sfence.vma %0\n" :: "r" (cpu_id()) : "memory"); } void flush_tlb_all() { - __asm__("sfence.vma\n" ::: "memory"); + __asm__ volatile ("sfence.vma\n" ::: "memory"); } static void start_vmem(struct vm_branch *branch, enum mm_mode m) diff --git a/arch/riscv/mm.txt b/arch/riscv64/mm.txt index e5c52cb..e5c52cb 100644 --- a/arch/riscv/mm.txt +++ b/arch/riscv64/mm.txt diff --git a/arch/riscv/proc.txt b/arch/riscv64/proc.txt index 29b2e92..29b2e92 100644 --- a/arch/riscv/proc.txt +++ b/arch/riscv64/proc.txt diff --git a/arch/riscv64/source.mk b/arch/riscv64/source.mk new file mode 100644 index 0000000..d1986d4 --- /dev/null +++ b/arch/riscv64/source.mk @@ -0,0 +1,17 @@ +KERNEL_LOCAL != echo arch/riscv64/kernel/*.[cS] +KERNEL_SOURCES += $(KERNEL_LOCAL) +INIT_SOURCES += arch/riscv64/init/*.[cS] + +CLEANUP_CMD := ./arch/riscv64/conf/rmimage.sh + +ARCH_FLAGS := -mcmodel=medany + +# llvm compilation doesn't seem to work, either bfd reads the object files wrong +# and outputs incorrect symbols or lld handles relocations in a dumb way. Only +# GCC compiles the kernel correctly at the moment (on RISCV, at least). I'll +# look into this later, but for now I'll just use GCC. The code does _compile_ +# with clang, it just won't link. +#LINKFLAGS := -fuse-ld=bfd + +run: + ./arch/riscv64/conf/mkimage.sh diff --git a/common/bytes.c b/common/bytes.c index af65568..a5b3c4a 100644 --- a/common/bytes.c +++ b/common/bytes.c @@ -3,11 +3,13 @@ #include <apos/bytes.h> #include <apos/builtin.h> +#undef __bswap16 __weak uint16_t __bswap16(uint16_t u) { return (u & 0xff00) >> 8 | (u & 0x00ff) << 8; } +#undef __bswap32 __weak uint32_t __bswap32(uint32_t u) { return (u & 0xff000000) >> 24 | @@ -16,6 +18,7 @@ __weak uint32_t __bswap32(uint32_t u) (u & 0x000000ff) << 24; } +#undef __bswap64 __weak uint64_t __bswap64(uint64_t u) { return (u & 0xff00000000000000ULL) >> 56 | diff --git a/common/initrd.c b/common/initrd.c index afce2ec..77ce091 100644 --- a/common/initrd.c +++ b/common/initrd.c @@ -6,7 +6,7 @@ #include <libfdt.h> /* GNU cpio, use POSIX 'newc' format */ -__packed struct cpio_header { +struct __packed cpio_header { char c_magic[6]; char c_ino[8]; char c_mode[8]; diff --git a/include/apos/elf.h b/include/apos/elf.h index 4fe74d1..c0de62e 100644 --- a/include/apos/elf.h +++ b/include/apos/elf.h @@ -31,7 +31,7 @@ #define EM_RISCV 0xf3 -__packed struct elf_ident { +struct __packed elf_ident { uint32_t ei_magic; uint8_t ei_class; uint8_t ei_data; @@ -41,7 +41,7 @@ __packed struct elf_ident { uint8_t ei_pad[7]; }; -__packed struct elf32_header { +struct __packed elf32_header { struct elf_ident e_ident; uint16_t e_type; uint16_t e_machine; @@ -58,7 +58,7 @@ __packed struct elf32_header { uint16_t e_shstrndx; }; -__packed struct elf64_header { +struct __packed elf64_header { struct elf_ident e_ident; uint16_t e_type; uint16_t e_machine; @@ -92,7 +92,7 @@ __packed struct elf64_header { #define PF_W (1 << 1) #define PF_R (1 << 2) -__packed struct program32_header { +struct __packed program32_header { uint32_t p_type; uint32_t p_offset; uint32_t p_vaddr; @@ -103,7 +103,7 @@ __packed struct program32_header { uint32_t p_align; }; -__packed struct program64_header { +struct __packed program64_header { uint32_t p_type; uint32_t p_flags; uint64_t p_offset; @@ -146,7 +146,7 @@ __packed struct program64_header { #define SHF_MASKOS 0x0ff00000 #define SHF_MASKPROC 0xf0000000 -__packed struct section32_header { +struct __packed section32_header { uint32_t sh_name; uint32_t sh_type; uint32_t sh_flags; @@ -159,7 +159,7 @@ __packed struct section32_header { uint32_t sh_entsize; }; -__packed struct section64_header { +struct __packed section64_header { uint32_t sh_name; uint32_t sh_type; uint64_t sh_flags; |
