Compare commits

...

5 Commits

Author SHA1 Message Date
Drew Galbraith e5da93757a Working AHCI DMA IPC from yellowstone to denali.
We have a weird bug in context switching where a process's rsp0 ends up
pointing at the wrong value sometimes which crashes the OS.
2023-06-16 01:31:23 -07:00
Drew Galbraith ccfe1b15ab Compile with -fno-rtti 2023-06-16 01:30:31 -07:00
Drew Galbraith f154d11626 Add deletion functions 2023-06-16 01:30:08 -07:00
Drew Galbraith 6e20e5a610 Add more accessors to mammoth classes 2023-06-16 01:29:49 -07:00
Drew Galbraith 71e51730b7 Mark unblocked threads as runnable before enqueueing them 2023-06-16 01:29:00 -07:00
19 changed files with 187 additions and 88 deletions

View File

@ -9,7 +9,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_EXPORT_COMPILE_COMMANDS True) set(CMAKE_EXPORT_COMPILE_COMMANDS True)
set(BASE_COMPILE_FLAGS "-ffreestanding -fno-exceptions -mgeneral-regs-only") set(BASE_COMPILE_FLAGS "-fno-rtti -fno-exceptions -mgeneral-regs-only")
set(BASE_LINK_FLAGS "-nostdlib") set(BASE_LINK_FLAGS "-nostdlib")
add_subdirectory(zion) add_subdirectory(zion)

View File

@ -4,3 +4,5 @@
[[nodiscard]] void* operator new(std::size_t size) { return malloc(size); } [[nodiscard]] void* operator new(std::size_t size) { return malloc(size); }
[[nodiscard]] void* operator new[](std::size_t size) { return malloc(size); } [[nodiscard]] void* operator new[](std::size_t size) { return malloc(size); }
void operator delete(void*, std::size_t) {}

View File

