Skip to content
Draft
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
61 changes: 27 additions & 34 deletions src/aml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,14 @@ where
let operand = operand.clone().unwrap_reference();
let token = self.object_token.lock();

let operand = unsafe {operand.gain_mut(&token)};
let operand = unsafe { operand.gain_mut(&token) };
let value = match operand {
Object::Integer(operand) => {
*operand
}
Object::BufferField{..} => {
operand.to_integer(self.integer_size)?
}
_ => Err(AmlError::ObjectNotOfExpectedType { expected: ObjectType::Integer, got: operand.typ() })?
Object::Integer(operand) => *operand,
Object::BufferField { .. } => operand.to_integer(self.integer_size)?,
_ => Err(AmlError::ObjectNotOfExpectedType {
expected: ObjectType::Integer,
got: operand.typ(),
})?,
};

let new_value = match op.op {
Expand All @@ -542,10 +541,10 @@ where
Object::Integer(operand) => {
*operand = new_value;
}
Object::BufferField{..} => {
Object::BufferField { .. } => {
operand.write_buffer_field(&new_value.to_le_bytes(), &token)?;
}
_ => unreachable!()
_ => unreachable!(),
}

context.contribute_arg(Argument::Object(Object::Integer(new_value).wrap()));
Expand Down Expand Up @@ -2540,35 +2539,29 @@ where
Ok(object)
}

