psloop.c 17 KB

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