lwp_shm.c 9.8 KB

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