module.c 38.5 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 473 474
	/* construct module symbol table */
	for (index = 0; index < elf_module->e_shnum; index ++)
	{	
		/* find .dynsym section */
D
dzzxzz 已提交
475
		rt_uint8_t *shstrab = (rt_uint8_t *)module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
D
dzzxzz 已提交
476 477
		if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
			break;
478 479
	}

qiuyiuestc's avatar
qiuyiuestc 已提交
480
	/* found .dynsym section */
D
dzzxzz 已提交
481
	if (index != elf_module->e_shnum)
482 483 484
	{
		int i, count = 0;
		Elf32_Sym *symtab = RT_NULL;
D
dzzxzz 已提交
485
		rt_uint8_t *strtab = RT_NULL;
486

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

D
dzzxzz 已提交
490
		for (i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
491
		{
D
dzzxzz 已提交
492 493
			if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
				count ++;
494 495
		}

D
dzzxzz 已提交
496
		module->symtab = (struct rt_module_symtab *)rt_malloc(count * sizeof(struct rt_module_symtab));
497
		module->nsym = count;
D
dzzxzz 已提交
498
		for (i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
499
		{
D
dzzxzz 已提交
500
			if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
501
			{
D
dzzxzz 已提交
502
				rt_size_t length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
503

D
dzzxzz 已提交
504
				module->symtab[count].addr = (void *)(module->module_space + symtab[i].st_value);
505
				module->symtab[count].name = rt_malloc(length);
D
dzzxzz 已提交
506 507 508
				rt_memset((void *)module->symtab[count].name, 0, length);
				rt_memcpy((void *)module->symtab[count].name, strtab + symtab[i].st_name, length);
				count ++;
509 510
			}	
		}	
511
	}
B
bernard.xiong@gmail.com 已提交
512

513 514 515
	return module;
}

D
dzzxzz 已提交
516
static struct rt_module* _load_relocated_object(const char *name, void *module_ptr)
517 518 519
{
	rt_uint32_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
	rt_uint32_t module_addr = 0, module_size = 0;
D
dzzxzz 已提交
520
	struct rt_module *module = RT_NULL;
521 522
	rt_uint8_t *ptr, *strtab, *shstrab;
	rt_bool_t linked = RT_FALSE;
B
bernard.xiong@gmail.com 已提交
523

524 525 526 527 528
	if(rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
	{
		/* rtmlinker finished */
		linked = RT_TRUE;
	}
B
bernard.xiong@gmail.com 已提交
529

530 531 532 533 534 535 536 537 538 539 540 541 542
	/* 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 已提交
543
		}
544 545 546 547 548 549 550 551 552
		/* 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 已提交
553
		}
554 555 556
	}

	/* no text, data and bss on image */
D
dzzxzz 已提交
557 558
	if (module_size == 0)
		return RT_NULL;
559 560

	/* allocate module */
D
dzzxzz 已提交
561 562 563
	module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char *)name);
	if (module == RT_NULL)
		return RT_NULL;
564 565 566 567 568 569

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

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
		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 已提交
588

589 590 591 592 593 594 595 596
		/* 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 已提交
597

598 599 600
		/* load data section */
		if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
		{
B
bernard.xiong@gmail.com 已提交
601
			rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
602 603 604 605 606 607 608 609 610 611
			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 已提交
612
			RT_DEBUG_LOG(RT_DEBUG_MODULE,("load bss 0x%x, size %d,\n", ptr, shdr[index].sh_size));
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
		}
	}

	/* 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 已提交
642

643
				if (sym->st_shndx != STN_UNDEF)
B
bernard.xiong@gmail.com 已提交
644 645
				{
					if((ELF_ST_TYPE(sym->st_info) == STT_SECTION)
646
						|| (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
B
bernard.xiong@gmail.com 已提交
647
					{
648 649 650 651 652 653
						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));
						}
654
						else if(rt_strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_BSS, 5) == 0)
655
						{
B
bernard.xiong@gmail.com 已提交
656
							/* relocate bss section */
657 658 659
							RT_DEBUG_LOG(RT_DEBUG_MODULE,("bss\n"));
							rt_module_arm_relocate(module, rel, (Elf32_Addr)bss_addr + sym->st_value);
						}
660
						else if(rt_strncmp(shstrab + shdr[sym->st_shndx].sh_name, ELF_DATA, 6) == 0)
