From 3d91b47b425ffb880c0ed6c45fd04ba48d95ca8d Mon Sep 17 00:00:00 2001 From: Jakub Czapiga Date: Wed, 15 Sep 2021 14:52:45 +0200 Subject: libpayload: Add mock architecture Mock architecture can be used to build libpayload using host compiler. It can be enabled by setting ARCH_MOCK=y in the dotconfig. It sets LITTLE_ENDIAN=y, as most machines these days use little-endian CPUs. Libpayload will use HOSTCC as CC, HOSTLD as LD, etc. instead of tools provided by xcompile. Mock architecture configuration can be used by payloads for testing purposes. Thanks to it, tests can be architecture-independent, and can be executed without requiring compatible Kconfig options, e.g. ARCH_ARM64=y for ARM64 machine. However, one has to provide implementation for most architecture-specific functions present in arch/* directories. Signed-off-by: Jakub Czapiga Change-Id: Ie3a6e6f6cad2f8a2e48a8e546d3b79c577653080 Reviewed-on: https://review.coreboot.org/c/coreboot/+/57708 Tested-by: build bot (Jenkins) Reviewed-by: Julius Werner --- payloads/libpayload/sample/arch_mock/Makefile | 48 ++++++++++++++++++++++ payloads/libpayload/sample/arch_mock/hello.c | 13 ++++++ payloads/libpayload/sample/arch_mock/hello_mocks.c | 43 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 payloads/libpayload/sample/arch_mock/Makefile create mode 100644 payloads/libpayload/sample/arch_mock/hello.c create mode 100644 payloads/libpayload/sample/arch_mock/hello_mocks.c (limited to 'payloads/libpayload/sample/arch_mock') diff --git a/payloads/libpayload/sample/arch_mock/Makefile b/payloads/libpayload/sample/arch_mock/Makefile new file mode 100644 index 0000000000..a1e748111e --- /dev/null +++ b/payloads/libpayload/sample/arch_mock/Makefile @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: GPL-2.0-only + +# Sample libpayload Makefile for ARCH_MOCK +# ARCH_MOCK is not intended to be used with xcompile +include ../../.config + +ifneq ($(CONFIG_LP_ARCH_MOCK),y) +$(error This example supports ARCH_MOCK only.) +endif + +CC := gcc +AS := as +OBJCOPY := objcopy +LIBPAYLOAD_DIR := ../../install/libpayload +CFLAGS := -fno-builtin -Wall -Werror -Os \ + -include $(LIBPAYLOAD_DIR)/include/kconfig.h \ + -include $(LIBPAYLOAD_DIR)/include/compiler.h \ + -I $(LIBPAYLOAD_DIR)/include \ + -I $(LIBPAYLOAD_DIR)/include/mock \ + -ffunction-sections \ + -fdata-sections -g3 +LDFLAGS := -Wl,--gc-sections +TARGET := hello +OBJS := $(TARGET).o +OBJS-mock := $(TARGET)_mocks.o +LIBPAYLOAD-local := libpayload.a +mocks := console_write + +all: $(TARGET).elf + +$(TARGET).elf: $(OBJS) $(OBJS-mock) $(LIBPAYLOAD-local) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBPAYLOAD-local) \ + -Wl,--exclude-libs,ALL -lc $(OBJS-mock) + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +%.S.o: %.S + $(AS) --32 -o $@ $< + +# Copy libpayload and weaken all mocked symbols +$(LIBPAYLOAD-local): $(LIBPAYLOAD_DIR)/lib/libpayload.a + $(OBJCOPY) $(foreach mock,$(mocks),--weaken-symbol=$(mock)) $< $@ + +clean: + rm -f $(TARGET).elf *.o $(LIBPAYLOAD-local) + +distclean: clean diff --git a/payloads/libpayload/sample/arch_mock/hello.c b/payloads/libpayload/sample/arch_mock/hello.c new file mode 100644 index 0000000000..5a96e42e75 --- /dev/null +++ b/payloads/libpayload/sample/arch_mock/hello.c @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* Example file for libpayload. */ + +#include +#include + +int main(void) +{ + printf("Hello world!\n"); + halt(); + return 0; +} diff --git a/payloads/libpayload/sample/arch_mock/hello_mocks.c b/payloads/libpayload/sample/arch_mock/hello_mocks.c new file mode 100644 index 0000000000..84e86ff257 --- /dev/null +++ b/payloads/libpayload/sample/arch_mock/hello_mocks.c @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include +#include + +/* Use libc version. calling exit() or abort() would cause infinite recursion */ +__attribute__((noreturn)) +void _exit(int); + +__attribute__((noreturn)) +void halt(void) +{ + _exit(0); +} + +#define TEST_SYMBOL(symbol, value) asm(".set " #symbol ", " #value "\n\t.globl " #symbol) + +#define TEST_REGION(region, size) uint8_t _##region[size]; \ + TEST_SYMBOL(_e##region, _##region + size); \ + TEST_SYMBOL(_##region##_size, size) + +TEST_REGION(heap, CONFIG_LP_HEAP_SIZE); + +uint64_t timer_raw_value(void) +{ + return 0; +} + +uint64_t timer_hz(void) +{ + return 0; +} + +/* Not present in libpayload. Can be used to write to real stdout. */ +ssize_t write(int fildes, const void *buf, size_t nbyte); + +void console_write(const void *buffer, size_t count) +{ + write(1, buffer, count); +} -- cgit v1.2.3