dsmethod.c 25 KB

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