lwp_shm.c 11 KB

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