xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/executer/exmutex.c (revision 599289f2f3bacad6ed9b4783daf52e17dbe55e47)
1 /******************************************************************************
2  *
3  * Module Name: exmutex - ASL Mutex Acquire/Release functions
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115 
116 #include "acpi.h"
117 #include "accommon.h"
118 #include "acinterp.h"
119 #include "acevents.h"
120 
121 #define _COMPONENT          ACPI_EXECUTER
122         ACPI_MODULE_NAME    ("exmutex")
123 
124 /* Local prototypes */
125 
126 static void
127 AcpiExLinkMutex (
128     ACPI_OPERAND_OBJECT     *ObjDesc,
129     ACPI_THREAD_STATE       *Thread);
130 
131 
132 /*******************************************************************************
133  *
134  * FUNCTION:    AcpiExUnlinkMutex
135  *
136  * PARAMETERS:  ObjDesc             - The mutex to be unlinked
137  *
138  * RETURN:      None
139  *
140  * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
141  *
142  ******************************************************************************/
143 
144 void
145 AcpiExUnlinkMutex (
146     ACPI_OPERAND_OBJECT     *ObjDesc)
147 {
148     ACPI_THREAD_STATE       *Thread = ObjDesc->Mutex.OwnerThread;
149 
150 
151     if (!Thread)
152     {
153         return;
154     }
155 
156     /* Doubly linked list */
157 
158     if (ObjDesc->Mutex.Next)
159     {
160         (ObjDesc->Mutex.Next)->Mutex.Prev = ObjDesc->Mutex.Prev;
161     }
162 
163     if (ObjDesc->Mutex.Prev)
164     {
165         (ObjDesc->Mutex.Prev)->Mutex.Next = ObjDesc->Mutex.Next;
166 
167         /*
168          * Migrate the previous sync level associated with this mutex to
169          * the previous mutex on the list so that it may be preserved.
170          * This handles the case where several mutexes have been acquired
171          * at the same level, but are not released in opposite order.
172          */
173         (ObjDesc->Mutex.Prev)->Mutex.OriginalSyncLevel =
174             ObjDesc->Mutex.OriginalSyncLevel;
175     }
176     else
177     {
178         Thread->AcquiredMutexList = ObjDesc->Mutex.Next;
179     }
180 }
181 
182 
183 /*******************************************************************************
184  *
185  * FUNCTION:    AcpiExLinkMutex
186  *
187  * PARAMETERS:  ObjDesc             - The mutex to be linked
188  *              Thread              - Current executing thread object
189  *
190  * RETURN:      None
191  *
192  * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
193  *
194  ******************************************************************************/
195 
196 static void
197 AcpiExLinkMutex (
198     ACPI_OPERAND_OBJECT     *ObjDesc,
199     ACPI_THREAD_STATE       *Thread)
200 {
201     ACPI_OPERAND_OBJECT     *ListHead;
202 
203 
204     ListHead = Thread->AcquiredMutexList;
205 
206     /* This object will be the first object in the list */
207 
208     ObjDesc->Mutex.Prev = NULL;
209     ObjDesc->Mutex.Next = ListHead;
210 
211     /* Update old first object to point back to this object */
212 
213     if (ListHead)
214     {
215         ListHead->Mutex.Prev = ObjDesc;
216     }
217 
218     /* Update list head */
219 
220     Thread->AcquiredMutexList = ObjDesc;
221 }
222 
223 
224 /*******************************************************************************
225  *
226  * FUNCTION:    AcpiExAcquireMutexObject
227  *
228  * PARAMETERS:  Timeout             - Timeout in milliseconds
229  *              ObjDesc             - Mutex object
230  *              ThreadId            - Current thread state
231  *
232  * RETURN:      Status
233  *
234  * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
235  *              path that supports multiple acquires by the same thread.
236  *
237  * MUTEX:       Interpreter must be locked
238  *
239  * NOTE: This interface is called from three places:
240  * 1) From AcpiExAcquireMutex, via an AML Acquire() operator
241  * 2) From AcpiExAcquireGlobalLock when an AML Field access requires the
242  *    global lock
243  * 3) From the external interface, AcpiAcquireGlobalLock
244  *
245  ******************************************************************************/
246 
247 ACPI_STATUS
248 AcpiExAcquireMutexObject (
249     UINT16                  Timeout,
250     ACPI_OPERAND_OBJECT     *ObjDesc,
251     ACPI_THREAD_ID          ThreadId)
252 {
253     ACPI_STATUS             Status;
254 
255 
256     ACPI_FUNCTION_TRACE_PTR (ExAcquireMutexObject, ObjDesc);
257 
258 
259     if (!ObjDesc)
260     {
261         return_ACPI_STATUS (AE_BAD_PARAMETER);
262     }
263 
264     /* Support for multiple acquires by the owning thread */
265 
266     if (ObjDesc->Mutex.ThreadId == ThreadId)
267     {
268         /*
269          * The mutex is already owned by this thread, just increment the
270          * acquisition depth
271          */
272         ObjDesc->Mutex.AcquisitionDepth++;
273         return_ACPI_STATUS (AE_OK);
274     }
275 
276     /* Acquire the mutex, wait if necessary. Special case for Global Lock */
277 
278     if (ObjDesc == AcpiGbl_GlobalLockMutex)
279     {
280         Status = AcpiEvAcquireGlobalLock (Timeout);
281     }
282     else
283     {
284         Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex,
285                     Timeout);
286     }
287 
288     if (ACPI_FAILURE (Status))
289     {
290         /* Includes failure from a timeout on TimeDesc */
291 
292         return_ACPI_STATUS (Status);
293     }
294 
295     /* Acquired the mutex: update mutex object */
296 
297     ObjDesc->Mutex.ThreadId = ThreadId;
298     ObjDesc->Mutex.AcquisitionDepth = 1;
299     ObjDesc->Mutex.OriginalSyncLevel = 0;
300     ObjDesc->Mutex.OwnerThread = NULL;      /* Used only for AML Acquire() */
301 
302     return_ACPI_STATUS (AE_OK);
303 }
304 
305 
306 /*******************************************************************************
307  *
308  * FUNCTION:    AcpiExAcquireMutex
309  *
310  * PARAMETERS:  TimeDesc            - Timeout integer
311  *              ObjDesc             - Mutex object
312  *              WalkState           - Current method execution state
313  *
314  * RETURN:      Status
315  *
316  * DESCRIPTION: Acquire an AML mutex
317  *
318  ******************************************************************************/
319 
320 ACPI_STATUS
321 AcpiExAcquireMutex (
322     ACPI_OPERAND_OBJECT     *TimeDesc,
323     ACPI_OPERAND_OBJECT     *ObjDesc,
324     ACPI_WALK_STATE         *WalkState)
325 {
326     ACPI_STATUS             Status;
327 
328 
329     ACPI_FUNCTION_TRACE_PTR (ExAcquireMutex, ObjDesc);
330 
331 
332     if (!ObjDesc)
333     {
334         return_ACPI_STATUS (AE_BAD_PARAMETER);
335     }
336 
337     /* Must have a valid thread state struct */
338 
339     if (!WalkState->Thread)
340     {
341         ACPI_ERROR ((AE_INFO,
342             "Cannot acquire Mutex [%4.4s], null thread info",
343             AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
344         return_ACPI_STATUS (AE_AML_INTERNAL);
345     }
346 
347     /*
348      * Current sync level must be less than or equal to the sync level of the
349      * mutex. This mechanism provides some deadlock prevention
350      */
351     if (WalkState->Thread->CurrentSyncLevel > ObjDesc->Mutex.SyncLevel)
352     {
353         ACPI_ERROR ((AE_INFO,
354             "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%u)",
355             AcpiUtGetNodeName (ObjDesc->Mutex.Node),
356             WalkState->Thread->CurrentSyncLevel));
357         return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
358     }
359 
360     Status = AcpiExAcquireMutexObject ((UINT16) TimeDesc->Integer.Value,
361                 ObjDesc, WalkState->Thread->ThreadId);
362     if (ACPI_SUCCESS (Status) && ObjDesc->Mutex.AcquisitionDepth == 1)
363     {
364         /* Save Thread object, original/current sync levels */
365 
366         ObjDesc->Mutex.OwnerThread = WalkState->Thread;
367         ObjDesc->Mutex.OriginalSyncLevel = WalkState->Thread->CurrentSyncLevel;
368         WalkState->Thread->CurrentSyncLevel = ObjDesc->Mutex.SyncLevel;
369 
370         /* Link the mutex to the current thread for force-unlock at method exit */
371 
372         AcpiExLinkMutex (ObjDesc, WalkState->Thread);
373     }
374 
375     return_ACPI_STATUS (Status);
376 }
377 
378 
379 /*******************************************************************************
380  *
381  * FUNCTION:    AcpiExReleaseMutexObject
382  *
383  * PARAMETERS:  ObjDesc             - The object descriptor for this op
384  *
385  * RETURN:      Status
386  *
387  * DESCRIPTION: Release a previously acquired Mutex, low level interface.
388  *              Provides a common path that supports multiple releases (after
389  *              previous multiple acquires) by the same thread.
390  *
391  * MUTEX:       Interpreter must be locked
392  *
393  * NOTE: This interface is called from three places:
394  * 1) From AcpiExReleaseMutex, via an AML Acquire() operator
395  * 2) From AcpiExReleaseGlobalLock when an AML Field access requires the
396  *    global lock
397  * 3) From the external interface, AcpiReleaseGlobalLock
398  *
399  ******************************************************************************/
400 
401 ACPI_STATUS
402 AcpiExReleaseMutexObject (
403     ACPI_OPERAND_OBJECT     *ObjDesc)
404 {
405     ACPI_STATUS             Status = AE_OK;
406 
407 
408     ACPI_FUNCTION_TRACE (ExReleaseMutexObject);
409 
410 
411     if (ObjDesc->Mutex.AcquisitionDepth == 0)
412     {
413         return_ACPI_STATUS (AE_NOT_ACQUIRED);
414     }
415 
416     /* Match multiple Acquires with multiple Releases */
417 
418     ObjDesc->Mutex.AcquisitionDepth--;
419     if (ObjDesc->Mutex.AcquisitionDepth != 0)
420     {
421         /* Just decrement the depth and return */
422 
423         return_ACPI_STATUS (AE_OK);
424     }
425 
426     if (ObjDesc->Mutex.OwnerThread)
427     {
428         /* Unlink the mutex from the owner's list */
429 
430         AcpiExUnlinkMutex (ObjDesc);
431         ObjDesc->Mutex.OwnerThread = NULL;
432     }
433 
434     /* Release the mutex, special case for Global Lock */
435 
436     if (ObjDesc == AcpiGbl_GlobalLockMutex)
437     {
438         Status = AcpiEvReleaseGlobalLock ();
439     }
440     else
441     {
442         AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
443     }
444 
445     /* Clear mutex info */
446 
447     ObjDesc->Mutex.ThreadId = 0;
448     return_ACPI_STATUS (Status);
449 }
450 
451 
452 /*******************************************************************************
453  *
454  * FUNCTION:    AcpiExReleaseMutex
455  *
456  * PARAMETERS:  ObjDesc             - The object descriptor for this op
457  *              WalkState           - Current method execution state
458  *
459  * RETURN:      Status
460  *
461  * DESCRIPTION: Release a previously acquired Mutex.
462  *
463  ******************************************************************************/
464 
465 ACPI_STATUS
466 AcpiExReleaseMutex (
467     ACPI_OPERAND_OBJECT     *ObjDesc,
468     ACPI_WALK_STATE         *WalkState)
469 {
470     ACPI_STATUS             Status = AE_OK;
471     UINT8                   PreviousSyncLevel;
472     ACPI_THREAD_STATE       *OwnerThread;
473 
474 
475     ACPI_FUNCTION_TRACE (ExReleaseMutex);
476 
477 
478     if (!ObjDesc)
479     {
480         return_ACPI_STATUS (AE_BAD_PARAMETER);
481     }
482 
483     OwnerThread = ObjDesc->Mutex.OwnerThread;
484 
485     /* The mutex must have been previously acquired in order to release it */
486 
487     if (!OwnerThread)
488     {
489         ACPI_ERROR ((AE_INFO,
490             "Cannot release Mutex [%4.4s], not acquired",
491             AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
492         return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
493     }
494 
495     /* Must have a valid thread ID */
496 
497     if (!WalkState->Thread)
498     {
499         ACPI_ERROR ((AE_INFO,
500             "Cannot release Mutex [%4.4s], null thread info",
501             AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
502         return_ACPI_STATUS (AE_AML_INTERNAL);
503     }
504 
505     /*
506      * The Mutex is owned, but this thread must be the owner.
507      * Special case for Global Lock, any thread can release
508      */
509     if ((OwnerThread->ThreadId != WalkState->Thread->ThreadId) &&
510         (ObjDesc != AcpiGbl_GlobalLockMutex))
511     {
512         ACPI_ERROR ((AE_INFO,
513             "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
514             (UINT32) WalkState->Thread->ThreadId,
515             AcpiUtGetNodeName (ObjDesc->Mutex.Node),
516             (UINT32) OwnerThread->ThreadId));
517         return_ACPI_STATUS (AE_AML_NOT_OWNER);
518     }
519 
520     /*
521      * The sync level of the mutex must be equal to the current sync level. In
522      * other words, the current level means that at least one mutex at that
523      * level is currently being held. Attempting to release a mutex of a
524      * different level can only mean that the mutex ordering rule is being
525      * violated. This behavior is clarified in ACPI 4.0 specification.
526      */
527     if (ObjDesc->Mutex.SyncLevel != OwnerThread->CurrentSyncLevel)
528     {
529         ACPI_ERROR ((AE_INFO,
530             "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %u current %u",
531             AcpiUtGetNodeName (ObjDesc->Mutex.Node),
532             ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel));
533         return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
534     }
535 
536     /*
537      * Get the previous SyncLevel from the head of the acquired mutex list.
538      * This handles the case where several mutexes at the same level have been
539      * acquired, but are not released in reverse order.
540      */
541     PreviousSyncLevel =
542         OwnerThread->AcquiredMutexList->Mutex.OriginalSyncLevel;
543 
544     Status = AcpiExReleaseMutexObject (ObjDesc);
545     if (ACPI_FAILURE (Status))
546     {
547         return_ACPI_STATUS (Status);
548     }
549 
550     if (ObjDesc->Mutex.AcquisitionDepth == 0)
551     {
552         /* Restore the previous SyncLevel */
553 
554         OwnerThread->CurrentSyncLevel = PreviousSyncLevel;
555     }
556 
557     return_ACPI_STATUS (Status);
558 }
559 
560 
561 /*******************************************************************************
562  *
563  * FUNCTION:    AcpiExReleaseAllMutexes
564  *
565  * PARAMETERS:  Thread              - Current executing thread object
566  *
567  * RETURN:      Status
568  *
569  * DESCRIPTION: Release all mutexes held by this thread
570  *
571  * NOTE: This function is called as the thread is exiting the interpreter.
572  * Mutexes are not released when an individual control method is exited, but
573  * only when the parent thread actually exits the interpreter. This allows one
574  * method to acquire a mutex, and a different method to release it, as long as
575  * this is performed underneath a single parent control method.
576  *
577  ******************************************************************************/
578 
579 void
580 AcpiExReleaseAllMutexes (
581     ACPI_THREAD_STATE       *Thread)
582 {
583     ACPI_OPERAND_OBJECT     *Next = Thread->AcquiredMutexList;
584     ACPI_OPERAND_OBJECT     *ObjDesc;
585 
586 
587     ACPI_FUNCTION_NAME (ExReleaseAllMutexes);
588 
589 
590     /* Traverse the list of owned mutexes, releasing each one */
591 
592     while (Next)
593     {
594         ObjDesc = Next;
595         Next = ObjDesc->Mutex.Next;
596 
597         ObjDesc->Mutex.Prev = NULL;
598         ObjDesc->Mutex.Next = NULL;
599         ObjDesc->Mutex.AcquisitionDepth = 0;
600 
601         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
602             "Force-releasing held mutex: %p\n", ObjDesc));
603 
604         /* Release the mutex, special case for Global Lock */
605 
606         if (ObjDesc == AcpiGbl_GlobalLockMutex)
607         {
608             /* Ignore errors */
609 
610             (void) AcpiEvReleaseGlobalLock ();
611         }
612         else
613         {
614             AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
615         }
616 
617         /* Mark mutex unowned */
618 
619         ObjDesc->Mutex.OwnerThread = NULL;
620         ObjDesc->Mutex.ThreadId = 0;
621 
622         /* Update Thread SyncLevel (Last mutex is the important one) */
623 
624         Thread->CurrentSyncLevel = ObjDesc->Mutex.OriginalSyncLevel;
625     }
626 }
627