psloop.c 17 KB

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