Skip to content

Commit 7fe6862

Browse files
committed
core: add boot_mem section for pager
With CFG_WITH_PAGER=y, a special section of memory is carved out by the linker script to be used by the boot_mem*() functions during boot. The amount of memory must cover the worst case, but the remaining unused memory will be returned to be managed by the pager at end of boot in a later patch. This enables boot configuration in the same way regardless of CFG_DYN_CONFIG. Add support in boot_mem_release_unused() and boot_mem_release_tmp_alloc() to pass the unused memory to the pager. This is needed in later patches with CFG_WITH_PAGER=y and CFG_DYN_CONFIG=y. Signed-off-by: Jens Wiklander <[email protected]>
1 parent 11657db commit 7fe6862

File tree

6 files changed

+85
-63
lines changed

6 files changed

+85
-63
lines changed

core/arch/arm/kernel/boot.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,6 @@ static void init_pager_runtime(unsigned long pageable_part)
511511
assert(hashes);
512512
asan_memcpy_unchecked(hashes, tmp_hashes, hash_size);
513513

514-
/*
515-
* The pager is about the be enabled below, eventual temporary boot
516-
* memory allocation must be removed now.
517-
*/
518-
boot_mem_release_tmp_alloc();
519-
520514
carve_out_asan_mem();
521515

522516
mm = nex_phys_mem_ta_alloc(pageable_size);
@@ -966,15 +960,6 @@ static void init_primary(unsigned long pageable_part)
966960
core_mmu_save_mem_map();
967961
core_mmu_init_phys_mem();
968962
boot_mem_foreach_padding(add_padding_to_pool, NULL);
969-
va = boot_mem_release_unused();
970-
if (!IS_ENABLED(CFG_WITH_PAGER)) {
971-
/*
972-
* We must update boot_cached_mem_end to reflect the memory
973-
* just unmapped by boot_mem_release_unused().
974-
*/
975-
assert(va && va <= boot_cached_mem_end);
976-
boot_cached_mem_end = va;
977-
}
978963

979964
if (IS_ENABLED(CFG_DYN_CONFIG)) {
980965
/*
@@ -999,6 +984,23 @@ static void init_primary(unsigned long pageable_part)
999984
init_pager_runtime(pageable_part);
1000985
}
1001986

987+
va = boot_mem_release_unused();
988+
if (IS_ENABLED(CFG_WITH_PAGER)) {
989+
/*
990+
* Paging is activated, and anything beyond the start of
991+
* the released unused memory is managed by the pager.
992+
*/
993+
assert(va && va <= core_mmu_linear_map_end);
994+
core_mmu_linear_map_end = va;
995+
} else {
996+
/*
997+
* We must update boot_cached_mem_end to reflect the memory
998+
* just unmapped by boot_mem_release_unused().
999+
*/
1000+
assert(va && va <= boot_cached_mem_end);
1001+
boot_cached_mem_end = va;
1002+
}
1003+
10021004
/* Initialize canaries around the stacks */
10031005
thread_init_canaries();
10041006
thread_init_per_cpu();
@@ -1117,9 +1119,7 @@ void __weak boot_init_primary_runtime(void)
11171119
~THREAD_EXCP_NATIVE_INTR);
11181120
init_tee_runtime();
11191121
}
1120-
1121-
if (!IS_ENABLED(CFG_WITH_PAGER))
1122-
boot_mem_release_tmp_alloc();
1122+
boot_mem_release_tmp_alloc();
11231123
}
11241124

11251125
void __weak boot_init_primary_final(void)

core/arch/arm/kernel/entry_a32.S

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,23 +534,16 @@ shadow_stack_access_ok:
534534
bl boot_save_args
535535
add sp, sp, #(2 * 4)
536536

537+
ldr r0, =__vcore_free_start
538+
ldr r2, =__vcore_free_end
537539
#ifdef CFG_WITH_PAGER
538-
ldr r0, =__init_end /* pointer to boot_embdata */
539-
ldr r1, [r0] /* struct boot_embdata::total_len */
540-
add r0, r0, r1
541-
mov_imm r1, 0xfff
542-
add r0, r0, r1 /* round up */
543-
bic r0, r0, r1 /* to next page */
544-
mov_imm r1, (TEE_RAM_PH_SIZE + TEE_RAM_START)
545-
mov r2, r1
540+
mov r1, r2
546541
#else
547-
ldr r0, =__vcore_free_start
548542
ldr r1, =boot_embdata_ptr
549543
ldr r1, [r1]
550544
#ifdef CFG_DYN_CONFIG
551545
sub r1, r1, #THREAD_BOOT_INIT_TMP_ALLOC
552546
#endif
553-
ldr r2, =__vcore_free_end
554547
#endif
555548
bl boot_mem_init
556549

core/arch/arm/kernel/entry_a64.S

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,22 +388,16 @@ clear_nex_bss:
388388
mov x4, xzr
389389
bl boot_save_args
390390

