module.c 47 KB

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