Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clang): add clang support for riscv64 and aarch64 #77

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 46 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,26 @@ define current_directory
$(realpath $(dir $(lastword $(MAKEFILE_LIST))))
endef

#Makefile arguments and default values
DEBUG:=y
OPTIMIZATIONS:=2
CONFIG=
PLATFORM=
LLVM?=

# Setup toolchain macros

ifneq ($(LLVM),)
cpp= $(LLVM)/clang-cpp
sstrip= $(LLVM)/llvm-strip
cc= $(LLVM)/clang
ld = $(LLVM)/ld.lld
as= $(LLVM)/llvm-as
objcopy= $(LLVM)/llvm-objcopy
objdump= $(LLVM)/llvm-objdump
readelf= $(LLVM)/llvm-readelf
size= $(LLVM)/llvm-size
else
cpp= $(CROSS_COMPILE)cpp
sstrip= $(CROSS_COMPILE)strip
cc= $(CROSS_COMPILE)gcc
Expand All @@ -21,14 +40,21 @@ objcopy= $(CROSS_COMPILE)objcopy
objdump= $(CROSS_COMPILE)objdump
readelf= $(CROSS_COMPILE)readelf
size= $(CROSS_COMPILE)size
endif

HOST_CC:=gcc

#Makefile arguments and default values
DEBUG:=y
OPTIMIZATIONS:=2
CONFIG=
PLATFORM=
# Check if CC is clang
ifneq ($(LLVM),)
COMPILER_IS_CLANG = y
else
COMPILER_IS_CLANG = n
endif

# Parse CLANG target
ifeq ($(COMPILER_IS_CLANG),y)
CLANG_ARCH_TARGET := $(patsubst %-,%,$(lastword $(subst /, ,$(CROSS_COMPILE))))
endif

# Setup version

Expand Down Expand Up @@ -170,22 +196,33 @@ objs-y+=$(config_obj)
build_macros:=
ifeq ($(arch_mem_prot),mmu)
build_macros+=-DMEM_PROT_MMU
build_macros+=-DCOMPILER_IS_CLANG
endif
ifeq ($(arch_mem_prot),mpu)
build_macros+=-DMEM_PROT_MPU
build_macros+=-DCOMPILER_IS_CLANG
endif

override CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
$(platform-cppflags) $(build_macros)
vpath:.=CPPFLAGS

override HOST_CPPFLAGS+=$(addprefix -I, $(inc_dirs)) $(arch-cppflags) \
$(platform-cppflags) $(build_macros)

ifeq ($(DEBUG), y)
debug_flags:=-g
endif

override CFLAGS+=-O$(OPTIMIZATIONS) -Wall -Werror -ffreestanding -std=gnu11 \
-fno-pic $(arch-cflags) $(platform-cflags) $(CPPFLAGS) $(debug_flags)

ifeq ($(COMPILER_IS_CLANG), y)
override CFLAGS+=-Wno-unused-command-line-argument --target=$(CLANG_ARCH_TARGET)
override CPPFLAGS+=--target=$(CLANG_ARCH_TARGET)
override LDFLAGS+=--no-check-sections
endif

override ASFLAGS+=$(CFLAGS) $(arch-asflags) $(platform-asflags)

override LDFLAGS+=-build-id=none -nostdlib --fatal-warnings \
Expand Down Expand Up @@ -244,8 +281,8 @@ ifneq ($(wildcard $(asm_defs_src)),)
$(asm_defs_hdr): $(asm_defs_src)
@echo "Generating header $(patsubst $(cur_dir)/%, %, $@)"
@$(cc) -S $(CFLAGS) -DGENERATING_DEFS $< -o - \
| awk '($$1 == "->") \
{ gsub("#", "", $$3); print "#define " $$2 " " $$3 }' > $@
| awk '($$1 == "//#" || $$1 == "##") \
{print "#define " $$2 " " $$3 }' > $@

$(asm_defs_hdr).d: $(asm_defs_src)
@echo "Creating dependency $(patsubst $(cur_dir)/%, %,\
Expand All @@ -262,7 +299,7 @@ $(config_dep): $(config_src)

$(config_def_generator): $(config_def_generator_src) $(config_src)
@echo "Compiling generator $(patsubst $(cur_dir)/%, %, $@)"
@$(HOST_CC) $^ $(build_macros) $(CPPFLAGS) -DGENERATING_DEFS \
@$(HOST_CC) $^ $(build_macros) $(HOST_CPPFLAGS) -DGENERATING_DEFS \
$(addprefix -I, $(inc_dirs)) -o $@

$(config_defs): $(config_def_generator)
Expand All @@ -271,7 +308,7 @@ $(config_defs): $(config_def_generator)

$(platform_def_generator): $(platform_def_generator_src) $(platform_description)
@echo "Compiling generator $(patsubst $(cur_dir)/%, %, $@)"
@$(HOST_CC) $^ $(build_macros) $(CPPFLAGS) -DGENERATING_DEFS -D$(ARCH) \
@$(HOST_CC) $^ $(build_macros) $(HOST_CPPFLAGS) -DGENERATING_DEFS -D$(ARCH) \
$(addprefix -I, $(inc_dirs)) -o $@

$(platform_defs): $(platform_def_generator)
Expand Down
10 changes: 9 additions & 1 deletion src/arch/armv8/aarch64/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ _reset_handler:
* description this early in the initialization.
*/

