Skip to content

Commit

Permalink
Merge branch 'boot' into new_intr
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Aug 5, 2024
2 parents 27585ac + 9466fcf commit b89f2ae
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 121 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
# 可选项,qemu7.0 自带了 opensbi1.0
-bios ${opensbi_BINARY_DIR}/platform/generic/firmware/fw_jump.elf
-kernel $<TARGET_FILE:kernel>
# @todo 暂时还不支持 riscv64 的 uefi 启动
# 预期路线: qemu->uefi->opensbi + kernel(payload)
# -bios ${ovmf_BINARY_DIR}/OVMF_${CMAKE_SYSTEM_PROCESSOR}.fd
# -hda fat:rw:./image/
)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
list(APPEND QEMU_FLAGS
Expand All @@ -81,7 +85,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/doc)

# 添加 run 和 debug target
add_run_target(
DEPENDS $<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},x86_64>:boot> $<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},aarch64>:boot> kernel ${RUN_DEPENDS} ${DEBUG_DEPENDS}
DEPENDS $<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},x86_64>:boot> $<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},riscv64>:boot> $<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},aarch64>:boot> kernel
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
TARGET ${CMAKE_SYSTEM_PROCESSOR}
BOOT ${PROJECT_BINARY_DIR}/src/boot/boot.efi
Expand Down
180 changes: 89 additions & 91 deletions cmake/3rd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -121,99 +121,97 @@ if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64")
add_subdirectory(3rd/opensbi_interface)
endif ()

