VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/vbi/memobj-r0drv-solaris.c@ 26430

最後變更 在這個檔案從26430是 26430,由 vboxsync 提交於 15 年 前

Introducing RTR0MemObjAllocPhysEx

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 13.0 KB
 
1/* $Id: memobj-r0drv-solaris.c 26430 2010-02-11 14:23:01Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "../the-solaris-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/memobj.h>
38
39#include <iprt/assert.h>
40#include <iprt/err.h>
41#include <iprt/log.h>
42#include <iprt/mem.h>
43#include <iprt/param.h>
44#include <iprt/process.h>
45#include "internal/memobj.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * The Solaris version of the memory object structure.
53 */
54typedef struct RTR0MEMOBJSOLARIS
55{
56 /** The core structure. */
57 RTR0MEMOBJINTERNAL Core;
58 /** Pointer to kernel memory cookie. */
59 ddi_umem_cookie_t Cookie;
60 /** Shadow locked pages. */
61 void *pvHandle;
62 /** Access during locking. */
63 int fAccess;
64} RTR0MEMOBJSOLARIS, *PRTR0MEMOBJSOLARIS;
65
66
67
68int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
69{
70 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
71
72 switch (pMemSolaris->Core.enmType)
73 {
74 case RTR0MEMOBJTYPE_LOW:
75 vbi_lowmem_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
76 break;
77
78 case RTR0MEMOBJTYPE_CONT:
79 vbi_contig_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
80 break;
81
82 case RTR0MEMOBJTYPE_PAGE:
83 ddi_umem_free(pMemSolaris->Cookie);
84 break;
85
86 case RTR0MEMOBJTYPE_LOCK:
87 vbi_unlock_va(pMemSolaris->Core.pv, pMemSolaris->Core.cb, pMemSolaris->fAccess, pMemSolaris->pvHandle);
88 break;
89
90 case RTR0MEMOBJTYPE_MAPPING:
91 vbi_unmap(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
92 break;
93
94 case RTR0MEMOBJTYPE_RES_VIRT:
95 {
96 if (pMemSolaris->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
97 vmem_xfree(heap_arena, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
98 else
99 AssertFailed();
100 break;
101 }
102
103 /* unused */
104 case RTR0MEMOBJTYPE_PHYS:
105 default:
106 AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
107 return VERR_INTERNAL_ERROR;
108 }
109
110 return VINF_SUCCESS;
111}
112
113
114int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
115{
116 /* Create the object */
117 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
118 if (!pMemSolaris)
119 return VERR_NO_MEMORY;
120
121 void *virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
122 if (!virtAddr)
123 {
124 rtR0MemObjDelete(&pMemSolaris->Core);
125 return VERR_NO_PAGE_MEMORY;
126 }
127
128 pMemSolaris->Core.pv = virtAddr;
129 pMemSolaris->pvHandle = NULL;
130 *ppMem = &pMemSolaris->Core;
131 return VINF_SUCCESS;
132}
133
134
135int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
136{
137 NOREF(fExecutable);
138
139 /* Create the object */
140 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOW, NULL, cb);
141 if (!pMemSolaris)
142 return VERR_NO_MEMORY;
143
144 /* Allocate physically low page-aligned memory. */
145 caddr_t virtAddr;
146 uint64_t phys = (unsigned)0xffffffff;
147 virtAddr = vbi_lowmem_alloc(phys, cb);
148 if (virtAddr == NULL)
149 {
150 rtR0MemObjDelete(&pMemSolaris->Core);
151 return VERR_NO_LOW_MEMORY;
152 }
153 pMemSolaris->Core.pv = virtAddr;
154 pMemSolaris->pvHandle = NULL;
155 *ppMem = &pMemSolaris->Core;
156 return VINF_SUCCESS;
157}
158
159
160int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
161{
162 NOREF(fExecutable);
163
164 /* Create the object */
165 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_CONT, NULL, cb);
166 if (!pMemSolaris)
167 return VERR_NO_MEMORY;
168
169 /* Allocate physically contiguous page-aligned memory. */
170 caddr_t virtAddr;
171 uint64_t phys = (unsigned)0xffffffff;
172 virtAddr = vbi_contig_alloc(&phys, cb);
173 if (virtAddr == NULL)
174 {
175 rtR0MemObjDelete(&pMemSolaris->Core);
176 return VERR_NO_CONT_MEMORY;
177 }
178 Assert(phys < (uint64_t)1 << 32);
179 pMemSolaris->Core.pv = virtAddr;
180 pMemSolaris->Core.u.Cont.Phys = phys;
181 pMemSolaris->pvHandle = NULL;
182 *ppMem = &pMemSolaris->Core;
183 return VINF_SUCCESS;
184}
185
186
187int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
188{
189 /** @todo rtR0MemObjNativeAllocPhysNC / solaris */
190 return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
191}
192
193
194int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
195{
196 AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%RHp\n", PhysHighest), VERR_NOT_IMPLEMENTED);
197
198 /** @todo */
199 if ( uAlignment != 0
200 && uAlignment != PAGE_SIZE)
201 return VERR_NOT_SUPPORTED;
202
203 return rtR0MemObjNativeAllocCont(ppMem, cb, false);
204}
205
206
207int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
208{
209 /* Create the object */
210 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
211 if (!pMemSolaris)
212 return VERR_NO_MEMORY;
213
214 /* There is no allocation here, it needs to be mapped somewhere first */
215 pMemSolaris->Core.u.Phys.fAllocated = false;
216 pMemSolaris->Core.u.Phys.PhysBase = Phys;
217 *ppMem = &pMemSolaris->Core;
218 return VINF_SUCCESS;
219}
220
221
222int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
223{
224 AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
225 NOREF(fAccess);
226
227 /* Create the locking object */
228 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
229 if (!pMemSolaris)
230 return VERR_NO_MEMORY;
231
232 int fPageAccess = S_READ;
233 if (fAccess & RTMEM_PROT_WRITE)
234 fPageAccess = S_WRITE;
235 if (fAccess & RTMEM_PROT_EXEC)
236 fPageAccess = S_EXEC;
237 void *pvPageList = NULL;
238
239 /* Lock down user pages */
240 int rc = vbi_lock_va((caddr_t)R3Ptr, cb, fPageAccess, &pvPageList);
241 if (rc != 0)
242 {
243 cmn_err(CE_NOTE,"rtR0MemObjNativeLockUser: vbi_lock_va failed rc=%d\n", rc);
244 rtR0MemObjDelete(&pMemSolaris->Core);
245 return VERR_LOCK_FAILED;
246 }
247
248 pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)vbi_proc();
249 pMemSolaris->pvHandle = pvPageList;
250 pMemSolaris->fAccess = fPageAccess;
251 *ppMem = &pMemSolaris->Core;
252 return VINF_SUCCESS;
253}
254
255
256int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
257{
258 NOREF(fAccess);
259
260 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
261 if (!pMemSolaris)
262 return VERR_NO_MEMORY;
263
264 int fPageAccess = S_READ;
265 if (fAccess & RTMEM_PROT_WRITE)
266 fPageAccess = S_WRITE;
267 if (fAccess & RTMEM_PROT_EXEC)
268 fPageAccess = S_EXEC;
269 void *pvPageList = NULL;
270 int rc = vbi_lock_va((caddr_t)pv, cb, fPageAccess, &pvPageList);
271 if (rc != 0)
272 {
273 cmn_err(CE_NOTE,"rtR0MemObjNativeLockKernel: vbi_lock_va failed rc=%d\n", rc);
274 rtR0MemObjDelete(&pMemSolaris->Core);
275 return VERR_LOCK_FAILED;
276 }
277
278 pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
279 pMemSolaris->pvHandle = pvPageList;
280 pMemSolaris->fAccess = fPageAccess;
281 *ppMem = &pMemSolaris->Core;
282 return VINF_SUCCESS;
283}
284
285
286int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
287{
288 PRTR0MEMOBJSOLARIS pMemSolaris;
289 void *pv;
290
291 /*
292 * Use xalloc.
293 */
294 pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /*phase*/, 0 /*nocross*/,
295 NULL /*minaddr*/, NULL /*maxaddr*/, VM_SLEEP);
296 if (!pv)
297 return VERR_NO_MEMORY;
298 pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
299 if (!pMemSolaris)
300 {
301 vmem_xfree(heap_arena, pv, cb);
302 return VERR_NO_MEMORY;
303 }
304
305 pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
306 *ppMem = &pMemSolaris->Core;
307 return VINF_SUCCESS;
308}
309
310
311int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
312{
313 return VERR_NOT_IMPLEMENTED;
314}
315
316int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
317 unsigned fProt, size_t offSub, size_t cbSub)
318{
319 /** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
320 return VERR_NOT_IMPLEMENTED;
321}
322
323
324int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
325{
326 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
327 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
328 if (uAlignment > PAGE_SIZE)
329 return VERR_NOT_SUPPORTED;
330
331 PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
332 size_t size = pMemToMapSolaris->Core.cb;
333 void *pv = pMemToMapSolaris->Core.pv;
334 pgcnt_t cPages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
335 pgcnt_t iPage;
336 uint64_t *paddrs;
337 caddr_t addr;
338 int rc;
339
340 /* Create the mapping object */
341 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
342 if (!pMemSolaris)
343 return VERR_NO_MEMORY;
344
345 paddrs = kmem_zalloc(sizeof(uint64_t) * cPages, KM_SLEEP);
346 for (iPage = 0; iPage < cPages; iPage++)
347 {
348 paddrs[iPage] = vbi_va_to_pa(pv);
349 if (paddrs[iPage] == -(uint64_t)1)
350 {
351 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: no page to map.\n");
352 rc = VERR_MAP_FAILED;
353 goto l_done;
354 }
355 pv = (void *)((uintptr_t)pv + PAGE_SIZE);
356 }
357
358 rc = vbi_user_map(&addr, fProt, paddrs, size);
359 if (rc != 0)
360 {
361 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: vbi failure.\n");
362 rc = VERR_MAP_FAILED;
363 rtR0MemObjDelete(&pMemSolaris->Core);
364 goto l_done;
365 }
366 else
367 rc = VINF_SUCCESS;
368
369 pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)vbi_proc();
370 pMemSolaris->Core.pv = addr;
371 *ppMem = &pMemSolaris->Core;
372l_done:
373 kmem_free(paddrs, sizeof(uint64_t) * cPages);
374 return rc;
375}
376
377
378int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
379{
380 NOREF(pMem);
381 NOREF(offSub);
382 NOREF(cbSub);
383 NOREF(fProt);
384 return VERR_NOT_SUPPORTED;
385}
386
387
388RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
389{
390 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
391
392 switch (pMemSolaris->Core.enmType)
393 {
394 case RTR0MEMOBJTYPE_PAGE:
395 case RTR0MEMOBJTYPE_LOW:
396 case RTR0MEMOBJTYPE_MAPPING:
397 case RTR0MEMOBJTYPE_LOCK:
398 {
399 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
400 return vbi_va_to_pa(pb);
401 }
402
403 case RTR0MEMOBJTYPE_CONT:
404 return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
405
406 case RTR0MEMOBJTYPE_PHYS:
407 return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
408
409 case RTR0MEMOBJTYPE_PHYS_NC:
410 AssertFailed(/* not implemented */);
411 case RTR0MEMOBJTYPE_RES_VIRT:
412 default:
413 return NIL_RTHCPHYS;
414 }
415}
416
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette