Skip to content
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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

#include("targets/${TARGET_PLATFORM}/config.cmake")

project(magia-sdk LANGUAGES C ASM)

set(ABI ilp32)
Expand All @@ -55,6 +57,12 @@ else()
message(STATUS "[MAGIA-SDK] DISASSEMBLE_LIBRARIES : OFF")
endif()

# Set host-specific flags
set(MESH_ARCH "riscv32")
set(MESH_FAMILY "riscv32")
set(MESH_ENDIAN "little")
set(MESH_SYSTEM "unknown")

include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)

################################################################################
Expand All @@ -65,6 +73,9 @@ include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
# Depending on the target, following static libraries have to added by the targets
add_subdirectory(targets)

# VIVIANEP: Include the picolibc library
#include(${CMAKE_CURRENT_LIST_DIR}/cmake/picolibc.cmake)

# Include other subdirectories
add_subdirectory(hal)
# add_subdirectory(devices)
Expand Down
77 changes: 49 additions & 28 deletions cmake/picolibc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

include(ExternalProject)

message(STATUS "[MAGIA-SDK] Setting up picolibc for Host (ISA: ${ISA}, ABI: ${ABI})")
message(STATUS "[MAGIA-SDK] Setting up picolibc for Mesh (ISA: ${ISA}, ABI: ${ABI})")

set(CROSS_C_COMPILER "${CMAKE_C_COMPILER}")
set(CROSS_C_COMPILER_ARGS "-target ${CROSS_COMPILE} -march=${ISA} -nostdlib" CACHE STRING "Compiler arguments")
# VIVIANEP: These flags are only for building Picolibc; adding them globally breaks
# app builds (e.g., missing <sys/types.h>) or causes issues in freestanding mode.
set(CROSS_C_ARGS "-Werror=double-promotion -Wno-unsupported-floating-point-opt -fshort-enums ${CMAKE_ALT_C_OPTIONS} -march=${ISA} -mabi=${ABI}")
set(CROSS_C_LINK_ARGS "-Wl,-z,noexecstack -march=${ISA} -mabi=${ABI}")
set(CROSS_C_COMPILER "${CMAKE_C_COMPILER}")
set(CROSS_C_ARGS "-march=${ISA} -mabi=${ABI} -nostdlib -ggdb -gdwarf-4 -gstrict-dwarf -mno-relax")
set(CROSS_C_LINK_ARGS "-march=${ISA} -mabi=${ABI} -nostdlib -fno-common -Wl,-z,noexecstack ")

set(CROSS_AR "${CMAKE_AR}")
set(CROSS_STRIP "${CMAKE_STRIP}")
set(CROSS_AR "${CMAKE_AR}")
set(CROSS_STRIP "${CMAKE_STRIP}")

set(CROSS_SKIP_SANITY_CHECK "true")
set(CROSS_CPU "${MESH_ARCH}")
set(CROSS_CPU_FAMILY "${MESH_FAMILY}")
set(CROSS_ENDIAN "${MESH_ENDIAN}")
set(CROSS_SYSTEM "${MESH_SYSTEM}")

# Prepare Meson arrays
function(prepare_meson_array output_var input_string)
Expand All @@ -32,44 +32,65 @@ function(prepare_meson_array output_var input_string)
set(${output_var} "${result}" PARENT_SCOPE)
endfunction()

prepare_meson_array(CROSS_C_COMPILER_ARGS_LIST "${CROSS_C_COMPILER_ARGS}")
prepare_meson_array(CROSS_C_ARGS_LIST "${CROSS_C_ARGS}")
prepare_meson_array(CROSS_C_LINK_ARGS_LIST "${CROSS_C_LINK_ARGS}")

set(PICOLIBC_SRC_DIR ${CMAKE_BINARY_DIR}/picolibc-src)

set(PICOLIBC_BUILD_DIR ${CMAKE_BINARY_DIR}/picolibc-build-${ISA}-${ABI})
set(PICOLIBC_INSTALL_DIR ${CMAKE_BINARY_DIR}/picolibc-install-${ISA}-${ABI})
set(PICOLIBC_CROSS_FILE ${CMAKE_BINARY_DIR}/picolibc-cross-file-${ISA}-${ABI}.txt)

set(PICOLIBC_BUILD_DIR ${CMAKE_BINARY_DIR}/picolibc-build)
set(PICOLIBC_INSTALL_DIR ${CMAKE_BINARY_DIR}/picolibc-install)
set(PICOLIBC_CROSS_FILE ${CMAKE_BINARY_DIR}/picolibc-cross-file.txt)

# Generate the Meson cross-file
configure_file(${CMAKE_CURRENT_LIST_DIR}/../scripts/picolibc-cross-file.txt.in ${PICOLIBC_CROSS_FILE} @ONLY)

message(STATUS "[MAGIA-SDK] Saving cross compilation file to ${PICOLIBC_CROSS_FILE}")

