1# SPDX-License-Identifier: GPL-2.0 2# Makefile for nolibc tests 3# we're in ".../tools/testing/selftests/nolibc" 4ifeq ($(srctree),) 5srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR))) 6endif 7 8include $(srctree)/tools/scripts/utilities.mak 9# We need this for the "__cc-option" macro. 10include $(srctree)/scripts/Makefile.compiler 11 12ifneq ($(O),) 13ifneq ($(call is-absolute,$(O)),y) 14$(error Only absolute O= parameters are supported) 15endif 16objtree := $(O) 17else 18objtree ?= $(srctree) 19endif 20 21ifeq ($(ARCH),) 22include $(srctree)/scripts/subarch.include 23ARCH = $(SUBARCH) 24endif 25 26cc-option = $(call __cc-option, $(CC),$(CLANG_CROSS_FLAGS),$(1),$(2)) 27 28# XARCH extends the kernel's ARCH with a few variants of the same 29# architecture that only differ by the configuration, the toolchain 30# and the Qemu program used. It is copied as-is into ARCH except for 31# a few specific values which are mapped like this: 32# 33# XARCH | ARCH | config 34# -------------|-----------|------------------------- 35# ppc | powerpc | 32 bits 36# ppc64 | powerpc | 64 bits big endian 37# ppc64le | powerpc | 64 bits little endian 38# 39# It is recommended to only use XARCH, though it does not harm if 40# ARCH is already set. For simplicity, ARCH is sufficient for all 41# architectures where both are equal. 42 43# configure default variants for target kernel supported architectures 44XARCH_powerpc = ppc 45XARCH_mips = mips32le 46XARCH_riscv = riscv64 47XARCH = $(or $(XARCH_$(ARCH)),$(ARCH)) 48 49# map from user input variants to their kernel supported architectures 50ARCH_armthumb = arm 51ARCH_ppc = powerpc 52ARCH_ppc64 = powerpc 53ARCH_ppc64le = powerpc 54ARCH_mips32le = mips 55ARCH_mips32be = mips 56ARCH_riscv32 = riscv 57ARCH_riscv64 = riscv 58ARCH_s390x = s390 59ARCH := $(or $(ARCH_$(XARCH)),$(XARCH)) 60 61# kernel image names by architecture 62IMAGE_i386 = arch/x86/boot/bzImage 63IMAGE_x86_64 = arch/x86/boot/bzImage 64IMAGE_x86 = arch/x86/boot/bzImage 65IMAGE_arm64 = arch/arm64/boot/Image 66IMAGE_arm = arch/arm/boot/zImage 67IMAGE_armthumb = arch/arm/boot/zImage 68IMAGE_mips32le = vmlinuz 69IMAGE_mips32be = vmlinuz 70IMAGE_ppc = vmlinux 71IMAGE_ppc64 = vmlinux 72IMAGE_ppc64le = arch/powerpc/boot/zImage 73IMAGE_riscv = arch/riscv/boot/Image 74IMAGE_riscv32 = arch/riscv/boot/Image 75IMAGE_riscv64 = arch/riscv/boot/Image 76IMAGE_s390x = arch/s390/boot/bzImage 77IMAGE_s390 = arch/s390/boot/bzImage 78IMAGE_loongarch = arch/loongarch/boot/vmlinuz.efi 79IMAGE = $(objtree)/$(IMAGE_$(XARCH)) 80IMAGE_NAME = $(notdir $(IMAGE)) 81 82# default kernel configurations that appear to be usable 83DEFCONFIG_i386 = defconfig 84DEFCONFIG_x86_64 = defconfig 85DEFCONFIG_x86 = defconfig 86DEFCONFIG_arm64 = defconfig 87DEFCONFIG_arm = multi_v7_defconfig 88DEFCONFIG_armthumb = multi_v7_defconfig 89DEFCONFIG_mips32le = malta_defconfig 90DEFCONFIG_mips32be = malta_defconfig generic/eb.config 91DEFCONFIG_ppc = pmac32_defconfig 92DEFCONFIG_ppc64 = powernv_be_defconfig 93DEFCONFIG_ppc64le = powernv_defconfig 94DEFCONFIG_riscv = defconfig 95DEFCONFIG_riscv32 = rv32_defconfig 96DEFCONFIG_riscv64 = defconfig 97DEFCONFIG_s390x = defconfig 98DEFCONFIG_s390 = defconfig compat.config 99DEFCONFIG_loongarch = defconfig 100DEFCONFIG = $(DEFCONFIG_$(XARCH)) 101 102EXTRACONFIG = $(EXTRACONFIG_$(XARCH)) 103 104# optional tests to run (default = all) 105TEST = 106 107# QEMU_ARCH: arch names used by qemu 108QEMU_ARCH_i386 = i386 109QEMU_ARCH_x86_64 = x86_64 110QEMU_ARCH_x86 = x86_64 111QEMU_ARCH_arm64 = aarch64 112QEMU_ARCH_arm = arm 113QEMU_ARCH_armthumb = arm 114QEMU_ARCH_mips32le = mipsel # works with malta_defconfig 115QEMU_ARCH_mips32be = mips 116QEMU_ARCH_ppc = ppc 117QEMU_ARCH_ppc64 = ppc64 118QEMU_ARCH_ppc64le = ppc64 119QEMU_ARCH_riscv = riscv64 120QEMU_ARCH_riscv32 = riscv32 121QEMU_ARCH_riscv64 = riscv64 122QEMU_ARCH_s390x = s390x 123QEMU_ARCH_s390 = s390x 124QEMU_ARCH_loongarch = loongarch64 125QEMU_ARCH = $(QEMU_ARCH_$(XARCH)) 126 127QEMU_ARCH_USER_ppc64le = ppc64le 128QEMU_ARCH_USER = $(or $(QEMU_ARCH_USER_$(XARCH)),$(QEMU_ARCH_$(XARCH))) 129 130QEMU_BIOS_DIR = /usr/share/edk2/ 131QEMU_BIOS_loongarch = $(QEMU_BIOS_DIR)/loongarch64/OVMF_CODE.fd 132 133ifneq ($(QEMU_BIOS_$(XARCH)),) 134QEMU_ARGS_BIOS = -bios $(QEMU_BIOS_$(XARCH)) 135endif 136 137# QEMU_ARGS : some arch-specific args to pass to qemu 138QEMU_ARGS_i386 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 139QEMU_ARGS_x86_64 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 140QEMU_ARGS_x86 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 141QEMU_ARGS_arm64 = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 142QEMU_ARGS_arm = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 143QEMU_ARGS_armthumb = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 144QEMU_ARGS_mips32le = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 145QEMU_ARGS_mips32be = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 146QEMU_ARGS_ppc = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 147QEMU_ARGS_ppc64 = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 148QEMU_ARGS_ppc64le = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 149QEMU_ARGS_riscv = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 150QEMU_ARGS_riscv32 = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 151QEMU_ARGS_riscv64 = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 152QEMU_ARGS_s390x = -M s390-ccw-virtio -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 153QEMU_ARGS_s390 = -M s390-ccw-virtio -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 154QEMU_ARGS_loongarch = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 155QEMU_ARGS = -m 1G $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_BIOS) $(QEMU_ARGS_EXTRA) 156 157# OUTPUT is only set when run from the main makefile, otherwise 158# it defaults to this nolibc directory. 159OUTPUT ?= $(CURDIR)/ 160 161ifeq ($(V),1) 162Q= 163else 164Q=@ 165endif 166 167CFLAGS_i386 = $(call cc-option,-m32) 168CFLAGS_arm = -marm 169CFLAGS_armthumb = -mthumb -march=armv6t2 170CFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 171CFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 172CFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2) 173CFLAGS_s390x = -m64 174CFLAGS_s390 = -m31 175CFLAGS_mips32le = -EL -mabi=32 -fPIC 176CFLAGS_mips32be = -EB -mabi=32 177CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) 178CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \ 179 $(call cc-option,-fno-stack-protector) $(call cc-option,-Wmissing-prototypes) \ 180 $(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) $(CFLAGS_EXTRA) 181LDFLAGS := 182 183LIBGCC := -lgcc 184 185ifneq ($(LLVM),) 186# Not needed for clang 187LIBGCC := 188endif 189 190# Modify CFLAGS based on LLVM= 191include $(srctree)/tools/scripts/Makefile.include 192 193# GCC uses "s390", clang "systemz" 194CLANG_CROSS_FLAGS := $(subst --target=s390-linux,--target=systemz-linux,$(CLANG_CROSS_FLAGS)) 195 196REPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \ 197 END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \ 198 if (f || !p) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \ 199 printf("\nSee all results in %s\n", ARGV[1]); }' 200 201help: 202 @echo "Supported targets under selftests/nolibc:" 203 @echo " all call the \"run\" target below" 204 @echo " help this help" 205 @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" 206 @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" 207 @echo " libc-test build an executable using the compiler's default libc instead" 208 @echo " run-user runs the executable under QEMU (uses \$$XARCH, \$$TEST)" 209 @echo " initramfs.cpio prepare the initramfs archive with nolibc-test" 210 @echo " initramfs prepare the initramfs tree with nolibc-test" 211 @echo " defconfig create a fresh new default config (uses \$$XARCH)" 212 @echo " kernel (re)build the kernel (uses \$$XARCH)" 213 @echo " kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH)" 214 @echo " run runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)" 215 @echo " rerun runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)" 216 @echo " clean clean the sysroot, initramfs, build and output files" 217 @echo "" 218 @echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST." 219 @echo "" 220 @echo "Currently using the following variables:" 221 @echo " ARCH = $(ARCH)" 222 @echo " XARCH = $(XARCH)" 223 @echo " CROSS_COMPILE = $(CROSS_COMPILE)" 224 @echo " CC = $(CC)" 225 @echo " OUTPUT = $(OUTPUT)" 226 @echo " TEST = $(TEST)" 227 @echo " QEMU_ARCH = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]" 228 @echo " IMAGE_NAME = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]" 229 @echo "" 230 231all: run 232 233sysroot: sysroot/$(ARCH)/include 234 235sysroot/$(ARCH)/include: | defconfig 236 $(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot 237 $(QUIET_MKDIR)mkdir -p sysroot 238 $(Q)$(MAKE) -C $(srctree) outputmakefile 239 $(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone 240 $(Q)mv sysroot/sysroot sysroot/$(ARCH) 241 242ifneq ($(NOLIBC_SYSROOT),0) 243nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include 244 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 245 -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC) 246else 247nolibc-test: nolibc-test.c nolibc-test-linkage.c 248 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 249 -nostdlib -static -include $(srctree)/tools/include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c $(LIBGCC) 250endif 251 252libc-test: nolibc-test.c nolibc-test-linkage.c 253 $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c 254 255# local libc-test 256run-libc-test: libc-test 257 $(Q)./libc-test > "$(CURDIR)/run.out" || : 258 $(Q)$(REPORT) $(CURDIR)/run.out 259 260# local nolibc-test 261run-nolibc-test: nolibc-test 262 $(Q)./nolibc-test > "$(CURDIR)/run.out" || : 263 $(Q)$(REPORT) $(CURDIR)/run.out 264 265# qemu user-land test 266run-user: nolibc-test 267 $(Q)qemu-$(QEMU_ARCH_USER) ./nolibc-test > "$(CURDIR)/run.out" || : 268 $(Q)$(REPORT) $(CURDIR)/run.out 269 270initramfs.cpio: kernel nolibc-test 271 $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio 272 273initramfs: nolibc-test 274 $(QUIET_MKDIR)mkdir -p initramfs 275 $(call QUIET_INSTALL, initramfs/init) 276 $(Q)cp nolibc-test initramfs/init 277 278defconfig: 279 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(DEFCONFIG) 280 $(Q)if [ -n "$(EXTRACONFIG)" ]; then \ 281 $(srctree)/scripts/config --file $(objtree)/.config $(EXTRACONFIG); \ 282 $(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) olddefconfig < /dev/null; \ 283 fi 284 285kernel: | defconfig 286 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) < /dev/null 287 288kernel-standalone: initramfs | defconfig 289 $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs < /dev/null 290 291# run the tests after building the kernel 292run: kernel initramfs.cpio 293 $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 294 $(Q)$(REPORT) $(CURDIR)/run.out 295 296# re-run the tests from an existing kernel 297rerun: 298 $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 299 $(Q)$(REPORT) $(CURDIR)/run.out 300 301# report with existing test log 302report: 303 $(Q)$(REPORT) $(CURDIR)/run.out 304 305clean: 306 $(call QUIET_CLEAN, sysroot) 307 $(Q)rm -rf sysroot 308 $(call QUIET_CLEAN, nolibc-test) 309 $(Q)rm -f nolibc-test 310 $(call QUIET_CLEAN, libc-test) 311 $(Q)rm -f libc-test 312 $(call QUIET_CLEAN, initramfs.cpio) 313 $(Q)rm -rf initramfs.cpio 314 $(call QUIET_CLEAN, initramfs) 315 $(Q)rm -rf initramfs 316 $(call QUIET_CLEAN, run.out) 317 $(Q)rm -rf run.out 318 319.PHONY: sysroot/$(ARCH)/include 320