切换到第一个任务是遇到了问题,产生通用保护错误,也不知是什么原因

kernel.c

#include "sprintf.h"


/* gdtidt.h */

typedef struct descriptor {
	short limit_low;
	short base_low;
	char base_mid;
	char access_right;
	char limit_high;
	char base_high;
}descriptor;

typedef struct tss {
	int backlink;
	int esp0;
	int ss0;
	int esp1;
	int ss1;
	int esp2;
	int ss2;
	int cr3;
	int eip;
	int eflags;
	int eax;
	int ecx;
	int edx;
	int ebx;
	int esp;
	int ebp;
	int esi;
	int edi;
	int es;
	int cs;
	int ss;
	int ds;
	int fs;
	int gs;
	int ldt_sel;
	short trap;
	short iobase;
}tss, *TSS;

typedef struct proc_stack {
	int gs;
	int fs;
	int es;
	int ds;
	int edi;
	int esi;
	int ebp;
	int kernel_esp;
	int ebx;
	int edx;
	int ecx;
	int eax;
	int retaddr;
	int eip;
	int cs;
	int eflags;
	int esp;
	int ss;
}proc_stack;

typedef struct process {
	proc_stack procstack;
	
	short ldt_selector;
	descriptor ldt[0x8];
	int pid;
	char name[32];
}process, *PROCESS;




struct SEGMENT_DESCRIPTOR {
	short limit_low, base_low;
	char base_mid, access_right;
	char limit_high, base_high;
};

struct GATE_DESCRIPTOR {
	short offset_low, selector;
	char dw_count, access_right;
	short offset_high;
};

void delay(unsigned int times);

void out(int port, int value);
int in(int port);
void sti(void);
void cli(void);
void set_eflags(void);



void asm_inthandler_0(unsigned int esp);
void asm_inthandler_20(void);
void asm_inthandler_21(void);
void asm_inthandler_all(void);

//  NMI
void asm_inthandler_1(unsigned int esp);
void asm_inthandler_2(unsigned int esp);
void asm_inthandler_3(unsigned int esp);
void asm_inthandler_4(unsigned int esp);
void asm_inthandler_5(unsigned int esp);
void asm_inthandler_6(unsigned int esp);
void asm_inthandler_7(unsigned int esp);
void asm_inthandler_8(unsigned int esp);
void asm_inthandler_9(unsigned int esp);
void asm_inthandler_a(unsigned int esp);
void asm_inthandler_b(unsigned int esp);
void asm_inthandler_c(unsigned int esp);
void asm_inthandler_d(unsigned int esp);
void asm_inthandler_e(unsigned int esp);
void asm_inthandler_f(unsigned int esp);
void asm_inthandler_10(unsigned int esp);
void asm_inthandler_11(unsigned int esp);
void asm_inthandler_12(unsigned int esp);
void asm_inthandler_13(unsigned int esp);
void asm_inthandler_14(unsigned int esp);
void asm_inthandler_15(unsigned int esp);
void asm_inthandler_16(unsigned int esp);
void asm_inthandler_17(unsigned int esp);
void asm_inthandler_18(unsigned int esp);
void asm_inthandler_19(unsigned int esp);
void asm_inthandler_1a(unsigned int esp);
void asm_inthandler_1b(unsigned int esp);
void asm_inthandler_1c(unsigned int esp);
void asm_inthandler_1d(unsigned int esp);
void asm_inthandler_1e(unsigned int esp);
void asm_inthandler_1f(unsigned int esp);
// NMI



void inthandler_all(int esp);
void inthandler_20(void);

void init_gdtidt(void);
void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar);
void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar);
void load_gdtr(int limit, int addr);
void load_idtr(int limit, int addr);

void init_8259a(void);
void init_8254(void);

void myhlt(void);
void setup_page(void);
void change_page(void);
void change0(void);
void fillbox(int color, int x0, int y0, int x1, int y1);
void myfillbox(int color, int x0, int y0, int x1, int y1);
void makefont16 (int color, int x, int y, short *font);
void print_gb2312(int color, int x, int y, unsigned char *str);
void copy_str(char *str, char *mystr);

