aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/cpu/interrupts.cpp
blob: dc236e6cdacff4599d774a5c5727ac8356bc7da6 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "arch/cpu/interrupts.hpp"

#include "kapi/cpu.hpp"
#include "kapi/memory.hpp"

#include "arch/cpu/legacy_pic.hpp"
#include "arch/cpu/segment_selector.hpp"

#include <kstd/print>

#include <cstdint>

namespace arch::cpu
{

  namespace
  {
    enum struct exception
    {
      divide_error,
      debug_exception,
      non_maskable_interrupt,
      breakpoint,
      overflow,
      bound_range_exceeded,
      invalid_opcode,
      device_not_available,
      double_fault,
      coprocessor_segment_overrun,
      invalid_tss,
      stack_segment_fault,
      general_protection_fault,
      page_fault,
      x87_fpu_floating_point_error,
      alignment_check,
      machine_check,
      simd_floating_point_error,
      virtualization_exception,
      control_protection_exception,
      vmm_communication_exception,
      security_exception,
    };

    constexpr auto pic_master_irq_start = 0x20;
    constexpr auto pic_master_irq_end = pic_master_irq_start + 8;
    constexpr auto pic_slave_irq_start = pic_master_irq_end;
    constexpr auto pic_slave_irq_end = pic_slave_irq_start + 8;

    constexpr auto to_exception_type(exception e)
    {
      switch (e)
      {
        case exception::divide_error:
        case exception::x87_fpu_floating_point_error:
        case exception::simd_floating_point_error:
          return kapi::cpu::exception::type::arithmetic_error;
        case exception::breakpoint:
          return kapi::cpu::exception::type::breakpoint;
        case exception::invalid_opcode:
          return kapi::cpu::exception::type::illegal_instruction;
        case exception::stack_segment_fault:
          return kapi::cpu::exception::type::memory_access_fault;
        case exception::general_protection_fault:
          return kapi::cpu::exception::type::privilege_violation;
        case exception::alignment_check:
          return kapi::cpu::exception::type::alignment_fault;
        default:
          return kapi::cpu::exception::type::unknown;
      }
    }

    auto dispatch_exception(interrupt_frame * frame) -> bool
    {
      auto type = to_exception_type(static_cast<exception>(frame->interrupt.number));
      auto fault_address = kapi::memory::linear_address{};
      auto instruction_pointer = frame->cpu_saved.rip;

      switch (type)
      {
        case kapi::cpu::exception::type::page_fault:
        {
          asm volatile("mov %%cr2, %0" : "=r"(fault_address));
          auto present = (frame->interrupt.error_code & 0x1) != 0;
          auto write = (frame->interrupt.error_code & 0x2) != 0;
          auto user = (frame->interrupt.error_code & 0x4) != 0;

          return kapi::cpu::get_exception_handler().handle(
              {type, instruction_pointer, fault_address, present, write, user});
        }
        default:
          return kapi::cpu::get_exception_handler().handle({type, instruction_pointer});
      }
    }

    auto handle_legacy_interrupt(interrupt_frame * frame) -> void
    {
      kstd::println("[x86_64:SYS] Ignoring 8259 legacy interrupt {:#04x}", frame->interrupt.number);

      if (frame->interrupt.number >= pic_slave_irq_start)
      {
        pic_slave_control_port::write(pic_end_of_interrupt);
      }
      pic_master_control_port::write(pic_end_of_interrupt);
    }
  }  // namespace

  extern "C"
  {
    extern std::uintptr_t const isr_stub_table[256];

    auto interrupt_dispatch(interrupt_frame * frame) -> void
    {
      auto [number, code] = frame->interrupt;

      if (number < pic_master_irq_start && !dispatch_exception(frame))
      {
        kstd::println(kstd::print_sink::stderr,
                      "[x86_64:CPU] Unhandled exception number {:#04x} received with code {:#04x}", number, code);
        kapi::cpu::halt();
      }

      if ((number >= pic_master_irq_start && number < pic_master_irq_end) ||
          (number >= pic_slave_irq_start && number < pic_slave_irq_end))
      {
        handle_legacy_interrupt(frame);
        return;
      }

      kstd::println(kstd::print_sink::stderr, "[x86_64:CPU] Unhandled interrupt {:#04x} received with code {:#04x}",
                    frame->interrupt.number, frame->interrupt.error_code);
      kapi::cpu::halt();
    }
  }

  interrupt_descriptor_table::interrupt_descriptor_table() noexcept
  {
    for (auto i = 0uz; i < 256; ++i)
    {
      m_descriptors[i] = gate_descriptor{
        .offset_low = static_cast<std::uint16_t>(isr_stub_table[i] & 0xffff), // NOLINT(readability-magic-numbers)
        .m_code_segment = segment_selector{0, false, 1},
        .interrupt_stack_table_selector = 0,
        .gate_type = gate_type::interrupt_gate,
        .descriptor_privilege_level = 0,
        .present = true,
        .offset_middle =
            static_cast<std::uint16_t>((isr_stub_table[i] >> 16) & 0xffff), // NOLINT(readability-magic-numbers)
        .offset_high =
            static_cast<std::uint32_t>((isr_stub_table[i] >> 32) & 0xffff'ffff), // NOLINT(readability-magic-numbers)
      };
    }
  }

  auto interrupt_descriptor_table::load() const -> void
  {
    interrupt_descriptor_table_register{.limit = sizeof(m_descriptors) - 1, .base = m_descriptors.data()}.load();
  }

  auto interrupt_descriptor_table_register::load() const -> void
  {
    asm volatile("lidt %0" : : "m"(*this));
  }

  auto interrupt_descriptor_table_register::read() -> interrupt_descriptor_table_register
  {
    interrupt_descriptor_table_register idtr{};
    asm volatile("sidt %0" : : "m"(idtr));
    return idtr;
  }

}  // namespace arch::cpu