module.c 59 KB

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