nsaccess.c 18 KB

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