lwp_user_mm.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /**
  53. * @brief Test if address from user is accessible address by user
  54. *
  55. * @param lwp target process
  56. * @param addr address from user space
  57. * @param size the bytes to access
  58. * @return int RT_FALSE/RT_TRUE
  59. */
  60. int lwp_user_accessible_ext(struct rt_lwp *lwp, void *addr, size_t size);
  61. /**
  62. * @brief Test if address from user is accessible address by user
  63. * Same as lwp_user_accessible_ext except that lwp is current lwp
  64. *
  65. * @param addr address from user space
  66. * @param size the bytes to access
  67. * @return int RT_FALSE/RT_TRUE
  68. */
  69. int lwp_user_accessable(void *addr, size_t size);
  70. /**
  71. * @brief Copy n bytes data from src to dst.
  72. * Same as std libc memcpy, except that both src and dst may come from
  73. * user space. lwp_memcpy will test and select the implementation based
  74. * on the memory attribution on run-time
  75. *
  76. * @param dst where the data writes to
  77. * @param src where the data comes from
  78. * @param size the bytes to copy
  79. * @return void* the destination address
  80. */
  81. void *lwp_memcpy(void * __restrict dst, const void * __restrict src, size_t size);
  82. /**
  83. * @brief memcpy from address in user address space to kernel space buffer
  84. *
  85. * @param lwp target process
  86. * @param dst kernel space address where the data writes to
  87. * @param src user space address where the data comes from
  88. * @param size the bytes to copy
  89. * @return size_t the bytes copied
  90. */
  91. size_t lwp_data_get(struct rt_lwp *lwp, void *dst, void *src, size_t size);
  92. /**
  93. * @brief lwp_data_get except that lwp is current lwp
  94. *
  95. * @param dst kernel space address where the data writes to
  96. * @param src user space address where the data comes from
  97. * @param size the bytes to copy
  98. * @return size_t the bytes copied
  99. */
  100. size_t lwp_get_from_user(void *dst, void *src, size_t size);
  101. /**
  102. * @brief memcpy from kernel space buffer to address in user address space
  103. *
  104. * @param lwp target process
  105. * @param dst user space address where the data writes to
  106. * @param src kernel space address where the data comes from
  107. * @param size the bytes to copy
  108. * @return size_t the bytes copied
  109. */
  110. size_t lwp_data_put(struct rt_lwp *lwp, void *dst, void *src, size_t size);
  111. /**
  112. * @brief lwp_data_put except that lwp is current lwp
  113. *
  114. * @param dst user space address where the data writes to
  115. * @param src kernel space address where the data comes from
  116. * @param size the bytes to copy
  117. * @return size_t the bytes copied
  118. */
  119. size_t lwp_put_to_user(void *dst, void *src, size_t size);
  120. /**
  121. * @brief memset to address in user address space
  122. *
  123. * @param lwp target process
  124. * @param dst user space address where the data writes to
  125. * @param c the value to write
  126. * @param size the bytes to copy
  127. * @return size_t the bytes written
  128. */
  129. size_t lwp_data_set(struct rt_lwp *lwp, void *dst, int c, size_t size);
  130. int lwp_user_space_init(struct rt_lwp *lwp, rt_bool_t is_fork);
  131. void lwp_unmap_user_space(struct rt_lwp *lwp);
  132. int lwp_unmap_user(struct rt_lwp *lwp, void *va);
  133. void *lwp_map_user(struct rt_lwp *lwp, void *map_va, size_t map_size, rt_bool_t text);
  134. void lwp_free_command_line_args(char** argv);
  135. char** lwp_get_command_line_args(struct rt_lwp *lwp);
  136. rt_varea_t lwp_map_user_varea(struct rt_lwp *lwp, void *map_va, size_t map_size);
  137. /* check LWP_MAP_FLAG_* */
  138. rt_varea_t lwp_map_user_varea_ext(struct rt_lwp *lwp, void *map_va, size_t map_size, size_t flags);
  139. void *lwp_map_user_phy(struct rt_lwp *lwp, void *map_va, void *map_pa, size_t map_size, rt_bool_t cached);
  140. int lwp_unmap_user_phy(struct rt_lwp *lwp, void *va);
  141. rt_base_t lwp_brk(void *addr);
  142. size_t lwp_user_strlen(const char *s);
  143. size_t lwp_user_strlen_ext(struct rt_lwp *lwp, const char *s);
  144. int lwp_fork_aspace(struct rt_lwp *dest_lwp, struct rt_lwp *src_lwp);
  145. void lwp_data_cache_flush(struct rt_lwp *lwp, void *vaddr, size_t size);
  146. static inline void *_lwp_v2p(struct rt_lwp *lwp, void *vaddr)
  147. {
  148. return rt_hw_mmu_v2p(lwp->aspace, vaddr);
  149. }
  150. static inline void *lwp_v2p(struct rt_lwp *lwp, void *vaddr)
  151. {
  152. RD_LOCK(lwp->aspace);
  153. void *paddr = _lwp_v2p(lwp, vaddr);
  154. RD_UNLOCK(lwp->aspace);
  155. return paddr;
  156. }
  157. /**
  158. * @brief Remapping user space memory region to kernel
  159. *
  160. * @warning the remapped region in kernel should be unmapped after usage
  161. *
  162. * @param lwp target process
  163. * @param uaddr user space address where the data writes to
  164. * @param length the bytes to redirect
  165. * @return void * the redirection address in kernel space
  166. */
  167. void *lwp_user_memory_remap_to_kernel(rt_lwp_t lwp, void *uaddr, size_t length);
  168. rt_inline rt_size_t lwp_user_mm_flag_to_kernel(int flags)
  169. {
  170. rt_size_t k_flags = 0;
  171. if (flags & MAP_FIXED)
  172. k_flags |= MMF_MAP_FIXED;
  173. if (flags & (MAP_PRIVATE | MAP_ANON | MAP_ANONYMOUS))
  174. k_flags |= MMF_MAP_PRIVATE;
  175. if (flags & MAP_SHARED)
  176. k_flags |= MMF_MAP_SHARED;
  177. return k_flags;
  178. }
  179. rt_inline rt_size_t lwp_user_mm_attr_to_kernel(int prot)
  180. {
  181. rt_size_t k_attr = 0;
  182. #ifdef IMPL_MPROTECT
  183. if ((prot & PROT_EXEC) || (prot & PROT_WRITE) ||
  184. ((prot & PROT_READ) && (prot & PROT_WRITE)))
  185. k_attr = MMU_MAP_U_RWCB;
  186. else if (prot == PROT_NONE)
  187. k_attr = MMU_MAP_K_RWCB;
  188. else
  189. k_attr = MMU_MAP_U_ROCB;
  190. #else
  191. k_attr = MMU_MAP_U_RWCB;
  192. #endif /* IMPL_MPROTECT */
  193. return k_attr;
  194. }
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif
  199. #endif /*__LWP_USER_MM_H__*/