Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /******************************************************************************* |
2 | * | |
3 | * Module Name: nssearch - Namespace search | |
4 | * | |
5 | ******************************************************************************/ | |
6 | ||
7 | /* | |
c8100dc4 | 8 | * Copyright (C) 2000 - 2016, Intel Corp. |
1da177e4 LT |
9 | * All rights reserved. |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | |
14 | * 1. Redistributions of source code must retain the above copyright | |
15 | * notice, this list of conditions, and the following disclaimer, | |
16 | * without modification. | |
17 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer | |
18 | * substantially similar to the "NO WARRANTY" disclaimer below | |
19 | * ("Disclaimer") and any redistribution must be conditioned upon | |
20 | * including a substantially similar Disclaimer requirement for further | |
21 | * binary redistribution. | |
22 | * 3. Neither the names of the above-listed copyright holders nor the names | |
23 | * of any contributors may be used to endorse or promote products derived | |
24 | * from this software without specific prior written permission. | |
25 | * | |
26 | * Alternatively, this software may be distributed under the terms of the | |
27 | * GNU General Public License ("GPL") version 2 as published by the Free | |
28 | * Software Foundation. | |
29 | * | |
30 | * NO WARRANTY | |
31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR | |
34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
35 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
40 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
41 | * POSSIBILITY OF SUCH DAMAGES. | |
42 | */ | |
43 | ||
1da177e4 | 44 | #include <acpi/acpi.h> |
e2f7a777 LB |
45 | #include "accommon.h" |
46 | #include "acnamesp.h" | |
1da177e4 | 47 | |
e4c1ebfc BM |
48 | #ifdef ACPI_ASL_COMPILER |
49 | #include "amlcode.h" | |
50 | #endif | |
51 | ||
1da177e4 | 52 | #define _COMPONENT ACPI_NAMESPACE |
4be44fcd | 53 | ACPI_MODULE_NAME("nssearch") |
1da177e4 | 54 | |
44f6c012 | 55 | /* Local prototypes */ |
44f6c012 | 56 | static acpi_status |
4be44fcd LB |
57 | acpi_ns_search_parent_tree(u32 target_name, |
58 | struct acpi_namespace_node *node, | |
59 | acpi_object_type type, | |
60 | struct acpi_namespace_node **return_node); | |
1da177e4 LT |
61 | |
62 | /******************************************************************************* | |
63 | * | |
4119532c | 64 | * FUNCTION: acpi_ns_search_one_scope |
1da177e4 | 65 | * |
44f6c012 | 66 | * PARAMETERS: target_name - Ascii ACPI name to search for |
4119532c | 67 | * parent_node - Starting node where search will begin |
ba494bee | 68 | * type - Object type to match |
44f6c012 | 69 | * return_node - Where the matched Named obj is returned |
1da177e4 LT |
70 | * |
71 | * RETURN: Status | |
72 | * | |
4119532c | 73 | * DESCRIPTION: Search a single level of the namespace. Performs a |
1da177e4 LT |
74 | * simple search of the specified level, and does not add |
75 | * entries or search parents. | |
76 | * | |
77 | * | |
78 | * Named object lists are built (and subsequently dumped) in the | |
79 | * order in which the names are encountered during the namespace load; | |
80 | * | |
81 | * All namespace searching is linear in this implementation, but | |
82 | * could be easily modified to support any improved search | |
4119532c | 83 | * algorithm. However, the linear search was chosen for simplicity |
1da177e4 LT |
84 | * and because the trees are small and the other interpreter |
85 | * execution overhead is relatively high. | |
86 | * | |
4119532c BM |
87 | * Note: CPU execution analysis has shown that the AML interpreter spends |
88 | * a very small percentage of its time searching the namespace. Therefore, | |
89 | * the linear search seems to be sufficient, as there would seem to be | |
90 | * little value in improving the search. | |
91 | * | |
1da177e4 LT |
92 | ******************************************************************************/ |
93 | ||
94 | acpi_status | |
4119532c BM |
95 | acpi_ns_search_one_scope(u32 target_name, |
96 | struct acpi_namespace_node *parent_node, | |
97 | acpi_object_type type, | |
98 | struct acpi_namespace_node **return_node) | |
1da177e4 | 99 | { |
4119532c | 100 | struct acpi_namespace_node *node; |
1da177e4 | 101 | |
4119532c | 102 | ACPI_FUNCTION_TRACE(ns_search_one_scope); |
1da177e4 LT |
103 | |
104 | #ifdef ACPI_DEBUG_OUTPUT | |
105 | if (ACPI_LV_NAMES & acpi_dbg_level) { | |
4be44fcd | 106 | char *scope_name; |
1da177e4 | 107 | |
0e166e4f | 108 | scope_name = acpi_ns_get_normalized_pathname(parent_node, TRUE); |
1da177e4 | 109 | if (scope_name) { |
4be44fcd LB |
110 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
111 | "Searching %s (%p) For [%4.4s] (%s)\n", | |
4119532c BM |
112 | scope_name, parent_node, |
113 | ACPI_CAST_PTR(char, &target_name), | |
4be44fcd | 114 | acpi_ut_get_type_name(type))); |
1da177e4 | 115 | |
8313524a | 116 | ACPI_FREE(scope_name); |
1da177e4 LT |
117 | } |
118 | } | |
119 | #endif | |
120 | ||
121 | /* | |
122 | * Search for name at this namespace level, which is to say that we | |
123 | * must search for the name among the children of this object | |
124 | */ | |
4119532c BM |
125 | node = parent_node->child; |
126 | while (node) { | |
52fc0b02 | 127 | |
1da177e4 LT |
128 | /* Check for match against the name */ |
129 | ||
4119532c | 130 | if (node->name.integer == target_name) { |
52fc0b02 | 131 | |
1da177e4 LT |
132 | /* Resolve a control method alias if any */ |
133 | ||
4119532c | 134 | if (acpi_ns_get_type(node) == |
4be44fcd | 135 | ACPI_TYPE_LOCAL_METHOD_ALIAS) { |
4119532c | 136 | node = |
4be44fcd | 137 | ACPI_CAST_PTR(struct acpi_namespace_node, |
4119532c | 138 | node->object); |
1da177e4 LT |
139 | } |
140 | ||
793c2388 BM |
141 | /* Found matching entry */ |
142 | ||
4be44fcd LB |
143 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
144 | "Name [%4.4s] (%s) %p found in scope [%4.4s] %p\n", | |
defba1d8 | 145 | ACPI_CAST_PTR(char, &target_name), |
4119532c BM |
146 | acpi_ut_get_type_name(node->type), |
147 | node, | |
148 | acpi_ut_get_node_name(parent_node), | |
149 | parent_node)); | |
1da177e4 | 150 | |
4119532c | 151 | *return_node = node; |
4be44fcd | 152 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
153 | } |
154 | ||
1da177e4 LT |
155 | /* Didn't match name, move on to the next peer object */ |
156 | ||
4119532c | 157 | node = node->peer; |
1da177e4 LT |
158 | } |
159 | ||
160 | /* Searched entire namespace level, not found */ | |
161 | ||
4be44fcd | 162 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
d4913dc6 BM |
163 | "Name [%4.4s] (%s) not found in search in scope [%4.4s] " |
164 | "%p first child %p\n", | |
defba1d8 BM |
165 | ACPI_CAST_PTR(char, &target_name), |
166 | acpi_ut_get_type_name(type), | |
4119532c BM |
167 | acpi_ut_get_node_name(parent_node), parent_node, |
168 | parent_node->child)); | |
1da177e4 | 169 | |
4be44fcd | 170 | return_ACPI_STATUS(AE_NOT_FOUND); |
1da177e4 LT |
171 | } |
172 | ||
1da177e4 LT |
173 | /******************************************************************************* |
174 | * | |
175 | * FUNCTION: acpi_ns_search_parent_tree | |
176 | * | |
44f6c012 | 177 | * PARAMETERS: target_name - Ascii ACPI name to search for |
ba494bee BM |
178 | * node - Starting node where search will begin |
179 | * type - Object type to match | |
44f6c012 | 180 | * return_node - Where the matched Node is returned |
1da177e4 LT |
181 | * |
182 | * RETURN: Status | |
183 | * | |
184 | * DESCRIPTION: Called when a name has not been found in the current namespace | |
4119532c | 185 | * level. Before adding it or giving up, ACPI scope rules require |
1da177e4 LT |
186 | * searching enclosing scopes in cases identified by acpi_ns_local(). |
187 | * | |
188 | * "A name is located by finding the matching name in the current | |
189 | * name space, and then in the parent name space. If the parent | |
190 | * name space does not contain the name, the search continues | |
191 | * recursively until either the name is found or the name space | |
4119532c | 192 | * does not have a parent (the root of the name space). This |
1da177e4 LT |
193 | * indicates that the name is not found" (From ACPI Specification, |
194 | * section 5.3) | |
195 | * | |
196 | ******************************************************************************/ | |
197 | ||
198 | static acpi_status | |
4be44fcd LB |
199 | acpi_ns_search_parent_tree(u32 target_name, |
200 | struct acpi_namespace_node *node, | |
201 | acpi_object_type type, | |
202 | struct acpi_namespace_node **return_node) | |
1da177e4 | 203 | { |
4be44fcd LB |
204 | acpi_status status; |
205 | struct acpi_namespace_node *parent_node; | |
1da177e4 | 206 | |
b229cf92 | 207 | ACPI_FUNCTION_TRACE(ns_search_parent_tree); |
1da177e4 | 208 | |
c45b5c09 | 209 | parent_node = node->parent; |
1da177e4 LT |
210 | |
211 | /* | |
212 | * If there is no parent (i.e., we are at the root) or type is "local", | |
213 | * we won't be searching the parent tree. | |
214 | */ | |
215 | if (!parent_node) { | |
4be44fcd | 216 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "[%4.4s] has no parent\n", |
defba1d8 | 217 | ACPI_CAST_PTR(char, &target_name))); |
4be44fcd | 218 | return_ACPI_STATUS(AE_NOT_FOUND); |
1da177e4 LT |
219 | } |
220 | ||
4be44fcd LB |
221 | if (acpi_ns_local(type)) { |
222 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, | |
223 | "[%4.4s] type [%s] must be local to this scope (no parent search)\n", | |
defba1d8 | 224 | ACPI_CAST_PTR(char, &target_name), |
4be44fcd LB |
225 | acpi_ut_get_type_name(type))); |
226 | return_ACPI_STATUS(AE_NOT_FOUND); | |
1da177e4 LT |
227 | } |
228 | ||
229 | /* Search the parent tree */ | |
230 | ||
4be44fcd LB |
231 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
232 | "Searching parent [%4.4s] for [%4.4s]\n", | |
233 | acpi_ut_get_node_name(parent_node), | |
defba1d8 | 234 | ACPI_CAST_PTR(char, &target_name))); |
1da177e4 | 235 | |
d4913dc6 BM |
236 | /* Search parents until target is found or we have backed up to the root */ |
237 | ||
1da177e4 LT |
238 | while (parent_node) { |
239 | /* | |
4119532c | 240 | * Search parent scope. Use TYPE_ANY because we don't care about the |
1da177e4 | 241 | * object type at this point, we only care about the existence of |
4119532c | 242 | * the actual name we are searching for. Typechecking comes later. |
1da177e4 | 243 | */ |
4119532c BM |
244 | status = |
245 | acpi_ns_search_one_scope(target_name, parent_node, | |
4be44fcd LB |
246 | ACPI_TYPE_ANY, return_node); |
247 | if (ACPI_SUCCESS(status)) { | |
248 | return_ACPI_STATUS(status); | |
1da177e4 LT |
249 | } |
250 | ||
793c2388 BM |
251 | /* Not found here, go up another level (until we reach the root) */ |
252 | ||
c45b5c09 | 253 | parent_node = parent_node->parent; |
1da177e4 LT |
254 | } |
255 | ||
256 | /* Not found in parent tree */ | |
257 | ||
4be44fcd | 258 | return_ACPI_STATUS(AE_NOT_FOUND); |
1da177e4 LT |
259 | } |
260 | ||
1da177e4 LT |
261 | /******************************************************************************* |
262 | * | |
263 | * FUNCTION: acpi_ns_search_and_enter | |
264 | * | |
265 | * PARAMETERS: target_name - Ascii ACPI name to search for (4 chars) | |
266 | * walk_state - Current state of the walk | |
ba494bee | 267 | * node - Starting node where search will begin |
1da177e4 LT |
268 | * interpreter_mode - Add names only in ACPI_MODE_LOAD_PASS_x. |
269 | * Otherwise,search only. | |
ba494bee BM |
270 | * type - Object type to match |
271 | * flags - Flags describing the search restrictions | |
44f6c012 | 272 | * return_node - Where the Node is returned |
1da177e4 LT |
273 | * |
274 | * RETURN: Status | |
275 | * | |
276 | * DESCRIPTION: Search for a name segment in a single namespace level, | |
4119532c | 277 | * optionally adding it if it is not found. If the passed |
1da177e4 LT |
278 | * Type is not Any and the type previously stored in the |
279 | * entry was Any (i.e. unknown), update the stored type. | |
280 | * | |
281 | * In ACPI_IMODE_EXECUTE, search only. | |
282 | * In other modes, search and add if not found. | |
283 | * | |
284 | ******************************************************************************/ | |
285 | ||
286 | acpi_status | |
4be44fcd LB |
287 | acpi_ns_search_and_enter(u32 target_name, |
288 | struct acpi_walk_state *walk_state, | |
289 | struct acpi_namespace_node *node, | |
290 | acpi_interpreter_mode interpreter_mode, | |
291 | acpi_object_type type, | |
292 | u32 flags, struct acpi_namespace_node **return_node) | |
1da177e4 | 293 | { |
4be44fcd LB |
294 | acpi_status status; |
295 | struct acpi_namespace_node *new_node; | |
1da177e4 | 296 | |
b229cf92 | 297 | ACPI_FUNCTION_TRACE(ns_search_and_enter); |
1da177e4 LT |
298 | |
299 | /* Parameter validation */ | |
300 | ||
301 | if (!node || !target_name || !return_node) { | |
b8e4d893 | 302 | ACPI_ERROR((AE_INFO, |
f6a22b0b | 303 | "Null parameter: Node %p Name 0x%X ReturnNode %p", |
b8e4d893 | 304 | node, target_name, return_node)); |
4be44fcd | 305 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
306 | } |
307 | ||
793c2388 BM |
308 | /* |
309 | * Name must consist of valid ACPI characters. We will repair the name if | |
310 | * necessary because we don't want to abort because of this, but we want | |
311 | * all namespace names to be printable. A warning message is appropriate. | |
312 | * | |
313 | * This issue came up because there are in fact machines that exhibit | |
314 | * this problem, and we want to be able to enable ACPI support for them, | |
315 | * even though there are a few bad names. | |
316 | */ | |
45dcd315 | 317 | acpi_ut_repair_name(ACPI_CAST_PTR(char, &target_name)); |
1da177e4 LT |
318 | |
319 | /* Try to find the name in the namespace level specified by the caller */ | |
320 | ||
321 | *return_node = ACPI_ENTRY_NOT_FOUND; | |
4119532c | 322 | status = acpi_ns_search_one_scope(target_name, node, type, return_node); |
1da177e4 LT |
323 | if (status != AE_NOT_FOUND) { |
324 | /* | |
325 | * If we found it AND the request specifies that a find is an error, | |
326 | * return the error | |
327 | */ | |
8f6f0361 LZ |
328 | if (status == AE_OK) { |
329 | ||
330 | /* The node was found in the namespace */ | |
331 | ||
332 | /* | |
333 | * If the namespace override feature is enabled for this node, | |
8ea98655 BM |
334 | * delete any existing attached sub-object and make the node |
335 | * look like a new node that is owned by the override table. | |
8f6f0361 LZ |
336 | */ |
337 | if (flags & ACPI_NS_OVERRIDE_IF_FOUND) { | |
8ea98655 BM |
338 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
339 | "Namespace override: %4.4s pass %u type %X Owner %X\n", | |
340 | ACPI_CAST_PTR(char, | |
341 | &target_name), | |
342 | interpreter_mode, | |
343 | (*return_node)->type, | |
344 | walk_state->owner_id)); | |
345 | ||
8f6f0361 | 346 | acpi_ns_delete_children(*return_node); |
8ea98655 BM |
347 | if (acpi_gbl_runtime_namespace_override) { |
348 | acpi_ut_remove_reference((*return_node)->object); | |
349 | (*return_node)->object = NULL; | |
350 | (*return_node)->owner_id = | |
351 | walk_state->owner_id; | |
352 | } else { | |
353 | acpi_ns_remove_node(*return_node); | |
354 | *return_node = ACPI_ENTRY_NOT_FOUND; | |
355 | } | |
8f6f0361 LZ |
356 | } |
357 | ||
358 | /* Return an error if we don't expect to find the object */ | |
359 | ||
360 | else if (flags & ACPI_NS_ERROR_IF_FOUND) { | |
361 | status = AE_ALREADY_EXISTS; | |
362 | } | |
1da177e4 | 363 | } |
56324c10 LZ |
364 | #ifdef ACPI_ASL_COMPILER |
365 | if (*return_node && (*return_node)->type == ACPI_TYPE_ANY) { | |
366 | (*return_node)->flags |= ANOBJ_IS_EXTERNAL; | |
367 | } | |
368 | #endif | |
1da177e4 | 369 | |
793c2388 BM |
370 | /* Either found it or there was an error: finished either way */ |
371 | ||
4be44fcd | 372 | return_ACPI_STATUS(status); |
1da177e4 LT |
373 | } |
374 | ||
375 | /* | |
4119532c | 376 | * The name was not found. If we are NOT performing the first pass |
1da177e4 LT |
377 | * (name entry) of loading the namespace, search the parent tree (all the |
378 | * way to the root if necessary.) We don't want to perform the parent | |
4119532c | 379 | * search when the namespace is actually being loaded. We want to perform |
1da177e4 LT |
380 | * the search when namespace references are being resolved (load pass 2) |
381 | * and during the execution phase. | |
382 | */ | |
383 | if ((interpreter_mode != ACPI_IMODE_LOAD_PASS1) && | |
4be44fcd | 384 | (flags & ACPI_NS_SEARCH_PARENT)) { |
1da177e4 LT |
385 | /* |
386 | * Not found at this level - search parent tree according to the | |
387 | * ACPI specification | |
388 | */ | |
4be44fcd LB |
389 | status = |
390 | acpi_ns_search_parent_tree(target_name, node, type, | |
391 | return_node); | |
392 | if (ACPI_SUCCESS(status)) { | |
393 | return_ACPI_STATUS(status); | |
1da177e4 LT |
394 | } |
395 | } | |
396 | ||
793c2388 BM |
397 | /* In execute mode, just search, never add names. Exit now */ |
398 | ||
1da177e4 | 399 | if (interpreter_mode == ACPI_IMODE_EXECUTE) { |
4be44fcd LB |
400 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
401 | "%4.4s Not found in %p [Not adding]\n", | |
defba1d8 | 402 | ACPI_CAST_PTR(char, &target_name), node)); |
1da177e4 | 403 | |
4be44fcd | 404 | return_ACPI_STATUS(AE_NOT_FOUND); |
1da177e4 LT |
405 | } |
406 | ||
407 | /* Create the new named object */ | |
408 | ||
4be44fcd | 409 | new_node = acpi_ns_create_node(target_name); |
1da177e4 | 410 | if (!new_node) { |
4be44fcd | 411 | return_ACPI_STATUS(AE_NO_MEMORY); |
1da177e4 | 412 | } |
958dd242 | 413 | #ifdef ACPI_ASL_COMPILER |
d4913dc6 BM |
414 | |
415 | /* Node is an object defined by an External() statement */ | |
416 | ||
99567bc5 LZ |
417 | if (flags & ACPI_NS_EXTERNAL || |
418 | (walk_state && walk_state->opcode == AML_SCOPE_OP)) { | |
958dd242 BM |
419 | new_node->flags |= ANOBJ_IS_EXTERNAL; |
420 | } | |
421 | #endif | |
1da177e4 | 422 | |
d1fdda83 BM |
423 | if (flags & ACPI_NS_TEMPORARY) { |
424 | new_node->flags |= ANOBJ_TEMPORARY; | |
425 | } | |
426 | ||
1da177e4 LT |
427 | /* Install the new object into the parent's list of children */ |
428 | ||
4be44fcd | 429 | acpi_ns_install_node(walk_state, node, new_node, type); |
1da177e4 | 430 | *return_node = new_node; |
4be44fcd | 431 | return_ACPI_STATUS(AE_OK); |
1da177e4 | 432 | } |