psobject.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /******************************************************************************
  2. *
  3. * Module Name: psobject - Support for parse objects
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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. /* Dump the context surrounding the invalid opcode */
  108. acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
  109. aml - 16), 48, DB_BYTE_DISPLAY,
  110. (aml_offset +
  111. sizeof(struct acpi_table_header) -
  112. 16));
  113. acpi_os_printf(" */\n");
  114. #endif
  115. }
  116. /* Increment past one-byte or two-byte opcode */
  117. walk_state->parser_state.aml++;
  118. if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */
  119. walk_state->parser_state.aml++;
  120. }
  121. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  122. default:
  123. /* Found opcode info, this is a normal opcode */
  124. walk_state->parser_state.aml +=
  125. acpi_ps_get_opcode_size(walk_state->opcode);
  126. walk_state->arg_types = walk_state->op_info->parse_args;
  127. break;
  128. }
  129. return_ACPI_STATUS(AE_OK);
  130. }
  131. /*******************************************************************************
  132. *
  133. * FUNCTION: acpi_ps_build_named_op
  134. *
  135. * PARAMETERS: walk_state - Current state
  136. * aml_op_start - Begin of named Op in AML
  137. * unnamed_op - Early Op (not a named Op)
  138. * op - Returned Op
  139. *
  140. * RETURN: Status
  141. *
  142. * DESCRIPTION: Parse a named Op
  143. *
  144. ******************************************************************************/
  145. acpi_status
  146. acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
  147. u8 *aml_op_start,
  148. union acpi_parse_object *unnamed_op,
  149. union acpi_parse_object **op)
  150. {
  151. acpi_status status = AE_OK;
  152. union acpi_parse_object *arg = NULL;
  153. ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
  154. unnamed_op->common.value.arg = NULL;
  155. unnamed_op->common.arg_list_length = 0;
  156. unnamed_op->common.aml_opcode = walk_state->opcode;
  157. /*
  158. * Get and append arguments until we find the node that contains
  159. * the name (the type ARGP_NAME).
  160. */
  161. while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
  162. (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
  163. status =
  164. acpi_ps_get_next_arg(walk_state,
  165. &(walk_state->parser_state),
  166. GET_CURRENT_ARG_TYPE(walk_state->
  167. arg_types), &arg);
  168. if (ACPI_FAILURE(status)) {
  169. return_ACPI_STATUS(status);
  170. }
  171. acpi_ps_append_arg(unnamed_op, arg);
  172. INCREMENT_ARG_LIST(walk_state->arg_types);
  173. }
  174. /*
  175. * Make sure that we found a NAME and didn't run out of arguments
  176. */
  177. if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
  178. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  179. }
  180. /* We know that this arg is a name, move to next arg */
  181. INCREMENT_ARG_LIST(walk_state->arg_types);
  182. /*
  183. * Find the object. This will either insert the object into
  184. * the namespace or simply look it up
  185. */
  186. walk_state->op = NULL;
  187. status = walk_state->descending_callback(walk_state, op);
  188. if (ACPI_FAILURE(status)) {
  189. if (status != AE_CTRL_TERMINATE) {
  190. ACPI_EXCEPTION((AE_INFO, status,
  191. "During name lookup/catalog"));
  192. }
  193. return_ACPI_STATUS(status);
  194. }
  195. if (!*op) {
  196. return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
  197. }
  198. status = acpi_ps_next_parse_state(walk_state, *op, status);
  199. if (ACPI_FAILURE(status)) {
  200. if (status == AE_CTRL_PENDING) {
  201. status = AE_CTRL_PARSE_PENDING;
  202. }
  203. return_ACPI_STATUS(status);
  204. }
  205. acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
  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. 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, aml_op_start);
  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. }
  298. /*
  299. * Special case for both Increment() and Decrement(), where
  300. * the lone argument is both a source and a target.
  301. */
  302. else if ((parent_scope->common.aml_opcode == AML_INCREMENT_OP)
  303. || (parent_scope->common.aml_opcode ==
  304. AML_DECREMENT_OP)) {
  305. op->common.flags |= ACPI_PARSEOP_TARGET;
  306. }
  307. }
  308. if (walk_state->descending_callback != NULL) {
  309. /*
  310. * Find the object. This will either insert the object into
  311. * the namespace or simply look it up
  312. */
  313. walk_state->op = *new_op = op;
  314. status = walk_state->descending_callback(walk_state, &op);
  315. status = acpi_ps_next_parse_state(walk_state, op, status);
  316. if (status == AE_CTRL_PENDING) {
  317. status = AE_CTRL_PARSE_PENDING;
  318. }
  319. }
  320. return_ACPI_STATUS(status);
  321. }
  322. /*******************************************************************************
  323. *
  324. * FUNCTION: acpi_ps_complete_op
  325. *
  326. * PARAMETERS: walk_state - Current state
  327. * op - Returned Op
  328. * status - Parse status before complete Op
  329. *
  330. * RETURN: Status
  331. *
  332. * DESCRIPTION: Complete Op
  333. *
  334. ******************************************************************************/
  335. acpi_status
  336. acpi_ps_complete_op(struct acpi_walk_state *walk_state,
  337. union acpi_parse_object **op, acpi_status status)
  338. {
  339. acpi_status status2;
  340. ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
  341. /*
  342. * Finished one argument of the containing scope
  343. */
  344. walk_state->parser_state.scope->parse_scope.arg_count--;
  345. /* Close this Op (will result in parse subtree deletion) */
  346. status2 = acpi_ps_complete_this_op(walk_state, *op);
  347. if (ACPI_FAILURE(status2)) {
  348. return_ACPI_STATUS(status2);
  349. }
  350. *op = NULL;
  351. switch (status) {
  352. case AE_OK:
  353. break;
  354. case AE_CTRL_TRANSFER:
  355. /* We are about to transfer to a called method */
  356. walk_state->prev_op = NULL;
  357. walk_state->prev_arg_types = walk_state->arg_types;
  358. return_ACPI_STATUS(status);
  359. case AE_CTRL_END:
  360. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  361. &walk_state->arg_types,
  362. &walk_state->arg_count);
  363. if (*op) {
  364. walk_state->op = *op;
  365. walk_state->op_info =
  366. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  367. walk_state->opcode = (*op)->common.aml_opcode;
  368. status = walk_state->ascending_callback(walk_state);
  369. status =
  370. acpi_ps_next_parse_state(walk_state, *op, status);
  371. status2 = acpi_ps_complete_this_op(walk_state, *op);
  372. if (ACPI_FAILURE(status2)) {
  373. return_ACPI_STATUS(status2);
  374. }
  375. }
  376. status = AE_OK;
  377. break;
  378. case AE_CTRL_BREAK:
  379. case AE_CTRL_CONTINUE:
  380. /* Pop off scopes until we find the While */
  381. while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
  382. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  383. &walk_state->arg_types,
  384. &walk_state->arg_count);
  385. }
  386. /* Close this iteration of the While loop */
  387. walk_state->op = *op;
  388. walk_state->op_info =
  389. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  390. walk_state->opcode = (*op)->common.aml_opcode;
  391. status = walk_state->ascending_callback(walk_state);
  392. status = acpi_ps_next_parse_state(walk_state, *op, status);
  393. status2 = acpi_ps_complete_this_op(walk_state, *op);
  394. if (ACPI_FAILURE(status2)) {
  395. return_ACPI_STATUS(status2);
  396. }
  397. status = AE_OK;
  398. break;
  399. case AE_CTRL_TERMINATE:
  400. /* Clean up */
  401. do {
  402. if (*op) {
  403. status2 =
  404. acpi_ps_complete_this_op(walk_state, *op);
  405. if (ACPI_FAILURE(status2)) {
  406. return_ACPI_STATUS(status2);
  407. }
  408. acpi_ut_delete_generic_state
  409. (acpi_ut_pop_generic_state
  410. (&walk_state->control_state));
  411. }
  412. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  413. &walk_state->arg_types,
  414. &walk_state->arg_count);
  415. } while (*op);
  416. return_ACPI_STATUS(AE_OK);
  417. default: /* All other non-AE_OK status */
  418. do {
  419. if (*op) {
  420. status2 =
  421. acpi_ps_complete_this_op(walk_state, *op);
  422. if (ACPI_FAILURE(status2)) {
  423. return_ACPI_STATUS(status2);
  424. }
  425. }
  426. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  427. &walk_state->arg_types,
  428. &walk_state->arg_count);
  429. } while (*op);
  430. #if 0
  431. /*
  432. * TBD: Cleanup parse ops on error
  433. */
  434. if (*op == NULL) {
  435. acpi_ps_pop_scope(parser_state, op,
  436. &walk_state->arg_types,
  437. &walk_state->arg_count);
  438. }
  439. #endif
  440. walk_state->prev_op = NULL;
  441. walk_state->prev_arg_types = walk_state->arg_types;
  442. return_ACPI_STATUS(status);
  443. }
  444. /* This scope complete? */
  445. if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
  446. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  447. &walk_state->arg_types,
  448. &walk_state->arg_count);
  449. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
  450. } else {
  451. *op = NULL;
  452. }
  453. return_ACPI_STATUS(AE_OK);
  454. }
  455. /*******************************************************************************
  456. *
  457. * FUNCTION: acpi_ps_complete_final_op
  458. *
  459. * PARAMETERS: walk_state - Current state
  460. * op - Current Op
  461. * status - Current parse status before complete last
  462. * Op
  463. *
  464. * RETURN: Status
  465. *
  466. * DESCRIPTION: Complete last Op.
  467. *
  468. ******************************************************************************/
  469. acpi_status
  470. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  471. union acpi_parse_object *op, acpi_status status)
  472. {
  473. acpi_status status2;
  474. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  475. /*
  476. * Complete the last Op (if not completed), and clear the scope stack.
  477. * It is easily possible to end an AML "package" with an unbounded number
  478. * of open scopes (such as when several ASL blocks are closed with
  479. * sequential closing braces). We want to terminate each one cleanly.
  480. */
  481. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  482. op));
  483. do {
  484. if (op) {
  485. if (walk_state->ascending_callback != NULL) {
  486. walk_state->op = op;
  487. walk_state->op_info =
  488. acpi_ps_get_opcode_info(op->common.
  489. aml_opcode);
  490. walk_state->opcode = op->common.aml_opcode;
  491. status =
  492. walk_state->ascending_callback(walk_state);
  493. status =
  494. acpi_ps_next_parse_state(walk_state, op,
  495. status);
  496. if (status == AE_CTRL_PENDING) {
  497. status =
  498. acpi_ps_complete_op(walk_state, &op,
  499. AE_OK);
  500. if (ACPI_FAILURE(status)) {
  501. return_ACPI_STATUS(status);
  502. }
  503. }
  504. if (status == AE_CTRL_TERMINATE) {
  505. status = AE_OK;
  506. /* Clean up */
  507. do {
  508. if (op) {
  509. status2 =
  510. acpi_ps_complete_this_op
  511. (walk_state, op);
  512. if (ACPI_FAILURE
  513. (status2)) {
  514. return_ACPI_STATUS
  515. (status2);
  516. }
  517. }
  518. acpi_ps_pop_scope(&
  519. (walk_state->
  520. parser_state),
  521. &op,
  522. &walk_state->
  523. arg_types,
  524. &walk_state->
  525. arg_count);
  526. } while (op);
  527. return_ACPI_STATUS(status);
  528. }
  529. else if (ACPI_FAILURE(status)) {
  530. /* First error is most important */
  531. (void)
  532. acpi_ps_complete_this_op(walk_state,
  533. op);
  534. return_ACPI_STATUS(status);
  535. }
  536. }
  537. status2 = acpi_ps_complete_this_op(walk_state, op);
  538. if (ACPI_FAILURE(status2)) {
  539. return_ACPI_STATUS(status2);
  540. }
  541. }
  542. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  543. &walk_state->arg_types,
  544. &walk_state->arg_count);
  545. } while (op);
  546. return_ACPI_STATUS(status);
  547. }