nseval.c 15 KB

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