dsmethod.c 19 KB

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