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