psloop.c 33 KB

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