// SPDX-License-Identifier: MPL-2.0
#ifndef SH_LIB_IRQ_H
#define SH_LIB_IRQ_H
#include "std/stdlib.h"
// Interrupt frame structure
#pragma pack(1)
typedef struct {
  sh_uint64 r11,r10,r9,r8,rdx,rcx,rax;
  sh_uint64 vector;
  sh_uint64 error_code;
  sh_uint64 rip;
  sh_uint64 cs;
  sh_uint64 rflags;
} sh_irq_INTERRUPT_FRAME;
#pragma pack()
#define SH_IRQ_DIV_BY_ZERO_FAULT_STRING "Division by Zero #DE"
#define SH_IRQ_DEBUG_STRING "Debug #DB\nWarning: Usage of debugger in Shelter isn't supported and any #DB fault will result in a dead kernel."
#define SH_IRQ_PAGE_FAULT_STRING "Page fault #PF"
#define SH_IRQ_NMI_FAULT_STRING "Non maskable interrupt"
#define SH_IRQ_BREAKPOINT_STRING "Breakpoint #BP\nWarning: Usage of debugger in Shelter isn't supported but usage of #BP fault will not block the kernel."
#define SH_IRQ_OVERFLOW_STRING "Overflow #OF"
#define SH_IRQ_BOUND_RANGE_STRING "Bound range Exceedeed #BR"
#define SH_IRQ_INVALID_OPCODE_STRING "Invalid Opcode #UD"
#define SH_IRQ_DEVICE_NOT_AVAILABLE_STRING "Device Not Available #NM"
#define SH_IRQ_DOUBLE_FAULT_STRING "Double Fault #DF"
#define SH_IRQ_COPROCESSOR_SEGMENT_OVRERRUN_STRING "Coprocessor segment overrun"
#define SH_IRQ_INVALID_TSS_STRING "Invalid TSS #TS"
#define SH_IRQ_SEGMENT_NOT_PRESENT_STRING "Segment Not Present #NP"
#define SH_IRQ_STACK_SEGMENT_FAULT_STRING "Stack Segment Fault #SS"
#define SH_IRQ_GENERAL_PROTECTION_FAULT_STRING "General Protection Fault #GP"
#define SH_IRQ_X87_FLOATING_POINT_EXCEPTION_STRING "x87 Floating Point Exception #MF"
#define SH_IRQ_ALIGNMENT_CHECK_STRING "Alignment Check #AC"
#define SH_IRQ_MACHINE_CHECK_STRING "Machine Check #MC"
#define SH_IRQ_SIMD_FLOATING_POINT_EXCEPTION_STRING "SIMD Floating Point Exception #XM"
#define SH_IRQ_VIRTUALIZATION_EXCEPTION_STRING "Virtualization Exception #VE"
#define SH_IRQ_CONTROL_PROTECTION_EXCEPTION_STRING "Control Protection Exception #CP"
#define SH_IRQ_HYPERVISOR_INJECTION_EXCEPTION_STRING "Hypervisor Injection Exception #HV\nWarning: Shelter will never communicate with an hypervisor."
#define SH_IRQ_VMM_COMMUNICATION_EXCEPTION_STRING "VMM Communication Exception #VC\nWarning: Shelter will never communicate with an hypervisor."
#define SH_IRQ_SECURITY_EXCEPTION_STRING "Security Exception #SX\nWarning: Shelter will never communicate with an hypervisor."
typedef void (*sh_irq_HANDLER_PTR)();
// Start TSC calibration
void sh_irq_start_tsc();
// Return TSC delta mesured between two PIC interrupts
sh_uint64 sh_irq_get_tsc_delta();
// Set timer state of this CPU to SH_TRUE
void sh_irq_start_timer();
// Get timer state of this CPU
sh_bool sh_irq_get_timer_state();
// Set the irq_managed_by_ioapic to SH_TRUE
SH_STATUS sh_irq_switch_irq_management();
// Register a legacy IRQ handler, unmask the corresponding GSI
SH_STATUS sh_irq_legacy_register_handler(sh_uint8 legacy_irq,sh_irq_HANDLER_PTR handler);
// Present interrupt frame if it's a fault
void sh_irq_present_frame(sh_irq_INTERRUPT_FRAME *frame);
// Dispatch interrupts
void sh_irq_dispatch(sh_irq_INTERRUPT_FRAME *frame);
#endif
