1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #ifndef TEE_MMU_H 6 #define TEE_MMU_H 7 8 #include <tee_api_types.h> 9 #include <kernel/tee_ta_manager.h> 10 #include <kernel/user_ta.h> 11 12 /*----------------------------------------------------------------------------- 13 * Allocate context resources like ASID and MMU table information 14 *---------------------------------------------------------------------------*/ 15 TEE_Result vm_info_init(struct user_mode_ctx *uctx); 16 17 /*----------------------------------------------------------------------------- 18 * Release context resources like ASID 19 *---------------------------------------------------------------------------*/ 20 void vm_info_final(struct user_mode_ctx *uctx); 21 22 /* 23 * Creates a memory map of a mobj. 24 * Desired virtual address can be specified in @va otherwise @va must be 25 * initialized to 0 if the next available can be chosen. 26 * 27 * @pad_begin and @pad_end specify how much extra free space should be kept 28 * when establishing the map. This allows mapping the first part of for 29 * instance an ELF file while knowing that the next part which has to be of 30 * a certain offset from the first part also will succeed. 31 */ 32 33 TEE_Result vm_map_pad(struct user_mode_ctx *uctx, vaddr_t *va, size_t len, 34 uint32_t prot, uint32_t flags, struct mobj *mobj, 35 size_t offs, size_t pad_begin, size_t pad_end, 36 size_t align); 37 38 /* 39 * Creates a memory map of a mobj. 40 * Desired virtual address can be specified in @va otherwise @va must be 41 * initialized to 0 if the next available can be chosen. 42 */ 43 static inline TEE_Result vm_map(struct user_mode_ctx *uctx, vaddr_t *va, 44 size_t len, uint32_t prot, uint32_t flags, 45 struct mobj *mobj, size_t offs) 46 { 47 return vm_map_pad(uctx, va, len, prot, flags, mobj, offs, 0, 0, 0); 48 } 49 50 TEE_Result vm_remap(struct user_mode_ctx *uctx, vaddr_t *new_va, vaddr_t old_va, 51 size_t len, size_t pad_begin, size_t pad_end); 52 53 TEE_Result vm_get_flags(struct user_mode_ctx *uctx, vaddr_t va, size_t len, 54 uint32_t *flags); 55 56 TEE_Result vm_get_prot(struct user_mode_ctx *uctx, vaddr_t va, size_t len, 57 uint16_t *prot); 58 59 TEE_Result vm_set_prot(struct user_mode_ctx *uctx, vaddr_t va, size_t len, 60 uint32_t prot); 61 62 TEE_Result vm_unmap(struct user_mode_ctx *uctx, vaddr_t va, size_t len); 63 64 /* Map parameters for a user TA */ 65 TEE_Result vm_map_param(struct user_mode_ctx *uctx, struct tee_ta_param *param, 66 void *param_va[TEE_NUM_PARAMS]); 67 void vm_clean_param(struct user_mode_ctx *uctx); 68 69 TEE_Result vm_add_rwmem(struct user_mode_ctx *uctx, struct mobj *mobj, 70 vaddr_t *va); 71 void vm_rem_rwmem(struct user_mode_ctx *uctx, struct mobj *mobj, vaddr_t va); 72 73 /* 74 * User mode private memory is defined as user mode image static segment 75 * (code, ro/rw static data, heap, stack). The sole other virtual memory 76 * mapped to user mode are memref parameters. These later are considered 77 * outside user mode private memory as it might be accessed by the user 78 * mode context and its client(s). 79 */ 80 bool vm_buf_is_inside_um_private(const struct user_mode_ctx *uctx, 81 const void *va, size_t size); 82 83 bool vm_buf_intersects_um_private(const struct user_mode_ctx *uctx, 84 const void *va, size_t size); 85 86 TEE_Result vm_buf_to_mboj_offs(const struct user_mode_ctx *uctx, 87 const void *va, size_t size, 88 struct mobj **mobj, size_t *offs); 89 90 /*----------------------------------------------------------------------------- 91 * vm_va2pa - Translate virtual user address to physical address 92 * given the user context. 93 * Interface is deprecated, use virt_to_phys() instead. 94 *---------------------------------------------------------------------------*/ 95 TEE_Result vm_va2pa(const struct user_mode_ctx *uctx, void *ua, paddr_t *pa); 96 97 /*----------------------------------------------------------------------------- 98 * vm_pa2va - Translate physical address to virtual user address 99 * given the user context. 100 * Interface is deprecated, use phys_to_virt() instead. 101 *---------------------------------------------------------------------------*/ 102 TEE_Result vm_pa2va(const struct user_mode_ctx *uctx, paddr_t pa, void **va); 103 104 TEE_Result vm_check_access_rights(const struct user_mode_ctx *uctx, 105 uint32_t flags, uaddr_t uaddr, size_t len); 106 107 /*----------------------------------------------------------------------------- 108 * If ctx is NULL user mapping is removed and ASID set to 0 109 *---------------------------------------------------------------------------*/ 110 void vm_set_ctx(struct ts_ctx *ctx); 111 112 #endif /*TEE_MMU_H*/ 113