The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
isr.h
Go to the documentation of this file.
1#pragma once
5#ifdef __x86_64__
6
7/**
8 * saved register state on the kernel stack (x86_64). the layout matches
9 * isr_common in asm64/isr.asm:
10 * 1. the general-purpose registers (pushed by isr_common)
11 * 2. interrupt number and error code (pushed by the stub or the CPU)
12 * 3. rip, cs, rflags, rsp, ss (pushed by the CPU on every interrupt)
13 * the syscall entry (asm64/syscall.asm) makes the same frame.
14 */
15struct registers
16{
17 uintptr_t r15, r14, r13, r12, r11, r10, r9, r8;
18 uintptr_t rbp, rdi, rsi, rdx, rcx, rbx, rax;
19 uintptr_t interrupt; // interrupt vector number.
20 uintptr_t error; // error code (or zero for interrupts without one).
21 uintptr_t rip, cs, rflags, rsp, ss;
22} __attribute__((packed));
23
24#define REG_IP(r) ((r)->rip)
25#define REG_SP(r) ((r)->rsp)
26#define REG_FP(r) ((r)->rbp)
27#define REG_FLAGS(r) ((r)->rflags)
28#define REG_RESULT(r) ((r)->rax)
29
30#else
31
32/**
33 * saved register state pushed onto the stack by ISR stubs.
34 * this structure matches the layout created by the assembly ISR common handler:
35 * 1. data segment (pushed by us)
36 * 2. general-purpose registers (pushed by pusha)
37 * 3. interrupt number and error code (pushed by stub / CPU)
38 * 4. eip, cs, eflags, esp, ss (pushed automatically by the CPU)
39 */
41{
42 uint32_t ds; // data segment selector.
43 uint32_t edi, esi, ebp, kern_esp, ebx, edx, ecx, eax; // general-purpose registers from pusha.
44 uint32_t interrupt; // interrupt vector number.
45 uint32_t error; // error code (or zero for interrupts without one).
46 uint32_t eip, cs, eflags, esp, ss; // pushed automatically by the CPU on interrupt.
47} __attribute__((packed));
48
49#define REG_IP(r) ((r)->eip)
50#define REG_SP(r) ((r)->esp)
51#define REG_FP(r) ((r)->ebp)
52#define REG_FLAGS(r) ((r)->eflags)
53#define REG_RESULT(r) ((r)->eax)
54
55#endif
56
57// the interrupt flag in EFLAGS (RFLAGS): set when interrupts are enabled
58#define EFLAGS_IF 0x200u
59
60/*
61 * REG_IP, REG_SP, REG_FP, REG_FLAGS and REG_RESULT name the instruction
62 * pointer, the stack pointer, the frame pointer, the flags and the
63 * accumulator (the syscall number in, the result out) of a struct
64 * registers. portable code uses them instead of the register names.
65 */
66
67/**
68 * true if the interrupted code ran in user mode (ring 3).
69 */
70static inline bool regs_from_user(const struct registers *regs)
71{
72 return (regs->cs & 3u) == 3u;
73}
74
75/**
76 * interrupt service routine handler function type.
77 * @param regs pointer to the saved register state at the time of the interrupt.
78 */
79typedef void (*isr_handler_t)(struct registers *regs);
80
81/**
82 * initialize the interrupt service routine subsystem.
83 * installs all 256 ISR gates into the IDT and enables them.
84 */
85void isr_initialize(void);
86
87/**
88 * register a handler function for a specific interrupt vector.
89 * @param vector interrupt vector number (0–255).
90 * @param handler pointer to the handler function to call when this interrupt fires.
91 */
92void isr_register_handler(int vector, isr_handler_t handler);
uint32_t ss
Definition gdt.c:20
uint32_t cs
Definition gdt.c:19
void isr_initialize(void)
initialize the interrupt system.
Definition isr.c:50
void isr_register_handler(int vector, isr_handler_t handler)
register a handler for an interrupt vector.
Definition isr.c:93
uint32_t interrupt
Definition isr.h:2
void(*) isr_handler_t(struct registers *regs)
interrupt service routine handler function type.
Definition isr.h:79
uint32_t error
Definition isr.h:3
unsigned int uint32_t
Definition fb.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
ELF32 file header.
Definition elf.h:51
saved register state pushed onto the stack by ISR stubs.
Definition isr.h:41
uint32_t ecx
Definition isr.h:43
uint32_t ss
Definition isr.h:46
uint32_t eax
Definition isr.h:43
uint32_t interrupt
Definition isr.h:44
uint32_t error
Definition isr.h:45
uint32_t cs
Definition isr.h:46
uint32_t ds
Definition isr.h:42
uint32_t ebp
Definition isr.h:43
uint32_t esi
Definition isr.h:43
uint32_t esp
Definition isr.h:46
uint32_t ebx
Definition isr.h:43
uint32_t eip
Definition isr.h:46
uint32_t kern_esp
Definition isr.h:43
uint32_t edi
Definition isr.h:43
uint32_t edx
Definition isr.h:43
uint32_t eflags
Definition isr.h:46