psparse.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /******************************************************************************
  2. *
  3. * Module Name: psparse - Parser top level AML parse routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. /*
  43. * Parse the AML and build an operation tree as most interpreters,
  44. * like Perl, do. Parsing is done by hand rather than with a YACC
  45. * generated parser to tightly constrain stack and dynamic memory
  46. * usage. At the same time, parsing is kept flexible and the code
  47. * fairly compact by parsing based on a list of AML opcode
  48. * templates in aml_op_info[]
  49. */
  50. #include <acpi/acpi.h>
  51. #include <acpi/acparser.h>
  52. #include <acpi/acdispat.h>
  53. #include <acpi/amlcode.h>
  54. #include <acpi/acnamesp.h>
  55. #include <acpi/acinterp.h>
  56. #define _COMPONENT ACPI_PARSER
  57. ACPI_MODULE_NAME ("psparse")
  58. static u32 acpi_gbl_depth = 0;
  59. /* Local prototypes */
  60. static void
  61. acpi_ps_complete_this_op (
  62. struct acpi_walk_state *walk_state,
  63. union acpi_parse_object *op);
  64. static acpi_status
  65. acpi_ps_next_parse_state (
  66. struct acpi_walk_state *walk_state,
  67. union acpi_parse_object *op,
  68. acpi_status callback_status);
  69. static acpi_status
  70. acpi_ps_parse_loop (
  71. struct acpi_walk_state *walk_state);
  72. /*******************************************************************************
  73. *
  74. * FUNCTION: acpi_ps_get_opcode_size
  75. *
  76. * PARAMETERS: Opcode - An AML opcode
  77. *
  78. * RETURN: Size of the opcode, in bytes (1 or 2)
  79. *
  80. * DESCRIPTION: Get the size of the current opcode.
  81. *
  82. ******************************************************************************/
  83. u32
  84. acpi_ps_get_opcode_size (
  85. u32 opcode)
  86. {
  87. /* Extended (2-byte) opcode if > 255 */
  88. if (opcode > 0x00FF) {
  89. return (2);
  90. }
  91. /* Otherwise, just a single byte opcode */
  92. return (1);
  93. }
  94. /*******************************************************************************
  95. *
  96. * FUNCTION: acpi_ps_peek_opcode
  97. *
  98. * PARAMETERS: parser_state - A parser state object
  99. *
  100. * RETURN: Next AML opcode
  101. *
  102. * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
  103. *
  104. ******************************************************************************/
  105. u16
  106. acpi_ps_peek_opcode (
  107. struct acpi_parse_state *parser_state)
  108. {
  109. u8 *aml;
  110. u16 opcode;
  111. aml = parser_state->aml;
  112. opcode = (u16) ACPI_GET8 (aml);
  113. if (opcode == AML_EXTOP) {
  114. /* Extended opcode */
  115. aml++;
  116. opcode = (u16) ((opcode << 8) | ACPI_GET8 (aml));
  117. }
  118. return (opcode);
  119. }
  120. /*******************************************************************************
  121. *
  122. * FUNCTION: acpi_ps_complete_this_op
  123. *
  124. * PARAMETERS: walk_state - Current State
  125. * Op - Op to complete
  126. *
  127. * RETURN: None.
  128. *
  129. * DESCRIPTION: Perform any cleanup at the completion of an Op.
  130. *
  131. ******************************************************************************/
  132. static void
  133. acpi_ps_complete_this_op (
  134. struct acpi_walk_state *walk_state,
  135. union acpi_parse_object *op)
  136. {
  137. union acpi_parse_object *prev;
  138. union acpi_parse_object *next;
  139. const struct acpi_opcode_info *parent_info;
  140. union acpi_parse_object *replacement_op = NULL;
  141. ACPI_FUNCTION_TRACE_PTR ("ps_complete_this_op", op);
  142. /* Check for null Op, can happen if AML code is corrupt */
  143. if (!op) {
  144. return_VOID;
  145. }
  146. /* Delete this op and the subtree below it if asked to */
  147. if (((walk_state->parse_flags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
  148. (walk_state->op_info->class == AML_CLASS_ARGUMENT)) {
  149. return_VOID;
  150. }
  151. /* Make sure that we only delete this subtree */
  152. if (op->common.parent) {
  153. /*
  154. * Check if we need to replace the operator and its subtree
  155. * with a return value op (placeholder op)
  156. */
  157. parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
  158. switch (parent_info->class) {
  159. case AML_CLASS_CONTROL:
  160. break;
  161. case AML_CLASS_CREATE:
  162. /*
  163. * These opcodes contain term_arg operands. The current
  164. * op must be replaced by a placeholder return op
  165. */
  166. replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
  167. if (!replacement_op) {
  168. goto cleanup;
  169. }
  170. break;
  171. case AML_CLASS_NAMED_OBJECT:
  172. /*
  173. * These opcodes contain term_arg operands. The current
  174. * op must be replaced by a placeholder return op
  175. */
  176. if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
  177. (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
  178. (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
  179. (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
  180. (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
  181. replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
  182. if (!replacement_op) {
  183. goto cleanup;
  184. }
  185. }
  186. if ((op->common.parent->common.aml_opcode == AML_NAME_OP) &&
  187. (walk_state->descending_callback != acpi_ds_exec_begin_op)) {
  188. if ((op->common.aml_opcode == AML_BUFFER_OP) ||
  189. (op->common.aml_opcode == AML_PACKAGE_OP) ||
  190. (op->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
  191. replacement_op = acpi_ps_alloc_op (op->common.aml_opcode);
  192. if (!replacement_op) {
  193. goto cleanup;
  194. }
  195. replacement_op->named.data = op->named.data;
  196. replacement_op->named.length = op->named.length;
  197. }
  198. }
  199. break;
  200. default:
  201. replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
  202. if (!replacement_op) {
  203. goto cleanup;
  204. }
  205. }
  206. /* We must unlink this op from the parent tree */
  207. prev = op->common.parent->common.value.arg;
  208. if (prev == op) {
  209. /* This op is the first in the list */
  210. if (replacement_op) {
  211. replacement_op->common.parent = op->common.parent;
  212. replacement_op->common.value.arg = NULL;
  213. replacement_op->common.node = op->common.node;
  214. op->common.parent->common.value.arg = replacement_op;
  215. replacement_op->common.next = op->common.next;
  216. }
  217. else {
  218. op->common.parent->common.value.arg = op->common.next;
  219. }
  220. }
  221. /* Search the parent list */
  222. else while (prev) {
  223. /* Traverse all siblings in the parent's argument list */
  224. next = prev->common.next;
  225. if (next == op) {
  226. if (replacement_op) {
  227. replacement_op->common.parent = op->common.parent;
  228. replacement_op->common.value.arg = NULL;
  229. replacement_op->common.node = op->common.node;
  230. prev->common.next = replacement_op;
  231. replacement_op->common.next = op->common.next;
  232. next = NULL;
  233. }
  234. else {
  235. prev->common.next = op->common.next;
  236. next = NULL;
  237. }
  238. }
  239. prev = next;
  240. }
  241. }
  242. cleanup:
  243. /* Now we can actually delete the subtree rooted at Op */
  244. acpi_ps_delete_parse_tree (op);
  245. return_VOID;
  246. }
  247. /*******************************************************************************
  248. *
  249. * FUNCTION: acpi_ps_next_parse_state
  250. *
  251. * PARAMETERS: walk_state - Current state
  252. * Op - Current parse op
  253. * callback_status - Status from previous operation
  254. *
  255. * RETURN: Status
  256. *
  257. * DESCRIPTION: Update the parser state based upon the return exception from
  258. * the parser callback.
  259. *
  260. ******************************************************************************/
  261. static acpi_status
  262. acpi_ps_next_parse_state (
  263. struct acpi_walk_state *walk_state,
  264. union acpi_parse_object *op,
  265. acpi_status callback_status)
  266. {
  267. struct acpi_parse_state *parser_state = &walk_state->parser_state;
  268. acpi_status status = AE_CTRL_PENDING;
  269. ACPI_FUNCTION_TRACE_PTR ("ps_next_parse_state", op);
  270. switch (callback_status) {
  271. case AE_CTRL_TERMINATE:
  272. /*
  273. * A control method was terminated via a RETURN statement.
  274. * The walk of this method is complete.
  275. */
  276. parser_state->aml = parser_state->aml_end;
  277. status = AE_CTRL_TERMINATE;
  278. break;
  279. case AE_CTRL_BREAK:
  280. parser_state->aml = walk_state->aml_last_while;
  281. walk_state->control_state->common.value = FALSE;
  282. status = AE_CTRL_BREAK;
  283. break;
  284. case AE_CTRL_CONTINUE:
  285. parser_state->aml = walk_state->aml_last_while;
  286. status = AE_CTRL_CONTINUE;
  287. break;
  288. case AE_CTRL_PENDING:
  289. parser_state->aml = walk_state->aml_last_while;
  290. break;
  291. #if 0
  292. case AE_CTRL_SKIP:
  293. parser_state->aml = parser_state->scope->parse_scope.pkg_end;
  294. status = AE_OK;
  295. break;
  296. #endif
  297. case AE_CTRL_TRUE:
  298. /*
  299. * Predicate of an IF was true, and we are at the matching ELSE.
  300. * Just close out this package
  301. */
  302. parser_state->aml = acpi_ps_get_next_package_end (parser_state);
  303. break;
  304. case AE_CTRL_FALSE:
  305. /*
  306. * Either an IF/WHILE Predicate was false or we encountered a BREAK
  307. * opcode. In both cases, we do not execute the rest of the
  308. * package; We simply close out the parent (finishing the walk of
  309. * this branch of the tree) and continue execution at the parent
  310. * level.
  311. */
  312. parser_state->aml = parser_state->scope->parse_scope.pkg_end;
  313. /* In the case of a BREAK, just force a predicate (if any) to FALSE */
  314. walk_state->control_state->common.value = FALSE;
  315. status = AE_CTRL_END;
  316. break;
  317. case AE_CTRL_TRANSFER:
  318. /* A method call (invocation) -- transfer control */
  319. status = AE_CTRL_TRANSFER;
  320. walk_state->prev_op = op;
  321. walk_state->method_call_op = op;
  322. walk_state->method_call_node = (op->common.value.arg)->common.node;
  323. /* Will return value (if any) be used by the caller? */
  324. walk_state->return_used = acpi_ds_is_result_used (op, walk_state);
  325. break;
  326. default:
  327. status = callback_status;
  328. if ((callback_status & AE_CODE_MASK) == AE_CODE_CONTROL) {
  329. status = AE_OK;
  330. }
  331. break;
  332. }
  333. return_ACPI_STATUS (status);
  334. }
  335. /*******************************************************************************
  336. *
  337. * FUNCTION: acpi_ps_parse_loop
  338. *
  339. * PARAMETERS: walk_state - Current state
  340. *
  341. * RETURN: Status
  342. *
  343. * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
  344. * a tree of ops.
  345. *
  346. ******************************************************************************/
  347. static acpi_status
  348. acpi_ps_parse_loop (
  349. struct acpi_walk_state *walk_state)
  350. {
  351. acpi_status status = AE_OK;
  352. union acpi_parse_object *op = NULL; /* current op */
  353. union acpi_parse_object *arg = NULL;
  354. union acpi_parse_object *pre_op = NULL;
  355. struct acpi_parse_state *parser_state;
  356. u8 *aml_op_start = NULL;
  357. ACPI_FUNCTION_TRACE_PTR ("ps_parse_loop", walk_state);
  358. if (walk_state->descending_callback == NULL) {
  359. return_ACPI_STATUS (AE_BAD_PARAMETER);
  360. }
  361. parser_state = &walk_state->parser_state;
  362. walk_state->arg_types = 0;
  363. #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
  364. if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
  365. /* We are restarting a preempted control method */
  366. if (acpi_ps_has_completed_scope (parser_state)) {
  367. /*
  368. * We must check if a predicate to an IF or WHILE statement
  369. * was just completed
  370. */
  371. if ((parser_state->scope->parse_scope.op) &&
  372. ((parser_state->scope->parse_scope.op->common.aml_opcode == AML_IF_OP) ||
  373. (parser_state->scope->parse_scope.op->common.aml_opcode == AML_WHILE_OP)) &&
  374. (walk_state->control_state) &&
  375. (walk_state->control_state->common.state ==
  376. ACPI_CONTROL_PREDICATE_EXECUTING)) {
  377. /*
  378. * A predicate was just completed, get the value of the
  379. * predicate and branch based on that value
  380. */
  381. walk_state->op = NULL;
  382. status = acpi_ds_get_predicate_value (walk_state, ACPI_TO_POINTER (TRUE));
  383. if (ACPI_FAILURE (status) &&
  384. ((status & AE_CODE_MASK) != AE_CODE_CONTROL)) {
  385. if (status == AE_AML_NO_RETURN_VALUE) {
  386. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  387. "Invoked method did not return a value, %s\n",
  388. acpi_format_exception (status)));
  389. }
  390. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  391. "get_predicate Failed, %s\n",
  392. acpi_format_exception (status)));
  393. return_ACPI_STATUS (status);
  394. }
  395. status = acpi_ps_next_parse_state (walk_state, op, status);
  396. }
  397. acpi_ps_pop_scope (parser_state, &op,
  398. &walk_state->arg_types, &walk_state->arg_count);
  399. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
  400. }
  401. else if (walk_state->prev_op) {
  402. /* We were in the middle of an op */
  403. op = walk_state->prev_op;
  404. walk_state->arg_types = walk_state->prev_arg_types;
  405. }
  406. }
  407. #endif
  408. /* Iterative parsing loop, while there is more AML to process: */
  409. while ((parser_state->aml < parser_state->aml_end) || (op)) {
  410. aml_op_start = parser_state->aml;
  411. if (!op) {
  412. /* Get the next opcode from the AML stream */
  413. walk_state->aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
  414. parser_state->aml_start);
  415. walk_state->opcode = acpi_ps_peek_opcode (parser_state);
  416. /*
  417. * First cut to determine what we have found:
  418. * 1) A valid AML opcode
  419. * 2) A name string
  420. * 3) An unknown/invalid opcode
  421. */
  422. walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
  423. switch (walk_state->op_info->class) {
  424. case AML_CLASS_ASCII:
  425. case AML_CLASS_PREFIX:
  426. /*
  427. * Starts with a valid prefix or ASCII char, this is a name
  428. * string. Convert the bare name string to a namepath.
  429. */
  430. walk_state->opcode = AML_INT_NAMEPATH_OP;
  431. walk_state->arg_types = ARGP_NAMESTRING;
  432. break;
  433. case AML_CLASS_UNKNOWN:
  434. /* The opcode is unrecognized. Just skip unknown opcodes */
  435. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  436. "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
  437. walk_state->opcode, parser_state->aml, walk_state->aml_offset));
  438. ACPI_DUMP_BUFFER (parser_state->aml, 128);
  439. /* Assume one-byte bad opcode */
  440. parser_state->aml++;
  441. continue;
  442. default:
  443. /* Found opcode info, this is a normal opcode */
  444. parser_state->aml += acpi_ps_get_opcode_size (walk_state->opcode);
  445. walk_state->arg_types = walk_state->op_info->parse_args;
  446. break;
  447. }
  448. /* Create Op structure and append to parent's argument list */
  449. if (walk_state->op_info->flags & AML_NAMED) {
  450. /* Allocate a new pre_op if necessary */
  451. if (!pre_op) {
  452. pre_op = acpi_ps_alloc_op (walk_state->opcode);
  453. if (!pre_op) {
  454. status = AE_NO_MEMORY;
  455. goto close_this_op;
  456. }
  457. }
  458. pre_op->common.value.arg = NULL;
  459. pre_op->common.aml_opcode = walk_state->opcode;
  460. /*
  461. * Get and append arguments until we find the node that contains
  462. * the name (the type ARGP_NAME).
  463. */
  464. while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
  465. (GET_CURRENT_ARG_TYPE (walk_state->arg_types) != ARGP_NAME)) {
  466. status = acpi_ps_get_next_arg (walk_state, parser_state,
  467. GET_CURRENT_ARG_TYPE (walk_state->arg_types), &arg);
  468. if (ACPI_FAILURE (status)) {
  469. goto close_this_op;
  470. }
  471. acpi_ps_append_arg (pre_op, arg);
  472. INCREMENT_ARG_LIST (walk_state->arg_types);
  473. }
  474. /*
  475. * Make sure that we found a NAME and didn't run out of
  476. * arguments
  477. */
  478. if (!GET_CURRENT_ARG_TYPE (walk_state->arg_types)) {
  479. status = AE_AML_NO_OPERAND;
  480. goto close_this_op;
  481. }
  482. /* We know that this arg is a name, move to next arg */
  483. INCREMENT_ARG_LIST (walk_state->arg_types);
  484. /*
  485. * Find the object. This will either insert the object into
  486. * the namespace or simply look it up
  487. */
  488. walk_state->op = NULL;
  489. status = walk_state->descending_callback (walk_state, &op);
  490. if (ACPI_FAILURE (status)) {
  491. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  492. "During name lookup/catalog, %s\n",
  493. acpi_format_exception (status)));
  494. goto close_this_op;
  495. }
  496. if (!op) {
  497. continue;
  498. }
  499. status = acpi_ps_next_parse_state (walk_state, op, status);
  500. if (status == AE_CTRL_PENDING) {
  501. status = AE_OK;
  502. goto close_this_op;
  503. }
  504. if (ACPI_FAILURE (status)) {
  505. goto close_this_op;
  506. }
  507. acpi_ps_append_arg (op, pre_op->common.value.arg);
  508. acpi_gbl_depth++;
  509. if (op->common.aml_opcode == AML_REGION_OP) {
  510. /*
  511. * Defer final parsing of an operation_region body,
  512. * because we don't have enough info in the first pass
  513. * to parse it correctly (i.e., there may be method
  514. * calls within the term_arg elements of the body.)
  515. *
  516. * However, we must continue parsing because
  517. * the opregion is not a standalone package --
  518. * we don't know where the end is at this point.
  519. *
  520. * (Length is unknown until parse of the body complete)
  521. */
  522. op->named.data = aml_op_start;
  523. op->named.length = 0;
  524. }
  525. }
  526. else {
  527. /* Not a named opcode, just allocate Op and append to parent */
  528. walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
  529. op = acpi_ps_alloc_op (walk_state->opcode);
  530. if (!op) {
  531. status = AE_NO_MEMORY;
  532. goto close_this_op;
  533. }
  534. if (walk_state->op_info->flags & AML_CREATE) {
  535. /*
  536. * Backup to beginning of create_xXXfield declaration
  537. * body_length is unknown until we parse the body
  538. */
  539. op->named.data = aml_op_start;
  540. op->named.length = 0;
  541. }
  542. acpi_ps_append_arg (acpi_ps_get_parent_scope (parser_state), op);
  543. if ((walk_state->descending_callback != NULL)) {
  544. /*
  545. * Find the object. This will either insert the object into
  546. * the namespace or simply look it up
  547. */
  548. walk_state->op = op;
  549. status = walk_state->descending_callback (walk_state, &op);
  550. status = acpi_ps_next_parse_state (walk_state, op, status);
  551. if (status == AE_CTRL_PENDING) {
  552. status = AE_OK;
  553. goto close_this_op;
  554. }
  555. if (ACPI_FAILURE (status)) {
  556. goto close_this_op;
  557. }
  558. }
  559. }
  560. op->common.aml_offset = walk_state->aml_offset;
  561. if (walk_state->op_info) {
  562. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
  563. "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
  564. (u32) op->common.aml_opcode, walk_state->op_info->name,
  565. op, parser_state->aml, op->common.aml_offset));
  566. }
  567. }
  568. /*
  569. * Start arg_count at zero because we don't know if there are
  570. * any args yet
  571. */
  572. walk_state->arg_count = 0;
  573. /* Are there any arguments that must be processed? */
  574. if (walk_state->arg_types) {
  575. /* Get arguments */
  576. switch (op->common.aml_opcode) {
  577. case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
  578. case AML_WORD_OP: /* AML_WORDDATA_ARG */
  579. case AML_DWORD_OP: /* AML_DWORDATA_ARG */
  580. case AML_QWORD_OP: /* AML_QWORDATA_ARG */
  581. case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
  582. /* Fill in constant or string argument directly */
  583. acpi_ps_get_next_simple_arg (parser_state,
  584. GET_CURRENT_ARG_TYPE (walk_state->arg_types), op);
  585. break;
  586. case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
  587. status = acpi_ps_get_next_namepath (walk_state, parser_state, op, 1);
  588. if (ACPI_FAILURE (status)) {
  589. goto close_this_op;
  590. }
  591. walk_state->arg_types = 0;
  592. break;
  593. default:
  594. /*
  595. * Op is not a constant or string, append each argument
  596. * to the Op
  597. */
  598. while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
  599. !walk_state->arg_count) {
  600. walk_state->aml_offset = (u32)
  601. ACPI_PTR_DIFF (parser_state->aml, parser_state->aml_start);
  602. status = acpi_ps_get_next_arg (walk_state, parser_state,
  603. GET_CURRENT_ARG_TYPE (walk_state->arg_types),
  604. &arg);
  605. if (ACPI_FAILURE (status)) {
  606. goto close_this_op;
  607. }
  608. if (arg) {
  609. arg->common.aml_offset = walk_state->aml_offset;
  610. acpi_ps_append_arg (op, arg);
  611. }
  612. INCREMENT_ARG_LIST (walk_state->arg_types);
  613. }
  614. /* Special processing for certain opcodes */
  615. switch (op->common.aml_opcode) {
  616. case AML_METHOD_OP:
  617. /*
  618. * Skip parsing of control method
  619. * because we don't have enough info in the first pass
  620. * to parse it correctly.
  621. *
  622. * Save the length and address of the body
  623. */
  624. op->named.data = parser_state->aml;
  625. op->named.length = (u32) (parser_state->pkg_end -
  626. parser_state->aml);
  627. /* Skip body of method */
  628. parser_state->aml = parser_state->pkg_end;
  629. walk_state->arg_count = 0;
  630. break;
  631. case AML_BUFFER_OP:
  632. case AML_PACKAGE_OP:
  633. case AML_VAR_PACKAGE_OP:
  634. if ((op->common.parent) &&
  635. (op->common.parent->common.aml_opcode == AML_NAME_OP) &&
  636. (walk_state->descending_callback != acpi_ds_exec_begin_op)) {
  637. /*
  638. * Skip parsing of Buffers and Packages
  639. * because we don't have enough info in the first pass
  640. * to parse them correctly.
  641. */
  642. op->named.data = aml_op_start;
  643. op->named.length = (u32) (parser_state->pkg_end -
  644. aml_op_start);
  645. /* Skip body */
  646. parser_state->aml = parser_state->pkg_end;
  647. walk_state->arg_count = 0;
  648. }
  649. break;
  650. case AML_WHILE_OP:
  651. if (walk_state->control_state) {
  652. walk_state->control_state->control.package_end =
  653. parser_state->pkg_end;
  654. }
  655. break;
  656. default:
  657. /* No action for all other opcodes */
  658. break;
  659. }
  660. break;
  661. }
  662. }
  663. /* Check for arguments that need to be processed */
  664. if (walk_state->arg_count) {
  665. /*
  666. * There are arguments (complex ones), push Op and
  667. * prepare for argument
  668. */
  669. status = acpi_ps_push_scope (parser_state, op,
  670. walk_state->arg_types, walk_state->arg_count);
  671. if (ACPI_FAILURE (status)) {
  672. goto close_this_op;
  673. }
  674. op = NULL;
  675. continue;
  676. }
  677. /*
  678. * All arguments have been processed -- Op is complete,
  679. * prepare for next
  680. */
  681. walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
  682. if (walk_state->op_info->flags & AML_NAMED) {
  683. if (acpi_gbl_depth) {
  684. acpi_gbl_depth--;
  685. }
  686. if (op->common.aml_opcode == AML_REGION_OP) {
  687. /*
  688. * Skip parsing of control method or opregion body,
  689. * because we don't have enough info in the first pass
  690. * to parse them correctly.
  691. *
  692. * Completed parsing an op_region declaration, we now
  693. * know the length.
  694. */
  695. op->named.length = (u32) (parser_state->aml - op->named.data);
  696. }
  697. }
  698. if (walk_state->op_info->flags & AML_CREATE) {
  699. /*
  700. * Backup to beginning of create_xXXfield declaration (1 for
  701. * Opcode)
  702. *
  703. * body_length is unknown until we parse the body
  704. */
  705. op->named.length = (u32) (parser_state->aml - op->named.data);
  706. }
  707. /* This op complete, notify the dispatcher */
  708. if (walk_state->ascending_callback != NULL) {
  709. walk_state->op = op;
  710. walk_state->opcode = op->common.aml_opcode;
  711. status = walk_state->ascending_callback (walk_state);
  712. status = acpi_ps_next_parse_state (walk_state, op, status);
  713. if (status == AE_CTRL_PENDING) {
  714. status = AE_OK;
  715. goto close_this_op;
  716. }
  717. }
  718. close_this_op:
  719. /*
  720. * Finished one argument of the containing scope
  721. */
  722. parser_state->scope->parse_scope.arg_count--;
  723. /* Close this Op (will result in parse subtree deletion) */
  724. acpi_ps_complete_this_op (walk_state, op);
  725. op = NULL;
  726. if (pre_op) {
  727. acpi_ps_free_op (pre_op);
  728. pre_op = NULL;
  729. }
  730. switch (status) {
  731. case AE_OK:
  732. break;
  733. case AE_CTRL_TRANSFER:
  734. /* We are about to transfer to a called method. */
  735. walk_state->prev_op = op;
  736. walk_state->prev_arg_types = walk_state->arg_types;
  737. return_ACPI_STATUS (status);
  738. case AE_CTRL_END:
  739. acpi_ps_pop_scope (parser_state, &op,
  740. &walk_state->arg_types, &walk_state->arg_count);
  741. if (op) {
  742. walk_state->op = op;
  743. walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
  744. walk_state->opcode = op->common.aml_opcode;
  745. status = walk_state->ascending_callback (walk_state);
  746. status = acpi_ps_next_parse_state (walk_state, op, status);
  747. acpi_ps_complete_this_op (walk_state, op);
  748. op = NULL;
  749. }
  750. status = AE_OK;
  751. break;
  752. case AE_CTRL_BREAK:
  753. case AE_CTRL_CONTINUE:
  754. /* Pop off scopes until we find the While */
  755. while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
  756. acpi_ps_pop_scope (parser_state, &op,
  757. &walk_state->arg_types, &walk_state->arg_count);
  758. }
  759. /* Close this iteration of the While loop */
  760. walk_state->op = op;
  761. walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
  762. walk_state->opcode = op->common.aml_opcode;
  763. status = walk_state->ascending_callback (walk_state);
  764. status = acpi_ps_next_parse_state (walk_state, op, status);
  765. acpi_ps_complete_this_op (walk_state, op);
  766. op = NULL;
  767. status = AE_OK;
  768. break;
  769. case AE_CTRL_TERMINATE:
  770. status = AE_OK;
  771. /* Clean up */
  772. do {
  773. if (op) {
  774. acpi_ps_complete_this_op (walk_state, op);
  775. }
  776. acpi_ps_pop_scope (parser_state, &op,
  777. &walk_state->arg_types, &walk_state->arg_count);
  778. } while (op);
  779. return_ACPI_STATUS (status);
  780. default: /* All other non-AE_OK status */
  781. do {
  782. if (op) {
  783. acpi_ps_complete_this_op (walk_state, op);
  784. }
  785. acpi_ps_pop_scope (parser_state, &op,
  786. &walk_state->arg_types, &walk_state->arg_count);
  787. } while (op);
  788. /*
  789. * TBD: Cleanup parse ops on error
  790. */
  791. #if 0
  792. if (op == NULL) {
  793. acpi_ps_pop_scope (parser_state, &op,
  794. &walk_state->arg_types, &walk_state->arg_count);
  795. }
  796. #endif
  797. walk_state->prev_op = op;
  798. walk_state->prev_arg_types = walk_state->arg_types;
  799. return_ACPI_STATUS (status);
  800. }
  801. /* This scope complete? */
  802. if (acpi_ps_has_completed_scope (parser_state)) {
  803. acpi_ps_pop_scope (parser_state, &op,
  804. &walk_state->arg_types, &walk_state->arg_count);
  805. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
  806. }
  807. else {
  808. op = NULL;
  809. }
  810. } /* while parser_state->Aml */
  811. /*
  812. * Complete the last Op (if not completed), and clear the scope stack.
  813. * It is easily possible to end an AML "package" with an unbounded number
  814. * of open scopes (such as when several ASL blocks are closed with
  815. * sequential closing braces). We want to terminate each one cleanly.
  816. */
  817. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", op));
  818. do {
  819. if (op) {
  820. if (walk_state->ascending_callback != NULL) {
  821. walk_state->op = op;
  822. walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
  823. walk_state->opcode = op->common.aml_opcode;
  824. status = walk_state->ascending_callback (walk_state);
  825. status = acpi_ps_next_parse_state (walk_state, op, status);
  826. if (status == AE_CTRL_PENDING) {
  827. status = AE_OK;
  828. goto close_this_op;
  829. }
  830. if (status == AE_CTRL_TERMINATE) {
  831. status = AE_OK;
  832. /* Clean up */
  833. do {
  834. if (op) {
  835. acpi_ps_complete_this_op (walk_state, op);
  836. }
  837. acpi_ps_pop_scope (parser_state, &op,
  838. &walk_state->arg_types, &walk_state->arg_count);
  839. } while (op);
  840. return_ACPI_STATUS (status);
  841. }
  842. else if (ACPI_FAILURE (status)) {
  843. acpi_ps_complete_this_op (walk_state, op);
  844. return_ACPI_STATUS (status);
  845. }
  846. }
  847. acpi_ps_complete_this_op (walk_state, op);
  848. }
  849. acpi_ps_pop_scope (parser_state, &op, &walk_state->arg_types,
  850. &walk_state->arg_count);
  851. } while (op);
  852. return_ACPI_STATUS (status);
  853. }
  854. /*******************************************************************************
  855. *
  856. * FUNCTION: acpi_ps_parse_aml
  857. *
  858. * PARAMETERS: walk_state - Current state
  859. *
  860. *
  861. * RETURN: Status
  862. *
  863. * DESCRIPTION: Parse raw AML and return a tree of ops
  864. *
  865. ******************************************************************************/
  866. acpi_status
  867. acpi_ps_parse_aml (
  868. struct acpi_walk_state *walk_state)
  869. {
  870. acpi_status status;
  871. acpi_status terminate_status;
  872. struct acpi_thread_state *thread;
  873. struct acpi_thread_state *prev_walk_list = acpi_gbl_current_walk_list;
  874. struct acpi_walk_state *previous_walk_state;
  875. ACPI_FUNCTION_TRACE ("ps_parse_aml");
  876. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
  877. "Entered with walk_state=%p Aml=%p size=%X\n",
  878. walk_state, walk_state->parser_state.aml,
  879. walk_state->parser_state.aml_size));
  880. /* Create and initialize a new thread state */
  881. thread = acpi_ut_create_thread_state ();
  882. if (!thread) {
  883. return_ACPI_STATUS (AE_NO_MEMORY);
  884. }
  885. walk_state->thread = thread;
  886. acpi_ds_push_walk_state (walk_state, thread);
  887. /*
  888. * This global allows the AML debugger to get a handle to the currently
  889. * executing control method.
  890. */
  891. acpi_gbl_current_walk_list = thread;
  892. /*
  893. * Execute the walk loop as long as there is a valid Walk State. This
  894. * handles nested control method invocations without recursion.
  895. */
  896. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", walk_state));
  897. status = AE_OK;
  898. while (walk_state) {
  899. if (ACPI_SUCCESS (status)) {
  900. /*
  901. * The parse_loop executes AML until the method terminates
  902. * or calls another method.
  903. */
  904. status = acpi_ps_parse_loop (walk_state);
  905. }
  906. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
  907. "Completed one call to walk loop, %s State=%p\n",
  908. acpi_format_exception (status), walk_state));
  909. if (status == AE_CTRL_TRANSFER) {
  910. /*
  911. * A method call was detected.
  912. * Transfer control to the called control method
  913. */
  914. status = acpi_ds_call_control_method (thread, walk_state, NULL);
  915. /*
  916. * If the transfer to the new method method call worked, a new walk
  917. * state was created -- get it
  918. */
  919. walk_state = acpi_ds_get_current_walk_state (thread);
  920. continue;
  921. }
  922. else if (status == AE_CTRL_TERMINATE) {
  923. status = AE_OK;
  924. }
  925. else if ((status != AE_OK) && (walk_state->method_desc)) {
  926. ACPI_REPORT_METHOD_ERROR ("Method execution failed",
  927. walk_state->method_node, NULL, status);
  928. /* Check for possible multi-thread reentrancy problem */
  929. if ((status == AE_ALREADY_EXISTS) &&
  930. (!walk_state->method_desc->method.semaphore)) {
  931. /*
  932. * This method is marked not_serialized, but it tried to create
  933. * a named object, causing the second thread entrance to fail.
  934. * We will workaround this by marking the method permanently
  935. * as Serialized.
  936. */
  937. walk_state->method_desc->method.method_flags |= AML_METHOD_SERIALIZED;
  938. walk_state->method_desc->method.concurrency = 1;
  939. }
  940. }
  941. if (walk_state->method_desc) {
  942. /* Decrement the thread count on the method parse tree */
  943. if (walk_state->method_desc->method.thread_count) {
  944. walk_state->method_desc->method.thread_count--;
  945. }
  946. }
  947. /* We are done with this walk, move on to the parent if any */
  948. walk_state = acpi_ds_pop_walk_state (thread);
  949. /* Reset the current scope to the beginning of scope stack */
  950. acpi_ds_scope_stack_clear (walk_state);
  951. /*
  952. * If we just returned from the execution of a control method,
  953. * there's lots of cleanup to do
  954. */
  955. if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) {
  956. terminate_status = acpi_ds_terminate_control_method (walk_state);
  957. if (ACPI_FAILURE (terminate_status)) {
  958. ACPI_REPORT_ERROR ((
  959. "Could not terminate control method properly\n"));
  960. /* Ignore error and continue */
  961. }
  962. }
  963. /* Delete this walk state and all linked control states */
  964. acpi_ps_cleanup_scope (&walk_state->parser_state);
  965. previous_walk_state = walk_state;
  966. ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
  967. "return_value=%p, implicit_value=%p State=%p\n",
  968. walk_state->return_desc, walk_state->implicit_return_obj, walk_state));
  969. /* Check if we have restarted a preempted walk */
  970. walk_state = acpi_ds_get_current_walk_state (thread);
  971. if (walk_state) {
  972. if (ACPI_SUCCESS (status)) {
  973. /*
  974. * There is another walk state, restart it.
  975. * If the method return value is not used by the parent,
  976. * The object is deleted
  977. */
  978. if (!previous_walk_state->return_desc) {
  979. status = acpi_ds_restart_control_method (walk_state,
  980. previous_walk_state->implicit_return_obj);
  981. }
  982. else {
  983. /*
  984. * We have a valid return value, delete any implicit
  985. * return value.
  986. */
  987. acpi_ds_clear_implicit_return (previous_walk_state);
  988. status = acpi_ds_restart_control_method (walk_state,
  989. previous_walk_state->return_desc);
  990. }
  991. if (ACPI_SUCCESS (status)) {
  992. walk_state->walk_type |= ACPI_WALK_METHOD_RESTART;
  993. }
  994. }
  995. else {
  996. /* On error, delete any return object */
  997. acpi_ut_remove_reference (previous_walk_state->return_desc);
  998. }
  999. }
  1000. /*
  1001. * Just completed a 1st-level method, save the final internal return
  1002. * value (if any)
  1003. */
  1004. else if (previous_walk_state->caller_return_desc) {
  1005. if (previous_walk_state->implicit_return_obj) {
  1006. *(previous_walk_state->caller_return_desc) =
  1007. previous_walk_state->implicit_return_obj;
  1008. }
  1009. else {
  1010. /* NULL if no return value */
  1011. *(previous_walk_state->caller_return_desc) =
  1012. previous_walk_state->return_desc;
  1013. }
  1014. }
  1015. else {
  1016. if (previous_walk_state->return_desc) {
  1017. /* Caller doesn't want it, must delete it */
  1018. acpi_ut_remove_reference (previous_walk_state->return_desc);
  1019. }
  1020. if (previous_walk_state->implicit_return_obj) {
  1021. /* Caller doesn't want it, must delete it */
  1022. acpi_ut_remove_reference (previous_walk_state->implicit_return_obj);
  1023. }
  1024. }
  1025. acpi_ds_delete_walk_state (previous_walk_state);
  1026. }
  1027. /* Normal exit */
  1028. acpi_ex_release_all_mutexes (thread);
  1029. acpi_ut_delete_generic_state (ACPI_CAST_PTR (union acpi_generic_state, thread));
  1030. acpi_gbl_current_walk_list = prev_walk_list;
  1031. return_ACPI_STATUS (status);
  1032. }