module.c 59 KB

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