panic: Add stub code for UHCI, EHCI & XHCI controllers

This commit is contained in:
EnderIce2 2024-11-10 16:30:43 +02:00
parent a211280891
commit ef1c5bb39e
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
12 changed files with 1127 additions and 29 deletions

View File

@ -38,7 +38,7 @@
#endif
#include "../../kernel.h"
#include "keyboard.hpp"
#include "kbd/keyboard.hpp"
extern void ExPrint(const char *Format, ...);
extern void DisplayTopOverlay();

94
core/panic/kbd/ehci.cpp Normal file
View File

@ -0,0 +1,94 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ehci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
nsa void CrashEHCIKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
{
}
nsa bool CrashEHCIKeyboardDriver::Initialize()
{
return false;
}
nsa CrashEHCIKeyboardDriver::CrashEHCIKeyboardDriver(PCI::PCIDevice dev)
: Interrupts::Handler(dev)
{
Header = (PCIDeviceHeader *)dev.Header;
switch (Header->HeaderType)
{
case 128:
{
ExPrint(ERROR_COLOR "Unknown header type %d! Guessing PCI Header 0\n" DEFAULT_COLOR,
Header->HeaderType);
[[fallthrough]];
}
case 0: /* PCI Header 0 */
{
PCI::PCIHeader0 *hdr = (PCI::PCIHeader0 *)Header;
UNUSED(hdr);
break;
}
case 1: /* PCI Header 1 (PCI-to-PCI Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-PCI Bridge not supported\n" DEFAULT_COLOR);
break;
}
case 2: /* PCI Header 2 (PCI-to-CardBus Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-CardBus Bridge not supported\n" DEFAULT_COLOR);
break;
}
default:
{
ExPrint(ERROR_COLOR "Invalid PCI header type\n" DEFAULT_COLOR);
break;
}
}
}

31
core/panic/kbd/ehci.hpp Normal file
View File

@ -0,0 +1,31 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashEHCIKeyboardDriver : public Interrupts::Handler
{
public:
PCI::PCIDeviceHeader *Header = nullptr;
void OnInterruptReceived(CPU::TrapFrame *Frame);
bool Initialize();
CrashEHCIKeyboardDriver(PCI::PCIDevice dev);
~CrashEHCIKeyboardDriver() = default;
};

101
core/panic/kbd/keyboard.cpp Normal file
View File

@ -0,0 +1,101 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "keyboard.hpp"
#include "ps2.hpp"
#include "uhci.hpp"
#include "ehci.hpp"
#include "xhci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
nsa bool DetectUSBKeyboard()
{
if (!PCIManager)
return false;
std::list<PCIDevice> uhci = PCIManager->FindPCIDevice(0xC, 0x3, 0x00); /* UHCI */
debug("There are %lu UHCI devices", uhci.size());
std::list<PCIDevice> ohci = PCIManager->FindPCIDevice(0xC, 0x3, 0x10); /* OHCI */
debug("There are %lu OHCI devices", ohci.size());
std::list<PCIDevice> ehci = PCIManager->FindPCIDevice(0xC, 0x3, 0x20); /* EHCI */
debug("There are %lu EHCI devices", uhci.size());
std::list<PCIDevice> xhci = PCIManager->FindPCIDevice(0xC, 0x3, 0x30); /* XHCI */
debug("There are %lu XHCI devices", xhci.size());
debug("Initializing UHCI devices");
for (const auto &dev : uhci)
{
CrashUHCIKeyboardDriver *usb = new CrashUHCIKeyboardDriver(dev);
if (!usb->Initialize())
{
error("Failed to initialize UHCI keyboard driver");
uhci.pop_front();
}
}
debug("Initializing EHCI devices");
for (const auto &dev : ehci)
{
CrashEHCIKeyboardDriver *usb = new CrashEHCIKeyboardDriver(dev);
if (!usb->Initialize())
{
error("Failed to initialize UHCI keyboard driver");
ehci.pop_front();
}
}
debug("Initializing XHCI devices");
for (const auto &dev : xhci)
{
CrashXHCIKeyboardDriver *usb = new CrashXHCIKeyboardDriver(dev);
if (!usb->Initialize())
{
error("Failed to initialize XHCI keyboard driver");
xhci.pop_front();
}
}
return xhci.size() > 0 || uhci.size() > 0 || ohci.size() > 0;
}
nsa void InitializeKeyboards()
{
if (DetectUSBKeyboard() == false)
new CrashPS2KeyboardDriver;
}

View File

@ -203,17 +203,4 @@ enum Keys
KEY_U_F12 = 0xd8,
};
class CrashKeyboardDriver : public Interrupts::Handler
{
private:
void PS2Wait(bool Read);
void OnInterruptReceived(CPU::TrapFrame *Frame);
int BackSpaceLimit = 0;
char UserInputBuffer[256];
int TimeoutCallNumber = 0;
public:
CrashKeyboardDriver();
~CrashKeyboardDriver() {}
};
void InitializeKeyboards();

