Skip to content

Commit

Permalink
Interpreter: Fix MLC issues by switching to new TermList grammar for …
Browse files Browse the repository at this point in the history
…table loading

The MLC (Module Level Code) is an ACPICA terminology describing the AML
code out of any control method, its support is an indication of the
interpreter behavior during the table loading.

The original implementation of MLC in ACPICA had several issues:
1. Out of any control method, besides of the object creating opcodes, only
   the code blocks wrapped by "If/Else/While" opcodes were supported.
2. The supported MLC code blocks were executed after loading the table
   rather than being executed right in place.
   ============================================================
   The demo of this order issue is as follows:
     Name (OBJ1, 1)
     If (CND1 == 1)
     {
       Name (OBJ2, 2)
     }
     Name (OBJ3, 3)
   The original MLC support created OBJ2 after OBJ3's creation.
   ============================================================
Other than these limitations, MLC support in ACPICA looks correct. And
supporting this should be easy/natural for ACPICA, but enabling of this was
blocked by some ACPICA internal and OSPM specific initialization order
issues we've fixed recently. The wrong support started from the following
false bug fixing commit:
  Commit: 80d7951
  Subject: Add support for module-level executable AML code.

We can confirm Windows interpreter behavior via reverse engineering means.
It can be proven that not only If/Else/While wrapped code blocks, all
opcodes can be executed at the module level, including operation region
accesses. And it can be proven that the MLC should be executed right in
place, not in such a deferred way executed after loading the table.

And the above facts indeed reflect the spec words around ACPI definition
block tables (DSDT/SSDT/...), the entire table and the Scope object is
defined by the AML specification in BNF style as:
  AMLCode := DefBlockHeader TermList
  DefScope := ScopeOp PkgLength NameString TermList
The bodies of the scope opening terms (AMLCode/Scope) are all TermList,
thus the table loading should be no difference than the control method
evaluations as the body of the Method is also defined by the AML
specification as TermList:
  DefMethod := MethodOp PkgLength NameString MethodFlags TermList
The only difference is: after evaluating control method, created named
objects may be freed due to no reference, while named objects created by
the table loading should only be freed after unloading the table.

So this patch follows the spec and the de-facto standard behavior, enables
the new grammar (TermList) for the table loading.

By doing so, beyond the fixes to the above issues, we can see additional
differences comparing to the old grammar based table loading:
1. Originally, beyond the scope opening terms (AMLCode/Scope),
   If/Else/While wrapped code blocks under the scope creating terms
   (Device/PowerResource/Processor/ThermalZone) are also supported as
   deferred MLC, which violates the spec defined grammar where ObjectList
   is enforced. With MLC support improved as non-deferred, the interpreter
   parses such scope creating terms as TermList rather ObjectList like the
   scope opening terms.
   After probing the Windows behavior and proving that it also parses these
   terms as TermList, we submitted an ECR (Engineering Change Request) to
   the ASWG (ACPI Specification Working Group) to clarify this. The ECR is
   titled as "ASL Grammar Clarification for Executable AML Opcodes" and has
   been accepted by the ASWG. The new grammar will appear in ACPI
   specification 6.2.
2. Originally, Buffer/Package/OperationRegion/CreateXXXField/BankField
   arguments are evaluated in a deferred way after loading the table. With
   MLC support improved, they are also parsed right in place during the
   table loading.
   This is also Windows compliant and the only difference is the removal
   of the debugging messages implemented before AcpiDsExecuteArguments(),
   see Link 1 for the details. A previous commit should have ensured that
   AcpiCheckAddressRange() won't regress.

Note that enabling this feature may cause regressions due to long term
Linux ACPI support on top of the wrong grammar. So this patch also prepares
a global option to be used to roll back to the old grammar during the
period between a regression is reported and the regression is
root-cause-fixed. Lv Zheng.

Link 1: https://bugzilla.kernel.org/show_bug.cgi?id=112911
Tested-by: Chris Bainbridge <chris.bainbridge@gmail.com>
Signed-off-by: Lv Zheng <lv.zheng@intel.com>
  • Loading branch information