unsigned int count = 0;

void ltr(int tr);
void lldt(int ldtr);
void func_a(void);
void func_b(void);
void ud2(void);
void goto_ring1(void);


process proc[128];
tss tss0;
	
PROCESS myproc;
PROCESS proc_point_0;

extern int func_a_stack;
extern int func_b_stack;
extern int func_a_kstack;
extern int func_b_kstack;


int mymain(int eax, int ebx) {

	out(0x21, 0xff);
	out(0xa1, 0xff);
	int *myfb = (int *)0xe0000000;
	fillbox(0x001e90ff, 0, 0, 799, 599);
	myfillbox(0x00000000, 0, 0, 799, 599);
	unsigned char valuebuf[0x100];
	
	proc_point_0 = myproc = proc;

	
	myproc->procstack.eip = (int) func_a;
	myproc->procstack.cs = 0x0 + 0x7;
	myproc->procstack.eflags = 0x1202;
	myproc->procstack.esp = func_a_stack;
	myproc->procstack.ss = 0x8 + 0x7;
	tss0.iobase = 0x120;
	
	tss0.eip = (int) func_a;
	tss0.ss0 = 0x8;
	tss0.esp0 = func_a_kstack;
	tss0.esp = func_a_stack;
	
	tss0.cs = 0x7;
	tss0.ss = 0xf;
	tss0.ds = 0xf;
	tss0.es = 0xf;
	tss0.fs = 0xf;
	tss0.gs = 0xf;
	
	tss0.ldt_sel = 0x18;
	
	
	set_segmdesc((struct SEGMENT_DESCRIPTOR *)(&myproc->ldt[0]), 0x1000, 0x00000000, 0xc0fa);
	set_segmdesc((struct SEGMENT_DESCRIPTOR *)(&myproc->ldt[1]), 0x1000, 0x00000000, 0xc0f2);
	
	
	init_gdtidt();
//	setup_page();	
//	init_8259a();
//	init_8254();
	
	out(0x21, 0xf8);
	out(0xa1, 0xff);
//	sti();

	set_eflags();
	ltr(0x20);
	lldt(0x18);
	goto_ring1();
	
	while(1) {
//		func_a();
		func_b();
	}
}

void myprintf(char * str) {
	return;
}

void fillbox(int color, int x0, int y0, int x1, int y1) {
	int *myfb = (int *)0xe0000000;
	int xsize = 800;
	int x, y;
	for (y = 0; y <= y1 - y0; y++) {
		for (x = 0; x <= x1 - x0; x++) {
			myfb[x0 + x + (y0 + y) * xsize] = color;
		}
	}
}


void myfillbox(int color, int x0, int y0, int x1, int y1) {
	int *myfb = (int *)0xe0000000;
	int xsize = 800;
	int x, y;
	if (x0 < 80 || y0 < 80 || x1 > 719 || y1 > 519) {
		x0 = 80;
		y0 = 80;
		x1 = 719;
		y1 = 519;
	}
	for (y = 0; y <= y1 - y0; y++) {
		for (x = 0; x <= x1 - x0; x++) {
			myfb[x0 + x + (y0 + y) * xsize] = color;
		}
	}
}

void print_ascii (int color, int x, int y, char *font) {
	int *myfb = (int *)0xe0000000;
	int xsize = 800;
	char d;
	int *p;
	int i;
	for (i = 0; i < 16; i++){
		p = myfb + (y + i) * xsize + x;
		d = font[i];
		if ((d & 0x80) != 0) {p[0] = color;}
		if ((d & 0x40) != 0) {p[1] = color;}
		if ((d & 0x20) != 0) {p[2] = color;}
		if ((d & 0x10) != 0) {p[3] = color;}
		if ((d & 0x08) != 0) {p[4] = color;}
		if ((d & 0x04) != 0) {p[5] = color;}
		if ((d & 0x02) != 0) {p[6] = color;}
		if ((d & 0x01) != 0) {p[7] = color;}
	}
	return;
}

