psloop.c 32 KB

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