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