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);
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 TEE_Result vm_add_rwmem(struct user_mode_ctx *uctx, struct mobj *mobj,
66 vaddr_t *va);
67 void vm_rem_rwmem(struct user_mode_ctx *uctx, struct mobj *mobj, vaddr_t va);
68
69 /*
70 * User mode private memory is defined as user mode image static segment
71 * (code, ro/rw static data, heap, stack). The sole other virtual memory
72 * mapped to user mode are memref parameters. These later are considered
73 * outside user mode private memory as it might be accessed by the user
74 * mode context and its client(s).
75 */
76 bool vm_buf_is_inside_um_private(const struct user_mode_ctx *uctx,
77 const void *va, size_t size);
78
79 bool vm_buf_intersects_um_private(const struct user_mode_ctx *uctx,
80 const void *va, size_t size);
81
82 TEE_Result vm_buf_to_mboj_offs(const struct user_mode_ctx *uctx,
83 const void *va, size_t size,
84 struct mobj **mobj, size_t *offs);
85
86 /* Helper function for virt_to_phys(), shouldn't be used directly elsewhere */
87 TEE_Result vm_va2pa(const struct user_mode_ctx *uctx, void *ua, paddr_t *pa);
88
89 /* Helper function for phys_to_virt(), shouldn't be used directly elsewhere */
90 void *vm_pa2va(const struct user_mode_ctx *uctx, paddr_t pa, size_t pa_size);
91
92 /*
93 * Return TEE_SUCCESS or TEE_ERROR_ACCESS_DENIED when buffer exists or return
94 * another TEE_Result code.
95 */
96 TEE_Result vm_check_access_rights(const struct user_mode_ctx *uctx,
97 uint32_t flags, uaddr_t uaddr, size_t len);
98
99 /* Set user context @ctx or core privileged context if @ctx is NULL */
100 void vm_set_ctx(struct ts_ctx *ctx);
101
102 struct mobj *vm_get_mobj(struct user_mode_ctx *uctx, vaddr_t va, size_t *len,
103 uint16_t *prot, size_t *offs);
104 #endif /*TEE_MMU_H*/
105