set(PICOLIB_MULTILIB ${PICOLIB_MESH})
if(NOT ${PICOLIB_CLUSTER_SNITCH} STREQUAL "None")
list(APPEND PICOLIB_MULTILIB ${PICOLIB_CLUSTER_SNITCH})
endif()
string(JOIN "," PICOLIB_MULTILIB ${PICOLIB_MULTILIB})


# Add picolibc as an external project
ExternalProject_Add(
picolibc-${ISA}-${ABI}
picolibc
GIT_REPOSITORY https://github.com/picolibc/picolibc.git
GIT_TAG main
SOURCE_DIR ${PICOLIBC_SRC_DIR}
BINARY_DIR ${PICOLIBC_BUILD_DIR}
INSTALL_DIR ${PICOLIBC_INSTALL_DIR}
CONFIGURE_COMMAND meson setup ${PICOLIBC_BUILD_DIR} ${PICOLIBC_SRC_DIR} --cross-file ${PICOLIBC_CROSS_FILE} --prefix ${PICOLIBC_INSTALL_DIR} --default-library=static
CONFIGURE_COMMAND meson setup ${PICOLIBC_BUILD_DIR} ${PICOLIBC_SRC_DIR} --cross-file ${PICOLIBC_CROSS_FILE} -D multilib-list=${PICOLIB_MULTILIB} --prefix ${PICOLIBC_INSTALL_DIR} --default-library=static
BUILD_COMMAND ninja -C ${PICOLIBC_BUILD_DIR}
INSTALL_COMMAND ninja -C ${PICOLIBC_BUILD_DIR} install
BUILD_BYPRODUCTS ${PICOLIBC_INSTALL_DIR}/lib/libc.a
LOG_CONFIGURE ON
LOG_BUILD ON
BUILD_BYPRODUCTS
${PICOLIBC_INSTALL_DIR}/lib/${PICOLIB_MESH}/libc.a
${PICOLIBC_INSTALL_DIR}/lib/${PICOLIB_CLUSTER_SNITCH}/libc.a
#LOG_CONFIGURE ON
#LOG_BUILD ON
LOG_INSTALL ON
)

set(PICOLIBC_TARGET picolibc-${ISA}-${ABI})

add_library(picolibc STATIC IMPORTED GLOBAL)
set(PICOLIBC_TARGET picolibc)

set_target_properties(picolibc PROPERTIES
IMPORTED_LOCATION "${PICOLIBC_INSTALL_DIR}/lib/libc.a"
################################################################################
# Mesh Picolibc Library
################################################################################
add_library(picolibc_mesh STATIC IMPORTED GLOBAL)
set_target_properties(picolibc_mesh PROPERTIES
IMPORTED_LOCATION "${PICOLIBC_INSTALL_DIR}/lib/${PICOLIB_MESH}/libc.a"
)

add_dependencies(picolibc picolibc-${ISA_HOST}-${ABI})
add_dependencies(picolibc_mesh picolibc)

################################################################################
# Snitch Picolibc Library
################################################################################
if(${ISA_CLUSTER_SNITCH} STREQUAL "None")
return()
else()
add_library(picolibc_cluster_snitch STATIC IMPORTED GLOBAL)
set_target_properties(picolibc_cluster_snitch PROPERTIES
IMPORTED_LOCATION "${PICOLIBC_INSTALL_DIR}/lib/${PICOLIB_CLUSTER_SNITCH}/libc.a"
)
add_dependencies(picolibc_cluster_snitch picolibc)
endif()
15 changes: 9 additions & 6 deletions cmake/toolchain_gcc_pulp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ set(CMAKE_EXECUTABLE_SUFFIX ".elf")

set(CMAKE_SYSTEM_NAME Generic)

set(CMAKE_C_COMPILER riscv32-unknown-elf-gcc)
set(CMAKE_CXX_COMPILER riscv32-unknown-elf-g++)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_OBJCOPY riscv32-unknown-elf-objcopy)
set(CMAKE_OBJDUMP riscv32-unknown-elf-objdump)
set(CMAKE_AR riscv32-unknown-elf-ar)
set(CROSS_COMPILE riscv32-unknown-elf)
set(CMAKE_C_COMPILER ${CROSS_COMPILE}-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}-g++)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_OBJCOPY ${CROSS_COMPILE}-objcopy)
set(CMAKE_OBJDUMP ${CROSS_COMPILE}-objdump)
set(CMAKE_AR ${CROSS_COMPILE}-ar)
set(CMAKE_STRIP ${CROSS_COMPILE}-strip)

2 changes: 2 additions & 0 deletions targets/magia_v2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

add_library(runtime STATIC)

#add_dependencies(runtime picolibc)

