lwp_user_mm.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-28 Jesven first version
  9. * 2021-02-12 lizhirui add 64-bit support for lwp_brk
  10. * 2023-09-19 Shell add lwp_user_memory_remap_to_kernel
  11. */
  12. #ifndef __LWP_USER_MM_H__
  13. #define __LWP_USER_MM_H__
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #ifdef ARCH_MM_MMU
  17. #include <lwp.h>
  18. #include <mmu.h>
  19. #include <mm_aspace.h>
  20. #include <mm_fault.h>
  21. #include <mm_page.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define LWP_MAP_FLAG_NONE 0x0000
  26. #define LWP_MAP_FLAG_NOCACHE 0x0001
  27. #define LWP_MAP_FLAG_MAP_FIXED 0x00010000ul
  28. #define LWP_MAP_FLAG_PREFETCH 0x00020000ul
  29. /**
  30. * @brief Map files or devices into memory
  31. * It will create a new mapping in the virtual address space of the target lwp
  32. *
  33. * @param lwp target process
  34. * @param addr address from user space
  35. * @param length length in bytes of mapping
  36. * @param prot protect attribution of mapping
  37. * @param flags flags of control
  38. * @param fd file descriptor
  39. * @param pgoffset offset to fd in 4096 bytes unit
  40. * @return void* the address is successful, otherwise return MAP_FAILED
  41. */
  42. void* lwp_mmap2(struct rt_lwp *lwp, void *addr, size_t length, int prot, int flags, int fd, off_t pgoffset);
  43. /**
  44. * @brief Unmap memory region in user space
  45. *
  46. * @param lwp target process
  47. * @param addr address to unmap
  48. * @param length length in bytes of unmapping
  49. * @return int errno
  50. */
  51. int lwp_munmap(struct rt_lwp *lwp, void *addr, size_t length);
  52. void *lwp_mremap(struct rt_lwp *lwp, void *old_address, size_t old_size,
  53. size_t new_size, int flags, void *new_address);
  54. /**
  55. * @brief Test if address from user is accessible address by user
  56. *
  57. * @param lwp target process
  58. * @param addr address from user space
  59. * @param size the bytes to access
  60. * @return int RT_FALSE/RT_TRUE
  61. */
  62. int lwp_user_accessible_ext(struct rt_lwp *lwp, void *addr, size_t size);
  63. /**
  64. * @brief Test if address from user is accessible address by user
  65. * Same as lwp_user_accessible_ext except that lwp is current lwp
  66. *
  67. * @param addr address from user space
  68. * @param size the bytes to access
  69. * @return int RT_FALSE/RT_TRUE
  70. */
  71. int lwp_user_accessable(void *addr, size_t size);
  72. /**
  73. * @brief Copy n bytes data from src to dst.
  74. * Same as std libc memcpy, except that both src and dst may come from
  75. * user space. lwp_memcpy will test and select the implementation based
  76. * on the memory attribution on run-time
  77. *
  78. * @param dst where the data writes to
  79. * @param src where the data comes from
  80. * @param size the bytes to copy
  81. * @return void* the destination address
  82. */
  83. void *lwp_memcpy(void * __restrict dst, const void * __restrict src, size_t size);
  84. /**
  85. * @brief memcpy from address in user address space to kernel space buffer
  86. *
  87. * @param lwp target process
  88. * @param dst kernel space address where the data writes to
  89. * @param src user space address where the data comes from
  90. * @param size the bytes to copy
  91. * @return size_t the bytes copied
  92. */
  93. size_t lwp_data_get(struct rt_lwp *lwp, void *dst, void *src, size_t size);
  94. /**
  95. * @brief lwp_data_get except that lwp is current lwp
  96. *
  97. * @param dst kernel space address where the data writes to
  98. * @param src user space address where the data comes from
  99. * @param size the bytes to copy
  100. * @return size_t the bytes copied
  101. */
  102. size_t lwp_get_from_user(void *dst, void *src, size_t size);
  103. /**
  104. * @brief memcpy from kernel space buffer to address in user address space
  105. *
  106. * @param lwp target process
  107. * @param dst user space address where the data writes to
  108. * @param src kernel space address where the data comes from
  109. * @param size the bytes to copy
  110. * @return size_t the bytes copied
  111. */
  112. size_t lwp_data_put(struct rt_lwp *lwp, void *dst, void *src, size_t size);
  113. /**
  114. * @brief lwp_data_put except that lwp is current lwp
  115. *
  116. * @param dst user space address where the data writes to
  117. * @param src kernel space address where the data comes from
  118. * @param size the bytes to copy
  119. * @return size_t the bytes copied
  120. */
  121. size_t lwp_put_to_user(void *dst, void *src, size_t size);
  122. /**
  123. * @brief memset to address in user address space
  124. *
  125. * @param lwp target process
  126. * @param dst user space address where the data writes to
  127. * @param c the value to write
  128. * @param size the bytes to copy
  129. * @return size_t the bytes written
  130. */
  131. size_t lwp_data_set(struct rt_lwp *lwp, void *dst, int c, size_t size);
  132. int lwp_user_space_init(struct rt_lwp *lwp, rt_bool_t is_fork);
  133. void lwp_unmap_user_space(struct rt_lwp *lwp);
  134. int lwp_unmap_user(struct rt_lwp *lwp, void *va);
  135. void *lwp_map_user(struct rt_lwp *lwp, void *map_va, size_t map_size, rt_bool_t text);
  136. void lwp_free_command_line_args(char** argv);
  137. char** lwp_get_command_line_args(struct rt_lwp *lwp);
  138. rt_varea_t lwp_map_user_varea(struct rt_lwp *lwp, void *map_va, size_t map_size);
  139. /* check LWP_MAP_FLAG_* */
  140. rt_varea_t lwp_map_user_varea_ext(struct rt_lwp *lwp, void *map_va, size_t map_size, size_t flags);
  141. void *lwp_map_user_phy(struct rt_lwp *lwp, void *map_va, void *map_pa, size_t map_size, rt_bool_t cached);
  142. int lwp_unmap_user_phy(struct rt_lwp *lwp, void *va);
  143. rt_base_t lwp_brk(void *addr);
  144. size_t lwp_user_strlen(const char *s);
  145. size_t lwp_user_strlen_ext(struct rt_lwp *lwp, const char *s);
  146. size_t lwp_strlen(struct rt_lwp *lwp, const char *s);
  147. int lwp_fork_aspace(struct rt_lwp *dest_lwp, struct rt_lwp *src_lwp);
  148. void lwp_data_cache_flush(struct rt_lwp *lwp, void *vaddr, size_t size);
  149. static inline void *_lwp_v2p(struct rt_lwp *lwp, void *vaddr)
  150. {
  151. return rt_hw_mmu_v2p(lwp->aspace, vaddr);
  152. }
  153. static inline void *lwp_v2p(struct rt_lwp *lwp, void *vaddr)
  154. {
  155. RD_LOCK(lwp->aspace);
  156. void *paddr = _lwp_v2p(lwp, vaddr);
  157. RD_UNLOCK(lwp->aspace);
  158. return paddr;
  159. }
  160. /**
  161. * @brief Remapping user space memory region to kernel
  162. *
  163. * @warning the remapped region in kernel should be unmapped after usage
  164. *
  165. * @param lwp target process
  166. * @param uaddr user space address where the data writes to
  167. * @param length the bytes to redirect
  168. * @return void * the redirection address in kernel space
  169. */
  170. void *lwp_user_memory_remap_to_kernel(rt_lwp_t lwp, void *uaddr, size_t length);
  171. rt_inline rt_size_t lwp_user_mm_flag_to_kernel(int flags)
  172. {
  173. rt_size_t k_flags = 0;
  174. if (flags & MAP_FIXED)
  175. k_flags |= MMF_MAP_FIXED;
  176. if (flags & (MAP_PRIVATE | MAP_ANON | MAP_ANONYMOUS))
  177. k_flags |= MMF_MAP_PRIVATE;
  178. if (flags & MAP_SHARED)
  179. k_flags |= MMF_MAP_SHARED;
  180. return k_flags;
  181. }
  182. rt_inline rt_size_t lwp_user_mm_attr_to_kernel(int prot)
  183. {
  184. RT_UNUSED(prot);
  185. rt_size_t k_attr = 0;
  186. #ifdef IMPL_MPROTECT
  187. if ((prot & PROT_EXEC) || (prot & PROT_WRITE) ||
  188. ((prot & PROT_READ) && (prot & PROT_WRITE)))
  189. k_attr = MMU_MAP_U_RWCB;
  190. else if (prot == PROT_NONE)
  191. k_attr = MMU_MAP_K_RWCB;
  192. else
  193. k_attr = MMU_MAP_U_ROCB;
  194. #else
  195. k_attr = MMU_MAP_U_RWCB;
  196. #endif /* IMPL_MPROTECT */
  197. return k_attr;
  198. }
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif
  203. #endif /*__LWP_USER_MM_H__*/