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