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 <jacz@semihalf.com>
Change-Id: Ie3a6e6f6cad2f8a2e48a8e546d3b79c577653080
Reviewed-on: https://review.coreboot.org/c/coreboot/+/57708
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Jakub Czapiga
2021-09-15 14:52:45 +02:00
committed by Felix Held
parent b40fdbaa64
commit 3d91b47b42
19 changed files with 329 additions and 2 deletions

View File

@@ -26,8 +26,13 @@
## SUCH DAMAGE.
##
# Sample libpayload Makefile.
include ../.config
ifeq ($(CONFIG_LP_ARCH_MOCK),y)
$(error This sample program does not support ARCH_MOCK. Use sample/arch_mock instead)
endif
include ../build/xcompile
ARCH-$(CONFIG_LP_ARCH_ARM) := arm

View File

@@ -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

View File

@@ -0,0 +1,13 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* Example file for libpayload. */
#include <libpayload-config.h>
#include <libpayload.h>
int main(void)
{
printf("Hello world!\n");
halt();
return 0;
}

View File

@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <libpayload-config.h>
#include <arch/types.h>
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
/* 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);
}