您的位置:首页 > 其它

嵌入式操作系统内核原理和开发(任务创建和堆栈溢出检查)

2012-06-01 23:57 393 查看
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

虽然写操作系统的博客要比写普通的技术点要麻烦一些,但是心中还是挺开心的。一方面,通过几行代码就可以说明一些问题,把理论实践化,这本身就很具有挑战性;另外一方面还锻炼自己的沟通能力,让更多的人明白你的想法,认可你的想法。
其实,通过上面一篇博客,我们就已经清楚任务的创建是怎么一回事,但是我们还是愿意就这个问题讲得更细一点,说得更多一点。系统本身是多线程的,那说明所有线程的地址空间都是共享的。由于资源都是操作系统本身提供的,所以线程本身的要求就很低,函数名、堆栈、入口点、堆栈大小、优先级,大体上也就是这么多。至于这个堆栈是哪里的内存,其实已经不太重要了。为了简单起见,我们对原来的初始化函数 稍微修改了一下,

void task_init()
{
	UINT32 unit = STACK_LENGTH;

	memset((void*)data, 0, STACK_LENGTH * sizeof(UINT32));
	data[unit -1] = (UINT32) hello;
	data[unit -2] = 0;
	data[unit -3] = 0;
	data[unit -4] = 0;
	data[unit -5] = 0;
	data[unit -6] = 0;
	data[unit -7] = 0;
	data[unit -8] = 0;
	data[unit -9] = 0;
	data[unit -10] = (UINT32) &data[unit - 9];
	new = (UINT32) &data[unit -10];
}
上面的操作比较简陋,只是对堆栈进行了设置。这是线程初始化的时候必须要做的一步。当然,这里的hello就是我们的函数入口点。因为这里用SIGALRM代替的时钟中断是没有办法做到抢占的,所以我们可以人为多设置一些调度点,比如象这样,

void hello()
{
	printf("count = %d in sub!\n", count ++);
	swap(&new, &old);
	printf("count = %d in sub!\n", count ++);
	swap(&new, &old);
	printf("count = %d in sub!\n", count ++);
	swap(&new, &old);
	printf("count = %d in sub!\n", count ++);
	swap(&new, &old);
	printf("count = %d in sub!\n", count ++);
	quit = 1;
	swap(&new, &old);
}
在编写程序的时候,最恐怖的事情就是堆栈溢出了。但是在操作系统中,我们完全可以自己判断当前的堆栈是否已经溢出。因为我们知道,在线程调度的时候,保存的堆栈esp永远指向最低的那个地址。

int check_stack_overflow(unsigned int base, unsigned int current)
{
    assert(0 != base && 0 != current);

    return (current < base) ? 1 :0;
}
当然,这些说的都是线程调度的事,你也可以编写输入输出命令,实现对嵌入式操作系统的某种控制。要打印什么,设置什么,保存什么,都可以通过你的输入命令来解析执行,这些都是和signal处理是分开来的。后面这部分还要详细讨论,这里可以稍微添加一下,

int main()
{
	char val;

	task_init();
	set_timer();
	signal(SIGALRM, signal_handler);

	while(1)
	{
	    scanf("%c", &val);
	}

	exit(0);
	return 1;
}
最后,还是老规矩,附上详细的代码。虽然这一过程有点繁琐和冗余,但是至少看上去更完整一些。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>
#include <sys/time.h>

#define UINT32 unsigned int
#define STACK_LENGTH 512

static struct itimerval oldtv;
UINT32 old = 0;
UINT32 new = 0;
UINT32 count = 0;
UINT32 data[STACK_LENGTH] = {0};
UINT32 quit = 0;

void set_timer()
{
struct itimerval itv;
itv.it_interval.tv_sec = 1;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, &oldtv);
}

void swap(UINT32* prev, UINT32* next)
{
__asm("push %%eax\n\t"
"push %%ebx\n\t"
"push %%ecx\n\t"
"push %%edx\n\t"
"push %%esi\n\t"
"push %%edi\n\t"
"push %%ebp\n\t"
"push %%esp\n\t"
"lea 0x8(%%ebp), %%eax\n\t"
"mov (%%eax), %%eax\n\t"
"mov %%esp, (%%eax)\n\t"

"lea 0xc(%%ebp), %%eax\n\t"
"mov (%%eax), %%eax\n\t"
"mov (%%eax), %%esp\n\t"
"pop %%esp\n\t"
"pop %%ebp\n\t"
"pop %%edi\n\t"
"pop %%esi\n\t"
"pop %%edx\n\t"
"pop %%ecx\n\t"
"pop %%ebx\n\t"
"pop %%eax\n\t"
::);
}

void hello() { printf("count = %d in sub!\n", count ++); swap(&new, &old); printf("count = %d in sub!\n", count ++); swap(&new, &old); printf("count = %d in sub!\n", count ++); swap(&new, &old); printf("count = %d in sub!\n", count ++); swap(&new, &old); printf("count = %d in sub!\n", count ++); quit = 1; swap(&new, &old); }
void task_init() { UINT32 unit = STACK_LENGTH; memset((void*)data, 0, STACK_LENGTH * sizeof(UINT32)); data[unit -1] = (UINT32) hello; data[unit -2] = 0; data[unit -3] = 0; data[unit -4] = 0; data[unit -5] = 0; data[unit -6] = 0; data[unit -7] = 0; data[unit -8] = 0; data[unit -9] = 0; data[unit -10] = (UINT32) &data[unit - 9]; new = (UINT32) &data[unit -10]; }

int check_stack_overflow(unsigned int base, unsigned int current) { assert(0 != base && 0 != current); return (current < base) ? 1 :0; }
void signal_handler(int m)
{
if(0 == quit)
{
swap(&old, &new);
assert(0 == check_stack_overflow(data,new));
return;
}

printf("count = %d in main!\n", count ++);
}

int main() { char val; task_init(); set_timer(); signal(SIGALRM, signal_handler); while(1) { scanf("%c", &val); } exit(0); return 1; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: