VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/krnlmod-solaris.cpp@ 77754

Last change on this file since 77754 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1/* $Id: krnlmod-solaris.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Kernel module, Linux.
4 */
5
6/*
7 * Copyright (C) 2017-2019 Oracle Corporation
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
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_SYSTEM
32#include <iprt/krnlmod.h>
33#include <iprt/asm.h>
34#include <iprt/assert.h>
35#include <iprt/dir.h>
36#include <iprt/errcore.h>
37#include <iprt/mem.h>
38#include <iprt/string.h>
39#include <iprt/types.h>
40
41#include <iprt/stream.h>
42
43#include <sys/modctl.h>
44#include <errno.h>
45
46/**
47 * Internal kernel information record state.
48 */
49typedef struct RTKRNLMODINFOINT
50{
51 /** Reference counter. */
52 volatile uint32_t cRefs;
53 /** Load address of the kernel module. */
54 RTR0UINTPTR uLoadAddr;
55 /** Size of the kernel module. */
56 size_t cbKrnlMod;
57 /** Size of the name in characters including the zero terminator. */
58 size_t cchName;
59 /** Module name - variable in size. */
60 char achName[1];
61} RTKRNLMODINFOINT;
62/** Pointer to the internal kernel module information record. */
63typedef RTKRNLMODINFOINT *PRTKRNLMODINFOINT;
64/** Pointer to a const internal kernel module information record. */
65typedef const RTKRNLMODINFOINT *PCRTKRNLMODINFOINT;
66
67
68
69/**
70 * Destroy the given kernel module information record.
71 *
72 * @returns nothing.
73 * @param pThis The record to destroy.
74 */
75static void rtKrnlModInfoDestroy(PRTKRNLMODINFOINT pThis)
76{
77 RTMemFree(pThis);
78}
79
80
81/**
82 * Creates a new kernel module information record for the given module.
83 *
84 * @returns IPRT status code.
85 * @param pModInfo The Solaris kernel module information.
86 * @param phKrnlModInfo Where to store the handle to the kernel module information record
87 * on success.
88 */
89static int rtKrnlModSolInfoCreate(struct modinfo *pModInfo, PRTKRNLMODINFO phKrnlModInfo)
90{
91 int rc = VINF_SUCCESS;
92 size_t cchName = strlen(&pModInfo->mi_name[0]) + 1;
93 PRTKRNLMODINFOINT pThis = (PRTKRNLMODINFOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(RTKRNLMODINFOINT, achName[cchName]));
94 if (RT_LIKELY(pThis))
95 {
96 memcpy(&pThis->achName[0], &pModInfo->mi_name[0], cchName);
97 pThis->cchName = cchName;
98 pThis->cRefs = 1;
99 pThis->cbKrnlMod = pModInfo->mi_size;
100 pThis->uLoadAddr = (RTR0UINTPTR)pModInfo->mi_base;
101
102 *phKrnlModInfo = pThis;
103 }
104 else
105 rc = VERR_NO_MEMORY;
106
107 return rc;
108}
109
110
111RTDECL(int) RTKrnlModQueryLoaded(const char *pszName, bool *pfLoaded)
112{
113 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
114 AssertPtrReturn(pfLoaded, VERR_INVALID_POINTER);
115
116 RTKRNLMODINFO hKrnlModInfo = NIL_RTKRNLMODINFO;
117 int rc = RTKrnlModLoadedQueryInfo(pszName, &hKrnlModInfo);
118 if (RT_SUCCESS(rc))
119 {
120 *pfLoaded = true;
121 RTKrnlModInfoRelease(hKrnlModInfo);
122 }
123 else if (rc == VERR_NOT_FOUND)
124 {
125 *pfLoaded = false;
126 rc = VINF_SUCCESS;
127 }
128
129 return rc;
130}
131
132
133RTDECL(int) RTKrnlModLoadedQueryInfo(const char *pszName, PRTKRNLMODINFO phKrnlModInfo)
134{
135 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
136 AssertPtrReturn(phKrnlModInfo, VERR_INVALID_POINTER);
137
138 int rc = VERR_NOT_FOUND;
139 int iId = -1;
140 struct modinfo ModInfo;
141
142 ModInfo.mi_info = MI_INFO_ALL | MI_INFO_CNT;
143 ModInfo.mi_id = iId;
144 ModInfo.mi_nextid = iId;
145 do
146 {
147 int rcSol = modctl(MODINFO, iId, &ModInfo);
148 if (rcSol < 0)
149 {
150 rc = RTErrConvertFromErrno(errno);
151 break;
152 }
153
154 if (ModInfo.mi_id != -1)
155 {
156 ModInfo.mi_name[MODMAXNAMELEN - 1] = '\0'; /* Paranoia. */
157 if (!RTStrCmp(pszName, &ModInfo.mi_name[0]))
158 {
159 rc = rtKrnlModSolInfoCreate(&ModInfo, phKrnlModInfo);
160 break;
161 }
162 }
163
164 iId = ModInfo.mi_id;
165 } while (iId != -1);
166
167 return rc;
168}
169
170
171RTDECL(uint32_t) RTKrnlModLoadedGetCount(void)
172{
173 uint32_t cKmodsLoaded = 0;
174 int iId = -1;
175 struct modinfo ModInfo;
176
177 ModInfo.mi_info = MI_INFO_ALL | MI_INFO_CNT;
178 ModInfo.mi_id = iId;
179 ModInfo.mi_nextid = iId;
180 do
181 {
182 int rcSol = modctl(MODINFO, iId, &ModInfo);
183 if (rcSol < 0)
184 break;
185
186 cKmodsLoaded++;
187
188 iId = ModInfo.mi_id;
189 } while (iId != -1);
190
191 return cKmodsLoaded;
192}
193
194
195RTDECL(int) RTKrnlModLoadedQueryInfoAll(PRTKRNLMODINFO pahKrnlModInfo, uint32_t cEntriesMax,
196 uint32_t *pcEntries)
197{
198 AssertReturn(VALID_PTR(pahKrnlModInfo) || cEntriesMax == 0, VERR_INVALID_PARAMETER);
199
200 uint32_t cKmodsLoaded = RTKrnlModLoadedGetCount();
201 if (cEntriesMax < cKmodsLoaded)
202 {
203 if (*pcEntries)
204 *pcEntries = cKmodsLoaded;
205 return VERR_BUFFER_OVERFLOW;
206 }
207
208 int rc = VINF_SUCCESS;
209 int iId = -1;
210 unsigned idxKrnlModInfo = 0;
211 struct modinfo ModInfo;
212
213 ModInfo.mi_info = MI_INFO_ALL | MI_INFO_CNT;
214 ModInfo.mi_id = iId;
215 ModInfo.mi_nextid = iId;
216 do
217 {
218 int rcSol = modctl(MODINFO, iId, &ModInfo);
219 if (rcSol < 0)
220 {
221 rc = RTErrConvertFromErrno(errno);
222 if (rc == VERR_INVALID_PARAMETER && idxKrnlModInfo > 0)
223 rc = VINF_SUCCESS;
224 break;
225 }
226
227 ModInfo.mi_name[MODMAXNAMELEN - 1] = '\0'; /* Paranoia. */
228 rc = rtKrnlModSolInfoCreate(&ModInfo, &pahKrnlModInfo[idxKrnlModInfo]);
229 if (RT_SUCCESS(rc))
230 idxKrnlModInfo++;
231
232 iId = ModInfo.mi_id;
233 } while (iId != -1);
234
235 if (RT_FAILURE(rc))
236 {
237 /* Rollback */
238 while (idxKrnlModInfo-- > 0)
239 RTKrnlModInfoRelease(pahKrnlModInfo[idxKrnlModInfo]);
240 }
241 else if (pcEntries)
242 *pcEntries = idxKrnlModInfo;
243
244 return rc;
245}
246
247
248RTDECL(uint32_t) RTKrnlModInfoRetain(RTKRNLMODINFO hKrnlModInfo)
249{
250 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
251 AssertPtrReturn(pThis, UINT32_MAX);
252
253 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
254 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
255 return cRefs;
256}
257
258
259RTDECL(uint32_t) RTKrnlModInfoRelease(RTKRNLMODINFO hKrnlModInfo)
260{
261 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
262 if (!pThis)
263 return 0;
264 AssertPtrReturn(pThis, UINT32_MAX);
265
266 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
267 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
268 if (cRefs == 0)
269 rtKrnlModInfoDestroy(pThis);
270 return cRefs;
271}
272
273
274RTDECL(uint32_t) RTKrnlModInfoGetRefCnt(RTKRNLMODINFO hKrnlModInfo)
275{
276 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
277 AssertPtrReturn(pThis, 0);
278
279 return 0;
280}
281
282
283RTDECL(const char *) RTKrnlModInfoGetName(RTKRNLMODINFO hKrnlModInfo)
284{
285 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
286 AssertPtrReturn(pThis, NULL);
287
288 return &pThis->achName[0];
289}
290
291
292RTDECL(const char *) RTKrnlModInfoGetFilePath(RTKRNLMODINFO hKrnlModInfo)
293{
294 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
295 AssertPtrReturn(pThis, NULL);
296
297 return NULL;
298}
299
300
301RTDECL(size_t) RTKrnlModInfoGetSize(RTKRNLMODINFO hKrnlModInfo)
302{
303 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
304 AssertPtrReturn(pThis, 0);
305
306 return pThis->cbKrnlMod;
307}
308
309
310RTDECL(RTR0UINTPTR) RTKrnlModInfoGetLoadAddr(RTKRNLMODINFO hKrnlModInfo)
311{
312 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
313 AssertPtrReturn(pThis, 0);
314
315 return pThis->uLoadAddr;
316}
317
318
319RTDECL(int) RTKrnlModInfoQueryRefModInfo(RTKRNLMODINFO hKrnlModInfo, uint32_t idx,
320 PRTKRNLMODINFO phKrnlModInfoRef)
321{
322 RT_NOREF3(hKrnlModInfo, idx, phKrnlModInfoRef);
323 return VERR_NOT_IMPLEMENTED;
324}
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