fix: kernel concurrency, interrupt context, and user VA safety

This commit is contained in:
daniel committed 2026-08-01 15:43:38 +02:00
1 parent f222bf1f04
commit b1c55073c7
71 files changed
+2654 -1092

No files matched your search

+66 -52
View File
@@ -14,6 +14,8 @@
#include <Libraries/Memory.hpp>
#include <Hal/Apic/Interrupts.hpp>
#include <Hal/Apic/IoApic.hpp>
#include <CppLib/Spinlock.hpp>
#include <atomic>
using namespace Kt;
@@ -104,6 +106,11 @@ namespace Drivers::Net::E1000E {
// Current descriptor indices
static uint32_t g_rxTail = 0;
static uint32_t g_txTail = 0;
static kcp::Spinlock g_txLock;
static std::atomic<bool> g_rxWorkPending{false};
static std::atomic<bool> g_rxProcessing{false};
static constexpr uint32_t RX_INTERRUPT_MASK = ICR_RXT0 | ICR_RXDMT0 | ICR_RXO;
// Statistics
static uint64_t g_rxPacketCount = 0;
@@ -410,7 +417,7 @@ namespace Drivers::Net::E1000E {
// MSI setup
// -------------------------------------------------------------------------
static void HandleInterrupt(uint8_t irq); // forward declaration
static void HandleInterrupt(uint8_t irq, bool fromUser); // forward declaration
static bool SetupMsi(uint8_t bus, uint8_t dev, uint8_t func) {
uint8_t cap = Pci::FindCapability(bus, dev, func, Pci::PCI_CAP_MSI);
@@ -419,6 +426,8 @@ namespace Drivers::Net::E1000E {
return false;
}
Pci::DisableInterruptDelivery(bus, dev, func);
KernelLogStream(INFO, "E1000E") << "MSI capability at offset " << base::hex << (uint64_t)cap;
// Read Message Control (cap+2)
@@ -461,7 +470,7 @@ namespace Drivers::Net::E1000E {
// Interrupt handler
// -------------------------------------------------------------------------
static void HandleInterrupt(uint8_t irq) {
static void HandleInterrupt(uint8_t irq, bool) {
(void)irq;
uint32_t icr = ReadReg(REG_ICR);
@@ -472,42 +481,21 @@ namespace Drivers::Net::E1000E {
}
if (icr & ICR_LSC) {
uint32_t status = ReadReg(REG_STATUS);
bool linkUp = (status & (1 << 1)) != 0;
KernelLogStream(INFO, "E1000E") << "Link status change: " << (linkUp ? "UP" : "DOWN");
// Acknowledge/observe the new state without printing from IRQ
// context; KernelLogStream's terminal mutex is not IRQ-safe.
(void)ReadReg(REG_STATUS);
}
// Both receive-timer and descriptor-threshold causes mean completed
// RX descriptors may be waiting. ICR is clear-on-read, so ignoring
// RXDMT0 can strand a lone packet until unrelated traffic arrives.
if (icr & (ICR_RXT0 | ICR_RXDMT0)) {
while (true) {
uint32_t nextIdx = (g_rxTail + 1) % RX_DESC_COUNT;
RxDescriptor& desc = g_rxDescs[nextIdx];
if (!(desc.Status & RXSTA_DD)) {
break;
}
uint16_t length = desc.Length;
g_rxPacketCount++;
if (g_rxCallback != nullptr) {
g_rxCallback(g_rxBuffers[nextIdx], length);
}
desc.Status = 0;
desc.Length = 0;
desc.Errors = 0;
g_rxTail = nextIdx;
WriteReg(REG_RDT, g_rxTail);
}
// Defer the full network/IPC stack. It takes process-context mutexes
// and cannot safely run nested under MSI/legacy interrupt delivery.
if (icr & RX_INTERRUPT_MASK) {
WriteReg(REG_IMC, RX_INTERRUPT_MASK);
g_rxWorkPending.store(true, std::memory_order_release);
}
if (icr & (ICR_TXDW | ICR_TXQE)) {
// TX completion — nothing to do for now
}
// TX completion interrupts are intentionally disabled. Descriptor DD
// is checked by SendPacket before reuse, so an interrupt per packet
// only creates a high-vector MSI load that can starve the LAPIC timer.
}
// -------------------------------------------------------------------------
@@ -596,12 +584,17 @@ namespace Drivers::Net::E1000E {
ConfigureInterruptModeration();
if (SetupMsi(bus, device, function)) {
WriteReg(REG_IMS, ICR_RXT0 | ICR_TXDW | ICR_TXQE | ICR_LSC | ICR_RXDMT0);
WriteReg(REG_IMS, RX_INTERRUPT_MASK | ICR_LSC);
} else if (g_irqLine != 0xFF) {
KernelLogStream(INFO, "E1000E") << "Falling back to legacy IRQ " << base::dec << (uint64_t)g_irqLine;
Pci::DisableInterruptDelivery(bus, device, function);
uint16_t command = Pci::LegacyRead16(bus, device, function,
(uint8_t)Pci::PCI_REG_COMMAND);
Pci::LegacyWrite16(bus, device, function,
(uint8_t)Pci::PCI_REG_COMMAND, command & ~Pci::PCI_CMD_INTX_DISABLE);
Hal::RegisterIrqHandler(g_irqLine, HandleInterrupt);
Hal::IoApic::UnmaskIrq(Hal::IoApic::GetGsiForIrq(g_irqLine));
WriteReg(REG_IMS, ICR_RXT0 | ICR_TXDW | ICR_TXQE | ICR_LSC | ICR_RXDMT0);
WriteReg(REG_IMS, RX_INTERRUPT_MASK | ICR_LSC);
} else {
KernelLogStream(WARNING, "E1000E") << "No MSI or legacy IRQ available, using polling mode";
g_pollingMode = true;
@@ -673,18 +666,12 @@ namespace Drivers::Net::E1000E {
// Polling: process received packets (with reentrancy guard)
// -------------------------------------------------------------------------
static bool g_polling = false;
static void PollRx() {
// Guard against reentrancy: RX callback can trigger ARP reply →
// Ethernet::Send → SendPacket → PollRx(). Two concurrent callers
// modifying g_rxTail / RDT would corrupt the descriptor ring.
if (g_polling) {
return;
}
g_polling = true;
while (true) {
// Never let a continuously refilled DMA ring monopolize the idle
// bottom half. Process at most one revolution; ProcessDeferredWork's
// final descriptor check schedules another pass if traffic remains.
uint32_t processed = 0;
while (processed < RX_DESC_COUNT) {
uint32_t nextIdx = (g_rxTail + 1) % RX_DESC_COUNT;
RxDescriptor& desc = g_rxDescs[nextIdx];
@@ -705,9 +692,9 @@ namespace Drivers::Net::E1000E {
g_rxTail = nextIdx;
WriteReg(REG_RDT, g_rxTail);
processed++;
}
g_polling = false;
}
bool SendPacket(const uint8_t* data, uint16_t length) {
@@ -715,9 +702,13 @@ namespace Drivers::Net::E1000E {
return false;
}
// RX processing can send replies from MSI/timer context while a
// userspace sender runs on another CPU. Protect the shared TX ring.
g_txLock.Acquire();
TxDescriptor& desc = g_txDescs[g_txTail];
if (!(desc.Status & TXSTA_DD)) {
KernelLogStream(WARNING, "E1000E") << "TX ring full";
g_txLock.Release();
return false;
}
@@ -732,6 +723,7 @@ namespace Drivers::Net::E1000E {
WriteReg(REG_TDT, g_txTail);
g_txPacketCount++;
g_txLock.Release();
return true;
}
@@ -765,10 +757,32 @@ namespace Drivers::Net::E1000E {
}
void Poll() {
if (!g_initialized) {
return;
}
if (g_initialized) g_rxWorkPending.store(true, std::memory_order_release);
}
bool HasDeferredWork() {
return g_initialized && g_rxWorkPending.load(std::memory_order_acquire);
}
void ProcessDeferredWork() {
if (!g_initialized || !g_rxWorkPending.load(std::memory_order_acquire)) return;
bool expected = false;
if (!g_rxProcessing.compare_exchange_strong(expected, true,
std::memory_order_acquire, std::memory_order_relaxed)) return;
g_rxWorkPending.store(false, std::memory_order_release);
PollRx();
if (!g_pollingMode) {
WriteReg(REG_IMS, RX_INTERRUPT_MASK);
uint32_t nextIdx = (g_rxTail + 1) % RX_DESC_COUNT;
if (g_rxDescs[nextIdx].Status & RXSTA_DD) {
WriteReg(REG_IMC, RX_INTERRUPT_MASK);
g_rxWorkPending.store(true, std::memory_order_release);
}
}
g_rxProcessing.store(false, std::memory_order_release);
}
};