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