VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/threadpreempt-r0drv-darwin.cpp@ 37085

Last change on this file since 37085 was 37055, checked in by vboxsync, 14 years ago

threadpreempt-r0drv-darwin.cpp: g_aPreemptHacks is too small for snow leopard. Forget about saving heap, just do a full IPRT CPU set.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette