psloop.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /******************************************************************************
  2. *
  3. * Module Name: psloop - Main AML parse loop
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2007, R. Byron Moore
  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 <acpi/acparser.h>
  51. #include <acpi/acdispat.h>
  52. #include <acpi/amlcode.h>
  53. #define _COMPONENT ACPI_PARSER
  54. ACPI_MODULE_NAME("psloop")
  55. static u32 acpi_gbl_depth = 0;
  56. /* Local prototypes */
  57. static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
  58. static acpi_status
  59. acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
  60. u8 * aml_op_start,
  61. union acpi_parse_object *unnamed_op,
  62. union acpi_parse_object **op);
  63. static acpi_status
  64. acpi_ps_create_op(struct acpi_walk_state *walk_state,
  65. u8 * aml_op_start, union acpi_parse_object **new_op);
  66. static acpi_status
  67. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  68. u8 * aml_op_start, union acpi_parse_object *op);
  69. static acpi_status
  70. acpi_ps_complete_op(struct acpi_walk_state *walk_state,
  71. union acpi_parse_object **op, acpi_status status);
  72. static acpi_status
  73. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  74. union acpi_parse_object *op, acpi_status status);
  75. /*******************************************************************************
  76. *
  77. * FUNCTION: acpi_ps_get_aml_opcode
  78. *
  79. * PARAMETERS: walk_state - Current state
  80. *
  81. * RETURN: Status
  82. *
  83. * DESCRIPTION: Extract the next AML opcode from the input stream.
  84. *
  85. ******************************************************************************/
  86. static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
  87. {
  88. ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
  89. walk_state->aml_offset =
  90. (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
  91. walk_state->parser_state.aml_start);
  92. walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
  93. /*
  94. * First cut to determine what we have found:
  95. * 1) A valid AML opcode
  96. * 2) A name string
  97. * 3) An unknown/invalid opcode
  98. */
  99. walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
  100. switch (walk_state->op_info->class) {
  101. case AML_CLASS_ASCII:
  102. case AML_CLASS_PREFIX:
  103. /*
  104. * Starts with a valid prefix or ASCII char, this is a name
  105. * string. Convert the bare name string to a namepath.
  106. */
  107. walk_state->opcode = AML_INT_NAMEPATH_OP;
  108. walk_state->arg_types = ARGP_NAMESTRING;
  109. break;
  110. case AML_CLASS_UNKNOWN:
  111. /* The opcode is unrecognized. Just skip unknown opcodes */
  112. ACPI_ERROR((AE_INFO,
  113. "Found unknown opcode %X at AML address %p offset %X, ignoring",
  114. walk_state->opcode, walk_state->parser_state.aml,
  115. walk_state->aml_offset));
  116. ACPI_DUMP_BUFFER(walk_state->parser_state.aml, 128);
  117. /* Assume one-byte bad opcode */
  118. walk_state->parser_state.aml++;
  119. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  120. default:
  121. /* Found opcode info, this is a normal opcode */
  122. walk_state->parser_state.aml +=
  123. acpi_ps_get_opcode_size(walk_state->opcode);
  124. walk_state->arg_types = walk_state->op_info->parse_args;
  125. break;
  126. }
  127. return_ACPI_STATUS(AE_OK);
  128. }
  129. /*******************************************************************************
  130. *
  131. * FUNCTION: acpi_ps_build_named_op
  132. *
  133. * PARAMETERS: walk_state - Current state
  134. * aml_op_start - Begin of named Op in AML
  135. * unnamed_op - Early Op (not a named Op)
  136. * Op - Returned Op
  137. *
  138. * RETURN: Status
  139. *
  140. * DESCRIPTION: Parse a named Op
  141. *
  142. ******************************************************************************/
  143. static acpi_status
  144. acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
  145. u8 * aml_op_start,
  146. union acpi_parse_object *unnamed_op,
  147. union acpi_parse_object **op)
  148. {
  149. acpi_status status = AE_OK;
  150. union acpi_parse_object *arg = NULL;
  151. ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
  152. unnamed_op->common.value.arg = NULL;
  153. unnamed_op->common.aml_opcode = walk_state->opcode;
  154. /*
  155. * Get and append arguments until we find the node that contains
  156. * the name (the type ARGP_NAME).
  157. */
  158. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
  159. (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
  160. status =
  161. acpi_ps_get_next_arg(walk_state,
  162. &(walk_state->parser_state),
  163. GET_CURRENT_ARG_TYPE(walk_state->
  164. arg_types), &arg);
  165. if (ACPI_FAILURE(status)) {
  166. return_ACPI_STATUS(status);
  167. }
  168. acpi_ps_append_arg(unnamed_op, arg);
  169. INCREMENT_ARG_LIST(walk_state->arg_types);
  170. }
  171. /*
  172. * Make sure that we found a NAME and didn't run out of arguments
  173. */
  174. if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
  175. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  176. }
  177. /* We know that this arg is a name, move to next arg */
  178. INCREMENT_ARG_LIST(walk_state->arg_types);
  179. /*
  180. * Find the object. This will either insert the object into
  181. * the namespace or simply look it up
  182. */
  183. walk_state->op = NULL;
  184. status = walk_state->descending_callback(walk_state, op);
  185. if (ACPI_FAILURE(status)) {
  186. ACPI_EXCEPTION((AE_INFO, status, "During name lookup/catalog"));
  187. return_ACPI_STATUS(status);
  188. }
  189. if (!*op) {
  190. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  191. }
  192. status = acpi_ps_next_parse_state(walk_state, *op, status);
  193. if (ACPI_FAILURE(status)) {
  194. if (status == AE_CTRL_PENDING) {
  195. return_ACPI_STATUS(AE_CTRL_PARSE_PENDING);
  196. }
  197. return_ACPI_STATUS(status);
  198. }
  199. acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
  200. acpi_gbl_depth++;
  201. if ((*op)->common.aml_opcode == AML_REGION_OP) {
  202. /*
  203. * Defer final parsing of an operation_region body, because we don't
  204. * have enough info in the first pass to parse it correctly (i.e.,
  205. * there may be method calls within the term_arg elements of the body.)
  206. *
  207. * However, we must continue parsing because the opregion is not a
  208. * standalone package -- we don't know where the end is at this point.
  209. *
  210. * (Length is unknown until parse of the body complete)
  211. */
  212. (*op)->named.data = aml_op_start;
  213. (*op)->named.length = 0;
  214. }
  215. return_ACPI_STATUS(AE_OK);
  216. }
  217. /*******************************************************************************
  218. *
  219. * FUNCTION: acpi_ps_create_op
  220. *
  221. * PARAMETERS: walk_state - Current state
  222. * aml_op_start - Op start in AML
  223. * new_op - Returned Op
  224. *
  225. * RETURN: Status
  226. *
  227. * DESCRIPTION: Get Op from AML
  228. *
  229. ******************************************************************************/
  230. static acpi_status
  231. acpi_ps_create_op(struct acpi_walk_state *walk_state,
  232. u8 * aml_op_start, union acpi_parse_object **new_op)
  233. {
  234. acpi_status status = AE_OK;
  235. union acpi_parse_object *op;
  236. union acpi_parse_object *named_op = NULL;
  237. ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
  238. status = acpi_ps_get_aml_opcode(walk_state);
  239. if (status == AE_CTRL_PARSE_CONTINUE) {
  240. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  241. }
  242. /* Create Op structure and append to parent's argument list */
  243. walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
  244. op = acpi_ps_alloc_op(walk_state->opcode);
  245. if (!op) {
  246. return_ACPI_STATUS(AE_NO_MEMORY);
  247. }
  248. if (walk_state->op_info->flags & AML_NAMED) {
  249. status =
  250. acpi_ps_build_named_op(walk_state, aml_op_start, op,
  251. &named_op);
  252. acpi_ps_free_op(op);
  253. if (ACPI_FAILURE(status)) {
  254. return_ACPI_STATUS(status);
  255. }
  256. *new_op = named_op;
  257. return_ACPI_STATUS(AE_OK);
  258. }
  259. /* Not a named opcode, just allocate Op and append to parent */
  260. if (walk_state->op_info->flags & AML_CREATE) {
  261. /*
  262. * Backup to beginning of create_xXXfield declaration
  263. * body_length is unknown until we parse the body
  264. */
  265. op->named.data = aml_op_start;
  266. op->named.length = 0;
  267. }
  268. acpi_ps_append_arg(acpi_ps_get_parent_scope
  269. (&(walk_state->parser_state)), op);
  270. if (walk_state->descending_callback != NULL) {
  271. /*
  272. * Find the object. This will either insert the object into
  273. * the namespace or simply look it up
  274. */
  275. walk_state->op = *new_op = op;
  276. status = walk_state->descending_callback(walk_state, &op);
  277. status = acpi_ps_next_parse_state(walk_state, op, status);
  278. if (status == AE_CTRL_PENDING) {
  279. status = AE_CTRL_PARSE_PENDING;
  280. }
  281. }
  282. return_ACPI_STATUS(status);
  283. }
  284. /*******************************************************************************
  285. *
  286. * FUNCTION: acpi_ps_get_arguments
  287. *
  288. * PARAMETERS: walk_state - Current state
  289. * aml_op_start - Op start in AML
  290. * Op - Current Op
  291. *
  292. * RETURN: Status
  293. *
  294. * DESCRIPTION: Get arguments for passed Op.
  295. *
  296. ******************************************************************************/
  297. static acpi_status
  298. acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
  299. u8 * aml_op_start, union acpi_parse_object *op)
  300. {
  301. acpi_status status = AE_OK;
  302. union acpi_parse_object *arg = NULL;
  303. ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
  304. switch (op->common.aml_opcode) {
  305. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  306. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  307. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  308. case AML_QWORD_OP: /* AML_QWORDATA_ARG */
  309. case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
  310. /* Fill in constant or string argument directly */
  311. acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
  312. GET_CURRENT_ARG_TYPE(walk_state->
  313. arg_types),
  314. op);
  315. break;
  316. case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
  317. status =
  318. acpi_ps_get_next_namepath(walk_state,
  319. &(walk_state->parser_state), op,
  320. 1);
  321. if (ACPI_FAILURE(status)) {
  322. return_ACPI_STATUS(status);
  323. }
  324. walk_state->arg_types = 0;
  325. break;
  326. default:
  327. /*
  328. * Op is not a constant or string, append each argument to the Op
  329. */
  330. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types)
  331. && !walk_state->arg_count) {
  332. walk_state->aml_offset =
  333. (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
  334. walk_state->parser_state.
  335. aml_start);
  336. status =
  337. acpi_ps_get_next_arg(walk_state,
  338. &(walk_state->parser_state),
  339. GET_CURRENT_ARG_TYPE
  340. (walk_state->arg_types), &arg);
  341. if (ACPI_FAILURE(status)) {
  342. return_ACPI_STATUS(status);
  343. }
  344. if (arg) {
  345. arg->common.aml_offset = walk_state->aml_offset;
  346. acpi_ps_append_arg(op, arg);
  347. }
  348. INCREMENT_ARG_LIST(walk_state->arg_types);
  349. }
  350. /* Special processing for certain opcodes */
  351. /* TBD (remove): Temporary mechanism to disable this code if needed */
  352. #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
  353. if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS1) &&
  354. ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
  355. /*
  356. * We want to skip If/Else/While constructs during Pass1 because we
  357. * want to actually conditionally execute the code during Pass2.
  358. *
  359. * Except for disassembly, where we always want to walk the
  360. * If/Else/While packages
  361. */
  362. switch (op->common.aml_opcode) {
  363. case AML_IF_OP:
  364. case AML_ELSE_OP:
  365. case AML_WHILE_OP:
  366. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  367. "Pass1: Skipping an If/Else/While body\n"));
  368. /* Skip body of if/else/while in pass 1 */
  369. walk_state->parser_state.aml =
  370. walk_state->parser_state.pkg_end;
  371. walk_state->arg_count = 0;
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. #endif
  378. switch (op->common.aml_opcode) {
  379. case AML_METHOD_OP:
  380. /*
  381. * Skip parsing of control method because we don't have enough
  382. * info in the first pass to parse it correctly.
  383. *
  384. * Save the length and address of the body
  385. */
  386. op->named.data = walk_state->parser_state.aml;
  387. op->named.length = (u32)
  388. (walk_state->parser_state.pkg_end -
  389. walk_state->parser_state.aml);
  390. /* Skip body of method */
  391. walk_state->parser_state.aml =
  392. walk_state->parser_state.pkg_end;
  393. walk_state->arg_count = 0;
  394. break;
  395. case AML_BUFFER_OP:
  396. case AML_PACKAGE_OP:
  397. case AML_VAR_PACKAGE_OP:
  398. if ((op->common.parent) &&
  399. (op->common.parent->common.aml_opcode ==
  400. AML_NAME_OP)
  401. && (walk_state->pass_number <=
  402. ACPI_IMODE_LOAD_PASS2)) {
  403. /*
  404. * Skip parsing of Buffers and Packages because we don't have
  405. * enough info in the first pass to parse them correctly.
  406. */
  407. op->named.data = aml_op_start;
  408. op->named.length = (u32)
  409. (walk_state->parser_state.pkg_end -
  410. aml_op_start);
  411. /* Skip body */
  412. walk_state->parser_state.aml =
  413. walk_state->parser_state.pkg_end;
  414. walk_state->arg_count = 0;
  415. }
  416. break;
  417. case AML_WHILE_OP:
  418. if (walk_state->control_state) {
  419. walk_state->control_state->control.package_end =
  420. walk_state->parser_state.pkg_end;
  421. }
  422. break;
  423. default:
  424. /* No action for all other opcodes */
  425. break;
  426. }
  427. break;
  428. }
  429. return_ACPI_STATUS(AE_OK);
  430. }
  431. /*******************************************************************************
  432. *
  433. * FUNCTION: acpi_ps_complete_op
  434. *
  435. * PARAMETERS: walk_state - Current state
  436. * Op - Returned Op
  437. * Status - Parse status before complete Op
  438. *
  439. * RETURN: Status
  440. *
  441. * DESCRIPTION: Complete Op
  442. *
  443. ******************************************************************************/
  444. static acpi_status
  445. acpi_ps_complete_op(struct acpi_walk_state *walk_state,
  446. union acpi_parse_object **op, acpi_status status)
  447. {
  448. acpi_status status2;
  449. ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
  450. /*
  451. * Finished one argument of the containing scope
  452. */
  453. walk_state->parser_state.scope->parse_scope.arg_count--;
  454. /* Close this Op (will result in parse subtree deletion) */
  455. status2 = acpi_ps_complete_this_op(walk_state, *op);
  456. if (ACPI_FAILURE(status2)) {
  457. return_ACPI_STATUS(status2);
  458. }
  459. *op = NULL;
  460. switch (status) {
  461. case AE_OK:
  462. break;
  463. case AE_CTRL_TRANSFER:
  464. /* We are about to transfer to a called method */
  465. walk_state->prev_op = NULL;
  466. walk_state->prev_arg_types = walk_state->arg_types;
  467. return_ACPI_STATUS(status);
  468. case AE_CTRL_END:
  469. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  470. &walk_state->arg_types,
  471. &walk_state->arg_count);
  472. if (*op) {
  473. walk_state->op = *op;
  474. walk_state->op_info =
  475. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  476. walk_state->opcode = (*op)->common.aml_opcode;
  477. status = walk_state->ascending_callback(walk_state);
  478. status =
  479. acpi_ps_next_parse_state(walk_state, *op, status);
  480. status2 = acpi_ps_complete_this_op(walk_state, *op);
  481. if (ACPI_FAILURE(status2)) {
  482. return_ACPI_STATUS(status2);
  483. }
  484. }
  485. status = AE_OK;
  486. break;
  487. case AE_CTRL_BREAK:
  488. case AE_CTRL_CONTINUE:
  489. /* Pop off scopes until we find the While */
  490. while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
  491. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  492. &walk_state->arg_types,
  493. &walk_state->arg_count);
  494. if ((*op)->common.aml_opcode != AML_WHILE_OP) {
  495. status2 = acpi_ds_result_stack_pop(walk_state);
  496. if (ACPI_FAILURE(status2)) {
  497. return_ACPI_STATUS(status2);
  498. }
  499. }
  500. }
  501. /* Close this iteration of the While loop */
  502. walk_state->op = *op;
  503. walk_state->op_info =
  504. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  505. walk_state->opcode = (*op)->common.aml_opcode;
  506. status = walk_state->ascending_callback(walk_state);
  507. status = acpi_ps_next_parse_state(walk_state, *op, status);
  508. status2 = acpi_ps_complete_this_op(walk_state, *op);
  509. if (ACPI_FAILURE(status2)) {
  510. return_ACPI_STATUS(status2);
  511. }
  512. status = AE_OK;
  513. break;
  514. case AE_CTRL_TERMINATE:
  515. /* Clean up */
  516. do {
  517. if (*op) {
  518. status2 =
  519. acpi_ps_complete_this_op(walk_state, *op);
  520. if (ACPI_FAILURE(status2)) {
  521. return_ACPI_STATUS(status2);
  522. }
  523. status2 = acpi_ds_result_stack_pop(walk_state);
  524. if (ACPI_FAILURE(status2)) {
  525. return_ACPI_STATUS(status2);
  526. }
  527. acpi_ut_delete_generic_state
  528. (acpi_ut_pop_generic_state
  529. (&walk_state->control_state));
  530. }
  531. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  532. &walk_state->arg_types,
  533. &walk_state->arg_count);
  534. } while (*op);
  535. return_ACPI_STATUS(AE_OK);
  536. default: /* All other non-AE_OK status */
  537. do {
  538. if (*op) {
  539. status2 =
  540. acpi_ps_complete_this_op(walk_state, *op);
  541. if (ACPI_FAILURE(status2)) {
  542. return_ACPI_STATUS(status2);
  543. }
  544. }
  545. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  546. &walk_state->arg_types,
  547. &walk_state->arg_count);
  548. } while (*op);
  549. #if 0
  550. /*
  551. * TBD: Cleanup parse ops on error
  552. */
  553. if (*op == NULL) {
  554. acpi_ps_pop_scope(parser_state, op,
  555. &walk_state->arg_types,
  556. &walk_state->arg_count);
  557. }
  558. #endif
  559. walk_state->prev_op = NULL;
  560. walk_state->prev_arg_types = walk_state->arg_types;
  561. return_ACPI_STATUS(status);
  562. }
  563. /* This scope complete? */
  564. if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
  565. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  566. &walk_state->arg_types,
  567. &walk_state->arg_count);
  568. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
  569. } else {
  570. *op = NULL;
  571. }
  572. return_ACPI_STATUS(AE_OK);
  573. }
  574. /*******************************************************************************
  575. *
  576. * FUNCTION: acpi_ps_complete_final_op
  577. *
  578. * PARAMETERS: walk_state - Current state
  579. * Op - Current Op
  580. * Status - Current parse status before complete last
  581. * Op
  582. *
  583. * RETURN: Status
  584. *
  585. * DESCRIPTION: Complete last Op.
  586. *
  587. ******************************************************************************/
  588. static acpi_status
  589. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  590. union acpi_parse_object *op, acpi_status status)
  591. {
  592. acpi_status status2;
  593. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  594. /*
  595. * Complete the last Op (if not completed), and clear the scope stack.
  596. * It is easily possible to end an AML "package" with an unbounded number
  597. * of open scopes (such as when several ASL blocks are closed with
  598. * sequential closing braces). We want to terminate each one cleanly.
  599. */
  600. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  601. op));
  602. do {
  603. if (op) {
  604. if (walk_state->ascending_callback != NULL) {
  605. walk_state->op = op;
  606. walk_state->op_info =
  607. acpi_ps_get_opcode_info(op->common.
  608. aml_opcode);
  609. walk_state->opcode = op->common.aml_opcode;
  610. status =
  611. walk_state->ascending_callback(walk_state);
  612. status =
  613. acpi_ps_next_parse_state(walk_state, op,
  614. status);
  615. if (status == AE_CTRL_PENDING) {
  616. status =
  617. acpi_ps_complete_op(walk_state, &op,
  618. AE_OK);
  619. if (ACPI_FAILURE(status)) {
  620. return_ACPI_STATUS(status);
  621. }
  622. }
  623. if (status == AE_CTRL_TERMINATE) {
  624. status = AE_OK;
  625. /* Clean up */
  626. do {
  627. if (op) {
  628. status2 =
  629. acpi_ps_complete_this_op
  630. (walk_state, op);
  631. if (ACPI_FAILURE
  632. (status2)) {
  633. return_ACPI_STATUS
  634. (status2);
  635. }
  636. }
  637. acpi_ps_pop_scope(&
  638. (walk_state->
  639. parser_state),
  640. &op,
  641. &walk_state->
  642. arg_types,
  643. &walk_state->
  644. arg_count);
  645. } while (op);
  646. return_ACPI_STATUS(status);
  647. }
  648. else if (ACPI_FAILURE(status)) {
  649. /* First error is most important */
  650. (void)
  651. acpi_ps_complete_this_op(walk_state,
  652. op);
  653. return_ACPI_STATUS(status);
  654. }
  655. }
  656. status2 = acpi_ps_complete_this_op(walk_state, op);
  657. if (ACPI_FAILURE(status2)) {
  658. return_ACPI_STATUS(status2);
  659. }
  660. }
  661. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  662. &walk_state->arg_types,
  663. &walk_state->arg_count);
  664. } while (op);
  665. return_ACPI_STATUS(status);
  666. }
  667. /*******************************************************************************
  668. *
  669. * FUNCTION: acpi_ps_parse_loop
  670. *
  671. * PARAMETERS: walk_state - Current state
  672. *
  673. * RETURN: Status
  674. *
  675. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  676. * a tree of ops.
  677. *
  678. ******************************************************************************/
  679. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  680. {
  681. acpi_status status = AE_OK;
  682. union acpi_parse_object *op = NULL; /* current op */
  683. struct acpi_parse_state *parser_state;
  684. u8 *aml_op_start = NULL;
  685. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  686. if (walk_state->descending_callback == NULL) {
  687. return_ACPI_STATUS(AE_BAD_PARAMETER);
  688. }
  689. parser_state = &walk_state->parser_state;
  690. walk_state->arg_types = 0;
  691. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  692. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  693. /* We are restarting a preempted control method */
  694. if (acpi_ps_has_completed_scope(parser_state)) {
  695. /*
  696. * We must check if a predicate to an IF or WHILE statement
  697. * was just completed
  698. */
  699. if ((parser_state->scope->parse_scope.op) &&
  700. ((parser_state->scope->parse_scope.op->common.
  701. aml_opcode == AML_IF_OP)
  702. || (parser_state->scope->parse_scope.op->common.
  703. aml_opcode == AML_WHILE_OP))
  704. && (walk_state->control_state)
  705. && (walk_state->control_state->common.state ==
  706. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  707. /*
  708. * A predicate was just completed, get the value of the
  709. * predicate and branch based on that value
  710. */
  711. walk_state->op = NULL;
  712. status =
  713. acpi_ds_get_predicate_value(walk_state,
  714. ACPI_TO_POINTER
  715. (TRUE));
  716. if (ACPI_FAILURE(status)
  717. && ((status & AE_CODE_MASK) !=
  718. AE_CODE_CONTROL)) {
  719. if (status == AE_AML_NO_RETURN_VALUE) {
  720. ACPI_EXCEPTION((AE_INFO, status,
  721. "Invoked method did not return a value"));
  722. }
  723. ACPI_EXCEPTION((AE_INFO, status,
  724. "GetPredicate Failed"));
  725. return_ACPI_STATUS(status);
  726. }
  727. status =
  728. acpi_ps_next_parse_state(walk_state, op,
  729. status);
  730. }
  731. acpi_ps_pop_scope(parser_state, &op,
  732. &walk_state->arg_types,
  733. &walk_state->arg_count);
  734. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  735. "Popped scope, Op=%p\n", op));
  736. } else if (walk_state->prev_op) {
  737. /* We were in the middle of an op */
  738. op = walk_state->prev_op;
  739. walk_state->arg_types = walk_state->prev_arg_types;
  740. }
  741. }
  742. #endif
  743. /* Iterative parsing loop, while there is more AML to process: */
  744. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  745. aml_op_start = parser_state->aml;
  746. if (!op) {
  747. status =
  748. acpi_ps_create_op(walk_state, aml_op_start, &op);
  749. if (ACPI_FAILURE(status)) {
  750. if (status == AE_CTRL_PARSE_CONTINUE) {
  751. continue;
  752. }
  753. if (status == AE_CTRL_PARSE_PENDING) {
  754. status = AE_OK;
  755. }
  756. status =
  757. acpi_ps_complete_op(walk_state, &op,
  758. status);
  759. if (ACPI_FAILURE(status)) {
  760. return_ACPI_STATUS(status);
  761. }
  762. continue;
  763. }
  764. op->common.aml_offset = walk_state->aml_offset;
  765. if (walk_state->op_info) {
  766. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  767. "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
  768. (u32) op->common.aml_opcode,
  769. walk_state->op_info->name, op,
  770. parser_state->aml,
  771. op->common.aml_offset));
  772. }
  773. }
  774. /*
  775. * Start arg_count at zero because we don't know if there are
  776. * any args yet
  777. */
  778. walk_state->arg_count = 0;
  779. /* Are there any arguments that must be processed? */
  780. if (walk_state->arg_types) {
  781. /* Get arguments */
  782. status =
  783. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  784. if (ACPI_FAILURE(status)) {
  785. status =
  786. acpi_ps_complete_op(walk_state, &op,
  787. status);
  788. if (ACPI_FAILURE(status)) {
  789. return_ACPI_STATUS(status);
  790. }
  791. continue;
  792. }
  793. }
  794. /* Check for arguments that need to be processed */
  795. if (walk_state->arg_count) {
  796. /*
  797. * There are arguments (complex ones), push Op and
  798. * prepare for argument
  799. */
  800. status = acpi_ps_push_scope(parser_state, op,
  801. walk_state->arg_types,
  802. walk_state->arg_count);
  803. if (ACPI_FAILURE(status)) {
  804. status =
  805. acpi_ps_complete_op(walk_state, &op,
  806. status);
  807. if (ACPI_FAILURE(status)) {
  808. return_ACPI_STATUS(status);
  809. }
  810. continue;
  811. }
  812. op = NULL;
  813. continue;
  814. }
  815. /*
  816. * All arguments have been processed -- Op is complete,
  817. * prepare for next
  818. */
  819. walk_state->op_info =
  820. acpi_ps_get_opcode_info(op->common.aml_opcode);
  821. if (walk_state->op_info->flags & AML_NAMED) {
  822. if (acpi_gbl_depth) {
  823. acpi_gbl_depth--;
  824. }
  825. if (op->common.aml_opcode == AML_REGION_OP) {
  826. /*
  827. * Skip parsing of control method or opregion body,
  828. * because we don't have enough info in the first pass
  829. * to parse them correctly.
  830. *
  831. * Completed parsing an op_region declaration, we now
  832. * know the length.
  833. */
  834. op->named.length =
  835. (u32) (parser_state->aml - op->named.data);
  836. }
  837. }
  838. if (walk_state->op_info->flags & AML_CREATE) {
  839. /*
  840. * Backup to beginning of create_xXXfield declaration (1 for
  841. * Opcode)
  842. *
  843. * body_length is unknown until we parse the body
  844. */
  845. op->named.length =
  846. (u32) (parser_state->aml - op->named.data);
  847. }
  848. /* This op complete, notify the dispatcher */
  849. if (walk_state->ascending_callback != NULL) {
  850. walk_state->op = op;
  851. walk_state->opcode = op->common.aml_opcode;
  852. status = walk_state->ascending_callback(walk_state);
  853. status =
  854. acpi_ps_next_parse_state(walk_state, op, status);
  855. if (status == AE_CTRL_PENDING) {
  856. status = AE_OK;
  857. }
  858. }
  859. status = acpi_ps_complete_op(walk_state, &op, status);
  860. if (ACPI_FAILURE(status)) {
  861. return_ACPI_STATUS(status);
  862. }
  863. } /* while parser_state->Aml */
  864. status = acpi_ps_complete_final_op(walk_state, op, status);
  865. return_ACPI_STATUS(status);
  866. }