void makefont16 (int color, int x, int y, short *font) {
	int *myfb = (int *)0xe0000000;
	int xsize = 800;
	short d;
	int *p;
	int i;
	for (i = 0; i < 16; i++){
		p = myfb + (y + i) * xsize + x;
		d = font[i];
		if ((d & 0x80) != 0) {p[0] = color;}
		if ((d & 0x40) != 0) {p[1] = color;}
		if ((d & 0x20) != 0) {p[2] = color;}
		if ((d & 0x10) != 0) {p[3] = color;}
		if ((d & 0x08) != 0) {p[4] = color;}
		if ((d & 0x04) != 0) {p[5] = color;}
		if ((d & 0x02) != 0) {p[6] = color;}
		if ((d & 0x01) != 0) {p[7] = color;}
		if ((d & 0x8000) != 0) {p[8] = color;}
		if ((d & 0x4000) != 0) {p[9] = color;}
		if ((d & 0x2000) != 0) {p[10] = color;}
		if ((d & 0x1000) != 0) {p[11] = color;}
		if ((d & 0x800) != 0) {p[12] = color;}
		if ((d & 0x400) != 0) {p[13] = color;}
		if ((d & 0x200) != 0) {p[14] = color;}
		if ((d & 0x100) != 0) {p[15] = color;}
	}
	return;
}

void print_gb2312(int color, int x, int y, unsigned char *str) {
	extern short font[0x8000];
	extern char myfont[0x1000];
	int i = 0, j = 0;
	unsigned char a, b;
	unsigned int offset;
	while (str[i] != 0) {
		if (str[i] < 0x80) {
			a = str[i];
			print_ascii (color, x + j, y, myfont + a * 16);
			if (str[i + 1] == 0)
				break;
			i++;
			j += 8;
		} else {	
			a = str[i] - 0xa0;
			b = str[i + 1] - 0xa0;
			offset = ((a - 1) * 94 + (b - 1)) * 16;
			makefont16 (color, x + j , y, font + offset);
			i += 2;
			j += 16;
		}
	}
	return;
}

void copy_str(char *str, char *mystr) {
	int i = 0;
	for (; i <= strnlen(mystr, 0); i++)
		*(str + i) = *(mystr + i); 
	return;
}


void init_gdtidt(void)
{
	struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) 0x00270000;
	struct GATE_DESCRIPTOR    *idt = (struct GATE_DESCRIPTOR    *) 0x0026f800;

	int i;

	for (i = 0; i < 8192; i++) {
		set_segmdesc(gdt + i, 0, 0, 0);
	}
	set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, 0xc092);
	set_segmdesc(gdt + 2, 0xffffffff, 0x00000000, 0xc09a);
	set_segmdesc(gdt + 3, 0x1000, (int)&(myproc->ldt[0]), 0xe2);
	set_segmdesc(gdt + 4, 0x100, (int)&tss0, 0xe9);


	for (i = 0; i < 256; i++) {
		set_gatedesc(idt + i, (int) asm_inthandler_all, 0x10, 0x8e);
	}
	load_idtr(0x7ff, 0x0026f800);
	

	set_gatedesc(idt + 0x1, (int) asm_inthandler_1, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x2, (int) asm_inthandler_2, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x3, (int) asm_inthandler_3, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x4, (int) asm_inthandler_4, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x5, (int) asm_inthandler_5, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x6, (int) asm_inthandler_6, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x7, (int) asm_inthandler_7, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x8, (int) asm_inthandler_8, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x9, (int) asm_inthandler_9, 2 * 8, 0x8e);
	set_gatedesc(idt + 0xa, (int) asm_inthandler_a, 2 * 8, 0x8e);
	set_gatedesc(idt + 0xb, (int) asm_inthandler_b, 2 * 8, 0x8e);
	set_gatedesc(idt + 0xc, (int) asm_inthandler_c, 2 * 8, 0x8e);
	set_gatedesc(idt + 0xd, (int) asm_inthandler_d, 2 * 8, 0x8e);
	set_gatedesc(idt + 0xe, (int) asm_inthandler_e, 2 * 8, 0x8e);
	set_gatedesc(idt + 0xf, (int) asm_inthandler_f, 2 * 8, 0x8e);
	
	set_gatedesc(idt + 0x10, (int) asm_inthandler_10, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x11, (int) asm_inthandler_11, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x12, (int) asm_inthandler_12, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x13, (int) asm_inthandler_13, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x14, (int) asm_inthandler_14, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x15, (int) asm_inthandler_15, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x16, (int) asm_inthandler_16, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x17, (int) asm_inthandler_17, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x18, (int) asm_inthandler_18, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x19, (int) asm_inthandler_19, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x1a, (int) asm_inthandler_1a, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x1b, (int) asm_inthandler_1b, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x1c, (int) asm_inthandler_1c, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x1d, (int) asm_inthandler_1d, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x1e, (int) asm_inthandler_1e, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x1f, (int) asm_inthandler_1f, 2 * 8, 0x8e);

