summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/HALKit/AMD64/HalCoreInterruptHandler.cpp
blob: c6217ccb0af6693c0e02603e5a44cdaaf9ae93f1 (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
172
173
174
175
176
177
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/ne-foss-org/nekernel

#include <ArchKit/ArchKit.h>
#include <KernelKit/ProcessScheduler.h>
#include <KernelKit/UserMgr+User.h>
#include <NeKit/Atom.h>
#include <NeKit/KString.h>
#include <SignalKit/Signals.h>

EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip);
EXTERN_C Kernel::UIntPtr kApicBaseAddress;

STATIC BOOL kIsRunning{NO};

/// @brief Notify APIC and PIC that we're done with the interrupt.
/// @note
static void hal_idt_send_eoi(UInt8 vector) {
  ((volatile UInt32*) kApicBaseAddress)[0xB0 / 4] = 0;

  if (vector >= kPICCommand && vector <= 0x2F) {
    if (vector >= 0x28) {
      Kernel::HAL::rt_out8(kPIC2Command, kPICCommand);
    }
    Kernel::HAL::rt_out8(kPICCommand, kPICCommand);
  }
}

/// @brief Handle GPF fault.
/// @param rsp
EXTERN_C Kernel::Void idt_handle_gpf(Kernel::UIntPtr rsp) {
  auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess();

  if (process) process.Crash();

  hal_idt_send_eoi(13);

  if (process) {
    process.Signal.SignalArg = rsp;
    process.Signal.SignalID  = SIGKILL;
    process.Signal.Status    = process.Status;
  }
}

/// @brief Handle page fault.
/// @param rsp
EXTERN_C void idt_handle_pf(Kernel::UIntPtr rsp) {
  auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess();

  if (process) process.Crash();

  hal_idt_send_eoi(14);

  if (process) {
    process.Signal.SignalArg = rsp;
    process.Signal.SignalID  = SIGKILL;
    process.Signal.Status    = process.Status;
  }
}

/// @brief Handle scheduler interrupt.
EXTERN_C void idt_handle_scheduler(Kernel::UIntPtr rsp) {
  NE_UNUSED(rsp);

  hal_idt_send_eoi(32);

  while (kIsRunning);

  kIsRunning = YES;

  Kernel::UserProcessHelper::StartScheduling();

  kIsRunning = NO;
}

/// @brief Handle math fault.
/// @param rsp
EXTERN_C void idt_handle_math(Kernel::UIntPtr rsp) {
  auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess();

  if (process) process.Crash();

  hal_idt_send_eoi(8);

  if (process) {
    process.Signal.SignalArg = rsp;
    process.Signal.SignalID  = sig_generate_unique<SIGKILL>();
    process.Signal.Status    = process.Status;
  }
}

/// @brief Handle any generic fault.
/// @param rsp
EXTERN_C void idt_handle_generic(Kernel::UIntPtr rsp) {
  auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess();

  if (process) process.Crash();

  hal_idt_send_eoi(30);

  Kernel::kout << "Kernel: Generic Process Fault.\r";

  process.Signal.SignalArg = rsp;
  process.Signal.SignalID  = sig_generate_unique<SIGSEG>();
  ;
  process.Signal.Status = process.Status;

  Kernel::kout << "Kernel: SIGKILL status.\r";
}

EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip) {
  auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess();

  if (process) process.Crash();

  hal_idt_send_eoi(3);

  process.Signal.SignalArg = rip;
  process.Signal.SignalID  = sig_generate_unique<SIGTRAP>();

  process.Signal.Status = process.Status;

  process.Status = Kernel::ProcessStatusKind::kFrozen;
}

/// @brief Handle #UD fault.
/// @param rsp
EXTERN_C void idt_handle_ud(Kernel::UIntPtr rsp) {
  auto process = Kernel::UserProcessScheduler::The().TheCurrentProcess();

  if (process) process.Crash();

  hal_idt_send_eoi(6);

  process.Signal.SignalArg = rsp;
  process.Signal.SignalID  = sig_generate_unique<SIGKILL>();
  process.Signal.Status    = process.Status;
}

/// @brief Enter syscall from assembly (libSystem only)
/// @param stack the stack pushed from assembly routine.
/// @return nothing.
EXTERN_C Kernel::Void hal_system_call_enter(Kernel::UIntPtr rcx_hash,
                                            Kernel::UIntPtr rdx_syscall_arg) {
  hal_idt_send_eoi(50);

  if (!Kernel::kCurrentUser) return;

  for (SizeT i = 0UL; i < kMaxDispatchCallCount; ++i) {
    if (kSysCalls[i].fHooked && rcx_hash == kSysCalls[i].fHash) {
      if (kSysCalls[i].fProc) {
        (kSysCalls[i].fProc)((Kernel::VoidPtr) rdx_syscall_arg);
      }
    }
  }
}

/// @brief Enter Kernel call from assembly (libDDK only).
/// @param stack the stack pushed from assembly routine.
/// @return nothing.
EXTERN_C Kernel::Void hal_kernel_call_enter(Kernel::UIntPtr rcx_hash, Kernel::SizeT cnt,
                                            Kernel::UIntPtr arg, Kernel::SizeT sz) {
  hal_idt_send_eoi(51);

  if (!Kernel::kRootUser) return;
  if (Kernel::kCurrentUser != Kernel::kRootUser) return;
  if (!Kernel::kCurrentUser->IsSuperUser()) return;

  for (SizeT i = 0UL; i < kMaxDispatchCallCount; ++i) {
    if (kKernCalls[i].fHooked && rcx_hash == kKernCalls[rcx_hash].fHash) {
      if (kKernCalls[i].fProc) {
        (kKernCalls[i].fProc)(cnt, (Kernel::VoidPtr) arg, sz);
      }
    }
  }
}