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