psloop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #define _COMPONENT ACPI_PARSER
  56. ACPI_MODULE_NAME("psloop")
  57. /* Local prototypes */
  58. static acpi_status
  59. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  60. u8 * aml_op_start, union acpi_parse_object *op);
  61. static void
  62. acpi_ps_link_module_code(union acpi_parse_object *parent_op,
  63. u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
  64. /*******************************************************************************
  65. *
  66. * FUNCTION: acpi_ps_get_arguments
  67. *
  68. * PARAMETERS: walk_state - Current state
  69. * aml_op_start - Op start in AML
  70. * op - Current Op
  71. *
  72. * RETURN: Status
  73. *
  74. * DESCRIPTION: Get arguments for passed Op.
  75. *
  76. ******************************************************************************/
  77. static acpi_status
  78. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  79. u8 * aml_op_start, union acpi_parse_object *op)
  80. {
  81. acpi_status status = AE_OK;
  82. union acpi_parse_object *arg = NULL;
  83. const struct acpi_opcode_info *op_info;
  84. ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
  85. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  86. "Get arguments for opcode [%s]\n",
  87. op->common.aml_op_name));
  88. switch (op->common.aml_opcode) {
  89. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  90. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  91. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  92. case AML_QWORD_OP: /* AML_QWORDATA_ARG */
  93. case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
  94. /* Fill in constant or string argument directly */
  95. acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
  96. GET_CURRENT_ARG_TYPE(walk_state->
  97. arg_types),
  98. op);
  99. break;
  100. case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
  101. status = acpi_ps_get_next_namepath(walk_state,
  102. &(walk_state->parser_state),
  103. op,
  104. ACPI_POSSIBLE_METHOD_CALL);
  105. if (ACPI_FAILURE(status)) {
  106. return_ACPI_STATUS(status);
  107. }
  108. walk_state->arg_types = 0;
  109. break;
  110. default:
  111. /*
  112. * Op is not a constant or string, append each argument to the Op
  113. */
  114. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
  115. !walk_state->arg_count) {
  116. walk_state->aml = walk_state->parser_state.aml;
  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. if (arg) {
  126. acpi_ps_append_arg(op, arg);
  127. }
  128. INCREMENT_ARG_LIST(walk_state->arg_types);
  129. }
  130. /*
  131. * Handle executable code at "module-level". This refers to
  132. * executable opcodes that appear outside of any control method.
  133. */
  134. if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
  135. ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
  136. /*
  137. * We want to skip If/Else/While constructs during Pass1 because we
  138. * want to actually conditionally execute the code during Pass2.
  139. *
  140. * Except for disassembly, where we always want to walk the
  141. * If/Else/While packages
  142. */
  143. switch (op->common.aml_opcode) {
  144. case AML_IF_OP:
  145. case AML_ELSE_OP:
  146. case AML_WHILE_OP:
  147. /*
  148. * Currently supported module-level opcodes are:
  149. * IF/ELSE/WHILE. These appear to be the most common,
  150. * and easiest to support since they open an AML
  151. * package.
  152. */
  153. if (walk_state->pass_number ==
  154. ACPI_IMODE_LOAD_PASS1) {
  155. acpi_ps_link_module_code(op->common.
  156. parent,
  157. aml_op_start,
  158. (u32)
  159. (walk_state->
  160. parser_state.
  161. pkg_end -
  162. aml_op_start),
  163. walk_state->
  164. owner_id);
  165. }
  166. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  167. "Pass1: Skipping an If/Else/While body\n"));
  168. /* Skip body of if/else/while in pass 1 */
  169. walk_state->parser_state.aml =
  170. walk_state->parser_state.pkg_end;
  171. walk_state->arg_count = 0;
  172. break;
  173. default:
  174. /*
  175. * Check for an unsupported executable opcode at module
  176. * level. We must be in PASS1, the parent must be a SCOPE,
  177. * The opcode class must be EXECUTE, and the opcode must
  178. * not be an argument to another opcode.
  179. */
  180. if ((walk_state->pass_number ==
  181. ACPI_IMODE_LOAD_PASS1)
  182. && (op->common.parent->common.aml_opcode ==
  183. AML_SCOPE_OP)) {
  184. op_info =
  185. acpi_ps_get_opcode_info(op->common.
  186. aml_opcode);
  187. if ((op_info->class ==
  188. AML_CLASS_EXECUTE) && (!arg)) {
  189. ACPI_WARNING((AE_INFO,
  190. "Unsupported module-level executable opcode "
  191. "0x%.2X at table offset 0x%.4X",
  192. op->common.
  193. aml_opcode,
  194. (u32)
  195. (ACPI_PTR_DIFF
  196. (aml_op_start,
  197. walk_state->
  198. parser_state.
  199. aml_start) +
  200. sizeof(struct
  201. acpi_table_header))));
  202. }
  203. }
  204. break;
  205. }
  206. }
  207. /* Special processing for certain opcodes */
  208. switch (op->common.aml_opcode) {
  209. case AML_METHOD_OP:
  210. /*
  211. * Skip parsing of control method because we don't have enough
  212. * info in the first pass to parse it correctly.
  213. *
  214. * Save the length and address of the body
  215. */
  216. op->named.data = walk_state->parser_state.aml;
  217. op->named.length = (u32)
  218. (walk_state->parser_state.pkg_end -
  219. walk_state->parser_state.aml);
  220. /* Skip body of method */
  221. walk_state->parser_state.aml =
  222. walk_state->parser_state.pkg_end;
  223. walk_state->arg_count = 0;
  224. break;
  225. case AML_BUFFER_OP:
  226. case AML_PACKAGE_OP:
  227. case AML_VAR_PACKAGE_OP:
  228. if ((op->common.parent) &&
  229. (op->common.parent->common.aml_opcode ==
  230. AML_NAME_OP)
  231. && (walk_state->pass_number <=
  232. ACPI_IMODE_LOAD_PASS2)) {
  233. /*
  234. * Skip parsing of Buffers and Packages because we don't have
  235. * enough info in the first pass to parse them correctly.
  236. */
  237. op->named.data = aml_op_start;
  238. op->named.length = (u32)
  239. (walk_state->parser_state.pkg_end -
  240. aml_op_start);
  241. /* Skip body */
  242. walk_state->parser_state.aml =
  243. walk_state->parser_state.pkg_end;
  244. walk_state->arg_count = 0;
  245. }
  246. break;
  247. case AML_WHILE_OP:
  248. if (walk_state->control_state) {
  249. walk_state->control_state->control.package_end =
  250. walk_state->parser_state.pkg_end;
  251. }
  252. break;
  253. default:
  254. /* No action for all other opcodes */
  255. break;
  256. }
  257. break;
  258. }
  259. return_ACPI_STATUS(AE_OK);
  260. }
  261. /*******************************************************************************
  262. *
  263. * FUNCTION: acpi_ps_link_module_code
  264. *
  265. * PARAMETERS: parent_op - Parent parser op
  266. * aml_start - Pointer to the AML
  267. * aml_length - Length of executable AML
  268. * owner_id - owner_id of module level code
  269. *
  270. * RETURN: None.
  271. *
  272. * DESCRIPTION: Wrap the module-level code with a method object and link the
  273. * object to the global list. Note, the mutex field of the method
  274. * object is used to link multiple module-level code objects.
  275. *
  276. ******************************************************************************/
  277. static void
  278. acpi_ps_link_module_code(union acpi_parse_object *parent_op,
  279. u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
  280. {
  281. union acpi_operand_object *prev;
  282. union acpi_operand_object *next;
  283. union acpi_operand_object *method_obj;
  284. struct acpi_namespace_node *parent_node;
  285. ACPI_FUNCTION_TRACE(ps_link_module_code);
  286. /* Get the tail of the list */
  287. prev = next = acpi_gbl_module_code_list;
  288. while (next) {
  289. prev = next;
  290. next = next->method.mutex;
  291. }
  292. /*
  293. * Insert the module level code into the list. Merge it if it is
  294. * adjacent to the previous element.
  295. */
  296. if (!prev ||
  297. ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
  298. /* Create, initialize, and link a new temporary method object */
  299. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  300. if (!method_obj) {
  301. return_VOID;
  302. }
  303. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  304. "Create/Link new code block: %p\n",
  305. method_obj));
  306. if (parent_op->common.node) {
  307. parent_node = parent_op->common.node;
  308. } else {
  309. parent_node = acpi_gbl_root_node;
  310. }
  311. method_obj->method.aml_start = aml_start;
  312. method_obj->method.aml_length = aml_length;
  313. method_obj->method.owner_id = owner_id;
  314. method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
  315. /*
  316. * Save the parent node in next_object. This is cheating, but we
  317. * don't want to expand the method object.
  318. */
  319. method_obj->method.next_object =
  320. ACPI_CAST_PTR(union acpi_operand_object, parent_node);
  321. if (!prev) {
  322. acpi_gbl_module_code_list = method_obj;
  323. } else {
  324. prev->method.mutex = method_obj;
  325. }
  326. } else {
  327. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  328. "Appending to existing code block: %p\n",
  329. prev));
  330. prev->method.aml_length += aml_length;
  331. }
  332. return_VOID;
  333. }
  334. /*******************************************************************************
  335. *
  336. * FUNCTION: acpi_ps_parse_loop
  337. *
  338. * PARAMETERS: walk_state - Current state
  339. *
  340. * RETURN: Status
  341. *
  342. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  343. * a tree of ops.
  344. *
  345. ******************************************************************************/
  346. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  347. {
  348. acpi_status status = AE_OK;
  349. union acpi_parse_object *op = NULL; /* current op */
  350. struct acpi_parse_state *parser_state;
  351. u8 *aml_op_start = NULL;
  352. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  353. if (walk_state->descending_callback == NULL) {
  354. return_ACPI_STATUS(AE_BAD_PARAMETER);
  355. }
  356. parser_state = &walk_state->parser_state;
  357. walk_state->arg_types = 0;
  358. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  359. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  360. /* We are restarting a preempted control method */
  361. if (acpi_ps_has_completed_scope(parser_state)) {
  362. /*
  363. * We must check if a predicate to an IF or WHILE statement
  364. * was just completed
  365. */
  366. if ((parser_state->scope->parse_scope.op) &&
  367. ((parser_state->scope->parse_scope.op->common.
  368. aml_opcode == AML_IF_OP)
  369. || (parser_state->scope->parse_scope.op->common.
  370. aml_opcode == AML_WHILE_OP))
  371. && (walk_state->control_state)
  372. && (walk_state->control_state->common.state ==
  373. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  374. /*
  375. * A predicate was just completed, get the value of the
  376. * predicate and branch based on that value
  377. */
  378. walk_state->op = NULL;
  379. status =
  380. acpi_ds_get_predicate_value(walk_state,
  381. ACPI_TO_POINTER
  382. (TRUE));
  383. if (ACPI_FAILURE(status)
  384. && ((status & AE_CODE_MASK) !=
  385. AE_CODE_CONTROL)) {
  386. if (status == AE_AML_NO_RETURN_VALUE) {
  387. ACPI_EXCEPTION((AE_INFO, status,
  388. "Invoked method did not return a value"));
  389. }
  390. ACPI_EXCEPTION((AE_INFO, status,
  391. "GetPredicate Failed"));
  392. return_ACPI_STATUS(status);
  393. }
  394. status =
  395. acpi_ps_next_parse_state(walk_state, op,
  396. status);
  397. }
  398. acpi_ps_pop_scope(parser_state, &op,
  399. &walk_state->arg_types,
  400. &walk_state->arg_count);
  401. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  402. "Popped scope, Op=%p\n", op));
  403. } else if (walk_state->prev_op) {
  404. /* We were in the middle of an op */
  405. op = walk_state->prev_op;
  406. walk_state->arg_types = walk_state->prev_arg_types;
  407. }
  408. }
  409. #endif
  410. /* Iterative parsing loop, while there is more AML to process: */
  411. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  412. aml_op_start = parser_state->aml;
  413. if (!op) {
  414. status =
  415. acpi_ps_create_op(walk_state, aml_op_start, &op);
  416. if (ACPI_FAILURE(status)) {
  417. if (status == AE_CTRL_PARSE_CONTINUE) {
  418. continue;
  419. }
  420. if (status == AE_CTRL_PARSE_PENDING) {
  421. status = AE_OK;
  422. }
  423. if (status == AE_CTRL_TERMINATE) {
  424. return_ACPI_STATUS(status);
  425. }
  426. status =
  427. acpi_ps_complete_op(walk_state, &op,
  428. status);
  429. if (ACPI_FAILURE(status)) {
  430. return_ACPI_STATUS(status);
  431. }
  432. continue;
  433. }
  434. acpi_ex_start_trace_opcode(op, walk_state);
  435. }
  436. /*
  437. * Start arg_count at zero because we don't know if there are
  438. * any args yet
  439. */
  440. walk_state->arg_count = 0;
  441. /* Are there any arguments that must be processed? */
  442. if (walk_state->arg_types) {
  443. /* Get arguments */
  444. status =
  445. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  446. if (ACPI_FAILURE(status)) {
  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. }
  456. /* Check for arguments that need to be processed */
  457. if (walk_state->arg_count) {
  458. /*
  459. * There are arguments (complex ones), push Op and
  460. * prepare for argument
  461. */
  462. status = acpi_ps_push_scope(parser_state, op,
  463. walk_state->arg_types,
  464. walk_state->arg_count);
  465. if (ACPI_FAILURE(status)) {
  466. status =
  467. acpi_ps_complete_op(walk_state, &op,
  468. status);
  469. if (ACPI_FAILURE(status)) {
  470. return_ACPI_STATUS(status);
  471. }
  472. continue;
  473. }
  474. op = NULL;
  475. continue;
  476. }
  477. /*
  478. * All arguments have been processed -- Op is complete,
  479. * prepare for next
  480. */
  481. walk_state->op_info =
  482. acpi_ps_get_opcode_info(op->common.aml_opcode);
  483. if (walk_state->op_info->flags & AML_NAMED) {
  484. if (op->common.aml_opcode == AML_REGION_OP ||
  485. op->common.aml_opcode == AML_DATA_REGION_OP) {
  486. /*
  487. * Skip parsing of control method or opregion body,
  488. * because we don't have enough info in the first pass
  489. * to parse them correctly.
  490. *
  491. * Completed parsing an op_region declaration, we now
  492. * know the length.
  493. */
  494. op->named.length =
  495. (u32) (parser_state->aml - op->named.data);
  496. }
  497. }
  498. if (walk_state->op_info->flags & AML_CREATE) {
  499. /*
  500. * Backup to beginning of create_XXXfield declaration (1 for
  501. * Opcode)
  502. *
  503. * body_length is unknown until we parse the body
  504. */
  505. op->named.length =
  506. (u32) (parser_state->aml - op->named.data);
  507. }
  508. if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
  509. /*
  510. * Backup to beginning of bank_field declaration
  511. *
  512. * body_length is unknown until we parse the body
  513. */
  514. op->named.length =
  515. (u32) (parser_state->aml - op->named.data);
  516. }
  517. /* This op complete, notify the dispatcher */
  518. if (walk_state->ascending_callback != NULL) {
  519. walk_state->op = op;
  520. walk_state->opcode = op->common.aml_opcode;
  521. status = walk_state->ascending_callback(walk_state);
  522. status =
  523. acpi_ps_next_parse_state(walk_state, op, status);
  524. if (status == AE_CTRL_PENDING) {
  525. status = AE_OK;
  526. }
  527. }
  528. status = acpi_ps_complete_op(walk_state, &op, status);
  529. if (ACPI_FAILURE(status)) {
  530. return_ACPI_STATUS(status);
  531. }
  532. } /* while parser_state->Aml */
  533. status = acpi_ps_complete_final_op(walk_state, op, status);
  534. return_ACPI_STATUS(status);
  535. }