nsparse.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nsparse - namespace interface to AML parser
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acnamesp.h"
  12. #include "acparser.h"
  13. #include "acdispat.h"
  14. #include "actables.h"
  15. #include "acinterp.h"
  16. #define _COMPONENT ACPI_NAMESPACE
  17. ACPI_MODULE_NAME("nsparse")
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: ns_execute_table
  21. *
  22. * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
  23. * start_node - Where to enter the table into the namespace
  24. *
  25. * RETURN: Status
  26. *
  27. * DESCRIPTION: Load ACPI/AML table by executing the entire table as a single
  28. * large control method.
  29. *
  30. * NOTE: The point of this is to execute any module-level code in-place
  31. * as the table is parsed. Some AML code depends on this behavior.
  32. *
  33. * It is a run-time option at this time, but will eventually become
  34. * the default.
  35. *
  36. * Note: This causes the table to only have a single-pass parse.
  37. * However, this is compatible with other ACPI implementations.
  38. *
  39. ******************************************************************************/
  40. acpi_status
  41. acpi_ns_execute_table(u32 table_index, struct acpi_namespace_node *start_node)
  42. {
  43. acpi_status status;
  44. struct acpi_table_header *table;
  45. acpi_owner_id owner_id;
  46. struct acpi_evaluate_info *info = NULL;
  47. u32 aml_length;
  48. u8 *aml_start;
  49. union acpi_operand_object *method_obj = NULL;
  50. ACPI_FUNCTION_TRACE(ns_execute_table);
  51. status = acpi_get_table_by_index(table_index, &table);
  52. if (ACPI_FAILURE(status)) {
  53. return_ACPI_STATUS(status);
  54. }
  55. /* Table must consist of at least a complete header */
  56. if (table->length < sizeof(struct acpi_table_header)) {
  57. return_ACPI_STATUS(AE_BAD_HEADER);
  58. }
  59. aml_start = (u8 *)table + sizeof(struct acpi_table_header);
  60. aml_length = table->length - sizeof(struct acpi_table_header);
  61. status = acpi_tb_get_owner_id(table_index, &owner_id);
  62. if (ACPI_FAILURE(status)) {
  63. return_ACPI_STATUS(status);
  64. }
  65. /* Create, initialize, and link a new temporary method object */
  66. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  67. if (!method_obj) {
  68. return_ACPI_STATUS(AE_NO_MEMORY);
  69. }
  70. /* Allocate the evaluation information block */
  71. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  72. if (!info) {
  73. status = AE_NO_MEMORY;
  74. goto cleanup;
  75. }
  76. ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
  77. "%s: Create table pseudo-method for [%4.4s] @%p, method %p\n",
  78. ACPI_GET_FUNCTION_NAME, table->signature, table,
  79. method_obj));
  80. method_obj->method.aml_start = aml_start;
  81. method_obj->method.aml_length = aml_length;
  82. method_obj->method.owner_id = owner_id;
  83. method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
  84. info->pass_number = ACPI_IMODE_EXECUTE;
  85. info->node = start_node;
  86. info->obj_desc = method_obj;
  87. info->node_flags = info->node->flags;
  88. info->full_pathname = acpi_ns_get_normalized_pathname(info->node, TRUE);
  89. if (!info->full_pathname) {
  90. status = AE_NO_MEMORY;
  91. goto cleanup;
  92. }
  93. /* Optional object evaluation log */
  94. ACPI_DEBUG_PRINT_RAW((ACPI_DB_EVALUATION,
  95. "%-26s: (Definition Block level)\n",
  96. "Module-level evaluation"));
  97. status = acpi_ps_execute_table(info);
  98. /* Optional object evaluation log */
  99. ACPI_DEBUG_PRINT_RAW((ACPI_DB_EVALUATION,
  100. "%-26s: (Definition Block level)\n",
  101. "Module-level complete"));
  102. cleanup:
  103. if (info) {
  104. ACPI_FREE(info->full_pathname);
  105. info->full_pathname = NULL;
  106. }
  107. ACPI_FREE(info);
  108. acpi_ut_remove_reference(method_obj);
  109. return_ACPI_STATUS(status);
  110. }
  111. /*******************************************************************************
  112. *
  113. * FUNCTION: ns_one_complete_parse
  114. *
  115. * PARAMETERS: pass_number - 1 or 2
  116. * table_desc - The table to be parsed.
  117. *
  118. * RETURN: Status
  119. *
  120. * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
  121. *
  122. ******************************************************************************/
  123. acpi_status
  124. acpi_ns_one_complete_parse(u32 pass_number,
  125. u32 table_index,
  126. struct acpi_namespace_node *start_node)
  127. {
  128. union acpi_parse_object *parse_root;
  129. acpi_status status;
  130. u32 aml_length;
  131. u8 *aml_start;
  132. struct acpi_walk_state *walk_state;
  133. struct acpi_table_header *table;
  134. acpi_owner_id owner_id;
  135. ACPI_FUNCTION_TRACE(ns_one_complete_parse);
  136. status = acpi_get_table_by_index(table_index, &table);
  137. if (ACPI_FAILURE(status)) {
  138. return_ACPI_STATUS(status);
  139. }
  140. /* Table must consist of at least a complete header */
  141. if (table->length < sizeof(struct acpi_table_header)) {
  142. return_ACPI_STATUS(AE_BAD_HEADER);
  143. }
  144. aml_start = (u8 *)table + sizeof(struct acpi_table_header);
  145. aml_length = table->length - sizeof(struct acpi_table_header);
  146. status = acpi_tb_get_owner_id(table_index, &owner_id);
  147. if (ACPI_FAILURE(status)) {
  148. return_ACPI_STATUS(status);
  149. }
  150. /* Create and init a Root Node */
  151. parse_root = acpi_ps_create_scope_op(aml_start);
  152. if (!parse_root) {
  153. return_ACPI_STATUS(AE_NO_MEMORY);
  154. }
  155. /* Create and initialize a new walk state */
  156. walk_state = acpi_ds_create_walk_state(owner_id, NULL, NULL, NULL);
  157. if (!walk_state) {
  158. acpi_ps_free_op(parse_root);
  159. return_ACPI_STATUS(AE_NO_MEMORY);
  160. }
  161. status = acpi_ds_init_aml_walk(walk_state, parse_root, NULL,
  162. aml_start, aml_length, NULL,
  163. (u8)pass_number);
  164. if (ACPI_FAILURE(status)) {
  165. acpi_ds_delete_walk_state(walk_state);
  166. goto cleanup;
  167. }
  168. /* Found OSDT table, enable the namespace override feature */
  169. if (ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_OSDT) &&
  170. pass_number == ACPI_IMODE_LOAD_PASS1) {
  171. walk_state->namespace_override = TRUE;
  172. }
  173. /* start_node is the default location to load the table */
  174. if (start_node && start_node != acpi_gbl_root_node) {
  175. status =
  176. acpi_ds_scope_stack_push(start_node, ACPI_TYPE_METHOD,
  177. walk_state);
  178. if (ACPI_FAILURE(status)) {
  179. acpi_ds_delete_walk_state(walk_state);
  180. goto cleanup;
  181. }
  182. }
  183. /* Parse the AML */
  184. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  185. "*PARSE* pass %u parse\n", pass_number));
  186. acpi_ex_enter_interpreter();
  187. status = acpi_ps_parse_aml(walk_state);
  188. acpi_ex_exit_interpreter();
  189. cleanup:
  190. acpi_ps_delete_parse_tree(parse_root);
  191. return_ACPI_STATUS(status);
  192. }
  193. /*******************************************************************************
  194. *
  195. * FUNCTION: acpi_ns_parse_table
  196. *
  197. * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
  198. * start_node - Where to enter the table into the namespace
  199. *
  200. * RETURN: Status
  201. *
  202. * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
  203. *
  204. ******************************************************************************/
  205. acpi_status
  206. acpi_ns_parse_table(u32 table_index, struct acpi_namespace_node *start_node)
  207. {
  208. acpi_status status;
  209. ACPI_FUNCTION_TRACE(ns_parse_table);
  210. /*
  211. * Executes the AML table as one large control method.
  212. * The point of this is to execute any module-level code in-place
  213. * as the table is parsed. Some AML code depends on this behavior.
  214. *
  215. * Note: This causes the table to only have a single-pass parse.
  216. * However, this is compatible with other ACPI implementations.
  217. */
  218. ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
  219. "%s: **** Start table execution pass\n",
  220. ACPI_GET_FUNCTION_NAME));
  221. status = acpi_ns_execute_table(table_index, start_node);
  222. return_ACPI_STATUS(status);
  223. }