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