nsparse.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /******************************************************************************
  2. *
  3. * Module Name: nsparse - namespace interface to AML parser
  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 "acnamesp.h"
  45. #include "acparser.h"
  46. #include "acdispat.h"
  47. #include "actables.h"
  48. #include "acinterp.h"
  49. #define _COMPONENT ACPI_NAMESPACE
  50. ACPI_MODULE_NAME("nsparse")
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: ns_execute_table
  54. *
  55. * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
  56. * start_node - Where to enter the table into the namespace
  57. *
  58. * RETURN: Status
  59. *
  60. * DESCRIPTION: Load ACPI/AML table by executing the entire table as a
  61. * term_list.
  62. *
  63. ******************************************************************************/
  64. acpi_status
  65. acpi_ns_execute_table(u32 table_index, struct acpi_namespace_node *start_node)
  66. {
  67. acpi_status status;
  68. struct acpi_table_header *table;
  69. acpi_owner_id owner_id;
  70. struct acpi_evaluate_info *info = NULL;
  71. u32 aml_length;
  72. u8 *aml_start;
  73. union acpi_operand_object *method_obj = NULL;
  74. ACPI_FUNCTION_TRACE(ns_execute_table);
  75. status = acpi_get_table_by_index(table_index, &table);
  76. if (ACPI_FAILURE(status)) {
  77. return_ACPI_STATUS(status);
  78. }
  79. /* Table must consist of at least a complete header */
  80. if (table->length < sizeof(struct acpi_table_header)) {
  81. return_ACPI_STATUS(AE_BAD_HEADER);
  82. }
  83. aml_start = (u8 *)table + sizeof(struct acpi_table_header);
  84. aml_length = table->length - sizeof(struct acpi_table_header);
  85. status = acpi_tb_get_owner_id(table_index, &owner_id);
  86. if (ACPI_FAILURE(status)) {
  87. return_ACPI_STATUS(status);
  88. }
  89. /* Create, initialize, and link a new temporary method object */
  90. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  91. if (!method_obj) {
  92. return_ACPI_STATUS(AE_NO_MEMORY);
  93. }
  94. /* Allocate the evaluation information block */
  95. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  96. if (!info) {
  97. status = AE_NO_MEMORY;
  98. goto cleanup;
  99. }
  100. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  101. "Create table code block: %p\n", method_obj));
  102. method_obj->method.aml_start = aml_start;
  103. method_obj->method.aml_length = aml_length;
  104. method_obj->method.owner_id = owner_id;
  105. method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
  106. info->pass_number = ACPI_IMODE_EXECUTE;
  107. info->node = start_node;
  108. info->obj_desc = method_obj;
  109. info->node_flags = info->node->flags;
  110. info->full_pathname = acpi_ns_get_normalized_pathname(info->node, TRUE);
  111. if (!info->full_pathname) {
  112. status = AE_NO_MEMORY;
  113. goto cleanup;
  114. }
  115. status = acpi_ps_execute_table(info);
  116. cleanup:
  117. if (info) {
  118. ACPI_FREE(info->full_pathname);
  119. info->full_pathname = NULL;
  120. }
  121. ACPI_FREE(info);
  122. acpi_ut_remove_reference(method_obj);
  123. return_ACPI_STATUS(status);
  124. }
  125. /*******************************************************************************
  126. *
  127. * FUNCTION: ns_one_complete_parse
  128. *
  129. * PARAMETERS: pass_number - 1 or 2
  130. * table_desc - The table to be parsed.
  131. *
  132. * RETURN: Status
  133. *
  134. * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
  135. *
  136. ******************************************************************************/
  137. acpi_status
  138. acpi_ns_one_complete_parse(u32 pass_number,
  139. u32 table_index,
  140. struct acpi_namespace_node *start_node)
  141. {
  142. union acpi_parse_object *parse_root;
  143. acpi_status status;
  144. u32 aml_length;
  145. u8 *aml_start;
  146. struct acpi_walk_state *walk_state;
  147. struct acpi_table_header *table;
  148. acpi_owner_id owner_id;
  149. ACPI_FUNCTION_TRACE(ns_one_complete_parse);
  150. status = acpi_get_table_by_index(table_index, &table);
  151. if (ACPI_FAILURE(status)) {
  152. return_ACPI_STATUS(status);
  153. }
  154. /* Table must consist of at least a complete header */
  155. if (table->length < sizeof(struct acpi_table_header)) {
  156. return_ACPI_STATUS(AE_BAD_HEADER);
  157. }
  158. aml_start = (u8 *)table + sizeof(struct acpi_table_header);
  159. aml_length = table->length - sizeof(struct acpi_table_header);
  160. status = acpi_tb_get_owner_id(table_index, &owner_id);
  161. if (ACPI_FAILURE(status)) {
  162. return_ACPI_STATUS(status);
  163. }
  164. /* Create and init a Root Node */
  165. parse_root = acpi_ps_create_scope_op(aml_start);
  166. if (!parse_root) {
  167. return_ACPI_STATUS(AE_NO_MEMORY);
  168. }
  169. /* Create and initialize a new walk state */
  170. walk_state = acpi_ds_create_walk_state(owner_id, NULL, NULL, NULL);
  171. if (!walk_state) {
  172. acpi_ps_free_op(parse_root);
  173. return_ACPI_STATUS(AE_NO_MEMORY);
  174. }
  175. status = acpi_ds_init_aml_walk(walk_state, parse_root, NULL,
  176. aml_start, aml_length, NULL,
  177. (u8)pass_number);
  178. if (ACPI_FAILURE(status)) {
  179. acpi_ds_delete_walk_state(walk_state);
  180. goto cleanup;
  181. }
  182. /* Found OSDT table, enable the namespace override feature */
  183. if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_OSDT) &&
  184. pass_number == ACPI_IMODE_LOAD_PASS1) {
  185. walk_state->namespace_override = TRUE;
  186. }
  187. /* start_node is the default location to load the table */
  188. if (start_node && start_node != acpi_gbl_root_node) {
  189. status =
  190. acpi_ds_scope_stack_push(start_node, ACPI_TYPE_METHOD,
  191. walk_state);
  192. if (ACPI_FAILURE(status)) {
  193. acpi_ds_delete_walk_state(walk_state);
  194. goto cleanup;
  195. }
  196. }
  197. /* Parse the AML */
  198. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  199. "*PARSE* pass %u parse\n", pass_number));
  200. acpi_ex_enter_interpreter();
  201. status = acpi_ps_parse_aml(walk_state);
  202. acpi_ex_exit_interpreter();
  203. cleanup:
  204. acpi_ps_delete_parse_tree(parse_root);
  205. return_ACPI_STATUS(status);
  206. }
  207. /*******************************************************************************
  208. *
  209. * FUNCTION: acpi_ns_parse_table
  210. *
  211. * PARAMETERS: table_desc - An ACPI table descriptor for table to parse
  212. * start_node - Where to enter the table into the namespace
  213. *
  214. * RETURN: Status
  215. *
  216. * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
  217. *
  218. ******************************************************************************/
  219. acpi_status
  220. acpi_ns_parse_table(u32 table_index, struct acpi_namespace_node *start_node)
  221. {
  222. acpi_status status;
  223. ACPI_FUNCTION_TRACE(ns_parse_table);
  224. if (acpi_gbl_parse_table_as_term_list) {
  225. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start load pass\n"));
  226. status = acpi_ns_execute_table(table_index, start_node);
  227. if (ACPI_FAILURE(status)) {
  228. return_ACPI_STATUS(status);
  229. }
  230. } else {
  231. /*
  232. * AML Parse, pass 1
  233. *
  234. * In this pass, we load most of the namespace. Control methods
  235. * are not parsed until later. A parse tree is not created.
  236. * Instead, each Parser Op subtree is deleted when it is finished.
  237. * This saves a great deal of memory, and allows a small cache of
  238. * parse objects to service the entire parse. The second pass of
  239. * the parse then performs another complete parse of the AML.
  240. */
  241. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start pass 1\n"));
  242. status = acpi_ns_one_complete_parse(ACPI_IMODE_LOAD_PASS1,
  243. table_index, start_node);
  244. if (ACPI_FAILURE(status)) {
  245. return_ACPI_STATUS(status);
  246. }
  247. /*
  248. * AML Parse, pass 2
  249. *
  250. * In this pass, we resolve forward references and other things
  251. * that could not be completed during the first pass.
  252. * Another complete parse of the AML is performed, but the
  253. * overhead of this is compensated for by the fact that the
  254. * parse objects are all cached.
  255. */
  256. ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "**** Start pass 2\n"));
  257. status = acpi_ns_one_complete_parse(ACPI_IMODE_LOAD_PASS2,
  258. table_index, start_node);
  259. if (ACPI_FAILURE(status)) {
  260. return_ACPI_STATUS(status);
  261. }
  262. }
  263. return_ACPI_STATUS(status);
  264. }