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 /* Allocate context resources like ASID and MMU table information */
13 TEE_Result vm_info_init(struct user_mode_ctx *uctx, struct ts_ctx *ts_ctx);
14
15 /* Release context resources like ASID */
16 void vm_info_final(struct user_mode_ctx *uctx);
17
18 /*
19 * Creates a memory map of a mobj.
20 * Desired virtual address can be specified in @va otherwise @va must be
21 * initialized to 0 if the next available can be chosen.
22 *
23 * @pad_begin and @pad_end specify how much extra free space should be kept
24 * when establishing the map. This allows mapping the first part of for
25 * instance an ELF file while knowing that the next part which has to be of
26 * a certain offset from the first part also will succeed.
27 */
28
29 TEE_Result vm_map_pad(struct user_mode_ctx *uctx, vaddr_t *va, size_t len,
30 uint32_t prot, uint32_t flags, struct mobj *mobj,
31 size_t offs, size_t pad_begin, size_t pad_end,
32 size_t align);
33
34 /*
35 * Creates a memory map of a mobj.
36 * Desired virtual address can be specified in @va otherwise @va must be
37 * initialized to 0 if the next available can be chosen.
38 */
vm_map(struct user_mode_ctx * uctx,vaddr_t * va,size_t len,uint32_t prot,uint32_t flags,struct mobj * mobj,size_t offs)39 static inline TEE_Result vm_map(struct user_mode_ctx *uctx, vaddr_t *va,
40 size_t len, uint32_t prot, uint32_t flags,
41 struct mobj *mobj, size_t offs)
42 {
43 return vm_map_pad(uctx, va, len, prot, flags, mobj, offs, 0, 0, 0);
44 }
45
46 TEE_Result vm_remap(struct user_mode_ctx *uctx, vaddr_t *new_va, vaddr_t old_va,
47 size_t len, size_t pad_begin, size_t pad_end);
48
49 TEE_Result vm_get_flags(struct user_mode_ctx *uctx, vaddr_t va, size_t len,
50 uint32_t *flags);
51
52 TEE_Result vm_get_prot(struct user_mode_ctx *uctx, vaddr_t va, size_t len,
53 uint16_t *prot);
54
55 TEE_Result vm_set_prot(struct user_mode_ctx *uctx, vaddr_t va, size_t len,
56 uint32_t prot);
57
58 TEE_Result vm_unmap(struct user_mode_ctx *uctx, vaddr_t va, size_t len);
59
60 /* Map parameters for a user TA */
61 TEE_Result vm_map_param(struct user_mode_ctx *uctx, struct tee_ta_param *param,
62 void *param_va[TEE_NUM_PARAMS]);
63 void vm_clean_param(struct user_mode_ctx *uctx);
64
65 /*
66 * These two functions are deprecated and should only be called from
67 * mobj_seccpy_shm_alloc() and mobj_seccpy_shm_free().
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 /* Helper function for virt_to_phys(), shouldn't be used directly elsewhere */
91 TEE_Result vm_va2pa(const struct user_mode_ctx *uctx, void *ua, paddr_t *pa);
92
93 /* Helper function for phys_to_virt(), shouldn't be used directly elsewhere */
94 void *vm_pa2va(const struct user_mode_ctx *uctx, paddr_t pa, size_t pa_size);
95
96 /*
97 * Return TEE_SUCCESS or TEE_ERROR_ACCESS_DENIED when buffer exists or return
98 * another TEE_Result code.
99 */
100 TEE_Result vm_check_access_rights(const struct user_mode_ctx *uctx,
101 uint32_t flags, uaddr_t uaddr, size_t len);
102
103 /* Set user context @ctx or core privileged context if @ctx is NULL */
104 void vm_set_ctx(struct ts_ctx *ctx);
105
106 struct mobj *vm_get_mobj(struct user_mode_ctx *uctx, vaddr_t va, size_t *len,
107 uint16_t *prot, size_t *offs);
108 #endif /*TEE_MMU_H*/
109