PatchworkOS
Loading...
Searching...
No Matches
irq.c
Go to the documentation of this file.
1#include <kernel/cpu/irq.h>
2
4#include <kernel/cpu/smp.h>
6#include <kernel/log/log.h>
7#include <kernel/log/panic.h>
9
12
14{
16
18 irq_handler_t* handler = &handlers[irq];
19
20 bool handled = false;
21 for (uint64_t i = 0; i < handler->callbackAmount; i++)
22 {
23 irq_callback_t* callback = &handler->callbacks[i];
24 if (callback->func != NULL)
25 {
26 callback->func(irq, callback->data);
27 handled = true;
28 }
29 }
30
31 if (!handled)
32 {
33 LOG_WARN("unhandled irq %llu (vector=0x%x)\n", irq, frame->vector);
34 }
35
36 lapic_eoi();
37}
38
40{
42
43 irq_handler_t* handler = &handlers[irq];
44 if (handler->callbackAmount >= IRQ_MAX_CALLBACK)
45 {
46 panic(NULL, "IRQ handler limit exceeded for irq=%d\n", irq);
47 }
48
49 uint64_t slot = handler->callbackAmount;
50 handler->callbacks[slot].func = func;
51 handler->callbacks[slot].data = data;
52 handler->callbackAmount++;
53
54 if (!handler->redirected)
55 {
58 }
59
60 LOG_INFO("installed handler for irq=%d slot=%u\n", irq, slot);
61}
62
64{
66
67 irq_handler_t* handler = &handlers[irq];
68 for (uint32_t i = 0; i < IRQ_MAX_CALLBACK; i++)
69 {
70 irq_callback_t* callback = &handler->callbacks[i];
71 if (callback->func == func)
72 {
73 callback->func = NULL;
74 callback->data = NULL;
75 handler->callbackAmount--;
76 LOG_INFO("uninstalled handler for irq=%d slot=%u\n", irq, i);
77 }
78 }
79}
static fd_t data
Definition dwm.c:21
@ EXTERNAL_INTERRUPT_BASE
Definition interrupt.h:132
void irq_install(irq_t irq, irq_callback_func_t func, void *data)
Install an IRQ handler.
Definition irq.c:39
#define IRQ_MAX_CALLBACK
Maximum amount of callbacks per IRQ.
Definition irq.h:46
irq_t
IRQ numbers.
Definition irq.h:23
void irq_dispatch(interrupt_frame_t *frame)
Dispatch an IRQ.
Definition irq.c:13
void irq_uninstall(irq_t irq, irq_callback_func_t func)
Uninstall an IRQ handler.
Definition irq.c:63
void(* irq_callback_func_t)(irq_t irq, void *data)
Callback function type for IRQs.
Definition irq.h:51
@ IRQ_AMOUNT
Definition irq.h:40
static cpu_t * smp_self_unsafe(void)
Returns a pointer to the cpu_t structure of the current CPU.
Definition smp.h:90
void lapic_eoi(void)
Send an End Of Interrupt (EOI) signal to the local apic.
Definition apic.c:236
void ioapic_set_redirect(interrupt_t vector, ioapic_gsi_t gsi, ioapic_delivery_mode_t deliveryMode, ioapic_polarity_t polarity, ioapic_trigger_mode_t triggerMode, cpu_t *cpu, bool enable)
Set an IOAPIC redirection entry.
Definition apic.c:307
@ IOAPIC_DELIVERY_NORMAL
Definition apic.h:204
@ IOAPIC_POLARITY_HIGH
Definition apic.h:238
@ IOAPIC_TRIGGER_EDGE
Definition apic.h:228
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
#define LOG_WARN(format,...)
Definition log.h:88
#define LOG_INFO(format,...)
Definition log.h:87
#define RWLOCK_CREATE
Create a rwlock initializer.
Definition rwlock.h:45
#define RWLOCK_READ_SCOPE(lock)
Acquires a rwlock for reading for the reminder of the current scope.
Definition rwlock.h:27
#define RWLOCK_WRITE_SCOPE(lock)
Acquires a rwlock for writing for the reminder of the current scope.
Definition rwlock.h:36
#define NULL
Pointer error value.
Definition NULL.h:23
static irq_handler_t handlers[IRQ_AMOUNT]
Definition irq.c:11
static rwlock_t lock
Definition irq.c:10
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Trap Frame Structure.
Definition interrupt.h:42
uint64_t vector
Definition interrupt.h:59
Structure to hold an IRQ callback and its data.
Definition irq.h:57
irq_callback_func_t func
Definition irq.h:58
void * data
Definition irq.h:59
Structure to hold all callbacks for an IRQ.
Definition irq.h:66
uint32_t callbackAmount
Definition irq.h:68
irq_callback_t callbacks[IRQ_MAX_CALLBACK]
Definition irq.h:67
bool redirected
Definition irq.h:69
Read-Write Ticket Lock structure.
Definition rwlock.h:59