VirtualBox

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

Last change on this file since 27414 was 27414, checked in by vboxsync, 15 years ago

Solaris/r0drv: use ddi_umem_alloc for NC pages.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.7 KB
Line 
1/* $Id: memobj-r0drv-solaris.c 27414 2010-03-16 16:14:52Z 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.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 * 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* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * The Solaris version of the memory object structure.
52 */
53typedef struct RTR0MEMOBJSOLARIS
54{
55 /** The core structure. */
56 RTR0MEMOBJINTERNAL Core;
57 /** Pointer to kernel memory cookie. */
58 ddi_umem_cookie_t Cookie;
59 /** Shadow locked pages. */
60 void *pvHandle;
61 /** Access during locking. */
62 int fAccess;
63} RTR0MEMOBJSOLARIS, *PRTR0MEMOBJSOLARIS;
64
65
66
67int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
68{
69 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
70
71 switch (pMemSolaris->Core.enmType)
72 {
73 case RTR0MEMOBJTYPE_LOW:
74 vbi_lowmem_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
75 break;
76
77 case RTR0MEMOBJTYPE_CONT:
78 case RTR0MEMOBJTYPE_PHYS:
79 vbi_phys_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
80 break;
81
82 case RTR0MEMOBJTYPE_PHYS_NC:
83#if 0
84 vbi_phys_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
85#endif
86 ddi_umem_free(pMemSolaris->Cookie);
87 break;
88
89 case RTR0MEMOBJTYPE_PAGE:
90 ddi_umem_free(pMemSolaris->Cookie);
91 break;
92
93 case RTR0MEMOBJTYPE_LOCK:
94 vbi_unlock_va(pMemSolaris->Core.pv, pMemSolaris->Core.cb, pMemSolaris->fAccess, pMemSolaris->pvHandle);
95 break;
96
97 case RTR0MEMOBJTYPE_MAPPING:
98 vbi_unmap(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
99 break;
100
101 case RTR0MEMOBJTYPE_RES_VIRT:
102 {
103 if (pMemSolaris->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
104 vmem_xfree(heap_arena, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
105 else
106 AssertFailed();
107 break;
108 }
109
110 default:
111 AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
112 return VERR_INTERNAL_ERROR;
113 }
114
115 return VINF_SUCCESS;
116}
117
118
119int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
120{
121 /* Create the object. */
122 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
123 if (!pMemSolaris)
124 return VERR_NO_MEMORY;
125
126 void *virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
127 if (!virtAddr)
128 {
129 rtR0MemObjDelete(&pMemSolaris->Core);
130 return VERR_NO_PAGE_MEMORY;
131 }
132
133 pMemSolaris->Core.pv = virtAddr;
134 pMemSolaris->pvHandle = NULL;
135 *ppMem = &pMemSolaris->Core;
136 return VINF_SUCCESS;
137}
138
139
140int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
141{
142 NOREF(fExecutable);
143
144 /* Create the object */
145 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOW, NULL, cb);
146 if (!pMemSolaris)
147 return VERR_NO_MEMORY;
148
149 /* Allocate physically low page-aligned memory. */
150 uint64_t physAddr = _4G - 1;
151 caddr_t virtAddr = vbi_lowmem_alloc(physAddr, cb);
152 if (virtAddr == NULL)
153 {
154 rtR0MemObjDelete(&pMemSolaris->Core);
155 return VERR_NO_LOW_MEMORY;
156 }
157 pMemSolaris->Core.pv = virtAddr;
158 pMemSolaris->pvHandle = NULL;
159 *ppMem = &pMemSolaris->Core;
160 return VINF_SUCCESS;
161}
162
163
164int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
165{
166 NOREF(fExecutable);
167 return rtR0MemObjNativeAllocPhys(ppMem, cb, NIL_RTHCPHYS, PAGE_SIZE /* alignment */);
168}
169
170
171int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
172{
173#if HC_ARCH_BITS == 64
174 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS_NC, NULL, cb);
175 if (!pMemSolaris)
176 return VERR_NO_MEMORY;
177
178 /* Allocate physically non-contiguous page-aligned memory. */
179 uint64_t physAddr = PhysHighest;
180
181#if 0
182 /*
183 * The contig_alloc() way of allocating NC pages is broken or does not match our semantics. Refer #4716 for details.
184 */
185 caddr_t virtAddr = vbi_phys_alloc(&physAddr, cb, PAGE_SIZE, 0 /* non-contiguous */);
186#endif
187 caddr_t virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
188 if (RT_UNLIKELY(virtAddr == NULL))
189 {
190 rtR0MemObjDelete(&pMemSolaris->Core);
191 return VERR_NO_MEMORY;
192 }
193 Assert(!(physAddr & PAGE_OFFSET_MASK));
194 pMemSolaris->Core.pv = virtAddr;
195 pMemSolaris->Core.u.Phys.PhysBase = physAddr;
196 pMemSolaris->Core.u.Phys.fAllocated = true;
197 pMemSolaris->pvHandle = NULL;
198 *ppMem = &pMemSolaris->Core;
199 return VINF_SUCCESS;
200#else
201 /** @todo rtR0MemObjNativeAllocPhysNC / solaris */
202 return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
203#endif
204}
205
206
207int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
208{
209 AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%RHp\n", PhysHighest), VERR_NOT_IMPLEMENTED);
210
211 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
212 if (!pMemSolaris)
213 return VERR_NO_MEMORY;
214
215 AssertCompile(NIL_RTHCPHYS == UINT64_MAX);
216
217 /* Allocate physically contiguous memory aligned as specified. */
218 uint64_t physAddr = PhysHighest;
219 caddr_t virtAddr = vbi_phys_alloc(&physAddr, cb, uAlignment, 1 /* contiguous */);
220 if (RT_UNLIKELY(virtAddr == NULL))
221 {
222 rtR0MemObjDelete(&pMemSolaris->Core);
223 return VERR_NO_CONT_MEMORY;
224 }
225 Assert(!(physAddr & PAGE_OFFSET_MASK));
226 Assert(physAddr < PhysHighest);
227 Assert(physAddr + cb <= PhysHighest);
228#if 0
229 if (uAlignment != PAGE_SIZE)
230 {
231 /* uAlignment is always a multiple of PAGE_SIZE */
232 pgcnt_t cPages = (cb + uAlignment - 1) >> PAGE_SHIFT;
233 void *pvPage = virtAddr;
234 while (cPages-- > 0)
235 {
236 uint64_t u64Page = vbi_va_to_pa(pvPage);
237 if (u64Page & (uAlignment - 1))
238 {
239 LogRel(("rtR0MemObjNativeAllocPhys: alignment mismatch! cb=%u uAlignment=%u physAddr=%#x\n", cb, uAlignment, u64Page));
240 vbi_phys_free(virtAddr, cb);
241 rtR0MemObjDelete(&pMemSolaris->Core);
242 return VERR_NO_MEMORY;
243 }
244 pvPage = (void *)((uintptr_t)pvPage + PAGE_SIZE);
245 }
246 }
247#endif
248 pMemSolaris->Core.pv = virtAddr;
249 pMemSolaris->Core.u.Cont.Phys = physAddr;
250 pMemSolaris->pvHandle = NULL;
251 *ppMem = &pMemSolaris->Core;
252 return VINF_SUCCESS;
253}
254
255
256int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
257{
258 /* Create the object. */
259 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
260 if (!pMemSolaris)
261 return VERR_NO_MEMORY;
262
263 /* There is no allocation here, it needs to be mapped somewhere first. */
264 pMemSolaris->Core.u.Phys.fAllocated = false;
265 pMemSolaris->Core.u.Phys.PhysBase = Phys;
266 *ppMem = &pMemSolaris->Core;
267 return VINF_SUCCESS;
268}
269
270
271int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
272{
273 AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
274 NOREF(fAccess);
275
276 /* Create the locking object */
277 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
278 if (!pMemSolaris)
279 return VERR_NO_MEMORY;
280
281 int fPageAccess = S_READ;
282 if (fAccess & RTMEM_PROT_WRITE)
283 fPageAccess = S_WRITE;
284 if (fAccess & RTMEM_PROT_EXEC)
285 fPageAccess = S_EXEC;
286 void *pvPageList = NULL;
287
288 /* Lock down user pages */
289 int rc = vbi_lock_va((caddr_t)R3Ptr, cb, fPageAccess, &pvPageList);
290 if (rc != 0)
291 {
292 LogRel(("rtR0MemObjNativeLockUser: vbi_lock_va failed rc=%d\n", rc));
293 rtR0MemObjDelete(&pMemSolaris->Core);
294 return VERR_LOCK_FAILED;
295 }
296
297 pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)vbi_proc();
298 pMemSolaris->pvHandle = pvPageList;
299 pMemSolaris->fAccess = fPageAccess;
300 *ppMem = &pMemSolaris->Core;
301 return VINF_SUCCESS;
302}
303
304
305int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
306{
307 NOREF(fAccess);
308
309 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
310 if (!pMemSolaris)
311 return VERR_NO_MEMORY;
312
313 int fPageAccess = S_READ;
314 if (fAccess & RTMEM_PROT_WRITE)
315 fPageAccess = S_WRITE;
316 if (fAccess & RTMEM_PROT_EXEC)
317 fPageAccess = S_EXEC;
318 void *pvPageList = NULL;
319 int rc = vbi_lock_va((caddr_t)pv, cb, fPageAccess, &pvPageList);
320 if (rc != 0)
321 {
322 LogRel(("rtR0MemObjNativeLockKernel: vbi_lock_va failed rc=%d\n", rc));
323 rtR0MemObjDelete(&pMemSolaris->Core);
324 return VERR_LOCK_FAILED;
325 }
326
327 pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
328 pMemSolaris->pvHandle = pvPageList;
329 pMemSolaris->fAccess = fPageAccess;
330 *ppMem = &pMemSolaris->Core;
331 return VINF_SUCCESS;
332}
333
334
335int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
336{
337 PRTR0MEMOBJSOLARIS pMemSolaris;
338
339 /*
340 * Use xalloc.
341 */
342 void *pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /*phase*/, 0 /*nocross*/,
343 NULL /*minaddr*/, NULL /*maxaddr*/, VM_SLEEP);
344 if (RT_UNLIKELY(!pv))
345 return VERR_NO_MEMORY;
346
347 /* Create the object. */
348 pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
349 if (!pMemSolaris)
350 {
351 LogRel(("rtR0MemObjNativeReserveKernel failed to alloc memory object.\n"));
352 vmem_xfree(heap_arena, pv, cb);
353 return VERR_NO_MEMORY;
354 }
355
356 pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
357 *ppMem = &pMemSolaris->Core;
358 return VINF_SUCCESS;
359}
360
361
362int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
363{
364 return VERR_NOT_IMPLEMENTED;
365}
366
367int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
368 unsigned fProt, size_t offSub, size_t cbSub)
369{
370 /** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
371 return VERR_NOT_IMPLEMENTED;
372}
373
374
375int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
376{
377 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
378 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
379 if (uAlignment != PAGE_SIZE)
380 return VERR_NOT_SUPPORTED;
381
382 PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
383 size_t cb = pMemToMapSolaris->Core.cb;
384 void *pv = pMemToMapSolaris->Core.pv;
385 pgcnt_t cPages = (cb + PAGE_SIZE - 1) >> PAGE_SHIFT;
386
387 /* Create the mapping object */
388 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, cb);
389 if (!pMemSolaris)
390 return VERR_NO_MEMORY;
391
392 uint64_t *paPhysAddrs = kmem_zalloc(sizeof(uint64_t) * cPages, KM_SLEEP);
393 for (pgcnt_t iPage = 0; iPage < cPages; iPage++)
394 {
395 paPhysAddrs[iPage] = vbi_va_to_pa(pv);
396 if (RT_UNLIKELY(paPhysAddrs[iPage] == -(uint64_t)1))
397 {
398 LogRel(("rtR0MemObjNativeMapUser: no page to map.\n"));
399 kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
400 rtR0MemObjDelete(&pMemSolaris->Core);
401 return VERR_MAP_FAILED;
402 }
403 pv = (void *)((uintptr_t)pv + PAGE_SIZE);
404 }
405
406 caddr_t virtAddr = NULL;
407 int rc = vbi_user_map(&virtAddr, fProt, paPhysAddrs, cb);
408 if (rc != 0)
409 {
410 LogRel(("rtR0MemObjNativeMapUser: vbi mapping failure.\n"));
411 kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
412 rtR0MemObjDelete(&pMemSolaris->Core);
413 return VERR_MAP_FAILED;
414 }
415 else
416 rc = VINF_SUCCESS;
417
418 pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)vbi_proc();
419 pMemSolaris->Core.pv = virtAddr;
420 *ppMem = &pMemSolaris->Core;
421 kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
422 return rc;
423}
424
425
426int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
427{
428 NOREF(pMem);
429 NOREF(offSub);
430 NOREF(cbSub);
431 NOREF(fProt);
432 return VERR_NOT_SUPPORTED;
433}
434
435
436RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
437{
438 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
439
440 switch (pMemSolaris->Core.enmType)
441 {
442 case RTR0MEMOBJTYPE_PAGE:
443 case RTR0MEMOBJTYPE_LOW:
444 case RTR0MEMOBJTYPE_LOCK:
445 {
446 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
447 return vbi_va_to_pa(pb);
448 }
449
450 /*
451 * Although mapping can be handled by vbi_va_to_pa(offset) like the above case,
452 * request it from the parent so that we have a clear distinction between CONT/PHYS_NC.
453 */
454 case RTR0MEMOBJTYPE_MAPPING:
455 return rtR0MemObjNativeGetPagePhysAddr(pMemSolaris->Core.uRel.Child.pParent, iPage);
456
457 case RTR0MEMOBJTYPE_CONT:
458 case RTR0MEMOBJTYPE_PHYS:
459 return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
460
461 case RTR0MEMOBJTYPE_PHYS_NC:
462 if (pMemSolaris->Core.u.Phys.fAllocated == true)
463 {
464 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
465 return vbi_va_to_pa(pb);
466 }
467 return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
468
469 case RTR0MEMOBJTYPE_RES_VIRT:
470 default:
471 return NIL_RTHCPHYS;
472 }
473}
474
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