391+
adr_l x0, __vcore_free_start
392+
adr_l x2, __vcore_free_end
391393
#ifdef CFG_WITH_PAGER
392-
adr_l x0, __init_end /* pointer to boot_embdata */
393-
ldr w1, [x0] /* struct boot_embdata::total_len */
394-
add x0, x0, x1
395-
add x0, x0, #0xfff /* round up */
396-
bic x0, x0, #0xfff /* to next page */
397-
mov_imm x1, (TEE_RAM_PH_SIZE + TEE_RAM_START)
398-
mov x2, x1
394+
mov x1, x2
399395
#else
400-
adr_l x0, __vcore_free_start
401396
adr_l x1, boot_embdata_ptr
402397
ldr x1, [x1]
403398
#ifdef CFG_DYN_CONFIG
404399
sub x1, x1, #THREAD_BOOT_INIT_TMP_ALLOC
405400
#endif
406-
adr_l x2, __vcore_free_end;
407401
#endif
408402
bl boot_mem_init
409403

core/arch/arm/kernel/kern.ld.S

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,12 @@ SECTIONS
244244
__bss_end = .;
245245
}
246246

247-
#ifdef CFG_DYN_CONFIG
247+
#if defined(CFG_DYN_CONFIG) && !defined(CFG_WITH_PAGER)
248248
. = ALIGN(SMALL_PAGE_SIZE);
249249
__flatmap_free_start = .;
250-
#else
250+
#endif
251+
252+
#ifndef CFG_DYN_CONFIG
251253
.heap1 (NOLOAD) : {
252254
/*
253255
* We're keeping track of the padding added before the
@@ -285,10 +287,7 @@ SECTIONS
285287
. = ALIGN(8);
286288
__nozi_stack_end = .;
287289
}
288-
#ifndef CFG_WITH_PAGER
289-
. = ALIGN(SMALL_PAGE_SIZE);
290-
__flatmap_free_start = .;
291-
#else
290+
#ifdef CFG_WITH_PAGER
292291
.heap2 (NOLOAD) : {
293292
__heap2_start = .;
294293
/*
@@ -300,6 +299,27 @@ SECTIONS
300299
. = ALIGN(SMALL_PAGE_SIZE);
301300
__heap2_end = .;
302301
}
302+
#endif
303+
#ifndef CFG_WITH_PAGER
304+
. = ALIGN(SMALL_PAGE_SIZE);
305+
__flatmap_free_start = .;
306+
#endif
307+
#endif /*!CFG_DYN_CONFIG*/
308+
309+
#ifdef CFG_WITH_PAGER
310+
/*
311+
* This memory is used by the boot_mem*() functions during boot.
312+
* Enough memory must be carved out to support the worst case
313+
* memory allocation, but the remaining unused memory will be
314+
* returned to be managed by the pager at end of boot. The tradeoff
315+
* is that the init code follows this section and must also fit in
316+
* the physical memory.
317+
*/
318+
.pager_boot_mem (NOLOAD): {
319+
. += SMALL_PAGE_SIZE;
320+
}
321+
__flatmap_free_start = ADDR(.pager_boot_mem);
322+
__flatmap_free_size = SIZEOF(.pager_boot_mem);
303323

304324
/* Start page aligned read-only memory */
305325
__flatmap_unpg_rw_size = . - __flatmap_unpg_rw_start;
@@ -386,10 +406,11 @@ SECTIONS
386406
(__pageable_end - __pageable_start) / 4096 * 32 +
387407
SIZEOF(.rel) / 2 + SIZEOF(.rela) / 3 ,
388408
"Too few free pages to initialize paging")
389-
390-
391409
#endif /*CFG_WITH_PAGER*/
392-
#endif /*CFG_DYN_CONFIG*/
410+
411+
#ifndef CFG_WITH_PAGER
412+
__flatmap_free_start = .;
413+
#endif
393414

394415
#ifdef CFG_CORE_SANITIZE_KADDRESS
395416
/*
@@ -483,11 +504,9 @@ __vcore_unpg_rw_start = __flatmap_unpg_rw_start;
483504
__vcore_unpg_rw_size = __flatmap_unpg_rw_size;
484505
__vcore_unpg_rw_end = __vcore_unpg_rw_start + __vcore_unpg_rw_size;
485506

486-
#ifndef CFG_WITH_PAGER
487507
__vcore_free_start = __flatmap_free_start;
488508
__vcore_free_size = __flatmap_free_size;
489509
__vcore_free_end = __flatmap_free_start + __flatmap_free_size;
490-
#endif
491510

492511
#ifdef CFG_NS_VIRTUALIZATION
493512
/* Nexus read-write memory */

core/include/kernel/linker.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,10 @@ extern const uint8_t __extab_end[];
4646

4747
#define VCORE_START_VA ((vaddr_t)__text_start)
4848

