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