QUESTION
_start는 언제 인자를 보존하고, 어느 시점에 주소 변환을 켜는가?
이 글은 OP-TEE OS 4.10.0의 AArch64 entry_a64.S를 따른다. BL31에서 받은 x0–x3를 _start가 먼저 x19–x22에 잠시 복사한다. 이것만으로 각 인자의 의미가 확정되지는 않는다. 스택이 마련된 뒤 boot_save_args()가 빌드 설정에 따라 해석한다.
초기 vector와 SCTLR 설정 → pager 유무에 따른 이미지 복사 → BSS zero → 조건부 물리 relocation → 임시 스택/core-local → 인자 저장·boot memory·MMU map → enable_mmu() → primary C 초기화 순서다. console_init()과 boot_save_args() 같은 C 호출도 MMU를 켜기 전에 있다.
STRUCTURE
EL3 인계와 OP-TEE 내부 준비의 경계
BL31 → Secure EL1
_start: x19–x22에 임시 복사reset vector를 VBAR_EL1에 두고 SCTLR 정책을 조정한다.OP-TEE 초기화
CFG_DYN_CONFIG에 따라 확보 방식이 달라진다.그 뒤 x19–x22를 x0–x3로 되돌려 boot_save_args()를 호출한다. 초기 reset_vect_table과 최종 OPTEED 경로에서 BL31에 주는 thread_vector_table은 다르다.
entry 165–183 · stack/args 304–389 · runtime vector 631–638.
CALL PATH
MMU 전후의 실제 호출 경계
MMU가 아직 꺼진 구간
set_sctlr_el1은 SCTLR.M을 켜지 않는다.boot_save_args → boot_mem_init → core_init_mmu_mapMMU 이전에도 C 코드에 들어간다.주소 변환 이후
enable_mmuTTBR/TCR/MAIR와 SCTLR.M, VBAR/SP/LR offset을 전환한다.boot_init_primary_earlypager라면 pageable part address로 runtime을 준비한다.boot_init_primary_late → runtime → finalDT/스레드·서비스 초기화는 별도 단계다.CFG_CORE_FFA에 따라 마지막 제어권 처리가 다르다.첫 C 호출 시점과 MMU 전환 시점은 같은 경계가 아니다. 보조 CPU의 cpu_on_handler는 별도 진입점이다.
STATE LEDGER
다음 단계가 실제로 읽는 상태
아래는 primary _start다. 설정이 꺼져 있으면 해당 분기는 실행되지 않는다.
| 경계 | 조건 | 확보한 상태 | 다음 소비자 |
|---|---|---|---|
| 165–184 | BL32 entry x0–x3 | x19–x22 임시 사본; reset VBAR | 메모리 준비 |
| 186–243 | CFG_WITH_PAGER | init+embedded data 또는 embedded data만 역방향 복사 | BSS zero |
| 245–284 | BSS 범위; NS virtualization·물리 relocation 옵션 | BSS와 조건부 nex BSS zero; 조건부 load PA offset | stack |
| 304–389 | CFG_DYN_CONFIG | SP_EL0 임시 stack·SP_EL1 core-local; 해석된 boot arg | boot memory/map |
| 391–453 | pager/ASLR/MEMTAG 설정 | boot_mem·MMU map; 조건부 VA relocation | enable_mmu |
| 705–775 | identity map, core position | TTBR/TCR/MAIR, SCTLR.M, VBAR/SP/LR offset | primary init |
| 507–638 | MMU·초기화 결과 | early→late→runtime→final; OPTEED SMC 또는 FF-A wait | BL31 또는 FF-A |
VISUAL WALKTHROUGH
이미지 배치와 boot argument는 설정으로 갈린다
CFG_WITH_PAGER=y
__data_end → __init_startinit code·rodata와 뒤따른 boot_embdata를 역방향 복사한다.boot_cached_mem_end복사 영역 끝을 기록한다. pageable backing 주소 x0는 여기서 복사하는 대상과 다르다.CFG_WITH_PAGER=n
__data_end → __vcore_free_end 앞embedded data 길이를 읽고 page 경계로 내린 목적지에 역방향 복사한다.boot_embdata_ptr목적지 주소와 cached-memory 끝을 기록한다.이후 BSS를 지운다. 물리 relocation(CFG_CORE_PHYS_RELOCATABLE)과 가상 map offset relocation(CFG_CORE_ASLR)은 서로 다른 조건이다.
| 설정 | x0 | x1 | x2 | x3 |
|---|---|---|---|---|
기본 · CFG_CORE_FFA=n | pager면 pageable part PA | AArch64 기본 인자에서 미사용 | system DTB (있으면) | 미사용 |
CFG_CORE_FFA=y | TOS FW config 또는 FF-A boot info blob | 기본 인자에서 미사용 | SEL2 SPMC가 아니면 DTB | 기본 인자에서 미사용 |
CFG_TRANSFER_LIST=y handoff | DTB PA 또는 0 | TL signature/version | 0이어야 함 | TL base PA |
Transfer List는 boot_save_args()가 검증한다. FF-A·TL·기본 pager 인자를 동시에 소비하는 그림은 틀리다. 플랫폼의 weak 함수 override도 별도 확인한다.
UPSTREAM SOURCE
원본 코드
아래 코드는 OP-TEE OS 4.10.0의 core/arch/arm/kernel/entry_a64.S에서 141-229줄을 그대로 가져온 것이다.
141
142 .macro init_pauth_secondary_cpu
143 msr spsel, #1
144 ldp x0, x1, [sp, #THREAD_CORE_LOCAL_KEYS]
145 msr spsel, #0
146 write_apiakeyhi x0
147 write_apiakeylo x1
148 mrs x0, sctlr_el1
149 orr x0, x0, #SCTLR_ENIA
150 msr sctlr_el1, x0
151 isb
152 .endm
153
154 .macro init_pan
155 read_feat_pan x0
156 cmp x0, #0
157 b.eq 1f
158 mrs x0, sctlr_el1
159 bic x0, x0, #SCTLR_SPAN
160 msr sctlr_el1, x0
161 write_pan_enable
162 1:
163 .endm
164
165FUNC _start , :
166 /*
167 * Temporary copy of boot argument registers, will be passed to
168 * boot_save_args() further down.
169 */
170 mov x19, x0
171 mov x20, x1
172 mov x21, x2
173 mov x22, x3
174
175 adr x0, reset_vect_table
176 msr vbar_el1, x0
177 isb
178
179#ifdef CFG_PAN
180 init_pan
181#endif
182
183 set_sctlr_el1
184 isb
185
186#ifdef CFG_WITH_PAGER
187 /*
188 * Move init code into correct location and move hashes to a
189 * temporary safe location until the heap is initialized.
190 *
191 * The binary is built as:
192 * [Pager code, rodata and data] : In correct location
193 * [Init code and rodata] : Should be copied to __init_start
194 * [struct boot_embdata + data] : Should be saved before
195 * initializing pager, first uint32_t tells the length of the data
196 */
197 adr x0, __init_start /* dst */
198 adr x1, __data_end /* src */
199 adr x2, __init_end
200 sub x2, x2, x0 /* init len */
201 ldr w4, [x1, x2] /* length of hashes etc */
202 add x2, x2, x4 /* length of init and hashes etc */
203 /* Copy backwards (as memmove) in case we're overlapping */
204 add x0, x0, x2 /* __init_start + len */
205 add x1, x1, x2 /* __data_end + len */
206 adr_l x3, boot_cached_mem_end
207 str x0, [x3]
208 adr x2, __init_start
209copy_init:
210 ldp x3, x4, [x1, #-16]!
211 stp x3, x4, [x0, #-16]!
212 cmp x0, x2
213 b.gt copy_init
214#else
215 /*
216 * The binary is built as:
217 * [Core, rodata and data] : In correct location
218 * [struct boot_embdata + data] : Should be moved to right before
219 * __vcore_free_end, the first uint32_t tells the length of the
220 * struct + data
221 */
222 adr_l x1, __data_end /* src */
223 ldr w2, [x1] /* struct boot_embdata::total_len */
224 /* dst */
225 adr_l x0, __vcore_free_end
226 sub x0, x0, x2
227 /* round down to beginning of page */
228 bic x0, x0, #(SMALL_PAGE_SIZE - 1)
229 adr_l x3, boot_embdata_ptr
READING CONTRACT
발췌 범위와 읽기 기준
위 원본 코드는 entry_a64.S:141–229만 보여 준다. _start는 165행에서 시작하지만 229행은 non-pager boot_embdata_ptr 준비 중이며 함수 끝이 아니다. 원래의 boot.c와 pager 발췌는 서로 다른 소스 범위다.
stack·MMU·primary init·마지막 SMC는 entry_a64.S:230–639까지 읽어야 한다. 위 발췌의 CFG_WITH_PAGER 양쪽 분기를 동시에 실행하는 것으로 읽지 않는다.
RELATED SOURCE
함께 읽어야 하는 원본 코드
첫 코드 조각만으로 동작이 완성되지 않는 경우 호출 매크로, 객체 정의와 실제 실행 목록을 같은 페이지에 묶었다. 각 조각은 같은 기준 commit에서 가져왔다.
01. assembly 초기화 뒤 호출되는 primary C 초기화
core/arch/arm/kernel/boot.c 871-993줄이다.
871}
872
873static bool add_padding_to_pool(vaddr_t va, size_t len, void *ptr __unused)
874{
875#ifdef CFG_NS_VIRTUALIZATION
876 nex_malloc_add_pool((void *)va, len);
877#else
878 malloc_add_pool((void *)va, len);
879#endif
880 return true;
881}
882
883static void init_primary(unsigned long pageable_part)
884{
885 vaddr_t va = 0;
886
887 /*
888 * Mask asynchronous exceptions before switch to the thread vector
889 * as the thread handler requires those to be masked while
890 * executing with the temporary stack. The thread subsystem also
891 * asserts that the foreign interrupts are blocked when using most of
892 * its functions.
893 */
894 thread_set_exceptions(THREAD_EXCP_ALL);
895 primary_save_cntfrq();
896 init_vfp_sec();
897
898 if (IS_ENABLED(CFG_CRYPTO_WITH_CE))
899 check_crypto_extensions();
900
901 init_asan();
902
903 /*
904 * By default whole OP-TEE uses malloc, so we need to initialize
905 * it early. But, when virtualization is enabled, malloc is used
906 * only by TEE runtime, so malloc should be initialized later, for
907 * every virtual partition separately. Core code uses nex_malloc
908 * instead.
909 */
910#ifdef CFG_WITH_PAGER
911 /* Add heap2 first as heap1 may be too small as initial bget pool */
912 malloc_add_pool(__heap2_start, __heap2_end - __heap2_start);
913#endif
914#ifdef CFG_NS_VIRTUALIZATION
915 nex_malloc_add_pool(__nex_heap_start, __nex_heap_end -
916 __nex_heap_start);
917#else
918 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
919#endif
920 IMSG_RAW("\n");
921 if (IS_ENABLED(CFG_DYN_CONFIG)) {
922 size_t sz = sizeof(struct thread_core_local) *
923 CFG_TEE_CORE_NB_CORE;
924 void *p = boot_mem_alloc(sz, alignof(void *) * 2);
925
926#ifdef CFG_NS_VIRTUALIZATION
927 nex_malloc_add_pool(p, sz);
928#else
929 malloc_add_pool(p, sz);
930#endif
931 }
932
933 core_mmu_save_mem_map();
934 core_mmu_init_phys_mem();
935 boot_mem_foreach_padding(add_padding_to_pool, NULL);
936 va = boot_mem_release_unused();
937 if (!IS_ENABLED(CFG_WITH_PAGER)) {
938 /*
939 * We must update boot_cached_mem_end to reflect the memory
940 * just unmapped by boot_mem_release_unused().
941 */
942 assert(va && va <= boot_cached_mem_end);
943 boot_cached_mem_end = va;
944 }
945
946 if (IS_ENABLED(CFG_DYN_CONFIG)) {
947 /*
948 * This is needed to enable virt_page_alloc() now that
949 * boot_mem_alloc() can't be used any longer.
950 */
951 if (IS_ENABLED(CFG_NS_VIRTUALIZATION))
952 nex_page_alloc_init();
953 else
954 page_alloc_init();
955 }
956
957 if (IS_ENABLED(CFG_WITH_PAGER)) {
958 /*
959 * Pager: init_runtime() calls thread_kernel_enable_vfp()
960 * so we must set a current thread right now to avoid a
961 * chicken-and-egg problem (thread_init_boot_thread() sets
962 * the current thread but needs things set by
963 * init_runtime()).
964 */
965 thread_get_core_local()->curr_thread = 0;
966 init_pager_runtime(pageable_part);
967 }
968
969 /* Initialize canaries around the stacks */
970 thread_init_canaries();
971 thread_init_per_cpu();
972}
973
974static bool cpu_nmfi_enabled(void)
975{
976#if defined(ARM32)
977 return read_sctlr() & SCTLR_NMFI;
978#else
979 /* Note: ARM64 does not feature non-maskable FIQ support. */
980 return false;
981#endif
982}
983
984/*
985 * Note: this function is weak just to make it possible to exclude it from
986 * the unpaged area.
987 */
988void __weak boot_init_primary_late(unsigned long fdt __unused,
989 unsigned long manifest __unused)
990{
991 size_t fdt_size = CFG_DTB_MAX_SIZE;
992
993 if (IS_ENABLED(CFG_TRANSFER_LIST) && mapped_tl) {
02. pageable image를 검증하고 pager runtime을 구성
core/arch/arm/kernel/boot.c 464-646줄이다.
464#ifdef CFG_CORE_ASLR
465 unsigned int reloc_offs = (vaddr_t)__pageable_start - VCORE_START_VA;
466 const struct boot_embdata *embdata = (const void *)__init_end;
467 const void *reloc = __init_end + embdata->reloc_offset;
468
469 return fobj_ro_reloc_paged_alloc(num_pages, hashes, reloc_offs,
470 reloc, embdata->reloc_len, store);
471#else
472 return fobj_ro_paged_alloc(num_pages, hashes, store);
473#endif
474}
475
476static void init_pager_runtime(unsigned long pageable_part)
477{
478 size_t n;
479 size_t init_size = (size_t)(__init_end - __init_start);
480 size_t pageable_start = (size_t)__pageable_start;
481 size_t pageable_end = (size_t)__pageable_end;
482 size_t pageable_size = pageable_end - pageable_start;
483 vaddr_t tzsram_end = TZSRAM_BASE + TZSRAM_SIZE - TEE_LOAD_ADDR +
484 VCORE_START_VA;
485 size_t hash_size = (pageable_size / SMALL_PAGE_SIZE) *
486 TEE_SHA256_HASH_SIZE;
487 const struct boot_embdata *embdata = (const void *)__init_end;
488 const void *tmp_hashes = NULL;
489 tee_mm_entry_t *mm = NULL;
490 struct fobj *fobj = NULL;
491 uint8_t *paged_store = NULL;
492 uint8_t *hashes = NULL;
493
494 assert(pageable_size % SMALL_PAGE_SIZE == 0);
495 assert(embdata->total_len >= embdata->hashes_offset +
496 embdata->hashes_len);
497 assert(hash_size == embdata->hashes_len);
498
499 tmp_hashes = __init_end + embdata->hashes_offset;
500
501 /*
502 * This needs to be initialized early to support address lookup
503 * in MEM_AREA_TEE_RAM
504 */
505 tee_pager_early_init();
506
507 hashes = malloc(hash_size);
508 IMSG_RAW("\n");
509 IMSG("Pager is enabled. Hashes: %zu bytes", hash_size);
510 assert(hashes);
511 asan_memcpy_unchecked(hashes, tmp_hashes, hash_size);
512
513 /*
514 * The pager is about the be enabled below, eventual temporary boot
515 * memory allocation must be removed now.
516 */
517 boot_mem_release_tmp_alloc();
518
519 carve_out_asan_mem();
520
521 mm = nex_phys_mem_ta_alloc(pageable_size);
522 assert(mm);
523 paged_store = phys_to_virt(tee_mm_get_smem(mm),
524 MEM_AREA_SEC_RAM_OVERALL, pageable_size);
525 /*
526 * Load pageable part in the dedicated allocated area:
527 * - Move pageable non-init part into pageable area. Note bootloader
528 * may have loaded it anywhere in TA RAM hence use memmove().
529 * - Copy pageable init part from current location into pageable area.
530 */
531 memmove(paged_store + init_size,
532 phys_to_virt(pageable_part,
533 core_mmu_get_type_by_pa(pageable_part),
534 __pageable_part_end - __pageable_part_start),
535 __pageable_part_end - __pageable_part_start);
536 asan_memcpy_unchecked(paged_store, __init_start, init_size);
537 /*
538 * Undo eventual relocation for the init part so the hash checks
539 * can pass.
540 */
541 undo_init_relocation(paged_store);
542
543 /* Check that hashes of what's in pageable area is OK */
544 DMSG("Checking hashes of pageable area");
545 for (n = 0; (n * SMALL_PAGE_SIZE) < pageable_size; n++) {
546 const uint8_t *hash = hashes + n * TEE_SHA256_HASH_SIZE;
547 const uint8_t *page = paged_store + n * SMALL_PAGE_SIZE;
548 TEE_Result res;
549
550 DMSG("hash pg_idx %zu hash %p page %p", n, hash, page);
551 res = hash_sha256_check(hash, page, SMALL_PAGE_SIZE);
552 if (res != TEE_SUCCESS) {
553 EMSG("Hash failed for page %zu at %p: res 0x%x",
554 n, (void *)page, res);
555 panic();
556 }
557 }
558
559 /*
560 * Assert prepaged init sections are page aligned so that nothing
561 * trails uninited at the end of the premapped init area.
562 */
563 assert(!(init_size & SMALL_PAGE_MASK));
564
565 /*
566 * Initialize the virtual memory pool used for main_mmu_l2_ttb which
567 * is supplied to tee_pager_init() below.
568 */
569 init_virt_pool(&core_virt_mem_pool);
570
571 /*
572 * Assign alias area for pager end of the small page block the rest
573 * of the binary is loaded into. We're taking more than needed, but
574 * we're guaranteed to not need more than the physical amount of
575 * TZSRAM.
576 */
577 mm = tee_mm_alloc2(&core_virt_mem_pool,
578 (vaddr_t)core_virt_mem_pool.lo +
579 core_virt_mem_pool.size - TZSRAM_SIZE,
580 TZSRAM_SIZE);
581 assert(mm);
582 tee_pager_set_alias_area(mm);
583
584 /*
585 * Claim virtual memory which isn't paged.
586 * Linear memory (flat map core memory) ends there.
587 */
588 mm = tee_mm_alloc2(&core_virt_mem_pool, VCORE_UNPG_RX_PA,
589 (vaddr_t)(__pageable_start - VCORE_UNPG_RX_PA));
590 assert(mm);
591
592 /*
593 * Allocate virtual memory for the pageable area and let the pager
594 * take charge of all the pages already assigned to that memory.
595 */
596 mm = tee_mm_alloc2(&core_virt_mem_pool, (vaddr_t)__pageable_start,
597 pageable_size);
598 assert(mm);
599 fobj = ro_paged_alloc(mm, hashes, paged_store);
600 assert(fobj);
601 tee_pager_add_core_region(tee_mm_get_smem(mm), PAGED_REGION_TYPE_RO,
602 fobj);
603 fobj_put(fobj);
604
605 tee_pager_add_pages(pageable_start, init_size / SMALL_PAGE_SIZE, false);
606 tee_pager_add_pages(pageable_start + init_size,
607 (pageable_size - init_size) / SMALL_PAGE_SIZE,
608 true);
609 if (pageable_end < tzsram_end)
610 tee_pager_add_pages(pageable_end, (tzsram_end - pageable_end) /
611 SMALL_PAGE_SIZE, true);
612
613 /*
614 * There may be physical pages in TZSRAM before the core load address.
615 * These pages can be added to the physical pages pool of the pager.
616 * This setup may happen when a the secure bootloader runs in TZRAM
617 * and its memory can be reused by OP-TEE once boot stages complete.
618 */
619 tee_pager_add_pages(core_virt_mem_pool.lo,
620 (VCORE_UNPG_RX_PA - core_virt_mem_pool.lo) /
621 SMALL_PAGE_SIZE,
622 true);
623
624 print_pager_pool_size();
625}
626#else /*!CFG_WITH_PAGER*/
627static void init_pager_runtime(unsigned long pageable_part __unused)
628{
629}
630#endif
631
632#if defined(CFG_DT)
633static int add_optee_dt_node(struct dt_descriptor *dt)
634{
635 int offs;
636 int ret;
637
638 if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) {
639 DMSG("OP-TEE Device Tree node already exists!");
640 return 0;
641 }
642
643 offs = fdt_path_offset(dt->blob, "/firmware");
644 if (offs < 0) {
645 offs = add_dt_path_subnode(dt, "/", "firmware");
646 if (offs < 0)
DETAILS
_start의 세 가지 주소 판단
이미지 내부 복사
pager 빌드는 init code·rodata와 embedded metadata를 __init_start로, 비-pager는 embedded metadata를 free 영역 끝 근처로 옮긴다. 둘 다 overlap을 고려해 뒤에서 앞으로 복사한다.
물리 relocation과 가상 ASLR
CFG_CORE_PHYS_RELOCATABLE이면 실제 _start PA와 TEE_LOAD_ADDR 차이로 초기 relocation을 적용한다. CFG_CORE_ASLR이면 MMU map 준비 후 가상 offset relocation을 다시 적용한다.
서로 다른 vector
초기의 reset_vect_table은 VBAR_EL1에 설정되고, enable_mmu()에서 offset이 조정된다. 일반 OPTEED 경로의 마지막 SMC는 thread_vector_table을 BL31에 준다. FF-A 설정에서는 thread_ffa_msg_wait로 간다.
IMPLEMENTATION NOTES
초기화 단계를 읽을 때 구분할 것
스택 이전과 이후
set_sp는 core position을 구해 지원되지 않는 CPU를 park하고 SP_EL0 임시 stack, SP_EL1 core-local을 잡는다. CFG_DYN_CONFIG면 embedded data 근처의 임시 할당을 사용한다. BSS와 이미지 복사는 그보다 앞이다.
boot argument 저장
x19–x22는 초기 register 사용 중 인자를 보존한다. stack과 console이 준비된 뒤 x0–x3로 되돌려 boot_save_args()를 호출한다. boot_arg_pageable_part·FDT·TL·manifest는 설정별로 결정된다.
MMU 이후 primary C
enable_mmu()는 identity map 안에서 실행하면서 translation과 SP/vector offset을 바꾼다. 이어 boot_init_primary_early()는 pager가 켜졌을 때 pageable 주소를 init_primary()에 전달한다. late/runtime/final과 secondary handler는 별개다.
stack·args · MMU · primary early.
DEEP ANALYSIS
조건부 분기와 관찰할 값
| 설정 | 이 버전의 변화 | 확인할 값 |
|---|---|---|
CFG_WITH_PAGER | init+metadata 복사; early init에서 pageable 주소 소비 | copy src/dst, boot_arg_pageable_part 또는 TL entry |
CFG_CORE_PHYS_RELOCATABLE | pre-MMU 실제 load PA offset 적용 | core_mmu_tee_load_pa, TEE_LOAD_ADDR |
CFG_CORE_ASLR | map offset relocation 및 stack/end 보정 | boot_mmu_config.map_offset, PC VA |
CFG_DYN_CONFIG | 임시 SP/core-local 후 새 per-CPU 구조로 전환 | SP_EL0, SP_EL1, core-local pointer |
CFG_NS_VIRTUALIZATION | 초기 nex BSS zero; boot thread/partition init 방식 변경 | __nex_bss_*, curr_thread |
CFG_CORE_FFA | 마지막 thread_ffa_msg_wait | 일반 OPTEED ENTRY_DONE SMC와 구분 |
DEVICE TREE LIFETIME
DTB 주소를 받았다고 같은 자료를 계속 공유하는 것은 아닙니다
OP-TEE 4.10.0의 DTB는 위치와 소비자가 다릅니다. Normal world가 직접 읽을 외부 DTB는 non-secure 메모리에 있어야 합니다. 내장 DTB는 OP-TEE core의 읽기 전용 자료이고, 외부 DTB 주소는 초기 부트 인자입니다. CFG_DT는 파싱 지원, CFG_EXTERNAL_DT는 외부 DTB 사용·수정 경로를 구분합니다. get_dt()는 내장 DTB → 외부 DTB → manifest DTB 순으로 찾지만, 이것이 세 자료를 합치거나 신뢰 수준을 같게 만드는 것은 아닙니다.
| 자료 | 선택과 위치 | 수명·다음 소비자 |
|---|---|---|
| 내장 DTB | CFG_EMBED_DTB와 CFG_EMBED_DTB_SOURCE_FILE로 core에 포함 | 읽기 전용 section. 외부 DTB의 임시 매핑과 별개 |
| 일반 외부 DTB | CFG_EXTERNAL_DT; 부트 인자 또는 해당 경로의 CFG_DT_ADDR | OP-TEE 초기화 중 읽기·수정. 후속 부트 단계의 사용과 runtime 재사용은 별도 계약 |
| 외부 overlay | CFG_EXTERNAL_DTB_OVERLAY 또는 CFG_GENERATE_DTB_OVERLAY | 후속 부트 단계가 다른 DTB에 병합. 두 옵션은 동시에 켤 수 없음 |
| secure 매핑의 외부 DTB | CFG_MAP_EXT_DT_SECURE; 기본값은 n | 고정 코드에 secure 매핑·선택 분기가 있음. Linux가 직접 읽는 공유 DTB라는 뜻은 아님 |
boot_init_primary_late()는 외부 DTB를 준비한 뒤 메모리를 조사하고 update_external_dt()를 호출합니다. 실제 수정은 설정에 따릅니다. OP-TEE 노드 추가는 CFG_CORE_FFA가 꺼진 경로이고, 정적 공유 메모리 예약은 CFG_CORE_RESERVED_SHM 조건입니다. PSCI 노드를 추가하는 구현은 CFG_PSCI_ARM32 분기입니다. 이 AArch64 진입 경로에서 세 종류의 노드가 언제나 모두 추가된다고 읽으면 안 됩니다.
release_external_dt()는 boot-final 단계에 등록됩니다. DTB가 MEM_AREA_EXT_DT에 매핑된 경우 fdt_pack() 후 임시 매핑을 제거하고 포인터를 NULL로 바꿉니다. 다른 메모리 영역에 이미 매핑된 경우에는 이 정리를 건너뜁니다. 매핑 해제는 DTB 원본 삭제가 아닙니다. 반대로 주소를 한 번 받았다는 이유로 OP-TEE runtime에서 그 버퍼가 계속 유효하다고 가정해서도 안 됩니다.
Overlay 모드에서 CFG_EXTERNAL_DTB_OVERLAY는 기존의 유효한 트리에 fragment를 추가할 수 있고, 유효한 트리가 없으면 빈 트리를 만듭니다. CFG_GENERATE_DTB_OVERLAY는 새 트리를 만드는 별도 선택입니다. 다음 단계의 fdt_overlay_apply() 같은 병합과 OP-TEE의 overlay 생성은 다른 작업입니다. DTB 주소도 모든 설정에서 x2로 고정되지 않습니다. 앞의 설정별 인자 표처럼 Transfer List와 FF-A 경로를 먼저 구분합니다.
4.10.0 문서에는 secure 외부 DTB를 미구현으로 설명하는 문장이 남아 있지만, 같은 버전의 mk/config.mk에는 CFG_MAP_EXT_DT_SECURE가 있고, get_secure_dt()는 이 조건에서 외부 DTB를 선택합니다. core_mmu.c도 해당 외부 영역에 secure·non-cacheable 속성을 부여하는 분기를 갖습니다. 따라서 문서의 미구현 서술을 버전 전체의 결론으로 옮기지 않습니다. 이 코드 경로의 존재는 특정 보드의 메모리 보호 설정이나 실제 부팅을 검증했다는 뜻이 아닙니다.
English note
In OP-TEE 4.10.0, embedded, external and overlay device trees have different lifetimes. External DTB mapping cleanup is conditional and does not delete the physical blob. Overlay creation and downstream merging are separate. Fixed source contains CFG_MAP_EXT_DT_SECURE handling despite the documentation's unimplemented wording; this is source analysis, not a platform validation.
OP-TEE 4.10.0 Device Tree 문서 · DTB 선택 · overlay와 임시 매핑 수명 · 빌드 조건 · secure 외부 매핑 속성 · 조건부 노드 수정
OBJECT LIFETIME
entry에서 넘겨 받는 상태의 수명
| 대상 | 유효한 구간 | 소비·변경 |
|---|---|---|
| x19–x22 | 첫 명령부터 boot_save_args()까지 | 초기 x0–x3의 임시 사본 |
boot_arg_* | boot_save_args() 뒤 primary init | pager 주소·FDT·TL/manifest를 설정별 기록 |
boot_embdata_ptr | non-pager 복사 뒤 | embedded data 목적지 |
boot_mmu_config | MMU map 생성 뒤 | TTBR/TCR/MAIR와 map offset |
| SP_EL0 / SP_EL1 | core-local·thread 초기화 중 | 임시 stack과 core-local; 동적 설정에서 전환 |
| reset / thread vector | 초기 예외 / 이후 OPTEED runtime | VBAR 초기값과 BL31에 반환한 주소 |
FAILURE PATH
멈춘 경계에서 먼저 확인할 것
| 증상 | 가능한 불일치 | 소스 위치 |
|---|---|---|
| 초기 exception loop | EL/SPSR, VBAR 또는 stack 전 fault | entry 165–184, reset vector |
| 복사/BSS 직후 손상 | pager별 layout, metadata 길이, linker bounds | entry 186–266 |
| 지원하지 않는 CPU 정지 | __get_core_pos() ≥ CFG_TEE_CORE_NB_CORE | set_sp → unhandled_cpu |
| MMU 직후 abort | TTBR/TCR/MAIR, map offset, SP/vector 보정 | enable_mmu:705–775 |
| pager init 주소 오류 | 일반/FF-A/TL x0 의미와 pager 설정 | boot_save_args:1365–1473 |
이 초기 경로의 문제는 잘못된 entry 상태·메모리 배치·주소 변환부터 조사한다.
EVIDENCE
소스 밖에서 확인할 자료
기존 OP-TEE 산출물을 읽는 예시다. 이 리뷰는 새 빌드나 보드 실행을 하지 않았다.
| 목적 | 읽기 예시 | 대조할 값 |
|---|---|---|
| 링커 section | ${CROSS_COMPILE64}readelf -W -lS out/arm/core/tee.elf | init, BSS, identity map, load PA |
| 진입 symbol | ${CROSS_COMPILE64}nm -n out/arm/core/tee.elf | grep -E ' (_start|enable_mmu|cpu_on_handler)$' | C 선언문 아닌 실제 symbol 이름 |
| 실행 명령 | ${CROSS_COMPILE64}objdump -dr out/arm/core/tee.elf | relocation, boot_save_args, enable_mmu |
| 보드 로그 | Secure console Primary CPU 로그와 BL31 OPTEED 로그 | ENTRY_DONE 또는 FF-A wait |
LAB
설정별로 따로 재현할 실험
- 01boot argument 해석
entry x0–x3와
boot_save_args()뒤 pageable 주소, FDT, TL/manifest를 같은 빌드 설정에서 비교한다. - 02MMU 전후 주소
_startload PA,boot_mmu_config.map_offset,enable_mmu()전후 PC/stack/vector를 기록한다. - 03pager와 non-pager 분리
각 이미지의 init/embedded-data 배치와 복사 목적지를 대조한다. 둘이 동일한 copy loop를 수행한다고 가정하지 않는다.
PRIMARY REFERENCES