661
						{
B
bernard.xiong@gmail.com 已提交
662
							/* relocate data section */
663 664
							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 已提交
665 666 667
						}
					}
				}
668
				else if(ELF_ST_TYPE(sym->st_info) == STT_FUNC )
B
bernard.xiong@gmail.com 已提交
669
				{
670 671
					/* relocate function */
					rt_module_arm_relocate(module, rel,
B
bernard.xiong@gmail.com 已提交
672
						(Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value));
673 674 675 676 677 678
				}
				else
				{
					Elf32_Addr addr;

					if(ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
B
bernard.xiong@gmail.com 已提交
679
					{
680 681 682 683 684 685 686
						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 已提交
687
						}
D
dzzxzz 已提交
688 689
						else
							rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
690 691 692
					}
					else
					{
693 694
						rt_module_arm_relocate(module, rel,
							(Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value));
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
					}
				}
				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 已提交
713
rt_module_t rt_module_load(const char *name, void *module_ptr)
714 715
{
	rt_module_t module;
B
bernard.xiong@gmail.com 已提交
716

717 718 719 720 721 722 723 724 725
	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 已提交
726

727 728 729 730 731 732 733
		return RT_NULL;
	}

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

735 736
		return RT_NULL;
	}
B
bernard.xiong@gmail.com 已提交
737

738 739 740 741 742 743 744 745 746 747 748
	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 已提交
749

750 751 752
		return RT_NULL;
	}

D
dzzxzz 已提交
753 754
	if(module == RT_NULL)
		return RT_NULL;
B
bernard.xiong@gmail.com 已提交
755

qiuyiuestc's avatar
qiuyiuestc 已提交
756 757
	/* init module object container */
	rt_module_init_object_container(module);
qiuyiuestc's avatar
qiuyiuestc 已提交
758
		
D
dzzxzz 已提交
759 760
	/* increase module reference count */
	module->nref ++;
qiuyiuestc's avatar
qiuyiuestc 已提交
761

D
dzzxzz 已提交
762
	if (elf_module->e_entry != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
763
	{	
qiuyiuestc's avatar
qiuyiuestc 已提交
764
#ifdef RT_USING_SLAB
qiuyiuestc's avatar
qiuyiuestc 已提交
765
		/* init module memory allocator */
qiuyiuestc's avatar
qiuyiuestc 已提交
766
		module->mem_list = RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
767 768 769 770

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

qiuyiuestc's avatar
qiuyiuestc 已提交
773
		/* create module thread */
qiuyiuestc's avatar
qiuyiuestc 已提交
774
		module->stack_size = 2048;
qiuyiuestc's avatar
qiuyiuestc 已提交
775
		module->thread_priority = 25;
qiuyiuestc's avatar
qiuyiuestc 已提交
776 777 778 779 780 781
		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 已提交
782
		module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
B
bernard.xiong@gmail.com 已提交
783

qiuyiuestc's avatar
qiuyiuestc 已提交
784 785 786 787 788 789 790 791 792
		/* startup module thread */
		rt_thread_startup(module->module_thread);
	}	
	else
	{
		/* without entry point */
		module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
	}	

qiuyiuestc's avatar
qiuyiuestc 已提交
793
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
794
	if (rt_module_load_hook != RT_NULL)
qiuyiuestc's avatar
qiuyiuestc 已提交
795 796 797 798 799
	{
		rt_module_load_hook(module);
	}
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
800
	return module;
qiuyiuestc's avatar
qiuyiuestc 已提交
801 802
}	

qiuyiuestc's avatar
qiuyiuestc 已提交
803 804 805
#ifdef RT_USING_DFS
#include <dfs_posix.h>
/**
B
bernard.xiong@gmail.com 已提交
806
 * This function will load a module from a file
qiuyiuestc's avatar
qiuyiuestc 已提交
807
 *
B
bernard.xiong@gmail.com 已提交
808
 * @param path the full path of application module
qiuyiuestc's avatar
qiuyiuestc 已提交
809 810 811
 *
 * @return the module object
 */
