psloop.c 32 KB

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