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