Skip to content

Commit f052003

Browse files
authored
Implement memory regions iterators (#104)
1 parent b813585 commit f052003

10 files changed

Lines changed: 887 additions & 35 deletions

File tree

CMake/TestsConfig.cmake

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ function(SETUP_UNIT_TEST name)
22
add_executable(${name})
33
SETUP_COMMON(${name})
44

5-
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS *.c)
6-
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS *.h)
5+
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS *.h)
76

8-
target_sources(${name}
9-
PRIVATE
10-
${SOURCES}
11-
${ARGN}
12-
PRIVATE
13-
FILE_SET HEADERS
14-
BASE_DIRS .
15-
FILES ${HEADERS}
16-
)
7+
# If explicit sources were passed to the macro, use them only.
8+
if (ARGN)
9+
set(EXPLICIT_SOURCES ${ARGN})
10+
else()
11+
file(GLOB_RECURSE EXPLICIT_SOURCES CONFIGURE_DEPENDS *.c)
12+
endif()
1713

18-
target_link_libraries(${name} PRIVATE unity)
14+
target_sources(${name}
15+
PRIVATE
16+
${EXPLICIT_SOURCES}
17+
PRIVATE
18+
FILE_SET HEADERS
19+
BASE_DIRS .
20+
FILES ${HEADERS}
21+
)
22+
23+
# Link common libraries required by tests that reference core helpers and DT parsing.
24+
target_link_libraries(${name} PRIVATE unity libcore libboot_dt)
1925

2026
add_test(NAME ${name} COMMAND ${name})
2127

docs/doxygen/groups.dox

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@defgroup palloc Physical memory allocator
66
@defgroup kinit Kernel initialization
77
@defgroup hal Hardware abstraction layer
8+
@defgroup hal_riscv_mem RISC-V memory regions
89
@defgroup libboot_dt
910
@defgroup libcore
1011
@defgroup sbi

docs/source/pages/api/kernel/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Internal kernel APIs
66
:maxdepth: 1
77

88
mm/index
9+
memory_regions
910

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=======================
2+
Memory regions
3+
=======================
4+
5+
This module provides iterators to enumerate memory regions from the device tree:
6+
7+
- **Memory regions**: Physical memory regions available for use (from /memory nodes)
8+
- **Reserved regions**: Memory ranges reserved by the system and must not be allocated
9+
(from /reserved-memory node and memory reservations)
10+
11+
Usage Pattern
12+
=============
13+
14+
Both iterators follow the same init-then-iterate pattern:
15+
16+
.. code-block:: c
17+
18+
// Initialize iterator
19+
hal_memory_iterator_t iter;
20+
if (hal_get_memory_regions_iterator(&iter) != ERR_NONE)
21+
return; // Handle error
22+
23+
// Iterate through regions
24+
physical_memory_region_t region;
25+
while (hal_get_next_memory_region(&iter, &region) == ERR_NONE) {
26+
// Process region
27+
}
28+
// Loop exits when hal_get_next_memory_region returns ERR_NOT_FOUND
29+
30+
Return Value Semantics
31+
======================
32+
33+
- ``ERR_NONE``: Successfully retrieved next region; output parameter is valid
34+
- ``ERR_NOT_FOUND``: End of iteration reached (normal termination, not an error)
35+
- Other error codes: Actual error occurred; output parameters remain unchanged
36+
37+
API Reference
38+
=============
39+
40+
.. doxygengroup:: hal_riscv_mem

include/libboot/dt/dt.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ error_t dt_get_prop_buffer(const fdt_t* fdt, dt_prop_t prop, buffer_t* bufOUT);
236236
[[gnu::nonnull(3)]]
237237
error_t dt_get_rsv_mem_entry(const fdt_t* fdt, u32 index, fdt_rsv_entry* entryOUT);
238238

239+
/**
240+
* @brief Get #address-cells and #size-cells properties from a node.
241+
* @param fdt Pointer to the fdt object.
242+
* @param node The node to read cell counts from.
243+
* @param[out] address_cellsOUT Number of address cells (default 2).
244+
* @param[out] size_cellsOUT Number of size cells (default 1).
245+
* @retval ERR_NONE on success
246+
* @retval ERR_BAD_ARG on nullptr args
247+
* @retval ERR_NOT_VALID if the FDT is invalid or cell counts exceed 2
248+
*/
249+
[[gnu::nonnull(3, 4)]]
250+
error_t dt_get_reg_cell_counts(const fdt_t* fdt, dt_node_t node, u32* address_cellsOUT, u32* size_cellsOUT);
251+
239252
/// @}
240253

241254
#endif // !DT_DT

0 commit comments

Comments
 (0)