module.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. /*
  2. * File : module.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-01-09 Bernard first version
  13. * 2010-04-09 yi.qiu implement based on first version
  14. * 2010-10-23 yi.qiu implement module memory allocator
  15. * 2011-05-25 yi.qiu implement module hook function
  16. * 2011-06-23 yi.qiu rewrite module memory allocator
  17. * 2012-11-23 Bernard using RT_DEBUG_LOG instead of rt_kprintf.
  18. * 2012-11-28 Bernard remove rt_current_module and user
  19. * can use rt_module_unload to remove a module.
  20. */
  21. #include <rthw.h>
  22. #include <rtthread.h>
  23. #include <rtm.h>
  24. #ifdef RT_USING_MODULE
  25. #include "module.h"
  26. #define elf_module ((Elf32_Ehdr *)module_ptr)
  27. #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
  28. #define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
  29. #define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
  30. #define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
  31. #define IS_REL(s) (s.sh_type == SHT_REL)
  32. #define IS_RELA(s) (s.sh_type == SHT_RELA)
  33. #define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
  34. #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
  35. #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
  36. #ifdef RT_USING_SLAB
  37. #define PAGE_COUNT_MAX 256
  38. /* module memory allocator */
  39. struct rt_mem_head
  40. {
  41. rt_size_t size; /* size of memory block */
  42. struct rt_mem_head *next; /* next valid memory block */
  43. };
  44. struct rt_page_info
  45. {
  46. rt_uint32_t *page_ptr;
  47. rt_uint32_t npage;
  48. };
  49. static void *rt_module_malloc_page(rt_size_t npages);
  50. static void rt_module_free_page(rt_module_t module,
  51. void *page_ptr,
  52. rt_size_t npages);
  53. static struct rt_semaphore mod_sem;
  54. #endif
  55. static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
  56. static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
  57. /**
  58. * @ingroup SystemInit
  59. *
  60. * This function will initialize system module
  61. */
  62. void rt_system_module_init(void)
  63. {
  64. #ifdef __GNUC__
  65. extern int __rtmsymtab_start;
  66. extern int __rtmsymtab_end;
  67. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  68. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  69. #elif defined (__CC_ARM)
  70. extern int RTMSymTab$$Base;
  71. extern int RTMSymTab$$Limit;
  72. _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
  73. _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit;
  74. #endif
  75. #ifdef RT_USING_SLAB
  76. /* initialize heap semaphore */
  77. rt_sem_init(&mod_sem, "module", 1, RT_IPC_FLAG_FIFO);
  78. #endif
  79. }
  80. static rt_uint32_t rt_module_symbol_find(const char *sym_str)
  81. {
  82. /* find in kernel symbol table */
  83. struct rt_module_symtab *index;
  84. for (index = _rt_module_symtab_begin;
  85. index != _rt_module_symtab_end;
  86. index ++)
  87. {
  88. if (rt_strcmp(index->name, sym_str) == 0)
  89. return (rt_uint32_t)index->addr;
  90. }
  91. return 0;
  92. }
  93. /**
  94. * This function will return self module object
  95. *
  96. * @return the self module object
  97. */
  98. rt_module_t rt_module_self(void)
  99. {
  100. rt_thread_t tid;
  101. tid = rt_thread_self();
  102. if (tid == RT_NULL)
  103. return RT_NULL;
  104. /* return current module */
  105. return (rt_module_t)tid->module_id;
  106. }
  107. static int rt_module_arm_relocate(struct rt_module *module,
  108. Elf32_Rel *rel,
  109. Elf32_Addr sym_val)
  110. {
  111. Elf32_Addr *where, tmp;
  112. Elf32_Sword addend, offset;
  113. rt_uint32_t upper, lower, sign, j1, j2;
  114. where = (Elf32_Addr *)((rt_uint8_t *)module->module_space + rel->r_offset);
  115. switch (ELF32_R_TYPE(rel->r_info))
  116. {
  117. case R_ARM_NONE:
  118. break;
  119. case R_ARM_ABS32:
  120. *where += (Elf32_Addr)sym_val;
  121. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n",
  122. where, *where));
  123. break;
  124. case R_ARM_PC24:
  125. case R_ARM_PLT32:
  126. case R_ARM_CALL:
  127. case R_ARM_JUMP24:
  128. addend = *where & 0x00ffffff;
  129. if (addend & 0x00800000)
  130. addend |= 0xff000000;
  131. tmp = sym_val - (Elf32_Addr)where + (addend << 2);
  132. tmp >>= 2;
  133. *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
  134. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n",
  135. where, *where));
  136. break;
  137. case R_ARM_REL32:
  138. *where += sym_val - (Elf32_Addr)where;
  139. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  140. ("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
  141. where, *where, sym_val, rel->r_offset));
  142. break;
  143. case R_ARM_V4BX:
  144. *where &= 0xf000000f;
  145. *where |= 0x01a0f000;
  146. break;
  147. case R_ARM_GLOB_DAT:
  148. case R_ARM_JUMP_SLOT:
  149. *where = (Elf32_Addr)sym_val;
  150. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n",
  151. where, *where, sym_val));
  152. break;
  153. #if 0 /* To do */
  154. case R_ARM_GOT_BREL:
  155. temp = (Elf32_Addr)sym_val;
  156. *where = (Elf32_Addr)&temp;
  157. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x\n",
  158. where, *where, sym_val));
  159. break;
  160. #endif
  161. case R_ARM_RELATIVE:
  162. *where = (Elf32_Addr)sym_val + *where;
  163. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n",
  164. where, *where, sym_val));
  165. break;
  166. case R_ARM_THM_CALL:
  167. case R_ARM_THM_JUMP24:
  168. upper = *(rt_uint16_t *)where;
  169. lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
  170. sign = (upper >> 10) & 1;
  171. j1 = (lower >> 13) & 1;
  172. j2 = (lower >> 11) & 1;
  173. offset = (sign << 24) |
  174. ((~(j1 ^ sign) & 1) << 23) |
  175. ((~(j2 ^ sign) & 1) << 22) |
  176. ((upper & 0x03ff) << 12) |
  177. ((lower & 0x07ff) << 1);
  178. if (offset & 0x01000000)
  179. offset -= 0x02000000;
  180. offset += sym_val - (Elf32_Addr)where;
  181. if (!(offset & 1) ||
  182. offset <= (rt_int32_t)0xff000000 ||
  183. offset >= (rt_int32_t)0x01000000)
  184. {
  185. rt_kprintf("Module: Only Thumb addresses allowed\n");
  186. return -1;
  187. }
  188. sign = (offset >> 24) & 1;
  189. j1 = sign ^ (~(offset >> 23) & 1);
  190. j2 = sign ^ (~(offset >> 22) & 1);
  191. *(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) |
  192. (sign << 10) |
  193. ((offset >> 12) & 0x03ff));
  194. *(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
  195. (j1 << 13) | (j2 << 11) |
  196. ((offset >> 1) & 0x07ff));
  197. upper = *(rt_uint16_t *)where;
  198. lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
  199. break;
  200. default:
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. static void rt_module_init_object_container(struct rt_module *module)
  206. {
  207. RT_ASSERT(module != RT_NULL);
  208. /* initialize object container - thread */
  209. rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
  210. module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
  211. module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;
  212. #ifdef RT_USING_SEMAPHORE
  213. /* initialize object container - semaphore */
  214. rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
  215. module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
  216. module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
  217. #endif
  218. #ifdef RT_USING_MUTEX
  219. /* initialize object container - mutex */
  220. rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
  221. module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
  222. module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
  223. #endif
  224. #ifdef RT_USING_EVENT
  225. /* initialize object container - event */
  226. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  227. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  228. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  229. #endif
  230. #ifdef RT_USING_MAILBOX
  231. /* initialize object container - mailbox */
  232. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  233. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  234. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  235. #endif
  236. #ifdef RT_USING_MESSAGEQUEUE
  237. /* initialize object container - message queue */
  238. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  239. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  240. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  241. #endif
  242. #ifdef RT_USING_MEMHEAP
  243. /* initialize object container - memory heap */
  244. rt_list_init(&(module->module_object[RT_Object_Class_MemHeap].object_list));
  245. module->module_object[RT_Object_Class_MemHeap].object_size = sizeof(struct rt_memheap);
  246. module->module_object[RT_Object_Class_MemHeap].type = RT_Object_Class_MemHeap;
  247. #endif
  248. #ifdef RT_USING_MEMPOOL
  249. /* initialize object container - memory pool */
  250. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  251. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  252. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  253. #endif
  254. #ifdef RT_USING_DEVICE
  255. /* initialize object container - device */
  256. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  257. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  258. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  259. #endif
  260. /* initialize object container - timer */
  261. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  262. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  263. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  264. }
  265. #ifdef RT_USING_HOOK
  266. static void (*rt_module_load_hook)(rt_module_t module);
  267. static void (*rt_module_unload_hook)(rt_module_t module);
  268. /**
  269. * @addtogroup Hook
  270. */
  271. /*@{*/
  272. /**
  273. * This function will set a hook function, which will be invoked when module
  274. * be loaded to system.
  275. *
  276. * @param hook the hook function
  277. */
  278. void rt_module_load_sethook(void (*hook)(rt_module_t module))
  279. {
  280. rt_module_load_hook = hook;
  281. }
  282. /**
  283. * This function will set a hook function, which will be invoked when module
  284. * be unloaded from system.
  285. *
  286. * @param hook the hook function
  287. */
  288. void rt_module_unload_sethook(void (*hook)(rt_module_t module))
  289. {
  290. rt_module_unload_hook = hook;
  291. }
  292. /*@}*/
  293. #endif
  294. static struct rt_module *_load_shared_object(const char *name,
  295. void *module_ptr)
  296. {
  297. rt_uint8_t *ptr = RT_NULL;
  298. rt_module_t module = RT_NULL;
  299. rt_bool_t linked = RT_FALSE;
  300. rt_uint32_t index, module_size = 0;
  301. RT_ASSERT(module_ptr != RT_NULL);
  302. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
  303. {
  304. /* rtmlinker finished */
  305. linked = RT_TRUE;
  306. }
  307. /* get the ELF image size */
  308. for (index = 0; index < elf_module->e_phnum; index++)
  309. {
  310. if (phdr[index].p_type == PT_LOAD)
  311. module_size += phdr[index].p_memsz;
  312. }
  313. if (module_size == 0)
  314. {
  315. rt_kprintf("Module: size error\n");
  316. return RT_NULL;
  317. }
  318. /* allocate module */
  319. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
  320. name);
  321. if (!module)
  322. return RT_NULL;
  323. module->nref = 0;
  324. /* allocate module space */
  325. module->module_space = rt_malloc(module_size);
  326. if (module->module_space == RT_NULL)
  327. {
  328. rt_kprintf("Module: allocate space failed.\n");
  329. rt_object_delete(&(module->parent));
  330. return RT_NULL;
  331. }
  332. /* zero all space */
  333. ptr = module->module_space;
  334. rt_memset(ptr, 0, module_size);
  335. for (index = 0; index < elf_module->e_phnum; index++)
  336. {
  337. if (phdr[index].p_type == PT_LOAD)
  338. {
  339. rt_memcpy(ptr + phdr[index].p_paddr,
  340. (rt_uint8_t *)elf_module + phdr[index].p_offset,
  341. phdr[index].p_filesz);
  342. }
  343. }
  344. /* set module entry */
  345. module->module_entry = module->module_space + elf_module->e_entry;
  346. /* handle relocation section */
  347. for (index = 0; index < elf_module->e_shnum; index ++)
  348. {
  349. rt_uint32_t i, nr_reloc;
  350. Elf32_Sym *symtab;
  351. Elf32_Rel *rel;
  352. rt_uint8_t *strtab;
  353. static rt_bool_t unsolved = RT_FALSE;
  354. if (!IS_REL(shdr[index]))
  355. continue;
  356. /* get relocate item */
  357. rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  358. /* locate .rel.plt and .rel.dyn section */
  359. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
  360. shdr[shdr[index].sh_link].sh_offset);
  361. strtab = (rt_uint8_t *)module_ptr +
  362. shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  363. nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
  364. /* relocate every items */
  365. for (i = 0; i < nr_reloc; i ++)
  366. {
  367. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  368. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n",
  369. strtab + sym->st_name,
  370. sym->st_shndx));
  371. if ((sym->st_shndx != SHT_NULL) ||
  372. (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
  373. {
  374. rt_module_arm_relocate(module, rel,
  375. (Elf32_Addr)(module->module_space + sym->st_value));
  376. }
  377. else if (!linked)
  378. {
  379. Elf32_Addr addr;
  380. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  381. strtab + sym->st_name));
  382. /* need to resolve symbol in kernel symbol table */
  383. addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
  384. if (addr == 0)
  385. {
  386. rt_kprintf("Module: can't find %s in kernel symbol table\n",
  387. strtab + sym->st_name);
  388. unsolved = RT_TRUE;
  389. }
  390. else
  391. rt_module_arm_relocate(module, rel, addr);
  392. }
  393. rel ++;
  394. }
  395. if (unsolved)
  396. {
  397. rt_object_delete(&(module->parent));
  398. return RT_NULL;
  399. }
  400. }
  401. /* construct module symbol table */
  402. for (index = 0; index < elf_module->e_shnum; index ++)
  403. {
  404. /* find .dynsym section */
  405. rt_uint8_t *shstrab;
  406. shstrab = (rt_uint8_t *)module_ptr +
  407. shdr[elf_module->e_shstrndx].sh_offset;
  408. if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
  409. break;
  410. }
  411. /* found .dynsym section */
  412. if (index != elf_module->e_shnum)
  413. {
  414. int i, count = 0;
  415. Elf32_Sym *symtab = RT_NULL;
  416. rt_uint8_t *strtab = RT_NULL;
  417. symtab =(Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  418. strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
  419. for (i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
  420. {
  421. if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
  422. (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
  423. count ++;
  424. }
  425. module->symtab = (struct rt_module_symtab *)rt_malloc
  426. (count * sizeof(struct rt_module_symtab));
  427. module->nsym = count;
  428. for (i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
  429. {
  430. rt_size_t length;
  431. if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
  432. (ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC))
  433. continue;
  434. length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
  435. module->symtab[count].addr =
  436. (void *)(module->module_space + symtab[i].st_value);
  437. module->symtab[count].name = rt_malloc(length);
  438. rt_memset((void *)module->symtab[count].name, 0, length);
  439. rt_memcpy((void *)module->symtab[count].name,
  440. strtab + symtab[i].st_name,
  441. length);
  442. count ++;
  443. }
  444. }
  445. return module;
  446. }
  447. static struct rt_module* _load_relocated_object(const char *name,
  448. void *module_ptr)
  449. {
  450. rt_uint32_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
  451. rt_uint32_t module_addr = 0, module_size = 0;
  452. struct rt_module *module = RT_NULL;
  453. rt_uint8_t *ptr, *strtab, *shstrab;
  454. /* get the ELF image size */
  455. for (index = 0; index < elf_module->e_shnum; index ++)
  456. {
  457. /* text */
  458. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  459. {
  460. module_size += shdr[index].sh_size;
  461. module_addr = shdr[index].sh_addr;
  462. }
  463. /* rodata */
  464. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  465. {
  466. module_size += shdr[index].sh_size;
  467. }
  468. /* data */
  469. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  470. {
  471. module_size += shdr[index].sh_size;
  472. }
  473. /* bss */
  474. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  475. {
  476. module_size += shdr[index].sh_size;
  477. }
  478. }
  479. /* no text, data and bss on image */
  480. if (module_size == 0)
  481. return RT_NULL;
  482. /* allocate module */
  483. module = (struct rt_module *)
  484. rt_object_allocate(RT_Object_Class_Module, (const char *)name);
  485. if (module == RT_NULL)
  486. return RT_NULL;
  487. /* allocate module space */
  488. module->module_space = rt_malloc(module_size);
  489. if (module->module_space == RT_NULL)
  490. {
  491. rt_kprintf("Module: allocate space failed.\n");
  492. rt_object_delete(&(module->parent));
  493. return RT_NULL;
  494. }
  495. /* zero all space */
  496. ptr = module->module_space;
  497. rt_memset(ptr, 0, module_size);
  498. /* load text and data section */
  499. for (index = 0; index < elf_module->e_shnum; index ++)
  500. {
  501. /* load text section */
  502. if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
  503. {
  504. rt_memcpy(ptr,
  505. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  506. shdr[index].sh_size);
  507. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load text 0x%x, size %d\n",
  508. ptr, shdr[index].sh_size));
  509. ptr += shdr[index].sh_size;
  510. }
  511. /* load rodata section */
  512. if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
  513. {
  514. rt_memcpy(ptr,
  515. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  516. shdr[index].sh_size);
  517. rodata_addr = (rt_uint32_t)ptr;
  518. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  519. ("load rodata 0x%x, size %d, rodata 0x%x\n",
  520. ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
  521. ptr += shdr[index].sh_size;
  522. }
  523. /* load data section */
  524. if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
  525. {
  526. rt_memcpy(ptr,
  527. (rt_uint8_t *)elf_module + shdr[index].sh_offset,
  528. shdr[index].sh_size);
  529. data_addr = (rt_uint32_t)ptr;
  530. RT_DEBUG_LOG(RT_DEBUG_MODULE,
  531. ("load data 0x%x, size %d, data 0x%x\n",
  532. ptr, shdr[index].sh_size, *(rt_uint32_t *)data_addr));
  533. ptr += shdr[index].sh_size;
  534. }
  535. /* load bss section */
  536. if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
  537. {
  538. rt_memset(ptr, 0, shdr[index].sh_size);
  539. bss_addr = (rt_uint32_t)ptr;
  540. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("load bss 0x%x, size %d,\n",
  541. ptr, shdr[index].sh_size));
  542. }
  543. }
  544. /* set module entry */
  545. module->module_entry =
  546. (rt_uint8_t *)module->module_space + elf_module->e_entry - module_addr;
  547. /* handle relocation section */
  548. for (index = 0; index < elf_module->e_shnum; index ++)
  549. {
  550. rt_uint32_t i, nr_reloc;
  551. Elf32_Sym *symtab;
  552. Elf32_Rel *rel;
  553. if (!IS_REL(shdr[index]))
  554. continue;
  555. /* get relocate item */
  556. rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
  557. /* locate .dynsym and .dynstr */
  558. symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr +
  559. shdr[shdr[index].sh_link].sh_offset);
  560. strtab = (rt_uint8_t *)module_ptr +
  561. shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  562. shstrab = (rt_uint8_t *)module_ptr +
  563. shdr[elf_module->e_shstrndx].sh_offset;
  564. nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
  565. /* relocate every items */
  566. for (i = 0; i < nr_reloc; i ++)
  567. {
  568. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  569. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  570. strtab + sym->st_name));
  571. if (sym->st_shndx != STN_UNDEF)
  572. {
  573. if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) ||
  574. (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
  575. {
  576. if (rt_strncmp((const char *)(shstrab +
  577. shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
  578. {
  579. /* relocate rodata section */
  580. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rodata\n"));
  581. rt_module_arm_relocate(module, rel,
  582. (Elf32_Addr)(rodata_addr + sym->st_value));
  583. }
  584. else if (rt_strncmp((const char*)
  585. (shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
  586. {
  587. /* relocate bss section */
  588. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("bss\n"));
  589. rt_module_arm_relocate(module, rel,
  590. (Elf32_Addr)bss_addr + sym->st_value);
  591. }
  592. else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name),
  593. ELF_DATA, 6) == 0)
  594. {
  595. /* relocate data section */
  596. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("data\n"));
  597. rt_module_arm_relocate(module, rel,
  598. (Elf32_Addr)data_addr + sym->st_value);
  599. }
  600. }
  601. }
  602. else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
  603. {
  604. /* relocate function */
  605. rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t *)
  606. module->module_space - module_addr + sym->st_value));
  607. }
  608. else
  609. {
  610. Elf32_Addr addr;
  611. if (ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
  612. {
  613. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol: %s\n",
  614. strtab + sym->st_name));
  615. /* need to resolve symbol in kernel symbol table */
  616. addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
  617. if (addr != (Elf32_Addr)RT_NULL)
  618. {
  619. rt_module_arm_relocate(module, rel, addr);
  620. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("symbol addr 0x%x\n",
  621. addr));
  622. }
  623. else
  624. rt_kprintf("Module: can't find %s in kernel symbol table\n",
  625. strtab + sym->st_name);
  626. }
  627. else
  628. {
  629. rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t*)
  630. module->module_space - module_addr + sym->st_value));
  631. }
  632. }
  633. rel ++;
  634. }
  635. }
  636. return module;
  637. }
  638. /**
  639. * This function will load a module from memory and create a thread for it
  640. *
  641. * @param name the name of module, which shall be unique
  642. * @param module_ptr the memory address of module image
  643. *
  644. * @return the module object
  645. */
  646. rt_module_t rt_module_load(const char *name, void *module_ptr)
  647. {
  648. rt_module_t module;
  649. RT_DEBUG_NOT_IN_INTERRUPT;
  650. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_load: %s ,", name));
  651. /* check ELF header */
  652. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0 &&
  653. rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  654. {
  655. rt_kprintf("Module: magic error\n");
  656. return RT_NULL;
  657. }
  658. /* check ELF class */
  659. if (elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  660. {
  661. rt_kprintf("Module: ELF class error\n");
  662. return RT_NULL;
  663. }
  664. if (elf_module->e_type == ET_REL)
  665. {
  666. module = _load_relocated_object(name, module_ptr);
  667. }
  668. else if (elf_module->e_type == ET_DYN)
  669. {
  670. module = _load_shared_object(name, module_ptr);
  671. }
  672. else
  673. {
  674. rt_kprintf("Module: unsupported elf type\n");
  675. return RT_NULL;
  676. }
  677. if (module == RT_NULL)
  678. return RT_NULL;
  679. /* init module object container */
  680. rt_module_init_object_container(module);
  681. /* increase module reference count */
  682. module->nref ++;
  683. if (elf_module->e_entry != 0)
  684. {
  685. rt_uint32_t *stack_size;
  686. rt_uint8_t *priority;
  687. #ifdef RT_USING_SLAB
  688. /* init module memory allocator */
  689. module->mem_list = RT_NULL;
  690. /* create page array */
  691. module->page_array =
  692. (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
  693. module->page_cnt = 0;
  694. #endif
  695. /* get the main thread stack size */
  696. module->stack_size = 2048;
  697. module->thread_priority = RT_THREAD_PRIORITY_MAX - 2;
  698. /* create module thread */
  699. module->module_thread =
  700. rt_thread_create(name,
  701. (void(*)(void *))module->module_entry,
  702. RT_NULL,
  703. module->stack_size,
  704. module->thread_priority,
  705. 10);
  706. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("thread entry 0x%x\n",
  707. module->module_entry));
  708. /* set module id */
  709. module->module_thread->module_id = (void *)module;
  710. module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
  711. /* startup module thread */
  712. rt_thread_startup(module->module_thread);
  713. }
  714. else
  715. {
  716. /* without entry point */
  717. module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
  718. }
  719. #ifdef RT_USING_HOOK
  720. if (rt_module_load_hook != RT_NULL)
  721. {
  722. rt_module_load_hook(module);
  723. }
  724. #endif
  725. return module;
  726. }
  727. #ifdef RT_USING_DFS
  728. #include <dfs_posix.h>
  729. static char* _module_name(const char *path)
  730. {
  731. const char *first, *end, *ptr;
  732. char *name;
  733. int size;
  734. ptr = (char *)path;
  735. first = ptr;
  736. end = path + rt_strlen(path);
  737. while (*ptr != '\0')
  738. {
  739. if (*ptr == '/')
  740. first = ptr + 1;
  741. if (*ptr == '.')
  742. end = ptr - 1;
  743. ptr ++;
  744. }
  745. size = end - first + 1;
  746. name = rt_malloc(size);
  747. rt_strncpy(name, first, size);
  748. name[size] = '\0';
  749. return name;
  750. }
  751. /**
  752. * This function will load a module from a file
  753. *
  754. * @param path the full path of application module
  755. *
  756. * @return the module object
  757. */
  758. rt_module_t rt_module_open(const char *path)
  759. {
  760. int fd, length;
  761. struct rt_module *module;
  762. struct stat s;
  763. char *buffer, *offset_ptr;
  764. char *name;
  765. RT_DEBUG_NOT_IN_INTERRUPT;
  766. /* check parameters */
  767. RT_ASSERT(path != RT_NULL);
  768. if (stat(path, &s) !=0)
  769. {
  770. rt_kprintf("Module: access %s failed\n", path);
  771. return RT_NULL;
  772. }
  773. buffer = (char *)rt_malloc(s.st_size);
  774. if (buffer == RT_NULL)
  775. {
  776. rt_kprintf("Module: out of memory\n");
  777. return RT_NULL;
  778. }
  779. offset_ptr = buffer;
  780. fd = open(path, O_RDONLY, 0);
  781. if (fd < 0)
  782. {
  783. rt_kprintf("Module: open %s failed\n", path);
  784. rt_free(buffer);
  785. return RT_NULL;
  786. }
  787. do
  788. {
  789. length = read(fd, offset_ptr, 4096);
  790. if (length > 0)
  791. {
  792. offset_ptr += length;
  793. }
  794. }while (length > 0);
  795. /* close fd */
  796. close(fd);
  797. if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
  798. {
  799. rt_kprintf("Module: read file failed\n");
  800. rt_free(buffer);
  801. return RT_NULL;
  802. }
  803. name = _module_name(path);
  804. module = rt_module_load(name, (void *)buffer);
  805. rt_free(buffer);
  806. rt_free(name);
  807. return module;
  808. }
  809. #if defined(RT_USING_FINSH)
  810. #include <finsh.h>
  811. FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from a file);
  812. #endif
  813. #endif
  814. /**
  815. * This function will destroy a module and release its resource.
  816. *
  817. * @param module the module to be destroyed.
  818. *
  819. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  820. */
  821. rt_err_t rt_module_destroy(rt_module_t module)
  822. {
  823. int i;
  824. struct rt_object *object;
  825. struct rt_list_node *list;
  826. RT_DEBUG_NOT_IN_INTERRUPT;
  827. /* check parameter */
  828. RT_ASSERT(module != RT_NULL);
  829. RT_ASSERT(module->nref == 0);
  830. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_destroy: %8.*s\n",
  831. RT_NAME_MAX, module->parent.name));
  832. /* module has entry point */
  833. if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  834. {
  835. #ifdef RT_USING_SEMAPHORE
  836. /* delete semaphores */
  837. list = &module->module_object[RT_Object_Class_Thread].object_list;
  838. while (list->next != list)
  839. {
  840. object = rt_list_entry(list->next, struct rt_object, list);
  841. if (rt_object_is_systemobject(object) == RT_TRUE)
  842. {
  843. /* detach static object */
  844. rt_sem_detach((rt_sem_t)object);
  845. }
  846. else
  847. {
  848. /* delete dynamic object */
  849. rt_sem_delete((rt_sem_t)object);
  850. }
  851. }
  852. #endif
  853. #ifdef RT_USING_MUTEX
  854. /* delete mutexs*/
  855. list = &module->module_object[RT_Object_Class_Mutex].object_list;
  856. while (list->next != list)
  857. {
  858. object = rt_list_entry(list->next, struct rt_object, list);
  859. if (rt_object_is_systemobject(object) == RT_TRUE)
  860. {
  861. /* detach static object */
  862. rt_mutex_detach((rt_mutex_t)object);
  863. }
  864. else
  865. {
  866. /* delete dynamic object */
  867. rt_mutex_delete((rt_mutex_t)object);
  868. }
  869. }
  870. #endif
  871. #ifdef RT_USING_EVENT
  872. /* delete mailboxs */
  873. list = &module->module_object[RT_Object_Class_Event].object_list;
  874. while (list->next != list)
  875. {
  876. object = rt_list_entry(list->next, struct rt_object, list);
  877. if (rt_object_is_systemobject(object) == RT_TRUE)
  878. {
  879. /* detach static object */
  880. rt_event_detach((rt_event_t)object);
  881. }
  882. else
  883. {
  884. /* delete dynamic object */
  885. rt_event_delete((rt_event_t)object);
  886. }
  887. }
  888. #endif
  889. #ifdef RT_USING_MAILBOX
  890. /* delete mailboxs */
  891. list = &module->module_object[RT_Object_Class_MailBox].object_list;
  892. while (list->next != list)
  893. {
  894. object = rt_list_entry(list->next, struct rt_object, list);
  895. if (rt_object_is_systemobject(object) == RT_TRUE)
  896. {
  897. /* detach static object */
  898. rt_mb_detach((rt_mailbox_t)object);
  899. }
  900. else
  901. {
  902. /* delete dynamic object */
  903. rt_mb_delete((rt_mailbox_t)object);
  904. }
  905. }
  906. #endif
  907. #ifdef RT_USING_MESSAGEQUEUE
  908. /* delete msgqueues */
  909. list = &module->module_object[RT_Object_Class_MessageQueue].object_list;
  910. while (list->next != list)
  911. {
  912. object = rt_list_entry(list->next, struct rt_object, list);
  913. if (rt_object_is_systemobject(object) == RT_TRUE)
  914. {
  915. /* detach static object */
  916. rt_mq_detach((rt_mq_t)object);
  917. }
  918. else
  919. {
  920. /* delete dynamic object */
  921. rt_mq_delete((rt_mq_t)object);
  922. }
  923. }
  924. #endif
  925. #ifdef RT_USING_MEMPOOL
  926. /* delete mempools */
  927. list = &module->module_object[RT_Object_Class_MemPool].object_list;
  928. while (list->next != list)
  929. {
  930. object = rt_list_entry(list->next, struct rt_object, list);
  931. if (rt_object_is_systemobject(object) == RT_TRUE)
  932. {
  933. /* detach static object */
  934. rt_mp_detach((rt_mp_t)object);
  935. }
  936. else
  937. {
  938. /* delete dynamic object */
  939. rt_mp_delete((rt_mp_t)object);
  940. }
  941. }
  942. #endif
  943. #ifdef RT_USING_DEVICE
  944. /* delete devices */
  945. list = &module->module_object[RT_Object_Class_Device].object_list;
  946. while (list->next != list)
  947. {
  948. object = rt_list_entry(list->next, struct rt_object, list);
  949. rt_device_unregister((rt_device_t)object);
  950. }
  951. #endif
  952. /* delete timers */
  953. list = &module->module_object[RT_Object_Class_Timer].object_list;
  954. while (list->next != list)
  955. {
  956. object = rt_list_entry(list->next, struct rt_object, list);
  957. if (rt_object_is_systemobject(object) == RT_TRUE)
  958. {
  959. /* detach static object */
  960. rt_timer_detach((rt_timer_t)object);
  961. }
  962. else
  963. {
  964. /* delete dynamic object */
  965. rt_timer_delete((rt_timer_t)object);
  966. }
  967. }
  968. }
  969. #ifdef RT_USING_SLAB
  970. if (module->page_cnt > 0)
  971. {
  972. struct rt_page_info *page = (struct rt_page_info *)module->page_array;
  973. rt_kprintf("Module: warning - memory still hasn't been free finished\n");
  974. while (module->page_cnt != 0)
  975. {
  976. rt_module_free_page(module, page[0].page_ptr, page[0].npage);
  977. }
  978. }
  979. #endif
  980. /* release module space memory */
  981. rt_free(module->module_space);
  982. /* release module symbol table */
  983. for (i = 0; i < module->nsym; i ++)
  984. {
  985. rt_free((void *)module->symtab[i].name);
  986. }
  987. if (module->symtab != RT_NULL)
  988. rt_free(module->symtab);
  989. #ifdef RT_USING_SLAB
  990. if (module->page_array != RT_NULL)
  991. rt_free(module->page_array);
  992. #endif
  993. /* delete module object */
  994. rt_object_delete((rt_object_t)module);
  995. return RT_EOK;
  996. }
  997. /**
  998. * This function will unload a module from memory and release resources
  999. *
  1000. * @param module the module to be unloaded
  1001. *
  1002. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  1003. */
  1004. rt_err_t rt_module_unload(rt_module_t module)
  1005. {
  1006. int i;
  1007. rt_err_t result;
  1008. struct rt_object *object;
  1009. struct rt_list_node *list;
  1010. RT_DEBUG_NOT_IN_INTERRUPT;
  1011. /* check parameter */
  1012. if (module == RT_NULL)
  1013. return -RT_ERROR;
  1014. rt_enter_critical();
  1015. if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  1016. {
  1017. /* delete all sub-threads */
  1018. list = &module->module_object[RT_Object_Class_Thread].object_list;
  1019. while (list->next != list)
  1020. {
  1021. object = rt_list_entry(list->next, struct rt_object, list);
  1022. if (rt_object_is_systemobject(object) == RT_TRUE)
  1023. {
  1024. /* detach static object */
  1025. rt_thread_detach((rt_thread_t)object);
  1026. }
  1027. else
  1028. {
  1029. /* delete dynamic object */
  1030. rt_thread_delete((rt_thread_t)object);
  1031. }
  1032. }
  1033. /* delete the main thread of module */
  1034. if (module->module_thread != RT_NULL)
  1035. {
  1036. rt_thread_delete(module->module_thread);
  1037. }
  1038. }
  1039. rt_exit_critical();
  1040. #ifdef RT_USING_HOOK
  1041. if (rt_module_unload_hook != RT_NULL)
  1042. {
  1043. rt_module_unload_hook(module);
  1044. }
  1045. #endif
  1046. return RT_EOK;
  1047. }
  1048. /**
  1049. * This function will find the specified module.
  1050. *
  1051. * @param name the name of module finding
  1052. *
  1053. * @return the module
  1054. */
  1055. rt_module_t rt_module_find(const char *name)
  1056. {
  1057. struct rt_object_information *information;
  1058. struct rt_object *object;
  1059. struct rt_list_node *node;
  1060. extern struct rt_object_information rt_object_container[];
  1061. RT_DEBUG_NOT_IN_INTERRUPT;
  1062. /* enter critical */
  1063. rt_enter_critical();
  1064. /* try to find device object */
  1065. information = &rt_object_container[RT_Object_Class_Module];
  1066. for (node = information->object_list.next;
  1067. node != &(information->object_list);
  1068. node = node->next)
  1069. {
  1070. object = rt_list_entry(node, struct rt_object, list);
  1071. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  1072. {
  1073. /* leave critical */
  1074. rt_exit_critical();
  1075. return (rt_module_t)object;
  1076. }
  1077. }
  1078. /* leave critical */
  1079. rt_exit_critical();
  1080. /* not found */
  1081. return RT_NULL;
  1082. }
  1083. #ifdef RT_USING_SLAB
  1084. /*
  1085. * This function will allocate the numbers page with specified size
  1086. * in page memory.
  1087. *
  1088. * @param size the size of memory to be allocated.
  1089. * @note this function is used for RT-Thread Application Module
  1090. */
  1091. static void *rt_module_malloc_page(rt_size_t npages)
  1092. {
  1093. void *chunk;
  1094. struct rt_page_info *page;
  1095. rt_module_t self_module;
  1096. self_module = rt_module_self();
  1097. RT_ASSERT(self_module != RT_NULL);
  1098. chunk = rt_page_alloc(npages);
  1099. if (chunk == RT_NULL)
  1100. return RT_NULL;
  1101. page = (struct rt_page_info *)self_module->page_array;
  1102. page[self_module->page_cnt].page_ptr = chunk;
  1103. page[self_module->page_cnt].npage = npages;
  1104. self_module->page_cnt ++;
  1105. RT_ASSERT(self_module->page_cnt <= PAGE_COUNT_MAX);
  1106. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_malloc_page 0x%x %d\n",
  1107. chunk, npages));
  1108. return chunk;
  1109. }
  1110. /*
  1111. * This function will release the previously allocated memory page
  1112. * by rt_malloc_page.
  1113. *
  1114. * @param page_ptr the page address to be released.
  1115. * @param npages the number of page shall be released.
  1116. *
  1117. * @note this function is used for RT-Thread Application Module
  1118. */
  1119. static void rt_module_free_page(rt_module_t module,
  1120. void *page_ptr,
  1121. rt_size_t npages)
  1122. {
  1123. int i, index;
  1124. struct rt_page_info *page;
  1125. rt_module_t self_module;
  1126. self_module = rt_module_self();
  1127. RT_ASSERT(self_module != RT_NULL);
  1128. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_free_page 0x%x %d\n",
  1129. page_ptr, npages));
  1130. rt_page_free(page_ptr, npages);
  1131. page = (struct rt_page_info *)module->page_array;
  1132. for (i = 0; i < module->page_cnt; i ++)
  1133. {
  1134. if (page[i].page_ptr == page_ptr)
  1135. {
  1136. if (page[i].npage == npages + 1)
  1137. {
  1138. page[i].page_ptr +=
  1139. npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t);
  1140. page[i].npage -= npages;
  1141. }
  1142. else if (page[i].npage == npages)
  1143. {
  1144. for (index = i; index < module->page_cnt-1; index ++)
  1145. {
  1146. page[index].page_ptr = page[index + 1].page_ptr;
  1147. page[index].npage = page[index + 1].npage;
  1148. }
  1149. page[module->page_cnt - 1].page_ptr = RT_NULL;
  1150. page[module->page_cnt - 1].npage = 0;
  1151. module->page_cnt --;
  1152. }
  1153. else
  1154. RT_ASSERT(RT_FALSE);
  1155. self_module->page_cnt --;
  1156. return;
  1157. }
  1158. }
  1159. /* should not get here */
  1160. RT_ASSERT(RT_FALSE);
  1161. }
  1162. /**
  1163. * rt_module_malloc - allocate memory block in free list
  1164. */
  1165. void *rt_module_malloc(rt_size_t size)
  1166. {
  1167. struct rt_mem_head *b, *n, *up;
  1168. struct rt_mem_head **prev;
  1169. rt_uint32_t npage;
  1170. rt_size_t nunits;
  1171. rt_module_t self_module;
  1172. self_module = rt_module_self();
  1173. RT_ASSERT(self_module != RT_NULL);
  1174. RT_DEBUG_NOT_IN_INTERRUPT;
  1175. nunits = (size + sizeof(struct rt_mem_head) - 1) /
  1176. sizeof(struct rt_mem_head)
  1177. + 1;
  1178. RT_ASSERT(size != 0);
  1179. RT_ASSERT(nunits != 0);
  1180. rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
  1181. for (prev = (struct rt_mem_head **)&self_module->mem_list;
  1182. (b = *prev) != RT_NULL;
  1183. prev = &(b->next))
  1184. {
  1185. if (b->size > nunits)
  1186. {
  1187. /* split memory */
  1188. n = b + nunits;
  1189. n->next = b->next;
  1190. n->size = b->size - nunits;
  1191. b->size = nunits;
  1192. *prev = n;
  1193. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_malloc 0x%x, %d\n",
  1194. b + 1, size));
  1195. rt_sem_release(&mod_sem);
  1196. return (void *)(b + 1);
  1197. }
  1198. if (b->size == nunits)
  1199. {
  1200. /* this node fit, remove this node */
  1201. *prev = b->next;
  1202. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_malloc 0x%x, %d\n",
  1203. b + 1, size));
  1204. rt_sem_release(&mod_sem);
  1205. return (void *)(b + 1);
  1206. }
  1207. }
  1208. /* allocate pages from system heap */
  1209. npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1) /
  1210. RT_MM_PAGE_SIZE;
  1211. if ((up = (struct rt_mem_head *)rt_module_malloc_page(npage)) == RT_NULL)
  1212. return RT_NULL;
  1213. up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
  1214. for (prev = (struct rt_mem_head **)&self_module->mem_list;
  1215. (b = *prev) != RT_NULL;
  1216. prev = &(b->next))
  1217. {
  1218. if (b > up + up->size)
  1219. break;
  1220. }
  1221. up->next = b;
  1222. *prev = up;
  1223. rt_sem_release(&mod_sem);
  1224. return rt_module_malloc(size);
  1225. }
  1226. /**
  1227. * rt_module_free - free memory block in free list
  1228. */
  1229. void rt_module_free(rt_module_t module, void *addr)
  1230. {
  1231. struct rt_mem_head *b, *n, *r;
  1232. struct rt_mem_head **prev;
  1233. RT_DEBUG_NOT_IN_INTERRUPT;
  1234. RT_ASSERT(addr);
  1235. RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0);
  1236. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_free 0x%x\n", addr));
  1237. rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
  1238. n = (struct rt_mem_head *)addr - 1;
  1239. prev = (struct rt_mem_head **)&module->mem_list;
  1240. while ((b = *prev) != RT_NULL)
  1241. {
  1242. RT_ASSERT(b->size > 0);
  1243. RT_ASSERT(b > n || b + b->size <= n);
  1244. if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0))
  1245. {
  1246. if (b + (b->size + n->size) == b->next)
  1247. {
  1248. b->size += b->next->size + n->size;
  1249. b->next = b->next->next;
  1250. }
  1251. else
  1252. b->size += n->size;
  1253. if ((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0)
  1254. {
  1255. int npage =
  1256. b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
  1257. if (npage > 0)
  1258. {
  1259. if ((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
  1260. {
  1261. rt_size_t nunits = npage *
  1262. RT_MM_PAGE_SIZE /
  1263. sizeof(struct rt_mem_head);
  1264. /* split memory */
  1265. r = b + nunits;
  1266. r->next = b->next;
  1267. r->size = b->size - nunits;
  1268. *prev = r;
  1269. }
  1270. else
  1271. {
  1272. *prev = b->next;
  1273. }
  1274. rt_module_free_page(module, b, npage);
  1275. }
  1276. }
  1277. /* unlock */
  1278. rt_sem_release(&mod_sem);
  1279. return;
  1280. }
  1281. if (b == n + n->size)
  1282. {
  1283. n->size = b->size + n->size;
  1284. n->next = b->next;
  1285. if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
  1286. {
  1287. int npage =
  1288. n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
  1289. if (npage > 0)
  1290. {
  1291. if ((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
  1292. {
  1293. rt_size_t nunits = npage *
  1294. RT_MM_PAGE_SIZE /
  1295. sizeof(struct rt_mem_head);
  1296. /* split memory */
  1297. r = n + nunits;
  1298. r->next = n->next;
  1299. r->size = n->size - nunits;
  1300. *prev = r;
  1301. }
  1302. else
  1303. *prev = n->next;
  1304. rt_module_free_page(module, n, npage);
  1305. }
  1306. }
  1307. else
  1308. {
  1309. *prev = n;
  1310. }
  1311. /* unlock */
  1312. rt_sem_release(&mod_sem);
  1313. return;
  1314. }
  1315. if (b > n + n->size)
  1316. break;
  1317. prev = &(b->next);
  1318. }
  1319. if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
  1320. {
  1321. int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
  1322. if (npage > 0)
  1323. {
  1324. rt_module_free_page(module, n, npage);
  1325. if (n->size % RT_MM_PAGE_SIZE != 0)
  1326. {
  1327. rt_size_t nunits =
  1328. npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
  1329. /* split memory */
  1330. r = n + nunits;
  1331. r->next = b;
  1332. r->size = n->size - nunits;
  1333. *prev = r;
  1334. }
  1335. else
  1336. {
  1337. *prev = b;
  1338. }
  1339. }
  1340. }
  1341. else
  1342. {
  1343. n->next = b;
  1344. *prev = n;
  1345. }
  1346. /* unlock */
  1347. rt_sem_release(&mod_sem);
  1348. }
  1349. /**
  1350. * rt_module_realloc - realloc memory block in free list
  1351. */
  1352. void *rt_module_realloc(void *ptr, rt_size_t size)
  1353. {
  1354. struct rt_mem_head *b, *p, *prev, *tmpp;
  1355. rt_size_t nunits;
  1356. rt_module_t self_module;
  1357. self_module = rt_module_self();
  1358. RT_ASSERT(self_module != RT_NULL);
  1359. RT_DEBUG_NOT_IN_INTERRUPT;
  1360. if (!ptr)
  1361. return rt_module_malloc(size);
  1362. if (size == 0)
  1363. {
  1364. rt_module_free(self_module, ptr);
  1365. return RT_NULL;
  1366. }
  1367. nunits = (size + sizeof(struct rt_mem_head) - 1) /
  1368. sizeof(struct rt_mem_head)
  1369. +1;
  1370. b = (struct rt_mem_head *)ptr - 1;
  1371. if (nunits <= b->size)
  1372. {
  1373. /* new size is smaller or equal then before */
  1374. if (nunits == b->size)
  1375. return ptr;
  1376. else
  1377. {
  1378. p = b + nunits;
  1379. p->size = b->size - nunits;
  1380. b->size = nunits;
  1381. rt_module_free(self_module, (void *)(p + 1));
  1382. return (void *)(b + 1);
  1383. }
  1384. }
  1385. else
  1386. {
  1387. /* more space then required */
  1388. prev = (struct rt_mem_head *)self_module->mem_list;
  1389. for (p = prev->next;
  1390. p != (b->size + b) && p != RT_NULL;
  1391. prev = p, p = p->next)
  1392. {
  1393. break;
  1394. }
  1395. /* available block after ap in freelist */
  1396. if (p != RT_NULL &&
  1397. (p->size >= (nunits - (b->size))) &&
  1398. p == (b + b->size))
  1399. {
  1400. /* perfect match */
  1401. if (p->size == (nunits - (b->size)))
  1402. {
  1403. b->size = nunits;
  1404. prev->next = p->next;
  1405. }
  1406. else /* more space then required, split block */
  1407. {
  1408. /* pointer to old header */
  1409. tmpp = p;
  1410. p = b + nunits;
  1411. /* restoring old pointer */
  1412. p->next = tmpp->next;
  1413. /* new size for p */
  1414. p->size = tmpp->size + b->size - nunits;
  1415. b->size = nunits;
  1416. prev->next = p;
  1417. }
  1418. self_module->mem_list = (void *)prev;
  1419. return (void *)(b + 1);
  1420. }
  1421. else /* allocate new memory and copy old data */
  1422. {
  1423. if ((p = rt_module_malloc(size)) == RT_NULL)
  1424. return RT_NULL;
  1425. rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head)));
  1426. rt_module_free(self_module, (void *)(b + 1));
  1427. return (void *)(p);
  1428. }
  1429. }
  1430. }
  1431. #ifdef RT_USING_FINSH
  1432. #include <finsh.h>
  1433. void list_memlist(const char *name)
  1434. {
  1435. rt_module_t module;
  1436. struct rt_mem_head **prev;
  1437. struct rt_mem_head *b;
  1438. module = rt_module_find(name);
  1439. if (module == RT_NULL)
  1440. return;
  1441. for (prev = (struct rt_mem_head **)&module->mem_list;
  1442. (b = *prev) != RT_NULL;
  1443. prev = &(b->next))
  1444. {
  1445. rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head));
  1446. }
  1447. }
  1448. FINSH_FUNCTION_EXPORT(list_memlist, list module free memory information)
  1449. void list_mempage(const char *name)
  1450. {
  1451. rt_module_t module;
  1452. struct rt_page_info *page;
  1453. int i;
  1454. module = rt_module_find(name);
  1455. if (module == RT_NULL)
  1456. return;
  1457. page = (struct rt_page_info *)module->page_array;
  1458. for (i = 0; i < module->page_cnt; i ++)
  1459. {
  1460. rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage);
  1461. }
  1462. }
  1463. FINSH_FUNCTION_EXPORT(list_mempage, list module using memory page information)
  1464. #endif
  1465. #endif
  1466. #endif