summaryrefslogtreecommitdiff
path: root/src/kernel/arch/amd64/interrupts/isr.c
blob: dd97fd467b0b8bbb309282753bcc5bdc7fd896ef (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
#include <kernel/arch/amd64/interrupts.h>
#include <kernel/arch/amd64/port_io.h>
#include <kernel/arch/generic.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <stdbool.h>
#include <stdint.h>

enum {
	NMI = 0x02,
	GP_FAULT = 0x0d,
	PAGE_FAULT = 0x0e,
};

void (*irq_fn[16])(void) = {0};

static void log_interrupt(int interrupt, uint64_t *stackframe) {
	kprintf("interrupt %d, rip = k/%08x, cs 0x%x, code 0x%x\n",
			interrupt, stackframe[0], stackframe[1], stackframe[-1]);
	if ((stackframe[1] & 0x3) == 0) {
		uint64_t *stack = (void*)stackframe[3];
		kprintf("kernel rip = %p, *rip = %p\n", stack, *stack);
	}
	if (interrupt == PAGE_FAULT) {
		uint64_t addr = 0x69;
		asm("mov %%cr2, %0" : "=r"(addr));
		kprintf("addr 0x%x\n", addr);
	}
}

void isr_stage3(uint8_t interrupt, uint64_t *stackframe) {
	uint8_t irqn = interrupt - IRQ_IBASE;
	if (irqn < 16) {
		if (irq_fn[irqn]) {
			irq_fn[irqn]();
			irq_eoi(irqn);
			return;
		}
	}

	if (interrupt == NMI) { /* print some debugging information */
		log_interrupt(interrupt, stackframe);
		mem_debugprint();
		return;
	}

	if (interrupt == PAGE_FAULT || interrupt == GP_FAULT) {
		stackframe++;
	}

	if ((stackframe[1] & 0x3) == 0) { /* in kernel */
		log_interrupt(interrupt, stackframe);
		cpu_halt();
	} else { /* in user */
		proc_kill(proc_cur, interrupt);
		proc_switch_any();
	}
}