Skip to content

Commit

Permalink
Unicorn: allow building in EFI environment.
Browse files Browse the repository at this point in the history
With some restrictions:
- no timeout support in uc_emu_start

Tiano uses its own custom build environment.

Every target emulator is built as a separate lib. Today, only
UnicornLibX86, UnicornLibArm64, UnicornLibRV64 and
UnicornLibRV32 are provided.

Building:
export GCC5_RISCV64_PREFIX=riscv64-linux-gnu build -a RISCV64 -t GCC5 -p unicorn/efi/UnicornPkg.dsc -b RELEASE
export GCC5_AARCH64_PREFIX=aarch64-linux-gnu build -a AARCH64 -t GCC5 -p unicorn/efi/UnicornPkg.dsc -b RELEASE

You get UnicornSampleX86, UnicornSampleArm64 and UnicornSampleRV.
The latter shows how it's possible to link against multiple target
emu libs.

Ran into an AArch64 issue where due to LTO the build of
UnicornSameArm64 takes forever and then bombs out complaining
about missing __ashrti3. This remains to be investigated,
and a w/a is to modify Conf/tools_def.txt to avoid LTO.

Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
  • Loading branch information
andreiw committed Mar 16, 2023
1 parent c4550f6 commit 9fab897
Show file tree
Hide file tree
Showing 74 changed files with 9,445 additions and 7 deletions.
86 changes: 86 additions & 0 deletions efi/UnicornArm64Lib.inf
@@ -0,0 +1,86 @@
## @file
#
# Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = UnicornArm64Lib
FILE_GUID = 6987936E-EFFF-46db-AE97-1FA5E4ED2116
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = UnicornArm64Lib
UNICORN_EMU_ARCH = aarch64

[Sources]
../qemu/exec.c
../qemu/exec-vary.c

../qemu/softmmu/cpus.c
../qemu/softmmu/ioport.c
../qemu/softmmu/memory.c
../qemu/softmmu/memory_mapping.c

../qemu/fpu/softfloat.c

../qemu/tcg/optimize.c
../qemu/tcg/tcg.c
../qemu/tcg/tcg-op.c
../qemu/tcg/tcg-op-gvec.c
../qemu/tcg/tcg-op-vec.c

../qemu/accel/tcg/cpu-exec.c
../qemu/accel/tcg/cpu-exec-common.c
../qemu/accel/tcg/cputlb.c
../qemu/accel/tcg/tcg-all.c
../qemu/accel/tcg/tcg-runtime.c
../qemu/accel/tcg/tcg-runtime-gvec.c
../qemu/accel/tcg/translate-all.c
../qemu/accel/tcg/translator.c

../qemu/target/arm/cpu64.c
../qemu/target/arm/cpu.c
../qemu/target/arm/crypto_helper.c
../qemu/target/arm/debug_helper.c
../qemu/target/arm/helper-a64.c
../qemu/target/arm/helper.c
../qemu/target/arm/iwmmxt_helper.c
../qemu/target/arm/m_helper.c
../qemu/target/arm/neon_helper.c
../qemu/target/arm/op_helper.c
../qemu/target/arm/pauth_helper.c
../qemu/target/arm/psci.c
../qemu/target/arm/sve_helper.c
../qemu/target/arm/tlb_helper.c
../qemu/target/arm/translate-a64.c
../qemu/target/arm/translate.c
../qemu/target/arm/translate-sve.c
../qemu/target/arm/vec_helper.c
../qemu/target/arm/vfp_helper.c
../qemu/target/arm/unicorn_aarch64.c

