lwp_shm.c 11 KB

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