49-
#ifndef CFG_WITH_PAGER
5049
#define VCORE_FREE_PA ((unsigned long)__vcore_free_start)
5150
#define VCORE_FREE_SZ ((size_t)(__vcore_free_end - \
5251
__vcore_free_start))
5352
#define VCORE_FREE_END_PA ((unsigned long)__vcore_free_end)
54-
#else
55-
/* No VCORE_FREE range in pager configuration since it uses all memory */
56-
#define VCORE_FREE_PA PADDR_MAX
57-
#define VCORE_FREE_SZ 0
58-
#define VCORE_FREE_END_PA PADDR_MAX
59-
#endif
6053

6154
#define EMIT_SECTION_INFO_SYMBOLS(section_name) \
6255
extern const uint8_t __vcore_ ## section_name ## _start[]; \

core/mm/boot_mem.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
#include <string.h>
1515
#include <util.h>
1616

17+
#ifdef CFG_WITH_PAGER
18+
#include <mm/tee_pager.h>
19+
#endif
20+
21+
static inline void pager_add_pages(vaddr_t vaddr __maybe_unused,
22+
size_t npages __maybe_unused)
23+
{
24+
#ifdef CFG_WITH_PAGER
25+
tee_pager_add_pages(vaddr, npages, true);
26+
#endif
27+
}
28+
1729
/*
1830
* struct boot_mem_reloc - Pointers relocated in memory during boot
1931
* @ptrs: Array of relocation
@@ -109,7 +121,6 @@ static void *mem_alloc(struct boot_mem_desc *desc, size_t len, size_t align)
109121
if (IS_ENABLED(CFG_CORE_SANITIZE_KADDRESS))
110122
align = MAX(align, ASAN_BLOCK_SIZE);
111123

112-
runtime_assert(!IS_ENABLED(CFG_WITH_PAGER));
113124
assert(desc && desc->mem_start && desc->mem_end);
114125
assert(IS_POWER_OF_TWO(align));
115126
va = ROUNDUP2(desc->mem_start, align);
@@ -301,18 +312,24 @@ vaddr_t boot_mem_release_unused(void)
301312
(size_t)(boot_mem_desc->orig_mem_end - boot_mem_desc->mem_end),
302313
boot_mem_desc->mem_end);
303314

304-
if (IS_ENABLED(CFG_WITH_PAGER))
315+
va = ROUNDUP(boot_mem_desc->mem_start, SMALL_PAGE_SIZE);
316+
tmp_va = ROUNDDOWN(boot_mem_desc->mem_end, SMALL_PAGE_SIZE);
317+
318+
if (IS_ENABLED(CFG_WITH_PAGER)) {
319+
if (tmp_va > va) {
320+
n = tmp_va - va;
321+
DMSG("Releasing %zu bytes from va %#"PRIxVA, n, va);
322+
pager_add_pages(va, n / SMALL_PAGE_SIZE);
323+
}
305324
goto out;
325+
}
306326

307327
pa = vaddr_to_phys(ROUNDUP(boot_mem_desc->orig_mem_start,
308328
SMALL_PAGE_SIZE));
309329
mm = nex_phys_mem_mm_find(pa);
310330
if (!mm)
311331
panic();
312332

313-
va = ROUNDUP(boot_mem_desc->mem_start, SMALL_PAGE_SIZE);
314-
315-
tmp_va = ROUNDDOWN(boot_mem_desc->mem_end, SMALL_PAGE_SIZE);
316333
tmp_n = boot_mem_desc->orig_mem_end - tmp_va;
317334
tmp_pa = vaddr_to_phys(tmp_va);
318335

@@ -343,6 +360,7 @@ vaddr_t boot_mem_release_unused(void)
343360
void boot_mem_release_tmp_alloc(void)
344361
{
345362
tee_mm_entry_t *mm = NULL;
363+
vaddr_t tmp_va = 0;
346364
vaddr_t va = 0;
347365
paddr_t pa = 0;
348366
size_t n = 0;
@@ -353,10 +371,15 @@ void boot_mem_release_tmp_alloc(void)
353371
va = MAX(ROUNDDOWN(boot_mem_desc->mem_end, SMALL_PAGE_SIZE),
354372
ROUNDUP(boot_mem_desc->final_mem_start, SMALL_PAGE_SIZE));
355373
if (IS_ENABLED(CFG_WITH_PAGER)) {
356-
n = ROUNDDOWN(boot_mem_desc->orig_mem_end, SMALL_PAGE_SIZE) -
357-
va;
374+
tmp_va = ROUNDDOWN(boot_mem_desc->orig_mem_end,
375+
SMALL_PAGE_SIZE);
376+
if (tmp_va > va) {
377+
n = tmp_va - va;
378+
DMSG("Releasing %zu bytes from va %#"PRIxVA, n, va);
379+
pager_add_pages(va, n / SMALL_PAGE_SIZE);
380+
}
381+
358382
boot_mem_desc = NULL;
359-
DMSG("Releasing %zu bytes from va %#"PRIxVA, n, va);
360383
return;
361384
}
362385

0 commit comments

Comments
 (0)