1200483Srwatson# SPDX-License-Identifier: GPL-2.0 2200483Srwatson# Makefile for nolibc tests 3200483Srwatsoninclude ../../../scripts/Makefile.include 4200483Srwatsoninclude ../../../scripts/utilities.mak 5200483Srwatson# We need this for the "cc-option" macro. 6200483Srwatsoninclude ../../../build/Build.include 7200483Srwatson 8200483Srwatsonifneq ($(O),) 9200483Srwatsonifneq ($(call is-absolute,$(O)),y) 10200483Srwatson$(error Only absolute O= parameters are supported) 11200483Srwatsonendif 12200483Srwatsonendif 13200483Srwatson 14200483Srwatson# we're in ".../tools/testing/selftests/nolibc" 15200483Srwatsonifeq ($(srctree),) 16200483Srwatsonsrctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR))) 17200483Srwatsonendif 18200483Srwatson 19200483Srwatsonifeq ($(ARCH),) 20200483Srwatsoninclude $(srctree)/scripts/subarch.include 21223845SjonathanARCH = $(SUBARCH) 22200483Srwatsonendif 23200483Srwatson 24200483Srwatsonobjtree ?= $(srctree) 25200483Srwatson 26200483Srwatson# XARCH extends the kernel's ARCH with a few variants of the same 27200483Srwatson# architecture that only differ by the configuration, the toolchain 28200483Srwatson# and the Qemu program used. It is copied as-is into ARCH except for 29200483Srwatson# a few specific values which are mapped like this: 30200483Srwatson# 31200483Srwatson# XARCH | ARCH | config 32200483Srwatson# -------------|-----------|------------------------- 33200483Srwatson# ppc | powerpc | 32 bits 34200483Srwatson# ppc64 | powerpc | 64 bits big endian 35200483Srwatson# ppc64le | powerpc | 64 bits little endian 36200483Srwatson# 37200483Srwatson# It is recommended to only use XARCH, though it does not harm if 38200483Srwatson# ARCH is already set. For simplicity, ARCH is sufficient for all 39200483Srwatson# architectures where both are equal. 40200483Srwatson 41200483Srwatson# configure default variants for target kernel supported architectures 42200483SrwatsonXARCH_powerpc = ppc 43200483SrwatsonXARCH_mips = mips32le 44200483SrwatsonXARCH = $(or $(XARCH_$(ARCH)),$(ARCH)) 45200483Srwatson 46200483Srwatson# map from user input variants to their kernel supported architectures 47200483SrwatsonARCH_ppc = powerpc 48200483SrwatsonARCH_ppc64 = powerpc 49200483SrwatsonARCH_ppc64le = powerpc 50200483SrwatsonARCH_mips32le = mips 51200483SrwatsonARCH_mips32be = mips 52200483SrwatsonARCH := $(or $(ARCH_$(XARCH)),$(XARCH)) 53200483Srwatson 54200483Srwatson# kernel image names by architecture 55200483SrwatsonIMAGE_i386 = arch/x86/boot/bzImage 56200483SrwatsonIMAGE_x86_64 = arch/x86/boot/bzImage 57200483SrwatsonIMAGE_x86 = arch/x86/boot/bzImage 58200483SrwatsonIMAGE_arm64 = arch/arm64/boot/Image 59200483SrwatsonIMAGE_arm = arch/arm/boot/zImage 60200483SrwatsonIMAGE_mips32le = vmlinuz 61200483SrwatsonIMAGE_mips32be = vmlinuz 62200483SrwatsonIMAGE_ppc = vmlinux 63200483SrwatsonIMAGE_ppc64 = vmlinux 64200483SrwatsonIMAGE_ppc64le = arch/powerpc/boot/zImage 65200483SrwatsonIMAGE_riscv = arch/riscv/boot/Image 66200483SrwatsonIMAGE_s390 = arch/s390/boot/bzImage 67200483SrwatsonIMAGE_loongarch = arch/loongarch/boot/vmlinuz.efi 68200483SrwatsonIMAGE = $(objtree)/$(IMAGE_$(XARCH)) 69200483SrwatsonIMAGE_NAME = $(notdir $(IMAGE)) 70200483Srwatson 71200483Srwatson# default kernel configurations that appear to be usable 72295012SvangyzenDEFCONFIG_i386 = defconfig 73295012SvangyzenDEFCONFIG_x86_64 = defconfig 74295012SvangyzenDEFCONFIG_x86 = defconfig 75295012SvangyzenDEFCONFIG_arm64 = defconfig 76295012SvangyzenDEFCONFIG_arm = multi_v7_defconfig 77295012SvangyzenDEFCONFIG_mips32le = malta_defconfig 78295012SvangyzenDEFCONFIG_mips32be = malta_defconfig 79295012SvangyzenDEFCONFIG_ppc = pmac32_defconfig 80295012SvangyzenDEFCONFIG_ppc64 = powernv_be_defconfig 81295012SvangyzenDEFCONFIG_ppc64le = powernv_defconfig 82295012SvangyzenDEFCONFIG_riscv = defconfig 83295012SvangyzenDEFCONFIG_s390 = defconfig 84295012SvangyzenDEFCONFIG_loongarch = defconfig 85295012SvangyzenDEFCONFIG = $(DEFCONFIG_$(XARCH)) 86295012Svangyzen 87295012SvangyzenEXTRACONFIG_mips32be = -d CONFIG_CPU_LITTLE_ENDIAN -e CONFIG_CPU_BIG_ENDIAN 88295012SvangyzenEXTRACONFIG = $(EXTRACONFIG_$(XARCH)) 89295012Svangyzen 90295012Svangyzen# optional tests to run (default = all) 91295012SvangyzenTEST = 92295012Svangyzen 93295012Svangyzen# QEMU_ARCH: arch names used by qemu 94200483SrwatsonQEMU_ARCH_i386 = i386 95200483SrwatsonQEMU_ARCH_x86_64 = x86_64 96200483SrwatsonQEMU_ARCH_x86 = x86_64 97200483SrwatsonQEMU_ARCH_arm64 = aarch64 98200483SrwatsonQEMU_ARCH_arm = arm 99200483SrwatsonQEMU_ARCH_mips32le = mipsel # works with malta_defconfig 100200483SrwatsonQEMU_ARCH_mips32be = mips 101200483SrwatsonQEMU_ARCH_ppc = ppc 102200483SrwatsonQEMU_ARCH_ppc64 = ppc64 103200483SrwatsonQEMU_ARCH_ppc64le = ppc64 104200483SrwatsonQEMU_ARCH_riscv = riscv64 105200483SrwatsonQEMU_ARCH_s390 = s390x 106200483SrwatsonQEMU_ARCH_loongarch = loongarch64 107295012SvangyzenQEMU_ARCH = $(QEMU_ARCH_$(XARCH)) 108295012Svangyzen 109295012SvangyzenQEMU_ARCH_USER_ppc64le = ppc64le 110295012SvangyzenQEMU_ARCH_USER = $(or $(QEMU_ARCH_USER_$(XARCH)),$(QEMU_ARCH_$(XARCH))) 111295012Svangyzen 112295012SvangyzenQEMU_BIOS_DIR = /usr/share/edk2/ 113295012SvangyzenQEMU_BIOS_loongarch = $(QEMU_BIOS_DIR)/loongarch64/OVMF_CODE.fd 114295012Svangyzen 115295012Svangyzenifneq ($(QEMU_BIOS_$(XARCH)),) 116295012SvangyzenQEMU_ARGS_BIOS = -bios $(QEMU_BIOS_$(XARCH)) 117295012Svangyzenendif 118295012Svangyzen 119295012Svangyzen# QEMU_ARGS : some arch-specific args to pass to qemu 120295012SvangyzenQEMU_ARGS_i386 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 121295012SvangyzenQEMU_ARGS_x86_64 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 122295012SvangyzenQEMU_ARGS_x86 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 123295012SvangyzenQEMU_ARGS_arm64 = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 124295012SvangyzenQEMU_ARGS_arm = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 125295012SvangyzenQEMU_ARGS_mips32le = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 126295012SvangyzenQEMU_ARGS_mips32be = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 127200483SrwatsonQEMU_ARGS_ppc = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 128295012SvangyzenQEMU_ARGS_ppc64 = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 129200483SrwatsonQEMU_ARGS_ppc64le = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 130295012SvangyzenQEMU_ARGS_riscv = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 131295012SvangyzenQEMU_ARGS_s390 = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 132295012SvangyzenQEMU_ARGS_loongarch = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 133200483SrwatsonQEMU_ARGS = $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_BIOS) $(QEMU_ARGS_EXTRA) 134295012Svangyzen 135200483Srwatson# OUTPUT is only set when run from the main makefile, otherwise 136295012Svangyzen# it defaults to this nolibc directory. 137295012SvangyzenOUTPUT ?= $(CURDIR)/ 138295012Svangyzen 139295012Svangyzenifeq ($(V),1) 140200483SrwatsonQ= 141200483Srwatsonelse 142200483SrwatsonQ=@ 143200483Srwatsonendif 144200483Srwatson 145200483SrwatsonCFLAGS_i386 = $(call cc-option,-m32) 146200483SrwatsonCFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 147200483SrwatsonCFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 148200483SrwatsonCFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2) 149200483SrwatsonCFLAGS_s390 = -m64 150200483SrwatsonCFLAGS_mips32le = -EL -mabi=32 -fPIC 151200483SrwatsonCFLAGS_mips32be = -EB -mabi=32 152200483SrwatsonCFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) 153200483SrwatsonCFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \ 154200483Srwatson $(call cc-option,-fno-stack-protector) \ 155200483Srwatson $(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) 156200483SrwatsonLDFLAGS := 157200483Srwatson 158200483SrwatsonREPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \ 159200483Srwatson END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \ 160200483Srwatson if (f) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \ 161200483Srwatson printf("\nSee all results in %s\n", ARGV[1]); }' 162200483Srwatson 163200483Srwatsonhelp: 164200483Srwatson @echo "Supported targets under selftests/nolibc:" 165200483Srwatson @echo " all call the \"run\" target below" 166200483Srwatson @echo " help this help" 167200483Srwatson @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" 168200483Srwatson @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" 169200483Srwatson @echo " libc-test build an executable using the compiler's default libc instead" 170200483Srwatson @echo " run-user runs the executable under QEMU (uses \$$XARCH, \$$TEST)" 171200483Srwatson @echo " initramfs.cpio prepare the initramfs archive with nolibc-test" 172200483Srwatson @echo " initramfs prepare the initramfs tree with nolibc-test" 173200483Srwatson @echo " defconfig create a fresh new default config (uses \$$XARCH)" 174200483Srwatson @echo " kernel (re)build the kernel (uses \$$XARCH)" 175200483Srwatson @echo " kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH)" 176200483Srwatson @echo " run runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)" 177200483Srwatson @echo " rerun runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)" 178200483Srwatson @echo " clean clean the sysroot, initramfs, build and output files" 179200483Srwatson @echo "" 180200483Srwatson @echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST." 181200483Srwatson @echo "" 182200483Srwatson @echo "Currently using the following variables:" 183200483Srwatson @echo " ARCH = $(ARCH)" 184200483Srwatson @echo " XARCH = $(XARCH)" 185200483Srwatson @echo " CROSS_COMPILE = $(CROSS_COMPILE)" 186200483Srwatson @echo " CC = $(CC)" 187200483Srwatson @echo " OUTPUT = $(OUTPUT)" 188200483Srwatson @echo " TEST = $(TEST)" 189200483Srwatson @echo " QEMU_ARCH = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]" 190200483Srwatson @echo " IMAGE_NAME = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]" 191200483Srwatson @echo "" 192200483Srwatson 193200483Srwatsonall: run 194200483Srwatson 195200483Srwatsonsysroot: sysroot/$(ARCH)/include 196200483Srwatson 197200483Srwatsonsysroot/$(ARCH)/include: 198200483Srwatson $(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot 199200483Srwatson $(QUIET_MKDIR)mkdir -p sysroot 200200483Srwatson $(Q)$(MAKE) -C $(srctree) outputmakefile 201200483Srwatson $(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone 202200483Srwatson $(Q)mv sysroot/sysroot sysroot/$(ARCH) 203200483Srwatson 204200483Srwatsonifneq ($(NOLIBC_SYSROOT),0) 205200483Srwatsonnolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include 206200483Srwatson $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 207200483Srwatson -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c -lgcc 208200483Srwatsonelse 209200483Srwatsonnolibc-test: nolibc-test.c nolibc-test-linkage.c 210200483Srwatson $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 211200483Srwatson -nostdlib -static -include $(srctree)/tools/include/nolibc/nolibc.h nolibc-test.c nolibc-test-linkage.c -lgcc 212200483Srwatsonendif 213200483Srwatson 214200483Srwatsonlibc-test: nolibc-test.c nolibc-test-linkage.c 215200483Srwatson $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c 216200483Srwatson 217200483Srwatson# local libc-test 218200483Srwatsonrun-libc-test: libc-test 219200483Srwatson $(Q)./libc-test > "$(CURDIR)/run.out" || : 220200483Srwatson $(Q)$(REPORT) $(CURDIR)/run.out 221200483Srwatson 222200483Srwatson# local nolibc-test 223200483Srwatsonrun-nolibc-test: nolibc-test 224200483Srwatson $(Q)./nolibc-test > "$(CURDIR)/run.out" || : 225200483Srwatson $(Q)$(REPORT) $(CURDIR)/run.out 226200483Srwatson 227200483Srwatson# qemu user-land test 228200483Srwatsonrun-user: nolibc-test 229200483Srwatson $(Q)qemu-$(QEMU_ARCH_USER) ./nolibc-test > "$(CURDIR)/run.out" || : 230200483Srwatson $(Q)$(REPORT) $(CURDIR)/run.out 231200483Srwatson 232200483Srwatsoninitramfs.cpio: kernel nolibc-test 233200483Srwatson $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio 234200483Srwatson 235200483Srwatsoninitramfs: nolibc-test 236200483Srwatson $(QUIET_MKDIR)mkdir -p initramfs 237200483Srwatson $(call QUIET_INSTALL, initramfs/init) 238200483Srwatson $(Q)cp nolibc-test initramfs/init 239200483Srwatson 240200483Srwatsondefconfig: 241200483Srwatson $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare 242200483Srwatson $(Q)if [ -n "$(EXTRACONFIG)" ]; then \ 243200483Srwatson $(srctree)/scripts/config --file $(objtree)/.config $(EXTRACONFIG); \ 244200483Srwatson $(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) olddefconfig < /dev/null; \ 245200483Srwatson fi 246200483Srwatson 247200483Srwatsonkernel: 248200483Srwatson $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) < /dev/null 249200483Srwatson 250200483Srwatsonkernel-standalone: initramfs 251200483Srwatson $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs < /dev/null 252200483Srwatson 253200483Srwatson# run the tests after building the kernel 254200483Srwatsonrun: kernel initramfs.cpio 255200483Srwatson $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 256200483Srwatson $(Q)$(REPORT) $(CURDIR)/run.out 257200483Srwatson 258200483Srwatson# re-run the tests from an existing kernel 259200483Srwatsonrerun: 260200483Srwatson $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 261200483Srwatson $(Q)$(REPORT) $(CURDIR)/run.out 262200483Srwatson 263200483Srwatson# report with existing test log 264200483Srwatsonreport: 265200483Srwatson $(Q)$(REPORT) $(CURDIR)/run.out 266200483Srwatson 267200483Srwatsonclean: 268200483Srwatson $(call QUIET_CLEAN, sysroot) 269200483Srwatson $(Q)rm -rf sysroot 270223845Sjonathan $(call QUIET_CLEAN, nolibc-test) 271200483Srwatson $(Q)rm -f nolibc-test 272200483Srwatson $(call QUIET_CLEAN, libc-test) 273200483Srwatson $(Q)rm -f libc-test 274200483Srwatson $(call QUIET_CLEAN, initramfs.cpio) 275200573Srwatson $(Q)rm -rf initramfs.cpio 276200483Srwatson $(call QUIET_CLEAN, initramfs) 277200573Srwatson $(Q)rm -rf initramfs 278200573Srwatson $(call QUIET_CLEAN, run.out) 279200573Srwatson $(Q)rm -rf run.out 280200573Srwatson 281200483Srwatson.PHONY: sysroot/$(ARCH)/include 282200483Srwatson