/*
	set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x2c, (int) asm_inthandler2c, 2 * 8, 0x8e);
*/
	set_gatedesc(idt + 0x0, (int) asm_inthandler_0, 2 * 8, 0x8e);	
	set_gatedesc(idt + 0x20, (int) asm_inthandler_20, 2 * 8, 0x8e);
	set_gatedesc(idt + 0x21, (int) asm_inthandler_21, 2 * 8, 0x8e);
	load_gdtr(0xffff, 0x00270000);
	return;
}

void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar)
{
	if (limit > 0xfffff) {
		ar |= 0x8000; 
		limit /= 0x1000;
	}
	sd->limit_low    = limit & 0xffff;
	sd->base_low     = base & 0xffff;
	sd->base_mid     = (base >> 16) & 0xff;
	sd->access_right = ar & 0xff;
	sd->limit_high   = ((limit >> 16) & 0x0f) | ((ar >> 8) & 0xf0);
	sd->base_high    = (base >> 24) & 0xff;
	return;
}

void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar)
{
	gd->offset_low   = offset & 0xffff;
	gd->selector     = selector;
	gd->dw_count     = (ar >> 8) & 0xff;
	gd->access_right = ar & 0xff;
	gd->offset_high  = (offset >> 16) & 0xffff;
	return;
}

void inthandler_0(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "除零");
	return;
}

void inthandler_1(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "调试");
	return;
}
void inthandler_2(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "非屏蔽外部中断");
	return;
}
void inthandler_3(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "断点");
	return;
}
void inthandler_4(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "溢出");
	return;
}
void inthandler_5(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "边界范围超出");
	return;
}
void inthandler_6(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "无效操作码");
	return;
}
void inthandler_7(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "无数学协处理器");
	return;
}
void inthandler_8(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "双重错误");
	return;
}
void inthandler_9(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "协处理器段超越");
	return;
}
void inthandler_a(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "无效的任务状态段");
	return;
}
void inthandler_b(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "段不存在");
	return;
}
void inthandler_c(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "堆栈段错误");
	return;
}
void inthandler_d(unsigned int esp) {
	int *myesp = (int *) esp;
	char buf[0x100];
	sprintf(buf, "一般保护错误 出错码 %x,cs: %x, eip: 0x%x", *myesp, *(myesp + 8), *(myesp + 4));
	print_gb2312(0x00ee0000, 0, 584, buf);
	while (1){
		myhlt();
	}
	return;
}
void inthandler_e(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "页面错误");
	return;
}
void inthandler_f(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}



void inthandler_10(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "浮点错误");
	return;
}

