aboutsummaryrefslogtreecommitdiff
path: root/src/idt.cc
blob: 5be738aa179d67905e55277fa5f89c98b240a085 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "idt.h"
#include <stdlib.h>
#include "cpu/irq.h"
#include "ports.h"

static_assert(sizeof(IDT::Pointer) == 6);
constexpr uint8_t irq_base = 0x20;

static InterruptHandler* handlers[256] = {nullptr};

bool IDT::install(uint8_t irq, InterruptHandler* h) {
  if (h != nullptr && handlers[irq + irq_base] == nullptr) {
    handlers[irq + irq_base] = h;
    return true;
  }
  return false;
}
bool IDT::uninstall(uint8_t irq, InterruptHandler* h) {
  if (handlers[irq + irq_base] == h) {
    handlers[irq + irq_base] = nullptr;
    return true;
  }
  return false;
}

/* 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) */

extern "C" uint32_t handle_exception(cpu_state_t* r) {
  printk("exception ", uhex{r->irq}, " error ", uhex{r->error}, '\n');
  asm volatile("cli");
  while (true) asm volatile("hlt");
  __builtin_unreachable();

  return reinterpret_cast<uint32_t>(r);
};

extern "C" uint32_t handle_interrupt(cpu_state_t* r) {
  if (r->irq < irq_base) return reinterpret_cast<uint32_t>(r);

  if (handlers[r->irq] != nullptr) {
    r = handlers[r->irq]->trigger(r);
  }

  pic1_t pic1;
  pic1.write(0x20);
  if (r->irq > irq_base + 7) {
    pic2_t pic2;
    pic2.write(0x20);
  }

  return reinterpret_cast<uint32_t>(r);
};

IDT::IDT(const uint16_t selector) {
  table[0x00] = Entry(&exception0x00, selector, Intr);
  table[0x01] = Entry(&exception0x01, selector, Intr);
  table[0x02] = Entry(&exception0x02, selector, Intr);
  table[0x03] = Entry(&exception0x03, selector, Intr);
  table[0x04] = Entry(&exception0x04, selector, Intr);
  table[0x05] = Entry(&exception0x05, selector, Intr);
  table[0x06] = Entry(&exception0x06, selector, Intr);
  table[0x07] = Entry(&exception0x07, selector, Intr);
  table[0x08] = Entry(&exception0x08, selector, Intr);
  table[0x09] = Entry(&exception0x09, selector, Intr);
  table[0x0a] = Entry(&exception0x0a, selector, Intr);
  table[0x0b] = Entry(&exception0x0b, selector, Intr);
  table[0x0c] = Entry(&exception0x0c, selector, Intr);
  table[0x0d] = Entry(&exception0x0d, selector, Intr);
  table[0x0e] = Entry(&exception0x0e, selector, Intr);
  table[0x0f] = Entry(&exception0x0f, selector, Intr);
  table[0x10] = Entry(&exception0x10, selector, Intr);
  table[0x11] = Entry(&exception0x11, selector, Intr);
  table[0x12] = Entry(&exception0x12, selector, Intr);
  table[0x13] = Entry(&exception0x13, selector, Intr);

  table[0x20] = Entry(&interrupt0x00, selector, Intr);
  table[0x21] = Entry(&interrupt0x01, selector, Intr);
  table[0x22] = Entry(&interrupt0x02, selector, Intr);
  table[0x23] = Entry(&interrupt0x03, selector, Intr);
  table[0x24] = Entry(&interrupt0x04, selector, Intr);
  table[0x25] = Entry(&interrupt0x05, selector, Intr);
  table[0x26] = Entry(&interrupt0x06, selector, Intr);
  table[0x27] = Entry(&interrupt0x07, selector, Intr);

  table[0x28] = Entry(&interrupt0x08, selector, Intr);
  table[0x29] = Entry(&interrupt0x09, selector, Intr);
  table[0x2a] = Entry(&interrupt0x0a, selector, Intr);
  table[0x2b] = Entry(&interrupt0x0b, selector, Intr);
  table[0x2c] = Entry(&interrupt0x0c, selector, Intr);
  table[0x2d] = Entry(&interrupt0x0d, selector, Intr);
  table[0x2e] = Entry(&interrupt0x0e, selector, Intr);
  table[0x2f] = Entry(&interrupt0x0f, selector, Intr);

  pic1_t pic1;
  pic2_t pic2;

  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(0x04, 0x1);  // tell master pic there is a slave pic
  pic2.write(0x02, 0x1);  // tell slave pic its cascade identity

  pic1.write(ICW4_8086, 0x1);
  pic2.write(ICW4_8086, 0x1);

  // pic masks
  pic1.write(0xfc, 0x1);  // only enable irq1
  pic2.write(0xff, 0x1);

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

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