module.c 60 KB

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