VirtualBox

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

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

Remove unused code and spaces.

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