Skip to content

Commit

Permalink
feat(clang): add clang support for riscv64 and aarch64
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Sa <bruno.vilaca.sa@gmail.com>
  • Loading branch information
ninolomata committed Jul 20, 2023
1 parent 03eeaf1 commit dbab11e
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 23 deletions.
52 changes: 42 additions & 10 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,16 @@ 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

# List existing submakes
submakes:=config
Expand Down Expand Up @@ -158,23 +179,34 @@ 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)

override ASFLAGS+=$(CFLAGS) $(arch-asflags) $(platform-asflags)
ifeq ($(COMPILER_IS_CLANG), y)
override CFLAGS+=-Wno-unused-command-line-argument --target=$(arch-target) -fuse-ld=lld
override CPPFLAGS+=--target=$(arch-target)
override LDFLAGS+=--no-check-sections
endif

override ASFLAGS+=$(CFLAGS) $(arch-asflags) $(platform-asflags) $(debug_flags)
override LDFLAGS+=-build-id=none -nostdlib --fatal-warnings \
-z common-page-size=$(PAGE_SIZE) -z max-page-size=$(PAGE_SIZE) \
$(arch-ldflags) $(platform-ldflags)
Expand Down Expand Up @@ -228,8 +260,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 @@ -246,7 +278,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 @@ -255,7 +287,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
1 change: 1 addition & 0 deletions src/arch/armv8/aarch64/arch_sub.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ CROSS_COMPILE ?= aarch64-none-elf-

arch-cppflags+=-DAARCH64
arch-cflags+= -mcmodel=large -mstrict-align
arch-target += aarch64-none-elf
arch-asflags+=
arch-ldflags+=
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 @@ -180,7 +180,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 @@ -189,7 +191,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 @@ -198,7 +202,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 @@ -225,6 +231,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


5 changes: 4 additions & 1 deletion 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 @@ -315,5 +317,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
3 changes: 1 addition & 2 deletions src/arch/armv8/vgic.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ bool vgic_add_lr(struct vcpu *vcpu, struct vgic_int *interrupt)
if (lr_ind < 0) {
unsigned min_prio_pend = interrupt->prio, min_prio_act = interrupt->prio;
unsigned min_id_act = interrupt->id, min_id_pend = interrupt->id;
size_t pend_found = 0, act_found = 0;
size_t pend_found = 0;
ssize_t pend_ind = -1, act_ind = -1;

for (size_t i = 0; i < NUM_LRS; i++) {
Expand All @@ -348,7 +348,6 @@ bool vgic_add_lr(struct vcpu *vcpu, struct vgic_int *interrupt)
min_prio_act = lr_prio;
act_ind = i;
}
act_found++;
} else if (lr_state & GICH_LR_STATE_PND) {
if (lr_prio > min_prio_pend ||
(lr_prio == min_prio_pend && lr_id > min_id_pend)) {
Expand Down
1 change: 1 addition & 0 deletions src/arch/riscv/arch.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ arch-cppflags =
arch-cflags = -mcmodel=medany -march=rv64g -mstrict-align
arch-asflags =
arch-ldflags =
arch-target = riscv64

arch_mem_prot:=mmu
PAGE_SIZE:=0x1000
3 changes: 2 additions & 1 deletion 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,7 +95,7 @@ _reset_handler:

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

/**
* Setup stvec early. In case of we cause an exception in this boot code
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
10 changes: 8 additions & 2 deletions src/arch/riscv/sbi.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,20 @@ struct sbiret sbi_ipi_handler(unsigned long fid)

unsigned long hart_mask = vcpu_readreg(cpu()->vcpu, REG_A0);
unsigned long hart_mask_base = vcpu_readreg(cpu()->vcpu, REG_A1);
// Temporary workaround on the bitmap library.
// In clang, strange behaviour happens when converting from unsigned long to a array of bitmap
bitmap_t bit_hart_mask[sizeof(hart_mask)/sizeof(bitmap_t)] = {};

for (size_t i = 0; i < sizeof(hart_mask)/sizeof(bitmap_t); i++) {
bit_hart_mask[i] = hart_mask >> (i*sizeof(bitmap_t)*8);
}

struct cpu_msg msg = {
.handler = SBI_MSG_ID,
.event = SEND_IPI,
};

for (size_t i = 0; i < sizeof(hart_mask) * 8; i++) {
if (bitmap_get((bitmap_t*)&hart_mask, i)) {
if (bitmap_get((bitmap_t*)&bit_hart_mask, i)) {
vcpuid_t vhart_id = hart_mask_base + i;
cpuid_t phart_id = vm_translate_to_pcpuid(cpu()->vcpu->vm, vhart_id);
if(phart_id != INVALID_CPUID) cpu_send_msg(phart_id, &msg);
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

0 comments on commit dbab11e

Please sign in to comment.