nssearch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*******************************************************************************
  2. *
  3. * Module Name: nssearch - Namespace search
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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. #ifdef ACPI_ASL_COMPILER
  46. #include "amlcode.h"
  47. #endif
  48. #define _COMPONENT ACPI_NAMESPACE
  49. ACPI_MODULE_NAME("nssearch")
  50. /* Local prototypes */
  51. static acpi_status
  52. acpi_ns_search_parent_tree(u32 target_name,
  53. struct acpi_namespace_node *node,
  54. acpi_object_type type,
  55. struct acpi_namespace_node **return_node);
  56. /*******************************************************************************
  57. *
  58. * FUNCTION: acpi_ns_search_one_scope
  59. *
  60. * PARAMETERS: target_name - Ascii ACPI name to search for
  61. * parent_node - Starting node where search will begin
  62. * Type - Object type to match
  63. * return_node - Where the matched Named obj is returned
  64. *
  65. * RETURN: Status
  66. *
  67. * DESCRIPTION: Search a single level of the namespace. Performs a
  68. * simple search of the specified level, and does not add
  69. * entries or search parents.
  70. *
  71. *
  72. * Named object lists are built (and subsequently dumped) in the
  73. * order in which the names are encountered during the namespace load;
  74. *
  75. * All namespace searching is linear in this implementation, but
  76. * could be easily modified to support any improved search
  77. * algorithm. However, the linear search was chosen for simplicity
  78. * and because the trees are small and the other interpreter
  79. * execution overhead is relatively high.
  80. *
  81. * Note: CPU execution analysis has shown that the AML interpreter spends
  82. * a very small percentage of its time searching the namespace. Therefore,
  83. * the linear search seems to be sufficient, as there would seem to be
  84. * little value in improving the search.
  85. *
  86. ******************************************************************************/
  87. acpi_status
  88. acpi_ns_search_one_scope(u32 target_name,
  89. struct acpi_namespace_node *parent_node,
  90. acpi_object_type type,
  91. struct acpi_namespace_node **return_node)
  92. {
  93. struct acpi_namespace_node *node;
  94. ACPI_FUNCTION_TRACE(ns_search_one_scope);
  95. #ifdef ACPI_DEBUG_OUTPUT
  96. if (ACPI_LV_NAMES & acpi_dbg_level) {
  97. char *scope_name;
  98. scope_name = acpi_ns_get_external_pathname(parent_node);
  99. if (scope_name) {
  100. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  101. "Searching %s (%p) For [%4.4s] (%s)\n",
  102. scope_name, parent_node,
  103. ACPI_CAST_PTR(char, &target_name),
  104. acpi_ut_get_type_name(type)));
  105. ACPI_FREE(scope_name);
  106. }
  107. }
  108. #endif
  109. /*
  110. * Search for name at this namespace level, which is to say that we
  111. * must search for the name among the children of this object
  112. */
  113. node = parent_node->child;
  114. while (node) {
  115. /* Check for match against the name */
  116. if (node->name.integer == target_name) {
  117. /* Resolve a control method alias if any */
  118. if (acpi_ns_get_type(node) ==
  119. ACPI_TYPE_LOCAL_METHOD_ALIAS) {
  120. node =
  121. ACPI_CAST_PTR(struct acpi_namespace_node,
  122. node->object);
  123. }
  124. /* Found matching entry */
  125. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  126. "Name [%4.4s] (%s) %p found in scope [%4.4s] %p\n",
  127. ACPI_CAST_PTR(char, &target_name),
  128. acpi_ut_get_type_name(node->type),
  129. node,
  130. acpi_ut_get_node_name(parent_node),
  131. parent_node));
  132. *return_node = node;
  133. return_ACPI_STATUS(AE_OK);
  134. }
  135. /* Didn't match name, move on to the next peer object */
  136. node = node->peer;
  137. }
  138. /* Searched entire namespace level, not found */
  139. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  140. "Name [%4.4s] (%s) not found in search in scope [%4.4s] "
  141. "%p first child %p\n",
  142. ACPI_CAST_PTR(char, &target_name),
  143. acpi_ut_get_type_name(type),
  144. acpi_ut_get_node_name(parent_node), parent_node,
  145. parent_node->child));
  146. return_ACPI_STATUS(AE_NOT_FOUND);
  147. }
  148. /*******************************************************************************
  149. *
  150. * FUNCTION: acpi_ns_search_parent_tree
  151. *
  152. * PARAMETERS: target_name - Ascii ACPI name to search for
  153. * Node - Starting node where search will begin
  154. * Type - Object type to match
  155. * return_node - Where the matched Node is returned
  156. *
  157. * RETURN: Status
  158. *
  159. * DESCRIPTION: Called when a name has not been found in the current namespace
  160. * level. Before adding it or giving up, ACPI scope rules require
  161. * searching enclosing scopes in cases identified by acpi_ns_local().
  162. *
  163. * "A name is located by finding the matching name in the current
  164. * name space, and then in the parent name space. If the parent
  165. * name space does not contain the name, the search continues
  166. * recursively until either the name is found or the name space
  167. * does not have a parent (the root of the name space). This
  168. * indicates that the name is not found" (From ACPI Specification,
  169. * section 5.3)
  170. *
  171. ******************************************************************************/
  172. static acpi_status
  173. acpi_ns_search_parent_tree(u32 target_name,
  174. struct acpi_namespace_node *node,
  175. acpi_object_type type,
  176. struct acpi_namespace_node **return_node)
  177. {
  178. acpi_status status;
  179. struct acpi_namespace_node *parent_node;
  180. ACPI_FUNCTION_TRACE(ns_search_parent_tree);
  181. parent_node = node->parent;
  182. /*
  183. * If there is no parent (i.e., we are at the root) or type is "local",
  184. * we won't be searching the parent tree.
  185. */
  186. if (!parent_node) {
  187. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "[%4.4s] has no parent\n",
  188. ACPI_CAST_PTR(char, &target_name)));
  189. return_ACPI_STATUS(AE_NOT_FOUND);
  190. }
  191. if (acpi_ns_local(type)) {
  192. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  193. "[%4.4s] type [%s] must be local to this scope (no parent search)\n",
  194. ACPI_CAST_PTR(char, &target_name),
  195. acpi_ut_get_type_name(type)));
  196. return_ACPI_STATUS(AE_NOT_FOUND);
  197. }
  198. /* Search the parent tree */
  199. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  200. "Searching parent [%4.4s] for [%4.4s]\n",
  201. acpi_ut_get_node_name(parent_node),
  202. ACPI_CAST_PTR(char, &target_name)));
  203. /* Search parents until target is found or we have backed up to the root */
  204. while (parent_node) {
  205. /*
  206. * Search parent scope. Use TYPE_ANY because we don't care about the
  207. * object type at this point, we only care about the existence of
  208. * the actual name we are searching for. Typechecking comes later.
  209. */
  210. status =
  211. acpi_ns_search_one_scope(target_name, parent_node,
  212. ACPI_TYPE_ANY, return_node);
  213. if (ACPI_SUCCESS(status)) {
  214. return_ACPI_STATUS(status);
  215. }
  216. /* Not found here, go up another level (until we reach the root) */
  217. parent_node = parent_node->parent;
  218. }
  219. /* Not found in parent tree */
  220. return_ACPI_STATUS(AE_NOT_FOUND);
  221. }
  222. /*******************************************************************************
  223. *
  224. * FUNCTION: acpi_ns_search_and_enter
  225. *
  226. * PARAMETERS: target_name - Ascii ACPI name to search for (4 chars)
  227. * walk_state - Current state of the walk
  228. * Node - Starting node where search will begin
  229. * interpreter_mode - Add names only in ACPI_MODE_LOAD_PASS_x.
  230. * Otherwise,search only.
  231. * Type - Object type to match
  232. * Flags - Flags describing the search restrictions
  233. * return_node - Where the Node is returned
  234. *
  235. * RETURN: Status
  236. *
  237. * DESCRIPTION: Search for a name segment in a single namespace level,
  238. * optionally adding it if it is not found. If the passed
  239. * Type is not Any and the type previously stored in the
  240. * entry was Any (i.e. unknown), update the stored type.
  241. *
  242. * In ACPI_IMODE_EXECUTE, search only.
  243. * In other modes, search and add if not found.
  244. *
  245. ******************************************************************************/
  246. acpi_status
  247. acpi_ns_search_and_enter(u32 target_name,
  248. struct acpi_walk_state *walk_state,
  249. struct acpi_namespace_node *node,
  250. acpi_interpreter_mode interpreter_mode,
  251. acpi_object_type type,
  252. u32 flags, struct acpi_namespace_node **return_node)
  253. {
  254. acpi_status status;
  255. struct acpi_namespace_node *new_node;
  256. ACPI_FUNCTION_TRACE(ns_search_and_enter);
  257. /* Parameter validation */
  258. if (!node || !target_name || !return_node) {
  259. ACPI_ERROR((AE_INFO,
  260. "Null parameter: Node %p Name 0x%X ReturnNode %p",
  261. node, target_name, return_node));
  262. return_ACPI_STATUS(AE_BAD_PARAMETER);
  263. }
  264. /*
  265. * Name must consist of valid ACPI characters. We will repair the name if
  266. * necessary because we don't want to abort because of this, but we want
  267. * all namespace names to be printable. A warning message is appropriate.
  268. *
  269. * This issue came up because there are in fact machines that exhibit
  270. * this problem, and we want to be able to enable ACPI support for them,
  271. * even though there are a few bad names.
  272. */
  273. if (!acpi_ut_valid_acpi_name(target_name)) {
  274. target_name =
  275. acpi_ut_repair_name(ACPI_CAST_PTR(char, &target_name));
  276. /* Report warning only if in strict mode or debug mode */
  277. if (!acpi_gbl_enable_interpreter_slack) {
  278. ACPI_WARNING((AE_INFO,
  279. "Found bad character(s) in name, repaired: [%4.4s]\n",
  280. ACPI_CAST_PTR(char, &target_name)));
  281. } else {
  282. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  283. "Found bad character(s) in name, repaired: [%4.4s]\n",
  284. ACPI_CAST_PTR(char, &target_name)));
  285. }
  286. }
  287. /* Try to find the name in the namespace level specified by the caller */
  288. *return_node = ACPI_ENTRY_NOT_FOUND;
  289. status = acpi_ns_search_one_scope(target_name, node, type, return_node);
  290. if (status != AE_NOT_FOUND) {
  291. /*
  292. * If we found it AND the request specifies that a find is an error,
  293. * return the error
  294. */
  295. if ((status == AE_OK) && (flags & ACPI_NS_ERROR_IF_FOUND)) {
  296. status = AE_ALREADY_EXISTS;
  297. }
  298. /* Either found it or there was an error: finished either way */
  299. return_ACPI_STATUS(status);
  300. }
  301. /*
  302. * The name was not found. If we are NOT performing the first pass
  303. * (name entry) of loading the namespace, search the parent tree (all the
  304. * way to the root if necessary.) We don't want to perform the parent
  305. * search when the namespace is actually being loaded. We want to perform
  306. * the search when namespace references are being resolved (load pass 2)
  307. * and during the execution phase.
  308. */
  309. if ((interpreter_mode != ACPI_IMODE_LOAD_PASS1) &&
  310. (flags & ACPI_NS_SEARCH_PARENT)) {
  311. /*
  312. * Not found at this level - search parent tree according to the
  313. * ACPI specification
  314. */
  315. status =
  316. acpi_ns_search_parent_tree(target_name, node, type,
  317. return_node);
  318. if (ACPI_SUCCESS(status)) {
  319. return_ACPI_STATUS(status);
  320. }
  321. }
  322. /* In execute mode, just search, never add names. Exit now */
  323. if (interpreter_mode == ACPI_IMODE_EXECUTE) {
  324. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  325. "%4.4s Not found in %p [Not adding]\n",
  326. ACPI_CAST_PTR(char, &target_name), node));
  327. return_ACPI_STATUS(AE_NOT_FOUND);
  328. }
  329. /* Create the new named object */
  330. new_node = acpi_ns_create_node(target_name);
  331. if (!new_node) {
  332. return_ACPI_STATUS(AE_NO_MEMORY);
  333. }
  334. #ifdef ACPI_ASL_COMPILER
  335. /* Node is an object defined by an External() statement */
  336. if (flags & ACPI_NS_EXTERNAL) {
  337. new_node->flags |= ANOBJ_IS_EXTERNAL;
  338. }
  339. #endif
  340. if (flags & ACPI_NS_TEMPORARY) {
  341. new_node->flags |= ANOBJ_TEMPORARY;
  342. }
  343. /* Install the new object into the parent's list of children */
  344. acpi_ns_install_node(walk_state, node, new_node, type);
  345. *return_node = new_node;
  346. return_ACPI_STATUS(AE_OK);
  347. }