[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
unicorn/efi/UnicornPkg.dec

[LibraryClasses]

[FeaturePcd]

[Pcd]

[BuildOptions]
GCC:*_*_*_CC_FLAGS = -nostdinc -I$(MODULE_DIR)/.. -I$(MODULE_DIR)/../include -I$(MODULE_DIR)/../qemu -I$(MODULE_DIR)/../qemu/include -I$(MODULE_DIR)/../qemu/tcg -I$(MODULE_DIR)/../qemu/target/arm -I$(MODULE_DIR)/../glib_compat -include $(UNICORN_EMU_ARCH).h -DCONFIG_TARGET_HEADER=\"config-target-$(UNICORN_EMU_ARCH).h\" -DUNICORN_FOR_EFI -DUNICORN_HAS_ARM64 -DNEED_CPU_H -DUNICORN_FOR_EFI_INTERNAL

[BuildOptions.AARCH64]
GCC:*_*_*_CC_FLAGS = -I$(MODULE_DIR)/../qemu/tcg/aarch64

[BuildOptions.RISCV64]
GCC:*_*_*_CC_FLAGS = -I$(MODULE_DIR)/../qemu/tcg/riscv
99 changes: 99 additions & 0 deletions efi/UnicornEngineLib.inf
@@ -0,0 +1,99 @@
## @file
#
# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = UnicornEngineLib
FILE_GUID = 6987936E-ED34-45db-AE97-1FA5E4ED2116
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = UnicornEngineLib

[Sources.RISCV64]
riscv/glue.c
riscv/ctz.c
riscv/clz.c
riscv/popcount.c
riscv/clrsb.c

[Sources.AARCH64]
aarch64/glue.c

[Sources]
glue.c
efi_glue.c
dlmalloc.c
ashrti3.c
divti3.c
udivmodti4.c

../list.c
../glib_compat/garray.c
../glib_compat/glib_compat.c
../glib_compat/glist.c
../glib_compat/grand.c
../glib_compat/gmem.c
../glib_compat/gpattern.c
../glib_compat/gslice.c
../glib_compat/gtestutils.c
../glib_compat/gtree.c
../qemu/util/bitmap.c
../qemu/util/bitops.c
../qemu/util/crc32c.c
../qemu/util/cutils.c
../qemu/util/getauxval.c
../qemu/util/guest-random.c
../qemu/util/host-utils.c
../qemu/util/osdep.c
../qemu/util/oslib-efi.c
../qemu/util/qdist.c
../qemu/util/qemu-timer.c
../qemu/util/qemu-timer-common.c
../qemu/util/range.c
../qemu/util/qht.c
../qemu/util/pagesize.c
../qemu/util/cacheinfo.c
../qemu/crypto/aes.c

../uc.c
../qemu/softmmu/vl.c
../qemu/hw/core/cpu.c

[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
unicorn/efi/UnicornPkg.dec

[LibraryClasses]
BaseLib
DebugLib
TimerLib
BaseMemoryLib
CacheMaintenanceLib
MemoryAllocationLib
UefiBootServicesTableLib
PrintLib

[FeaturePcd]

[Pcd]

[Guids]
gEfiDxeServicesTableGuid

[BuildOptions]
GCC:*_*_*_CC_FLAGS = -nostdinc -I$(MODULE_DIR)/.. -I$(MODULE_DIR)/../include -I$(MODULE_DIR)/../qemu -I$(MODULE_DIR)/../qemu/include -I$(MODULE_DIR)/../qemu/tcg -I$(MODULE_DIR)/../glib_compat -DUNICORN_FOR_EFI -DUNICORN_HAS_X86 -DUNICORN_HAS_ARM -DUNICORN_HAS_ARM64 -DUNICORN_HAS_RISCV -DUNICORN_HAS_MIPS -DUNICORN_HAS_MIPSEL -DUNICORN_HAS_MIPS64 -DUNICORN_HAS_MIPS64EL -DUNICORN_HAS_SPARC -DUNICORN_HAS_M68K -DUNICORN_HAS_PPC -DUNICORN_HAS_S390X -DUNICORN_HAS_TRICORE -DUNICORN_FOR_EFI_INTERNAL

[BuildOptions.AARCH64]
GCC:*_*_*_CC_FLAGS = -I$(MODULE_DIR)/../qemu/tcg/aarch64

[BuildOptions.RISCV64]
GCC:*_*_*_CC_FLAGS = -I$(MODULE_DIR)/../qemu/tcg/riscv -I$(MODULE_DIR)/include/riscv
20 changes: 20 additions & 0 deletions efi/UnicornPkg.dec
@@ -0,0 +1,20 @@
## @file
#
# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
##

[Defines]
DEC_SPECIFICATION = 0x00010005
PACKAGE_NAME = UnicornPkg
PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6755
PACKAGE_VERSION = 1.00

[Includes]
include
../include/unicorn
63 changes: 63 additions & 0 deletions efi/UnicornPkg.dsc
@@ -0,0 +1,63 @@
## @file
#
# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
##

[Defines]
PLATFORM_NAME = Unicorn
PLATFORM_GUID = 587DB498-6CBE-43cd-94E2-186218569478
PLATFORM_VERSION = 0.98
DSC_SPECIFICATION = 0x00010005
OUTPUT_DIRECTORY = Build/Unicorn
SUPPORTED_ARCHITECTURES = AARCH64|RISCV64
BUILD_TARGETS = DEBUG|RELEASE|NOOPT
SKUID_IDENTIFIER = DEFAULT

!include unicorn/efi/UnicornPkg.dsc.inc

[PcdsFixedAtBuild.common]
# DEBUG_ASSERT_ENABLED 0x01
# DEBUG_PRINT_ENABLED 0x02
# DEBUG_CODE_ENABLED 0x04
# CLEAR_MEMORY_ENABLED 0x08
# ASSERT_BREAKPOINT_ENABLED 0x10
# ASSERT_DEADLOOP_ENABLED 0x20
!if $(TARGET) == RELEASE
gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x23
!else
gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2f
!endif

# DEBUG_INIT 0x00000001 // Initialization
# DEBUG_WARN 0x00000002 // Warnings
# DEBUG_LOAD 0x00000004 // Load events
# DEBUG_FS 0x00000008 // EFI File system
# DEBUG_POOL 0x00000010 // Alloc & Free (pool)
# DEBUG_PAGE 0x00000020 // Alloc & Free (page)
# DEBUG_INFO 0x00000040 // Informational debug messages
# DEBUG_DISPATCH 0x00000080 // PEI/DXE/SMM Dispatchers
# DEBUG_VARIABLE 0x00000100 // Variable
# DEBUG_BM 0x00000400 // Boot Manager
# DEBUG_BLKIO 0x00001000 // BlkIo Driver
# DEBUG_NET 0x00004000 // SNP Driver
# DEBUG_UNDI 0x00010000 // UNDI Driver
# DEBUG_LOADFILE 0x00020000 // LoadFile
# DEBUG_EVENT 0x00080000 // Event messages
# DEBUG_GCD 0x00100000 // Global Coherency Database changes
# DEBUG_CACHE 0x00200000 // Memory range cachability changes
# DEBUG_VERBOSE 0x00400000 // Detailed debug messages that may
# // significantly impact boot performance
# DEBUG_ERROR 0x80000000 // Error
gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x8000004F

[Components]
unicorn/efi/UnicornSampleX86.inf
unicorn/efi/UnicornSampleRV.inf
unicorn/efi/UnicornSampleArm64.inf

90 changes: 90 additions & 0 deletions efi/UnicornPkg.dsc.inc
@@ -0,0 +1,90 @@
## @file
#
# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
##

[LibraryClasses]
#
# Entry point
#
UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
#
# Basic
#
BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf

#
# UEFI & PI
#
UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
#
# Since software stack checking may be heuristically enabled by the compiler
# include BaseStackCheckLib unconditionally.
#
NULL|MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf
#
# Misc.
#
# When building UnicornPkg.dsc (i.e. the samples), always build with
# a ConOut DebugLib because printfs are wired up to DebugLib. When building
# as a library used elsewhere, it's assumed that no output will be desired
# by default.
#
!if $(TARGET) == RELEASE && $(PLATFORM_NAME) != Unicorn
DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
!else
DebugLib|MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf
!endif
DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
[LibraryClasses.AARCH64]
ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
#
# It is not possible to prevent ARM compiler calls to generic intrinsic functions.
# This library provides the instrinsic functions generated by a given compiler.
# [LibraryClasses.ARM] and NULL mean link this library into all ARM images.
#
NULL|ArmPkg/Library/CompilerIntrinsicsLib/CompilerIntrinsicsLib.inf
TimerLib|ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.inf
ArmGenericTimerCounterLib|ArmPkg/Library/ArmGenericTimerPhyCounterLib/ArmGenericTimerPhyCounterLib.inf
CacheMaintenanceLib|ArmPkg/Library/ArmCacheMaintenanceLib/ArmCacheMaintenanceLib.inf
[LibraryClasses.RISCV64]
TimerLib|UefiCpuPkg/Library/CpuTimerLib/BaseCpuTimerLib.inf
CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf
[LibraryClasses.common.UEFI_APPLICATION, LibraryClasses.common.DXE_DRIVER]
MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
#
# Unicorn
#
UnicornEngineLib|unicorn/efi/UnicornEngineLib.inf
UnicornStubLib|unicorn/efi/UnicornStubLib.inf
UnicornRV64Lib|unicorn/efi/UnicornRV64Lib.inf
UnicornRV32Lib|unicorn/efi/UnicornRV32Lib.inf
UnicornX86Lib|unicorn/efi/UnicornX86Lib.inf
UnicornArm64Lib|unicorn/efi/UnicornArm64Lib.inf
[PcdsFeatureFlag]
[PcdsDynamicExDefault]
[Components]
[BuildOptions]
GCC:*_*_AARCH64_CC_XIPFLAGS ==
[BuildOptions.AARCH64]
GCC:*_*_*_CC_FLAGS = -Wno-psabi

0 comments on commit 9fab897

Please sign in to comment.