VirtualBox

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

Last change on this file since 4284 was 4071, checked in by vboxsync, 18 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

File size: 5.0 KB
Line 
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/** BSD Device name. */
46#define DEVICE_NAME "/dev/vboxdrv"
47
48
49/*******************************************************************************
50* Global Variables *
51*******************************************************************************/
52/** Handle to the open device. */
53static int g_hDevice = -1;
54
55
56int 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 g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
68 if (g_hDevice < 0)
69 {
70 int rc = errno;
71 LogRel(("Failed to open \"%s\", errno=%d\n", rc));
72 return RTErrConvertFromErrno(rc);
73 }
74
75 /*
76 * We're done.
77 */
78 NOREF(cbReserve);
79 return VINF_SUCCESS;
80}
81
82
83int suplibOsTerm(void)
84{
85 /*
86 * Check if we're initited at all.
87 */
88 if (g_hDevice >= 0)
89 {
90 if (close(g_hDevice))
91 AssertFailed();
92 g_hDevice = -1;
93 }
94 return VINF_SUCCESS;
95}
96
97
98/**
99 * Installs anything required by the support library.
100 *
101 * @returns 0 on success.
102 * @returns error code on failure.
103 */
104int suplibOsInstall(void)
105{
106// int rc = mknod(DEVICE_NAME, S_IFCHR, );
107
108 return VERR_NOT_IMPLEMENTED;
109}
110
111
112/**
113 * Installs anything required by the support library.
114 *
115 * @returns 0 on success.
116 * @returns error code on failure.
117 */
118int suplibOsUninstall(void)
119{
120// int rc = unlink(DEVICE_NAME);
121
122 return VERR_NOT_IMPLEMENTED;
123}
124
125
126/**
127 * Send a I/O Control request to the device.
128 *
129 * @returns 0 on success.
130 * @returns VBOX error code on failure.
131 * @param uFunction IO Control function.
132 * @param pvIn Input data buffer.
133 * @param cbIn Size of input data.
134 * @param pvOut Output data buffer.
135 * @param cbOut Size of output data.
136 */
137int suplibOsIOCtl(unsigned uFunction, void *pvIn, size_t cbIn, void *pvOut, size_t cbOut)
138{
139 AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
140 /*
141 * Issue device iocontrol.
142 */
143 SUPDRVIOCTLDATA Args;
144 Args.pvIn = pvIn;
145 Args.cbIn = cbIn;
146 Args.pvOut = pvOut;
147 Args.cbOut = cbOut;
148
149 if (ioctl(g_hDevice, uFunction, &Args) >= 0)
150 return 0;
151 /* This is the reverse operation of the one found in SUPDrv-linux.c */
152 switch (errno)
153 {
154 case EACCES: return VERR_GENERAL_FAILURE;
155 case EINVAL: return VERR_INVALID_PARAMETER;
156 case ENOSYS: return VERR_INVALID_MAGIC;
157 case ENXIO: return VERR_INVALID_HANDLE;
158 case EFAULT: return VERR_INVALID_POINTER;
159 case ENOLCK: return VERR_LOCK_FAILED;
160 case EEXIST: return VERR_ALREADY_LOADED;
161 }
162
163 return RTErrConvertFromErrno(errno);
164}
165
166#ifdef VBOX_WITHOUT_IDT_PATCHING
167int suplibOSIOCtlFast(unsigned uFunction)
168{
169 int rc = ioctl(g_hDevice, uFunction, NULL);
170 if (rc == -1)
171 rc = errno;
172 return rc;
173}
174#endif
175
176
177/**
178 * Allocate a number of zero-filled pages in user space.
179 *
180 * @returns VBox status code.
181 * @param cPages Number of pages to allocate.
182 * @param ppvPages Where to return the base pointer.
183 */
184int suplibOsPageAlloc(size_t cPages, void **ppvPages)
185{
186 *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
187 if (*ppvPages)
188 return VINF_SUCCESS;
189 return RTErrConvertFromErrno(errno);
190}
191
192
193/**
194 * Frees pages allocated by suplibOsPageAlloc().
195 *
196 * @returns VBox status code.
197 * @param pvPages Pointer to pages.
198 */
199int suplibOsPageFree(void *pvPages, size_t /* cPages */)
200{
201 RTMemPageFree(pvPages);
202 return VINF_SUCCESS;
203}
204
Note: See TracBrowser for help on using the repository browser.

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