module.c 39 KB

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