Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 | sed -e 's/i.86/i386/'),i386)
  6. KBUILD_DEFCONFIG := i386_defconfig
  7. else
  8. KBUILD_DEFCONFIG := x86_64_defconfig
  9. endif
  10. else
  11. KBUILD_DEFCONFIG := $(ARCH)_defconfig
  12. endif
  13. ifdef CONFIG_CC_IS_GCC
  14. RETPOLINE_CFLAGS := -mindirect-branch=thunk-extern -mindirect-branch-register
  15. RETPOLINE_VDSO_CFLAGS := -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_MITIGATION_RETHUNK
  23. RETHUNK_CFLAGS := -mfunction-return=thunk-extern
  24. RETHUNK_RUSTFLAGS := -Zfunction-return=thunk-extern
  25. RETPOLINE_CFLAGS += $(RETHUNK_CFLAGS)
  26. RETPOLINE_RUSTFLAGS += $(RETHUNK_RUSTFLAGS)
  27. endif
  28. export RETHUNK_CFLAGS
  29. export RETHUNK_RUSTFLAGS
  30. export RETPOLINE_CFLAGS
  31. export RETPOLINE_RUSTFLAGS
  32. export RETPOLINE_VDSO_CFLAGS
  33. # For gcc stack alignment is specified with -mpreferred-stack-boundary,
  34. # clang has the option -mstack-alignment for that purpose.
  35. ifdef CONFIG_CC_IS_GCC
  36. cc_stack_align4 := -mpreferred-stack-boundary=2
  37. cc_stack_align8 := -mpreferred-stack-boundary=3
  38. endif
  39. ifdef CONFIG_CC_IS_CLANG
  40. cc_stack_align4 := -mstack-alignment=4
  41. cc_stack_align8 := -mstack-alignment=8
  42. endif
  43. # How to compile the 16-bit code. Note we always compile for -march=i386;
  44. # that way we can complain to the user if the CPU is insufficient.
  45. REALMODE_CFLAGS := -std=gnu11 -fms-extensions -m16 -g -Os \
  46. -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \
  47. -Wall -Wstrict-prototypes -march=i386 -mregparm=3 \
  48. -fno-strict-aliasing -fomit-frame-pointer -fno-pic \
  49. -mno-mmx -mno-sse $(call cc-option,-fcf-protection=none)
  50. REALMODE_CFLAGS += -ffreestanding
  51. REALMODE_CFLAGS += -fno-stack-protector
  52. REALMODE_CFLAGS += -Wno-address-of-packed-member
  53. REALMODE_CFLAGS += $(cc_stack_align4)
  54. REALMODE_CFLAGS += $(CLANG_FLAGS)
  55. ifdef CONFIG_CC_IS_CLANG
  56. REALMODE_CFLAGS += -Wno-gnu
  57. REALMODE_CFLAGS += -Wno-microsoft-anon-tag
  58. endif
  59. export REALMODE_CFLAGS
  60. # BITS is used as extension for files which are available in a 32 bit
  61. # and a 64 bit version to simplify shared Makefiles.
  62. # e.g.: obj-y += foo_$(BITS).o
  63. export BITS
  64. #
  65. # Prevent GCC from generating any FP code by mistake.
  66. #
  67. # This must happen before we try the -mpreferred-stack-boundary, see:
  68. #
  69. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383
  70. #
  71. KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -mno-sse4a
  72. KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json
  73. KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2
  74. #
  75. # CFLAGS for compiling floating point code inside the kernel.
  76. #
  77. CC_FLAGS_FPU := -msse -msse2
  78. ifdef CONFIG_CC_IS_GCC
  79. CC_FLAGS_FPU += -mhard-float
  80. endif
  81. ifeq ($(CONFIG_X86_KERNEL_IBT),y)
  82. #
  83. # Kernel IBT has S_CET.NOTRACK_EN=0, as such the compilers must not generate
  84. # NOTRACK prefixes. Current generation compilers unconditionally employ NOTRACK
  85. # for jump-tables, as such, disable jump-tables for now.
  86. #
  87. # (jump-tables are implicitly disabled by RETPOLINE)
  88. #
  89. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104816
  90. #
  91. KBUILD_CFLAGS += $(call cc-option,-fcf-protection=branch -fno-jump-tables)
  92. KBUILD_RUSTFLAGS += -Zcf-protection=branch $(if $(call rustc-min-version,109300),-Cjump-tables=n,-Zno-jump-tables)
  93. else
  94. KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
  95. endif
  96. ifeq ($(CONFIG_X86_32),y)
  97. BITS := 32
  98. UTS_MACHINE := i386
  99. CHECKFLAGS += -D__i386__
  100. KBUILD_AFLAGS += -m32
  101. KBUILD_CFLAGS += -m32
  102. KBUILD_CFLAGS += -msoft-float -mregparm=3 -freg-struct-return
  103. # Never want PIC in a 32-bit kernel, prevent breakage with GCC built
  104. # with nonstandard options
  105. KBUILD_CFLAGS += -fno-pic
  106. # Align the stack to the register width instead of using the default
  107. # alignment of 16 bytes. This reduces stack usage and the number of
  108. # alignment instructions.
  109. KBUILD_CFLAGS += $(cc_stack_align4)
  110. # CPU-specific tuning. Anything which can be shared with UML should go here.
  111. include $(srctree)/arch/x86/Makefile_32.cpu
  112. KBUILD_CFLAGS += $(cflags-y)
  113. ifneq ($(call clang-min-version, 160000),y)
  114. # https://github.com/llvm/llvm-project/issues/53645
  115. KBUILD_CFLAGS += -ffreestanding
  116. endif
  117. percpu_seg := fs
  118. else
  119. BITS := 64
  120. UTS_MACHINE := x86_64
  121. CHECKFLAGS += -D__x86_64__
  122. KBUILD_AFLAGS += -m64
  123. KBUILD_CFLAGS += -m64
  124. # Align jump targets to 1 byte, not the default 16 bytes:
  125. KBUILD_CFLAGS += $(call cc-option,-falign-jumps=1)
  126. # Pack loops tightly as well:
  127. KBUILD_CFLAGS += $(call cc-option,-falign-loops=1)
  128. # Don't autogenerate traditional x87 instructions
  129. KBUILD_CFLAGS += -mno-80387
  130. KBUILD_CFLAGS += -mno-fp-ret-in-387
  131. # By default gcc and clang use a stack alignment of 16 bytes for x86.
  132. # However the standard kernel entry on x86-64 leaves the stack on an
  133. # 8-byte boundary. If the compiler isn't informed about the actual
  134. # alignment it will generate extra alignment instructions for the
  135. # default alignment which keep the stack *mis*aligned.
  136. # Furthermore an alignment to the register width reduces stack usage
  137. # and the number of alignment instructions.
  138. KBUILD_CFLAGS += $(cc_stack_align8)
  139. # Use -mskip-rax-setup if supported.
  140. KBUILD_CFLAGS += -mskip-rax-setup
  141. ifdef CONFIG_X86_NATIVE_CPU
  142. KBUILD_CFLAGS += -march=native
  143. KBUILD_RUSTFLAGS += -Ctarget-cpu=native
  144. else
  145. KBUILD_CFLAGS += -march=x86-64 -mtune=generic
  146. KBUILD_RUSTFLAGS += -Ctarget-cpu=x86-64 -Ztune-cpu=generic
  147. endif
  148. KBUILD_CFLAGS += -mno-red-zone
  149. KBUILD_CFLAGS += -mcmodel=kernel
  150. KBUILD_RUSTFLAGS += -Cno-redzone=y
  151. KBUILD_RUSTFLAGS += -Ccode-model=kernel
  152. percpu_seg := gs
  153. endif
  154. ifeq ($(CONFIG_STACKPROTECTOR),y)
  155. ifeq ($(CONFIG_SMP),y)
  156. KBUILD_CFLAGS += -mstack-protector-guard-reg=$(percpu_seg)
  157. KBUILD_CFLAGS += -mstack-protector-guard-symbol=__ref_stack_chk_guard
  158. else
  159. KBUILD_CFLAGS += -mstack-protector-guard=global
  160. endif
  161. endif
  162. #
  163. # If the function graph tracer is used with mcount instead of fentry,
  164. # '-maccumulate-outgoing-args' is needed to prevent a GCC bug
  165. # (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42109)
  166. #
  167. ifdef CONFIG_FUNCTION_GRAPH_TRACER
  168. ifndef CONFIG_HAVE_FENTRY
  169. ACCUMULATE_OUTGOING_ARGS := 1
  170. endif
  171. endif
  172. ifeq ($(ACCUMULATE_OUTGOING_ARGS), 1)
  173. # This compiler flag is not supported by Clang:
  174. KBUILD_CFLAGS += $(call cc-option,-maccumulate-outgoing-args,)
  175. endif
  176. # Workaround for a gcc prelease that unfortunately was shipped in a suse release
  177. KBUILD_CFLAGS += -Wno-sign-compare
  178. #
  179. KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
  180. # Avoid indirect branches in kernel to deal with Spectre
  181. ifdef CONFIG_MITIGATION_RETPOLINE
  182. KBUILD_CFLAGS += $(RETPOLINE_CFLAGS)
  183. KBUILD_RUSTFLAGS += $(RETPOLINE_RUSTFLAGS)
  184. # Additionally, avoid generating expensive indirect jumps which
  185. # are subject to retpolines for small number of switch cases.
  186. # LLVM turns off jump table generation by default when under
  187. # retpoline builds, however, gcc does not for x86. This has
  188. # only been fixed starting from gcc stable version 8.4.0 and
  189. # onwards, but not for older ones. See gcc bug #86952.
  190. ifndef CONFIG_CC_IS_CLANG
  191. KBUILD_CFLAGS += -fno-jump-tables
  192. endif
  193. endif
  194. ifdef CONFIG_MITIGATION_SLS
  195. KBUILD_CFLAGS += -mharden-sls=all
  196. endif
  197. ifdef CONFIG_CALL_PADDING
  198. PADDING_CFLAGS := -fpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES)
  199. KBUILD_CFLAGS += $(PADDING_CFLAGS)
  200. export PADDING_CFLAGS
  201. PADDING_RUSTFLAGS := -Zpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES)
  202. KBUILD_RUSTFLAGS += $(PADDING_RUSTFLAGS)
  203. export PADDING_RUSTFLAGS
  204. endif
  205. KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)
  206. #
  207. # The 64-bit kernel must be aligned to 2MB. Pass -z max-page-size=0x200000 to
  208. # the linker to force 2MB page size regardless of the default page size used
  209. # by the linker.
  210. #
  211. ifdef CONFIG_X86_64
  212. LDFLAGS_vmlinux += -z max-page-size=0x200000
  213. endif
  214. archscripts: scripts_basic
  215. $(Q)$(MAKE) $(build)=arch/x86/tools relocs vdso2c
  216. ###
  217. # Syscall table generation
  218. archheaders:
  219. $(Q)$(MAKE) $(build)=arch/x86/entry/syscalls all
  220. ###
  221. # <asm/cpufeaturemasks.h> header generation
  222. cpufeaturemasks.hdr := arch/x86/include/generated/asm/cpufeaturemasks.h
  223. cpufeaturemasks.awk := $(srctree)/arch/x86/tools/cpufeaturemasks.awk
  224. cpufeatures_hdr := $(srctree)/arch/x86/include/asm/cpufeatures.h
  225. targets += $(cpufeaturemasks.hdr)
  226. filechk_gen_featuremasks = $(AWK) -f $(cpufeaturemasks.awk) $(cpufeatures_hdr) $(KCONFIG_CONFIG)
  227. $(cpufeaturemasks.hdr): $(cpufeaturemasks.awk) $(cpufeatures_hdr) $(KCONFIG_CONFIG) FORCE
  228. $(shell mkdir -p $(dir $@))
  229. $(call filechk,gen_featuremasks)
  230. archprepare: $(cpufeaturemasks.hdr)
  231. ###
  232. # Kernel objects
  233. libs-y += arch/x86/lib/
  234. # drivers-y are linked after core-y
  235. drivers-$(CONFIG_MATH_EMULATION) += arch/x86/math-emu/
  236. drivers-$(CONFIG_PCI) += arch/x86/pci/
  237. # suspend and hibernation support
  238. drivers-$(CONFIG_PM) += arch/x86/power/
  239. drivers-$(CONFIG_VIDEO) += arch/x86/video/
  240. ####
  241. # boot loader support. Several targets are kept for legacy purposes
  242. boot := arch/x86/boot
  243. BOOT_TARGETS = bzdisk fdimage fdimage144 fdimage288 hdimage isoimage
  244. PHONY += bzImage $(BOOT_TARGETS)
  245. # Default kernel to build
  246. all: bzImage
  247. # KBUILD_IMAGE specify target image being built
  248. KBUILD_IMAGE := $(boot)/bzImage
  249. bzImage: vmlinux
  250. ifeq ($(CONFIG_X86_DECODER_SELFTEST),y)
  251. $(Q)$(MAKE) $(build)=arch/x86/tools posttest
  252. endif
  253. $(Q)$(MAKE) $(build)=$(boot) $(KBUILD_IMAGE)
  254. $(Q)mkdir -p $(objtree)/arch/$(UTS_MACHINE)/boot
  255. $(Q)ln -fsn ../../x86/boot/bzImage $(objtree)/arch/$(UTS_MACHINE)/boot/$@
  256. $(BOOT_TARGETS): vmlinux
  257. $(Q)$(MAKE) $(build)=$(boot) $@
  258. PHONY += install
  259. install:
  260. $(call cmd,install)
  261. vdso-install-$(CONFIG_X86_64) += arch/x86/entry/vdso/vdso64/vdso64.so.dbg
  262. vdso-install-$(CONFIG_X86_X32_ABI) += arch/x86/entry/vdso/vdso64/vdsox32.so.dbg
  263. vdso-install-$(CONFIG_COMPAT_32) += arch/x86/entry/vdso/vdso32/vdso32.so.dbg
  264. archprepare: checkbin
  265. checkbin:
  266. ifdef CONFIG_MITIGATION_RETPOLINE
  267. ifeq ($(RETPOLINE_CFLAGS),)
  268. @echo "You are building kernel with non-retpoline compiler." >&2
  269. @echo "Please update your compiler." >&2
  270. @false
  271. endif
  272. endif
  273. ifdef CONFIG_UNWINDER_ORC
  274. orc_hash_h := arch/$(SRCARCH)/include/generated/asm/orc_hash.h
  275. orc_hash_sh := $(srctree)/scripts/orc_hash.sh
  276. targets += $(orc_hash_h)
  277. quiet_cmd_orc_hash = GEN $@
  278. cmd_orc_hash = mkdir -p $(dir $@); \
  279. $(CONFIG_SHELL) $(orc_hash_sh) < $< > $@
  280. $(orc_hash_h): $(srctree)/arch/x86/include/asm/orc_types.h $(orc_hash_sh) FORCE
  281. $(call if_changed,orc_hash)
  282. archprepare: $(orc_hash_h)
  283. endif
  284. archclean:
  285. $(Q)rm -rf $(objtree)/arch/i386
  286. $(Q)rm -rf $(objtree)/arch/x86_64
  287. define archhelp
  288. echo '* bzImage - Compressed kernel image (arch/x86/boot/bzImage)'
  289. echo ' install - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or'
  290. echo ' (distribution) /sbin/$(INSTALLKERNEL) or install to '
  291. echo ' $$(INSTALL_PATH) and run lilo'
  292. echo ''
  293. echo ' fdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)'
  294. echo ' fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)'
  295. echo ' fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)'
  296. echo ' hdimage - Create a BIOS/EFI hard disk image (arch/x86/boot/hdimage)'
  297. echo ' isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)'
  298. echo ' bzdisk/fdimage*/hdimage/isoimage also accept:'
  299. echo ' FDARGS="..." arguments for the booted kernel'
  300. echo ' FDINITRD=file initrd for the booted kernel'
  301. endef