psobject.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /******************************************************************************
  2. *
  3. * Module Name: psobject - Support for parse objects
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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. } 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_complete_op
  318. *
  319. * PARAMETERS: walk_state - Current state
  320. * op - Returned Op
  321. * status - Parse status before complete Op
  322. *
  323. * RETURN: Status
  324. *
  325. * DESCRIPTION: Complete Op
  326. *
  327. ******************************************************************************/
  328. acpi_status
  329. acpi_ps_complete_op(struct acpi_walk_state *walk_state,
  330. union acpi_parse_object **op, acpi_status status)
  331. {
  332. acpi_status status2;
  333. ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
  334. /*
  335. * Finished one argument of the containing scope
  336. */
  337. walk_state->parser_state.scope->parse_scope.arg_count--;
  338. /* Close this Op (will result in parse subtree deletion) */
  339. status2 = acpi_ps_complete_this_op(walk_state, *op);
  340. if (ACPI_FAILURE(status2)) {
  341. return_ACPI_STATUS(status2);
  342. }
  343. *op = NULL;
  344. switch (status) {
  345. case AE_OK:
  346. break;
  347. case AE_CTRL_TRANSFER:
  348. /* We are about to transfer to a called method */
  349. walk_state->prev_op = NULL;
  350. walk_state->prev_arg_types = walk_state->arg_types;
  351. return_ACPI_STATUS(status);
  352. case AE_CTRL_END:
  353. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  354. &walk_state->arg_types,
  355. &walk_state->arg_count);
  356. if (*op) {
  357. walk_state->op = *op;
  358. walk_state->op_info =
  359. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  360. walk_state->opcode = (*op)->common.aml_opcode;
  361. status = walk_state->ascending_callback(walk_state);
  362. status =
  363. acpi_ps_next_parse_state(walk_state, *op, status);
  364. status2 = acpi_ps_complete_this_op(walk_state, *op);
  365. if (ACPI_FAILURE(status2)) {
  366. return_ACPI_STATUS(status2);
  367. }
  368. }
  369. status = AE_OK;
  370. break;
  371. case AE_CTRL_BREAK:
  372. case AE_CTRL_CONTINUE:
  373. /* Pop off scopes until we find the While */
  374. while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
  375. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  376. &walk_state->arg_types,
  377. &walk_state->arg_count);
  378. }
  379. /* Close this iteration of the While loop */
  380. walk_state->op = *op;
  381. walk_state->op_info =
  382. acpi_ps_get_opcode_info((*op)->common.aml_opcode);
  383. walk_state->opcode = (*op)->common.aml_opcode;
  384. status = walk_state->ascending_callback(walk_state);
  385. status = acpi_ps_next_parse_state(walk_state, *op, status);
  386. status2 = acpi_ps_complete_this_op(walk_state, *op);
  387. if (ACPI_FAILURE(status2)) {
  388. return_ACPI_STATUS(status2);
  389. }
  390. status = AE_OK;
  391. break;
  392. case AE_CTRL_TERMINATE:
  393. /* Clean up */
  394. do {
  395. if (*op) {
  396. status2 =
  397. acpi_ps_complete_this_op(walk_state, *op);
  398. if (ACPI_FAILURE(status2)) {
  399. return_ACPI_STATUS(status2);
  400. }
  401. acpi_ut_delete_generic_state
  402. (acpi_ut_pop_generic_state
  403. (&walk_state->control_state));
  404. }
  405. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  406. &walk_state->arg_types,
  407. &walk_state->arg_count);
  408. } while (*op);
  409. return_ACPI_STATUS(AE_OK);
  410. default: /* All other non-AE_OK status */
  411. do {
  412. if (*op) {
  413. status2 =
  414. acpi_ps_complete_this_op(walk_state, *op);
  415. if (ACPI_FAILURE(status2)) {
  416. return_ACPI_STATUS(status2);
  417. }
  418. }
  419. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  420. &walk_state->arg_types,
  421. &walk_state->arg_count);
  422. } while (*op);
  423. #if 0
  424. /*
  425. * TBD: Cleanup parse ops on error
  426. */
  427. if (*op == NULL) {
  428. acpi_ps_pop_scope(parser_state, op,
  429. &walk_state->arg_types,
  430. &walk_state->arg_count);
  431. }
  432. #endif
  433. walk_state->prev_op = NULL;
  434. walk_state->prev_arg_types = walk_state->arg_types;
  435. return_ACPI_STATUS(status);
  436. }
  437. /* This scope complete? */
  438. if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
  439. acpi_ps_pop_scope(&(walk_state->parser_state), op,
  440. &walk_state->arg_types,
  441. &walk_state->arg_count);
  442. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
  443. } else {
  444. *op = NULL;
  445. }
  446. return_ACPI_STATUS(AE_OK);
  447. }
  448. /*******************************************************************************
  449. *
  450. * FUNCTION: acpi_ps_complete_final_op
  451. *
  452. * PARAMETERS: walk_state - Current state
  453. * op - Current Op
  454. * status - Current parse status before complete last
  455. * Op
  456. *
  457. * RETURN: Status
  458. *
  459. * DESCRIPTION: Complete last Op.
  460. *
  461. ******************************************************************************/
  462. acpi_status
  463. acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
  464. union acpi_parse_object *op, acpi_status status)
  465. {
  466. acpi_status status2;
  467. ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
  468. /*
  469. * Complete the last Op (if not completed), and clear the scope stack.
  470. * It is easily possible to end an AML "package" with an unbounded number
  471. * of open scopes (such as when several ASL blocks are closed with
  472. * sequential closing braces). We want to terminate each one cleanly.
  473. */
  474. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
  475. op));
  476. do {
  477. if (op) {
  478. if (walk_state->ascending_callback != NULL) {
  479. walk_state->op = op;
  480. walk_state->op_info =
  481. acpi_ps_get_opcode_info(op->common.
  482. aml_opcode);
  483. walk_state->opcode = op->common.aml_opcode;
  484. status =
  485. walk_state->ascending_callback(walk_state);
  486. status =
  487. acpi_ps_next_parse_state(walk_state, op,
  488. status);
  489. if (status == AE_CTRL_PENDING) {
  490. status =
  491. acpi_ps_complete_op(walk_state, &op,
  492. AE_OK);
  493. if (ACPI_FAILURE(status)) {
  494. return_ACPI_STATUS(status);
  495. }
  496. }
  497. if (status == AE_CTRL_TERMINATE) {
  498. status = AE_OK;
  499. /* Clean up */
  500. do {
  501. if (op) {
  502. status2 =
  503. acpi_ps_complete_this_op
  504. (walk_state, op);
  505. if (ACPI_FAILURE
  506. (status2)) {
  507. return_ACPI_STATUS
  508. (status2);
  509. }
  510. }
  511. acpi_ps_pop_scope(&
  512. (walk_state->
  513. parser_state),
  514. &op,
  515. &walk_state->
  516. arg_types,
  517. &walk_state->
  518. arg_count);
  519. } while (op);
  520. return_ACPI_STATUS(status);
  521. }
  522. else if (ACPI_FAILURE(status)) {
  523. /* First error is most important */
  524. (void)
  525. acpi_ps_complete_this_op(walk_state,
  526. op);
  527. return_ACPI_STATUS(status);
  528. }
  529. }
  530. status2 = acpi_ps_complete_this_op(walk_state, op);
  531. if (ACPI_FAILURE(status2)) {
  532. return_ACPI_STATUS(status2);
  533. }
  534. }
  535. acpi_ps_pop_scope(&(walk_state->parser_state), &op,
  536. &walk_state->arg_types,
  537. &walk_state->arg_count);
  538. } while (op);
  539. return_ACPI_STATUS(status);
  540. }