Lv Zheng committed Aug 26, 2016
1 parent 5ae7d8f commit 0e24fb6
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 40 deletions.
8 changes: 8 additions & 0 deletions source/compiler/aslstubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#include "acevents.h"
#include "acinterp.h"
#include "acnamesp.h"
#include "acparser.h"

#define _COMPONENT ACPI_COMPILER
ACPI_MODULE_NAME ("aslstubs")
Expand All @@ -142,6 +143,13 @@ AcpiNsInitializeObjects (
return (AE_OK);
}

ACPI_STATUS
AcpiPsExecuteTable (
ACPI_EVALUATE_INFO *Info)
{
return (AE_OK);
}

ACPI_STATUS
AcpiHwReadPort (
ACPI_IO_ADDRESS Address,
Expand Down
3 changes: 2 additions & 1 deletion source/components/events/evrgnini.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ AcpiEvInitializeRegion (
*
* See AcpiNsExecModuleCode
*/
if (ObjDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL)
if (!AcpiGbl_ParseTableAsTermList &&
ObjDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL)
{
HandlerObj = ObjDesc->Method.Dispatch.Handler;
}
Expand Down
2 changes: 1 addition & 1 deletion source/components/executer/exconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ AcpiExAddTable (
/* Execute any module-level code that was found in the table */

AcpiExExitInterpreter ();
if (AcpiGbl_GroupModuleLevelCode)
if (!AcpiGbl_ParseTableAsTermList && AcpiGbl_GroupModuleLevelCode)
{
AcpiNsExecModuleCodeList ();
}
Expand Down
2 changes: 1 addition & 1 deletion source/components/namespace/nsload.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ AcpiNsLoadTable (
* other ACPI implementations. Optionally, the execution can be deferred
* until later, see AcpiInitializeObjects.
*/
if (!AcpiGbl_GroupModuleLevelCode)
if (!AcpiGbl_ParseTableAsTermList && !AcpiGbl_GroupModuleLevelCode)
{
AcpiNsExecModuleCodeList ();
}
Expand Down
178 changes: 147 additions & 31 deletions source/components/namespace/nsparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,109 @@
ACPI_MODULE_NAME ("nsparse")


/*******************************************************************************
*
* FUNCTION: NsExecuteTable
*
* PARAMETERS: TableDesc - An ACPI table descriptor for table to parse
* StartNode - Where to enter the table into the namespace
*
* RETURN: Status
*
* DESCRIPTION: Load ACPI/AML table by executing the entire table as a
* TermList.
*
******************************************************************************/

ACPI_STATUS
AcpiNsExecuteTable (
UINT32 TableIndex,
ACPI_NAMESPACE_NODE *StartNode)
{
ACPI_STATUS Status;
ACPI_TABLE_HEADER *Table;
ACPI_OWNER_ID OwnerId;
ACPI_EVALUATE_INFO *Info = NULL;
UINT32 AmlLength;
UINT8 *AmlStart;
ACPI_OPERAND_OBJECT *MethodObj = NULL;


ACPI_FUNCTION_TRACE (NsExecuteTable);


Status = AcpiGetTableByIndex (TableIndex, &Table);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}

/* Table must consist of at least a complete header */

if (Table->Length < sizeof (ACPI_TABLE_HEADER))
{
return_ACPI_STATUS (AE_BAD_HEADER);
}

AmlStart = (UINT8 *) Table + sizeof (ACPI_TABLE_HEADER);
AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);

Status = AcpiTbGetOwnerId (TableIndex, &OwnerId);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}

/* Create, initialize, and link a new temporary method object */

MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
if (!MethodObj)
{
return_ACPI_STATUS (AE_NO_MEMORY);
}

/* Allocate the evaluation information block */

Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
if (!Info)
{
Status = AE_NO_MEMORY;
goto Cleanup;
}

ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
"Create table code block: %p\n", MethodObj));

MethodObj->Method.AmlStart = AmlStart;
MethodObj->Method.AmlLength = AmlLength;
MethodObj->Method.OwnerId = OwnerId;
MethodObj->Method.InfoFlags |= ACPI_METHOD_MODULE_LEVEL;

