dlmodule.c 18 KB

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