OP-TEE / optee_os

Trusted side of the TEE
Other
1.51k stars 1.03k forks source link

Difference between register_phys_mem and register_ddr #6902

Open qazsdcx opened 1 week ago

qazsdcx commented 1 week ago

Hi, OP-TEE experts

I have a question about register_phys_mem and register_ddr.

If I want to register a block of non-secure memory, can I use the following: a.register_phys_mem(MEM_AREA_DDR_OVERALL, 0xXXXX, 0x100000) b.register_ddr(0xXXXX, 0x100000) What is the difference between these 2 ways? I read the write code and found that they have different _section, register_phys_mem is registered in phys_mem_map and register_ddr is registered in phys_ddr_overall. Can both methods be used? If register_phys_mem(MEM_AREA_DDR_OVERALL, 0xXXXX, 0x100000) is used, it seems that there is no type corresponding to MEM_AREA_DDR_OVERALL in core_mmu_type_to_attr()?

Register a piece of non-secure memory. c.register_phys_mem(MEM_AREA_RAM_NSEC, 0xXXXX, 0x100000) d.register_ddr(0xXXXX, 0x100000) Can either of these 2 methods register memory as non-secure?

Thanks!

jenswi-linaro commented 1 week ago

register_phys_mem() maps memory during boot, typically device memory. register_ddr() lets OP-TEE know which ranges of physical memory the normal world might use as shared memory, but it doesn't establish any mappings until the normal world requests to share some memory.

qazsdcx commented 1 week ago

Thank you very much for your answer.

I have another question. Can I use register_phys_mem(MEM_AREA_RAM_NSEC, 0x12000000, 0x100000) instead of register_ddr(0x12000000, 0x100000)? (the 0x12000000 memory is non-secure, and the memoy will be visited by optee os )

Thanks!

etienne-lms commented 1 week ago

The 2 mentioned macros target different needs and behaviors.

register_phys_mem(MEM_AREA_RAM_NSEC, ..., ...) defines a physical memory address range that is mapped in OP-TEE core at boot time as non-secure cached memory: OP-TEE core will be able to access at any time (note that TAs will not).

register_ddr(..., ...) defines a physical memory address range that non-secure world can use as shared memory. OP-TEE does not map this memory range at boot time. It is only when a shared memory buffer is used in a command invocation parameter (see TEE_PARAM_TYPE_MEMREF_*) that OP-TEE checks the buffer matches the registers range(s) and that the related buffer is mapped to the command target endpoint (can be a user TA or a core PTA) for the time the related command is processed, after which the buffer is unmapped.

(I see I just rephrased @jenswi-linaro comment above. Just focus on my 1st sentence: they serve different needs and have different behaviors so it depends on what you expect to do why that piece of memory).

qazsdcx commented 1 week ago

If register_phys_mem and register_ddr register the same memory addr (Or there are overlapping addresses registered for these two operations). Is there any problem with this operation?

etienne-lms commented 1 week ago

It is ok if there are overlaps between the areas registered with register_ddr() and register_phys_mem(MEM_AREA_RAM_NSEC, ...). Both consider memory mapped as non-secure cached memory.

core_mmu_set_discovered_nsec_ddr() checks that the memory ranges registered as legitimate shared memory do not conflict with other registered memory in core static mapping. Actually, the implementation was lacking this explicit test before release tag 4.1.0 and it has been added by commit https://github.com/OP-TEE/optee_os/commit/cbaf4c83e33dbf287d2c421f37f272426980f39e. This change can be backported on earlier release of OP-TEE if needed.