@ -8,6 +8,7 @@ class Channel {
Channel() {} Channel() {}
void adopt_cap(uint64_t id); void adopt_cap(uint64_t id);
uint64_t release_cap(); uint64_t release_cap();
uint64_t cap();
z_err_t WriteStr(const char* msg); z_err_t WriteStr(const char* msg);
z_err_t ReadStr(char* buffer, uint64_t* size); z_err_t ReadStr(char* buffer, uint64_t* size);

View File

@ -15,6 +15,9 @@ class MappedMemoryRegion {
uint64_t paddr() { return paddr_; } uint64_t paddr() { return paddr_; }
uint64_t vaddr() { return vaddr_; } uint64_t vaddr() { return vaddr_; }
uint64_t size() { return size_; }
uint64_t cap() { return vmmo_cap_; }
operator bool() { return vmmo_cap_ != 0; } operator bool() { return vmmo_cap_ != 0; }

View File

@ -29,6 +29,8 @@ uint64_t Channel::release_cap() {
return cap; return cap;
} }
uint64_t Channel::cap() { return chan_cap_; }
z_err_t Channel::WriteStr(const char* msg) { z_err_t Channel::WriteStr(const char* msg) {
if (!chan_cap_) { if (!chan_cap_) {
return Z_ERR_NULL; return Z_ERR_NULL;

View File

@ -69,7 +69,7 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
dbgln("Create mem object"); dbgln("Create mem object");
#endif #endif
uint64_t mem_cap; uint64_t mem_cap;
uint64_t size = program.filesz; uint64_t size = program.memsz;
check(ZMemoryObjectCreate(size, &mem_cap)); check(ZMemoryObjectCreate(size, &mem_cap));
#if MAM_PROC_DEBUG #if MAM_PROC_DEBUG

View File

@ -1,6 +1,7 @@
add_executable(denali add_executable(denali
ahci/ahci_device.cpp ahci/ahci_device.cpp
ahci/ahci_driver.cpp ahci/ahci_driver.cpp
ahci/command.cpp
denali.cpp denali.cpp
denali_server.cpp denali_server.cpp
) )

View File

@ -96,7 +96,7 @@ struct CommandTable {
uint8_t command_fis[64]; uint8_t command_fis[64];
uint8_t atapi_command[16]; uint8_t atapi_command[16];
uint8_t reserved[48]; uint8_t reserved[48];
PhysicalRegionDescriptor prds[65535]; PhysicalRegionDescriptor prdt[65535];
} __attribute__((packed)); } __attribute__((packed));
typedef enum { typedef enum {

View File

@ -4,12 +4,6 @@
#include <string.h> #include <string.h>
#include <zcall.h> #include <zcall.h>
namespace {
void HandleIdent(AhciDevice* dev) { dev->HandleIdentify(); }
} // namespace
AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) { AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
if ((port_struct_->sata_status & 0x103) != 0x103) { if ((port_struct_->sata_status & 0x103) != 0x103) {
return; return;
@ -38,59 +32,25 @@ AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
reinterpret_cast<CommandTable*>(command_structures_.vaddr() + ct_off); reinterpret_cast<CommandTable*>(command_structures_.vaddr() + ct_off);
port_struct_->interrupt_enable = 0xFFFFFFFF; port_struct_->interrupt_enable = 0xFFFFFFFF;
if (port_struct_->signature == 0x101) {
SendIdentify();
}
} }
z_err_t AhciDevice::SendIdentify() { z_err_t AhciDevice::IssueCommand(Command* command) {
HostToDeviceRegisterFis fis{ command->PopulateFis(command_table_->command_fis);
.fis_type = FIS_TYPE_REG_H2D, command->PopulatePrdt(command_table_->prdt);
.pmp_and_c = 0x80,
.command = 0xEC,
.featurel = 0,
.lba0 = 0, command_list_->command_headers[0].command =
.lba1 = 0, (sizeof(HostToDeviceRegisterFis) / 2) & 0x1F;
.lba2 = 0,
.device = 0,
.lba3 = 0,
.lba4 = 0,
.lba5 = 0,
.featureh = 0,
.count = 0,
.icc = 0,
.control = 0,
.reserved = 0,
};
command_list_->command_headers[0].command = (sizeof(fis) / 2) & 0x1F;
command_list_->command_headers[0].prd_table_length = 1; command_list_->command_headers[0].prd_table_length = 1;
command_list_->command_headers[0].prd_byte_count = 0;
memcpy(command_table_->command_fis, &fis, sizeof(fis)); commands_[0] = command;
commands_[0].region = MappedMemoryRegion::ContiguousPhysical(512);
// commands_[0].callback = HandleIdent;
command_table_->prds[0].region_address = commands_[0].region.paddr();
command_table_->prds[0].byte_count = 512;
port_struct_->command_issue |= 1;
commands_issued_ |= 1; commands_issued_ |= 1;
port_struct_->command_issue |= 1;
return Z_OK; return Z_OK;
} }
void AhciDevice::HandleIdentify() {
dbgln("Handling Idenify");
uint16_t* ident = reinterpret_cast<uint16_t*>(commands_[0].region.vaddr());
dbgln("Ident: %x", ident[0]);
}
void AhciDevice::DumpInfo() { void AhciDevice::DumpInfo() {
dbgln("Comlist: %lx", port_struct_->command_list_base); dbgln("Comlist: %lx", port_struct_->command_list_base);
dbgln("FIS: %lx", port_struct_->fis_base); dbgln("FIS: %lx", port_struct_->fis_base);
@ -116,11 +76,15 @@ void AhciDevice::HandleIrq() {
port_struct_->interrupt_status = int_status; port_struct_->interrupt_status = int_status;
uint32_t commands_finished = commands_issued_ & ~port_struct_->command_issue; uint32_t commands_finished = commands_issued_ & ~port_struct_->command_issue;
dbgln("finished %x", commands_finished);
dbgln("status %x", int_status);
dbgln("Issued %x, %x", commands_issued_, port_struct_->command_issue);
// FIXME: Pass error codes to the callback.
for (uint64_t i = 0; i < 32; i++) { for (uint64_t i = 0; i < 32; i++) {
if (commands_finished & (1 << i)) { if (commands_finished & (1 << i)) {
// commands_[i].callback(this);
commands_issued_ &= ~(1 << i); commands_issued_ &= ~(1 << i);
commands_[i]->Callback();
} }
} }
@ -134,6 +98,7 @@ void AhciDevice::HandleIrq() {
} }
if (fis.error) { if (fis.error) {
dbgln("D2H err: %x", fis.error); dbgln("D2H err: %x", fis.error);
dbgln("status: %x", fis.status);
} }
} }
if (int_status & 0x2) { if (int_status & 0x2) {

View File

@ -4,6 +4,7 @@
#include <zerrors.h> #include <zerrors.h>
#include "ahci/ahci.h" #include "ahci/ahci.h"
#include "ahci/command.h"
class AhciDevice { class AhciDevice {
public: public:
@ -15,11 +16,13 @@ class AhciDevice {
bool IsInit() { return port_struct_ != nullptr && command_structures_; } bool IsInit() { return port_struct_ != nullptr && command_structures_; }
z_err_t SendIdentify(); z_err_t IssueCommand(Command* command);
void HandleIdentify();
void HandleIrq(); void HandleIrq();
AhciDevice(const AhciDevice&) = delete;
AhciDevice& operator=(const AhciDevice&) = delete;
private: private:
AhciPort* port_struct_ = nullptr; AhciPort* port_struct_ = nullptr;
MappedMemoryRegion command_structures_; MappedMemoryRegion command_structures_;
@ -28,10 +31,6 @@ class AhciDevice {
ReceivedFis* received_fis_ = nullptr; ReceivedFis* received_fis_ = nullptr;
CommandTable* command_table_ = nullptr; CommandTable* command_table_ = nullptr;
struct Command { Command* commands_[32];
MappedMemoryRegion region; volatile uint32_t commands_issued_ = 0;
// std::function<void(MappedMemoryRegion)> callback;
};
Command commands_[32];
uint32_t commands_issued_ = 0;
}; };

View File

@ -33,16 +33,16 @@ z_err_t AhciDriver::Init() {
return Z_OK; return Z_OK;
} }
z_err_t AhciDriver::GetDevice(uint64_t id, AhciDevice& device) { z_err_t AhciDriver::GetDevice(uint64_t id, AhciDevice** device) {
if (id >= 32) { if (id >= 32) {
return Z_ERR_INVALID; return Z_ERR_INVALID;
} }
if (!devices_[id].IsInit()) { if (devices_[id] != nullptr && !devices_[id]->IsInit()) {
return Z_ERR_NOT_FOUND; return Z_ERR_NOT_FOUND;
} }
device = devices_[id]; *device = devices_[id];
return Z_OK; return Z_OK;
} }
@ -126,24 +126,27 @@ void AhciDriver::DumpCapabilities() {
void AhciDriver::DumpPorts() { void AhciDriver::DumpPorts() {
for (uint64_t i = 0; i < 6; i++) { for (uint64_t i = 0; i < 6; i++) {
AhciDevice& dev = devices_[i]; AhciDevice* dev = devices_[i];
if (!dev.IsInit()) { if (dev == nullptr || !dev->IsInit()) {
continue; continue;
} }
dbgln(""); dbgln("");
dbgln("Port %u:", i); dbgln("Port %u:", i);
dev.DumpInfo(); dev->DumpInfo();
} }
} }
void AhciDriver::InterruptLoop() { void AhciDriver::InterruptLoop() {
dbgln("Starting interrupt loop");
while (true) { while (true) {
uint64_t type, bytes, caps; uint64_t type, bytes, caps;
check(ZPortRecv(irq_port_cap_, 0, 0, 0, 0, &type, &bytes, &caps)); check(ZPortRecv(irq_port_cap_, 0, 0, 0, 0, &type, &bytes, &caps));
for (uint64_t i = 0; i < 32; i++) { for (uint64_t i = 0; i < 32; i++) {
if (devices_[i].IsInit() && (ahci_hba_->interrupt_status & (1 << i))) { if (devices_[i] != nullptr && devices_[i]->IsInit() &&
devices_[i].HandleIrq(); (ahci_hba_->interrupt_status & (1 << i))) {
dbgln("Interrupt for %u", i);
devices_[i]->HandleIrq();
ahci_hba_->interrupt_status &= ~(1 << i); ahci_hba_->interrupt_status &= ~(1 << i);
} }
} }
@ -212,7 +215,7 @@ z_err_t AhciDriver::LoadDevices() {
} }
uint64_t port_addr = uint64_t port_addr =
reinterpret_cast<uint64_t>(ahci_hba_) + 0x100 + (0x80 * i); reinterpret_cast<uint64_t>(ahci_hba_) + 0x100 + (0x80 * i);
devices_[i] = AhciDevice(reinterpret_cast<AhciPort*>(port_addr)); devices_[i] = new AhciDevice(reinterpret_cast<AhciPort*>(port_addr));
} }
return Z_OK; return Z_OK;
} }

View File

@ -12,7 +12,7 @@ class AhciDriver {
void InterruptLoop(); void InterruptLoop();
z_err_t GetDevice(uint64_t id, AhciDevice& device); z_err_t GetDevice(uint64_t id, AhciDevice** device);
void DumpCapabilities(); void DumpCapabilities();
void DumpPorts(); void DumpPorts();
@ -24,7 +24,7 @@ class AhciDriver {
AhciHba* ahci_hba_ = nullptr; AhciHba* ahci_hba_ = nullptr;
// TODO: Allocate these dynamically. // TODO: Allocate these dynamically.
AhciDevice devices_[32]; AhciDevice* devices_[32];
Thread irq_thread_; Thread irq_thread_;
uint64_t irq_port_cap_ = 0; uint64_t irq_port_cap_ = 0;

View File

@ -0,0 +1,49 @@
#include "ahci/command.h"
#include <string.h>
#include "ahci/ahci.h"
Command::~Command() {}
DmaReadCommand::DmaReadCommand(uint64_t lba, uint64_t sector_cnt,
DmaCallback callback)
: lba_(lba), sector_cnt_(sector_cnt), callback_(callback) {
region_ = MappedMemoryRegion::ContiguousPhysical(sector_cnt * 512);
}
DmaReadCommand::~DmaReadCommand() {}
void DmaReadCommand::PopulateFis(uint8_t* command_fis) {
HostToDeviceRegisterFis fis{
.fis_type = FIS_TYPE_REG_H2D,
.pmp_and_c = 0x80,
.command = 0x25,
.featurel = 0,
.lba0 = static_cast<uint8_t>(lba_ & 0xFF),
.lba1 = static_cast<uint8_t>((lba_ >> 8) & 0xFF),
.lba2 = static_cast<uint8_t>((lba_ >> 16) & 0xFF),
.device = (1 << 6), // ATA LBA Mode
.lba3 = static_cast<uint8_t>((lba_ >> 24) & 0xFF),
.lba4 = static_cast<uint8_t>((lba_ >> 32) & 0xFF),
.lba5 = static_cast<uint8_t>((lba_ >> 40) & 0xFF),
.featureh = 0,
.count = static_cast<uint16_t>(sector_cnt_),
.icc = 0,
.control = 0,
.reserved = 0,
};
uint64_t bytes = sector_cnt_ * 512;
memcpy(command_fis, &fis, sizeof(fis));
}
void DmaReadCommand::PopulatePrdt(PhysicalRegionDescriptor* prdt) {
prdt[0].region_address = region_.paddr();
prdt[0].byte_count = region_.size();
}
void DmaReadCommand::Callback() { callback_(lba_, sector_cnt_, region_.cap()); }

33
sys/denali/ahci/command.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <mammoth/memory_region.h>
#include <stdint.h>
#include "ahci/ahci.h"
class Command {
public:
virtual ~Command();
virtual void PopulateFis(uint8_t* command_fis) = 0;
virtual void PopulatePrdt(PhysicalRegionDescriptor* prdt) = 0;
virtual void Callback() = 0;
};
class DmaReadCommand : public Command {
public:
typedef void (*DmaCallback)(uint64_t, uint64_t, uint64_t);
DmaReadCommand(uint64_t lba, uint64_t sector_cnt, DmaCallback callback);
virtual ~DmaReadCommand() override;
void PopulateFis(uint8_t* command_fis) override;
void PopulatePrdt(PhysicalRegionDescriptor* prdt) override;
void Callback() override;
private:
uint64_t lba_;
uint64_t sector_cnt_;
DmaCallback callback_;
MappedMemoryRegion region_;
};

View File

@ -3,8 +3,17 @@
#include <mammoth/debug.h> #include <mammoth/debug.h>
#include <zcall.h> #include <zcall.h>
namespace {
DenaliServer* gServer = nullptr;
void HandleResponse(uint64_t lba, uint64_t size, uint64_t cap) {
gServer->HandleResponse(lba, size, cap);
}
} // namespace
DenaliServer::DenaliServer(uint64_t channel_cap, AhciDriver& driver) DenaliServer::DenaliServer(uint64_t channel_cap, AhciDriver& driver)
: channel_cap_(channel_cap), driver_(driver) {} : channel_cap_(channel_cap), driver_(driver) {
gServer = this;
}
z_err_t DenaliServer::RunServer() { z_err_t DenaliServer::RunServer() {
while (true) { while (true) {
@ -20,12 +29,7 @@ z_err_t DenaliServer::RunServer() {
case DENALI_READ: { case DENALI_READ: {
DenaliRead* read_req = reinterpret_cast<DenaliRead*>(read_buffer_); DenaliRead* read_req = reinterpret_cast<DenaliRead*>(read_buffer_);
uint64_t memcap = 0; uint64_t memcap = 0;
DenaliReadResponse resp; RET_ERR(HandleRead(*read_req));
RET_ERR(HandleRead(*read_req, resp, memcap));
uint64_t caps_len = memcap ? 1 : 0;
RET_ERR(ZChannelSend(channel_cap_, 0, sizeof(DenaliReadResponse),
reinterpret_cast<uint8_t*>(&resp), caps_len,
&memcap));
break; break;
} }
default: default:
@ -35,10 +39,22 @@ z_err_t DenaliServer::RunServer() {
} }
} }
z_err_t DenaliServer::HandleRead(const DenaliRead& read, z_err_t DenaliServer::HandleRead(const DenaliRead& read) {
DenaliReadResponse& resp, uint64_t& memcap) { AhciDevice* device;
AhciDevice device; RET_ERR(driver_.GetDevice(read.device_id, &device));
RET_ERR(driver_.GetDevice(read.device_id, device));
device->IssueCommand(
new DmaReadCommand(read.lba, read.size, ::HandleResponse));
return Z_OK; return Z_OK;
} }
void DenaliServer::HandleResponse(uint64_t lba, uint64_t size, uint64_t cap) {
DenaliReadResponse resp{
.device_id = 0,
.lba = lba,
.size = size,
};
check(ZChannelSend(channel_cap_, DENALI_READ, sizeof(resp),
reinterpret_cast<uint8_t*>(&resp), 1, &cap));
}

View File

@ -9,6 +9,8 @@ class DenaliServer {
z_err_t RunServer(); z_err_t RunServer();
void HandleResponse(uint64_t lba, uint64_t size, uint64_t cap);
private: private:
static const uint64_t kBuffSize = 1024; static const uint64_t kBuffSize = 1024;
uint64_t channel_cap_; uint64_t channel_cap_;
@ -16,6 +18,5 @@ class DenaliServer {
AhciDriver& driver_; AhciDriver& driver_;
z_err_t HandleRead(const DenaliRead& read, DenaliReadResponse& resp, z_err_t HandleRead(const DenaliRead& read);
uint64_t& mem_cap);
}; };

View File

@ -6,14 +6,34 @@
#include "hw/pcie.h" #include "hw/pcie.h"
uint64_t main() { int main() {
dbgln("Yellowstone Initializing."); dbgln("Yellowstone Initializing.");
uint64_t vaddr; uint64_t vaddr;
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, Z_INIT_BOOT_VMMO, &vaddr)); check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, Z_INIT_BOOT_VMMO, &vaddr));
Channel local; Channel local;
check(SpawnProcessFromElfRegion(vaddr, local)); check(SpawnProcessFromElfRegion(vaddr, local));
local.WriteStr("Hello!");
DenaliRead read{
.device_id = 0,
.lba = 0,
.size = 1,
};
check(ZChannelSend(local.cap(), DENALI_READ, sizeof(DenaliRead),
reinterpret_cast<uint8_t*>(&read), 0, nullptr));
DenaliReadResponse resp;
uint64_t mem_cap, type, bytes, caps;
check(ZChannelRecv(local.cap(), sizeof(resp),
reinterpret_cast<uint8_t*>(&resp), 1, &mem_cap, &type,
&bytes, &caps));
dbgln("Resp: %u", type);
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, mem_cap, &vaddr));
uint32_t* mbr = reinterpret_cast<uint32_t*>(vaddr + 0x1FE);
dbgln("MBR: %x", *mbr);
DumpPciEDevices(); DumpPciEDevices();

View File

@ -82,7 +82,9 @@ z_err_t Channel::EnqueueMessage(const ZMessage& msg) {
pending_messages_.PushBack(message); pending_messages_.PushBack(message);
if (blocked_threads_.size() > 0) { if (blocked_threads_.size() > 0) {
gScheduler->Enqueue(blocked_threads_.PopFront()); auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
} }
return Z_OK; return Z_OK;
} }

View File

@ -27,7 +27,9 @@ z_err_t Port::Write(const ZMessage& msg) {
MutexHolder lock(mutex_); MutexHolder lock(mutex_);
pending_messages_.PushBack(message); pending_messages_.PushBack(message);
if (blocked_threads_.size() > 0) { if (blocked_threads_.size() > 0) {
gScheduler->Enqueue(blocked_threads_.PopFront()); auto thread = blocked_threads_.PopFront();
thread->SetState(Thread::RUNNABLE);
gScheduler->Enqueue(thread);
} }
return Z_OK; return Z_OK;
} }