aboutsummaryrefslogtreecommitdiff
path: root/src/idt.cc
blob: e4c21b7a0df59148a81996dd3be1c46ed8712ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "idt.h"
#include <stdlib.h>
#include "ports.h"

static_assert(sizeof(IDT::Pointer) == 6);
// size of IDT::Entry in protected mode is 64 bits
// and in long mode is 128 bits
static_assert(sizeof(IDT::Entry) == 8);

/* reinitialize the PIC controllers, giving them specified vector offsets
   rather than 8h and 70h, as configured by default */

#define ICW1_ICW4 0x01      /* ICW4 (not) needed */
#define ICW1_SINGLE 0x02    /* Single (cascade) mode */
#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
#define ICW1_LEVEL 0x08     /* Level triggered (edge) mode */
#define ICW1_INIT 0x10      /* Initialization - required! */

#define ICW4_8086 0x01       /* 8086/88 (MCS-80/85) mode */
#define ICW4_AUTO 0x02       /* Auto (normal) EOI */
#define ICW4_BUF_SLAVE 0x08  /* Buffered mode/slave */
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
#define ICW4_SFNM 0x10       /* Special fully nested (not) */

template <uint8_t irq>
__attribute__((interrupt)) void t_irq(interrupt_frame*) {
  static_assert(irq <= 15);

  com1_port_t com1;
  com1.write("0123456789abcdef"[irq]);
  com1.write('\n');
  com1.write('\r');

  pic1_t pic1;
  pic1.write(0x20);
  if constexpr (irq > 7) {
    pic2_t pic2;
    pic2.write(0x20);
  }
};

IDT::IDT() {
  pic1_t pic1;
  pic2_t pic2;

  // const auto mask1 = pic1.read(0x1);
  // const auto mask2 = pic2.read(0x1);

  pic1.write(ICW1_INIT | ICW1_ICW4);
  pic2.write(ICW1_INIT | ICW1_ICW4);
  pic1.write(0x20, 0x1);  // offset 0x20
  pic2.write(0x28, 0x1);  // offset 0x28
  pic1.write(4, 0x1);     // tell master pic there is a slave pic
  pic2.write(2, 0x1);     // tell slave pic its cascade identity
  pic1.write(ICW4_8086, 0x1);
  pic2.write(ICW4_8086, 0x1);

  // pic1.write(mask1);
  // pic2.write(mask2);
  pic1.write(0xfd, 0x1);  // only enable irq1
  pic2.write(0xff, 0x1);

  table[0x20] = Entry(reinterpret_cast<unsigned long>(t_irq<0x0>), 0x08, InterruptGate);
  table[0x21] = Entry(reinterpret_cast<unsigned long>(t_irq<0x1>), 0x08, InterruptGate);
  table[0x22] = Entry(reinterpret_cast<unsigned long>(t_irq<0x2>), 0x08, InterruptGate);
  table[0x23] = Entry(reinterpret_cast<unsigned long>(t_irq<0x3>), 0x08, InterruptGate);
  table[0x24] = Entry(reinterpret_cast<unsigned long>(t_irq<0x4>), 0x08, InterruptGate);
  table[0x25] = Entry(reinterpret_cast<unsigned long>(t_irq<0x5>), 0x08, InterruptGate);
  table[0x26] = Entry(reinterpret_cast<unsigned long>(t_irq<0x6>), 0x08, InterruptGate);
  table[0x27] = Entry(reinterpret_cast<unsigned long>(t_irq<0x7>), 0x08, InterruptGate);
  table[0x28] = Entry(reinterpret_cast<unsigned long>(t_irq<0x8>), 0x08, InterruptGate);
  table[0x29] = Entry(reinterpret_cast<unsigned long>(t_irq<0x9>), 0x08, InterruptGate);
  table[0x2a] = Entry(reinterpret_cast<unsigned long>(t_irq<0xa>), 0x08, InterruptGate);
  table[0x2b] = Entry(reinterpret_cast<unsigned long>(t_irq<0xb>), 0x08, InterruptGate);
  table[0x2c] = Entry(reinterpret_cast<unsigned long>(t_irq<0xc>), 0x08, InterruptGate);
  table[0x2d] = Entry(reinterpret_cast<unsigned long>(t_irq<0xd>), 0x08, InterruptGate);
  table[0x2e] = Entry(reinterpret_cast<unsigned long>(t_irq<0xe>), 0x08, InterruptGate);

  Pointer ptr{.limit = sizeof(table), .base = reinterpret_cast<uint32_t>(table)};
  asm volatile("lidt %0" : : "m"(ptr));
  asm volatile("sti");

  printk("IDT installed at ", uhex{ptr.base}, '\n');
}