psloop.c 28 KB

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