View File

@ -15,6 +15,7 @@
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ps2.hpp"
#include "keyboard.hpp"
#include <interface/aip.h>
@ -28,12 +29,12 @@
#include <io.h>
#if defined(a64)
#include "../../arch/amd64/cpu/gdt.hpp"
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../kernel.h"
#include "../../../kernel.h"
using namespace KernelConsole;
@ -104,7 +105,7 @@ nsa static inline int GetLetterFromScanCode(uint8_t ScanCode)
return KEY_INVALID;
}
nsa void CrashKeyboardDriver::PS2Wait(bool Output)
nsa void CrashPS2KeyboardDriver::PS2Wait(bool Output)
{
#if defined(a86)
TimeoutCallNumber++;
@ -140,10 +141,8 @@ nsa void CrashKeyboardDriver::PS2Wait(bool Output)
automatically sent to I/O APIC if enabled/supported which is bad.
FIXME: On some real devices, the PS/2 keyboard doesn't send interrupts.
TODO: Implement a way to handle USB keyboards in the future.
*/
CrashKeyboardDriver::CrashKeyboardDriver() : Interrupts::Handler(1) /* IRQ1 */
nsa CrashPS2KeyboardDriver::CrashPS2KeyboardDriver() : Interrupts::Handler(1) /* IRQ1 */
{
#define WaitRead PS2Wait(true)
#define WaitWrite PS2Wait(false)
@ -361,10 +360,9 @@ CrashKeyboardDriver::CrashKeyboardDriver() : Interrupts::Handler(1) /* IRQ1 */
CPU::Interrupts(CPU::Enable);
}
nsa void CrashKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
nsa void CrashPS2KeyboardDriver::OnInterruptReceived(CPU::TrapFrame *)
{
#if defined(a86)
UNUSED(Frame);
uint8_t scanCode = inb(PS2_DATA);
if (scanCode == KEY_D_TAB ||

35
core/panic/kbd/ps2.hpp Normal file
View File

@ -0,0 +1,35 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashPS2KeyboardDriver : public Interrupts::Handler
{
private:
void PS2Wait(bool Read);
void OnInterruptReceived(CPU::TrapFrame *Frame);
int BackSpaceLimit = 0;
char UserInputBuffer[256];
int TimeoutCallNumber = 0;
public:
CrashPS2KeyboardDriver();
~CrashPS2KeyboardDriver() = default;
};

178
core/panic/kbd/uhci.cpp Normal file
View File

@ -0,0 +1,178 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "uhci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
nsa void CrashUHCIKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
{
}
__no_sanitize("undefined") nsa bool CrashUHCIKeyboardDriver::Initialize()
{
return false; /* FIXME: stub */
debug("Allocating resources");
this->FrameList = (uint32_t *)(uintptr_t)KernelAllocator.RequestPages(TO_PAGES(1024 * sizeof(FrameList[0])));
this->td = (TD *)KernelAllocator.RequestPages(TO_PAGES(sizeof(TD) * 32));
memset(td, 0, sizeof(TD) * 32);
this->qh = (QH *)KernelAllocator.RequestPages(TO_PAGES(sizeof(QH) * 8));
memset(qh, 0, sizeof(QH) * 8);
/* FIXME: stub */
debug("Initializing controller");
outw((uint16_t)((uintptr_t)io + 0xC0), 0x8F00); /* Disable Legacy Mode Support */
/* Disable All Interrupts */
io->USBINTR.TOCRC = 0;
io->USBINTR.RIE = 0;
io->USBINTR.IOCE = 0;
io->USBINTR.SPIE = 0;
/* Configure Frame List */
io->FRNUM.FN = 0;
io->FRBASEADD.BA = (uint32_t)(uintptr_t)FrameList;
io->SOFMOD.SOFTVAL = 0b1000000;
io->USBSTS.raw = 0xFFFF; /* Clear USBSTS */
if (io->USBSTS.HCH)
io->USBCMD.RS = 1;
else
{
debug("Controller not halted, what to do?");
}
return true;
}
nsa CrashUHCIKeyboardDriver::CrashUHCIKeyboardDriver(PCI::PCIDevice dev)
: Interrupts::Handler(dev)
{
Header = (PCIDeviceHeader *)dev.Header;
switch (Header->HeaderType)
{
case 128:
{
ExPrint(ERROR_COLOR "Unknown header type %d! Guessing PCI Header 0\n" DEFAULT_COLOR,
Header->HeaderType);
[[fallthrough]];
}
case 0: /* PCI Header 0 */
{
PCI::PCIHeader0 *hdr = (PCI::PCIHeader0 *)Header;
uint32_t BAR[6];
size_t BARsSize[6];
BAR[0] = hdr->BAR0;
BAR[1] = hdr->BAR1;
BAR[2] = hdr->BAR2;
BAR[3] = hdr->BAR3;
BAR[4] = hdr->BAR4;
BAR[5] = hdr->BAR5;
/* BARs Size */
for (short i = 0; i < 6; i++)
{
if (BAR[i] == 0)
continue;
size_t size;
if ((BAR[i] & 1) == 0) /* Memory Base */
{
hdr->BAR0 = 0xFFFFFFFF;
size = hdr->BAR0;
hdr->BAR0 = BAR[i];
BARsSize[i] = size & (~15);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFFFFFF;
debug("MEM BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
else if ((BAR[i] & 1) == 1) /* I/O Base */
{
hdr->BAR1 = 0xFFFFFFFF;
size = hdr->BAR1;
hdr->BAR1 = BAR[i];
BARsSize[i] = size & (~3);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFF;
debug("I/O BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
}
uintptr_t baseAddress = BAR[4];
assert(baseAddress & 0x1); /* must be I/O */
debug("baseAddress: %#lx size %#lx", baseAddress, BARsSize[4]);
Memory::Virtual vmm;
vmm.Map((void *)baseAddress, (void *)baseAddress, BARsSize[4], Memory::RW);
io = (IORegisters *)baseAddress;
break;
}
case 1: /* PCI Header 1 (PCI-to-PCI Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-PCI Bridge not supported\n" DEFAULT_COLOR);
break;
}
case 2: /* PCI Header 2 (PCI-to-CardBus Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-CardBus Bridge not supported\n" DEFAULT_COLOR);
break;
}
default:
{
ExPrint(ERROR_COLOR "Invalid PCI header type\n" DEFAULT_COLOR);
break;
}
}
}

215
core/panic/kbd/uhci.hpp Normal file
View File

@ -0,0 +1,215 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashUHCIKeyboardDriver : public Interrupts::Handler
{
public:
struct IORegisters
{
union
{
struct
{
uint16_t RS : 1;
uint16_t HCRESET : 1;
uint16_t GRESET : 1;
uint16_t EGSM : 1;
uint16_t FGR : 1;
uint16_t SWDBG : 1;
uint16_t CF : 1;
uint16_t MAXP : 1;
uint16_t __reserved0 : 8;
} __packed;
uint16_t raw;
} USBCMD;
union
{
struct
{
uint16_t USBINT : 1;
uint16_t USBERRINT : 1;
uint16_t RD : 1;
uint16_t HSE : 1;
uint16_t HCPE : 1;
uint16_t HCH : 1;
uint16_t __reserved0 : 10;
} __packed;
uint16_t raw;
} USBSTS;
union
{
struct
{
uint16_t TOCRC : 1;
uint16_t RIE : 1;
uint16_t IOCE : 1;
uint16_t SPIE : 1;
uint16_t __reserved0 : 12;
} __packed;
uint16_t raw;
} USBINTR;
union
{
struct
{
uint16_t FN : 9;
uint16_t __reserved0 : 7;
} __packed;
uint16_t raw;
} FRNUM;
union
{
struct
{
uint32_t __reserved0 : 12;
uint32_t BA : 20;
} __packed;
uint32_t raw;
} FRBASEADD;
union
{
struct
{
uint8_t SOFTVAL : 7;
uint8_t __reserved0 : 1;
} __packed;
uint8_t raw;
} SOFMOD;
union
{
struct
{
uint16_t CCS : 1;
uint16_t CSC : 1;
uint16_t PE : 1;
uint16_t PEC : 1;
uint16_t LS : 1;
uint16_t RD : 1;
uint16_t __reserved0 : 1; // always 1
uint16_t LSDA : 1;
uint16_t PR : 1;
uint16_t __reserved1 : 2;
uint16_t SUS : 1;
uint16_t __reserved2 : 4;
} __packed;
uint16_t raw;
} PORTSC[2];
} __packed;
struct TD
{
union
{
struct
{
uint32_t T : 1;
uint32_t Q : 1;
uint32_t Vf : 1;
uint32_t __reserved0 : 1;
uint32_t LP : 28;
} __packed;
uint32_t raw;
} LINK;
union
{
struct
{
uint32_t ActLen : 11;
uint32_t __reserved0 : 5;
uint32_t STATUS : 8;
uint32_t IOC : 1;
uint32_t IOS : 1;
uint32_t LS : 1;
uint32_t unknown_0 : 2; /* missing in the spec @ 3.2.2 */
uint32_t SPD : 1;
uint32_t __reserved1 : 2;
} __packed;
uint32_t raw;
} CS;
union
{
struct
{
uint32_t PID : 8;
uint32_t DeviceAddress : 6;
uint32_t EndPt : 4;
uint32_t D : 1;
uint32_t __reserved0 : 1;
uint32_t MaxLen : 11;
} __packed;
uint32_t raw;
} TOKEN;
union
{
struct
{
uint32_t Addr : 32;
} __packed;
uint32_t raw;
} BUFFER;
/* The last 4 DWords of the
Transfer Descriptor are
reserved for use by software.
- UHCI Design Guide 1.1 @ 3.2.5
*/
uint32_t __padding[4];
} *td __aligned(16) __packed;
struct QH
{
union
{
struct
{
uint32_t T : 1;
uint32_t Q : 1;
uint32_t __reserved0 : 2; /* must be 0 */
uint32_t QHLP : 28;
} __packed;
uint32_t raw;
} HEAD;
union
{
struct
{
uint32_t T : 1;
uint32_t Q : 1;
uint32_t __reserved0 : 1;
uint32_t __reserved1 : 1; /* must be 0 */
uint32_t QELP : 28;
} __packed;
uint32_t raw;
} ELEMENT;
/* FIXME: It is the same with TD? */
} *qh __aligned(16) __packed;
PCI::PCIDeviceHeader *Header = nullptr;
IORegisters *io = nullptr;
uint32_t *FrameList = nullptr;
void OnInterruptReceived(CPU::TrapFrame *Frame);
bool Initialize();
CrashUHCIKeyboardDriver(PCI::PCIDevice dev);
~CrashUHCIKeyboardDriver() = default;
};

204
core/panic/kbd/xhci.cpp Normal file
View File

@ -0,0 +1,204 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "xhci.hpp"
#include <interface/aip.h>
#include <display.hpp>
#include <convert.h>
#include <printf.h>
#include <kcon.hpp>
#include <debug.h>
#include <smp.hpp>
#include <cpu.hpp>
#include <io.h>
#if defined(a64)
#include "../../../arch/amd64/cpu/gdt.hpp"
#elif defined(a32)
#elif defined(aa64)
#endif
#include "../../../kernel.h"
using namespace KernelConsole;
using namespace PCI;
#define ERROR_COLOR "\x1b[31m"
#define WARN_COLOR "\x1b[33m"
#define DEFAULT_COLOR "\x1b[0m"
extern void ExPrint(const char *Format, ...);
extern void ArrowInput(uint8_t key);
extern void UserInput(char *Input);
extern FontRenderer CrashFontRenderer;
nsa bool CrashXHCIKeyboardDriver::TakeOwnership()
{
HCExtCap *exCap = (HCExtCap *)(uintptr_t)this->ExtendedCaps;
if (exCap->USBLEGSUP.CapID != 1)
return true;
if (exCap->USBLEGSUP.BIOSOwnsHC == 0)
return true;
exCap->USBLEGSUP.OSOwnsHC = 1;
TimeManager->Sleep(200, Time::Milliseconds);
if (exCap->USBLEGSUP.BIOSOwnsHC == 0)
return true;
ExPrint(ERROR_COLOR "BIOS owns the USB controller\n" DEFAULT_COLOR);
return false;
}
nsa bool CrashXHCIKeyboardDriver::Initialize()
{
int timeout = 10;
if (!TakeOwnership())
{
ExPrint(ERROR_COLOR "Failed to take ownership\n" DEFAULT_COLOR);
return false;
}
stub;
return false; /* FIXME: stub */
}
nsa CrashXHCIKeyboardDriver::CrashXHCIKeyboardDriver(PCIDevice xhci)
: Interrupts::Handler(xhci)
{
Header = (PCIDeviceHeader *)xhci.Header;
switch (Header->HeaderType)
{
case 128:
{
ExPrint(ERROR_COLOR "Unknown header type %d! Guessing PCI Header 0\n" DEFAULT_COLOR,
Header->HeaderType);
[[fallthrough]];
}
case 0: /* PCI Header 0 */
{
PCI::PCIHeader0 *hdr = (PCI::PCIHeader0 *)Header;
uint32_t BAR[6];
size_t BARsSize[6];
BAR[0] = hdr->BAR0;
BAR[1] = hdr->BAR1;
BAR[2] = hdr->BAR2;
BAR[3] = hdr->BAR3;
BAR[4] = hdr->BAR4;
BAR[5] = hdr->BAR5;
/* BARs Size */
for (short i = 0; i < 6; i++)
{
if (BAR[i] == 0)
continue;
size_t size;
if ((BAR[i] & 1) == 0) /* Memory Base */
{
hdr->BAR0 = 0xFFFFFFFF;
size = hdr->BAR0;
hdr->BAR0 = BAR[i];
BARsSize[i] = size & (~15);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFFFFFF;
debug("MEM BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
else if ((BAR[i] & 1) == 1) /* I/O Base */
{
hdr->BAR1 = 0xFFFFFFFF;
size = hdr->BAR1;
hdr->BAR1 = BAR[i];
BARsSize[i] = size & (~3);
BARsSize[i] = ~BARsSize[i] + 1;
BARsSize[i] = BARsSize[i] & 0xFFFF;
debug("IO BAR%d %#lx size: %d",
i, BAR[i], BARsSize[i]);
}
}
debug("IO %d 64-BIT %d", BAR[0] & 0x1, BAR[0] & 0x4);
uintptr_t baseAddress = BAR[0];
if (BAR[0] & 0x4)
baseAddress |= (uintptr_t)BAR[1] << 32;
if (baseAddress & 0x1)
baseAddress &= 0xFFFFFFFFFFFFFFFC;
else
baseAddress &= 0xFFFFFFFFFFFFFFF0;
debug("baseAddress: %#lx", baseAddress);
Memory::Virtual vmm;
vmm.Map((void *)baseAddress, (void *)baseAddress, BARsSize[0], Memory::RW);
caps = (XHCIcap *)baseAddress;
ops = (XHCIop *)(baseAddress + caps->CAPLENGTH);
port = (XHCIport *)(baseAddress + caps->CAPLENGTH + 0x400);
runtime = (XHCIruntime *)(baseAddress + (caps->RTSOFF & ~0x1F));
doorbell = (XHCIdoorbell *)(baseAddress + (caps->DBOFF & ~0x3));
uint16_t exCapOffset = caps->HCCPARAMS1.xHCIExtendedCapacitiesPointer << 2;
ExtendedCaps = (uintptr_t)caps + exCapOffset;
debug("ExtendedCaps: %#lx (%#lx + %#lx)", ExtendedCaps, caps, exCapOffset);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
Interrupter = &runtime->Interrupter[0];
#pragma GCC diagnostic pop
break;
}
case 1: /* PCI Header 1 (PCI-to-PCI Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-PCI Bridge not supported\n" DEFAULT_COLOR);
break;
}
case 2: /* PCI Header 2 (PCI-to-CardBus Bridge) */
{
ExPrint(ERROR_COLOR "PCI-to-CardBus Bridge not supported\n" DEFAULT_COLOR);
break;
}
default:
{
ExPrint(ERROR_COLOR "Invalid PCI header type\n" DEFAULT_COLOR);
break;
}
}
}
nsa void CrashXHCIKeyboardDriver::OnInterruptReceived(CPU::TrapFrame *Frame)
{
debug("Interrupt received");
Interrupter->IMAN.IP = 1;
if (!ops->USBSTS.EINT)
debug("!USBSTS.EINT");
// return;
ops->USBSTS.EINT = 1;
stub;
Interrupter->IMAN.IP = 0;
ops->USBSTS.EINT = 0;
}

257
core/panic/kbd/xhci.hpp Normal file
View File

@ -0,0 +1,257 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ints.hpp>
class CrashXHCIKeyboardDriver : public Interrupts::Handler
{
private:
struct XHCIcap
{
uint8_t CAPLENGTH;
uint8_t __reserved0;
uint16_t HCIVERSION;
uint32_t HCSPARAMS1;
uint32_t HCSPARAMS2;
uint32_t HCSPARAMS3;
union
{
struct
{
uint32_t AC64 : 1;
uint32_t BNC : 1;
uint32_t CSZ : 1;
uint32_t PPC : 1;
uint32_t PIND : 1;
uint32_t LHRC : 1;
uint32_t LTC : 1;
uint32_t NSS : 1;
uint32_t PAE : 1;
uint32_t SPC : 1;
uint32_t SEC : 1;
uint32_t CFC : 1;
uint32_t MaxPSASize : 4;
uint32_t xHCIExtendedCapacitiesPointer : 16;
} __packed;
uint32_t raw;
} HCCPARAMS1;
uint32_t DBOFF;
uint32_t RTSOFF;
uint32_t HCCPARAMS2;
} *caps __packed;
struct XHCIop
{
union
{
struct
{
uint32_t RS : 1;
uint32_t HCRST : 1;
uint32_t INTE : 1;
uint32_t HSEE : 1;
uint32_t __reserved0 : 3;
uint32_t LHCRST : 1;
uint32_t CSS : 1;
uint32_t CRS : 1;
uint32_t EWE : 1;
uint32_t EU3S : 1;
uint32_t __reserved1 : 1;
uint32_t CME : 1;
uint32_t ETE : 1;
uint32_t TSCEN : 1;
uint32_t VTIOEN : 1;
uint32_t __reserved2 : 15;
} __packed;
uint32_t raw;
} USBCMD;
union
{
struct
{
uint32_t HCH : 1;
uint32_t __reserved0 : 1;
uint32_t HSE : 1;
uint32_t EINT : 1;
uint32_t PCB : 1;
uint32_t __reserved1 : 3;
uint32_t SSS : 1;
uint32_t RSS : 1;
uint32_t SRE : 1;
uint32_t CNR : 1;
uint32_t HCE : 1;
uint32_t __reserved2 : 18;
} __packed;
uint32_t raw;
} USBSTS;
uint32_t PAGESIZE;
uint8_t __reserved0[8];
uint32_t DNCTRL;
union
{
struct
{
uint64_t RCS : 1;
uint64_t CS : 1;
uint64_t CA : 1;
uint64_t CRR : 1;
uint64_t __reserved0 : 2;
uint64_t CRP : 58;
} __packed;
uint64_t raw;
} CRCR;
uint8_t __reserved1[16];
uint64_t DCBAAP;
uint32_t CONFIG;
} *ops __packed;
struct XHCIport
{
uint32_t PORTSC;
uint32_t PORTMSC;
uint32_t PORTLI;
uint32_t PORTHLPMC;
} *port __packed;
struct XHCIruntime
{
uint32_t MFINDEX;
uint32_t __reserved0[7];
struct XHCIinterrupter
{
union
{
struct
{
uint32_t IP : 1;
uint32_t IE : 1;
uint32_t __reserved0 : 30;
} __packed;
uint32_t raw;
} IMAN;
union
{
struct
{
uint32_t IMODI : 16;
uint32_t IMODC : 16;
} __packed;
uint32_t raw;
} IMOD;
union
{
struct
{
uint32_t ERSTSZ : 16;
uint32_t __reserved0 : 16;
} __packed;
uint32_t raw;
} ERSTSZ;
uint32_t __reserved;
union
{
struct
{
uint64_t __reserved0 : 6;
uint64_t ERSTBAR : 58;
} __packed;
uint64_t raw;
} ERSTBA;
union
{
struct
{
uint64_t DESI : 3;
uint64_t EHB : 1;
uint64_t ERDP : 60;
} __packed;
uint64_t raw;
} ERDP;
} Interrupter[] __packed;
} *runtime __packed;
union XHCIdoorbell
{
struct
{
uint32_t DBTarget : 8;
uint32_t __reserved : 8;
uint32_t DBTaskID : 16;
} __packed;
uint32_t raw;
} *doorbell;
struct HCExtCap
{
union
{
struct
{
uint32_t CapID : 8;
uint32_t NextCapPtr : 8;
uint32_t BIOSOwnsHC : 1;
uint32_t __reserved0 : 7;
uint32_t OSOwnsHC : 1;
uint32_t __reserved1 : 7;
} __packed;
uint32_t raw;
} USBLEGSUP;
union
{
struct
{
uint32_t unknown : 32;
} __packed;
uint32_t raw;
} USBLEGCTLSTS;
} __packed;
struct XHCIprotocol
{
uint8_t CAPID;
uint8_t NextCapPtr;
uint8_t RevisionMinor;
uint8_t RevisionMajor;
uint8_t Name[4];
uint8_t CompPortOffset;
uint8_t CompPortCount;
uint16_t ProtocolDefined : 12;
uint8_t PSIC : 4;
uint8_t ProtocolSlotType : 4;
uint32_t __reserved0 : 28;
} __packed;
PCI::PCIDeviceHeader *Header = nullptr;
uintptr_t ExtendedCaps = 0;
void *baseAddrArray = nullptr;
std::vector<XHCIprotocol *> Protocols = {};
XHCIruntime::XHCIinterrupter *Interrupter = nullptr;
void OnInterruptReceived(CPU::TrapFrame *Frame);
bool TakeOwnership();
public:
bool Initialize();
CrashXHCIKeyboardDriver(PCI::PCIDevice dev);
~CrashXHCIKeyboardDriver() {}
};

View File

@ -37,7 +37,7 @@
#endif
#include "../../kernel.h"
#include "keyboard.hpp"
#include "kbd/keyboard.hpp"
extern void ExPrint(const char *Format, ...);
extern void DiagnosticDataCollection();
@ -640,13 +640,9 @@ nsa void DisplayCrashScreen(CPU::ExceptionFrame *Frame)
}
DisplayTopOverlay();
DisplayMainScreen(Frame);
new CrashKeyboardDriver;
InitializeKeyboards();
DisplayBottomOverlay();
// CPU::Halt(true);
#ifdef DEBUG
static int once = 0;
@ -749,6 +745,8 @@ nsa void DisplayCrashScreen(CPU::ExceptionFrame *Frame)
ExPrint(keyBuf);
}
#endif
debug("cpu halted");
CPU::Halt(true);
}
nsa void DisplayStackSmashing()