Makefile 7.9 KB

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