VirtualBox

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

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

IPRT,VMM,SUPDrv,VBGLR0: Added a parameter to RTR0MemObjLockUser/Kernel that indicates read/write intent so we can correctly lock readonly memory on Windows and OS/2. (Guest property strings, see #4238.)

  • 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 23610 2009-10-07 21:22:10Z 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, uint32_t fAccess, RTR0PROCESS R0Process)
275{
276 AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
277 NOREF(fAccess);
278
279 /* Create the locking object */
280 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
281 if (!pMemSolaris)
282 return VERR_NO_MEMORY;
283
284 proc_t *userproc = (proc_t *)R0Process;
285 struct as *useras = userproc->p_as;
286 page_t **ppl;
287
288 /* Lock down user pages */
289 int rc;
290 ppl = NULL;
291 if ((uintptr_t)R3Ptr < kernelbase)
292 rc = as_pagelock(useras, &ppl, (caddr_t)R3Ptr, cb, S_WRITE);
293 else
294 rc = 0;
295 if (rc == 0)
296 {
297 pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)userproc;
298 pMemSolaris->ppShadowPages = ppl;
299 *ppMem = &pMemSolaris->Core;
300 return VINF_SUCCESS;
301 }
302
303 cmn_err(CE_NOTE,"rtR0MemObjNativeLockUser: as_pagelock failed rc=%d\n", rc);
304 rtR0MemObjDelete(&pMemSolaris->Core);
305 return VERR_LOCK_FAILED;
306}
307
308
309int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
310{
311 NOREF(fAccess);
312
313 /* Create the locking object */
314 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
315 if (!pMemSolaris)
316 return VERR_NO_MEMORY;
317
318 /* Nothing to do here for kernel addresses. */
319
320 pMemSolaris->Core.u.Lock.R0Process = RTR0ProcHandleSelf();
321 pMemSolaris->ppShadowPages = NULL;
322 *ppMem = &pMemSolaris->Core;
323 return VINF_SUCCESS;
324}
325
326
327int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
328{
329 PRTR0MEMOBJSOLARIS pMemSolaris;
330 void *pv;
331
332 /*
333 * Use xalloc.
334 */
335 pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /*phase*/, 0 /*nocross*/,
336 NULL /*minaddr*/, NULL /*maxaddr*/, VM_SLEEP);
337 if (!pv)
338 return VERR_NO_MEMORY;
339 pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
340 if (!pMemSolaris)
341 {
342 vmem_xfree(heap_arena, pv, cb);
343 return VERR_NO_MEMORY;
344 }
345
346 pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
347 *ppMem = &pMemSolaris->Core;
348 return VINF_SUCCESS;
349}
350
351
352int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
353{
354 return VERR_NOT_IMPLEMENTED;
355}
356
357int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
358 unsigned fProt, size_t offSub, size_t cbSub)
359{
360 /** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
361 return VERR_NOT_IMPLEMENTED;
362}
363
364
365int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
366{
367 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
368 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
369
370 PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
371 size_t size = pMemToMapSolaris->Core.cb;
372 proc_t *userproc = (proc_t *)R0Process;
373 struct as *useras = userproc->p_as;
374 void *pv = pMemToMapSolaris->Core.pv;
375 pgcnt_t cPages = btop(size);
376 pgcnt_t iPage;
377 caddr_t addr;
378 int rc;
379
380 /* Request the system for a mapping address. */
381 as_rangelock(useras);
382 map_addr(&addr, size, 0 /* offset */, 1 /* vac-align */, MAP_SHARED | MAP_ANONYMOUS);
383 if (!addr)
384 {
385 as_rangeunlock(useras);
386 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr failed\n");
387 return VERR_MAP_FAILED;
388 }
389
390 /* Check address against alignment, fail if it doesn't match */
391 if ((uintptr_t)addr & (uAlignment - 1))
392 {
393 as_rangeunlock(useras);
394 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr alignment(%ld) failed.\n", uAlignment);
395 if (uAlignment > PAGE_SIZE)
396 return VERR_NOT_SUPPORTED;
397 return VERR_MAP_FAILED;
398 }
399
400 /* Our protection masks are identical to <sys/mman.h> but we
401 * need to add PROT_USER for the pages to be accessible by user
402 */
403 struct segvn_crargs crArgs = SEGVN_ZFOD_ARGS(fProt | PROT_USER, PROT_ALL);
404 rc = as_map(useras, addr, size, segvn_create, &crArgs);
405 as_rangeunlock(useras);
406 if (rc != 0)
407 {
408 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: as_map failure.\n");
409 return VERR_MAP_FAILED;
410 }
411
412 /* Create the mapping object */
413 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
414 if (!pMemSolaris)
415 {
416 /* Undo mapping on failure. */
417 as_unmap(useras, addr, size);
418 return VERR_NO_MEMORY;
419 }
420
421 /* Map each page into user space */
422 rw_enter(&useras->a_lock, RW_READER);
423 caddr_t kernAddr = pv;
424 caddr_t pageAddr = addr;
425 for (iPage = 0; iPage < cPages; iPage++)
426 {
427 page_t *pp = page_numtopp_nolock(hat_getpfnum(kas.a_hat, kernAddr));
428 hat_memload(useras->a_hat, pageAddr, pp, (fProt | PROT_USER), HAT_LOAD_LOCK);
429 pageAddr += ptob(1);
430 kernAddr += ptob(1);
431 }
432 rw_exit(&useras->a_lock);
433
434 pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)userproc;
435 pMemSolaris->Core.pv = addr;
436 *ppMem = &pMemSolaris->Core;
437 return VINF_SUCCESS;
438}
439
440
441int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
442{
443 NOREF(pMem);
444 NOREF(offSub);
445 NOREF(cbSub);
446 NOREF(fProt);
447 return VERR_NOT_SUPPORTED;
448}
449
450
451RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
452{
453 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
454
455 switch (pMemSolaris->Core.enmType)
456 {
457 case RTR0MEMOBJTYPE_PAGE:
458 case RTR0MEMOBJTYPE_LOW:
459 case RTR0MEMOBJTYPE_MAPPING:
460 {
461 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
462 return rtR0MemObjSolarisVirtToPhys(kas.a_hat, pb);
463 }
464
465 case RTR0MEMOBJTYPE_LOCK:
466 {
467 struct hat *hatSpace;
468 if (pMemSolaris->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
469 {
470 /* User */
471 proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Lock.R0Process;
472 hatSpace = userProc->p_as->a_hat;
473 }
474 else /* Kernel */
475 hatSpace = kas.a_hat;
476
477 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
478 return rtR0MemObjSolarisVirtToPhys(hatSpace, pb);
479 }
480
481 case RTR0MEMOBJTYPE_CONT:
482 return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
483
484 case RTR0MEMOBJTYPE_PHYS:
485 return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
486
487 case RTR0MEMOBJTYPE_PHYS_NC:
488 AssertFailed(/* not implemented */);
489 case RTR0MEMOBJTYPE_RES_VIRT:
490 default:
491 return NIL_RTHCPHYS;
492 }
493}
494
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