forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllext_manager.c
More file actions
1101 lines (910 loc) · 34.7 KB
/
llext_manager.c
File metadata and controls
1101 lines (910 loc) · 34.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2022 Intel Corporation. All rights reserved.
//
// Author: Jaroslaw Stelter <jaroslaw.stelter@intel.com>
// Pawel Dobrowolski<pawelx.dobrowolski@intel.com>
/*
* Dynamic module loading functions using Zephyr Linkable Loadable Extensions (LLEXT) interface.
*/
#include <sof/audio/buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/common.h>
#include <sof/compiler_attributes.h>
#include <sof/ipc/topology.h>
#include <rtos/sof.h>
#include <rtos/spinlock.h>
#include <sof/lib/cpu-clk-manager.h>
#include <sof/lib_manager.h>
#include <sof/lib/regions_mm.h>
#include <sof/llext_manager.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/module_adapter/module/modules.h>
#include <zephyr/cache.h>
#include <zephyr/app_memory/mem_domain.h>
#include <zephyr/drivers/mm/system_mm.h>
#include <zephyr/llext/buf_loader.h>
#include <zephyr/llext/loader.h>
#include <zephyr/llext/llext.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/llext/inspect.h>
#include <kernel_arch_interface.h>
#include <rimage/sof/user/manifest.h>
#include <module/module/api_ver.h>
#include <adsp_memory_regions.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
LOG_MODULE_DECLARE(lib_manager, CONFIG_SOF_LOG_LEVEL);
extern struct tr_ctx lib_manager_tr;
#define PAGE_SZ CONFIG_MM_DRV_PAGE_SIZE
static int llext_manager_update_flags(void __sparse_cache *vma, size_t size, uint32_t flags)
{
size_t pre_pad_size = (uintptr_t)vma & (PAGE_SZ - 1);
void *aligned_vma = (__sparse_force uint8_t *)vma - pre_pad_size;
return sys_mm_drv_update_region_flags(aligned_vma,
ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags);
}
static int llext_manager_align_map(const struct sys_mm_drv_region *virtual_region,
void __sparse_cache *vma, size_t size, uint32_t flags)
{
size_t pre_pad_size = (uintptr_t)vma & (PAGE_SZ - 1);
void *aligned_vma = (__sparse_force uint8_t *)vma - pre_pad_size;
return sys_mm_drv_map_region_safe(virtual_region, aligned_vma, POINTER_TO_UINT(NULL),
ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags);
}
static int llext_manager_align_unmap(void __sparse_cache *vma, size_t size)
{
size_t pre_pad_size = (uintptr_t)vma & (PAGE_SZ - 1);
void *aligned_vma = (__sparse_force uint8_t *)vma - pre_pad_size;
return sys_mm_drv_unmap_region(aligned_vma, ALIGN_UP(pre_pad_size + size, PAGE_SZ));
}
static void llext_manager_detached_update_flags(void __sparse_cache *vma,
size_t size, uint32_t flags)
{
#ifdef CONFIG_MMU
size_t pre_pad_size = (uintptr_t)vma & (PAGE_SZ - 1);
void *aligned_vma = (__sparse_force uint8_t *)vma - pre_pad_size;
/* Use cached virtual address */
uintptr_t va = POINTER_TO_UINT(sys_cache_cached_ptr_get(aligned_vma));
arch_mem_map(aligned_vma, va, ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags);
#endif
}
/*
* Map the memory range covered by 'vma' and 'size' as writable, copy all
* sections that belong to the specified 'region' and are contained in the
* memory range, then remap the same area according to the 'flags' parameter.
*/
static int llext_manager_load_data_from_storage(const struct sys_mm_drv_region *virtual_region,
const struct llext_loader *ldr,
const struct llext *ext,
enum llext_mem region,
void __sparse_cache *vma,
size_t size, uint32_t flags)
{
unsigned int i;
const void *region_addr;
/* check if there region to be mapped exists */
if (size == 0)
return 0;
int ret = llext_manager_align_map(virtual_region, vma, size, SYS_MM_MEM_PERM_RW);
if (ret < 0) {
tr_err(&lib_manager_tr, "cannot map %u of %p", size, (__sparse_force void *)vma);
return ret;
}
llext_get_region_info(ldr, ext, region, NULL, ®ion_addr, NULL);
/* Need to copy sections within regions individually, offsets may differ */
for (i = 0; i < llext_section_count(ext); i++) {
const elf_shdr_t *shdr;
enum llext_mem s_region = LLEXT_MEM_COUNT;
size_t s_offset = 0;
int ret = llext_get_section_info(ldr, ext, i, &shdr, &s_region, &s_offset);
if (ret < 0) {
tr_err(&lib_manager_tr, "no section info: %d", ret);
continue;
}
/* skip sections not in the requested region */
if (s_region != region)
continue;
/* skip detached sections (will be outside requested VMA area) */
if ((uintptr_t)shdr->sh_addr < (uintptr_t)vma ||
(uintptr_t)shdr->sh_addr >= (uintptr_t)vma + size) {
llext_manager_detached_update_flags((__sparse_cache void *)
((uint8_t *)region_addr + s_offset),
shdr->sh_size, flags);
if (flags & SYS_MM_MEM_PERM_EXEC)
icache_invalidate_region((__sparse_cache void *)
((uint8_t *)region_addr + s_offset),
shdr->sh_size);
continue;
}
ret = memcpy_s((__sparse_force void *)shdr->sh_addr, size - s_offset,
(const uint8_t *)region_addr + s_offset, shdr->sh_size);
if (ret < 0)
return ret;
}
/*
* We don't know what flags we're changing to, maybe the buffer will be
* executable or read-only. Need to write back caches now
*/
dcache_writeback_region(vma, size);
ret = llext_manager_update_flags(vma, size, flags);
if (!ret && (flags & SYS_MM_MEM_PERM_EXEC))
icache_invalidate_region(vma, size);
return ret;
}
static void llext_manager_unmap_detached_sections(const struct llext_loader *ldr,
const struct llext *ext,
enum llext_mem region,
void __sparse_cache *vma,
size_t size)
{
#ifdef CONFIG_MMU
unsigned int i;
const void *region_addr;
llext_get_region_info(ldr, ext, region, NULL, ®ion_addr, NULL);
for (i = 0; i < llext_section_count(ext); i++) {
const elf_shdr_t *shdr;
enum llext_mem s_region = LLEXT_MEM_COUNT;
size_t s_offset = 0;
llext_get_section_info(ldr, ext, i, &shdr, &s_region, &s_offset);
/* skip sections not in the requested region */
if (s_region != region)
continue;
/* unmap detached sections (will be outside requested VMA area) */
if ((uintptr_t)shdr->sh_addr < (uintptr_t)vma ||
(uintptr_t)shdr->sh_addr >= (uintptr_t)vma + size)
llext_manager_detached_update_flags((__sparse_force void *)
((uint8_t *)region_addr + s_offset),
shdr->sh_size, 0);
}
#endif
}
static int llext_manager_load_module(struct lib_manager_module *mctx)
{
/* Executable code (.text) */
void __sparse_cache *va_base_text = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_TEXT].addr;
size_t text_size = mctx->segment[LIB_MANAGER_TEXT].size;
/* Read-only data (.rodata and others) */
void __sparse_cache *va_base_rodata = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_RODATA].addr;
size_t rodata_size = mctx->segment[LIB_MANAGER_RODATA].size;
/* Writable data (.data, .bss and others) */
void __sparse_cache *va_base_data = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_DATA].addr;
size_t data_size = mctx->segment[LIB_MANAGER_DATA].size;
/* .bss, should be within writable data above */
void __sparse_cache *bss_addr = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_BSS].addr;
size_t bss_size = mctx->segment[LIB_MANAGER_BSS].size;
int ret;
/* Check, that .bss is within .data */
if (bss_size &&
((uintptr_t)bss_addr + bss_size <= (uintptr_t)va_base_data ||
(uintptr_t)bss_addr >= (uintptr_t)va_base_data + data_size)) {
size_t bss_align = MIN(PAGE_SZ, BIT(__builtin_ctz((uintptr_t)bss_addr)));
if ((!data_size || (uintptr_t)bss_addr + bss_size == (uintptr_t)va_base_data) &&
bss_align >= PAGE_SZ) {
/*
* .bss is properly aligned and either there's no writable data,
* or .bss is directly in front of writable data, prepend .bss
*/
va_base_data = bss_addr;
data_size += bss_size;
} else if ((uintptr_t)bss_addr == (uintptr_t)va_base_data +
ALIGN_UP(data_size, bss_align)) {
/* .bss directly behind writable data, append */
data_size += bss_size;
} else {
tr_err(&lib_manager_tr, ".bss %#x @%p isn't within writable data %#x @%p!",
bss_size, (__sparse_force void *)bss_addr,
data_size, (__sparse_force void *)va_base_data);
return -EPROTO;
}
}
const struct llext_loader *ldr = &mctx->ebl->loader;
const struct llext *ext = mctx->llext;
/* find dedicated virtual memory zone */
const struct sys_mm_drv_region *virtual_memory_regions = sys_mm_drv_query_memory_regions();
const struct sys_mm_drv_region *virtual_region;
if (!virtual_memory_regions)
return -EFAULT;
SYS_MM_DRV_MEMORY_REGION_FOREACH(virtual_memory_regions, virtual_region) {
if (virtual_region->attr == VIRTUAL_REGION_LLEXT_LIBRARIES_ATTR)
break;
}
if (!virtual_region->size)
return -EFAULT;
/* Copy Code */
ret = llext_manager_load_data_from_storage(virtual_region, ldr, ext, LLEXT_MEM_TEXT,
va_base_text, text_size, SYS_MM_MEM_PERM_EXEC);
if (ret < 0)
return ret;
/* Copy read-only data */
ret = llext_manager_load_data_from_storage(virtual_region, ldr, ext, LLEXT_MEM_RODATA,
va_base_rodata, rodata_size, 0);
if (ret < 0)
goto e_text;
/* Copy writable data */
/*
* NOTE: va_base_data and data_size refer to an address range that
* spans over the BSS area as well, so the mapping will cover
* both, but only LLEXT_MEM_DATA sections will be copied.
*/
ret = llext_manager_load_data_from_storage(virtual_region, ldr, ext, LLEXT_MEM_DATA,
va_base_data, data_size, SYS_MM_MEM_PERM_RW);
if (ret < 0)
goto e_rodata;
memset((__sparse_force void *)bss_addr, 0, bss_size);
mctx->mapped = true;
return 0;
e_rodata:
if (rodata_size)
llext_manager_align_unmap(va_base_rodata, rodata_size);
e_text:
llext_manager_align_unmap(va_base_text, text_size);
return ret;
}
static int llext_manager_unload_module(struct lib_manager_module *mctx)
{
const struct llext_loader *ldr = &mctx->ebl->loader;
const struct llext *ext = mctx->llext;
/* Executable code (.text) */
void __sparse_cache *va_base_text = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_TEXT].addr;
size_t text_size = mctx->segment[LIB_MANAGER_TEXT].size;
/* Read-only data (.rodata, etc.) */
void __sparse_cache *va_base_rodata = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_RODATA].addr;
size_t rodata_size = mctx->segment[LIB_MANAGER_RODATA].size;
/* Writable data (.data, .bss, etc.) */
void __sparse_cache *va_base_data = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_DATA].addr;
void __sparse_cache *va_base_bss = (void __sparse_cache *)
mctx->segment[LIB_MANAGER_BSS].addr;
size_t data_size = mctx->segment[LIB_MANAGER_DATA].size +
mctx->segment[LIB_MANAGER_BSS].size;
int err = 0, ret;
llext_manager_unmap_detached_sections(ldr, ext, LLEXT_MEM_TEXT,
va_base_text, text_size);
ret = llext_manager_align_unmap(va_base_text, text_size);
if (ret < 0)
err = ret;
/* Mimic the logic from load_module where the .bss address is used for mapping
* in case of e.g. lack of writable .data section
*/
if (!va_base_data)
va_base_data = va_base_bss;
llext_manager_unmap_detached_sections(ldr, ext, LLEXT_MEM_DATA,
va_base_data, data_size);
ret = llext_manager_align_unmap(va_base_data, data_size);
if (ret < 0 && !err)
err = ret;
llext_manager_unmap_detached_sections(ldr, ext, LLEXT_MEM_RODATA,
va_base_rodata, rodata_size);
ret = llext_manager_align_unmap(va_base_rodata, rodata_size);
if (ret < 0 && !err)
err = ret;
mctx->mapped = false;
return err;
}
static bool llext_manager_section_detached(const elf_shdr_t *shdr)
{
return shdr->sh_addr < SOF_MODULE_DRAM_LINK_END;
}
static int llext_manager_link(const char *name,
struct lib_manager_module *mctx, const void **buildinfo,
const struct sof_man_module_manifest **mod_manifest)
{
struct llext **llext = &mctx->llext;
struct llext_loader *ldr = &mctx->ebl->loader;
const elf_shdr_t *hdr;
int ret;
if (*llext && !mctx->mapped) {
/*
* All module instances have been terminated, so we freed SRAM,
* but we kept the full Zephyr LLEXT context. Now a new instance
* is starting, so we just re-use all the configuration and only
* re-allocate SRAM and copy the module into it
*/
*mod_manifest = mctx->mod_manifest;
return 0;
}
if (!*llext || mctx->mapped) {
/*
* Either the very first time loading this module, or the module
* is already mapped, we just call llext_load() to refcount it
*/
struct llext_load_param ldr_parm = {
.relocate_local = !*llext,
.pre_located = true,
.section_detached = llext_manager_section_detached,
.keep_section_info = true,
};
ret = llext_load(ldr, name, llext, &ldr_parm);
if (ret)
return ret;
}
/* All code sections */
llext_get_region_info(ldr, *llext, LLEXT_MEM_TEXT, &hdr, NULL, NULL);
mctx->segment[LIB_MANAGER_TEXT].addr = hdr->sh_addr;
mctx->segment[LIB_MANAGER_TEXT].size = hdr->sh_size;
tr_dbg(&lib_manager_tr, ".text: start: %#lx size %#x",
mctx->segment[LIB_MANAGER_TEXT].addr,
mctx->segment[LIB_MANAGER_TEXT].size);
/* All read-only data sections */
llext_get_region_info(ldr, *llext, LLEXT_MEM_RODATA, &hdr, NULL, NULL);
mctx->segment[LIB_MANAGER_RODATA].addr = hdr->sh_addr;
mctx->segment[LIB_MANAGER_RODATA].size = hdr->sh_size;
tr_dbg(&lib_manager_tr, ".rodata: start: %#lx size %#x",
mctx->segment[LIB_MANAGER_RODATA].addr,
mctx->segment[LIB_MANAGER_RODATA].size);
/* All writable data sections */
llext_get_region_info(ldr, *llext, LLEXT_MEM_DATA, &hdr, NULL, NULL);
mctx->segment[LIB_MANAGER_DATA].addr = hdr->sh_addr;
mctx->segment[LIB_MANAGER_DATA].size = hdr->sh_size;
tr_dbg(&lib_manager_tr, ".data: start: %#lx size %#x",
mctx->segment[LIB_MANAGER_DATA].addr,
mctx->segment[LIB_MANAGER_DATA].size);
/* Writable uninitialized data section */
llext_get_region_info(ldr, *llext, LLEXT_MEM_BSS, &hdr, NULL, NULL);
mctx->segment[LIB_MANAGER_BSS].addr = hdr->sh_addr;
mctx->segment[LIB_MANAGER_BSS].size = hdr->sh_size;
tr_dbg(&lib_manager_tr, ".bss: start: %#lx size %#x",
mctx->segment[LIB_MANAGER_BSS].addr,
mctx->segment[LIB_MANAGER_BSS].size);
*buildinfo = NULL;
ret = llext_section_shndx(ldr, *llext, ".mod_buildinfo");
if (ret >= 0) {
llext_get_section_info(ldr, *llext, ret, &hdr, NULL, NULL);
*buildinfo = llext_peek(ldr, hdr->sh_offset);
}
*mod_manifest = NULL;
ret = llext_section_shndx(ldr, *llext, ".module");
if (ret >= 0) {
llext_get_section_info(ldr, *llext, ret, &hdr, NULL, NULL);
*mod_manifest = llext_peek(ldr, hdr->sh_offset);
}
return *buildinfo && *mod_manifest ? 0 : -EPROTO;
}
/* Count "module files" in the library, allocate and initialize memory for their descriptors */
static int llext_manager_mod_init(struct lib_manager_mod_ctx *ctx,
const struct sof_man_fw_desc *desc)
{
struct sof_man_module *mod_array = (struct sof_man_module *)((uint8_t *)desc +
SOF_MAN_MODULE_OFFSET(0));
unsigned int i, n_mod;
size_t offs;
/* count modules */
for (i = 0, n_mod = 0, offs = ~0; i < desc->header.num_module_entries; i++)
if (mod_array[i].segment[LIB_MANAGER_TEXT].file_offset != offs) {
offs = mod_array[i].segment[LIB_MANAGER_TEXT].file_offset;
n_mod++;
}
/*
* Loadable modules are loaded to DRAM once and never unloaded from it.
* Context, related to them, is never freed
*/
ctx->mod = rmalloc(SOF_MEM_FLAG_KERNEL | SOF_MEM_FLAG_COHERENT,
n_mod * sizeof(ctx->mod[0]));
if (!ctx->mod)
return -ENOMEM;
ctx->n_mod = n_mod;
for (i = 0, n_mod = 0, offs = ~0; i < desc->header.num_module_entries; i++)
if (mod_array[i].segment[LIB_MANAGER_TEXT].file_offset != offs) {
offs = mod_array[i].segment[LIB_MANAGER_TEXT].file_offset;
ctx->mod[n_mod].mapped = false;
ctx->mod[n_mod].llext = NULL;
ctx->mod[n_mod].ebl = NULL;
ctx->mod[n_mod].n_dependent = 0;
ctx->mod[n_mod++].start_idx = i;
}
return 0;
}
/* Find a module context, containing the driver with the supplied index */
static unsigned int llext_manager_mod_find(const struct lib_manager_mod_ctx *ctx, unsigned int idx)
{
unsigned int i;
for (i = 0; i < ctx->n_mod; i++)
if (ctx->mod[i].start_idx > idx)
break;
return i - 1;
}
static int llext_manager_link_single(uint32_t module_id, const struct sof_man_fw_desc *desc,
struct lib_manager_mod_ctx *ctx, const void **buildinfo,
const struct sof_man_module_manifest **mod_manifest)
{
struct sof_man_module *mod_array = (struct sof_man_module *)((uint8_t *)desc +
SOF_MAN_MODULE_OFFSET(0));
uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
size_t mod_offset = mod_array[entry_index].segment[LIB_MANAGER_TEXT].file_offset;
int ret;
tr_dbg(&lib_manager_tr, "mod_id: %u", module_id);
if (entry_index >= desc->header.num_module_entries) {
tr_err(&lib_manager_tr, "Invalid driver index %u exceeds %d",
entry_index, desc->header.num_module_entries - 1);
return -EINVAL;
}
unsigned int mod_ctx_idx = llext_manager_mod_find(ctx, entry_index);
struct lib_manager_module *mctx = ctx->mod + mod_ctx_idx;
size_t mod_size;
int i, inst_idx;
/*
* We don't know the number of ELF files that this library is built of.
* We know the number of module drivers, but each of those ELF files can
* also contain multiple such drivers. Each driver brings two copies of
* its manifest with it: one in the ".module" ELF section and one in an
* array of manifests at the beginning of the library. This latter array
* is created from a TOML configuration file. The order is preserved -
* this is guaranteed by rimage.
* All module drivers within a single ELF file have equal .file_offset,
* this makes it possible to find borders between them.
* We know the global index of the requested driver in that array, but
* we need to find the matching manifest in ".module" because only it
* contains the entry point. For safety we calculate the ELF driver
* index and then also check the driver name.
* We also need a module size. For this we search the manifest array for
* the next ELF file, then the difference between offsets gives us the
* module size.
*/
for (i = entry_index - 1; i >= 0; i--)
if (mod_array[i].segment[LIB_MANAGER_TEXT].file_offset != mod_offset)
break;
/* Driver index within a single module */
inst_idx = entry_index - i - 1;
/* Find the next module or stop at the end */
for (i = entry_index + 1; i < desc->header.num_module_entries; i++)
if (mod_array[i].segment[LIB_MANAGER_TEXT].file_offset != mod_offset)
break;
if (i == desc->header.num_module_entries)
mod_size = desc->header.preload_page_count * PAGE_SZ - mod_offset;
else
mod_size = ALIGN_UP(mod_array[i].segment[LIB_MANAGER_TEXT].file_offset - mod_offset,
PAGE_SZ);
if (!mctx->ebl) {
/* allocate once, never freed */
mctx->ebl = rmalloc(SOF_MEM_FLAG_KERNEL | SOF_MEM_FLAG_COHERENT,
sizeof(struct llext_buf_loader));
if (!mctx->ebl) {
tr_err(&lib_manager_tr, "loader alloc failed");
return -ENOMEM;
}
uint8_t *dram_base = (uint8_t *)desc - SOF_MAN_ELF_TEXT_OFFSET;
*mctx->ebl = (struct llext_buf_loader)LLEXT_BUF_LOADER(dram_base + mod_offset,
mod_size);
}
/*
* LLEXT linking is only needed once for all the "drivers" in the
* module. This calls llext_load(), which also takes references to any
* dependencies, sets up sections and retrieves buildinfo and
* mod_manifest
*/
ret = llext_manager_link(mod_array[entry_index - inst_idx].name, mctx,
buildinfo, mod_manifest);
if (ret < 0) {
tr_err(&lib_manager_tr, "linking failed: %d", ret);
return ret;
}
/* if ret > 0, then the "driver" is already loaded */
if (!ret)
/* mctx->mod_manifest points to a const array of module manifests */
mctx->mod_manifest = *mod_manifest;
/* Return the manifest, related to the specific instance */
*mod_manifest = mctx->mod_manifest + inst_idx;
if (strncmp(mod_array[entry_index].name, (*mod_manifest)->module.name,
sizeof(mod_array[0].name))) {
tr_err(&lib_manager_tr, "Name mismatch %s vs. %s",
mod_array[entry_index].name, (*mod_manifest)->module.name);
return -ENOEXEC;
}
return mod_ctx_idx;
}
static int llext_lib_find(const struct llext *llext, struct lib_manager_module **dep_ctx)
{
struct ext_library *_ext_lib = ext_lib_get();
unsigned int i, j;
if (!llext)
return -EINVAL;
for (i = 0; i < ARRAY_SIZE(_ext_lib->desc); i++) {
if (!_ext_lib->desc[i])
continue;
for (j = 0; j < _ext_lib->desc[i]->n_mod; j++)
if (_ext_lib->desc[i]->mod[j].llext == llext) {
*dep_ctx = _ext_lib->desc[i]->mod + j;
return i;
}
}
return -ENOENT;
}
/* n can be -1 */
static void llext_manager_depend_unlink_rollback(struct lib_manager_module *dep_ctx[], int n)
{
for (; n >= 0; n--)
if (!dep_ctx[n])
tr_err(&lib_manager_tr, "dependency %d NULL", n);
else if (!--dep_ctx[n]->n_dependent)
llext_manager_unload_module(dep_ctx[n]);
}
uintptr_t llext_manager_allocate_module(const struct comp_ipc_config *ipc_config,
const void *ipc_specific_config)
{
uint32_t module_id = IPC4_MOD_ID(ipc_config->id);
/* Library manifest */
const struct sof_man_fw_desc *desc = (struct sof_man_fw_desc *)
lib_manager_get_library_manifest(module_id);
/* Library context */
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
if (!ctx || !desc) {
tr_err(&lib_manager_tr, "failed to get module descriptor");
return 0;
}
/* Array of all "module drivers" (manifests) in the library */
const struct sof_man_module_manifest *mod_manifest;
const struct sof_module_api_build_info *buildinfo = NULL;
/* "module file" index in the ctx->mod array */
int mod_ctx_idx = llext_manager_link_single(module_id, desc, ctx,
(const void **)&buildinfo, &mod_manifest);
if (mod_ctx_idx < 0)
return 0;
struct lib_manager_module *mctx = ctx->mod + mod_ctx_idx;
if (buildinfo) {
/* First instance: check that the module is native */
if (buildinfo->format != SOF_MODULE_API_BUILD_INFO_FORMAT ||
buildinfo->api_version_number.full != SOF_MODULE_API_CURRENT_VERSION) {
tr_err(&lib_manager_tr, "Unsupported module API version");
return 0;
}
}
if (!mctx->mapped) {
int i, ret;
/*
* Check if any dependencies need to be mapped - collect
* pointers to library contexts
*/
struct lib_manager_module *dep_ctx[LLEXT_MAX_DEPENDENCIES] = {};
for (i = 0; i < ARRAY_SIZE(mctx->llext->dependency); i++) {
struct lib_manager_module *dep;
/* Dependencies are filled from the beginning of the array upwards */
if (!mctx->llext->dependency[i])
break;
ret = llext_lib_find(mctx->llext->dependency[i], &dep);
if (ret < 0) {
tr_err(&lib_manager_tr,
"Unmet dependency: cannot find dependency %u", i);
continue;
}
tr_dbg(&lib_manager_tr, "%s depending on %s index %u, %u users",
mctx->llext->name, mctx->llext->dependency[i]->name,
dep->start_idx, dep->n_dependent);
/*
* Protected by the IPC serialization, but maybe we should protect the
* dependent-count explicitly too. It is incremented when a new dependent
* is identified. If it's non-zero, then some other modules also depend
* on it and have already mapped it.
*/
if (dep->n_dependent++)
continue;
/* First user of this dependency, load it into SRAM */
ret = llext_manager_load_module(dep);
if (ret < 0) {
dep->n_dependent--;
llext_manager_depend_unlink_rollback(dep_ctx, i - 1);
return 0;
}
dep_ctx[i] = dep;
}
/* Map executable code and data */
ret = llext_manager_load_module(mctx);
if (ret < 0)
return 0;
}
return mod_manifest->module.entry_point;
}
#ifdef CONFIG_USERSPACE
static int llext_manager_add_partition(struct k_mem_domain *domain,
uintptr_t addr, size_t size,
k_mem_partition_attr_t attr)
{
size_t pre_pad_size = addr & (PAGE_SZ - 1);
struct k_mem_partition part = {
.start = addr - pre_pad_size,
.size = ALIGN_UP(pre_pad_size + size, PAGE_SZ),
.attr = attr,
};
tr_dbg(&lib_manager_tr, "add %#zx @ %lx partition", part.size, part.start);
return k_mem_domain_add_partition(domain, &part);
}
static int llext_manager_rm_partition(struct k_mem_domain *domain,
uintptr_t addr, size_t size,
k_mem_partition_attr_t attr)
{
size_t pre_pad_size = addr & (PAGE_SZ - 1);
struct k_mem_partition part = {
.start = addr - pre_pad_size,
.size = ALIGN_UP(pre_pad_size + size, PAGE_SZ),
.attr = attr,
};
tr_dbg(&lib_manager_tr, "remove %#zx @ %lx partition", part.size, part.start);
return k_mem_domain_remove_partition(domain, &part);
}
int llext_manager_add_domain(const uint32_t component_id, struct k_mem_domain *domain)
{
const uint32_t module_id = IPC4_MOD_ID(component_id);
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
const unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
struct lib_manager_module *mctx = ctx->mod + mod_idx;
const struct llext *ext = mctx->llext;
const struct llext_loader *ldr = &mctx->ebl->loader;
/* Executable code (.text) */
uintptr_t va_base_text = mctx->segment[LIB_MANAGER_TEXT].addr;
size_t text_size = mctx->segment[LIB_MANAGER_TEXT].size;
/* Read-only data (.rodata and others) */
uintptr_t va_base_rodata = mctx->segment[LIB_MANAGER_RODATA].addr;
size_t rodata_size = mctx->segment[LIB_MANAGER_RODATA].size;
/* Writable data (.data, .bss and others) */
uintptr_t va_base_data = mctx->segment[LIB_MANAGER_DATA].addr;
size_t data_size = mctx->segment[LIB_MANAGER_DATA].size;
int ret = llext_manager_add_partition(domain, va_base_text, text_size,
K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB);
if (ret < 0)
return ret;
if (rodata_size) {
ret = llext_manager_add_partition(domain, va_base_rodata, rodata_size,
K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB);
if (ret < 0)
goto e_text;
}
if (data_size) {
ret = llext_manager_add_partition(domain, va_base_data, data_size,
K_MEM_PARTITION_P_RW_U_RW | XTENSA_MMU_CACHED_WB);
if (ret < 0)
goto e_rodata;
}
elf_shdr_t shdr_cold, shdr_coldrodata;
bool rodata = false, text = false;
const void *rodata_addr = NULL, *text_addr = NULL;
size_t text_offset = 0, rodata_offset = 0;
shdr_cold.sh_size = 0;
shdr_coldrodata.sh_size = 0;
ret = llext_get_section_header(ldr, ext, ".cold", &shdr_cold);
if (ret < 0)
tr_warn(&lib_manager_tr, "couldn't get .cold header");
else
llext_get_region_info(ldr, ext, LLEXT_MEM_TEXT, NULL, &text_addr, NULL);
ret = llext_get_section_header(ldr, ext, ".coldrodata", &shdr_coldrodata);
if (ret < 0)
tr_warn(&lib_manager_tr, "couldn't get .coldrodata header");
else
llext_get_region_info(ldr, ext, LLEXT_MEM_RODATA, NULL, &rodata_addr, NULL);
for (unsigned int i = 0; i < llext_section_count(ext) && (!rodata || !text); i++) {
const elf_shdr_t *shdr;
enum llext_mem s_region = LLEXT_MEM_COUNT;
size_t s_offset = 0;
ret = llext_get_section_info(ldr, ext, i, &shdr, &s_region, &s_offset);
if (ret < 0)
continue;
switch (s_region) {
case LLEXT_MEM_TEXT:
if (shdr_cold.sh_size &&
shdr->sh_name == shdr_cold.sh_name &&
shdr->sh_offset == shdr_cold.sh_offset && !text) {
text = true;
text_offset = s_offset;
}
break;
case LLEXT_MEM_RODATA:
if (shdr_coldrodata.sh_size &&
shdr->sh_name == shdr_coldrodata.sh_name &&
shdr->sh_offset == shdr_coldrodata.sh_offset && !rodata) {
rodata = true;
rodata_offset = s_offset;
}
break;
default:
break;
}
}
if (text) {
tr_dbg(&lib_manager_tr, ".cold %#x @ %#lx",
shdr_cold.sh_size, (uintptr_t)text_addr + text_offset);
ret = llext_manager_add_partition(domain, (uintptr_t)text_addr + text_offset,
shdr_cold.sh_size,
K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB);
if (ret < 0)
goto e_data;
mctx->segment[LIB_MANAGER_COLD].addr = (uintptr_t)text_addr + text_offset;
mctx->segment[LIB_MANAGER_COLD].size = shdr_cold.sh_size;
}
if (rodata) {
tr_dbg(&lib_manager_tr, ".coldrodata %#x @ %#lx",
shdr_coldrodata.sh_size, (uintptr_t)rodata_addr + rodata_offset);
ret = llext_manager_add_partition(domain, (uintptr_t)rodata_addr + rodata_offset,
shdr_coldrodata.sh_size,
K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB);
if (ret < 0)
goto e_cold;
mctx->segment[LIB_MANAGER_COLDRODATA].addr = (uintptr_t)rodata_addr + rodata_offset;
mctx->segment[LIB_MANAGER_COLDRODATA].size = shdr_coldrodata.sh_size;
}
return 0;
e_cold:
llext_manager_rm_partition(domain, (uintptr_t)text_addr + text_offset, shdr_cold.sh_size,
K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB);
mctx->segment[LIB_MANAGER_COLD].addr = 0;
mctx->segment[LIB_MANAGER_COLD].size = 0;
e_data:
llext_manager_rm_partition(domain, va_base_data, data_size,
K_MEM_PARTITION_P_RW_U_RW | XTENSA_MMU_CACHED_WB);
e_rodata:
llext_manager_rm_partition(domain, va_base_rodata, rodata_size,
K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB);
e_text:
llext_manager_rm_partition(domain, va_base_text, text_size,
K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB);
return ret;
}
int llext_manager_rm_domain(const uint32_t component_id, struct k_mem_domain *domain)
{
const uint32_t module_id = IPC4_MOD_ID(component_id);
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
const unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
struct lib_manager_module *mctx = ctx->mod + mod_idx;
/* Executable code (.text) */
uintptr_t va_base_text = mctx->segment[LIB_MANAGER_TEXT].addr;
size_t text_size = mctx->segment[LIB_MANAGER_TEXT].size;
/* Read-only data (.rodata and others) */
uintptr_t va_base_rodata = mctx->segment[LIB_MANAGER_RODATA].addr;
size_t rodata_size = mctx->segment[LIB_MANAGER_RODATA].size;
/* Writable data (.data, .bss and others) */
uintptr_t va_base_data = mctx->segment[LIB_MANAGER_DATA].addr;
size_t data_size = mctx->segment[LIB_MANAGER_DATA].size;
int err, ret = llext_manager_rm_partition(domain, va_base_text, text_size,
K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB);
if (ret < 0)
tr_err(&lib_manager_tr, "failed to remove .text memory partition: %d", ret);
if (rodata_size) {
err = llext_manager_rm_partition(domain, va_base_rodata, rodata_size,
K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB);
if (err < 0) {
tr_err(&lib_manager_tr, "failed to remove .rodata memory partition: %d",
err);
if (!ret)
ret = err;
}
}
if (data_size) {
err = llext_manager_rm_partition(domain, va_base_data, data_size,
K_MEM_PARTITION_P_RW_U_RW | XTENSA_MMU_CACHED_WB);
if (err < 0) {
tr_err(&lib_manager_tr, "failed to remove .data memory partition: %d", err);
if (!ret)
ret = err;
}
}
if (mctx->segment[LIB_MANAGER_COLD].addr) {
err = llext_manager_rm_partition(domain,
mctx->segment[LIB_MANAGER_COLD].addr,
mctx->segment[LIB_MANAGER_COLD].size,
K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB);
if (err < 0) {
tr_err(&lib_manager_tr, "failed to remove .cold memory partition: %d", err);
if (!ret)
ret = err;
}
}
if (mctx->segment[LIB_MANAGER_COLDRODATA].addr) {
err = llext_manager_rm_partition(domain,
mctx->segment[LIB_MANAGER_COLDRODATA].addr,
mctx->segment[LIB_MANAGER_COLDRODATA].size,
K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB);
if (err < 0) {
tr_err(&lib_manager_tr,
"failed to remove .coldrodata memory partition: %d", err);
if (!ret)
ret = err;
}
}
return ret;
}
#endif
int llext_manager_free_module(const uint32_t component_id)
{
const uint32_t module_id = IPC4_MOD_ID(component_id);
struct sof_man_fw_desc *desc = (struct sof_man_fw_desc *)lib_manager_get_library_manifest(module_id);
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
if (entry_index >= desc->header.num_module_entries) {
tr_err(&lib_manager_tr, "Invalid driver index %u exceeds %d",
entry_index, desc->header.num_module_entries - 1);
return -ENOENT;
}
if (!ctx->mod) {
tr_err(&lib_manager_tr, "NULL module array: ID %#x ctx %p", component_id, ctx);
return -ENOENT;
}
unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
struct lib_manager_module *mctx = ctx->mod + mod_idx;
/* Protected by IPC serialization */