VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/krnlmod-linux.cpp@ 95685

Last change on this file since 95685 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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