VirtualBox

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

Last change on this file since 22073 was 22073, checked in by vboxsync, 16 years ago

iprt/r0drv/solaris: context assertions (RT_MORE_STRICT).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 KB
Line 
1/* $Id: memobj-r0drv-solaris.c 22073 2009-08-07 15:26:56Z 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/*******************************************************************************
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 page_t **ppShadowPages;
62} RTR0MEMOBJSOLARIS, *PRTR0MEMOBJSOLARIS;
63
64
65/*******************************************************************************
66* Global Variables *
67*******************************************************************************/
68/**
69 * Used for supplying the solaris kernel info. about memory limits
70 * during contiguous allocations (i_ddi_mem_alloc)
71 */
72struct ddi_dma_attr g_SolarisX86PhysMemLimits =
73{
74 DMA_ATTR_V0, /* Version Number */
75 (uint64_t)0, /* lower limit */
76 (uint64_t)0xffffffff, /* high limit (32-bit PA, 4G) */
77 (uint64_t)0xffffffff, /* counter limit */
78 (uint64_t)PAGE_SIZE, /* alignment */
79 (uint64_t)PAGE_SIZE, /* burst size */
80 (uint64_t)PAGE_SIZE, /* effective DMA size */
81 (uint64_t)0xffffffff, /* max DMA xfer size */
82 (uint64_t)0xffffffff, /* segment boundary */
83 1, /* scatter-gather list length (1 for contiguous) */
84 1, /* device granularity */
85 0 /* bus-specific flags */
86};
87
88
89
90static uint64_t rtR0MemObjSolarisVirtToPhys(struct hat* hatSpace, caddr_t virtAddr)
91{
92 /* We could use paddr_t (more solaris-like) rather than uint64_t but paddr_t isn't defined for 64-bit */
93 pfn_t pfn = hat_getpfnum(hatSpace, virtAddr);
94 if (pfn == PFN_INVALID)
95 {
96 AssertMsgFailed(("rtR0MemObjSolarisVirtToPhys: hat_getpfnum for %p failed.\n", virtAddr));
97 return PFN_INVALID;
98 }
99
100 uint64_t physAddr = ((uint64_t)pfn << MMU_PAGESHIFT) | ((uintptr_t)virtAddr & MMU_PAGEOFFSET);
101 return physAddr;
102}
103
104
105int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
106{
107 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
108
109 switch (pMemSolaris->Core.enmType)
110 {
111 case RTR0MEMOBJTYPE_CONT:
112 i_ddi_mem_free(pMemSolaris->Core.pv, NULL);
113 break;
114
115 case RTR0MEMOBJTYPE_PAGE:
116 ddi_umem_free(pMemSolaris->Cookie);
117 break;
118
119 case RTR0MEMOBJTYPE_LOCK:
120 {
121 struct as *addrSpace;
122 if ((uintptr_t)pMemSolaris->Core.pv < kernelbase)
123 {
124 addrSpace = ((proc_t *)pMemSolaris->Core.u.Lock.R0Process)->p_as;
125 as_pageunlock(addrSpace, pMemSolaris->ppShadowPages, pMemSolaris->Core.pv, pMemSolaris->Core.cb, S_WRITE);
126 }
127 /* Nothing to unlock for kernel addresses. */
128 break;
129 }
130
131 case RTR0MEMOBJTYPE_MAPPING:
132 {
133 struct hat *hatSpace;
134 struct as *addrSpace;
135 if (pMemSolaris->Core.u.Mapping.R0Process == NIL_RTR0PROCESS)
136 {
137 /* Kernel process*/
138 hatSpace = kas.a_hat;
139 addrSpace = &kas;
140 }
141 else
142 {
143 /* User process */
144 proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Mapping.R0Process;
145 hatSpace = userProc->p_as->a_hat;
146 addrSpace = userProc->p_as;
147 }
148
149 rw_enter(&addrSpace->a_lock, RW_READER);
150 hat_unload(hatSpace, pMemSolaris->Core.pv, pMemSolaris->Core.cb, HAT_UNLOAD_UNLOCK);
151 rw_exit(&addrSpace->a_lock);
152 as_unmap(addrSpace, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
153 break;
154 }
155
156 case RTR0MEMOBJTYPE_RES_VIRT:
157 {
158 if (pMemSolaris->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
159 vmem_xfree(heap_arena, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
160 else
161 AssertFailed();
162 break;
163 }
164
165 /* unused */
166 case RTR0MEMOBJTYPE_LOW:
167 case RTR0MEMOBJTYPE_PHYS:
168 default:
169 AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
170 return VERR_INTERNAL_ERROR;
171 }
172
173 return VINF_SUCCESS;
174}
175
176
177int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
178{
179 /* Create the object */
180 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
181 if (!pMemSolaris)
182 return VERR_NO_MEMORY;
183
184 void *virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
185 if (!virtAddr)
186 {
187 rtR0MemObjDelete(&pMemSolaris->Core);
188 return VERR_NO_PAGE_MEMORY;
189 }
190
191 pMemSolaris->Core.pv = virtAddr;
192 pMemSolaris->ppShadowPages = NULL;
193 *ppMem = &pMemSolaris->Core;
194 return VINF_SUCCESS;
195}
196
197
198int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
199{
200 /* Try page alloc first */
201 int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
202 if (RT_SUCCESS(rc))
203 {
204 size_t iPage = cb >> PAGE_SHIFT;
205 while (iPage-- > 0)
206 if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) > (_4G - PAGE_SIZE))
207 {
208 /* Failed! Fall back to physical contiguous alloc */
209 RTR0MemObjFree(*ppMem, false);
210 rc = rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
211 break;
212 }
213 }
214 return rc;
215}
216
217
218int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
219{
220 NOREF(fExecutable);
221
222 /* Create the object */
223 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_CONT, NULL, cb);
224 if (!pMemSolaris)
225 return VERR_NO_MEMORY;
226
227 /* Allocate physically contiguous page-aligned memory. */
228 caddr_t virtAddr;
229 int rc = i_ddi_mem_alloc(NULL, &g_SolarisX86PhysMemLimits, cb, 1, 0, NULL, &virtAddr, NULL, NULL);
230 if (rc != DDI_SUCCESS)
231 {
232 rtR0MemObjDelete(&pMemSolaris->Core);
233 return VERR_NO_CONT_MEMORY;
234 }
235
236 pMemSolaris->Core.pv = virtAddr;
237 pMemSolaris->Core.u.Cont.Phys = rtR0MemObjSolarisVirtToPhys(kas.a_hat, virtAddr);
238 pMemSolaris->ppShadowPages = NULL;
239 *ppMem = &pMemSolaris->Core;
240 return VINF_SUCCESS;
241}
242
243
244int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
245{
246 /** @todo rtR0MemObjNativeAllocPhysNC / solaris */
247 return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
248}
249
250
251int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
252{
253 AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%RHp\n", PhysHighest), VERR_NOT_IMPLEMENTED);
254
255 return rtR0MemObjNativeAllocCont(ppMem, cb, false);
256}
257
258
259int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
260{
261 /* Create the object */
262 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
263 if (!pMemSolaris)
264 return VERR_NO_MEMORY;
265
266 /* There is no allocation here, it needs to be mapped somewhere first */
267 pMemSolaris->Core.u.Phys.fAllocated = false;
268 pMemSolaris->Core.u.Phys.PhysBase = Phys;
269 *ppMem = &pMemSolaris->Core;
270 return VINF_SUCCESS;
271}
272
273
274int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
275{
276 AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
277
278 /* Create the locking object */
279 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
280 if (!pMemSolaris)
281 return VERR_NO_MEMORY;
282
283 proc_t *userproc = (proc_t *)R0Process;
284 struct as *useras = userproc->p_as;
285 page_t **ppl;
286
287 /* Lock down user pages */
288 int rc;
289 ppl = NULL;
290 if ((uintptr_t)R3Ptr < kernelbase)
291 rc = as_pagelock(useras, &ppl, (caddr_t)R3Ptr, cb, S_WRITE);
292 else
293 rc = 0;
294 if (rc == 0)
295 {
296 pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)userproc;
297 pMemSolaris->ppShadowPages = ppl;
298 *ppMem = &pMemSolaris->Core;
299 return VINF_SUCCESS;
300 }
301
302 cmn_err(CE_NOTE,"rtR0MemObjNativeLockUser: as_pagelock failed rc=%d\n", rc);
303 rtR0MemObjDelete(&pMemSolaris->Core);
304 return VERR_LOCK_FAILED;
305}
306
307
308int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
309{
310 /* Create the locking object */
311 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
312 if (!pMemSolaris)
313 return VERR_NO_MEMORY;
314
315 /* Nothing to do here for kernel addresses. */
316
317 pMemSolaris->Core.u.Lock.R0Process = RTR0ProcHandleSelf();
318 pMemSolaris->ppShadowPages = NULL;
319 *ppMem = &pMemSolaris->Core;
320 return VINF_SUCCESS;
321}
322
323
324int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
325{
326 PRTR0MEMOBJSOLARIS pMemSolaris;
327 void *pv;
328
329 /*
330 * Use xalloc.
331 */
332 pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /*phase*/, 0 /*nocross*/,
333 NULL /*minaddr*/, NULL /*maxaddr*/, VM_SLEEP);
334 if (!pv)
335 return VERR_NO_MEMORY;
336 pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
337 if (!pMemSolaris)
338 {
339 vmem_xfree(heap_arena, pv, cb);
340 return VERR_NO_MEMORY;
341 }
342
343 pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
344 *ppMem = &pMemSolaris->Core;
345 return VINF_SUCCESS;
346}
347
348
349int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
350{
351 return VERR_NOT_IMPLEMENTED;
352}
353
354int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
355 unsigned fProt, size_t offSub, size_t cbSub)
356{
357 /** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
358 return VERR_NOT_IMPLEMENTED;
359}
360
361
362int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
363{
364 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
365 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
366
367 PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
368 size_t size = pMemToMapSolaris->Core.cb;
369 proc_t *userproc = (proc_t *)R0Process;
370 struct as *useras = userproc->p_as;
371 void *pv = pMemToMapSolaris->Core.pv;
372 pgcnt_t cPages = btop(size);
373 pgcnt_t iPage;
374 caddr_t addr;
375 int rc;
376
377 /* Request the system for a mapping address. */
378 as_rangelock(useras);
379 map_addr(&addr, size, 0 /* offset */, 1 /* vac-align */, MAP_SHARED | MAP_ANONYMOUS);
380 if (!addr)
381 {
382 as_rangeunlock(useras);
383 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr failed\n");
384 return VERR_MAP_FAILED;
385 }
386
387 /* Check address against alignment, fail if it doesn't match */
388 if ((uintptr_t)addr & (uAlignment - 1))
389 {
390 as_rangeunlock(useras);
391 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr alignment(%ld) failed.\n", uAlignment);
392 if (uAlignment > PAGE_SIZE)
393 return VERR_NOT_SUPPORTED;
394 return VERR_MAP_FAILED;
395 }
396
397 /* Our protection masks are identical to <sys/mman.h> but we
398 * need to add PROT_USER for the pages to be accessible by user
399 */
400 struct segvn_crargs crArgs = SEGVN_ZFOD_ARGS(fProt | PROT_USER, PROT_ALL);
401 rc = as_map(useras, addr, size, segvn_create, &crArgs);
402 as_rangeunlock(useras);
403 if (rc != 0)
404 {
405 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: as_map failure.\n");
406 return VERR_MAP_FAILED;
407 }
408
409 /* Create the mapping object */
410 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
411 if (!pMemSolaris)
412 {
413 /* Undo mapping on failure. */
414 as_unmap(useras, addr, size);
415 return VERR_NO_MEMORY;
416 }
417
418 /* Map each page into user space */
419 rw_enter(&useras->a_lock, RW_READER);
420 caddr_t kernAddr = pv;
421 caddr_t pageAddr = addr;
422 for (iPage = 0; iPage < cPages; iPage++)
423 {
424 page_t *pp = page_numtopp_nolock(hat_getpfnum(kas.a_hat, kernAddr));
425 hat_memload(useras->a_hat, pageAddr, pp, (fProt | PROT_USER), HAT_LOAD_LOCK);
426 pageAddr += ptob(1);
427 kernAddr += ptob(1);
428 }
429 rw_exit(&useras->a_lock);
430
431 pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)userproc;
432 pMemSolaris->Core.pv = addr;
433 *ppMem = &pMemSolaris->Core;
434 return VINF_SUCCESS;
435}
436
437
438int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
439{
440 NOREF(pMem);
441 NOREF(offSub);
442 NOREF(cbSub);
443 NOREF(fProt);
444 return VERR_NOT_SUPPORTED;
445}
446
447
448RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
449{
450 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
451
452 switch (pMemSolaris->Core.enmType)
453 {
454 case RTR0MEMOBJTYPE_PAGE:
455 case RTR0MEMOBJTYPE_LOW:
456 case RTR0MEMOBJTYPE_MAPPING:
457 {
458 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
459 return rtR0MemObjSolarisVirtToPhys(kas.a_hat, pb);
460 }
461
462 case RTR0MEMOBJTYPE_LOCK:
463 {
464 struct hat *hatSpace;
465 if (pMemSolaris->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
466 {
467 /* User */
468 proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Lock.R0Process;
469 hatSpace = userProc->p_as->a_hat;
470 }
471 else /* Kernel */
472 hatSpace = kas.a_hat;
473
474 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
475 return rtR0MemObjSolarisVirtToPhys(hatSpace, pb);
476 }
477
478 case RTR0MEMOBJTYPE_CONT:
479 return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
480
481 case RTR0MEMOBJTYPE_PHYS:
482 return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
483
484 case RTR0MEMOBJTYPE_PHYS_NC:
485 AssertFailed(/* not implemented */);
486 case RTR0MEMOBJTYPE_RES_VIRT:
487 default:
488 return NIL_RTHCPHYS;
489 }
490}
491
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