psloop.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. }
  495. /* Close this iteration of the While loop */
  496. walk_state->op = *op;
  497. walk_state->op_info =
  498. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  499. walk_state->opcode = (*op)->common.aml_opcode;
  500. status = walk_state->ascending_callback(walk_state);
  501. status = acpi_ps_next_parse_state(walk_state, *op, status);
  502. status2 = acpi_ps_complete_this_op(walk_state, *op);
  503. if (ACPI_FAILURE(status2)) {
  504. return_ACPI_STATUS(status2);
  505. }
  506. status = AE_OK;
  507. break;
  508. case AE_CTRL_TERMINATE:
  509. /* Clean up */
  510. do {
  511. if (*op) {
  512. status2 =
  513. acpi_ps_complete_this_op(walk_state, *op);
  514. if (ACPI_FAILURE(status2)) {
  515. return_ACPI_STATUS(status2);
  516. }
  517. acpi_ut_delete_generic_state
  518. (acpi_ut_pop_generic_state
  519. (&walk_state->control_state));
  520. }
  521. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  522. &walk_state->arg_types,
  523. &walk_state->arg_count);
  524. } while (*op);
  525. return_ACPI_STATUS(AE_OK);
  526. default: /* All other non-AE_OK status */
  527. do {
  528. if (*op) {
  529. status2 =
  530. acpi_ps_complete_this_op(walk_state, *op);
  531. if (ACPI_FAILURE(status2)) {
  532. return_ACPI_STATUS(status2);
  533. }
  534. }
  535. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  536. &walk_state->arg_types,
  537. &walk_state->arg_count);
  538. } while (*op);
  539. #if 0
  540. /*
  541. * TBD: Cleanup parse ops on error
  542. */
  543. if (*op == NULL) {
  544. acpi_ps_pop_scope(parser_state, op,
  545. &walk_state->arg_types,
  546. &walk_state->arg_count);
  547. }
  548. #endif
  549. walk_state->prev_op = NULL;
  550. walk_state->prev_arg_types = walk_state->arg_types;
  551. return_ACPI_STATUS(status);
  552. }
  553. /* This scope complete? */
  554. if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
  555. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  556. &walk_state->arg_types,
  557. &walk_state->arg_count);
  558. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
  559. } else {
  560. *op = NULL;
  561. }
  562. return_ACPI_STATUS(AE_OK);
  563. }
  564. /*******************************************************************************
  565. *
  566. * FUNCTION: acpi_ps_complete_final_op
  567. *
  568. * PARAMETERS: walk_state - Current state
  569. * Op - Current Op
  570. * Status - Current parse status before complete last
  571. * Op
  572. *
  573. * RETURN: Status
  574. *
  575. * DESCRIPTION: Complete last Op.
  576. *
  577. ******************************************************************************/
  578. static acpi_status
  579. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  580. union acpi_parse_object *op, acpi_status status)
  581. {
  582. acpi_status status2;
  583. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  584. /*
  585. * Complete the last Op (if not completed), and clear the scope stack.
  586. * It is easily possible to end an AML "package" with an unbounded number
  587. * of open scopes (such as when several ASL blocks are closed with
  588. * sequential closing braces). We want to terminate each one cleanly.
  589. */
  590. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  591. op));
  592. do {
  593. if (op) {
  594. if (walk_state->ascending_callback != NULL) {
  595. walk_state->op = op;
  596. walk_state->op_info =
  597. acpi_ps_get_opcode_info(op->common.
  598. aml_opcode);
  599. walk_state->opcode = op->common.aml_opcode;
  600. status =
  601. walk_state->ascending_callback(walk_state);
  602. status =
  603. acpi_ps_next_parse_state(walk_state, op,
  604. status);
  605. if (status == AE_CTRL_PENDING) {
  606. status =
  607. acpi_ps_complete_op(walk_state, &op,
  608. AE_OK);
  609. if (ACPI_FAILURE(status)) {
  610. return_ACPI_STATUS(status);
  611. }
  612. }
  613. if (status == AE_CTRL_TERMINATE) {
  614. status = AE_OK;
  615. /* Clean up */
  616. do {
  617. if (op) {
  618. status2 =
  619. acpi_ps_complete_this_op
  620. (walk_state, op);
  621. if (ACPI_FAILURE
  622. (status2)) {
  623. return_ACPI_STATUS
  624. (status2);
  625. }
  626. }
  627. acpi_ps_pop_scope(&
  628. (walk_state->
  629. parser_state),
  630. &op,
  631. &walk_state->
  632. arg_types,
  633. &walk_state->
  634. arg_count);
  635. } while (op);
  636. return_ACPI_STATUS(status);
  637. }
  638. else if (ACPI_FAILURE(status)) {
  639. /* First error is most important */
  640. (void)
  641. acpi_ps_complete_this_op(walk_state,
  642. op);
  643. return_ACPI_STATUS(status);
  644. }
  645. }
  646. status2 = acpi_ps_complete_this_op(walk_state, op);
  647. if (ACPI_FAILURE(status2)) {
  648. return_ACPI_STATUS(status2);
  649. }
  650. }
  651. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  652. &walk_state->arg_types,
  653. &walk_state->arg_count);
  654. } while (op);
  655. return_ACPI_STATUS(status);
  656. }
  657. /*******************************************************************************
  658. *
  659. * FUNCTION: acpi_ps_parse_loop
  660. *
  661. * PARAMETERS: walk_state - Current state
  662. *
  663. * RETURN: Status
  664. *
  665. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  666. * a tree of ops.
  667. *
  668. ******************************************************************************/
  669. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  670. {
  671. acpi_status status = AE_OK;
  672. union acpi_parse_object *op = NULL; /* current op */
  673. struct acpi_parse_state *parser_state;
  674. u8 *aml_op_start = NULL;
  675. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  676. if (walk_state->descending_callback == NULL) {
  677. return_ACPI_STATUS(AE_BAD_PARAMETER);
  678. }
  679. parser_state = &walk_state->parser_state;
  680. walk_state->arg_types = 0;
  681. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  682. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  683. /* We are restarting a preempted control method */
  684. if (acpi_ps_has_completed_scope(parser_state)) {
  685. /*
  686. * We must check if a predicate to an IF or WHILE statement
  687. * was just completed
  688. */
  689. if ((parser_state->scope->parse_scope.op) &&
  690. ((parser_state->scope->parse_scope.op->common.
  691. aml_opcode == AML_IF_OP)
  692. || (parser_state->scope->parse_scope.op->common.
  693. aml_opcode == AML_WHILE_OP))
  694. && (walk_state->control_state)
  695. && (walk_state->control_state->common.state ==
  696. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  697. /*
  698. * A predicate was just completed, get the value of the
  699. * predicate and branch based on that value
  700. */
  701. walk_state->op = NULL;
  702. status =
  703. acpi_ds_get_predicate_value(walk_state,
  704. ACPI_TO_POINTER
  705. (TRUE));
  706. if (ACPI_FAILURE(status)
  707. && ((status & AE_CODE_MASK) !=
  708. AE_CODE_CONTROL)) {
  709. if (status == AE_AML_NO_RETURN_VALUE) {
  710. ACPI_EXCEPTION((AE_INFO, status,
  711. "Invoked method did not return a value"));
  712. }
  713. ACPI_EXCEPTION((AE_INFO, status,
  714. "GetPredicate Failed"));
  715. return_ACPI_STATUS(status);
  716. }
  717. status =
  718. acpi_ps_next_parse_state(walk_state, op,
  719. status);
  720. }
  721. acpi_ps_pop_scope(parser_state, &op,
  722. &walk_state->arg_types,
  723. &walk_state->arg_count);
  724. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  725. "Popped scope, Op=%p\n", op));
  726. } else if (walk_state->prev_op) {
  727. /* We were in the middle of an op */
  728. op = walk_state->prev_op;
  729. walk_state->arg_types = walk_state->prev_arg_types;
  730. }
  731. }
  732. #endif
  733. /* Iterative parsing loop, while there is more AML to process: */
  734. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  735. aml_op_start = parser_state->aml;
  736. if (!op) {
  737. status =
  738. acpi_ps_create_op(walk_state, aml_op_start, &op);
  739. if (ACPI_FAILURE(status)) {
  740. if (status == AE_CTRL_PARSE_CONTINUE) {
  741. continue;
  742. }
  743. if (status == AE_CTRL_PARSE_PENDING) {
  744. status = AE_OK;
  745. }
  746. status =
  747. acpi_ps_complete_op(walk_state, &op,
  748. status);
  749. if (ACPI_FAILURE(status)) {
  750. return_ACPI_STATUS(status);
  751. }
  752. continue;
  753. }
  754. op->common.aml_offset = walk_state->aml_offset;
  755. if (walk_state->op_info) {
  756. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  757. "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
  758. (u32) op->common.aml_opcode,
  759. walk_state->op_info->name, op,
  760. parser_state->aml,
  761. op->common.aml_offset));
  762. }
  763. }
  764. /*
  765. * Start arg_count at zero because we don't know if there are
  766. * any args yet
  767. */
  768. walk_state->arg_count = 0;
  769. /* Are there any arguments that must be processed? */
  770. if (walk_state->arg_types) {
  771. /* Get arguments */
  772. status =
  773. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  774. if (ACPI_FAILURE(status)) {
  775. status =
  776. acpi_ps_complete_op(walk_state, &op,
  777. status);
  778. if (ACPI_FAILURE(status)) {
  779. return_ACPI_STATUS(status);
  780. }
  781. continue;
  782. }
  783. }
  784. /* Check for arguments that need to be processed */
  785. if (walk_state->arg_count) {
  786. /*
  787. * There are arguments (complex ones), push Op and
  788. * prepare for argument
  789. */
  790. status = acpi_ps_push_scope(parser_state, op,
  791. walk_state->arg_types,
  792. walk_state->arg_count);
  793. if (ACPI_FAILURE(status)) {
  794. status =
  795. acpi_ps_complete_op(walk_state, &op,
  796. status);
  797. if (ACPI_FAILURE(status)) {
  798. return_ACPI_STATUS(status);
  799. }
  800. continue;
  801. }
  802. op = NULL;
  803. continue;
  804. }
  805. /*
  806. * All arguments have been processed -- Op is complete,
  807. * prepare for next
  808. */
  809. walk_state->op_info =
  810. acpi_ps_get_opcode_info(op->common.aml_opcode);
  811. if (walk_state->op_info->flags & AML_NAMED) {
  812. if (acpi_gbl_depth) {
  813. acpi_gbl_depth--;
  814. }
  815. if (op->common.aml_opcode == AML_REGION_OP) {
  816. /*
  817. * Skip parsing of control method or opregion body,
  818. * because we don't have enough info in the first pass
  819. * to parse them correctly.
  820. *
  821. * Completed parsing an op_region declaration, we now
  822. * know the length.
  823. */
  824. op->named.length =
  825. (u32) (parser_state->aml - op->named.data);
  826. }
  827. }
  828. if (walk_state->op_info->flags & AML_CREATE) {
  829. /*
  830. * Backup to beginning of create_xXXfield declaration (1 for
  831. * Opcode)
  832. *
  833. * body_length is unknown until we parse the body
  834. */
  835. op->named.length =
  836. (u32) (parser_state->aml - op->named.data);
  837. }
  838. /* This op complete, notify the dispatcher */
  839. if (walk_state->ascending_callback != NULL) {
  840. walk_state->op = op;
  841. walk_state->opcode = op->common.aml_opcode;
  842. status = walk_state->ascending_callback(walk_state);
  843. status =
  844. acpi_ps_next_parse_state(walk_state, op, status);
  845. if (status == AE_CTRL_PENDING) {
  846. status = AE_OK;
  847. }
  848. }
  849. status = acpi_ps_complete_op(walk_state, &op, status);
  850. if (ACPI_FAILURE(status)) {
  851. return_ACPI_STATUS(status);
  852. }
  853. } /* while parser_state->Aml */
  854. status = acpi_ps_complete_final_op(walk_state, op, status);
  855. return_ACPI_STATUS(status);
  856. }