nseval.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*******************************************************************************
  2. *
  3. * Module Name: nseval - Object evaluation, includes control method execution
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, 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 "acparser.h"
  45. #include "acinterp.h"
  46. #include "acnamesp.h"
  47. #define _COMPONENT ACPI_NAMESPACE
  48. ACPI_MODULE_NAME("nseval")
  49. /* Local prototypes */
  50. static void
  51. acpi_ns_exec_module_code(union acpi_operand_object *method_obj,
  52. struct acpi_evaluate_info *info);
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_ns_evaluate
  56. *
  57. * PARAMETERS: info - Evaluation info block, contains:
  58. * prefix_node - Prefix or Method/Object Node to execute
  59. * relative_path - Name of method to execute, If NULL, the
  60. * Node is the object to execute
  61. * parameters - List of parameters to pass to the method,
  62. * terminated by NULL. Params itself may be
  63. * NULL if no parameters are being passed.
  64. * return_object - Where to put method's return value (if
  65. * any). If NULL, no value is returned.
  66. * parameter_type - Type of Parameter list
  67. * return_object - Where to put method's return value (if
  68. * any). If NULL, no value is returned.
  69. * flags - ACPI_IGNORE_RETURN_VALUE to delete return
  70. *
  71. * RETURN: Status
  72. *
  73. * DESCRIPTION: Execute a control method or return the current value of an
  74. * ACPI namespace object.
  75. *
  76. * MUTEX: Locks interpreter
  77. *
  78. ******************************************************************************/
  79. acpi_status acpi_ns_evaluate(struct acpi_evaluate_info *info)
  80. {
  81. acpi_status status;
  82. ACPI_FUNCTION_TRACE(ns_evaluate);
  83. if (!info) {
  84. return_ACPI_STATUS(AE_BAD_PARAMETER);
  85. }
  86. if (!info->node) {
  87. /*
  88. * Get the actual namespace node for the target object if we
  89. * need to. Handles these cases:
  90. *
  91. * 1) Null node, valid pathname from root (absolute path)
  92. * 2) Node and valid pathname (path relative to Node)
  93. * 3) Node, Null pathname
  94. */
  95. status =
  96. acpi_ns_get_node(info->prefix_node, info->relative_pathname,
  97. ACPI_NS_NO_UPSEARCH, &info->node);
  98. if (ACPI_FAILURE(status)) {
  99. return_ACPI_STATUS(status);
  100. }
  101. }
  102. /*
  103. * For a method alias, we must grab the actual method node so that
  104. * proper scoping context will be established before execution.
  105. */
  106. if (acpi_ns_get_type(info->node) == ACPI_TYPE_LOCAL_METHOD_ALIAS) {
  107. info->node =
  108. ACPI_CAST_PTR(struct acpi_namespace_node,
  109. info->node->object);
  110. }
  111. /* Complete the info block initialization */
  112. info->return_object = NULL;
  113. info->node_flags = info->node->flags;
  114. info->obj_desc = acpi_ns_get_attached_object(info->node);
  115. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "%s [%p] Value %p\n",
  116. info->relative_pathname, info->node,
  117. acpi_ns_get_attached_object(info->node)));
  118. /* Get info if we have a predefined name (_HID, etc.) */
  119. info->predefined =
  120. acpi_ut_match_predefined_method(info->node->name.ascii);
  121. /* Get the full pathname to the object, for use in warning messages */
  122. info->full_pathname = acpi_ns_get_external_pathname(info->node);
  123. if (!info->full_pathname) {
  124. return_ACPI_STATUS(AE_NO_MEMORY);
  125. }
  126. /* Count the number of arguments being passed in */
  127. info->param_count = 0;
  128. if (info->parameters) {
  129. while (info->parameters[info->param_count]) {
  130. info->param_count++;
  131. }
  132. /* Warn on impossible argument count */
  133. if (info->param_count > ACPI_METHOD_NUM_ARGS) {
  134. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  135. ACPI_WARN_ALWAYS,
  136. "Excess arguments (%u) - using only %u",
  137. info->param_count,
  138. ACPI_METHOD_NUM_ARGS));
  139. info->param_count = ACPI_METHOD_NUM_ARGS;
  140. }
  141. }
  142. /*
  143. * For predefined names: Check that the declared argument count
  144. * matches the ACPI spec -- otherwise this is a BIOS error.
  145. */
  146. acpi_ns_check_acpi_compliance(info->full_pathname, info->node,
  147. info->predefined);
  148. /*
  149. * For all names: Check that the incoming argument count for
  150. * this method/object matches the actual ASL/AML definition.
  151. */
  152. acpi_ns_check_argument_count(info->full_pathname, info->node,
  153. info->param_count, info->predefined);
  154. /* For predefined names: Typecheck all incoming arguments */
  155. acpi_ns_check_argument_types(info);
  156. /*
  157. * Three major evaluation cases:
  158. *
  159. * 1) Object types that cannot be evaluated by definition
  160. * 2) The object is a control method -- execute it
  161. * 3) The object is not a method -- just return it's current value
  162. */
  163. switch (acpi_ns_get_type(info->node)) {
  164. case ACPI_TYPE_DEVICE:
  165. case ACPI_TYPE_EVENT:
  166. case ACPI_TYPE_MUTEX:
  167. case ACPI_TYPE_REGION:
  168. case ACPI_TYPE_THERMAL:
  169. case ACPI_TYPE_LOCAL_SCOPE:
  170. /*
  171. * 1) Disallow evaluation of certain object types. For these,
  172. * object evaluation is undefined and not supported.
  173. */
  174. ACPI_ERROR((AE_INFO,
  175. "%s: Evaluation of object type [%s] is not supported",
  176. info->full_pathname,
  177. acpi_ut_get_type_name(info->node->type)));
  178. status = AE_TYPE;
  179. goto cleanup;
  180. case ACPI_TYPE_METHOD:
  181. /*
  182. * 2) Object is a control method - execute it
  183. */
  184. /* Verify that there is a method object associated with this node */
  185. if (!info->obj_desc) {
  186. ACPI_ERROR((AE_INFO,
  187. "%s: Method has no attached sub-object",
  188. info->full_pathname));
  189. status = AE_NULL_OBJECT;
  190. goto cleanup;
  191. }
  192. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  193. "**** Execute method [%s] at AML address %p length %X\n",
  194. info->full_pathname,
  195. info->obj_desc->method.aml_start + 1,
  196. info->obj_desc->method.aml_length - 1));
  197. /*
  198. * Any namespace deletion must acquire both the namespace and
  199. * interpreter locks to ensure that no thread is using the portion of
  200. * the namespace that is being deleted.
  201. *
  202. * Execute the method via the interpreter. The interpreter is locked
  203. * here before calling into the AML parser
  204. */
  205. acpi_ex_enter_interpreter();
  206. status = acpi_ps_execute_method(info);
  207. acpi_ex_exit_interpreter();
  208. break;
  209. default:
  210. /*
  211. * 3) All other non-method objects -- get the current object value
  212. */
  213. /*
  214. * Some objects require additional resolution steps (e.g., the Node
  215. * may be a field that must be read, etc.) -- we can't just grab
  216. * the object out of the node.
  217. *
  218. * Use resolve_node_to_value() to get the associated value.
  219. *
  220. * NOTE: we can get away with passing in NULL for a walk state because
  221. * the Node is guaranteed to not be a reference to either a method
  222. * local or a method argument (because this interface is never called
  223. * from a running method.)
  224. *
  225. * Even though we do not directly invoke the interpreter for object
  226. * resolution, we must lock it because we could access an op_region.
  227. * The op_region access code assumes that the interpreter is locked.
  228. */
  229. acpi_ex_enter_interpreter();
  230. /* TBD: resolve_node_to_value has a strange interface, fix */
  231. info->return_object =
  232. ACPI_CAST_PTR(union acpi_operand_object, info->node);
  233. status =
  234. acpi_ex_resolve_node_to_value(ACPI_CAST_INDIRECT_PTR
  235. (struct acpi_namespace_node,
  236. &info->return_object), NULL);
  237. acpi_ex_exit_interpreter();
  238. if (ACPI_FAILURE(status)) {
  239. goto cleanup;
  240. }
  241. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Returned object %p [%s]\n",
  242. info->return_object,
  243. acpi_ut_get_object_type_name(info->
  244. return_object)));
  245. status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
  246. break;
  247. }
  248. /*
  249. * For predefined names, check the return value against the ACPI
  250. * specification. Some incorrect return value types are repaired.
  251. */
  252. (void)acpi_ns_check_return_value(info->node, info, info->param_count,
  253. status, &info->return_object);
  254. /* Check if there is a return value that must be dealt with */
  255. if (status == AE_CTRL_RETURN_VALUE) {
  256. /* If caller does not want the return value, delete it */
  257. if (info->flags & ACPI_IGNORE_RETURN_VALUE) {
  258. acpi_ut_remove_reference(info->return_object);
  259. info->return_object = NULL;
  260. }
  261. /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
  262. status = AE_OK;
  263. }
  264. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  265. "*** Completed evaluation of object %s ***\n",
  266. info->relative_pathname));
  267. cleanup:
  268. /*
  269. * Namespace was unlocked by the handling acpi_ns* function, so we
  270. * just free the pathname and return
  271. */
  272. ACPI_FREE(info->full_pathname);
  273. info->full_pathname = NULL;
  274. return_ACPI_STATUS(status);
  275. }
  276. /*******************************************************************************
  277. *
  278. * FUNCTION: acpi_ns_exec_module_code_list
  279. *
  280. * PARAMETERS: None
  281. *
  282. * RETURN: None. Exceptions during method execution are ignored, since
  283. * we cannot abort a table load.
  284. *
  285. * DESCRIPTION: Execute all elements of the global module-level code list.
  286. * Each element is executed as a single control method.
  287. *
  288. ******************************************************************************/
  289. void acpi_ns_exec_module_code_list(void)
  290. {
  291. union acpi_operand_object *prev;
  292. union acpi_operand_object *next;
  293. struct acpi_evaluate_info *info;
  294. u32 method_count = 0;
  295. ACPI_FUNCTION_TRACE(ns_exec_module_code_list);
  296. /* Exit now if the list is empty */
  297. next = acpi_gbl_module_code_list;
  298. if (!next) {
  299. return_VOID;
  300. }
  301. /* Allocate the evaluation information block */
  302. info = ACPI_ALLOCATE(sizeof(struct acpi_evaluate_info));
  303. if (!info) {
  304. return_VOID;
  305. }
  306. /* Walk the list, executing each "method" */
  307. while (next) {
  308. prev = next;
  309. next = next->method.mutex;
  310. /* Clear the link field and execute the method */
  311. prev->method.mutex = NULL;
  312. acpi_ns_exec_module_code(prev, info);
  313. method_count++;
  314. /* Delete the (temporary) method object */
  315. acpi_ut_remove_reference(prev);
  316. }
  317. ACPI_INFO((AE_INFO,
  318. "Executed %u blocks of module-level executable AML code",
  319. method_count));
  320. ACPI_FREE(info);
  321. acpi_gbl_module_code_list = NULL;
  322. return_VOID;
  323. }
  324. /*******************************************************************************
  325. *
  326. * FUNCTION: acpi_ns_exec_module_code
  327. *
  328. * PARAMETERS: method_obj - Object container for the module-level code
  329. * info - Info block for method evaluation
  330. *
  331. * RETURN: None. Exceptions during method execution are ignored, since
  332. * we cannot abort a table load.
  333. *
  334. * DESCRIPTION: Execute a control method containing a block of module-level
  335. * executable AML code. The control method is temporarily
  336. * installed to the root node, then evaluated.
  337. *
  338. ******************************************************************************/
  339. static void
  340. acpi_ns_exec_module_code(union acpi_operand_object *method_obj,
  341. struct acpi_evaluate_info *info)
  342. {
  343. union acpi_operand_object *parent_obj;
  344. struct acpi_namespace_node *parent_node;
  345. acpi_object_type type;
  346. acpi_status status;
  347. ACPI_FUNCTION_TRACE(ns_exec_module_code);
  348. /*
  349. * Get the parent node. We cheat by using the next_object field
  350. * of the method object descriptor.
  351. */
  352. parent_node = ACPI_CAST_PTR(struct acpi_namespace_node,
  353. method_obj->method.next_object);
  354. type = acpi_ns_get_type(parent_node);
  355. /*
  356. * Get the region handler and save it in the method object. We may need
  357. * this if an operation region declaration causes a _REG method to be run.
  358. *
  359. * We can't do this in acpi_ps_link_module_code because
  360. * acpi_gbl_root_node->Object is NULL at PASS1.
  361. */
  362. if ((type == ACPI_TYPE_DEVICE) && parent_node->object) {
  363. method_obj->method.dispatch.handler =
  364. parent_node->object->device.handler;
  365. }
  366. /* Must clear next_object (acpi_ns_attach_object needs the field) */
  367. method_obj->method.next_object = NULL;
  368. /* Initialize the evaluation information block */
  369. ACPI_MEMSET(info, 0, sizeof(struct acpi_evaluate_info));
  370. info->prefix_node = parent_node;
  371. /*
  372. * Get the currently attached parent object. Add a reference, because the
  373. * ref count will be decreased when the method object is installed to
  374. * the parent node.
  375. */
  376. parent_obj = acpi_ns_get_attached_object(parent_node);
  377. if (parent_obj) {
  378. acpi_ut_add_reference(parent_obj);
  379. }
  380. /* Install the method (module-level code) in the parent node */
  381. status = acpi_ns_attach_object(parent_node, method_obj,
  382. ACPI_TYPE_METHOD);
  383. if (ACPI_FAILURE(status)) {
  384. goto exit;
  385. }
  386. /* Execute the parent node as a control method */
  387. status = acpi_ns_evaluate(info);
  388. ACPI_DEBUG_PRINT((ACPI_DB_INIT, "Executed module-level code at %p\n",
  389. method_obj->method.aml_start));
  390. /* Delete a possible implicit return value (in slack mode) */
  391. if (info->return_object) {
  392. acpi_ut_remove_reference(info->return_object);
  393. }
  394. /* Detach the temporary method object */
  395. acpi_ns_detach_object(parent_node);
  396. /* Restore the original parent object */
  397. if (parent_obj) {
  398. status = acpi_ns_attach_object(parent_node, parent_obj, type);
  399. } else {
  400. parent_node->type = (u8)type;
  401. }
  402. exit:
  403. if (parent_obj) {
  404. acpi_ut_remove_reference(parent_obj);
  405. }
  406. return_VOID;
  407. }