[커널20차] 11주차

2023.07.15 22:28

이경재 조회 수:86

2023.07.08 (11주차) - 약 23명 참여

 

head.S

SYM_CODE_START(primary_entry)의 record_mmu_state 분석

 

/*
 * IAMROOT20 20230715
 * - 'SYM_CODE_START'는 인자로 전달된 심볼을 정의 및 align. (링커에 정보 전달)
 * - 참고: linux-stable/Documentation/core-api/asm-annotations.rst
 *
 * - Cache ON - MMU On : no need for cache invalidation
 *   Cache ON - MMU OFF: need to invalidate cache
 *   Cache Off - MMU ON/OFF : Cache hasn't been used, no need for invalidation
 */
SYM_CODE_START(primary_entry)
    bl  record_mmu_state
    /*  
     * IAMROOT20 20230715
     * 'record_mmu_state'에서 return 시 x19의 값이
     *      - 0: cache maintenance 필요
     *      - 1: cache maintenance 필요X
     */
    bl  preserve_boot_args
    bl  create_idmap

    /*
     * If we entered with the MMU and caches on, clean the ID mapped part
     * of the primary boot code to the PoC so we can safely execute it with
     * the MMU off.
     */
    cbz x19, 0f
    adrp    x0, __idmap_text_start
    adr_l   x1, __idmap_text_end
    adr_l   x2, dcache_clean_poc
    blr x2
0:  mov x0, x19
    bl  init_kernel_el          // w0=cpu_boot_mode
    mov x20, x0

    /*
     * The following calls CPU setup code, see arch/arm64/mm/proc.S for
     * details.
     * On return, the CPU will be ready for the MMU to be turned on and
     * the TCR will have been set.
     */
#if VA_BITS > 48
    mrs_s   x0, SYS_ID_AA64MMFR2_EL1
    tst x0, #0xf << ID_AA64MMFR2_EL1_VARange_SHIFT
    mov x0, #VA_BITS
    mov x25, #VA_BITS_MIN
    csel    x25, x25, x0, eq
    mov x0, x25
#endif
    bl  __cpu_setup         // initialise processor
    b   __primary_switch
SYM_CODE_END(primary_entry)

    __INIT
SYM_CODE_START_LOCAL(record_mmu_state)
    mrs x19, CurrentEL
    cmp x19, #CurrentEL_EL2
    mrs x19, sctlr_el1
    b.ne    0f      /* IAMROOT20 20230715 여기 0은 주소가 아닌 레이블 */
    mrs x19, sctlr_el2
0:
/*
 * IAMROOT20 20230715
 * - Endianness 디폴트 세팅은 Little endian. 잘못 세팅되어있다면 예외처리를
 * 위한 레이블로 점프.
 * - CPU_LE 매크로 인자로 사용된 '1f' 는 전방(forward)의 레이블 '1'이라는
 * 의미.
 */
CPU_LE( tbnz    x19, #SCTLR_ELx_EE_SHIFT, 1f    )
CPU_BE( tbz x19, #SCTLR_ELx_EE_SHIFT, 1f    )
    /*
     * IAMROOT20 20230715
     * Endianness 세팅에 이상이 없다면 이어서 아래 코드 실행.
     */
    tst x19, #SCTLR_ELx_C       // Z := (C == 0)
    and x19, x19, #SCTLR_ELx_M      // isolate M bit
    csel    x19, xzr, x19, eq       // clear x19 if Z
    ret

    /*
     * Set the correct endianness early so all memory accesses issued
     * before init_kernel_el() occur in the correct byte order. Note that
     * this means the MMU must be disabled, or the active ID map will end
     * up getting interpreted with the wrong byte order.
     */

    /*
     * IAMROOT20 20230715
     * - 아래 코드는 endianness가 잘못 설정되어있을 시 수행하는 예외처리.
     *      - x19의 'SCTLR_ELx_EE' 비트 반전
     *      - x19의 'SCTLR_ELx_M' 비트 clear
     *      - 수정된 x19의 내용을 sctlr_el1 (또는 sctlr_el2)에 업데이트
     * - 'pre_disable_mmu_workaround' 는 특정 칩을 위한 예외처리 매크로.
     */
1:  eor x19, x19, #SCTLR_ELx_EE

    bic x19, x19, #SCTLR_ELx_M
    b.ne    2f
    pre_disable_mmu_workaround
    msr sctlr_el2, x19
    b   3f
2:  pre_disable_mmu_workaround
    msr sctlr_el1, x19
3:  isb
    mov x19, xzr
    ret
SYM_CODE_END(record_mmu_state)

 

 

디버깅을 통해 배우는 리눅스 커널의 구조와 원리

3.1.2 디버깅과 코드 학습 능력

Q. ftrace를 통한 trace log 추출 순서

A: tracing off -> 필요한 부분 추출을 위한 세팅 (필터 등등) -> tracing on -> 관찰하고자 하는 커널 동작 실행 -> trace off 및 로그 추출 -> 분석

 

3.2 printk() 함수

 

Q: asmlinkage ?

A: asmlinkage는 어셈블리와 링크가 가능하다는 뜻 즉, 어셈블리어로 짜여진 코드에서 이 함수를 호출 할 수 있다는 뜻

https://rootfriend.tistory.com/entry/asmlinkage-attribute


 

Q: 아래 그림의 콜 스택에서 마지막 hex 값의 의미는?

Ghs81m_8DqzG8BKdHpFeZ02DUZpJpQVF2gkal3L6

A: 해당 함수의 epilogue 끝의 offset 을 의미 (A/B -> A out of B)

 

번호 제목 글쓴이 날짜 조회 수
공지 [공지] 스터디 정리 노트 공간입니다. woos 2016.05.14 627
228 [커널 20차] 20주차 [2] 이민찬 2023.09.17 182
227 [커널 20차] 19주차 이민찬 2023.09.10 103
226 [커널 20차] 18주차 이민찬 2023.09.03 85
225 [커널 20차] 17주차 이민찬 2023.08.27 129
224 [커널 20차] 16주차 이민찬 2023.08.20 132
223 [커널 19차] 64 주차 Min 2023.08.19 91
222 [커널 20차] 15주차 이민찬 2023.08.13 126
221 [커널 19차] 63 주차 Min 2023.08.12 60
220 [커널 19차] 62 주차 Min 2023.08.05 79
219 [커널 20차] 13주차 이민찬 2023.07.30 107
218 [커널 19차] 61 주차 Min 2023.07.30 45
217 [커널 20차] 12주차 이민찬 2023.07.22 87
216 [커널 19차] 59 ~ 60 주차 Min 2023.07.22 37
215 [커널 18차] 113주차 kkr 2023.07.22 78
» [커널20차] 11주차 이경재 2023.07.15 86
213 [커널20차] 10주차 이경재 2023.07.09 90
212 [커널20차] 9주차 이경재 2023.07.02 85
211 [커널 19차] 58 주차 Min 2023.07.01 41
210 [커널 19차] 56 ~ 57 주차 Min 2023.06.25 40
209 [커널 18차] 109주차 kkr 2023.06.24 33
XE Login