dsmethod.c 26 KB

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