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