This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to do from Secure(EL3) to Non-secure Exception level transition in ARMV8-A ?

Hi all i trying do transition from EL3 to EL2 exception ,but after ERET of EL3 mode it change the mode to EL2 , but as soon as when it will execute first instruction of EL2 , then It goes to Exception ...

This is happen for every secure to non secure transition in Armv8-A .

Please Guide if you know why this happen?

Thank you in advance.

Regards

Sanjay Kumar

Parents
  • .global test_el3_to_el2_asm
    .p2align 2
    .type test_el3_to_el2_asm,%function
    test_el3_to_el2_asm:
    
    #store return address into C world - this will be retrieved from SMC handler
    adr x0,return_addr
    str x30,[x0]
    
    #set sp_el1 to sp_el3 in order for the code in C test function to work
    ldr x0, =__stack_end__
    mov x1, #1000
    sub x0, x0, x1
    bic x0, x0, #0xf
    msr sp_el1, x0
    mov x1, #1000
    sub x0, x0, x1
    bic x0, x0, #0xf
    msr sp_el0, x0
    mov x1, #1000
    sub x0, x0, x1
    bic x0, x0, #7
    msr sp_el2, x0
    
    
    //Initialize SCTLR_EL2 and HCR_EL2 to save values before entering EL2.
    MSR SCTLR_EL2, XZR
    MSR HCR_EL2, XZR
    MRS X0, HCR_EL2
    ORR X0, X0, #(1<<19)
    ORR X0, X0, #(1<<31)
    MSR HCR_EL2, X0
    
    
    //clear CPTR_EL3 and CPTR_EL2 to prevent trapping of accesses to CPACR at EL1
    bl clear_cptr_el3_el2_asm
    
    // Determine the EL2 Execution state.
    
    MRS X0, SCR_EL3
    ORR X0, X0, #(1<<10) // RW EL2 Execution state is AArch64.
    ORR X0, X0, #(1<<0) //NS=1 El1 Non Secure state
    BIC X0, X0, #(1<<3)
    MSR SCR_EL3, x0
    isb
    
    MOV X0, #0b01001 // DAIF=0000
    MSR SPSR_EL3, X0 
    isb
    
    // Determine EL2 entry.
    ADR X0, el2_entry // el2_entry points to the first instruction of
    MSR ELR_EL3, X0 // EL2 code.
    ERET
    
    el2_entry:
    nop
    add x0, x0, #1
    smc 0x0
    
    

    Hi @ , Thanks for reply... above is my code for Transition from EL3 to EL2 and again return to EL3 but the problem is while Executing first instruction in EL2 mode then it goes to again EL3 mode and goes to unexpected handler.

    Please check above code suggest me .

    Thank You.

    Regards, 

    Sanjay Kumar

Reply
  • .global test_el3_to_el2_asm
    .p2align 2
    .type test_el3_to_el2_asm,%function
    test_el3_to_el2_asm:
    
    #store return address into C world - this will be retrieved from SMC handler
    adr x0,return_addr
    str x30,[x0]
    
    #set sp_el1 to sp_el3 in order for the code in C test function to work
    ldr x0, =__stack_end__
    mov x1, #1000
    sub x0, x0, x1
    bic x0, x0, #0xf
    msr sp_el1, x0
    mov x1, #1000
    sub x0, x0, x1
    bic x0, x0, #0xf
    msr sp_el0, x0
    mov x1, #1000
    sub x0, x0, x1
    bic x0, x0, #7
    msr sp_el2, x0
    
    
    //Initialize SCTLR_EL2 and HCR_EL2 to save values before entering EL2.
    MSR SCTLR_EL2, XZR
    MSR HCR_EL2, XZR
    MRS X0, HCR_EL2
    ORR X0, X0, #(1<<19)
    ORR X0, X0, #(1<<31)
    MSR HCR_EL2, X0
    
    
    //clear CPTR_EL3 and CPTR_EL2 to prevent trapping of accesses to CPACR at EL1
    bl clear_cptr_el3_el2_asm
    
    // Determine the EL2 Execution state.
    
    MRS X0, SCR_EL3
    ORR X0, X0, #(1<<10) // RW EL2 Execution state is AArch64.
    ORR X0, X0, #(1<<0) //NS=1 El1 Non Secure state
    BIC X0, X0, #(1<<3)
    MSR SCR_EL3, x0
    isb
    
    MOV X0, #0b01001 // DAIF=0000
    MSR SPSR_EL3, X0 
    isb
    
    // Determine EL2 entry.
    ADR X0, el2_entry // el2_entry points to the first instruction of
    MSR ELR_EL3, X0 // EL2 code.
    ERET
    
    el2_entry:
    nop
    add x0, x0, #1
    smc 0x0
    
    

    Hi @ , Thanks for reply... above is my code for Transition from EL3 to EL2 and again return to EL3 but the problem is while Executing first instruction in EL2 mode then it goes to again EL3 mode and goes to unexpected handler.

    Please check above code suggest me .

    Thank You.

    Regards, 

    Sanjay Kumar

Children