nsaccess.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsaccess - Top-level functions for accessing ACPI namespace
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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 <acpi/amlcode.h>
  44. #include <acpi/acnamesp.h>
  45. #include <acpi/acdispat.h>
  46. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME("nsaccess")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ns_root_initialize
  51. *
  52. * PARAMETERS: None
  53. *
  54. * RETURN: Status
  55. *
  56. * DESCRIPTION: Allocate and initialize the default root named objects
  57. *
  58. * MUTEX: Locks namespace for entire execution
  59. *
  60. ******************************************************************************/
  61. acpi_status acpi_ns_root_initialize(void)
  62. {
  63. acpi_status status;
  64. const struct acpi_predefined_names *init_val = NULL;
  65. struct acpi_namespace_node *new_node;
  66. union acpi_operand_object *obj_desc;
  67. acpi_string val = NULL;
  68. ACPI_FUNCTION_TRACE("ns_root_initialize");
  69. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  70. if (ACPI_FAILURE(status)) {
  71. return_ACPI_STATUS(status);
  72. }
  73. /*
  74. * The global root ptr is initially NULL, so a non-NULL value indicates
  75. * that acpi_ns_root_initialize() has already been called; just return.
  76. */
  77. if (acpi_gbl_root_node) {
  78. status = AE_OK;
  79. goto unlock_and_exit;
  80. }
  81. /*
  82. * Tell the rest of the subsystem that the root is initialized
  83. * (This is OK because the namespace is locked)
  84. */
  85. acpi_gbl_root_node = &acpi_gbl_root_node_struct;
  86. /* Enter the pre-defined names in the name table */
  87. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  88. "Entering predefined entries into namespace\n"));
  89. for (init_val = acpi_gbl_pre_defined_names; init_val->name; init_val++) {
  90. /* _OSI is optional for now, will be permanent later */
  91. if (!ACPI_STRCMP(init_val->name, "_OSI")
  92. && !acpi_gbl_create_osi_method) {
  93. continue;
  94. }
  95. status = acpi_ns_lookup(NULL, init_val->name, init_val->type,
  96. ACPI_IMODE_LOAD_PASS2,
  97. ACPI_NS_NO_UPSEARCH, NULL, &new_node);
  98. if (ACPI_FAILURE(status) || (!new_node)) { /* Must be on same line for code converter */
  99. ACPI_REPORT_ERROR(("Could not create predefined name %s, %s\n", init_val->name, acpi_format_exception(status)));
  100. }
  101. /*
  102. * Name entered successfully.
  103. * If entry in pre_defined_names[] specifies an
  104. * initial value, create the initial value.
  105. */
  106. if (init_val->val) {
  107. status = acpi_os_predefined_override(init_val, &val);
  108. if (ACPI_FAILURE(status)) {
  109. ACPI_REPORT_ERROR(("Could not override predefined %s\n", init_val->name));
  110. }
  111. if (!val) {
  112. val = init_val->val;
  113. }
  114. /*
  115. * Entry requests an initial value, allocate a
  116. * descriptor for it.
  117. */
  118. obj_desc =
  119. acpi_ut_create_internal_object(init_val->type);
  120. if (!obj_desc) {
  121. status = AE_NO_MEMORY;
  122. goto unlock_and_exit;
  123. }
  124. /*
  125. * Convert value string from table entry to
  126. * internal representation. Only types actually
  127. * used for initial values are implemented here.
  128. */
  129. switch (init_val->type) {
  130. case ACPI_TYPE_METHOD:
  131. obj_desc->method.param_count =
  132. (u8) ACPI_TO_INTEGER(val);
  133. obj_desc->common.flags |= AOPOBJ_DATA_VALID;
  134. #if defined (ACPI_ASL_COMPILER)
  135. /* save the parameter count for the i_aSL compiler */
  136. new_node->value = obj_desc->method.param_count;
  137. #else
  138. /* Mark this as a very SPECIAL method */
  139. obj_desc->method.method_flags =
  140. AML_METHOD_INTERNAL_ONLY;
  141. #ifndef ACPI_DUMP_APP
  142. obj_desc->method.implementation =
  143. acpi_ut_osi_implementation;
  144. #endif
  145. #endif
  146. break;
  147. case ACPI_TYPE_INTEGER:
  148. obj_desc->integer.value = ACPI_TO_INTEGER(val);
  149. break;
  150. case ACPI_TYPE_STRING:
  151. /*
  152. * Build an object around the static string
  153. */
  154. obj_desc->string.length =
  155. (u32) ACPI_STRLEN(val);
  156. obj_desc->string.pointer = val;
  157. obj_desc->common.flags |= AOPOBJ_STATIC_POINTER;
  158. break;
  159. case ACPI_TYPE_MUTEX:
  160. obj_desc->mutex.node = new_node;
  161. obj_desc->mutex.sync_level =
  162. (u8) (ACPI_TO_INTEGER(val) - 1);
  163. if (ACPI_STRCMP(init_val->name, "_GL_") == 0) {
  164. /*
  165. * Create a counting semaphore for the
  166. * global lock
  167. */
  168. status =
  169. acpi_os_create_semaphore
  170. (ACPI_NO_UNIT_LIMIT, 1,
  171. &obj_desc->mutex.semaphore);
  172. if (ACPI_FAILURE(status)) {
  173. acpi_ut_remove_reference
  174. (obj_desc);
  175. goto unlock_and_exit;
  176. }
  177. /*
  178. * We just created the mutex for the
  179. * global lock, save it
  180. */
  181. acpi_gbl_global_lock_semaphore =
  182. obj_desc->mutex.semaphore;
  183. } else {
  184. /* Create a mutex */
  185. status = acpi_os_create_semaphore(1, 1,
  186. &obj_desc->
  187. mutex.
  188. semaphore);
  189. if (ACPI_FAILURE(status)) {
  190. acpi_ut_remove_reference
  191. (obj_desc);
  192. goto unlock_and_exit;
  193. }
  194. }
  195. break;
  196. default:
  197. ACPI_REPORT_ERROR(("Unsupported initial type value %X\n", init_val->type));
  198. acpi_ut_remove_reference(obj_desc);
  199. obj_desc = NULL;
  200. continue;
  201. }
  202. /* Store pointer to value descriptor in the Node */
  203. status = acpi_ns_attach_object(new_node, obj_desc,
  204. ACPI_GET_OBJECT_TYPE
  205. (obj_desc));
  206. /* Remove local reference to the object */
  207. acpi_ut_remove_reference(obj_desc);
  208. }
  209. }
  210. unlock_and_exit:
  211. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  212. /* Save a handle to "_GPE", it is always present */
  213. if (ACPI_SUCCESS(status)) {
  214. status =
  215. acpi_ns_get_node_by_path("\\_GPE", NULL,
  216. ACPI_NS_NO_UPSEARCH,
  217. &acpi_gbl_fadt_gpe_device);
  218. }
  219. return_ACPI_STATUS(status);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: acpi_ns_lookup
  224. *
  225. * PARAMETERS: scope_info - Current scope info block
  226. * Pathname - Search pathname, in internal format
  227. * (as represented in the AML stream)
  228. * Type - Type associated with name
  229. * interpreter_mode - IMODE_LOAD_PASS2 => add name if not found
  230. * Flags - Flags describing the search restrictions
  231. * walk_state - Current state of the walk
  232. * return_node - Where the Node is placed (if found
  233. * or created successfully)
  234. *
  235. * RETURN: Status
  236. *
  237. * DESCRIPTION: Find or enter the passed name in the name space.
  238. * Log an error if name not found in Exec mode.
  239. *
  240. * MUTEX: Assumes namespace is locked.
  241. *
  242. ******************************************************************************/
  243. acpi_status
  244. acpi_ns_lookup(union acpi_generic_state *scope_info,
  245. char *pathname,
  246. acpi_object_type type,
  247. acpi_interpreter_mode interpreter_mode,
  248. u32 flags,
  249. struct acpi_walk_state *walk_state,
  250. struct acpi_namespace_node **return_node)
  251. {
  252. acpi_status status;
  253. char *path = pathname;
  254. struct acpi_namespace_node *prefix_node;
  255. struct acpi_namespace_node *current_node = NULL;
  256. struct acpi_namespace_node *this_node = NULL;
  257. u32 num_segments;
  258. u32 num_carats;
  259. acpi_name simple_name;
  260. acpi_object_type type_to_check_for;
  261. acpi_object_type this_search_type;
  262. u32 search_parent_flag = ACPI_NS_SEARCH_PARENT;
  263. u32 local_flags = flags & ~(ACPI_NS_ERROR_IF_FOUND |
  264. ACPI_NS_SEARCH_PARENT);
  265. ACPI_FUNCTION_TRACE("ns_lookup");
  266. if (!return_node) {
  267. return_ACPI_STATUS(AE_BAD_PARAMETER);
  268. }
  269. acpi_gbl_ns_lookup_count++;
  270. *return_node = ACPI_ENTRY_NOT_FOUND;
  271. if (!acpi_gbl_root_node) {
  272. return_ACPI_STATUS(AE_NO_NAMESPACE);
  273. }
  274. /*
  275. * Get the prefix scope.
  276. * A null scope means use the root scope
  277. */
  278. if ((!scope_info) || (!scope_info->scope.node)) {
  279. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  280. "Null scope prefix, using root node (%p)\n",
  281. acpi_gbl_root_node));
  282. prefix_node = acpi_gbl_root_node;
  283. } else {
  284. prefix_node = scope_info->scope.node;
  285. if (ACPI_GET_DESCRIPTOR_TYPE(prefix_node) !=
  286. ACPI_DESC_TYPE_NAMED) {
  287. ACPI_REPORT_ERROR(("%p is not a namespace node [%s]\n",
  288. prefix_node,
  289. acpi_ut_get_descriptor_name
  290. (prefix_node)));
  291. return_ACPI_STATUS(AE_AML_INTERNAL);
  292. }
  293. /*
  294. * This node might not be a actual "scope" node (such as a
  295. * Device/Method, etc.) It could be a Package or other object node.
  296. * Backup up the tree to find the containing scope node.
  297. */
  298. while (!acpi_ns_opens_scope(prefix_node->type) &&
  299. prefix_node->type != ACPI_TYPE_ANY) {
  300. prefix_node = acpi_ns_get_parent_node(prefix_node);
  301. }
  302. }
  303. /* Save type TBD: may be no longer necessary */
  304. type_to_check_for = type;
  305. /*
  306. * Begin examination of the actual pathname
  307. */
  308. if (!pathname) {
  309. /* A Null name_path is allowed and refers to the root */
  310. num_segments = 0;
  311. this_node = acpi_gbl_root_node;
  312. path = "";
  313. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  314. "Null Pathname (Zero segments), Flags=%X\n",
  315. flags));
  316. } else {
  317. /*
  318. * Name pointer is valid (and must be in internal name format)
  319. *
  320. * Check for scope prefixes:
  321. *
  322. * As represented in the AML stream, a namepath consists of an
  323. * optional scope prefix followed by a name segment part.
  324. *
  325. * If present, the scope prefix is either a Root Prefix (in
  326. * which case the name is fully qualified), or one or more
  327. * Parent Prefixes (in which case the name's scope is relative
  328. * to the current scope).
  329. */
  330. if (*path == (u8) AML_ROOT_PREFIX) {
  331. /* Pathname is fully qualified, start from the root */
  332. this_node = acpi_gbl_root_node;
  333. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  334. /* Point to name segment part */
  335. path++;
  336. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  337. "Path is absolute from root [%p]\n",
  338. this_node));
  339. } else {
  340. /* Pathname is relative to current scope, start there */
  341. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  342. "Searching relative to prefix scope [%4.4s] (%p)\n",
  343. acpi_ut_get_node_name(prefix_node),
  344. prefix_node));
  345. /*
  346. * Handle multiple Parent Prefixes (carat) by just getting
  347. * the parent node for each prefix instance.
  348. */
  349. this_node = prefix_node;
  350. num_carats = 0;
  351. while (*path == (u8) AML_PARENT_PREFIX) {
  352. /* Name is fully qualified, no search rules apply */
  353. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  354. /*
  355. * Point past this prefix to the name segment
  356. * part or the next Parent Prefix
  357. */
  358. path++;
  359. /* Backup to the parent node */
  360. num_carats++;
  361. this_node = acpi_ns_get_parent_node(this_node);
  362. if (!this_node) {
  363. /* Current scope has no parent scope */
  364. ACPI_REPORT_ERROR(("ACPI path has too many parent prefixes (^) - reached beyond root node\n"));
  365. return_ACPI_STATUS(AE_NOT_FOUND);
  366. }
  367. }
  368. if (search_parent_flag == ACPI_NS_NO_UPSEARCH) {
  369. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  370. "Search scope is [%4.4s], path has %d carat(s)\n",
  371. acpi_ut_get_node_name
  372. (this_node), num_carats));
  373. }
  374. }
  375. /*
  376. * Determine the number of ACPI name segments in this pathname.
  377. *
  378. * The segment part consists of either:
  379. * - A Null name segment (0)
  380. * - A dual_name_prefix followed by two 4-byte name segments
  381. * - A multi_name_prefix followed by a byte indicating the
  382. * number of segments and the segments themselves.
  383. * - A single 4-byte name segment
  384. *
  385. * Examine the name prefix opcode, if any, to determine the number of
  386. * segments.
  387. */
  388. switch (*path) {
  389. case 0:
  390. /*
  391. * Null name after a root or parent prefixes. We already
  392. * have the correct target node and there are no name segments.
  393. */
  394. num_segments = 0;
  395. type = this_node->type;
  396. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  397. "Prefix-only Pathname (Zero name segments), Flags=%X\n",
  398. flags));
  399. break;
  400. case AML_DUAL_NAME_PREFIX:
  401. /* More than one name_seg, search rules do not apply */
  402. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  403. /* Two segments, point to first name segment */
  404. num_segments = 2;
  405. path++;
  406. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  407. "Dual Pathname (2 segments, Flags=%X)\n",
  408. flags));
  409. break;
  410. case AML_MULTI_NAME_PREFIX_OP:
  411. /* More than one name_seg, search rules do not apply */
  412. search_parent_flag = ACPI_NS_NO_UPSEARCH;
  413. /* Extract segment count, point to first name segment */
  414. path++;
  415. num_segments = (u32) (u8) * path;
  416. path++;
  417. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  418. "Multi Pathname (%d Segments, Flags=%X)\n",
  419. num_segments, flags));
  420. break;
  421. default:
  422. /*
  423. * Not a Null name, no Dual or Multi prefix, hence there is
  424. * only one name segment and Pathname is already pointing to it.
  425. */
  426. num_segments = 1;
  427. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  428. "Simple Pathname (1 segment, Flags=%X)\n",
  429. flags));
  430. break;
  431. }
  432. ACPI_DEBUG_EXEC(acpi_ns_print_pathname(num_segments, path));
  433. }
  434. /*
  435. * Search namespace for each segment of the name. Loop through and
  436. * verify (or add to the namespace) each name segment.
  437. *
  438. * The object type is significant only at the last name
  439. * segment. (We don't care about the types along the path, only
  440. * the type of the final target object.)
  441. */
  442. this_search_type = ACPI_TYPE_ANY;
  443. current_node = this_node;
  444. while (num_segments && current_node) {
  445. num_segments--;
  446. if (!num_segments) {
  447. /*
  448. * This is the last segment, enable typechecking
  449. */
  450. this_search_type = type;
  451. /*
  452. * Only allow automatic parent search (search rules) if the caller
  453. * requested it AND we have a single, non-fully-qualified name_seg
  454. */
  455. if ((search_parent_flag != ACPI_NS_NO_UPSEARCH) &&
  456. (flags & ACPI_NS_SEARCH_PARENT)) {
  457. local_flags |= ACPI_NS_SEARCH_PARENT;
  458. }
  459. /* Set error flag according to caller */
  460. if (flags & ACPI_NS_ERROR_IF_FOUND) {
  461. local_flags |= ACPI_NS_ERROR_IF_FOUND;
  462. }
  463. }
  464. /* Extract one ACPI name from the front of the pathname */
  465. ACPI_MOVE_32_TO_32(&simple_name, path);
  466. /* Try to find the single (4 character) ACPI name */
  467. status =
  468. acpi_ns_search_and_enter(simple_name, walk_state,
  469. current_node, interpreter_mode,
  470. this_search_type, local_flags,
  471. &this_node);
  472. if (ACPI_FAILURE(status)) {
  473. if (status == AE_NOT_FOUND) {
  474. /* Name not found in ACPI namespace */
  475. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  476. "Name [%4.4s] not found in scope [%4.4s] %p\n",
  477. (char *)&simple_name,
  478. (char *)&current_node->name,
  479. current_node));
  480. }
  481. *return_node = this_node;
  482. return_ACPI_STATUS(status);
  483. }
  484. /*
  485. * Sanity typecheck of the target object:
  486. *
  487. * If 1) This is the last segment (num_segments == 0)
  488. * 2) And we are looking for a specific type
  489. * (Not checking for TYPE_ANY)
  490. * 3) Which is not an alias
  491. * 4) Which is not a local type (TYPE_SCOPE)
  492. * 5) And the type of target object is known (not TYPE_ANY)
  493. * 6) And target object does not match what we are looking for
  494. *
  495. * Then we have a type mismatch. Just warn and ignore it.
  496. */
  497. if ((num_segments == 0) &&
  498. (type_to_check_for != ACPI_TYPE_ANY) &&
  499. (type_to_check_for != ACPI_TYPE_LOCAL_ALIAS) &&
  500. (type_to_check_for != ACPI_TYPE_LOCAL_METHOD_ALIAS) &&
  501. (type_to_check_for != ACPI_TYPE_LOCAL_SCOPE) &&
  502. (this_node->type != ACPI_TYPE_ANY) &&
  503. (this_node->type != type_to_check_for)) {
  504. /* Complain about a type mismatch */
  505. ACPI_REPORT_WARNING(("ns_lookup: Type mismatch on %4.4s (%s), searching for (%s)\n", ACPI_CAST_PTR(char, &simple_name), acpi_ut_get_type_name(this_node->type), acpi_ut_get_type_name(type_to_check_for)));
  506. }
  507. /*
  508. * If this is the last name segment and we are not looking for a
  509. * specific type, but the type of found object is known, use that type
  510. * to see if it opens a scope.
  511. */
  512. if ((num_segments == 0) && (type == ACPI_TYPE_ANY)) {
  513. type = this_node->type;
  514. }
  515. /* Point to next name segment and make this node current */
  516. path += ACPI_NAME_SIZE;
  517. current_node = this_node;
  518. }
  519. /*
  520. * Always check if we need to open a new scope
  521. */
  522. if (!(flags & ACPI_NS_DONT_OPEN_SCOPE) && (walk_state)) {
  523. /*
  524. * If entry is a type which opens a scope, push the new scope on the
  525. * scope stack.
  526. */
  527. if (acpi_ns_opens_scope(type)) {
  528. status =
  529. acpi_ds_scope_stack_push(this_node, type,
  530. walk_state);
  531. if (ACPI_FAILURE(status)) {
  532. return_ACPI_STATUS(status);
  533. }
  534. }
  535. }
  536. *return_node = this_node;
  537. return_ACPI_STATUS(AE_OK);
  538. }