Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/aml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* - Correct DefStore / DefCopyObject behaviour
* - Load and LoadTable
* - DefDataRegion
* - Notify
*
* - Method recursion depth?
* - Loop timeouts
Expand Down Expand Up @@ -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)?,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/aml/namespace.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: cargo fmt slightly changes your code in path_of - please could you run cargo fmt?

Original file line number Diff line number Diff line change
Expand Up @@ -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<AmlName> {
fn search_level(level: &NamespaceLevel, scope: &AmlName, object: &WrappedObject) -> Option<AmlName> {
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<bool, AmlError>` -
/// 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.
Expand Down
7 changes: 7 additions & 0 deletions src/aml/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ assert_impl_all!(Object: Send, Sync);
#[derive(Clone, Debug)]
pub struct WrappedObject(Arc<UnsafeCell<Object>>);

impl WrappedObject {
/// Returns `true` if both wrappers refer to the same underlying object.
pub(crate) fn ptr_eq(&self, other: &WrappedObject) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this function into the main impl block instead of starting a new one.

Arc::ptr_eq(&self.0, &other.0)
}
}

unsafe impl Send for WrappedObject {}
unsafe impl Sync for WrappedObject {}

Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Handler> Handler for &H {
Expand Down Expand Up @@ -634,6 +643,12 @@ impl<H: Handler> 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)]
Expand Down
183 changes: 183 additions & 0 deletions tests/notify.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could the tests be grouped together? (As opposed to having this handler in the middle)

Did you consider merging this with CheckCommandHandler?

struct NotifyRecorder {
notifications: Arc<Mutex<Vec<(AmlName, u64)>>>,
}

impl Handler for NotifyRecorder {
unsafe fn map_physical_region<T>(&self, physical_address: usize, size: usize) -> RawPhysicalMapping<T> {
unsafe { NullHandler.map_physical_region(physical_address, size) }
}

unsafe fn unmap_physical_region<T>(&self, region: RawPhysicalMapping<T>) {
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);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please would you add a test of the error path? e.g.

Local0 = 1 // Local0 does not refer to a namespace object
Notify(Local0, 2)

5 changes: 5 additions & 0 deletions tools/aml-test-tools/src/handlers/logging_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}