dsmethod.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /******************************************************************************
  2. *
  3. * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, 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 "acdispat.h"
  45. #include "acinterp.h"
  46. #include "acnamesp.h"
  47. #ifdef ACPI_DISASSEMBLER
  48. #include "acdisasm.h"
  49. #endif
  50. #include "acparser.h"
  51. #include "amlcode.h"
  52. #define _COMPONENT ACPI_DISPATCHER
  53. ACPI_MODULE_NAME("dsmethod")
  54. /* Local prototypes */
  55. static acpi_status
  56. acpi_ds_detect_named_opcodes(struct acpi_walk_state *walk_state,
  57. union acpi_parse_object **out_op);
  58. static acpi_status
  59. acpi_ds_create_method_mutex(union acpi_operand_object *method_desc);
  60. /*******************************************************************************
  61. *
  62. * FUNCTION: acpi_ds_auto_serialize_method
  63. *
  64. * PARAMETERS: node - Namespace Node of the method
  65. * obj_desc - Method object attached to node
  66. *
  67. * RETURN: Status
  68. *
  69. * DESCRIPTION: Parse a control method AML to scan for control methods that
  70. * need serialization due to the creation of named objects.
  71. *
  72. * NOTE: It is a bit of overkill to mark all such methods serialized, since
  73. * there is only a problem if the method actually blocks during execution.
  74. * A blocking operation is, for example, a Sleep() operation, or any access
  75. * to an operation region. However, it is probably not possible to easily
  76. * detect whether a method will block or not, so we simply mark all suspicious
  77. * methods as serialized.
  78. *
  79. * NOTE2: This code is essentially a generic routine for parsing a single
  80. * control method.
  81. *
  82. ******************************************************************************/
  83. acpi_status
  84. acpi_ds_auto_serialize_method(struct acpi_namespace_node *node,
  85. union acpi_operand_object *obj_desc)
  86. {
  87. acpi_status status;
  88. union acpi_parse_object *op = NULL;
  89. struct acpi_walk_state *walk_state;
  90. ACPI_FUNCTION_TRACE_PTR(ds_auto_serialize_method, node);
  91. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  92. "Method auto-serialization parse [%4.4s] %p\n",
  93. acpi_ut_get_node_name(node), node));
  94. /* Create/Init a root op for the method parse tree */
  95. op = acpi_ps_alloc_op(AML_METHOD_OP);
  96. if (!op) {
  97. return_ACPI_STATUS(AE_NO_MEMORY);
  98. }
  99. acpi_ps_set_name(op, node->name.integer);
  100. op->common.node = node;
  101. /* Create and initialize a new walk state */
  102. walk_state =
  103. acpi_ds_create_walk_state(node->owner_id, NULL, NULL, NULL);
  104. if (!walk_state) {
  105. return_ACPI_STATUS(AE_NO_MEMORY);
  106. }
  107. status =
  108. acpi_ds_init_aml_walk(walk_state, op, node,
  109. obj_desc->method.aml_start,
  110. obj_desc->method.aml_length, NULL, 0);
  111. if (ACPI_FAILURE(status)) {
  112. acpi_ds_delete_walk_state(walk_state);
  113. return_ACPI_STATUS(status);
  114. }
  115. walk_state->descending_callback = acpi_ds_detect_named_opcodes;
  116. /* Parse the method, scan for creation of named objects */
  117. status = acpi_ps_parse_aml(walk_state);
  118. if (ACPI_FAILURE(status)) {
  119. return_ACPI_STATUS(status);
  120. }
  121. acpi_ps_delete_parse_tree(op);
  122. return_ACPI_STATUS(status);
  123. }
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_ds_detect_named_opcodes
  127. *
  128. * PARAMETERS: walk_state - Current state of the parse tree walk
  129. * out_op - Unused, required for parser interface
  130. *
  131. * RETURN: Status
  132. *
  133. * DESCRIPTION: Descending callback used during the loading of ACPI tables.
  134. * Currently used to detect methods that must be marked serialized
  135. * in order to avoid problems with the creation of named objects.
  136. *
  137. ******************************************************************************/
  138. static acpi_status
  139. acpi_ds_detect_named_opcodes(struct acpi_walk_state *walk_state,
  140. union acpi_parse_object **out_op)
  141. {
  142. ACPI_FUNCTION_NAME(acpi_ds_detect_named_opcodes);
  143. /* We are only interested in opcodes that create a new name */
  144. if (!
  145. (walk_state->op_info->
  146. flags & (AML_NAMED | AML_CREATE | AML_FIELD))) {
  147. return (AE_OK);
  148. }
  149. /*
  150. * At this point, we know we have a Named object opcode.
  151. * Mark the method as serialized. Later code will create a mutex for
  152. * this method to enforce serialization.
  153. *
  154. * Note, ACPI_METHOD_IGNORE_SYNC_LEVEL flag means that we will ignore the
  155. * Sync Level mechanism for this method, even though it is now serialized.
  156. * Otherwise, there can be conflicts with existing ASL code that actually
  157. * uses sync levels.
  158. */
  159. walk_state->method_desc->method.sync_level = 0;
  160. walk_state->method_desc->method.info_flags |=
  161. (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
  162. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  163. "Method serialized [%4.4s] %p - [%s] (%4.4X)\n",
  164. walk_state->method_node->name.ascii,
  165. walk_state->method_node, walk_state->op_info->name,
  166. walk_state->opcode));
  167. /* Abort the parse, no need to examine this method any further */
  168. return (AE_CTRL_TERMINATE);
  169. }
  170. /*******************************************************************************
  171. *
  172. * FUNCTION: acpi_ds_method_error
  173. *
  174. * PARAMETERS: status - Execution status
  175. * walk_state - Current state
  176. *
  177. * RETURN: Status
  178. *
  179. * DESCRIPTION: Called on method error. Invoke the global exception handler if
  180. * present, dump the method data if the disassembler is configured
  181. *
  182. * Note: Allows the exception handler to change the status code
  183. *
  184. ******************************************************************************/
  185. acpi_status
  186. acpi_ds_method_error(acpi_status status, struct acpi_walk_state * walk_state)
  187. {
  188. ACPI_FUNCTION_ENTRY();
  189. /* Ignore AE_OK and control exception codes */
  190. if (ACPI_SUCCESS(status) || (status & AE_CODE_CONTROL)) {
  191. return (status);
  192. }
  193. /* Invoke the global exception handler */
  194. if (acpi_gbl_exception_handler) {
  195. /* Exit the interpreter, allow handler to execute methods */
  196. acpi_ex_exit_interpreter();
  197. /*
  198. * Handler can map the exception code to anything it wants, including
  199. * AE_OK, in which case the executing method will not be aborted.
  200. */
  201. status = acpi_gbl_exception_handler(status,
  202. walk_state->method_node ?
  203. walk_state->method_node->
  204. name.integer : 0,
  205. walk_state->opcode,
  206. walk_state->aml_offset,
  207. NULL);
  208. acpi_ex_enter_interpreter();
  209. }
  210. acpi_ds_clear_implicit_return(walk_state);
  211. #ifdef ACPI_DISASSEMBLER
  212. if (ACPI_FAILURE(status)) {
  213. /* Display method locals/args if disassembler is present */
  214. acpi_dm_dump_method_info(status, walk_state, walk_state->op);
  215. }
  216. #endif
  217. return (status);
  218. }
  219. /*******************************************************************************
  220. *
  221. * FUNCTION: acpi_ds_create_method_mutex
  222. *
  223. * PARAMETERS: obj_desc - The method object
  224. *
  225. * RETURN: Status
  226. *
  227. * DESCRIPTION: Create a mutex object for a serialized control method
  228. *
  229. ******************************************************************************/
  230. static acpi_status
  231. acpi_ds_create_method_mutex(union acpi_operand_object *method_desc)
  232. {
  233. union acpi_operand_object *mutex_desc;
  234. acpi_status status;
  235. ACPI_FUNCTION_TRACE(ds_create_method_mutex);
  236. /* Create the new mutex object */
  237. mutex_desc = acpi_ut_create_internal_object(ACPI_TYPE_MUTEX);
  238. if (!mutex_desc) {
  239. return_ACPI_STATUS(AE_NO_MEMORY);
  240. }
  241. /* Create the actual OS Mutex */
  242. status = acpi_os_create_mutex(&mutex_desc->mutex.os_mutex);
  243. if (ACPI_FAILURE(status)) {
  244. acpi_ut_delete_object_desc(mutex_desc);
  245. return_ACPI_STATUS(status);
  246. }
  247. mutex_desc->mutex.sync_level = method_desc->method.sync_level;
  248. method_desc->method.mutex = mutex_desc;
  249. return_ACPI_STATUS(AE_OK);
  250. }
  251. /*******************************************************************************
  252. *
  253. * FUNCTION: acpi_ds_begin_method_execution
  254. *
  255. * PARAMETERS: method_node - Node of the method
  256. * obj_desc - The method object
  257. * walk_state - current state, NULL if not yet executing
  258. * a method.
  259. *
  260. * RETURN: Status
  261. *
  262. * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
  263. * increments the thread count, and waits at the method semaphore
  264. * for clearance to execute.
  265. *
  266. ******************************************************************************/
  267. acpi_status
  268. acpi_ds_begin_method_execution(struct acpi_namespace_node *method_node,
  269. union acpi_operand_object *obj_desc,
  270. struct acpi_walk_state *walk_state)
  271. {
  272. acpi_status status = AE_OK;
  273. ACPI_FUNCTION_TRACE_PTR(ds_begin_method_execution, method_node);
  274. if (!method_node) {
  275. return_ACPI_STATUS(AE_NULL_ENTRY);
  276. }
  277. /* Prevent wraparound of thread count */
  278. if (obj_desc->method.thread_count == ACPI_UINT8_MAX) {
  279. ACPI_ERROR((AE_INFO,
  280. "Method reached maximum reentrancy limit (255)"));
  281. return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
  282. }
  283. /*
  284. * If this method is serialized, we need to acquire the method mutex.
  285. */
  286. if (obj_desc->method.info_flags & ACPI_METHOD_SERIALIZED) {
  287. /*
  288. * Create a mutex for the method if it is defined to be Serialized
  289. * and a mutex has not already been created. We defer the mutex creation
  290. * until a method is actually executed, to minimize the object count
  291. */
  292. if (!obj_desc->method.mutex) {
  293. status = acpi_ds_create_method_mutex(obj_desc);
  294. if (ACPI_FAILURE(status)) {
  295. return_ACPI_STATUS(status);
  296. }
  297. }
  298. /*
  299. * The current_sync_level (per-thread) must be less than or equal to
  300. * the sync level of the method. This mechanism provides some
  301. * deadlock prevention.
  302. *
  303. * If the method was auto-serialized, we just ignore the sync level
  304. * mechanism, because auto-serialization of methods can interfere
  305. * with ASL code that actually uses sync levels.
  306. *
  307. * Top-level method invocation has no walk state at this point
  308. */
  309. if (walk_state &&
  310. (!(obj_desc->method.
  311. info_flags & ACPI_METHOD_IGNORE_SYNC_LEVEL))
  312. && (walk_state->thread->current_sync_level >
  313. obj_desc->method.mutex->mutex.sync_level)) {
  314. ACPI_ERROR((AE_INFO,
  315. "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%u)",
  316. acpi_ut_get_node_name(method_node),
  317. walk_state->thread->current_sync_level));
  318. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  319. }
  320. /*
  321. * Obtain the method mutex if necessary. Do not acquire mutex for a
  322. * recursive call.
  323. */
  324. if (!walk_state ||
  325. !obj_desc->method.mutex->mutex.thread_id ||
  326. (walk_state->thread->thread_id !=
  327. obj_desc->method.mutex->mutex.thread_id)) {
  328. /*
  329. * Acquire the method mutex. This releases the interpreter if we
  330. * block (and reacquires it before it returns)
  331. */
  332. status =
  333. acpi_ex_system_wait_mutex(obj_desc->method.mutex->
  334. mutex.os_mutex,
  335. ACPI_WAIT_FOREVER);
  336. if (ACPI_FAILURE(status)) {
  337. return_ACPI_STATUS(status);
  338. }
  339. /* Update the mutex and walk info and save the original sync_level */
  340. if (walk_state) {
  341. obj_desc->method.mutex->mutex.
  342. original_sync_level =
  343. walk_state->thread->current_sync_level;
  344. obj_desc->method.mutex->mutex.thread_id =
  345. walk_state->thread->thread_id;
  346. walk_state->thread->current_sync_level =
  347. obj_desc->method.sync_level;
  348. } else {
  349. obj_desc->method.mutex->mutex.
  350. original_sync_level =
  351. obj_desc->method.mutex->mutex.sync_level;
  352. }
  353. }
  354. /* Always increase acquisition depth */
  355. obj_desc->method.mutex->mutex.acquisition_depth++;
  356. }
  357. /*
  358. * Allocate an Owner ID for this method, only if this is the first thread
  359. * to begin concurrent execution. We only need one owner_id, even if the
  360. * method is invoked recursively.
  361. */
  362. if (!obj_desc->method.owner_id) {
  363. status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
  364. if (ACPI_FAILURE(status)) {
  365. goto cleanup;
  366. }
  367. }
  368. /*
  369. * Increment the method parse tree thread count since it has been
  370. * reentered one more time (even if it is the same thread)
  371. */
  372. obj_desc->method.thread_count++;
  373. acpi_method_count++;
  374. return_ACPI_STATUS(status);
  375. cleanup:
  376. /* On error, must release the method mutex (if present) */
  377. if (obj_desc->method.mutex) {
  378. acpi_os_release_mutex(obj_desc->method.mutex->mutex.os_mutex);
  379. }
  380. return_ACPI_STATUS(status);
  381. }
  382. /*******************************************************************************
  383. *
  384. * FUNCTION: acpi_ds_call_control_method
  385. *
  386. * PARAMETERS: thread - Info for this thread
  387. * this_walk_state - Current walk state
  388. * op - Current Op to be walked
  389. *
  390. * RETURN: Status
  391. *
  392. * DESCRIPTION: Transfer execution to a called control method
  393. *
  394. ******************************************************************************/
  395. acpi_status
  396. acpi_ds_call_control_method(struct acpi_thread_state *thread,
  397. struct acpi_walk_state *this_walk_state,
  398. union acpi_parse_object *op)
  399. {
  400. acpi_status status;
  401. struct acpi_namespace_node *method_node;
  402. struct acpi_walk_state *next_walk_state = NULL;
  403. union acpi_operand_object *obj_desc;
  404. struct acpi_evaluate_info *info;
  405. u32 i;
  406. ACPI_FUNCTION_TRACE_PTR(ds_call_control_method, this_walk_state);
  407. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  408. "Calling method %p, currentstate=%p\n",
  409. this_walk_state->prev_op, this_walk_state));
  410. /*
  411. * Get the namespace entry for the control method we are about to call
  412. */
  413. method_node = this_walk_state->method_call_node;
  414. if (!method_node) {
  415. return_ACPI_STATUS(AE_NULL_ENTRY);
  416. }
  417. obj_desc = acpi_ns_get_attached_object(method_node);
  418. if (!obj_desc) {
  419. return_ACPI_STATUS(AE_NULL_OBJECT);
  420. }
  421. /* Init for new method, possibly wait on method mutex */
  422. status = acpi_ds_begin_method_execution(method_node, obj_desc,
  423. this_walk_state);
  424. if (ACPI_FAILURE(status)) {
  425. return_ACPI_STATUS(status);
  426. }
  427. /* Begin method parse/execution. Create a new walk state */
  428. next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
  429. NULL, obj_desc, thread);
  430. if (!next_walk_state) {
  431. status = AE_NO_MEMORY;
  432. goto cleanup;
  433. }
  434. /*
  435. * The resolved arguments were put on the previous walk state's operand
  436. * stack. Operands on the previous walk state stack always
  437. * start at index 0. Also, null terminate the list of arguments
  438. */
  439. this_walk_state->operands[this_walk_state->num_operands] = NULL;
  440. /*
  441. * Allocate and initialize the evaluation information block
  442. * TBD: this is somewhat inefficient, should change interface to
  443. * ds_init_aml_walk. For now, keeps this struct off the CPU stack
  444. */
  445. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  446. if (!info) {
  447. status = AE_NO_MEMORY;
  448. goto cleanup;
  449. }
  450. info->parameters = &this_walk_state->operands[0];
  451. status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
  452. obj_desc->method.aml_start,
  453. obj_desc->method.aml_length, info,
  454. ACPI_IMODE_EXECUTE);
  455. ACPI_FREE(info);
  456. if (ACPI_FAILURE(status)) {
  457. goto cleanup;
  458. }
  459. /*
  460. * Delete the operands on the previous walkstate operand stack
  461. * (they were copied to new objects)
  462. */
  463. for (i = 0; i < obj_desc->method.param_count; i++) {
  464. acpi_ut_remove_reference(this_walk_state->operands[i]);
  465. this_walk_state->operands[i] = NULL;
  466. }
  467. /* Clear the operand stack */
  468. this_walk_state->num_operands = 0;
  469. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  470. "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
  471. method_node->name.ascii, next_walk_state));
  472. /* Invoke an internal method if necessary */
  473. if (obj_desc->method.info_flags & ACPI_METHOD_INTERNAL_ONLY) {
  474. status =
  475. obj_desc->method.dispatch.implementation(next_walk_state);
  476. if (status == AE_OK) {
  477. status = AE_CTRL_TERMINATE;
  478. }
  479. }
  480. return_ACPI_STATUS(status);
  481. cleanup:
  482. /* On error, we must terminate the method properly */
  483. acpi_ds_terminate_control_method(obj_desc, next_walk_state);
  484. if (next_walk_state) {
  485. acpi_ds_delete_walk_state(next_walk_state);
  486. }
  487. return_ACPI_STATUS(status);
  488. }
  489. /*******************************************************************************
  490. *
  491. * FUNCTION: acpi_ds_restart_control_method
  492. *
  493. * PARAMETERS: walk_state - State for preempted method (caller)
  494. * return_desc - Return value from the called method
  495. *
  496. * RETURN: Status
  497. *
  498. * DESCRIPTION: Restart a method that was preempted by another (nested) method
  499. * invocation. Handle the return value (if any) from the callee.
  500. *
  501. ******************************************************************************/
  502. acpi_status
  503. acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
  504. union acpi_operand_object *return_desc)
  505. {
  506. acpi_status status;
  507. int same_as_implicit_return;
  508. ACPI_FUNCTION_TRACE_PTR(ds_restart_control_method, walk_state);
  509. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  510. "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
  511. acpi_ut_get_node_name(walk_state->method_node),
  512. walk_state->method_call_op, return_desc));
  513. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  514. " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
  515. walk_state->return_used,
  516. walk_state->results, walk_state));
  517. /* Did the called method return a value? */
  518. if (return_desc) {
  519. /* Is the implicit return object the same as the return desc? */
  520. same_as_implicit_return =
  521. (walk_state->implicit_return_obj == return_desc);
  522. /* Are we actually going to use the return value? */
  523. if (walk_state->return_used) {
  524. /* Save the return value from the previous method */
  525. status = acpi_ds_result_push(return_desc, walk_state);
  526. if (ACPI_FAILURE(status)) {
  527. acpi_ut_remove_reference(return_desc);
  528. return_ACPI_STATUS(status);
  529. }
  530. /*
  531. * Save as THIS method's return value in case it is returned
  532. * immediately to yet another method
  533. */
  534. walk_state->return_desc = return_desc;
  535. }
  536. /*
  537. * The following code is the optional support for the so-called
  538. * "implicit return". Some AML code assumes that the last value of the
  539. * method is "implicitly" returned to the caller, in the absence of an
  540. * explicit return value.
  541. *
  542. * Just save the last result of the method as the return value.
  543. *
  544. * NOTE: this is optional because the ASL language does not actually
  545. * support this behavior.
  546. */
  547. else if (!acpi_ds_do_implicit_return
  548. (return_desc, walk_state, FALSE)
  549. || same_as_implicit_return) {
  550. /*
  551. * Delete the return value if it will not be used by the
  552. * calling method or remove one reference if the explicit return
  553. * is the same as the implicit return value.
  554. */
  555. acpi_ut_remove_reference(return_desc);
  556. }
  557. }
  558. return_ACPI_STATUS(AE_OK);
  559. }
  560. /*******************************************************************************
  561. *
  562. * FUNCTION: acpi_ds_terminate_control_method
  563. *
  564. * PARAMETERS: method_desc - Method object
  565. * walk_state - State associated with the method
  566. *
  567. * RETURN: None
  568. *
  569. * DESCRIPTION: Terminate a control method. Delete everything that the method
  570. * created, delete all locals and arguments, and delete the parse
  571. * tree if requested.
  572. *
  573. * MUTEX: Interpreter is locked
  574. *
  575. ******************************************************************************/
  576. void
  577. acpi_ds_terminate_control_method(union acpi_operand_object *method_desc,
  578. struct acpi_walk_state *walk_state)
  579. {
  580. ACPI_FUNCTION_TRACE_PTR(ds_terminate_control_method, walk_state);
  581. /* method_desc is required, walk_state is optional */
  582. if (!method_desc) {
  583. return_VOID;
  584. }
  585. if (walk_state) {
  586. /* Delete all arguments and locals */
  587. acpi_ds_method_data_delete_all(walk_state);
  588. /*
  589. * If method is serialized, release the mutex and restore the
  590. * current sync level for this thread
  591. */
  592. if (method_desc->method.mutex) {
  593. /* Acquisition Depth handles recursive calls */
  594. method_desc->method.mutex->mutex.acquisition_depth--;
  595. if (!method_desc->method.mutex->mutex.acquisition_depth) {
  596. walk_state->thread->current_sync_level =
  597. method_desc->method.mutex->mutex.
  598. original_sync_level;
  599. acpi_os_release_mutex(method_desc->method.
  600. mutex->mutex.os_mutex);
  601. method_desc->method.mutex->mutex.thread_id = 0;
  602. }
  603. }
  604. /*
  605. * Delete any namespace objects created anywhere within the
  606. * namespace by the execution of this method. Unless:
  607. * 1) This method is a module-level executable code method, in which
  608. * case we want make the objects permanent.
  609. * 2) There are other threads executing the method, in which case we
  610. * will wait until the last thread has completed.
  611. */
  612. if (!(method_desc->method.info_flags & ACPI_METHOD_MODULE_LEVEL)
  613. && (method_desc->method.thread_count == 1)) {
  614. /* Delete any direct children of (created by) this method */
  615. acpi_ns_delete_namespace_subtree(walk_state->
  616. method_node);
  617. /*
  618. * Delete any objects that were created by this method
  619. * elsewhere in the namespace (if any were created).
  620. * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
  621. * deletion such that we don't have to perform an entire
  622. * namespace walk for every control method execution.
  623. */
  624. if (method_desc->method.
  625. info_flags & ACPI_METHOD_MODIFIED_NAMESPACE) {
  626. acpi_ns_delete_namespace_by_owner(method_desc->
  627. method.
  628. owner_id);
  629. method_desc->method.info_flags &=
  630. ~ACPI_METHOD_MODIFIED_NAMESPACE;
  631. }
  632. }
  633. }
  634. /* Decrement the thread count on the method */
  635. if (method_desc->method.thread_count) {
  636. method_desc->method.thread_count--;
  637. } else {
  638. ACPI_ERROR((AE_INFO, "Invalid zero thread count in method"));
  639. }
  640. /* Are there any other threads currently executing this method? */
  641. if (method_desc->method.thread_count) {
  642. /*
  643. * Additional threads. Do not release the owner_id in this case,
  644. * we immediately reuse it for the next thread executing this method
  645. */
  646. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  647. "*** Completed execution of one thread, %u threads remaining\n",
  648. method_desc->method.thread_count));
  649. } else {
  650. /* This is the only executing thread for this method */
  651. /*
  652. * Support to dynamically change a method from not_serialized to
  653. * Serialized if it appears that the method is incorrectly written and
  654. * does not support multiple thread execution. The best example of this
  655. * is if such a method creates namespace objects and blocks. A second
  656. * thread will fail with an AE_ALREADY_EXISTS exception.
  657. *
  658. * This code is here because we must wait until the last thread exits
  659. * before marking the method as serialized.
  660. */
  661. if (method_desc->method.
  662. info_flags & ACPI_METHOD_SERIALIZED_PENDING) {
  663. if (walk_state) {
  664. ACPI_INFO((AE_INFO,
  665. "Marking method %4.4s as Serialized because of AE_ALREADY_EXISTS error",
  666. walk_state->method_node->name.
  667. ascii));
  668. }
  669. /*
  670. * Method tried to create an object twice and was marked as
  671. * "pending serialized". The probable cause is that the method
  672. * cannot handle reentrancy.
  673. *
  674. * The method was created as not_serialized, but it tried to create
  675. * a named object and then blocked, causing the second thread
  676. * entrance to begin and then fail. Workaround this problem by
  677. * marking the method permanently as Serialized when the last
  678. * thread exits here.
  679. */
  680. method_desc->method.info_flags &=
  681. ~ACPI_METHOD_SERIALIZED_PENDING;
  682. method_desc->method.info_flags |=
  683. (ACPI_METHOD_SERIALIZED |
  684. ACPI_METHOD_IGNORE_SYNC_LEVEL);
  685. method_desc->method.sync_level = 0;
  686. }
  687. /* No more threads, we can free the owner_id */
  688. if (!
  689. (method_desc->method.
  690. info_flags & ACPI_METHOD_MODULE_LEVEL)) {
  691. acpi_ut_release_owner_id(&method_desc->method.owner_id);
  692. }
  693. }
  694. return_VOID;
  695. }