void init_gdtidt(void){ struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *)0x00270000; struct GATE_DESCRIPTOR *idt= (struct GATE_DESCRIPTOR*)0x0026f800; int i; //gdt initialize for(i=0; i<8192; i++){ set_segmdesc(gdt+i, 0, 0, 0); } set_segmdesc(gdt+1, 0xffffffff, 0x00000000, 0x4092); set_segmdesc(gdt+2, 0x000fffff, 0x00000000, 0x409a); set_segmdesc(gdt+3, 0x000fffff, 0x00280000, 0x409a); load_gdtr(0xfff, 0x00270000);//??or 0xfff? //idt initialize for(i=0; i<256; i++){ set_gatedesc(idt+i, 0, 0, 0); } for(i=0; i<256; i++){ set_gatedesc(idt+i, (int)asm_inthandler21, 3*8, 0x008e); } set_gatedesc(idt+0x21, (int)asm_inthandler21-0x280000, 3*8, 0x008e); set_gatedesc(idt+0x2c, (int)asm_inthandler2c, 2*8, AR_INTGATE32); load_idtr(0x7ff, 0x0026f800); return; }
void init_gdtidt(void) { struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT; struct GATE_DESCRIPTOR *idt = (struct GATE_DESCRIPTOR *) ADR_IDT; int i; /* initialize GDT */ for (i = 0; i < 8192; i++) { set_segmdesc(gdt + i, 0, 0, 0); } set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, 0x4092); // it represents all the memory that CPU can manage. limit = 4G // 0x4092 0100 0000 10010010 -> 32-bit mode, system special, read and write, not execute set_segmdesc(gdt + 2, 0x0007ffff, 0x00280000, 0x409a); // 512K, from 0x00280000, prepared for bootpack.hrb // 0x409a -> 32-bit mode, system special(ring0), execute and read, not write load_gdtr(0xffff, ADR_GDT); // with the help of assembly /* initialize IDT */ for (i = 0; i < 256; i++) { set_gatedesc(idt + i, 0, 0, 0); } load_idtr(0x7ff, ADR_IDT); // with the help of assembly /* setting IDT */ set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x2c, (int) asm_inthandler2c, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, AR_INTGATE32); return; }
void init_gdtidt() { struct GDT *gdt=(struct GDT *)(0x00270000); struct IDT *idt=(struct IDT *)(0x0026f800); int i; for(i=0;i<8192;i++) { setgdt(gdt+i,0,0,0); } setgdt(gdt+1,0xffffffff ,0x00000000,0x4092);//entry.s main.c data 4GB空间的数据都能访问 setgdt(gdt+2,0x000fffff ,0x00000000,0x409a);//entry.S code //setgdt(gdt+3,0x000fffff ,0x00280000,0x409a); //main.c code 0x7ffff=512kB setgdt(gdt+3,0x000fffff ,0x00000000,0x409a);//entry.S code load_gdtr(0xfff,0X00270000);//this is right for(i=0;i<256;i++) { setidt(idt+i,0,0,0); } for(i=0;i<256;i++) { //setidt(idt+i,(int)asm_inthandler21,3*8,0x008e);//用printdebug显示之后,证明这一部分是写进去了 } //setidt(idt+0x21,(int)asm_inthandler21-0x280000,3*8,0x008e);//用printdebug显示之后,证明这一部分是写进去了 load_idtr(0x7ff,0x0026f800);//this is right return; }
//初始化GDT、IDT void init_gdtidt(void) { struct SEGMENT_DESCRIPTOR *gdt=(struct SEGMENT_DESCRIPTOR *)0x00270000; struct GATE_DESCRIPTOR *idt=(struct GATE_DESCRIPTOR *)0x0026f800; int i; /*GDT的初始化*/ for(i=0;i<8192;i++){ set_segmdesc(gdt+i,0,0,0); } set_segmdesc(gdt+1,0xffffffff,0x00000000,AR_DATA32_RW); set_segmdesc(gdt + 2, LIMIT_BOTPAK, ADR_BOTPAK, AR_CODE32_ER); load_gdtr(LIMIT_GDT, ADR_GDT); /*IDT的初始化*/ for(i=0;i<256;i++){ set_gatedesc(idt+i,0,0,0); } load_idtr(LIMIT_IDT, ADR_IDT); /*IDT的设定*/ set_gatedesc(idt+0x21,(int)asm_inthandler21,2*8,AR_INTGATE32); set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, AR_INTGATE32); set_gatedesc(idt+0x2c,(int)asm_inthandler2c,2*8,AR_INTGATE32); return ; }
static inline void init_idt(void) { //setup idt for (int i = 0; i < 256; i++) { idt[i].raw[0] = 0; idt[i].raw[1] = 0; } for (int i = 0; i < 256; i++) { idt_map_vec(i, isrtbl[i]); } load_idtr(idt, sizeof(idt)); }
void init_exceptions() { int i = 0; // add all Exception Interrupts set_gate(0, int0); set_gate(1, int1); set_gate(2, int2); set_gate(3, int3); set_gate(4, int4); set_gate(5, int5); set_gate(6, int6); set_gate(7, int7); set_gate(8, int8); set_gate(9, int9); set_gate(10, int10); set_gate(11, int11); set_gate(12, int12); set_gate(13, int13); set_gate(14, int14); set_gate(15, int15); set_gate(16, int16); set_gate(17, int17); set_gate(18, int18); set_gate(19, int19); set_gate(20, int20); set_gate(21, int21); set_gate(22, int22); set_gate(23, int23); set_gate(24, int24); set_gate(25, int25); set_gate(26, int26); set_gate(27, int27); set_gate(28, int28); set_gate(29, int29); set_gate(30, int30); set_gate(31, int31); i=32; for (; i < 48; i++) set_gate(i, hwint); set_gate(48, int48); load_idtr(); }
void init_gdtidt() { struct GDT *gdt=(struct GDT *)(0x00270000); struct IDT *idt=(struct IDT *)(0x0026f800); int i; for(i=0;i<8192;i++) { setgdt(gdt+i,0,0,0); } setgdt(gdt+1,0xffffffff ,0x00000000,0x4092);//entry.s main.c data 4GB空间的数据都能访问 setgdt(gdt+2,0x000fffff ,0x00000000,0x409a);//entry.S code setgdt(gdt+3,0x000fffff ,0x00280000,0x409a); //main.c code 0x7ffff=512kB load_gdtr(0xfff,0X00270000);//this is right for(i=0;i<256;i++) { setidt(idt+i,0,0,0); } for(i=0;i<256;i++) { setidt(idt+i,(int)asm_inthandler21,3*8,0x008e);//用printdebug显示之后,证明这一部分是写进去了 } setidt(idt+0x20,(int)asm_inthandler20-0x280000,3*8,0x008e);//挂载timer interrrupt service 汇编程序 setidt(idt+0x21,(int)asm_inthandler21-0x280000,3*8,0x008e);//挂载keyboard interrupt service汇编程序 setidt(idt+0x2c,(int)asm_inthandler2c-0x280000,3*8,0x008e);//挂载mouse interrupt service汇编程序 // setidt(idt+0x21,(int)asm_inthandler21,2*8,0x008e);//挂载keyboard interrupt service // setidt(idt+0x2c,(int)asm_inthandler2c,2*8,0x008e);//挂载mouse interrupt serv //printdebug(asm_inthandler2c,0); load_idtr(0x7ff,0x0026f800);//this is right return; }
//初始化GDT、IDT void init_gdtidt(void) { struct SEGMENT_DESCRIPTOR *gdt=(struct SEGMENT_DESCRIPTOR *)0x00270000; struct GATE_DESCRIPTOR *idt=(struct GATE_DESCRIPTOR *)0x0026f800; int i; /*GDT的初始化*/ for(i=0;i<8192;i++){ set_segmdesc(gdt+i,0,0,0); } set_segmdesc(gdt+1,0xffffffff,0x00000000,0x4092); set_segmdesc(gdt+2,0x0007ffff,0x00280000,0x409a); load_gdtr(0xffff,0x00270000); /*IDT的初始化*/ for(i=0;i<256;i++){ set_gatedesc(idt+i,0,0,0); } load_idtr(0x7ff,0x0026800); return ; }
void init_idt (void) { int i, num_pages_needed; page_frame_t *tmp_page; // // Alloc pages and put TSS'es onto them, we need to share them // between all processes // num_pages_needed = INT_TSS_NUM_PAGES; for (i = INT_TSS_START_PAGE; i < INT_TSS_START_PAGE + num_pages_needed; i++) { tmp_page = get_page_at (i * PAGE_SIZE); add_to_kernel_pages (tmp_page); } for (i = 0; i < NO_OF_IRQS; i++) { int_tss[i] = (tss_386_t *) (INT_TSS_START_PAGE * PAGE_SIZE + sizeof(tss_386_t) * i); } for (i = 0; i < NO_OF_EXCEPTIONS; i++) { exception_tss[i] = (tss_386_t *) (INT_TSS_START_PAGE * PAGE_SIZE + sizeof(tss_386_t) * (i + NO_OF_IRQS)); } system_call_tss = (tss_386_t *) (INT_TSS_START_PAGE * PAGE_SIZE + sizeof(tss_386_t) * (NO_OF_IRQS + NO_OF_EXCEPTIONS)); /* Set pointer */ idt = &_idt; /* set default to bad */ for (i = 0; i <= 255; i++) { set_int_gate (__bad_int, i, D_DPL0); } /* add hardware irq stubs */ set_task_int_gate (__hw_irq_0, IRQ0_VECTOR, D_DPL0, int_tss[0], GDT_INT_START_INDEX); set_task_int_gate (__hw_irq_1, IRQ0_VECTOR + 1, D_DPL0, int_tss[1], GDT_INT_START_INDEX + 1); set_task_int_gate (__hw_irq_2, IRQ0_VECTOR + 2, D_DPL0, int_tss[2], GDT_INT_START_INDEX + 2); set_task_int_gate (__hw_irq_3, IRQ0_VECTOR + 3, D_DPL0, int_tss[3], GDT_INT_START_INDEX + 3); set_task_int_gate (__hw_irq_4, IRQ0_VECTOR + 4, D_DPL0, int_tss[4], GDT_INT_START_INDEX + 4); set_task_int_gate (__hw_irq_5, IRQ0_VECTOR + 5, D_DPL0, int_tss[5], GDT_INT_START_INDEX + 5); set_task_int_gate (__hw_irq_6, IRQ0_VECTOR + 6, D_DPL0, int_tss[6], GDT_INT_START_INDEX + 6); set_task_int_gate (__hw_irq_7, IRQ0_VECTOR + 7, D_DPL0, int_tss[7], GDT_INT_START_INDEX + 7); set_task_int_gate (__hw_irq_8, IRQ8_VECTOR, D_DPL0, int_tss[8], GDT_INT_START_INDEX + 8); set_task_int_gate (__hw_irq_9, IRQ8_VECTOR + 1, D_DPL0, int_tss[9], GDT_INT_START_INDEX + 9); set_task_int_gate (__hw_irq_10, IRQ8_VECTOR + 2, D_DPL0, int_tss[10], GDT_INT_START_INDEX + 10); set_task_int_gate (__hw_irq_11, IRQ8_VECTOR + 3, D_DPL0, int_tss[11], GDT_INT_START_INDEX + 11); set_task_int_gate (__hw_irq_12, IRQ8_VECTOR + 4, D_DPL0, int_tss[12], GDT_INT_START_INDEX + 12); set_task_int_gate (__hw_irq_13, IRQ8_VECTOR + 5, D_DPL0, int_tss[13], GDT_INT_START_INDEX + 13); set_task_int_gate (__hw_irq_14, IRQ8_VECTOR + 6, D_DPL0, int_tss[14], GDT_INT_START_INDEX + 14); set_task_int_gate (__hw_irq_15, IRQ8_VECTOR + 7, D_DPL0, int_tss[15], GDT_INT_START_INDEX + 15); /* add exception stubs */ set_task_int_gate (__exception_0, 0, D_DPL0, exception_tss[0], GDT_INT_START_INDEX + 16); set_task_int_gate (__exception_1, 1, D_DPL0, exception_tss[1], GDT_INT_START_INDEX + 17); set_task_int_gate (__exception_2, 2, D_DPL0, exception_tss[2], GDT_INT_START_INDEX + 18); set_task_int_gate (__exception_3, 3, D_DPL0, exception_tss[3], GDT_INT_START_INDEX + 19); set_task_int_gate (__exception_4, 4, D_DPL0, exception_tss[4], GDT_INT_START_INDEX + 20); set_task_int_gate (__exception_5, 5, D_DPL0, exception_tss[5], GDT_INT_START_INDEX + 21); set_task_int_gate (__exception_6, 6, D_DPL0, exception_tss[6], GDT_INT_START_INDEX + 22); set_task_int_gate (__exception_7, 7, D_DPL0, exception_tss[7], GDT_INT_START_INDEX + 23); set_task_int_gate (__exception_8, 8, D_DPL0, exception_tss[8], GDT_INT_START_INDEX + 24); set_task_int_gate (__exception_9, 9, D_DPL0, exception_tss[9], GDT_INT_START_INDEX + 25); set_task_int_gate (__exception_10, 10, D_DPL0, exception_tss[10], GDT_INT_START_INDEX + 26); set_task_int_gate (__exception_11, 11, D_DPL0, exception_tss[11], GDT_INT_START_INDEX + 27); set_task_int_gate (__exception_12, 12, D_DPL0, exception_tss[12], GDT_INT_START_INDEX + 28); set_task_int_gate (__exception_13, 13, D_DPL0, exception_tss[13], GDT_INT_START_INDEX + 29); set_task_int_gate (__exception_14, 14, D_DPL0, exception_tss[14], GDT_INT_START_INDEX + 30); set_task_int_gate (__exception_16, 16, D_DPL0, exception_tss[15], GDT_INT_START_INDEX + 31); /* * add system call handler * * This is not a TSS gate since it only * switches the current task to kernel-mode * * -- My stacks between user/kernel mode are probably messed up * * OLD: set_int_gate (__system_call_entry, SYSCALL_VECTOR, D_DPL3, system_call_tss, GDT_INT_START_INDEX + 32); * * */ //set_task_int_gate (__system_call_entry, SYSCALL_VECTOR, D_DPL3, system_call_tss, GDT_INT_START_INDEX + 32); set_int_gate (__system_call_entry, SYSCALL_VECTOR, D_DPL3); /* set the IDTR register */ load_idtr(); }
void init_gdtidt(void) { gdt = (struct SEGMENT_DESCRIPTOR *)memory_allocate( (sizeof(struct SEGMENT_DESCRIPTOR) * NUM_GDT)); /* struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *)GDT_ADDR; */ /* struct GATE_DISCRIPTOR *idt = (struct GATE_DISCRIPTOR *) IDT_ADDR; */ idt = (struct GATE_DISCRIPTOR*)memory_allocate( (sizeof(struct GATE_DISCRIPTOR) * NUM_IDT)); printf(TEXT_MODE_SCREEN_RIGHT, "idt: 0x%x", idt); printf(TEXT_MODE_SCREEN_RIGHT, "gdt: 0x%x", gdt); if(idt == NULL){ return; } int i; //init GDT for(i = 0; i < NUM_GDT; i++){ set_segmdesc(gdt + i, 0, 0, 0, 0, 0, 0, 0); } set_segmdesc( gdt + CODE_SEGMENT_NUM, 0xffffffff, 0x00000000, 0, SEG_TYPE_CODE_XRC, DESC_TYPE_SEGMENT, PRIVILEGE_LEVEL_OS, PRESENT); set_segmdesc( gdt + DATA_SEGMENT_NUM, 0xffffffff, 0x00000000, 0, SEG_TYPE_DATE_REW, DESC_TYPE_SEGMENT, PRIVILEGE_LEVEL_OS, PRESENT); /* load_gdtr(0xffff, (uintptr_t)gdt); */ load_gdtr(sizeof(struct SEGMENT_DESCRIPTOR)*4, (uintptr_t)gdt); //init IDT for(int i = 0; i < NUM_IDT; i++){ set_gatedesc(idt + i, (uintptr_t)io_hlt, 0, 0, 0, 0, 0); /* set_gatedesc( */ /* idt + 0x20, (uintptr_t)asm_timer_inthandler, 1*8, GATE_TYPE_32BIT_INT, 0, */ /* PRIVILEGE_LEVEL_OS, PRESENT); */ } /* for(int i = 0; i < 14; i++){ */ /* set_gatedesc( */ /* idt + i, (uintptr_t)asm_fault_inthandler2, 1*8, GATE_TYPE_32BIT_INT, 0, */ /* PRIVILEGE_LEVEL_OS, PRESENT); */ /* } */ /* for(int i = 14; i < 20; i++){ */ /* set_gatedesc( */ /* idt + i, (uintptr_t)asm_fault_inthandler, 1*8, GATE_TYPE_32BIT_INT, 0, */ /* PRIVILEGE_LEVEL_OS, PRESENT); */ /* } */ set_gatedesc( idt + 13, (uintptr_t)asm_fault_inthandler2, CODE_SEGMENT_NUM*8, GATE_TYPE_32BIT_INT, 0, PRIVILEGE_LEVEL_OS, PRESENT); set_gatedesc( idt + 8, (uintptr_t)asm_fault_inthandler, CODE_SEGMENT_NUM*8, GATE_TYPE_32BIT_INT, 0, PRIVILEGE_LEVEL_OS, PRESENT); /* load_idtr(IDT_LIMIT, (uintptr_t)idt); */ load_idtr(sizeof(struct GATE_DISCRIPTOR) * NUM_IDT, (uintptr_t)idt); set_gatedesc( idt + 0x20, (uintptr_t)asm_timer_inthandler, CODE_SEGMENT_NUM*8, GATE_TYPE_32BIT_INT, 0, PRIVILEGE_LEVEL_OS, PRESENT); set_gatedesc( idt + 0x21, (uintptr_t)asm_inthandler21, CODE_SEGMENT_NUM*8, GATE_TYPE_32BIT_INT, 0, PRIVILEGE_LEVEL_OS, PRESENT); return; }
void init_idtr() { idtr.limit = IDT_SIZE*sizeof(struct idt_entry)-1; idtr.base = (u32)idt; load_idtr(idtr); }