/kiddie/sys/arch/i386/idt.c
C | 147 lines | 55 code | 23 blank | 69 comment | 3 complexity | b450f8f4d46a0b3db552864213144842 MD5 | raw file
- /*
- * Copyright (c) 2008, Artur Emagulov
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the names of any co-contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /*
- * robot-os
- * 09.09.2008
- * idt.c -
- *
- */
- #include <i386/atoms.h>
- #include <i386/cpu.h>
- #include <sys/types.h>
- typedef void (*trapfn_t)(void);
- /**
- * Global Segment descriptor
- */
- static struct idt_entry idt[NIDTS];
- static struct idt_ptr idtp;
- /**
- * Interrupt table
- */
- static const trapfn_t intr_table[] =
- {
- intr_0, intr_1, intr_2,
- intr_3, intr_4, intr_5,
- intr_6, intr_7, intr_8,
- intr_9, intr_10, intr_11,
- intr_12, intr_13, intr_14,
- intr_15
- };
- /**
- * Trap table
- */
- static const trapfn_t trap_table[] = {
- trap_0, trap_1, trap_2, trap_3,
- trap_4, trap_5, trap_6, trap_7,
- trap_8, trap_9, trap_10, trap_11,
- trap_12,trap_13, trap_14, trap_15,
- trap_16, trap_17, trap_18 };
- #define NTRAPS (int)(sizeof(trap_table) / sizeof(void *))
- /**
- * Set IDT (interrupt descriptor table) members into specified vector
- */
- static void idt_set_gate(int vec, u_long base, u_short sel, u_char flags) {
- /* The interrupt routine's base address */
- idt[vec].base_lo = (base & 0xFFFF);
- idt[vec].base_hi = (base >> 16) & 0xFFFF;
- /* The segment or 'selector' that this IDT entry will use
- * is set here, along with any access flags */
- idt[vec].selector = sel;
- idt[vec].always0 = 0;
- idt[vec].flags = flags;
- }
- /**
- * Setup the interrupt descriptor table and load it.
- *
- * IDT layout:
- * 0x00 - 0x12 ... S/W trap
- * 0x13 - 0x1f ... Intel reserved
- * 0x20 - 0x3f ... H/W interrupt
- * 0x40 ... System call trap
- */
- void __arch_idt_init(void) {
- int i;
- #if 0
- printk("Initializing IRQ's...");
- interrupt_init();
- printk("OK\n");
- #endif
- printk("Lodaing IDT... ");
- idtp.limit = (u16)(sizeof(idt) - 1);
- idtp.base = (u32)&idt;
- /*memset(&idt, 0, sizeof(struct idt_entry) * 256);*/
- /* Fill all vectors with default handler */
- for (i = 0; i < NIDTS; i++)
- {
- idt_set_gate(i, (u_long)trap_default, KERNEL_CS, ST_PRESENT | ST_KERN | ST_TRAP_GATE);
- }
- /* Setup trap handlers */
- for (i = 0; i < NTRAPS; i++)
- {
- /* printk ("Setting IDT[%d] 0x%x flags[0x%x]",i,KERNEL_CS, ST_PRESENT | ST_KERN | ST_TRAP_GATE); */
- idt_set_gate(i, (u_long)trap_table[i], KERNEL_CS, ST_PRESENT | ST_KERN | ST_TRAP_GATE);
- }
- /* Setup interrupt handlers */
- for (i = 0; i < 16; i++)
- {
- /*printk ("Setting IDT[%d] 0x%x flags[0x%x]\n",0x20 + i,KERNEL_CS, ST_PRESENT | ST_KERN | ST_INTR_GATE);*/
- idt_set_gate(0x20 + i, (u_long)intr_table[i], KERNEL_CS, ST_PRESENT | ST_KERN | ST_INTR_GATE);
- }
- /* Setup debug trap */
- /* idt_set_gate(3, trap_3, KERNEL_CS, ST_USER | ST_TRAP_GATE);
- */
- /* Setup system call handler */
- idt_set_gate(SYSCALL_INT, (u_long)syscall_entry, KERNEL_CS, ST_PRESENT | ST_USER | ST_TRAP_GATE);
- /* Load IDT */
- lidt(&idtp);
- printk("OK\n");
- }