nsparse.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 - 2018, 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. status = acpi_ps_execute_table(info);
  94. cleanup:
  95. if (info) {
  96. ACPI_FREE(info->full_pathname);
  97. info->full_pathname = NULL;
  98. }
  99. ACPI_FREE(info);
  100. acpi_ut_remove_reference(method_obj);
  101. return_ACPI_STATUS(status);
  102. }
  103. /*******************************************************************************
  104. *
  105. * FUNCTION: ns_one_complete_parse
  106. *
  107. * PARAMETERS: pass_number - 1 or 2
  108. * table_desc - The table to be parsed.
  109. *
  110. * RETURN: Status
  111. *
  112. * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
  113. *
  114. ******************************************************************************/
  115. acpi_status
  116. acpi_ns_one_complete_parse(u32 pass_number,
  117. u32 table_index,
  118. struct acpi_namespace_node *start_node)
  119. {
  120. union acpi_parse_object *parse_root;
  121. acpi_status status;
  122. u32 aml_length;
  123. u8 *aml_start;
  124. struct acpi_walk_state *walk_state;
  125. struct acpi_table_header *table;
  126. acpi_owner_id owner_id;
  127. ACPI_FUNCTION_TRACE(ns_one_complete_parse);
  128. status = acpi_get_table_by_index(table_index, &table);
  129. if (ACPI_FAILURE(status)) {
  130. return_ACPI_STATUS(status);
  131. }
  132. /* Table must consist of at least a complete header */
  133. if (table->length < sizeof(struct acpi_table_header)) {
  134. return_ACPI_STATUS(AE_BAD_HEADER);
  135. }
  136. aml_start = (u8 *)table + sizeof(struct acpi_table_header);
  137. aml_length = table->length - sizeof(struct acpi_table_header);
  138. status = acpi_tb_get_owner_id(table_index, &owner_id);
  139. if (ACPI_FAILURE(status)) {
  140. return_ACPI_STATUS(status);
  141. }
  142. /* Create and init a Root Node */
  143. parse_root = acpi_ps_create_scope_op(aml_start);
  144. if (!parse_root) {
  145. return_ACPI_STATUS(AE_NO_MEMORY);
  146. }
  147. /* Create and initialize a new walk state */
  148. walk_state = acpi_ds_create_walk_state(owner_id, NULL, NULL, NULL);
  149. if (!walk_state) {
  150. acpi_ps_free_op(parse_root);
  151. return_ACPI_STATUS(AE_NO_MEMORY);
  152. }
  153. status = acpi_ds_init_aml_walk(walk_state, parse_root, NULL,
  154. aml_start, aml_length, NULL,
  155. (u8)pass_number);
  156. if (ACPI_FAILURE(status)) {
  157. acpi_ds_delete_walk_state(walk_state);
  158. goto cleanup;
  159. }
  160. /* Found OSDT table, enable the namespace override feature */
  161. if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_OSDT) &&
  162. pass_number == ACPI_IMODE_LOAD_PASS1) {
  163. walk_state->namespace_override = TRUE;
  164. }
  165. /* start_node is the default location to load the table */
  166. if (start_node && start_node != acpi_gbl_root_node) {
  167. status =
  168. acpi_ds_scope_stack_push(start_node, ACPI_TYPE_METHOD,
  169. walk_state);
  170. if (ACPI_FAILURE(status)) {
  171. acpi_ds_delete_walk_state(walk_state);
  172. goto cleanup;
  173. }
  174. }
  175. /* Parse the AML */
  176. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  177. "*PARSE* pass %u parse\n", pass_number));
  178. acpi_ex_enter_interpreter();
  179. status = acpi_ps_parse_aml(walk_state);
  180. acpi_ex_exit_interpreter();
  181. cleanup:
  182. acpi_ps_delete_parse_tree(parse_root);
  183. return_ACPI_STATUS(status);
  184. }
  185. /*******************************************************************************
  186. *
  187. * FUNCTION: acpi_ns_parse_table
  188. *
  189. * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
  190. * start_node - Where to enter the table into the namespace
  191. *
  192. * RETURN: Status
  193. *
  194. * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
  195. *
  196. ******************************************************************************/
  197. acpi_status
  198. acpi_ns_parse_table(u32 table_index, struct acpi_namespace_node *start_node)
  199. {
  200. acpi_status status;
  201. ACPI_FUNCTION_TRACE(ns_parse_table);
  202. if (acpi_gbl_execute_tables_as_methods) {
  203. /*
  204. * This case executes the AML table as one large control method.
  205. * The point of this is to execute any module-level code in-place
  206. * as the table is parsed. Some AML code depends on this behavior.
  207. *
  208. * It is a run-time option at this time, but will eventually become
  209. * the default.
  210. *
  211. * Note: This causes the table to only have a single-pass parse.
  212. * However, this is compatible with other ACPI implementations.
  213. */
  214. ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
  215. "%s: **** Start table execution pass\n",
  216. ACPI_GET_FUNCTION_NAME));
  217. status = acpi_ns_execute_table(table_index, start_node);
  218. if (ACPI_FAILURE(status)) {
  219. return_ACPI_STATUS(status);
  220. }
  221. } else {
  222. /*
  223. * AML Parse, pass 1
  224. *
  225. * In this pass, we load most of the namespace. Control methods
  226. * are not parsed until later. A parse tree is not created.
  227. * Instead, each Parser Op subtree is deleted when it is finished.
  228. * This saves a great deal of memory, and allows a small cache of
  229. * parse objects to service the entire parse. The second pass of
  230. * the parse then performs another complete parse of the AML.
  231. */
  232. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start pass 1\n"));
  233. status = acpi_ns_one_complete_parse(ACPI_IMODE_LOAD_PASS1,
  234. table_index, start_node);
  235. if (ACPI_FAILURE(status)) {
  236. return_ACPI_STATUS(status);
  237. }
  238. /*
  239. * AML Parse, pass 2
  240. *
  241. * In this pass, we resolve forward references and other things
  242. * that could not be completed during the first pass.
  243. * Another complete parse of the AML is performed, but the
  244. * overhead of this is compensated for by the fact that the
  245. * parse objects are all cached.
  246. */
  247. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start pass 2\n"));
  248. status = acpi_ns_one_complete_parse(ACPI_IMODE_LOAD_PASS2,
  249. table_index, start_node);
  250. if (ACPI_FAILURE(status)) {
  251. return_ACPI_STATUS(status);
  252. }
  253. }
  254. return_ACPI_STATUS(status);
  255. }