psparse.c 18 KB

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