1# This file is included by the global makefile so that you can add your own 2# architecture-specific flags and dependencies. 3# 4# This file is subject to the terms and conditions of the GNU General Public 5# License. See the file "COPYING" in the main directory of this archive 6# for more details. 7# 8 9LDFLAGS_vmlinux := -z norelro 10ifeq ($(CONFIG_RELOCATABLE),y) 11 LDFLAGS_vmlinux += -shared -Bsymbolic -z notext 12 KBUILD_CFLAGS += -fPIE 13endif 14ifeq ($(CONFIG_DYNAMIC_FTRACE),y) 15 LDFLAGS_vmlinux += --no-relax 16 KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY 17ifeq ($(CONFIG_RISCV_ISA_C),y) 18 CC_FLAGS_FTRACE := -fpatchable-function-entry=8,4 19else 20 CC_FLAGS_FTRACE := -fpatchable-function-entry=4,2 21endif 22endif 23 24ifeq ($(CONFIG_CMODEL_MEDLOW),y) 25KBUILD_CFLAGS_MODULE += -mcmodel=medany 26endif 27 28export BITS 29ifeq ($(CONFIG_ARCH_RV64I),y) 30 BITS := 64 31 32 KBUILD_CFLAGS += -mabi=lp64 33 KBUILD_AFLAGS += -mabi=lp64 34 35 KBUILD_LDFLAGS += -melf64lriscv 36 37 KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \ 38 -Cno-redzone 39else 40 BITS := 32 41 42 KBUILD_CFLAGS += -mabi=ilp32 43 KBUILD_AFLAGS += -mabi=ilp32 44 KBUILD_LDFLAGS += -melf32lriscv 45endif 46 47UTS_MACHINE := riscv$(BITS) 48 49# LLVM has an issue with target-features and LTO: https://github.com/llvm/llvm-project/issues/59350 50# Ensure it is aware of linker relaxation with LTO, otherwise relocations may 51# be incorrect: https://github.com/llvm/llvm-project/issues/65090 52ifeq ($(CONFIG_LTO_CLANG),y) 53 KBUILD_LDFLAGS += -mllvm -mattr=+c -mllvm -mattr=+relax 54endif 55 56ifeq ($(CONFIG_SHADOW_CALL_STACK),y) 57 KBUILD_LDFLAGS += --no-relax-gp 58endif 59 60# ISA string setting 61riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima 62riscv-march-$(CONFIG_ARCH_RV64I) := rv64ima 63riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd 64riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c 65riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v 66 67ifneq ($(CONFIG_RISCV_ISA_C),y) 68 KBUILD_RUSTFLAGS += -Ctarget-feature=-c 69endif 70 71ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC 72KBUILD_CFLAGS += -Wa,-misa-spec=2.2 73KBUILD_AFLAGS += -Wa,-misa-spec=2.2 74else 75riscv-march-$(CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI) := $(riscv-march-y)_zicsr_zifencei 76endif 77 78KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') 79export KBUILD_BASE_ISA 80 81# Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by 82# matching non-v and non-multi-letter extensions out with the filter ([^v_]*) 83KBUILD_CFLAGS += $(KBUILD_BASE_ISA) 84 85KBUILD_AFLAGS += -march=$(riscv-march-y) 86 87# For C code built with floating-point support, exclude V but keep F and D. 88CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/') 89 90KBUILD_CFLAGS += -mno-save-restore 91 92ifeq ($(CONFIG_CMODEL_MEDLOW),y) 93 KBUILD_CFLAGS += -mcmodel=medlow 94endif 95ifeq ($(CONFIG_CMODEL_MEDANY),y) 96 KBUILD_CFLAGS += -mcmodel=medany 97endif 98 99# Avoid generating .eh_frame sections. 100KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables 101 102# The RISC-V attributes frequently cause compatibility issues and provide no 103# information, so just turn them off. 104KBUILD_CFLAGS += $(call cc-option,-mno-riscv-attribute) 105KBUILD_AFLAGS += $(call cc-option,-mno-riscv-attribute) 106KBUILD_CFLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr) 107KBUILD_AFLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr) 108 109KBUILD_CFLAGS_MODULE += $(call cc-option,-mno-relax) 110KBUILD_AFLAGS_MODULE += $(call as-option,-Wa$(comma)-mno-relax) 111 112# GCC versions that support the "-mstrict-align" option default to allowing 113# unaligned accesses. While unaligned accesses are explicitly allowed in the 114# RISC-V ISA, they're emulated by machine mode traps on all extant 115# architectures. It's faster to have GCC emit only aligned accesses. 116ifneq ($(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS),y) 117KBUILD_CFLAGS += $(call cc-option,-mstrict-align) 118endif 119 120ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y) 121prepare: stack_protector_prepare 122stack_protector_prepare: prepare0 123 $(eval KBUILD_CFLAGS += -mstack-protector-guard=tls \ 124 -mstack-protector-guard-reg=tp \ 125 -mstack-protector-guard-offset=$(shell \ 126 awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}' \ 127 $(objtree)/include/generated/asm-offsets.h)) 128endif 129 130# arch specific predefines for sparse 131CHECKFLAGS += -D__riscv -D__riscv_xlen=$(BITS) 132 133# Default target when executing plain make 134boot := arch/riscv/boot 135boot-image-y := Image 136boot-image-$(CONFIG_KERNEL_BZIP2) := Image.bz2 137boot-image-$(CONFIG_KERNEL_GZIP) := Image.gz 138boot-image-$(CONFIG_KERNEL_LZ4) := Image.lz4 139boot-image-$(CONFIG_KERNEL_LZMA) := Image.lzma 140boot-image-$(CONFIG_KERNEL_LZO) := Image.lzo 141boot-image-$(CONFIG_KERNEL_ZSTD) := Image.zst 142boot-image-$(CONFIG_KERNEL_XZ) := Image.xz 143ifdef CONFIG_RISCV_M_MODE 144boot-image-$(CONFIG_SOC_CANAAN_K210) := loader.bin 145endif 146boot-image-$(CONFIG_EFI_ZBOOT) := vmlinuz.efi 147KBUILD_IMAGE := $(boot)/$(boot-image-y) 148 149libs-y += arch/riscv/lib/ 150libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a 151 152ifeq ($(KBUILD_EXTMOD),) 153ifeq ($(CONFIG_MMU),y) 154prepare: vdso_prepare 155vdso_prepare: prepare0 156 $(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso include/generated/vdso-offsets.h 157 $(if $(CONFIG_RISCV_USER_CFI),$(Q)$(MAKE) \ 158 $(build)=arch/riscv/kernel/vdso_cfi include/generated/vdso-cfi-offsets.h) 159 $(if $(CONFIG_COMPAT),$(Q)$(MAKE) \ 160 $(build)=arch/riscv/kernel/compat_vdso include/generated/compat_vdso-offsets.h) 161 162endif 163endif 164 165vdso-install-$(CONFIG_MMU) += arch/riscv/kernel/vdso/vdso.so.dbg 166vdso-install-$(CONFIG_RISCV_USER_CFI) += arch/riscv/kernel/vdso_cfi/vdso-cfi.so.dbg 167vdso-install-$(CONFIG_COMPAT) += arch/riscv/kernel/compat_vdso/compat_vdso.so.dbg 168 169BOOT_TARGETS := Image Image.gz Image.bz2 Image.lz4 Image.lzma Image.lzo Image.zst Image.xz loader loader.bin vmlinuz.efi 170 171all: $(notdir $(KBUILD_IMAGE)) 172 173loader.bin: loader 174Image.gz Image.bz2 Image.lz4 Image.lzma Image.lzo Image.zst Image.xz loader vmlinuz.efi: Image 175 176$(BOOT_TARGETS): vmlinux 177 $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@ 178 @$(kecho) ' Kernel: $(boot)/$@ is ready' 179 180# the install target always installs KBUILD_IMAGE (which may be compressed) 181# but keep the zinstall target for compatibility with older releases 182install zinstall: 183 $(call cmd,install) 184 185PHONY += rv32_randconfig 186rv32_randconfig: 187 $(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/32-bit.config \ 188 -f $(srctree)/Makefile randconfig 189 190PHONY += rv64_randconfig 191rv64_randconfig: 192 $(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/64-bit.config \ 193 -f $(srctree)/Makefile randconfig 194 195PHONY += rv32_defconfig 196rv32_defconfig: 197 $(Q)$(MAKE) -f $(srctree)/Makefile defconfig 32-bit.config 198 199PHONY += rv32_nommu_virt_defconfig 200rv32_nommu_virt_defconfig: 201 $(Q)$(MAKE) -f $(srctree)/Makefile nommu_virt_defconfig 32-bit.config 202 203define archhelp 204 echo ' Image - Uncompressed kernel image (arch/riscv/boot/Image)' 205 echo ' Image.gz - Compressed kernel image (arch/riscv/boot/Image.gz)' 206 echo ' Image.bz2 - Compressed kernel image (arch/riscv/boot/Image.bz2)' 207 echo ' Image.lz4 - Compressed kernel image (arch/riscv/boot/Image.lz4)' 208 echo ' Image.lzma - Compressed kernel image (arch/riscv/boot/Image.lzma)' 209 echo ' Image.lzo - Compressed kernel image (arch/riscv/boot/Image.lzo)' 210 echo ' Image.zst - Compressed kernel image (arch/riscv/boot/Image.zst)' 211 echo ' Image.xz - Compressed kernel image (arch/riscv/boot/Image.xz)' 212 echo ' vmlinuz.efi - Compressed EFI kernel image (arch/riscv/boot/vmlinuz.efi)' 213 echo ' Default when CONFIG_EFI_ZBOOT=y' 214 echo ' install - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or' 215 echo ' (distribution) /sbin/$(INSTALLKERNEL) or install to ' 216 echo ' $$(INSTALL_PATH)' 217endef 218