module.c 38.8 KB
Newer Older
qiuyiuestc's avatar
qiuyiuestc 已提交
1 2 3
/*
 * File      : module.c
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
qiuyiuestc's avatar
qiuyiuestc 已提交
5 6 7 8 9 10
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
D
dzzxzz 已提交
11 12 13 14 15 16
 * Date           Author       Notes
 * 2010-01-09     Bernard      first version
 * 2010-04-09     yi.qiu       implement based on first version
 * 2010-10-23     yi.qiu       implement module memory allocator
 * 2011-05-25     yi.qiu       implement module hook function
 * 2011-06-23     yi.qiu       rewrite module memory allocator
qiuyiuestc's avatar
qiuyiuestc 已提交
17
 */
qiuyiuestc's avatar
qiuyiuestc 已提交
18

19
#include <rthw.h>
qiuyiuestc's avatar
qiuyiuestc 已提交
20
#include <rtthread.h>
qiuyiuestc's avatar
qiuyiuestc 已提交
21
#include <rtm.h>
qiuyiuestc's avatar
qiuyiuestc 已提交
22

qiuyiuestc's avatar
qiuyiuestc 已提交
23
#include "string.h"
qiuyiuestc's avatar
qiuyiuestc 已提交
24

qiuyiuestc's avatar
qiuyiuestc 已提交
25
#ifdef RT_USING_MODULE
26
#include "module.h"
qiuyiuestc's avatar
qiuyiuestc 已提交
27

D
dzzxzz 已提交
28 29 30
#define elf_module          ((Elf32_Ehdr *)module_ptr)
#define shdr                ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
#define phdr                ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
qiuyiuestc's avatar
qiuyiuestc 已提交
31

D
dzzxzz 已提交
32 33 34 35 36 37 38
#define IS_PROG(s)          (s.sh_type == SHT_PROGBITS)
#define IS_NOPROG(s)        (s.sh_type == SHT_NOBITS)
#define IS_REL(s)           (s.sh_type == SHT_REL)
#define IS_RELA(s)          (s.sh_type == SHT_RELA)
#define IS_ALLOC(s)         (s.sh_flags == SHF_ALLOC)
#define IS_AX(s)            ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
#define IS_AW(s)            ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
qiuyiuestc's avatar
qiuyiuestc 已提交
39

B
bernard.xiong@gmail.com 已提交
40
#define PAGE_COUNT_MAX	256
qiuyiuestc's avatar
qiuyiuestc 已提交
41

qiuyiuestc's avatar
qiuyiuestc 已提交
42 43 44 45 46 47 48
/* module memory allocator */
struct rt_mem_head
{
	rt_size_t size;				/* size of memory block  */
	struct rt_mem_head *next;		/* next valid memory block */
};

qiuyiuestc's avatar
qiuyiuestc 已提交
49 50 51 52 53 54
struct rt_page_info
{
	rt_uint32_t *page_ptr;
	rt_uint32_t npage;
};

55
#ifdef RT_USING_SLAB
qiuyiuestc's avatar
qiuyiuestc 已提交
56
static void *rt_module_malloc_page(rt_size_t npages);
57 58
static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t npages);
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
59

qiuyiuestc's avatar
qiuyiuestc 已提交
60
static rt_module_t rt_current_module = RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
61 62
static struct rt_semaphore mod_sem;
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
63
rt_list_t rt_module_symbol_list;
qiuyiuestc's avatar
qiuyiuestc 已提交
64

D
dzzxzz 已提交
65
static char *_strip_name(const char *string)
qiuyiuestc's avatar
qiuyiuestc 已提交
66 67
{
	int i = 0, p = 0, q = 0;
D
dzzxzz 已提交
68 69
	const char *str = string;
	char *dest = RT_NULL;
B
bernard.xiong@gmail.com 已提交
70

D
dzzxzz 已提交
71
	while (*str != '\n' && *str != '\0')
qiuyiuestc's avatar
qiuyiuestc 已提交
72
	{
D
dzzxzz 已提交
73
		if (*str =='/' ) p = i + 1;
B
bernard.xiong@gmail.com 已提交
74
		if (*str == '.') q = i;
qiuyiuestc's avatar
qiuyiuestc 已提交
75 76 77
		str++; i++;
	}

D
dzzxzz 已提交
78
	if (p < q)
qiuyiuestc's avatar
qiuyiuestc 已提交
79 80
	{
		int len = q - p;
D
dzzxzz 已提交
81
		dest = (char *)rt_malloc(len + 1);
qiuyiuestc's avatar
qiuyiuestc 已提交
82 83 84 85 86 87
		rt_strncpy(dest, &string[p], len);
		dest[len] = '\0';
	}

	return dest;
}
88 89 90 91

/**
 * @ingroup SystemInit
 *
B
bernard.xiong@gmail.com 已提交
92
 * This function will initialize system module
93 94 95
 */
