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