D
dzzxzz 已提交
812
rt_module_t rt_module_open(const char *path)
qiuyiuestc's avatar
qiuyiuestc 已提交
813 814
{
	int fd, length;
D
dzzxzz 已提交
815
	struct rt_module *module;
B
bernard.xiong@gmail.com 已提交
816
	struct stat s;
qiuyiuestc's avatar
qiuyiuestc 已提交
817
	char *buffer, *offset_ptr;
qiuyiuestc's avatar
qiuyiuestc 已提交
818

819
	RT_DEBUG_NOT_IN_INTERRUPT;
820

qiuyiuestc's avatar
qiuyiuestc 已提交
821
	/* check parameters */
qiuyiuestc's avatar
qiuyiuestc 已提交
822
	RT_ASSERT(path != RT_NULL);
qiuyiuestc's avatar
qiuyiuestc 已提交
823

qiuyiuestc's avatar
qiuyiuestc 已提交
824
	if (stat(path, &s) !=0)
825
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
826
		rt_kprintf("access %s failed\n", path);
D
dzzxzz 已提交
827

828 829
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
830
	buffer = (char *)rt_malloc(s.st_size);
831 832 833
	if (buffer == RT_NULL)
	{
		rt_kprintf("out of memory\n");
D
dzzxzz 已提交
834

835 836 837 838
		return RT_NULL;
	}

	offset_ptr = buffer;
qiuyiuestc's avatar
qiuyiuestc 已提交
839
	fd = open(path, O_RDONLY, 0);
840 841
	if (fd < 0)
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
842
		rt_kprintf("open %s failed\n", path);
843
		rt_free(buffer);
D
dzzxzz 已提交
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
		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 已提交
861 862 863
	{
		rt_kprintf("check: read file failed\n");
		rt_free(buffer);
D
dzzxzz 已提交
864

qiuyiuestc's avatar
qiuyiuestc 已提交
865 866
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
867

qiuyiuestc's avatar
qiuyiuestc 已提交
868
	module = rt_module_load(path, (void *)buffer);
qiuyiuestc's avatar
qiuyiuestc 已提交
869 870 871 872
	rt_free(buffer);

	return module;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
873 874 875

#if defined(RT_USING_FINSH)
#include <finsh.h>
876
FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from file);
qiuyiuestc's avatar
qiuyiuestc 已提交
877
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
878 879
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
880
/**
D
dzzxzz 已提交
881
 * This function will unload a module from memory and release resources
qiuyiuestc's avatar
qiuyiuestc 已提交
882 883 884 885 886 887
 *
 * @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 已提交
888
{
889
	int i;
D
dzzxzz 已提交
890
	struct rt_object *object;
qiuyiuestc's avatar
qiuyiuestc 已提交
891
	struct rt_list_node *list;
qiuyiuestc's avatar
qiuyiuestc 已提交
892

893
	RT_DEBUG_NOT_IN_INTERRUPT;
894

qiuyiuestc's avatar
qiuyiuestc 已提交
895 896 897
	/* check parameter */
	RT_ASSERT(module != RT_NULL);

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

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

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
947 948
		/* delete mutexs*/
		list = &module->module_object[RT_Object_Class_Mutex].object_list;	
D
dzzxzz 已提交
949
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
950
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
951 952 953
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
954
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
955 956 957
				rt_mutex_detach((rt_mutex_t)object);
			}
			else
D
dzzxzz 已提交
958
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
959 960
				/* delete dynamic object */
				rt_mutex_delete((rt_mutex_t)object);
D
dzzxzz 已提交
961
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
962 963 964 965
		}
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
966 967
		/* delete mailboxs */
		list = &module->module_object[RT_Object_Class_Event].object_list;	
D
dzzxzz 已提交
968
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
969
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
970 971 972
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
973
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
974 975 976
				rt_event_detach((rt_event_t)object);
			}
			else
D
dzzxzz 已提交
977
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
978 979
				/* delete dynamic object */
				rt_event_delete((rt_event_t)object);
D
dzzxzz 已提交
980
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
981
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
982 983 984
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
985 986
		/* delete mailboxs */
		list = &module->module_object[RT_Object_Class_MailBox].object_list;	
D
dzzxzz 已提交
987
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
988
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
989 990 991
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
992
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
993 994 995
				rt_mb_detach((rt_mailbox_t)object);
			}
			else
D
dzzxzz 已提交
996
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
997 998
				/* delete dynamic object */
				rt_mb_delete((rt_mailbox_t)object);
D
dzzxzz 已提交
999
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1000 1001 1002 1003
		}
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
1004
		/* delete msgqueues */