mov x3, x0, lsr #8
lsr x3, x0, #8
and x3, x3, 0xff
adr x4, platform
ldr x4, [x4, PLAT_ARCH_OFF+PLAT_ARCH_CLUSTERS_OFF+PLAT_CLUSTERS_CORES_NUM_OFF]
Expand Down Expand Up @@ -183,7 +183,9 @@ _set_master_cpu:
/***** Helper functions for boot code. ******/

.global boot_clear
#ifndef COMPILER_IS_CLANG
.func boot_clear
#endif
boot_clear:
2:
cmp x16, x17
Expand All @@ -192,7 +194,9 @@ boot_clear:
b 2b
1:
ret
#ifndef COMPILER_IS_CLANG
.endfunc
#endif

/*
* Code taken from "Application Note Bare-metal Boot Code for ARMv8-A
Expand All @@ -201,7 +205,9 @@ boot_clear:
* x0 - cache level to be invalidated (0 - dl1$, 1 - il1$, 2 - l2$)
*/
.global boot_cache_invalidate
#ifndef COMPILER_IS_CLANG
.func boot_cache_invalidate
#endif
boot_cache_invalidate:
msr csselr_el1, x0
mrs x4, ccsidr_el1 // read cache size id.
Expand All @@ -228,6 +234,8 @@ set_loop:
cmp x5, x3 // last way reached yet?
ble way_loop // if not, iterate way_loop
ret
#ifndef COMPILER_IS_CLANG
.endfunc
#endif


4 changes: 4 additions & 0 deletions src/arch/armv8/armv8-a/aarch64/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ _enter_vas:
ret

.global psci_boot_entry
#ifndef COMPILER_IS_CLANG
.func psci_boot_entry
#endif
psci_boot_entry:
warm_boot:

Expand Down Expand Up @@ -316,4 +318,6 @@ _enter_vas_warm:
bl psci_wake
b .

#ifndef COMPILER_IS_CLANG
.endfunc
#endif
2 changes: 1 addition & 1 deletion src/arch/armv8/armv8-a/pagetables.S
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <arch/page_table.h>

.section .glb_page_tables, "aw"
.section .glb_page_tables, "aw", @nobits

.globl root_l1_pt
.balign PAGE_SIZE, 0
Expand Down
2 changes: 2 additions & 0 deletions src/arch/riscv/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ _dmem_beg_sym: .8byte _dmem_beg
_enter_vas_sym: .8byte _enter_vas
_bss_start_sym: .8byte _bss_start
_bss_end_sym: .8byte _bss_end
_extra_allocated_phys_mem_sym: .8byte extra_allocated_phys_mem

.data
.align 3
Expand Down Expand Up @@ -94,6 +95,7 @@ _reset_handler:

mv a2, a1
la a1, _image_start
LD_SYM s6, _extra_allocated_phys_mem_sym
la s6, extra_allocated_phys_mem

/**
Expand Down
2 changes: 1 addition & 1 deletion src/arch/riscv/root_pt.S
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <arch/page_table.h>

.section .glb_page_tables, "aw"
.section .glb_page_tables, "aw",@nobits

.globl root_l1_pt
.balign PAGE_SIZE, 0
Expand Down
5 changes: 3 additions & 2 deletions src/core/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ OBJPOOL_ALLOC(msg_pool, struct cpu_msg_node, CPU_MSG_POOL_SIZE);
struct cpu_synctoken cpu_glb_sync = {.ready = false};

extern cpu_msg_handler_t ipi_cpumsg_handlers[];
extern uint8_t _ipi_cpumsg_handlers_size;
extern size_t _ipi_cpumsg_handlers_size;
extern size_t _ipi_cpumsg_handlers_id_start[];
size_t ipi_cpumsg_handler_num;
size_t __ipi_cpumsg_handlers_size_sym = (size_t ) &_ipi_cpumsg_handlers_size;

struct cpuif cpu_interfaces[PLAT_CPU_NUM];

Expand All @@ -45,7 +46,7 @@ void cpu_init(cpuid_t cpu_id, paddr_t load_addr)
cpu_sync_init(&cpu_glb_sync, platform.cpu_num);

ipi_cpumsg_handler_num =
((size_t)&_ipi_cpumsg_handlers_size) / sizeof(cpu_msg_handler_t);
(__ipi_cpumsg_handlers_size_sym) / sizeof(cpu_msg_handler_t);
for (size_t i = 0; i < ipi_cpumsg_handler_num; i++) {
((size_t*)_ipi_cpumsg_handlers_id_start)[i] = i;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/inc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
#ifndef __ASSEMBLER__

#define DEFINE_OFFSET(SYMBOL, STRUCT, FIELD) \
asm volatile("\n-> " XSTR(SYMBOL) " %0 \n" : : "i"(offsetof(STRUCT, FIELD)))
asm volatile("\n## " XSTR(SYMBOL) " %0 \n" : : "i"(offsetof(STRUCT, FIELD)))

#define DEFINE_SIZE(SYMBOL, TYPE) \
asm volatile("\n-> " XSTR(SYMBOL) " %0 \n" : : "i"(sizeof(TYPE)))
asm volatile("\n## " XSTR(SYMBOL) " %0 \n" : : "i"(sizeof(TYPE)))

#define max(n1, n2) (((n1) > (n2)) ? (n1) : (n2))
#define min(n1, n2) (((n1) < (n2)) ? (n1) : (n2))
Expand Down