VirtualBox

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

Last change on this file since 2552 was 1890, checked in by vboxsync, 18 years ago

Attempt to fix ALSA on Linux kernels <= 2.6.17: use mmap not memalign for allocating pages. Use madvise or mprotect to separater VM area structs inside the kernel. Most SUP* functions work on cPages now (not cBytes anymore). The free functions take a cPages parameter which is used for munmap on Linux.

File size: 5.2 KB
Line 
1/** @file
2 * SUPLib - FreeBSD Hosts,
3 */
4
5/*
6 * Copyright (C) 2006 InnoTek Systemberatung 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 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_SUP
26#include <VBox/types.h>
27#include <VBox/sup.h>
28#include <VBox/param.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31#include <iprt/path.h>
32#include <iprt/assert.h>
33#include <iprt/mem.h>
34#include <iprt/err.h>
35#include <iprt/string.h>
36#include "SUPLibInternal.h"
37#include "SUPDRVIOC.h"
38
39#include <sys/fcntl.h>
40#include <sys/ioctl.h>
41#include <errno.h>
42#include <unistd.h>
43#include <stdlib.h>
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49/** BSD Device name. */
50#define DEVICE_NAME "/dev/vboxdrv"
51
52
53/*******************************************************************************
54* Global Variables *
55*******************************************************************************/
56/** Handle to the open device. */
57static int g_hDevice = -1;
58
59
60int suplibOsInit(size_t cbReserve)
61{
62 /*
63 * Check if already initialized.
64 */
65 if (g_hDevice >= 0)
66 return VINF_SUCCESS;
67
68 /*
69 * Try open the BSD device.
70 */
71 g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
72 if (g_hDevice < 0)
73 {
74 int rc = errno;
75 LogRel(("Failed to open \"%s\", errno=%d\n", rc));
76 return RTErrConvertFromErrno(rc);
77 }
78
79 /*
80 * We're done.
81 */
82 NOREF(cbReserve);
83 return VINF_SUCCESS;
84}
85
86
87int suplibOsTerm(void)
88{
89 /*
90 * Check if we're initited at all.
91 */
92 if (g_hDevice >= 0)
93 {
94 if (close(g_hDevice))
95 AssertFailed();
96 g_hDevice = -1;
97 }
98 return VINF_SUCCESS;
99}
100
101
102/**
103 * Installs anything required by the support library.
104 *
105 * @returns 0 on success.
106 * @returns error code on failure.
107 */
108int suplibOsInstall(void)
109{
110// int rc = mknod(DEVICE_NAME, S_IFCHR, );
111
112 return VERR_NOT_IMPLEMENTED;
113}
114
115
116/**
117 * Installs anything required by the support library.
118 *
119 * @returns 0 on success.
120 * @returns error code on failure.
121 */
122int suplibOsUninstall(void)
123{
124// int rc = unlink(DEVICE_NAME);
125
126 return VERR_NOT_IMPLEMENTED;
127}
128
129
130/**
131 * Send a I/O Control request to the device.
132 *
133 * @returns 0 on success.
134 * @returns VBOX error code on failure.
135 * @param uFunction IO Control function.
136 * @param pvIn Input data buffer.
137 * @param cbIn Size of input data.
138 * @param pvOut Output data buffer.
139 * @param cbOut Size of output data.
140 */
141int suplibOsIOCtl(unsigned uFunction, void *pvIn, size_t cbIn, void *pvOut, size_t cbOut)
142{
143 AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
144 /*
145 * Issue device iocontrol.
146 */
147 SUPDRVIOCTLDATA Args;
148 Args.pvIn = pvIn;
149 Args.cbIn = cbIn;
150 Args.pvOut = pvOut;
151 Args.cbOut = cbOut;
152
153 if (ioctl(g_hDevice, uFunction, &Args) >= 0)
154 return 0;
155 /* This is the reverse operation of the one found in SUPDrv-linux.c */
156 switch (errno)
157 {
158 case EACCES: return VERR_GENERAL_FAILURE;
159 case EINVAL: return VERR_INVALID_PARAMETER;
160 case ENOSYS: return VERR_INVALID_MAGIC;
161 case ENXIO: return VERR_INVALID_HANDLE;
162 case EFAULT: return VERR_INVALID_POINTER;
163 case ENOLCK: return VERR_LOCK_FAILED;
164 case EEXIST: return VERR_ALREADY_LOADED;
165 }
166
167 return RTErrConvertFromErrno(errno);
168}
169
170#ifdef VBOX_WITHOUT_IDT_PATCHING
171int suplibOSIOCtlFast(unsigned uFunction)
172{
173 int rc = ioctl(g_hDevice, uFunction, NULL);
174 if (rc == -1)
175 rc = errno;
176 return rc;
177}
178#endif
179
180
181/**
182 * Allocate a number of zero-filled pages in user space.
183 *
184 * @returns VBox status code.
185 * @param cPages Number of pages to allocate.
186 * @param ppvPages Where to return the base pointer.
187 */
188int suplibOsPageAlloc(size_t cPages, void **ppvPages)
189{
190 *ppvPages = RTMemPageAllocZ(cPages << PAGE_SHIFT);
191 if (*ppvPages)
192 return VINF_SUCCESS;
193 return RTErrConvertFromErrno(errno);
194}
195
196
197/**
198 * Frees pages allocated by suplibOsPageAlloc().
199 *
200 * @returns VBox status code.
201 * @param pvPages Pointer to pages.
202 */
203int suplibOsPageFree(void *pvPages, size_t /* cPages */)
204{
205 RTMemPageFree(pvPages);
206 return VINF_SUCCESS;
207}
208
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