KernOS
interrupt.cpp
Go to the documentation of this file.
1 //
2 // Created on 5/19/20.
3 //
4 
5 #include <interrupt.h>
6 #include <common.h>
7 #include <utilities.h>
8 #include <gdt.h>
9 #include <accessright.h>
10 #include <pic.h>
11 
12 
13 namespace TIMER
14 {
15  extern uint64_t timer_ticks;
16 }
17 
18 namespace INTRP // interrupt
19 {
21 
29  void RegisterHandler(DescriptorEntry IdtTable[], size_t Idx, func_ptr Handler)
30  {
31  IdtTable[Idx].m_OffsetLow = (ptr_t) (Handler) & 0xFFFF;
33  IdtTable[Idx].m_Reserve = 0x0;
34  IdtTable[Idx].m_Access = AR::INTERRUPT_ACCESS;
35  IdtTable[Idx].m_OffsetHigh = ((ptr_t) (Handler) >> 16) & 0xFFFF;
36  }
37 
41  {
42  kprintf("Unhandled exception encountered\n");
43  Hang();
44  }
45 
49  {
51 
52  kprintf("Timer interrupt occurred\n");
53  Hang();
54  }
55 
59  {
60  kprintf("Page fault handler is not yet set up\n");
61  Hang();
62  }
63 
67  {
68  kprintf("Unhandled interrupt encountered\n");
69  Hang();
70  }
71 
76  {
77  for (size_t Idx = IVT::RESERVED_START; Idx <= IVT::RESERVED_END; ++Idx)
78  RegisterHandler(IdtTable, Idx, UnhandledException);
79 
81  }
82 
87  {
88  for (size_t Idx = IVT::USER_DEFINED_START; Idx <= IVT::USER_DEFINED_END; ++Idx)
89  RegisterHandler(IdtTable, Idx, UnhandledInterrupt);
90 
92  }
93 
98  inline void Load_lidt(void *lidtAddress, uint16_t LimitUse)
99  {
100  struct [[gnu::packed]]
101  {
102  uint16_t Limit;
103  void *Base;
104  } IDTR = { LimitUse, lidtAddress };
105 
106  asm volatile
107  (
108  "lidt %0" // load interrupt descriptor table
109  : // no output
110  : "m"(IDTR) // memory operand allowed, with any kind of address that the machine supports
111  );
112  }
113 
116  void Install_idt()
117  {
118  SetExceptionHandler (idt_table);
119  SetInterruptHandler (idt_table);
120  Load_lidt(idt_table, IDT_ENTRIES * 8 - 1);
121  }
122 } // namespace INTRP
123 
124 namespace INIT
125 {
126  void idt()
127  {
128  PIC::Remap(); // remap PIC's interrupt number as default by Bios unsuitable in protected mode
129  INTRP::Install_idt(); // install exception, and interrupt handlers
130  }
131 }
132 
void Remap()
Remap default 8259 PIC interrupt number to new range.
Definition: pic.cpp:20
uint16_t m_CS_Selector
Definition: interrupt.h:88
DescriptorEntry idt_table[IDT_ENTRIES]
Definition: interrupt.cpp:20
void RegisterHandler(DescriptorEntry IdtTable[], size_t Idx, func_ptr Handler)
Creates interrupt descriptor entries in idt_table, and loads into CPU.
Definition: interrupt.cpp:29
uint16_t m_OffsetHigh
Definition: interrupt.h:91
void Load_lidt(void *lidtAddress, uint16_t LimitUse)
assembly instruction to load idt table to CPU
Definition: interrupt.cpp:98
void(*)() func_ptr
Definition: ktypes.h:16
void kprintf(const char *Str)
Prints string to display.
Definition: kprintf.cpp:13
void idt()
Creates interrupt descriptor table and loads to CPU.
Definition: interrupt.cpp:126
interrupt namespace
Definition: interrupt.h:16
uint8_t m_Access
Definition: interrupt.h:90
void SetInterruptHandler(DescriptorEntry IdtTable[])
Installs default interrupt handler to all interrupts.
Definition: interrupt.cpp:86
uint8_t m_Reserve
Definition: interrupt.h:89
void Install_idt()
Assigns exception and interrupt handler to idt_table, and load to CPU.
Definition: interrupt.cpp:116
const uint16_t IDT_ENTRIES
Definition: interrupt.h:71
uint64_t ptr_t
Definition: ktypes.h:13
contains all kernel initialization routines
Definition: cpu.h:10
Timer namespace.
Definition: pit.h:10
uint16_t m_OffsetLow
Definition: interrupt.h:87
constexpr uint8_t SEG_OFFSET(const Segment Seg)
Translates code segment enum to code segment selector.
Definition: gdt.h:29
void UnhandledInterrupt()
default unhandled interrupt handler
Definition: interrupt.cpp:66
kernel code segment
Definition: gdt.h:19
Exception and interrupt gate descriptor entry.
Definition: interrupt.h:83
uint64_t timer_ticks
Definition: pit.cpp:30
void SetExceptionHandler(DescriptorEntry IdtTable[])
Installs default exception handler to all exceptions.
Definition: interrupt.cpp:75
const uint8_t INTERRUPT_ACCESS
Interrupt access descriptor.
Definition: accessright.h:95
void UnhandledException()
default unhandled exception handler
Definition: interrupt.cpp:40
void PageFaultHandler()
Page fault handler.
Definition: interrupt.cpp:58
void TimerInterruptHandler()
Timer interrupt handler.
Definition: interrupt.cpp:48