VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/freebsd/SUPLib-freebsd.cpp@ 19281

Last change on this file since 19281 was 13865, checked in by vboxsync, 16 years ago

SUP: pass idCpu up on all platforms (save OS/2), handle it in ring-0 on solaris, linux and freebsd.

File size: 5.2 KB
Line 
1/* $Id: $ */
2/** @file
3 * VirtualBox Support Library - FreeBSD specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#define LOG_GROUP LOG_GROUP_SUP
35#ifdef IN_SUP_HARDENED_R3
36# undef DEBUG /* Warning: disables RT_STRICT */
37# define LOG_DISABLED
38 /** @todo RTLOGREL_DISABLED */
39# include <iprt/log.h>
40# undef LogRelIt
41# define LogRelIt(pvInst, fFlags, iGroup, fmtargs) do { } while (0)
42#endif
43
44#include <VBox/types.h>
45#include <VBox/sup.h>
46#include <VBox/param.h>
47#include <VBox/err.h>
48#include <VBox/log.h>
49#include <iprt/path.h>
50#include <iprt/assert.h>
51#include <iprt/mem.h>
52#include <iprt/err.h>
53#include <iprt/string.h>
54#include "../SUPLibInternal.h"
55#include "../SUPDrvIOC.h"
56
57#include <sys/fcntl.h>
58#include <sys/ioctl.h>
59#include <errno.h>
60#include <unistd.h>
61#include <stdlib.h>
62
63
64/*******************************************************************************
65* Defined Constants And Macros *
66*******************************************************************************/
67/** FreeBSD base device name. */
68#define DEVICE_NAME "/dev/vboxdrv"
69
70
71
72int suplibOsInit(PSUPLIBDATA pThis, bool fPreInited)
73{
74 /*
75 * Nothing to do if pre-inited.
76 */
77 if (fPreInited)
78 return VINF_SUCCESS;
79
80 /*
81 * Try open the BSD device.
82 */
83 int hDevice = -1;
84 char szDevice[sizeof(DEVICE_NAME) + 16];
85 for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
86 {
87 errno = 0;
88 RTStrPrintf(szDevice, sizeof(szDevice), DEVICE_NAME "%d", iUnit);
89 hDevice = open(szDevice, O_RDWR, 0);
90 if (hDevice >= 0 || errno != EBUSY)
91 break;
92 }
93 if (hDevice < 0)
94 {
95 int rc;
96 switch (errno)
97 {
98 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
99 case EPERM:
100 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
101 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
102 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
103 }
104 LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", szDevice, errno, rc));
105 return rc;
106 }
107
108 /*
109 * Mark the file handle close on exec.
110 */
111 if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
112 {
113#ifdef IN_SUP_HARDENED_R3
114 int rc = VERR_INTERNAL_ERROR;
115#else
116 int err = errno;
117 int rc = RTErrConvertFromErrno(err);
118 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
119#endif
120 close(hDevice);
121 return rc;
122 }
123
124 /*
125 * We're done.
126 */
127 pThis->hDevice = hDevice;
128 return VINF_SUCCESS;
129}
130
131
132#ifndef IN_SUP_HARDENED_R3
133
134int suplibOsTerm(PSUPLIBDATA pThis)
135{
136 /*
137 * Check if we're initited at all.
138 */
139 if (pThis->hDevice != NIL_RTFILE)
140 {
141 if (close(pThis->hDevice))
142 AssertFailed();
143 pThis->hDevice = NIL_RTFILE;
144 }
145 return VINF_SUCCESS;
146}
147
148
149int suplibOsInstall(void)
150{
151 return VERR_NOT_IMPLEMENTED;
152}
153
154
155int suplibOsUninstall(void)
156{
157 return VERR_NOT_IMPLEMENTED;
158}
159
160
161int suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
162{
163 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
164 return VINF_SUCCESS;
165 return RTErrConvertFromErrno(errno);
166}
167
168
169int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
170{
171 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
172 if (rc == -1)
173 rc = errno;
174 return rc;
175}
176
177
178int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
179{
180 NOREF(pThis);
181 *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
182 if (*ppvPages)
183 return VINF_SUCCESS;
184 return RTErrConvertFromErrno(errno);
185}
186
187
188int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t /* cPages */)
189{
190 NOREF(pThis);
191 RTMemPageFree(pvPages);
192 return VINF_SUCCESS;
193}
194
195#endif /* !IN_SUP_HARDENED_R3 */
196
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