Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
llext.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Intel Corporation
3 * Copyright (c) 2024 Schneider Electric
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_LLEXT_H
9#define ZEPHYR_LLEXT_H
10
11#include <zephyr/sys/slist.h>
12#include <zephyr/llext/elf.h>
13#include <zephyr/llext/symbol.h>
14#include <zephyr/kernel.h>
15#include <sys/types.h>
16#include <stdbool.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
35
62
64
65/* Number of memory partitions used by LLEXT */
66#define LLEXT_MEM_PARTITIONS (LLEXT_MEM_BSS+1)
67
68#ifdef CONFIG_LLEXT_RODATA_NO_RELOC
69/* Section name for read-only data kept in flash */
70#define LLEXT_SECT_RODATA_NO_RELOC llext.rodata.noreloc
71
72/* Full section name as string for comparisons */
73#define LLEXT_SECTION_RODATA_NO_RELOC ("." STRINGIFY(LLEXT_SECT_RODATA_NO_RELOC))
74
79#define LLEXT_RODATA_NO_RELOC Z_GENERIC_DOT_SECTION(LLEXT_SECT_RODATA_NO_RELOC)
80#else
81#define LLEXT_RODATA_NO_RELOC
82#endif
83
84struct llext_loader;
86
88#define LLEXT_MAX_NAME_LEN 15
89
91#define LLEXT_MAX_DEPENDENCIES 8
92
93#ifdef CONFIG_LLEXT_HEAP_MEMBLK
94struct llext_alloc {
95 int num_blocks;
96 void *memblk_ptr;
97};
98struct llext_alloc_map {
99 int idx;
100 struct llext_alloc map[LLEXT_MEM_COUNT];
101};
102#endif
103
110struct llext {
112 sys_snode_t llext_list;
113
114#ifdef CONFIG_USERSPACE
115 struct k_mem_partition mem_parts[LLEXT_MEM_PARTITIONS];
116#endif
117
119
122
125
128
129#ifdef CONFIG_LLEXT_HEAP_MEMBLK
130 struct llext_alloc_map mem_alloc_map;
131#endif
132
135
138
146
153
155 unsigned int use_count;
156
159
161 unsigned int sect_cnt;
162 elf_shdr_t *sect_hdrs;
163 bool sect_hdrs_on_heap;
164 bool mmu_permissions_set;
166};
167
168static inline const elf_shdr_t *llext_section_headers(const struct llext *ext)
169{
170 return ext->sect_hdrs;
171}
172
173static inline unsigned int llext_section_count(const struct llext *ext)
174{
175 return ext->sect_cnt;
176}
177
213
215#define LLEXT_LOAD_PARAM_DEFAULT { .relocate_local = true, }
216
223struct llext *llext_by_name(const char *name);
224
236int llext_iterate(int (*fn)(struct llext *ext, void *arg), void *arg);
237
253int llext_load(struct llext_loader *loader, const char *name, struct llext **ext,
254 const struct llext_load_param *ldr_parm);
255
261int llext_unload(struct llext **ext);
262
274int llext_free_inspection_data(struct llext_loader *ldr, struct llext *ext);
275
277typedef void (*llext_entry_fn_t)(void *user_data);
278
290int llext_bringup(struct llext *ext);
291
304int llext_teardown(struct llext *ext);
305
322void llext_bootstrap(struct llext *ext, llext_entry_fn_t entry_fn, void *user_data);
323
342__syscall ssize_t llext_get_fn_table(struct llext *ext, bool is_init, void *buf, size_t size);
343
356const void *llext_find_sym(const struct llext_symtable *sym_table, const char *sym_name);
357
370int llext_call_fn(struct llext *ext, const char *sym_name);
371
385int llext_add_domain(struct llext *ext, struct k_mem_domain *domain);
386
406int arch_elf_relocate(struct llext_loader *ldr, struct llext *ext, elf_rela_t *rel,
407 const elf_shdr_t *shdr);
408
418ssize_t llext_find_section(struct llext_loader *loader, const char *search_name);
419
433int llext_get_section_header(const struct llext_loader *loader, const struct llext *ext,
434 const char *search_name, elf_shdr_t *shdr);
435
448int llext_heap_init(void *mem, size_t bytes);
449
464int llext_heap_init_harvard(void *instr_mem, size_t instr_bytes, void *data_mem, size_t data_bytes);
465
474
493int llext_relink_dependency(struct llext *ext, unsigned int n_ext);
494
520int llext_restore(struct llext **ext, struct llext_loader **ldr, unsigned int n_ext);
524
525#ifdef __cplusplus
526}
527#endif
528
529#include <zephyr/syscalls/llext.h>
530
531#endif /* ZEPHYR_LLEXT_H */
int llext_bringup(struct llext *ext)
Calls bringup functions for an extension.
#define LLEXT_MAX_DEPENDENCIES
Maximum number of dependency LLEXTs.
Definition llext.h:91
int llext_load(struct llext_loader *loader, const char *name, struct llext **ext, const struct llext_load_param *ldr_parm)
Load and link an extension.
static unsigned int llext_section_count(const struct llext *ext)
Definition llext.h:173
void(* llext_entry_fn_t)(void *user_data)
Entry point function signature for an extension.
Definition llext.h:277
int llext_iterate(int(*fn)(struct llext *ext, void *arg), void *arg)
Iterate over all loaded extensions.
int llext_heap_init(void *mem, size_t bytes)
Initialize LLEXT heap dynamically.
ssize_t llext_get_fn_table(struct llext *ext, bool is_init, void *buf, size_t size)
Get pointers to setup or cleanup functions for an extension.
static const elf_shdr_t * llext_section_headers(const struct llext *ext)
Definition llext.h:168
ssize_t llext_find_section(struct llext_loader *loader, const char *search_name)
Locates an ELF section in the file.
int llext_free_inspection_data(struct llext_loader *ldr, struct llext *ext)
Free any inspection-related memory for the specified loader and extension.
int llext_add_domain(struct llext *ext, struct k_mem_domain *domain)
Add an extension to a memory domain.
void llext_bootstrap(struct llext *ext, llext_entry_fn_t entry_fn, void *user_data)
Bring up, execute, and teardown an extension.
llext_mem
List of memory regions stored or referenced in the LLEXT subsystem.
Definition llext.h:44
#define LLEXT_MAX_NAME_LEN
Maximum length of an extension name.
Definition llext.h:88
struct llext * llext_by_name(const char *name)
Find an llext by name.
int llext_heap_uninit(void)
Mark LLEXT heap as uninitialized.
int llext_relink_dependency(struct llext *ext, unsigned int n_ext)
Relink dependencies to prepare for suspend.
const void * llext_find_sym(const struct llext_symtable *sym_table, const char *sym_name)
Find the address for an arbitrary symbol.
int llext_restore(struct llext **ext, struct llext_loader **ldr, unsigned int n_ext)
Restore LLEXT context from saved data.
int llext_unload(struct llext **ext)
Unload an extension.
int llext_call_fn(struct llext *ext, const char *sym_name)
Call a function by name.
int llext_heap_init_harvard(void *instr_mem, size_t instr_bytes, void *data_mem, size_t data_bytes)
Initialize LLEXT heap dynamically for Harvard architecture.
int llext_teardown(struct llext *ext)
Calls teardown functions for an extension.
int llext_get_section_header(const struct llext_loader *loader, const struct llext *ext, const char *search_name, elf_shdr_t *shdr)
Extract ELF section header by name.
int arch_elf_relocate(struct llext_loader *ldr, struct llext *ext, elf_rela_t *rel, const elf_shdr_t *shdr)
Architecture specific opcode update function.
@ LLEXT_MEM_SYMTAB
Symbol table.
Definition llext.h:50
@ LLEXT_MEM_SHSTRTAB
Section name strings.
Definition llext.h:52
@ LLEXT_MEM_TEXT
Executable code.
Definition llext.h:45
@ LLEXT_MEM_DATA
Initialized data.
Definition llext.h:46
@ LLEXT_MEM_PREINIT
Array of early setup functions.
Definition llext.h:53
@ LLEXT_MEM_BSS
Uninitialized data.
Definition llext.h:48
@ LLEXT_MEM_RODATA
Read-only data.
Definition llext.h:47
@ LLEXT_MEM_INIT
Array of setup functions.
Definition llext.h:54
@ LLEXT_MEM_FINI
Array of cleanup functions.
Definition llext.h:55
@ LLEXT_MEM_COUNT
Number of regions managed by LLEXT.
Definition llext.h:60
@ LLEXT_MEM_STRTAB
Symbol name strings.
Definition llext.h:51
@ LLEXT_MEM_EXPORT
Exported symbol table.
Definition llext.h:49
struct elf64_shdr elf_shdr_t
Machine sized section header structure.
Definition elf.h:463
struct elf64_rela elf_rela_t
Machine sized relocation struct with addend.
Definition elf.h:475
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
Public kernel APIs.
__SIZE_TYPE__ ssize_t
Definition types.h:28
Data structures and constants defined in the ELF specification.
#define bool
Definition stdbool.h:13
Memory Domain.
Definition mem_domain.h:80
Memory Partition.
Definition mem_domain.h:55
Advanced llext_load parameters.
Definition llext.h:183
bool relocate_local
Perform local relocation.
Definition llext.h:185
bool pre_located
Use the virtual symbol addresses from the ELF, not addresses within the memory buffer,...
Definition llext.h:195
bool keep_section_info
Keep the ELF section data in memory after loading the extension.
Definition llext.h:211
bool(* section_detached)(const elf_shdr_t *shdr)
Extensions can implement custom ELF sections to be loaded in specific memory regions,...
Definition llext.h:202
Linkable loadable extension loader context.
Definition loader.h:80
A symbol table.
Definition symbol.h:82
Structure describing a linkable loadable extension.
Definition llext.h:110
char name[15+1]
Name of the llext.
Definition llext.h:121
size_t mem_size[LLEXT_MEM_COUNT]
Size of each stored region.
Definition llext.h:134
struct llext_symtable sym_tab
Table of all global symbols in the extension; used internally as part of the linking process.
Definition llext.h:145
size_t alloc_size
Total llext allocation size.
Definition llext.h:137
unsigned int use_count
Extension use counter, prevents unloading while in use.
Definition llext.h:155
struct llext * dependency[8]
Array of extensions, whose symbols this extension accesses.
Definition llext.h:158
bool mem_on_heap[LLEXT_MEM_COUNT]
Is the memory for this region allocated on heap?
Definition llext.h:127
struct llext_symtable exp_tab
Table of symbols exported by the llext via LL_EXTENSION_SYMBOL.
Definition llext.h:152
void * mem[LLEXT_MEM_COUNT]
Lookup table of memory regions.
Definition llext.h:124
Linkable loadable extension symbol definitions.