void inthandler_11(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "对齐检查");
	return;
}
void inthandler_12(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "机器检查,与CPU类型有关");
	return;
}
void inthandler_13(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "SIMD浮点异常");
	return;
}
void inthandler_14(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_15(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_16(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_17(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_18(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_19(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_1a(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_1b(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_1c(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_1d(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_1e(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}
void inthandler_1f(unsigned int esp) {
	print_gb2312(0x00ee0000, 0, 584, "保留,勿动");
	return;
}



void inthandler_all(int esp) {
	char buf[0x100];
	sprintf(buf, "发生了异常 %x", esp);
	print_gb2312(0x00ee0000, 0, 584, buf);
	for (;;) {
		myhlt();
	}
	return;
}

void inthandler_20(void) {
	count++;
	out(0x20, 0x60);
	return;
}

void inthandler_21(void) {
	out(0x20, 0x61);
	int temp = in(0x60);
	char buf[0x100];
	fillbox(0x001e90ff, 301, 568, 500, 586);
	sprintf(buf, "发生了键盘中断!%d", count);
	print_gb2312(0x00000000, 301, 568, buf);
	return;
}

void init_8254(void) {
	out(0x43, 0x36);
	out(0x40, 0x9c);
	out(0x40, 0x2e);
	return;
}

void func_a(void) {	
	int i = 0;
	char buf[0x100];
	while(1) {
		sprintf(buf, "计数值 %d ", i++);
		print_gb2312(0xffffffff, 80, 80, buf);
		delay(0xfffffff);	
		fillbox(0x00000000, 80, 80, 240, 96);	
	}
	return;
}

void func_b(void) {	
	int i = 0;
	char buf[0x100];
	while(1) {
		sprintf(buf, "计数值 %d ", i++);
		print_gb2312(0xffffffff, 80, 96, buf);
		delay(0xffffff);	
		fillbox(0x000000ff, 80, 96, 240, 112);	
	}
	return;
}

boot.s

MULTIBOOT2_HEADER_MAGIC = 0xe85250d6
GRUB_MULTIBOOT_ARCHITECTURE_I386 = 0x0
MULTIBOOT_HEADER_TAG_ADDRESS = 0x2
MULTIBOOT_HEADER_TAG_OPTIONAL = 0x1
MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 0x3
MULTIBOOT_HEADER_TAG_END = 0x0
MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 0x5

.globl _delay, _ud2, _set_eflags, _start, _myhlt, _setup_page, _change_page, _change0
.extern _mymain, _myprintf

.global	_load_gdtr, _load_idtr, _sti, _cli, _init_8259a, _out, _in
.global	_asm_inthandler_0, _asm_inthandler_20, _asm_inthandler_21, _asm_inthandler27, _asm_inthandler2c, _asm_inthandler_all
.extern	_inthandler_0, _inthandler_20, _inthandler_21, _inthandler27, _inthandler2c, _inthandler_all

.global _func_a_stack, _func_b_stack, _func_a_kstack, _func_a_kstack, _goto_ring1,
.global _ltr, _lldt
.extern _tss0, _proc_point_0, _func_a


.global _asm_inthandler_1, _asm_inthandler_2, _asm_inthandler_3, _asm_inthandler_4
.global _asm_inthandler_5, _asm_inthandler_6, _asm_inthandler_7, _asm_inthandler_8
.global _asm_inthandler_9, _asm_inthandler_a, _asm_inthandler_b, _asm_inthandler_c
.global _asm_inthandler_d, _asm_inthandler_e, _asm_inthandler_f

.global _asm_inthandler_11, _asm_inthandler_12, _asm_inthandler_13, _asm_inthandler_14
.global _asm_inthandler_15, _asm_inthandler_16, _asm_inthandler_17, _asm_inthandler_18
.global _asm_inthandler_19, _asm_inthandler_1a, _asm_inthandler_1b, _asm_inthandler_1c
.global _asm_inthandler_1d, _asm_inthandler_1e, _asm_inthandler_1f, _asm_inthandler_10

.extern _inthandler_1, _inthandler_2, _inthandler_3, _inthandler_4, _inthandler_5
.extern _inthandler_6, _inthandler_7, _inthandler_8, _inthandler_9, _inthandler_a
.extern _inthandler_b, _inthandler_c, _inthandler_d, _inthandler_e, _inthandler_f
.extern _inthandler_11, _inthandler_12, _inthandler_13, _inthandler_14, _inthandler_15
.extern _inthandler_16, _inthandler_17, _inthandler_18, _inthandler_19, _inthandler_1a
.extern _inthandler_1b, _inthandler_1c, _inthandler_1d, _inthandler_1e, _inthandler_1f 
.extern _inthandler_10


_start:
	jmp multiboot_entry
.align 8

multiboot_header:
	.int MULTIBOOT2_HEADER_MAGIC
	.int GRUB_MULTIBOOT_ARCHITECTURE_I386
	.int multiboot_header_end - multiboot_header
	.int -(MULTIBOOT2_HEADER_MAGIC + GRUB_MULTIBOOT_ARCHITECTURE_I386 + (multiboot_header_end - multiboot_header))

.align 8
	
address_tag_start:
	.short MULTIBOOT_HEADER_TAG_ADDRESS
	.short MULTIBOOT_HEADER_TAG_OPTIONAL
	.int address_tag_end - address_tag_start
	.int multiboot_header
	.int _start
	.int 0x0
	.int 0x0
address_tag_end:

.align 8

entry_address_tag_start:
	.short MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS
	.short MULTIBOOT_HEADER_TAG_OPTIONAL
	.int entry_address_tag_end - entry_address_tag_start
	.int multiboot_entry
entry_address_tag_end:

.align 8

framebuffer_tag_start:
	.short MULTIBOOT_HEADER_TAG_FRAMEBUFFER
	.short MULTIBOOT_HEADER_TAG_OPTIONAL
	.int framebuffer_tag_end - framebuffer_tag_start
	.int 800
	.int 600
	.int 32
framebuffer_tag_end:

.align 8

undefined_tag:
	.short MULTIBOOT_HEADER_TAG_END
	.short 0
	.int 8
	
multiboot_header_end:
.align 8
multiboot_entry:

	movl $stack, %esp
	pushl $0
	popf
	
#	push %eax
	
#	mov $0x08, %eax
#	mov %eax, %ds
#	mov %eax, %es
#	mov %eax, %fs
#	mov %eax, %gs

#	ljmp $0x10, $newstart
newstart:	
	
	pop %eax
	
	pushl %ebx
	pushl %eax
	
	call _mymain
	
	pushl $str
	call _myprintf
	
1:
	hlt
	jmp 1b

str:
	.string "idealOS is halt!\n"

.align 0x1000	
.fill 0x9000, 1, 0
stack:

.fill 0x100, 1, 0
_func_a_kstack:
.fill 0x100, 1, 0
_func_b_kstack:
.fill 0x100, 1, 0
_func_a_stack:
.fill 0x100, 1, 0
_func_b_stack:

_gdtr:
	.short 3 * 8 - 1
	.int _gdt
	
.align 8
_gdt:
	.quad 0x0000000000000000
	.quad 0x00c092000000ffff
	.quad 0x00c09a000000ffff
	
_myhlt:
#	cli
	hlt
	ret

_load_gdtr: # void load_gdtr (int limit, int addr);
	mov 4(%esp), %ax
	mov %ax, 6(%esp)
	lgdt 6(%esp)
	
	mov $0x08, %eax
	mov %eax, %ds
	mov %eax, %es
	mov %eax, %fs
	mov %eax, %gs

	mov %eax, %ss

#	mov $0, %esp
	
	ljmp $0x10, $mystart
mystart:
	ret
	
_load_idtr: # void load_idtr (int limit, int addr);
	mov 4(%esp), %ax
	mov %ax, 6(%esp)
	lidt 6(%esp)
	ret

.align 8
_setup_page:
	
	movl $0x400, %ecx
	movl $0x1000007, %eax
	movl $0xfff000, %edi
	cld
1:
	stosl
	addl $0x1000, %eax
	loop 1b

	
	movl $0x100000, %ecx
	movl $0x7, %eax
	movl $0x1000000, %edi
	
1:
	stosl
	addl $0x1000, %eax
	loop 1b

	movl $0xfff000, %eax
	movl %eax, %cr3
	
	movl %cr0, %eax
	orl $0x80000000, %eax
	movl %eax, %cr0
	ret
	
_change_page:
	mov $0x7007, %eax
	mov $0x1000000, %edi
	stosl
	
	mov $0x0007, %eax
	mov $(0x1000000 + 7 * 4), %edi
	stosl
	
	movl $0xfff000, %eax
	movl %eax, %cr3
	ret
	
_change0:
	mov $0x0007, %eax
	mov $0x1000000, %edi
	stosl
	
	movl $0xfff000, %eax
	movl %eax, %cr3
	ret

	
_asm_inthandler_all:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_all
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret

# start NMI

_asm_inthandler_0:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_0
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret	
	
_asm_inthandler_1:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_2:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_2
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_3:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_3
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_4:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_4
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_5:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_5
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_6:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_6
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_7:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_7
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_8:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_8
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_9:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_9
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_a:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_a
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_b:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_b
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_c:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_c
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_d:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_d
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_e:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_e
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_f:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_f
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	

	
	
	
_asm_inthandler_10:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_10
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
		
	
_asm_inthandler_11:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_11
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_12:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_12
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_13:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_13
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_14:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_14
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_15:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_15
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_16:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_16
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_17:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_17
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_18:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_18
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_19:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_19
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_1a:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1a
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_1b:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1b
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_1c:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1c
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_1d:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1d
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_1e:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1e
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_asm_inthandler_1f:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_1f
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
# end NMI	
	

	
_asm_inthandler_20:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_20
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret

_asm_inthandler_21:
	cli
	push	%es
	push	%ds
	pusha
	mov		%esp, %eax
	push	%eax
	mov		%ss, %ax
	mov		%ax, %ds
	mov		%ax, %es
	call	_inthandler_21
	pop		%eax
	popa
	pop		%ds
	pop		%es
	iret
	
_sti: #void sti(void);
	sti
	ret
	
_cli: # void cli(void);
	cli
	ret

_init_8259a:
	mov $0x11, %al
	out %al, $0x20
	nop
	out %al, $0xa0
	nop
	mov $0x20, %al
	out %al, $0x21
	nop
	mov $0x28, %al
	out %al, $0xa1
	nop
	mov $0x04, %al
	out %al, $0x21
	nop
	mov $0x02, %al
	out %al, $0xa1
	nop
	mov $0x01, %al
	out %al, $0x21
	nop
	out %al, $0xa1
	nop
	mov $0xff, %al
	out %al, $0x21
	nop
	out %al, $0xa1
	nop
	ret	
	
_out: #void out(int port, int value);
	movl 4(%esp), %edx
	movl 8(%esp), %eax
	out %al, %dx
	ret
	
_in: # unsigned char in(int port);
	movl 4(%esp), %edx
	xor %eax, %eax
	in %dx, %al
	ret
	
	
_delay: #void delay(unsigned int times);
	pushl %ecx
	movl 8(%esp), %ecx
1:
	nop
	loop 1b
	popl %ecx
	ret

_ud2: #void ud2(void);
	ud2
	ret
	
_goto_ring1: #void goto_ring1(void);
	movl (_proc_point_0), %esp
	movl $0x18, %eax
	lldt %ax
	lea 72(%esp), %eax
	movl %eax, (_tss0+ 4)

	popl %gs
	popl %fs
	popl %es
	popl %ds
	popal	
	add $4, %esp
	iret


_set_eflags: #void set_eflags(void);
	pushfl
	andl $0xffffbfff, (%esp)
	popfl
	ret
	
_ltr: # void ltr(int tr);
	mov 4(%esp), %eax
	ltr %ax
	ret
	
_lldt: # void lldt(int ldtr);
	mov 4(%esp), %eax
	lldt %ax
	ret
	

猜你喜欢

转载自blog.csdn.net/weixin_39410618/article/details/82928403
今日推荐