1 | /* $Id: thread-r0drv-freebsd.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 1), Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-freebsd-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 | #include <iprt/thread.h>
|
---|
44 |
|
---|
45 | #include <iprt/asm.h>
|
---|
46 | #include <iprt/asm-amd64-x86.h>
|
---|
47 | #include <iprt/assert.h>
|
---|
48 | #include <iprt/err.h>
|
---|
49 | #include <iprt/mp.h>
|
---|
50 | #include "internal/thread.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
54 | {
|
---|
55 | return (RTNATIVETHREAD)curthread;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | static int rtR0ThreadFbsdSleepCommon(RTMSINTERVAL cMillies)
|
---|
60 | {
|
---|
61 | int rc;
|
---|
62 | int cTicks;
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * 0 ms sleep -> yield.
|
---|
66 | */
|
---|
67 | if (!cMillies)
|
---|
68 | {
|
---|
69 | RTThreadYield();
|
---|
70 | return VINF_SUCCESS;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Translate milliseconds into ticks and go to sleep.
|
---|
75 | */
|
---|
76 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
77 | {
|
---|
78 | if (hz == 1000)
|
---|
79 | cTicks = cMillies;
|
---|
80 | else if (hz == 100)
|
---|
81 | cTicks = cMillies / 10;
|
---|
82 | else
|
---|
83 | {
|
---|
84 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
85 | cTicks = (int)cTicks64;
|
---|
86 | if (cTicks != cTicks64)
|
---|
87 | cTicks = INT_MAX;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | else
|
---|
91 | cTicks = 0; /* requires giant lock! */
|
---|
92 |
|
---|
93 | rc = tsleep((void *)RTThreadSleep,
|
---|
94 | PZERO | PCATCH,
|
---|
95 | "iprtsl", /* max 6 chars */
|
---|
96 | cTicks);
|
---|
97 | switch (rc)
|
---|
98 | {
|
---|
99 | case 0:
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | case EWOULDBLOCK:
|
---|
102 | return VERR_TIMEOUT;
|
---|
103 | case EINTR:
|
---|
104 | case ERESTART:
|
---|
105 | return VERR_INTERRUPTED;
|
---|
106 | default:
|
---|
107 | AssertMsgFailed(("%d\n", rc));
|
---|
108 | return VERR_NO_TRANSLATION;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
114 | {
|
---|
115 | return rtR0ThreadFbsdSleepCommon(cMillies);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
|
---|
120 | {
|
---|
121 | return rtR0ThreadFbsdSleepCommon(cMillies);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(bool) RTThreadYield(void)
|
---|
126 | {
|
---|
127 | #if __FreeBSD_version >= 900032
|
---|
128 | kern_yield(curthread->td_user_pri);
|
---|
129 | #else
|
---|
130 | uio_yield();
|
---|
131 | #endif
|
---|
132 | return false; /** @todo figure this one ... */
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
137 | {
|
---|
138 | Assert(hThread == NIL_RTTHREAD);
|
---|
139 |
|
---|
140 | return curthread->td_critnest == 0
|
---|
141 | && ASMIntAreEnabled(); /** @todo is there a native freebsd function/macro for this? */
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
146 | {
|
---|
147 | Assert(hThread == NIL_RTTHREAD);
|
---|
148 |
|
---|
149 | return curthread->td_owepreempt == 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
|
---|
154 | {
|
---|
155 | /* yes, RTThreadPreemptIsPending is reliable. */
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTDECL(bool) RTThreadPreemptIsPossible(void)
|
---|
161 | {
|
---|
162 | /* yes, kernel preemption is possible. */
|
---|
163 | return true;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
168 | {
|
---|
169 | AssertPtr(pState);
|
---|
170 | Assert(pState->u32Reserved == 0);
|
---|
171 | pState->u32Reserved = 42;
|
---|
172 |
|
---|
173 | critical_enter();
|
---|
174 | RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
179 | {
|
---|
180 | AssertPtr(pState);
|
---|
181 | Assert(pState->u32Reserved == 42);
|
---|
182 | pState->u32Reserved = 0;
|
---|
183 |
|
---|
184 | RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
|
---|
185 | critical_exit();
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
|
---|
190 | {
|
---|
191 | Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
|
---|
192 | /** @todo FreeBSD: Implement RTThreadIsInInterrupt. Required for guest
|
---|
193 | * additions! */
|
---|
194 | return !ASMIntAreEnabled();
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | RTDECL(int) RTThreadQueryTerminationStatus(RTTHREAD hThread)
|
---|
199 | {
|
---|
200 | RT_NOREF(hThread);
|
---|
201 | /** @todo implement. */
|
---|
202 | return VERR_NOT_SUPPORTED;
|
---|
203 | }
|
---|
204 |
|
---|