Info->PassNumber = ACPI_IMODE_EXECUTE;
Info->Node = StartNode;
Info->ObjDesc = MethodObj;
Info->NodeFlags = Info->Node->Flags;
Info->FullPathname = AcpiNsGetNormalizedPathname (Info->Node, TRUE);
if (!Info->FullPathname)
{
Status = AE_NO_MEMORY;
goto Cleanup;
}

Status = AcpiPsExecuteTable (Info);

Cleanup:
if (Info)
{
ACPI_FREE (Info->FullPathname);
Info->FullPathname = NULL;
}
ACPI_FREE (Info);
AcpiUtRemoveReference (MethodObj);
return_ACPI_STATUS (Status);
}


/*******************************************************************************
*
* FUNCTION: NsOneCompleteParse
Expand Down Expand Up @@ -260,40 +363,53 @@ AcpiNsParseTable (
ACPI_FUNCTION_TRACE (NsParseTable);


/*
* AML Parse, pass 1
*
* In this pass, we load most of the namespace. Control methods
* are not parsed until later. A parse tree is not created. Instead,
* each Parser Op subtree is deleted when it is finished. This saves
* a great deal of memory, and allows a small cache of parse objects
* to service the entire parse. The second pass of the parse then
* performs another complete parse of the AML.
*/
ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 1\n"));

Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS1,
TableIndex, StartNode);
if (ACPI_FAILURE (Status))
if (AcpiGbl_ParseTableAsTermList)
{
return_ACPI_STATUS (Status);
}
ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start load pass\n"));

/*
* AML Parse, pass 2
*
* In this pass, we resolve forward references and other things
* that could not be completed during the first pass.
* Another complete parse of the AML is performed, but the
* overhead of this is compensated for by the fact that the
* parse objects are all cached.
*/
ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 2\n"));
Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2,
TableIndex, StartNode);
if (ACPI_FAILURE (Status))
Status = AcpiNsExecuteTable (TableIndex, StartNode);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}
}
else
{
return_ACPI_STATUS (Status);
/*
* AML Parse, pass 1
*
* In this pass, we load most of the namespace. Control methods
* are not parsed until later. A parse tree is not created.
* Instead, each Parser Op subtree is deleted when it is finished.
* This saves a great deal of memory, and allows a small cache of
* parse objects to service the entire parse. The second pass of
* the parse then performs another complete parse of the AML.
*/
ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 1\n"));

Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS1,
TableIndex, StartNode);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}

/*
* AML Parse, pass 2
*
* In this pass, we resolve forward references and other things
* that could not be completed during the first pass.
* Another complete parse of the AML is performed, but the
* overhead of this is compensated for by the fact that the
* parse objects are all cached.
*/
ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 2\n"));
Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2,
TableIndex, StartNode);
if (ACPI_FAILURE (Status))
{
return_ACPI_STATUS (Status);
}
}

