lwp_shm.c 11 KB

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