if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
# https://git.kernel.org/pub/scm/utils/dtc/dtc.git
set(dtc_SOURCE_DIR ${CMAKE_SOURCE_DIR}/3rd/dtc)
set(dtc_BINARY_DIR ${CMAKE_BINARY_DIR}/3rd/dtc)
set(dtc_CC ${CMAKE_C_COMPILER})
set(dtc_AR ${CMAKE_AR})
# 编译 libfdt
add_custom_target(dtc
COMMENT "build libdtc..."
# make 时编译
ALL
WORKING_DIRECTORY ${dtc_SOURCE_DIR}
COMMAND
${CMAKE_COMMAND}
-E
make_directory
${dtc_BINARY_DIR}/libfdt
COMMAND
CC=${dtc_CC}
AR=${dtc_AR}
HOME=${dtc_BINARY_DIR}
make libfdt/libfdt.a
COMMAND
${CMAKE_COMMAND}
-E
copy
${dtc_SOURCE_DIR}/libfdt/*.a
${dtc_SOURCE_DIR}/libfdt/*.h
${dtc_BINARY_DIR}/libfdt
COMMAND
make clean
)
endif ()

if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
# https://github.com/ncroxon/gnu-efi.git
set(gnu-efi_SOURCE_DIR ${CMAKE_SOURCE_DIR}/3rd/gnu-efi)
set(gnu-efi_BINARY_DIR ${CMAKE_BINARY_DIR}/3rd/gnu-efi)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR)
set(CC_ ${CMAKE_C_COMPILER})
set(AR_ ${CMAKE_AR})
elseif (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
set(CROSS_COMPILE_ x86_64-linux-gnu-)
endif ()
# 编译 gnu-efi
add_custom_target(gnu-efi
COMMENT "build gnu-efi..."
# make 时编译
ALL
WORKING_DIRECTORY ${gnu-efi_SOURCE_DIR}
COMMAND
${CMAKE_COMMAND}
-E
make_directory
${gnu-efi_BINARY_DIR}
COMMAND
# @note 仅支持 gcc
make lib gnuefi inc
CROSS_COMPILE=${CROSS_COMPILE_}
ARCH=${CMAKE_SYSTEM_PROCESSOR}
OBJDIR=${gnu-efi_BINARY_DIR}
V=1
COMMAND
${CMAKE_COMMAND}
-E
copy_directory
${gnu-efi_SOURCE_DIR}/inc
${gnu-efi_BINARY_DIR}/inc
)
# https://git.kernel.org/pub/scm/utils/dtc/dtc.git
set(dtc_SOURCE_DIR ${CMAKE_SOURCE_DIR}/3rd/dtc)
set(dtc_BINARY_DIR ${CMAKE_BINARY_DIR}/3rd/dtc)
set(dtc_CC ${CMAKE_C_COMPILER})
set(dtc_AR ${CMAKE_AR})
# 编译 libfdt
add_custom_target(dtc
COMMENT "build libdtc..."
# make 时编译
ALL
WORKING_DIRECTORY ${dtc_SOURCE_DIR}
COMMAND
${CMAKE_COMMAND}
-E
make_directory
${dtc_BINARY_DIR}/libfdt
COMMAND
CC=${dtc_CC}
AR=${dtc_AR}
HOME=${dtc_BINARY_DIR}
make libfdt/libfdt.a
COMMAND
${CMAKE_COMMAND}
-E
copy
${dtc_SOURCE_DIR}/libfdt/*.a
${dtc_SOURCE_DIR}/libfdt/*.h
${dtc_BINARY_DIR}/libfdt
COMMAND
make clean
)

# ovmf
# @todo 使用互联网连接或从 edk2 编译
# https://efi.akeo.ie/QEMU_EFI/QEMU_EFI-AA64.zip
set(ovmf_SOURCE_DIR ${CMAKE_SOURCE_DIR}/tools/ovmf)
set(ovmf_BINARY_DIR ${CMAKE_BINARY_DIR}/3rd/ovmf)
add_custom_target(ovmf
COMMENT "build ovmf ..."
# make 时编译
ALL
WORKING_DIRECTORY ${ovmf_SOURCE_DIR}
COMMAND
${CMAKE_COMMAND}
-E
make_directory
${ovmf_BINARY_DIR}
COMMAND
${CMAKE_COMMAND}
-E
copy
${ovmf_SOURCE_DIR}/*
${ovmf_BINARY_DIR}
)
# https://github.com/ncroxon/gnu-efi.git
set(gnu-efi_SOURCE_DIR ${CMAKE_SOURCE_DIR}/3rd/gnu-efi)
set(gnu-efi_BINARY_DIR ${CMAKE_BINARY_DIR}/3rd/gnu-efi)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR)
set(CC_ ${CMAKE_C_COMPILER})
set(AR_ ${CMAKE_AR})
elseif (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
set(CROSS_COMPILE_ x86_64-linux-gnu-)
elseif (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_SYSTEM_PROCESSOR MATCHES "riscv64")
set(CROSS_COMPILE_ riscv64-linux-gnu-)
endif ()
# 编译 gnu-efi
add_custom_target(gnu-efi
COMMENT "build gnu-efi..."
# make 时编译
ALL
WORKING_DIRECTORY ${gnu-efi_SOURCE_DIR}
COMMAND
${CMAKE_COMMAND}
-E
make_directory
${gnu-efi_BINARY_DIR}
COMMAND
# @note 仅支持 gcc
make lib gnuefi inc
CROSS_COMPILE=${CROSS_COMPILE_}
ARCH=${CMAKE_SYSTEM_PROCESSOR}
OBJDIR=${gnu-efi_BINARY_DIR}
V=1
COMMAND
${CMAKE_COMMAND}
-E
copy_directory
${gnu-efi_SOURCE_DIR}/inc
${gnu-efi_BINARY_DIR}/inc
)

# ovmf
# @todo 使用互联网连接或从 edk2 编译
# https://efi.akeo.ie/QEMU_EFI/QEMU_EFI-AA64.zip
set(ovmf_SOURCE_DIR ${CMAKE_SOURCE_DIR}/tools/ovmf)
set(ovmf_BINARY_DIR ${CMAKE_BINARY_DIR}/3rd/ovmf)
add_custom_target(ovmf
COMMENT "build ovmf ..."
# make 时编译
ALL
WORKING_DIRECTORY ${ovmf_SOURCE_DIR}
COMMAND
${CMAKE_COMMAND}
-E
make_directory
${ovmf_BINARY_DIR}
COMMAND
${CMAKE_COMMAND}
-E
copy
${ovmf_SOURCE_DIR}/*
${ovmf_BINARY_DIR}
)

# gdb
find_program(GDB_EXE gdb)
Expand Down
19 changes: 19 additions & 0 deletions cmake/compile_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ list(APPEND DEFAULT_BOOT_LINK_OPTIONS
-Wl,-Bsymbolic
>

$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},riscv64>:
# 编译为共享库
-shared
# 符号级别绑定
-Wl,-Bsymbolic
>

$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},aarch64>:
-shared
-Wl,-Bsymbolic
Expand All @@ -124,6 +131,14 @@ list(APPEND DEFAULT_BOOT_LINK_LIB
${gnu-efi_BINARY_DIR}/lib/libefi.a
>

$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},riscv64>:
# 链接 gnu-efi
${gnu-efi_BINARY_DIR}/gnuefi/reloc_${CMAKE_SYSTEM_PROCESSOR}.o
${gnu-efi_BINARY_DIR}/gnuefi/crt0-efi-${CMAKE_SYSTEM_PROCESSOR}.o
${gnu-efi_BINARY_DIR}/gnuefi/libgnuefi.a
${gnu-efi_BINARY_DIR}/lib/libefi.a
>

$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},aarch64>:
# 链接 gnu-efi
${gnu-efi_BINARY_DIR}/gnuefi/reloc_${CMAKE_SYSTEM_PROCESSOR}.o
Expand Down Expand Up @@ -194,18 +209,22 @@ list(APPEND DEFAULT_KERNEL_LINK_LIB
# 编译依赖
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
list(APPEND COMPILE_DEPENDS
ovmf
gnu-efi
printf_bare_metal
)
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64")
list(APPEND COMPILE_DEPENDS
ovmf
opensbi
opensbi_interface
printf_bare_metal
dtc
gnu-efi
)
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
list(APPEND COMPILE_DEPENDS
ovmf
gnu-efi
dtc
)
Expand Down
6 changes: 6 additions & 0 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ function(add_run_target)
list(APPEND commands
COMMAND ${CMAKE_COMMAND} -E copy ${ARG_BOOT} image/
)
elseif (${ARG_TARGET} STREQUAL "riscv64")
get_filename_component(BOOT_FILE_NAME ${ARG_BOOT} NAME)
configure_file(${CMAKE_SOURCE_DIR}/tools/startup.nsh.in image/startup.nsh @ONLY)
list(APPEND commands
COMMAND ${CMAKE_COMMAND} -E copy ${ARG_BOOT} image/
)
elseif (${ARG_TARGET} STREQUAL "aarch64")
get_filename_component(BOOT_FILE_NAME ${ARG_BOOT} NAME)
configure_file(${CMAKE_SOURCE_DIR}/tools/startup.nsh.in image/startup.nsh @ONLY)
Expand Down
21 changes: 0 additions & 21 deletions cmake/project_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,6 @@ if (NOT ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR "${CMAKE_CXX_COMPILER_ID}"
message(FATAL_ERROR "Only support gnu-gcc/clang")
endif ()

# qemu 运行依赖
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
list(APPEND RUN_DEPENDS
ovmf
)
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64")
list(APPEND RUN_DEPENDS
opensbi
)
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
list(APPEND RUN_DEPENDS
ovmf
)
endif ()

# qemu 调试依赖
list(APPEND DEBUG_DEPENDS
${RUN_DEPENDS}
gdbinit
)

# qemu gdb 调试端口
if (NOT DEFINED QEMU_GDB_PORT)
set(QEMU_GDB_PORT tcp::1234)
Expand Down
5 changes: 1 addition & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@
#
# CMakeLists.txt for Simple-XX/SimpleKernel.

# riscv64 不支持 uefi
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/boot)
endif ()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/boot)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernel)
2 changes: 2 additions & 0 deletions src/boot/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include "project_config.h"

// efi 使用的全局变量
#if defined(__x86_64__)
uintptr_t ImageBase = 0;
#endif

extern "C" [[maybe_unused]] EFI_STATUS EFIAPI
efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) {
Expand Down
5 changes: 3 additions & 2 deletions test/system_test/cxx_init_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ objdump_readelf_nm(${PROJECT_NAME})
# 添加 run 和 debug target
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
add_run_target(NAME ${PROJECT_NAME}_
DEPENDS boot ${PROJECT_NAME} ${RUN_DEPENDS} ${DEBUG_DEPENDS}
DEPENDS boot ${PROJECT_NAME}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
TARGET ${CMAKE_SYSTEM_PROCESSOR}
BOOT ${boot_BINARY_DIR}/${BOOT_EFI_OUTPUT_NAME}
Expand All @@ -62,9 +62,10 @@ if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
)
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64")
add_run_target(NAME ${PROJECT_NAME}_
DEPENDS ${PROJECT_NAME} ${RUN_DEPENDS} ${DEBUG_DEPENDS}
DEPENDS boot ${PROJECT_NAME}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
TARGET ${CMAKE_SYSTEM_PROCESSOR}
BOOT ${boot_BINARY_DIR}/${BOOT_EFI_OUTPUT_NAME}
KERNEL $<TARGET_FILE:${PROJECT_NAME}>
QEMU_FLAGS
-serial stdio -monitor telnet::2333,server,nowait
Expand Down
2 changes: 1 addition & 1 deletion test/system_test/gnu_efi_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ objdump_readelf_nm(${PROJECT_NAME})

# 添加 run 和 debug target
add_run_target(NAME ${PROJECT_NAME}_
DEPENDS boot ${PROJECT_NAME} ${RUN_DEPENDS} ${DEBUG_DEPENDS}
DEPENDS boot ${PROJECT_NAME}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
TARGET ${CMAKE_SYSTEM_PROCESSOR}
BOOT ${boot_BINARY_DIR}/${BOOT_EFI_OUTPUT_NAME}
Expand Down
3 changes: 2 additions & 1 deletion test/system_test/opensbi_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ objdump_readelf_nm(${PROJECT_NAME})

# 添加 run 和 debug target
add_run_target(NAME ${PROJECT_NAME}_
DEPENDS ${PROJECT_NAME} ${RUN_DEPENDS} ${DEBUG_DEPENDS}
DEPENDS ${PROJECT_NAME}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
TARGET ${CMAKE_SYSTEM_PROCESSOR}
BOOT ${boot_BINARY_DIR}/${BOOT_EFI_OUTPUT_NAME}
KERNEL $<TARGET_FILE:${PROJECT_NAME}>
QEMU_FLAGS
-serial stdio -monitor telnet::2333,server,nowait
Expand Down
3 changes: 3 additions & 0 deletions tools/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ RUN apt install --no-install-recommends --fix-missing -y \
sudo \
zsh \
zip \
unzip \
telnet \
tar
RUN apt install --no-install-recommends --fix-missing -y \
file \
vim \
doxygen \
valgrind \
Expand Down
Binary file added tools/ovmf/OVMF_riscv64.fd
Binary file not shown.

0 comments on commit b89f2ae

Please sign in to comment.