VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 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.9 KB
Line 
1/* $Id: krnlmod-linux.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/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 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 RTDIR hDir = NULL;
209 int rc = RTDirOpen(&hDir, "/sys/module");
210 if (RT_SUCCESS(rc))
211 {
212 unsigned idxKrnlModInfo = 0;
213 RTDIRENTRY DirEnt;
214
215 rc = RTDirRead(hDir, &DirEnt, NULL);
216 while (RT_SUCCESS(rc))
217 {
218 if (!RTDirEntryIsStdDotLink(&DirEnt))
219 {
220 rc = rtKrnlModLinuxInfoCreate(DirEnt.szName, &pahKrnlModInfo[idxKrnlModInfo]);
221 if (RT_SUCCESS(rc))
222 idxKrnlModInfo++;
223 }
224
225 if (RT_SUCCESS(rc))
226 rc = RTDirRead(hDir, &DirEnt, NULL);
227 }
228
229 if (rc == VERR_NO_MORE_FILES)
230 rc = VINF_SUCCESS;
231 else if (RT_FAILURE(rc))
232 {
233 /* Rollback */
234 while (idxKrnlModInfo-- > 0)
235 RTKrnlModInfoRelease(pahKrnlModInfo[idxKrnlModInfo]);
236 }
237
238 if (*pcEntries)
239 *pcEntries = cKmodsLoaded;
240
241 RTDirClose(hDir);
242 }
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 pThis->cRefKrnlMod;
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.

© 2023 Oracle
ContactPrivacy policyTerms of Use