⚠️ The code is based on: https://gitlab.com/riseproject/riscv-optee/opensbi/-/tree/dev-optee-mpxy
Commit ID:
7d4c90953afe3bd86f9e2501bd4c2501e8db1898
-
mpxy_opteed_init()Match compatible string:
"riscv,sbi-mpxy-opteed".Allocate channel.
opteed_domain_setup()- Setup domain for OP-TEE dispatcher by looking up the domain specified by
opensbi-domain-instancephandle in DTS. - Assign the domain name to
opteed_domain_name.
- Setup domain for OP-TEE dispatcher by looking up the domain specified by
Get channel ID from DTS property:
riscv,sbi-mpxy-channel-id.Initialize channel:
Register channel:
sbi_mpxy_register_channel()Assign
tdomain(Trusted domain).- Look up the domain with the name specified in
opteed_domain_name.
- Look up the domain with the name specified in
Assign
udomain(Untrusted domain).- Look up the domain with the name
"untrusted-domain".
- Look up the domain with the name
-
mpxy_opteed_send_message()-
If message ID is
OPTEED_MSG_COMMUNICATE(0x1) (Called fromudomain):- If
tdomain’s shared memory is NOT valid, returnSBI_EINVAL. - Otherwise:
- Direction:
udomain-> OpenSBI - Copy the source data from
udomaintotdomain’s shared memory.
- Direction:
- Call
sbi_ecall_tee_domain_enter()to switch totdomain.- Direction:
udomain->tdomain. - The entry point to be switched to is defined by the function type:
- Function type is defined by the first
unsigned longinudomain’s shared memory:((ulong *)shmem_base)[0]. - If function type is:
ABI_ENTRY_TYPE_FAST(i.e.ARM_SMCCC_FAST_CALL)- Entry point ⇒
entry_vector_table->fast_abi_entryentry_vector_table⇒thread_vector_tableis defined in OP-TEE. Therefore,entry_vector_table->fast_abi_entry⇒vector_fast_abi_entry()
- Entry point ⇒
- Otherwise:
- Entry point ⇒
entry_vector_table->yield_abi_entryentry_vector_table⇒thread_vector_tableis defined in OP-TEE. Therefore,entry_vector_table->yield_abi_entry⇒vector_std_abi_entry()
- Entry point ⇒
- Function type is defined by the first
- Direction:
- If
- If message ID is
OPTEED_MSG_COMPLETE(0x2) (Called fromtdomain):- If
udomain’s shared memory is NOT valid:- Direction:
tdomain-> OpenSBI. - If
((ulong *)msgbuf)[0](fromtdomain’s shared memory, i.e.$a0) isTEEABI_OPTEED_RETURN_CALL_DONE(0xbe000000):- Register OP-TEE entry table:
- Set
entry_vector_tableto((ulong *)msgbuf)[1](i.e.$a1), i.e.thread_vector_tabledefined in OP-TEE.
- Set
- Register OP-TEE entry table:
- Direction:
- Otherwise, copy the source data from
tdomaintoudomain’s shared memory.- Direction:
tdomain->udomain. - Only
$a1~$a4(fromtdomain’s shared memory) are copied toudomain’s shared memory,$a0is skipped.
- Direction:
- Call
sbi_ecall_tee_domain_exit()to exit from the currenttdomain.- The domain to switch to:
- Switch to the previous domain
- Switch to the use-define next domain.
- Fallback to the root domain.
- The domain to switch to:
- If
- P.S. Domain switching only involves domain contexts save and restore. The mode is switched by
mretin the trap handler.
-
If message ID is
-
sbi_mpxy_register_channel()- Initialize channel’s attributes:
mpxy_std_attrs_init()- Check whether MSI, SSE and Events State capabilities are available in the domain.
- Add the allocated channel to
mpxy_channel_listlist.
- Initialize channel’s attributes:
-
sbi_mpxy_set_shmem()- Validate passed in shared memory address and size.
- Set current hart’s
sbi_domainshared memory address and size.
-
sbi_mpxy_send_message()- Call
channel->send_message().
- Call
-
sbi_ecall_tee_domain_enter()- The domain is
tdomain. sbi_domain_context_set_mepc()- Set
mepcto the entry point.
- Set
sbi_domain_context_enter()- Switch to
tdomain.
- Switch to
- The domain is
Example DTS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23chosen { opensbi-domains { ..... trusted-domain { compatible = "opensbi,domain,instance"; regions = <0x04 0x3f>; possible-harts = <0x03 0x01>; next-addr = <0x00 0xf1000000>; next-mode = <0x01>; phandle = <0x02>; }; ..... }; }; sbi-mpxy-opteed { opensbi-domain-instance = <0x02>; riscv,sbi-mpxy-channel-id = <0x02>; compatible = "riscv,sbi-mpxy-opteed"; };