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