psloop.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. return_ACPI_STATUS(AE_OK);
  590. }
  591. /*******************************************************************************
  592. *
  593. * FUNCTION: acpi_ps_complete_final_op
  594. *
  595. * PARAMETERS: walk_state - Current state
  596. * Op - Current Op
  597. * Status - Current parse status before complete last
  598. * Op
  599. *
  600. * RETURN: Status
  601. *
  602. * DESCRIPTION: Complete last Op.
  603. *
  604. ******************************************************************************/
  605. static acpi_status
  606. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  607. union acpi_parse_object *op, acpi_status status)
  608. {
  609. acpi_status status2;
  610. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  611. /*
  612. * Complete the last Op (if not completed), and clear the scope stack.
  613. * It is easily possible to end an AML "package" with an unbounded number
  614. * of open scopes (such as when several ASL blocks are closed with
  615. * sequential closing braces). We want to terminate each one cleanly.
  616. */
  617. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  618. op));
  619. do {
  620. if (op) {
  621. if (walk_state->ascending_callback != NULL) {
  622. walk_state->op = op;
  623. walk_state->op_info =
  624. acpi_ps_get_opcode_info(op->common.
  625. aml_opcode);
  626. walk_state->opcode = op->common.aml_opcode;
  627. status =
  628. walk_state->ascending_callback(walk_state);
  629. status =
  630. acpi_ps_next_parse_state(walk_state, op,
  631. status);
  632. if (status == AE_CTRL_PENDING) {
  633. status =
  634. acpi_ps_complete_op(walk_state, &op,
  635. AE_OK);
  636. if (ACPI_FAILURE(status)) {
  637. return_ACPI_STATUS(status);
  638. }
  639. }
  640. if (status == AE_CTRL_TERMINATE) {
  641. status = AE_OK;
  642. /* Clean up */
  643. do {
  644. if (op) {
  645. status2 =
  646. acpi_ps_complete_this_op
  647. (walk_state, op);
  648. if (ACPI_FAILURE
  649. (status2)) {
  650. return_ACPI_STATUS
  651. (status2);
  652. }
  653. }
  654. acpi_ps_pop_scope(&
  655. (walk_state->
  656. parser_state),
  657. &op,
  658. &walk_state->
  659. arg_types,
  660. &walk_state->
  661. arg_count);
  662. } while (op);
  663. return_ACPI_STATUS(status);
  664. }
  665. else if (ACPI_FAILURE(status)) {
  666. /* First error is most important */
  667. (void)
  668. acpi_ps_complete_this_op(walk_state,
  669. op);
  670. return_ACPI_STATUS(status);
  671. }
  672. }
  673. status2 = acpi_ps_complete_this_op(walk_state, op);
  674. if (ACPI_FAILURE(status2)) {
  675. return_ACPI_STATUS(status2);
  676. }
  677. }
  678. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  679. &walk_state->arg_types,
  680. &walk_state->arg_count);
  681. } while (op);
  682. return_ACPI_STATUS(status);
  683. }
  684. /*******************************************************************************
  685. *
  686. * FUNCTION: acpi_ps_parse_loop
  687. *
  688. * PARAMETERS: walk_state - Current state
  689. *
  690. * RETURN: Status
  691. *
  692. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  693. * a tree of ops.
  694. *
  695. ******************************************************************************/
  696. acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
  697. {
  698. acpi_status status = AE_OK;
  699. union acpi_parse_object *op = NULL; /* current op */
  700. struct acpi_parse_state *parser_state;
  701. u8 *aml_op_start = NULL;
  702. ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
  703. if (walk_state->descending_callback == NULL) {
  704. return_ACPI_STATUS(AE_BAD_PARAMETER);
  705. }
  706. parser_state = &walk_state->parser_state;
  707. walk_state->arg_types = 0;
  708. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  709. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  710. /* We are restarting a preempted control method */
  711. if (acpi_ps_has_completed_scope(parser_state)) {
  712. /*
  713. * We must check if a predicate to an IF or WHILE statement
  714. * was just completed
  715. */
  716. if ((parser_state->scope->parse_scope.op) &&
  717. ((parser_state->scope->parse_scope.op->common.
  718. aml_opcode == AML_IF_OP)
  719. || (parser_state->scope->parse_scope.op->common.
  720. aml_opcode == AML_WHILE_OP))
  721. && (walk_state->control_state)
  722. && (walk_state->control_state->common.state ==
  723. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  724. /*
  725. * A predicate was just completed, get the value of the
  726. * predicate and branch based on that value
  727. */
  728. walk_state->op = NULL;
  729. status =
  730. acpi_ds_get_predicate_value(walk_state,
  731. ACPI_TO_POINTER
  732. (TRUE));
  733. if (ACPI_FAILURE(status)
  734. && ((status & AE_CODE_MASK) !=
  735. AE_CODE_CONTROL)) {
  736. if (status == AE_AML_NO_RETURN_VALUE) {
  737. ACPI_EXCEPTION((AE_INFO, status,
  738. "Invoked method did not return a value"));
  739. }
  740. ACPI_EXCEPTION((AE_INFO, status,
  741. "GetPredicate Failed"));
  742. return_ACPI_STATUS(status);
  743. }
  744. status =
  745. acpi_ps_next_parse_state(walk_state, op,
  746. status);
  747. }
  748. acpi_ps_pop_scope(parser_state, &op,
  749. &walk_state->arg_types,
  750. &walk_state->arg_count);
  751. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  752. "Popped scope, Op=%p\n", op));
  753. } else if (walk_state->prev_op) {
  754. /* We were in the middle of an op */
  755. op = walk_state->prev_op;
  756. walk_state->arg_types = walk_state->prev_arg_types;
  757. }
  758. }
  759. #endif
  760. /* Iterative parsing loop, while there is more AML to process: */
  761. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  762. aml_op_start = parser_state->aml;
  763. if (!op) {
  764. status =
  765. acpi_ps_create_op(walk_state, aml_op_start, &op);
  766. if (ACPI_FAILURE(status)) {
  767. if (status == AE_CTRL_PARSE_CONTINUE) {
  768. continue;
  769. }
  770. if (status == AE_CTRL_PARSE_PENDING) {
  771. status = AE_OK;
  772. }
  773. status =
  774. acpi_ps_complete_op(walk_state, &op,
  775. status);
  776. if (ACPI_FAILURE(status)) {
  777. return_ACPI_STATUS(status);
  778. }
  779. continue;
  780. }
  781. op->common.aml_offset = walk_state->aml_offset;
  782. if (walk_state->op_info) {
  783. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  784. "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
  785. (u32) op->common.aml_opcode,
  786. walk_state->op_info->name, op,
  787. parser_state->aml,
  788. op->common.aml_offset));
  789. }
  790. }
  791. /*
  792. * Start arg_count at zero because we don't know if there are
  793. * any args yet
  794. */
  795. walk_state->arg_count = 0;
  796. /* Are there any arguments that must be processed? */
  797. if (walk_state->arg_types) {
  798. /* Get arguments */
  799. status =
  800. acpi_ps_get_arguments(walk_state, aml_op_start, op);
  801. if (ACPI_FAILURE(status)) {
  802. status =
  803. acpi_ps_complete_op(walk_state, &op,
  804. status);
  805. if (ACPI_FAILURE(status)) {
  806. return_ACPI_STATUS(status);
  807. }
  808. continue;
  809. }
  810. }
  811. /* Check for arguments that need to be processed */
  812. if (walk_state->arg_count) {
  813. /*
  814. * There are arguments (complex ones), push Op and
  815. * prepare for argument
  816. */
  817. status = acpi_ps_push_scope(parser_state, op,
  818. walk_state->arg_types,
  819. walk_state->arg_count);
  820. if (ACPI_FAILURE(status)) {
  821. status =
  822. acpi_ps_complete_op(walk_state, &op,
  823. status);
  824. if (ACPI_FAILURE(status)) {
  825. return_ACPI_STATUS(status);
  826. }
  827. continue;
  828. }
  829. op = NULL;
  830. continue;
  831. }
  832. /*
  833. * All arguments have been processed -- Op is complete,
  834. * prepare for next
  835. */
  836. walk_state->op_info =
  837. acpi_ps_get_opcode_info(op->common.aml_opcode);
  838. if (walk_state->op_info->flags & AML_NAMED) {
  839. if (acpi_gbl_depth) {
  840. acpi_gbl_depth--;
  841. }
  842. if (op->common.aml_opcode == AML_REGION_OP ||
  843. op->common.aml_opcode == AML_DATA_REGION_OP) {
  844. /*
  845. * Skip parsing of control method or opregion body,
  846. * because we don't have enough info in the first pass
  847. * to parse them correctly.
  848. *
  849. * Completed parsing an op_region declaration, we now
  850. * know the length.
  851. */
  852. op->named.length =
  853. (u32) (parser_state->aml - op->named.data);
  854. }
  855. }
  856. if (walk_state->op_info->flags & AML_CREATE) {
  857. /*
  858. * Backup to beginning of create_xXXfield declaration (1 for
  859. * Opcode)
  860. *
  861. * body_length is unknown until we parse the body
  862. */
  863. op->named.length =
  864. (u32) (parser_state->aml - op->named.data);
  865. }
  866. if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
  867. /*
  868. * Backup to beginning of bank_field declaration
  869. *
  870. * body_length is unknown until we parse the body
  871. */
  872. op->named.length =
  873. (u32) (parser_state->aml - op->named.data);
  874. }
  875. /* This op complete, notify the dispatcher */
  876. if (walk_state->ascending_callback != NULL) {
  877. walk_state->op = op;
  878. walk_state->opcode = op->common.aml_opcode;
  879. status = walk_state->ascending_callback(walk_state);
  880. status =
  881. acpi_ps_next_parse_state(walk_state, op, status);
  882. if (status == AE_CTRL_PENDING) {
  883. status = AE_OK;
  884. }
  885. }
  886. status = acpi_ps_complete_op(walk_state, &op, status);
  887. if (ACPI_FAILURE(status)) {
  888. return_ACPI_STATUS(status);
  889. }
  890. } /* while parser_state->Aml */
  891. status = acpi_ps_complete_final_op(walk_state, op, status);
  892. return_ACPI_STATUS(status);
  893. }