void rt_system_module_init(void)
{
qiuyiuestc's avatar
qiuyiuestc 已提交
96
#ifdef __GNUC__
97 98
	extern int __rtmsymtab_start;
	extern int __rtmsymtab_end;
qiuyiuestc's avatar
qiuyiuestc 已提交
99
	
100 101
	_rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
	_rt_module_symtab_end   = (struct rt_module_symtab *)&__rtmsymtab_end;
D
dzzxzz 已提交
102
#elif defined (__CC_ARM)
qiuyiuestc's avatar
qiuyiuestc 已提交
103 104
	extern int RTMSymTab$$Base;
	extern int RTMSymTab$$Limit;
D
dzzxzz 已提交
105

qiuyiuestc's avatar
qiuyiuestc 已提交
106 107
	_rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base;
	_rt_module_symtab_end   = (struct rt_module_symtab *)&RTMSymTab$$Limit;	
qiuyiuestc's avatar
qiuyiuestc 已提交
108
#endif
109 110

	rt_list_init(&rt_module_symbol_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
111

qiuyiuestc's avatar
qiuyiuestc 已提交
112 113 114
	/* initialize heap semaphore */
	rt_sem_init(&mod_sem, "module", 1, RT_IPC_FLAG_FIFO);

qiuyiuestc's avatar
qiuyiuestc 已提交
115 116
	/* init current module */
	rt_current_module = RT_NULL;
117 118
}

D
dzzxzz 已提交
119
static rt_uint32_t rt_module_symbol_find(const char *sym_str)
120 121
{
	/* find in kernel symbol table */
D
dzzxzz 已提交
122
	struct rt_module_symtab *index;
123 124
	for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
125 126
		if (rt_strcmp(index->name, sym_str) == 0)
			return (rt_uint32_t)index->addr;
127 128 129 130
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
131

qiuyiuestc's avatar
qiuyiuestc 已提交
132 133 134
/**
 * This function will return self module object
 *
qiuyiuestc's avatar
qiuyiuestc 已提交
135
 * @return the self module object
qiuyiuestc's avatar
qiuyiuestc 已提交
136
 */
D
dzzxzz 已提交
137
rt_module_t rt_module_self(void)
qiuyiuestc's avatar
qiuyiuestc 已提交
138
{
qiuyiuestc's avatar
qiuyiuestc 已提交
139
	/* return current module */
qiuyiuestc's avatar
qiuyiuestc 已提交
140 141 142
	return rt_current_module;
}

qiuyiuestc's avatar
qiuyiuestc 已提交
143 144 145 146 147
/**
 * This function will set current module object
 *
 * @return RT_EOK
 */
D
dzzxzz 已提交
148
rt_err_t rt_module_set(rt_module_t module)
qiuyiuestc's avatar
qiuyiuestc 已提交
149 150 151 152 153 154 155
{
	/* set current module */
	rt_current_module = module;

	return RT_EOK;
}

D
dzzxzz 已提交
156
static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel, Elf32_Addr sym_val)
qiuyiuestc's avatar
qiuyiuestc 已提交
157 158
{
	Elf32_Addr *where, tmp;
159 160
	Elf32_Sword addend, offset;
	rt_uint32_t upper, lower, sign, j1, j2;
qiuyiuestc's avatar
qiuyiuestc 已提交
161

D
dzzxzz 已提交
162
	where = (Elf32_Addr *)((rt_uint8_t *)module->module_space + rel->r_offset);
qiuyiuestc's avatar
qiuyiuestc 已提交
163 164 165 166 167 168
	switch (ELF32_R_TYPE(rel->r_info))
	{
	case R_ARM_NONE:
		break;
	case R_ARM_ABS32:
		*where += (Elf32_Addr)sym_val;
D
dzzxzz 已提交
169
		RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n", where, *where));
qiuyiuestc's avatar
qiuyiuestc 已提交
170 171 172 173 174 175 176 177 178 179 180
		break;
	case R_ARM_PC24:
	case R_ARM_PLT32:
	case R_ARM_CALL:
	case R_ARM_JUMP24:
		addend = *where & 0x00ffffff;
		if (addend & 0x00800000)
			addend |= 0xff000000;
		tmp = sym_val - (Elf32_Addr)where + (addend << 2);
		tmp >>= 2;
		*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
D
dzzxzz 已提交
181
		RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n", where, *where));
qiuyiuestc's avatar
qiuyiuestc 已提交
182
		break;
183 184 185 186
	case R_ARM_REL32:
		*where += sym_val - (Elf32_Addr)where;
		RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_REL32: %x -> %x, sym %x, offset %x\n", where, *where, sym_val, rel->r_offset));
		break;
qiuyiuestc's avatar
qiuyiuestc 已提交
187 188 189 190
	case R_ARM_V4BX:
		*where &= 0xf000000f;
		*where |= 0x01a0f000;
		break;
qiuyiuestc's avatar
qiuyiuestc 已提交
191 192
	case R_ARM_GLOB_DAT:
	case R_ARM_JUMP_SLOT:
qiuyiuestc's avatar
qiuyiuestc 已提交
193
		*where = (Elf32_Addr)sym_val;
194 195
		RT_DEBUG_LOG(RT_DEBUG_MODULE,
			("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val));
196 197 198 199 200 201 202 203
		break;
#if 0		/* To do */
	case R_ARM_GOT_BREL:
		temp = (Elf32_Addr)sym_val;
		*where = (Elf32_Addr)&temp;
		RT_DEBUG_LOG(RT_DEBUG_MODULE,
			("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val));
		break;
B
bernard.xiong@gmail.com 已提交
204
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
205 206
	case R_ARM_RELATIVE:
		*where += (Elf32_Addr)sym_val;
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		//RT_DEBUG_LOG(RT_DEBUG_MODULE,
			//("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val));
		break;
	case R_ARM_THM_CALL:
	case R_ARM_THM_JUMP24:
		upper = *(rt_uint16_t *)where;
		lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);

		sign = (upper >> 10) & 1;
		j1 = (lower >> 13) & 1;
		j2 = (lower >> 11) & 1;
		offset = (sign << 24) | ((~(j1 ^ sign) & 1) << 23) |
			 				 ((~(j2 ^ sign) & 1) << 22) |
			 				 ((upper & 0x03ff) << 12) |
			 				 ((lower & 0x07ff) << 1);
		if (offset & 0x01000000)
			 				 offset -= 0x02000000;
		offset += sym_val - (Elf32_Addr)where;
B
bernard.xiong@gmail.com 已提交
225

226
		if (!(offset & 1) || offset <= (rt_int32_t)0xff000000 ||
B
bernard.xiong@gmail.com 已提交
227
			 	offset >= (rt_int32_t)0x01000000)
228 229
		{
			rt_kprintf("only Thumb addresses allowed\n");
D
dzzxzz 已提交
230
			
231 232
			return -1;
		}
B
bernard.xiong@gmail.com 已提交
233

234 235 236 237 238 239 240 241 242 243
		sign = (offset >> 24) & 1;
		j1 = sign ^ (~(offset >> 23) & 1);
		j2 = sign ^ (~(offset >> 22) & 1);
		*(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) | (sign << 10) |
			 						 ((offset >> 12) & 0x03ff));
		*(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
			 						   (j1 << 13) | (j2 << 11) |
			 						   ((offset >> 1) & 0x07ff));
		upper = *(rt_uint16_t *)where;
		lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
B
bernard.xiong@gmail.com 已提交
244
		break;
qiuyiuestc's avatar
qiuyiuestc 已提交
245 246 247 248 249 250 251
	default:
		return -1;
	}

	return 0;
}

D
dzzxzz 已提交
252
static void rt_module_init_object_container(struct rt_module *module)
qiuyiuestc's avatar
qiuyiuestc 已提交
253 254 255
{
	RT_ASSERT(module != RT_NULL);

B
bernard.xiong@gmail.com 已提交
256
	/* initialize object container - thread */
qiuyiuestc's avatar
qiuyiuestc 已提交
257 258 259 260 261
	rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
	module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
	module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;

#ifdef RT_USING_SEMAPHORE
B
bernard.xiong@gmail.com 已提交
262
	/* initialize object container - semaphore */
qiuyiuestc's avatar
qiuyiuestc 已提交
263 264 265 266 267 268
	rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
	module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
	module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
#endif

#ifdef RT_USING_MUTEX
B
bernard.xiong@gmail.com 已提交
269
	/* initialize object container - mutex */
qiuyiuestc's avatar
qiuyiuestc 已提交
270 271 272 273 274 275
	rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
	module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
	module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
#endif

#ifdef RT_USING_EVENT
B
bernard.xiong@gmail.com 已提交
276
	/* initialize object container - event */
qiuyiuestc's avatar
qiuyiuestc 已提交
277 278 279 280 281 282
	rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
	module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
	module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
#endif

#ifdef RT_USING_MAILBOX
B
bernard.xiong@gmail.com 已提交
283
	/* initialize object container - mailbox */
qiuyiuestc's avatar
qiuyiuestc 已提交
284 285 286 287 288 289
	rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
	module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
	module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
#endif

#ifdef RT_USING_MESSAGEQUEUE
B
bernard.xiong@gmail.com 已提交
290
	/* initialize object container - message queue */
qiuyiuestc's avatar
qiuyiuestc 已提交
291 292 293 294 295 296
	rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
	module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
	module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
#endif

#ifdef RT_USING_MEMPOOL
B
bernard.xiong@gmail.com 已提交
297
	/* initialize object container - memory pool */
qiuyiuestc's avatar
qiuyiuestc 已提交
298 299 300 301 302 303
	rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
	module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
	module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
#endif

#ifdef RT_USING_DEVICE
B
bernard.xiong@gmail.com 已提交
304
	/* initialize object container - device */
qiuyiuestc's avatar
qiuyiuestc 已提交
305 306 307 308 309
	rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
	module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
	module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
#endif

B
bernard.xiong@gmail.com 已提交
310
	/* initialize object container - timer */
qiuyiuestc's avatar
qiuyiuestc 已提交
311 312 313 314 315
	rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
	module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
	module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
}

qiuyiuestc's avatar
qiuyiuestc 已提交
316 317 318 319 320 321 322
#ifdef RT_USING_HOOK
static void (*rt_module_load_hook)(rt_module_t module);
static void (*rt_module_unload_hook)(rt_module_t module);

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
323

qiuyiuestc's avatar
qiuyiuestc 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
/*@{*/

/**
 * This function will set a hook function, which will be invoked when module
 * be loaded to system.
 *
 * @param hook the hook function
 */
void rt_module_load_sethook(void (*hook)(rt_module_t module))
{
	rt_module_load_hook = hook;
}

/**
 * This function will set a hook function, which will be invoked when module
 * be unloaded from system.
 *
 * @param hook the hook function
 */
void rt_module_unload_sethook(void (*hook)(rt_module_t module))
{
	rt_module_unload_hook = hook;
}

/*@}*/
#endif

D
dzzxzz 已提交
351
static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
qiuyiuestc's avatar
qiuyiuestc 已提交
352
{
353
	rt_uint8_t *ptr = RT_NULL;
354 355
	rt_module_t module = RT_NULL;
	rt_bool_t linked = RT_FALSE;
356
	rt_uint32_t index, module_size = 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
357

358
	RT_ASSERT(module_ptr != RT_NULL);
B
bernard.xiong@gmail.com 已提交
359

360
	if(rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
361
	{
362
		/* rtmlinker finished */
363 364
		linked = RT_TRUE;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
365
	
qiuyiuestc's avatar
qiuyiuestc 已提交
366
	/* get the ELF image size */
qiuyiuestc's avatar
qiuyiuestc 已提交
367
	for (index = 0; index < elf_module->e_phnum; index++)
qiuyiuestc's avatar
qiuyiuestc 已提交
368
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
369 370 371 372
		if(phdr[index].p_type == PT_LOAD)
			module_size += phdr[index].p_memsz;
	}	
	
D
dzzxzz 已提交
373
	if (module_size == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
374 375
	{
		rt_kprintf(" module size error\n");
qiuyiuestc's avatar
qiuyiuestc 已提交
376
		return RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
377
	}	
qiuyiuestc's avatar
qiuyiuestc 已提交
378 379

	/* allocate module */
qiuyiuestc's avatar
qiuyiuestc 已提交
380
	module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, name);
qiuyiuestc's avatar
qiuyiuestc 已提交
381
	if (!module) return RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
382

qiuyiuestc's avatar
qiuyiuestc 已提交
383 384
	module->nref = 0;
	
qiuyiuestc's avatar
qiuyiuestc 已提交
385 386 387 388 389
	/* allocate module space */
	module->module_space = rt_malloc(module_size);
	if (module->module_space == RT_NULL)
	{
		rt_object_delete(&(module->parent));
D
dzzxzz 已提交
390

qiuyiuestc's avatar
qiuyiuestc 已提交
391 392 393 394 395 396 397
		return RT_NULL;
	}

	/* zero all space */
	ptr = module->module_space;
	rt_memset(ptr, 0, module_size);

qiuyiuestc's avatar
qiuyiuestc 已提交
398 399
	rt_kprintf(" load address at 0x%x\n", ptr);

qiuyiuestc's avatar
qiuyiuestc 已提交
400
	for (index = 0; index < elf_module->e_phnum; index++)
qiuyiuestc's avatar
qiuyiuestc 已提交
401
	{
D
dzzxzz 已提交
402
		if (phdr[index].p_type == PT_LOAD)
qiuyiuestc's avatar
qiuyiuestc 已提交
403
		{
D
dzzxzz 已提交
404
			rt_memcpy(ptr, (rt_uint8_t *)elf_module + phdr[index].p_offset, phdr[index].p_filesz);
qiuyiuestc's avatar
qiuyiuestc 已提交
405 406 407
			ptr += phdr[index].p_memsz;
		}		
	}	
qiuyiuestc's avatar
qiuyiuestc 已提交
408 409

	/* set module entry */
qiuyiuestc's avatar
qiuyiuestc 已提交
410
	module->module_entry = module->module_space + elf_module->e_entry;
qiuyiuestc's avatar
qiuyiuestc 已提交
411
	
qiuyiuestc's avatar
qiuyiuestc 已提交
412 413 414 415 416 417 418 419
	/* handle relocation section */
	for (index = 0; index < elf_module->e_shnum; index ++)
	{
		if (IS_REL(shdr[index]))
		{
			rt_uint32_t i, nr_reloc;
			Elf32_Sym *symtab;
			Elf32_Rel *rel;
420 421
			rt_uint8_t *strtab;
			static rt_bool_t unsolved = RT_FALSE;
qiuyiuestc's avatar
qiuyiuestc 已提交
422 423

			/* get relocate item */
D
dzzxzz 已提交
424
			rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
qiuyiuestc's avatar
qiuyiuestc 已提交
425

426
			/* locate .rel.plt and .rel.dyn section */
427 428 429
			symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset);
			strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
			nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
qiuyiuestc's avatar
qiuyiuestc 已提交
430 431 432 433 434

			/* relocate every items */
			for (i = 0; i < nr_reloc; i ++)
			{
				Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
435 436 437 438

				RT_DEBUG_LOG(RT_DEBUG_MODULE,
					("relocate symbol %s shndx %d\n", strtab + sym->st_name, sym->st_shndx));

B
bernard.xiong@gmail.com 已提交
439
				if((sym->st_shndx != SHT_NULL) || (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
440 441
					rt_module_arm_relocate(module, rel,  (Elf32_Addr)(module->module_space + sym->st_value));
				else if(!linked)
442 443
				{
					Elf32_Addr addr;
444 445

					RT_DEBUG_LOG(RT_DEBUG_MODULE,
446
						("relocate symbol: %s\n", strtab + sym->st_name));
447

448
					/* need to resolve symbol in kernel symbol table */
D
dzzxzz 已提交
449
					addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
450 451 452
					if (addr == 0)
					{
						rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
453
						unsolved = RT_TRUE;
454
					}	
D
dzzxzz 已提交
455 456
					else
						rt_module_arm_relocate(module, rel, addr);
457
				}
qiuyiuestc's avatar
qiuyiuestc 已提交
458 459
				rel ++;
			}
460

D
dzzxzz 已提交
461
			if (unsolved)
462 463 464
			{
				rt_object_delete(&(module->parent));
				rt_free(module);
D
dzzxzz 已提交
465

466 467
				return RT_NULL;
			}	
qiuyiuestc's avatar
qiuyiuestc 已提交
468 469 470
		}
	}

471 472
#if 0
	for (index = 0; index < elf_module->e_shnum; index ++)
B
bernard.xiong@gmail.com 已提交
473
	{
474 475
		/* find .dynsym section */
		rt_uint8_t* shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
B
bernard.xiong@gmail.com 已提交
476 477
		if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_GOT) == 0)
		{
478 479
			rt_hw_set_got_base(module->module_space + shdr[index].sh_offset);
			break;
B
bernard.xiong@gmail.com 已提交
480
		}
481 482 483
	}
#endif

484 485 486 487
	/* construct module symbol table */
	for (index = 0; index < elf_module->e_shnum; index ++)
	{	
		/* find .dynsym section */
D
dzzxzz 已提交
488
		rt_uint8_t *shstrab = (rt_uint8_t *)module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
D
dzzxzz 已提交
489 490
		if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
			break;
491 492
	}

qiuyiuestc's avatar
qiuyiuestc 已提交
493
	/* found .dynsym section */
D
dzzxzz 已提交
494
	if (index != elf_module->e_shnum)
495 496 497
	{
		int i, count = 0;
		Elf32_Sym *symtab = RT_NULL;
D
dzzxzz 已提交
498
		rt_uint8_t *strtab = RT_NULL;
499

D
dzzxzz 已提交
500
		symtab =(Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
B
bernard.xiong@gmail.com 已提交
501
		strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
502

D
dzzxzz 已提交
503
		for (i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
504
		{
D
dzzxzz 已提交
505 506
			if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
				count ++;
507 508
		}

D
dzzxzz 已提交
509
		module->symtab = (struct rt_module_symtab *)rt_malloc(count * sizeof(struct rt_module_symtab));
510
		module->nsym = count;
D
dzzxzz 已提交
511
		for (i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
512
		{
D
dzzxzz 已提交
513
			if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
514
			{
D
dzzxzz 已提交
515
				rt_size_t length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
516

D
dzzxzz 已提交
517
				module->symtab[count].addr = (void *)(module->module_space + symtab[i].st_value);
518
				module->symtab[count].name = rt_malloc(length);
D
dzzxzz 已提交
519 520 521
				rt_memset((void *)module->symtab[count].name, 0, length);
				rt_memcpy((void *)module->symtab[count].name, strtab + symtab[i].st_name, length);
				count ++;
522 523
			}	
		}	
524
	}
B
bernard.xiong@gmail.com 已提交
525

526 527 528
	return module;
}

D
dzzxzz 已提交
529
static struct rt_module* _load_relocated_object(const char *name, void *module_ptr)
530 531 532
{
	rt_uint32_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
	rt_uint32_t module_addr = 0, module_size = 0;
D
dzzxzz 已提交
533
	struct rt_module *module = RT_NULL;
534 535
	rt_uint8_t *ptr, *strtab, *shstrab;
	rt_bool_t linked = RT_FALSE;
B
bernard.xiong@gmail.com 已提交
536

537 538 539 540 541
	if(rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
	{
		/* rtmlinker finished */
		linked = RT_TRUE;
	}
B
bernard.xiong@gmail.com 已提交
542

543 544 545 546 547 548 549 550 551 552 553 554 555
	/* get the ELF image size */
	for (index = 0; index < elf_module->e_shnum; index++)
	{
		/* text */
		if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
		{
			module_size += shdr[index].sh_size;
			module_addr = shdr[index].sh_addr;
		}
		/* rodata */
		if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
		{
			module_size += shdr[index].sh_size;
B
bernard.xiong@gmail.com 已提交
556
		}
557 558 559 560 561 562 563 564 565
		/* data */
		if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
		{
			module_size += shdr[index].sh_size;
		}
		/* bss */
		if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
		{
			module_size += shdr[index].sh_size;
B
bernard.xiong@gmail.com 已提交
566
		}
567 568 569
	}

	/* no text, data and bss on image */
D
dzzxzz 已提交
570 571
	if (module_size == 0)
		return RT_NULL;
572 573

	/* allocate module */
D
dzzxzz 已提交
574 575 576
	module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char *)name);
	if (module == RT_NULL)
		return RT_NULL;
577 578 579 580 581 582

	/* allocate module space */
	module->module_space = rt_malloc(module_size);
	if (module->module_space == RT_NULL)
	{
		rt_object_delete(&(module->parent));
D
dzzxzz 已提交
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		return RT_NULL;
	}

	/* zero all space */
	ptr = module->module_space;
	rt_memset(ptr, 0, module_size);

	/* load text and data section */
	for (index = 0; index < elf_module->e_shnum; index++)
	{
		/* load text section */
		if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
		{
			rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
			RT_DEBUG_LOG(RT_DEBUG_MODULE,("load text 0x%x, size %d\n", ptr, shdr[index].sh_size));
			ptr += shdr[index].sh_size;
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
601

602 603 604 605 606 607 608 609
		/* load rodata section */
		if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
		{
			rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
			rodata_addr = (rt_uint32_t)ptr;
			RT_DEBUG_LOG(RT_DEBUG_MODULE,("load rodata 0x%x, size %d, rodata 0x%x\n", ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
			ptr += shdr[index].sh_size;
		}
B
bernard.xiong@gmail.com 已提交
610

611 612 613
		/* load data section */
		if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
		{
B
bernard.xiong@gmail.com 已提交
614
			rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
615 616 617 618 619 620 621 622 623 624
			data_addr = (rt_uint32_t)ptr;
			RT_DEBUG_LOG(RT_DEBUG_MODULE,("load data 0x%x, size %d, data 0x%x\n", ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
			ptr += shdr[index].sh_size;
		}

		/* load bss section */
		if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
		{
			rt_memset(ptr, 0, shdr[index].sh_size);
			bss_addr = (rt_uint32_t)ptr;
B
bernard.xiong@gmail.com 已提交
625
			RT_DEBUG_LOG(RT_DEBUG_MODULE,("load bss 0x%x, size %d,\n", ptr, shdr[index].sh_size));
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
		}
	}

	/* set module entry */
	module->module_entry = (rt_uint8_t*)module->module_space + elf_module->e_entry - module_addr;

	/* handle relocation section */
	for (index = 0; index < elf_module->e_shnum; index ++)
	{
		if (IS_REL(shdr[index]))
		{
			rt_uint32_t i, nr_reloc;
			Elf32_Sym *symtab;
			Elf32_Rel *rel;

			/* get relocate item */
			rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);

			/* locate .dynsym and .dynstr */
			symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset);
			strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
			shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
			nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));

			/* relocate every items */
			for (i = 0; i < nr_reloc; i ++)
			{
				Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
				RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n", strtab + sym->st_name));
B
bernard.xiong@gmail.com 已提交
655

656
				if (sym->st_shndx != STN_UNDEF)
B
bernard.xiong@gmail.com 已提交
657 658
				{
					if((ELF_ST_TYPE(sym->st_info) == STT_SECTION)
659
						|| (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
B
bernard.xiong@gmail.com 已提交
660
					{
661 662 663 664 665 666 667 668
						if (rt_strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_RODATA, 8) == 0)
						{
							/* relocate rodata section */
							RT_DEBUG_LOG(RT_DEBUG_MODULE,("rodata\n"));
							rt_module_arm_relocate(module, rel,(Elf32_Addr)(rodata_addr + sym->st_value));
						}
						else if(strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_BSS, 5) == 0)
						{
B
bernard.xiong@gmail.com 已提交
669
							/* relocate bss section */
670 671 672 673 674
							RT_DEBUG_LOG(RT_DEBUG_MODULE,("bss\n"));
							rt_module_arm_relocate(module, rel, (Elf32_Addr)bss_addr + sym->st_value);
						}
						else if(strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_DATA, 6) == 0)
						{
B
bernard.xiong@gmail.com 已提交
675
							/* relocate data section */
676 677
							RT_DEBUG_LOG(RT_DEBUG_MODULE,("data\n"));
							rt_module_arm_relocate(module, rel, (Elf32_Addr)data_addr + sym->st_value);
B
bernard.xiong@gmail.com 已提交
678 679 680
						}
					}
				}
681
				else if(ELF_ST_TYPE(sym->st_info) == STT_FUNC )
B
bernard.xiong@gmail.com 已提交
682
				{
683 684
					/* relocate function */
					rt_module_arm_relocate(module, rel,
B
bernard.xiong@gmail.com 已提交
685
						(Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value));
686 687 688 689 690 691
				}
				else
				{
					Elf32_Addr addr;

					if(ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
B
bernard.xiong@gmail.com 已提交
692
					{
693 694 695 696 697 698 699
						RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n", strtab + sym->st_name));
						/* need to resolve symbol in kernel symbol table */
						addr = rt_module_symbol_find(strtab + sym->st_name);
						if (addr != (Elf32_Addr)RT_NULL)
						{
							rt_module_arm_relocate(module, rel, addr);
							RT_DEBUG_LOG(RT_DEBUG_MODULE,("symbol addr 0x%x\n", addr));
B
bernard.xiong@gmail.com 已提交
700
						}
D
dzzxzz 已提交
701 702
						else
							rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
					}
					else
					{
						rt_module_arm_relocate(module, rel, addr);
					}
				}
				rel ++;
			}
		}
	}

	return module;
}

/**
 * This function will load a module from memory and create a thread for it
 *
 * @param name the name of module, which shall be unique
 * @param module_ptr the memory address of module image
 *
 * @return the module object
 */
D
dzzxzz 已提交
725
rt_module_t rt_module_load(const char *name, void *module_ptr)
726 727
{
	rt_module_t module;
B
bernard.xiong@gmail.com 已提交
728

729 730 731 732 733 734 735 736 737
	RT_DEBUG_NOT_IN_INTERRUPT;

	rt_kprintf("rt_module_load: %s ,", name);

	/* check ELF header */
	if(rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) != 0
		&& rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
	{
		rt_kprintf(" module magic error\n");
D
dzzxzz 已提交
738

739 740 741 742 743 744 745
		return RT_NULL;
	}

	/* check ELF class */
	if(elf_module->e_ident[EI_CLASS] != ELFCLASS32)
	{
		rt_kprintf(" module class error\n");
D
dzzxzz 已提交
746

747 748
		return RT_NULL;
	}
B
bernard.xiong@gmail.com 已提交
749

750 751 752 753 754 755 756 757 758 759 760
	if(elf_module->e_type == ET_REL)
	{
		module = _load_relocated_object(name, module_ptr);
	}
	else if(elf_module->e_type == ET_DYN)
	{
		module = _load_shared_object(name, module_ptr);
	}
	else
	{
		rt_kprintf("unsupported elf type\n");
D
dzzxzz 已提交
761

762 763 764
		return RT_NULL;
	}

D
dzzxzz 已提交
765 766
	if(module == RT_NULL)
		return RT_NULL;
B
bernard.xiong@gmail.com 已提交
767

qiuyiuestc's avatar
qiuyiuestc 已提交
768 769
	/* init module object container */
	rt_module_init_object_container(module);
qiuyiuestc's avatar
qiuyiuestc 已提交
770
		
D
dzzxzz 已提交
771 772
	/* increase module reference count */
	module->nref ++;
qiuyiuestc's avatar
qiuyiuestc 已提交
773

D
dzzxzz 已提交
774
	if (elf_module->e_entry != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
775
	{	
qiuyiuestc's avatar
qiuyiuestc 已提交
776
#ifdef RT_USING_SLAB
qiuyiuestc's avatar
qiuyiuestc 已提交
777
		/* init module memory allocator */
qiuyiuestc's avatar
qiuyiuestc 已提交
778
		module->mem_list = RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
779 780 781 782

		/* create page array */
		module->page_array = (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
		module->page_cnt = 0;
783 784
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
785
		/* create module thread */
qiuyiuestc's avatar
qiuyiuestc 已提交
786
		module->stack_size = 2048;
qiuyiuestc's avatar
qiuyiuestc 已提交
787
		module->thread_priority = 25;
qiuyiuestc's avatar
qiuyiuestc 已提交
788 789 790 791 792 793
		module->module_thread = rt_thread_create(name,
			module->module_entry, RT_NULL,
			module->stack_size,
			module->thread_priority, 10);
		
		module->module_thread->module_id = (void*)module;
qiuyiuestc's avatar
qiuyiuestc 已提交
794
		module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
B
bernard.xiong@gmail.com 已提交
795

qiuyiuestc's avatar
qiuyiuestc 已提交
796 797 798 799 800 801 802 803 804
		/* startup module thread */
		rt_thread_startup(module->module_thread);
	}	
	else
	{
		/* without entry point */
		module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
	}	

qiuyiuestc's avatar
qiuyiuestc 已提交
805
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
806
	if (rt_module_load_hook != RT_NULL)
qiuyiuestc's avatar
qiuyiuestc 已提交
807 808 809 810 811
	{
		rt_module_load_hook(module);
	}
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
812
	return module;
qiuyiuestc's avatar
qiuyiuestc 已提交
813 814
}	

qiuyiuestc's avatar
qiuyiuestc 已提交
815 816 817
#ifdef RT_USING_DFS
#include <dfs_posix.h>
/**
B
bernard.xiong@gmail.com 已提交
818
 * This function will load a module from a file
qiuyiuestc's avatar
qiuyiuestc 已提交
819
 *
B
bernard.xiong@gmail.com 已提交
820
 * @param path the full path of application module
qiuyiuestc's avatar
qiuyiuestc 已提交
821 822 823
 *
 * @return the module object
 */
D
dzzxzz 已提交
824
rt_module_t rt_module_open(const char *path)
qiuyiuestc's avatar
qiuyiuestc 已提交
825 826
{
	int fd, length;
D
dzzxzz 已提交
827
	struct rt_module *module;
B
bernard.xiong@gmail.com 已提交
828
	struct stat s;
qiuyiuestc's avatar
qiuyiuestc 已提交
829
	char *buffer, *offset_ptr;
qiuyiuestc's avatar
qiuyiuestc 已提交
830

831
	RT_DEBUG_NOT_IN_INTERRUPT;
832

qiuyiuestc's avatar
qiuyiuestc 已提交
833
	/* check parameters */
qiuyiuestc's avatar
qiuyiuestc 已提交
834
	RT_ASSERT(path != RT_NULL);
qiuyiuestc's avatar
qiuyiuestc 已提交
835

qiuyiuestc's avatar
qiuyiuestc 已提交
836
	if (stat(path, &s) !=0)
837
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
838
		rt_kprintf("access %s failed\n", path);
D
dzzxzz 已提交
839

840 841
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
842
	buffer = (char *)rt_malloc(s.st_size);
843 844 845
	if (buffer == RT_NULL)
	{
		rt_kprintf("out of memory\n");
D
dzzxzz 已提交
846

847 848 849 850
		return RT_NULL;
	}

	offset_ptr = buffer;
qiuyiuestc's avatar
qiuyiuestc 已提交
851
	fd = open(path, O_RDONLY, 0);
852 853
	if (fd < 0)
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
854
		rt_kprintf("open %s failed\n", path);
855
		rt_free(buffer);
D
dzzxzz 已提交
856

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
		return RT_NULL;
	}

	do
	{
		length = read(fd, offset_ptr, 4096);
		if (length > 0)
		{
			offset_ptr += length;
		}
	}while (length > 0);

	/* close fd */
	close(fd);

	if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
qiuyiuestc's avatar
qiuyiuestc 已提交
873 874 875
	{
		rt_kprintf("check: read file failed\n");
		rt_free(buffer);
D
dzzxzz 已提交
876

qiuyiuestc's avatar
qiuyiuestc 已提交
877 878
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
879

qiuyiuestc's avatar
qiuyiuestc 已提交
880
	module = rt_module_load(path, (void *)buffer);
qiuyiuestc's avatar
qiuyiuestc 已提交
881 882 883 884
	rt_free(buffer);

	return module;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
885 886 887

#if defined(RT_USING_FINSH)
#include <finsh.h>
888
FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from file);
qiuyiuestc's avatar
qiuyiuestc 已提交
889
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
890 891
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
892
/**
D
dzzxzz 已提交
893
 * This function will unload a module from memory and release resources
qiuyiuestc's avatar
qiuyiuestc 已提交
894 895 896 897 898 899
 *
 * @param module the module to be unloaded
 *
 * @return the operation status, RT_EOK on OK; -RT_ERROR on error
 */
rt_err_t rt_module_unload(rt_module_t module)
qiuyiuestc's avatar
qiuyiuestc 已提交
900
{
901
	int i;
D
dzzxzz 已提交
902
	struct rt_object *object;
qiuyiuestc's avatar
qiuyiuestc 已提交
903
	struct rt_list_node *list;
qiuyiuestc's avatar
qiuyiuestc 已提交
904

905
	RT_DEBUG_NOT_IN_INTERRUPT;
906

qiuyiuestc's avatar
qiuyiuestc 已提交
907 908 909
	/* check parameter */
	RT_ASSERT(module != RT_NULL);

qiuyiuestc's avatar
qiuyiuestc 已提交
910
	rt_kprintf("rt_module_unload: %s\n", module->parent.name);
B
bernard.xiong@gmail.com 已提交
911

qiuyiuestc's avatar
qiuyiuestc 已提交
912
	/* module has entry point */
qiuyiuestc's avatar
qiuyiuestc 已提交
913
	if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
D
dzzxzz 已提交
914
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
915
		/* suspend module main thread */
D
dzzxzz 已提交
916 917
		if (module->module_thread != RT_NULL)
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
918 919 920 921 922
			if (module->module_thread->stat == RT_THREAD_READY)
				rt_thread_suspend(module->module_thread);
		}
		
		/* delete threads */
D
dzzxzz 已提交
923 924
		list = &module->module_object[RT_Object_Class_Thread].object_list;
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
925
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
926 927 928
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
929
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
930 931 932
				rt_thread_detach((rt_thread_t)object);
			}
			else
D
dzzxzz 已提交
933
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
934 935
				/* delete dynamic object */
				rt_thread_delete((rt_thread_t)object);
D
dzzxzz 已提交
936
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
937
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
938
		
qiuyiuestc's avatar
qiuyiuestc 已提交
939
#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
940 941
		/* delete semaphores */
		list = &module->module_object[RT_Object_Class_Thread].object_list;	
D
dzzxzz 已提交
942
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
943
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
944 945 946
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
947
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
948 949 950
				rt_sem_detach((rt_sem_t)object);
			}
			else
D
dzzxzz 已提交
951
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
952 953
				/* delete dynamic object */
				rt_sem_delete((rt_sem_t)object);
D
dzzxzz 已提交
954
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
955 956 957 958
		}
#endif

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
959 960
		/* delete mutexs*/
		list = &module->module_object[RT_Object_Class_Mutex].object_list;	
D
dzzxzz 已提交
961
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
962
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
963 964 965
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
966
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
967 968 969
				rt_mutex_detach((rt_mutex_t)object);
			}
			else
D
dzzxzz 已提交
970
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
971 972
				/* delete dynamic object */
				rt_mutex_delete((rt_mutex_t)object);
D
dzzxzz 已提交
973
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
974 975 976 977
		}
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
978 979
		/* delete mailboxs */
		list = &module->module_object[RT_Object_Class_Event].object_list;	
D
dzzxzz 已提交
980
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
981
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
982 983 984
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
985
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
986 987 988
				rt_event_detach((rt_event_t)object);
			}
			else
D
dzzxzz 已提交
989
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
990 991
				/* delete dynamic object */
				rt_event_delete((rt_event_t)object);
D
dzzxzz 已提交
992
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
993
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
994 995 996
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
997 998
		/* delete mailboxs */
		list = &module->module_object[RT_Object_Class_MailBox].object_list;	
D
dzzxzz 已提交
999
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1000
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1001 1002 1003
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1004
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1005 1006 1007
				rt_mb_detach((rt_mailbox_t)object);
			}
			else
D
dzzxzz 已提交
1008
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1009 1010
				/* delete dynamic object */
				rt_mb_delete((rt_mailbox_t)object);
D
dzzxzz 已提交
1011
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1012 1013 1014 1015
		}
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
1016
		/* delete msgqueues */
