Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: tee: optee: Use arch_page_phys_get for address conversion #83

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

oleksiimoisieiev
Copy link

According to the description of z_mem_phys_addr it uses Z_MEM_VM_OFFSET to make a conversion from virtual to physical address. And these macros are intended for assembly, linker code, and static initializers.
So when external libraries, such as PICOLIBC or NEWLIB_LIBC uses internal mappings for dynamic memory allocation z_mem_phys_addr may not work properly and return wrong physical address.

We have met this problem when testing work with MBEDTLS to generate certificate using PKCS11 in libckteec.
Mbedtls allocates buffer dynamically and ivokes tee command to request certificate from PKCS11 TA which should place result to the allocated buffer.
The result is the MBEDTLS is used with minimal libc - then everything works fine. But when CONFIG_PICOLIBC or CONFIG_NEWLIB_LIBC is enabled

  • it returns empty buffer. This happens because z_mem_phys_addr returns incorrect physical address.

@oleksiimoisieiev oleksiimoisieiev force-pushed the optee_z_mem_fix branch 2 times, most recently from ec20348 to a2781b4 Compare February 11, 2024 19:21
Copy link
Collaborator

@lorc lorc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't met a problem, but you can face it.

I am not sure that you need CONFIG_MMU checks. OP-TEE driver will not work correctly without it anyways.

static uintptr_t optee_mem_phys_addr(void *virt)
{
uintptr_t phys;
uint32_t offt = (uintptr_t)virt % CONFIG_MMU_PAGE_SIZE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pity that zephyr does not provide PAGE_MASK. I just hope that compiller will be smart enough to replace division with bitwise AND.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

unsigned long a4, unsigned long a5, unsigned long a6, unsigned long a7,
struct arm_smccc_res *res)
static void
optee_smccc_smc(unsigned long a0, unsigned long a1, unsigned long a2, unsigned long a3,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@@ -1113,7 +1146,7 @@ static int optee_suppl_send(const struct device *dev, unsigned int ret, unsigned
break;
case TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
case TEE_PARAM_ATTR_TYPE_MEMREF_INOUT:
LOG_WRN("Memref params are not fully tested");
/* LOG_WRN("Memcpyref params are not fully tested"); */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

@oleksiimoisieiev oleksiimoisieiev force-pushed the optee_z_mem_fix branch 4 times, most recently from dc6441d to dcf0c67 Compare February 12, 2024 12:53
@lorc
Copy link
Collaborator

lorc commented Feb 12, 2024

Reviewed-by: Volodymyr Babchuk <[email protected]>

@oleksiimoisieiev oleksiimoisieiev force-pushed the optee_z_mem_fix branch 2 times, most recently from 8fdb182 to 26502a4 Compare February 12, 2024 14:29
@oleksiimoisieiev oleksiimoisieiev changed the title drivers: tee: optee: Use arch_page_phys_get when MMU enabled drivers: tee: optee: Use arch_page_phys_get for address conversion Feb 12, 2024
According to the description of z_mem_phys_addr it uses
Z_MEM_VM_OFFSET to make a conversion from virtual to physical
address. And these macros are intended for assembly, linker code,
and static initializers.
So when external libraries, such as PICOLIBC or NEWLIB_LIBC
uses internal mappings for dynamic memory allocation z_mem_phys_addr
may not work properly and return wrong physical address.

We encountered this problem while testing the generation of a
certificate using PKCS11 in libckteec with MBEDTLS.
Mbedtls allocates buffer dynamically and ivokes tee command to request
certificate from PKCS11 TA which should place result to the allocated
buffer.
The result is the MBEDTLS is used with minimal libc - then everything
works fine. But when CONFIG_PICOLIBC or CONFIG_NEWLIB_LIBC is enabled
- it returns empty buffer. This happens because z_mem_phys_addr
returns incorrect physical address.

Signed-off-by: Oleksii Moisieiev <[email protected]>
Reviewed-by: Volodymyr Babchuk <[email protected]>
@lorc lorc merged commit d8be7fe into xen-troops:zephyr-v3.3.0-xt Feb 12, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants