psloop.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. /*
  143. * Handle executable code at "module-level". This refers to
  144. * executable opcodes that appear outside of any control method.
  145. */
  146. if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
  147. ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
  148. /*
  149. * We want to skip If/Else/While constructs during Pass1 because we
  150. * want to actually conditionally execute the code during Pass2.
  151. *
  152. * Except for disassembly, where we always want to walk the
  153. * If/Else/While packages
  154. */
  155. switch (op->common.aml_opcode) {
  156. case AML_IF_OP:
  157. case AML_ELSE_OP:
  158. case AML_WHILE_OP:
  159. /*
  160. * Currently supported module-level opcodes are:
  161. * IF/ELSE/WHILE. These appear to be the most common,
  162. * and easiest to support since they open an AML
  163. * package.
  164. */
  165. if (walk_state->pass_number ==
  166. ACPI_IMODE_LOAD_PASS1) {
  167. acpi_ps_link_module_code(op->common.
  168. parent,
  169. aml_op_start,
  170. (u32)
  171. (walk_state->
  172. parser_state.
  173. pkg_end -
  174. aml_op_start),
  175. walk_state->
  176. owner_id);
  177. }
  178. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  179. "Pass1: Skipping an If/Else/While body\n"));
  180. /* Skip body of if/else/while in pass 1 */
  181. walk_state->parser_state.aml =
  182. walk_state->parser_state.pkg_end;
  183. walk_state->arg_count = 0;
  184. break;
  185. default:
  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,
  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))));
  214. }
  215. }
  216. break;
  217. }
  218. }
  219. /* Special processing for certain opcodes */
  220. switch (op->common.aml_opcode) {
  221. case AML_METHOD_OP:
  222. /*
  223. * Skip parsing of control method because we don't have enough
  224. * info in the first pass to parse it correctly.
  225. *
  226. * Save the length and address of the body
  227. */
  228. op->named.data = walk_state->parser_state.aml;
  229. op->named.length = (u32)
  230. (walk_state->parser_state.pkg_end -
  231. walk_state->parser_state.aml);
  232. /* Skip body of method */
  233. walk_state->parser_state.aml =
  234. walk_state->parser_state.pkg_end;
  235. walk_state->arg_count = 0;
  236. break;
  237. case AML_BUFFER_OP:
  238. case AML_PACKAGE_OP:
  239. case AML_VARIABLE_PACKAGE_OP:
  240. if ((op->common.parent) &&
  241. (op->common.parent->common.aml_opcode ==
  242. AML_NAME_OP)
  243. && (walk_state->pass_number <=
  244. ACPI_IMODE_LOAD_PASS2)) {
  245. /*
  246. * Skip parsing of Buffers and Packages because we don't have
  247. * enough info in the first pass to parse them correctly.
  248. */
  249. op->named.data = aml_op_start;
  250. op->named.length = (u32)
  251. (walk_state->parser_state.pkg_end -
  252. aml_op_start);
  253. /* Skip body */
  254. walk_state->parser_state.aml =
  255. walk_state->parser_state.pkg_end;
  256. walk_state->arg_count = 0;
  257. }
  258. break;
  259. case AML_WHILE_OP:
  260. if (walk_state->control_state) {
  261. walk_state->control_state->control.package_end =
  262. walk_state->parser_state.pkg_end;
  263. }
  264. break;
  265. default:
  266. /* No action for all other opcodes */
  267. break;
  268. }
  269. break;
  270. }
  271. return_ACPI_STATUS(AE_OK);
  272. }
  273. /*******************************************************************************
  274. *
  275. * FUNCTION: acpi_ps_link_module_code
  276. *
  277. * PARAMETERS: parent_op - Parent parser op
  278. * aml_start - Pointer to the AML
  279. * aml_length - Length of executable AML
  280. * owner_id - owner_id of module level code
  281. *
  282. * RETURN: None.
  283. *
  284. * DESCRIPTION: Wrap the module-level code with a method object and link the
  285. * object to the global list. Note, the mutex field of the method
  286. * object is used to link multiple module-level code objects.
  287. *
  288. ******************************************************************************/
  289. static void
  290. acpi_ps_link_module_code(union acpi_parse_object *parent_op,
  291. u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
  292. {
  293. union acpi_operand_object *prev;
  294. union acpi_operand_object *next;
  295. union acpi_operand_object *method_obj;
  296. struct acpi_namespace_node *parent_node;
  297. ACPI_FUNCTION_TRACE(ps_link_module_code);
  298. /* Get the tail of the list */
  299. prev = next = acpi_gbl_module_code_list;
  300. while (next) {
  301. prev = next;
  302. next = next->method.mutex;
  303. }
  304. /*
  305. * Insert the module level code into the list. Merge it if it is
  306. * adjacent to the previous element.
  307. */
  308. if (!prev ||
  309. ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
  310. /* Create, initialize, and link a new temporary method object */
  311. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  312. if (!method_obj) {
  313. return_VOID;
  314. }
  315. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  316. "Create/Link new code block: %p\n",
  317. method_obj));
  318. if (parent_op->common.node) {
  319. parent_node = parent_op->common.node;
  320. } else {
  321. parent_node = acpi_gbl_root_node;
  322. }
  323. method_obj->method.aml_start = aml_start;
  324. method_obj->method.aml_length = aml_length;
  325. method_obj->method.owner_id = owner_id;
  326. method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
  327. /*
  328. * Save the parent node in next_object. This is cheating, but we
  329. * don't want to expand the method object.
  330. */
  331. method_obj->method.next_object =
  332. ACPI_CAST_PTR(union acpi_operand_object, parent_node);
  333. if (!prev) {
  334. acpi_gbl_module_code_list = method_obj;
  335. } else {
  336. prev->method.mutex = method_obj;
  337. }
  338. } else {
  339. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  340. "Appending to existing code block: %p\n",
  341. prev));
  342. prev->method.aml_length += aml_length;
  343. }
  344. return_VOID;
  345. }
  346. /*******************************************************************************
  347. *
  348. * FUNCTION: acpi_ps_parse_loop
  349. *
  350. * PARAMETERS: walk_state - Current state
  351. *
  352. * RETURN: Status
  353. *
  354. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  355. * a tree of ops.
  356. *
  357. ******************************************************************************/
  358. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  359. {
  360. acpi_status status = AE_OK;
  361. union acpi_parse_object *op = NULL; /* current op */
  362. struct acpi_parse_state *parser_state;
  363. u8 *aml_op_start = NULL;
  364. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  365. if (walk_state->descending_callback == NULL) {
  366. return_ACPI_STATUS(AE_BAD_PARAMETER);
  367. }
  368. parser_state = &walk_state->parser_state;
  369. walk_state->arg_types = 0;
  370. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  371. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  372. /* We are restarting a preempted control method */
  373. if (acpi_ps_has_completed_scope(parser_state)) {
  374. /*
  375. * We must check if a predicate to an IF or WHILE statement
  376. * was just completed
  377. */
  378. if ((parser_state->scope->parse_scope.op) &&
  379. ((parser_state->scope->parse_scope.op->common.
  380. aml_opcode == AML_IF_OP)
  381. || (parser_state->scope->parse_scope.op->common.
  382. aml_opcode == AML_WHILE_OP))
  383. && (walk_state->control_state)
  384. && (walk_state->control_state->common.state ==
  385. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  386. /*
  387. * A predicate was just completed, get the value of the
  388. * predicate and branch based on that value
  389. */
  390. walk_state->op = NULL;
  391. status =
  392. acpi_ds_get_predicate_value(walk_state,
  393. ACPI_TO_POINTER
  394. (TRUE));
  395. if (ACPI_FAILURE(status)
  396. && ((status & AE_CODE_MASK) !=
  397. AE_CODE_CONTROL)) {
  398. if (status == AE_AML_NO_RETURN_VALUE) {
  399. ACPI_EXCEPTION((AE_INFO, status,
  400. "Invoked method did not return a value"));
  401. }
  402. ACPI_EXCEPTION((AE_INFO, status,
  403. "GetPredicate Failed"));
  404. return_ACPI_STATUS(status);
  405. }
  406. status =
  407. acpi_ps_next_parse_state(walk_state, op,
  408. status);
  409. }
  410. acpi_ps_pop_scope(parser_state, &op,
  411. &walk_state->arg_types,
  412. &walk_state->arg_count);
  413. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  414. "Popped scope, Op=%p\n", op));
  415. } else if (walk_state->prev_op) {
  416. /* We were in the middle of an op */
  417. op = walk_state->prev_op;
  418. walk_state->arg_types = walk_state->prev_arg_types;
  419. }
  420. }
  421. #endif
  422. /* Iterative parsing loop, while there is more AML to process: */
  423. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  424. ASL_CV_CAPTURE_COMMENTS(walk_state);
  425. aml_op_start = parser_state->aml;
  426. if (!op) {
  427. status =
  428. acpi_ps_create_op(walk_state, aml_op_start, &op);
  429. if (ACPI_FAILURE(status)) {
  430. if (status == AE_CTRL_PARSE_CONTINUE) {
  431. continue;
  432. }
  433. if (status == AE_CTRL_PARSE_PENDING) {
  434. status = AE_OK;
  435. }
  436. if (status == AE_CTRL_TERMINATE) {
  437. return_ACPI_STATUS(status);
  438. }
  439. status =
  440. acpi_ps_complete_op(walk_state, &op,
  441. status);
  442. if (ACPI_FAILURE(status)) {
  443. return_ACPI_STATUS(status);
  444. }
  445. continue;
  446. }
  447. acpi_ex_start_trace_opcode(op, walk_state);
  448. }
  449. /*
  450. * Start arg_count at zero because we don't know if there are
  451. * any args yet
  452. */
  453. walk_state->arg_count = 0;
  454. switch (op->common.aml_opcode) {
  455. case AML_BYTE_OP:
  456. case AML_WORD_OP:
  457. case AML_DWORD_OP:
  458. case AML_QWORD_OP:
  459. break;
  460. default:
  461. ASL_CV_CAPTURE_COMMENTS(walk_state);
  462. break;
  463. }
  464. /* Are there any arguments that must be processed? */
  465. if (walk_state->arg_types) {
  466. /* Get arguments */
  467. status =
  468. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  469. if (ACPI_FAILURE(status)) {
  470. status =
  471. acpi_ps_complete_op(walk_state, &op,
  472. status);
  473. if (ACPI_FAILURE(status)) {
  474. return_ACPI_STATUS(status);
  475. }
  476. continue;
  477. }
  478. }
  479. /* Check for arguments that need to be processed */
  480. if (walk_state->arg_count) {
  481. /*
  482. * There are arguments (complex ones), push Op and
  483. * prepare for argument
  484. */
  485. status = acpi_ps_push_scope(parser_state, op,
  486. walk_state->arg_types,
  487. walk_state->arg_count);
  488. if (ACPI_FAILURE(status)) {
  489. status =
  490. acpi_ps_complete_op(walk_state, &op,
  491. status);
  492. if (ACPI_FAILURE(status)) {
  493. return_ACPI_STATUS(status);
  494. }
  495. continue;
  496. }
  497. op = NULL;
  498. continue;
  499. }
  500. /*
  501. * All arguments have been processed -- Op is complete,
  502. * prepare for next
  503. */
  504. walk_state->op_info =
  505. acpi_ps_get_opcode_info(op->common.aml_opcode);
  506. if (walk_state->op_info->flags & AML_NAMED) {
  507. if (op->common.aml_opcode == AML_REGION_OP ||
  508. op->common.aml_opcode == AML_DATA_REGION_OP) {
  509. /*
  510. * Skip parsing of control method or opregion body,
  511. * because we don't have enough info in the first pass
  512. * to parse them correctly.
  513. *
  514. * Completed parsing an op_region declaration, we now
  515. * know the length.
  516. */
  517. op->named.length =
  518. (u32) (parser_state->aml - op->named.data);
  519. }
  520. }
  521. if (walk_state->op_info->flags & AML_CREATE) {
  522. /*
  523. * Backup to beginning of create_XXXfield declaration (1 for
  524. * Opcode)
  525. *
  526. * body_length is unknown until we parse the body
  527. */
  528. op->named.length =
  529. (u32) (parser_state->aml - op->named.data);
  530. }
  531. if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
  532. /*
  533. * Backup to beginning of bank_field declaration
  534. *
  535. * body_length is unknown until we parse the body
  536. */
  537. op->named.length =
  538. (u32) (parser_state->aml - op->named.data);
  539. }
  540. /* This op complete, notify the dispatcher */
  541. if (walk_state->ascending_callback != NULL) {
  542. walk_state->op = op;
  543. walk_state->opcode = op->common.aml_opcode;
  544. status = walk_state->ascending_callback(walk_state);
  545. status =
  546. acpi_ps_next_parse_state(walk_state, op, status);
  547. if (status == AE_CTRL_PENDING) {
  548. status = AE_OK;
  549. }
  550. }
  551. status = acpi_ps_complete_op(walk_state, &op, status);
  552. if (ACPI_FAILURE(status)) {
  553. return_ACPI_STATUS(status);
  554. }
  555. } /* while parser_state->Aml */
  556. status = acpi_ps_complete_final_op(walk_state, op, status);
  557. return_ACPI_STATUS(status);
  558. }