Guest User

idt.c

a guest
Sep 5th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <string.h>                                                         // Include headers for int types and memset()
  3.  
  4. struct idt_entryStruct {                                                    // Struct for an entry into the IDT
  5.  
  6.     uint16_t    base1;                                                      // L.O. bits of ISR address
  7.     uint16_t    select;                                                     // Code segment offset into the GDT
  8.     uint8_t     always0;                                      
  9.     uint8_t     flags;                                                      // Flags specifying ring etc.
  10.     uint16_t    base2;                                                      // H.O. bits of ISR address
  11. } __attribute__((packed));
  12.  
  13.  
  14. struct idtrStruct {                                                         // Struct for what will be loaded into idtr
  15.  
  16.     uint16_t    limit;                                                      // Size of the IDT
  17.     uint32_t    base;                                                       // Base address of IDT
  18. } __attribute__((packed));
  19.  
  20.  
  21. struct idt_entryStruct idt[256];                                            // Let's have 256 entries
  22. struct idtrStruct idtr;                                                    
  23.  
  24. void idt_setgate (uint8_t id, uint32_t base, uint16_t select, uint8_t flags) {  // Function to set ISRs, called by loadisr() in isr.c
  25.  
  26.     idt[id].base1   = (base & 0xffff);                                      // Get the L.O. bits of base address
  27.     idt[id].select  = select;                                              
  28.     idt[id].always0 = 0;
  29.     idt[id].flags   = flags;
  30.     idt[id].base2   = (base >> 16) & 0xffff;                                // Get the H.O. bits of base address
  31. }
  32.  
  33. void loadidt (void) {                                                       // Function to load an empty IDT, called by kernel
  34.  
  35.     idtr.limit  = (sizeof(struct idt_entryStruct) * 256) - 1;               // Size of our IDT - 1
  36.     idtr.base   = (uint32_t) &idt;                                          // Base address. TODO: does this need a cast?
  37.    
  38.     memset (&idt, 0, sizeof(struct idt_entryStruct) * 256);                 // Clear out the IDT.
  39.    
  40.     extern void idt_init();                                                 // Call a function defined in asm to lidt our IDT
  41. }
Advertisement
Add Comment
Please, Sign In to add comment