VirtualBox

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

最後變更 在這個檔案從28800是 28800,由 vboxsync 提交於 15 年 前

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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