nssearch.c 12 KB

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