exmutex.c 10 KB

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