psloop.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 - 2025, 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. #include "acnamesp.h"
  24. #define _COMPONENT ACPI_PARSER
  25. ACPI_MODULE_NAME("psloop")
  26. /* Local prototypes */
  27. static acpi_status
  28. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  29. u8 * aml_op_start, union acpi_parse_object *op);
  30. /*******************************************************************************
  31. *
  32. * FUNCTION: acpi_ps_get_arguments
  33. *
  34. * PARAMETERS: walk_state - Current state
  35. * aml_op_start - Op start in AML
  36. * op - Current Op
  37. *
  38. * RETURN: Status
  39. *
  40. * DESCRIPTION: Get arguments for passed Op.
  41. *
  42. ******************************************************************************/
  43. static acpi_status
  44. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  45. u8 * aml_op_start, union acpi_parse_object *op)
  46. {
  47. acpi_status status = AE_OK;
  48. union acpi_parse_object *arg = NULL;
  49. ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
  50. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  51. "Get arguments for opcode [%s]\n",
  52. op->common.aml_op_name));
  53. switch (op->common.aml_opcode) {
  54. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  55. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  56. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  57. case AML_QWORD_OP: /* AML_QWORDATA_ARG */
  58. case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
  59. /* Fill in constant or string argument directly */
  60. acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
  61. GET_CURRENT_ARG_TYPE(walk_state->
  62. arg_types),
  63. op);
  64. break;
  65. case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
  66. status = acpi_ps_get_next_namepath(walk_state,
  67. &(walk_state->parser_state),
  68. op,
  69. ACPI_POSSIBLE_METHOD_CALL);
  70. if (ACPI_FAILURE(status)) {
  71. return_ACPI_STATUS(status);
  72. }
  73. walk_state->arg_types = 0;
  74. break;
  75. default:
  76. /*
  77. * Op is not a constant or string, append each argument to the Op
  78. */
  79. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
  80. !walk_state->arg_count) {
  81. walk_state->aml = walk_state->parser_state.aml;
  82. switch (op->common.aml_opcode) {
  83. case AML_METHOD_OP:
  84. case AML_BUFFER_OP:
  85. case AML_PACKAGE_OP:
  86. case AML_VARIABLE_PACKAGE_OP:
  87. case AML_WHILE_OP:
  88. break;
  89. default:
  90. ASL_CV_CAPTURE_COMMENTS(walk_state);
  91. break;
  92. }
  93. status =
  94. acpi_ps_get_next_arg(walk_state,
  95. &(walk_state->parser_state),
  96. GET_CURRENT_ARG_TYPE
  97. (walk_state->arg_types), &arg);
  98. if (ACPI_FAILURE(status)) {
  99. return_ACPI_STATUS(status);
  100. }
  101. if (arg) {
  102. acpi_ps_append_arg(op, arg);
  103. }
  104. INCREMENT_ARG_LIST(walk_state->arg_types);
  105. }
  106. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  107. "Final argument count: %8.8X pass %u\n",
  108. walk_state->arg_count,
  109. walk_state->pass_number));
  110. /* Special processing for certain opcodes */
  111. switch (op->common.aml_opcode) {
  112. case AML_METHOD_OP:
  113. /*
  114. * Skip parsing of control method because we don't have enough
  115. * info in the first pass to parse it correctly.
  116. *
  117. * Save the length and address of the body
  118. */
  119. op->named.data = walk_state->parser_state.aml;
  120. op->named.length = (u32)
  121. (walk_state->parser_state.pkg_end -
  122. walk_state->parser_state.aml);
  123. /* Skip body of method */
  124. walk_state->parser_state.aml =
  125. walk_state->parser_state.pkg_end;
  126. walk_state->arg_count = 0;
  127. break;
  128. case AML_BUFFER_OP:
  129. case AML_PACKAGE_OP:
  130. case AML_VARIABLE_PACKAGE_OP:
  131. if ((op->common.parent) &&
  132. (op->common.parent->common.aml_opcode ==
  133. AML_NAME_OP)
  134. && (walk_state->pass_number <=
  135. ACPI_IMODE_LOAD_PASS2)) {
  136. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  137. "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
  138. walk_state->pass_number,
  139. aml_op_start));
  140. /*
  141. * Skip parsing of Buffers and Packages because we don't have
  142. * enough info in the first pass to parse them correctly.
  143. */
  144. op->named.data = aml_op_start;
  145. op->named.length = (u32)
  146. (walk_state->parser_state.pkg_end -
  147. aml_op_start);
  148. /* Skip body */
  149. walk_state->parser_state.aml =
  150. walk_state->parser_state.pkg_end;
  151. walk_state->arg_count = 0;
  152. }
  153. break;
  154. case AML_WHILE_OP:
  155. if (walk_state->control_state) {
  156. walk_state->control_state->control.package_end =
  157. walk_state->parser_state.pkg_end;
  158. }
  159. break;
  160. default:
  161. /* No action for all other opcodes */
  162. break;
  163. }
  164. break;
  165. }
  166. return_ACPI_STATUS(AE_OK);
  167. }
  168. /*******************************************************************************
  169. *
  170. * FUNCTION: acpi_ps_parse_loop
  171. *
  172. * PARAMETERS: walk_state - Current state
  173. *
  174. * RETURN: Status
  175. *
  176. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  177. * a tree of ops.
  178. *
  179. ******************************************************************************/
  180. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  181. {
  182. acpi_status status = AE_OK;
  183. union acpi_parse_object *op = NULL; /* current op */
  184. struct acpi_parse_state *parser_state;
  185. u8 *aml_op_start = NULL;
  186. u8 opcode_length;
  187. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  188. if (walk_state->descending_callback == NULL) {
  189. return_ACPI_STATUS(AE_BAD_PARAMETER);
  190. }
  191. parser_state = &walk_state->parser_state;
  192. walk_state->arg_types = 0;
  193. #ifndef ACPI_CONSTANT_EVAL_ONLY
  194. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  195. /* We are restarting a preempted control method */
  196. if (acpi_ps_has_completed_scope(parser_state)) {
  197. /*
  198. * We must check if a predicate to an IF or WHILE statement
  199. * was just completed
  200. */
  201. if ((parser_state->scope->parse_scope.op) &&
  202. ((parser_state->scope->parse_scope.op->common.
  203. aml_opcode == AML_IF_OP)
  204. || (parser_state->scope->parse_scope.op->common.
  205. aml_opcode == AML_WHILE_OP))
  206. && (walk_state->control_state)
  207. && (walk_state->control_state->common.state ==
  208. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  209. /*
  210. * A predicate was just completed, get the value of the
  211. * predicate and branch based on that value
  212. */
  213. walk_state->op = NULL;
  214. status =
  215. acpi_ds_get_predicate_value(walk_state,
  216. ACPI_TO_POINTER
  217. (TRUE));
  218. if (ACPI_FAILURE(status)
  219. && !ACPI_CNTL_EXCEPTION(status)) {
  220. if (status == AE_AML_NO_RETURN_VALUE) {
  221. ACPI_EXCEPTION((AE_INFO, status,
  222. "Invoked method did not return a value"));
  223. }
  224. ACPI_EXCEPTION((AE_INFO, status,
  225. "GetPredicate Failed"));
  226. return_ACPI_STATUS(status);
  227. }
  228. status =
  229. acpi_ps_next_parse_state(walk_state, op,
  230. status);
  231. }
  232. acpi_ps_pop_scope(parser_state, &op,
  233. &walk_state->arg_types,
  234. &walk_state->arg_count);
  235. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  236. "Popped scope, Op=%p\n", op));
  237. } else if (walk_state->prev_op) {
  238. /* We were in the middle of an op */
  239. op = walk_state->prev_op;
  240. walk_state->arg_types = walk_state->prev_arg_types;
  241. }
  242. }
  243. #endif
  244. /* Iterative parsing loop, while there is more AML to process: */
  245. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  246. ASL_CV_CAPTURE_COMMENTS(walk_state);
  247. aml_op_start = parser_state->aml;
  248. if (!op) {
  249. status =
  250. acpi_ps_create_op(walk_state, aml_op_start, &op);
  251. if (ACPI_FAILURE(status)) {
  252. /*
  253. * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
  254. * executing it as a control method. However, if we encounter
  255. * an error while loading the table, we need to keep trying to
  256. * load the table rather than aborting the table load. Set the
  257. * status to AE_OK to proceed with the table load.
  258. */
  259. if ((walk_state->
  260. parse_flags & ACPI_PARSE_MODULE_LEVEL)
  261. && ((status == AE_ALREADY_EXISTS)
  262. || (status == AE_NOT_FOUND))) {
  263. status = AE_OK;
  264. }
  265. if (status == AE_CTRL_PARSE_CONTINUE) {
  266. continue;
  267. }
  268. if (status == AE_CTRL_PARSE_PENDING) {
  269. status = AE_OK;
  270. }
  271. if (status == AE_CTRL_TERMINATE) {
  272. return_ACPI_STATUS(status);
  273. }
  274. status =
  275. acpi_ps_complete_op(walk_state, &op,
  276. status);
  277. if (ACPI_FAILURE(status)) {
  278. return_ACPI_STATUS(status);
  279. }
  280. if (acpi_ns_opens_scope
  281. (acpi_ps_get_opcode_info
  282. (walk_state->opcode)->object_type)) {
  283. /*
  284. * If the scope/device op fails to parse, skip the body of
  285. * the scope op because the parse failure indicates that
  286. * the device may not exist.
  287. */
  288. ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state->opcode), walk_state->opcode));
  289. /*
  290. * Determine the opcode length before skipping the opcode.
  291. * An opcode can be 1 byte or 2 bytes in length.
  292. */
  293. opcode_length = 1;
  294. if ((walk_state->opcode & 0xFF00) ==
  295. AML_EXTENDED_OPCODE) {
  296. opcode_length = 2;
  297. }
  298. walk_state->parser_state.aml =
  299. walk_state->aml + opcode_length;
  300. walk_state->parser_state.aml =
  301. acpi_ps_get_next_package_end
  302. (&walk_state->parser_state);
  303. walk_state->aml =
  304. walk_state->parser_state.aml;
  305. }
  306. continue;
  307. }
  308. acpi_ex_start_trace_opcode(op, walk_state);
  309. }
  310. /*
  311. * Start arg_count at zero because we don't know if there are
  312. * any args yet
  313. */
  314. walk_state->arg_count = 0;
  315. switch (op->common.aml_opcode) {
  316. case AML_BYTE_OP:
  317. case AML_WORD_OP:
  318. case AML_DWORD_OP:
  319. case AML_QWORD_OP:
  320. break;
  321. default:
  322. ASL_CV_CAPTURE_COMMENTS(walk_state);
  323. break;
  324. }
  325. /* Are there any arguments that must be processed? */
  326. if (walk_state->arg_types) {
  327. /* Get arguments */
  328. status =
  329. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  330. if (ACPI_FAILURE(status)) {
  331. status =
  332. acpi_ps_complete_op(walk_state, &op,
  333. status);
  334. if (ACPI_FAILURE(status)) {
  335. return_ACPI_STATUS(status);
  336. }
  337. if ((walk_state->control_state) &&
  338. ((walk_state->control_state->control.
  339. opcode == AML_IF_OP)
  340. || (walk_state->control_state->control.
  341. opcode == AML_WHILE_OP))) {
  342. /*
  343. * If the if/while op fails to parse, we will skip parsing
  344. * the body of the op.
  345. */
  346. parser_state->aml =
  347. walk_state->control_state->control.
  348. aml_predicate_start + 1;
  349. parser_state->aml =
  350. acpi_ps_get_next_package_end
  351. (parser_state);
  352. walk_state->aml = parser_state->aml;
  353. ACPI_ERROR((AE_INFO,
  354. "Skipping While/If block"));
  355. if (*walk_state->aml == AML_ELSE_OP) {
  356. ACPI_ERROR((AE_INFO,
  357. "Skipping Else block"));
  358. walk_state->parser_state.aml =
  359. walk_state->aml + 1;
  360. walk_state->parser_state.aml =
  361. acpi_ps_get_next_package_end
  362. (parser_state);
  363. walk_state->aml =
  364. parser_state->aml;
  365. }
  366. ACPI_FREE(acpi_ut_pop_generic_state
  367. (&walk_state->control_state));
  368. }
  369. op = NULL;
  370. continue;
  371. }
  372. }
  373. /* Check for arguments that need to be processed */
  374. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  375. "Parseloop: argument count: %8.8X\n",
  376. walk_state->arg_count));
  377. if (walk_state->arg_count) {
  378. /*
  379. * There are arguments (complex ones), push Op and
  380. * prepare for argument
  381. */
  382. status = acpi_ps_push_scope(parser_state, op,
  383. walk_state->arg_types,
  384. walk_state->arg_count);
  385. if (ACPI_FAILURE(status)) {
  386. status =
  387. acpi_ps_complete_op(walk_state, &op,
  388. status);
  389. if (ACPI_FAILURE(status)) {
  390. return_ACPI_STATUS(status);
  391. }
  392. continue;
  393. }
  394. op = NULL;
  395. continue;
  396. }
  397. /*
  398. * All arguments have been processed -- Op is complete,
  399. * prepare for next
  400. */
  401. walk_state->op_info =
  402. acpi_ps_get_opcode_info(op->common.aml_opcode);
  403. if (walk_state->op_info->flags & AML_NAMED) {
  404. if (op->common.aml_opcode == AML_REGION_OP ||
  405. op->common.aml_opcode == AML_DATA_REGION_OP) {
  406. /*
  407. * Skip parsing of control method or opregion body,
  408. * because we don't have enough info in the first pass
  409. * to parse them correctly.
  410. *
  411. * Completed parsing an op_region declaration, we now
  412. * know the length.
  413. */
  414. op->named.length =
  415. (u32) (parser_state->aml - op->named.data);
  416. }
  417. }
  418. if (walk_state->op_info->flags & AML_CREATE) {
  419. /*
  420. * Backup to beginning of create_XXXfield declaration (1 for
  421. * Opcode)
  422. *
  423. * body_length is unknown until we parse the body
  424. */
  425. op->named.length =
  426. (u32) (parser_state->aml - op->named.data);
  427. }
  428. if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
  429. /*
  430. * Backup to beginning of bank_field declaration
  431. *
  432. * body_length is unknown until we parse the body
  433. */
  434. op->named.length =
  435. (u32) (parser_state->aml - op->named.data);
  436. }
  437. /* This op complete, notify the dispatcher */
  438. if (walk_state->ascending_callback != NULL) {
  439. walk_state->op = op;
  440. walk_state->opcode = op->common.aml_opcode;
  441. status = walk_state->ascending_callback(walk_state);
  442. status =
  443. acpi_ps_next_parse_state(walk_state, op, status);
  444. if (status == AE_CTRL_PENDING) {
  445. status = AE_OK;
  446. } else
  447. if ((walk_state->
  448. parse_flags & ACPI_PARSE_MODULE_LEVEL)
  449. && (ACPI_AML_EXCEPTION(status)
  450. || status == AE_ALREADY_EXISTS
  451. || status == AE_NOT_FOUND)) {
  452. /*
  453. * ACPI_PARSE_MODULE_LEVEL flag means that we
  454. * are currently loading a table by executing
  455. * it as a control method. However, if we
  456. * encounter an error while loading the table,
  457. * we need to keep trying to load the table
  458. * rather than aborting the table load (setting
  459. * the status to AE_OK continues the table
  460. * load). If we get a failure at this point, it
  461. * means that the dispatcher got an error while
  462. * trying to execute the Op.
  463. */
  464. status = AE_OK;
  465. }
  466. }
  467. status = acpi_ps_complete_op(walk_state, &op, status);
  468. if (ACPI_FAILURE(status)) {
  469. return_ACPI_STATUS(status);
  470. }
  471. } /* while parser_state->Aml */
  472. status = acpi_ps_complete_final_op(walk_state, op, status);
  473. return_ACPI_STATUS(status);
  474. }