D
dzzxzz 已提交
1017 1018
		list = &module->module_object[RT_Object_Class_MessageQueue].object_list;
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1019
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1020 1021 1022
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1023
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1024 1025 1026
				rt_mq_detach((rt_mq_t)object);
			}
			else
D
dzzxzz 已提交
1027
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1028 1029
				/* delete dynamic object */
				rt_mq_delete((rt_mq_t)object);
D
dzzxzz 已提交
1030
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1031 1032 1033 1034
		}
#endif

#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
1035 1036
		/* delete mempools */
		list = &module->module_object[RT_Object_Class_MemPool].object_list;	
D
dzzxzz 已提交
1037
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1038
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1039 1040 1041
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1042
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1043 1044 1045
				rt_mp_detach((rt_mp_t)object);
			}
			else
D
dzzxzz 已提交
1046
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1047 1048
				/* delete dynamic object */
				rt_mp_delete((rt_mp_t)object);
D
dzzxzz 已提交
1049
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1050 1051 1052 1053
		}
#endif

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
1054 1055
		/* delete devices */
		list = &module->module_object[RT_Object_Class_Device].object_list;	
D
dzzxzz 已提交
1056
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1057 1058 1059
		{
			object = rt_list_entry(list->next, struct rt_object, list);
			rt_device_unregister((rt_device_t)object);
D
dzzxzz 已提交
1060
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1061 1062
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
1063 1064
		/* delete timers */
		list = &module->module_object[RT_Object_Class_Timer].object_list;
D
dzzxzz 已提交
1065
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1066
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1067 1068 1069
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1070
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1071 1072 1073
				rt_timer_detach((rt_timer_t)object);
			}
			else
