dsmethod.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /******************************************************************************
  2. *
  3. * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, 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 <acpi/amlcode.h>
  44. #include <acpi/acdispat.h>
  45. #include <acpi/acinterp.h>
  46. #include <acpi/acnamesp.h>
  47. #include <acpi/acdisasm.h>
  48. #define _COMPONENT ACPI_DISPATCHER
  49. ACPI_MODULE_NAME("dsmethod")
  50. /* Local prototypes */
  51. static acpi_status
  52. acpi_ds_create_method_mutex(union acpi_operand_object *method_desc);
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_ds_method_error
  56. *
  57. * PARAMETERS: Status - Execution status
  58. * walk_state - Current state
  59. *
  60. * RETURN: Status
  61. *
  62. * DESCRIPTION: Called on method error. Invoke the global exception handler if
  63. * present, dump the method data if the disassembler is configured
  64. *
  65. * Note: Allows the exception handler to change the status code
  66. *
  67. ******************************************************************************/
  68. acpi_status
  69. acpi_ds_method_error(acpi_status status, struct acpi_walk_state *walk_state)
  70. {
  71. ACPI_FUNCTION_ENTRY();
  72. /* Ignore AE_OK and control exception codes */
  73. if (ACPI_SUCCESS(status) || (status & AE_CODE_CONTROL)) {
  74. return (status);
  75. }
  76. /* Invoke the global exception handler */
  77. if (acpi_gbl_exception_handler) {
  78. /* Exit the interpreter, allow handler to execute methods */
  79. acpi_ex_exit_interpreter();
  80. /*
  81. * Handler can map the exception code to anything it wants, including
  82. * AE_OK, in which case the executing method will not be aborted.
  83. */
  84. status = acpi_gbl_exception_handler(status,
  85. walk_state->method_node ?
  86. walk_state->method_node->
  87. name.integer : 0,
  88. walk_state->opcode,
  89. walk_state->aml_offset,
  90. NULL);
  91. acpi_ex_enter_interpreter();
  92. }
  93. #ifdef ACPI_DISASSEMBLER
  94. if (ACPI_FAILURE(status)) {
  95. /* Display method locals/args if disassembler is present */
  96. acpi_dm_dump_method_info(status, walk_state, walk_state->op);
  97. }
  98. #endif
  99. return (status);
  100. }
  101. /*******************************************************************************
  102. *
  103. * FUNCTION: acpi_ds_create_method_mutex
  104. *
  105. * PARAMETERS: obj_desc - The method object
  106. *
  107. * RETURN: Status
  108. *
  109. * DESCRIPTION: Create a mutex object for a serialized control method
  110. *
  111. ******************************************************************************/
  112. static acpi_status
  113. acpi_ds_create_method_mutex(union acpi_operand_object *method_desc)
  114. {
  115. union acpi_operand_object *mutex_desc;
  116. acpi_status status;
  117. ACPI_FUNCTION_TRACE(ds_create_method_mutex);
  118. /* Create the new mutex object */
  119. mutex_desc = acpi_ut_create_internal_object(ACPI_TYPE_MUTEX);
  120. if (!mutex_desc) {
  121. return_ACPI_STATUS(AE_NO_MEMORY);
  122. }
  123. /* Create the actual OS Mutex */
  124. status = acpi_os_create_mutex(&mutex_desc->mutex.os_mutex);
  125. if (ACPI_FAILURE(status)) {
  126. return_ACPI_STATUS(status);
  127. }
  128. mutex_desc->mutex.sync_level = method_desc->method.sync_level;
  129. method_desc->method.mutex = mutex_desc;
  130. return_ACPI_STATUS(AE_OK);
  131. }
  132. /*******************************************************************************
  133. *
  134. * FUNCTION: acpi_ds_begin_method_execution
  135. *
  136. * PARAMETERS: method_node - Node of the method
  137. * obj_desc - The method object
  138. * walk_state - current state, NULL if not yet executing
  139. * a method.
  140. *
  141. * RETURN: Status
  142. *
  143. * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
  144. * increments the thread count, and waits at the method semaphore
  145. * for clearance to execute.
  146. *
  147. ******************************************************************************/
  148. acpi_status
  149. acpi_ds_begin_method_execution(struct acpi_namespace_node *method_node,
  150. union acpi_operand_object *obj_desc,
  151. struct acpi_walk_state *walk_state)
  152. {
  153. acpi_status status = AE_OK;
  154. ACPI_FUNCTION_TRACE_PTR(ds_begin_method_execution, method_node);
  155. if (!method_node) {
  156. return_ACPI_STATUS(AE_NULL_ENTRY);
  157. }
  158. /* Prevent wraparound of thread count */
  159. if (obj_desc->method.thread_count == ACPI_UINT8_MAX) {
  160. ACPI_ERROR((AE_INFO,
  161. "Method reached maximum reentrancy limit (255)"));
  162. return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
  163. }
  164. /*
  165. * If this method is serialized, we need to acquire the method mutex.
  166. */
  167. if (obj_desc->method.method_flags & AML_METHOD_SERIALIZED) {
  168. /*
  169. * Create a mutex for the method if it is defined to be Serialized
  170. * and a mutex has not already been created. We defer the mutex creation
  171. * until a method is actually executed, to minimize the object count
  172. */
  173. if (!obj_desc->method.mutex) {
  174. status = acpi_ds_create_method_mutex(obj_desc);
  175. if (ACPI_FAILURE(status)) {
  176. return_ACPI_STATUS(status);
  177. }
  178. }
  179. /*
  180. * The current_sync_level (per-thread) must be less than or equal to
  181. * the sync level of the method. This mechanism provides some
  182. * deadlock prevention
  183. *
  184. * Top-level method invocation has no walk state at this point
  185. */
  186. if (walk_state &&
  187. (walk_state->thread->current_sync_level >
  188. obj_desc->method.mutex->mutex.sync_level)) {
  189. ACPI_ERROR((AE_INFO,
  190. "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%d)",
  191. acpi_ut_get_node_name(method_node),
  192. walk_state->thread->current_sync_level));
  193. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  194. }
  195. /*
  196. * Obtain the method mutex if necessary. Do not acquire mutex for a
  197. * recursive call.
  198. */
  199. if (!walk_state ||
  200. !obj_desc->method.mutex->mutex.thread_id ||
  201. (walk_state->thread->thread_id !=
  202. obj_desc->method.mutex->mutex.thread_id)) {
  203. /*
  204. * Acquire the method mutex. This releases the interpreter if we
  205. * block (and reacquires it before it returns)
  206. */
  207. status =
  208. acpi_ex_system_wait_mutex(obj_desc->method.mutex->
  209. mutex.os_mutex,
  210. ACPI_WAIT_FOREVER);
  211. if (ACPI_FAILURE(status)) {
  212. return_ACPI_STATUS(status);
  213. }
  214. /* Update the mutex and walk info and save the original sync_level */
  215. if (walk_state) {
  216. obj_desc->method.mutex->mutex.
  217. original_sync_level =
  218. walk_state->thread->current_sync_level;
  219. obj_desc->method.mutex->mutex.thread_id =
  220. walk_state->thread->thread_id;
  221. walk_state->thread->current_sync_level =
  222. obj_desc->method.sync_level;
  223. } else {
  224. obj_desc->method.mutex->mutex.
  225. original_sync_level =
  226. obj_desc->method.mutex->mutex.sync_level;
  227. }
  228. }
  229. /* Always increase acquisition depth */
  230. obj_desc->method.mutex->mutex.acquisition_depth++;
  231. }
  232. /*
  233. * Allocate an Owner ID for this method, only if this is the first thread
  234. * to begin concurrent execution. We only need one owner_id, even if the
  235. * method is invoked recursively.
  236. */
  237. if (!obj_desc->method.owner_id) {
  238. status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
  239. if (ACPI_FAILURE(status)) {
  240. goto cleanup;
  241. }
  242. }
  243. /*
  244. * Increment the method parse tree thread count since it has been
  245. * reentered one more time (even if it is the same thread)
  246. */
  247. obj_desc->method.thread_count++;
  248. return_ACPI_STATUS(status);
  249. cleanup:
  250. /* On error, must release the method mutex (if present) */
  251. if (obj_desc->method.mutex) {
  252. acpi_os_release_mutex(obj_desc->method.mutex->mutex.os_mutex);
  253. }
  254. return_ACPI_STATUS(status);
  255. }
  256. /*******************************************************************************
  257. *
  258. * FUNCTION: acpi_ds_call_control_method
  259. *
  260. * PARAMETERS: Thread - Info for this thread
  261. * this_walk_state - Current walk state
  262. * Op - Current Op to be walked
  263. *
  264. * RETURN: Status
  265. *
  266. * DESCRIPTION: Transfer execution to a called control method
  267. *
  268. ******************************************************************************/
  269. acpi_status
  270. acpi_ds_call_control_method(struct acpi_thread_state *thread,
  271. struct acpi_walk_state *this_walk_state,
  272. union acpi_parse_object *op)
  273. {
  274. acpi_status status;
  275. struct acpi_namespace_node *method_node;
  276. struct acpi_walk_state *next_walk_state = NULL;
  277. union acpi_operand_object *obj_desc;
  278. struct acpi_evaluate_info *info;
  279. u32 i;
  280. ACPI_FUNCTION_TRACE_PTR(ds_call_control_method, this_walk_state);
  281. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  282. "Calling method %p, currentstate=%p\n",
  283. this_walk_state->prev_op, this_walk_state));
  284. /*
  285. * Get the namespace entry for the control method we are about to call
  286. */
  287. method_node = this_walk_state->method_call_node;
  288. if (!method_node) {
  289. return_ACPI_STATUS(AE_NULL_ENTRY);
  290. }
  291. obj_desc = acpi_ns_get_attached_object(method_node);
  292. if (!obj_desc) {
  293. return_ACPI_STATUS(AE_NULL_OBJECT);
  294. }
  295. /* Init for new method, possibly wait on method mutex */
  296. status = acpi_ds_begin_method_execution(method_node, obj_desc,
  297. this_walk_state);
  298. if (ACPI_FAILURE(status)) {
  299. return_ACPI_STATUS(status);
  300. }
  301. /* Begin method parse/execution. Create a new walk state */
  302. next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
  303. NULL, obj_desc, thread);
  304. if (!next_walk_state) {
  305. status = AE_NO_MEMORY;
  306. goto cleanup;
  307. }
  308. /*
  309. * The resolved arguments were put on the previous walk state's operand
  310. * stack. Operands on the previous walk state stack always
  311. * start at index 0. Also, null terminate the list of arguments
  312. */
  313. this_walk_state->operands[this_walk_state->num_operands] = NULL;
  314. /*
  315. * Allocate and initialize the evaluation information block
  316. * TBD: this is somewhat inefficient, should change interface to
  317. * ds_init_aml_walk. For now, keeps this struct off the CPU stack
  318. */
  319. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  320. if (!info) {
  321. return_ACPI_STATUS(AE_NO_MEMORY);
  322. }
  323. info->parameters = &this_walk_state->operands[0];
  324. status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
  325. obj_desc->method.aml_start,
  326. obj_desc->method.aml_length, info,
  327. ACPI_IMODE_EXECUTE);
  328. ACPI_FREE(info);
  329. if (ACPI_FAILURE(status)) {
  330. goto cleanup;
  331. }
  332. /*
  333. * Delete the operands on the previous walkstate operand stack
  334. * (they were copied to new objects)
  335. */
  336. for (i = 0; i < obj_desc->method.param_count; i++) {
  337. acpi_ut_remove_reference(this_walk_state->operands[i]);
  338. this_walk_state->operands[i] = NULL;
  339. }
  340. /* Clear the operand stack */
  341. this_walk_state->num_operands = 0;
  342. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  343. "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
  344. method_node->name.ascii, next_walk_state));
  345. /* Invoke an internal method if necessary */
  346. if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
  347. status = obj_desc->method.implementation(next_walk_state);
  348. }
  349. return_ACPI_STATUS(status);
  350. cleanup:
  351. /* On error, we must terminate the method properly */
  352. acpi_ds_terminate_control_method(obj_desc, next_walk_state);
  353. if (next_walk_state) {
  354. acpi_ds_delete_walk_state(next_walk_state);
  355. }
  356. return_ACPI_STATUS(status);
  357. }
  358. /*******************************************************************************
  359. *
  360. * FUNCTION: acpi_ds_restart_control_method
  361. *
  362. * PARAMETERS: walk_state - State for preempted method (caller)
  363. * return_desc - Return value from the called method
  364. *
  365. * RETURN: Status
  366. *
  367. * DESCRIPTION: Restart a method that was preempted by another (nested) method
  368. * invocation. Handle the return value (if any) from the callee.
  369. *
  370. ******************************************************************************/
  371. acpi_status
  372. acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
  373. union acpi_operand_object *return_desc)
  374. {
  375. acpi_status status;
  376. int same_as_implicit_return;
  377. ACPI_FUNCTION_TRACE_PTR(ds_restart_control_method, walk_state);
  378. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  379. "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
  380. acpi_ut_get_node_name(walk_state->method_node),
  381. walk_state->method_call_op, return_desc));
  382. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  383. " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
  384. walk_state->return_used,
  385. walk_state->results, walk_state));
  386. /* Did the called method return a value? */
  387. if (return_desc) {
  388. /* Is the implicit return object the same as the return desc? */
  389. same_as_implicit_return =
  390. (walk_state->implicit_return_obj == return_desc);
  391. /* Are we actually going to use the return value? */
  392. if (walk_state->return_used) {
  393. /* Save the return value from the previous method */
  394. status = acpi_ds_result_push(return_desc, walk_state);
  395. if (ACPI_FAILURE(status)) {
  396. acpi_ut_remove_reference(return_desc);
  397. return_ACPI_STATUS(status);
  398. }
  399. /*
  400. * Save as THIS method's return value in case it is returned
  401. * immediately to yet another method
  402. */
  403. walk_state->return_desc = return_desc;
  404. }
  405. /*
  406. * The following code is the optional support for the so-called
  407. * "implicit return". Some AML code assumes that the last value of the
  408. * method is "implicitly" returned to the caller, in the absence of an
  409. * explicit return value.
  410. *
  411. * Just save the last result of the method as the return value.
  412. *
  413. * NOTE: this is optional because the ASL language does not actually
  414. * support this behavior.
  415. */
  416. else if (!acpi_ds_do_implicit_return
  417. (return_desc, walk_state, FALSE)
  418. || same_as_implicit_return) {
  419. /*
  420. * Delete the return value if it will not be used by the
  421. * calling method or remove one reference if the explicit return
  422. * is the same as the implicit return value.
  423. */
  424. acpi_ut_remove_reference(return_desc);
  425. }
  426. }
  427. return_ACPI_STATUS(AE_OK);
  428. }
  429. /*******************************************************************************
  430. *
  431. * FUNCTION: acpi_ds_terminate_control_method
  432. *
  433. * PARAMETERS: method_desc - Method object
  434. * walk_state - State associated with the method
  435. *
  436. * RETURN: None
  437. *
  438. * DESCRIPTION: Terminate a control method. Delete everything that the method
  439. * created, delete all locals and arguments, and delete the parse
  440. * tree if requested.
  441. *
  442. * MUTEX: Interpreter is locked
  443. *
  444. ******************************************************************************/
  445. void
  446. acpi_ds_terminate_control_method(union acpi_operand_object *method_desc,
  447. struct acpi_walk_state *walk_state)
  448. {
  449. ACPI_FUNCTION_TRACE_PTR(ds_terminate_control_method, walk_state);
  450. /* method_desc is required, walk_state is optional */
  451. if (!method_desc) {
  452. return_VOID;
  453. }
  454. if (walk_state) {
  455. /* Delete all arguments and locals */
  456. acpi_ds_method_data_delete_all(walk_state);
  457. /*
  458. * If method is serialized, release the mutex and restore the
  459. * current sync level for this thread
  460. */
  461. if (method_desc->method.mutex) {
  462. /* Acquisition Depth handles recursive calls */
  463. method_desc->method.mutex->mutex.acquisition_depth--;
  464. if (!method_desc->method.mutex->mutex.acquisition_depth) {
  465. walk_state->thread->current_sync_level =
  466. method_desc->method.mutex->mutex.
  467. original_sync_level;
  468. acpi_os_release_mutex(method_desc->method.
  469. mutex->mutex.os_mutex);
  470. method_desc->method.mutex->mutex.thread_id = NULL;
  471. }
  472. }
  473. /*
  474. * Delete any namespace objects created anywhere within
  475. * the namespace by the execution of this method
  476. */
  477. acpi_ns_delete_namespace_by_owner(method_desc->method.owner_id);
  478. }
  479. /* Decrement the thread count on the method */
  480. if (method_desc->method.thread_count) {
  481. method_desc->method.thread_count--;
  482. } else {
  483. ACPI_ERROR((AE_INFO, "Invalid zero thread count in method"));
  484. }
  485. /* Are there any other threads currently executing this method? */
  486. if (method_desc->method.thread_count) {
  487. /*
  488. * Additional threads. Do not release the owner_id in this case,
  489. * we immediately reuse it for the next thread executing this method
  490. */
  491. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  492. "*** Completed execution of one thread, %d threads remaining\n",
  493. method_desc->method.thread_count));
  494. } else {
  495. /* This is the only executing thread for this method */
  496. /*
  497. * Support to dynamically change a method from not_serialized to
  498. * Serialized if it appears that the method is incorrectly written and
  499. * does not support multiple thread execution. The best example of this
  500. * is if such a method creates namespace objects and blocks. A second
  501. * thread will fail with an AE_ALREADY_EXISTS exception
  502. *
  503. * This code is here because we must wait until the last thread exits
  504. * before creating the synchronization semaphore.
  505. */
  506. if ((method_desc->method.method_flags & AML_METHOD_SERIALIZED)
  507. && (!method_desc->method.mutex)) {
  508. (void)acpi_ds_create_method_mutex(method_desc);
  509. }
  510. /* No more threads, we can free the owner_id */
  511. acpi_ut_release_owner_id(&method_desc->method.owner_id);
  512. }
  513. return_VOID;
  514. }