Erik Schmauss | 9585763 | 2018-03-14 16:13:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 2 | /****************************************************************************** |
| 3 | * |
| 4 | * Module Name: psloop - Main AML parse loop |
| 5 | * |
Bob Moore | da6f832 | 2018-01-04 10:06:38 -0800 | [diff] [blame] | 6 | * Copyright (C) 2000 - 2018, Intel Corp. |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 7 | * |
Erik Schmauss | 9585763 | 2018-03-14 16:13:07 -0700 | [diff] [blame] | 8 | *****************************************************************************/ |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 9 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 10 | /* |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 11 | * Parse the AML and build an operation tree as most interpreters, (such as |
| 12 | * Perl) do. Parsing is done by hand rather than with a YACC generated parser |
| 13 | * to tightly constrain stack and dynamic memory usage. Parsing is kept |
| 14 | * flexible and the code fairly compact by parsing based on a list of AML |
| 15 | * opcode templates in aml_op_info[]. |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <acpi/acpi.h> |
Len Brown | e2f7a77 | 2009-01-09 00:30:03 -0500 | [diff] [blame] | 19 | #include "accommon.h" |
Lv Zheng | ab6c573 | 2015-07-23 12:52:59 +0800 | [diff] [blame] | 20 | #include "acinterp.h" |
Len Brown | e2f7a77 | 2009-01-09 00:30:03 -0500 | [diff] [blame] | 21 | #include "acparser.h" |
| 22 | #include "acdispat.h" |
| 23 | #include "amlcode.h" |
Bob Moore | 9cf7ade | 2017-04-28 08:53:22 +0800 | [diff] [blame] | 24 | #include "acconvert.h" |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 25 | |
| 26 | #define _COMPONENT ACPI_PARSER |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 27 | ACPI_MODULE_NAME("psloop") |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 28 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 29 | /* Local prototypes */ |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 30 | static acpi_status |
| 31 | acpi_ps_get_arguments(struct acpi_walk_state *walk_state, |
| 32 | u8 * aml_op_start, union acpi_parse_object *op); |
| 33 | |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 34 | static void |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 35 | acpi_ps_link_module_code(union acpi_parse_object *parent_op, |
| 36 | u8 *aml_start, u32 aml_length, acpi_owner_id owner_id); |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 37 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 38 | /******************************************************************************* |
| 39 | * |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 40 | * FUNCTION: acpi_ps_get_arguments |
| 41 | * |
| 42 | * PARAMETERS: walk_state - Current state |
| 43 | * aml_op_start - Op start in AML |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 44 | * op - Current Op |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 45 | * |
| 46 | * RETURN: Status |
| 47 | * |
| 48 | * DESCRIPTION: Get arguments for passed Op. |
| 49 | * |
| 50 | ******************************************************************************/ |
| 51 | |
| 52 | static acpi_status |
| 53 | acpi_ps_get_arguments(struct acpi_walk_state *walk_state, |
| 54 | u8 * aml_op_start, union acpi_parse_object *op) |
| 55 | { |
| 56 | acpi_status status = AE_OK; |
| 57 | union acpi_parse_object *arg = NULL; |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 58 | const struct acpi_opcode_info *op_info; |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 59 | |
| 60 | ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state); |
| 61 | |
Bob Moore | ce87e09 | 2016-12-28 15:29:43 +0800 | [diff] [blame] | 62 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
| 63 | "Get arguments for opcode [%s]\n", |
| 64 | op->common.aml_op_name)); |
| 65 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 66 | switch (op->common.aml_opcode) { |
| 67 | case AML_BYTE_OP: /* AML_BYTEDATA_ARG */ |
| 68 | case AML_WORD_OP: /* AML_WORDDATA_ARG */ |
| 69 | case AML_DWORD_OP: /* AML_DWORDATA_ARG */ |
| 70 | case AML_QWORD_OP: /* AML_QWORDATA_ARG */ |
| 71 | case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */ |
| 72 | |
| 73 | /* Fill in constant or string argument directly */ |
| 74 | |
| 75 | acpi_ps_get_next_simple_arg(&(walk_state->parser_state), |
| 76 | GET_CURRENT_ARG_TYPE(walk_state-> |
| 77 | arg_types), |
| 78 | op); |
| 79 | break; |
| 80 | |
| 81 | case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */ |
| 82 | |
Bob Moore | 89438f9 | 2015-12-29 14:00:07 +0800 | [diff] [blame] | 83 | status = acpi_ps_get_next_namepath(walk_state, |
| 84 | &(walk_state->parser_state), |
| 85 | op, |
| 86 | ACPI_POSSIBLE_METHOD_CALL); |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 87 | if (ACPI_FAILURE(status)) { |
| 88 | return_ACPI_STATUS(status); |
| 89 | } |
| 90 | |
| 91 | walk_state->arg_types = 0; |
| 92 | break; |
| 93 | |
| 94 | default: |
| 95 | /* |
| 96 | * Op is not a constant or string, append each argument to the Op |
| 97 | */ |
Bob Moore | 1fad873 | 2015-12-29 13:54:36 +0800 | [diff] [blame] | 98 | while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) && |
| 99 | !walk_state->arg_count) { |
Lv Zheng | 83482f7 | 2015-07-23 12:52:11 +0800 | [diff] [blame] | 100 | walk_state->aml = walk_state->parser_state.aml; |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 101 | |
Bob Moore | 9cf7ade | 2017-04-28 08:53:22 +0800 | [diff] [blame] | 102 | switch (op->common.aml_opcode) { |
| 103 | case AML_METHOD_OP: |
| 104 | case AML_BUFFER_OP: |
| 105 | case AML_PACKAGE_OP: |
| 106 | case AML_VARIABLE_PACKAGE_OP: |
| 107 | case AML_WHILE_OP: |
| 108 | |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | |
| 113 | ASL_CV_CAPTURE_COMMENTS(walk_state); |
| 114 | break; |
| 115 | } |
| 116 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 117 | status = |
| 118 | acpi_ps_get_next_arg(walk_state, |
| 119 | &(walk_state->parser_state), |
| 120 | GET_CURRENT_ARG_TYPE |
| 121 | (walk_state->arg_types), &arg); |
| 122 | if (ACPI_FAILURE(status)) { |
| 123 | return_ACPI_STATUS(status); |
| 124 | } |
| 125 | |
| 126 | if (arg) { |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 127 | acpi_ps_append_arg(op, arg); |
| 128 | } |
| 129 | |
| 130 | INCREMENT_ARG_LIST(walk_state->arg_types); |
| 131 | } |
| 132 | |
Bob Moore | a62a711 | 2017-08-03 14:27:22 +0800 | [diff] [blame] | 133 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
Bob Moore | 1ef6323 | 2018-02-15 13:09:28 -0800 | [diff] [blame] | 134 | "Final argument count: %8.8X pass %u\n", |
Bob Moore | a62a711 | 2017-08-03 14:27:22 +0800 | [diff] [blame] | 135 | walk_state->arg_count, |
| 136 | walk_state->pass_number)); |
| 137 | |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 138 | /* |
| 139 | * Handle executable code at "module-level". This refers to |
| 140 | * executable opcodes that appear outside of any control method. |
| 141 | */ |
| 142 | if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) && |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 143 | ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) { |
| 144 | /* |
| 145 | * We want to skip If/Else/While constructs during Pass1 because we |
| 146 | * want to actually conditionally execute the code during Pass2. |
| 147 | * |
| 148 | * Except for disassembly, where we always want to walk the |
| 149 | * If/Else/While packages |
| 150 | */ |
| 151 | switch (op->common.aml_opcode) { |
| 152 | case AML_IF_OP: |
| 153 | case AML_ELSE_OP: |
| 154 | case AML_WHILE_OP: |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 155 | /* |
| 156 | * Currently supported module-level opcodes are: |
| 157 | * IF/ELSE/WHILE. These appear to be the most common, |
| 158 | * and easiest to support since they open an AML |
| 159 | * package. |
| 160 | */ |
| 161 | if (walk_state->pass_number == |
| 162 | ACPI_IMODE_LOAD_PASS1) { |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 163 | acpi_ps_link_module_code(op->common. |
| 164 | parent, |
| 165 | aml_op_start, |
| 166 | (u32) |
| 167 | (walk_state-> |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 168 | parser_state. |
| 169 | pkg_end - |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 170 | aml_op_start), |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 171 | walk_state-> |
| 172 | owner_id); |
| 173 | } |
| 174 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 175 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
| 176 | "Pass1: Skipping an If/Else/While body\n")); |
| 177 | |
| 178 | /* Skip body of if/else/while in pass 1 */ |
| 179 | |
| 180 | walk_state->parser_state.aml = |
| 181 | walk_state->parser_state.pkg_end; |
| 182 | walk_state->arg_count = 0; |
| 183 | break; |
| 184 | |
| 185 | default: |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 186 | /* |
| 187 | * Check for an unsupported executable opcode at module |
| 188 | * level. We must be in PASS1, the parent must be a SCOPE, |
| 189 | * The opcode class must be EXECUTE, and the opcode must |
| 190 | * not be an argument to another opcode. |
| 191 | */ |
| 192 | if ((walk_state->pass_number == |
| 193 | ACPI_IMODE_LOAD_PASS1) |
| 194 | && (op->common.parent->common.aml_opcode == |
| 195 | AML_SCOPE_OP)) { |
| 196 | op_info = |
| 197 | acpi_ps_get_opcode_info(op->common. |
| 198 | aml_opcode); |
| 199 | if ((op_info->class == |
| 200 | AML_CLASS_EXECUTE) && (!arg)) { |
| 201 | ACPI_WARNING((AE_INFO, |
Bob Moore | 00eb325 | 2012-10-31 02:27:48 +0000 | [diff] [blame] | 202 | "Unsupported module-level executable opcode " |
| 203 | "0x%.2X at table offset 0x%.4X", |
| 204 | op->common. |
| 205 | aml_opcode, |
| 206 | (u32) |
| 207 | (ACPI_PTR_DIFF |
| 208 | (aml_op_start, |
| 209 | walk_state-> |
| 210 | parser_state. |
| 211 | aml_start) + |
| 212 | sizeof(struct |
| 213 | acpi_table_header)))); |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 214 | } |
| 215 | } |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 219 | |
| 220 | /* Special processing for certain opcodes */ |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 221 | |
| 222 | switch (op->common.aml_opcode) { |
| 223 | case AML_METHOD_OP: |
| 224 | /* |
| 225 | * Skip parsing of control method because we don't have enough |
| 226 | * info in the first pass to parse it correctly. |
| 227 | * |
| 228 | * Save the length and address of the body |
| 229 | */ |
| 230 | op->named.data = walk_state->parser_state.aml; |
| 231 | op->named.length = (u32) |
| 232 | (walk_state->parser_state.pkg_end - |
| 233 | walk_state->parser_state.aml); |
| 234 | |
| 235 | /* Skip body of method */ |
| 236 | |
| 237 | walk_state->parser_state.aml = |
| 238 | walk_state->parser_state.pkg_end; |
| 239 | walk_state->arg_count = 0; |
| 240 | break; |
| 241 | |
| 242 | case AML_BUFFER_OP: |
| 243 | case AML_PACKAGE_OP: |
Bob Moore | 9ff5a21a | 2017-04-26 16:18:40 +0800 | [diff] [blame] | 244 | case AML_VARIABLE_PACKAGE_OP: |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 245 | |
| 246 | if ((op->common.parent) && |
| 247 | (op->common.parent->common.aml_opcode == |
| 248 | AML_NAME_OP) |
| 249 | && (walk_state->pass_number <= |
| 250 | ACPI_IMODE_LOAD_PASS2)) { |
Bob Moore | a62a711 | 2017-08-03 14:27:22 +0800 | [diff] [blame] | 251 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
| 252 | "Setup Package/Buffer: Pass %u, AML Ptr: %p\n", |
| 253 | walk_state->pass_number, |
| 254 | aml_op_start)); |
| 255 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 256 | /* |
| 257 | * Skip parsing of Buffers and Packages because we don't have |
| 258 | * enough info in the first pass to parse them correctly. |
| 259 | */ |
| 260 | op->named.data = aml_op_start; |
| 261 | op->named.length = (u32) |
| 262 | (walk_state->parser_state.pkg_end - |
| 263 | aml_op_start); |
| 264 | |
| 265 | /* Skip body */ |
| 266 | |
| 267 | walk_state->parser_state.aml = |
| 268 | walk_state->parser_state.pkg_end; |
| 269 | walk_state->arg_count = 0; |
| 270 | } |
| 271 | break; |
| 272 | |
| 273 | case AML_WHILE_OP: |
| 274 | |
| 275 | if (walk_state->control_state) { |
| 276 | walk_state->control_state->control.package_end = |
| 277 | walk_state->parser_state.pkg_end; |
| 278 | } |
| 279 | break; |
| 280 | |
| 281 | default: |
| 282 | |
| 283 | /* No action for all other opcodes */ |
Chao Guan | 1d1ea1b | 2013-06-08 00:58:14 +0000 | [diff] [blame] | 284 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | return_ACPI_STATUS(AE_OK); |
| 292 | } |
| 293 | |
| 294 | /******************************************************************************* |
| 295 | * |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 296 | * FUNCTION: acpi_ps_link_module_code |
| 297 | * |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 298 | * PARAMETERS: parent_op - Parent parser op |
| 299 | * aml_start - Pointer to the AML |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 300 | * aml_length - Length of executable AML |
| 301 | * owner_id - owner_id of module level code |
| 302 | * |
| 303 | * RETURN: None. |
| 304 | * |
| 305 | * DESCRIPTION: Wrap the module-level code with a method object and link the |
| 306 | * object to the global list. Note, the mutex field of the method |
| 307 | * object is used to link multiple module-level code objects. |
| 308 | * |
| 309 | ******************************************************************************/ |
| 310 | |
| 311 | static void |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 312 | acpi_ps_link_module_code(union acpi_parse_object *parent_op, |
| 313 | u8 *aml_start, u32 aml_length, acpi_owner_id owner_id) |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 314 | { |
| 315 | union acpi_operand_object *prev; |
| 316 | union acpi_operand_object *next; |
| 317 | union acpi_operand_object *method_obj; |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 318 | struct acpi_namespace_node *parent_node; |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 319 | |
Bob Moore | 25823e7 | 2015-08-25 10:29:45 +0800 | [diff] [blame] | 320 | ACPI_FUNCTION_TRACE(ps_link_module_code); |
| 321 | |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 322 | /* Get the tail of the list */ |
| 323 | |
| 324 | prev = next = acpi_gbl_module_code_list; |
| 325 | while (next) { |
| 326 | prev = next; |
| 327 | next = next->method.mutex; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Insert the module level code into the list. Merge it if it is |
| 332 | * adjacent to the previous element. |
| 333 | */ |
| 334 | if (!prev || |
| 335 | ((prev->method.aml_start + prev->method.aml_length) != aml_start)) { |
| 336 | |
| 337 | /* Create, initialize, and link a new temporary method object */ |
| 338 | |
| 339 | method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD); |
| 340 | if (!method_obj) { |
Bob Moore | 25823e7 | 2015-08-25 10:29:45 +0800 | [diff] [blame] | 341 | return_VOID; |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 342 | } |
| 343 | |
Bob Moore | 25823e7 | 2015-08-25 10:29:45 +0800 | [diff] [blame] | 344 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
| 345 | "Create/Link new code block: %p\n", |
| 346 | method_obj)); |
| 347 | |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 348 | if (parent_op->common.node) { |
| 349 | parent_node = parent_op->common.node; |
| 350 | } else { |
| 351 | parent_node = acpi_gbl_root_node; |
| 352 | } |
| 353 | |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 354 | method_obj->method.aml_start = aml_start; |
| 355 | method_obj->method.aml_length = aml_length; |
| 356 | method_obj->method.owner_id = owner_id; |
Lin Ming | 2629484 | 2011-01-12 09:19:43 +0800 | [diff] [blame] | 357 | method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL; |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 358 | |
Lin Ming | 9a884ab | 2009-11-12 09:57:53 +0800 | [diff] [blame] | 359 | /* |
| 360 | * Save the parent node in next_object. This is cheating, but we |
| 361 | * don't want to expand the method object. |
| 362 | */ |
| 363 | method_obj->method.next_object = |
| 364 | ACPI_CAST_PTR(union acpi_operand_object, parent_node); |
| 365 | |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 366 | if (!prev) { |
| 367 | acpi_gbl_module_code_list = method_obj; |
| 368 | } else { |
| 369 | prev->method.mutex = method_obj; |
| 370 | } |
| 371 | } else { |
Bob Moore | 25823e7 | 2015-08-25 10:29:45 +0800 | [diff] [blame] | 372 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
| 373 | "Appending to existing code block: %p\n", |
| 374 | prev)); |
| 375 | |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 376 | prev->method.aml_length += aml_length; |
| 377 | } |
Bob Moore | 25823e7 | 2015-08-25 10:29:45 +0800 | [diff] [blame] | 378 | |
| 379 | return_VOID; |
Lin Ming | 7f0c826 | 2009-08-13 14:03:15 +0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /******************************************************************************* |
| 383 | * |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 384 | * FUNCTION: acpi_ps_parse_loop |
| 385 | * |
| 386 | * PARAMETERS: walk_state - Current state |
| 387 | * |
| 388 | * RETURN: Status |
| 389 | * |
| 390 | * DESCRIPTION: Parse AML (pointed to by the current parser state) and return |
| 391 | * a tree of ops. |
| 392 | * |
| 393 | ******************************************************************************/ |
| 394 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 395 | acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state) |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 396 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 397 | acpi_status status = AE_OK; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 398 | union acpi_parse_object *op = NULL; /* current op */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 399 | struct acpi_parse_state *parser_state; |
| 400 | u8 *aml_op_start = NULL; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 401 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 402 | ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 403 | |
| 404 | if (walk_state->descending_callback == NULL) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 405 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | parser_state = &walk_state->parser_state; |
| 409 | walk_state->arg_types = 0; |
| 410 | |
| 411 | #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY)) |
| 412 | |
| 413 | if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) { |
Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 414 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 415 | /* We are restarting a preempted control method */ |
| 416 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 417 | if (acpi_ps_has_completed_scope(parser_state)) { |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 418 | /* |
| 419 | * We must check if a predicate to an IF or WHILE statement |
| 420 | * was just completed |
| 421 | */ |
| 422 | if ((parser_state->scope->parse_scope.op) && |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 423 | ((parser_state->scope->parse_scope.op->common. |
| 424 | aml_opcode == AML_IF_OP) |
| 425 | || (parser_state->scope->parse_scope.op->common. |
| 426 | aml_opcode == AML_WHILE_OP)) |
| 427 | && (walk_state->control_state) |
| 428 | && (walk_state->control_state->common.state == |
| 429 | ACPI_CONTROL_PREDICATE_EXECUTING)) { |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 430 | /* |
| 431 | * A predicate was just completed, get the value of the |
| 432 | * predicate and branch based on that value |
| 433 | */ |
| 434 | walk_state->op = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 435 | status = |
| 436 | acpi_ds_get_predicate_value(walk_state, |
| 437 | ACPI_TO_POINTER |
| 438 | (TRUE)); |
| 439 | if (ACPI_FAILURE(status) |
| 440 | && ((status & AE_CODE_MASK) != |
| 441 | AE_CODE_CONTROL)) { |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 442 | if (status == AE_AML_NO_RETURN_VALUE) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 443 | ACPI_EXCEPTION((AE_INFO, status, |
| 444 | "Invoked method did not return a value")); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 445 | } |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 446 | |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 447 | ACPI_EXCEPTION((AE_INFO, status, |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 448 | "GetPredicate Failed")); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 449 | return_ACPI_STATUS(status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 452 | status = |
| 453 | acpi_ps_next_parse_state(walk_state, op, |
| 454 | status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 457 | acpi_ps_pop_scope(parser_state, &op, |
| 458 | &walk_state->arg_types, |
| 459 | &walk_state->arg_count); |
| 460 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
| 461 | "Popped scope, Op=%p\n", op)); |
| 462 | } else if (walk_state->prev_op) { |
Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 463 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 464 | /* We were in the middle of an op */ |
| 465 | |
| 466 | op = walk_state->prev_op; |
| 467 | walk_state->arg_types = walk_state->prev_arg_types; |
| 468 | } |
| 469 | } |
| 470 | #endif |
| 471 | |
| 472 | /* Iterative parsing loop, while there is more AML to process: */ |
| 473 | |
| 474 | while ((parser_state->aml < parser_state->aml_end) || (op)) { |
Bob Moore | 9cf7ade | 2017-04-28 08:53:22 +0800 | [diff] [blame] | 475 | ASL_CV_CAPTURE_COMMENTS(walk_state); |
| 476 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 477 | aml_op_start = parser_state->aml; |
| 478 | if (!op) { |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 479 | status = |
| 480 | acpi_ps_create_op(walk_state, aml_op_start, &op); |
| 481 | if (ACPI_FAILURE(status)) { |
| 482 | if (status == AE_CTRL_PARSE_CONTINUE) { |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 483 | continue; |
| 484 | } |
| 485 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 486 | if (status == AE_CTRL_PARSE_PENDING) { |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 487 | status = AE_OK; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Bob Moore | 22b5afc | 2014-03-24 14:49:00 +0800 | [diff] [blame] | 490 | if (status == AE_CTRL_TERMINATE) { |
| 491 | return_ACPI_STATUS(status); |
| 492 | } |
| 493 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 494 | status = |
| 495 | acpi_ps_complete_op(walk_state, &op, |
| 496 | status); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 497 | if (ACPI_FAILURE(status)) { |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 498 | return_ACPI_STATUS(status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 501 | continue; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 502 | } |
| 503 | |
Lv Zheng | ab6c573 | 2015-07-23 12:52:59 +0800 | [diff] [blame] | 504 | acpi_ex_start_trace_opcode(op, walk_state); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 507 | /* |
| 508 | * Start arg_count at zero because we don't know if there are |
| 509 | * any args yet |
| 510 | */ |
| 511 | walk_state->arg_count = 0; |
| 512 | |
Bob Moore | 9cf7ade | 2017-04-28 08:53:22 +0800 | [diff] [blame] | 513 | switch (op->common.aml_opcode) { |
| 514 | case AML_BYTE_OP: |
| 515 | case AML_WORD_OP: |
| 516 | case AML_DWORD_OP: |
| 517 | case AML_QWORD_OP: |
| 518 | |
| 519 | break; |
| 520 | |
| 521 | default: |
| 522 | |
| 523 | ASL_CV_CAPTURE_COMMENTS(walk_state); |
| 524 | break; |
| 525 | } |
| 526 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 527 | /* Are there any arguments that must be processed? */ |
| 528 | |
| 529 | if (walk_state->arg_types) { |
Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 530 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 531 | /* Get arguments */ |
| 532 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 533 | status = |
| 534 | acpi_ps_get_arguments(walk_state, aml_op_start, op); |
| 535 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 536 | status = |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 537 | acpi_ps_complete_op(walk_state, &op, |
| 538 | status); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 539 | if (ACPI_FAILURE(status)) { |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 540 | return_ACPI_STATUS(status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 543 | continue; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | /* Check for arguments that need to be processed */ |
| 548 | |
Bob Moore | a62a711 | 2017-08-03 14:27:22 +0800 | [diff] [blame] | 549 | ACPI_DEBUG_PRINT((ACPI_DB_PARSE, |
Bob Moore | 1ef6323 | 2018-02-15 13:09:28 -0800 | [diff] [blame] | 550 | "Parseloop: argument count: %8.8X\n", |
Bob Moore | a62a711 | 2017-08-03 14:27:22 +0800 | [diff] [blame] | 551 | walk_state->arg_count)); |
| 552 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 553 | if (walk_state->arg_count) { |
| 554 | /* |
| 555 | * There are arguments (complex ones), push Op and |
| 556 | * prepare for argument |
| 557 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 558 | status = acpi_ps_push_scope(parser_state, op, |
| 559 | walk_state->arg_types, |
| 560 | walk_state->arg_count); |
| 561 | if (ACPI_FAILURE(status)) { |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 562 | status = |
| 563 | acpi_ps_complete_op(walk_state, &op, |
| 564 | status); |
| 565 | if (ACPI_FAILURE(status)) { |
| 566 | return_ACPI_STATUS(status); |
| 567 | } |
| 568 | |
| 569 | continue; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 570 | } |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 571 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 572 | op = NULL; |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * All arguments have been processed -- Op is complete, |
| 578 | * prepare for next |
| 579 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 580 | walk_state->op_info = |
| 581 | acpi_ps_get_opcode_info(op->common.aml_opcode); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 582 | if (walk_state->op_info->flags & AML_NAMED) { |
Lin Ming | 941f48b | 2008-04-10 19:06:41 +0400 | [diff] [blame] | 583 | if (op->common.aml_opcode == AML_REGION_OP || |
| 584 | op->common.aml_opcode == AML_DATA_REGION_OP) { |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 585 | /* |
| 586 | * Skip parsing of control method or opregion body, |
| 587 | * because we don't have enough info in the first pass |
| 588 | * to parse them correctly. |
| 589 | * |
| 590 | * Completed parsing an op_region declaration, we now |
| 591 | * know the length. |
| 592 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 593 | op->named.length = |
| 594 | (u32) (parser_state->aml - op->named.data); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | if (walk_state->op_info->flags & AML_CREATE) { |
| 599 | /* |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 600 | * Backup to beginning of create_XXXfield declaration (1 for |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 601 | * Opcode) |
| 602 | * |
| 603 | * body_length is unknown until we parse the body |
| 604 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 605 | op->named.length = |
| 606 | (u32) (parser_state->aml - op->named.data); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 607 | } |
| 608 | |
Lin Ming | ef805d9 | 2008-04-10 19:06:41 +0400 | [diff] [blame] | 609 | if (op->common.aml_opcode == AML_BANK_FIELD_OP) { |
| 610 | /* |
| 611 | * Backup to beginning of bank_field declaration |
| 612 | * |
| 613 | * body_length is unknown until we parse the body |
| 614 | */ |
| 615 | op->named.length = |
| 616 | (u32) (parser_state->aml - op->named.data); |
| 617 | } |
| 618 | |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 619 | /* This op complete, notify the dispatcher */ |
| 620 | |
| 621 | if (walk_state->ascending_callback != NULL) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 622 | walk_state->op = op; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 623 | walk_state->opcode = op->common.aml_opcode; |
| 624 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 625 | status = walk_state->ascending_callback(walk_state); |
| 626 | status = |
| 627 | acpi_ps_next_parse_state(walk_state, op, status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 628 | if (status == AE_CTRL_PENDING) { |
| 629 | status = AE_OK; |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 633 | status = acpi_ps_complete_op(walk_state, &op, status); |
| 634 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 635 | return_ACPI_STATUS(status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 636 | } |
| 637 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 638 | } /* while parser_state->Aml */ |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 639 | |
Mikhail Kouzmich | 4d0b4af | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 640 | status = acpi_ps_complete_final_op(walk_state, op, status); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 641 | return_ACPI_STATUS(status); |
Robert Moore | 73459f7 | 2005-06-24 00:00:00 -0400 | [diff] [blame] | 642 | } |