Makefile 12 KB

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