1 | /* $Id: memobj-r0drv-solaris.c 5125 2007-10-01 14:10:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Ring-0 Memory Objects, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "the-solaris-kernel.h"
|
---|
23 |
|
---|
24 | #include <iprt/memobj.h>
|
---|
25 | #include <iprt/mem.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/log.h>
|
---|
29 | #include <iprt/param.h>
|
---|
30 | #include <iprt/process.h>
|
---|
31 | #include "internal/memobj.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /*******************************************************************************
|
---|
35 | * Structures and Typedefs *
|
---|
36 | *******************************************************************************/
|
---|
37 | /**
|
---|
38 | * The Solaris version of the memory object structure.
|
---|
39 | */
|
---|
40 | typedef struct RTR0MEMOBJSOLARIS
|
---|
41 | {
|
---|
42 | /** The core structure. */
|
---|
43 | RTR0MEMOBJINTERNAL Core;
|
---|
44 | /** Pointer to kernel memory cookie. */
|
---|
45 | ddi_umem_cookie_t Cookie;
|
---|
46 | /** Shadow locked pages. */
|
---|
47 | page_t **ppShadowPages;
|
---|
48 | } RTR0MEMOBJSOLARIS, *PRTR0MEMOBJSOLARIS;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Used for supplying the solaris kernel info. about memory limits
|
---|
52 | * during contiguous allocations (i_ddi_mem_alloc)
|
---|
53 | */
|
---|
54 | struct ddi_dma_attr g_SolarisX86PhysMemLimits =
|
---|
55 | {
|
---|
56 | DMA_ATTR_V0, /* Version Number */
|
---|
57 | (uint64_t)0, /* lower limit */
|
---|
58 | (uint64_t)0xffffffff, /* high limit (32-bit PA, 4G) */
|
---|
59 | (uint64_t)0xffffffff, /* counter limit */
|
---|
60 | (uint64_t)PAGE_SIZE, /* alignment */
|
---|
61 | (uint64_t)PAGE_SIZE, /* burst size */
|
---|
62 | (uint64_t)PAGE_SIZE, /* effective DMA size */
|
---|
63 | (uint64_t)0xffffffff, /* max DMA xfer size */
|
---|
64 | (uint64_t)0xffffffff, /* segment boundary */
|
---|
65 | 1, /* scatter-gather list length (1 for contiguous) */
|
---|
66 | 1, /* device granularity */
|
---|
67 | 0 /* bus-specific flags */
|
---|
68 | };
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | static uint64_t rtR0MemObjSolarisVirtToPhys(struct hat* hatSpace, caddr_t virtAddr)
|
---|
73 | {
|
---|
74 | /* We could use paddr_t (more solaris-like) rather than uint64_t but paddr_t isn't defined for 64-bit */
|
---|
75 | pfn_t pfn = hat_getpfnum(hatSpace, virtAddr);
|
---|
76 | if (pfn == PFN_INVALID)
|
---|
77 | {
|
---|
78 | AssertMsgFailed(("rtR0MemObjSolarisVirtToPhys: hat_getpfnum for %p failed.\n", virtAddr));
|
---|
79 | return PFN_INVALID;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* Both works, but second will work for non-page aligned virtAddr */
|
---|
83 | #if 0
|
---|
84 | uint64_t physAddr = PAGE_SIZE * pfn;
|
---|
85 | #else
|
---|
86 | uint64_t physAddr = ((uint64_t)pfn << MMU_PAGESHIFT) | ((uintptr_t)virtAddr & MMU_PAGEOFFSET);
|
---|
87 | #endif
|
---|
88 | return physAddr;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
|
---|
93 | {
|
---|
94 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
|
---|
95 |
|
---|
96 | switch (pMemSolaris->Core.enmType)
|
---|
97 | {
|
---|
98 | case RTR0MEMOBJTYPE_CONT:
|
---|
99 | i_ddi_mem_free(pMemSolaris->Core.pv, NULL);
|
---|
100 | break;
|
---|
101 |
|
---|
102 | case RTR0MEMOBJTYPE_PAGE:
|
---|
103 | ddi_umem_free(pMemSolaris->Cookie);
|
---|
104 | break;
|
---|
105 |
|
---|
106 | case RTR0MEMOBJTYPE_LOCK:
|
---|
107 | {
|
---|
108 | struct as *addrSpace;
|
---|
109 | if (pMemSolaris->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
110 | addrSpace = &kas;
|
---|
111 | else
|
---|
112 | addrSpace = ((proc_t *)pMemSolaris->Core.u.Lock.R0Process)->p_as;
|
---|
113 |
|
---|
114 | as_pageunlock(addrSpace, pMemSolaris->ppShadowPages, pMemSolaris->Core.pv, pMemSolaris->Core.cb, S_WRITE);
|
---|
115 | break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
119 | {
|
---|
120 | struct hat *hatSpace;
|
---|
121 | struct as *addrSpace;
|
---|
122 | if (pMemSolaris->Core.u.Mapping.R0Process == NIL_RTR0PROCESS)
|
---|
123 | {
|
---|
124 | /* Kernel process*/
|
---|
125 | hatSpace = kas.a_hat;
|
---|
126 | addrSpace = &kas;
|
---|
127 | }
|
---|
128 | else
|
---|
129 | {
|
---|
130 | /* User process */
|
---|
131 | proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Mapping.R0Process;
|
---|
132 | hatSpace = userProc->p_as->a_hat;
|
---|
133 | addrSpace = userProc->p_as;
|
---|
134 | }
|
---|
135 |
|
---|
136 | rw_enter(&addrSpace->a_lock, RW_READER);
|
---|
137 | hat_unload(hatSpace, pMemSolaris->Core.pv, pMemSolaris->Core.cb, HAT_UNLOAD_UNLOCK);
|
---|
138 | rw_exit(&addrSpace->a_lock);
|
---|
139 | as_unmap(addrSpace, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
|
---|
140 | break;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* unused */
|
---|
144 | case RTR0MEMOBJTYPE_LOW:
|
---|
145 | case RTR0MEMOBJTYPE_PHYS:
|
---|
146 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
147 | default:
|
---|
148 | AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
|
---|
149 | return VERR_INTERNAL_ERROR;
|
---|
150 | }
|
---|
151 |
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
157 | {
|
---|
158 | /* Create the object */
|
---|
159 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
|
---|
160 | if (!pMemSolaris)
|
---|
161 | return VERR_NO_MEMORY;
|
---|
162 |
|
---|
163 | void *virtAddr = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
|
---|
164 | if (!virtAddr)
|
---|
165 | {
|
---|
166 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
167 | return VERR_NO_PAGE_MEMORY;
|
---|
168 | }
|
---|
169 |
|
---|
170 | pMemSolaris->Core.pv = virtAddr;
|
---|
171 | pMemSolaris->ppShadowPages = NULL;
|
---|
172 | *ppMem = &pMemSolaris->Core;
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
178 | {
|
---|
179 | /* Try page alloc first */
|
---|
180 | int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | {
|
---|
183 | size_t iPage = cb >> PAGE_SHIFT;
|
---|
184 | while (iPage-- > 0)
|
---|
185 | if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) > (_4G - PAGE_SIZE))
|
---|
186 | {
|
---|
187 | /* Failed! Fall back to physical contiguous alloc */
|
---|
188 | RTR0MemObjFree(*ppMem, false);
|
---|
189 | rc = rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
198 | {
|
---|
199 | NOREF(fExecutable);
|
---|
200 |
|
---|
201 | /* Create the object */
|
---|
202 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_CONT, NULL, cb);
|
---|
203 | if (!pMemSolaris)
|
---|
204 | return VERR_NO_MEMORY;
|
---|
205 |
|
---|
206 | /* Allocate physically contiguous page-aligned memory. */
|
---|
207 | caddr_t virtAddr;
|
---|
208 | int rc = i_ddi_mem_alloc(NULL, &g_SolarisX86PhysMemLimits, cb, 1, 0, NULL, &virtAddr, NULL, NULL);
|
---|
209 | if (rc != DDI_SUCCESS)
|
---|
210 | {
|
---|
211 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
212 | return VERR_NO_CONT_MEMORY;
|
---|
213 | }
|
---|
214 |
|
---|
215 | pMemSolaris->Core.pv = virtAddr;
|
---|
216 | pMemSolaris->Core.u.Cont.Phys = rtR0MemObjSolarisVirtToPhys(kas.a_hat, virtAddr);
|
---|
217 | pMemSolaris->ppShadowPages = NULL;
|
---|
218 | *ppMem = &pMemSolaris->Core;
|
---|
219 | return VINF_SUCCESS;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
224 | {
|
---|
225 | /** @todo rtR0MemObjNativeAllocPhysNC / solaris */
|
---|
226 | return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
231 | {
|
---|
232 | AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%VHp\n", PhysHighest), VERR_NOT_IMPLEMENTED);
|
---|
233 |
|
---|
234 | return rtR0MemObjNativeAllocCont(ppMem, cb, false);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
|
---|
239 | {
|
---|
240 | /* Create the object */
|
---|
241 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
242 | if (!pMemSolaris)
|
---|
243 | return VERR_NO_MEMORY;
|
---|
244 |
|
---|
245 | /* There is no allocation here, it needs to be mapped somewhere first */
|
---|
246 | pMemSolaris->Core.u.Phys.fAllocated = false;
|
---|
247 | pMemSolaris->Core.u.Phys.PhysBase = Phys;
|
---|
248 | *ppMem = &pMemSolaris->Core;
|
---|
249 | return VINF_SUCCESS;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
|
---|
254 | {
|
---|
255 | /* Create the locking object */
|
---|
256 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
|
---|
257 | if (!pMemSolaris)
|
---|
258 | return VERR_NO_MEMORY;
|
---|
259 |
|
---|
260 | proc_t *userproc = curproc;
|
---|
261 | if (R0Process != NIL_RTR0PROCESS)
|
---|
262 | userproc = (proc_t *)R0Process;
|
---|
263 |
|
---|
264 | struct as *useras = userproc->p_as;
|
---|
265 | page_t **ppl;
|
---|
266 |
|
---|
267 | /* Lock down user pages */
|
---|
268 | int rc = as_pagelock(useras, &ppl, (caddr_t)R3Ptr, cb, S_WRITE);
|
---|
269 | if (rc != 0)
|
---|
270 | {
|
---|
271 | cmn_err(CE_NOTE,"rtR0MemObjNativeLockUser: as_pagelock failed rc=%d\n", rc);
|
---|
272 | return VERR_LOCK_FAILED;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (!ppl)
|
---|
276 | {
|
---|
277 | as_pageunlock(useras, ppl, (caddr_t)R3Ptr, cb, S_WRITE);
|
---|
278 | cmn_err(CE_NOTE, "rtR0MemObjNativeLockUser: as_pagelock failed to get shadow pages\n");
|
---|
279 | return VERR_LOCK_FAILED;
|
---|
280 | }
|
---|
281 |
|
---|
282 | pMemSolaris->Core.u.Lock.R0Process = (RTR0PROCESS)userproc;
|
---|
283 | pMemSolaris->ppShadowPages = ppl;
|
---|
284 | *ppMem = &pMemSolaris->Core;
|
---|
285 | return VINF_SUCCESS;
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
|
---|
290 | {
|
---|
291 | /* Create the locking object */
|
---|
292 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
293 | if (!pMemSolaris)
|
---|
294 | return VERR_NO_MEMORY;
|
---|
295 |
|
---|
296 | caddr_t virtAddr = (caddr_t)((uintptr_t)pv & (uintptr_t)PAGEMASK);
|
---|
297 | page_t **ppl;
|
---|
298 |
|
---|
299 | /* Lock down kernel pages */
|
---|
300 | int rc = as_pagelock(&kas, &ppl, virtAddr, cb, S_WRITE);
|
---|
301 | if (rc != 0)
|
---|
302 | {
|
---|
303 | cmn_err(CE_NOTE,"rtR0MemObjNativeLockKernel: as_pagelock failed rc=%d\n", rc);
|
---|
304 | return VERR_LOCK_FAILED;
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (!ppl)
|
---|
308 | {
|
---|
309 | as_pageunlock(&kas, ppl, virtAddr, cb, S_WRITE);
|
---|
310 | cmn_err(CE_NOTE, "rtR0MemObjNativeLockKernel: failed to get shadow pages\n");
|
---|
311 | return VERR_LOCK_FAILED;
|
---|
312 | }
|
---|
313 |
|
---|
314 | pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS; /* means kernel, see rtR0MemObjNativeFree() */
|
---|
315 | pMemSolaris->ppShadowPages = ppl;
|
---|
316 | *ppMem = &pMemSolaris->Core;
|
---|
317 | return VINF_SUCCESS;
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
322 | {
|
---|
323 | return VERR_NOT_IMPLEMENTED;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
|
---|
328 | {
|
---|
329 | return VERR_NOT_IMPLEMENTED;
|
---|
330 | }
|
---|
331 |
|
---|
332 | int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
|
---|
333 | {
|
---|
334 | PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
|
---|
335 | size_t size = pMemToMapSolaris->Core.cb;
|
---|
336 | void *pv = pMemToMapSolaris->Core.pv;
|
---|
337 | pgcnt_t cPages = btop(size);
|
---|
338 | pgcnt_t iPage;
|
---|
339 | caddr_t addr;
|
---|
340 | int rc;
|
---|
341 |
|
---|
342 | /* Create the mapping object */
|
---|
343 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
|
---|
344 | if (!pMemSolaris)
|
---|
345 | return VERR_NO_MEMORY;
|
---|
346 |
|
---|
347 | as_rangelock(&kas);
|
---|
348 | if (pvFixed != (void *)-1)
|
---|
349 | {
|
---|
350 | /* Use user specified address */
|
---|
351 | addr = (caddr_t)pvFixed;
|
---|
352 |
|
---|
353 | /* Blow away any previous mapping */
|
---|
354 | as_unmap(&kas, addr, size);
|
---|
355 | }
|
---|
356 | else
|
---|
357 | {
|
---|
358 | /* Let the system choose an address */
|
---|
359 | map_addr(&addr, size, 0, 1, MAP_SHARED | MAP_ANONYMOUS);
|
---|
360 | if (addr == NULL)
|
---|
361 | {
|
---|
362 | as_rangeunlock(&kas);
|
---|
363 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapKernel: map_addr failed\n");
|
---|
364 | return VERR_NO_MEMORY;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /* Check address against alignment, fail if it doesn't match */
|
---|
368 | if ((uintptr_t)addr & (uAlignment - 1))
|
---|
369 | {
|
---|
370 | as_rangeunlock(&kas);
|
---|
371 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapKernel: map_addr alignment(%ld) failed.\n", uAlignment);
|
---|
372 | return VERR_MAP_FAILED;
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* Our protection masks are identical to <sys/mman.h> but we
|
---|
377 | * need to add PROT_USER for the pages to be accessible by user
|
---|
378 | */
|
---|
379 | struct segvn_crargs crArgs = SEGVN_ZFOD_ARGS(fProt | PROT_USER, PROT_ALL);
|
---|
380 | rc = as_map(&kas, addr, size, segvn_create, &crArgs);
|
---|
381 | as_rangeunlock(&kas);
|
---|
382 | if (rc != 0)
|
---|
383 | {
|
---|
384 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapKernel: as_map failure.\n");
|
---|
385 | return VERR_NO_MEMORY;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /* Map each page into kernel space */
|
---|
389 | rw_enter(&kas.a_lock, RW_READER);
|
---|
390 | caddr_t kernAddr = pv;
|
---|
391 | caddr_t pageAddr = addr;
|
---|
392 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
393 | {
|
---|
394 | page_t *pp = page_numtopp_nolock(hat_getpfnum(kas.a_hat, kernAddr));
|
---|
395 | hat_memload(kas.a_hat, pageAddr, pp, (fProt | PROT_USER), HAT_LOAD_LOCK);
|
---|
396 | pageAddr += ptob(1);
|
---|
397 | kernAddr += ptob(1);
|
---|
398 | }
|
---|
399 | rw_exit(&kas.a_lock);
|
---|
400 |
|
---|
401 | pMemSolaris->Core.u.Mapping.R0Process = NIL_RTR0PROCESS; /* means kernel */
|
---|
402 | pMemSolaris->Core.pv = addr;
|
---|
403 | *ppMem = &pMemSolaris->Core;
|
---|
404 | return VINF_SUCCESS;
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
409 | {
|
---|
410 | PRTR0MEMOBJSOLARIS pMemToMapSolaris = (PRTR0MEMOBJSOLARIS)pMemToMap;
|
---|
411 | size_t size = pMemToMapSolaris->Core.cb;
|
---|
412 | proc_t *userproc = (proc_t *)R0Process;
|
---|
413 | struct as *useras = userproc->p_as;
|
---|
414 | void *pv = pMemToMapSolaris->Core.pv;
|
---|
415 | pgcnt_t cPages = btop(size);
|
---|
416 | pgcnt_t iPage;
|
---|
417 | caddr_t addr;
|
---|
418 | int rc;
|
---|
419 |
|
---|
420 | /* Create the mapping object */
|
---|
421 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, size);
|
---|
422 | if (!pMemSolaris)
|
---|
423 | return VERR_NO_MEMORY;
|
---|
424 |
|
---|
425 | as_rangelock(useras);
|
---|
426 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
427 | {
|
---|
428 | /* Use user specified address */
|
---|
429 | addr = (caddr_t)R3PtrFixed;
|
---|
430 |
|
---|
431 | /* Verify user address (a bit paranoid) */
|
---|
432 | rc = valid_usr_range(addr, size, fProt, useras, (caddr_t)USERLIMIT32);
|
---|
433 | if (rc != RANGE_OKAY)
|
---|
434 | {
|
---|
435 | as_rangeunlock(useras);
|
---|
436 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: valid_usr_range failed, returned %d\n", rc);
|
---|
437 | return VERR_INVALID_POINTER;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /* Blow away any previous mapping */
|
---|
441 | as_unmap(useras, addr, size);
|
---|
442 | }
|
---|
443 | else
|
---|
444 | {
|
---|
445 | /* Let the system choose an address */
|
---|
446 | map_addr(&addr, size, 0, 1, MAP_SHARED | MAP_ANONYMOUS);
|
---|
447 | if (addr == NULL)
|
---|
448 | {
|
---|
449 | as_rangeunlock(useras);
|
---|
450 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr failed\n");
|
---|
451 | return VERR_MAP_FAILED;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /* Check address against alignment, fail if it doesn't match */
|
---|
455 | if ((uintptr_t)addr & (uAlignment - 1))
|
---|
456 | {
|
---|
457 | as_rangeunlock(useras);
|
---|
458 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: map_addr alignment(%ld) failed.\n", uAlignment);
|
---|
459 | return VERR_MAP_FAILED;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /* Our protection masks are identical to <sys/mman.h> but we
|
---|
464 | * need to add PROT_USER for the pages to be accessible by user
|
---|
465 | */
|
---|
466 | struct segvn_crargs crArgs = SEGVN_ZFOD_ARGS(fProt | PROT_USER, PROT_ALL);
|
---|
467 | rc = as_map(useras, addr, size, segvn_create, &crArgs);
|
---|
468 | as_rangeunlock(useras);
|
---|
469 | if (rc != 0)
|
---|
470 | {
|
---|
471 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: as_map failure.\n");
|
---|
472 | return VERR_MAP_FAILED;
|
---|
473 | }
|
---|
474 |
|
---|
475 | #if 0
|
---|
476 | /* Lock down the pages and get the shadow page list
|
---|
477 | * In this case we must as_pageunlock if(ppShadowPages) exists while freeing CONT, PAGE
|
---|
478 | */
|
---|
479 | rc = as_pagelock(&kas, &pMemToMapSolaris->ppShadowPages, pv, size, S_WRITE);
|
---|
480 | if (rc != 0 || pMemToMapSolaris->ppShadowPages == NULL)
|
---|
481 | {
|
---|
482 | cmn_err(CE_NOTE, "rtR0MemObjNativeMapUser: as_pagelock failed\n");
|
---|
483 | as_unmap(useras, addr, size);
|
---|
484 | return VERR_NO_MEMORY;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /* Map each page into user space */
|
---|
488 | rw_enter(&useras->a_lock, RW_READER);
|
---|
489 | caddr_t pageAddr = addr;
|
---|
490 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
491 | {
|
---|
492 | hat_memload(useras->a_hat, pageAddr, pMemToMapSolaris->ppShadowPages[iPage], fProt | PROT_USER,
|
---|
493 | HAT_LOAD_NOCONSIST | HAT_STRICTORDER | HAT_LOAD_LOCK);
|
---|
494 | pageAddr += ptob(1);
|
---|
495 | }
|
---|
496 | rw_exit(&useras->a_lock, RW_READER);
|
---|
497 | #else
|
---|
498 | /* Map each page into user space */
|
---|
499 | rw_enter(&useras->a_lock, RW_READER);
|
---|
500 | caddr_t kernAddr = pv;
|
---|
501 | caddr_t pageAddr = addr;
|
---|
502 | for (iPage = 0; iPage < cPages; iPage++)
|
---|
503 | {
|
---|
504 | page_t *pp = page_numtopp_nolock(hat_getpfnum(kas.a_hat, kernAddr));
|
---|
505 | hat_memload(useras->a_hat, pageAddr, pp, (fProt | PROT_USER), HAT_LOAD_LOCK);
|
---|
506 | pageAddr += ptob(1);
|
---|
507 | kernAddr += ptob(1);
|
---|
508 | }
|
---|
509 | rw_exit(&useras->a_lock);
|
---|
510 | #endif
|
---|
511 |
|
---|
512 | pMemSolaris->Core.u.Mapping.R0Process = (RTR0PROCESS)userproc;
|
---|
513 | pMemSolaris->Core.pv = addr;
|
---|
514 | *ppMem = &pMemSolaris->Core;
|
---|
515 | return VINF_SUCCESS;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, unsigned iPage)
|
---|
520 | {
|
---|
521 | PRTR0MEMOBJSOLARIS pMemSolaris = (PRTR0MEMOBJSOLARIS)pMem;
|
---|
522 |
|
---|
523 | switch (pMemSolaris->Core.enmType)
|
---|
524 | {
|
---|
525 | case RTR0MEMOBJTYPE_PAGE:
|
---|
526 | case RTR0MEMOBJTYPE_LOW:
|
---|
527 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
528 | {
|
---|
529 | uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
|
---|
530 | return rtR0MemObjSolarisVirtToPhys(kas.a_hat, pb);
|
---|
531 | }
|
---|
532 |
|
---|
533 | case RTR0MEMOBJTYPE_LOCK:
|
---|
534 | {
|
---|
535 | struct hat *hatSpace;
|
---|
536 | if (pMemSolaris->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
|
---|
537 | {
|
---|
538 | /* User */
|
---|
539 | proc_t *userProc = (proc_t *)pMemSolaris->Core.u.Lock.R0Process;
|
---|
540 | hatSpace = userProc->p_as->a_hat;
|
---|
541 | }
|
---|
542 | else /* Kernel */
|
---|
543 | hatSpace = kas.a_hat;
|
---|
544 |
|
---|
545 | uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
|
---|
546 | return rtR0MemObjSolarisVirtToPhys(hatSpace, pb);
|
---|
547 | }
|
---|
548 |
|
---|
549 | case RTR0MEMOBJTYPE_CONT:
|
---|
550 | return pMemSolaris->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
|
---|
551 |
|
---|
552 | case RTR0MEMOBJTYPE_PHYS:
|
---|
553 | return pMemSolaris->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
|
---|
554 |
|
---|
555 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
556 | AssertFailed(/* not implemented */);
|
---|
557 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
558 | default:
|
---|
559 | return NIL_RTHCPHYS;
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|