IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
irq.cpp
Go to the documentation of this file.
1
11
12
13// IgrOS-Kernel arch x86
14#include <arch/x86/io.hpp>
15#include <arch/x86/irq.hpp>
16#include <arch/x86/isr.hpp>
17// IgrOS-Kernel library
18#include <klib/kPrint.hpp>
19
20
21#ifdef __cplusplus
22
23extern "C" {
24
25#endif // __cplusplus
26
27
29 void irqEnable() noexcept;
31 void irqDisable() noexcept;
32
33
34#ifdef __cplusplus
35
36} // extern "C"
37
38#endif // __cplusplus
39
40
41// x86 namespace
42namespace igros::x86 {
43
44
45 // Init IRQ
46 void irq::init() noexcept {
47 // Restart PIC`s
50 // Remap IRQ`s because of exceptions
53 // Setup PIC`s cascading
56 // Setup done
59 // Mask all interrupts (drivers unmask their own lines)
61 }
62
63
64 // Enable interrupts
65 void irq::enable() noexcept {
67 }
68
69 // Disable interrupts
70 void irq::disable() noexcept {
72 }
73
74
75 // Mask interrupt (disable line)
76 void irq::mask(const irq_t number) noexcept {
77 // Check if it's hardware interrupt
78 if (static_cast<igros_dword_t>(number) < 16_u32) [[likely]] {
79 // Set line bit in PIC mask
80 irq::setMask(static_cast<igros_word_t>(static_cast<igros_dword_t>(irq::getMask()) | (1_u32 << static_cast<igros_dword_t>(number))));
81 }
82 }
83
84 // Unmask interrupt (enable line)
85 void irq::unmask(const irq_t number) noexcept {
86 // Check if it's hardware interrupt
87 if (static_cast<igros_dword_t>(number) < 16_u32) [[likely]] {
88 // Clear line bit in PIC mask
89 irq::setMask(static_cast<igros_word_t>(static_cast<igros_dword_t>(irq::getMask()) & ~(1_u32 << static_cast<igros_dword_t>(number))));
90 }
91 }
92
93
94 // Set interrupts mask
95 void irq::setMask(const igros_word_t mask) noexcept {
96 // Mask bits in 32-bit arithmetic (16-bit value would be promoted to int)
97 const auto bits {static_cast<igros_dword_t>(mask)};
98 // Set Master controller mask
99 ::portWrite8(PIC_MASTER_DATA, static_cast<igros_byte_t>(bits & 0x00FF_u32));
100 // Set Slave controller mask
101 ::portWrite8(PIC_SLAVE_DATA, static_cast<igros_byte_t>((bits >> 8) & 0x00FF_u32));
102 }
103
104 // Get interrupts mask
105 [[nodiscard]]
106 auto irq::getMask() noexcept -> igros_word_t {
107 // Read slave PIC current mask
108 const auto high {static_cast<igros_dword_t>(::portRead8(PIC_SLAVE_DATA))};
109 // Read master PIC current mask
110 const auto low {static_cast<igros_dword_t>(::portRead8(PIC_MASTER_DATA))};
111 // Return IRQ mask
112 return static_cast<igros_word_t>((high << 8) | low);
113 }
114
115
116 // Send EOI (IRQ done)
117 void irq::eoi(const irq_t number) noexcept {
118 // Slave PIC lines (8..15) need EOI on both controllers
119 if (static_cast<igros_dword_t>(number) >= 8_u32) {
120 // Notify slave PIC
122 }
123 // Notify master PIC
125 }
126
127
128 // Check if IRQ is spurious (only lines 7 and 15 can be)
129 [[nodiscard]]
130 auto irq::isSpurious(const irq_t number) noexcept -> bool {
131 // Spurious IRQ is reported on the lowest priority line of PIC
132 if ((irq_t::LPT1 != number) && (irq_t::ATA_SECONDARY != number)) [[likely]] {
133 return false;
134 }
135 // Command port of PIC which raised IRQ
136 const auto control {(irq_t::LPT1 == number) ? PIC_MASTER_CONTROL : PIC_SLAVE_CONTROL};
137 // Select In-Service Register (OCW3)
138 ::portWrite8(control, 0x0B_u8);
139 // Line 7 of the PIC is not in service - IRQ is spurious
140 return 0x00_u8 == (::portRead8(control) & 0x80_u8);
141 }
142
143
144} // namespace igros::x86
145
static void disable() noexcept
Disable interrupts.
Definition irq.cpp:70
static auto getMask() noexcept -> igros_word_t
Get interrupts mask.
Definition irq.cpp:106
static void mask(const irq_t number) noexcept
Mask interrupt.
Definition irq.cpp:76
static void setMask(const igros_word_t mask=0xFFFF_u16) noexcept
Set interrupts mask.
Definition irq.cpp:95
static void enable() noexcept
Enable interrupts.
Definition irq.cpp:65
static void init() noexcept
Init IRQ.
Definition irq.cpp:46
static void unmask(const irq_t number) noexcept
Unmask interrupt.
Definition irq.cpp:85
static void eoi(const irq_t number) noexcept
Send EOI (IRQ done).
Definition irq.cpp:117
static auto isSpurious(const irq_t number) noexcept -> bool
Check if IRQ is spurious (only lines 7 and 15 can be).
Definition irq.cpp:130
std::uint16_t igros_word_t
16-bit unsigned integer
Definition types.hpp:42
std::uint8_t igros_byte_t
8-bit unsigned integer
Definition types.hpp:41
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
void irqEnable() noexcept
Enable interrupts.
void irqDisable() noexcept
Disable interrupts.
Interrupts low-level operations.
Kernel text print functions.
Code shared by i386 and x86_64: ports, interrupts, exceptions, paging.
constexpr auto PIC_MASTER_DATA
Master PIC data (mask) port.
Definition irq.hpp:55
constexpr auto PIC_SLAVE_DATA
Slave PIC data (mask) port.
Definition irq.hpp:58
constexpr auto PIC_MASTER_CONTROL
Master PIC command port.
Definition irq.hpp:54
constexpr auto PIC_SLAVE_CONTROL
Slave PIC command port.
Definition irq.hpp:57
irq_t
Interrupts number enumeration (PIC lines).
Definition irq.hpp:62
@ LPT1
Master PIC spurious.
Definition irq.hpp:70
@ ATA_SECONDARY
Slave PIC spurious.
Definition irq.hpp:78
IO low-level port operations.
auto portRead8(const igros::x86::port_t addr) noexcept -> igros::igros_byte_t
Read byte from port.
void portWrite8(const igros::x86::port_t addr, const igros::igros_byte_t value) noexcept
Write byte to port.
Interrupts low-level operations.