module.c 39 KB

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