exmutex.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /******************************************************************************
  2. *
  3. * Module Name: exmutex - ASL Mutex Acquire/Release functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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. /*
  76. * Migrate the previous sync level associated with this mutex to
  77. * the previous mutex on the list so that it may be preserved.
  78. * This handles the case where several mutexes have been acquired
  79. * at the same level, but are not released in opposite order.
  80. */
  81. (obj_desc->mutex.prev)->mutex.original_sync_level =
  82. obj_desc->mutex.original_sync_level;
  83. } else {
  84. thread->acquired_mutex_list = obj_desc->mutex.next;
  85. }
  86. }
  87. /*******************************************************************************
  88. *
  89. * FUNCTION: acpi_ex_link_mutex
  90. *
  91. * PARAMETERS: obj_desc - The mutex to be linked
  92. * thread - Current executing thread object
  93. *
  94. * RETURN: None
  95. *
  96. * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
  97. *
  98. ******************************************************************************/
  99. static void
  100. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  101. struct acpi_thread_state *thread)
  102. {
  103. union acpi_operand_object *list_head;
  104. list_head = thread->acquired_mutex_list;
  105. /* This object will be the first object in the list */
  106. obj_desc->mutex.prev = NULL;
  107. obj_desc->mutex.next = list_head;
  108. /* Update old first object to point back to this object */
  109. if (list_head) {
  110. list_head->mutex.prev = obj_desc;
  111. }
  112. /* Update list head */
  113. thread->acquired_mutex_list = obj_desc;
  114. }
  115. /*******************************************************************************
  116. *
  117. * FUNCTION: acpi_ex_acquire_mutex_object
  118. *
  119. * PARAMETERS: timeout - Timeout in milliseconds
  120. * obj_desc - Mutex object
  121. * thread_id - Current thread state
  122. *
  123. * RETURN: Status
  124. *
  125. * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
  126. * path that supports multiple acquires by the same thread.
  127. *
  128. * MUTEX: Interpreter must be locked
  129. *
  130. * NOTE: This interface is called from three places:
  131. * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
  132. * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
  133. * global lock
  134. * 3) From the external interface, acpi_acquire_global_lock
  135. *
  136. ******************************************************************************/
  137. acpi_status
  138. acpi_ex_acquire_mutex_object(u16 timeout,
  139. union acpi_operand_object *obj_desc,
  140. acpi_thread_id thread_id)
  141. {
  142. acpi_status status;
  143. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
  144. if (!obj_desc) {
  145. return_ACPI_STATUS(AE_BAD_PARAMETER);
  146. }
  147. /* Support for multiple acquires by the owning thread */
  148. if (obj_desc->mutex.thread_id == thread_id) {
  149. /*
  150. * The mutex is already owned by this thread, just increment the
  151. * acquisition depth
  152. */
  153. obj_desc->mutex.acquisition_depth++;
  154. return_ACPI_STATUS(AE_OK);
  155. }
  156. /* Acquire the mutex, wait if necessary. Special case for Global Lock */
  157. if (obj_desc == acpi_gbl_global_lock_mutex) {
  158. status = acpi_ev_acquire_global_lock(timeout);
  159. } else {
  160. status =
  161. acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
  162. timeout);
  163. }
  164. if (ACPI_FAILURE(status)) {
  165. /* Includes failure from a timeout on time_desc */
  166. return_ACPI_STATUS(status);
  167. }
  168. /* Acquired the mutex: update mutex object */
  169. obj_desc->mutex.thread_id = thread_id;
  170. obj_desc->mutex.acquisition_depth = 1;
  171. obj_desc->mutex.original_sync_level = 0;
  172. obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
  173. return_ACPI_STATUS(AE_OK);
  174. }
  175. /*******************************************************************************
  176. *
  177. * FUNCTION: acpi_ex_acquire_mutex
  178. *
  179. * PARAMETERS: time_desc - Timeout integer
  180. * obj_desc - Mutex object
  181. * walk_state - Current method execution state
  182. *
  183. * RETURN: Status
  184. *
  185. * DESCRIPTION: Acquire an AML mutex
  186. *
  187. ******************************************************************************/
  188. acpi_status
  189. acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
  190. union acpi_operand_object *obj_desc,
  191. struct acpi_walk_state *walk_state)
  192. {
  193. acpi_status status;
  194. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
  195. if (!obj_desc) {
  196. return_ACPI_STATUS(AE_BAD_PARAMETER);
  197. }
  198. /* Must have a valid thread state struct */
  199. if (!walk_state->thread) {
  200. ACPI_ERROR((AE_INFO,
  201. "Cannot acquire Mutex [%4.4s], null thread info",
  202. acpi_ut_get_node_name(obj_desc->mutex.node)));
  203. return_ACPI_STATUS(AE_AML_INTERNAL);
  204. }
  205. /*
  206. * Current sync level must be less than or equal to the sync level
  207. * of the mutex. This mechanism provides some deadlock prevention.
  208. */
  209. if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
  210. ACPI_ERROR((AE_INFO,
  211. "Cannot acquire Mutex [%4.4s], "
  212. "current SyncLevel is too large (%u)",
  213. acpi_ut_get_node_name(obj_desc->mutex.node),
  214. walk_state->thread->current_sync_level));
  215. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  216. }
  217. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  218. "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
  219. "Depth %u TID %p\n",
  220. obj_desc->mutex.sync_level,
  221. walk_state->thread->current_sync_level,
  222. obj_desc->mutex.acquisition_depth,
  223. walk_state->thread));
  224. status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value,
  225. obj_desc,
  226. walk_state->thread->thread_id);
  227. if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
  228. /* Save Thread object, original/current sync levels */
  229. obj_desc->mutex.owner_thread = walk_state->thread;
  230. obj_desc->mutex.original_sync_level =
  231. walk_state->thread->current_sync_level;
  232. walk_state->thread->current_sync_level =
  233. obj_desc->mutex.sync_level;
  234. /* Link the mutex to the current thread for force-unlock at method exit */
  235. acpi_ex_link_mutex(obj_desc, walk_state->thread);
  236. }
  237. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  238. "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
  239. obj_desc->mutex.sync_level,
  240. walk_state->thread->current_sync_level,
  241. obj_desc->mutex.acquisition_depth));
  242. return_ACPI_STATUS(status);
  243. }
  244. /*******************************************************************************
  245. *
  246. * FUNCTION: acpi_ex_release_mutex_object
  247. *
  248. * PARAMETERS: obj_desc - The object descriptor for this op
  249. *
  250. * RETURN: Status
  251. *
  252. * DESCRIPTION: Release a previously acquired Mutex, low level interface.
  253. * Provides a common path that supports multiple releases (after
  254. * previous multiple acquires) by the same thread.
  255. *
  256. * MUTEX: Interpreter must be locked
  257. *
  258. * NOTE: This interface is called from three places:
  259. * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
  260. * 2) From acpi_ex_release_global_lock when an AML Field access requires the
  261. * global lock
  262. * 3) From the external interface, acpi_release_global_lock
  263. *
  264. ******************************************************************************/
  265. acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
  266. {
  267. acpi_status status = AE_OK;
  268. ACPI_FUNCTION_TRACE(ex_release_mutex_object);
  269. if (obj_desc->mutex.acquisition_depth == 0) {
  270. return_ACPI_STATUS(AE_NOT_ACQUIRED);
  271. }
  272. /* Match multiple Acquires with multiple Releases */
  273. obj_desc->mutex.acquisition_depth--;
  274. if (obj_desc->mutex.acquisition_depth != 0) {
  275. /* Just decrement the depth and return */
  276. return_ACPI_STATUS(AE_OK);
  277. }
  278. if (obj_desc->mutex.owner_thread) {
  279. /* Unlink the mutex from the owner's list */
  280. acpi_ex_unlink_mutex(obj_desc);
  281. obj_desc->mutex.owner_thread = NULL;
  282. }
  283. /* Release the mutex, special case for Global Lock */
  284. if (obj_desc == acpi_gbl_global_lock_mutex) {
  285. status = acpi_ev_release_global_lock();
  286. } else {
  287. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  288. }
  289. /* Clear mutex info */
  290. obj_desc->mutex.thread_id = 0;
  291. return_ACPI_STATUS(status);
  292. }
  293. /*******************************************************************************
  294. *
  295. * FUNCTION: acpi_ex_release_mutex
  296. *
  297. * PARAMETERS: obj_desc - The object descriptor for this op
  298. * walk_state - Current method execution state
  299. *
  300. * RETURN: Status
  301. *
  302. * DESCRIPTION: Release a previously acquired Mutex.
  303. *
  304. ******************************************************************************/
  305. acpi_status
  306. acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
  307. struct acpi_walk_state *walk_state)
  308. {
  309. u8 previous_sync_level;
  310. struct acpi_thread_state *owner_thread;
  311. acpi_status status = AE_OK;
  312. ACPI_FUNCTION_TRACE(ex_release_mutex);
  313. if (!obj_desc) {
  314. return_ACPI_STATUS(AE_BAD_PARAMETER);
  315. }
  316. owner_thread = obj_desc->mutex.owner_thread;
  317. /* The mutex must have been previously acquired in order to release it */
  318. if (!owner_thread) {
  319. ACPI_ERROR((AE_INFO,
  320. "Cannot release Mutex [%4.4s], not acquired",
  321. acpi_ut_get_node_name(obj_desc->mutex.node)));
  322. return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
  323. }
  324. /* Must have a valid thread ID */
  325. if (!walk_state->thread) {
  326. ACPI_ERROR((AE_INFO,
  327. "Cannot release Mutex [%4.4s], null thread info",
  328. acpi_ut_get_node_name(obj_desc->mutex.node)));
  329. return_ACPI_STATUS(AE_AML_INTERNAL);
  330. }
  331. /*
  332. * The Mutex is owned, but this thread must be the owner.
  333. * Special case for Global Lock, any thread can release
  334. */
  335. if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
  336. (obj_desc != acpi_gbl_global_lock_mutex)) {
  337. ACPI_ERROR((AE_INFO,
  338. "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
  339. (u32)walk_state->thread->thread_id,
  340. acpi_ut_get_node_name(obj_desc->mutex.node),
  341. (u32)owner_thread->thread_id));
  342. return_ACPI_STATUS(AE_AML_NOT_OWNER);
  343. }
  344. /*
  345. * The sync level of the mutex must be equal to the current sync level. In
  346. * other words, the current level means that at least one mutex at that
  347. * level is currently being held. Attempting to release a mutex of a
  348. * different level can only mean that the mutex ordering rule is being
  349. * violated. This behavior is clarified in ACPI 4.0 specification.
  350. */
  351. if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
  352. ACPI_ERROR((AE_INFO,
  353. "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
  354. "mutex %u current %u",
  355. acpi_ut_get_node_name(obj_desc->mutex.node),
  356. obj_desc->mutex.sync_level,
  357. walk_state->thread->current_sync_level));
  358. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  359. }
  360. /*
  361. * Get the previous sync_level from the head of the acquired mutex list.
  362. * This handles the case where several mutexes at the same level have been
  363. * acquired, but are not released in reverse order.
  364. */
  365. previous_sync_level =
  366. owner_thread->acquired_mutex_list->mutex.original_sync_level;
  367. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  368. "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
  369. "Prev SyncLevel %u, Depth %u TID %p\n",
  370. obj_desc->mutex.sync_level,
  371. walk_state->thread->current_sync_level,
  372. previous_sync_level,
  373. obj_desc->mutex.acquisition_depth,
  374. walk_state->thread));
  375. status = acpi_ex_release_mutex_object(obj_desc);
  376. if (ACPI_FAILURE(status)) {
  377. return_ACPI_STATUS(status);
  378. }
  379. if (obj_desc->mutex.acquisition_depth == 0) {
  380. /* Restore the previous sync_level */
  381. owner_thread->current_sync_level = previous_sync_level;
  382. }
  383. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  384. "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
  385. "Prev SyncLevel %u, Depth %u\n",
  386. obj_desc->mutex.sync_level,
  387. walk_state->thread->current_sync_level,
  388. previous_sync_level,
  389. obj_desc->mutex.acquisition_depth));
  390. return_ACPI_STATUS(status);
  391. }
  392. /*******************************************************************************
  393. *
  394. * FUNCTION: acpi_ex_release_all_mutexes
  395. *
  396. * PARAMETERS: thread - Current executing thread object
  397. *
  398. * RETURN: Status
  399. *
  400. * DESCRIPTION: Release all mutexes held by this thread
  401. *
  402. * NOTE: This function is called as the thread is exiting the interpreter.
  403. * Mutexes are not released when an individual control method is exited, but
  404. * only when the parent thread actually exits the interpreter. This allows one
  405. * method to acquire a mutex, and a different method to release it, as long as
  406. * this is performed underneath a single parent control method.
  407. *
  408. ******************************************************************************/
  409. void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
  410. {
  411. union acpi_operand_object *next = thread->acquired_mutex_list;
  412. union acpi_operand_object *obj_desc;
  413. ACPI_FUNCTION_TRACE(ex_release_all_mutexes);
  414. /* Traverse the list of owned mutexes, releasing each one */
  415. while (next) {
  416. obj_desc = next;
  417. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  418. "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
  419. obj_desc->mutex.node->name.ascii,
  420. obj_desc->mutex.sync_level,
  421. obj_desc->mutex.acquisition_depth));
  422. /* Release the mutex, special case for Global Lock */
  423. if (obj_desc == acpi_gbl_global_lock_mutex) {
  424. /* Ignore errors */
  425. (void)acpi_ev_release_global_lock();
  426. } else {
  427. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  428. }
  429. /* Update Thread sync_level (Last mutex is the important one) */
  430. thread->current_sync_level =
  431. obj_desc->mutex.original_sync_level;
  432. /* Mark mutex unowned */
  433. next = obj_desc->mutex.next;
  434. obj_desc->mutex.prev = NULL;
  435. obj_desc->mutex.next = NULL;
  436. obj_desc->mutex.acquisition_depth = 0;
  437. obj_desc->mutex.owner_thread = NULL;
  438. obj_desc->mutex.thread_id = 0;
  439. }
  440. return_VOID;
  441. }