1 | /* $Id: thread-r0drv-freebsd.c 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2016 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-freebsd-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/thread.h>
|
---|
34 |
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/asm-amd64-x86.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/mp.h>
|
---|
40 | #include "internal/thread.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
44 | {
|
---|
45 | return (RTNATIVETHREAD)curthread;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | static int rtR0ThreadFbsdSleepCommon(RTMSINTERVAL cMillies)
|
---|
50 | {
|
---|
51 | int rc;
|
---|
52 | int cTicks;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * 0 ms sleep -> yield.
|
---|
56 | */
|
---|
57 | if (!cMillies)
|
---|
58 | {
|
---|
59 | RTThreadYield();
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Translate milliseconds into ticks and go to sleep.
|
---|
65 | */
|
---|
66 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
67 | {
|
---|
68 | if (hz == 1000)
|
---|
69 | cTicks = cMillies;
|
---|
70 | else if (hz == 100)
|
---|
71 | cTicks = cMillies / 10;
|
---|
72 | else
|
---|
73 | {
|
---|
74 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
75 | cTicks = (int)cTicks64;
|
---|
76 | if (cTicks != cTicks64)
|
---|
77 | cTicks = INT_MAX;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else
|
---|
81 | cTicks = 0; /* requires giant lock! */
|
---|
82 |
|
---|
83 | rc = tsleep((void *)RTThreadSleep,
|
---|
84 | PZERO | PCATCH,
|
---|
85 | "iprtsl", /* max 6 chars */
|
---|
86 | cTicks);
|
---|
87 | switch (rc)
|
---|
88 | {
|
---|
89 | case 0:
|
---|
90 | return VINF_SUCCESS;
|
---|
91 | case EWOULDBLOCK:
|
---|
92 | return VERR_TIMEOUT;
|
---|
93 | case EINTR:
|
---|
94 | case ERESTART:
|
---|
95 | return VERR_INTERRUPTED;
|
---|
96 | default:
|
---|
97 | AssertMsgFailed(("%d\n", rc));
|
---|
98 | return VERR_NO_TRANSLATION;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
104 | {
|
---|
105 | return rtR0ThreadFbsdSleepCommon(cMillies);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
|
---|
110 | {
|
---|
111 | return rtR0ThreadFbsdSleepCommon(cMillies);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(bool) RTThreadYield(void)
|
---|
116 | {
|
---|
117 | #if __FreeBSD_version >= 900032
|
---|
118 | kern_yield(curthread->td_user_pri);
|
---|
119 | #else
|
---|
120 | uio_yield();
|
---|
121 | #endif
|
---|
122 | return false; /** @todo figure this one ... */
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
127 | {
|
---|
128 | Assert(hThread == NIL_RTTHREAD);
|
---|
129 |
|
---|
130 | return curthread->td_critnest == 0
|
---|
131 | && ASMIntAreEnabled(); /** @todo is there a native freebsd function/macro for this? */
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
136 | {
|
---|
137 | Assert(hThread == NIL_RTTHREAD);
|
---|
138 |
|
---|
139 | return curthread->td_owepreempt == 1;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
144 | {
|
---|
145 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
151 | {
|
---|
152 | /* yes, kernel preemption is possible. */
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
158 | {
|
---|
159 | AssertPtr(pState);
|
---|
160 | Assert(pState->u32Reserved == 0);
|
---|
161 | pState->u32Reserved = 42;
|
---|
162 |
|
---|
163 | critical_enter();
|
---|
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 |
|
---|
174 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
175 | critical_exit();
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
180 | {
|
---|
181 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
182 | /** @todo FreeBSD: Implement RTThreadIsInInterrupt. Required for guest
|
---|
183 | * additions! */
|
---|
184 | return !ASMIntAreEnabled();
|
---|
185 | }
|
---|
186 |
|
---|