lwp_user_mm.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. */
  11. #ifndef __LWP_USER_MM_H__
  12. #define __LWP_USER_MM_H__
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #ifdef ARCH_MM_MMU
  16. #include <lwp.h>
  17. #include <mmu.h>
  18. #include <mm_aspace.h>
  19. #include <mm_fault.h>
  20. #include <mm_page.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #define LWP_MAP_FLAG_NONE 0x0000
  25. #define LWP_MAP_FLAG_NOCACHE 0x0001
  26. /**
  27. * @brief Map files or devices into memory
  28. * It will create a new mapping in the virtual address space of the target lwp
  29. *
  30. * @param lwp target process
  31. * @param addr address from user space
  32. * @param length length in bytes of mapping
  33. * @param prot protect attribution of mapping
  34. * @param flags flags of control
  35. * @param fd file descriptor
  36. * @param pgoffset offset to fd in 4096 bytes unit
  37. * @return void* the address is successful, otherwise return MAP_FAILED
  38. */
  39. void* lwp_mmap2(void *addr, size_t length, int prot, int flags, int fd, off_t pgoffset);
  40. /**
  41. * @brief Unmap memory region in user space
  42. *
  43. * @param lwp target process
  44. * @param addr address to unmap
  45. * @param length length in bytes of unmapping
  46. * @return int errno
  47. */
  48. int lwp_munmap(void *addr);
  49. /**
  50. * @brief Test if address from user is accessible address by user
  51. *
  52. * @param lwp target process
  53. * @param addr address from user space
  54. * @param size the bytes to access
  55. * @return int RT_FALSE/RT_TRUE
  56. */
  57. int lwp_user_accessible_ext(struct rt_lwp *lwp, void *addr, size_t size);
  58. /**
  59. * @brief Test if address from user is accessible address by user
  60. * Same as lwp_user_accessible_ext except that lwp is current lwp
  61. *
  62. * @param addr address from user space
  63. * @param size the bytes to access
  64. * @return int RT_FALSE/RT_TRUE
  65. */
  66. int lwp_user_accessable(void *addr, size_t size);
  67. /**
  68. * @brief Copy n bytes data from src to dst.
  69. * Same as std libc memcpy, except that both src and dst may come from
  70. * user space. lwp_memcpy will test and select the implementation based
  71. * on the memory attribution on run-time
  72. *
  73. * @param dst where the data writes to
  74. * @param src where the data comes from
  75. * @param size the bytes to copy
  76. * @return void* the destination address
  77. */
  78. void *lwp_memcpy(void * __restrict dst, const void * __restrict src, size_t size);
  79. /**
  80. * @brief memcpy from address in user address space to kernel space buffer
  81. *
  82. * @param lwp target process
  83. * @param dst kernel space address where the data writes to
  84. * @param src user space address where the data comes from
  85. * @param size the bytes to copy
  86. * @return size_t the bytes copied
  87. */
  88. size_t lwp_data_get(struct rt_lwp *lwp, void *dst, void *src, size_t size);
  89. /**
  90. * @brief lwp_data_get except that lwp is current lwp
  91. *
  92. * @param dst kernel space address where the data writes to
  93. * @param src user space address where the data comes from
  94. * @param size the bytes to copy
  95. * @return size_t the bytes copied
  96. */
  97. size_t lwp_get_from_user(void *dst, void *src, size_t size);
  98. /**
  99. * @brief memcpy from kernel space buffer to address in user address space
  100. *
  101. * @param lwp target process
  102. * @param dst user space address where the data writes to
  103. * @param src kernel space address where the data comes from
  104. * @param size the bytes to copy
  105. * @return size_t the bytes copied
  106. */
  107. size_t lwp_data_put(struct rt_lwp *lwp, void *dst, void *src, size_t size);
  108. /**
  109. * @brief lwp_data_put except that lwp is current lwp
  110. *
  111. * @param dst user space address where the data writes to
  112. * @param src kernel space address where the data comes from
  113. * @param size the bytes to copy
  114. * @return size_t the bytes copied
  115. */
  116. size_t lwp_put_to_user(void *dst, void *src, size_t size);
  117. /**
  118. * @brief memset to address in user address space
  119. *
  120. * @param lwp target process
  121. * @param dst user space address where the data writes to
  122. * @param c the value to write
  123. * @param size the bytes to copy
  124. * @return size_t the bytes written
  125. */
  126. size_t lwp_data_set(struct rt_lwp *lwp, void *dst, int c, size_t size);
  127. int lwp_user_space_init(struct rt_lwp *lwp, rt_bool_t is_fork);
  128. void lwp_unmap_user_space(struct rt_lwp *lwp);
  129. int lwp_unmap_user(struct rt_lwp *lwp, void *va);
  130. void *lwp_map_user(struct rt_lwp *lwp, void *map_va, size_t map_size, rt_bool_t text);
  131. rt_varea_t lwp_map_user_varea(struct rt_lwp *lwp, void *map_va, size_t map_size);
  132. /* check LWP_MAP_FLAG_* */
  133. rt_varea_t lwp_map_user_varea_ext(struct rt_lwp *lwp, void *map_va, size_t map_size, size_t flags);
  134. void *lwp_map_user_phy(struct rt_lwp *lwp, void *map_va, void *map_pa, size_t map_size, rt_bool_t cached);
  135. int lwp_unmap_user_phy(struct rt_lwp *lwp, void *va);
  136. rt_base_t lwp_brk(void *addr);
  137. void lwp_data_cache_flush(struct rt_lwp *lwp, void *vaddr, size_t size);
  138. static inline void *_lwp_v2p(struct rt_lwp *lwp, void *vaddr)
  139. {
  140. return rt_hw_mmu_v2p(lwp->aspace, vaddr);
  141. }
  142. static inline void *lwp_v2p(struct rt_lwp *lwp, void *vaddr)
  143. {
  144. RD_LOCK(lwp->aspace);
  145. void *paddr = _lwp_v2p(lwp, vaddr);
  146. RD_UNLOCK(lwp->aspace);
  147. return paddr;
  148. }
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif
  153. #endif /*__LWP_USER_MM_H__*/