exmutex.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /******************************************************************************
  2. *
  3. * Module Name: exmutex - ASL Mutex Acquire/Release functions
  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 "accommon.h"
  44. #include "acinterp.h"
  45. #include "acevents.h"
  46. #define _COMPONENT ACPI_EXECUTER
  47. ACPI_MODULE_NAME("exmutex")
  48. /* Local prototypes */
  49. static void
  50. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  51. struct acpi_thread_state *thread);
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ex_unlink_mutex
  55. *
  56. * PARAMETERS: obj_desc - The mutex to be unlinked
  57. *
  58. * RETURN: None
  59. *
  60. * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
  61. *
  62. ******************************************************************************/
  63. void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
  64. {
  65. struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
  66. if (!thread) {
  67. return;
  68. }
  69. /* Doubly linked list */
  70. if (obj_desc->mutex.next) {
  71. (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
  72. }
  73. if (obj_desc->mutex.prev) {
  74. (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
  75. } else {
  76. thread->acquired_mutex_list = obj_desc->mutex.next;
  77. }
  78. }
  79. /*******************************************************************************
  80. *
  81. * FUNCTION: acpi_ex_link_mutex
  82. *
  83. * PARAMETERS: obj_desc - The mutex to be linked
  84. * Thread - Current executing thread object
  85. *
  86. * RETURN: None
  87. *
  88. * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
  89. *
  90. ******************************************************************************/
  91. static void
  92. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  93. struct acpi_thread_state *thread)
  94. {
  95. union acpi_operand_object *list_head;
  96. list_head = thread->acquired_mutex_list;
  97. /* This object will be the first object in the list */
  98. obj_desc->mutex.prev = NULL;
  99. obj_desc->mutex.next = list_head;
  100. /* Update old first object to point back to this object */
  101. if (list_head) {
  102. list_head->mutex.prev = obj_desc;
  103. }
  104. /* Update list head */
  105. thread->acquired_mutex_list = obj_desc;
  106. }
  107. /*******************************************************************************
  108. *
  109. * FUNCTION: acpi_ex_acquire_mutex_object
  110. *
  111. * PARAMETERS: time_desc - Timeout in milliseconds
  112. * obj_desc - Mutex object
  113. * Thread - Current thread state
  114. *
  115. * RETURN: Status
  116. *
  117. * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
  118. * path that supports multiple acquires by the same thread.
  119. *
  120. * MUTEX: Interpreter must be locked
  121. *
  122. * NOTE: This interface is called from three places:
  123. * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
  124. * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
  125. * global lock
  126. * 3) From the external interface, acpi_acquire_global_lock
  127. *
  128. ******************************************************************************/
  129. acpi_status
  130. acpi_ex_acquire_mutex_object(u16 timeout,
  131. union acpi_operand_object *obj_desc,
  132. acpi_thread_id thread_id)
  133. {
  134. acpi_status status;
  135. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
  136. if (!obj_desc) {
  137. return_ACPI_STATUS(AE_BAD_PARAMETER);
  138. }
  139. /* Support for multiple acquires by the owning thread */
  140. if (obj_desc->mutex.thread_id == thread_id) {
  141. /*
  142. * The mutex is already owned by this thread, just increment the
  143. * acquisition depth
  144. */
  145. obj_desc->mutex.acquisition_depth++;
  146. return_ACPI_STATUS(AE_OK);
  147. }
  148. /* Acquire the mutex, wait if necessary. Special case for Global Lock */
  149. if (obj_desc == acpi_gbl_global_lock_mutex) {
  150. status = acpi_ev_acquire_global_lock(timeout);
  151. } else {
  152. status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
  153. timeout);
  154. }
  155. if (ACPI_FAILURE(status)) {
  156. /* Includes failure from a timeout on time_desc */
  157. return_ACPI_STATUS(status);
  158. }
  159. /* Acquired the mutex: update mutex object */
  160. obj_desc->mutex.thread_id = thread_id;
  161. obj_desc->mutex.acquisition_depth = 1;
  162. obj_desc->mutex.original_sync_level = 0;
  163. obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
  164. return_ACPI_STATUS(AE_OK);
  165. }
  166. /*******************************************************************************
  167. *
  168. * FUNCTION: acpi_ex_acquire_mutex
  169. *
  170. * PARAMETERS: time_desc - Timeout integer
  171. * obj_desc - Mutex object
  172. * walk_state - Current method execution state
  173. *
  174. * RETURN: Status
  175. *
  176. * DESCRIPTION: Acquire an AML mutex
  177. *
  178. ******************************************************************************/
  179. acpi_status
  180. acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
  181. union acpi_operand_object *obj_desc,
  182. struct acpi_walk_state *walk_state)
  183. {
  184. acpi_status status;
  185. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
  186. if (!obj_desc) {
  187. return_ACPI_STATUS(AE_BAD_PARAMETER);
  188. }
  189. /* Must have a valid thread ID */
  190. if (!walk_state->thread) {
  191. ACPI_ERROR((AE_INFO,
  192. "Cannot acquire Mutex [%4.4s], null thread info",
  193. acpi_ut_get_node_name(obj_desc->mutex.node)));
  194. return_ACPI_STATUS(AE_AML_INTERNAL);
  195. }
  196. /*
  197. * Current sync level must be less than or equal to the sync level of the
  198. * mutex. This mechanism provides some deadlock prevention
  199. */
  200. if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
  201. ACPI_ERROR((AE_INFO,
  202. "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)",
  203. acpi_ut_get_node_name(obj_desc->mutex.node),
  204. walk_state->thread->current_sync_level));
  205. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  206. }
  207. status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value,
  208. obj_desc,
  209. walk_state->thread->thread_id);
  210. if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
  211. /* Save Thread object, original/current sync levels */
  212. obj_desc->mutex.owner_thread = walk_state->thread;
  213. obj_desc->mutex.original_sync_level =
  214. walk_state->thread->current_sync_level;
  215. walk_state->thread->current_sync_level =
  216. obj_desc->mutex.sync_level;
  217. /* Link the mutex to the current thread for force-unlock at method exit */
  218. acpi_ex_link_mutex(obj_desc, walk_state->thread);
  219. }
  220. return_ACPI_STATUS(status);
  221. }
  222. /*******************************************************************************
  223. *
  224. * FUNCTION: acpi_ex_release_mutex_object
  225. *
  226. * PARAMETERS: obj_desc - The object descriptor for this op
  227. *
  228. * RETURN: Status
  229. *
  230. * DESCRIPTION: Release a previously acquired Mutex, low level interface.
  231. * Provides a common path that supports multiple releases (after
  232. * previous multiple acquires) by the same thread.
  233. *
  234. * MUTEX: Interpreter must be locked
  235. *
  236. * NOTE: This interface is called from three places:
  237. * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
  238. * 2) From acpi_ex_release_global_lock when an AML Field access requires the
  239. * global lock
  240. * 3) From the external interface, acpi_release_global_lock
  241. *
  242. ******************************************************************************/
  243. acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
  244. {
  245. acpi_status status = AE_OK;
  246. ACPI_FUNCTION_TRACE(ex_release_mutex_object);
  247. if (obj_desc->mutex.acquisition_depth == 0) {
  248. return (AE_NOT_ACQUIRED);
  249. }
  250. /* Match multiple Acquires with multiple Releases */
  251. obj_desc->mutex.acquisition_depth--;
  252. if (obj_desc->mutex.acquisition_depth != 0) {
  253. /* Just decrement the depth and return */
  254. return_ACPI_STATUS(AE_OK);
  255. }
  256. if (obj_desc->mutex.owner_thread) {
  257. /* Unlink the mutex from the owner's list */
  258. acpi_ex_unlink_mutex(obj_desc);
  259. obj_desc->mutex.owner_thread = NULL;
  260. }
  261. /* Release the mutex, special case for Global Lock */
  262. if (obj_desc == acpi_gbl_global_lock_mutex) {
  263. status = acpi_ev_release_global_lock();
  264. } else {
  265. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  266. }
  267. /* Clear mutex info */
  268. obj_desc->mutex.thread_id = NULL;
  269. return_ACPI_STATUS(status);
  270. }
  271. /*******************************************************************************
  272. *
  273. * FUNCTION: acpi_ex_release_mutex
  274. *
  275. * PARAMETERS: obj_desc - The object descriptor for this op
  276. * walk_state - Current method execution state
  277. *
  278. * RETURN: Status
  279. *
  280. * DESCRIPTION: Release a previously acquired Mutex.
  281. *
  282. ******************************************************************************/
  283. acpi_status
  284. acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
  285. struct acpi_walk_state *walk_state)
  286. {
  287. acpi_status status = AE_OK;
  288. ACPI_FUNCTION_TRACE(ex_release_mutex);
  289. if (!obj_desc) {
  290. return_ACPI_STATUS(AE_BAD_PARAMETER);
  291. }
  292. /* The mutex must have been previously acquired in order to release it */
  293. if (!obj_desc->mutex.owner_thread) {
  294. ACPI_ERROR((AE_INFO,
  295. "Cannot release Mutex [%4.4s], not acquired",
  296. acpi_ut_get_node_name(obj_desc->mutex.node)));
  297. return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
  298. }
  299. /*
  300. * The Mutex is owned, but this thread must be the owner.
  301. * Special case for Global Lock, any thread can release
  302. */
  303. if ((obj_desc->mutex.owner_thread->thread_id !=
  304. walk_state->thread->thread_id)
  305. && (obj_desc != acpi_gbl_global_lock_mutex)) {
  306. ACPI_ERROR((AE_INFO,
  307. "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX",
  308. (unsigned long)walk_state->thread->thread_id,
  309. acpi_ut_get_node_name(obj_desc->mutex.node),
  310. (unsigned long)obj_desc->mutex.owner_thread->
  311. thread_id));
  312. return_ACPI_STATUS(AE_AML_NOT_OWNER);
  313. }
  314. /* Must have a valid thread ID */
  315. if (!walk_state->thread) {
  316. ACPI_ERROR((AE_INFO,
  317. "Cannot release Mutex [%4.4s], null thread info",
  318. acpi_ut_get_node_name(obj_desc->mutex.node)));
  319. return_ACPI_STATUS(AE_AML_INTERNAL);
  320. }
  321. /*
  322. * The sync level of the mutex must be less than or equal to the current
  323. * sync level
  324. */
  325. if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
  326. ACPI_ERROR((AE_INFO,
  327. "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %d current %d",
  328. acpi_ut_get_node_name(obj_desc->mutex.node),
  329. obj_desc->mutex.sync_level,
  330. walk_state->thread->current_sync_level));
  331. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  332. }
  333. status = acpi_ex_release_mutex_object(obj_desc);
  334. if (obj_desc->mutex.acquisition_depth == 0) {
  335. /* Restore the original sync_level */
  336. walk_state->thread->current_sync_level =
  337. obj_desc->mutex.original_sync_level;
  338. }
  339. return_ACPI_STATUS(status);
  340. }
  341. /*******************************************************************************
  342. *
  343. * FUNCTION: acpi_ex_release_all_mutexes
  344. *
  345. * PARAMETERS: Thread - Current executing thread object
  346. *
  347. * RETURN: Status
  348. *
  349. * DESCRIPTION: Release all mutexes held by this thread
  350. *
  351. * NOTE: This function is called as the thread is exiting the interpreter.
  352. * Mutexes are not released when an individual control method is exited, but
  353. * only when the parent thread actually exits the interpreter. This allows one
  354. * method to acquire a mutex, and a different method to release it, as long as
  355. * this is performed underneath a single parent control method.
  356. *
  357. ******************************************************************************/
  358. void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
  359. {
  360. union acpi_operand_object *next = thread->acquired_mutex_list;
  361. union acpi_operand_object *obj_desc;
  362. ACPI_FUNCTION_ENTRY();
  363. /* Traverse the list of owned mutexes, releasing each one */
  364. while (next) {
  365. obj_desc = next;
  366. next = obj_desc->mutex.next;
  367. obj_desc->mutex.prev = NULL;
  368. obj_desc->mutex.next = NULL;
  369. obj_desc->mutex.acquisition_depth = 0;
  370. /* Release the mutex, special case for Global Lock */
  371. if (obj_desc == acpi_gbl_global_lock_mutex) {
  372. /* Ignore errors */
  373. (void)acpi_ev_release_global_lock();
  374. } else {
  375. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  376. }
  377. /* Mark mutex unowned */
  378. obj_desc->mutex.owner_thread = NULL;
  379. obj_desc->mutex.thread_id = NULL;
  380. /* Update Thread sync_level (Last mutex is the important one) */
  381. thread->current_sync_level =
  382. obj_desc->mutex.original_sync_level;
  383. }
  384. }