Back to home page

OSCL-LXR

 
 

    


0001 # SPDX-License-Identifier: GPL-2.0
0002 # Unified Makefile for i386 and x86_64
0003 
0004 # select defconfig based on actual architecture
0005 ifeq ($(ARCH),x86)
0006   ifeq ($(shell uname -m),x86_64)
0007         KBUILD_DEFCONFIG := x86_64_defconfig
0008   else
0009         KBUILD_DEFCONFIG := i386_defconfig
0010   endif
0011 else
0012         KBUILD_DEFCONFIG := $(ARCH)_defconfig
0013 endif
0014 
0015 ifdef CONFIG_CC_IS_GCC
0016 RETPOLINE_CFLAGS        := $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
0017 RETPOLINE_CFLAGS        += $(call cc-option,-mindirect-branch-cs-prefix)
0018 RETPOLINE_VDSO_CFLAGS   := $(call cc-option,-mindirect-branch=thunk-inline -mindirect-branch-register)
0019 endif
0020 ifdef CONFIG_CC_IS_CLANG
0021 RETPOLINE_CFLAGS        := -mretpoline-external-thunk
0022 RETPOLINE_VDSO_CFLAGS   := -mretpoline
0023 endif
0024 
0025 ifdef CONFIG_RETHUNK
0026 RETHUNK_CFLAGS          := -mfunction-return=thunk-extern
0027 RETPOLINE_CFLAGS        += $(RETHUNK_CFLAGS)
0028 endif
0029 
0030 export RETHUNK_CFLAGS
0031 export RETPOLINE_CFLAGS
0032 export RETPOLINE_VDSO_CFLAGS
0033 
0034 # For gcc stack alignment is specified with -mpreferred-stack-boundary,
0035 # clang has the option -mstack-alignment for that purpose.
0036 ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
0037       cc_stack_align4 := -mpreferred-stack-boundary=2
0038       cc_stack_align8 := -mpreferred-stack-boundary=3
0039 else ifneq ($(call cc-option, -mstack-alignment=16),)
0040       cc_stack_align4 := -mstack-alignment=4
0041       cc_stack_align8 := -mstack-alignment=8
0042 endif
0043 
0044 # How to compile the 16-bit code.  Note we always compile for -march=i386;
0045 # that way we can complain to the user if the CPU is insufficient.
0046 REALMODE_CFLAGS := -m16 -g -Os -DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \
0047                    -Wall -Wstrict-prototypes -march=i386 -mregparm=3 \
0048                    -fno-strict-aliasing -fomit-frame-pointer -fno-pic \
0049                    -mno-mmx -mno-sse $(call cc-option,-fcf-protection=none)
0050 
0051 REALMODE_CFLAGS += -ffreestanding
0052 REALMODE_CFLAGS += -fno-stack-protector
0053 REALMODE_CFLAGS += -Wno-address-of-packed-member
0054 REALMODE_CFLAGS += $(cc_stack_align4)
0055 REALMODE_CFLAGS += $(CLANG_FLAGS)
0056 export REALMODE_CFLAGS
0057 
0058 # BITS is used as extension for files which are available in a 32 bit
0059 # and a 64 bit version to simplify shared Makefiles.
0060 # e.g.: obj-y += foo_$(BITS).o
0061 export BITS
0062 
0063 #
0064 # Prevent GCC from generating any FP code by mistake.
0065 #
0066 # This must happen before we try the -mpreferred-stack-boundary, see:
0067 #
0068 #    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383
0069 #
0070 KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx
0071 
0072 ifeq ($(CONFIG_X86_KERNEL_IBT),y)
0073 #
0074 # Kernel IBT has S_CET.NOTRACK_EN=0, as such the compilers must not generate
0075 # NOTRACK prefixes. Current generation compilers unconditionally employ NOTRACK
0076 # for jump-tables, as such, disable jump-tables for now.
0077 #
0078 # (jump-tables are implicitly disabled by RETPOLINE)
0079 #
0080 #   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104816
0081 #
0082 KBUILD_CFLAGS += $(call cc-option,-fcf-protection=branch -fno-jump-tables)
0083 else
0084 KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
0085 endif
0086 
0087 ifeq ($(CONFIG_X86_32),y)
0088         BITS := 32
0089         UTS_MACHINE := i386
0090         CHECKFLAGS += -D__i386__
0091 
0092         KBUILD_AFLAGS += -m32
0093         KBUILD_CFLAGS += -m32
0094 
0095         KBUILD_CFLAGS += -msoft-float -mregparm=3 -freg-struct-return
0096 
0097         # Never want PIC in a 32-bit kernel, prevent breakage with GCC built
0098         # with nonstandard options
0099         KBUILD_CFLAGS += -fno-pic
0100 
0101         # Align the stack to the register width instead of using the default
0102         # alignment of 16 bytes. This reduces stack usage and the number of
0103         # alignment instructions.
0104         KBUILD_CFLAGS += $(cc_stack_align4)
0105 
0106         # CPU-specific tuning. Anything which can be shared with UML should go here.
0107         include $(srctree)/arch/x86/Makefile_32.cpu
0108         KBUILD_CFLAGS += $(cflags-y)
0109 
0110         # temporary until string.h is fixed
0111         KBUILD_CFLAGS += -ffreestanding
0112 
0113         ifeq ($(CONFIG_STACKPROTECTOR),y)
0114                 ifeq ($(CONFIG_SMP),y)
0115                         KBUILD_CFLAGS += -mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=__stack_chk_guard
0116                 else
0117                         KBUILD_CFLAGS += -mstack-protector-guard=global
0118                 endif
0119         endif
0120 else
0121         BITS := 64
0122         UTS_MACHINE := x86_64
0123         CHECKFLAGS += -D__x86_64__
0124 
0125         KBUILD_AFLAGS += -m64
0126         KBUILD_CFLAGS += -m64
0127 
0128         # Align jump targets to 1 byte, not the default 16 bytes:
0129         KBUILD_CFLAGS += $(call cc-option,-falign-jumps=1)
0130 
0131         # Pack loops tightly as well:
0132         KBUILD_CFLAGS += $(call cc-option,-falign-loops=1)
0133 
0134         # Don't autogenerate traditional x87 instructions
0135         KBUILD_CFLAGS += -mno-80387
0136         KBUILD_CFLAGS += $(call cc-option,-mno-fp-ret-in-387)
0137 
0138         # By default gcc and clang use a stack alignment of 16 bytes for x86.
0139         # However the standard kernel entry on x86-64 leaves the stack on an
0140         # 8-byte boundary. If the compiler isn't informed about the actual
0141         # alignment it will generate extra alignment instructions for the
0142         # default alignment which keep the stack *mis*aligned.
0143         # Furthermore an alignment to the register width reduces stack usage
0144         # and the number of alignment instructions.
0145         KBUILD_CFLAGS += $(cc_stack_align8)
0146 
0147         # Use -mskip-rax-setup if supported.
0148         KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)
0149 
0150         # FIXME - should be integrated in Makefile.cpu (Makefile_32.cpu)
0151         cflags-$(CONFIG_MK8)            += -march=k8
0152         cflags-$(CONFIG_MPSC)           += -march=nocona
0153         cflags-$(CONFIG_MCORE2)         += -march=core2
0154         cflags-$(CONFIG_MATOM)          += -march=atom
0155         cflags-$(CONFIG_GENERIC_CPU)    += -mtune=generic
0156         KBUILD_CFLAGS += $(cflags-y)
0157 
0158         KBUILD_CFLAGS += -mno-red-zone
0159         KBUILD_CFLAGS += -mcmodel=kernel
0160 endif
0161 
0162 #
0163 # If the function graph tracer is used with mcount instead of fentry,
0164 # '-maccumulate-outgoing-args' is needed to prevent a GCC bug
0165 # (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42109)
0166 #
0167 ifdef CONFIG_FUNCTION_GRAPH_TRACER
0168   ifndef CONFIG_HAVE_FENTRY
0169         ACCUMULATE_OUTGOING_ARGS := 1
0170   endif
0171 endif
0172 
0173 ifeq ($(ACCUMULATE_OUTGOING_ARGS), 1)
0174         # This compiler flag is not supported by Clang:
0175         KBUILD_CFLAGS += $(call cc-option,-maccumulate-outgoing-args,)
0176 endif
0177 
0178 # Workaround for a gcc prelease that unfortunately was shipped in a suse release
0179 KBUILD_CFLAGS += -Wno-sign-compare
0180 #
0181 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
0182 
0183 # Avoid indirect branches in kernel to deal with Spectre
0184 ifdef CONFIG_RETPOLINE
0185   KBUILD_CFLAGS += $(RETPOLINE_CFLAGS)
0186   # Additionally, avoid generating expensive indirect jumps which
0187   # are subject to retpolines for small number of switch cases.
0188   # clang turns off jump table generation by default when under
0189   # retpoline builds, however, gcc does not for x86. This has
0190   # only been fixed starting from gcc stable version 8.4.0 and
0191   # onwards, but not for older ones. See gcc bug #86952.
0192   ifndef CONFIG_CC_IS_CLANG
0193     KBUILD_CFLAGS += -fno-jump-tables
0194   endif
0195 endif
0196 
0197 ifdef CONFIG_SLS
0198   KBUILD_CFLAGS += -mharden-sls=all
0199 endif
0200 
0201 KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE)
0202 
0203 ifdef CONFIG_LTO_CLANG
0204 ifeq ($(shell test $(CONFIG_LLD_VERSION) -lt 130000; echo $$?),0)
0205 KBUILD_LDFLAGS  += -plugin-opt=-stack-alignment=$(if $(CONFIG_X86_32),4,8)
0206 endif
0207 endif
0208 
0209 ifdef CONFIG_X86_NEED_RELOCS
0210 LDFLAGS_vmlinux := --emit-relocs --discard-none
0211 else
0212 LDFLAGS_vmlinux :=
0213 endif
0214 
0215 #
0216 # The 64-bit kernel must be aligned to 2MB.  Pass -z max-page-size=0x200000 to
0217 # the linker to force 2MB page size regardless of the default page size used
0218 # by the linker.
0219 #
0220 ifdef CONFIG_X86_64
0221 LDFLAGS_vmlinux += -z max-page-size=0x200000
0222 endif
0223 
0224 
0225 archscripts: scripts_basic
0226         $(Q)$(MAKE) $(build)=arch/x86/tools relocs
0227 
0228 ###
0229 # Syscall table generation
0230 
0231 archheaders:
0232         $(Q)$(MAKE) $(build)=arch/x86/entry/syscalls all
0233 
0234 ###
0235 # Kernel objects
0236 
0237 head-y := arch/x86/kernel/head_$(BITS).o
0238 head-y += arch/x86/kernel/head$(BITS).o
0239 head-y += arch/x86/kernel/ebda.o
0240 head-y += arch/x86/kernel/platform-quirks.o
0241 
0242 libs-y  += arch/x86/lib/
0243 
0244 # drivers-y are linked after core-y
0245 drivers-$(CONFIG_MATH_EMULATION) += arch/x86/math-emu/
0246 drivers-$(CONFIG_PCI)            += arch/x86/pci/
0247 
0248 # suspend and hibernation support
0249 drivers-$(CONFIG_PM) += arch/x86/power/
0250 
0251 drivers-$(CONFIG_FB) += arch/x86/video/
0252 
0253 ####
0254 # boot loader support. Several targets are kept for legacy purposes
0255 
0256 boot := arch/x86/boot
0257 
0258 BOOT_TARGETS = bzdisk fdimage fdimage144 fdimage288 hdimage isoimage
0259 
0260 PHONY += bzImage $(BOOT_TARGETS)
0261 
0262 # Default kernel to build
0263 all: bzImage
0264 
0265 # KBUILD_IMAGE specify target image being built
0266 KBUILD_IMAGE := $(boot)/bzImage
0267 
0268 bzImage: vmlinux
0269 ifeq ($(CONFIG_X86_DECODER_SELFTEST),y)
0270         $(Q)$(MAKE) $(build)=arch/x86/tools posttest
0271 endif
0272         $(Q)$(MAKE) $(build)=$(boot) $(KBUILD_IMAGE)
0273         $(Q)mkdir -p $(objtree)/arch/$(UTS_MACHINE)/boot
0274         $(Q)ln -fsn ../../x86/boot/bzImage $(objtree)/arch/$(UTS_MACHINE)/boot/$@
0275 
0276 $(BOOT_TARGETS): vmlinux
0277         $(Q)$(MAKE) $(build)=$(boot) $@
0278 
0279 PHONY += install
0280 install:
0281         $(call cmd,install)
0282 
0283 PHONY += vdso_install
0284 vdso_install:
0285         $(Q)$(MAKE) $(build)=arch/x86/entry/vdso $@
0286 
0287 archprepare: checkbin
0288 checkbin:
0289 ifdef CONFIG_RETPOLINE
0290 ifeq ($(RETPOLINE_CFLAGS),)
0291         @echo "You are building kernel with non-retpoline compiler." >&2
0292         @echo "Please update your compiler." >&2
0293         @false
0294 endif
0295 endif
0296 
0297 archclean:
0298         $(Q)rm -rf $(objtree)/arch/i386
0299         $(Q)rm -rf $(objtree)/arch/x86_64
0300 
0301 define archhelp
0302   echo  '* bzImage              - Compressed kernel image (arch/x86/boot/bzImage)'
0303   echo  '  install              - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or'
0304   echo  '                         (distribution) /sbin/$(INSTALLKERNEL) or install to '
0305   echo  '                         $$(INSTALL_PATH) and run lilo'
0306   echo  ''
0307   echo  '  fdimage              - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)'
0308   echo  '  fdimage144           - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)'
0309   echo  '  fdimage288           - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)'
0310   echo  '  hdimage              - Create a BIOS/EFI hard disk image (arch/x86/boot/hdimage)'
0311   echo  '  isoimage             - Create a boot CD-ROM image (arch/x86/boot/image.iso)'
0312   echo  '                         bzdisk/fdimage*/hdimage/isoimage also accept:'
0313   echo  '                         FDARGS="..."  arguments for the booted kernel'
0314   echo  '                         FDINITRD=file initrd for the booted kernel'
0315   echo  ''
0316   echo  '  kvm_guest.config     - Enable Kconfig items for running this kernel as a KVM guest'
0317   echo  '  xen.config           - Enable Kconfig items for running this kernel as a Xen guest'
0318   echo  '  x86_debug.config     - Enable tip tree debugging options for testing'
0319 
0320 endef