1 /* 2 * File : trap.c 3 * This file is part of RT-Thread RTOS 4 * COPYRIGHT (C) 2006, RT-Thread Development Team 5 * 6 * The license and distribution terms for this file may be 7 * found in the file LICENSE in this distribution or at 8 * http://openlab.rt-thread.com/license/LICENSE 9 * 10 * Change Logs: 11 * Date Author Notes 12 */ 13 14 #include <rtthread.h> 15 #include <rthw.h> 16 17 #include <bsp.h> 18 19 /* Interrupt descriptor table. (Must be built at run time because 20 * shifted function addresses can't be represented in relocation records.) 21 */ 22 struct Gatedesc idt[256] = { {0}, }; 23 struct Pseudodesc idt_pd = 24 { 25 0, sizeof(idt) - 1, (unsigned long) idt, 26 }; 27 28 /* exception and interrupt handler table */ 29 extern rt_isr_handler_t isr_table[]; 30 extern rt_isr_handler_t trap_func[]; 31 extern rt_isr_handler_t hdinterrupt_func[]; 32 33 /** 34 * @addtogroup I386 35 */ 36 /*@{*/ 37 38 /** 39 * this function initializes the interrupt descript table 40 * 41 */ 42 void rt_hw_idt_init(void) 43 { 44 extern void Xdefault; 45 int i, j, func; 46 47 // install a default handler 48 for (i = 0; i < sizeof(idt)/sizeof(idt[0]); i++) 49 SETGATE(idt[i], 0, GD_KT, &Xdefault, 0); 50 51 /*install trap handler*/ 52 for(i = 0; i < 16; i++) 53 { 54 func = (int)trap_func[i]; 55 SETGATE(idt[i], 0, GD_KT, func, 0); 56 } 57 58 func = (int)trap_func[3]; 59 SETGATE(idt[3], 0, GD_KT, func, 3); 60 61 i = 0; 62 63 /*install exteral interrupt handler*/ 64 for(j = IRQ_OFFSET; j < IRQ_OFFSET + MAX_HANDLERS; j++) 65 { 66 func = (int)hdinterrupt_func[i]; 67 SETGATE(idt[j], 0, GD_KT, func, 0); 68 i++; 69 } 70 71 // Load the IDT 72 asm volatile("lidt idt_pd + 2"); 73 } 74 75 /** 76 * this function will deal with all kinds of kernel trap 77 * 78 *@param trapno the trap number 79 * 80 */ 81 void rt_hw_trap_irq(int trapno) 82 { 83 switch(trapno) 84 { 85 case T_DIVIDE: 86 rt_kprintf("Divide error interrupt\n"); 87 RT_ASSERT(0); 88 case T_PGFLT: 89 rt_kprintf("Page fault interrupt\n"); 90 RT_ASSERT(0); 91 case T_GPFLT: 92 rt_kprintf("General protection interrupt\n"); 93 RT_ASSERT(0); 94 case T_DEFAULT: 95 rt_hw_interrupt_handle(T_DEFAULT); 96 return; 97 } 98 99 /*kernel bug if run here*/ 100 RT_ASSERT(0); 101 } 102 103 /*@}*/ 104