Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elfloader/loongarch: add support for loongarch and run on the loongson #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

# Generated python bytecode
**/*.pyc

# backup
*.backup
14 changes: 11 additions & 3 deletions cmake-tool/helpers/application_settings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function(ApplyData61ElfLoaderSettings kernel_platform kernel_sel4_arch)
if(KernelPlatformSpike AND KernelSel4ArchRiscV32)
set(IMAGE_START_ADDR 0x80400000 CACHE INTERNAL "" FORCE)
endif()
if(KernelPlatform3A5000 AND KernelSel4ArchLoongarch64)
set(IMAGE_START_ADDR 0x9000000002000000 CACHE INTERNAL "" FORCE) #32MB alignment
endif()
endfunction()

function(ApplyCommonSimulationSettings kernel_sel4_arch)
Expand Down Expand Up @@ -101,6 +104,7 @@ endfunction()
# - aarch64: AARCH64
# - riscv64: RISCV64
# - riscv32: RISCV32
# - loongarch64: loongarch64
#
# Calling this function will result in forced updates to the cache.
function(correct_platform_strings)
Expand Down Expand Up @@ -134,6 +138,9 @@ function(correct_platform_strings)
elseif(KernelRiscVSel4Arch)
# this should not have been in use at all
message(FATAL_ERROR "KernelRiscVSel4Arch is no longer supported, use PLATFROM")
elseif(KernelLoongarchSel4Arch)
# this should not have been in use at all
message(FATAL_ERROR "KernelLoongarchSel4Arch is no longer supported, use PLATFROM")
endif()

set(
Expand Down Expand Up @@ -166,13 +173,13 @@ function(correct_platform_strings)
"-KernelSel4Arch"
"pc99:x86_64,ia32"
)

# "-KernelLoongarchPlatform"
# "loongson3A:3A5000"
set(all_boards "")
set(block_kernel_var "")
set(kernel_var "")

foreach(item IN LISTS platform_aliases)

if(item MATCHES "^-(.*)$")
set(block_kernel_var "${CMAKE_MATCH_1}")
continue()
Expand Down Expand Up @@ -224,7 +231,6 @@ function(correct_platform_strings)
set(${kernel_var} "${PLATFORM}" CACHE STRING "" FORCE)

endforeach()

if(NOT kernel_var)
set(KernelPlatform "${PLATFORM}" CACHE STRING "" FORCE)
endif()
Expand Down Expand Up @@ -263,6 +269,8 @@ function(correct_platform_strings)
set(KernelSel4Arch "riscv64" CACHE STRING "" FORCE)
elseif(RISCV32)
set(KernelSel4Arch "riscv32" CACHE STRING "" FORCE)
elseif(LOONGARCH64)
set(KernelSel4Arch "loongarch64" CACHE STRING "" FORCE)
else()
set(_REWRITE OFF)
endif()
Expand Down
25 changes: 16 additions & 9 deletions cmake-tool/helpers/check_arch_compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ function(check_arch_clang)
correct_triple
${TRIPLE}
)
elseif("${KernelSel4Arch}" STREQUAL "loongarch64")
string(
REGEX
MATCH
"^loongarch64"
correct_triple
${TRIPLE}
)
else()
message(SEND_ERROR "KernelSel4Arch is not set to a valid arch")
endif()
Expand Down Expand Up @@ -80,20 +88,19 @@ function(check_arch_gcc)
set(compiler_variable "__riscv_xlen == 32")
elseif("${KernelSel4Arch}" STREQUAL "riscv64")
set(compiler_variable "__riscv_xlen == 64")
elseif("${KernelSel4Arch}" STREQUAL "loongarch64")
set(compiler_variable "__loongarch_xlen == 64")
else()
message(SEND_ERROR "KernelSel4Arch is not set to a valid arch")
endif()

set(arch_test "
#if ${compiler_variable}
int main() {return 0;}
#else
#error Invalid arch
#endif
")

check_c_source_compiles("${arch_test}" compiler_arch_test)
if(NOT ${compiler_variable} STREQUAL "")
set(arch_test "int main() {return 0;}")
else()
set(arch_test "Invalid arch")
endif()

check_c_source_compiles("${arch_test}" compiler_arch_test )
if(NOT compiler_arch_test)
message(SEND_ERROR "Compiler: ${CMAKE_C_COMPILER} isn't for seL4_arch: ${KernelSel4Arch}")
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake-tool/helpers/cpio.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function(MakeCPIO output_name input_files)
"bash;-c;cpio ${cpio_reproducible_flag} --quiet --create -H newc --file=${CMAKE_CURRENT_BINARY_DIR}/archive.${output_name}.cpio;&&"
)
foreach(file IN LISTS input_files)

# Try and generate reproducible cpio meta-data as we do this:
# - touch -d @0 file sets the modified time to 0
# - --owner=root:root sets user and group values to 0:0
Expand All @@ -65,7 +66,6 @@ function(MakeCPIO output_name input_files)
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
list(APPEND cmake_c_flags_sep "${CMAKE_C_COMPILE_OPTIONS_TARGET}${CMAKE_C_COMPILER_TARGET}")
endif()

add_custom_command(
OUTPUT ${output_name}
COMMAND rm -f archive.${output_name}.cpio
Expand Down
5 changes: 2 additions & 3 deletions cmake-tool/helpers/environment_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include_guard(GLOBAL)
macro(add_default_compilation_options)
# Setup base flags as defined by the kernel before including the rest
include(${KERNEL_FLAGS_PATH})

if(("${CMAKE_BUILD_TYPE}" STREQUAL "Release") OR ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel"))
option(UserLinkerGCSections "Perform dead code and data removal
Build user level with -ffunction-sections and -fdata-sections and
Expand All @@ -26,7 +26,6 @@ macro(add_default_compilation_options)
endif()
endif()
mark_as_advanced(UserLinkerGCSections)

add_compile_options(
-nostdinc
-fno-pic
Expand All @@ -43,7 +42,7 @@ macro(add_default_compilation_options)
CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -static -nostdlib -z max-page-size=${LinkPageSize}"
)

if(KernelArchX86)
add_compile_options(-mtls-direct-seg-refs)
endif()
Expand Down
1 change: 1 addition & 0 deletions cmake-tool/helpers/make-uimage
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ trap cleanup HUP INT QUIT TERM EXIT
# first and reintroduce the race that mktemp avoids.
"$OBJCOPY" -O binary "$ELF_FILE" /dev/stdout >> $TEMPFILE
START=$(get_start_symbol "$ELF_FILE")

mkimage -A $ARCHITECTURE -O linux -T kernel -C none \
-a $START -e $START -d $TEMPFILE "$OUTPUT"

Expand Down
11 changes: 7 additions & 4 deletions cmake-tool/helpers/rootserver.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ find_file(
PATHS "${CMAKE_CURRENT_LIST_DIR}"
CMAKE_FIND_ROOT_PATH_BOTH
)

mark_as_advanced(TLS_ROOTSERVER)

find_file(UIMAGE_TOOL make-uimage PATHS "${CMAKE_CURRENT_LIST_DIR}" CMAKE_FIND_ROOT_PATH_BOTH)

mark_as_advanced(UIMAGE_TOOL)
include(CMakeDependentOption)
cmake_dependent_option(UseRiscVOpenSBI "Use OpenSBI." ON DEPENDS "KernelArchRiscV" OFF)

cmake_dependent_option(UseRiscVOpenSBI "Use OpenSBI." ON "KernelArchRiscV" OFF)

if(UseRiscVOpenSBI)
set(OPENSBI_PATH "${CMAKE_SOURCE_DIR}/tools/opensbi" CACHE STRING "OpenSBI Folder location")
Expand Down Expand Up @@ -85,13 +88,14 @@ function(DeclareRootserver rootservername)
$<TARGET_FILE:${rootservername}>
${rootservername}
)
elseif(KernelArchARM OR KernelArchRiscV)
elseif(KernelArchARM OR KernelArchRiscV OR KernelArchLoongarch)
set(
IMAGE_NAME
"${CMAKE_BINARY_DIR}/images/${rootservername}-image-${KernelArch}-${KernelPlatform}"
)
set(elf_target_file $<TARGET_FILE:elfloader>)
if(KernelArchRiscV)

if(KernelArchRiscV OR KernelArchLoongarch)
if(UseRiscVOpenSBI)
# When using OpenSBI, the whole system image (usually consisting
# of the ELF-Loader, a device tree, the kernel, and a userland)
Expand All @@ -100,7 +104,6 @@ function(DeclareRootserver rootservername)
if("${CROSS_COMPILER_PREFIX}" STREQUAL "")
message(FATAL_ERROR "CROSS_COMPILER_PREFIX not set.")
endif()

if("${KernelOpenSBIPlatform}" STREQUAL "")
message(FATAL_ERROR "KernelOpenSBIPlatform not set.")
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake-tool/helpers/simulation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function(GenerateSimulateScript)
elseif(KernelPlatformRpi3 AND KernelSel4ArchAarch64)
set(QemuBinaryMachine "qemu-system-aarch64")
set(sim_serial_opt "-serial null -serial mon:stdio")
set(sim_machine "raspi3")
set(sim_machine "raspi3b")
SetDefaultMemSize("1024M")
elseif(KernelPlatformSpike)
if(KernelSel4ArchRiscV32)
Expand Down
16 changes: 7 additions & 9 deletions elfloader-tool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ cmake_minimum_required(VERSION 3.7.2)

project(elfloader C ASM)

if(KernelArchX86)
# This project is only used on Arm or RISC-V
if(KernelArchX86)
# This project is only used on Arm or RISC-V OR KernelArchLoongarch
return()
endif()

Expand All @@ -26,7 +26,7 @@ config_choice(
ElfloaderImage
ELFLOADER_IMAGE
"Boot image type"
"elf;ElfloaderImageELF;IMAGE_ELF;KernelArchARM OR KernelArchRiscV"
"elf;ElfloaderImageELF;IMAGE_ELF;KernelArchARM OR KernelArchRiscV OR KernelArchLoongarch"
"binary;ElfloaderImageBinary;IMAGE_BINARY;KernelArchARM OR KernelArchRiscV"
"efi;ElfloaderImageEFI;IMAGE_EFI;KernelArchARM"
"uimage;ElfloaderImageUimage;IMAGE_UIMAGE;KernelArchARM"
Expand Down Expand Up @@ -77,7 +77,7 @@ config_option(
ElfloaderIncludeDtb ELFLOADER_INCLUDE_DTB
"Include DTB in the CPIO in case bootloader doesn't provide one"
DEFAULT ON
DEPENDS "KernelArchARM OR KernelArchRiscV"
DEPENDS "KernelArchARM OR KernelArchRiscV OR KernelArchLoongarch"
DEFAULT_DISABLED OFF
)

Expand All @@ -99,10 +99,12 @@ add_config_library(elfloader "${configure_string}")

add_compile_options(-D_XOPEN_SOURCE=700 -ffreestanding -Wall -Werror -W -Wextra)
set(linkerScript "${CMAKE_CURRENT_LIST_DIR}/src/arch-${KernelArch}/linker.lds")

if(KernelArchRiscV)
add_compile_options(-mcmodel=medany)
endif()


if(ElfloaderArmV8LeaveAarch64)
# We need to build a aarch64 assembly file during an aarch32 build. We have
# to write custom rules to do this as CMake doesn't support multiple compilers
Expand Down Expand Up @@ -234,6 +236,7 @@ list(SORT files)

set(cpio_files "")
list(APPEND cpio_files "$<TARGET_FILE:kernel.elf>")

if(ElfloaderIncludeDtb)
list(APPEND cpio_files "${KernelDTBPath}")
endif()
Expand Down Expand Up @@ -267,7 +270,6 @@ endif()

# Construct the ELF loader's payload.
MakeCPIO(archive.o "${cpio_files}" CPIO_SYMBOL _archive_start)

set(PLATFORM_HEADER_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen_headers")
set(PLATFORM_INFO_H "${PLATFORM_HEADER_DIR}/platform_info.h")
set(IMAGE_START_ADDR_H "${PLATFORM_HEADER_DIR}/image_start_addr.h")
Expand All @@ -289,7 +291,6 @@ elseif(NOT DEFINED platform_yaml)
message(FATAL_ERROR "IMAGE_START_ADDR not set and 'platform_yaml' undefined")

else()

# The following does two things. These commands are put together as for some weird
# reason, CMake does not execute the first command if it's by itself. This could be
# due to the CMakeLists.txt file in the capDL repository also invoking a similar command
Expand Down Expand Up @@ -324,9 +325,7 @@ else()
"${ELF_SIFT}"
"${SHOEHORN}"
)

endif()

if(DEFINED KernelDTBPath)
get_filename_component(KernelTools ${HARDWARE_GEN_PATH} DIRECTORY)
set(config_file "${KernelTools}/hardware.yml")
Expand All @@ -347,7 +346,6 @@ if(DEFINED KernelDTBPath)
)
set_property(SOURCE src/drivers/driver.c PROPERTY OBJECT_DEPENDS ${DEVICES_GEN_H})
endif()

# Generate linker script
separate_arguments(c_arguments NATIVE_COMMAND "${CMAKE_C_FLAGS}")
# Add extra compilation flags required for clang
Expand Down
31 changes: 31 additions & 0 deletions elfloader-tool/include/arch-loongarch/elfloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2022, tyyteam(Qingtao Liu, Yang Lei, Yang Chen)
* [email protected], [email protected], [email protected]
*
* Derived from:
* Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
* Copyright 2021, HENSOLDT Cyber
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#pragma once

#include <autoconf.h>
#include <elfloader_common.h>

/* This is a low level binary interface, thus we do not preserve the type
* information here. All parameters are just register values (or stack values
* that are register-sized).
*/
typedef void (*init_loongarch_kernel_t)(word_t ui_p_reg_start,
word_t ui_p_reg_end,
word_t pv_offset,
word_t v_entry,
word_t dtb_addr_p,
word_t dtb_size
#if CONFIG_MAX_NUM_NODES > 1
,
word_t hart_id,
word_t core_id
#endif
);
Loading