Skip to content

Commit c792d78

Browse files
laurenmurphyx64fabiobaltieri
authored andcommitted
llext: add fixes for various tests on mwdt / harvard arch
Adds fixes to get LLEXT tests to work for NSIM VPX on MWDT. Excludes prelocated extensions for MWDT for now. Signed-off-by: Lauren Murphy <[email protected]>
1 parent a444f7d commit c792d78

File tree

7 files changed

+79
-7
lines changed

7 files changed

+79
-7
lines changed

cmake/linker/arcmwdt/linker_flags.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ check_set_linker_property(TARGET linker PROPERTY orphan_error
3636
${LINKERFLAGPREFIX},--orphan-handling=error
3737
)
3838

39+
set_property(TARGET linker PROPERTY partial_linking "-r")
40+
3941
# Extra warnings options for twister run
4042
set_property(TARGET linker PROPERTY warnings_as_errors -Wl,--fatal-warnings)
4143

samples/subsys/llext/modules/sample.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ common:
77
- mps2/an385 # ARM Cortex-M3 (ARMv7-M ISA)
88
- mps2/an521/cpu0 # ARM Cortex-M33 (ARMv8-M ISA)
99
- qemu_xtensa/dc233c
10+
- qemu_arc/qemu_arc_em
1011
integration_platforms:
12+
- qemu_arc/qemu_arc_em
1113
- qemu_xtensa/dc233c
1214
- mps2/an385
1315
- qemu_cortex_a53
@@ -26,6 +28,7 @@ tests:
2628
# security benefits.
2729
- arch:arm:CONFIG_ARM_MPU=n
2830
- arch:arm:CONFIG_ARM_AARCH32_MMU=n
31+
- arch:arc:CONFIG_ARC_MPU_ENABLE=n
2932
harness_config:
3033
type: one_line
3134
regex:

subsys/llext/llext_load.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,21 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext,
312312
* regions.
313313
*/
314314
if (ldr_parm->section_detached && ldr_parm->section_detached(shdr)) {
315+
if (mem_idx == LLEXT_MEM_TEXT &&
316+
!INSTR_FETCHABLE(llext_peek(ldr, shdr->sh_offset), shdr->sh_size)) {
317+
#ifdef CONFIG_ARC
318+
LOG_ERR("ELF buffer's detached text section %s not in instruction "
319+
"memory: %p-%p",
320+
name, (void *)(llext_peek(ldr, shdr->sh_offset)),
321+
(void *)((char *)llext_peek(ldr, shdr->sh_offset) +
322+
shdr->sh_size));
323+
return -ENOEXEC;
324+
#else
325+
LOG_WRN("Unknown if ELF buffer's detached text section %s is in "
326+
"instruction memory; proceeding...",
327+
name);
328+
#endif
329+
}
315330
continue;
316331
}
317332

