dlmodule.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Copyright (c) 2006-2024 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. #ifdef RT_USING_POSIX_FS
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <sys/stat.h>
  19. #include <sys/statfs.h>
  20. #endif
  21. #define DBG_TAG "DLMD"
  22. #define DBG_LVL DBG_INFO
  23. #include <rtdbg.h> /* must after of DEBUG_ENABLE or some other options*/
  24. static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
  25. static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
  26. #if defined(__IAR_SYSTEMS_ICC__) /* for IAR compiler */
  27. #pragma section="RTMSymTab"
  28. #endif
  29. /* set the name of module */
  30. static void _dlmodule_set_name(struct rt_dlmodule *module, const char *path)
  31. {
  32. int size;
  33. struct rt_object *object;
  34. const char *first, *end, *ptr;
  35. object = &(module->parent);
  36. ptr = first = (char *)path;
  37. end = path + rt_strlen(path);
  38. while (*ptr != '\0')
  39. {
  40. if (*ptr == '/')
  41. first = ptr + 1;
  42. if (*ptr == '.')
  43. end = ptr - 1;
  44. ptr ++;
  45. }
  46. size = end - first + 1;
  47. if (size >= RT_NAME_MAX) size = RT_NAME_MAX - 1;
  48. rt_strncpy(object->name, first, size);
  49. object->name[size] = '\0';
  50. }
  51. #define RT_MODULE_ARG_MAX 8
  52. static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[])
  53. {
  54. int argc = 0;
  55. char *ptr = cmd;
  56. while ((ptr - cmd) < length)
  57. {
  58. /* strip bank and tab */
  59. while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length)
  60. *ptr++ = '\0';
  61. /* check whether it's the end of line */
  62. if ((ptr - cmd) >= length) break;
  63. /* handle string with quote */
  64. if (*ptr == '"')
  65. {
  66. argv[argc++] = ++ptr;
  67. /* skip this string */
  68. while (*ptr != '"' && (ptr - cmd) < length)
  69. if (*ptr ++ == '\\') ptr ++;
  70. if ((ptr - cmd) >= length) break;
  71. /* skip '"' */
  72. *ptr ++ = '\0';
  73. }
  74. else
  75. {
  76. argv[argc++] = ptr;
  77. while ((*ptr != ' ' && *ptr != '\t') && (ptr - cmd) < length)
  78. ptr ++;
  79. }
  80. if (argc >= RT_MODULE_ARG_MAX) break;
  81. }
  82. return argc;
  83. }
  84. /* invoked by main thread for exit */
  85. static void _dlmodule_exit(void)
  86. {
  87. struct rt_dlmodule *module;
  88. module = dlmodule_self();
  89. if (!module) return; /* not a module thread */
  90. rt_enter_critical();
  91. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  92. {
  93. struct rt_object *object = RT_NULL;
  94. struct rt_list_node *node = RT_NULL;
  95. /* set stat to closing */
  96. module->stat = RT_DLMODULE_STAT_CLOSING;
  97. /* suspend all threads in this module */
  98. for (node = module->object_list.next; node != &(module->object_list); node = node->next)
  99. {
  100. object = rt_list_entry(node, struct rt_object, list);
  101. if ((object->type & ~RT_Object_Class_Static) == RT_Object_Class_Thread)
  102. {
  103. rt_thread_t thread = (rt_thread_t)object;
  104. /* stop timer and suspend thread*/
  105. if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
  106. (RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  107. {
  108. rt_timer_stop(&(thread->thread_timer));
  109. rt_thread_suspend(thread);
  110. }
  111. }
  112. }
  113. }
  114. rt_exit_critical();
  115. return;
  116. }
  117. static void _dlmodule_thread_entry(void* parameter)
  118. {
  119. int argc = 0;
  120. char *argv[RT_MODULE_ARG_MAX];
  121. struct rt_dlmodule *module = (struct rt_dlmodule*)parameter;
  122. if (module == RT_NULL || module->cmd_line == RT_NULL)
  123. /* malloc for module_cmd_line failed. */
  124. return;
  125. if (module->cmd_line)
  126. {
  127. rt_memset(argv, 0x00, sizeof(argv));
  128. argc = _rt_module_split_arg((char *)module->cmd_line, rt_strlen(module->cmd_line), argv);
  129. if (argc == 0) goto __exit;
  130. }
  131. /* set status of module */
  132. module->stat = RT_DLMODULE_STAT_RUNNING;
  133. LOG_D("run main entry: 0x%p with %s",
  134. module->entry_addr,
  135. module->cmd_line);
  136. if (module->entry_addr)
  137. module->entry_addr(argc, argv);
  138. __exit:
  139. _dlmodule_exit();
  140. return ;
  141. }
  142. /**
  143. * @brief create a dynamic module object and initialize it.
  144. *
  145. * @return struct rt_dlmodule* If module create successfully, return the pointer to its rt_dlmodule structure.
  146. */
  147. struct rt_dlmodule *dlmodule_create(void)
  148. {
  149. struct rt_dlmodule *module = RT_NULL;
  150. module = (struct rt_dlmodule*) rt_object_allocate(RT_Object_Class_Module, "module");
  151. if (module)
  152. {
  153. module->stat = RT_DLMODULE_STAT_INIT;
  154. /* set initial priority and stack size */
  155. module->priority = RT_THREAD_PRIORITY_MAX - 1;
  156. module->stack_size = 2048;
  157. rt_list_init(&(module->object_list));
  158. }
  159. return module;
  160. }
  161. void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
  162. {
  163. RT_ASSERT(thread->parent.module_id== module);
  164. /* lock scheduler to prevent scheduling in cleanup function. */
  165. rt_enter_critical();
  166. rt_thread_close(thread);
  167. /* remove thread from thread_list (defunct thread list) */
  168. rt_list_remove(&RT_THREAD_LIST_NODE(thread));
  169. /* invoke thread cleanup */
  170. if (thread->cleanup != RT_NULL)
  171. thread->cleanup(thread);
  172. rt_exit_critical();
  173. #ifdef RT_USING_SIGNALS
  174. rt_thread_free_sig(thread);
  175. #endif
  176. if (thread->parent.type & RT_Object_Class_Static)
  177. {
  178. /* detach object */
  179. rt_object_detach((rt_object_t)thread);
  180. }
  181. #ifdef RT_USING_HEAP
  182. else
  183. {
  184. /* release thread's stack */
  185. RT_KERNEL_FREE(thread->stack_addr);
  186. /* delete thread object */
  187. rt_object_delete((rt_object_t)thread);
  188. }
  189. #endif
  190. }
  191. /**
  192. * @brief destroy dynamic module and cleanup all kernel objects inside it.
  193. *
  194. * @param module Pointer to the module to be destroyed.
  195. * @return rt_err_t On success, it returns RT_EOK. Otherwise, it returns the error code.
  196. */
  197. rt_err_t dlmodule_destroy(struct rt_dlmodule* module)
  198. {
  199. int i;
  200. RT_DEBUG_NOT_IN_INTERRUPT;
  201. /* check parameter */
  202. if (module == RT_NULL)
  203. return -RT_ERROR;
  204. /* can not destroy a running module */
  205. if (module->stat == RT_DLMODULE_STAT_RUNNING)
  206. return -RT_EBUSY;
  207. /* do module cleanup */
  208. if (module->cleanup_func)
  209. {
  210. rt_enter_critical();
  211. module->cleanup_func(module);
  212. rt_exit_critical();
  213. }
  214. /* list_object(&(module->object_list));*/
  215. /* cleanup for all kernel objects inside module*/
  216. {
  217. struct rt_object *object = RT_NULL;
  218. struct rt_list_node *node = RT_NULL;
  219. /* detach/delete all threads in this module */
  220. for (node = module->object_list.next; node != &(module->object_list); )
  221. {
  222. int object_type;
  223. object = rt_list_entry(node, struct rt_object, list);
  224. object_type = object->type & ~RT_Object_Class_Static;
  225. /* to next node */
  226. node = node->next;
  227. if (object->type & RT_Object_Class_Static)
  228. {
  229. switch (object_type)
  230. {
  231. case RT_Object_Class_Thread:
  232. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  233. break;
  234. #ifdef RT_USING_SEMAPHORE
  235. case RT_Object_Class_Semaphore:
  236. rt_sem_detach((rt_sem_t)object);
  237. break;
  238. #endif
  239. #ifdef RT_USING_MUTEX
  240. case RT_Object_Class_Mutex:
  241. rt_mutex_detach((rt_mutex_t)object);
  242. break;
  243. #endif
  244. #ifdef RT_USING_EVENT
  245. case RT_Object_Class_Event:
  246. rt_event_detach((rt_event_t)object);
  247. break;
  248. #endif
  249. #ifdef RT_USING_MAILBOX
  250. case RT_Object_Class_MailBox:
  251. rt_mb_detach((rt_mailbox_t)object);
  252. break;
  253. #endif
  254. #ifdef RT_USING_MESSAGEQUEUE
  255. case RT_Object_Class_MessageQueue:
  256. rt_mq_detach((rt_mq_t)object);
  257. break;
  258. #endif
  259. #ifdef RT_USING_MEMHEAP
  260. case RT_Object_Class_MemHeap:
  261. rt_memheap_detach((struct rt_memheap*)object);
  262. break;
  263. #endif
  264. #ifdef RT_USING_MEMPOOL
  265. case RT_Object_Class_MemPool:
  266. rt_mp_detach((struct rt_mempool*)object);
  267. break;
  268. #endif
  269. case RT_Object_Class_Timer:
  270. rt_timer_detach((rt_timer_t)object);
  271. break;
  272. default:
  273. LOG_E("Unsupported oject type in module.");
  274. break;
  275. }
  276. }
  277. else
  278. {
  279. switch (object_type)
  280. {
  281. case RT_Object_Class_Thread:
  282. dlmodule_destroy_subthread(module, (rt_thread_t)object);
  283. break;
  284. #ifdef RT_USING_SEMAPHORE
  285. case RT_Object_Class_Semaphore:
  286. rt_sem_delete((rt_sem_t)object);
  287. break;
  288. #endif
  289. #ifdef RT_USING_MUTEX
  290. case RT_Object_Class_Mutex:
  291. rt_mutex_delete((rt_mutex_t)object);
  292. break;
  293. #endif
  294. #ifdef RT_USING_EVENT
  295. case RT_Object_Class_Event:
  296. rt_event_delete((rt_event_t)object);
  297. break;
  298. #endif
  299. #ifdef RT_USING_MAILBOX
  300. case RT_Object_Class_MailBox:
  301. rt_mb_delete((rt_mailbox_t)object);
  302. break;
  303. #endif
  304. #ifdef RT_USING_MESSAGEQUEUE
  305. case RT_Object_Class_MessageQueue:
  306. rt_mq_delete((rt_mq_t)object);
  307. break;
  308. #endif
  309. #ifdef RT_USING_MEMHEAP
  310. /* no delete operation */
  311. #endif
  312. #ifdef RT_USING_MEMPOOL
  313. case RT_Object_Class_MemPool:
  314. rt_mp_delete((struct rt_mempool*)object);
  315. break;
  316. #endif
  317. case RT_Object_Class_Timer:
  318. rt_timer_delete((rt_timer_t)object);
  319. break;
  320. default:
  321. LOG_E("Unsupported oject type in module.");
  322. break;
  323. }
  324. }
  325. }
  326. }
  327. if (module->cmd_line) rt_free(module->cmd_line);
  328. /* release module symbol table */
  329. for (i = 0; i < module->nsym; i ++)
  330. {
  331. rt_free((void *)module->symtab[i].name);
  332. }
  333. if (module->symtab != RT_NULL)
  334. {
  335. rt_free(module->symtab);
  336. }
  337. /* destory module */
  338. rt_free(module->mem_space);
  339. /* delete module object */
  340. rt_object_delete((rt_object_t)module);
  341. return RT_EOK;
  342. }
  343. /**
  344. * @brief retrieve the dynamically loaded module that the current thread belongs to.
  345. *
  346. * @return struct rt_dlmodule* On success, it returns a pointer to the module. otherwise, it returns RT_NULL.
  347. */
  348. struct rt_dlmodule *dlmodule_self(void)
  349. {
  350. rt_thread_t tid;
  351. struct rt_dlmodule *ret = RT_NULL;
  352. tid = rt_thread_self();
  353. if (tid)
  354. {
  355. ret = (struct rt_dlmodule*) tid->parent.module_id;
  356. }
  357. return ret;
  358. }
  359. /*
  360. * Compatible with old API
  361. */
  362. struct rt_dlmodule *rt_module_self(void)
  363. {
  364. return dlmodule_self();
  365. }
  366. /**
  367. * @brief load an ELF module to memory.
  368. *
  369. * @param filename the path to the module to load.
  370. * @return struct rt_dlmodule* On success, it returns a pointer to the module object. otherwise, RT_NULL is returned.
  371. *
  372. * @note the function is used to load an ELF (Executable and Linkable Format) module from a file, validate it,
  373. * and initialize it as a dynamically loaded module. what it implements are as follows:
  374. * 1. Load and Validate ELF: It loads an ELF file, checks its validity, and identifies it as either a relocatable or shared object.
  375. * 2. Memory Allocation and Cleanup: Uses rt_malloc and rt_free to allocate and free memory for module data.
  376. * Error handling ensures all resources are released if an error occurs.
  377. * 3. Symbol Resolution and Initialization: Sets up init function and cleanup function, and calls the module_init function if it is present.
  378. * 4. Cache Management: Optionally (when RT_USING_CACHE defined) flushes data and invalidates instruction caches to ensure the module is correctly loaded into memory.
  379. */
  380. struct rt_dlmodule* dlmodule_load(const char* filename)
  381. {
  382. #ifdef RT_USING_POSIX_FS
  383. int fd = -1, length = 0;
  384. #endif
  385. rt_err_t ret = RT_EOK;
  386. rt_uint8_t *module_ptr = RT_NULL;
  387. struct rt_dlmodule *module = RT_NULL;
  388. #ifdef RT_USING_POSIX_FS
  389. fd = open(filename, O_RDONLY, 0);
  390. if (fd >= 0)
  391. {
  392. length = lseek(fd, 0, SEEK_END);
  393. lseek(fd, 0, SEEK_SET);
  394. if (length == 0) goto __exit;
  395. module_ptr = (uint8_t*) rt_malloc (length);
  396. if (!module_ptr) goto __exit;
  397. if (read(fd, module_ptr, length) != length)
  398. goto __exit;
  399. /* close file and release fd */
  400. close(fd);
  401. fd = -1;
  402. }
  403. else
  404. {
  405. goto __exit;
  406. }
  407. #endif
  408. if (!module_ptr) goto __exit;
  409. /* check ELF header */
  410. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  411. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  412. {
  413. rt_kprintf("Module: magic error\n");
  414. goto __exit;
  415. }
  416. /* check ELF class */
  417. if ((elf_module->e_ident[EI_CLASS] != ELFCLASS32)&&(elf_module->e_ident[EI_CLASS] != ELFCLASS64))
  418. {
  419. rt_kprintf("Module: ELF class error\n");
  420. goto __exit;
  421. }
  422. module = dlmodule_create();
  423. if (!module) goto __exit;
  424. /* set the name of module */
  425. _dlmodule_set_name(module, filename);
  426. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  427. if (elf_module->e_type == ET_REL)
  428. {
  429. ret = dlmodule_load_relocated_object(module, module_ptr);
  430. }
  431. else if (elf_module->e_type == ET_DYN)
  432. {
  433. ret = dlmodule_load_shared_object(module, module_ptr);
  434. }
  435. else
  436. {
  437. rt_kprintf("Module: unsupported elf type\n");
  438. goto __exit;
  439. }
  440. /* check return value */
  441. if (ret != RT_EOK) goto __exit;
  442. /* release module data */
  443. rt_free(module_ptr);
  444. /* increase module reference count */
  445. module->nref ++;
  446. /* deal with cache */
  447. #ifdef RT_USING_CACHE
  448. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  449. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  450. #endif
  451. /* set module initialization and cleanup function */
  452. module->init_func = dlsym(module, "module_init");
  453. module->cleanup_func = dlsym(module, "module_cleanup");
  454. module->stat = RT_DLMODULE_STAT_INIT;
  455. /* do module initialization */
  456. if (module->init_func)
  457. {
  458. module->init_func(module);
  459. }
  460. return module;
  461. __exit:
  462. #ifdef RT_USING_POSIX_FS
  463. if (fd >= 0) close(fd);
  464. #endif
  465. if (module_ptr) rt_free(module_ptr);
  466. if (module) dlmodule_destroy(module);
  467. return RT_NULL;
  468. }
  469. /**
  470. * @brief load a dynamic module, and create a thread to excute the module main function.
  471. *
  472. * @param pgname path of the module to be loaded.
  473. * @param cmd the command string (with commandline options) for startup module.
  474. * @param cmd_size the command's length.
  475. * @return struct rt_dlmodule* On success, it returns a pointer to the module object. otherwise, RT_NULL is returned.
  476. */
  477. struct rt_dlmodule* dlmodule_exec(const char* pgname, const char* cmd, int cmd_size)
  478. {
  479. struct rt_dlmodule *module = RT_NULL;
  480. module = dlmodule_load(pgname);
  481. if (module)
  482. {
  483. if (module->entry_addr)
  484. {
  485. /* exec this module */
  486. rt_thread_t tid;
  487. module->cmd_line = rt_strdup(cmd);
  488. /* check stack size and priority */
  489. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  490. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  491. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  492. module->stack_size, module->priority, 10);
  493. if (tid)
  494. {
  495. tid->parent.module_id= module;
  496. module->main_thread = tid;
  497. rt_thread_startup(tid);
  498. }
  499. else
  500. {
  501. /* destory dl module */
  502. dlmodule_destroy(module);
  503. module = RT_NULL;
  504. }
  505. }
  506. }
  507. return module;
  508. }
  509. #if defined(RT_USING_CUSTOM_DLMODULE)
  510. struct rt_dlmodule* dlmodule_load_custom(const char* filename, struct rt_dlmodule_ops* ops)
  511. {
  512. #ifdef RT_USING_POSIX_FS
  513. int fd = -1, length = 0;
  514. #endif
  515. rt_err_t ret = RT_EOK;
  516. rt_uint8_t *module_ptr = RT_NULL;
  517. struct rt_dlmodule *module = RT_NULL;
  518. if (ops)
  519. {
  520. RT_ASSERT(ops->load);
  521. RT_ASSERT(ops->unload);
  522. module_ptr = ops->load(filename);
  523. }
  524. #ifdef RT_USING_POSIX_FS
  525. else
  526. {
  527. fd = open(filename, O_RDONLY, 0);
  528. if (fd >= 0)
  529. {
  530. length = lseek(fd, 0, SEEK_END);
  531. lseek(fd, 0, SEEK_SET);
  532. if (length == 0) goto __exit;
  533. module_ptr = (uint8_t*) rt_malloc (length);
  534. if (!module_ptr) goto __exit;
  535. if (read(fd, module_ptr, length) != length)
  536. goto __exit;
  537. /* close file and release fd */
  538. close(fd);
  539. fd = -1;
  540. }
  541. else
  542. {
  543. goto __exit;
  544. }
  545. }
  546. #endif
  547. if (!module_ptr) goto __exit;
  548. /* check ELF header */
  549. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  550. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  551. {
  552. rt_kprintf("Module: magic error\n");
  553. goto __exit;
  554. }
  555. /* check ELF class */
  556. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  557. {
  558. rt_kprintf("Module: ELF class error\n");
  559. goto __exit;
  560. }
  561. module = dlmodule_create();
  562. if (!module) goto __exit;
  563. /* set the name of module */
  564. _dlmodule_set_name(module, filename);
  565. LOG_D("rt_module_load: %.*s", RT_NAME_MAX, module->parent.name);
  566. if (elf_module->e_type == ET_REL)
  567. {
  568. ret = dlmodule_load_relocated_object(module, module_ptr);
  569. }
  570. else if (elf_module->e_type == ET_DYN)
  571. {
  572. ret = dlmodule_load_shared_object(module, module_ptr);
  573. }
  574. else
  575. {
  576. rt_kprintf("Module: unsupported elf type\n");
  577. goto __exit;
  578. }
  579. /* check return value */
  580. if (ret != RT_EOK) goto __exit;
  581. /* release module data */
  582. if (ops)
  583. {
  584. ops->unload(module_ptr);
  585. }
  586. else
  587. {
  588. rt_free(module_ptr);
  589. }
  590. /* increase module reference count */
  591. module->nref ++;
  592. /* deal with cache */
  593. #ifdef RT_USING_CACHE
  594. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, module->mem_space, module->mem_size);
  595. rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, module->mem_space, module->mem_size);
  596. #endif
  597. /* set module initialization and cleanup function */
  598. module->init_func = dlsym(module, "module_init");
  599. module->cleanup_func = dlsym(module, "module_cleanup");
  600. module->stat = RT_DLMODULE_STAT_INIT;
  601. /* do module initialization */
  602. if (module->init_func)
  603. {
  604. module->init_func(module);
  605. }
  606. return module;
  607. __exit:
  608. #ifdef RT_USING_POSIX_FS
  609. if (fd >= 0) close(fd);
  610. #endif
  611. if (module_ptr)
  612. {
  613. if (ops)
  614. {
  615. ops->unload(module_ptr);
  616. }
  617. else
  618. {
  619. rt_free(module_ptr);
  620. }
  621. }
  622. if (module) dlmodule_destroy(module);
  623. return RT_NULL;
  624. }
  625. struct rt_dlmodule* dlmodule_exec_custom(const char* pgname, const char* cmd, int cmd_size, struct rt_dlmodule_ops* ops)
  626. {
  627. struct rt_dlmodule *module = RT_NULL;
  628. module = dlmodule_load_custom(pgname, ops);
  629. if (module)
  630. {
  631. if (module->entry_addr)
  632. {
  633. /* exec this module */
  634. rt_thread_t tid;
  635. module->cmd_line = rt_strdup(cmd);
  636. /* check stack size and priority */
  637. if (module->priority > RT_THREAD_PRIORITY_MAX) module->priority = RT_THREAD_PRIORITY_MAX - 1;
  638. if (module->stack_size < 2048 || module->stack_size > (1024 * 32)) module->stack_size = 2048;
  639. tid = rt_thread_create(module->parent.name, _dlmodule_thread_entry, (void*)module,
  640. module->stack_size, module->priority, 10);
  641. if (tid)
  642. {
  643. tid->parent.module_id= module;
  644. module->main_thread = tid;
  645. rt_thread_startup(tid);
  646. }
  647. else
  648. {
  649. /* destory dl module */
  650. dlmodule_destroy(module);
  651. module = RT_NULL;
  652. }
  653. }
  654. }
  655. return module;
  656. }
  657. #endif
  658. /**
  659. * @brief exit a dynamically loaded module.
  660. *
  661. * @param ret_code the return code for module exit.
  662. *
  663. * @note this function is responsible for gracefully exiting a dynamically loaded module, releasing resources associated with the module,
  664. * and handling cleanup operations. what it implements are as follows:
  665. * 1. Thread and Resource Cleanup: The function safely exits a module by deleting its main thread and freeing resources associated with it.
  666. * 2. Status Management: Checks and updates the module's state, setting a return code and calling _dlmodule_exit() to transition to a closing state.
  667. * 3. Critical Sections: Critical sections ensure that the exit process is atomic and free from race conditions.
  668. */
  669. void dlmodule_exit(int ret_code)
  670. {
  671. rt_thread_t thread;
  672. struct rt_dlmodule *module;
  673. module = dlmodule_self();
  674. if (!module) return;
  675. /* disable scheduling */
  676. rt_enter_critical();
  677. /* module is not running */
  678. if (module->stat != RT_DLMODULE_STAT_RUNNING)
  679. {
  680. /* restore scheduling */
  681. rt_exit_critical();
  682. return;
  683. }
  684. /* set return code */
  685. module->ret_code = ret_code;
  686. /* do exit for this module */
  687. _dlmodule_exit();
  688. /* the stat of module was changed to CLOSING in _dlmodule_exit */
  689. thread = module->main_thread;
  690. if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  691. {
  692. /* main thread already closed */
  693. rt_exit_critical();
  694. return ;
  695. }
  696. /* delete thread: insert to defunct thread list */
  697. rt_thread_delete(thread);
  698. /* enable scheduling */
  699. rt_exit_critical();
  700. }
  701. /**
  702. * @brief search for a symbol by its name in the kernel symbol table.
  703. *
  704. * @param sym_str the symbol name string.
  705. * @return rt_ubase_t On success, it returns the address of the symbol.
  706. * Otherwise, it returns 0 (indicating the symbol was not found).
  707. */
  708. rt_ubase_t dlmodule_symbol_find(const char *sym_str)
  709. {
  710. /* find in kernel symbol table */
  711. struct rt_module_symtab *index;
  712. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  713. {
  714. if (rt_strcmp(index->name, sym_str) == 0)
  715. return (rt_ubase_t)index->addr;
  716. }
  717. return 0;
  718. }
  719. int rt_system_dlmodule_init(void)
  720. {
  721. #if defined(__GNUC__) && !defined(__CC_ARM)
  722. extern int __rtmsymtab_start;
  723. extern int __rtmsymtab_end;
  724. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  725. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  726. #elif defined (__CC_ARM)
  727. extern int RTMSymTab$$Base;
  728. extern int RTMSymTab$$Limit;
  729. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  730. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  731. #elif defined (__IAR_SYSTEMS_ICC__)
  732. _rt_module_symtab_begin = __section_begin("RTMSymTab");
  733. _rt_module_symtab_end = __section_end("RTMSymTab");
  734. #endif
  735. return 0;
  736. }
  737. INIT_COMPONENT_EXPORT(rt_system_dlmodule_init);
  738. /**
  739. * This function will find the specified module.
  740. *
  741. * @param name the name of module finding
  742. *
  743. * @return the module
  744. */
  745. struct rt_dlmodule *dlmodule_find(const char *name)
  746. {
  747. rt_object_t object;
  748. struct rt_dlmodule *ret = RT_NULL;
  749. object = rt_object_find(name, RT_Object_Class_Module);
  750. if (object)
  751. {
  752. ret = (struct rt_dlmodule*) object;
  753. }
  754. return ret;
  755. }
  756. RTM_EXPORT(dlmodule_find);
  757. int list_symbols(void)
  758. {
  759. extern int __rtmsymtab_start;
  760. extern int __rtmsymtab_end;
  761. /* find in kernel symbol table */
  762. struct rt_module_symtab *index;
  763. for (index = _rt_module_symtab_begin;
  764. index != _rt_module_symtab_end;
  765. index ++)
  766. {
  767. rt_kprintf("%s => 0x%08x\n", index->name, index->addr);
  768. }
  769. return 0;
  770. }
  771. MSH_CMD_EXPORT(list_symbols, list symbols information);
  772. int list_module(void)
  773. {
  774. struct rt_dlmodule *module;
  775. struct rt_list_node *list, *node;
  776. struct rt_object_information *info;
  777. info = rt_object_get_information(RT_Object_Class_Module);
  778. list = &info->object_list;
  779. rt_kprintf("module ref address \n");
  780. rt_kprintf("-------- -------- ------------\n");
  781. for (node = list->next; node != list; node = node->next)
  782. {
  783. module = (struct rt_dlmodule *)(rt_list_entry(node, struct rt_object, list));
  784. rt_kprintf("%-*.*s %-04d 0x%08x\n",
  785. RT_NAME_MAX, RT_NAME_MAX, module->parent.name, module->nref, module->mem_space);
  786. }
  787. return 0;
  788. }
  789. MSH_CMD_EXPORT(list_module, list modules in system);