From a85ab3c933741d69a6a4241184b16e845740fdd2 Mon Sep 17 00:00:00 2001 From: Lexo Liu Date: Wed, 11 Nov 2026 03:42:41 -0500 Subject: [PATCH] aml: deliver Notify to the host through Handler::handle_notify Notify resolves its operand as a SuperName, so a name, an argument, a local or a reference all reach the same object; the interpreter finds the namespace entry holding that object and passes its absolute path and the notification value to the new Handler::handle_notify. The default ignores the notification, as ACPICA does with no notify handler installed. --- src/aml/mod.rs | 11 +- src/aml/namespace.rs | 19 ++ src/aml/object.rs | 7 + src/lib.rs | 15 ++ tests/notify.rs | 183 ++++++++++++++++++ .../src/handlers/logging_handler.rs | 5 + 6 files changed, 237 insertions(+), 3 deletions(-) diff --git a/src/aml/mod.rs b/src/aml/mod.rs index cc6698c8..8dc1928a 100644 --- a/src/aml/mod.rs +++ b/src/aml/mod.rs @@ -8,7 +8,6 @@ * - Correct DefStore / DefCopyObject behaviour * - Load and LoadTable * - DefDataRegion - * - Notify * * - Method recursion depth? * - Loop timeouts @@ -677,10 +676,13 @@ where } } Opcode::Notify => { - // TODO: support notify handlers (ACPICA without one ignores it too) extract_args!(op => [Argument::Object(object), Argument::Object(value)]); let value = value.clone().unwrap_transparent_reference().as_integer()?; - info!("Ignoring Notify of {} with value {}", object, value); + // The handler is told the path of the notified object, so find which namespace entry + // holds the object the operand (a name, argument, local or reference) resolved to. + let object = object.clone().unwrap_reference(); + let path = self.namespace.lock().path_of(&object).ok_or(AmlError::ObjectNotInNamespace)?; + self.handler.handle_notify(&path, value); context.retire_op(op); } Opcode::FromBCD => self.do_from_bcd(&mut context, op)?, @@ -3624,6 +3626,9 @@ pub enum AmlError { LevelDoesNotExist(AmlName), NameCollision(AmlName), ObjectDoesNotExist(AmlName), + /// An operation that identifies an object by its namespace path (e.g. `Notify`) was given an object that is + /// not held by any namespace entry. + ObjectNotInNamespace, NoCurrentOp, ElseFoundWithoutCorrespondingIf, diff --git a/src/aml/namespace.rs b/src/aml/namespace.rs index 60916545..17eaa0ff 100644 --- a/src/aml/namespace.rs +++ b/src/aml/namespace.rs @@ -335,6 +335,25 @@ impl Namespace { Ok((current_level, *last_seg)) } + /// Find the absolute path of the (non-alias) namespace entry that holds exactly this object, compared by + /// identity rather than by value. + pub(crate) fn path_of(&self, object: &WrappedObject) -> Option { + fn search_level(level: &NamespaceLevel, scope: &AmlName, object: &WrappedObject) -> Option { + let found = level.values.iter().find_map(|(seg, (flags, held))| { + (!flags.is_alias() && held.ptr_eq(object)).then(|| AmlName::from_name_seg(*seg).resolve(scope).ok()) + }); + if let Some(found) = found { + return found; + } + level.children.iter().find_map(|(seg, child)| { + let child_scope = AmlName::from_name_seg(*seg).resolve(scope).ok()?; + search_level(child, &child_scope, object) + }) + } + + search_level(&self.root, &AmlName::root(), object) + } + /// Traverse the namespace, calling `f` on each namespace level. `f` returns a `Result` - /// errors terminate the traversal and are propagated, and the `bool` on the successful path marks whether the /// children of the level should also be traversed. diff --git a/src/aml/object.rs b/src/aml/object.rs index cc96af52..b915f268 100644 --- a/src/aml/object.rs +++ b/src/aml/object.rs @@ -113,6 +113,13 @@ assert_impl_all!(Object: Send, Sync); #[derive(Clone, Debug)] pub struct WrappedObject(Arc>); +impl WrappedObject { + /// Returns `true` if both wrappers refer to the same underlying object. + pub(crate) fn ptr_eq(&self, other: &WrappedObject) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + unsafe impl Send for WrappedObject {} unsafe impl Sync for WrappedObject {} diff --git a/src/lib.rs b/src/lib.rs index 575ed13b..f03e8f78 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -471,6 +471,15 @@ pub trait Handler: Clone { fatal_arg ); } + + /// Called when AML executes a `Notify` operation. `object` is the absolute namespace path of the + /// device, processor or thermal zone being notified, and `value` is the notification value. + /// + /// This mirrors the notify handlers of ACPICA. The host is expected to act on the notification + /// (e.g. by re-enumerating a device or re-reading a battery status). The default implementation + /// ignores the notification, as ACPICA does when no handler is installed. + #[cfg(feature = "aml")] + fn handle_notify(&self, _object: &aml::namespace::AmlName, _value: u64) {} } impl Handler for &H { @@ -634,6 +643,12 @@ impl Handler for &H { fn handle_fatal_error(&self, fatal_type: u8, fatal_code: u32, fatal_arg: u64) { (**self).handle_fatal_error(fatal_type, fatal_code, fatal_arg) } + + #[inline] + #[cfg(feature = "aml")] + fn handle_notify(&self, object: &aml::namespace::AmlName, value: u64) { + (**self).handle_notify(object, value) + } } #[cfg(test)] diff --git a/tests/notify.rs b/tests/notify.rs index ed31f9a0..3c762244 100644 --- a/tests/notify.rs +++ b/tests/notify.rs @@ -1,4 +1,15 @@ +use acpi::{ + Handle, + Handler, + RawPhysicalMapping, + aml::{AmlError, namespace::AmlName}, +}; use aml_test_tools::handlers::null_handler::NullHandler; +use pci_types::PciAddress; +use std::{ + str::FromStr, + sync::{Arc, Mutex}, +}; mod test_infra; @@ -21,3 +32,175 @@ DefinitionBlock ("", "SSDT", 2, "RSACPI", "NOTIFY", 0x00000002) { let handler = NullHandler {}; test_infra::run_aml_test(AML, handler); } + +/// A [`NullHandler`] that records every notification it receives. +#[derive(Clone, Default)] +struct NotifyRecorder { + notifications: Arc>>, +} + +impl Handler for NotifyRecorder { + unsafe fn map_physical_region(&self, physical_address: usize, size: usize) -> RawPhysicalMapping { + unsafe { NullHandler.map_physical_region(physical_address, size) } + } + + unsafe fn unmap_physical_region(&self, region: RawPhysicalMapping) { + unsafe { NullHandler.unmap_physical_region(region) } + } + + fn read_u8(&self, address: usize) -> u8 { + NullHandler.read_u8(address) + } + + fn read_u16(&self, address: usize) -> u16 { + NullHandler.read_u16(address) + } + + fn read_u32(&self, address: usize) -> u32 { + NullHandler.read_u32(address) + } + + fn read_u64(&self, address: usize) -> u64 { + NullHandler.read_u64(address) + } + + fn write_u8(&self, address: usize, value: u8) { + NullHandler.write_u8(address, value) + } + + fn write_u16(&self, address: usize, value: u16) { + NullHandler.write_u16(address, value) + } + + fn write_u32(&self, address: usize, value: u32) { + NullHandler.write_u32(address, value) + } + + fn write_u64(&self, address: usize, value: u64) { + NullHandler.write_u64(address, value) + } + + fn read_io_u8(&self, port: u16) -> u8 { + NullHandler.read_io_u8(port) + } + + fn read_io_u16(&self, port: u16) -> u16 { + NullHandler.read_io_u16(port) + } + + fn read_io_u32(&self, port: u16) -> u32 { + NullHandler.read_io_u32(port) + } + + fn write_io_u8(&self, port: u16, value: u8) { + NullHandler.write_io_u8(port, value) + } + + fn write_io_u16(&self, port: u16, value: u16) { + NullHandler.write_io_u16(port, value) + } + + fn write_io_u32(&self, port: u16, value: u32) { + NullHandler.write_io_u32(port, value) + } + + fn read_pci_u8(&self, address: PciAddress, offset: u16) -> u8 { + NullHandler.read_pci_u8(address, offset) + } + + fn read_pci_u16(&self, address: PciAddress, offset: u16) -> u16 { + NullHandler.read_pci_u16(address, offset) + } + + fn read_pci_u32(&self, address: PciAddress, offset: u16) -> u32 { + NullHandler.read_pci_u32(address, offset) + } + + fn write_pci_u8(&self, address: PciAddress, offset: u16, value: u8) { + NullHandler.write_pci_u8(address, offset, value) + } + + fn write_pci_u16(&self, address: PciAddress, offset: u16, value: u16) { + NullHandler.write_pci_u16(address, offset, value) + } + + fn write_pci_u32(&self, address: PciAddress, offset: u16, value: u32) { + NullHandler.write_pci_u32(address, offset, value) + } + + fn nanos_since_boot(&self) -> u64 { + NullHandler.nanos_since_boot() + } + + fn stall(&self, microseconds: u64) { + NullHandler.stall(microseconds) + } + + fn sleep(&self, milliseconds: u64) { + NullHandler.sleep(milliseconds) + } + + fn create_mutex(&self) -> Handle { + NullHandler.create_mutex() + } + + fn acquire(&self, mutex: Handle, timeout: u16) -> Result<(), AmlError> { + NullHandler.acquire(mutex, timeout) + } + + fn release(&self, mutex: Handle) { + NullHandler.release(mutex) + } + + fn handle_notify(&self, object: &AmlName, value: u64) { + self.notifications.lock().unwrap().push((object.clone(), value)); + } +} + +#[test] +fn test_notify_delivers_resolved_path() { + const AML: &str = r#" +DefinitionBlock ("", "SSDT", 2, "RSACPI", "NOTIFY", 0x00000002) { + Scope (\_SB) { + Device (CPUS) { + Name (_HID, "ACPI0010") + Device (C001) { + Name (_HID, "ACPI0007") + } + Method (NTFY, 0, NotSerialized) { + // Relative, resolved against the scope of the running method (\_SB.CPUS) + Notify (C001, 0x01) + // Absolute + Notify (\_SB.CPUS.C001, 0x02) + // Relative with a parent prefix and a computed value + Local0 = 0x80 + Notify (^C001, Local0) + } + Method (NTFA, 2, NotSerialized) { + // The device arrives as an argument + Notify (Arg0, Arg1) + } + Method (NTFR, 0, NotSerialized) { + // The device is held by a local as a reference + Local0 = RefOf (C001) + Notify (Local0, 0x04) + } + } + } + Method (MAIN, 0, NotSerialized) { + \_SB.CPUS.NTFY () + \_SB.CPUS.NTFA (\_SB.CPUS.C001, 0x03) + \_SB.CPUS.NTFR () + Return (0) + } +} +"#; + + let handler = NotifyRecorder::default(); + let notifications = handler.notifications.clone(); + test_infra::run_aml_test(AML, handler); + + let path = AmlName::from_str("\\_SB_.CPUS.C001").unwrap(); + let expected: Vec<_> = [0x01, 0x02, 0x80, 0x03, 0x04].into_iter().map(|value| (path.clone(), value)).collect(); + assert_eq!(*notifications.lock().unwrap(), expected); +} diff --git a/tools/aml-test-tools/src/handlers/logging_handler.rs b/tools/aml-test-tools/src/handlers/logging_handler.rs index fdb30d21..373b3306 100644 --- a/tools/aml-test-tools/src/handlers/logging_handler.rs +++ b/tools/aml-test-tools/src/handlers/logging_handler.rs @@ -183,4 +183,9 @@ where info!("Debug store: {}", object); self.next_handler.handle_debug(object); } + + fn handle_notify(&self, object: &acpi::aml::namespace::AmlName, value: u64) { + info!("Notify of {} with value {}", object, value); + self.next_handler.handle_notify(object, value); + } }