D
dzzxzz 已提交
1005 1006
		list = &module->module_object[RT_Object_Class_MessageQueue].object_list;
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1007
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1008 1009 1010
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1011
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1012 1013 1014
				rt_mq_detach((rt_mq_t)object);
			}
			else
D
dzzxzz 已提交
1015
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1016 1017
				/* delete dynamic object */
				rt_mq_delete((rt_mq_t)object);
D
dzzxzz 已提交
1018
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1019 1020 1021 1022
		}
#endif

#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
1023 1024
		/* delete mempools */
		list = &module->module_object[RT_Object_Class_MemPool].object_list;	
D
dzzxzz 已提交
1025
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1026
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1027 1028 1029
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1030
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1031 1032 1033
				rt_mp_detach((rt_mp_t)object);
			}
			else
D
dzzxzz 已提交
1034
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1035 1036
				/* delete dynamic object */
				rt_mp_delete((rt_mp_t)object);
D
dzzxzz 已提交
1037
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1038 1039 1040 1041
		}
#endif

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
1042 1043
		/* delete devices */
		list = &module->module_object[RT_Object_Class_Device].object_list;	
D
dzzxzz 已提交
1044
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1045 1046 1047
		{
			object = rt_list_entry(list->next, struct rt_object, list);
			rt_device_unregister((rt_device_t)object);
D
dzzxzz 已提交
1048
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1049 1050
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
1051 1052
		/* delete timers */
		list = &module->module_object[RT_Object_Class_Timer].object_list;
D
dzzxzz 已提交
1053
		while (list->next != list)
qiuyiuestc's avatar
qiuyiuestc 已提交
1054
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1055 1056 1057
			object = rt_list_entry(list->next, struct rt_object, list);
			if (rt_object_is_systemobject(object) == RT_EOK)
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1058
				/* detach static object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1059 1060 1061
				rt_timer_detach((rt_timer_t)object);
			}
			else
D
dzzxzz 已提交
1062
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1063 1064
				/* delete dynamic object */
				rt_timer_delete((rt_timer_t)object);
D
dzzxzz 已提交
1065 1066
			}
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1067
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1068

