psparse.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /******************************************************************************
  2. *
  3. * Module Name: psparse - Parser top level AML parse routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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,
  44. * like Perl, do. Parsing is done by hand rather than with a YACC
  45. * generated parser to tightly constrain stack and dynamic memory
  46. * usage. At the same time, parsing is kept flexible and the code
  47. * fairly compact by parsing based on a list of AML opcode
  48. * templates in aml_op_info[]
  49. */
  50. #include <acpi/acpi.h>
  51. #include "accommon.h"
  52. #include "acparser.h"
  53. #include "acdispat.h"
  54. #include "amlcode.h"
  55. #include "acinterp.h"
  56. #define _COMPONENT ACPI_PARSER
  57. ACPI_MODULE_NAME("psparse")
  58. /*******************************************************************************
  59. *
  60. * FUNCTION: acpi_ps_get_opcode_size
  61. *
  62. * PARAMETERS: Opcode - An AML opcode
  63. *
  64. * RETURN: Size of the opcode, in bytes (1 or 2)
  65. *
  66. * DESCRIPTION: Get the size of the current opcode.
  67. *
  68. ******************************************************************************/
  69. u32 acpi_ps_get_opcode_size(u32 opcode)
  70. {
  71. /* Extended (2-byte) opcode if > 255 */
  72. if (opcode > 0x00FF) {
  73. return (2);
  74. }
  75. /* Otherwise, just a single byte opcode */
  76. return (1);
  77. }
  78. /*******************************************************************************
  79. *
  80. * FUNCTION: acpi_ps_peek_opcode
  81. *
  82. * PARAMETERS: parser_state - A parser state object
  83. *
  84. * RETURN: Next AML opcode
  85. *
  86. * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
  87. *
  88. ******************************************************************************/
  89. u16 acpi_ps_peek_opcode(struct acpi_parse_state * parser_state)
  90. {
  91. u8 *aml;
  92. u16 opcode;
  93. aml = parser_state->aml;
  94. opcode = (u16) ACPI_GET8(aml);
  95. if (opcode == AML_EXTENDED_OP_PREFIX) {
  96. /* Extended opcode, get the second opcode byte */
  97. aml++;
  98. opcode = (u16) ((opcode << 8) | ACPI_GET8(aml));
  99. }
  100. return (opcode);
  101. }
  102. /*******************************************************************************
  103. *
  104. * FUNCTION: acpi_ps_complete_this_op
  105. *
  106. * PARAMETERS: walk_state - Current State
  107. * Op - Op to complete
  108. *
  109. * RETURN: Status
  110. *
  111. * DESCRIPTION: Perform any cleanup at the completion of an Op.
  112. *
  113. ******************************************************************************/
  114. acpi_status
  115. acpi_ps_complete_this_op(struct acpi_walk_state * walk_state,
  116. union acpi_parse_object * op)
  117. {
  118. union acpi_parse_object *prev;
  119. union acpi_parse_object *next;
  120. const struct acpi_opcode_info *parent_info;
  121. union acpi_parse_object *replacement_op = NULL;
  122. acpi_status status = AE_OK;
  123. ACPI_FUNCTION_TRACE_PTR(ps_complete_this_op, op);
  124. /* Check for null Op, can happen if AML code is corrupt */
  125. if (!op) {
  126. return_ACPI_STATUS(AE_OK); /* OK for now */
  127. }
  128. /* Delete this op and the subtree below it if asked to */
  129. if (((walk_state->parse_flags & ACPI_PARSE_TREE_MASK) !=
  130. ACPI_PARSE_DELETE_TREE)
  131. || (walk_state->op_info->class == AML_CLASS_ARGUMENT)) {
  132. return_ACPI_STATUS(AE_OK);
  133. }
  134. /* Make sure that we only delete this subtree */
  135. if (op->common.parent) {
  136. prev = op->common.parent->common.value.arg;
  137. if (!prev) {
  138. /* Nothing more to do */
  139. goto cleanup;
  140. }
  141. /*
  142. * Check if we need to replace the operator and its subtree
  143. * with a return value op (placeholder op)
  144. */
  145. parent_info =
  146. acpi_ps_get_opcode_info(op->common.parent->common.
  147. aml_opcode);
  148. switch (parent_info->class) {
  149. case AML_CLASS_CONTROL:
  150. break;
  151. case AML_CLASS_CREATE:
  152. /*
  153. * These opcodes contain term_arg operands. The current
  154. * op must be replaced by a placeholder return op
  155. */
  156. replacement_op =
  157. acpi_ps_alloc_op(AML_INT_RETURN_VALUE_OP);
  158. if (!replacement_op) {
  159. status = AE_NO_MEMORY;
  160. }
  161. break;
  162. case AML_CLASS_NAMED_OBJECT:
  163. /*
  164. * These opcodes contain term_arg operands. The current
  165. * op must be replaced by a placeholder return op
  166. */
  167. if ((op->common.parent->common.aml_opcode ==
  168. AML_REGION_OP)
  169. || (op->common.parent->common.aml_opcode ==
  170. AML_DATA_REGION_OP)
  171. || (op->common.parent->common.aml_opcode ==
  172. AML_BUFFER_OP)
  173. || (op->common.parent->common.aml_opcode ==
  174. AML_PACKAGE_OP)
  175. || (op->common.parent->common.aml_opcode ==
  176. AML_BANK_FIELD_OP)
  177. || (op->common.parent->common.aml_opcode ==
  178. AML_VAR_PACKAGE_OP)) {
  179. replacement_op =
  180. acpi_ps_alloc_op(AML_INT_RETURN_VALUE_OP);
  181. if (!replacement_op) {
  182. status = AE_NO_MEMORY;
  183. }
  184. } else
  185. if ((op->common.parent->common.aml_opcode ==
  186. AML_NAME_OP)
  187. && (walk_state->pass_number <=
  188. ACPI_IMODE_LOAD_PASS2)) {
  189. if ((op->common.aml_opcode == AML_BUFFER_OP)
  190. || (op->common.aml_opcode == AML_PACKAGE_OP)
  191. || (op->common.aml_opcode ==
  192. AML_VAR_PACKAGE_OP)) {
  193. replacement_op =
  194. acpi_ps_alloc_op(op->common.
  195. aml_opcode);
  196. if (!replacement_op) {
  197. status = AE_NO_MEMORY;
  198. } else {
  199. replacement_op->named.data =
  200. op->named.data;
  201. replacement_op->named.length =
  202. op->named.length;
  203. }
  204. }
  205. }
  206. break;
  207. default:
  208. replacement_op =
  209. acpi_ps_alloc_op(AML_INT_RETURN_VALUE_OP);
  210. if (!replacement_op) {
  211. status = AE_NO_MEMORY;
  212. }
  213. }
  214. /* We must unlink this op from the parent tree */
  215. if (prev == op) {
  216. /* This op is the first in the list */
  217. if (replacement_op) {
  218. replacement_op->common.parent =
  219. op->common.parent;
  220. replacement_op->common.value.arg = NULL;
  221. replacement_op->common.node = op->common.node;
  222. op->common.parent->common.value.arg =
  223. replacement_op;
  224. replacement_op->common.next = op->common.next;
  225. } else {
  226. op->common.parent->common.value.arg =
  227. op->common.next;
  228. }
  229. }
  230. /* Search the parent list */
  231. else
  232. while (prev) {
  233. /* Traverse all siblings in the parent's argument list */
  234. next = prev->common.next;
  235. if (next == op) {
  236. if (replacement_op) {
  237. replacement_op->common.parent =
  238. op->common.parent;
  239. replacement_op->common.value.
  240. arg = NULL;
  241. replacement_op->common.node =
  242. op->common.node;
  243. prev->common.next =
  244. replacement_op;
  245. replacement_op->common.next =
  246. op->common.next;
  247. next = NULL;
  248. } else {
  249. prev->common.next =
  250. op->common.next;
  251. next = NULL;
  252. }
  253. }
  254. prev = next;
  255. }
  256. }
  257. cleanup:
  258. /* Now we can actually delete the subtree rooted at Op */
  259. acpi_ps_delete_parse_tree(op);
  260. return_ACPI_STATUS(status);
  261. }
  262. /*******************************************************************************
  263. *
  264. * FUNCTION: acpi_ps_next_parse_state
  265. *
  266. * PARAMETERS: walk_state - Current state
  267. * Op - Current parse op
  268. * callback_status - Status from previous operation
  269. *
  270. * RETURN: Status
  271. *
  272. * DESCRIPTION: Update the parser state based upon the return exception from
  273. * the parser callback.
  274. *
  275. ******************************************************************************/
  276. acpi_status
  277. acpi_ps_next_parse_state(struct acpi_walk_state *walk_state,
  278. union acpi_parse_object *op,
  279. acpi_status callback_status)
  280. {
  281. struct acpi_parse_state *parser_state = &walk_state->parser_state;
  282. acpi_status status = AE_CTRL_PENDING;
  283. ACPI_FUNCTION_TRACE_PTR(ps_next_parse_state, op);
  284. switch (callback_status) {
  285. case AE_CTRL_TERMINATE:
  286. /*
  287. * A control method was terminated via a RETURN statement.
  288. * The walk of this method is complete.
  289. */
  290. parser_state->aml = parser_state->aml_end;
  291. status = AE_CTRL_TERMINATE;
  292. break;
  293. case AE_CTRL_BREAK:
  294. parser_state->aml = walk_state->aml_last_while;
  295. walk_state->control_state->common.value = FALSE;
  296. status = AE_CTRL_BREAK;
  297. break;
  298. case AE_CTRL_CONTINUE:
  299. parser_state->aml = walk_state->aml_last_while;
  300. status = AE_CTRL_CONTINUE;
  301. break;
  302. case AE_CTRL_PENDING:
  303. parser_state->aml = walk_state->aml_last_while;
  304. break;
  305. #if 0
  306. case AE_CTRL_SKIP:
  307. parser_state->aml = parser_state->scope->parse_scope.pkg_end;
  308. status = AE_OK;
  309. break;
  310. #endif
  311. case AE_CTRL_TRUE:
  312. /*
  313. * Predicate of an IF was true, and we are at the matching ELSE.
  314. * Just close out this package
  315. */
  316. parser_state->aml = acpi_ps_get_next_package_end(parser_state);
  317. status = AE_CTRL_PENDING;
  318. break;
  319. case AE_CTRL_FALSE:
  320. /*
  321. * Either an IF/WHILE Predicate was false or we encountered a BREAK
  322. * opcode. In both cases, we do not execute the rest of the
  323. * package; We simply close out the parent (finishing the walk of
  324. * this branch of the tree) and continue execution at the parent
  325. * level.
  326. */
  327. parser_state->aml = parser_state->scope->parse_scope.pkg_end;
  328. /* In the case of a BREAK, just force a predicate (if any) to FALSE */
  329. walk_state->control_state->common.value = FALSE;
  330. status = AE_CTRL_END;
  331. break;
  332. case AE_CTRL_TRANSFER:
  333. /* A method call (invocation) -- transfer control */
  334. status = AE_CTRL_TRANSFER;
  335. walk_state->prev_op = op;
  336. walk_state->method_call_op = op;
  337. walk_state->method_call_node =
  338. (op->common.value.arg)->common.node;
  339. /* Will return value (if any) be used by the caller? */
  340. walk_state->return_used =
  341. acpi_ds_is_result_used(op, walk_state);
  342. break;
  343. default:
  344. status = callback_status;
  345. if ((callback_status & AE_CODE_MASK) == AE_CODE_CONTROL) {
  346. status = AE_OK;
  347. }
  348. break;
  349. }
  350. return_ACPI_STATUS(status);
  351. }
  352. /*******************************************************************************
  353. *
  354. * FUNCTION: acpi_ps_parse_aml
  355. *
  356. * PARAMETERS: walk_state - Current state
  357. *
  358. *
  359. * RETURN: Status
  360. *
  361. * DESCRIPTION: Parse raw AML and return a tree of ops
  362. *
  363. ******************************************************************************/
  364. acpi_status acpi_ps_parse_aml(struct acpi_walk_state *walk_state)
  365. {
  366. acpi_status status;
  367. struct acpi_thread_state *thread;
  368. struct acpi_thread_state *prev_walk_list = acpi_gbl_current_walk_list;
  369. struct acpi_walk_state *previous_walk_state;
  370. ACPI_FUNCTION_TRACE(ps_parse_aml);
  371. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  372. "Entered with WalkState=%p Aml=%p size=%X\n",
  373. walk_state, walk_state->parser_state.aml,
  374. walk_state->parser_state.aml_size));
  375. if (!walk_state->parser_state.aml) {
  376. return_ACPI_STATUS(AE_NULL_OBJECT);
  377. }
  378. /* Create and initialize a new thread state */
  379. thread = acpi_ut_create_thread_state();
  380. if (!thread) {
  381. if (walk_state->method_desc) {
  382. /* Executing a control method - additional cleanup */
  383. acpi_ds_terminate_control_method(
  384. walk_state->method_desc, walk_state);
  385. }
  386. acpi_ds_delete_walk_state(walk_state);
  387. return_ACPI_STATUS(AE_NO_MEMORY);
  388. }
  389. walk_state->thread = thread;
  390. /*
  391. * If executing a method, the starting sync_level is this method's
  392. * sync_level
  393. */
  394. if (walk_state->method_desc) {
  395. walk_state->thread->current_sync_level =
  396. walk_state->method_desc->method.sync_level;
  397. }
  398. acpi_ds_push_walk_state(walk_state, thread);
  399. /*
  400. * This global allows the AML debugger to get a handle to the currently
  401. * executing control method.
  402. */
  403. acpi_gbl_current_walk_list = thread;
  404. /*
  405. * Execute the walk loop as long as there is a valid Walk State. This
  406. * handles nested control method invocations without recursion.
  407. */
  408. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "State=%p\n", walk_state));
  409. status = AE_OK;
  410. while (walk_state) {
  411. if (ACPI_SUCCESS(status)) {
  412. /*
  413. * The parse_loop executes AML until the method terminates
  414. * or calls another method.
  415. */
  416. status = acpi_ps_parse_loop(walk_state);
  417. }
  418. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  419. "Completed one call to walk loop, %s State=%p\n",
  420. acpi_format_exception(status), walk_state));
  421. if (status == AE_CTRL_TRANSFER) {
  422. /*
  423. * A method call was detected.
  424. * Transfer control to the called control method
  425. */
  426. status =
  427. acpi_ds_call_control_method(thread, walk_state,
  428. NULL);
  429. if (ACPI_FAILURE(status)) {
  430. status =
  431. acpi_ds_method_error(status, walk_state);
  432. }
  433. /*
  434. * If the transfer to the new method method call worked, a new walk
  435. * state was created -- get it
  436. */
  437. walk_state = acpi_ds_get_current_walk_state(thread);
  438. continue;
  439. } else if (status == AE_CTRL_TERMINATE) {
  440. status = AE_OK;
  441. } else if ((status != AE_OK) && (walk_state->method_desc)) {
  442. /* Either the method parse or actual execution failed */
  443. ACPI_ERROR_METHOD("Method parse/execution failed",
  444. walk_state->method_node, NULL,
  445. status);
  446. /* Check for possible multi-thread reentrancy problem */
  447. if ((status == AE_ALREADY_EXISTS) &&
  448. (!(walk_state->method_desc->method.
  449. info_flags & ACPI_METHOD_SERIALIZED))) {
  450. /*
  451. * Method is not serialized and tried to create an object
  452. * twice. The probable cause is that the method cannot
  453. * handle reentrancy. Mark as "pending serialized" now, and
  454. * then mark "serialized" when the last thread exits.
  455. */
  456. walk_state->method_desc->method.info_flags |=
  457. ACPI_METHOD_SERIALIZED_PENDING;
  458. }
  459. }
  460. /* We are done with this walk, move on to the parent if any */
  461. walk_state = acpi_ds_pop_walk_state(thread);
  462. /* Reset the current scope to the beginning of scope stack */
  463. acpi_ds_scope_stack_clear(walk_state);
  464. /*
  465. * If we just returned from the execution of a control method or if we
  466. * encountered an error during the method parse phase, there's lots of
  467. * cleanup to do
  468. */
  469. if (((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) ==
  470. ACPI_PARSE_EXECUTE) || (ACPI_FAILURE(status))) {
  471. acpi_ds_terminate_control_method(walk_state->
  472. method_desc,
  473. walk_state);
  474. }
  475. /* Delete this walk state and all linked control states */
  476. acpi_ps_cleanup_scope(&walk_state->parser_state);
  477. previous_walk_state = walk_state;
  478. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  479. "ReturnValue=%p, ImplicitValue=%p State=%p\n",
  480. walk_state->return_desc,
  481. walk_state->implicit_return_obj, walk_state));
  482. /* Check if we have restarted a preempted walk */
  483. walk_state = acpi_ds_get_current_walk_state(thread);
  484. if (walk_state) {
  485. if (ACPI_SUCCESS(status)) {
  486. /*
  487. * There is another walk state, restart it.
  488. * If the method return value is not used by the parent,
  489. * The object is deleted
  490. */
  491. if (!previous_walk_state->return_desc) {
  492. /*
  493. * In slack mode execution, if there is no return value
  494. * we should implicitly return zero (0) as a default value.
  495. */
  496. if (acpi_gbl_enable_interpreter_slack &&
  497. !previous_walk_state->
  498. implicit_return_obj) {
  499. previous_walk_state->
  500. implicit_return_obj =
  501. acpi_ut_create_integer_object
  502. ((u64) 0);
  503. if (!previous_walk_state->
  504. implicit_return_obj) {
  505. return_ACPI_STATUS
  506. (AE_NO_MEMORY);
  507. }
  508. }
  509. /* Restart the calling control method */
  510. status =
  511. acpi_ds_restart_control_method
  512. (walk_state,
  513. previous_walk_state->
  514. implicit_return_obj);
  515. } else {
  516. /*
  517. * We have a valid return value, delete any implicit
  518. * return value.
  519. */
  520. acpi_ds_clear_implicit_return
  521. (previous_walk_state);
  522. status =
  523. acpi_ds_restart_control_method
  524. (walk_state,
  525. previous_walk_state->return_desc);
  526. }
  527. if (ACPI_SUCCESS(status)) {
  528. walk_state->walk_type |=
  529. ACPI_WALK_METHOD_RESTART;
  530. }
  531. } else {
  532. /* On error, delete any return object or implicit return */
  533. acpi_ut_remove_reference(previous_walk_state->
  534. return_desc);
  535. acpi_ds_clear_implicit_return
  536. (previous_walk_state);
  537. }
  538. }
  539. /*
  540. * Just completed a 1st-level method, save the final internal return
  541. * value (if any)
  542. */
  543. else if (previous_walk_state->caller_return_desc) {
  544. if (previous_walk_state->implicit_return_obj) {
  545. *(previous_walk_state->caller_return_desc) =
  546. previous_walk_state->implicit_return_obj;
  547. } else {
  548. /* NULL if no return value */
  549. *(previous_walk_state->caller_return_desc) =
  550. previous_walk_state->return_desc;
  551. }
  552. } else {
  553. if (previous_walk_state->return_desc) {
  554. /* Caller doesn't want it, must delete it */
  555. acpi_ut_remove_reference(previous_walk_state->
  556. return_desc);
  557. }
  558. if (previous_walk_state->implicit_return_obj) {
  559. /* Caller doesn't want it, must delete it */
  560. acpi_ut_remove_reference(previous_walk_state->
  561. implicit_return_obj);
  562. }
  563. }
  564. acpi_ds_delete_walk_state(previous_walk_state);
  565. }
  566. /* Normal exit */
  567. acpi_ex_release_all_mutexes(thread);
  568. acpi_ut_delete_generic_state(ACPI_CAST_PTR
  569. (union acpi_generic_state, thread));
  570. acpi_gbl_current_walk_list = prev_walk_list;
  571. return_ACPI_STATUS(status);
  572. }