Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Unified Makefile for i386 and x86_64
  3. # select defconfig based on actual architecture
  4. ifeq ($(ARCH),x86)
  5. ifeq ($(shell uname -m),x86_64)
  6. KBUILD_DEFCONFIG := x86_64_defconfig
  7. else
  8. KBUILD_DEFCONFIG := i386_defconfig
  9. endif
  10. else
  11. KBUILD_DEFCONFIG := $(ARCH)_defconfig
  12. endif
  13. ifdef CONFIG_CC_IS_GCC
  14. RETPOLINE_CFLAGS := $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
  15. RETPOLINE_VDSO_CFLAGS := $(call cc-option,-mindirect-branch=thunk-inline -mindirect-branch-register)
  16. endif
  17. ifdef CONFIG_CC_IS_CLANG
  18. RETPOLINE_CFLAGS := -mretpoline-external-thunk
  19. RETPOLINE_VDSO_CFLAGS := -mretpoline
  20. endif
  21. RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix)
  22. ifdef CONFIG_RETHUNK
  23. RETHUNK_CFLAGS := -mfunction-return=thunk-extern
  24. RETPOLINE_CFLAGS += $(RETHUNK_CFLAGS)
  25. endif
  26. export RETHUNK_CFLAGS
  27. export RETPOLINE_CFLAGS
  28. export RETPOLINE_VDSO_CFLAGS
  29. # For gcc stack alignment is specified with -mpreferred-stack-boundary,
  30. # clang has the option -mstack-alignment for that purpose.
  31. ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
  32. cc_stack_align4 := -mpreferred-stack-boundary=2
  33. cc_stack_align8 := -mpreferred-stack-boundary=3
  34. else ifneq ($(call cc-option, -mstack-alignment=16),)
  35. cc_stack_align4 := -mstack-alignment=4
  36. cc_stack_align8 := -mstack-alignment=8
  37. endif
  38. # How to compile the 16-bit code. Note we always compile for -march=i386;
  39. # that way we can complain to the user if the CPU is insufficient.
  40. REALMODE_CFLAGS := -m16 -g -Os -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \
  41. -Wall -Wstrict-prototypes -march=i386 -mregparm=3 \
  42. -fno-strict-aliasing -fomit-frame-pointer -fno-pic \
  43. -mno-mmx -mno-sse $(call cc-option,-fcf-protection=none)
  44. REALMODE_CFLAGS += -ffreestanding
  45. REALMODE_CFLAGS += -fno-stack-protector
  46. REALMODE_CFLAGS += -Wno-address-of-packed-member
  47. REALMODE_CFLAGS += $(cc_stack_align4)
  48. REALMODE_CFLAGS += $(CLANG_FLAGS)
  49. export REALMODE_CFLAGS
  50. # BITS is used as extension for files which are available in a 32 bit
  51. # and a 64 bit version to simplify shared Makefiles.
  52. # e.g.: obj-y += foo_$(BITS).o
  53. export BITS
  54. #
  55. # Prevent GCC from generating any FP code by mistake.
  56. #
  57. # This must happen before we try the -mpreferred-stack-boundary, see:
  58. #
  59. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383
  60. #
  61. KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx
  62. KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2
  63. ifeq ($(CONFIG_X86_KERNEL_IBT),y)
  64. #
  65. # Kernel IBT has S_CET.NOTRACK_EN=0, as such the compilers must not generate
  66. # NOTRACK prefixes. Current generation compilers unconditionally employ NOTRACK
  67. # for jump-tables, as such, disable jump-tables for now.
  68. #
  69. # (jump-tables are implicitly disabled by RETPOLINE)
  70. #
  71. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104816
  72. #
  73. KBUILD_CFLAGS += $(call cc-option,-fcf-protection=branch -fno-jump-tables)
  74. else
  75. KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
  76. endif
  77. ifeq ($(CONFIG_X86_32),y)
  78. BITS := 32
  79. UTS_MACHINE := i386
  80. CHECKFLAGS += -D__i386__
  81. KBUILD_AFLAGS += -m32
  82. KBUILD_CFLAGS += -m32
  83. KBUILD_CFLAGS += -msoft-float -mregparm=3 -freg-struct-return
  84. # Never want PIC in a 32-bit kernel, prevent breakage with GCC built
  85. # with nonstandard options
  86. KBUILD_CFLAGS += -fno-pic
  87. # Align the stack to the register width instead of using the default
  88. # alignment of 16 bytes. This reduces stack usage and the number of
  89. # alignment instructions.
  90. KBUILD_CFLAGS += $(cc_stack_align4)
  91. # CPU-specific tuning. Anything which can be shared with UML should go here.
  92. include $(srctree)/arch/x86/Makefile_32.cpu
  93. KBUILD_CFLAGS += $(cflags-y)
  94. # temporary until string.h is fixed
  95. KBUILD_CFLAGS += -ffreestanding
  96. ifeq ($(CONFIG_STACKPROTECTOR),y)
  97. ifeq ($(CONFIG_SMP),y)
  98. KBUILD_CFLAGS += -mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=__stack_chk_guard
  99. else
  100. KBUILD_CFLAGS += -mstack-protector-guard=global
  101. endif
  102. endif
  103. else
  104. BITS := 64
  105. UTS_MACHINE := x86_64
  106. CHECKFLAGS += -D__x86_64__
  107. KBUILD_AFLAGS += -m64
  108. KBUILD_CFLAGS += -m64
  109. # Align jump targets to 1 byte, not the default 16 bytes:
  110. KBUILD_CFLAGS += $(call cc-option,-falign-jumps=1)
  111. # Pack loops tightly as well:
  112. KBUILD_CFLAGS += $(call cc-option,-falign-loops=1)
  113. # Don't autogenerate traditional x87 instructions
  114. KBUILD_CFLAGS += -mno-80387
  115. KBUILD_CFLAGS += $(call cc-option,-mno-fp-ret-in-387)
  116. # By default gcc and clang use a stack alignment of 16 bytes for x86.
  117. # However the standard kernel entry on x86-64 leaves the stack on an
  118. # 8-byte boundary. If the compiler isn't informed about the actual
  119. # alignment it will generate extra alignment instructions for the
  120. # default alignment which keep the stack *mis*aligned.
  121. # Furthermore an alignment to the register width reduces stack usage
  122. # and the number of alignment instructions.
  123. KBUILD_CFLAGS += $(cc_stack_align8)
  124. # Use -mskip-rax-setup if supported.
  125. KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)
  126. # FIXME - should be integrated in Makefile.cpu (Makefile_32.cpu)
  127. cflags-$(CONFIG_MK8) += -march=k8
  128. cflags-$(CONFIG_MPSC) += -march=nocona
  129. cflags-$(CONFIG_MCORE2) += -march=core2
  130. cflags-$(CONFIG_MATOM) += -march=atom
  131. cflags-$(CONFIG_GENERIC_CPU) += -mtune=generic
  132. KBUILD_CFLAGS += $(cflags-y)
  133. rustflags-$(CONFIG_MK8) += -Ctarget-cpu=k8
  134. rustflags-$(CONFIG_MPSC) += -Ctarget-cpu=nocona
  135. rustflags-$(CONFIG_MCORE2) += -Ctarget-cpu=core2
  136. rustflags-$(CONFIG_MATOM) += -Ctarget-cpu=atom
  137. rustflags-$(CONFIG_GENERIC_CPU) += -Ztune-cpu=generic
  138. KBUILD_RUSTFLAGS += $(rustflags-y)
  139. KBUILD_CFLAGS += -mno-red-zone
  140. KBUILD_CFLAGS += -mcmodel=kernel
  141. KBUILD_RUSTFLAGS += -Cno-redzone=y
  142. KBUILD_RUSTFLAGS += -Ccode-model=kernel
  143. endif
  144. #
  145. # If the function graph tracer is used with mcount instead of fentry,
  146. # '-maccumulate-outgoing-args' is needed to prevent a GCC bug
  147. # (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42109)
  148. #
  149. ifdef CONFIG_FUNCTION_GRAPH_TRACER
  150. ifndef CONFIG_HAVE_FENTRY
  151. ACCUMULATE_OUTGOING_ARGS := 1
  152. endif
  153. endif
  154. ifeq ($(ACCUMULATE_OUTGOING_ARGS), 1)
  155. # This compiler flag is not supported by Clang:
  156. KBUILD_CFLAGS += $(call cc-option,-maccumulate-outgoing-args,)
  157. endif
  158. # Workaround for a gcc prelease that unfortunately was shipped in a suse release
  159. KBUILD_CFLAGS += -Wno-sign-compare
  160. #
  161. KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
  162. # Avoid indirect branches in kernel to deal with Spectre
  163. ifdef CONFIG_RETPOLINE
  164. KBUILD_CFLAGS += $(RETPOLINE_CFLAGS)
  165. # Additionally, avoid generating expensive indirect jumps which
  166. # are subject to retpolines for small number of switch cases.
  167. # clang turns off jump table generation by default when under
  168. # retpoline builds, however, gcc does not for x86. This has
  169. # only been fixed starting from gcc stable version 8.4.0 and
  170. # onwards, but not for older ones. See gcc bug #86952.
  171. ifndef CONFIG_CC_IS_CLANG
  172. KBUILD_CFLAGS += -fno-jump-tables
  173. endif
  174. endif
  175. ifdef CONFIG_SLS
  176. KBUILD_CFLAGS += -mharden-sls=all
  177. endif
  178. KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)
  179. ifdef CONFIG_LTO_CLANG
  180. ifeq ($(shell test $(CONFIG_LLD_VERSION) -lt 130000; echo $$?),0)
  181. KBUILD_LDFLAGS += -plugin-opt=-stack-alignment=$(if $(CONFIG_X86_32),4,8)
  182. endif
  183. endif
  184. ifdef CONFIG_X86_NEED_RELOCS
  185. LDFLAGS_vmlinux := --emit-relocs --discard-none
  186. else
  187. LDFLAGS_vmlinux :=
  188. endif
  189. #
  190. # The 64-bit kernel must be aligned to 2MB. Pass -z max-page-size=0x200000 to
  191. # the linker to force 2MB page size regardless of the default page size used
  192. # by the linker.
  193. #
  194. ifdef CONFIG_X86_64
  195. LDFLAGS_vmlinux += -z max-page-size=0x200000
  196. endif
  197. archscripts: scripts_basic
  198. $(Q)$(MAKE) $(build)=arch/x86/tools relocs
  199. ###
  200. # Syscall table generation
  201. archheaders:
  202. $(Q)$(MAKE) $(build)=arch/x86/entry/syscalls all
  203. ###
  204. # Kernel objects
  205. libs-y += arch/x86/lib/
  206. # drivers-y are linked after core-y
  207. drivers-$(CONFIG_MATH_EMULATION) += arch/x86/math-emu/
  208. drivers-$(CONFIG_PCI) += arch/x86/pci/
  209. # suspend and hibernation support
  210. drivers-$(CONFIG_PM) += arch/x86/power/
  211. drivers-$(CONFIG_FB) += arch/x86/video/
  212. ####
  213. # boot loader support. Several targets are kept for legacy purposes
  214. boot := arch/x86/boot
  215. BOOT_TARGETS = bzdisk fdimage fdimage144 fdimage288 hdimage isoimage
  216. PHONY += bzImage $(BOOT_TARGETS)
  217. # Don't compile Image in mixed build with "all" target
  218. ifndef KBUILD_MIXED_TREE
  219. # Default kernel to build
  220. all: bzImage
  221. endif
  222. # KBUILD_IMAGE specify target image being built
  223. KBUILD_IMAGE := $(boot)/bzImage
  224. bzImage: vmlinux
  225. ifeq ($(CONFIG_X86_DECODER_SELFTEST),y)
  226. $(Q)$(MAKE) $(build)=arch/x86/tools posttest
  227. endif
  228. $(Q)$(MAKE) $(build)=$(boot) $(KBUILD_IMAGE)
  229. $(Q)mkdir -p $(objtree)/arch/$(UTS_MACHINE)/boot
  230. $(Q)ln -fsn ../../x86/boot/bzImage $(objtree)/arch/$(UTS_MACHINE)/boot/$@
  231. $(BOOT_TARGETS): vmlinux
  232. $(Q)$(MAKE) $(build)=$(boot) $@
  233. PHONY += install
  234. install:
  235. $(call cmd,install)
  236. PHONY += vdso_install
  237. vdso_install:
  238. $(Q)$(MAKE) $(build)=arch/x86/entry/vdso $@
  239. archprepare: checkbin
  240. checkbin:
  241. ifdef CONFIG_RETPOLINE
  242. ifeq ($(RETPOLINE_CFLAGS),)
  243. @echo "You are building kernel with non-retpoline compiler." >&2
  244. @echo "Please update your compiler." >&2
  245. @false
  246. endif
  247. endif
  248. archclean:
  249. $(Q)rm -rf $(objtree)/arch/i386
  250. $(Q)rm -rf $(objtree)/arch/x86_64
  251. define archhelp
  252. echo '* bzImage - Compressed kernel image (arch/x86/boot/bzImage)'
  253. echo ' install - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or'
  254. echo ' (distribution) /sbin/$(INSTALLKERNEL) or install to '
  255. echo ' $$(INSTALL_PATH) and run lilo'
  256. echo ''
  257. echo ' fdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)'
  258. echo ' fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)'
  259. echo ' fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)'
  260. echo ' hdimage - Create a BIOS/EFI hard disk image (arch/x86/boot/hdimage)'
  261. echo ' isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)'
  262. echo ' bzdisk/fdimage*/hdimage/isoimage also accept:'
  263. echo ' FDARGS="..." arguments for the booted kernel'
  264. echo ' FDINITRD=file initrd for the booted kernel'
  265. echo ''
  266. echo ' kvm_guest.config - Enable Kconfig items for running this kernel as a KVM guest'
  267. echo ' xen.config - Enable Kconfig items for running this kernel as a Xen guest'
  268. echo ' x86_debug.config - Enable tip tree debugging options for testing'
  269. endef