exmutex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /******************************************************************************
  2. *
  3. * Module Name: exmutex - ASL Mutex Acquire/Release functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, 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/acinterp.h>
  44. #define _COMPONENT ACPI_EXECUTER
  45. ACPI_MODULE_NAME ("exmutex")
  46. /* Local prototypes */
  47. static void
  48. acpi_ex_link_mutex (
  49. union acpi_operand_object *obj_desc,
  50. struct acpi_thread_state *thread);
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ex_unlink_mutex
  54. *
  55. * PARAMETERS: obj_desc - The mutex to be unlinked
  56. *
  57. * RETURN: None
  58. *
  59. * DESCRIPTION: Remove a mutex from the "acquired_mutex" list
  60. *
  61. ******************************************************************************/
  62. void
  63. acpi_ex_unlink_mutex (
  64. union acpi_operand_object *obj_desc)
  65. {
  66. struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
  67. if (!thread) {
  68. return;
  69. }
  70. /* Doubly linked list */
  71. if (obj_desc->mutex.next) {
  72. (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
  73. }
  74. if (obj_desc->mutex.prev) {
  75. (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
  76. }
  77. else {
  78. thread->acquired_mutex_list = obj_desc->mutex.next;
  79. }
  80. }
  81. /*******************************************************************************
  82. *
  83. * FUNCTION: acpi_ex_link_mutex
  84. *
  85. * PARAMETERS: obj_desc - The mutex to be linked
  86. * Thread - Current executing thread object
  87. *
  88. * RETURN: None
  89. *
  90. * DESCRIPTION: Add a mutex to the "acquired_mutex" list for this walk
  91. *
  92. ******************************************************************************/
  93. static void
  94. acpi_ex_link_mutex (
  95. union acpi_operand_object *obj_desc,
  96. struct acpi_thread_state *thread)
  97. {
  98. union acpi_operand_object *list_head;
  99. list_head = thread->acquired_mutex_list;
  100. /* This object will be the first object in the list */
  101. obj_desc->mutex.prev = NULL;
  102. obj_desc->mutex.next = list_head;
  103. /* Update old first object to point back to this object */
  104. if (list_head) {
  105. list_head->mutex.prev = obj_desc;
  106. }
  107. /* Update list head */
  108. thread->acquired_mutex_list = obj_desc;
  109. }
  110. /*******************************************************************************
  111. *
  112. * FUNCTION: acpi_ex_acquire_mutex
  113. *
  114. * PARAMETERS: time_desc - Timeout integer
  115. * obj_desc - Mutex object
  116. * walk_state - Current method execution state
  117. *
  118. * RETURN: Status
  119. *
  120. * DESCRIPTION: Acquire an AML mutex
  121. *
  122. ******************************************************************************/
  123. acpi_status
  124. acpi_ex_acquire_mutex (
  125. union acpi_operand_object *time_desc,
  126. union acpi_operand_object *obj_desc,
  127. struct acpi_walk_state *walk_state)
  128. {
  129. acpi_status status;
  130. ACPI_FUNCTION_TRACE_PTR ("ex_acquire_mutex", obj_desc);
  131. if (!obj_desc) {
  132. return_ACPI_STATUS (AE_BAD_PARAMETER);
  133. }
  134. /* Sanity check -- we must have a valid thread ID */
  135. if (!walk_state->thread) {
  136. ACPI_REPORT_ERROR (("Cannot acquire Mutex [%4.4s], null thread info\n",
  137. acpi_ut_get_node_name (obj_desc->mutex.node)));
  138. return_ACPI_STATUS (AE_AML_INTERNAL);
  139. }
  140. /*
  141. * Current Sync must be less than or equal to the sync level of the
  142. * mutex. This mechanism provides some deadlock prevention
  143. */
  144. if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
  145. ACPI_REPORT_ERROR ((
  146. "Cannot acquire Mutex [%4.4s], incorrect sync_level\n",
  147. acpi_ut_get_node_name (obj_desc->mutex.node)));
  148. return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
  149. }
  150. /* Support for multiple acquires by the owning thread */
  151. if (obj_desc->mutex.owner_thread) {
  152. /* Special case for Global Lock, allow all threads */
  153. if ((obj_desc->mutex.owner_thread->thread_id ==
  154. walk_state->thread->thread_id) ||
  155. (obj_desc->mutex.semaphore ==
  156. acpi_gbl_global_lock_semaphore)) {
  157. /*
  158. * The mutex is already owned by this thread,
  159. * just increment the acquisition depth
  160. */
  161. obj_desc->mutex.acquisition_depth++;
  162. return_ACPI_STATUS (AE_OK);
  163. }
  164. }
  165. /* Acquire the mutex, wait if necessary */
  166. status = acpi_ex_system_acquire_mutex (time_desc, obj_desc);
  167. if (ACPI_FAILURE (status)) {
  168. /* Includes failure from a timeout on time_desc */
  169. return_ACPI_STATUS (status);
  170. }
  171. /* Have the mutex: update mutex and walk info and save the sync_level */
  172. obj_desc->mutex.owner_thread = walk_state->thread;
  173. obj_desc->mutex.acquisition_depth = 1;
  174. obj_desc->mutex.original_sync_level = walk_state->thread->current_sync_level;
  175. walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;
  176. /* Link the mutex to the current thread for force-unlock at method exit */
  177. acpi_ex_link_mutex (obj_desc, walk_state->thread);
  178. return_ACPI_STATUS (AE_OK);
  179. }
  180. /*******************************************************************************
  181. *
  182. * FUNCTION: acpi_ex_release_mutex
  183. *
  184. * PARAMETERS: obj_desc - The object descriptor for this op
  185. * walk_state - Current method execution state
  186. *
  187. * RETURN: Status
  188. *
  189. * DESCRIPTION: Release a previously acquired Mutex.
  190. *
  191. ******************************************************************************/
  192. acpi_status
  193. acpi_ex_release_mutex (
  194. union acpi_operand_object *obj_desc,
  195. struct acpi_walk_state *walk_state)
  196. {
  197. acpi_status status;
  198. ACPI_FUNCTION_TRACE ("ex_release_mutex");
  199. if (!obj_desc) {
  200. return_ACPI_STATUS (AE_BAD_PARAMETER);
  201. }
  202. /* The mutex must have been previously acquired in order to release it */
  203. if (!obj_desc->mutex.owner_thread) {
  204. ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], not acquired\n",
  205. acpi_ut_get_node_name (obj_desc->mutex.node)));
  206. return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
  207. }
  208. /* Sanity check -- we must have a valid thread ID */
  209. if (!walk_state->thread) {
  210. ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], null thread info\n",
  211. acpi_ut_get_node_name (obj_desc->mutex.node)));
  212. return_ACPI_STATUS (AE_AML_INTERNAL);
  213. }
  214. /*
  215. * The Mutex is owned, but this thread must be the owner.
  216. * Special case for Global Lock, any thread can release
  217. */
  218. if ((obj_desc->mutex.owner_thread->thread_id != walk_state->thread->thread_id) &&
  219. (obj_desc->mutex.semaphore != acpi_gbl_global_lock_semaphore)) {
  220. ACPI_REPORT_ERROR ((
  221. "Thread %X cannot release Mutex [%4.4s] acquired by thread %X\n",
  222. walk_state->thread->thread_id,
  223. acpi_ut_get_node_name (obj_desc->mutex.node),
  224. obj_desc->mutex.owner_thread->thread_id));
  225. return_ACPI_STATUS (AE_AML_NOT_OWNER);
  226. }
  227. /*
  228. * The sync level of the mutex must be less than or
  229. * equal to the current sync level
  230. */
  231. if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
  232. ACPI_REPORT_ERROR ((
  233. "Cannot release Mutex [%4.4s], incorrect sync_level\n",
  234. acpi_ut_get_node_name (obj_desc->mutex.node)));
  235. return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
  236. }
  237. /* Match multiple Acquires with multiple Releases */
  238. obj_desc->mutex.acquisition_depth--;
  239. if (obj_desc->mutex.acquisition_depth != 0) {
  240. /* Just decrement the depth and return */
  241. return_ACPI_STATUS (AE_OK);
  242. }
  243. /* Unlink the mutex from the owner's list */
  244. acpi_ex_unlink_mutex (obj_desc);
  245. /* Release the mutex */
  246. status = acpi_ex_system_release_mutex (obj_desc);
  247. /* Update the mutex and walk state, restore sync_level before acquire */
  248. obj_desc->mutex.owner_thread = NULL;
  249. walk_state->thread->current_sync_level = obj_desc->mutex.original_sync_level;
  250. return_ACPI_STATUS (status);
  251. }
  252. /*******************************************************************************
  253. *
  254. * FUNCTION: acpi_ex_release_all_mutexes
  255. *
  256. * PARAMETERS: Thread - Current executing thread object
  257. *
  258. * RETURN: Status
  259. *
  260. * DESCRIPTION: Release all mutexes held by this thread
  261. *
  262. ******************************************************************************/
  263. void
  264. acpi_ex_release_all_mutexes (
  265. struct acpi_thread_state *thread)
  266. {
  267. union acpi_operand_object *next = thread->acquired_mutex_list;
  268. union acpi_operand_object *this;
  269. acpi_status status;
  270. ACPI_FUNCTION_ENTRY ();
  271. /* Traverse the list of owned mutexes, releasing each one */
  272. while (next) {
  273. this = next;
  274. next = this->mutex.next;
  275. this->mutex.acquisition_depth = 1;
  276. this->mutex.prev = NULL;
  277. this->mutex.next = NULL;
  278. /* Release the mutex */
  279. status = acpi_ex_system_release_mutex (this);
  280. if (ACPI_FAILURE (status)) {
  281. continue;
  282. }
  283. /* Mark mutex unowned */
  284. this->mutex.owner_thread = NULL;
  285. /* Update Thread sync_level (Last mutex is the important one) */
  286. thread->current_sync_level = this->mutex.original_sync_level;
  287. }
  288. }