psobject.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /******************************************************************************
  2. *
  3. * Module Name: psobject - Support for parse objects
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, 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. if (status != AE_CTRL_TERMINATE) {
  188. ACPI_EXCEPTION((AE_INFO, status,
  189. "During name lookup/catalog"));
  190. }
  191. return_ACPI_STATUS(status);
  192. }
  193. if (!*op) {
  194. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  195. }
  196. status = acpi_ps_next_parse_state(walk_state, *op, status);
  197. if (ACPI_FAILURE(status)) {
  198. if (status == AE_CTRL_PENDING) {
  199. status = AE_CTRL_PARSE_PENDING;
  200. }
  201. return_ACPI_STATUS(status);
  202. }
  203. acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
  204. if ((*op)->common.aml_opcode == AML_REGION_OP ||
  205. (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
  206. /*
  207. * Defer final parsing of an operation_region body, because we don't
  208. * have enough info in the first pass to parse it correctly (i.e.,
  209. * there may be method calls within the term_arg elements of the body.)
  210. *
  211. * However, we must continue parsing because the opregion is not a
  212. * standalone package -- we don't know where the end is at this point.
  213. *
  214. * (Length is unknown until parse of the body complete)
  215. */
  216. (*op)->named.data = aml_op_start;
  217. (*op)->named.length = 0;
  218. }
  219. return_ACPI_STATUS(AE_OK);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: acpi_ps_create_op
  224. *
  225. * PARAMETERS: walk_state - Current state
  226. * aml_op_start - Op start in AML
  227. * new_op - Returned Op
  228. *
  229. * RETURN: Status
  230. *
  231. * DESCRIPTION: Get Op from AML
  232. *
  233. ******************************************************************************/
  234. acpi_status
  235. acpi_ps_create_op(struct acpi_walk_state *walk_state,
  236. u8 *aml_op_start, union acpi_parse_object **new_op)
  237. {
  238. acpi_status status = AE_OK;
  239. union acpi_parse_object *op;
  240. union acpi_parse_object *named_op = NULL;
  241. union acpi_parse_object *parent_scope;
  242. u8 argument_count;
  243. const struct acpi_opcode_info *op_info;
  244. ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
  245. status = acpi_ps_get_aml_opcode(walk_state);
  246. if (status == AE_CTRL_PARSE_CONTINUE) {
  247. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  248. }
  249. /* Create Op structure and append to parent's argument list */
  250. walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
  251. op = acpi_ps_alloc_op(walk_state->opcode);
  252. if (!op) {
  253. return_ACPI_STATUS(AE_NO_MEMORY);
  254. }
  255. if (walk_state->op_info->flags & AML_NAMED) {
  256. status =
  257. acpi_ps_build_named_op(walk_state, aml_op_start, op,
  258. &named_op);
  259. acpi_ps_free_op(op);
  260. if (ACPI_FAILURE(status)) {
  261. return_ACPI_STATUS(status);
  262. }
  263. *new_op = named_op;
  264. return_ACPI_STATUS(AE_OK);
  265. }
  266. /* Not a named opcode, just allocate Op and append to parent */
  267. if (walk_state->op_info->flags & AML_CREATE) {
  268. /*
  269. * Backup to beginning of create_XXXfield declaration
  270. * body_length is unknown until we parse the body
  271. */
  272. op->named.data = aml_op_start;
  273. op->named.length = 0;
  274. }
  275. if (walk_state->opcode == AML_BANK_FIELD_OP) {
  276. /*
  277. * Backup to beginning of bank_field declaration
  278. * body_length is unknown until we parse the body
  279. */
  280. op->named.data = aml_op_start;
  281. op->named.length = 0;
  282. }
  283. parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
  284. acpi_ps_append_arg(parent_scope, op);
  285. if (parent_scope) {
  286. op_info =
  287. acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
  288. if (op_info->flags & AML_HAS_TARGET) {
  289. argument_count =
  290. acpi_ps_get_argument_count(op_info->type);
  291. if (parent_scope->common.arg_list_length >
  292. argument_count) {
  293. op->common.flags |= ACPI_PARSEOP_TARGET;
  294. }
  295. } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) {
  296. op->common.flags |= ACPI_PARSEOP_TARGET;
  297. }
  298. }
  299. if (walk_state->descending_callback != NULL) {
  300. /*
  301. * Find the object. This will either insert the object into
  302. * the namespace or simply look it up
  303. */
  304. walk_state->op = *new_op = op;
  305. status = walk_state->descending_callback(walk_state, &op);
  306. status = acpi_ps_next_parse_state(walk_state, op, status);
  307. if (status == AE_CTRL_PENDING) {
  308. status = AE_CTRL_PARSE_PENDING;
  309. }
  310. }
  311. return_ACPI_STATUS(status);
  312. }
  313. /*******************************************************************************
  314. *
  315. * FUNCTION: acpi_ps_complete_op
  316. *
  317. * PARAMETERS: walk_state - Current state
  318. * op - Returned Op
  319. * status - Parse status before complete Op
  320. *
  321. * RETURN: Status
  322. *
  323. * DESCRIPTION: Complete Op
  324. *
  325. ******************************************************************************/
  326. acpi_status
  327. acpi_ps_complete_op(struct acpi_walk_state *walk_state,
  328. union acpi_parse_object **op, acpi_status status)
  329. {
  330. acpi_status status2;
  331. ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
  332. /*
  333. * Finished one argument of the containing scope
  334. */
  335. walk_state->parser_state.scope->parse_scope.arg_count--;
  336. /* Close this Op (will result in parse subtree deletion) */
  337. status2 = acpi_ps_complete_this_op(walk_state, *op);
  338. if (ACPI_FAILURE(status2)) {
  339. return_ACPI_STATUS(status2);
  340. }
  341. *op = NULL;
  342. switch (status) {
  343. case AE_OK:
  344. break;
  345. case AE_CTRL_TRANSFER:
  346. /* We are about to transfer to a called method */
  347. walk_state->prev_op = NULL;
  348. walk_state->prev_arg_types = walk_state->arg_types;
  349. return_ACPI_STATUS(status);
  350. case AE_CTRL_END:
  351. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  352. &walk_state->arg_types,
  353. &walk_state->arg_count);
  354. if (*op) {
  355. walk_state->op = *op;
  356. walk_state->op_info =
  357. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  358. walk_state->opcode = (*op)->common.aml_opcode;
  359. status = walk_state->ascending_callback(walk_state);
  360. status =
  361. acpi_ps_next_parse_state(walk_state, *op, status);
  362. status2 = acpi_ps_complete_this_op(walk_state, *op);
  363. if (ACPI_FAILURE(status2)) {
  364. return_ACPI_STATUS(status2);
  365. }
  366. }
  367. status = AE_OK;
  368. break;
  369. case AE_CTRL_BREAK:
  370. case AE_CTRL_CONTINUE:
  371. /* Pop off scopes until we find the While */
  372. while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
  373. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  374. &walk_state->arg_types,
  375. &walk_state->arg_count);
  376. }
  377. /* Close this iteration of the While loop */
  378. walk_state->op = *op;
  379. walk_state->op_info =
  380. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  381. walk_state->opcode = (*op)->common.aml_opcode;
  382. status = walk_state->ascending_callback(walk_state);
  383. status = acpi_ps_next_parse_state(walk_state, *op, status);
  384. status2 = acpi_ps_complete_this_op(walk_state, *op);
  385. if (ACPI_FAILURE(status2)) {
  386. return_ACPI_STATUS(status2);
  387. }
  388. status = AE_OK;
  389. break;
  390. case AE_CTRL_TERMINATE:
  391. /* Clean up */
  392. do {
  393. if (*op) {
  394. status2 =
  395. acpi_ps_complete_this_op(walk_state, *op);
  396. if (ACPI_FAILURE(status2)) {
  397. return_ACPI_STATUS(status2);
  398. }
  399. acpi_ut_delete_generic_state
  400. (acpi_ut_pop_generic_state
  401. (&walk_state->control_state));
  402. }
  403. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  404. &walk_state->arg_types,
  405. &walk_state->arg_count);
  406. } while (*op);
  407. return_ACPI_STATUS(AE_OK);
  408. default: /* All other non-AE_OK status */
  409. do {
  410. if (*op) {
  411. status2 =
  412. acpi_ps_complete_this_op(walk_state, *op);
  413. if (ACPI_FAILURE(status2)) {
  414. return_ACPI_STATUS(status2);
  415. }
  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. #if 0
  422. /*
  423. * TBD: Cleanup parse ops on error
  424. */
  425. if (*op == NULL) {
  426. acpi_ps_pop_scope(parser_state, op,
  427. &walk_state->arg_types,
  428. &walk_state->arg_count);
  429. }
  430. #endif
  431. walk_state->prev_op = NULL;
  432. walk_state->prev_arg_types = walk_state->arg_types;
  433. return_ACPI_STATUS(status);
  434. }
  435. /* This scope complete? */
  436. if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
  437. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  438. &walk_state->arg_types,
  439. &walk_state->arg_count);
  440. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
  441. } else {
  442. *op = NULL;
  443. }
  444. return_ACPI_STATUS(AE_OK);
  445. }
  446. /*******************************************************************************
  447. *
  448. * FUNCTION: acpi_ps_complete_final_op
  449. *
  450. * PARAMETERS: walk_state - Current state
  451. * op - Current Op
  452. * status - Current parse status before complete last
  453. * Op
  454. *
  455. * RETURN: Status
  456. *
  457. * DESCRIPTION: Complete last Op.
  458. *
  459. ******************************************************************************/
  460. acpi_status
  461. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  462. union acpi_parse_object *op, acpi_status status)
  463. {
  464. acpi_status status2;
  465. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  466. /*
  467. * Complete the last Op (if not completed), and clear the scope stack.
  468. * It is easily possible to end an AML "package" with an unbounded number
  469. * of open scopes (such as when several ASL blocks are closed with
  470. * sequential closing braces). We want to terminate each one cleanly.
  471. */
  472. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  473. op));
  474. do {
  475. if (op) {
  476. if (walk_state->ascending_callback != NULL) {
  477. walk_state->op = op;
  478. walk_state->op_info =
  479. acpi_ps_get_opcode_info(op->common.
  480. aml_opcode);
  481. walk_state->opcode = op->common.aml_opcode;
  482. status =
  483. walk_state->ascending_callback(walk_state);
  484. status =
  485. acpi_ps_next_parse_state(walk_state, op,
  486. status);
  487. if (status == AE_CTRL_PENDING) {
  488. status =
  489. acpi_ps_complete_op(walk_state, &op,
  490. AE_OK);
  491. if (ACPI_FAILURE(status)) {
  492. return_ACPI_STATUS(status);
  493. }
  494. }
  495. if (status == AE_CTRL_TERMINATE) {
  496. status = AE_OK;
  497. /* Clean up */
  498. do {
  499. if (op) {
  500. status2 =
  501. acpi_ps_complete_this_op
  502. (walk_state, op);
  503. if (ACPI_FAILURE
  504. (status2)) {
  505. return_ACPI_STATUS
  506. (status2);
  507. }
  508. }
  509. acpi_ps_pop_scope(&
  510. (walk_state->
  511. parser_state),
  512. &op,
  513. &walk_state->
  514. arg_types,
  515. &walk_state->
  516. arg_count);
  517. } while (op);
  518. return_ACPI_STATUS(status);
  519. }
  520. else if (ACPI_FAILURE(status)) {
  521. /* First error is most important */
  522. (void)
  523. acpi_ps_complete_this_op(walk_state,
  524. op);
  525. return_ACPI_STATUS(status);
  526. }
  527. }
  528. status2 = acpi_ps_complete_this_op(walk_state, op);
  529. if (ACPI_FAILURE(status2)) {
  530. return_ACPI_STATUS(status2);
  531. }
  532. }
  533. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  534. &walk_state->arg_types,
  535. &walk_state->arg_count);
  536. } while (op);
  537. return_ACPI_STATUS(status);
  538. }