psloop.c 28 KB

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