psloop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /******************************************************************************
  2. *
  3. * Module Name: psloop - Main AML parse loop
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, 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 "acparser.h"
  52. #include "acdispat.h"
  53. #include "amlcode.h"
  54. #define _COMPONENT ACPI_PARSER
  55. ACPI_MODULE_NAME("psloop")
  56. /* Local prototypes */
  57. static acpi_status
  58. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  59. u8 * aml_op_start, union acpi_parse_object *op);
  60. static void
  61. acpi_ps_link_module_code(union acpi_parse_object *parent_op,
  62. u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
  63. /*******************************************************************************
  64. *
  65. * FUNCTION: acpi_ps_get_arguments
  66. *
  67. * PARAMETERS: walk_state - Current state
  68. * aml_op_start - Op start in AML
  69. * op - Current Op
  70. *
  71. * RETURN: Status
  72. *
  73. * DESCRIPTION: Get arguments for passed Op.
  74. *
  75. ******************************************************************************/
  76. static acpi_status
  77. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  78. u8 * aml_op_start, union acpi_parse_object *op)
  79. {
  80. acpi_status status = AE_OK;
  81. union acpi_parse_object *arg = NULL;
  82. const struct acpi_opcode_info *op_info;
  83. ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
  84. switch (op->common.aml_opcode) {
  85. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  86. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  87. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  88. case AML_QWORD_OP: /* AML_QWORDATA_ARG */
  89. case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
  90. /* Fill in constant or string argument directly */
  91. acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
  92. GET_CURRENT_ARG_TYPE(walk_state->
  93. arg_types),
  94. op);
  95. break;
  96. case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
  97. status =
  98. acpi_ps_get_next_namepath(walk_state,
  99. &(walk_state->parser_state), op,
  100. 1);
  101. if (ACPI_FAILURE(status)) {
  102. return_ACPI_STATUS(status);
  103. }
  104. walk_state->arg_types = 0;
  105. break;
  106. default:
  107. /*
  108. * Op is not a constant or string, append each argument to the Op
  109. */
  110. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types)
  111. && !walk_state->arg_count) {
  112. walk_state->aml_offset =
  113. (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
  114. walk_state->parser_state.
  115. aml_start);
  116. status =
  117. acpi_ps_get_next_arg(walk_state,
  118. &(walk_state->parser_state),
  119. GET_CURRENT_ARG_TYPE
  120. (walk_state->arg_types), &arg);
  121. if (ACPI_FAILURE(status)) {
  122. return_ACPI_STATUS(status);
  123. }
  124. if (arg) {
  125. arg->common.aml_offset = walk_state->aml_offset;
  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. /* Get the tail of the list */
  286. prev = next = acpi_gbl_module_code_list;
  287. while (next) {
  288. prev = next;
  289. next = next->method.mutex;
  290. }
  291. /*
  292. * Insert the module level code into the list. Merge it if it is
  293. * adjacent to the previous element.
  294. */
  295. if (!prev ||
  296. ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
  297. /* Create, initialize, and link a new temporary method object */
  298. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  299. if (!method_obj) {
  300. return;
  301. }
  302. if (parent_op->common.node) {
  303. parent_node = parent_op->common.node;
  304. } else {
  305. parent_node = acpi_gbl_root_node;
  306. }
  307. method_obj->method.aml_start = aml_start;
  308. method_obj->method.aml_length = aml_length;
  309. method_obj->method.owner_id = owner_id;
  310. method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
  311. /*
  312. * Save the parent node in next_object. This is cheating, but we
  313. * don't want to expand the method object.
  314. */
  315. method_obj->method.next_object =
  316. ACPI_CAST_PTR(union acpi_operand_object, parent_node);
  317. if (!prev) {
  318. acpi_gbl_module_code_list = method_obj;
  319. } else {
  320. prev->method.mutex = method_obj;
  321. }
  322. } else {
  323. prev->method.aml_length += aml_length;
  324. }
  325. }
  326. /*******************************************************************************
  327. *
  328. * FUNCTION: acpi_ps_parse_loop
  329. *
  330. * PARAMETERS: walk_state - Current state
  331. *
  332. * RETURN: Status
  333. *
  334. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  335. * a tree of ops.
  336. *
  337. ******************************************************************************/
  338. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  339. {
  340. acpi_status status = AE_OK;
  341. union acpi_parse_object *op = NULL; /* current op */
  342. struct acpi_parse_state *parser_state;
  343. u8 *aml_op_start = NULL;
  344. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  345. if (walk_state->descending_callback == NULL) {
  346. return_ACPI_STATUS(AE_BAD_PARAMETER);
  347. }
  348. parser_state = &walk_state->parser_state;
  349. walk_state->arg_types = 0;
  350. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  351. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  352. /* We are restarting a preempted control method */
  353. if (acpi_ps_has_completed_scope(parser_state)) {
  354. /*
  355. * We must check if a predicate to an IF or WHILE statement
  356. * was just completed
  357. */
  358. if ((parser_state->scope->parse_scope.op) &&
  359. ((parser_state->scope->parse_scope.op->common.
  360. aml_opcode == AML_IF_OP)
  361. || (parser_state->scope->parse_scope.op->common.
  362. aml_opcode == AML_WHILE_OP))
  363. && (walk_state->control_state)
  364. && (walk_state->control_state->common.state ==
  365. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  366. /*
  367. * A predicate was just completed, get the value of the
  368. * predicate and branch based on that value
  369. */
  370. walk_state->op = NULL;
  371. status =
  372. acpi_ds_get_predicate_value(walk_state,
  373. ACPI_TO_POINTER
  374. (TRUE));
  375. if (ACPI_FAILURE(status)
  376. && ((status & AE_CODE_MASK) !=
  377. AE_CODE_CONTROL)) {
  378. if (status == AE_AML_NO_RETURN_VALUE) {
  379. ACPI_EXCEPTION((AE_INFO, status,
  380. "Invoked method did not return a value"));
  381. }
  382. ACPI_EXCEPTION((AE_INFO, status,
  383. "GetPredicate Failed"));
  384. return_ACPI_STATUS(status);
  385. }
  386. status =
  387. acpi_ps_next_parse_state(walk_state, op,
  388. status);
  389. }
  390. acpi_ps_pop_scope(parser_state, &op,
  391. &walk_state->arg_types,
  392. &walk_state->arg_count);
  393. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  394. "Popped scope, Op=%p\n", op));
  395. } else if (walk_state->prev_op) {
  396. /* We were in the middle of an op */
  397. op = walk_state->prev_op;
  398. walk_state->arg_types = walk_state->prev_arg_types;
  399. }
  400. }
  401. #endif
  402. /* Iterative parsing loop, while there is more AML to process: */
  403. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  404. aml_op_start = parser_state->aml;
  405. if (!op) {
  406. status =
  407. acpi_ps_create_op(walk_state, aml_op_start, &op);
  408. if (ACPI_FAILURE(status)) {
  409. if (status == AE_CTRL_PARSE_CONTINUE) {
  410. continue;
  411. }
  412. if (status == AE_CTRL_PARSE_PENDING) {
  413. status = AE_OK;
  414. }
  415. status =
  416. acpi_ps_complete_op(walk_state, &op,
  417. status);
  418. if (ACPI_FAILURE(status)) {
  419. return_ACPI_STATUS(status);
  420. }
  421. continue;
  422. }
  423. op->common.aml_offset = walk_state->aml_offset;
  424. if (walk_state->op_info) {
  425. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  426. "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
  427. (u32) op->common.aml_opcode,
  428. walk_state->op_info->name, op,
  429. parser_state->aml,
  430. op->common.aml_offset));
  431. }
  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. }