1 | /* $Id: threadpreempt-r0drv-darwin.cpp 22150 2009-08-11 09:41:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Thread Preemption, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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-darwin-kernel.h"
|
---|
36 | #include "internal/iprt.h"
|
---|
37 | #include <iprt/thread.h>
|
---|
38 |
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/mp.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | typedef struct RTDARWINPREEMPTHACK
|
---|
49 | {
|
---|
50 | /** The spinlock we exploit for disabling preemption. */
|
---|
51 | lck_spin_t *pSpinLock;
|
---|
52 | /** The preemption count for this CPU, to guard against nested calls. */
|
---|
53 | uint32_t cRecursion;
|
---|
54 | } RTDARWINPREEMPTHACK;
|
---|
55 | typedef RTDARWINPREEMPTHACK *PRTDARWINPREEMPTHACK;
|
---|
56 |
|
---|
57 |
|
---|
58 | /*******************************************************************************
|
---|
59 | * Global Variables *
|
---|
60 | *******************************************************************************/
|
---|
61 | static RTDARWINPREEMPTHACK g_aPreemptHacks[16]; /* see MAX_CPUS in i386/mp.h */
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Allocates the per-cpu spin locks used to disable preemption.
|
---|
66 | *
|
---|
67 | * Called by rtR0InitNative.
|
---|
68 | */
|
---|
69 | int rtThreadPreemptDarwinInit(void)
|
---|
70 | {
|
---|
71 | Assert(g_pDarwinLockGroup);
|
---|
72 |
|
---|
73 | for (size_t i = 0; i < RT_ELEMENTS(g_aPreemptHacks); i++)
|
---|
74 | {
|
---|
75 | g_aPreemptHacks[i].pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
76 | if (!g_aPreemptHacks[i].pSpinLock)
|
---|
77 | return VERR_NO_MEMORY; /* (The caller will invoke rtThreadPreemptDarwinTerm) */
|
---|
78 | }
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Frees the per-cpu spin locks used to disable preemption.
|
---|
85 | *
|
---|
86 | * Called by rtR0TermNative.
|
---|
87 | */
|
---|
88 | void rtThreadPreemptDarwinTerm(void)
|
---|
89 | {
|
---|
90 | for (size_t i = 0; i < RT_ELEMENTS(g_aPreemptHacks); i++)
|
---|
91 | if (g_aPreemptHacks[i].pSpinLock)
|
---|
92 | {
|
---|
93 | lck_spin_free(g_aPreemptHacks[i].pSpinLock, g_pDarwinLockGroup);
|
---|
94 | g_aPreemptHacks[i].pSpinLock = NULL;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
100 | {
|
---|
101 | Assert(hThread == NIL_RTTHREAD);
|
---|
102 | return preemption_enabled();
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
107 | {
|
---|
108 | Assert(hThread == NIL_RTTHREAD);
|
---|
109 |
|
---|
110 | /* HACK ALERT! This ASSUMES that the cpu_pending_ast member of cpu_data_t doesn't move. */
|
---|
111 | uint32_t ast_pending;
|
---|
112 | #if 1
|
---|
113 | __asm__ volatile("movl %%gs:%P1,%0\n\t"
|
---|
114 | : "=r" (ast_pending)
|
---|
115 | : "i" (7*sizeof(void*) + 7*sizeof(int)));
|
---|
116 | #else
|
---|
117 | cpu_data_t *pCpu = current_cpu_datap(void);
|
---|
118 | AssertCompileMemberOffset(cpu_data_t, cpu_pending_ast, 7*sizeof(void*) + 7*sizeof(int));
|
---|
119 | cpu_pending_ast = pCpu->cpu_pending_ast;
|
---|
120 | #endif
|
---|
121 | AssertMsg(!(ast_pending & UINT32_C(0xfffff000)),("%#x\n", ast_pending));
|
---|
122 | return (ast_pending & (AST_PREEMPT | AST_URGENT)) != 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
127 | {
|
---|
128 | /* yes, we think thaat RTThreadPreemptIsPending is reliable... */
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
134 | {
|
---|
135 | /* yes, kernel preemption is possible. */
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
141 | {
|
---|
142 | AssertPtr(pState);
|
---|
143 | Assert(pState->u32Reserved == 0);
|
---|
144 | pState->u32Reserved = 42;
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Disable to prevent preemption while we grab the per-cpu spin lock.
|
---|
148 | * Note! Only take the lock on the first call or we end up spinning for ever.
|
---|
149 | */
|
---|
150 | RTCCUINTREG fSavedFlags = ASMIntDisableFlags();
|
---|
151 | RTCPUID idCpu = RTMpCpuId();
|
---|
152 | if (RT_UNLIKELY(idCpu < RT_ELEMENTS(g_aPreemptHacks)))
|
---|
153 | {
|
---|
154 | Assert(g_aPreemptHacks[idCpu].cRecursion < UINT32_MAX / 2);
|
---|
155 | if (++g_aPreemptHacks[idCpu].cRecursion == 1)
|
---|
156 | {
|
---|
157 | lck_spin_t *pSpinLock = g_aPreemptHacks[idCpu].pSpinLock;
|
---|
158 | if (pSpinLock)
|
---|
159 | lck_spin_lock(pSpinLock);
|
---|
160 | else
|
---|
161 | AssertFailed();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | ASMSetFlags(fSavedFlags);
|
---|
165 | Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
|
---|
166 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
171 | {
|
---|
172 | AssertPtr(pState);
|
---|
173 | Assert(pState->u32Reserved == 42);
|
---|
174 | pState->u32Reserved = 0;
|
---|
175 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
176 |
|
---|
177 | RTCPUID idCpu = RTMpCpuId();
|
---|
178 | if (RT_UNLIKELY(idCpu < RT_ELEMENTS(g_aPreemptHacks)))
|
---|
179 | {
|
---|
180 | Assert(g_aPreemptHacks[idCpu].cRecursion > 0);
|
---|
181 | if (--g_aPreemptHacks[idCpu].cRecursion == 0)
|
---|
182 | {
|
---|
183 | lck_spin_t *pSpinLock = g_aPreemptHacks[idCpu].pSpinLock;
|
---|
184 | if (pSpinLock)
|
---|
185 | lck_spin_unlock(pSpinLock);
|
---|
186 | else
|
---|
187 | AssertFailed();
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
194 | {
|
---|
195 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
196 | /** @todo Darwin: Implement RTThreadIsInInterrupt. Required for guest
|
---|
197 | * additions! */
|
---|
198 | return !ASMIntAreEnabled();
|
---|
199 | }
|
---|
200 |
|
---|