1 | /** @file
|
---|
2 | * SUPLib - FreeBSD Hosts,
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_SUP
|
---|
22 | #include <VBox/types.h>
|
---|
23 | #include <VBox/sup.h>
|
---|
24 | #include <VBox/param.h>
|
---|
25 | #include <VBox/err.h>
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <iprt/path.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/mem.h>
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include "SUPLibInternal.h"
|
---|
33 | #include "SUPDRVIOC.h"
|
---|
34 |
|
---|
35 | #include <sys/fcntl.h>
|
---|
36 | #include <sys/ioctl.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <unistd.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Defined Constants And Macros *
|
---|
44 | *******************************************************************************/
|
---|
45 | /** FreeBSD base device name. */
|
---|
46 | #define DEVICE_NAME "/dev/vboxdrv"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Global Variables *
|
---|
51 | *******************************************************************************/
|
---|
52 | /** Handle to the open device. */
|
---|
53 | static int g_hDevice = -1;
|
---|
54 |
|
---|
55 |
|
---|
56 | int suplibOsInit(size_t cbReserve)
|
---|
57 | {
|
---|
58 | /*
|
---|
59 | * Check if already initialized.
|
---|
60 | */
|
---|
61 | if (g_hDevice >= 0)
|
---|
62 | return VINF_SUCCESS;
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Try open the BSD device.
|
---|
66 | */
|
---|
67 | char szDevice[sizeof(DEVICE_NAME) + 16];
|
---|
68 | for (unsigned iUnit = 0; iUnit < 1024; iUnit++)
|
---|
69 | {
|
---|
70 | errno = 0;
|
---|
71 | RTStrPrintf(szDevice, sizeof(szDevice), DEVICE_NAME "%d", iUnit);
|
---|
72 | g_hDevice = open(szDevice, O_RDWR, 0);
|
---|
73 | if (g_hDevice >= 0 || errno != EBUSY)
|
---|
74 | break;
|
---|
75 | }
|
---|
76 | if (g_hDevice < 0)
|
---|
77 | {
|
---|
78 | int rc;
|
---|
79 | switch (errno)
|
---|
80 | {
|
---|
81 | case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
|
---|
82 | case EPERM:
|
---|
83 | case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
|
---|
84 | case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
|
---|
85 | default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
|
---|
86 | }
|
---|
87 | LogRel(("Failed to open \"%s\", errno=%d, rc=%Vrc\n", szDevice, errno, rc));
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Mark the file handle close on exec.
|
---|
93 | */
|
---|
94 | if (fcntl(g_hDevice, F_SETFD, FD_CLOEXEC) != 0)
|
---|
95 | {
|
---|
96 | int rc = errno;
|
---|
97 | LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d\n", rc));
|
---|
98 | close(g_hDevice);
|
---|
99 | g_hDevice = -1;
|
---|
100 | return RTErrConvertFromErrno(rc);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * We're done.
|
---|
105 | */
|
---|
106 | NOREF(cbReserve);
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | int suplibOsTerm(void)
|
---|
112 | {
|
---|
113 | /*
|
---|
114 | * Check if we're initited at all.
|
---|
115 | */
|
---|
116 | if (g_hDevice >= 0)
|
---|
117 | {
|
---|
118 | if (close(g_hDevice))
|
---|
119 | AssertFailed();
|
---|
120 | g_hDevice = -1;
|
---|
121 | }
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | int suplibOsInstall(void)
|
---|
127 | {
|
---|
128 | return VERR_NOT_IMPLEMENTED;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | int suplibOsUninstall(void)
|
---|
133 | {
|
---|
134 | return VERR_NOT_IMPLEMENTED;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | int suplibOsIOCtl(uintptr_t uFunction, void *pvReq, size_t cbReq)
|
---|
139 | {
|
---|
140 | AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
|
---|
141 |
|
---|
142 | if (RT_LIKELY(ioctl(g_hDevice, uFunction, pvReq) >= 0))
|
---|
143 | return VINF_SUCCESS;
|
---|
144 | return RTErrConvertFromErrno(errno);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | int suplibOsIOCtlFast(uintptr_t uFunction)
|
---|
149 | {
|
---|
150 | int rc = ioctl(g_hDevice, uFunction, NULL);
|
---|
151 | if (rc == -1)
|
---|
152 | rc = errno;
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | int suplibOsPageAlloc(size_t cPages, void **ppvPages)
|
---|
158 | {
|
---|
159 | *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
|
---|
160 | if (*ppvPages)
|
---|
161 | return VINF_SUCCESS;
|
---|
162 | return RTErrConvertFromErrno(errno);
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | int suplibOsPageFree(void *pvPages, size_t /* cPages */)
|
---|
167 | {
|
---|
168 | RTMemPageFree(pvPages);
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | }
|
---|
171 |
|
---|