Compare commits

..

No commits in common. "5a18d7d559b9dfbe93c09de9a29d38db89ae2383" and "ebe72af71679cf77f48b807eb80c8ad164357c2a" have entirely different histories.

21 changed files with 82 additions and 190 deletions

View File

@ -10,9 +10,7 @@ class ArrayView {
ArrayView() : data_(nullptr), size_(0) {} ArrayView() : data_(nullptr), size_(0) {}
ArrayView(const ArrayView&) = default; ArrayView(const ArrayView&) = default;
ArrayView& operator=(const ArrayView&) = default;
ArrayView(ArrayView&&) = default; ArrayView(ArrayView&&) = default;
ArrayView& operator=(ArrayView&&) = default;
ArrayView(T* data, uint64_t size) : data_(data), size_(size) {} ArrayView(T* data, uint64_t size) : data_(data), size_(size) {}

View File

@ -20,8 +20,6 @@ class IntrusiveList {
void PushFront(const RefPtr<T>& obj); void PushFront(const RefPtr<T>& obj);
void PushBack(const RefPtr<T>& obj); void PushBack(const RefPtr<T>& obj);
void Remove(const RefPtr<T>& obj);
RefPtr<T> PopFront(); RefPtr<T> PopFront();
RefPtr<T> PopBack(); RefPtr<T> PopBack();
@ -66,29 +64,6 @@ void IntrusiveList<T>::PushBack(const RefPtr<T>& obj) {
back_ = obj; back_ = obj;
} }
template <typename T>
void IntrusiveList<T>::Remove(const RefPtr<T>& obj) {
if (!obj) {
return;
}
if (front_ == obj) {
front_ = obj->next_;
}
if (back_ == obj) {
back_ = obj->prev_;
}
if (obj->prev_) {
obj->prev_->next_ = obj->next_;
}
if (obj->next_) {
obj->next_->prev_ = obj->prev_;
}
obj->prev_ = nullptr;
obj->next_ = nullptr;
size_--;
}
template <typename T> template <typename T>
RefPtr<T> IntrusiveList<T>::PopFront() { RefPtr<T> IntrusiveList<T>::PopFront() {
if (front_ == nullptr) { if (front_ == nullptr) {

View File

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

View File

@ -37,10 +37,6 @@ struct PciMsiCap {
uint16_t message_data; uint16_t message_data;
} __attribute__((packed)); } __attribute__((packed));
const uint32_t kGlobalHostControl_HW_Reset = 1;
const uint32_t kGlobalHostControl_AHCI_Enable = (1 << 31);
const uint32_t kGlobalHostControl_Interrupt_Enable = (1 << 1);
struct AhciHba { struct AhciHba {
uint32_t capabilities; uint32_t capabilities;
uint32_t global_host_control; uint32_t global_host_control;
@ -55,9 +51,6 @@ struct AhciHba {
uint32_t bohc; // 0x28, BIOS/OS handoff control and status uint32_t bohc; // 0x28, BIOS/OS handoff control and status
} __attribute__((packed)); } __attribute__((packed));
const uint32_t kCommand_FIS_Receive_Enable = (1 << 4);
const uint32_t kCommand_Start = 1;
struct AhciPort { struct AhciPort {
uint64_t command_list_base; uint64_t command_list_base;
uint64_t fis_base; uint64_t fis_base;

View File

@ -6,8 +6,7 @@
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) {
crash("Creating device on port without a device", return;
glcr::FAILED_PRECONDITION);
} }
// 0x0-0x400 -> Command List // 0x0-0x400 -> Command List
@ -23,45 +22,35 @@ AhciDevice::AhciDevice(AhciPort* port) : port_struct_(port) {
received_fis_ = received_fis_ =
reinterpret_cast<ReceivedFis*>(command_structures_.vaddr() + 0x400); reinterpret_cast<ReceivedFis*>(command_structures_.vaddr() + 0x400);
port_struct_->fis_base = paddr + 0x400; port_struct_->fis_base = paddr + 0x400;
port_struct_->command |= kCommand_FIS_Receive_Enable;
command_tables_ = glcr::ArrayView( command_tables_ =
reinterpret_cast<CommandTable*>(command_structures_.vaddr() + 0x500), 32); reinterpret_cast<CommandTable*>(command_structures_.vaddr() + 0x500);
for (uint64_t i = 0; i < 32; i++) { for (uint64_t i = 0; i < 32; i++) {
// This leaves space for 2 prdt entries.
command_list_->command_headers[i].command_table_base_addr = command_list_->command_headers[i].command_table_base_addr =
(paddr + 0x500) + (0x100 * i); (paddr + 0x500) + (0x100 * i);
commands_[i] = nullptr;
} }
port_struct_->interrupt_enable = 0xFFFFFFFF; port_struct_->interrupt_enable = 0xFFFFFFFF;
port_struct_->sata_error = -1; // Reset the CMD and FRE bits since we move these structures.
port_struct_->command |= kCommand_Start; // FIXME: I think we need to poll these bits to make sure they become
// 0 before setting them back to one.
port_struct_->command &= ~(0x00000011);
port_struct_->command |= 0x00000011;
} }
glcr::ErrorCode AhciDevice::IssueCommand(Command* command) { glcr::ErrorCode AhciDevice::IssueCommand(Command* command) {
uint64_t slot; command->PopulateFis(command_tables_->command_fis);
for (slot = 0; slot < 32; slot++) { command->PopulatePrdt(command_tables_->prdt);
if (commands_[slot] == nullptr) {
break;
}
}
if (slot == 32) {
dbgln("All slots full");
return glcr::INTERNAL;
}
command->PopulateFis(command_tables_[slot].command_fis);
command->PopulatePrdt(command_tables_[slot].prdt);
command_list_->command_headers[slot].command = command_list_->command_headers[0].command =
(sizeof(HostToDeviceRegisterFis) / 2) & 0x1F; (sizeof(HostToDeviceRegisterFis) / 2) & 0x1F;
command_list_->command_headers[slot].prd_table_length = 1; command_list_->command_headers[0].prd_table_length = 1;
command_list_->command_headers[slot].prd_byte_count = 0; command_list_->command_headers[0].prd_byte_count = 0;
commands_[slot] = command; commands_[0] = command;
commands_issued_ |= (1 << slot); commands_issued_ |= 1;
port_struct_->command_issue |= (1 << slot); port_struct_->command_issue |= 1;
return glcr::OK; return glcr::OK;
} }
@ -72,9 +61,17 @@ void AhciDevice::DumpInfo() {
dbgln("Command: {x}", port_struct_->command); dbgln("Command: {x}", port_struct_->command);
dbgln("Signature: {x}", port_struct_->signature); dbgln("Signature: {x}", port_struct_->signature);
dbgln("SATA status: {x}", port_struct_->sata_status); dbgln("SATA status: {x}", port_struct_->sata_status);
dbgln("SATA error: {x}", port_struct_->sata_error);
dbgln("Int status: {x}", port_struct_->interrupt_status); dbgln("Int status: {x}", port_struct_->interrupt_status);
dbgln("Int enable: {x}", port_struct_->interrupt_enable); dbgln("Int enable: {x}", port_struct_->interrupt_enable);
// Just dump one command info for now.
for (uint64_t i = 0; i < 32; i++) {
dbgln("Command Header: {}", i);
dbgln("Command {x}", command_list_->command_headers[i].command);
dbgln("PRD Len: {x}", command_list_->command_headers[i].prd_table_length);
dbgln("Command Table {x}",
command_list_->command_headers[i].command_table_base_addr);
}
} }
void AhciDevice::HandleIrq() { void AhciDevice::HandleIrq() {
@ -95,8 +92,7 @@ void AhciDevice::HandleIrq() {
// TODO: Do something with this information. // TODO: Do something with this information.
if (int_status & 0x1) { if (int_status & 0x1) {
// Device to host. // Device to host.
volatile DeviceToHostRegisterFis& fis = DeviceToHostRegisterFis& fis = received_fis_->device_to_host_register_fis;
received_fis_->device_to_host_register_fis;
if (fis.fis_type != FIS_TYPE_REG_D2H) { if (fis.fis_type != FIS_TYPE_REG_D2H) {
dbgln("BAD FIS TYPE (exp,act): {x}, {x}", dbgln("BAD FIS TYPE (exp,act): {x}, {x}",
static_cast<uint64_t>(FIS_TYPE_REG_D2H), static_cast<uint64_t>(FIS_TYPE_REG_D2H),
@ -110,7 +106,7 @@ void AhciDevice::HandleIrq() {
} }
if (int_status & 0x2) { if (int_status & 0x2) {
// PIO. // PIO.
volatile PioSetupFis& fis = received_fis_->pio_set_fis; PioSetupFis& fis = received_fis_->pio_set_fis;
if (fis.fis_type != FIS_TYPE_PIO_SETUP) { if (fis.fis_type != FIS_TYPE_PIO_SETUP) {
dbgln("BAD FIS TYPE (exp,act): {x}, {x}", dbgln("BAD FIS TYPE (exp,act): {x}, {x}",
static_cast<uint64_t>(FIS_TYPE_PIO_SETUP), static_cast<uint64_t>(FIS_TYPE_PIO_SETUP),

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <glacier/container/array_view.h>
#include <glacier/status/error.h> #include <glacier/status/error.h>
#include <mammoth/util/memory_region.h> #include <mammoth/util/memory_region.h>
#include <ztypes.h> #include <ztypes.h>
@ -26,13 +25,13 @@ class AhciDevice {
AhciDevice& operator=(const AhciDevice&) = delete; AhciDevice& operator=(const AhciDevice&) = delete;
private: private:
volatile AhciPort* port_struct_ = nullptr; AhciPort* port_struct_ = nullptr;
mmth::OwnedMemoryRegion command_structures_; mmth::OwnedMemoryRegion command_structures_;
volatile CommandList* command_list_ = nullptr; CommandList* command_list_ = nullptr;
volatile ReceivedFis* received_fis_ = nullptr; ReceivedFis* received_fis_ = nullptr;
glcr::ArrayView<CommandTable> command_tables_; CommandTable* command_tables_ = nullptr;
Command* commands_[32]; Command* commands_[32];
uint32_t commands_issued_ = 0; volatile uint32_t commands_issued_ = 0;
}; };

View File

@ -1,4 +1,4 @@
#include "ahci/ahci_controller.h" #include "ahci/ahci_driver.h"
#include <glacier/status/error.h> #include <glacier/status/error.h>
#include <glacier/status/error_or.h> #include <glacier/status/error_or.h>
@ -11,7 +11,7 @@ namespace {
const uint64_t kGhc_InteruptEnable = 0x2; const uint64_t kGhc_InteruptEnable = 0x2;
void interrupt_thread(void* void_driver) { void interrupt_thread(void* void_driver) {
AhciController* driver = static_cast<AhciController*>(void_driver); AhciDriver* driver = static_cast<AhciDriver*>(void_driver);
driver->InterruptLoop(); driver->InterruptLoop();
@ -20,32 +20,31 @@ void interrupt_thread(void* void_driver) {
} // namespace } // namespace
glcr::ErrorOr<glcr::UniquePtr<AhciController>> AhciController::Init( glcr::ErrorOr<glcr::UniquePtr<AhciDriver>> AhciDriver::Init(
mmth::OwnedMemoryRegion&& pci_region) { mmth::OwnedMemoryRegion&& pci_region) {
glcr::UniquePtr<AhciController> driver( glcr::UniquePtr<AhciDriver> driver(new AhciDriver(glcr::Move(pci_region)));
new AhciController(glcr::Move(pci_region))); // RET_ERR(driver->LoadCapabilities());
RET_ERR(driver->LoadHbaRegisters()); RET_ERR(driver->LoadHbaRegisters());
driver->DumpCapabilities();
RET_ERR(driver->ResetHba());
RET_ERR(driver->RegisterIrq());
RET_ERR(driver->LoadDevices()); RET_ERR(driver->LoadDevices());
RET_ERR(driver->RegisterIrq());
// driver->DumpCapabilities();
// driver->DumpPorts(); // driver->DumpPorts();
return driver; return driver;
} }
glcr::ErrorOr<AhciDevice*> AhciController::GetDevice(uint64_t id) { glcr::ErrorOr<AhciDevice*> AhciDriver::GetDevice(uint64_t id) {
if (id >= 32) { if (id >= 32) {
return glcr::INVALID_ARGUMENT; return glcr::INVALID_ARGUMENT;
} }
if (devices_[id].empty()) { if (devices_[id] != nullptr && !devices_[id]->IsInit()) {
return glcr::NOT_FOUND; return glcr::NOT_FOUND;
} }
return devices_[id].get(); return devices_[id];
} }
void AhciController::DumpCapabilities() { void AhciDriver::DumpCapabilities() {
dbgln("AHCI Capabilities:"); dbgln("AHCI Capabilities:");
uint32_t caps = ahci_hba_->capabilities; uint32_t caps = ahci_hba_->capabilities;
@ -123,25 +122,26 @@ void AhciController::DumpCapabilities() {
dbgln("Control {x}", ahci_hba_->global_host_control); dbgln("Control {x}", ahci_hba_->global_host_control);
} }
void AhciController::DumpPorts() { void AhciDriver::DumpPorts() {
for (uint64_t i = 0; i < 6; i++) { for (uint64_t i = 0; i < 6; i++) {
if (devices_[i].empty()) { AhciDevice* dev = devices_[i];
if (dev == nullptr || !dev->IsInit()) {
continue; continue;
} }
dbgln(""); dbgln("");
dbgln("Port {}:", i); dbgln("Port {}:", i);
devices_[i]->DumpInfo(); dev->DumpInfo();
} }
} }
void AhciController::InterruptLoop() { void AhciDriver::InterruptLoop() {
dbgln("Starting interrupt loop"); dbgln("Starting interrupt loop");
while (true) { while (true) {
uint64_t bytes, caps; uint64_t bytes, caps;
check(ZPortRecv(irq_port_cap_, &bytes, nullptr, &caps, nullptr)); check(ZPortRecv(irq_port_cap_, &bytes, nullptr, &caps, nullptr));
for (uint64_t i = 0; i < 32; i++) { for (uint64_t i = 0; i < 32; i++) {
if (!devices_[i].empty() && devices_[i]->IsInit() && if (devices_[i] != nullptr && devices_[i]->IsInit() &&
(ahci_hba_->interrupt_status & (1 << i))) { (ahci_hba_->interrupt_status & (1 << i))) {
devices_[i]->HandleIrq(); devices_[i]->HandleIrq();
ahci_hba_->interrupt_status &= ~(1 << i); ahci_hba_->interrupt_status &= ~(1 << i);
@ -150,17 +150,15 @@ void AhciController::InterruptLoop() {
} }
} }
glcr::ErrorCode AhciController::LoadCapabilities() { glcr::ErrorCode AhciDriver::LoadCapabilities() {
if (!(pci_device_header_->status_reg & 0x10)) { if (!(pci_device_header_->status_reg & 0x10)) {
dbgln("No caps!"); dbgln("No caps!");
return glcr::FAILED_PRECONDITION; return glcr::FAILED_PRECONDITION;
} }
volatile uint8_t* base = uint8_t* base = reinterpret_cast<uint8_t*>(pci_device_header_);
reinterpret_cast<volatile uint8_t*>(pci_device_header_);
uint16_t offset = pci_device_header_->cap_ptr; uint16_t offset = pci_device_header_->cap_ptr;
do { do {
volatile uint16_t* cap = uint16_t* cap = reinterpret_cast<uint16_t*>(base + offset);
reinterpret_cast<volatile uint16_t*>(base + offset);
switch (*cap & 0xFF) { switch (*cap & 0xFF) {
case 0x01: case 0x01:
dbgln("Power Management"); dbgln("Power Management");
@ -181,7 +179,7 @@ glcr::ErrorCode AhciController::LoadCapabilities() {
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode AhciController::RegisterIrq() { glcr::ErrorCode AhciDriver::RegisterIrq() {
if (pci_device_header_->interrupt_pin == 0) { if (pci_device_header_->interrupt_pin == 0) {
crash("Can't register IRQ without a pin num", glcr::INVALID_ARGUMENT); crash("Can't register IRQ without a pin num", glcr::INVALID_ARGUMENT);
} }
@ -207,7 +205,7 @@ glcr::ErrorCode AhciController::RegisterIrq() {
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode AhciController::LoadHbaRegisters() { glcr::ErrorCode AhciDriver::LoadHbaRegisters() {
ahci_region_ = mmth::OwnedMemoryRegion ::DirectPhysical( ahci_region_ = mmth::OwnedMemoryRegion ::DirectPhysical(
pci_device_header_->abar, 0x1100); pci_device_header_->abar, 0x1100);
ahci_hba_ = reinterpret_cast<AhciHba*>(ahci_region_.vaddr()); ahci_hba_ = reinterpret_cast<AhciHba*>(ahci_region_.vaddr());
@ -217,34 +215,15 @@ glcr::ErrorCode AhciController::LoadHbaRegisters() {
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode AhciController::ResetHba() { glcr::ErrorCode AhciDriver::LoadDevices() {
ahci_hba_->global_host_control |= kGlobalHostControl_HW_Reset; for (uint8_t i = 0; i < 32; i++) {
// TODO: Consider sleeping here.
while (ahci_hba_->global_host_control & kGlobalHostControl_HW_Reset) {
continue;
}
ahci_hba_->global_host_control |= kGlobalHostControl_AHCI_Enable;
return static_cast<glcr::ErrorCode>(ZThreadSleep(50));
}
glcr::ErrorCode AhciController::LoadDevices() {
for (uint8_t i = 0; i <= num_ports_; i++) {
if (!(ahci_hba_->port_implemented & (1 << i))) { if (!(ahci_hba_->port_implemented & (1 << i))) {
devices_[i] = nullptr;
continue; continue;
} }
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);
AhciPort* port = reinterpret_cast<AhciPort*>(port_addr);
if ((port->sata_status & 0x103) != 0x103) {
continue;
}
devices_[i] = new AhciDevice(reinterpret_cast<AhciPort*>(port_addr)); devices_[i] = new AhciDevice(reinterpret_cast<AhciPort*>(port_addr));
devices_[i]->DumpInfo();
} }
return glcr::OK; return glcr::OK;
} }

View File

@ -8,9 +8,9 @@
#include "ahci/ahci.h" #include "ahci/ahci.h"
#include "ahci/ahci_device.h" #include "ahci/ahci_device.h"
class AhciController { class AhciDriver {
public: public:
static glcr::ErrorOr<glcr::UniquePtr<AhciController>> Init( static glcr::ErrorOr<glcr::UniquePtr<AhciDriver>> Init(
mmth::OwnedMemoryRegion&& ahci_phys); mmth::OwnedMemoryRegion&& ahci_phys);
glcr::ErrorCode RegisterIrq(); glcr::ErrorCode RegisterIrq();
@ -23,24 +23,24 @@ class AhciController {
private: private:
mmth::OwnedMemoryRegion pci_region_; mmth::OwnedMemoryRegion pci_region_;
volatile PciDeviceHeader* pci_device_header_ = nullptr; PciDeviceHeader* pci_device_header_ = nullptr;
mmth::OwnedMemoryRegion ahci_region_; mmth::OwnedMemoryRegion ahci_region_;
volatile AhciHba* ahci_hba_ = nullptr; AhciHba* ahci_hba_ = nullptr;
glcr::UniquePtr<AhciDevice> devices_[32]; // TODO: Allocate these dynamically.
AhciDevice* devices_[32];
Thread irq_thread_; Thread irq_thread_;
uint64_t irq_port_cap_ = 0; uint64_t irq_port_cap_ = 0;
uint8_t num_ports_; uint64_t num_ports_;
uint8_t num_commands_; uint64_t num_commands_;
glcr::ErrorCode LoadCapabilities(); glcr::ErrorCode LoadCapabilities();
glcr::ErrorCode LoadHbaRegisters(); glcr::ErrorCode LoadHbaRegisters();
glcr::ErrorCode ResetHba();
glcr::ErrorCode LoadDevices(); glcr::ErrorCode LoadDevices();
AhciController(mmth::OwnedMemoryRegion&& pci_region) AhciDriver(mmth::OwnedMemoryRegion&& pci_region)
: pci_region_(glcr::Move(pci_region)), : pci_region_(glcr::Move(pci_region)),
pci_device_header_( pci_device_header_(
reinterpret_cast<PciDeviceHeader*>(pci_region_.vaddr())) {} reinterpret_cast<PciDeviceHeader*>(pci_region_.vaddr())) {}

View File

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <yellowstone/yellowstone.yunq.client.h> #include <yellowstone/yellowstone.yunq.client.h>
#include "ahci/ahci_controller.h" #include "ahci/ahci_driver.h"
#include "denali_server.h" #include "denali_server.h"
using yellowstone::AhciInfo; using yellowstone::AhciInfo;
@ -19,7 +19,7 @@ uint64_t main(uint64_t init_port_cap) {
check(stub.GetAhciInfo(ahci)); check(stub.GetAhciInfo(ahci));
mmth::OwnedMemoryRegion ahci_region = mmth::OwnedMemoryRegion ahci_region =
mmth::OwnedMemoryRegion::FromCapability(ahci.ahci_region()); mmth::OwnedMemoryRegion::FromCapability(ahci.ahci_region());
ASSIGN_OR_RETURN(auto driver, AhciController::Init(glcr::Move(ahci_region))); ASSIGN_OR_RETURN(auto driver, AhciDriver::Init(glcr::Move(ahci_region)));
ASSIGN_OR_RETURN(glcr::UniquePtr<DenaliServer> server, ASSIGN_OR_RETURN(glcr::UniquePtr<DenaliServer> server,
DenaliServer::Create(*driver)); DenaliServer::Create(*driver));

View File

@ -6,7 +6,7 @@
#include <zcall.h> #include <zcall.h>
glcr::ErrorOr<glcr::UniquePtr<DenaliServer>> DenaliServer::Create( glcr::ErrorOr<glcr::UniquePtr<DenaliServer>> DenaliServer::Create(
AhciController& driver) { AhciDriver& driver) {
z_cap_t cap; z_cap_t cap;
RET_ERR(ZEndpointCreate(&cap)); RET_ERR(ZEndpointCreate(&cap));
return glcr::UniquePtr<DenaliServer>(new DenaliServer(cap, driver)); return glcr::UniquePtr<DenaliServer>(new DenaliServer(cap, driver));

View File

@ -2,13 +2,13 @@
#include <glacier/status/error.h> #include <glacier/status/error.h>
#include "ahci/ahci_controller.h" #include "ahci/ahci_driver.h"
#include "lib/denali/denali.yunq.server.h" #include "lib/denali/denali.yunq.server.h"
class DenaliServer : public DenaliServerBase { class DenaliServer : public DenaliServerBase {
public: public:
static glcr::ErrorOr<glcr::UniquePtr<DenaliServer>> Create( static glcr::ErrorOr<glcr::UniquePtr<DenaliServer>> Create(
AhciController& driver); AhciDriver& driver);
glcr::Status HandleRead(const ReadRequest& req, ReadResponse& resp) override; glcr::Status HandleRead(const ReadRequest& req, ReadResponse& resp) override;
glcr::Status HandleReadMany(const ReadManyRequest& req, glcr::Status HandleReadMany(const ReadManyRequest& req,
@ -18,8 +18,8 @@ class DenaliServer : public DenaliServerBase {
static const uint64_t kBuffSize = 1024; static const uint64_t kBuffSize = 1024;
uint8_t read_buffer_[kBuffSize]; uint8_t read_buffer_[kBuffSize];
AhciController& driver_; AhciDriver& driver_;
DenaliServer(z_cap_t endpoint_cap, AhciController& driver) DenaliServer(z_cap_t endpoint_cap, AhciDriver& driver)
: DenaliServerBase(endpoint_cap), driver_(driver) {} : DenaliServerBase(endpoint_cap), driver_(driver) {}
}; };

View File

@ -68,14 +68,9 @@ void Terminal::ExecuteCommand(const glcr::String& command) {
} }
auto file = mmth::File::Open(tokens[1]); auto file = mmth::File::Open(tokens[1]);
z_cap_t endpoint; // TODO: Wait until the process exits.
if (ZCapDuplicate(gInitEndpointCap, kZionPerm_All, &endpoint) != glcr::OK) { auto error_or_cap = mmth::SpawnProcessFromElfRegion(
console_.WriteString("Couldn't duplicate yellowstone cap for spawn"); (uint64_t)file.raw_ptr(), gInitEndpointCap);
return;
}
auto error_or_cap =
mmth::SpawnProcessFromElfRegion((uint64_t)file.raw_ptr(), endpoint);
if (!error_or_cap.ok()) { if (!error_or_cap.ok()) {
console_.WriteString( console_.WriteString(
glcr::StrFormat("Error: {}\n", error_or_cap.error())); glcr::StrFormat("Error: {}\n", error_or_cap.error()));

View File

@ -1,10 +1,7 @@
#include <mammoth/util/debug.h> #include <mammoth/util/debug.h>
#include <zcall.h>
uint64_t main(uint64_t init_port_cap) { uint64_t main(uint64_t init_port_cap) {
dbgln("testbed"); dbgln("testbed");
check(ZThreadSleep(2000));
dbgln("testbed2");
return glcr::OK; return glcr::OK;
} }

View File

@ -17,7 +17,6 @@ SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1,
uint64_t, arg2); uint64_t, arg2);
SYS0(ThreadExit); SYS0(ThreadExit);
SYS1(ThreadWait, z_cap_t, thread_cap); SYS1(ThreadWait, z_cap_t, thread_cap);
SYS1(ThreadSleep, uint64_t, millis);
SYS5(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t, SYS5(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t,
vmmo_cap, uint64_t, align, uint64_t*, vaddr); vmmo_cap, uint64_t, align, uint64_t*, vaddr);

View File

@ -19,7 +19,6 @@ const uint64_t kZionThreadCreate = 0x10;
const uint64_t kZionThreadStart = 0x11; const uint64_t kZionThreadStart = 0x11;
const uint64_t kZionThreadExit = 0x12; const uint64_t kZionThreadExit = 0x12;
const uint64_t kZionThreadWait = 0x13; const uint64_t kZionThreadWait = 0x13;
const uint64_t kZionThreadSleep = 0x14;
// Memory Calls // Memory Calls
const uint64_t kZionAddressSpaceMap = 0x21; const uint64_t kZionAddressSpaceMap = 0x21;

View File

@ -29,7 +29,6 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
RUNNING, RUNNING,
RUNNABLE, RUNNABLE,
BLOCKED, BLOCKED,
SLEEPING,
CLEANUP, CLEANUP,
FINISHED, FINISHED,
}; };
@ -70,9 +69,6 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
void Wait(); void Wait();
void SetSleepTicks(uint64_t sleep_ticks) { sleep_ticks_ = sleep_ticks; }
bool DecrementSleepTicks() { return --sleep_ticks_ == 0; }
private: private:
friend class glcr::MakeRefCountedFriend<Thread>; friend class glcr::MakeRefCountedFriend<Thread>;
Thread(Process& proc, uint64_t tid); Thread(Process& proc, uint64_t tid);
@ -83,7 +79,6 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
State state_ = CREATED; State state_ = CREATED;
bool is_kernel_ = false; bool is_kernel_ = false;
uint64_t user_stack_base_; uint64_t user_stack_base_;
uint64_t sleep_ticks_;
// Startup Context for the thread. // Startup Context for the thread.
uint64_t rip_; uint64_t rip_;

View File

@ -45,11 +45,14 @@ void Scheduler::Preempt() {
return; return;
} }
DecrementSleepingThreads();
ClearDeadThreadsFromFront(); ClearDeadThreadsFromFront();
asm volatile("cli"); asm volatile("cli");
if (current_thread_ == sleep_thread_) {
// Sleep should never be preempted. (We should yield it if another thread
// becomes scheduleable).
return;
}
if (runnable_threads_.size() == 0) { if (runnable_threads_.size() == 0) {
// Continue. // Continue.
@ -99,33 +102,9 @@ void Scheduler::Yield() {
SwapToCurrent(*prev); SwapToCurrent(*prev);
} }
void Scheduler::Sleep(uint64_t millis) {
// FIXME: Improve resolution of sleep calls.
uint64_t ticks = (millis / 50) + 1;
current_thread_->SetSleepTicks(ticks);
current_thread_->SetState(Thread::SLEEPING);
sleeping_threads_.PushBack(current_thread_);
Yield();
}
void Scheduler::ClearDeadThreadsFromFront() { void Scheduler::ClearDeadThreadsFromFront() {
while (runnable_threads_.size() > 0 && while (runnable_threads_.size() > 0 &&
runnable_threads_.PeekFront()->IsDying()) { runnable_threads_.PeekFront()->IsDying()) {
runnable_threads_.PopFront(); runnable_threads_.PopFront();
} }
} }
void Scheduler::DecrementSleepingThreads() {
auto thread = sleeping_threads_.PeekFront();
while (thread) {
if (thread->DecrementSleepTicks()) {
auto thread_next = thread->next_;
sleeping_threads_.Remove(thread);
thread->SetState(Thread::RUNNABLE);
runnable_threads_.PushBack(thread);
thread = thread_next;
} else {
thread = thread->next_;
}
}
}

View File

@ -23,23 +23,18 @@ class Scheduler {
void Preempt(); void Preempt();
void Yield(); void Yield();
void Sleep(uint64_t millis);
private: private:
bool enabled_ = false; bool enabled_ = false;
glcr::RefPtr<Thread> current_thread_; glcr::RefPtr<Thread> current_thread_;
glcr::IntrusiveList<Thread> runnable_threads_; glcr::IntrusiveList<Thread> runnable_threads_;
glcr::IntrusiveList<Thread> sleeping_threads_;
glcr::RefPtr<Thread> sleep_thread_; glcr::RefPtr<Thread> sleep_thread_;
Scheduler(); Scheduler();
void SwapToCurrent(Thread& prev); void SwapToCurrent(Thread& prev);
void ClearDeadThreadsFromFront(); void ClearDeadThreadsFromFront();
void DecrementSleepingThreads();
}; };
extern Scheduler* gScheduler; extern Scheduler* gScheduler;

View File

@ -59,7 +59,6 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
CASE(ThreadStart); CASE(ThreadStart);
CASE(ThreadExit); CASE(ThreadExit);
CASE(ThreadWait); CASE(ThreadWait);
CASE(ThreadSleep);
// syscall/address_space.h // syscall/address_space.h
CASE(AddressSpaceMap); CASE(AddressSpaceMap);
CASE(AddressSpaceUnmap); CASE(AddressSpaceUnmap);

View File

@ -53,8 +53,3 @@ glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) {
thread->Wait(); thread->Wait();
return glcr::OK; return glcr::OK;
} }
glcr::ErrorCode ThreadSleep(ZThreadSleepReq* req) {
gScheduler->Sleep(req->millis);
return glcr::OK;
}

View File

@ -8,4 +8,3 @@ glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req);
glcr::ErrorCode ThreadStart(ZThreadStartReq* req); glcr::ErrorCode ThreadStart(ZThreadStartReq* req);
glcr::ErrorCode ThreadExit(ZThreadExitReq*); glcr::ErrorCode ThreadExit(ZThreadExitReq*);
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req); glcr::ErrorCode ThreadWait(ZThreadWaitReq* req);
glcr::ErrorCode ThreadSleep(ZThreadSleepReq* req);