⚠️ The code is based on: https://gitlab.com/riseproject/riscv-optee/linux/-/tree/dev-optee-mpxy

Commit ID: df5dc01764820f113312f7a39f221b49985bbd7a

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// arch/riscv/kernel/mpxy-sbi.c

struct sbi_mpxy {
	void *shmem;
	phys_addr_t shmem_phys_addr;
	bool active;
};

// Define per-cpu varible: sbi_mpxy.
DEFINE_PER_CPU(struct sbi_mpxy, sbi_mpxy);


  • sbi_mpxy_init()
    • Check SBI version and if MPXY extension is supported by OpenSBI.

    • Setup CPUHP notifier to setup shared memory on all CPUs.

      1
      2
      3
      4
      5
      6
      
      // arch/riscv/kernel/mpxy-sbi.c
      
      cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
      			    "riscv/mpxy-sbi:cpu-shmem-init",
      			    __sbi_mpxy_setup_shmem,
      			    sbi_mpxy_exit);
      
  • __sbi_mpxy_setup_shmem()
    • Allocates 4KB shared memory (4KB aligned).
    • Call sbi_mpxy_set_shmem SBI call to set up MPXY shared memory for the current core. This will invoke OpenSBI’s sbi_mpxy_set_shmem() to save the shared memory address and size into current hart domain’s mpxy_state (Untrusted domain).
  • sbi_mpxy_tee_probe()
    • Check SBI version and whether MPXY extension is supported by OpenSBI.
    • Get channel ID from DTS property: riscv,sbi-mpxy-channel-id.
    • Read MPXY protocol ID attribute (SBI_MPXY_ATTR_MSG_PROT_ID (0x0)) from channel with sbi_mpxy_read_attrs() .
    • Check if the returned MPXY protocol ID equals SBI_MPXY_MSGPROTO_TEE_ID (0x2). Throw an error if not.
  • sbi_mpxy_read_attrs()
    • Disable preemption (get_cpu()).
      • Shared memory is per-core. We only need to disable the preemption to prevent the shared memory of current CPU from being corrupted by the other tasks.
    • Issue SBI call: SBI_EXT_MPXY_READ_ATTRS to read the attribute.
    • If successful, copy the attribute from the shared memory to the buffer.
    • Enable preemption (put_cpu()).
  • sbi_mpxy_write_attrs()
    • Disable preemption (get_cpu()).
      • Shared memory is per-core. We only need to disable the preemption to prevent the shared memory of current CPU from being corrupted by the other tasks.
    • Copy the attribute from the buffer to the shared memory.
    • Issue SBI call: SBI_EXT_MPXY_WRITE_ATTRS to write the attribute.
    • Enable preemption (put_cpu()).
  • sbi_mpxy_send_message_withresp()
    • Disable preemption (get_cpu()).
      • Shared memory is per-core. We only need to disable the preemption to prevent the shared memory of current CPU from being corrupted by the other tasks.
    • Issue SBI call: SBI_EXT_MPXY_SEND_MSG_WITH_RESP with message ID: OPTEED_MSG_COMMUNICATE (0x1) to send the message to tdomain and get the response.
    • Enable preemption (put_cpu()).