lwp_shm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /* then insert it into the balancing binary tree */
  138. node_key = (struct lwp_avl_struct *)rt_malloc(sizeof(struct lwp_avl_struct) * 2);
  139. if (!node_key)
  140. {
  141. goto err;
  142. }
  143. node_key->avl_key = p->key;
  144. node_key->data = (void *)p;
  145. lwp_avl_insert(node_key, &shm_tree_key);
  146. node_pa = node_key + 1;
  147. node_pa->avl_key = p->addr;
  148. node_pa->data = (void *)p;
  149. lwp_avl_insert(node_pa, &shm_tree_pa);
  150. }
  151. return id;
  152. err:
  153. if (id != -1)
  154. {
  155. shm_id_free(id);
  156. }
  157. if (page_addr)
  158. {
  159. rt_pages_free(page_addr, bit);
  160. }
  161. if (node_key)
  162. {
  163. rt_free(node_key);
  164. }
  165. return -1;
  166. }
  167. /* A wrapping function, get the shared memory with interrupts disabled. */
  168. int lwp_shmget(size_t key, size_t size, int create)
  169. {
  170. int ret = 0;
  171. rt_mm_lock();
  172. ret = _lwp_shmget(key, size, create);
  173. rt_mm_unlock();
  174. return ret;
  175. }
  176. /* Locate the binary tree node_key corresponding to the shared-memory id. */
  177. static struct lwp_avl_struct *shm_id_to_node(int id)
  178. {
  179. struct lwp_avl_struct *node_key = 0;
  180. struct lwp_shm_struct *p = RT_NULL;
  181. /* check id */
  182. if (id < 0 || id >= RT_LWP_SHM_MAX_NR)
  183. {
  184. return RT_NULL;
  185. }
  186. p = _shm_ary + id; /* the address of the shared-memory structure */
  187. node_key = lwp_avl_find(p->key, shm_tree_key);
  188. if (!node_key)
  189. {
  190. return RT_NULL;
  191. }
  192. if (node_key->data != (void *)p)
  193. {
  194. return RT_NULL;
  195. }
  196. return node_key;
  197. }
  198. /* Free the shared pages, the shared-memory structure and its binary tree node_key. */
  199. static int _lwp_shmrm(int id)
  200. {
  201. struct lwp_avl_struct *node_key = RT_NULL;
  202. struct lwp_avl_struct *node_pa = RT_NULL;
  203. struct lwp_shm_struct* p = RT_NULL;
  204. uint32_t bit = 0;
  205. node_key = shm_id_to_node(id);
  206. if (!node_key)
  207. {
  208. return -1;
  209. }
  210. p = (struct lwp_shm_struct *)node_key->data;
  211. if (p->ref)
  212. {
  213. return 0;
  214. }
  215. bit = rt_page_bits(p->size);
  216. rt_pages_free((void *)p->addr, bit);
  217. lwp_avl_remove(node_key, &shm_tree_key);
  218. node_pa = node_key + 1;
  219. lwp_avl_remove(node_pa, &shm_tree_pa);
  220. rt_free(node_key);
  221. shm_id_free(id);
  222. return 0;
  223. }
  224. /* A wrapping function, free the shared memory with interrupt disabled. */
  225. int lwp_shmrm(int id)
  226. {
  227. int ret = 0;
  228. ret = _lwp_shmrm(id);
  229. return ret;
  230. }
  231. /* Map the shared memory specified by 'id' to the specified virtual address. */
  232. static void *_lwp_shmat(int id, void *shm_vaddr)
  233. {
  234. int err;
  235. struct rt_lwp *lwp = RT_NULL;
  236. struct lwp_avl_struct *node_key = RT_NULL;
  237. struct lwp_shm_struct *p = RT_NULL;
  238. void *va = shm_vaddr;
  239. /* The id is used to locate the node_key in the binary tree, and then get the
  240. * shared-memory structure linked to the node_key. We don't use the id to refer
  241. * to the shared-memory structure directly, because the binary tree is used
  242. * to verify the structure is really in use.
  243. */
  244. node_key = shm_id_to_node(id);
  245. if (!node_key)
  246. {
  247. return RT_NULL;
  248. }
  249. p = (struct lwp_shm_struct *)node_key->data; /* p = _shm_ary[id]; */
  250. /* map the shared memory into the address space of the current thread */
  251. lwp = lwp_self();
  252. if (!lwp)
  253. {
  254. return RT_NULL;
  255. }
  256. err = rt_aspace_map(lwp->aspace, &va, p->size, MMU_MAP_U_RWCB, MMF_PREFETCH,
  257. &p->mem_obj, 0);
  258. if (err != RT_EOK)
  259. {
  260. va = RT_NULL;
  261. }
  262. return va;
  263. }
  264. /* A wrapping function: attach the shared memory to the specified address. */
  265. void *lwp_shmat(int id, void *shm_vaddr)
  266. {
  267. void *ret = RT_NULL;
  268. if (((size_t)shm_vaddr & ARCH_PAGE_MASK) != 0)
  269. {
  270. return RT_NULL;
  271. }
  272. ret = _lwp_shmat(id, shm_vaddr);
  273. return ret;
  274. }
  275. static struct lwp_shm_struct *_lwp_shm_struct_get(struct rt_lwp *lwp, void *shm_vaddr)
  276. {
  277. void *pa = RT_NULL;
  278. struct lwp_avl_struct *node_pa = RT_NULL;
  279. if (!lwp)
  280. {
  281. return RT_NULL;
  282. }
  283. pa = lwp_v2p(lwp, shm_vaddr); /* physical memory */
  284. node_pa = lwp_avl_find((size_t)pa, shm_tree_pa);
  285. if (!node_pa)
  286. {
  287. return RT_NULL;
  288. }
  289. return (struct lwp_shm_struct *)node_pa->data;
  290. }
  291. static int _lwp_shm_ref_inc(struct rt_lwp *lwp, void *shm_vaddr)
  292. {
  293. struct lwp_shm_struct* p = _lwp_shm_struct_get(lwp, shm_vaddr);
  294. if (p)
  295. {
  296. p->ref++;
  297. return p->ref;
  298. }
  299. return -1;
  300. }
  301. int lwp_shm_ref_inc(struct rt_lwp *lwp, void *shm_vaddr)
  302. {
  303. int ret = 0;
  304. rt_mm_lock();
  305. ret = _lwp_shm_ref_inc(lwp, shm_vaddr);
  306. rt_mm_unlock();
  307. return ret;
  308. }
  309. static int _lwp_shm_ref_dec(struct rt_lwp *lwp, void *shm_vaddr)
  310. {
  311. struct lwp_shm_struct* p = _lwp_shm_struct_get(lwp, shm_vaddr);
  312. if (p && (p->ref > 0))
  313. {
  314. p->ref--;
  315. return p->ref;
  316. }
  317. return -1;
  318. }
  319. int lwp_shm_ref_dec(struct rt_lwp *lwp, void *shm_vaddr)
  320. {
  321. int ret = 0;
  322. rt_mm_lock();
  323. ret = _lwp_shm_ref_dec(lwp, shm_vaddr);
  324. rt_mm_unlock();
  325. return ret;
  326. }
  327. /* Unmap the shared memory from the address space of the current thread. */
  328. int _lwp_shmdt(void *shm_vaddr)
  329. {
  330. struct rt_lwp *lwp = RT_NULL;
  331. int ret = 0;
  332. lwp = lwp_self();
  333. if (!lwp)
  334. {
  335. return -1;
  336. }
  337. ret = rt_aspace_unmap(lwp->aspace, shm_vaddr);
  338. if (ret != RT_EOK)
  339. {
  340. ret = -1;
  341. }
  342. return ret;
  343. }
  344. /* A wrapping function: detach the mapped shared memory. */
  345. int lwp_shmdt(void *shm_vaddr)
  346. {
  347. int ret = 0;
  348. rt_mm_lock();
  349. ret = _lwp_shmdt(shm_vaddr);
  350. rt_mm_unlock();
  351. return ret;
  352. }
  353. /* Get the virtual address of a shared memory in kernel. */
  354. void *_lwp_shminfo(int id)
  355. {
  356. struct lwp_avl_struct *node_key = RT_NULL;
  357. struct lwp_shm_struct *p = RT_NULL;
  358. /* the share memory is in use only if it exsits in the binary tree */
  359. node_key = shm_id_to_node(id);
  360. if (!node_key)
  361. {
  362. return RT_NULL;
  363. }
  364. p = (struct lwp_shm_struct *)node_key->data; /* p = _shm_ary[id]; */
  365. return (void *)((char *)p->addr - PV_OFFSET); /* get the virtual address */
  366. }
  367. /* A wrapping function: get the virtual address of a shared memory. */
  368. void *lwp_shminfo(int id)
  369. {
  370. void *vaddr = RT_NULL;
  371. rt_mm_lock();
  372. vaddr = _lwp_shminfo(id);
  373. rt_mm_unlock();
  374. return vaddr;
  375. }
  376. #ifdef RT_USING_FINSH
  377. static int _shm_info(struct lwp_avl_struct* node_key, void *data)
  378. {
  379. int id = 0;
  380. struct lwp_shm_struct* p = (struct lwp_shm_struct *)node_key->data;
  381. id = p - _shm_ary;
  382. rt_kprintf("0x%08x 0x%08x 0x%08x %8d\n", p->key, p->addr, p->size, id);
  383. return 0;
  384. }
  385. void list_shm(void)
  386. {
  387. rt_kprintf(" key paddr size id\n");
  388. rt_kprintf("---------- ---------- ---------- --------\n");
  389. rt_mm_lock();
  390. lwp_avl_traversal(shm_tree_key, _shm_info, NULL);
  391. rt_mm_unlock();
  392. }
  393. MSH_CMD_EXPORT(list_shm, show share memory info);
  394. #endif
  395. #endif