1# SPDX-License-Identifier: GPL-2.0
2# Makefile for nolibc tests
3include ../../../scripts/Makefile.include
4# We need this for the "cc-option" macro.
5include ../../../build/Build.include
6
7# we're in ".../tools/testing/selftests/nolibc"
8ifeq ($(srctree),)
9srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))
10endif
11
12ifeq ($(ARCH),)
13include $(srctree)/scripts/subarch.include
14ARCH = $(SUBARCH)
15endif
16
17# kernel image names by architecture
18IMAGE_i386       = arch/x86/boot/bzImage
19IMAGE_x86_64     = arch/x86/boot/bzImage
20IMAGE_x86        = arch/x86/boot/bzImage
21IMAGE_arm64      = arch/arm64/boot/Image
22IMAGE_arm        = arch/arm/boot/zImage
23IMAGE_mips       = vmlinuz
24IMAGE_riscv      = arch/riscv/boot/Image
25IMAGE_s390       = arch/s390/boot/bzImage
26IMAGE_loongarch  = arch/loongarch/boot/vmlinuz.efi
27IMAGE            = $(IMAGE_$(ARCH))
28IMAGE_NAME       = $(notdir $(IMAGE))
29
30# default kernel configurations that appear to be usable
31DEFCONFIG_i386       = defconfig
32DEFCONFIG_x86_64     = defconfig
33DEFCONFIG_x86        = defconfig
34DEFCONFIG_arm64      = defconfig
35DEFCONFIG_arm        = multi_v7_defconfig
36DEFCONFIG_mips       = malta_defconfig
37DEFCONFIG_riscv      = defconfig
38DEFCONFIG_s390       = defconfig
39DEFCONFIG_loongarch  = defconfig
40DEFCONFIG            = $(DEFCONFIG_$(ARCH))
41
42# optional tests to run (default = all)
43TEST =
44
45# QEMU_ARCH: arch names used by qemu
46QEMU_ARCH_i386       = i386
47QEMU_ARCH_x86_64     = x86_64
48QEMU_ARCH_x86        = x86_64
49QEMU_ARCH_arm64      = aarch64
50QEMU_ARCH_arm        = arm
51QEMU_ARCH_mips       = mipsel  # works with malta_defconfig
52QEMU_ARCH_riscv      = riscv64
53QEMU_ARCH_s390       = s390x
54QEMU_ARCH_loongarch  = loongarch64
55QEMU_ARCH            = $(QEMU_ARCH_$(ARCH))
56
57# QEMU_ARGS : some arch-specific args to pass to qemu
58QEMU_ARGS_i386       = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
59QEMU_ARGS_x86_64     = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
60QEMU_ARGS_x86        = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
61QEMU_ARGS_arm64      = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
62QEMU_ARGS_arm        = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
63QEMU_ARGS_mips       = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
64QEMU_ARGS_riscv      = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
65QEMU_ARGS_s390       = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
66QEMU_ARGS_loongarch  = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
67QEMU_ARGS            = $(QEMU_ARGS_$(ARCH))
68
69# OUTPUT is only set when run from the main makefile, otherwise
70# it defaults to this nolibc directory.
71OUTPUT ?= $(CURDIR)/
72
73ifeq ($(V),1)
74Q=
75else
76Q=@
77endif
78
79CFLAGS_STACKPROTECTOR = -DNOLIBC_STACKPROTECTOR \
80			$(call cc-option,-mstack-protector-guard=global) \
81			$(call cc-option,-fstack-protector-all)
82CFLAGS_STKP_i386 = $(CFLAGS_STACKPROTECTOR)
83CFLAGS_STKP_x86_64 = $(CFLAGS_STACKPROTECTOR)
84CFLAGS_STKP_x86 = $(CFLAGS_STACKPROTECTOR)
85CFLAGS_s390 = -m64
86CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables \
87		$(call cc-option,-fno-stack-protector) \
88		$(CFLAGS_STKP_$(ARCH)) $(CFLAGS_$(ARCH))
89LDFLAGS := -s
90
91help:
92	@echo "Supported targets under selftests/nolibc:"
93	@echo "  all          call the \"run\" target below"
94	@echo "  help         this help"
95	@echo "  sysroot      create the nolibc sysroot here (uses \$$ARCH)"
96	@echo "  nolibc-test  build the executable (uses \$$CC and \$$CROSS_COMPILE)"
97	@echo "  run-user     runs the executable under QEMU (uses \$$ARCH, \$$TEST)"
98	@echo "  initramfs    prepare the initramfs with nolibc-test"
99	@echo "  defconfig    create a fresh new default config (uses \$$ARCH)"
100	@echo "  kernel       (re)build the kernel with the initramfs (uses \$$ARCH)"
101	@echo "  run          runs the kernel in QEMU after building it (uses \$$ARCH, \$$TEST)"
102	@echo "  rerun        runs a previously prebuilt kernel in QEMU (uses \$$ARCH, \$$TEST)"
103	@echo "  clean        clean the sysroot, initramfs, build and output files"
104	@echo ""
105	@echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."
106	@echo ""
107	@echo "Currently using the following variables:"
108	@echo "  ARCH          = $(ARCH)"
109	@echo "  CROSS_COMPILE = $(CROSS_COMPILE)"
110	@echo "  CC            = $(CC)"
111	@echo "  OUTPUT        = $(OUTPUT)"
112	@echo "  TEST          = $(TEST)"
113	@echo "  QEMU_ARCH     = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$ARCH]"
114	@echo "  IMAGE_NAME    = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$ARCH]"
115	@echo ""
116
117all: run
118
119sysroot: sysroot/$(ARCH)/include
120
121sysroot/$(ARCH)/include:
122	$(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot
123	$(QUIET_MKDIR)mkdir -p sysroot
124	$(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone
125	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
126
127nolibc-test: nolibc-test.c sysroot/$(ARCH)/include
128	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
129	  -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc
130
131# qemu user-land test
132run-user: nolibc-test
133	$(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || :
134	$(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed."
135
136initramfs: nolibc-test
137	$(QUIET_MKDIR)mkdir -p initramfs
138	$(call QUIET_INSTALL, initramfs/init)
139	$(Q)cp nolibc-test initramfs/init
140
141defconfig:
142	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare
143
144kernel: initramfs
145	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs
146
147# run the tests after building the kernel
148run: kernel
149	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
150	$(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed."
151
152# re-run the tests from an existing kernel
153rerun:
154	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
155	$(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed."
156
157clean:
158	$(call QUIET_CLEAN, sysroot)
159	$(Q)rm -rf sysroot
160	$(call QUIET_CLEAN, nolibc-test)
161	$(Q)rm -f nolibc-test
162	$(call QUIET_CLEAN, initramfs)
163	$(Q)rm -rf initramfs
164	$(call QUIET_CLEAN, run.out)
165	$(Q)rm -rf run.out
166
167.PHONY: sysroot/$(ARCH)/include
168