return_ACPI_STATUS (Status);
Expand Down
3 changes: 2 additions & 1 deletion source/components/parser/psparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ AcpiPsParseAml (
* cleanup to do
*/
if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) ==
ACPI_PARSE_EXECUTE) ||
ACPI_PARSE_EXECUTE &&
!(WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL)) ||
(ACPI_FAILURE (Status)))
{
AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
Expand Down
80 changes: 80 additions & 0 deletions source/components/parser/psxface.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,86 @@ AcpiPsExecuteMethod (
}


/*******************************************************************************
*
* FUNCTION: AcpiPsExecuteTable
*
* PARAMETERS: Info - Method info block, contains:
* Node - Node to where the is entered into the
* namespace
* ObjDesc - Pseudo method object describing the AML
* code of the entire table
* PassNumber - Parse or execute pass
*
* RETURN: Status
*
* DESCRIPTION: Execute a table
*
******************************************************************************/

ACPI_STATUS
AcpiPsExecuteTable (
ACPI_EVALUATE_INFO *Info)
{
ACPI_STATUS Status;
ACPI_PARSE_OBJECT *Op = NULL;
ACPI_WALK_STATE *WalkState = NULL;


ACPI_FUNCTION_TRACE (PsExecuteTable);


/* Create and init a Root Node */

Op = AcpiPsCreateScopeOp (Info->ObjDesc->Method.AmlStart);
if (!Op)
{
Status = AE_NO_MEMORY;
goto Cleanup;
}

/* Create and initialize a new walk state */

WalkState = AcpiDsCreateWalkState (
Info->ObjDesc->Method.OwnerId, NULL, NULL, NULL);
if (!WalkState)
{
Status = AE_NO_MEMORY;
goto Cleanup;
}

Status = AcpiDsInitAmlWalk (WalkState, Op, Info->Node,
Info->ObjDesc->Method.AmlStart,
Info->ObjDesc->Method.AmlLength, Info, Info->PassNumber);
if (ACPI_FAILURE (Status))
{
goto Cleanup;
}

if (Info->ObjDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL)
{
WalkState->ParseFlags |= ACPI_PARSE_MODULE_LEVEL;
}

/*
* Parse the AML, WalkState will be deleted by ParseAml
*/
Status = AcpiPsParseAml (WalkState);
WalkState = NULL;

Cleanup:
if (WalkState)
{
AcpiDsDeleteWalkState (WalkState);
}
if (Op)
{
AcpiPsDeleteParseTree (Op);
}
return_ACPI_STATUS (Status);
}


/*******************************************************************************
*
* FUNCTION: AcpiPsUpdateParameterList
Expand Down
2 changes: 1 addition & 1 deletion source/components/tables/tbxfload.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ AcpiLoadTables (
"While loading namespace from ACPI tables"));
}

if (!AcpiGbl_GroupModuleLevelCode)
if (AcpiGbl_ParseTableAsTermList || !AcpiGbl_GroupModuleLevelCode)
{
/*
* Initialize the objects that remain uninitialized. This
Expand Down
2 changes: 1 addition & 1 deletion source/components/utilities/utxfinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ AcpiInitializeObjects (
* all of the tables have been loaded. It is a legacy option and is
* not compatible with other ACPI implementations. See AcpiNsLoadTable.
*/
if (AcpiGbl_GroupModuleLevelCode)
if (!AcpiGbl_ParseTableAsTermList && AcpiGbl_GroupModuleLevelCode)
{
AcpiNsExecModuleCodeList ();

Expand Down
5 changes: 5 additions & 0 deletions source/include/acnamesp.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ AcpiNsParseTable (
UINT32 TableIndex,
ACPI_NAMESPACE_NODE *StartNode);

ACPI_STATUS
AcpiNsExecuteTable (
UINT32 TableIndex,
ACPI_NAMESPACE_NODE *StartNode);

ACPI_STATUS
AcpiNsOneCompleteParse (
UINT32 PassNumber,
Expand Down
4 changes: 4 additions & 0 deletions source/include/acparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ ACPI_STATUS
AcpiPsExecuteMethod (
ACPI_EVALUATE_INFO *Info);

ACPI_STATUS
AcpiPsExecuteTable (
ACPI_EVALUATE_INFO *Info);


/*
* psargs - Parse AML opcode arguments
Expand Down
7 changes: 7 additions & 0 deletions source/include/acpixf.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ ACPI_INIT_GLOBAL (UINT8, AcpiGbl_DoNotUseXsdt, FALSE);
*/
ACPI_INIT_GLOBAL (UINT8, AcpiGbl_GroupModuleLevelCode, FALSE);

/*
* Optionally support module level code by parsing the entire table as
* a TermList. Default is FALSE, do not execute entire table until some
* lock order issues are fixed.
*/
ACPI_INIT_GLOBAL (UINT8, AcpiGbl_ParseTableAsTermList, FALSE);

/*
* Optionally use 32-bit FADT addresses if and when there is a conflict
* (address mismatch) between the 32-bit and 64-bit versions of the
Expand Down
Loading

0 comments on commit 0e24fb6

Please sign in to comment.