VirtualBox

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

Last change on this file since 22566 was 22073, checked in by vboxsync, 15 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: 12.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 void *handle;
62} RTR0MEMOBJSOLARIS, *PRTR0MEMOBJSOLARIS;
63
64
65
66int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
67{
68 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
69
70 switch (pMemSolaris->Core.enmType)
71 {
72 case RTR0MEMOBJTYPE_LOW:
73 vbi_lowmem_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
74 break;
75
76 case RTR0MEMOBJTYPE_CONT:
77 vbi_contig_free(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
78 break;
79
80 case RTR0MEMOBJTYPE_PAGE:
81 ddi_umem_free(pMemSolaris->Cookie);
82 break;
83
84 case RTR0MEMOBJTYPE_LOCK:
85 vbi_unlock_va(pMemSolaris->Core.pv, pMemSolaris->Core.cb, pMemSolaris->handle);
86 break;
87
88 case RTR0MEMOBJTYPE_MAPPING:
89 vbi_unmap(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
90 break;
91
92 case RTR0MEMOBJTYPE_RES_VIRT:
93 {
94 if (pMemSolaris->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
95 vmem_xfree(heap_arena, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
96 else
97 AssertFailed();
98 break;
99 }
100
101 /* unused */
102 case RTR0MEMOBJTYPE_PHYS:
103 default:
104 AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
105 return VERR_INTERNAL_ERROR;
106 }
107
108 return VINF_SUCCESS;
109}
110
111
112int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
113{
114 /* Create the object */
115 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
116 if (!pMemSolaris)
117 return VERR_NO_MEMORY;
118
119 void *virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
120 if (!virtAddr)
121 {
122 rtR0MemObjDelete(&pMemSolaris->Core);
123 return VERR_NO_PAGE_MEMORY;
124 }
125
126 pMemSolaris->Core.pv = virtAddr;
127 pMemSolaris->handle = NULL;
128 *ppMem = &pMemSolaris->Core;
129 return VINF_SUCCESS;
130}
131
132
133int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
134{
135 NOREF(fExecutable);
136
137 /* Create the object */
138 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOW, NULL, cb);
139 if (!pMemSolaris)
140 return VERR_NO_MEMORY;
141
142 /* Allocate physically low page-aligned memory. */
143 caddr_t virtAddr;
144 uint64_t phys = (unsigned)0xffffffff;
145 virtAddr = vbi_lowmem_alloc(phys, cb);
146 if (virtAddr == NULL)
147 {
148 rtR0MemObjDelete(&pMemSolaris->Core);
149 return VERR_NO_LOW_MEMORY;
150 }
151 pMemSolaris->Core.pv = virtAddr;
152 pMemSolaris->handle = NULL;
153 *ppMem = &pMemSolaris->Core;
154 return VINF_SUCCESS;
155}
156
157
158int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
159{
160 NOREF(fExecutable);
161
162 /* Create the object */
163 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_CONT, NULL, cb);
164 if (!pMemSolaris)
165 return VERR_NO_MEMORY;
166
167 /* Allocate physically contiguous page-aligned memory. */
168 caddr_t virtAddr;
169 uint64_t phys = (unsigned)0xffffffff;
170 virtAddr = vbi_contig_alloc(&phys, cb);
171 if (virtAddr == NULL)
172 {
173 rtR0MemObjDelete(&pMemSolaris->Core);
174 return VERR_NO_CONT_MEMORY;
175 }
176 Assert(phys < (uint64_t)1 << 32);
177 pMemSolaris->Core.pv = virtAddr;
178 pMemSolaris->Core.u.Cont.Phys = phys;
179 pMemSolaris->handle = NULL;
180 *ppMem = &pMemSolaris->Core;
181 return VINF_SUCCESS;
182}
183
184
185int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
186{
187 /** @todo rtR0MemObjNativeAllocPhysNC / solaris */
188 return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
189}
190
191
192int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
193{
194 AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%RHp\n", PhysHighest), VERR_NOT_IMPLEMENTED);
195
196 return rtR0MemObjNativeAllocCont(ppMem, cb, false);
197}
198
199
200int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
201{
202 /* Create the object */
203 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
204 if (!pMemSolaris)
205 return VERR_NO_MEMORY;
206
207 /* There is no allocation here, it needs to be mapped somewhere first */
208 pMemSolaris->Core.u.Phys.fAllocated = false;
209 pMemSolaris->Core.u.Phys.PhysBase = Phys;
210 *ppMem = &pMemSolaris->Core;
211 return VINF_SUCCESS;
212}
213
214
215int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
216{
217 AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
218
219 /* Create the locking object */
220 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
221 if (!pMemSolaris)
222 return VERR_NO_MEMORY;
223
224 void *ppl;
225
226 /* Lock down user pages */
227 int rc = vbi_lock_va((caddr_t)R3Ptr, cb, &ppl);
228 if (rc != 0)
229 {
230 cmn_err(CE_NOTE,"rtR0MemObjNativeLockUser: vbi_lock_va failed rc=%d\n", rc);
231 return VERR_LOCK_FAILED;
232 }
233
234 pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)vbi_proc();
235 pMemSolaris->handle = ppl;
236 *ppMem = &pMemSolaris->Core;
237 return VINF_SUCCESS;
238}
239
240
241int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
242{
243 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
244 if (!pMemSolaris)
245 return VERR_NO_MEMORY;
246
247 void *ppl;
248 int rc = vbi_lock_va((caddr_t)pv, cb, &ppl);
249 if (rc != 0)
250 {
251 cmn_err(CE_NOTE,"rtR0MemObjNativeLockKernel: vbi_lock_va failed rc=%d\n", rc);
252 return VERR_LOCK_FAILED;
253 }
254
255 pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
256 pMemSolaris->handle = ppl;
257 *ppMem = &pMemSolaris->Core;
258 return VINF_SUCCESS;
259}
260
261
262int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
263{
264 PRTR0MEMOBJSOLARIS pMemSolaris;
265 void *pv;
266
267 /*
268 * Use xalloc.
269 */
270 pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /*phase*/, 0 /*nocross*/,
271 NULL /*minaddr*/, NULL /*maxaddr*/, VM_SLEEP);
272 if (!pv)
273 return VERR_NO_MEMORY;
274 pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
275 if (!pMemSolaris)
276 {
277 vmem_xfree(heap_arena, pv, cb);
278 return VERR_NO_MEMORY;
279 }
280
281 pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
282 *ppMem = &pMemSolaris->Core;
283 return VINF_SUCCESS;
284}
285
286
287int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
288{
289 return VERR_NOT_IMPLEMENTED;
290}
291
292int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
293 unsigned fProt, size_t offSub, size_t cbSub)
294{
295 /** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
296 return VERR_NOT_IMPLEMENTED;
297}
298
299
300int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
301{
302 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
303 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
304 if (uAlignment > PAGE_SIZE)
305 return VERR_NOT_SUPPORTED;
306
307 PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
308 size_t size = pMemToMapSolaris->Core.cb;
309 void *pv = pMemToMapSolaris->Core.pv;
310 pgcnt_t cPages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
311 pgcnt_t iPage;
312 uint64_t *paddrs;
313 caddr_t addr;
314 int rc;
315
316 /* Create the mapping object */
317 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
318 if (!pMemSolaris)
319 return VERR_NO_MEMORY;
320
321 paddrs = kmem_zalloc(sizeof(uint64_t) * cPages, KM_SLEEP);
322 for (iPage = 0; iPage < cPages; iPage++)
323 {
324 paddrs[iPage] = vbi_va_to_pa(pv);
325 if (paddrs[iPage] == -(uint64_t)1)
326 {
327 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: no page to map.\n");
328 rc = VERR_MAP_FAILED;
329 goto l_done;
330 }
331 pv = (void *)((uintptr_t)pv + PAGE_SIZE);
332 }
333
334 rc = vbi_user_map(&addr, fProt, paddrs, size);
335 if (rc != 0)
336 {
337 cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: vbi failure.\n");
338 rc = VERR_MAP_FAILED;
339 rtR0MemObjDelete(&pMemSolaris->Core);
340 goto l_done;
341 }
342 else
343 rc = VINF_SUCCESS;
344
345 pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)vbi_proc();
346 pMemSolaris->Core.pv = addr;
347 *ppMem = &pMemSolaris->Core;
348l_done:
349 kmem_free(paddrs, sizeof(uint64_t) * cPages);
350 return rc;
351}
352
353
354int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
355{
356 NOREF(pMem);
357 NOREF(offSub);
358 NOREF(cbSub);
359 NOREF(fProt);
360 return VERR_NOT_SUPPORTED;
361}
362
363
364RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
365{
366 PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
367
368 switch (pMemSolaris->Core.enmType)
369 {
370 case RTR0MEMOBJTYPE_PAGE:
371 case RTR0MEMOBJTYPE_LOW:
372 case RTR0MEMOBJTYPE_MAPPING:
373 case RTR0MEMOBJTYPE_LOCK:
374 {
375 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
376 return vbi_va_to_pa(pb);
377 }
378
379 case RTR0MEMOBJTYPE_CONT:
380 return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
381
382 case RTR0MEMOBJTYPE_PHYS:
383 return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
384
385 case RTR0MEMOBJTYPE_PHYS_NC:
386 AssertFailed(/* not implemented */);
387 case RTR0MEMOBJTYPE_RES_VIRT:
388 default:
389 return NIL_RTHCPHYS;
390 }
391}
392
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