module.c 50 KB

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