Guest User

IDT C code

a guest
Sep 26th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdint.h>
  2. #include "idt.h"
  3.  
  4. extern int printf(const char *fmt, ...);
  5.  
  6. int interrupt_number;
  7.  
  8. struct idt_descriptor {
  9.     uint16_t base_low;          /* first 16 bits of the base address of the IR */
  10.     uint16_t selector;          /* segment selector */
  11.     uint8_t  reserved;          /* reserved: always 0 */
  12.     uint8_t  flags;             /* bits 39-41: flags, bits 42-45: DPL */
  13.     uint16_t base_high;         /* bits 16-31 of the base address of the IR */
  14. } __attribute__ ((packed));
  15.  
  16. struct idt_pointer {
  17.     uint16_t limit;             /* size of the IDT */
  18.     uint32_t base;              /* base address of the IDT */
  19. } __attribute__ ((packed));
  20.  
  21. struct idt_descriptor idt_desc[MAX_INTERRUPTS];
  22. struct idt_pointer idt_p;
  23.  
  24. /* default handler to catch unhandled system interrupts */
  25.  
  26. void interrupt_handler()
  27. {
  28.     printf("Unhandled exception ");
  29.    
  30.     while (1) {
  31.        
  32.     }  
  33. }
  34.  
  35. void set_interrupt_number(int num)
  36. {
  37.     interrupt_number = num;
  38. }
  39.  
  40. void idt_set_gate(uint32_t i, uint64_t base, uint16_t selector, uint8_t flags)
  41. {
  42.     /*if (i > MAX_INTERRUPTS)
  43.         return;*/
  44.    
  45.     /* The interrupt routine's base address */
  46.    
  47.     idt_desc[i].base_low    = (base & 0xFFFF);
  48.     idt_desc[i].base_high   = (base >> 16) & 0xFFFF;
  49.  
  50.     /* The segment or 'selector' that this IDT entry will use
  51.     *  is set here, along with any access flags */
  52.    
  53.     idt_desc[i].selector    = selector;
  54.     idt_desc[i].reserved    = 0;
  55.     idt_desc[i].flags       = flags;
  56. }
  57.  
  58. void idt_install()
  59. {
  60.     idt_p.limit = (sizeof(idt_desc) * MAX_INTERRUPTS) - 1;
  61.     idt_p.base  = (uint32_t) &idt_desc;
  62.    
  63.     /* null the entire IDT */
  64.    
  65.     memset(&idt_desc, 0, sizeof(idt_desc) * MAX_INTERRUPTS);
  66.    
  67.     /* install the new IDT */
  68.    
  69.     _idt_install();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment