Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: psloop - Main AML parse loop
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 /*
0011  * Parse the AML and build an operation tree as most interpreters, (such as
0012  * Perl) do. Parsing is done by hand rather than with a YACC generated parser
0013  * to tightly constrain stack and dynamic memory usage. Parsing is kept
0014  * flexible and the code fairly compact by parsing based on a list of AML
0015  * opcode templates in aml_op_info[].
0016  */
0017 
0018 #include <acpi/acpi.h>
0019 #include "accommon.h"
0020 #include "acinterp.h"
0021 #include "acparser.h"
0022 #include "acdispat.h"
0023 #include "amlcode.h"
0024 #include "acconvert.h"
0025 #include "acnamesp.h"
0026 
0027 #define _COMPONENT          ACPI_PARSER
0028 ACPI_MODULE_NAME("psloop")
0029 
0030 /* Local prototypes */
0031 static acpi_status
0032 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
0033               u8 * aml_op_start, union acpi_parse_object *op);
0034 
0035 /*******************************************************************************
0036  *
0037  * FUNCTION:    acpi_ps_get_arguments
0038  *
0039  * PARAMETERS:  walk_state          - Current state
0040  *              aml_op_start        - Op start in AML
0041  *              op                  - Current Op
0042  *
0043  * RETURN:      Status
0044  *
0045  * DESCRIPTION: Get arguments for passed Op.
0046  *
0047  ******************************************************************************/
0048 
0049 static acpi_status
0050 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
0051               u8 * aml_op_start, union acpi_parse_object *op)
0052 {
0053     acpi_status status = AE_OK;
0054     union acpi_parse_object *arg = NULL;
0055 
0056     ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
0057 
0058     ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
0059               "Get arguments for opcode [%s]\n",
0060               op->common.aml_op_name));
0061 
0062     switch (op->common.aml_opcode) {
0063     case AML_BYTE_OP:   /* AML_BYTEDATA_ARG */
0064     case AML_WORD_OP:   /* AML_WORDDATA_ARG */
0065     case AML_DWORD_OP:  /* AML_DWORDATA_ARG */
0066     case AML_QWORD_OP:  /* AML_QWORDATA_ARG */
0067     case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
0068 
0069         /* Fill in constant or string argument directly */
0070 
0071         acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
0072                         GET_CURRENT_ARG_TYPE(walk_state->
0073                                  arg_types),
0074                         op);
0075         break;
0076 
0077     case AML_INT_NAMEPATH_OP:   /* AML_NAMESTRING_ARG */
0078 
0079         status = acpi_ps_get_next_namepath(walk_state,
0080                            &(walk_state->parser_state),
0081                            op,
0082                            ACPI_POSSIBLE_METHOD_CALL);
0083         if (ACPI_FAILURE(status)) {
0084             return_ACPI_STATUS(status);
0085         }
0086 
0087         walk_state->arg_types = 0;
0088         break;
0089 
0090     default:
0091         /*
0092          * Op is not a constant or string, append each argument to the Op
0093          */
0094         while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
0095                !walk_state->arg_count) {
0096             walk_state->aml = walk_state->parser_state.aml;
0097 
0098             switch (op->common.aml_opcode) {
0099             case AML_METHOD_OP:
0100             case AML_BUFFER_OP:
0101             case AML_PACKAGE_OP:
0102             case AML_VARIABLE_PACKAGE_OP:
0103             case AML_WHILE_OP:
0104 
0105                 break;
0106 
0107             default:
0108 
0109                 ASL_CV_CAPTURE_COMMENTS(walk_state);
0110                 break;
0111             }
0112 
0113             status =
0114                 acpi_ps_get_next_arg(walk_state,
0115                          &(walk_state->parser_state),
0116                          GET_CURRENT_ARG_TYPE
0117                          (walk_state->arg_types), &arg);
0118             if (ACPI_FAILURE(status)) {
0119                 return_ACPI_STATUS(status);
0120             }
0121 
0122             if (arg) {
0123                 acpi_ps_append_arg(op, arg);
0124             }
0125 
0126             INCREMENT_ARG_LIST(walk_state->arg_types);
0127         }
0128 
0129         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
0130                   "Final argument count: %8.8X pass %u\n",
0131                   walk_state->arg_count,
0132                   walk_state->pass_number));
0133 
0134         /* Special processing for certain opcodes */
0135 
0136         switch (op->common.aml_opcode) {
0137         case AML_METHOD_OP:
0138             /*
0139              * Skip parsing of control method because we don't have enough
0140              * info in the first pass to parse it correctly.
0141              *
0142              * Save the length and address of the body
0143              */
0144             op->named.data = walk_state->parser_state.aml;
0145             op->named.length = (u32)
0146                 (walk_state->parser_state.pkg_end -
0147                  walk_state->parser_state.aml);
0148 
0149             /* Skip body of method */
0150 
0151             walk_state->parser_state.aml =
0152                 walk_state->parser_state.pkg_end;
0153             walk_state->arg_count = 0;
0154             break;
0155 
0156         case AML_BUFFER_OP:
0157         case AML_PACKAGE_OP:
0158         case AML_VARIABLE_PACKAGE_OP:
0159 
0160             if ((op->common.parent) &&
0161                 (op->common.parent->common.aml_opcode ==
0162                  AML_NAME_OP)
0163                 && (walk_state->pass_number <=
0164                 ACPI_IMODE_LOAD_PASS2)) {
0165                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
0166                           "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
0167                           walk_state->pass_number,
0168                           aml_op_start));
0169 
0170                 /*
0171                  * Skip parsing of Buffers and Packages because we don't have
0172                  * enough info in the first pass to parse them correctly.
0173                  */
0174                 op->named.data = aml_op_start;
0175                 op->named.length = (u32)
0176                     (walk_state->parser_state.pkg_end -
0177                      aml_op_start);
0178 
0179                 /* Skip body */
0180 
0181                 walk_state->parser_state.aml =
0182                     walk_state->parser_state.pkg_end;
0183                 walk_state->arg_count = 0;
0184             }
0185             break;
0186 
0187         case AML_WHILE_OP:
0188 
0189             if (walk_state->control_state) {
0190                 walk_state->control_state->control.package_end =
0191                     walk_state->parser_state.pkg_end;
0192             }
0193             break;
0194 
0195         default:
0196 
0197             /* No action for all other opcodes */
0198 
0199             break;
0200         }
0201 
0202         break;
0203     }
0204 
0205     return_ACPI_STATUS(AE_OK);
0206 }
0207 
0208 /*******************************************************************************
0209  *
0210  * FUNCTION:    acpi_ps_parse_loop
0211  *
0212  * PARAMETERS:  walk_state          - Current state
0213  *
0214  * RETURN:      Status
0215  *
0216  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
0217  *              a tree of ops.
0218  *
0219  ******************************************************************************/
0220 
0221 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
0222 {
0223     acpi_status status = AE_OK;
0224     union acpi_parse_object *op = NULL; /* current op */
0225     struct acpi_parse_state *parser_state;
0226     u8 *aml_op_start = NULL;
0227     u8 opcode_length;
0228 
0229     ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
0230 
0231     if (walk_state->descending_callback == NULL) {
0232         return_ACPI_STATUS(AE_BAD_PARAMETER);
0233     }
0234 
0235     parser_state = &walk_state->parser_state;
0236     walk_state->arg_types = 0;
0237 
0238 #ifndef ACPI_CONSTANT_EVAL_ONLY
0239 
0240     if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
0241 
0242         /* We are restarting a preempted control method */
0243 
0244         if (acpi_ps_has_completed_scope(parser_state)) {
0245             /*
0246              * We must check if a predicate to an IF or WHILE statement
0247              * was just completed
0248              */
0249             if ((parser_state->scope->parse_scope.op) &&
0250                 ((parser_state->scope->parse_scope.op->common.
0251                   aml_opcode == AML_IF_OP)
0252                  || (parser_state->scope->parse_scope.op->common.
0253                  aml_opcode == AML_WHILE_OP))
0254                 && (walk_state->control_state)
0255                 && (walk_state->control_state->common.state ==
0256                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
0257                 /*
0258                  * A predicate was just completed, get the value of the
0259                  * predicate and branch based on that value
0260                  */
0261                 walk_state->op = NULL;
0262                 status =
0263                     acpi_ds_get_predicate_value(walk_state,
0264                                 ACPI_TO_POINTER
0265                                 (TRUE));
0266                 if (ACPI_FAILURE(status)
0267                     && !ACPI_CNTL_EXCEPTION(status)) {
0268                     if (status == AE_AML_NO_RETURN_VALUE) {
0269                         ACPI_EXCEPTION((AE_INFO, status,
0270                                 "Invoked method did not return a value"));
0271                     }
0272 
0273                     ACPI_EXCEPTION((AE_INFO, status,
0274                             "GetPredicate Failed"));
0275                     return_ACPI_STATUS(status);
0276                 }
0277 
0278                 status =
0279                     acpi_ps_next_parse_state(walk_state, op,
0280                                  status);
0281             }
0282 
0283             acpi_ps_pop_scope(parser_state, &op,
0284                       &walk_state->arg_types,
0285                       &walk_state->arg_count);
0286             ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
0287                       "Popped scope, Op=%p\n", op));
0288         } else if (walk_state->prev_op) {
0289 
0290             /* We were in the middle of an op */
0291 
0292             op = walk_state->prev_op;
0293             walk_state->arg_types = walk_state->prev_arg_types;
0294         }
0295     }
0296 #endif
0297 
0298     /* Iterative parsing loop, while there is more AML to process: */
0299 
0300     while ((parser_state->aml < parser_state->aml_end) || (op)) {
0301         ASL_CV_CAPTURE_COMMENTS(walk_state);
0302 
0303         aml_op_start = parser_state->aml;
0304         if (!op) {
0305             status =
0306                 acpi_ps_create_op(walk_state, aml_op_start, &op);
0307             if (ACPI_FAILURE(status)) {
0308                 /*
0309                  * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
0310                  * executing it as a control method. However, if we encounter
0311                  * an error while loading the table, we need to keep trying to
0312                  * load the table rather than aborting the table load. Set the
0313                  * status to AE_OK to proceed with the table load.
0314                  */
0315                 if ((walk_state->
0316                      parse_flags & ACPI_PARSE_MODULE_LEVEL)
0317                     && ((status == AE_ALREADY_EXISTS)
0318                     || (status == AE_NOT_FOUND))) {
0319                     status = AE_OK;
0320                 }
0321                 if (status == AE_CTRL_PARSE_CONTINUE) {
0322                     continue;
0323                 }
0324 
0325                 if (status == AE_CTRL_PARSE_PENDING) {
0326                     status = AE_OK;
0327                 }
0328 
0329                 if (status == AE_CTRL_TERMINATE) {
0330                     return_ACPI_STATUS(status);
0331                 }
0332 
0333                 status =
0334                     acpi_ps_complete_op(walk_state, &op,
0335                             status);
0336                 if (ACPI_FAILURE(status)) {
0337                     return_ACPI_STATUS(status);
0338                 }
0339                 if (acpi_ns_opens_scope
0340                     (acpi_ps_get_opcode_info
0341                      (walk_state->opcode)->object_type)) {
0342                     /*
0343                      * If the scope/device op fails to parse, skip the body of
0344                      * the scope op because the parse failure indicates that
0345                      * the device may not exist.
0346                      */
0347                     ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state->opcode), walk_state->opcode));
0348 
0349                     /*
0350                      * Determine the opcode length before skipping the opcode.
0351                      * An opcode can be 1 byte or 2 bytes in length.
0352                      */
0353                     opcode_length = 1;
0354                     if ((walk_state->opcode & 0xFF00) ==
0355                         AML_EXTENDED_OPCODE) {
0356                         opcode_length = 2;
0357                     }
0358                     walk_state->parser_state.aml =
0359                         walk_state->aml + opcode_length;
0360 
0361                     walk_state->parser_state.aml =
0362                         acpi_ps_get_next_package_end
0363                         (&walk_state->parser_state);
0364                     walk_state->aml =
0365                         walk_state->parser_state.aml;
0366                 }
0367 
0368                 continue;
0369             }
0370 
0371             acpi_ex_start_trace_opcode(op, walk_state);
0372         }
0373 
0374         /*
0375          * Start arg_count at zero because we don't know if there are
0376          * any args yet
0377          */
0378         walk_state->arg_count = 0;
0379 
0380         switch (op->common.aml_opcode) {
0381         case AML_BYTE_OP:
0382         case AML_WORD_OP:
0383         case AML_DWORD_OP:
0384         case AML_QWORD_OP:
0385 
0386             break;
0387 
0388         default:
0389 
0390             ASL_CV_CAPTURE_COMMENTS(walk_state);
0391             break;
0392         }
0393 
0394         /* Are there any arguments that must be processed? */
0395 
0396         if (walk_state->arg_types) {
0397 
0398             /* Get arguments */
0399 
0400             status =
0401                 acpi_ps_get_arguments(walk_state, aml_op_start, op);
0402             if (ACPI_FAILURE(status)) {
0403                 status =
0404                     acpi_ps_complete_op(walk_state, &op,
0405                             status);
0406                 if (ACPI_FAILURE(status)) {
0407                     return_ACPI_STATUS(status);
0408                 }
0409                 if ((walk_state->control_state) &&
0410                     ((walk_state->control_state->control.
0411                       opcode == AML_IF_OP)
0412                      || (walk_state->control_state->control.
0413                      opcode == AML_WHILE_OP))) {
0414                     /*
0415                      * If the if/while op fails to parse, we will skip parsing
0416                      * the body of the op.
0417                      */
0418                     parser_state->aml =
0419                         walk_state->control_state->control.
0420                         aml_predicate_start + 1;
0421                     parser_state->aml =
0422                         acpi_ps_get_next_package_end
0423                         (parser_state);
0424                     walk_state->aml = parser_state->aml;
0425 
0426                     ACPI_ERROR((AE_INFO,
0427                             "Skipping While/If block"));
0428                     if (*walk_state->aml == AML_ELSE_OP) {
0429                         ACPI_ERROR((AE_INFO,
0430                                 "Skipping Else block"));
0431                         walk_state->parser_state.aml =
0432                             walk_state->aml + 1;
0433                         walk_state->parser_state.aml =
0434                             acpi_ps_get_next_package_end
0435                             (parser_state);
0436                         walk_state->aml =
0437                             parser_state->aml;
0438                     }
0439                     ACPI_FREE(acpi_ut_pop_generic_state
0440                           (&walk_state->control_state));
0441                 }
0442                 op = NULL;
0443                 continue;
0444             }
0445         }
0446 
0447         /* Check for arguments that need to be processed */
0448 
0449         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
0450                   "Parseloop: argument count: %8.8X\n",
0451                   walk_state->arg_count));
0452 
0453         if (walk_state->arg_count) {
0454             /*
0455              * There are arguments (complex ones), push Op and
0456              * prepare for argument
0457              */
0458             status = acpi_ps_push_scope(parser_state, op,
0459                             walk_state->arg_types,
0460                             walk_state->arg_count);
0461             if (ACPI_FAILURE(status)) {
0462                 status =
0463                     acpi_ps_complete_op(walk_state, &op,
0464                             status);
0465                 if (ACPI_FAILURE(status)) {
0466                     return_ACPI_STATUS(status);
0467                 }
0468 
0469                 continue;
0470             }
0471 
0472             op = NULL;
0473             continue;
0474         }
0475 
0476         /*
0477          * All arguments have been processed -- Op is complete,
0478          * prepare for next
0479          */
0480         walk_state->op_info =
0481             acpi_ps_get_opcode_info(op->common.aml_opcode);
0482         if (walk_state->op_info->flags & AML_NAMED) {
0483             if (op->common.aml_opcode == AML_REGION_OP ||
0484                 op->common.aml_opcode == AML_DATA_REGION_OP) {
0485                 /*
0486                  * Skip parsing of control method or opregion body,
0487                  * because we don't have enough info in the first pass
0488                  * to parse them correctly.
0489                  *
0490                  * Completed parsing an op_region declaration, we now
0491                  * know the length.
0492                  */
0493                 op->named.length =
0494                     (u32) (parser_state->aml - op->named.data);
0495             }
0496         }
0497 
0498         if (walk_state->op_info->flags & AML_CREATE) {
0499             /*
0500              * Backup to beginning of create_XXXfield declaration (1 for
0501              * Opcode)
0502              *
0503              * body_length is unknown until we parse the body
0504              */
0505             op->named.length =
0506                 (u32) (parser_state->aml - op->named.data);
0507         }
0508 
0509         if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
0510             /*
0511              * Backup to beginning of bank_field declaration
0512              *
0513              * body_length is unknown until we parse the body
0514              */
0515             op->named.length =
0516                 (u32) (parser_state->aml - op->named.data);
0517         }
0518 
0519         /* This op complete, notify the dispatcher */
0520 
0521         if (walk_state->ascending_callback != NULL) {
0522             walk_state->op = op;
0523             walk_state->opcode = op->common.aml_opcode;
0524 
0525             status = walk_state->ascending_callback(walk_state);
0526             status =
0527                 acpi_ps_next_parse_state(walk_state, op, status);
0528             if (status == AE_CTRL_PENDING) {
0529                 status = AE_OK;
0530             } else
0531                 if ((walk_state->
0532                  parse_flags & ACPI_PARSE_MODULE_LEVEL)
0533                 && (ACPI_AML_EXCEPTION(status)
0534                     || status == AE_ALREADY_EXISTS
0535                     || status == AE_NOT_FOUND)) {
0536                 /*
0537                  * ACPI_PARSE_MODULE_LEVEL flag means that we
0538                  * are currently loading a table by executing
0539                  * it as a control method. However, if we
0540                  * encounter an error while loading the table,
0541                  * we need to keep trying to load the table
0542                  * rather than aborting the table load (setting
0543                  * the status to AE_OK continues the table
0544                  * load). If we get a failure at this point, it
0545                  * means that the dispatcher got an error while
0546                  * trying to execute the Op.
0547                  */
0548                 status = AE_OK;
0549             }
0550         }
0551 
0552         status = acpi_ps_complete_op(walk_state, &op, status);
0553         if (ACPI_FAILURE(status)) {
0554             return_ACPI_STATUS(status);
0555         }
0556 
0557     }           /* while parser_state->Aml */
0558 
0559     status = acpi_ps_complete_final_op(walk_state, op, status);
0560     return_ACPI_STATUS(status);
0561 }