psparse.c 34 KB

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