/// Copy `object` into `target`, matching the expected behaviour of `DefCopyObject`, which
/// depends on the object referenced:
/// - Locals are overwritten
/// - Args are overwritten, unless they are references, in which case the referenced object is overwritten
/// - Objects referenced by name are overwritten
/// - Index references cause the object at the index to be overwritten
/// - Other reference operations are not allowed
/// Copy `object` into `target`, matching the expected behaviour of `DefCopyObject`.
///
/// Those rules break into two parts:
/// 1. How to deal with Args, Locals, references - which are dealt with by
/// [`WrappedObject::unwrap_ref_for_copyobject`]
/// 2. What to do when copying over objects of various types, which are dealt with here.
fn do_copy_object(&self, target: WrappedObject, object: WrappedObject) -> Result<(), AmlError> {
let Object::Reference { kind, ref inner } = *target else {
return Err(AmlError::InternalError("Target of CopyObject must be a reference".to_string()));
};
let dst = target.unwrap_ref_for_copyobject()?;
let object = object.clone().unwrap_transparent_reference();
let token = self.object_token.lock();

let dst = match kind {
ReferenceKind::Named | ReferenceKind::Local => inner.clone().unwrap_transparent_reference(),
ReferenceKind::Arg => {
if let Object::Reference { kind: _, inner: ref inner_inner } = **inner {
inner_inner.clone()
} else {
inner.clone().unwrap_transparent_reference()
}
match *dst {
Object::BufferField { .. } => {
unsafe { dst.gain_mut(&token) }
.write_buffer_field(object.to_buffer(self.integer_size)?.as_slice(), &token)?;
}
Object::FieldUnit(ref field_unit) => self.do_field_write(field_unit, object.clone())?,
_ => {
// TODO: This is definitively *unsound* if dst and object are one and the same...
// (due to mut and non-mut refs existing simultaneously)
let mut_ref = unsafe { dst.gain_mut(&token) };
*mut_ref = (*object).clone();
}
ReferenceKind::Index => inner.clone(),
ReferenceKind::RefOf | ReferenceKind::Unresolved => return Err(AmlError::StoreToInvalidReferenceType),
};

unsafe {
*dst.gain_mut(&token) = (*object).clone();
}

Ok(())
Expand Down
33 changes: 33 additions & 0 deletions src/aml/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,39 @@ impl WrappedObject {
target = inner.clone();
}
}

/// Decide where to store the result of a `CopyObject` command.
///
/// This depends on the object referenced:
/// - Locals are overwritten
/// - Args are overwritten, unless they are references, in which case the referenced object is overwritten
/// - Objects referenced by name are overwritten
/// - Index references cause the object at the index to be overwritten
/// - Other reference operations are not allowed
///
/// This is defined in the
/// [Object Storing and Copying Rules](https://uefi.org/specs/ACPI/6.6/19_ASL_Reference.html#object-storing-and-copying-rules)
/// table of the ACPI spec.
pub(crate) fn unwrap_ref_for_copyobject(self) -> Result<WrappedObject, AmlError> {
let Object::Reference { kind, ref inner } = *self else {
return Err(AmlError::InternalError("Target of CopyObject must be a reference".to_string()));
};

let dst = match kind {
ReferenceKind::Named | ReferenceKind::Local => inner.clone().unwrap_transparent_reference(),
ReferenceKind::Arg => {
if let Object::Reference { kind: _, inner: ref inner_inner } = **inner {
inner_inner.clone().unwrap_reference()
} else {
self
}
}
ReferenceKind::Index => todo!(),
ReferenceKind::RefOf | ReferenceKind::Unresolved => return Err(AmlError::StoreToInvalidReferenceType),
};

Ok(dst)
}
}

impl ops::Deref for WrappedObject {
Expand Down
107 changes: 107 additions & 0 deletions tests/copyobject.asl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// These tests are based on the following table:
// [Object Storing and Copying Rules](https://uefi.org/specs/ACPI/6.6/19_ASL_Reference.html#object-storing-and-copying-rules)
//
// Note: Tests are in a fairly arbitrary order of increasing "mental complexity". Which row of the above table they
// relate to is given in brackets.

// TODO: Check that a suitable error occurs for copying non-integer/buffer to fields. (as described in row 3 of the
// table)
DefinitionBlock ("", "DSDT", 2, "RSCAPI", "CPYOBJCT", 0xF0F0F0F0) {
Name(FCNT, 0)

Method (CHEK, 2) {
If (Arg0 != Arg1) {
Printf("Wrong result, %o != %o", ToHexString(Arg0), ToHexString(Arg1))
FCNT++
}
}

// Straightforward tests with a named object (row 4 of the table)
Method (T1) {
Name(TST, "Hello")
CopyObject(1, TST)
CHEK(TST, 1)

CopyObject("World", TST)
CHEK(TST, "World")
}

// Arguments are rebound unless they are a RefOf. (row 1 of the table)
Method (T2) {
Name(TST, "String")
T2IN(TST)
CHEK(TST, "String")
}

Method (T2IN, 1) {
CopyObject("Something else", Arg0)
}

// Arguments are not rebound if they are a RefOf (row 1 of the table)
Method (T3) {
Name(TST, "String")
T3IN(RefOf(TST))
CHEK(TST, "Something else")
}

Method (T3IN, 1) {
CopyObject("Something else", Arg0)
}

// When doing CopyObject to Arg, multiple RefOfs are skipped over before rebinding (row 1 of the table)
//
// TODO: uACPI and Windows pass this test, but ACPICA does not.
Method (T4) {
Name(TST, "String")
Local0 = RefOf(TST)
T4IN(RefOf(Local0))
CHEK(TST, "Something else")
}

Method (T4IN, 1) {
CopyObject("Something else", Arg0)
}

// Local is overwritten (row 2 of the table)
Method (T5) {
Local0 = "String"
CopyObject(1, Local0)
CHEK(Local0, 1)
}

// Local is overwritten, even if it contains a reference (row 2 of the table)
Method (T6) {
Local0 = "String"
Local1 = RefOf(Local0)
CopyObject(1, Local1)
CHEK(Local0, "String")
CHEK(Local1, 1)
}

Name(BUF, Buffer (12) { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c })

CreateDwordField(BUF, 0, DWRD)
CreateWordField(BUF, 1, WORD)

// Copy to buffer fields works (row 3 of the table)
// NB: Field Units cannot be properly tested in the ASL / aml-tester tests as reads there always return 0. They
// could be tested in the Rust integration tests if needed.
Method (T7) {
CopyObject(0xFFFFFFFF, WORD)

CHEK(WORD, 0xFFFF)
CHEK(DWRD, 0x04FFFF01)
}

Method (MAIN) {
T1()
T2()
T3()
T4()
T5()
T6()
T7()

Return (FCNT)
}
}
Loading