lwp_shm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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-12 Jesven first version
  9. * 2023-02-20 wangxiaoyao adapt to mm
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #ifdef ARCH_MM_MMU
  14. #include <lwp.h>
  15. #include <lwp_shm.h>
  16. #include <lwp_mm.h>
  17. #include <lwp_user_mm.h>
  18. #include <mmu.h>
  19. /* the kernel structure to represent a share-memory */
  20. struct lwp_shm_struct
  21. {
  22. struct rt_mem_obj mem_obj;
  23. size_t addr; /* point to the next item in the free list when not used */
  24. size_t size;
  25. int ref;
  26. size_t key;
  27. };
  28. static struct lwp_avl_struct *shm_tree_key;
  29. static struct lwp_avl_struct *shm_tree_pa;
  30. static int shm_free_list = -1; /* the single-direct list of freed items */
  31. static int shm_id_used = 0; /* the latest allocated item in the array */
  32. static struct lwp_shm_struct _shm_ary[RT_LWP_SHM_MAX_NR];
  33. static const char *get_shm_name(rt_varea_t varea)
  34. {
  35. return "user.shm";
  36. }
  37. static void on_shm_varea_open(struct rt_varea *varea)
  38. {
  39. struct lwp_shm_struct *shm;
  40. shm = rt_container_of(varea->mem_obj, struct lwp_shm_struct, mem_obj);
  41. shm->ref += 1;
  42. }
  43. static void on_shm_varea_close(struct rt_varea *varea)
  44. {
  45. struct lwp_shm_struct *shm;
  46. shm = rt_container_of(varea->mem_obj, struct lwp_shm_struct, mem_obj);
  47. shm->ref -= 1;
  48. }
  49. static void on_shm_page_fault(struct rt_varea *varea, struct rt_aspace_fault_msg *msg)
  50. {
  51. struct lwp_shm_struct *shm;
  52. int err;
  53. shm = rt_container_of(varea->mem_obj, struct lwp_shm_struct, mem_obj);
  54. /* map all share page frames to user space in a time */
  55. void *page = (void *)shm->addr;
  56. void *pg_paddr = (char *)page + PV_OFFSET;
  57. err = rt_varea_map_range(varea, varea->start, pg_paddr, shm->size);
  58. if (err == RT_EOK)
  59. {
  60. msg->response.status = MM_FAULT_STATUS_OK_MAPPED;
  61. msg->response.size = shm->size;
  62. msg->response.vaddr = page;
  63. }
  64. return ;
  65. }
  66. /*
  67. * Try to allocate an structure 'lwp_shm_struct' from the freed list or the
  68. * static array.
  69. */
  70. static int _shm_id_alloc(void)
  71. {
  72. int id = -1;
  73. if (shm_free_list != -1) /* first try the freed list */
  74. {
  75. id = shm_free_list;
  76. shm_free_list = (int)_shm_ary[shm_free_list].addr; /* single-direction */
  77. }
  78. else if (shm_id_used < RT_LWP_SHM_MAX_NR) /* then try the array */
  79. {
  80. id = shm_id_used;
  81. shm_id_used++;
  82. }
  83. return id;
  84. }
  85. /* Release the item in the static array to the freed list. */
  86. static void shm_id_free(int id)
  87. {
  88. /* link the freed itme to the single-direction list */
  89. _shm_ary[id].addr = (size_t)shm_free_list;
  90. shm_free_list = id;
  91. }
  92. /* Locate the shared memory through 'key' or create a new one. */
  93. static int _lwp_shmget(size_t key, size_t size, int create)
  94. {
  95. int id = -1;
  96. struct lwp_avl_struct *node_key = 0;
  97. struct lwp_avl_struct *node_pa = 0;
  98. void *page_addr = 0;
  99. uint32_t bit = 0;
  100. /* try to locate the item with the key in the binary tree */
  101. node_key = lwp_avl_find(key, shm_tree_key);
  102. if (node_key)
  103. {
  104. return (struct lwp_shm_struct *)node_key->data - _shm_ary; /* the index */
  105. }
  106. /* If there doesn't exist such an item and we're allowed to create one ... */
  107. if (create)
  108. {
  109. struct lwp_shm_struct* p;
  110. if (!size)
  111. {
  112. goto err;
  113. }
  114. id = _shm_id_alloc();
  115. if (id == -1)
  116. {
  117. goto err;
  118. }
  119. /* allocate pages up to 2's exponent to cover the required size */
  120. bit = rt_page_bits(size);
  121. page_addr = rt_pages_alloc_ext(bit, PAGE_ANY_AVAILABLE); /* virtual address */
  122. if (!page_addr)
  123. {
  124. goto err;
  125. }
  126. /* initialize the shared memory structure */
  127. p = _shm_ary + id;
  128. p->addr = (size_t)page_addr;
  129. p->size = (1UL << (bit + ARCH_PAGE_SHIFT));
  130. p->ref = 0;
  131. p->key = key;
  132. p->mem_obj.get_name = get_shm_name;
  133. p->mem_obj.on_page_fault = on_shm_page_fault;
  134. p->mem_obj.on_varea_open = on_shm_varea_open;
  135. p->mem_obj.on_varea_close = on_shm_varea_close;
  136. p->mem_obj.hint_free = NULL;
  137. p->mem_obj.on_page_offload = NULL;
  138. /* then insert it into the balancing binary tree */
  139. node_key = (struct lwp_avl_struct *)rt_malloc(sizeof(struct lwp_avl_struct) * 2);
  140. if (!node_key)
  141. {
  142. goto err;
  143. }
  144. node_key->avl_key = p->key;
  145. node_key->data = (void *)p;
  146. lwp_avl_insert(node_key, &shm_tree_key);
  147. node_pa = node_key + 1;
  148. node_pa->avl_key = p->addr;
  149. node_pa->data = (void *)p;
  150. lwp_avl_insert(node_pa, &shm_tree_pa);
  151. }
  152. return id;
  153. err:
  154. if (id != -1)
  155. {
  156. shm_id_free(id);
  157. }
  158. if (page_addr)
  159. {
  160. rt_pages_free(page_addr, bit);
  161. }
  162. if (node_key)
  163. {
  164. rt_free(node_key);
  165. }
  166. return -1;
  167. }
  168. /* A wrapping function, get the shared memory with interrupts disabled. */
  169. int lwp_shmget(size_t key, size_t size, int create)
  170. {
  171. int ret = 0;
  172. rt_mm_lock();
  173. ret = _lwp_shmget(key, size, create);
  174. rt_mm_unlock();
  175. return ret;
  176. }
  177. /* Locate the binary tree node_key corresponding to the shared-memory id. */
  178. static struct lwp_avl_struct *shm_id_to_node(int id)
  179. {
  180. struct lwp_avl_struct *node_key = 0;
  181. struct lwp_shm_struct *p = RT_NULL;
  182. /* check id */
  183. if (id < 0 || id >= RT_LWP_SHM_MAX_NR)
  184. {
  185. return RT_NULL;
  186. }
  187. p = _shm_ary + id; /* the address of the shared-memory structure */
  188. node_key = lwp_avl_find(p->key, shm_tree_key);
  189. if (!node_key)
  190. {
  191. return RT_NULL;
  192. }
  193. if (node_key->data != (void *)p)
  194. {
  195. return RT_NULL;
  196. }
  197. return node_key;
  198. }
  199. /* Free the shared pages, the shared-memory structure and its binary tree node_key. */
  200. static int _lwp_shmrm(int id)
  201. {
  202. struct lwp_avl_struct *node_key = RT_NULL;
  203. struct lwp_avl_struct *node_pa = RT_NULL;
  204. struct lwp_shm_struct* p = RT_NULL;
  205. uint32_t bit = 0;
  206. node_key = shm_id_to_node(id);
  207. if (!node_key)
  208. {
  209. return -1;
  210. }
  211. p = (struct lwp_shm_struct *)node_key->data;
  212. if (p->ref)
  213. {
  214. return 0;
  215. }
  216. bit = rt_page_bits(p->size);
  217. rt_pages_free((void *)p->addr, bit);
  218. lwp_avl_remove(node_key, &shm_tree_key);
  219. node_pa = node_key + 1;
  220. lwp_avl_remove(node_pa, &shm_tree_pa);
  221. rt_free(node_key);
  222. shm_id_free(id);
  223. return 0;
  224. }
  225. /* A wrapping function, free the shared memory with interrupt disabled. */
  226. int lwp_shmrm(int id)
  227. {
  228. int ret = 0;
  229. ret = _lwp_shmrm(id);
  230. return ret;
  231. }
  232. /* Map the shared memory specified by 'id' to the specified virtual address. */
  233. static void *_lwp_shmat(int id, void *shm_vaddr)
  234. {
  235. int err;
  236. struct rt_lwp *lwp = RT_NULL;
  237. struct lwp_avl_struct *node_key = RT_NULL;
  238. struct lwp_shm_struct *p = RT_NULL;
  239. void *va = shm_vaddr;
  240. /* The id is used to locate the node_key in the binary tree, and then get the
  241. * shared-memory structure linked to the node_key. We don't use the id to refer
  242. * to the shared-memory structure directly, because the binary tree is used
  243. * to verify the structure is really in use.
  244. */
  245. node_key = shm_id_to_node(id);
  246. if (!node_key)
  247. {
  248. return RT_NULL;
  249. }
  250. p = (struct lwp_shm_struct *)node_key->data; /* p = _shm_ary[id]; */
  251. /* map the shared memory into the address space of the current thread */
  252. lwp = lwp_self();
  253. if (!lwp)
  254. {
  255. return RT_NULL;
  256. }
  257. err = rt_aspace_map(lwp->aspace, &va, p->size, MMU_MAP_U_RWCB, MMF_PREFETCH,
  258. &p->mem_obj, 0);
  259. if (err != RT_EOK)
  260. {
  261. va = RT_NULL;
  262. }
  263. return va;
  264. }
  265. /* A wrapping function: attach the shared memory to the specified address. */
  266. void *lwp_shmat(int id, void *shm_vaddr)
  267. {
  268. void *ret = RT_NULL;
  269. if (((size_t)shm_vaddr & ARCH_PAGE_MASK) != 0)
  270. {
  271. return RT_NULL;
  272. }
  273. ret = _lwp_shmat(id, shm_vaddr);
  274. return ret;
  275. }
  276. static struct lwp_shm_struct *_lwp_shm_struct_get(struct rt_lwp *lwp, void *shm_vaddr)
  277. {
  278. void *pa = RT_NULL;
  279. struct lwp_avl_struct *node_pa = RT_NULL;
  280. if (!lwp)
  281. {
  282. return RT_NULL;
  283. }
  284. pa = lwp_v2p(lwp, shm_vaddr); /* physical memory */
  285. node_pa = lwp_avl_find((size_t)pa, shm_tree_pa);
  286. if (!node_pa)
  287. {
  288. return RT_NULL;
  289. }
  290. return (struct lwp_shm_struct *)node_pa->data;
  291. }
  292. static int _lwp_shm_ref_inc(struct rt_lwp *lwp, void *shm_vaddr)
  293. {
  294. struct lwp_shm_struct* p = _lwp_shm_struct_get(lwp, shm_vaddr);
  295. if (p)
  296. {
  297. p->ref++;
  298. return p->ref;
  299. }
  300. return -1;
  301. }
  302. int lwp_shm_ref_inc(struct rt_lwp *lwp, void *shm_vaddr)
  303. {
  304. int ret = 0;
  305. rt_mm_lock();
  306. ret = _lwp_shm_ref_inc(lwp, shm_vaddr);
  307. rt_mm_unlock();
  308. return ret;
  309. }
  310. static int _lwp_shm_ref_dec(struct rt_lwp *lwp, void *shm_vaddr)
  311. {
  312. struct lwp_shm_struct* p = _lwp_shm_struct_get(lwp, shm_vaddr);
  313. if (p && (p->ref > 0))
  314. {
  315. p->ref--;
  316. return p->ref;
  317. }
  318. return -1;
  319. }
  320. int lwp_shm_ref_dec(struct rt_lwp *lwp, void *shm_vaddr)
  321. {
  322. int ret = 0;
  323. rt_mm_lock();
  324. ret = _lwp_shm_ref_dec(lwp, shm_vaddr);
  325. rt_mm_unlock();
  326. return ret;
  327. }
  328. /* Unmap the shared memory from the address space of the current thread. */
  329. int _lwp_shmdt(void *shm_vaddr)
  330. {
  331. struct rt_lwp *lwp = RT_NULL;
  332. int ret = 0;
  333. lwp = lwp_self();
  334. if (!lwp)
  335. {
  336. return -1;
  337. }
  338. ret = rt_aspace_unmap(lwp->aspace, shm_vaddr);
  339. if (ret != RT_EOK)
  340. {
  341. ret = -1;
  342. }
  343. return ret;
  344. }
  345. /* A wrapping function: detach the mapped shared memory. */
  346. int lwp_shmdt(void *shm_vaddr)
  347. {
  348. int ret = 0;
  349. rt_mm_lock();
  350. ret = _lwp_shmdt(shm_vaddr);
  351. rt_mm_unlock();
  352. return ret;
  353. }
  354. /* Get the virtual address of a shared memory in kernel. */
  355. void *_lwp_shminfo(int id)
  356. {
  357. struct lwp_avl_struct *node_key = RT_NULL;
  358. struct lwp_shm_struct *p = RT_NULL;
  359. /* the share memory is in use only if it exsits in the binary tree */
  360. node_key = shm_id_to_node(id);
  361. if (!node_key)
  362. {
  363. return RT_NULL;
  364. }
  365. p = (struct lwp_shm_struct *)node_key->data; /* p = _shm_ary[id]; */
  366. return (void *)((char *)p->addr - PV_OFFSET); /* get the virtual address */
  367. }
  368. /* A wrapping function: get the virtual address of a shared memory. */
  369. void *lwp_shminfo(int id)
  370. {
  371. void *vaddr = RT_NULL;
  372. rt_mm_lock();
  373. vaddr = _lwp_shminfo(id);
  374. rt_mm_unlock();
  375. return vaddr;
  376. }
  377. #ifdef RT_USING_FINSH
  378. static int _shm_info(struct lwp_avl_struct* node_key, void *data)
  379. {
  380. int id = 0;
  381. struct lwp_shm_struct* p = (struct lwp_shm_struct *)node_key->data;
  382. id = p - _shm_ary;
  383. rt_kprintf("0x%08x 0x%08x 0x%08x %8d\n", p->key, p->addr, p->size, id);
  384. return 0;
  385. }
  386. void list_shm(void)
  387. {
  388. rt_kprintf(" key paddr size id\n");
  389. rt_kprintf("---------- ---------- ---------- --------\n");
  390. rt_mm_lock();
  391. lwp_avl_traversal(shm_tree_key, _shm_info, NULL);
  392. rt_mm_unlock();
  393. }
  394. MSH_CMD_EXPORT(list_shm, show share memory info);
  395. #endif
  396. #endif