module.c 59 KB

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