From c65613901f288275a3f082325912eddedc3664cf Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 10 Sep 2024 16:18:42 +0300 Subject: [PATCH] Kernel: Fix AML aliases and package elements --- kernel/include/kernel/ACPI/AML/Alias.h | 49 +++------------------ kernel/include/kernel/ACPI/AML/CopyObject.h | 3 -- kernel/include/kernel/ACPI/AML/Index.h | 7 ++- kernel/include/kernel/ACPI/AML/Node.h | 1 - kernel/include/kernel/ACPI/AML/ObjectType.h | 1 - kernel/include/kernel/ACPI/AML/Package.h | 16 +++---- kernel/include/kernel/ACPI/AML/SizeOf.h | 4 +- kernel/kernel/ACPI/ACPI.cpp | 20 +-------- kernel/kernel/ACPI/AML/Namespace.cpp | 1 - kernel/kernel/ACPI/AML/Scope.cpp | 2 + 10 files changed, 25 insertions(+), 79 deletions(-) diff --git a/kernel/include/kernel/ACPI/AML/Alias.h b/kernel/include/kernel/ACPI/AML/Alias.h index 6e66e2c8..499f5b9a 100644 --- a/kernel/include/kernel/ACPI/AML/Alias.h +++ b/kernel/include/kernel/ACPI/AML/Alias.h @@ -7,37 +7,8 @@ namespace Kernel::ACPI::AML { - struct Alias final : public AML::NamedObject + struct Alias { - BAN::RefPtr target; - - Alias(NameSeg name, BAN::RefPtr target) - : NamedObject(Node::Type::Alias, name) - , target(target) - {} - - BAN::RefPtr to_underlying() override { return target; } - - bool is_scope() const override { return target->is_scope(); } - - BAN::RefPtr convert(uint8_t mask) override - { - ASSERT(target); - return target->convert(mask); - } - - BAN::RefPtr copy() override - { - ASSERT(target); - return target->copy(); - } - - BAN::RefPtr store(BAN::RefPtr node) override - { - ASSERT(target); - return target->store(node); - } - static ParseResult parse(ParseContext& context) { ASSERT(context.aml_data.size() >= 1); @@ -59,27 +30,19 @@ namespace Kernel::ACPI::AML return ParseResult::Success; } - auto alias = MUST(BAN::RefPtr::create(alias_string.value().path.back(), source_object->to_underlying())); - if (!Namespace::root_namespace()->add_named_object(context, alias_string.value(), alias)) + if (!Namespace::root_namespace()->add_named_object(context, alias_string.value(), source_object)) return ParseResult::Success; #if AML_DEBUG_LEVEL >= 2 - alias->debug_print(0); + AML_DEBUG_PRINT("Alias \""); + alias_string->debug_print(); + AML_DEBUG_PRINT("\" => "); + source_object->debug_print(0); AML_DEBUG_PRINTLN(""); #endif return ParseResult::Success; } - - void debug_print(int indent) const override - { - AML_DEBUG_PRINT_INDENT(indent); - AML_DEBUG_PRINTLN("Alias {} { ", name); - target->debug_print(indent + 1); - AML_DEBUG_PRINTLN(""); - AML_DEBUG_PRINT_INDENT(indent); - AML_DEBUG_PRINT("}"); - } }; } diff --git a/kernel/include/kernel/ACPI/AML/CopyObject.h b/kernel/include/kernel/ACPI/AML/CopyObject.h index d6c35ef9..f5fe8c1b 100644 --- a/kernel/include/kernel/ACPI/AML/CopyObject.h +++ b/kernel/include/kernel/ACPI/AML/CopyObject.h @@ -50,9 +50,6 @@ namespace Kernel::ACPI::AML switch (destination->type) { - case AML::Node::Type::Alias: - static_cast(destination.ptr())->target = source->copy(); - return source; case AML::Node::Type::Name: static_cast(destination.ptr())->object = source->copy(); return source; diff --git a/kernel/include/kernel/ACPI/AML/Index.h b/kernel/include/kernel/ACPI/AML/Index.h index 430bf0eb..91977547 100644 --- a/kernel/include/kernel/ACPI/AML/Index.h +++ b/kernel/include/kernel/ACPI/AML/Index.h @@ -67,7 +67,12 @@ namespace Kernel::ACPI::AML return ParseResult::Failure; } auto package_element = package->elements[index]; - result = MUST(BAN::RefPtr::create(package_element->to_underlying())); + if (!package_element) + { + AML_ERROR("IndexOp target is null package element"); + return ParseResult::Failure; + } + result = MUST(BAN::RefPtr::create(package_element)); break; } case AML::Node::Type::String: diff --git a/kernel/include/kernel/ACPI/AML/Node.h b/kernel/include/kernel/ACPI/AML/Node.h index 1329c19c..71566a7d 100644 --- a/kernel/include/kernel/ACPI/AML/Node.h +++ b/kernel/include/kernel/ACPI/AML/Node.h @@ -29,7 +29,6 @@ namespace Kernel::ACPI::AML enum class Type : uint8_t { None, - Alias, BankFieldElement, Buffer, BufferField, diff --git a/kernel/include/kernel/ACPI/AML/ObjectType.h b/kernel/include/kernel/ACPI/AML/ObjectType.h index 37864d5f..6949b817 100644 --- a/kernel/include/kernel/ACPI/AML/ObjectType.h +++ b/kernel/include/kernel/ACPI/AML/ObjectType.h @@ -31,7 +31,6 @@ namespace Kernel::ACPI::AML switch (object->type) { case AML::Node::Type::None: - case AML::Node::Type::Alias: case AML::Node::Type::Name: case AML::Node::Type::PackageElement: case AML::Node::Type::Reference: diff --git a/kernel/include/kernel/ACPI/AML/Package.h b/kernel/include/kernel/ACPI/AML/Package.h index 36add225..d4354126 100644 --- a/kernel/include/kernel/ACPI/AML/Package.h +++ b/kernel/include/kernel/ACPI/AML/Package.h @@ -84,7 +84,7 @@ namespace Kernel::ACPI::AML { if (!initialized) { - AML_ERROR("Trying to store into uninitialized PackageElement"); + AML_ERROR("Trying to convert uninitialized PackageElement"); return {}; } if (!resolved && !resolve()) @@ -96,7 +96,7 @@ namespace Kernel::ACPI::AML { if (!initialized) { - AML_ERROR("Trying to store into uninitialized PackageElement"); + AML_ERROR("Trying to read uninitialized PackageElement"); return {}; } if (!resolved && !resolve()) @@ -108,17 +108,17 @@ namespace Kernel::ACPI::AML { if (!initialized) { - AML_ERROR("Trying to store into uninitialized PackageElement"); - return {}; + initialized = true; + resolved = true; } if (!resolved && !resolve()) return {}; - ASSERT(element->type != AML::Node::Type::Reference); + ASSERT(!element || element->type != AML::Node::Type::Reference); if (node->type == AML::Node::Type::Reference) - element = static_cast(element.ptr())->node; + element = static_cast(node.ptr())->node; else - element = element->copy(); - return element; + element = node->copy(); + return node; } static ParseResult parse(AML::ParseContext& context, BAN::RefPtr package) diff --git a/kernel/include/kernel/ACPI/AML/SizeOf.h b/kernel/include/kernel/ACPI/AML/SizeOf.h index d81bb099..92396ec8 100644 --- a/kernel/include/kernel/ACPI/AML/SizeOf.h +++ b/kernel/include/kernel/ACPI/AML/SizeOf.h @@ -22,8 +22,8 @@ namespace Kernel::ACPI::AML if (!object_result.success()) return ParseResult::Failure; auto object_node = object_result.node(); - if (object_node && object_node->type == AML::Node::Type::Register) - object_node = static_cast(object_node.ptr())->value; + if (object_node) + object_node = object_node->to_underlying(); if (!object_node) { AML_ERROR("SizeOf object is null"); diff --git a/kernel/kernel/ACPI/ACPI.cpp b/kernel/kernel/ACPI/ACPI.cpp index 465ba036..f7959c92 100644 --- a/kernel/kernel/ACPI/ACPI.cpp +++ b/kernel/kernel/ACPI/ACPI.cpp @@ -495,25 +495,7 @@ acpi_release_global_lock: dwarnln("\\_S5 not found"); return; } - BAN::RefPtr s5_evaluated = s5_object; - while (true) - { - bool done = false; - switch (s5_evaluated->type) - { - case AML::Node::Type::Alias: - s5_evaluated = static_cast(s5_evaluated.ptr())->target; - break; - case AML::Node::Type::Name: - s5_evaluated = static_cast(s5_evaluated.ptr())->object; - break; - default: - done = true; - break; - } - if (done) - break; - } + auto s5_evaluated = s5_object->to_underlying(); if (!s5_evaluated) { dwarnln("Failed to evaluate \\_S5"); diff --git a/kernel/kernel/ACPI/AML/Namespace.cpp b/kernel/kernel/ACPI/AML/Namespace.cpp index b73e75c1..c6d0e313 100644 --- a/kernel/kernel/ACPI/AML/Namespace.cpp +++ b/kernel/kernel/ACPI/AML/Namespace.cpp @@ -159,7 +159,6 @@ namespace Kernel::ACPI LockGuard _(m_object_mutex); ASSERT(!object_path.path.empty()); - ASSERT(object_path.path.back() == object->name); auto canonical_path = resolve_path(parse_context.scope, object_path, FindMode::ForceAbsolute, false); ASSERT(canonical_path.has_value()); diff --git a/kernel/kernel/ACPI/AML/Scope.cpp b/kernel/kernel/ACPI/AML/Scope.cpp index 0239ec6c..e0a3e560 100644 --- a/kernel/kernel/ACPI/AML/Scope.cpp +++ b/kernel/kernel/ACPI/AML/Scope.cpp @@ -93,6 +93,8 @@ namespace Kernel::ACPI auto result_integer = result.value() ? result.value()->convert(AML::Node::ConvInteger) : BAN::RefPtr(); + if (!result_integer) + return {}; return static_cast(result_integer.ptr()); }