VirtualBox

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

Last change on this file since 8527 was 8527, checked in by vboxsync, 17 years ago

Solaris vboxdrv: some memobj cleanups and added assertions.

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

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