dlmodule.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/08/29 Bernard first version
  9. */
  10. #include <rthw.h>
  11. #include "dlfcn.h"
  12. #include "dlmodule.h"
  13. #include "dlelf.h"
  14. #if defined(RT_USING_POSIX)
  15. #include <dfs_posix.h>
  16. #endif
  17. #define DBG_TAG "DLMD"
  18. #define DBG_LVL DBG_INFO
  19. #include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
  20. static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
  21. static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
  22. #if defined(__IAR_SYSTEMS_ICC__) /* for IAR compiler */
  23. #pragma section="RTMSymTab"
  24. #endif
  25. /* set the name of module */
  26. static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
  27. {
  28. int size;
  29. struct rt_object *object;
  30. const char *first, *end, *ptr;
  31. object = &(module->parent);
  32. ptr = first = (char *)path;
  33. end = path + rt_strlen(path);
  34. while (*ptr != '\0')
  35. {
  36. if (*ptr == '/')
  37. first = ptr + 1;
  38. if (*ptr == '.')
  39. end = ptr - 1;
  40. ptr ++;
  41. }
  42. size = end - first + 1;
  43. if (size > RT_NAME_MAX) size = RT_NAME_MAX;
  44. rt_strncpy(object->name, first, size);
  45. object->name[size] = '\0';
  46. }
  47. #define RT_MODULE_ARG_MAX 8
  48. static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[])
  49. {
  50. int argc = 0;
  51. char *ptr = cmd;
  52. while ((ptr - cmd) < length)
  53. {
  54. /* strip bank and tab */
  55. while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length)
  56. *ptr++ = '\0';
  57. /* check whether it's the end of line */
  58. if ((ptr - cmd) >= length) break;
  59. /* handle string with quote */
  60. if (*ptr == '"')
  61. {
  62. argv[argc++] = ++ptr;
  63. /* skip this string */
  64. while (*ptr != '"' && (ptr - cmd) < length)
  65. if (*ptr ++ == '\\') ptr ++;
  66. if ((ptr - cmd) >= length) break;
  67. /* skip '"' */
  68. *ptr ++ = '\0';
  69. }
  70. else
  71. {
  72. argv[argc++] = ptr;
  73. while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
  74. ptr ++;
  75. }
  76. if (argc >= RT_MODULE_ARG_MAX) break;
  77. }
  78. return argc;
  79. }
  80. /* invoked by main thread for exit */
  81. static void _dlmodule_exit(void)
  82. {
  83. struct rt_dlmodule *module;
  84. module = dlmodule_self();
  85. if (!module) return; /* not a module thread */
  86. rt_enter_critical();
  87. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  88. {
  89. struct rt_object *object = RT_NULL;
  90. struct rt_list_node *node = RT_NULL;
  91. /* set stat to closing */
  92. module->stat = RT_DLMODULE_STAT_CLOSING;
  93. /* suspend all threads in this module */
  94. for (node = module->object_list.next; node != &(module->object_list); node = node->next)
  95. {
  96. object = rt_list_entry(node, struct rt_object, list);
  97. if ((object->type & ~RT_Object_Class_Static) == RT_Object_Class_Thread)
  98. {
  99. rt_thread_t thread = (rt_thread_t)object;
  100. /* stop timer and suspend thread*/
  101. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  102. (thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  103. {
  104. rt_timer_stop(&(thread->thread_timer));
  105. rt_thread_suspend(thread);
  106. }
  107. }
  108. }
  109. }
  110. rt_exit_critical();
  111. return;
  112. }
  113. static void _dlmodule_thread_entry(void* parameter)
  114. {
  115. int argc = 0;
  116. char *argv[RT_MODULE_ARG_MAX];
  117. struct rt_dlmodule *module = (struct rt_dlmodule*)parameter;
  118. if (module == RT_NULL || module->cmd_line == RT_NULL)
  119. /* malloc for module_cmd_line failed. */
  120. return;
  121. if (module->cmd_line)
  122. {
  123. rt_memset(argv, 0x00, sizeof(argv));
  124. argc = _rt_module_split_arg((char *)module->cmd_line, rt_strlen(module->cmd_line), argv);
  125. if (argc == 0) goto __exit;
  126. }
  127. /* set status of module */
  128. module->stat = RT_DLMODULE_STAT_RUNNING;
  129. LOG_D("run main entry: 0x%p with %s",
  130. module->entry_addr,
  131. module->cmd_line);
  132. if (module->entry_addr)
  133. module->entry_addr(argc, argv);
  134. __exit:
  135. _dlmodule_exit();
  136. return ;
  137. }
  138. struct rt_dlmodule *dlmodule_create(void)
  139. {
  140. struct rt_dlmodule *module = RT_NULL;
  141. module = (struct rt_dlmodule*) rt_object_allocate(RT_Object_Class_Module, "module");
  142. if (module)
  143. {
  144. module->stat = RT_DLMODULE_STAT_INIT;
  145. /* set initial priority and stack size */
  146. module->priority = RT_THREAD_PRIORITY_MAX - 1;
  147. module->stack_size = 2048;
  148. rt_list_init(&(module->object_list));
  149. }
  150. return module;
  151. }
  152. void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
  153. {
  154. RT_ASSERT(thread->module_id == module);
  155. /* lock scheduler to prevent scheduling in cleanup function. */
  156. rt_enter_critical();
  157. /* remove thread from thread_list (ready or defunct thread list) */
  158. rt_list_remove(&(thread->tlist));
  159. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  160. (thread->thread_timer.parent.type == (RT_Object_Class_Static | RT_Object_Class_Timer)))
  161. {
  162. /* release thread timer */
  163. rt_timer_detach(&(thread->thread_timer));
  164. }
  165. /* change stat */
  166. thread->stat = RT_THREAD_CLOSE;
  167. /* invoke thread cleanup */
  168. if (thread->cleanup != RT_NULL)
  169. thread->cleanup(thread);
  170. rt_exit_critical();
  171. #ifdef RT_USING_SIGNALS
  172. rt_thread_free_sig(thread);
  173. #endif
  174. if (thread->type & RT_Object_Class_Static)
  175. {
  176. /* detach object */
  177. rt_object_detach((rt_object_t)thread);
  178. }
  179. #ifdef RT_USING_HEAP
  180. else
  181. {
  182. /* release thread's stack */
  183. RT_KERNEL_FREE(thread->stack_addr);
  184. /* delete thread object */
  185. rt_object_delete((rt_object_t)thread);
  186. }
  187. #endif
  188. }
  189. rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
  190. {
  191. int i;
  192. RT_DEBUG_NOT_IN_INTERRUPT;
  193. /* check parameter */
  194. if (module == RT_NULL)
  195. return -RT_ERROR;
  196. /* can not destroy a running module */
  197. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  198. return -RT_EBUSY;
  199. /* do module cleanup */
  200. if (module->cleanup_func)
  201. {
  202. rt_enter_critical();
  203. module->cleanup_func(module);
  204. rt_exit_critical();
  205. }
  206. // list_object(&(module->object_list));
  207. /* cleanup for all kernel objects inside module*/
  208. {
  209. struct rt_object *object = RT_NULL;
  210. struct rt_list_node *node = RT_NULL;
  211. /* detach/delete all threads in this module */
  212. for (node = module->object_list.next; node != &(module->object_list); )
  213. {
  214. int object_type;
  215. object = rt_list_entry(node, struct rt_object, list);
  216. object_type = object->type & ~RT_Object_Class_Static;
  217. /* to next node */
  218. node = node->next;
  219. if (object->type & RT_Object_Class_Static)
  220. {
  221. switch (object_type)
  222. {
  223. case RT_Object_Class_Thread:
  224. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  225. break;
  226. #ifdef RT_USING_SEMAPHORE
  227. case RT_Object_Class_Semaphore:
  228. rt_sem_detach((rt_sem_t)object);
  229. break;
  230. #endif
  231. #ifdef RT_USING_MUTEX
  232. case RT_Object_Class_Mutex:
  233. rt_mutex_detach((rt_mutex_t)object);
  234. break;
  235. #endif
  236. #ifdef RT_USING_EVENT
  237. case RT_Object_Class_Event:
  238. rt_event_detach((rt_event_t)object);
  239. break;
  240. #endif
  241. #ifdef RT_USING_MAILBOX
  242. case RT_Object_Class_MailBox:
  243. rt_mb_detach((rt_mailbox_t)object);
  244. break;
  245. #endif
  246. #ifdef RT_USING_MESSAGEQUEUE
  247. case RT_Object_Class_MessageQueue:
  248. rt_mq_detach((rt_mq_t)object);
  249. break;
  250. #endif
  251. #ifdef RT_USING_MEMHEAP
  252. case RT_Object_Class_MemHeap:
  253. rt_memheap_detach((struct rt_memheap*)object);
  254. break;
  255. #endif
  256. #ifdef RT_USING_MEMPOOL
  257. case RT_Object_Class_MemPool:
  258. rt_mp_detach((struct rt_mempool*)object);
  259. break;
  260. #endif
  261. case RT_Object_Class_Timer:
  262. rt_timer_detach((rt_timer_t)object);
  263. break;
  264. default:
  265. LOG_E("Unsupported oject type in module.");
  266. break;
  267. }
  268. }
  269. else
  270. {
  271. switch (object_type)
  272. {
  273. case RT_Object_Class_Thread:
  274. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  275. break;
  276. #ifdef RT_USING_SEMAPHORE
  277. case RT_Object_Class_Semaphore:
  278. rt_sem_delete((rt_sem_t)object);
  279. break;
  280. #endif
  281. #ifdef RT_USING_MUTEX
  282. case RT_Object_Class_Mutex:
  283. rt_mutex_delete((rt_mutex_t)object);
  284. break;
  285. #endif
  286. #ifdef RT_USING_EVENT
  287. case RT_Object_Class_Event:
  288. rt_event_delete((rt_event_t)object);
  289. break;
  290. #endif
  291. #ifdef RT_USING_MAILBOX
  292. case RT_Object_Class_MailBox:
  293. rt_mb_delete((rt_mailbox_t)object);
  294. break;
  295. #endif
  296. #ifdef RT_USING_MESSAGEQUEUE
  297. case RT_Object_Class_MessageQueue:
  298. rt_mq_delete((rt_mq_t)object);
  299. break;
  300. #endif
  301. #ifdef RT_USING_MEMHEAP
  302. /* no delete operation */
  303. #endif
  304. #ifdef RT_USING_MEMPOOL
  305. case RT_Object_Class_MemPool:
  306. rt_mp_delete((struct rt_mempool*)object);
  307. break;
  308. #endif
  309. case RT_Object_Class_Timer:
  310. rt_timer_delete((rt_timer_t)object);
  311. break;
  312. default:
  313. LOG_E("Unsupported oject type in module.");
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. if (module->cmd_line) rt_free(module->cmd_line);
  320. /* release module symbol table */
  321. for (i = 0; i < module->nsym; i ++)
  322. {
  323. rt_free((void *)module->symtab[i].name);
  324. }
  325. if (module->symtab != RT_NULL)
  326. {
  327. rt_free(module->symtab);
  328. }
  329. /* destory module */
  330. rt_free(module->mem_space);
  331. /* delete module object */
  332. rt_object_delete((rt_object_t)module);
  333. return RT_EOK;
  334. }
  335. struct rt_dlmodule *dlmodule_self(void)
  336. {
  337. rt_thread_t tid;
  338. struct rt_dlmodule *ret = RT_NULL;
  339. tid = rt_thread_self();
  340. if (tid)
  341. {
  342. ret = (struct rt_dlmodule*) tid->module_id;
  343. }
  344. return ret;
  345. }
  346. /*
  347. * Compatible with old API
  348. */
  349. struct rt_dlmodule *rt_module_self(void)
  350. {
  351. return dlmodule_self();
  352. }
  353. struct rt_dlmodule* dlmodule_load(const char* filename)
  354. {
  355. #if defined(RT_USING_POSIX)
  356. int fd = -1, length = 0;
  357. #endif
  358. rt_err_t ret = RT_EOK;
  359. rt_uint8_t *module_ptr = RT_NULL;
  360. struct rt_dlmodule *module = RT_NULL;
  361. #if defined(RT_USING_POSIX)
  362. fd = open(filename, O_RDONLY, 0);
  363. if (fd >= 0)
  364. {
  365. length = lseek(fd, 0, SEEK_END);
  366. lseek(fd, 0, SEEK_SET);
  367. if (length == 0) goto __exit;
  368. module_ptr = (uint8_t*) rt_malloc (length);
  369. if (!module_ptr) goto __exit;
  370. if (read(fd, module_ptr, length) != length)
  371. goto __exit;
  372. /* close file and release fd */
  373. close(fd);
  374. fd = -1;
  375. }
  376. else
  377. {
  378. goto __exit;
  379. }
  380. #endif
  381. if (!module_ptr) goto __exit;
  382. /* check ELF header */
  383. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  384. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  385. {
  386. rt_kprintf("Module: magic error\n");
  387. goto __exit;
  388. }
  389. /* check ELF class */
  390. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  391. {
  392. rt_kprintf("Module: ELF class error\n");
  393. goto __exit;
  394. }
  395. module = dlmodule_create();
  396. if (!module) goto __exit;
  397. /* set the name of module */
  398. _dlmodule_set_name(module, filename);
  399. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  400. if (elf_module->e_type == ET_REL)
  401. {
  402. ret = dlmodule_load_relocated_object(module, module_ptr);
  403. }
  404. else if (elf_module->e_type == ET_DYN)
  405. {
  406. ret = dlmodule_load_shared_object(module, module_ptr);
  407. }
  408. else
  409. {
  410. rt_kprintf("Module: unsupported elf type\n");
  411. goto __exit;
  412. }
  413. /* check return value */
  414. if (ret != RT_EOK) goto __exit;
  415. /* release module data */
  416. rt_free(module_ptr);
  417. /* increase module reference count */
  418. module->nref ++;
  419. /* deal with cache */
  420. #ifdef RT_USING_CACHE
  421. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  422. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  423. #endif
  424. /* set module initialization and cleanup function */
  425. module->init_func = dlsym(module, "module_init");
  426. module->cleanup_func = dlsym(module, "module_cleanup");
  427. module->stat = RT_DLMODULE_STAT_INIT;
  428. /* do module initialization */
  429. if (module->init_func)
  430. {
  431. module->init_func(module);
  432. }
  433. return module;
  434. __exit:
  435. #if defined(RT_USING_POSIX)
  436. if (fd >= 0) close(fd);
  437. #endif
  438. if (module_ptr) rt_free(module_ptr);
  439. if (module) dlmodule_destroy(module);
  440. return RT_NULL;
  441. }
  442. struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
  443. {
  444. struct rt_dlmodule *module = RT_NULL;
  445. module = dlmodule_load(pgname);
  446. if (module)
  447. {
  448. if (module->entry_addr)
  449. {
  450. /* exec this module */
  451. rt_thread_t tid;
  452. module->cmd_line = rt_strdup(cmd);
  453. /* check stack size and priority */
  454. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  455. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  456. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  457. module->stack_size, module->priority, 10);
  458. if (tid)
  459. {
  460. tid->module_id = module;
  461. module->main_thread = tid;
  462. rt_thread_startup(tid);
  463. }
  464. else
  465. {
  466. /* destory dl module */
  467. dlmodule_destroy(module);
  468. module = RT_NULL;
  469. }
  470. }
  471. }
  472. return module;
  473. }
  474. void dlmodule_exit(int ret_code)
  475. {
  476. rt_thread_t thread;
  477. struct rt_dlmodule *module;
  478. module = dlmodule_self();
  479. if (!module) return;
  480. /* disable scheduling */
  481. rt_enter_critical();
  482. /* module is not running */
  483. if (module->stat != RT_DLMODULE_STAT_RUNNING)
  484. {
  485. /* restore scheduling */
  486. rt_exit_critical();
  487. return;
  488. }
  489. /* set return code */
  490. module->ret_code = ret_code;
  491. /* do exit for this module */
  492. _dlmodule_exit();
  493. /* the stat of module was changed to CLOSING in _dlmodule_exit */
  494. thread = module->main_thread;
  495. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  496. {
  497. /* main thread already closed */
  498. rt_exit_critical();
  499. return ;
  500. }
  501. /* delete thread: insert to defunct thread list */
  502. rt_thread_delete(thread);
  503. /* enable scheduling */
  504. rt_exit_critical();
  505. }
  506. rt_uint32_t dlmodule_symbol_find(const char *sym_str)
  507. {
  508. /* find in kernel symbol table */
  509. struct rt_module_symtab *index;
  510. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  511. {
  512. if (rt_strcmp(index->name, sym_str) == 0)
  513. return (rt_uint32_t)index->addr;
  514. }
  515. return 0;
  516. }
  517. int rt_system_dlmodule_init(void)
  518. {
  519. #if defined(__GNUC__) && !defined(__CC_ARM)
  520. extern int __rtmsymtab_start;
  521. extern int __rtmsymtab_end;
  522. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  523. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  524. #elif defined (__CC_ARM)
  525. extern int RTMSymTab$$Base;
  526. extern int RTMSymTab$$Limit;
  527. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  528. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  529. #elif defined (__IAR_SYSTEMS_ICC__)
  530. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  531. _rt_module_symtab_end = __section_end("RTMSymTab");
  532. #endif
  533. return 0;
  534. }
  535. INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
  536. /**
  537. * This function will find the specified module.
  538. *
  539. * @param name the name of module finding
  540. *
  541. * @return the module
  542. */
  543. struct rt_dlmodule *dlmodule_find(const char *name)
  544. {
  545. rt_object_t object;
  546. struct rt_dlmodule *ret = RT_NULL;
  547. object = rt_object_find(name, RT_Object_Class_Module);
  548. if (object)
  549. {
  550. ret = (struct rt_dlmodule*) object;
  551. }
  552. return ret;
  553. }
  554. RTM_EXPORT(dlmodule_find);
  555. int list_symbols(void)
  556. {
  557. extern int __rtmsymtab_start;
  558. extern int __rtmsymtab_end;
  559. /* find in kernel symbol table */
  560. struct rt_module_symtab *index;
  561. for (index = _rt_module_symtab_begin;
  562. index != _rt_module_symtab_end;
  563. index ++)
  564. {
  565. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  566. }
  567. return 0;
  568. }
  569. MSH_CMD_EXPORT(list_symbols, list symbols information);
  570. int list_module(void)
  571. {
  572. struct rt_dlmodule *module;
  573. struct rt_list_node *list, *node;
  574. struct rt_object_information *info;
  575. info = rt_object_get_information(RT_Object_Class_Module);
  576. list = &info->object_list;
  577. rt_kprintf("module ref address \n");
  578. rt_kprintf("-------- -------- ------------\n");
  579. for (node = list->next; node != list; node = node->next)
  580. {
  581. module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
  582. rt_kprintf("%-*.*s %-04d 0x%08x\n",
  583. RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
  584. }
  585. return 0;
  586. }
  587. MSH_CMD_EXPORT(list_module, list modules in system);