[Teton] Load a font file and write a character to the screen.

This commit is contained in:
Drew Galbraith 2023-11-21 19:14:02 -08:00
parent 96063126cb
commit fe44804dd9
19 changed files with 412 additions and 17 deletions

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.