psobject.c 17 KB

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