Compare commits

...

4 Commits

28 changed files with 468 additions and 24 deletions

View File

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

View File

@ -26,4 +26,4 @@ target_include_directories(glacier_kernel
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
set_target_properties(glacier_kernel PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS} -mcmodel=kernel")
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS} -mcmodel=kernel -mgeneral-regs-only")

View File

@ -6,6 +6,7 @@
#include "glacier/container/linked_list.h"
#include "glacier/container/pair.h"
#include "glacier/status/error.h"
#include "glacier/string/string.h"
namespace glcr {
@ -22,6 +23,18 @@ struct HashFunc<uint64_t> {
}
};
template <>
struct HashFunc<String> {
uint64_t operator()(const String& value) {
// FIXME: Write a real hash function.
uint64_t acc = 0;
for (uint64_t i = 0; i < value.length(); i++) {
acc += value[i];
}
return 0xABBAABBAABBAABBA ^ acc;
}
};
template <typename K, typename V, class H = HashFunc<K>>
class HashMap {
public:

View File

@ -28,6 +28,46 @@ String::String(const char* cstr, uint64_t str_len) : length_(str_len) {
String::String(StringView str) : String(str.data(), str.size()) {}
String::String(const String& other) : String(other.cstr_, other.length_) {}
String& String::operator=(const String& other) {
if (cstr_) {
delete cstr_;
}
length_ = other.length_;
cstr_ = new char[length_ + 1];
for (uint64_t i = 0; i < length_; i++) {
cstr_[i] = other.cstr_[i];
}
cstr_[length_] = '\0';
return *this;
}
String::String(String&& other) : cstr_(other.cstr_), length_(other.length_) {
other.cstr_ = nullptr;
other.length_ = 0;
}
String& String::operator=(String&& other) {
if (cstr_) {
delete cstr_;
}
cstr_ = other.cstr_;
length_ = other.length_;
other.cstr_ = nullptr;
other.length_ = 0;
return *this;
}
String::~String() {
if (cstr_) {
delete cstr_;
}
}
bool String::operator==(const String& other) {
if (other.length_ != length_) {
return false;

View File

@ -13,7 +13,12 @@ class String {
String(const char* cstr, uint64_t str_len);
String(StringView str);
String(const String&) = delete;
String(const String&);
String& operator=(const String&);
String(String&&);
String& operator=(String&&);
~String();
const char* cstr() const { return cstr_; }
uint64_t length() const { return length_; }

View File

@ -1,5 +1,6 @@
add_executable(teton
framebuffer/framebuffer.cpp
framebuffer/psf.cpp
teton.cpp
)
@ -10,6 +11,7 @@ target_include_directories(teton
target_link_libraries(teton
glacier
mammoth
victoriafalls_yunq
yellowstone_yunq
)

View File

@ -1,6 +1,7 @@
#include "framebuffer/framebuffer.h"
Framebuffer::Framebuffer(const FramebufferInfo& info) : fb_info_(info) {
Framebuffer::Framebuffer(const FramebufferInfo& info)
: fb_info_(info), cursor_pos_(0) {
uint64_t buff_size_bytes = fb_info_.height() * fb_info_.pitch();
fb_memory_ = OwnedMemoryRegion::DirectPhysical(fb_info_.address_phys(),
buff_size_bytes);
@ -11,3 +12,18 @@ void Framebuffer::DrawPixel(uint32_t row, uint32_t col, uint32_t pixel) {
// Div by 4 because pitch is in bytes and fb_ is a 32bit array.
fb_[(row * fb_info_.pitch() / 4) + col] = pixel;
}
void Framebuffer::DrawGlyph(uint8_t* glyph) {
uint32_t gl_width = 8;
uint32_t gl_height = 16;
for (uint8_t r = 0; r < gl_height; r++) {
for (uint8_t c = 0; c < gl_width; c++) {
if (((glyph[r] >> c) % 2) == 1) {
DrawPixel(r, gl_width - c - 1, 0xFFFF'FFFF);
} else {
DrawPixel(r, gl_width - c - 1, 0);
}
}
}
};

View File

@ -9,6 +9,8 @@ class Framebuffer {
void DrawPixel(uint32_t row, uint32_t col, uint32_t pixel);
void DrawGlyph(uint8_t* glyph);
private:
// FIXME: Implement Yunq copy or move so we
// don't have to store a reference here.
@ -16,4 +18,5 @@ class Framebuffer {
OwnedMemoryRegion fb_memory_;
uint32_t* fb_;
uint32_t cursor_pos_;
};

View File

@ -0,0 +1,46 @@
#include "framebuffer/psf.h"
#include <glacier/memory/move.h>
#include <mammoth/debug.h>
namespace {
const uint32_t kMagic = 0x864AB572;
}
Psf::Psf(OwnedMemoryRegion&& psf_file)
: psf_file_(glcr::Move(psf_file)),
header_(reinterpret_cast<PsfHeader*>(psf_file_.vaddr())) {
EnsureValid();
}
void Psf::DumpHeader() {
dbgln("Magic: {x}", header_->magic);
dbgln("Version: {x}", header_->version);
dbgln("Header Sz: {x}", header_->headersize);
dbgln("Flags: {x}", header_->flags);
dbgln("Length: {x}", header_->numglyph);
dbgln("Glyph Size: {x}", header_->bytesperglyph);
dbgln("Height: {x}", header_->height);
dbgln("Width: {x}", header_->width);
}
void Psf::EnsureValid() {
if (header_->magic != kMagic) {
dbgln("PSF: Magic value: {x}", header_->magic);
crash("PSF: Invalid magic value", glcr::INVALID_ARGUMENT);
}
if (header_->version != 0) {
crash("PSF non-zero version", glcr::INVALID_ARGUMENT);
}
if (header_->height != 0x10) {
crash("PSF height other than 16 not handled", glcr::UNIMPLEMENTED);
}
if (header_->width != 0x8) {
crash("PSF width other than 8 not handled", glcr::UNIMPLEMENTED);
}
}

View File

@ -0,0 +1,34 @@
#pragma once
#include <mammoth/memory_region.h>
struct PsfHeader {
uint32_t magic; /* magic bytes to identify PSF */
uint32_t version; /* zero */
uint32_t headersize; /* offset of bitmaps in file, 32 */
uint32_t flags; /* 0 if there's no unicode table */
uint32_t numglyph; /* number of glyphs */
uint32_t bytesperglyph; /* size of each glyph */
uint32_t height; /* height in pixels */
uint32_t width; /* width in pixels */
};
class Psf {
public:
Psf(OwnedMemoryRegion&& psf_file);
void DumpHeader();
uint32_t size() { return header_->numglyph; }
uint8_t* glyph(uint32_t index) {
return reinterpret_cast<uint8_t*>(psf_file_.vaddr() + header_->headersize +
(index * header_->bytesperglyph));
}
private:
OwnedMemoryRegion psf_file_;
PsfHeader* header_;
void EnsureValid();
};

View File

@ -1,8 +1,10 @@
#include <mammoth/debug.h>
#include <mammoth/init.h>
#include <victoriafalls/victoriafalls.yunq.client.h>
#include <yellowstone/yellowstone.yunq.client.h>
#include "framebuffer/framebuffer.h"
#include "framebuffer/psf.h"
uint64_t main(uint64_t init_port) {
ParseInitPort(init_port);
@ -28,6 +30,23 @@ uint64_t main(uint64_t init_port) {
// 2. Parse a font file.
GetEndpointRequest req;
req.set_endpoint_name("victoriafalls");
Endpoint resp;
check(client.GetEndpoint(req, resp));
VFSClient vfs(resp.endpoint());
OpenFileRequest freq;
freq.set_path("/default8x16.psfu");
OpenFileResponse fresp;
check(vfs.OpenFile(freq, fresp));
Psf psf(OwnedMemoryRegion::FromCapability(fresp.memory()));
psf.DumpHeader();
fbuf.DrawGlyph(psf.glyph('C'));
// 3. Write a line to the screen.
return 0;

View File

@ -1,5 +1,6 @@
interface Yellowstone {
method RegisterEndpoint(RegisterEndpointRequest) -> (Empty);
method GetEndpoint(GetEndpointRequest) -> (Endpoint);
method GetAhciInfo(Empty) -> (AhciInfo);
method GetFramebufferInfo(Empty) -> (FramebufferInfo);
method GetDenali(Empty) -> (DenaliInfo);
@ -14,6 +15,14 @@ message Empty {
}
message GetEndpointRequest {
string endpoint_name;
}
message Endpoint {
capability endpoint;
}
message AhciInfo {
capability ahci_region;
u64 region_length;

View File

@ -40,7 +40,7 @@ glcr::ErrorCode YellowstoneClient::RegisterEndpoint(const RegisterEndpointReques
glcr::ErrorCode YellowstoneClient::GetAhciInfo(const Empty& request, AhciInfo& response) {
glcr::ErrorCode YellowstoneClient::GetEndpoint(const GetEndpointRequest& request, Endpoint& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
@ -72,7 +72,7 @@ glcr::ErrorCode YellowstoneClient::GetAhciInfo(const Empty& request, AhciInfo& r
glcr::ErrorCode YellowstoneClient::GetFramebufferInfo(const Empty& request, FramebufferInfo& response) {
glcr::ErrorCode YellowstoneClient::GetAhciInfo(const Empty& request, AhciInfo& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
@ -104,7 +104,7 @@ glcr::ErrorCode YellowstoneClient::GetFramebufferInfo(const Empty& request, Fram
glcr::ErrorCode YellowstoneClient::GetDenali(const Empty& request, DenaliInfo& response) {
glcr::ErrorCode YellowstoneClient::GetFramebufferInfo(const Empty& request, FramebufferInfo& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
@ -135,3 +135,35 @@ glcr::ErrorCode YellowstoneClient::GetDenali(const Empty& request, DenaliInfo& r
}
glcr::ErrorCode YellowstoneClient::GetDenali(const Empty& request, DenaliInfo& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
const uint32_t kSentinel = 0xBEEFDEAD;
buffer_.WriteAt<uint32_t>(0, kSentinel);
buffer_.WriteAt<uint64_t>(8, 4);
cap_buffer_.Reset();
uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_);
buffer_.WriteAt<uint32_t>(4, 16 + length);
z_cap_t reply_port_cap;
RET_ERR(ZEndpointSend(endpoint_, 16 + length, buffer_.RawPtr(), cap_buffer_.UsedSlots(), cap_buffer_.RawPtr(), &reply_port_cap));
// FIXME: Add a way to zero out the first buffer.
RET_ERR(ZReplyPortRecv(reply_port_cap, &buffer_size, buffer_.RawPtr(), &cap_size, cap_buffer_.RawPtr()));
if (buffer_.At<uint32_t>(0) != kSentinel) {
return glcr::INVALID_RESPONSE;
}
// Check Response Code.
RET_ERR(buffer_.At<uint64_t>(8));
response.ParseFromBytes(buffer_, 16, cap_buffer_);
return glcr::OK;
}

View File

@ -19,6 +19,8 @@ class YellowstoneClient {
[[nodiscard]] glcr::ErrorCode RegisterEndpoint(const RegisterEndpointRequest& request, Empty& response);
[[nodiscard]] glcr::ErrorCode GetEndpoint(const GetEndpointRequest& request, Endpoint& response);
[[nodiscard]] glcr::ErrorCode GetAhciInfo(const Empty& request, AhciInfo& response);
[[nodiscard]] glcr::ErrorCode GetFramebufferInfo(const Empty& request, FramebufferInfo& response);

View File

@ -134,6 +134,113 @@ uint64_t Empty::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr:
return next_extension;
}
void GetEndpointRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
ParseFromBytesInternal(bytes, offset);
}
void GetEndpointRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
ParseFromBytesInternal(bytes, offset);
}
void GetEndpointRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
CheckHeader(bytes);
// Parse endpoint_name.
auto endpoint_name_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_endpoint_name(bytes.StringAt(offset + endpoint_name_pointer.offset, endpoint_name_pointer.length));
}
uint64_t GetEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
// Write endpoint_name.
ExtPointer endpoint_name_ptr{
.offset = next_extension,
// FIXME: Check downcast of str length.
.length = (uint32_t)endpoint_name().length(),
};
bytes.WriteStringAt(offset + next_extension, endpoint_name());
next_extension += endpoint_name_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), endpoint_name_ptr);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
uint64_t GetEndpointRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
uint64_t next_cap = 0;
// Write endpoint_name.
ExtPointer endpoint_name_ptr{
.offset = next_extension,
// FIXME: Check downcast of str length.
.length = (uint32_t)endpoint_name().length(),
};
bytes.WriteStringAt(offset + next_extension, endpoint_name());
next_extension += endpoint_name_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), endpoint_name_ptr);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
void Endpoint::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
ParseFromBytesInternal(bytes, offset);
// Parse endpoint.
// FIXME: Implement in-buffer capabilities for inprocess serialization.
set_endpoint(0);
}
void Endpoint::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
ParseFromBytesInternal(bytes, offset);
// Parse endpoint.
uint64_t endpoint_ptr = bytes.At<uint64_t>(offset + header_size + (8 * 0));
set_endpoint(caps.At(endpoint_ptr));
}
void Endpoint::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
CheckHeader(bytes);
// Parse endpoint.
// Skip Cap.
}
uint64_t Endpoint::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
// Write endpoint.
// FIXME: Implement inbuffer capabilities.
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 0), 0);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
uint64_t Endpoint::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
uint64_t next_cap = 0;
// Write endpoint.
caps.WriteAt(next_cap, endpoint());
bytes.WriteAt<uint64_t>(offset + header_size + (8 * 0), next_cap++);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
void AhciInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
ParseFromBytesInternal(bytes, offset);
// Parse ahci_region.

View File

@ -46,6 +46,46 @@ class Empty {
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};
class GetEndpointRequest {
public:
GetEndpointRequest() {}
// Delete copy and move until implemented.
GetEndpointRequest(const GetEndpointRequest&) = delete;
GetEndpointRequest(GetEndpointRequest&&) = delete;
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const glcr::String& endpoint_name() const { return endpoint_name_; }
void set_endpoint_name(const glcr::String& value) { endpoint_name_ = value; }
private:
glcr::String endpoint_name_;
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};
class Endpoint {
public:
Endpoint() {}
// Delete copy and move until implemented.
Endpoint(const Endpoint&) = delete;
Endpoint(Endpoint&&) = delete;
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const z_cap_t& endpoint() const { return endpoint_; }
void set_endpoint(const z_cap_t& value) { endpoint_ = value; }
private:
z_cap_t endpoint_;
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};
class AhciInfo {
public:
AhciInfo() {}

View File

@ -98,6 +98,17 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req
break;
}
case 1: {
GetEndpointRequest yunq_request;
Endpoint yunq_response;
yunq_request.ParseFromBytes(request, kHeaderSize, req_caps);
RET_ERR(HandleGetEndpoint(yunq_request, yunq_response));
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
case 2: {
Empty yunq_request;
AhciInfo yunq_response;
@ -108,7 +119,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
case 2: {
case 3: {
Empty yunq_request;
FramebufferInfo yunq_response;
@ -119,7 +130,7 @@ glcr::ErrorCode YellowstoneServerBase::HandleRequest(const glcr::ByteBuffer& req
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
case 3: {
case 4: {
Empty yunq_request;
DenaliInfo yunq_response;

View File

@ -23,6 +23,8 @@ class YellowstoneServerBase {
[[nodiscard]] virtual glcr::ErrorCode HandleRegisterEndpoint(const RegisterEndpointRequest&, Empty&) = 0;
[[nodiscard]] virtual glcr::ErrorCode HandleGetEndpoint(const GetEndpointRequest&, Endpoint&) = 0;
[[nodiscard]] virtual glcr::ErrorCode HandleGetAhciInfo(const Empty&, AhciInfo&) = 0;
[[nodiscard]] virtual glcr::ErrorCode HandleGetFramebufferInfo(const Empty&, FramebufferInfo&) = 0;

View File

@ -82,8 +82,11 @@ glcr::ErrorCode YellowstoneServer::HandleGetFramebufferInfo(
glcr::ErrorCode YellowstoneServer::HandleGetDenali(const Empty&,
DenaliInfo& info) {
if (!endpoint_map_.Contains("denali")) {
return glcr::NOT_FOUND;
}
z_cap_t new_denali;
check(ZCapDuplicate(denali_cap_, kZionPerm_All, &new_denali));
check(ZCapDuplicate(endpoint_map_.at("denali"), kZionPerm_All, &new_denali));
info.set_denali_endpoint(new_denali);
info.set_device_id(device_id_);
info.set_lba_offset(lba_offset_);
@ -93,11 +96,9 @@ glcr::ErrorCode YellowstoneServer::HandleGetDenali(const Empty&,
glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint(
const RegisterEndpointRequest& req, Empty&) {
dbgln("Registering {}.", req.endpoint_name().view());
check(endpoint_map_.Insert(req.endpoint_name(), req.endpoint_capability()));
if (req.endpoint_name() == "denali") {
// FIXME: Rather than blocking and calling the denali service
// immediately we should signal the main thread that it can continue init.
denali_cap_ = req.endpoint_capability();
auto part_info_or = HandleDenaliRegistration(denali_cap_);
auto part_info_or = HandleDenaliRegistration(req.endpoint_capability());
if (!part_info_or.ok()) {
check(part_info_or.error());
}
@ -106,10 +107,9 @@ glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint(
check(has_denali_mutex_.Release());
} else if (req.endpoint_name() == "victoriafalls") {
victoria_falls_cap_ = req.endpoint_capability();
// FIXME: Probably make a separate copy for use within yellowstone vs
// transmit to other processes.
vfs_client_ = glcr::MakeShared<VFSClient>(victoria_falls_cap_);
vfs_client_ = glcr::MakeShared<VFSClient>(req.endpoint_capability());
check(has_victoriafalls_mutex_.Release());
} else {
dbgln("[WARN] Got endpoint cap type: {}", req.endpoint_name().cstr());
@ -117,6 +117,18 @@ glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint(
return glcr::OK;
}
glcr::ErrorCode YellowstoneServer::HandleGetEndpoint(
const GetEndpointRequest& req, Endpoint& resp) {
if (!endpoint_map_.Contains(req.endpoint_name())) {
return glcr::NOT_FOUND;
}
z_cap_t cap = endpoint_map_.at(req.endpoint_name());
z_cap_t new_cap;
check(ZCapDuplicate(cap, kZionPerm_All, &new_cap));
resp.set_endpoint(new_cap);
return glcr::OK;
}
glcr::ErrorCode YellowstoneServer::WaitDenaliRegistered() {
RET_ERR(has_denali_mutex_.Lock());
return has_denali_mutex_.Release();

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/container/hash_map.h>
#include <glacier/memory/shared_ptr.h>
#include <glacier/memory/unique_ptr.h>
#include <glacier/status/error_or.h>
@ -22,6 +23,8 @@ class YellowstoneServer : public YellowstoneServerBase {
glcr::ErrorCode HandleGetDenali(const Empty&, DenaliInfo&) override;
glcr::ErrorCode HandleRegisterEndpoint(const RegisterEndpointRequest&,
Empty&) override;
glcr::ErrorCode HandleGetEndpoint(const GetEndpointRequest&,
Endpoint&) override;
glcr::ErrorCode WaitDenaliRegistered();
glcr::ErrorCode WaitVictoriaFallsRegistered();
@ -29,11 +32,10 @@ class YellowstoneServer : public YellowstoneServerBase {
glcr::SharedPtr<VFSClient> GetVFSClient();
private:
// TODO: Store these in a data structure.
z_cap_t denali_cap_ = 0;
glcr::HashMap<glcr::String, z_cap_t> endpoint_map_;
uint64_t device_id_ = 0;
uint64_t lba_offset_ = 0;
z_cap_t victoria_falls_cap_ = 0;
glcr::SharedPtr<VFSClient> vfs_client_;
PciReader pci_reader_;

BIN
sysroot/default8x16.psfu Normal file

Binary file not shown.

View File

@ -17,6 +17,10 @@ void ProbeCpuAndEnableFeatures() {
panic("SSE3, SSSE3 not available.");
}
if (!(ecx & (0x3 << 19))) {
dbgln("SSE4 not available.");
}
dbgln("Setting SSE");
asm volatile(
"mov %%cr0, %%rax;"

View File

@ -130,6 +130,11 @@ extern "C" void interrupt_page_fault(InterruptFrame* frame) {
panic("PF");
}
extern "C" void isr_fpe_fault();
extern "C" void interrupt_fpe_fault(InterruptFrame* frame) {
panic("Floating point exception");
}
extern "C" void isr_timer();
extern "C" void interrupt_timer(InterruptFrame*) {
gApicTimer->Calibrate();
@ -178,8 +183,9 @@ extern "C" void interrupt_pci4(InterruptFrame*) {
void InitIdt() {
gIdt[0] = CreateDescriptor(isr_divide_by_zero);
gIdt[13] = CreateDescriptor(isr_protection_fault);
gIdt[14] = CreateDescriptor(isr_page_fault);
gIdt[0xD] = CreateDescriptor(isr_protection_fault);
gIdt[0xE] = CreateDescriptor(isr_page_fault);
gIdt[0x13] = CreateDescriptor(isr_fpe_fault);
gIdt[0x20] = CreateDescriptor(isr_timer);
gIdt[0x21] = CreateDescriptor(isr_apic_timer);

View File

@ -58,6 +58,7 @@ isr_\name:
isr_handler divide_by_zero
isr_handler protection_fault,1
isr_handler page_fault,1
isr_handler fpe_fault
isr_handler timer
isr_handler apic_timer

View File

@ -30,7 +30,8 @@ glcr::RefPtr<Thread> Thread::Create(Process& proc, uint64_t tid) {
return glcr::MakeRefCounted<Thread>(proc, tid);
}
Thread::Thread(Process& proc, uint64_t tid) : process_(proc), id_(tid) {
Thread::Thread(Process& proc, uint64_t tid)
: process_(proc), id_(tid), fx_data_(new uint8_t[520]) {
uint64_t* stack_ptr =
reinterpret_cast<uint64_t*>(proc.vmas()->AllocateKernelStack());
// 0: rip
@ -43,6 +44,11 @@ Thread::Thread(Process& proc, uint64_t tid) : process_(proc), id_(tid) {
*(stack_ptr - 16) = proc.vmas()->cr3();
rsp0_ = reinterpret_cast<uint64_t>(stack_ptr - 16);
rsp0_start_ = reinterpret_cast<uint64_t>(stack_ptr);
// Super hacky way to align to 16 bits.
if (reinterpret_cast<uint64_t>(fx_data_) & 0x8) {
fx_data_ += 8;
}
}
uint64_t Thread::pid() const { return process_.id(); }
@ -61,6 +67,10 @@ void Thread::Init() {
dbgln("Thread start.", pid(), id_);
#endif
uint64_t rsp = process_.vmas()->AllocateUserStack();
// TODO: Investigate this further but without this GCC
// will emit movaps calls to non-16-bit-aligned stack
// addresses.
rsp -= 0x8;
SetRsp0(rsp0_start_);
jump_user_space(rip_, rsp, arg1_, arg2_);
}

View File

@ -43,6 +43,8 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
uint64_t* Rsp0Ptr() { return &rsp0_; }
uint64_t Rsp0Start() { return rsp0_start_; }
uint8_t* FxData() { return fx_data_; }
// Switches the thread's state to runnable and enqueues it.
void Start(uint64_t entry, uint64_t arg1, uint64_t arg2);
@ -86,5 +88,8 @@ class Thread : public KernelObject, public glcr::IntrusiveListNode<Thread> {
// I don't think me mind clobbering the stack here.
uint64_t rsp0_start_;
// Pointer to a 512 byte region for FXSAVE and FXRSTOR
uint8_t* fx_data_ = nullptr;
glcr::IntrusiveList<Thread> blocked_threads_;
};

View File

@ -1,5 +1,10 @@
.global context_switch
context_switch:
# %rdi -> Prev Task RSP address
# %rsi -> New Task RSP Address
# %rdx -> Prev Task FXSAVE Address
# %rcx -> New Task FXRSTOR Address
push %rax
push %rcx
push %rdx
@ -18,9 +23,25 @@ context_switch:
mov %cr3, %rax
push %rax
// For the sleep thread rdx will
// be a nullptr.
test %rdx, %rdx
jz switch
fxsave (%rdx)
switch:
mov %rsp, (%rdi) # Save rsp to the prev task.
mov (%rsi), %rsp # Load the next task's rsp.
// For the sleep thread rcx will
// be a nullptr.
test %rcx, %rcx
jz restore
fxrstor (%rcx)
restore:
pop %rax
mov %rax, %cr3
pop %r15

View File

@ -6,7 +6,8 @@
namespace {
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp,
uint8_t* current_fx_data, uint8_t* next_fx_data);
} // namespace
@ -26,7 +27,8 @@ void Scheduler::SwapToCurrent(Thread& prev) {
current_thread_->SetState(Thread::RUNNING);
SetRsp0(current_thread_->Rsp0Start());
context_switch(prev.Rsp0Ptr(), current_thread_->Rsp0Ptr());
context_switch(prev.Rsp0Ptr(), current_thread_->Rsp0Ptr(), prev.FxData(),
current_thread_->FxData());
asm volatile("sti");
}