VirtualBox

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

Last change on this file since 40966 was 40966, checked in by vboxsync, 13 years ago

Runtime/r0drv/solaris: Dissolve VBI into IPRT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.9 KB
Line 
1/* $Id: memobj-r0drv-solaris.c 40966 2012-04-17 16:43:28Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "../the-solaris-kernel.h"
32#include "internal/iprt.h"
33#include <iprt/memobj.h>
34
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#include <iprt/err.h>
38#include <iprt/log.h>
39#include <iprt/mem.h>
40#include <iprt/param.h>
41#include <iprt/process.h>
42#include "internal/memobj.h"
43#include "memobj-r0drv-solaris.h"
44
45#define SOL_IS_KRNL_ADDR(vx) ((uintptr_t)(vx) >= kernelbase)
46static vnode_t s_PageVnode;
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * The Solaris version of the memory object structure.
53 */
54typedef struct RTR0MEMOBJSOL
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 *pvHandle;
62 /** Access during locking. */
63 int fAccess;
64 /** Set if large pages are involved in an RTR0MEMOBJTYPE_PHYS
65 * allocation. */
66 bool fLargePage;
67} RTR0MEMOBJSOL, *PRTR0MEMOBJSOL;
68
69
70/**
71 * Returns the physical address for a virtual address.
72 *
73 * @param pv The virtual address.
74 *
75 * @returns The physical address corresponding to @a pv.
76 */
77static uint64_t rtR0MemObjSolVirtToPhys(void *pv)
78{
79 struct hat *pHat = NULL;
80 pfn_t PageFrameNum = 0;
81 uintptr_t uVirtAddr = (uintptr_t)pv;
82
83 if (SOL_IS_KRNL_ADDR(pv))
84 pHat = kas.a_hat;
85 else
86 {
87 proc_t *pProcess = (proc_t *)RTR0ProcHandleSelf();
88 AssertRelease(pProcess);
89 pHat = pProcess->p_as->a_hat;
90 }
91
92 PageFrameNum = hat_getpfnum(pHat, (caddr_t)(uVirtAddr & PAGEMASK));
93 AssertReleaseMsg(PageFrameNum != PFN_INVALID, ("rtR0MemObjSolVirtToPhys failed. pv=%p\n", pv));
94 return (((uint64_t)PageFrameNum << PAGESHIFT) | (uVirtAddr & PAGEOFFSET));
95}
96
97
98/**
99 * Returns the physical address of a page from an array of pages.
100 *
101 * @param ppPages The array of pages.
102 * @param iPage Index of the page in the array to get the physical
103 * address.
104 *
105 * @returns Physical address of specific page within the list of pages specified
106 * in @a ppPages.
107 */
108static inline uint64_t rtR0MemObjSolPageToPhys(page_t **ppPages, size_t iPage)
109{
110 pfn_t PageFrameNum = page_pptonum(ppPages[iPage]);
111 AssertReleaseMsg(PageFrameNum != PFN_INVALID, ("rtR0MemObjSolPageToPhys failed. ppPages=%p iPage=%u\n", ppPages, iPage));
112 return (uint64_t)PageFrameNum << PAGESHIFT;
113}
114
115
116/**
117 * Retreives a free page from the kernel freelist.
118 *
119 * @param virtAddr The virtual address to which this page maybe mapped in
120 * the future.
121 * @param cbPage The size of the page.
122 *
123 * @returns Pointer to the allocated page, NULL on failure.
124 */
125static page_t *rtR0MemObjSolPageFromFreelist(caddr_t virtAddr, size_t cbPage)
126{
127 seg_t KernelSeg;
128 KernelSeg.s_as = &kas;
129 page_t *pPage = page_get_freelist(&s_PageVnode, 0 /* offset */, &KernelSeg, virtAddr,
130 cbPage, 0 /* flags */, NULL /* NUMA group */);
131 if ( !pPage
132 && g_frtSolUseKflt)
133 {
134 pPage = page_get_freelist(&s_PageVnode, 0 /* offset */, &KernelSeg, virtAddr,
135 cbPage, 0x200 /* PG_KFLT */, NULL /* NUMA group */);
136 }
137 return pPage;
138}
139
140
141/**
142 * Retrieves a free page from the kernel cachelist.
143 *
144 * @param virtAddr The virtual address to which this page maybe mapped in
145 * the future.
146 * @param cbPage The size of the page.
147 *
148 * @return Pointer to the allocated page, NULL on failure.
149 */
150static page_t *rtR0MemObjSolPageFromCachelist(caddr_t virtAddr, size_t cbPage)
151{
152 seg_t KernelSeg;
153 KernelSeg.s_as = &kas;
154 page_t *pPage = page_get_cachelist(&s_PageVnode, 0 /* offset */, &KernelSeg, virtAddr,
155 0 /* flags */, NULL /* NUMA group */);
156 if ( !pPage
157 && g_frtSolUseKflt)
158 {
159 pPage = page_get_cachelist(&s_PageVnode, 0 /* offset */, &KernelSeg, virtAddr,
160 0x200 /* PG_KFLT */, NULL /* NUMA group */);
161 }
162
163 /*
164 * Remove association with the vnode for pages from the cachelist.
165 */
166 if (!PP_ISAGED(pPage))
167 page_hashout(pPage, NULL /* mutex */);
168
169 return pPage;
170}
171
172
173/**
174 * Allocates physical non-contiguous memory.
175 *
176 * @param uPhysHi The upper physical address limit (inclusive).
177 * @param puPhys Where to store the physical address of first page. Optional,
178 * can be NULL.
179 * @param cb The size of the allocation.
180 *
181 * @return Array of allocated pages, NULL on failure.
182 */
183static page_t **rtR0MemObjSolPagesAlloc(uint64_t uPhysHi, uint64_t *puPhys, size_t cb)
184{
185 /** @todo We need to satisfy the upper physical address constraint */
186
187 /*
188 * The page freelist and cachelist both hold pages that are not mapped into any address space.
189 * The cachelist is not really free pages but when memory is exhausted they'll be moved to the
190 * free lists, it's the total of the free+cache list that we see on the 'free' column in vmstat.
191 *
192 * Reserve available memory for pages and create the pages.
193 */
194 pgcnt_t cPages = (cb + PAGESIZE - 1) >> PAGESHIFT;
195 int rc = page_resv(cPages, KM_NOSLEEP);
196 if (rc)
197 {
198 rc = page_create_wait(cPages, 0 /* flags */);
199 if (rc)
200 {
201 size_t cbPages = cPages * sizeof(page_t *);
202 page_t **ppPages = kmem_zalloc(cbPages, KM_SLEEP);
203 if (RT_LIKELY(ppPages))
204 {
205 /*
206 * Get pages from kseg, the 'virtAddr' here is only for colouring but unfortunately
207 * we don't yet have the 'virtAddr' to which this memory may be mapped.
208 */
209 caddr_t virtAddr = NULL;
210 for (size_t i = 0; i < cPages; i++, virtAddr += PAGESIZE)
211 {
212 /*
213 * Get a page from the freelist or cachelist.
214 */
215 page_t *pPage = rtR0MemObjSolPageFromFreelist(virtAddr, PAGESIZE);
216 if (!pPage)
217 pPage = rtR0MemObjSolPageFromCachelist(virtAddr, PAGESIZE);
218 if (RT_UNLIKELY(!pPage))
219 {
220 /*
221 * No more pages found, release was grabbed so far.
222 */
223 page_create_putback(cPages - i);
224 while (--i >= 0)
225 page_free(ppPages[i], 0 /* don't need page, move to tail of pagelist */);
226 kmem_free(ppPages, cbPages);
227 page_unresv(cPages);
228 return NULL;
229 }
230
231 PP_CLRFREE(pPage); /* Page is no longer free */
232 PP_CLRAGED(pPage); /* Page is not hashed in */
233 ppPages[i] = pPage;
234 }
235
236 /*
237 * We now have the pages locked exclusively, before they are mapped in
238 * we must downgrade the lock.
239 */
240 if (puPhys)
241 *puPhys = (uint64_t)page_pptonum(ppPages[0]) << PAGESHIFT;
242 return ppPages;
243 }
244
245 page_create_putback(cPages);
246 }
247
248 page_unresv(cPages);
249 }
250
251 return NULL;
252}
253
254
255/**
256 * Prepares pages allocated by rtR0MemObjSolPagesAlloc for mapping.
257 *
258 * @param ppPages Pointer to the page list.
259 * @param cb Size of the allocation.
260 * @param auPhys Where to store the physical address of the premapped
261 * pages.
262 * @param cPages The number of pages (entries) in @a auPhys.
263 *
264 * @returns IPRT status code.
265 */
266static int rtR0MemObjSolPagesPreMap(page_t **ppPages, size_t cb, uint64_t auPhys[], size_t cPages)
267{
268 AssertPtrReturn(ppPages, VERR_INVALID_PARAMETER);
269 AssertPtrReturn(auPhys, VERR_INVALID_PARAMETER);
270
271 for (size_t iPage = 0; iPage < cPages; iPage++)
272 {
273 /*
274 * Prepare pages for mapping into kernel/user-space. Downgrade the
275 * exclusive page lock to a shared lock if necessary.
276 */
277 if (page_tryupgrade(ppPages[iPage]) == 1)
278 page_downgrade(ppPages[iPage]);
279
280 auPhys[iPage] = rtR0MemObjSolPageToPhys(ppPages, iPage);
281 }
282
283 return VINF_SUCCESS;
284}
285
286
287/**
288 * Frees pages allocated by rtR0MemObjSolPagesAlloc.
289 *
290 * @param ppPages Pointer to the page list.
291 * @param cbPages Size of the allocation.
292 */
293static void rtR0MemObjSolPagesFree(page_t **ppPages, size_t cb)
294{
295 size_t cPages = (cb + PAGESIZE - 1) >> PAGESHIFT;
296 size_t cbPages = cPages * sizeof(page_t *);
297 for (size_t iPage = 0; iPage < cPages; iPage++)
298 {
299 /*
300 * We need to exclusive lock the pages before freeing them.
301 */
302 int rc = page_tryupgrade(ppPages[iPage]);
303 if (!rc)
304 {
305 page_unlock(ppPages[iPage]);
306 while (!page_lock(ppPages[iPage], SE_EXCL, NULL /* mutex */, P_RECLAIM))
307 {
308 /* nothing */;
309 }
310 }
311 page_free(ppPages[iPage], 0 /* don't need page, move to tail of pagelist */);
312 }
313 kmem_free(ppPages, cbPages);
314 page_unresv(cPages);
315}
316
317
318/**
319 * Allocates a large page to cover the required allocation size.
320 *
321 * @param puPhys Where to store the physical address of the allocated
322 * page. Optional, can be NULL.
323 * @param cb Size of the allocation.
324 *
325 * @returns Pointer to the allocated large page, NULL on failure.
326 */
327static page_t *rtR0MemObjSolLargePageAlloc(uint64_t *puPhys, size_t cb)
328{
329 /*
330 * Reserve available memory and create the sub-pages.
331 */
332 const pgcnt_t cPages = cb >> PAGESHIFT;
333 int rc = page_resv(cPages, KM_NOSLEEP);
334 if (rc)
335 {
336 rc = page_create_wait(cPages, 0 /* flags */);
337 if (rc)
338 {
339 /*
340 * Get a page off the free list. We set virtAddr to 0 since we don't know where
341 * the memory is going to be mapped.
342 */
343 seg_t KernelSeg;
344 caddr_t virtAddr = NULL;
345 KernelSeg.s_as = &kas;
346 page_t *pRootPage = rtR0MemObjSolPageFromFreelist(virtAddr, cb);
347 if (pRootPage)
348 {
349 AssertMsg(!(page_pptonum(pRootPage) & (cPages - 1)), ("%p:%lx cPages=%lx\n", pRootPage, page_pptonum(pRootPage), cPages));
350
351 /*
352 * Mark all the sub-pages as non-free and not-hashed-in.
353 * It is paramount that we destroy the list (before freeing it).
354 */
355 page_t *pPageList = pRootPage;
356 for (size_t iPage = 0; iPage < cPages; iPage++)
357 {
358 page_t *pPage = pPageList;
359 AssertPtr(pPage);
360 AssertMsg(page_pptonum(pPage) == iPage + page_pptonum(pRootPage),
361 ("%p:%lx %lx+%lx\n", pPage, page_pptonum(pPage), iPage, page_pptonum(pRootPage)));
362 page_sub(&pPageList, pPage);
363
364 /*
365 * Ensure page is now be free and the page size-code must match that of the root page.
366 */
367 AssertMsg(PP_ISFREE(pPage), ("%p\n", pPage));
368 AssertMsg(pPage->p_szc == pRootPage->p_szc, ("%p - %d expected %d \n", pPage, pPage->p_szc, pRootPage->p_szc));
369
370 PP_CLRFREE(pPage); /* Page no longer free */
371 PP_CLRAGED(pPage); /* Page no longer hashed-in */
372 }
373
374 uint64_t uPhys = (uint64_t)page_pptonum(pRootPage) << PAGESHIFT;
375 AssertMsg(!(uPhys & (cb - 1)), ("%llx %zx\n", uPhys, cb));
376 if (puPhys)
377 *puPhys = uPhys;
378
379 return pRootPage;
380 }
381
382 page_create_putback(cPages);
383 }
384
385 page_unresv(cPages);
386 }
387
388 return NULL;
389}
390
391/**
392 * Prepares the large page allocated by rtR0MemObjSolLargePageAlloc to be mapped.
393 *
394 * @param pRootPage Pointer to the root page.
395 * @param cb Size of the allocation.
396 *
397 * @returns IPRT status code.
398 */
399static int rtR0MemObjSolLargePagePreMap(page_t *pRootPage, size_t cb)
400{
401 const pgcnt_t cPages = cb >> PAGESHIFT;
402
403 Assert(page_get_pagecnt(pRootPage->p_szc) == cPages);
404 AssertMsg(!(page_pptonum(pRootPage) & (cPages - 1)), ("%p:%lx npages=%lx\n", pRootPage, page_pptonum(pRootPage), cPages));
405
406 /*
407 * We need to downgrade the sub-pages from exclusive to shared locking
408 * because otherweise we cannot <you go figure>.
409 */
410 for (pgcnt_t iPage = 0; iPage < cPages; iPage++)
411 {
412 page_t *pPage = page_nextn(pRootPage, iPage);
413 AssertMsg(page_pptonum(pPage) == iPage + page_pptonum(pRootPage),
414 ("%p:%lx %lx+%lx\n", pPage, page_pptonum(pPage), iPage, page_pptonum(pRootPage)));
415 AssertMsg(!PP_ISFREE(pPage), ("%p\n", pPage));
416
417 if (page_tryupgrade(pPage) == 1)
418 page_downgrade(pPage);
419 AssertMsg(!PP_ISFREE(pPage), ("%p\n", pPage));
420 }
421
422 return VINF_SUCCESS;
423}
424
425
426/**
427 * Frees the page allocated by rtR0MemObjSolLargePageAlloc.
428 *
429 * @param pRootPage Pointer to the root page.
430 * @param cb Allocated size.
431 */
432static void rtR0MemObjSolLargePageFree(page_t *pRootPage, size_t cb)
433{
434 pgcnt_t cPages = cb >> PAGESHIFT;
435
436 Assert(page_get_pagecnt(pRootPage->p_szc) == cPages);
437 AssertMsg(!(page_pptonum(pRootPage) & (cPages - 1)), ("%p:%lx cPages=%lx\n", pRootPage, page_pptonum(pRootPage), cPages));
438
439 /*
440 * We need to exclusively lock the sub-pages before freeing the large one.
441 */
442 for (pgcnt_t iPage = 0; iPage < cPages; iPage++)
443 {
444 page_t *pPage = page_nextn(pRootPage, iPage);
445 AssertMsg(page_pptonum(pPage) == iPage + page_pptonum(pRootPage),
446 ("%p:%lx %lx+%lx\n", pPage, page_pptonum(pPage), iPage, page_pptonum(pRootPage)));
447 AssertMsg(!PP_ISFREE(pPage), ("%p\n", pPage));
448
449 int rc = page_tryupgrade(pPage);
450 if (!rc)
451 {
452 page_unlock(pPage);
453 while (!page_lock(pPage, SE_EXCL, NULL /* mutex */, P_RECLAIM))
454 {
455 /* nothing */;
456 }
457 }
458 }
459
460 /*
461 * Free the large page and unreserve the memory.
462 */
463 page_free_pages(pRootPage);
464 page_unresv(cPages);
465
466}
467
468
469/**
470 * Unmaps kernel/user-space mapped memory.
471 *
472 * @param pv Pointer to the mapped memory block.
473 * @param cb Size of the memory block.
474 */
475static void rtR0MemObjSolUnmap(void *pv, size_t cb)
476{
477 if (SOL_IS_KRNL_ADDR(pv))
478 {
479 hat_unload(kas.a_hat, pv, cb, HAT_UNLOAD | HAT_UNLOAD_UNLOCK);
480 vmem_free(heap_arena, pv, cb);
481 }
482 else
483 {
484 struct as *pAddrSpace = ((proc_t *)RTR0ProcHandleSelf())->p_as;
485 AssertPtr(pAddrSpace);
486 as_rangelock(pAddrSpace);
487 as_unmap(pAddrSpace, pv, cb);
488 as_rangeunlock(pAddrSpace);
489 }
490}
491
492/**
493 * Lock down memory mappings for a virtual address.
494 *
495 * @param pv Pointer to the memory to lock down.
496 * @param cb Size of the memory block.
497 * @param fAccess Page access rights (S_READ, S_WRITE, S_EXEC)
498 *
499 * @returns IPRT status code.
500 */
501static int rtR0MemObjSolLock(void *pv, size_t cb, int fPageAccess)
502{
503 /*
504 * Kernel memory mappings on x86/amd64 are always locked, only handle user-space memory.
505 */
506 if (!SOL_IS_KRNL_ADDR(pv))
507 {
508 proc_t *pProc = (proc_t *)RTR0ProcHandleSelf();
509 AssertPtr(pProc);
510 faultcode_t rc = as_fault(pProc->p_as->a_hat, pProc->p_as, (caddr_t)pv, cb, F_SOFTLOCK, fPageAccess);
511 if (rc)
512 {
513 LogRel(("rtR0MemObjSolLock failed for pv=%pv cb=%lx fPageAccess=%d rc=%d\n", pv, cb, fPageAccess, rc));
514 return VERR_LOCK_FAILED;
515 }
516 }
517 return VINF_SUCCESS;
518}
519
520
521/**
522 * Unlock memory mappings for a virtual address.
523 *
524 * @param pv Pointer to the locked memory.
525 * @param cb Size of the memory block.
526 * @param fPageAccess Page access rights (S_READ, S_WRITE, S_EXEC).
527 */
528static void rtR0MemObjSolUnlock(void *pv, size_t cb, int fPageAccess)
529{
530 if (!SOL_IS_KRNL_ADDR(pv))
531 {
532 proc_t *pProcess = (proc_t *)RTR0ProcHandleSelf();
533 AssertPtr(pProcess);
534 as_fault(pProcess->p_as->a_hat, pProcess->p_as, (caddr_t)pv, cb, F_SOFTUNLOCK, fPageAccess);
535 }
536}
537
538
539/**
540 * Maps a list of physical pages into user address space.
541 *
542 * @param pVirtAddr Where to store the virtual address of the mapping.
543 * @param fPageAccess Page access rights (PROT_READ, PROT_WRITE,
544 * PROT_EXEC)
545 * @param paPhysAddrs Array of physical addresses to pages.
546 * @param cb Size of memory being mapped.
547 *
548 * @returns IPRT status code.
549 */
550static int rtR0MemObjSolUserMap(caddr_t *pVirtAddr, unsigned fPageAccess, uint64_t *paPhysAddrs, size_t cb)
551{
552 struct as *pAddrSpace = ((proc_t *)RTR0ProcHandleSelf())->p_as;
553 int rc = VERR_INTERNAL_ERROR;
554 SEGVBOX_CRARGS Args;
555
556 Args.paPhysAddrs = paPhysAddrs;
557 Args.fPageAccess = fPageAccess;
558
559 as_rangelock(pAddrSpace);
560 map_addr(pVirtAddr, cb, 0 /* offset */, 0 /* vacalign */, MAP_SHARED);
561 if (*pVirtAddr != NULL)
562 rc = as_map(pAddrSpace, *pVirtAddr, cb, rtR0SegVBoxSolCreate, &Args);
563 else
564 rc = ENOMEM;
565 as_rangeunlock(pAddrSpace);
566
567 return RTErrConvertFromErrno(rc);
568}
569
570
571DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
572{
573 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)pMem;
574
575 switch (pMemSolaris->Core.enmType)
576 {
577 case RTR0MEMOBJTYPE_LOW:
578 rtR0SolMemFree(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
579 break;
580
581 case RTR0MEMOBJTYPE_PHYS:
582 if (pMemSolaris->Core.u.Phys.fAllocated)
583 {
584 if (pMemSolaris->fLargePage)
585 rtR0MemObjSolLargePageFree(pMemSolaris->pvHandle, pMemSolaris->Core.cb);
586 else
587 rtR0SolMemFree(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
588 }
589 break;
590
591 case RTR0MEMOBJTYPE_PHYS_NC:
592 rtR0MemObjSolPagesFree(pMemSolaris->pvHandle, pMemSolaris->Core.cb);
593 break;
594
595 case RTR0MEMOBJTYPE_PAGE:
596 ddi_umem_free(pMemSolaris->Cookie);
597 break;
598
599 case RTR0MEMOBJTYPE_LOCK:
600 rtR0MemObjSolUnlock(pMemSolaris->Core.pv, pMemSolaris->Core.cb, pMemSolaris->fAccess);
601 break;
602
603 case RTR0MEMOBJTYPE_MAPPING:
604 rtR0MemObjSolUnmap(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
605 break;
606
607 case RTR0MEMOBJTYPE_RES_VIRT:
608 {
609 if (pMemSolaris->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
610 vmem_xfree(heap_arena, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
611 else
612 AssertFailed();
613 break;
614 }
615
616 case RTR0MEMOBJTYPE_CONT: /* we don't use this type here. */
617 default:
618 AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
619 return VERR_INTERNAL_ERROR;
620 }
621
622 return VINF_SUCCESS;
623}
624
625
626DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
627{
628 /* Create the object. */
629 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
630 if (RT_UNLIKELY(!pMemSolaris))
631 return VERR_NO_MEMORY;
632
633 void *pvMem = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
634 if (RT_UNLIKELY(!pvMem))
635 {
636 rtR0MemObjDelete(&pMemSolaris->Core);
637 return VERR_NO_PAGE_MEMORY;
638 }
639
640 pMemSolaris->Core.pv = pvMem;
641 pMemSolaris->pvHandle = NULL;
642 *ppMem = &pMemSolaris->Core;
643 return VINF_SUCCESS;
644}
645
646
647DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
648{
649 NOREF(fExecutable);
650
651 /* Create the object */
652 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOW, NULL, cb);
653 if (!pMemSolaris)
654 return VERR_NO_MEMORY;
655
656 /* Allocate physically low page-aligned memory. */
657 uint64_t uPhysHi = _4G - 1;
658 void *pvMem = rtR0SolMemAlloc(uPhysHi, NULL /* puPhys */, cb, PAGESIZE, false /* fContig */);
659 if (RT_UNLIKELY(!pvMem))
660 {
661 rtR0MemObjDelete(&pMemSolaris->Core);
662 return VERR_NO_LOW_MEMORY;
663 }
664 pMemSolaris->Core.pv = pvMem;
665 pMemSolaris->pvHandle = NULL;
666 *ppMem = &pMemSolaris->Core;
667 return VINF_SUCCESS;
668}
669
670
671DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
672{
673 NOREF(fExecutable);
674 return rtR0MemObjNativeAllocPhys(ppMem, cb, _4G - 1, PAGE_SIZE /* alignment */);
675}
676
677
678DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
679{
680#if HC_ARCH_BITS == 64
681 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS_NC, NULL, cb);
682 if (RT_UNLIKELY(!pMemSolaris))
683 return VERR_NO_MEMORY;
684
685 uint64_t PhysAddr = UINT64_MAX;
686 void *pvPages = rtR0MemObjSolPagesAlloc((uint64_t)PhysHighest, &PhysAddr, cb);
687 if (!pvPages)
688 {
689 LogRel(("rtR0MemObjNativeAllocPhysNC: rtR0MemObjSolPagesAlloc failed for cb=%u.\n", cb));
690 rtR0MemObjDelete(&pMemSolaris->Core);
691 return VERR_NO_MEMORY;
692 }
693 pMemSolaris->Core.pv = NULL;
694 pMemSolaris->pvHandle = pvPages;
695
696 Assert(PhysAddr != UINT64_MAX);
697 Assert(!(PhysAddr & PAGE_OFFSET_MASK));
698 *ppMem = &pMemSolaris->Core;
699 return VINF_SUCCESS;
700
701#else /* 32 bit: */
702 return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
703#endif
704}
705
706
707DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
708{
709 AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%RHp\n", PhysHighest), VERR_NOT_SUPPORTED);
710
711 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
712 if (RT_UNLIKELY(!pMemSolaris))
713 return VERR_NO_MEMORY;
714
715 /*
716 * Allocating one large page gets special treatment.
717 */
718 static uint32_t s_cbLargePage = UINT32_MAX;
719 if (s_cbLargePage == UINT32_MAX)
720 {
721#if 0 /* currently not entirely stable, so disabled. */
722 if (page_num_pagesizes() > 1)
723 ASMAtomicWriteU32(&s_cbLargePage, page_get_pagesize(1));
724 else
725#endif
726 ASMAtomicWriteU32(&s_cbLargePage, 0);
727 }
728 uint64_t PhysAddr;
729 if ( cb == s_cbLargePage
730 && cb == uAlignment
731 && PhysHighest == NIL_RTHCPHYS)
732 {
733 /*
734 * Allocate one large page.
735 */
736 cmn_err(CE_NOTE, "calling rtR0MemObjSolLargePageAlloc\n");
737 void *pvPages = rtR0MemObjSolLargePageAlloc(&PhysAddr, cb);
738 if (RT_LIKELY(pvPages))
739 {
740 AssertMsg(!(PhysAddr & (cb - 1)), ("%RHp\n", PhysAddr));
741 pMemSolaris->Core.pv = NULL;
742 pMemSolaris->Core.u.Phys.PhysBase = PhysAddr;
743 pMemSolaris->Core.u.Phys.fAllocated = true;
744 pMemSolaris->pvHandle = pvPages;
745 pMemSolaris->fLargePage = true;
746
747 *ppMem = &pMemSolaris->Core;
748 return VINF_SUCCESS;
749 }
750 }
751 else
752 {
753 /*
754 * Allocate physically contiguous memory aligned as specified.
755 */
756 cmn_err(CE_NOTE, "rtR0MemObjNativeAllocPhys->rtR0SolMemAlloc\n");
757 AssertCompile(NIL_RTHCPHYS == UINT64_MAX);
758 PhysAddr = PhysHighest;
759 void *pvMem = rtR0SolMemAlloc(PhysHighest, &PhysAddr, cb, uAlignment, true /* fContig */);
760 if (RT_LIKELY(pvMem))
761 {
762 Assert(!(PhysAddr & PAGE_OFFSET_MASK));
763 Assert(PhysAddr < PhysHighest);
764 Assert(PhysAddr + cb <= PhysHighest);
765
766 pMemSolaris->Core.pv = pvMem;
767 pMemSolaris->Core.u.Phys.PhysBase = PhysAddr;
768 pMemSolaris->Core.u.Phys.fAllocated = true;
769 pMemSolaris->pvHandle = NULL;
770 pMemSolaris->fLargePage = false;
771
772 *ppMem = &pMemSolaris->Core;
773 return VINF_SUCCESS;
774 }
775 }
776 rtR0MemObjDelete(&pMemSolaris->Core);
777 return VERR_NO_CONT_MEMORY;
778}
779
780
781DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
782{
783 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
784
785 /* Create the object. */
786 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
787 if (!pMemSolaris)
788 return VERR_NO_MEMORY;
789
790 /* There is no allocation here, it needs to be mapped somewhere first. */
791 pMemSolaris->Core.u.Phys.fAllocated = false;
792 pMemSolaris->Core.u.Phys.PhysBase = Phys;
793 pMemSolaris->Core.u.Phys.uCachePolicy = uCachePolicy;
794 *ppMem = &pMemSolaris->Core;
795 return VINF_SUCCESS;
796}
797
798
799DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess,
800 RTR0PROCESS R0Process)
801{
802 AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
803 NOREF(fAccess);
804
805 /* Create the locking object */
806 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
807 if (!pMemSolaris)
808 return VERR_NO_MEMORY;
809
810 /* Lock down user pages. */
811 int fPageAccess = S_READ;
812 if (fAccess & RTMEM_PROT_WRITE)
813 fPageAccess = S_WRITE;
814 if (fAccess & RTMEM_PROT_EXEC)
815 fPageAccess = S_EXEC;
816 int rc = rtR0MemObjSolLock((void *)R3Ptr, cb, fPageAccess);
817 if (RT_FAILURE(rc))
818 {
819 LogRel(("rtR0MemObjNativeLockUser: rtR0MemObjSolLock failed rc=%d\n", rc));
820 rtR0MemObjDelete(&pMemSolaris->Core);
821 return rc;
822 }
823
824 /* Fill in the object attributes and return successfully. */
825 pMemSolaris->Core.u.Lock.R0Process = R0Process;
826 pMemSolaris->pvHandle = NULL;
827 pMemSolaris->fAccess = fPageAccess;
828 *ppMem = &pMemSolaris->Core;
829 return VINF_SUCCESS;
830}
831
832
833DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
834{
835 NOREF(fAccess);
836
837 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
838 if (!pMemSolaris)
839 return VERR_NO_MEMORY;
840
841 /* Lock down kernel pages. */
842 int fPageAccess = S_READ;
843 if (fAccess & RTMEM_PROT_WRITE)
844 fPageAccess = S_WRITE;
845 if (fAccess & RTMEM_PROT_EXEC)
846 fPageAccess = S_EXEC;
847 int rc = rtR0MemObjSolLock(pv, cb, fPageAccess);
848 if (RT_FAILURE(rc))
849 {
850 LogRel(("rtR0MemObjNativeLockKernel: rtR0MemObjSolLock failed rc=%d\n", rc));
851 rtR0MemObjDelete(&pMemSolaris->Core);
852 return rc;
853 }
854
855 /* Fill in the object attributes and return successfully. */
856 pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
857 pMemSolaris->pvHandle = NULL;
858 pMemSolaris->fAccess = fPageAccess;
859 *ppMem = &pMemSolaris->Core;
860 return VINF_SUCCESS;
861}
862
863
864DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
865{
866 PRTR0MEMOBJSOL pMemSolaris;
867
868 /*
869 * Use xalloc.
870 */
871 void *pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /* phase */, 0 /* nocross */,
872 NULL /* minaddr */, NULL /* maxaddr */, VM_SLEEP);
873 if (RT_UNLIKELY(!pv))
874 return VERR_NO_MEMORY;
875
876 /* Create the object. */
877 pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
878 if (!pMemSolaris)
879 {
880 LogRel(("rtR0MemObjNativeReserveKernel failed to alloc memory object.\n"));
881 vmem_xfree(heap_arena, pv, cb);
882 return VERR_NO_MEMORY;
883 }
884
885 pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
886 *ppMem = &pMemSolaris->Core;
887 return VINF_SUCCESS;
888}
889
890
891DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
892{
893 return VERR_NOT_SUPPORTED;
894}
895
896
897DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
898 unsigned fProt, size_t offSub, size_t cbSub)
899{
900 /** @todo rtR0MemObjNativeMapKernel / Solaris - Should be fairly simple alloc kernel memory and memload it. */
901 return VERR_NOT_SUPPORTED;
902}
903
904
905DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed,
906 size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
907{
908 /*
909 * Fend off things we cannot do.
910 */
911 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
912 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
913 if (uAlignment != PAGE_SIZE)
914 return VERR_NOT_SUPPORTED;
915
916 /*
917 * Get parameters from the source object.
918 */
919 PRTR0MEMOBJSOL pMemToMapSolaris = (PRTR0MEMOBJSOL)pMemToMap;
920 void *pv = pMemToMapSolaris->Core.pv;
921 size_t cb = pMemToMapSolaris->Core.cb;
922 size_t cPages = cb >> PAGE_SHIFT;
923
924 /*
925 * Create the mapping object
926 */
927 PRTR0MEMOBJSOL pMemSolaris;
928 pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, cb);
929 if (RT_UNLIKELY(!pMemSolaris))
930 return VERR_NO_MEMORY;
931
932 int rc = VINF_SUCCESS;
933 uint64_t *paPhysAddrs = kmem_zalloc(sizeof(uint64_t) * cPages, KM_SLEEP);
934 if (RT_LIKELY(paPhysAddrs))
935 {
936 /*
937 * Prepare the pages according to type.
938 */
939 if (pMemToMapSolaris->Core.enmType == RTR0MEMOBJTYPE_PHYS_NC)
940 rc = rtR0MemObjSolPagesPreMap(pMemToMapSolaris->pvHandle, cb, paPhysAddrs, cPages);
941 else if ( pMemToMapSolaris->Core.enmType == RTR0MEMOBJTYPE_PHYS
942 && pMemToMapSolaris->fLargePage)
943 {
944 RTHCPHYS Phys = pMemToMapSolaris->Core.u.Phys.PhysBase;
945 for (pgcnt_t iPage = 0; iPage < cPages; iPage++, Phys += PAGE_SIZE)
946 paPhysAddrs[iPage] = Phys;
947 rc = rtR0MemObjSolLargePagePreMap(pMemToMapSolaris->pvHandle, cb);
948 }
949 else
950 {
951 /*
952 * Have kernel mapping, just translate virtual to physical.
953 */
954 AssertPtr(pv);
955 rc = VINF_SUCCESS;
956 for (size_t iPage = 0; iPage < cPages; iPage++)
957 {
958 paPhysAddrs[iPage] = rtR0MemObjSolVirtToPhys(pv);
959 if (RT_UNLIKELY(paPhysAddrs[iPage] == -(uint64_t)1))
960 {
961 LogRel(("rtR0MemObjNativeMapUser: no page to map.\n"));
962 rc = VERR_MAP_FAILED;
963 break;
964 }
965 pv = (void *)((uintptr_t)pv + PAGE_SIZE);
966 }
967 }
968 if (RT_SUCCESS(rc))
969 {
970 unsigned fPageAccess = PROT_READ;
971 if (fProt & RTMEM_PROT_WRITE)
972 fPageAccess |= PROT_WRITE;
973 if (fProt & RTMEM_PROT_EXEC)
974 fPageAccess |= PROT_EXEC;
975
976 /*
977 * Perform the actual mapping.
978 */
979 caddr_t UserAddr = NULL;
980 rc = rtR0MemObjSolUserMap(&UserAddr, fPageAccess, paPhysAddrs, cb);
981 if (RT_SUCCESS(rc))
982 {
983 pMemSolaris->Core.u.Mapping.R0Process = R0Process;
984 pMemSolaris->Core.pv = UserAddr;
985
986 *ppMem = &pMemSolaris->Core;
987 kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
988 return VINF_SUCCESS;
989 }
990
991 LogRel(("rtR0MemObjNativeMapUser: rtR0MemObjSolUserMap failed rc=%d.\n", rc));
992 }
993
994 rc = VERR_MAP_FAILED;
995 kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
996 }
997 else
998 rc = VERR_NO_MEMORY;
999 rtR0MemObjDelete(&pMemSolaris->Core);
1000 return rc;
1001}
1002
1003
1004DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1005{
1006 NOREF(pMem);
1007 NOREF(offSub);
1008 NOREF(cbSub);
1009 NOREF(fProt);
1010 return VERR_NOT_SUPPORTED;
1011}
1012
1013
1014DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1015{
1016 PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)pMem;
1017
1018 switch (pMemSolaris->Core.enmType)
1019 {
1020 case RTR0MEMOBJTYPE_PHYS_NC:
1021 if (pMemSolaris->Core.u.Phys.fAllocated)
1022 {
1023 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
1024 return rtR0MemObjSolVirtToPhys(pb);
1025 }
1026 return rtR0MemObjSolPageToPhys(pMemSolaris->pvHandle, iPage);
1027
1028 case RTR0MEMOBJTYPE_PAGE:
1029 case RTR0MEMOBJTYPE_LOW:
1030 case RTR0MEMOBJTYPE_LOCK:
1031 {
1032 uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
1033 return rtR0MemObjSolVirtToPhys(pb);
1034 }
1035
1036 /*
1037 * Although mapping can be handled by rtR0MemObjSolVirtToPhys(offset) like the above case,
1038 * request it from the parent so that we have a clear distinction between CONT/PHYS_NC.
1039 */
1040 case RTR0MEMOBJTYPE_MAPPING:
1041 return rtR0MemObjNativeGetPagePhysAddr(pMemSolaris->Core.uRel.Child.pParent, iPage);
1042
1043 case RTR0MEMOBJTYPE_CONT:
1044 case RTR0MEMOBJTYPE_PHYS:
1045 AssertFailed(); /* handled by the caller */
1046 case RTR0MEMOBJTYPE_RES_VIRT:
1047 default:
1048 return NIL_RTHCPHYS;
1049 }
1050}
1051
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