VirtualBox

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

Last change on this file was 99739, checked in by vboxsync, 13 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1/* $Id: krnlmod-linux.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
2/** @file
3 * IPRT - Kernel module, Linux.
4 */
5
6/*
7 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_SYSTEM
42#include <iprt/krnlmod.h>
43#include <iprt/linux/sysfs.h>
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/dir.h>
47#include <iprt/err.h>
48#include <iprt/mem.h>
49#include <iprt/string.h>
50#include <iprt/types.h>
51
52
53/**
54 * Internal kernel information record state.
55 */
56typedef struct RTKRNLMODINFOINT
57{
58 /** Reference counter. */
59 volatile uint32_t cRefs;
60 /** Reference count for the kernel module. */
61 uint32_t cRefKrnlMod;
62 /** Load address of the kernel module. */
63 RTR0UINTPTR uLoadAddr;
64 /** Size of the kernel module. */
65 size_t cbKrnlMod;
66 /** Size of the name in characters including the zero terminator. */
67 size_t cchName;
68 /** Module name - variable in size. */
69 char achName[1];
70} RTKRNLMODINFOINT;
71/** Pointer to the internal kernel module information record. */
72typedef RTKRNLMODINFOINT *PRTKRNLMODINFOINT;
73/** Pointer to a const internal kernel module information record. */
74typedef const RTKRNLMODINFOINT *PCRTKRNLMODINFOINT;
75
76
77
78/**
79 * Destroy the given kernel module information record.
80 *
81 * @param pThis The record to destroy.
82 */
83static void rtKrnlModInfoDestroy(PRTKRNLMODINFOINT pThis)
84{
85 RTMemFree(pThis);
86}
87
88
89static int rtKrnlModLinuxReadIntFileDef(unsigned uBase, int64_t *pi64, int64_t i64Def,
90 const char *pszName, const char *pszPath)
91{
92 int rc = RTLinuxSysFsReadIntFile(uBase, pi64, "module/%s/%s", pszName, pszPath);
93 if (rc == VERR_FILE_NOT_FOUND)
94 {
95 *pi64 = i64Def;
96 rc = VINF_SUCCESS;
97 }
98
99 return rc;
100}
101
102/**
103 * Creates a new kernel module information record for the given module.
104 *
105 * @returns IPRT status code.
106 * @param pszName The kernel module name.
107 * @param phKrnlModInfo Where to store the handle to the kernel module information record
108 * on success.
109 */
110static int rtKrnlModLinuxInfoCreate(const char *pszName, PRTKRNLMODINFO phKrnlModInfo)
111{
112 int rc = VINF_SUCCESS;
113 size_t cchName = strlen(pszName) + 1;
114 PRTKRNLMODINFOINT pThis = (PRTKRNLMODINFOINT)RTMemAllocZ(RT_UOFFSETOF_DYN(RTKRNLMODINFOINT, achName[cchName]));
115 if (RT_LIKELY(pThis))
116 {
117 memcpy(&pThis->achName[0], pszName, cchName);
118 pThis->cchName = cchName;
119 pThis->cRefs = 1;
120
121 int64_t iTmp = 0;
122 rc = rtKrnlModLinuxReadIntFileDef(10, &iTmp, 0, pszName, "refcnt");
123 if (RT_SUCCESS(rc))
124 pThis->cRefKrnlMod = (uint32_t)iTmp;
125
126 rc = rtKrnlModLinuxReadIntFileDef(10, &iTmp, 0, pszName, "coresize");
127 if (RT_SUCCESS(rc))
128 pThis->cbKrnlMod = iTmp;
129
130 rc = rtKrnlModLinuxReadIntFileDef(16, &iTmp, 0, pszName, "sections/.text");
131 if (RT_SUCCESS(rc))
132 pThis->uLoadAddr = iTmp;
133
134 if (RT_SUCCESS(rc))
135 *phKrnlModInfo = pThis;
136 else
137 RTMemFree(pThis);
138 }
139 else
140 rc = VERR_NO_MEMORY;
141
142 return rc;
143}
144
145
146RTDECL(int) RTKrnlModQueryLoaded(const char *pszName, bool *pfLoaded)
147{
148 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
149 AssertPtrReturn(pfLoaded, VERR_INVALID_POINTER);
150
151 int rc = RTLinuxSysFsExists("module/%s", pszName);
152 if (rc == VINF_SUCCESS)
153 *pfLoaded = true;
154 else if (rc == VERR_FILE_NOT_FOUND)
155 {
156 *pfLoaded = false;
157 rc = VINF_SUCCESS;
158 }
159
160 return rc;
161}
162
163
164RTDECL(int) RTKrnlModLoadedQueryInfo(const char *pszName, PRTKRNLMODINFO phKrnlModInfo)
165{
166 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
167 AssertPtrReturn(phKrnlModInfo, VERR_INVALID_POINTER);
168
169 int rc = RTLinuxSysFsExists("module/%s", pszName);
170 if (rc == VINF_SUCCESS)
171 rc = rtKrnlModLinuxInfoCreate(pszName, phKrnlModInfo);
172 else if (rc == VERR_FILE_NOT_FOUND)
173 rc = VERR_NOT_FOUND;
174
175 return rc;
176}
177
178
179RTDECL(uint32_t) RTKrnlModLoadedGetCount(void)
180{
181 uint32_t cKmodsLoaded = 0;
182
183 RTDIR hDir = NULL;
184 int rc = RTDirOpen(&hDir, "/sys/module");
185 if (RT_SUCCESS(rc))
186 {
187 RTDIRENTRY DirEnt;
188 rc = RTDirRead(hDir, &DirEnt, NULL);
189 while (RT_SUCCESS(rc))
190 {
191 if (!RTDirEntryIsStdDotLink(&DirEnt))
192 cKmodsLoaded++;
193 rc = RTDirRead(hDir, &DirEnt, NULL);
194 }
195
196 RTDirClose(hDir);
197 }
198
199
200 return cKmodsLoaded;
201}
202
203
204RTDECL(int) RTKrnlModLoadedQueryInfoAll(PRTKRNLMODINFO pahKrnlModInfo, uint32_t cEntriesMax,
205 uint32_t *pcEntries)
206{
207 if (cEntriesMax > 0)
208 AssertPtrReturn(pahKrnlModInfo, VERR_INVALID_POINTER);
209
210 uint32_t cKmodsLoaded = RTKrnlModLoadedGetCount();
211 if (cEntriesMax < cKmodsLoaded)
212 {
213 if (*pcEntries)
214 *pcEntries = cKmodsLoaded;
215 return VERR_BUFFER_OVERFLOW;
216 }
217
218 RTDIR hDir = NULL;
219 int rc = RTDirOpen(&hDir, "/sys/module");
220 if (RT_SUCCESS(rc))
221 {
222 unsigned idxKrnlModInfo = 0;
223 RTDIRENTRY DirEnt;
224
225 rc = RTDirRead(hDir, &DirEnt, NULL);
226 while (RT_SUCCESS(rc))
227 {
228 if (!RTDirEntryIsStdDotLink(&DirEnt))
229 {
230 rc = rtKrnlModLinuxInfoCreate(DirEnt.szName, &pahKrnlModInfo[idxKrnlModInfo]);
231 if (RT_SUCCESS(rc))
232 idxKrnlModInfo++;
233 }
234
235 if (RT_SUCCESS(rc))
236 rc = RTDirRead(hDir, &DirEnt, NULL);
237 }
238
239 if (rc == VERR_NO_MORE_FILES)
240 rc = VINF_SUCCESS;
241 else if (RT_FAILURE(rc))
242 {
243 /* Rollback */
244 while (idxKrnlModInfo-- > 0)
245 RTKrnlModInfoRelease(pahKrnlModInfo[idxKrnlModInfo]);
246 }
247
248 if (*pcEntries)
249 *pcEntries = cKmodsLoaded;
250
251 RTDirClose(hDir);
252 }
253
254 return rc;
255}
256
257
258RTDECL(uint32_t) RTKrnlModInfoRetain(RTKRNLMODINFO hKrnlModInfo)
259{
260 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
261 AssertPtrReturn(pThis, UINT32_MAX);
262
263 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
264 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
265 return cRefs;
266}
267
268
269RTDECL(uint32_t) RTKrnlModInfoRelease(RTKRNLMODINFO hKrnlModInfo)
270{
271 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
272 if (!pThis)
273 return 0;
274 AssertPtrReturn(pThis, UINT32_MAX);
275
276 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
277 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
278 if (cRefs == 0)
279 rtKrnlModInfoDestroy(pThis);
280 return cRefs;
281}
282
283
284RTDECL(uint32_t) RTKrnlModInfoGetRefCnt(RTKRNLMODINFO hKrnlModInfo)
285{
286 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
287 AssertPtrReturn(pThis, 0);
288
289 return pThis->cRefKrnlMod;
290}
291
292
293RTDECL(const char *) RTKrnlModInfoGetName(RTKRNLMODINFO hKrnlModInfo)
294{
295 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
296 AssertPtrReturn(pThis, NULL);
297
298 return &pThis->achName[0];
299}
300
301
302RTDECL(const char *) RTKrnlModInfoGetFilePath(RTKRNLMODINFO hKrnlModInfo)
303{
304 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
305 AssertPtrReturn(pThis, NULL);
306
307 return NULL;
308}
309
310
311RTDECL(size_t) RTKrnlModInfoGetSize(RTKRNLMODINFO hKrnlModInfo)
312{
313 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
314 AssertPtrReturn(pThis, 0);
315
316 return pThis->cbKrnlMod;
317}
318
319
320RTDECL(RTR0UINTPTR) RTKrnlModInfoGetLoadAddr(RTKRNLMODINFO hKrnlModInfo)
321{
322 PRTKRNLMODINFOINT pThis = hKrnlModInfo;
323 AssertPtrReturn(pThis, 0);
324
325 return pThis->uLoadAddr;
326}
327
328
329RTDECL(int) RTKrnlModInfoQueryRefModInfo(RTKRNLMODINFO hKrnlModInfo, uint32_t idx,
330 PRTKRNLMODINFO phKrnlModInfoRef)
331{
332 RT_NOREF3(hKrnlModInfo, idx, phKrnlModInfoRef);
333 return VERR_NOT_IMPLEMENTED;
334}
335
336
337RTDECL(int) RTKrnlModLoadByName(const char *pszName)
338{
339 AssertPtrReturn(pszName, VERR_INVALID_PARAMETER);
340
341 return VERR_NOT_SUPPORTED;
342}
343
344
345RTDECL(int) RTKrnlModLoadByPath(const char *pszPath)
346{
347 AssertPtrReturn(pszPath, VERR_INVALID_PARAMETER);
348
349 return VERR_NOT_SUPPORTED;
350}
351
352
353RTDECL(int) RTKrnlModUnloadByName(const char *pszName)
354{
355 AssertPtrReturn(pszName, VERR_INVALID_PARAMETER);
356
357 return VERR_NOT_SUPPORTED;
358}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use