psloop.c 28 KB

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