Kernel: Implement ACPI IndexOp into Strings

This commit is contained in:
Bananymous 2024-06-25 23:21:54 +03:00
parent cd41d5f6dd
commit 2fccff5a35
4 changed files with 49 additions and 18 deletions

View File

@ -5,6 +5,7 @@
#include <kernel/ACPI/AML/Node.h> #include <kernel/ACPI/AML/Node.h>
#include <kernel/ACPI/AML/ParseContext.h> #include <kernel/ACPI/AML/ParseContext.h>
#include <kernel/ACPI/AML/Pkg.h> #include <kernel/ACPI/AML/Pkg.h>
#include <kernel/ACPI/AML/String.h>
namespace Kernel::ACPI::AML namespace Kernel::ACPI::AML
{ {
@ -67,11 +68,12 @@ namespace Kernel::ACPI::AML
struct BufferField : AML::NamedObject struct BufferField : AML::NamedObject
{ {
BAN::RefPtr<Buffer> buffer; BAN::RefPtr<AML::Node> buffer;
size_t field_bit_offset; size_t field_bit_offset;
size_t field_bit_size; size_t field_bit_size;
BufferField(AML::NameSeg name, BAN::RefPtr<Buffer> buffer, size_t field_bit_offset, size_t field_bit_size) template<typename T> requires BAN::is_same_v<T, AML::Buffer> || BAN::is_same_v<T, AML::String>
BufferField(AML::NameSeg name, BAN::RefPtr<T> buffer, size_t field_bit_offset, size_t field_bit_size)
: AML::NamedObject(Node::Type::BufferField, name) : AML::NamedObject(Node::Type::BufferField, name)
, buffer(buffer) , buffer(buffer)
, field_bit_offset(field_bit_offset) , field_bit_offset(field_bit_offset)
@ -81,7 +83,11 @@ namespace Kernel::ACPI::AML
BAN::RefPtr<AML::Node> evaluate() override BAN::RefPtr<AML::Node> evaluate() override
{ {
ASSERT(buffer); ASSERT(buffer);
ASSERT(field_bit_offset + field_bit_size <= buffer->buffer.size() * 8); ASSERT(buffer->type == AML::Node::Type::Buffer || buffer->type == AML::Node::Type::String);
const auto& buffer = (this->buffer->type == AML::Node::Type::Buffer)
? static_cast<AML::Buffer*>(this->buffer.ptr())->buffer
: static_cast<AML::String*>(this->buffer.ptr())->string;
ASSERT(field_bit_offset + field_bit_size <= buffer.size() * 8);
uint64_t value = 0; uint64_t value = 0;
@ -89,13 +95,13 @@ namespace Kernel::ACPI::AML
const size_t bit_offset = field_bit_offset % 8; const size_t bit_offset = field_bit_offset % 8;
if (field_bit_size == 1) if (field_bit_size == 1)
{ {
value = (buffer->buffer[byte_offset] >> bit_offset) & 1; value = (buffer[byte_offset] >> bit_offset) & 1;
} }
else else
{ {
ASSERT(bit_offset == 0); ASSERT(bit_offset == 0);
for (size_t byte = 0; byte < field_bit_size / 8; byte++) for (size_t byte = 0; byte < field_bit_size / 8; byte++)
value |= buffer->buffer[byte_offset + byte] << byte; value |= buffer[byte_offset + byte] << byte;
} }
return MUST(BAN::RefPtr<AML::Integer>::create(value)); return MUST(BAN::RefPtr<AML::Integer>::create(value));
@ -104,7 +110,11 @@ namespace Kernel::ACPI::AML
bool store(BAN::RefPtr<AML::Node> node) override bool store(BAN::RefPtr<AML::Node> node) override
{ {
ASSERT(buffer); ASSERT(buffer);
ASSERT(field_bit_offset + field_bit_size <= buffer->buffer.size() * 8); ASSERT(buffer->type == AML::Node::Type::Buffer || buffer->type == AML::Node::Type::String);
auto& buffer = (this->buffer->type == AML::Node::Type::Buffer)
? static_cast<AML::Buffer*>(this->buffer.ptr())->buffer
: static_cast<AML::String*>(this->buffer.ptr())->string;
ASSERT(field_bit_offset + field_bit_size <= buffer.size() * 8);
auto value = node->as_integer(); auto value = node->as_integer();
if (!value.has_value()) if (!value.has_value())
@ -114,14 +124,14 @@ namespace Kernel::ACPI::AML
const size_t bit_offset = field_bit_offset % 8; const size_t bit_offset = field_bit_offset % 8;
if (field_bit_size == 1) if (field_bit_size == 1)
{ {
buffer->buffer[byte_offset] &= ~(1 << bit_offset); buffer[byte_offset] &= ~(1 << bit_offset);
buffer->buffer[byte_offset] |= (value.value() & 1) << bit_offset; buffer[byte_offset] |= (value.value() & 1) << bit_offset;
} }
else else
{ {
ASSERT(bit_offset == 0); ASSERT(bit_offset == 0);
for (size_t byte = 0; byte < field_bit_size / 8; byte++) for (size_t byte = 0; byte < field_bit_size / 8; byte++)
buffer->buffer[byte_offset + byte] = (value.value() >> (byte * 8)) & 0xFF; buffer[byte_offset + byte] = (value.value() >> (byte * 8)) & 0xFF;
} }
return true; return true;
@ -163,7 +173,7 @@ namespace Kernel::ACPI::AML
AML_ERROR("Buffer source does not evaluate to a Buffer"); AML_ERROR("Buffer source does not evaluate to a Buffer");
return ParseResult::Failure; return ParseResult::Failure;
} }
auto buffer = static_cast<Buffer*>(buffer_node.ptr()); auto buffer = BAN::RefPtr<AML::Buffer>(static_cast<AML::Buffer*>(buffer_node.ptr()));
auto index_result = AML::parse_object(context); auto index_result = AML::parse_object(context);
if (!index_result.success()) if (!index_result.success())

View File

@ -42,7 +42,7 @@ namespace Kernel::ACPI::AML
{ {
case AML::Node::Type::Buffer: case AML::Node::Type::Buffer:
{ {
auto buffer = static_cast<AML::Buffer*>(source.ptr()); auto buffer = BAN::RefPtr<AML::Buffer>(static_cast<AML::Buffer*>(source.ptr()));
if (index.value() >= buffer->buffer.size()) if (index.value() >= buffer->buffer.size())
{ {
AML_ERROR("IndexOp index is out of buffer bounds"); AML_ERROR("IndexOp index is out of buffer bounds");
@ -65,8 +65,17 @@ namespace Kernel::ACPI::AML
break; break;
} }
case AML::Node::Type::String: case AML::Node::Type::String:
AML_TODO("IndexOp source String"); {
return ParseResult::Failure; auto string = BAN::RefPtr<AML::String>(static_cast<AML::String*>(source.ptr()));
if (index.value() >= string->string.size())
{
AML_ERROR("IndexOp index is out of string bounds");
return ParseResult::Failure;
}
auto buffer_field = MUST(BAN::RefPtr<BufferField>::create(NameSeg(""_sv), string, index.value() * 8, 8));
result = MUST(BAN::RefPtr<AML::Reference>::create(buffer_field));
break;
}
default: default:
AML_ERROR("IndexOp source is not a Buffer, Package, or String"); AML_ERROR("IndexOp source is not a Buffer, Package, or String");
return ParseResult::Failure; return ParseResult::Failure;

View File

@ -9,28 +9,40 @@ namespace Kernel::ACPI::AML
struct String : public AML::Node struct String : public AML::Node
{ {
BAN::String string; BAN::Vector<uint8_t> string;
String() : Node(Node::Type::String) {} String() : Node(Node::Type::String) {}
String(BAN::StringView string)
: Node(Node::Type::String)
{
MUST(this->string.resize(string.size()));
for (size_t i = 0; i < string.size(); i++)
this->string[i] = string[i];
}
BAN::RefPtr<AML::Node> evaluate() override BAN::RefPtr<AML::Node> evaluate() override
{ {
return this; return this;
} }
BAN::StringView string_view() const
{
return BAN::StringView(reinterpret_cast<const char*>(string.data()), string.size());
}
static ParseResult parse(ParseContext& context) static ParseResult parse(ParseContext& context)
{ {
ASSERT(context.aml_data.size() >= 1); ASSERT(context.aml_data.size() >= 1);
ASSERT(static_cast<AML::Byte>(context.aml_data[0]) == AML::Byte::StringPrefix); ASSERT(static_cast<AML::Byte>(context.aml_data[0]) == AML::Byte::StringPrefix);
context.aml_data = context.aml_data.slice(1); context.aml_data = context.aml_data.slice(1);
BAN::String string; BAN::Vector<uint8_t> string;
while (context.aml_data.size() > 0) while (context.aml_data.size() > 0)
{ {
if (context.aml_data[0] == 0x00) if (context.aml_data[0] == 0x00)
break; break;
MUST(string.push_back(static_cast<char>(context.aml_data[0]))); MUST(string.push_back(context.aml_data[0]));
context.aml_data = context.aml_data.slice(1); context.aml_data = context.aml_data.slice(1);
} }
@ -49,7 +61,7 @@ namespace Kernel::ACPI::AML
virtual void debug_print(int indent) const override virtual void debug_print(int indent) const override
{ {
AML_DEBUG_PRINT_INDENT(indent); AML_DEBUG_PRINT_INDENT(indent);
AML_DEBUG_PRINT("String \"{}\"", string); AML_DEBUG_PRINT("String \"{}\"", string_view());
} }
}; };

View File

@ -222,7 +222,7 @@ namespace Kernel::ACPI
return {}; return {};
} }
auto string = static_cast<AML::String*>(arg.ptr()); auto string = static_cast<AML::String*>(arg.ptr());
return string->string == "Linux" ? AML::Integer::Constants::Ones : AML::Integer::Constants::Zero; return string->string_view() == "Linux"_sv ? AML::Integer::Constants::Ones : AML::Integer::Constants::Zero;
}; };
ASSERT(s_root_namespace->add_named_object(context, AML::NameString("\\_OSI"), osi)); ASSERT(s_root_namespace->add_named_object(context, AML::NameString("\\_OSI"), osi));