psobject.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /******************************************************************************
  2. *
  3. * Module Name: psobject - Support for parse objects
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acparser.h"
  45. #include "amlcode.h"
  46. #define _COMPONENT ACPI_PARSER
  47. ACPI_MODULE_NAME("psobject")
  48. /* Local prototypes */
  49. static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_ps_get_aml_opcode
  53. *
  54. * PARAMETERS: walk_state - Current state
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Extract the next AML opcode from the input stream.
  59. *
  60. ******************************************************************************/
  61. static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
  62. {
  63. u32 aml_offset;
  64. ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
  65. walk_state->aml = walk_state->parser_state.aml;
  66. walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
  67. /*
  68. * First cut to determine what we have found:
  69. * 1) A valid AML opcode
  70. * 2) A name string
  71. * 3) An unknown/invalid opcode
  72. */
  73. walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
  74. switch (walk_state->op_info->class) {
  75. case AML_CLASS_ASCII:
  76. case AML_CLASS_PREFIX:
  77. /*
  78. * Starts with a valid prefix or ASCII char, this is a name
  79. * string. Convert the bare name string to a namepath.
  80. */
  81. walk_state->opcode = AML_INT_NAMEPATH_OP;
  82. walk_state->arg_types = ARGP_NAMESTRING;
  83. break;
  84. case AML_CLASS_UNKNOWN:
  85. /* The opcode is unrecognized. Complain and skip unknown opcodes */
  86. if (walk_state->pass_number == 2) {
  87. aml_offset = (u32)ACPI_PTR_DIFF(walk_state->aml,
  88. walk_state->
  89. parser_state.aml_start);
  90. ACPI_ERROR((AE_INFO,
  91. "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
  92. walk_state->opcode,
  93. (u32)(aml_offset +
  94. sizeof(struct acpi_table_header))));
  95. ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
  96. 48);
  97. #ifdef ACPI_ASL_COMPILER
  98. /*
  99. * This is executed for the disassembler only. Output goes
  100. * to the disassembled ASL output file.
  101. */
  102. acpi_os_printf
  103. ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
  104. walk_state->opcode,
  105. (u32)(aml_offset +
  106. sizeof(struct acpi_table_header)));
  107. ACPI_ERROR((AE_INFO,
  108. "Aborting disassembly, AML byte code is corrupt"));
  109. /* Dump the context surrounding the invalid opcode */
  110. acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
  111. aml - 16), 48, DB_BYTE_DISPLAY,
  112. (aml_offset +
  113. sizeof(struct acpi_table_header) -
  114. 16));
  115. acpi_os_printf(" */\n");
  116. /*
  117. * Just abort the disassembly, cannot continue because the
  118. * parser is essentially lost. The disassembler can then
  119. * randomly fail because an ill-constructed parse tree
  120. * can result.
  121. */
  122. return_ACPI_STATUS(AE_AML_BAD_OPCODE);
  123. #endif
  124. }
  125. /* Increment past one-byte or two-byte opcode */
  126. walk_state->parser_state.aml++;
  127. if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */
  128. walk_state->parser_state.aml++;
  129. }
  130. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  131. default:
  132. /* Found opcode info, this is a normal opcode */
  133. walk_state->parser_state.aml +=
  134. acpi_ps_get_opcode_size(walk_state->opcode);
  135. walk_state->arg_types = walk_state->op_info->parse_args;
  136. break;
  137. }
  138. return_ACPI_STATUS(AE_OK);
  139. }
  140. /*******************************************************************************
  141. *
  142. * FUNCTION: acpi_ps_build_named_op
  143. *
  144. * PARAMETERS: walk_state - Current state
  145. * aml_op_start - Begin of named Op in AML
  146. * unnamed_op - Early Op (not a named Op)
  147. * op - Returned Op
  148. *
  149. * RETURN: Status
  150. *
  151. * DESCRIPTION: Parse a named Op
  152. *
  153. ******************************************************************************/
  154. acpi_status
  155. acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
  156. u8 *aml_op_start,
  157. union acpi_parse_object *unnamed_op,
  158. union acpi_parse_object **op)
  159. {
  160. acpi_status status = AE_OK;
  161. union acpi_parse_object *arg = NULL;
  162. ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
  163. unnamed_op->common.value.arg = NULL;
  164. unnamed_op->common.arg_list_length = 0;
  165. unnamed_op->common.aml_opcode = walk_state->opcode;
  166. /*
  167. * Get and append arguments until we find the node that contains
  168. * the name (the type ARGP_NAME).
  169. */
  170. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
  171. (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
  172. status =
  173. acpi_ps_get_next_arg(walk_state,
  174. &(walk_state->parser_state),
  175. GET_CURRENT_ARG_TYPE(walk_state->
  176. arg_types), &arg);
  177. if (ACPI_FAILURE(status)) {
  178. return_ACPI_STATUS(status);
  179. }
  180. acpi_ps_append_arg(unnamed_op, arg);
  181. INCREMENT_ARG_LIST(walk_state->arg_types);
  182. }
  183. /*
  184. * Make sure that we found a NAME and didn't run out of arguments
  185. */
  186. if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
  187. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  188. }
  189. /* We know that this arg is a name, move to next arg */
  190. INCREMENT_ARG_LIST(walk_state->arg_types);
  191. /*
  192. * Find the object. This will either insert the object into
  193. * the namespace or simply look it up
  194. */
  195. walk_state->op = NULL;
  196. status = walk_state->descending_callback(walk_state, op);
  197. if (ACPI_FAILURE(status)) {
  198. if (status != AE_CTRL_TERMINATE) {
  199. ACPI_EXCEPTION((AE_INFO, status,
  200. "During name lookup/catalog"));
  201. }
  202. return_ACPI_STATUS(status);
  203. }
  204. if (!*op) {
  205. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  206. }
  207. status = acpi_ps_next_parse_state(walk_state, *op, status);
  208. if (ACPI_FAILURE(status)) {
  209. if (status == AE_CTRL_PENDING) {
  210. status = AE_CTRL_PARSE_PENDING;
  211. }
  212. return_ACPI_STATUS(status);
  213. }
  214. acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
  215. if ((*op)->common.aml_opcode == AML_REGION_OP ||
  216. (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
  217. /*
  218. * Defer final parsing of an operation_region body, because we don't
  219. * have enough info in the first pass to parse it correctly (i.e.,
  220. * there may be method calls within the term_arg elements of the body.)
  221. *
  222. * However, we must continue parsing because the opregion is not a
  223. * standalone package -- we don't know where the end is at this point.
  224. *
  225. * (Length is unknown until parse of the body complete)
  226. */
  227. (*op)->named.data = aml_op_start;
  228. (*op)->named.length = 0;
  229. }
  230. return_ACPI_STATUS(AE_OK);
  231. }
  232. /*******************************************************************************
  233. *
  234. * FUNCTION: acpi_ps_create_op
  235. *
  236. * PARAMETERS: walk_state - Current state
  237. * aml_op_start - Op start in AML
  238. * new_op - Returned Op
  239. *
  240. * RETURN: Status
  241. *
  242. * DESCRIPTION: Get Op from AML
  243. *
  244. ******************************************************************************/
  245. acpi_status
  246. acpi_ps_create_op(struct acpi_walk_state *walk_state,
  247. u8 *aml_op_start, union acpi_parse_object **new_op)
  248. {
  249. acpi_status status = AE_OK;
  250. union acpi_parse_object *op;
  251. union acpi_parse_object *named_op = NULL;
  252. union acpi_parse_object *parent_scope;
  253. u8 argument_count;
  254. const struct acpi_opcode_info *op_info;
  255. ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
  256. status = acpi_ps_get_aml_opcode(walk_state);
  257. if (status == AE_CTRL_PARSE_CONTINUE) {
  258. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  259. }
  260. if (ACPI_FAILURE(status)) {
  261. return_ACPI_STATUS(status);
  262. }
  263. /* Create Op structure and append to parent's argument list */
  264. walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
  265. op = acpi_ps_alloc_op(walk_state->opcode, aml_op_start);
  266. if (!op) {
  267. return_ACPI_STATUS(AE_NO_MEMORY);
  268. }
  269. if (walk_state->op_info->flags & AML_NAMED) {
  270. status =
  271. acpi_ps_build_named_op(walk_state, aml_op_start, op,
  272. &named_op);
  273. acpi_ps_free_op(op);
  274. if (ACPI_FAILURE(status)) {
  275. return_ACPI_STATUS(status);
  276. }
  277. *new_op = named_op;
  278. return_ACPI_STATUS(AE_OK);
  279. }
  280. /* Not a named opcode, just allocate Op and append to parent */
  281. if (walk_state->op_info->flags & AML_CREATE) {
  282. /*
  283. * Backup to beginning of create_XXXfield declaration
  284. * body_length is unknown until we parse the body
  285. */
  286. op->named.data = aml_op_start;
  287. op->named.length = 0;
  288. }
  289. if (walk_state->opcode == AML_BANK_FIELD_OP) {
  290. /*
  291. * Backup to beginning of bank_field declaration
  292. * body_length is unknown until we parse the body
  293. */
  294. op->named.data = aml_op_start;
  295. op->named.length = 0;
  296. }
  297. parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
  298. acpi_ps_append_arg(parent_scope, op);
  299. if (parent_scope) {
  300. op_info =
  301. acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
  302. if (op_info->flags & AML_HAS_TARGET) {
  303. argument_count =
  304. acpi_ps_get_argument_count(op_info->type);
  305. if (parent_scope->common.arg_list_length >
  306. argument_count) {
  307. op->common.flags |= ACPI_PARSEOP_TARGET;
  308. }
  309. } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) {
  310. op->common.flags |= ACPI_PARSEOP_TARGET;
  311. }
  312. }
  313. if (walk_state->descending_callback != NULL) {
  314. /*
  315. * Find the object. This will either insert the object into
  316. * the namespace or simply look it up
  317. */
  318. walk_state->op = *new_op = op;
  319. status = walk_state->descending_callback(walk_state, &op);
  320. status = acpi_ps_next_parse_state(walk_state, op, status);
  321. if (status == AE_CTRL_PENDING) {
  322. status = AE_CTRL_PARSE_PENDING;
  323. }
  324. }
  325. return_ACPI_STATUS(status);
  326. }
  327. /*******************************************************************************
  328. *
  329. * FUNCTION: acpi_ps_complete_op
  330. *
  331. * PARAMETERS: walk_state - Current state
  332. * op - Returned Op
  333. * status - Parse status before complete Op
  334. *
  335. * RETURN: Status
  336. *
  337. * DESCRIPTION: Complete Op
  338. *
  339. ******************************************************************************/
  340. acpi_status
  341. acpi_ps_complete_op(struct acpi_walk_state *walk_state,
  342. union acpi_parse_object **op, acpi_status status)
  343. {
  344. acpi_status status2;
  345. ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
  346. /*
  347. * Finished one argument of the containing scope
  348. */
  349. walk_state->parser_state.scope->parse_scope.arg_count--;
  350. /* Close this Op (will result in parse subtree deletion) */
  351. status2 = acpi_ps_complete_this_op(walk_state, *op);
  352. if (ACPI_FAILURE(status2)) {
  353. return_ACPI_STATUS(status2);
  354. }
  355. *op = NULL;
  356. switch (status) {
  357. case AE_OK:
  358. break;
  359. case AE_CTRL_TRANSFER:
  360. /* We are about to transfer to a called method */
  361. walk_state->prev_op = NULL;
  362. walk_state->prev_arg_types = walk_state->arg_types;
  363. return_ACPI_STATUS(status);
  364. case AE_CTRL_END:
  365. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  366. &walk_state->arg_types,
  367. &walk_state->arg_count);
  368. if (*op) {
  369. walk_state->op = *op;
  370. walk_state->op_info =
  371. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  372. walk_state->opcode = (*op)->common.aml_opcode;
  373. status = walk_state->ascending_callback(walk_state);
  374. status =
  375. acpi_ps_next_parse_state(walk_state, *op, status);
  376. status2 = acpi_ps_complete_this_op(walk_state, *op);
  377. if (ACPI_FAILURE(status2)) {
  378. return_ACPI_STATUS(status2);
  379. }
  380. }
  381. status = AE_OK;
  382. break;
  383. case AE_CTRL_BREAK:
  384. case AE_CTRL_CONTINUE:
  385. /* Pop off scopes until we find the While */
  386. while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
  387. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  388. &walk_state->arg_types,
  389. &walk_state->arg_count);
  390. }
  391. /* Close this iteration of the While loop */
  392. walk_state->op = *op;
  393. walk_state->op_info =
  394. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  395. walk_state->opcode = (*op)->common.aml_opcode;
  396. status = walk_state->ascending_callback(walk_state);
  397. status = acpi_ps_next_parse_state(walk_state, *op, status);
  398. status2 = acpi_ps_complete_this_op(walk_state, *op);
  399. if (ACPI_FAILURE(status2)) {
  400. return_ACPI_STATUS(status2);
  401. }
  402. status = AE_OK;
  403. break;
  404. case AE_CTRL_TERMINATE:
  405. /* Clean up */
  406. do {
  407. if (*op) {
  408. status2 =
  409. acpi_ps_complete_this_op(walk_state, *op);
  410. if (ACPI_FAILURE(status2)) {
  411. return_ACPI_STATUS(status2);
  412. }
  413. acpi_ut_delete_generic_state
  414. (acpi_ut_pop_generic_state
  415. (&walk_state->control_state));
  416. }
  417. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  418. &walk_state->arg_types,
  419. &walk_state->arg_count);
  420. } while (*op);
  421. return_ACPI_STATUS(AE_OK);
  422. default: /* All other non-AE_OK status */
  423. do {
  424. if (*op) {
  425. status2 =
  426. acpi_ps_complete_this_op(walk_state, *op);
  427. if (ACPI_FAILURE(status2)) {
  428. return_ACPI_STATUS(status2);
  429. }
  430. }
  431. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  432. &walk_state->arg_types,
  433. &walk_state->arg_count);
  434. } while (*op);
  435. #if 0
  436. /*
  437. * TBD: Cleanup parse ops on error
  438. */
  439. if (*op == NULL) {
  440. acpi_ps_pop_scope(parser_state, op,
  441. &walk_state->arg_types,
  442. &walk_state->arg_count);
  443. }
  444. #endif
  445. walk_state->prev_op = NULL;
  446. walk_state->prev_arg_types = walk_state->arg_types;
  447. return_ACPI_STATUS(status);
  448. }
  449. /* This scope complete? */
  450. if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
  451. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  452. &walk_state->arg_types,
  453. &walk_state->arg_count);
  454. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
  455. } else {
  456. *op = NULL;
  457. }
  458. return_ACPI_STATUS(AE_OK);
  459. }
  460. /*******************************************************************************
  461. *
  462. * FUNCTION: acpi_ps_complete_final_op
  463. *
  464. * PARAMETERS: walk_state - Current state
  465. * op - Current Op
  466. * status - Current parse status before complete last
  467. * Op
  468. *
  469. * RETURN: Status
  470. *
  471. * DESCRIPTION: Complete last Op.
  472. *
  473. ******************************************************************************/
  474. acpi_status
  475. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  476. union acpi_parse_object *op, acpi_status status)
  477. {
  478. acpi_status status2;
  479. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  480. /*
  481. * Complete the last Op (if not completed), and clear the scope stack.
  482. * It is easily possible to end an AML "package" with an unbounded number
  483. * of open scopes (such as when several ASL blocks are closed with
  484. * sequential closing braces). We want to terminate each one cleanly.
  485. */
  486. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  487. op));
  488. do {
  489. if (op) {
  490. if (walk_state->ascending_callback != NULL) {
  491. walk_state->op = op;
  492. walk_state->op_info =
  493. acpi_ps_get_opcode_info(op->common.
  494. aml_opcode);
  495. walk_state->opcode = op->common.aml_opcode;
  496. status =
  497. walk_state->ascending_callback(walk_state);
  498. status =
  499. acpi_ps_next_parse_state(walk_state, op,
  500. status);
  501. if (status == AE_CTRL_PENDING) {
  502. status =
  503. acpi_ps_complete_op(walk_state, &op,
  504. AE_OK);
  505. if (ACPI_FAILURE(status)) {
  506. return_ACPI_STATUS(status);
  507. }
  508. }
  509. if (status == AE_CTRL_TERMINATE) {
  510. status = AE_OK;
  511. /* Clean up */
  512. do {
  513. if (op) {
  514. status2 =
  515. acpi_ps_complete_this_op
  516. (walk_state, op);
  517. if (ACPI_FAILURE
  518. (status2)) {
  519. return_ACPI_STATUS
  520. (status2);
  521. }
  522. }
  523. acpi_ps_pop_scope(&
  524. (walk_state->
  525. parser_state),
  526. &op,
  527. &walk_state->
  528. arg_types,
  529. &walk_state->
  530. arg_count);
  531. } while (op);
  532. return_ACPI_STATUS(status);
  533. }
  534. else if (ACPI_FAILURE(status)) {
  535. /* First error is most important */
  536. (void)
  537. acpi_ps_complete_this_op(walk_state,
  538. op);
  539. return_ACPI_STATUS(status);
  540. }
  541. }
  542. status2 = acpi_ps_complete_this_op(walk_state, op);
  543. if (ACPI_FAILURE(status2)) {
  544. return_ACPI_STATUS(status2);
  545. }
  546. }
  547. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  548. &walk_state->arg_types,
  549. &walk_state->arg_count);
  550. } while (op);
  551. return_ACPI_STATUS(status);
  552. }