D
dzzxzz 已提交
1074
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1075 1076
				/* delete dynamic object */
				rt_timer_delete((rt_timer_t)object);
D
dzzxzz 已提交
1077 1078
			}
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1079
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1080

1081
#ifdef RT_USING_SLAB
D
dzzxzz 已提交
1082
	if (module->page_cnt > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1083
	{
D
dzzxzz 已提交
1084
		struct rt_page_info *page = (struct rt_page_info *)module->page_array;
B
bernard.xiong@gmail.com 已提交
1085 1086

		rt_kprintf("warning: module memory still hasn't been free finished\n");
qiuyiuestc's avatar
qiuyiuestc 已提交
1087

1088
		while(module->page_cnt != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1089
		{
1090
			rt_module_free_page(module, page[0].page_ptr, page[0].npage);
B
bernard.xiong@gmail.com 已提交
1091
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1092
	}
1093
#endif
D
dzzxzz 已提交
1094

qiuyiuestc's avatar
qiuyiuestc 已提交
1095
	/* release module space memory */
qiuyiuestc's avatar
qiuyiuestc 已提交
1096
	rt_free(module->module_space);
qiuyiuestc's avatar
qiuyiuestc 已提交
1097

1098
	/* release module symbol table */
D
dzzxzz 已提交
1099 1100 1101 1102
	for (i=0; i<module->nsym; i++)
		rt_free((void *)module->symtab[i].name);
	if (module->symtab != RT_NULL)
		rt_free(module->symtab);
qiuyiuestc's avatar
qiuyiuestc 已提交
1103

qiuyiuestc's avatar
qiuyiuestc 已提交
1104
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
1105 1106 1107 1108
	if (rt_module_unload_hook != RT_NULL)
	{
		rt_module_unload_hook(module);
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1109 1110
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
1111 1112 1113 1114
#ifdef RT_USING_SLAB
	if(module->page_array != RT_NULL) 
		rt_free(module->page_array);
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
1115

qiuyiuestc's avatar
qiuyiuestc 已提交
1116
	/* delete module object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1117
	rt_object_delete((rt_object_t)module);
qiuyiuestc's avatar
qiuyiuestc 已提交
1118 1119

	return RT_EOK;
qiuyiuestc's avatar
qiuyiuestc 已提交
1120 1121
}

qiuyiuestc's avatar
qiuyiuestc 已提交
1122 1123 1124 1125 1126 1127 1128
/**
 * This function will find the specified module.
 *
 * @param name the name of module finding
 *
 * @return the module
 */
D
dzzxzz 已提交
1129
rt_module_t rt_module_find(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
1130
{
B
bernard.xiong 已提交
1131
	struct rt_object_information *information;
D
dzzxzz 已提交
1132 1133
	struct rt_object *object;
	struct rt_list_node *node;
qiuyiuestc's avatar
qiuyiuestc 已提交
1134

B
bernard.xiong 已提交
1135 1136
	extern struct rt_object_information rt_object_container[];

1137
	RT_DEBUG_NOT_IN_INTERRUPT;
1138

B
bernard.xiong 已提交
1139 1140 1141 1142
	/* enter critical */
	rt_enter_critical();

	/* try to find device object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1143
	information = &rt_object_container[RT_Object_Class_Module];
B
bernard.xiong 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	for (node = information->object_list.next; node != &(information->object_list); node = node->next)
	{
		object = rt_list_entry(node, struct rt_object, list);
		if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
		{
			/* leave critical */
			rt_exit_critical();

			return (rt_module_t)object;
		}
	}

	/* leave critical */
	rt_exit_critical();

	/* not found */
	return RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
1161 1162
}

qiuyiuestc's avatar
qiuyiuestc 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171
#ifdef RT_USING_SLAB
/*
 * This function will allocate the numbers page with specified size
 * in page memory.
 *
 * @param size the size of memory to be allocated.
 * @note this function is used for RT-Thread Application Module
 */
static void *rt_module_malloc_page(rt_size_t npages)
qiuyiuestc's avatar
qiuyiuestc 已提交
1172
{
D
dzzxzz 已提交
1173
	void *chunk;
qiuyiuestc's avatar
qiuyiuestc 已提交
1174
	struct rt_page_info *page;
qiuyiuestc's avatar
qiuyiuestc 已提交
1175

qiuyiuestc's avatar
qiuyiuestc 已提交
1176
	chunk = rt_page_alloc(npages);
D
dzzxzz 已提交
1177 1178
	if (chunk == RT_NULL)
		return RT_NULL;
1179

D
dzzxzz 已提交
1180
	page = (struct rt_page_info *)rt_current_module->page_array;
qiuyiuestc's avatar
qiuyiuestc 已提交
1181
	page[rt_current_module->page_cnt].page_ptr = chunk;
D
dzzxzz 已提交
1182 1183
	page[rt_current_module->page_cnt].npage = npages;
	rt_current_module->page_cnt ++;
qiuyiuestc's avatar
qiuyiuestc 已提交
1184

qiuyiuestc's avatar
qiuyiuestc 已提交
1185
	RT_ASSERT(rt_current_module->page_cnt <= PAGE_COUNT_MAX);
1186
	rt_kprintf("rt_module_malloc_page 0x%x %d\n", chunk, npages);
D
dzzxzz 已提交
1187

qiuyiuestc's avatar
qiuyiuestc 已提交
1188 1189
	return chunk;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
1190

qiuyiuestc's avatar
qiuyiuestc 已提交
1191
/*
D
dzzxzz 已提交
1192
 * This function will release the previously allocated memory page
qiuyiuestc's avatar
qiuyiuestc 已提交
1193 1194 1195 1196
 * by rt_malloc_page.
 *
 * @param page_ptr the page address to be released.
 * @param npages the number of page shall be released.
B
bernard.xiong@gmail.com 已提交
1197
 *
qiuyiuestc's avatar
qiuyiuestc 已提交
1198 1199
 * @note this function is used for RT-Thread Application Module
 */
1200
static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t npages)
qiuyiuestc's avatar
qiuyiuestc 已提交
1201 1202 1203
{
	int i, index;
	struct rt_page_info *page;
qiuyiuestc's avatar
qiuyiuestc 已提交
1204

1205
	rt_kprintf("rt_module_free_page 0x%x %d\n", page_ptr, npages);
qiuyiuestc's avatar
qiuyiuestc 已提交
1206 1207
	rt_page_free(page_ptr, npages);

1208
	page = (struct rt_page_info*)module->page_array;
qiuyiuestc's avatar
qiuyiuestc 已提交
1209

1210
	for(i=0; i<module->page_cnt; i++)
qiuyiuestc's avatar
qiuyiuestc 已提交
1211
	{
D
dzzxzz 已提交
1212
		if (page[i].page_ptr == page_ptr)
qiuyiuestc's avatar
qiuyiuestc 已提交
1213
		{
D
dzzxzz 已提交
1214
			if (page[i].npage == npages + 1)
qiuyiuestc's avatar
qiuyiuestc 已提交
1215 1216
			{
				page[i].page_ptr += npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t);
1217
				page[i].npage -= npages;
qiuyiuestc's avatar
qiuyiuestc 已提交
1218
			}
1219
			else if(page[i].npage == npages)
B
bernard.xiong@gmail.com 已提交
1220
			{
1221
				for(index=i; index<module->page_cnt-1; index++)
qiuyiuestc's avatar
qiuyiuestc 已提交
1222 1223 1224 1225
				{
					page[index].page_ptr = page[index + 1].page_ptr;
					page[index].npage = page[index + 1].npage;
				}
1226 1227
				page[module->page_cnt - 1].page_ptr = RT_NULL;
				page[module->page_cnt - 1].npage = 0;
B
bernard.xiong@gmail.com 已提交
1228 1229

				module->page_cnt--;
qiuyiuestc's avatar
qiuyiuestc 已提交
1230
			}
D
dzzxzz 已提交
1231 1232
			else
				RT_ASSERT(RT_FALSE);
1233
			rt_current_module->page_cnt--;
qiuyiuestc's avatar
qiuyiuestc 已提交
1234

qiuyiuestc's avatar
qiuyiuestc 已提交
1235 1236 1237 1238 1239 1240
			return;
		}
	}

	/* should not be get here */
	RT_ASSERT(RT_FALSE);
qiuyiuestc's avatar
qiuyiuestc 已提交
1241
}
qiuyiuestc's avatar
qiuyiuestc 已提交
1242 1243

/*
B
bernard.xiong@gmail.com 已提交
1244
  rt_module_malloc - allocate memory block in free list
qiuyiuestc's avatar
qiuyiuestc 已提交
1245 1246 1247
*/
void *rt_module_malloc(rt_size_t size)
{
qiuyiuestc's avatar
qiuyiuestc 已提交
1248
	struct rt_mem_head *b, *n, *up;
qiuyiuestc's avatar
qiuyiuestc 已提交
1249
	struct rt_mem_head **prev;
D
dzzxzz 已提交
1250
	rt_uint32_t npage;
qiuyiuestc's avatar
qiuyiuestc 已提交
1251
	rt_size_t nunits;
1252

1253
	RT_DEBUG_NOT_IN_INTERRUPT;
1254

D
dzzxzz 已提交
1255
	nunits = (size + sizeof(struct rt_mem_head) -1)/sizeof(struct rt_mem_head) + 1;
qiuyiuestc's avatar
qiuyiuestc 已提交
1256 1257 1258 1259

	RT_ASSERT(size != 0);
	RT_ASSERT(nunits != 0);

qiuyiuestc's avatar
qiuyiuestc 已提交
1260
	rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
qiuyiuestc's avatar
qiuyiuestc 已提交
1261

D
dzzxzz 已提交
1262
	for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
qiuyiuestc's avatar
qiuyiuestc 已提交
1263
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1264 1265
		if (b->size > nunits)
		{
B
bernard.xiong@gmail.com 已提交
1266
			/* split memory */
qiuyiuestc's avatar
qiuyiuestc 已提交
1267 1268 1269 1270 1271
			n = b + nunits;
			n->next = b->next;
			n->size = b->size - nunits;
			b->size = nunits;
			*prev = n;
qiuyiuestc's avatar
qiuyiuestc 已提交
1272

1273
			rt_kprintf("rt_module_malloc 0x%x, %d\n",b + 1, size);
qiuyiuestc's avatar
qiuyiuestc 已提交
1274
			rt_sem_release(&mod_sem);
D
dzzxzz 已提交
1275

qiuyiuestc's avatar
qiuyiuestc 已提交
1276
			return (void *)(b + 1);
qiuyiuestc's avatar
qiuyiuestc 已提交
1277 1278 1279 1280 1281 1282
		}

		if (b->size == nunits)
		{
			/* this node fit, remove this node */
			*prev = b->next;
qiuyiuestc's avatar
qiuyiuestc 已提交
1283 1284

			rt_kprintf("rt_module_malloc 0x%x, %d\n",b + 1, size);
B
bernard.xiong@gmail.com 已提交
1285

qiuyiuestc's avatar
qiuyiuestc 已提交
1286
			rt_sem_release(&mod_sem);
D
dzzxzz 已提交
1287

qiuyiuestc's avatar
qiuyiuestc 已提交
1288
			return (void *)(b + 1);
qiuyiuestc's avatar
qiuyiuestc 已提交
1289
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1290
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1291

qiuyiuestc's avatar
qiuyiuestc 已提交
1292 1293
	/* allocate pages from system heap */
	npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1294 1295
	if ((up = (struct rt_mem_head *)rt_module_malloc_page(npage)) == RT_NULL)
		return RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
1296 1297

	up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
B
bernard.xiong@gmail.com 已提交
1298 1299

	for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
qiuyiuestc's avatar
qiuyiuestc 已提交
1300
	{
D
dzzxzz 已提交
1301 1302
		if (b > up + up->size)
			break;
qiuyiuestc's avatar
qiuyiuestc 已提交
1303 1304
	}

qiuyiuestc's avatar
qiuyiuestc 已提交
1305 1306 1307 1308
	up->next = b;
	*prev = up;

	rt_sem_release(&mod_sem);
D
dzzxzz 已提交
1309

qiuyiuestc's avatar
qiuyiuestc 已提交
1310
	return rt_module_malloc(size);
qiuyiuestc's avatar
qiuyiuestc 已提交
1311 1312 1313
}

/*
qiuyiuestc's avatar
qiuyiuestc 已提交
1314
  rt_module_free - free memory block in free list
qiuyiuestc's avatar
qiuyiuestc 已提交
1315 1316 1317
*/
void rt_module_free(rt_module_t module, void *addr)
{
qiuyiuestc's avatar
qiuyiuestc 已提交
1318
	struct rt_mem_head *b, *n, *r;
qiuyiuestc's avatar
qiuyiuestc 已提交
1319
	struct rt_mem_head **prev;
1320

1321
	RT_DEBUG_NOT_IN_INTERRUPT;
1322

qiuyiuestc's avatar
qiuyiuestc 已提交
1323
	RT_ASSERT(addr);
qiuyiuestc's avatar
qiuyiuestc 已提交
1324 1325
	RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0);

1326
	rt_kprintf("rt_module_free 0x%x\n", addr);
D
dzzxzz 已提交
1327

qiuyiuestc's avatar
qiuyiuestc 已提交
1328
	rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
D
dzzxzz 已提交
1329

qiuyiuestc's avatar
qiuyiuestc 已提交
1330
	n = (struct rt_mem_head *)addr - 1;
qiuyiuestc's avatar
qiuyiuestc 已提交
1331
	prev = (struct rt_mem_head **)&module->mem_list;
qiuyiuestc's avatar
qiuyiuestc 已提交
1332 1333

	while ((b = *prev) != RT_NULL)
D
dzzxzz 已提交
1334
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1335 1336 1337
		RT_ASSERT(b->size > 0);
		RT_ASSERT(b > n || b + b->size <= n);

D
dzzxzz 已提交
1338
		if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0))
qiuyiuestc's avatar
qiuyiuestc 已提交
1339
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1340
			if (b + (b->size + n->size) == b->next)
qiuyiuestc's avatar
qiuyiuestc 已提交
1341
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1342 1343
				b->size += b->next->size + n->size;
				b->next = b->next->next;
qiuyiuestc's avatar
qiuyiuestc 已提交
1344
			}
