1 | /* $Id: spinlock-r0drv-linux.c 13770 2008-11-03 19:18:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-linux-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/spinlock.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/alloc.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Structures and Typedefs *
|
---|
46 | *******************************************************************************/
|
---|
47 | /**
|
---|
48 | * Wrapper for the spinlock_t structure.
|
---|
49 | */
|
---|
50 | typedef struct RTSPINLOCKINTERNAL
|
---|
51 | {
|
---|
52 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
53 | uint32_t volatile u32Magic;
|
---|
54 | /** The linux spinlock structure. */
|
---|
55 | spinlock_t Spinlock;
|
---|
56 | #if !defined(CONFIG_SMP) || defined(RT_ARCH_AMD64)
|
---|
57 | /** A placeholder on Uni systems so we won't piss off RTMemAlloc(). */
|
---|
58 | void *pvUniDummy;
|
---|
59 | #endif
|
---|
60 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
65 | {
|
---|
66 | /*
|
---|
67 | * Allocate.
|
---|
68 | */
|
---|
69 | PRTSPINLOCKINTERNAL pSpinlockInt;
|
---|
70 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
71 | pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
72 | if (!pSpinlockInt)
|
---|
73 | return VERR_NO_MEMORY;
|
---|
74 | /*
|
---|
75 | * Initialize and return.
|
---|
76 | */
|
---|
77 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
78 | spin_lock_init(&pSpinlockInt->Spinlock);
|
---|
79 |
|
---|
80 | *pSpinlock = pSpinlockInt;
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
86 | {
|
---|
87 | /*
|
---|
88 | * Validate input.
|
---|
89 | */
|
---|
90 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
91 | if (!pSpinlockInt)
|
---|
92 | return VERR_INVALID_PARAMETER;
|
---|
93 | if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
|
---|
94 | {
|
---|
95 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
|
---|
96 | return VERR_INVALID_PARAMETER;
|
---|
97 | }
|
---|
98 |
|
---|
99 | ASMAtomicIncU32(&pSpinlockInt->u32Magic);
|
---|
100 | RTMemFree(pSpinlockInt);
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
106 | {
|
---|
107 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
108 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
109 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
110 | NOREF(pSpinlockInt);
|
---|
111 |
|
---|
112 | spin_lock_irqsave(&pSpinlockInt->Spinlock, pTmp->flFlags);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
117 | {
|
---|
118 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
119 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
120 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
121 | NOREF(pSpinlockInt);
|
---|
122 |
|
---|
123 | spin_unlock_irqrestore(&pSpinlockInt->Spinlock, pTmp->flFlags);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
128 | {
|
---|
129 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
130 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
131 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
132 | NOREF(pSpinlockInt); NOREF(pTmp);
|
---|
133 |
|
---|
134 | spin_lock(&pSpinlockInt->Spinlock);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
139 | {
|
---|
140 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
141 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
|
---|
142 | ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
|
---|
143 | NOREF(pSpinlockInt); NOREF(pTmp);
|
---|
144 |
|
---|
145 | spin_unlock(&pSpinlockInt->Spinlock);
|
---|
146 | }
|
---|
147 |
|
---|
148 | #if defined(IN_GUEST_R0) && defined(IN_MODULE)
|
---|
149 | EXPORT_SYMBOL(RTSpinlockCreate);
|
---|
150 | EXPORT_SYMBOL(RTSpinlockDestroy);
|
---|
151 | EXPORT_SYMBOL(RTSpinlockAcquireNoInts);
|
---|
152 | EXPORT_SYMBOL(RTSpinlockReleaseNoInts);
|
---|
153 | #endif
|
---|