MonkOS  v0.1
A simple 64-bit operating system (x86_64)
interrupt.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file interrupt.h
3 /// @brief Interrupt handling operations.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license
7 // that can be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 #include <core.h>
13 #include <kernel/x86/cpu.h>
14 
15 //----------------------------------------------------------------------------
16 // Constants
17 //----------------------------------------------------------------------------
18 
19 // Hardware IRQ values
20 #define IRQ_TIMER 0
21 #define IRQ_KEYBOARD 1
22 
23 // Interrupt vector numbers: hardware IRQ traps
24 #define TRAP_IRQ_TIMER 0x20
25 #define TRAP_IRQ_KEYBOARD 0x21
26 
27 // PIC port constants
28 #define PIC_PORT_CMD_MASTER 0x20 ///< Command port for master PIC
29 #define PIC_PORT_CMD_SLAVE 0xa0 ///< Command port for slave PIC
30 #define PIC_PORT_DATA_MASTER 0x21 ///< Data port for master PIC
31 #define PIC_PORT_DATA_SLAVE 0xa1 ///< Data port for slave PIC
32 
33 // PIC commands
34 #define PIC_CMD_EOI 0x20 ///< End of interrupt
35 
36 //----------------------------------------------------------------------------
37 // @struct interrupt_context
38 /// @brief A record describing the CPU state at the time of the
39 /// interrupt.
40 //----------------------------------------------------------------------------
42 {
43  registers_t regs; ///< all general-purpose registers.
44  uint64_t error; ///< exception error identifier.
45  uint64_t interrupt; ///< interrupt vector number.
46  uint64_t retaddr; ///< interrupt return address.
47  uint64_t cs; ///< code segment.
48  uint64_t rflags; ///< flags register.
49  uint64_t rsp; ///< stack pointer.
50  uint64_t ss; ///< stack segment.
51 };
52 
53 typedef struct interrupt_context interrupt_context_t;
54 
55 //----------------------------------------------------------------------------
56 // @function interrupts_init
57 /// @brief Initialize all interrupt tables.
58 /// @details Initialize a table of interrupt service routine thunks, one
59 /// for each of the 256 possible interrupts. Then set up the
60 /// interrupt descriptor table (IDT) to point to each of the
61 /// thunks.
62 ///
63 /// Interrupts should not be enabled until this function has
64 /// been called.
65 //----------------------------------------------------------------------------
66 void
68 
69 //----------------------------------------------------------------------------
70 // @typedef isr_handler
71 /// @brief Interrupt service routine called when an interrupt occurs.
72 /// @param[in] context The CPU state at the time of the interrupt.
73 //----------------------------------------------------------------------------
74 typedef void (*isr_handler)(const interrupt_context_t *context);
75 
76 //----------------------------------------------------------------------------
77 // @function isr_set
78 /// @brief Set an interrupt service routine for the given interrupt
79 /// number.
80 /// @details Interrupts should be disabled while setting these handlers.
81 /// To disable an ISR, set its handler to null.
82 /// @param[in] interrupt Interrupt number (0-255).
83 /// @param[in] handler Interrupt service routine handler function.
84 //----------------------------------------------------------------------------
85 void
86 isr_set(int interrupt, isr_handler handler);
87 
88 //----------------------------------------------------------------------------
89 // @function irq_enable
90 /// @brief Tell the PIC to enable a hardware interrupt.
91 /// @param[in] irq IRQ number to enable (0-15).
92 //----------------------------------------------------------------------------
93 void
94 irq_enable(uint8_t irq);
95 
96 //----------------------------------------------------------------------------
97 // @function irq_disable
98 /// @brief Tell the PIC to disable a hardware interrupt.
99 /// @param[in] irq IRQ number to enable (0-15).
100 //----------------------------------------------------------------------------
101 void
102 irq_disable(uint8_t irq);
uint64_t retaddr
interrupt return address.
Definition: interrupt.h:46
void isr_set(int interrupt, isr_handler handler)
Set an interrupt service routine for the given interrupt number.
Core include file.
A record describing all 64-bit general-purpose registers.
Definition: cpu.h:38
void irq_disable(uint8_t irq)
Tell the PIC to disable a hardware interrupt.
void interrupts_init()
Initialize all interrupt tables.
uint64_t interrupt
interrupt vector number.
Definition: interrupt.h:45
uint64_t cs
code segment.
Definition: interrupt.h:47
uint64_t rsp
stack pointer.
Definition: interrupt.h:49
A record describing the CPU state at the time of the interrupt.
Definition: interrupt.h:41
uint64_t error
exception error identifier.
Definition: interrupt.h:44
void(* isr_handler)(const interrupt_context_t *context)
Interrupt service routine called when an interrupt occurs.
Definition: interrupt.h:74
x86 CPU-specific function implementations.
void irq_enable(uint8_t irq)
Tell the PIC to enable a hardware interrupt.
registers_t regs
all general-purpose registers.
Definition: interrupt.h:43
The log context describes the state of the log buffers.
Definition: log.c:46
uint64_t rflags
flags register.
Definition: interrupt.h:48
uint64_t ss
stack segment.
Definition: interrupt.h:50