nssearch.c 14 KB

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