PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/kiddie/sys/arch/i386/idt.c

http://kiddie.googlecode.com/
C | 147 lines | 55 code | 23 blank | 69 comment | 3 complexity | b450f8f4d46a0b3db552864213144842 MD5 | raw file
  1. /*
  2. * Copyright (c) 2008, Artur Emagulov
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the author nor the names of any co-contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * robot-os
  31. * 09.09.2008
  32. * idt.c -
  33. *
  34. */
  35. #include <i386/atoms.h>
  36. #include <i386/cpu.h>
  37. #include <sys/types.h>
  38. typedef void (*trapfn_t)(void);
  39. /**
  40. * Global Segment descriptor
  41. */
  42. static struct idt_entry idt[NIDTS];
  43. static struct idt_ptr idtp;
  44. /**
  45. * Interrupt table
  46. */
  47. static const trapfn_t intr_table[] =
  48. {
  49. intr_0, intr_1, intr_2,
  50. intr_3, intr_4, intr_5,
  51. intr_6, intr_7, intr_8,
  52. intr_9, intr_10, intr_11,
  53. intr_12, intr_13, intr_14,
  54. intr_15
  55. };
  56. /**
  57. * Trap table
  58. */
  59. static const trapfn_t trap_table[] = {
  60. trap_0, trap_1, trap_2, trap_3,
  61. trap_4, trap_5, trap_6, trap_7,
  62. trap_8, trap_9, trap_10, trap_11,
  63. trap_12,trap_13, trap_14, trap_15,
  64. trap_16, trap_17, trap_18 };
  65. #define NTRAPS (int)(sizeof(trap_table) / sizeof(void *))
  66. /**
  67. * Set IDT (interrupt descriptor table) members into specified vector
  68. */
  69. static void idt_set_gate(int vec, u_long base, u_short sel, u_char flags) {
  70. /* The interrupt routine's base address */
  71. idt[vec].base_lo = (base & 0xFFFF);
  72. idt[vec].base_hi = (base >> 16) & 0xFFFF;
  73. /* The segment or 'selector' that this IDT entry will use
  74. * is set here, along with any access flags */
  75. idt[vec].selector = sel;
  76. idt[vec].always0 = 0;
  77. idt[vec].flags = flags;
  78. }
  79. /**
  80. * Setup the interrupt descriptor table and load it.
  81. *
  82. * IDT layout:
  83. * 0x00 - 0x12 ... S/W trap
  84. * 0x13 - 0x1f ... Intel reserved
  85. * 0x20 - 0x3f ... H/W interrupt
  86. * 0x40 ... System call trap
  87. */
  88. void __arch_idt_init(void) {
  89. int i;
  90. #if 0
  91. printk("Initializing IRQ's...");
  92. interrupt_init();
  93. printk("OK\n");
  94. #endif
  95. printk("Lodaing IDT... ");
  96. idtp.limit = (u16)(sizeof(idt) - 1);
  97. idtp.base = (u32)&idt;
  98. /*memset(&idt, 0, sizeof(struct idt_entry) * 256);*/
  99. /* Fill all vectors with default handler */
  100. for (i = 0; i < NIDTS; i++)
  101. {
  102. idt_set_gate(i, (u_long)trap_default, KERNEL_CS, ST_PRESENT | ST_KERN | ST_TRAP_GATE);
  103. }
  104. /* Setup trap handlers */
  105. for (i = 0; i < NTRAPS; i++)
  106. {
  107. /* printk ("Setting IDT[%d] 0x%x flags[0x%x]",i,KERNEL_CS, ST_PRESENT | ST_KERN | ST_TRAP_GATE); */
  108. idt_set_gate(i, (u_long)trap_table[i], KERNEL_CS, ST_PRESENT | ST_KERN | ST_TRAP_GATE);
  109. }
  110. /* Setup interrupt handlers */
  111. for (i = 0; i < 16; i++)
  112. {
  113. /*printk ("Setting IDT[%d] 0x%x flags[0x%x]\n",0x20 + i,KERNEL_CS, ST_PRESENT | ST_KERN | ST_INTR_GATE);*/
  114. idt_set_gate(0x20 + i, (u_long)intr_table[i], KERNEL_CS, ST_PRESENT | ST_KERN | ST_INTR_GATE);
  115. }
  116. /* Setup debug trap */
  117. /* idt_set_gate(3, trap_3, KERNEL_CS, ST_USER | ST_TRAP_GATE);
  118. */
  119. /* Setup system call handler */
  120. idt_set_gate(SYSCALL_INT, (u_long)syscall_entry, KERNEL_CS, ST_PRESENT | ST_USER | ST_TRAP_GATE);
  121. /* Load IDT */
  122. lidt(&idtp);
  123. printk("OK\n");
  124. }