1069
#ifdef RT_USING_SLAB
D
dzzxzz 已提交
1070
	if (module->page_cnt > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1071
	{
D
dzzxzz 已提交
1072
		struct rt_page_info *page = (struct rt_page_info *)module->page_array;
B
bernard.xiong@gmail.com 已提交
1073 1074

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

1076
		while(module->page_cnt != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1077
		{
1078
			rt_module_free_page(module, page[0].page_ptr, page[0].npage);
B
bernard.xiong@gmail.com 已提交
1079
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1080
	}
1081
#endif
D
dzzxzz 已提交
1082

qiuyiuestc's avatar
qiuyiuestc 已提交
1083
	/* release module space memory */
qiuyiuestc's avatar
qiuyiuestc 已提交
1084
	rt_free(module->module_space);
qiuyiuestc's avatar
qiuyiuestc 已提交
1085

1086
	/* release module symbol table */
D
dzzxzz 已提交
1087 1088 1089 1090
	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 已提交
1091

qiuyiuestc's avatar
qiuyiuestc 已提交
1092
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
1093 1094 1095 1096
	if (rt_module_unload_hook != RT_NULL)
	{
		rt_module_unload_hook(module);
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1097 1098
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
1099 1100 1101 1102
#ifdef RT_USING_SLAB
	if(module->page_array != RT_NULL) 
		rt_free(module->page_array);
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
1103

qiuyiuestc's avatar
qiuyiuestc 已提交
1104
	/* delete module object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1105
	rt_object_delete((rt_object_t)module);
qiuyiuestc's avatar
qiuyiuestc 已提交
1106 1107

	return RT_EOK;
qiuyiuestc's avatar
qiuyiuestc 已提交
1108 1109
}

qiuyiuestc's avatar
qiuyiuestc 已提交
1110 1111 1112 1113 1114 1115 1116
/**
 * This function will find the specified module.
 *
 * @param name the name of module finding
 *
 * @return the module
 */
D
dzzxzz 已提交
1117
rt_module_t rt_module_find(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
1118
{
B
bernard.xiong 已提交
1119
	struct rt_object_information *information;
D
dzzxzz 已提交
1120 1121
	struct rt_object *object;
	struct rt_list_node *node;
qiuyiuestc's avatar
qiuyiuestc 已提交
1122

B
bernard.xiong 已提交
1123 1124
	extern struct rt_object_information rt_object_container[];

1125
	RT_DEBUG_NOT_IN_INTERRUPT;
1126

B
bernard.xiong 已提交
1127 1128 1129 1130
	/* enter critical */
	rt_enter_critical();

	/* try to find device object */
qiuyiuestc's avatar
qiuyiuestc 已提交
1131
	information = &rt_object_container[RT_Object_Class_Module];
B
bernard.xiong 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
	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 已提交
1149 1150
}

qiuyiuestc's avatar
qiuyiuestc 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159
#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 已提交
1160
{
D
dzzxzz 已提交
1161
	void *chunk;
qiuyiuestc's avatar
qiuyiuestc 已提交
1162
	struct rt_page_info *page;
qiuyiuestc's avatar
qiuyiuestc 已提交
1163

qiuyiuestc's avatar
qiuyiuestc 已提交
1164
	chunk = rt_page_alloc(npages);
D
dzzxzz 已提交
1165 1166
	if (chunk == RT_NULL)
		return RT_NULL;
1167

D
dzzxzz 已提交
1168
	page = (struct rt_page_info *)rt_current_module->page_array;
qiuyiuestc's avatar
qiuyiuestc 已提交
1169
	page[rt_current_module->page_cnt].page_ptr = chunk;
D
dzzxzz 已提交
1170 1171
	page[rt_current_module->page_cnt].npage = npages;
	rt_current_module->page_cnt ++;
qiuyiuestc's avatar
qiuyiuestc 已提交
1172

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

qiuyiuestc's avatar
qiuyiuestc 已提交
1176 1177
	return chunk;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
1178

qiuyiuestc's avatar
qiuyiuestc 已提交
1179
/*
D
dzzxzz 已提交
1180
 * This function will release the previously allocated memory page
qiuyiuestc's avatar
qiuyiuestc 已提交
1181 1182 1183 1184
 * 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 已提交
1185
 *
qiuyiuestc's avatar
qiuyiuestc 已提交
1186 1187
 * @note this function is used for RT-Thread Application Module
 */
1188
static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t npages)
qiuyiuestc's avatar
qiuyiuestc 已提交
1189 1190 1191
{
	int i, index;
	struct rt_page_info *page;
qiuyiuestc's avatar
qiuyiuestc 已提交
1192

1193
	rt_kprintf("rt_module_free_page 0x%x %d\n", page_ptr, npages);
qiuyiuestc's avatar
qiuyiuestc 已提交
1194 1195
	rt_page_free(page_ptr, npages);

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

1198
	for(i=0; i<module->page_cnt; i++)
qiuyiuestc's avatar
qiuyiuestc 已提交
1199
	{
D
dzzxzz 已提交
1200
		if (page[i].page_ptr == page_ptr)
qiuyiuestc's avatar
qiuyiuestc 已提交
1201
		{
D
dzzxzz 已提交
1202
			if (page[i].npage == npages + 1)
qiuyiuestc's avatar
qiuyiuestc 已提交
1203 1204
			{
				page[i].page_ptr += npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t);
1205
				page[i].npage -= npages;
qiuyiuestc's avatar
qiuyiuestc 已提交
1206
			}
1207
			else if(page[i].npage == npages)
B
bernard.xiong@gmail.com 已提交
1208
			{
1209
				for(index=i; index<module->page_cnt-1; index++)
qiuyiuestc's avatar
qiuyiuestc 已提交
1210 1211 1212 1213
				{
					page[index].page_ptr = page[index + 1].page_ptr;
					page[index].npage = page[index + 1].npage;
				}
1214 1215
				page[module->page_cnt - 1].page_ptr = RT_NULL;
				page[module->page_cnt - 1].npage = 0;
B
bernard.xiong@gmail.com 已提交
1216 1217

				module->page_cnt--;
qiuyiuestc's avatar
qiuyiuestc 已提交
1218
			}
D
dzzxzz 已提交
1219 1220
			else
				RT_ASSERT(RT_FALSE);
1221
			rt_current_module->page_cnt--;
qiuyiuestc's avatar
qiuyiuestc 已提交
1222

qiuyiuestc's avatar
qiuyiuestc 已提交
1223 1224 1225 1226 1227 1228
			return;
		}
	}

	/* should not be get here */
	RT_ASSERT(RT_FALSE);
qiuyiuestc's avatar
qiuyiuestc 已提交
1229
}
qiuyiuestc's avatar
qiuyiuestc 已提交
1230 1231

/*
B
bernard.xiong@gmail.com 已提交
1232
  rt_module_malloc - allocate memory block in free list
qiuyiuestc's avatar
qiuyiuestc 已提交
1233 1234 1235
*/
void *rt_module_malloc(rt_size_t size)
{
qiuyiuestc's avatar
qiuyiuestc 已提交
1236
	struct rt_mem_head *b, *n, *up;
qiuyiuestc's avatar
qiuyiuestc 已提交
1237
	struct rt_mem_head **prev;
D
dzzxzz 已提交
1238
	rt_uint32_t npage;
qiuyiuestc's avatar
qiuyiuestc 已提交
1239
	rt_size_t nunits;
1240

1241
	RT_DEBUG_NOT_IN_INTERRUPT;
1242

D
dzzxzz 已提交
1243
	nunits = (size + sizeof(struct rt_mem_head) -1)/sizeof(struct rt_mem_head) + 1;
qiuyiuestc's avatar
qiuyiuestc 已提交
1244 1245 1246 1247

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

qiuyiuestc's avatar
qiuyiuestc 已提交
1248
	rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
qiuyiuestc's avatar
qiuyiuestc 已提交
1249

D
dzzxzz 已提交
1250
	for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
qiuyiuestc's avatar
qiuyiuestc 已提交
1251
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1252 1253
		if (b->size > nunits)
		{
B
bernard.xiong@gmail.com 已提交
1254
			/* split memory */
qiuyiuestc's avatar
qiuyiuestc 已提交
1255 1256 1257 1258 1259
			n = b + nunits;
			n->next = b->next;
			n->size = b->size - nunits;
			b->size = nunits;
			*prev = n;
qiuyiuestc's avatar
qiuyiuestc 已提交
1260

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

qiuyiuestc's avatar
qiuyiuestc 已提交
1264
			return (void *)(b + 1);
qiuyiuestc's avatar
qiuyiuestc 已提交
1265 1266 1267 1268 1269 1270
		}

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

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

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
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1278
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1279

qiuyiuestc's avatar
qiuyiuestc 已提交
1280 1281
	/* allocate pages from system heap */
	npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1282 1283
	if ((up = (struct rt_mem_head *)rt_module_malloc_page(npage)) == RT_NULL)
		return RT_NULL;
qiuyiuestc's avatar
qiuyiuestc 已提交
1284 1285

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

	for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
qiuyiuestc's avatar
qiuyiuestc 已提交
1288
	{
D
dzzxzz 已提交
1289 1290
		if (b > up + up->size)
			break;
qiuyiuestc's avatar
qiuyiuestc 已提交
1291 1292
	}

qiuyiuestc's avatar
qiuyiuestc 已提交
1293 1294 1295 1296
	up->next = b;
	*prev = up;

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

qiuyiuestc's avatar
qiuyiuestc 已提交
1298
	return rt_module_malloc(size);
qiuyiuestc's avatar
qiuyiuestc 已提交
1299 1300 1301
}

/*
qiuyiuestc's avatar
qiuyiuestc 已提交
1302
  rt_module_free - free memory block in free list
qiuyiuestc's avatar
qiuyiuestc 已提交
1303 1304 1305
*/
void rt_module_free(rt_module_t module, void *addr)
{
qiuyiuestc's avatar
qiuyiuestc 已提交
1306
	struct rt_mem_head *b, *n, *r;
qiuyiuestc's avatar
qiuyiuestc 已提交
1307
	struct rt_mem_head **prev;
1308

1309
	RT_DEBUG_NOT_IN_INTERRUPT;
1310

qiuyiuestc's avatar
qiuyiuestc 已提交
1311
	RT_ASSERT(addr);
qiuyiuestc's avatar
qiuyiuestc 已提交
1312 1313
	RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0);

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

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

qiuyiuestc's avatar
qiuyiuestc 已提交
1318
	n = (struct rt_mem_head *)addr - 1;
qiuyiuestc's avatar
qiuyiuestc 已提交
1319
	prev = (struct rt_mem_head **)&module->mem_list;
qiuyiuestc's avatar
qiuyiuestc 已提交
1320 1321

	while ((b = *prev) != RT_NULL)
D
dzzxzz 已提交
1322
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1323 1324 1325
		RT_ASSERT(b->size > 0);
		RT_ASSERT(b > n || b + b->size <= n);

D
dzzxzz 已提交
1326
		if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0))
qiuyiuestc's avatar
qiuyiuestc 已提交
1327
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
1328
			if (b + (b->size + n->size) == b->next)
qiuyiuestc's avatar
qiuyiuestc 已提交
1329
			{
qiuyiuestc's avatar
qiuyiuestc 已提交
1330 1331
				b->size += b->next->size + n->size;
				b->next = b->next->next;
qiuyiuestc's avatar
qiuyiuestc 已提交
1332
			}
D
dzzxzz 已提交
1333 1334
			else
				b->size += n->size;
qiuyiuestc's avatar
qiuyiuestc 已提交
1335

D
dzzxzz 已提交
1336
			if ((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1337 1338
			{
				int npage = b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1339
				if (npage > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1340
				{
D
dzzxzz 已提交
1341
					if ((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
					{
						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 已提交
1352
						*prev = b->next;
qiuyiuestc's avatar
qiuyiuestc 已提交
1353
					}
B
bernard.xiong@gmail.com 已提交
1354

1355
					rt_module_free_page(module, b, npage);
qiuyiuestc's avatar
qiuyiuestc 已提交
1356
				}
B
bernard.xiong@gmail.com 已提交
1357
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1358

qiuyiuestc's avatar
qiuyiuestc 已提交
1359 1360
			/* unlock */
			rt_sem_release(&mod_sem);
D
dzzxzz 已提交
1361

qiuyiuestc's avatar
qiuyiuestc 已提交
1362 1363 1364 1365 1366 1367 1368
			return;
		}

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

D
dzzxzz 已提交
1370
			if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1371 1372
			{
				int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1373
				if (npage > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1374
				{
D
dzzxzz 已提交
1375
					if ((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1376 1377 1378 1379 1380 1381 1382 1383
					{
						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 已提交
1384
					else *prev = n->next;
B
bernard.xiong@gmail.com 已提交
1385

1386
					rt_module_free_page(module, n, npage);
qiuyiuestc's avatar
qiuyiuestc 已提交
1387
				}
D
dzzxzz 已提交
1388
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1389 1390 1391 1392 1393 1394 1395
			else
			{
				*prev = n;
			}

			/* unlock */
			rt_sem_release(&mod_sem);
qiuyiuestc's avatar
qiuyiuestc 已提交
1396 1397 1398

			return;
		}
D
dzzxzz 已提交
1399 1400
		if (b > n + n->size)
			break;
qiuyiuestc's avatar
qiuyiuestc 已提交
1401 1402 1403 1404

		prev = &(b->next);
	}

D
dzzxzz 已提交
1405
	if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1406 1407
	{
		int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
D
dzzxzz 已提交
1408
		if (npage > 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1409
		{
1410
			rt_module_free_page(module, n, npage);
D
dzzxzz 已提交
1411
			if (n->size % RT_MM_PAGE_SIZE != 0)
qiuyiuestc's avatar
qiuyiuestc 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
			{
				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 已提交
1422 1423 1424
				*prev = b;
			}
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
1425
	}
D
dzzxzz 已提交
1426 1427
	else
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1428 1429 1430
		n->next = b;
		*prev = n;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1431

qiuyiuestc's avatar
qiuyiuestc 已提交
1432
	/* unlock */
D
dzzxzz 已提交
1433
	rt_sem_release(&mod_sem);
qiuyiuestc's avatar
qiuyiuestc 已提交
1434 1435 1436 1437 1438 1439 1440
}

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

1444
	RT_DEBUG_NOT_IN_INTERRUPT;
1445

D
dzzxzz 已提交
1446 1447
	if (!ptr)
		return rt_module_malloc(size);
qiuyiuestc's avatar
qiuyiuestc 已提交
1448 1449 1450
	if (size == 0)
	{
		rt_module_free(rt_current_module, ptr);
D
dzzxzz 已提交
1451

qiuyiuestc's avatar
qiuyiuestc 已提交
1452 1453
		return RT_NULL;
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1454

qiuyiuestc's avatar
qiuyiuestc 已提交
1455 1456 1457
	nunits = (size + sizeof(struct rt_mem_head) - 1) / sizeof(struct rt_mem_head) + 1;
	b = (struct rt_mem_head *)ptr - 1;

D
dzzxzz 已提交
1458 1459 1460
	if (nunits <= b->size)
	{
		/* new size is smaller or equal then before */
D
dzzxzz 已提交
1461 1462
		if (nunits == b->size)
			return ptr;
D
dzzxzz 已提交
1463
		else
qiuyiuestc's avatar
qiuyiuestc 已提交
1464 1465 1466 1467 1468
		{
			p = b + nunits;
			p->size = b->size - nunits;
			b->size = nunits;
			rt_module_free(rt_current_module, (void *)(p + 1));
D
dzzxzz 已提交
1469

qiuyiuestc's avatar
qiuyiuestc 已提交
1470 1471 1472
			return (void *)(b + 1);
		}
	}
D
dzzxzz 已提交
1473 1474
	else
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
1475
		/* more space then required */
qiuyiuestc's avatar
qiuyiuestc 已提交
1476
		prev = (struct rt_mem_head *)rt_current_module->mem_list;
D
dzzxzz 已提交
1477 1478
		for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next)
			break;
qiuyiuestc's avatar
qiuyiuestc 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

		/* 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 已提交
1492
				tmpp = p;
qiuyiuestc's avatar
qiuyiuestc 已提交
1493 1494 1495 1496 1497 1498
				p = b + nunits;

				/* restoring old pointer */
				p->next = tmpp->next;
				
				/* new size for p */
D
dzzxzz 已提交
1499
				p->size = tmpp->size + b->size - nunits;
qiuyiuestc's avatar
qiuyiuestc 已提交
1500 1501 1502
				b->size = nunits;
				prev->next = p;
			}
qiuyiuestc's avatar
qiuyiuestc 已提交
1503
			rt_current_module->mem_list = (void *)prev;
D
dzzxzz 已提交
1504

D
dzzxzz 已提交
1505
			return (void *)(b + 1);
qiuyiuestc's avatar
qiuyiuestc 已提交
1506 1507 1508 1509 1510 1511
		}
		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 已提交
1512

D
dzzxzz 已提交
1513
			return (void *)(p);
qiuyiuestc's avatar
qiuyiuestc 已提交
1514 1515 1516
		}
	}
}
qiuyiuestc's avatar
qiuyiuestc 已提交
1517 1518 1519

#ifdef RT_USING_FINSH
#include <finsh.h>
D
dzzxzz 已提交
1520
void list_memlist(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
1521 1522 1523 1524
{
	rt_module_t module;
	struct rt_mem_head **prev;
	struct rt_mem_head *b;
B
bernard.xiong@gmail.com 已提交
1525

qiuyiuestc's avatar
qiuyiuestc 已提交
1526
	module = rt_module_find(name);
D
dzzxzz 已提交
1527 1528
	if (module == RT_NULL)
		return;
qiuyiuestc's avatar
qiuyiuestc 已提交
1529

D
dzzxzz 已提交
1530
	for (prev = (struct rt_mem_head **)&module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
qiuyiuestc's avatar
qiuyiuestc 已提交
1531 1532 1533 1534 1535 1536
	{
		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 已提交
1537
void list_mempage(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
1538 1539 1540 1541 1542 1543
{
	rt_module_t module;
	struct rt_page_info *page;
	int i;

	module = rt_module_find(name);
D
dzzxzz 已提交
1544 1545
	if (module == RT_NULL)
		return;
qiuyiuestc's avatar
qiuyiuestc 已提交
1546 1547 1548

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

D
dzzxzz 已提交
1549
	for (i=0; i<module->page_cnt; i++)
qiuyiuestc's avatar
qiuyiuestc 已提交
1550 1551
	{
		rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage);
D
dzzxzz 已提交
1552
	}
qiuyiuestc's avatar
qiuyiuestc 已提交
1553 1554 1555
}
FINSH_FUNCTION_EXPORT(list_mempage, list module using memory page information)
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
1556

qiuyiuestc's avatar
qiuyiuestc 已提交
1557
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
1558 1559

#endif