D
dzzxzz 已提交
1345 1346
			else
				b->size += n->size;
qiuyiuestc's avatar
qiuyiuestc 已提交
1347

D
dzzxzz 已提交
1348
			if ((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1349 1350
			{
				int npage = b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1351
				if (npage > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1352
				{
D
dzzxzz 已提交
1353
					if ((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
					{
						rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
						/* split memory */
						r = b + nunits;
						r->next = b->next;
						r->size = b->size - nunits;
						*prev = r;
					}
					else
					{
D
dzzxzz 已提交
1364
						*prev = b->next;
qiuyiuestc's avatar
qiuyiuestc 已提交
1365
					}
B
bernard.xiong@gmail.com 已提交
1366

1367
					rt_module_free_page(module, b, npage);
qiuyiuestc's avatar
qiuyiuestc 已提交
1368
				}
B
bernard.xiong@gmail.com 已提交
1369
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1370

qiuyiuestc's avatar
qiuyiuestc 已提交
1371 1372
			/* unlock */
			rt_sem_release(&mod_sem);
D
dzzxzz 已提交
1373

qiuyiuestc's avatar
qiuyiuestc 已提交
1374 1375 1376 1377 1378 1379 1380
			return;
		}

		if (b == n + n->size)
		{
			n->size = b->size + n->size;
			n->next = b->next;
B
bernard.xiong@gmail.com 已提交
1381

D
dzzxzz 已提交
1382
			if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1383 1384
			{
				int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1385
				if (npage > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1386
				{
D
dzzxzz 已提交
1387
					if ((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1388 1389 1390 1391 1392 1393 1394 1395
					{
						rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
						/* split memory */
						r = n + nunits;
						r->next = n->next;
						r->size = n->size - nunits;
						*prev = r;
					}
D
dzzxzz 已提交
1396
					else *prev = n->next;
B
bernard.xiong@gmail.com 已提交
1397

1398
					rt_module_free_page(module, n, npage);
qiuyiuestc's avatar
qiuyiuestc 已提交
1399
				}
D
dzzxzz 已提交
1400
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1401 1402 1403 1404 1405 1406 1407
			else
			{
				*prev = n;
			}

			/* unlock */
			rt_sem_release(&mod_sem);
qiuyiuestc's avatar
qiuyiuestc 已提交
1408 1409 1410

			return;
		}
D
dzzxzz 已提交
1411 1412
		if (b > n + n->size)
			break;
qiuyiuestc's avatar
qiuyiuestc 已提交
1413 1414 1415 1416

		prev = &(b->next);
	}

D
dzzxzz 已提交
1417
	if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1418 1419
	{
		int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1420
		if (npage > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1421
		{
1422
			rt_module_free_page(module, n, npage);
D
dzzxzz 已提交
1423
			if (n->size % RT_MM_PAGE_SIZE != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
			{
				rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
				/* split memory */
				r = n + nunits;
				r->next = b;
				r->size = n->size - nunits;
				*prev = r;
			}
			else
			{
B
bernard.xiong@gmail.com 已提交
1434 1435 1436
				*prev = b;
			}
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1437
	}
D
dzzxzz 已提交
1438 1439
	else
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1440 1441 1442
		n->next = b;
		*prev = n;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1443

qiuyiuestc's avatar
qiuyiuestc 已提交
1444
	/* unlock */
D
dzzxzz 已提交
1445
	rt_sem_release(&mod_sem);
qiuyiuestc's avatar
qiuyiuestc 已提交
1446 1447 1448 1449 1450 1451 1452
}

/*
  rt_module_realloc - realloc memory block in free list
*/
void *rt_module_realloc(void *ptr, rt_size_t size)
{
qiuyiuestc's avatar
qiuyiuestc 已提交
1453 1454
	struct rt_mem_head *b, *p, *prev, *tmpp;
	rt_size_t nunits;
qiuyiuestc's avatar
qiuyiuestc 已提交
1455

1456
	RT_DEBUG_NOT_IN_INTERRUPT;
1457

D
dzzxzz 已提交
1458 1459
	if (!ptr)
		return rt_module_malloc(size);
qiuyiuestc's avatar
qiuyiuestc 已提交
1460 1461 1462
	if (size == 0)
	{
		rt_module_free(rt_current_module, ptr);
D
dzzxzz 已提交
1463

qiuyiuestc's avatar
qiuyiuestc 已提交
1464 1465
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1466

qiuyiuestc's avatar
qiuyiuestc 已提交
1467 1468 1469
	nunits = (size + sizeof(struct rt_mem_head) - 1) / sizeof(struct rt_mem_head) + 1;
	b = (struct rt_mem_head *)ptr - 1;

D
dzzxzz 已提交
1470 1471 1472
	if (nunits <= b->size)
	{
		/* new size is smaller or equal then before */
D
dzzxzz 已提交
1473 1474
		if (nunits == b->size)
			return ptr;
D
dzzxzz 已提交
1475
		else
qiuyiuestc's avatar
qiuyiuestc 已提交
1476 1477 1478 1479 1480
		{
			p = b + nunits;
			p->size = b->size - nunits;
			b->size = nunits;
			rt_module_free(rt_current_module, (void *)(p + 1));
D
dzzxzz 已提交
1481

qiuyiuestc's avatar
qiuyiuestc 已提交
1482 1483 1484
			return (void *)(b + 1);
		}
	}
D
dzzxzz 已提交
1485 1486
	else
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1487
		/* more space then required */
qiuyiuestc's avatar
qiuyiuestc 已提交
1488
		prev = (struct rt_mem_head *)rt_current_module->mem_list;
D
dzzxzz 已提交
1489 1490
		for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next)
			break;
qiuyiuestc's avatar
qiuyiuestc 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503

		/* available block after ap in freelist */ 
		if (p != RT_NULL && (p->size >= (nunits - (b->size))) &&  p == (b + b->size))  
		{
			/* perfect match */
			if (p->size == (nunits - (b->size)))  
			{
				b->size = nunits;
				prev->next = p->next;
			}
			else  /* more space then required, split block*/
			{
				/* pointer to old header */
D
dzzxzz 已提交
1504
				tmpp = p;
qiuyiuestc's avatar
qiuyiuestc 已提交
1505 1506 1507 1508 1509 1510
				p = b + nunits;

				/* restoring old pointer */
				p->next = tmpp->next;
				
				/* new size for p */
D
dzzxzz 已提交
1511
				p->size = tmpp->size + b->size - nunits;
qiuyiuestc's avatar
qiuyiuestc 已提交
1512 1513 1514
				b->size = nunits;
				prev->next = p;
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1515
			rt_current_module->mem_list = (void *)prev;
D
dzzxzz 已提交
1516

D
dzzxzz 已提交
1517
			return (void *)(b + 1);
qiuyiuestc's avatar
qiuyiuestc 已提交
1518 1519 1520 1521 1522 1523
		}
		else /* allocate new memory and copy old data */
		{
			if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL;
			rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head)));
			rt_module_free(rt_current_module, (void *)(b + 1));
D
dzzxzz 已提交
1524

D
dzzxzz 已提交
1525
			return (void *)(p);
qiuyiuestc's avatar
qiuyiuestc 已提交
1526 1527 1528
		}
	}
}
qiuyiuestc's avatar
qiuyiuestc 已提交
1529 1530 1531

#ifdef RT_USING_FINSH
#include <finsh.h>
D
dzzxzz 已提交
1532
void list_memlist(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
1533 1534 1535 1536
{
	rt_module_t module;
	struct rt_mem_head **prev;
	struct rt_mem_head *b;
B
bernard.xiong@gmail.com 已提交
1537

qiuyiuestc's avatar
qiuyiuestc 已提交
1538
	module = rt_module_find(name);
D
dzzxzz 已提交
1539 1540
	if (module == RT_NULL)
		return;
qiuyiuestc's avatar
qiuyiuestc 已提交
1541

D
dzzxzz 已提交
1542
	for (prev = (struct rt_mem_head **)&module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
qiuyiuestc's avatar
qiuyiuestc 已提交
1543 1544 1545 1546 1547 1548
	{
		rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head));
	}
}
FINSH_FUNCTION_EXPORT(list_memlist, list module free memory information)

D
dzzxzz 已提交
1549
void list_mempage(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
1550 1551 1552 1553 1554 1555
{
	rt_module_t module;
	struct rt_page_info *page;
	int i;

	module = rt_module_find(name);
D
dzzxzz 已提交
1556 1557
	if (module == RT_NULL)
		return;
qiuyiuestc's avatar
qiuyiuestc 已提交
1558 1559 1560

	page = (struct rt_page_info*)module->page_array;

D
dzzxzz 已提交
1561
	for (i=0; i<module->page_cnt; i++)
qiuyiuestc's avatar
qiuyiuestc 已提交
1562 1563
	{
		rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage);
D
dzzxzz 已提交
1564
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1565 1566 1567
}
FINSH_FUNCTION_EXPORT(list_mempage, list module using memory page information)
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
1568

qiuyiuestc's avatar
qiuyiuestc 已提交
1569
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
1570 1571

#endif