psloop.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /******************************************************************************
  2. *
  3. * Module Name: psloop - Main AML parse loop
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. /*
  43. * Parse the AML and build an operation tree as most interpreters, (such as
  44. * Perl) do. Parsing is done by hand rather than with a YACC generated parser
  45. * to tightly constrain stack and dynamic memory usage. Parsing is kept
  46. * flexible and the code fairly compact by parsing based on a list of AML
  47. * opcode templates in aml_op_info[].
  48. */
  49. #include <acpi/acpi.h>
  50. #include "accommon.h"
  51. #include "acinterp.h"
  52. #include "acparser.h"
  53. #include "acdispat.h"
  54. #include "amlcode.h"
  55. #include "acconvert.h"
  56. #define _COMPONENT ACPI_PARSER
  57. ACPI_MODULE_NAME("psloop")
  58. /* Local prototypes */
  59. static acpi_status
  60. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  61. u8 * aml_op_start, union acpi_parse_object *op);
  62. static void
  63. acpi_ps_link_module_code(union acpi_parse_object *parent_op,
  64. u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
  65. /*******************************************************************************
  66. *
  67. * FUNCTION: acpi_ps_get_arguments
  68. *
  69. * PARAMETERS: walk_state - Current state
  70. * aml_op_start - Op start in AML
  71. * op - Current Op
  72. *
  73. * RETURN: Status
  74. *
  75. * DESCRIPTION: Get arguments for passed Op.
  76. *
  77. ******************************************************************************/
  78. static acpi_status
  79. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  80. u8 * aml_op_start, union acpi_parse_object *op)
  81. {
  82. acpi_status status = AE_OK;
  83. union acpi_parse_object *arg = NULL;
  84. const struct acpi_opcode_info *op_info;
  85. ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
  86. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  87. "Get arguments for opcode [%s]\n",
  88. op->common.aml_op_name));
  89. switch (op->common.aml_opcode) {
  90. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  91. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  92. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  93. case AML_QWORD_OP: /* AML_QWORDATA_ARG */
  94. case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
  95. /* Fill in constant or string argument directly */
  96. acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
  97. GET_CURRENT_ARG_TYPE(walk_state->
  98. arg_types),
  99. op);
  100. break;
  101. case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
  102. status = acpi_ps_get_next_namepath(walk_state,
  103. &(walk_state->parser_state),
  104. op,
  105. ACPI_POSSIBLE_METHOD_CALL);
  106. if (ACPI_FAILURE(status)) {
  107. return_ACPI_STATUS(status);
  108. }
  109. walk_state->arg_types = 0;
  110. break;
  111. default:
  112. /*
  113. * Op is not a constant or string, append each argument to the Op
  114. */
  115. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
  116. !walk_state->arg_count) {
  117. walk_state->aml = walk_state->parser_state.aml;
  118. switch (op->common.aml_opcode) {
  119. case AML_METHOD_OP:
  120. case AML_BUFFER_OP:
  121. case AML_PACKAGE_OP:
  122. case AML_VARIABLE_PACKAGE_OP:
  123. case AML_WHILE_OP:
  124. break;
  125. default:
  126. ASL_CV_CAPTURE_COMMENTS(walk_state);
  127. break;
  128. }
  129. status =
  130. acpi_ps_get_next_arg(walk_state,
  131. &(walk_state->parser_state),
  132. GET_CURRENT_ARG_TYPE
  133. (walk_state->arg_types), &arg);
  134. if (ACPI_FAILURE(status)) {
  135. return_ACPI_STATUS(status);
  136. }
  137. if (arg) {
  138. acpi_ps_append_arg(op, arg);
  139. }
  140. INCREMENT_ARG_LIST(walk_state->arg_types);
  141. }
  142. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  143. "Final argument count: %u pass %u\n",
  144. walk_state->arg_count,
  145. walk_state->pass_number));
  146. /*
  147. * Handle executable code at "module-level". This refers to
  148. * executable opcodes that appear outside of any control method.
  149. */
  150. if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
  151. ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
  152. /*
  153. * We want to skip If/Else/While constructs during Pass1 because we
  154. * want to actually conditionally execute the code during Pass2.
  155. *
  156. * Except for disassembly, where we always want to walk the
  157. * If/Else/While packages
  158. */
  159. switch (op->common.aml_opcode) {
  160. case AML_IF_OP:
  161. case AML_ELSE_OP:
  162. case AML_WHILE_OP:
  163. /*
  164. * Currently supported module-level opcodes are:
  165. * IF/ELSE/WHILE. These appear to be the most common,
  166. * and easiest to support since they open an AML
  167. * package.
  168. */
  169. if (walk_state->pass_number ==
  170. ACPI_IMODE_LOAD_PASS1) {
  171. acpi_ps_link_module_code(op->common.
  172. parent,
  173. aml_op_start,
  174. (u32)
  175. (walk_state->
  176. parser_state.
  177. pkg_end -
  178. aml_op_start),
  179. walk_state->
  180. owner_id);
  181. }
  182. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  183. "Pass1: Skipping an If/Else/While body\n"));
  184. /* Skip body of if/else/while in pass 1 */
  185. walk_state->parser_state.aml =
  186. walk_state->parser_state.pkg_end;
  187. walk_state->arg_count = 0;
  188. break;
  189. default:
  190. /*
  191. * Check for an unsupported executable opcode at module
  192. * level. We must be in PASS1, the parent must be a SCOPE,
  193. * The opcode class must be EXECUTE, and the opcode must
  194. * not be an argument to another opcode.
  195. */
  196. if ((walk_state->pass_number ==
  197. ACPI_IMODE_LOAD_PASS1)
  198. && (op->common.parent->common.aml_opcode ==
  199. AML_SCOPE_OP)) {
  200. op_info =
  201. acpi_ps_get_opcode_info(op->common.
  202. aml_opcode);
  203. if ((op_info->class ==
  204. AML_CLASS_EXECUTE) && (!arg)) {
  205. ACPI_WARNING((AE_INFO,
  206. "Unsupported module-level executable opcode "
  207. "0x%.2X at table offset 0x%.4X",
  208. op->common.
  209. aml_opcode,
  210. (u32)
  211. (ACPI_PTR_DIFF
  212. (aml_op_start,
  213. walk_state->
  214. parser_state.
  215. aml_start) +
  216. sizeof(struct
  217. acpi_table_header))));
  218. }
  219. }
  220. break;
  221. }
  222. }
  223. /* Special processing for certain opcodes */
  224. switch (op->common.aml_opcode) {
  225. case AML_METHOD_OP:
  226. /*
  227. * Skip parsing of control method because we don't have enough
  228. * info in the first pass to parse it correctly.
  229. *
  230. * Save the length and address of the body
  231. */
  232. op->named.data = walk_state->parser_state.aml;
  233. op->named.length = (u32)
  234. (walk_state->parser_state.pkg_end -
  235. walk_state->parser_state.aml);
  236. /* Skip body of method */
  237. walk_state->parser_state.aml =
  238. walk_state->parser_state.pkg_end;
  239. walk_state->arg_count = 0;
  240. break;
  241. case AML_BUFFER_OP:
  242. case AML_PACKAGE_OP:
  243. case AML_VARIABLE_PACKAGE_OP:
  244. if ((op->common.parent) &&
  245. (op->common.parent->common.aml_opcode ==
  246. AML_NAME_OP)
  247. && (walk_state->pass_number <=
  248. ACPI_IMODE_LOAD_PASS2)) {
  249. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  250. "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
  251. walk_state->pass_number,
  252. aml_op_start));
  253. /*
  254. * Skip parsing of Buffers and Packages because we don't have
  255. * enough info in the first pass to parse them correctly.
  256. */
  257. op->named.data = aml_op_start;
  258. op->named.length = (u32)
  259. (walk_state->parser_state.pkg_end -
  260. aml_op_start);
  261. /* Skip body */
  262. walk_state->parser_state.aml =
  263. walk_state->parser_state.pkg_end;
  264. walk_state->arg_count = 0;
  265. }
  266. break;
  267. case AML_WHILE_OP:
  268. if (walk_state->control_state) {
  269. walk_state->control_state->control.package_end =
  270. walk_state->parser_state.pkg_end;
  271. }
  272. break;
  273. default:
  274. /* No action for all other opcodes */
  275. break;
  276. }
  277. break;
  278. }
  279. return_ACPI_STATUS(AE_OK);
  280. }
  281. /*******************************************************************************
  282. *
  283. * FUNCTION: acpi_ps_link_module_code
  284. *
  285. * PARAMETERS: parent_op - Parent parser op
  286. * aml_start - Pointer to the AML
  287. * aml_length - Length of executable AML
  288. * owner_id - owner_id of module level code
  289. *
  290. * RETURN: None.
  291. *
  292. * DESCRIPTION: Wrap the module-level code with a method object and link the
  293. * object to the global list. Note, the mutex field of the method
  294. * object is used to link multiple module-level code objects.
  295. *
  296. ******************************************************************************/
  297. static void
  298. acpi_ps_link_module_code(union acpi_parse_object *parent_op,
  299. u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
  300. {
  301. union acpi_operand_object *prev;
  302. union acpi_operand_object *next;
  303. union acpi_operand_object *method_obj;
  304. struct acpi_namespace_node *parent_node;
  305. ACPI_FUNCTION_TRACE(ps_link_module_code);
  306. /* Get the tail of the list */
  307. prev = next = acpi_gbl_module_code_list;
  308. while (next) {
  309. prev = next;
  310. next = next->method.mutex;
  311. }
  312. /*
  313. * Insert the module level code into the list. Merge it if it is
  314. * adjacent to the previous element.
  315. */
  316. if (!prev ||
  317. ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
  318. /* Create, initialize, and link a new temporary method object */
  319. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  320. if (!method_obj) {
  321. return_VOID;
  322. }
  323. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  324. "Create/Link new code block: %p\n",
  325. method_obj));
  326. if (parent_op->common.node) {
  327. parent_node = parent_op->common.node;
  328. } else {
  329. parent_node = acpi_gbl_root_node;
  330. }
  331. method_obj->method.aml_start = aml_start;
  332. method_obj->method.aml_length = aml_length;
  333. method_obj->method.owner_id = owner_id;
  334. method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
  335. /*
  336. * Save the parent node in next_object. This is cheating, but we
  337. * don't want to expand the method object.
  338. */
  339. method_obj->method.next_object =
  340. ACPI_CAST_PTR(union acpi_operand_object, parent_node);
  341. if (!prev) {
  342. acpi_gbl_module_code_list = method_obj;
  343. } else {
  344. prev->method.mutex = method_obj;
  345. }
  346. } else {
  347. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  348. "Appending to existing code block: %p\n",
  349. prev));
  350. prev->method.aml_length += aml_length;
  351. }
  352. return_VOID;
  353. }
  354. /*******************************************************************************
  355. *
  356. * FUNCTION: acpi_ps_parse_loop
  357. *
  358. * PARAMETERS: walk_state - Current state
  359. *
  360. * RETURN: Status
  361. *
  362. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  363. * a tree of ops.
  364. *
  365. ******************************************************************************/
  366. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  367. {
  368. acpi_status status = AE_OK;
  369. union acpi_parse_object *op = NULL; /* current op */
  370. struct acpi_parse_state *parser_state;
  371. u8 *aml_op_start = NULL;
  372. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  373. if (walk_state->descending_callback == NULL) {
  374. return_ACPI_STATUS(AE_BAD_PARAMETER);
  375. }
  376. parser_state = &walk_state->parser_state;
  377. walk_state->arg_types = 0;
  378. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  379. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  380. /* We are restarting a preempted control method */
  381. if (acpi_ps_has_completed_scope(parser_state)) {
  382. /*
  383. * We must check if a predicate to an IF or WHILE statement
  384. * was just completed
  385. */
  386. if ((parser_state->scope->parse_scope.op) &&
  387. ((parser_state->scope->parse_scope.op->common.
  388. aml_opcode == AML_IF_OP)
  389. || (parser_state->scope->parse_scope.op->common.
  390. aml_opcode == AML_WHILE_OP))
  391. && (walk_state->control_state)
  392. && (walk_state->control_state->common.state ==
  393. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  394. /*
  395. * A predicate was just completed, get the value of the
  396. * predicate and branch based on that value
  397. */
  398. walk_state->op = NULL;
  399. status =
  400. acpi_ds_get_predicate_value(walk_state,
  401. ACPI_TO_POINTER
  402. (TRUE));
  403. if (ACPI_FAILURE(status)
  404. && ((status & AE_CODE_MASK) !=
  405. AE_CODE_CONTROL)) {
  406. if (status == AE_AML_NO_RETURN_VALUE) {
  407. ACPI_EXCEPTION((AE_INFO, status,
  408. "Invoked method did not return a value"));
  409. }
  410. ACPI_EXCEPTION((AE_INFO, status,
  411. "GetPredicate Failed"));
  412. return_ACPI_STATUS(status);
  413. }
  414. status =
  415. acpi_ps_next_parse_state(walk_state, op,
  416. status);
  417. }
  418. acpi_ps_pop_scope(parser_state, &op,
  419. &walk_state->arg_types,
  420. &walk_state->arg_count);
  421. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  422. "Popped scope, Op=%p\n", op));
  423. } else if (walk_state->prev_op) {
  424. /* We were in the middle of an op */
  425. op = walk_state->prev_op;
  426. walk_state->arg_types = walk_state->prev_arg_types;
  427. }
  428. }
  429. #endif
  430. /* Iterative parsing loop, while there is more AML to process: */
  431. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  432. ASL_CV_CAPTURE_COMMENTS(walk_state);
  433. aml_op_start = parser_state->aml;
  434. if (!op) {
  435. status =
  436. acpi_ps_create_op(walk_state, aml_op_start, &op);
  437. if (ACPI_FAILURE(status)) {
  438. if (status == AE_CTRL_PARSE_CONTINUE) {
  439. continue;
  440. }
  441. if (status == AE_CTRL_PARSE_PENDING) {
  442. status = AE_OK;
  443. }
  444. if (status == AE_CTRL_TERMINATE) {
  445. return_ACPI_STATUS(status);
  446. }
  447. status =
  448. acpi_ps_complete_op(walk_state, &op,
  449. status);
  450. if (ACPI_FAILURE(status)) {
  451. return_ACPI_STATUS(status);
  452. }
  453. continue;
  454. }
  455. acpi_ex_start_trace_opcode(op, walk_state);
  456. }
  457. /*
  458. * Start arg_count at zero because we don't know if there are
  459. * any args yet
  460. */
  461. walk_state->arg_count = 0;
  462. switch (op->common.aml_opcode) {
  463. case AML_BYTE_OP:
  464. case AML_WORD_OP:
  465. case AML_DWORD_OP:
  466. case AML_QWORD_OP:
  467. break;
  468. default:
  469. ASL_CV_CAPTURE_COMMENTS(walk_state);
  470. break;
  471. }
  472. /* Are there any arguments that must be processed? */
  473. if (walk_state->arg_types) {
  474. /* Get arguments */
  475. status =
  476. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  477. if (ACPI_FAILURE(status)) {
  478. status =
  479. acpi_ps_complete_op(walk_state, &op,
  480. status);
  481. if (ACPI_FAILURE(status)) {
  482. return_ACPI_STATUS(status);
  483. }
  484. continue;
  485. }
  486. }
  487. /* Check for arguments that need to be processed */
  488. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  489. "Parseloop: argument count: %u\n",
  490. walk_state->arg_count));
  491. if (walk_state->arg_count) {
  492. /*
  493. * There are arguments (complex ones), push Op and
  494. * prepare for argument
  495. */
  496. status = acpi_ps_push_scope(parser_state, op,
  497. walk_state->arg_types,
  498. walk_state->arg_count);
  499. if (ACPI_FAILURE(status)) {
  500. status =
  501. acpi_ps_complete_op(walk_state, &op,
  502. status);
  503. if (ACPI_FAILURE(status)) {
  504. return_ACPI_STATUS(status);
  505. }
  506. continue;
  507. }
  508. op = NULL;
  509. continue;
  510. }
  511. /*
  512. * All arguments have been processed -- Op is complete,
  513. * prepare for next
  514. */
  515. walk_state->op_info =
  516. acpi_ps_get_opcode_info(op->common.aml_opcode);
  517. if (walk_state->op_info->flags & AML_NAMED) {
  518. if (op->common.aml_opcode == AML_REGION_OP ||
  519. op->common.aml_opcode == AML_DATA_REGION_OP) {
  520. /*
  521. * Skip parsing of control method or opregion body,
  522. * because we don't have enough info in the first pass
  523. * to parse them correctly.
  524. *
  525. * Completed parsing an op_region declaration, we now
  526. * know the length.
  527. */
  528. op->named.length =
  529. (u32) (parser_state->aml - op->named.data);
  530. }
  531. }
  532. if (walk_state->op_info->flags & AML_CREATE) {
  533. /*
  534. * Backup to beginning of create_XXXfield declaration (1 for
  535. * Opcode)
  536. *
  537. * body_length is unknown until we parse the body
  538. */
  539. op->named.length =
  540. (u32) (parser_state->aml - op->named.data);
  541. }
  542. if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
  543. /*
  544. * Backup to beginning of bank_field declaration
  545. *
  546. * body_length is unknown until we parse the body
  547. */
  548. op->named.length =
  549. (u32) (parser_state->aml - op->named.data);
  550. }
  551. /* This op complete, notify the dispatcher */
  552. if (walk_state->ascending_callback != NULL) {
  553. walk_state->op = op;
  554. walk_state->opcode = op->common.aml_opcode;
  555. status = walk_state->ascending_callback(walk_state);
  556. status =
  557. acpi_ps_next_parse_state(walk_state, op, status);
  558. if (status == AE_CTRL_PENDING) {
  559. status = AE_OK;
  560. }
  561. }
  562. status = acpi_ps_complete_op(walk_state, &op, status);
  563. if (ACPI_FAILURE(status)) {
  564. return_ACPI_STATUS(status);
  565. }
  566. } /* while parser_state->Aml */
  567. status = acpi_ps_complete_final_op(walk_state, op, status);
  568. return_ACPI_STATUS(status);
  569. }