file(GLOB_RECURSE ASM_SOURCES
"src/crt0.S"
)
Expand Down
13 changes: 13 additions & 0 deletions targets/magia_v2/config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-FileCopyrightText: 2025 ETH Zurich and University of Bologna
# SPDX-License-Identifier: Apache-2.0

# WIESEP: It is important to set the ISA and ABI for the host and the cluster snitch
set(ABI_MESH ilp32)
set(ISA_MESH rv32imcxgap9)
set(PICOLIB_MESH rv32im/ilp32)
set(COMPILERRT_MESH rv32imcxgap9)

set(ABI_CLUSTER_SNITCH None)
set(ISA_CLUSTER_SNITCH None)
set(PICOLIB_CLUSTER_SNITCH None)
set(COMPILERRT_CLUSTER_SNITCH None)
1 change: 1 addition & 0 deletions targets/magia_v2/include/addr_map/tile_addr_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#define TEST_END_ADDR (0xCCFF0000)
#define PRINT_ADDR (0xFFFF0004)
#define MHARTID_OFFSET (0x00100000)
#define L2_HEAP_SIZE (0x00100000)

#define IDMA_BASE_AXI2OBI (IDMA_BASE) // direction=0, L2 to L1
#define IDMA_BASE_OBI2AXI (IDMA_BASE + 0x200) // direction=1, L1 to L2
Expand Down
126 changes: 126 additions & 0 deletions targets/magia_v2/include/utils/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2023-2026 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* SPDX-License-Identifier: Apache-2.0
*
* Authors:
* Alberto Dequino <alberto.dequino@unibo.it>
*
* MAGIA allocator
*/

#ifndef ALLOC_H
#define ALLOC_H


#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

/*
* All allocated memory blocks are aligned to this boundary for optimal access performance.
*/
#define ALLOC_ALIGNMENT 4

/**
* Macro to align a size to the memory alignment boundary.
*/
#define ALLOC_ALIGN(size) (((size) + (ALLOC_ALIGNMENT - 1)) & ~(ALLOC_ALIGNMENT - 1))

/**
* Memory block structure for the freelist allocator.
*
* This structure is used internally to track free memory blocks in a linked list.
* Each allocated block has this header prepended to store metadata.
*/
typedef struct MemoryBlock {
struct MemoryBlock *next; /**< Pointer to the next free block in the list. */
size_t size; /**< Size of the memory block in bytes. */
} MemoryBlock;

/**
* This function allocates a block of memory of the specified size from the dataram heap.
* The allocated memory is aligned to ALLOC_ALIGNMENT bytes. The memory is not initialized.
*
* The allocated memory should be freed using magia_l2_free() when no longer needed.
*/
void *magia_l2_malloc(size_t size);

/**
* This function returns a memory block to the free list, making it available for future
* allocations. The memory block is added to the head of the freelist for efficient reuse.
*
* The pointer must have been returned by a previous call to magia_l2_malloc().
* After calling this function, the memory pointed to by ptr should not be accessed.
*/
void magia_l2_free(void *ptr);

/**
* This function allocates memory for an array of num elements, each of size bytes,
* and initializes all bytes to zero.
*
* The allocated memory should be freed using magia_l2_free() when no longer needed.
*/
void *magia_l2_calloc(size_t num, size_t size);

/**
* This function changes the size of the memory block pointed to by ptr to size bytes.
* The contents are unchanged up to the minimum of the old and new sizes.
* If the new size is larger, the additional memory is uninitialized.
*
* If reallocation fails, the original memory block is unchanged.
*/
void *magia_l2_realloc(void *ptr, size_t size);

/**
* Gets the total size of the dataram l2 heap.
*/
size_t magia_l2_heap_size(void);

/**
* This function calculates the total amount of free memory by traversing the freelist
* and summing the sizes of all free blocks, plus any remaining unallocated heap space.
*/
size_t magia_l2_heap_free(void);

/**
* Gets the amount of allocated memory in the l2 heap.
*/
size_t magia_l2_heap_used(void);

/**
* This function verifies if the given pointer points to a memory block that was
* allocated by magia_l2_malloc() and is within the heap bounds.
*/
bool magia_l2_ptr_valid(void *ptr);

/**
* This function validates the integrity of the heap data structures, including:
* - Freelist consistency
* - Memory block header validity
* - Heap boundary checks
*
* This function is intended for debugging and should not be used in production code
* due to its performance impact.
*/
bool magia_l2_heap_check(void);


#endif //ALLOC_H






10 changes: 10 additions & 0 deletions targets/magia_v2/link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ SECTIONS
_bss_end = .;
} > dataram

.l2_heap :
{
. = ALIGN(4);
sl2_heap = .;
_sl2_heap = .;
. = . + L2_HEAP_SIZE;
el2_heap = .;
_el2_heap = .;
} > dataram

/* ensure there is enough room for stack */
.stack (NOLOAD): {
. = ALIGN(4);
Expand Down
Loading
Loading