subsys/llext/llext_mem.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ static int llext_copy_region(struct llext_loader *ldr, struct llext *ext,
127127
/* Region has data in the file, check if peek() is supported */
128128
ext->mem[mem_idx] = llext_peek(ldr, region->sh_offset);
129129
if (ext->mem[mem_idx]) {
130-
if (IS_ALIGNED(ext->mem[mem_idx], region_align) ||
131-
ldr_parm->pre_located) {
130+
if ((IS_ALIGNED(ext->mem[mem_idx], region_align) ||
131+
ldr_parm->pre_located) &&
132+
((mem_idx != LLEXT_MEM_TEXT) ||
133+
INSTR_FETCHABLE(ext->mem[mem_idx], region_alloc))) {
132134
/* Map this region directly to the ELF buffer */
133135
llext_init_mem_part(ext, mem_idx,
134136
(uintptr_t)ext->mem[mem_idx],
@@ -137,8 +139,18 @@ static int llext_copy_region(struct llext_loader *ldr, struct llext *ext,
137139
return 0;
138140
}
139141

140-
LOG_WRN("Cannot peek region %d: %p not aligned to %#zx",
141-
mem_idx, ext->mem[mem_idx], (size_t)region_align);
142+
if ((mem_idx == LLEXT_MEM_TEXT) &&
143+
!INSTR_FETCHABLE(ext->mem[mem_idx], region_alloc)) {
144+
LOG_WRN("Cannot reuse ELF buffer for region %d, not "
145+
"instruction memory: %p-%p",
146+
mem_idx, ext->mem[mem_idx],
147+
(void *)((uintptr_t)(ext->mem[mem_idx]) +
148+
region->sh_size));
149+
}
150+
if (!IS_ALIGNED(ext->mem[mem_idx], region_align)) {
151+
LOG_WRN("Cannot peek region %d: %p not aligned to %#zx",
152+
mem_idx, ext->mem[mem_idx], (size_t)region_align);
153+
}
142154
}
143155
} else if (ldr_parm->pre_located) {
144156
/*

subsys/llext/llext_priv.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111
#include <zephyr/llext/llext.h>
1212
#include <zephyr/llext/llext_internal.h>
1313
#include <zephyr/sys/slist.h>
14+
#include <zephyr/device.h>
15+
#include <zephyr/devicetree.h>
16+
17+
/*
18+
* Macro to determine if section / region is in instruction memory
19+
* Will need to be updated if any non-ARC boards using Harvard architecture is added
20+
*/
21+
#if CONFIG_HARVARD && CONFIG_ARC
22+
#define IN_NODE(inst, compat, base_addr, alloc) \
23+
(((uintptr_t)(base_addr) >= DT_REG_ADDR(DT_INST(inst, compat)) && \
24+
(uintptr_t)(base_addr + alloc) <= \
25+
DT_REG_ADDR(DT_INST(inst, compat)) + DT_REG_SIZE(DT_INST(inst, compat)))) ||
26+
#define INSTR_FETCHABLE(base_addr, alloc) \
27+
DT_COMPAT_FOREACH_STATUS_OKAY_VARGS(arc_iccm, IN_NODE, base_addr, alloc) false
28+
#elif CONFIG_HARVARD && !CONFIG_ARC
29+
/* Unknown if section / region is in instruction memory; warn or compensate */
30+
#define INSTR_FETCHABLE(base_addr, alloc) false
31+
#else /* all non-Harvard architectures */
32+
#define INSTR_FETCHABLE(base_addr, alloc) true
33+
#endif
1434

1535
/*
1636
* Global extension list

tests/subsys/llext/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ get_property(COMPILER_TOPT TARGET compiler PROPERTY linker_script)
7474
set_ifndef( TOPT "${COMPILER_TOPT}")
7575
set_ifndef( TOPT -Wl,-T) # Use this if the compiler driver doesn't set a value
7676

77-
if (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE AND NOT CONFIG_ARM AND NOT CONFIG_RISCV)
77+
if (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE AND CONFIG_XTENSA)
7878
# Manually fix the pre_located extension's text address at a multiple of 4
7979
get_target_property(pre_located_target pre_located_ext lib_target)
8080
get_target_property(pre_located_file pre_located_ext pkg_input)

tests/subsys/llext/src/test_llext.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ LLEXT_LOAD_UNLOAD(hello_world,
267267
.kernel_only = true
268268
)
269269

270-
#ifndef CONFIG_LLEXT_TYPE_ELF_SHAREDLIB
270+
/* When compiled with CCAC, init_fini's sections are unfixably out of order */
271+
#if !defined(CONFIG_LLEXT_TYPE_ELF_SHAREDLIB) && !defined(__CCAC__)
271272
static LLEXT_CONST uint8_t init_fini_ext[] ELF_ALIGN = {
272273
#include "init_fini.inc"
273274
};
@@ -375,10 +376,24 @@ ZTEST(llext, test_inspect)
375376
res = llext_load(ldr, "inspect", &ext, &ldr_parm);
376377
zassert_ok(res, "load should succeed");
377378

379+
/* MWDT puts variables that are supposed to go into .bss into .data,
380+
* and, when Harvard / CCM is enabled, puts rodata in data-type sections.
381+
*/
382+
#ifdef __CCAC__
383+
do_inspect_checks(ldr, ext, LLEXT_MEM_DATA, ".data", "number_in_bss");
384+
#else
378385
do_inspect_checks(ldr, ext, LLEXT_MEM_BSS, ".bss", "number_in_bss");
379-
do_inspect_checks(ldr, ext, LLEXT_MEM_DATA, ".data", "number_in_data");
386+
#endif
387+
388+
#if defined(CONFIG_HARVARD) && defined(__CCAC__)
389+
do_inspect_checks(ldr, ext, LLEXT_MEM_DATA, ".rodata_in_data", "number_in_rodata");
390+
do_inspect_checks(ldr, ext, LLEXT_MEM_DATA, ".my_rodata", "number_in_my_rodata");
391+
#else
380392
do_inspect_checks(ldr, ext, LLEXT_MEM_RODATA, ".rodata", "number_in_rodata");
381393
do_inspect_checks(ldr, ext, LLEXT_MEM_RODATA, ".my_rodata", "number_in_my_rodata");
394+
#endif
395+
396+
do_inspect_checks(ldr, ext, LLEXT_MEM_DATA, ".data", "number_in_data");
382397
do_inspect_checks(ldr, ext, LLEXT_MEM_TEXT, ".text", "function_in_text");
383398

384399
max_alloc_bytes = ext->alloc_size;
@@ -526,7 +541,12 @@ ZTEST(llext, test_find_section)
526541
llext_unload(&ext);
527542
}
528543

544+
/* For Harvard architectures, the detached section must be placed in instruction memory. */
545+
#ifdef CONFIG_HARVARD
546+
static LLEXT_CONST uint8_t test_detached_ext[] Z_GENERIC_SECTION(.text) ELF_ALIGN = {
547+
#else
529548
static LLEXT_CONST uint8_t test_detached_ext[] ELF_ALIGN = {
549+
#endif
530550
#include "detached_fn.inc"
531551
};
532552

0 commit comments

Comments
 (0)