1 | /* $Id: memobj-r0drv-solaris.c 41680 2012-06-13 11:19:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Ring-0 Memory Objects, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 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 | /*******************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *******************************************************************************/
|
---|
48 | #define SOL_IS_KRNL_ADDR(vx) ((uintptr_t)(vx) >= kernelbase)
|
---|
49 |
|
---|
50 |
|
---|
51 | /*******************************************************************************
|
---|
52 | * Structures and Typedefs *
|
---|
53 | *******************************************************************************/
|
---|
54 | /**
|
---|
55 | * The Solaris version of the memory object structure.
|
---|
56 | */
|
---|
57 | typedef struct RTR0MEMOBJSOL
|
---|
58 | {
|
---|
59 | /** The core structure. */
|
---|
60 | RTR0MEMOBJINTERNAL Core;
|
---|
61 | /** Pointer to kernel memory cookie. */
|
---|
62 | ddi_umem_cookie_t Cookie;
|
---|
63 | /** Shadow locked pages. */
|
---|
64 | void *pvHandle;
|
---|
65 | /** Access during locking. */
|
---|
66 | int fAccess;
|
---|
67 | /** Set if large pages are involved in an RTR0MEMOBJTYPE_PHYS
|
---|
68 | * allocation. */
|
---|
69 | bool fLargePage;
|
---|
70 | /** Whether we have individual pages or a kernel-mapped virtual memory block in
|
---|
71 | * an RTR0MEMOBJTYPE_PHYS_NC allocation. */
|
---|
72 | bool fIndivPages;
|
---|
73 | } RTR0MEMOBJSOL, *PRTR0MEMOBJSOL;
|
---|
74 |
|
---|
75 |
|
---|
76 | /*******************************************************************************
|
---|
77 | * Global Variables *
|
---|
78 | *******************************************************************************/
|
---|
79 | static vnode_t g_PageVnode;
|
---|
80 | static kmutex_t g_OffsetMtx;
|
---|
81 | static u_offset_t g_offPage;
|
---|
82 |
|
---|
83 | static vnode_t g_LargePageVnode;
|
---|
84 | static kmutex_t g_LargePageOffsetMtx;
|
---|
85 | static u_offset_t g_offLargePage;
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns the physical address for a virtual address.
|
---|
90 | *
|
---|
91 | * @param pv The virtual address.
|
---|
92 | *
|
---|
93 | * @returns The physical address corresponding to @a pv.
|
---|
94 | */
|
---|
95 | static uint64_t rtR0MemObjSolVirtToPhys(void *pv)
|
---|
96 | {
|
---|
97 | struct hat *pHat = NULL;
|
---|
98 | pfn_t PageFrameNum = 0;
|
---|
99 | uintptr_t uVirtAddr = (uintptr_t)pv;
|
---|
100 |
|
---|
101 | if (SOL_IS_KRNL_ADDR(pv))
|
---|
102 | pHat = kas.a_hat;
|
---|
103 | else
|
---|
104 | {
|
---|
105 | proc_t *pProcess = (proc_t *)RTR0ProcHandleSelf();
|
---|
106 | AssertRelease(pProcess);
|
---|
107 | pHat = pProcess->p_as->a_hat;
|
---|
108 | }
|
---|
109 |
|
---|
110 | PageFrameNum = hat_getpfnum(pHat, (caddr_t)(uVirtAddr & PAGEMASK));
|
---|
111 | AssertReleaseMsg(PageFrameNum != PFN_INVALID, ("rtR0MemObjSolVirtToPhys failed. pv=%p\n", pv));
|
---|
112 | return (((uint64_t)PageFrameNum << PAGE_SHIFT) | (uVirtAddr & PAGE_OFFSET_MASK));
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Returns the physical address for a page.
|
---|
118 | *
|
---|
119 | * @param pPage Pointer to the page.
|
---|
120 | *
|
---|
121 | * @returns The physical address for a page.
|
---|
122 | */
|
---|
123 | static inline uint64_t rtR0MemObjSolPagePhys(page_t *pPage)
|
---|
124 | {
|
---|
125 | AssertPtr(pPage);
|
---|
126 | pfn_t PageFrameNum = page_pptonum(pPage);
|
---|
127 | AssertReleaseMsg(PageFrameNum != PFN_INVALID, ("rtR0MemObjSolPagePhys failed pPage=%p\n"));
|
---|
128 | return (uint64_t)PageFrameNum << PAGE_SHIFT;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Allocates one page.
|
---|
134 | *
|
---|
135 | * @param virtAddr The virtual address to which this page maybe mapped in
|
---|
136 | * the future.
|
---|
137 | *
|
---|
138 | * @returns Pointer to the allocated page, NULL on failure.
|
---|
139 | */
|
---|
140 | static page_t *rtR0MemObjSolPageAlloc(caddr_t virtAddr)
|
---|
141 | {
|
---|
142 | u_offset_t offPage;
|
---|
143 | seg_t KernelSeg;
|
---|
144 |
|
---|
145 | mutex_enter(&g_OffsetMtx);
|
---|
146 | AssertCompileSize(u_offset_t, sizeof(uint64_t)); NOREF(RTASSERTVAR);
|
---|
147 | g_offPage = RT_ALIGN_64(g_offPage, PAGE_SIZE) + PAGE_SIZE;
|
---|
148 | offPage = g_offPage;
|
---|
149 | mutex_exit(&g_OffsetMtx);
|
---|
150 |
|
---|
151 | KernelSeg.s_as = &kas;
|
---|
152 | page_t *pPage = page_create_va(&g_PageVnode, offPage, PAGE_SIZE, PG_WAIT | PG_NORELOC, &KernelSeg, virtAddr);
|
---|
153 | if (RT_LIKELY(pPage))
|
---|
154 | {
|
---|
155 | /*
|
---|
156 | * Lock this page into memory "long term" to prevent this page from being paged out
|
---|
157 | * when we drop the page lock temporarily (during free).
|
---|
158 | */
|
---|
159 | page_pp_lock(pPage, 0 /* COW */, 1 /* Kernel */);
|
---|
160 | page_io_unlock(pPage);
|
---|
161 | page_downgrade(pPage);
|
---|
162 | Assert(PAGE_LOCKED_SE(pPage, SE_SHARED));
|
---|
163 | }
|
---|
164 |
|
---|
165 | return pPage;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Allocates physical, non-contiguous memory of pages.
|
---|
171 | *
|
---|
172 | * @param puPhys Where to store the physical address of first page. Optional,
|
---|
173 | * can be NULL.
|
---|
174 | * @param cb The size of the allocation.
|
---|
175 | *
|
---|
176 | * @return Array of allocated pages, NULL on failure.
|
---|
177 | */
|
---|
178 | static page_t **rtR0MemObjSolPagesAlloc(uint64_t *puPhys, size_t cb)
|
---|
179 | {
|
---|
180 | /*
|
---|
181 | * VM1:
|
---|
182 | * The page freelist and cachelist both hold pages that are not mapped into any address space.
|
---|
183 | * The cachelist is not really free pages but when memory is exhausted they'll be moved to the
|
---|
184 | * free lists, it's the total of the free+cache list that we see on the 'free' column in vmstat.
|
---|
185 | *
|
---|
186 | * VM2:
|
---|
187 | * @todo Document what happens behind the scenes in VM2 regarding the free and cachelist.
|
---|
188 | */
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Non-pageable memory reservation request for _4K pages, don't sleep.
|
---|
192 | */
|
---|
193 | size_t cPages = (cb + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
---|
194 | int rc = page_resv(cPages, KM_NOSLEEP);
|
---|
195 | if (rc)
|
---|
196 | {
|
---|
197 | size_t cbPages = cPages * sizeof(page_t *);
|
---|
198 | page_t **ppPages = kmem_zalloc(cbPages, KM_SLEEP);
|
---|
199 | if (RT_LIKELY(ppPages))
|
---|
200 | {
|
---|
201 | /*
|
---|
202 | * Get pages from kseg, the 'virtAddr' here is only for colouring but unfortunately
|
---|
203 | * we don't yet have the 'virtAddr' to which this memory may be mapped.
|
---|
204 | */
|
---|
205 | caddr_t virtAddr = 0;
|
---|
206 | for (size_t i = 0; i < cPages; i++, virtAddr += PAGE_SIZE)
|
---|
207 | {
|
---|
208 | /*
|
---|
209 | * Get a page from the free list locked exclusively. The page will be named (hashed in)
|
---|
210 | * and we rely on it during free. Downgrade the page to a shared lock to prevent the page
|
---|
211 | * from being relocated.
|
---|
212 | */
|
---|
213 | page_t *pPage = rtR0MemObjSolPageAlloc(virtAddr);
|
---|
214 | if (RT_UNLIKELY(!pPage))
|
---|
215 | {
|
---|
216 | /*
|
---|
217 | * No page found, release whatever pages we grabbed so far.
|
---|
218 | */
|
---|
219 | for (size_t k = 0; k < i; k++)
|
---|
220 | page_destroy(ppPages[k], 0 /* move it to the free list */);
|
---|
221 | kmem_free(ppPages, cbPages);
|
---|
222 | page_unresv(cPages);
|
---|
223 | return NULL;
|
---|
224 | }
|
---|
225 |
|
---|
226 | ppPages[i] = pPage;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (puPhys)
|
---|
230 | *puPhys = rtR0MemObjSolPagePhys(ppPages[0]);
|
---|
231 | return ppPages;
|
---|
232 | }
|
---|
233 |
|
---|
234 | page_unresv(cPages);
|
---|
235 | }
|
---|
236 |
|
---|
237 | return NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Frees the allocates pages.
|
---|
243 | *
|
---|
244 | * @param ppPages Pointer to the page list.
|
---|
245 | * @param cbPages Size of the allocation.
|
---|
246 | */
|
---|
247 | static void rtR0MemObjSolPagesFree(page_t **ppPages, size_t cb)
|
---|
248 | {
|
---|
249 | size_t cPages = (cb + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
---|
250 | size_t cbPages = cPages * sizeof(page_t *);
|
---|
251 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
252 | {
|
---|
253 | /*
|
---|
254 | * We need to exclusive lock the pages before freeing them.
|
---|
255 | */
|
---|
256 | page_t *pPage = ppPages[iPage];
|
---|
257 | u_offset_t offPage = pPage->p_offset;
|
---|
258 |
|
---|
259 | int rc = page_tryupgrade(ppPages[iPage]);
|
---|
260 | if (!rc)
|
---|
261 | {
|
---|
262 | page_unlock(pPage);
|
---|
263 | page_t *pFoundPage = page_lookup(&g_PageVnode, offPage, SE_EXCL);
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Since we allocated the pages as PG_NORELOC we should only get back the exact page always.
|
---|
267 | */
|
---|
268 | AssertReleaseMsg(pFoundPage == pPage, ("Page lookup failed %p:%llx returned %p, expected %p\n",
|
---|
269 | &g_PageVnode, offPage, pFoundPage, pPage));
|
---|
270 | }
|
---|
271 | Assert(PAGE_LOCKED_SE(pPage, SE_EXCL));
|
---|
272 | page_pp_unlock(pPage, 0 /* COW */, 1 /* Kernel */);
|
---|
273 | page_destroy(pPage, 0 /* move it to the free list */);
|
---|
274 | }
|
---|
275 | kmem_free(ppPages, cbPages);
|
---|
276 | page_unresv(cPages);
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Allocates one large page.
|
---|
282 | *
|
---|
283 | * @param puPhys Where to store the physical address of the allocated
|
---|
284 | * page. Optional, can be NULL.
|
---|
285 | * @param cbLargePage Size of the large page.
|
---|
286 | *
|
---|
287 | * @returns Pointer to a list of pages that cover the large page, NULL on
|
---|
288 | * failure.
|
---|
289 | */
|
---|
290 | static page_t **rtR0MemObjSolLargePageAlloc(uint64_t *puPhys, size_t cbLargePage)
|
---|
291 | {
|
---|
292 | /*
|
---|
293 | * Non-pageable memory reservation request for _4K pages, don't sleep.
|
---|
294 | */
|
---|
295 | size_t cPages = (cbLargePage + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
---|
296 | size_t cbPages = cPages * sizeof(page_t *);
|
---|
297 | u_offset_t offPage = 0;
|
---|
298 | int rc = page_resv(cPages, KM_NOSLEEP);
|
---|
299 | if (rc)
|
---|
300 | {
|
---|
301 | page_t **ppPages = kmem_zalloc(cbPages, KM_SLEEP);
|
---|
302 | if (RT_LIKELY(ppPages))
|
---|
303 | {
|
---|
304 | mutex_enter(&g_LargePageOffsetMtx);
|
---|
305 | AssertCompileSize(u_offset_t, sizeof(uint64_t)); NOREF(RTASSERTVAR);
|
---|
306 | g_offLargePage = RT_ALIGN_64(g_offLargePage, cbLargePage) + cbLargePage;
|
---|
307 | offPage = g_offLargePage;
|
---|
308 | mutex_exit(&g_LargePageOffsetMtx);
|
---|
309 |
|
---|
310 | seg_t KernelSeg;
|
---|
311 | KernelSeg.s_as = &kas;
|
---|
312 | page_t *pRootPage = page_create_va_large(&g_LargePageVnode, offPage, cbLargePage,
|
---|
313 | PG_EXCL, &KernelSeg, 0 /* vaddr */, NULL /* locality group */);
|
---|
314 | if (pRootPage)
|
---|
315 | {
|
---|
316 | /*
|
---|
317 | * Split it into sub-pages, downgrade each page to a shared lock to prevent page relocation.
|
---|
318 | */
|
---|
319 | page_t *pPageList = pRootPage;
|
---|
320 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
321 | {
|
---|
322 | page_t *pPage = pPageList;
|
---|
323 | AssertPtr(pPage);
|
---|
324 | AssertMsg(page_pptonum(pPage) == iPage + page_pptonum(pRootPage),
|
---|
325 | ("%p:%lx %lx+%lx\n", pPage, page_pptonum(pPage), iPage, page_pptonum(pRootPage)));
|
---|
326 | AssertMsg(pPage->p_szc == pRootPage->p_szc, ("Size code mismatch %p %d %d\n", pPage,
|
---|
327 | (int)pPage->p_szc, (int)pRootPage->p_szc));
|
---|
328 |
|
---|
329 | /*
|
---|
330 | * Lock the page into memory "long term". This prevents callers of page_try_demote_pages() (such as the
|
---|
331 | * pageout scanner) from demoting the large page into smaller pages while we temporarily release the
|
---|
332 | * exclusive lock (during free). We pass "0, 1" since we've already accounted for availrmem during
|
---|
333 | * page_resv().
|
---|
334 | */
|
---|
335 | page_pp_lock(pPage, 0 /* COW */, 1 /* Kernel */);
|
---|
336 |
|
---|
337 | page_sub(&pPageList, pPage);
|
---|
338 | page_io_unlock(pPage);
|
---|
339 | page_downgrade(pPage);
|
---|
340 | Assert(PAGE_LOCKED_SE(pPage, SE_SHARED));
|
---|
341 |
|
---|
342 | ppPages[iPage] = pPage;
|
---|
343 | }
|
---|
344 | Assert(pPageList == NULL);
|
---|
345 | Assert(ppPages[0] == pRootPage);
|
---|
346 |
|
---|
347 | uint64_t uPhys = rtR0MemObjSolPagePhys(pRootPage);
|
---|
348 | AssertMsg(!(uPhys & (cbLargePage - 1)), ("%llx %zx\n", uPhys, cbLargePage));
|
---|
349 | if (puPhys)
|
---|
350 | *puPhys = uPhys;
|
---|
351 | return ppPages;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*
|
---|
355 | * Don't restore offPrev in case of failure (race condition), we have plenty of offset space.
|
---|
356 | * The offset must be unique (for the same vnode) or we'll encounter panics on page_create_va_large().
|
---|
357 | */
|
---|
358 | kmem_free(ppPages, cbPages);
|
---|
359 | }
|
---|
360 |
|
---|
361 | page_unresv(cPages);
|
---|
362 | }
|
---|
363 | return NULL;
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Frees the large page.
|
---|
369 | *
|
---|
370 | * @param ppPages Pointer to the list of small pages that cover the
|
---|
371 | * large page.
|
---|
372 | * @param cbLargePage Size of the allocation (i.e. size of the large
|
---|
373 | * page).
|
---|
374 | */
|
---|
375 | static void rtR0MemObjSolLargePageFree(page_t **ppPages, size_t cbLargePage)
|
---|
376 | {
|
---|
377 | Assert(ppPages);
|
---|
378 | Assert(cbLargePage > PAGE_SIZE);
|
---|
379 |
|
---|
380 | bool fDemoted = false;
|
---|
381 | size_t cPages = (cbLargePage + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
---|
382 | size_t cbPages = cPages * sizeof(page_t *);
|
---|
383 | page_t *pPageList = ppPages[0];
|
---|
384 |
|
---|
385 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
386 | {
|
---|
387 | /*
|
---|
388 | * We need the pages exclusively locked, try upgrading the shared lock.
|
---|
389 | * If it fails, drop the shared page lock (cannot access any page_t members once this is done)
|
---|
390 | * and lookup the page from the page hash locking it exclusively.
|
---|
391 | */
|
---|
392 | page_t *pPage = ppPages[iPage];
|
---|
393 | u_offset_t offPage = pPage->p_offset;
|
---|
394 | int rc = page_tryupgrade(pPage);
|
---|
395 | if (!rc)
|
---|
396 | {
|
---|
397 | page_unlock(pPage);
|
---|
398 | page_t *pFoundPage = page_lookup(&g_LargePageVnode, offPage, SE_EXCL);
|
---|
399 | AssertRelease(pFoundPage);
|
---|
400 |
|
---|
401 | #if 0
|
---|
402 | /*
|
---|
403 | * This can only be guaranteed if PG_NORELOC is used while allocating the pages.
|
---|
404 | */
|
---|
405 | AssertReleaseMsg(pFoundPage == pPage,
|
---|
406 | ("lookup failed %p:%llu returned %p, expected %p\n", &g_LargePageVnode, offPage,
|
---|
407 | pFoundPage, pPage));
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Check for page demotion (regardless of relocation). Some places in Solaris (e.g. VM1 page_retire())
|
---|
412 | * could possibly demote the large page to _4K pages between our call to page_unlock() and page_lookup().
|
---|
413 | */
|
---|
414 | if (page_get_pagecnt(pFoundPage->p_szc) == 1) /* Base size of only _4K associated with this page. */
|
---|
415 | fDemoted = true;
|
---|
416 | pPage = pFoundPage;
|
---|
417 | ppPages[iPage] = pFoundPage;
|
---|
418 | }
|
---|
419 | Assert(PAGE_LOCKED_SE(pPage, SE_EXCL));
|
---|
420 | page_pp_unlock(pPage, 0 /* COW */, 1 /* Kernel */);
|
---|
421 | }
|
---|
422 |
|
---|
423 | if (fDemoted)
|
---|
424 | {
|
---|
425 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
426 | {
|
---|
427 | Assert(page_get_pagecnt(ppPages[iPage]->p_szc) == 1);
|
---|
428 | page_destroy(ppPages[iPage], 0 /* move it to the free list */);
|
---|
429 | }
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | /*
|
---|
434 | * Although we shred the adjacent pages in the linked list, page_destroy_pages works on
|
---|
435 | * adjacent pages via array increments. So this does indeed free all the pages.
|
---|
436 | */
|
---|
437 | AssertPtr(pPageList);
|
---|
438 | page_destroy_pages(pPageList);
|
---|
439 | }
|
---|
440 | kmem_free(ppPages, cbPages);
|
---|
441 | page_unresv(cPages);
|
---|
442 | }
|
---|
443 |
|
---|
444 |
|
---|
445 | /**
|
---|
446 | * Unmaps kernel/user-space mapped memory.
|
---|
447 | *
|
---|
448 | * @param pv Pointer to the mapped memory block.
|
---|
449 | * @param cb Size of the memory block.
|
---|
450 | */
|
---|
451 | static void rtR0MemObjSolUnmap(void *pv, size_t cb)
|
---|
452 | {
|
---|
453 | if (SOL_IS_KRNL_ADDR(pv))
|
---|
454 | {
|
---|
455 | hat_unload(kas.a_hat, pv, cb, HAT_UNLOAD | HAT_UNLOAD_UNLOCK);
|
---|
456 | vmem_free(heap_arena, pv, cb);
|
---|
457 | }
|
---|
458 | else
|
---|
459 | {
|
---|
460 | struct as *pAddrSpace = ((proc_t *)RTR0ProcHandleSelf())->p_as;
|
---|
461 | AssertPtr(pAddrSpace);
|
---|
462 | as_rangelock(pAddrSpace);
|
---|
463 | as_unmap(pAddrSpace, pv, cb);
|
---|
464 | as_rangeunlock(pAddrSpace);
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Lock down memory mappings for a virtual address.
|
---|
471 | *
|
---|
472 | * @param pv Pointer to the memory to lock down.
|
---|
473 | * @param cb Size of the memory block.
|
---|
474 | * @param fAccess Page access rights (S_READ, S_WRITE, S_EXEC)
|
---|
475 | *
|
---|
476 | * @returns IPRT status code.
|
---|
477 | */
|
---|
478 | static int rtR0MemObjSolLock(void *pv, size_t cb, int fPageAccess)
|
---|
479 | {
|
---|
480 | /*
|
---|
481 | * Kernel memory mappings on x86/amd64 are always locked, only handle user-space memory.
|
---|
482 | */
|
---|
483 | if (!SOL_IS_KRNL_ADDR(pv))
|
---|
484 | {
|
---|
485 | proc_t *pProc = (proc_t *)RTR0ProcHandleSelf();
|
---|
486 | AssertPtr(pProc);
|
---|
487 | faultcode_t rc = as_fault(pProc->p_as->a_hat, pProc->p_as, (caddr_t)pv, cb, F_SOFTLOCK, fPageAccess);
|
---|
488 | if (rc)
|
---|
489 | {
|
---|
490 | LogRel(("rtR0MemObjSolLock failed for pv=%pv cb=%lx fPageAccess=%d rc=%d\n", pv, cb, fPageAccess, rc));
|
---|
491 | return VERR_LOCK_FAILED;
|
---|
492 | }
|
---|
493 | }
|
---|
494 | return VINF_SUCCESS;
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Unlock memory mappings for a virtual address.
|
---|
500 | *
|
---|
501 | * @param pv Pointer to the locked memory.
|
---|
502 | * @param cb Size of the memory block.
|
---|
503 | * @param fPageAccess Page access rights (S_READ, S_WRITE, S_EXEC).
|
---|
504 | */
|
---|
505 | static void rtR0MemObjSolUnlock(void *pv, size_t cb, int fPageAccess)
|
---|
506 | {
|
---|
507 | if (!SOL_IS_KRNL_ADDR(pv))
|
---|
508 | {
|
---|
509 | proc_t *pProcess = (proc_t *)RTR0ProcHandleSelf();
|
---|
510 | AssertPtr(pProcess);
|
---|
511 | as_fault(pProcess->p_as->a_hat, pProcess->p_as, (caddr_t)pv, cb, F_SOFTUNLOCK, fPageAccess);
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * Maps a list of physical pages into user address space.
|
---|
518 | *
|
---|
519 | * @param pVirtAddr Where to store the virtual address of the mapping.
|
---|
520 | * @param fPageAccess Page access rights (PROT_READ, PROT_WRITE,
|
---|
521 | * PROT_EXEC)
|
---|
522 | * @param paPhysAddrs Array of physical addresses to pages.
|
---|
523 | * @param cb Size of memory being mapped.
|
---|
524 | *
|
---|
525 | * @returns IPRT status code.
|
---|
526 | */
|
---|
527 | static int rtR0MemObjSolUserMap(caddr_t *pVirtAddr, unsigned fPageAccess, uint64_t *paPhysAddrs, size_t cb, size_t cbPageSize)
|
---|
528 | {
|
---|
529 | struct as *pAddrSpace = ((proc_t *)RTR0ProcHandleSelf())->p_as;
|
---|
530 | int rc = VERR_INTERNAL_ERROR;
|
---|
531 | SEGVBOX_CRARGS Args;
|
---|
532 |
|
---|
533 | Args.paPhysAddrs = paPhysAddrs;
|
---|
534 | Args.fPageAccess = fPageAccess;
|
---|
535 | Args.cbPageSize = cbPageSize;
|
---|
536 |
|
---|
537 | as_rangelock(pAddrSpace);
|
---|
538 | map_addr(pVirtAddr, cb, 0 /* offset */, 0 /* vacalign */, MAP_SHARED);
|
---|
539 | if (*pVirtAddr != NULL)
|
---|
540 | rc = as_map(pAddrSpace, *pVirtAddr, cb, rtR0SegVBoxSolCreate, &Args);
|
---|
541 | else
|
---|
542 | rc = ENOMEM;
|
---|
543 | as_rangeunlock(pAddrSpace);
|
---|
544 |
|
---|
545 | return RTErrConvertFromErrno(rc);
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
|
---|
550 | {
|
---|
551 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)pMem;
|
---|
552 |
|
---|
553 | switch (pMemSolaris->Core.enmType)
|
---|
554 | {
|
---|
555 | case RTR0MEMOBJTYPE_LOW:
|
---|
556 | rtR0SolMemFree(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
|
---|
557 | break;
|
---|
558 |
|
---|
559 | case RTR0MEMOBJTYPE_PHYS:
|
---|
560 | if (pMemSolaris->Core.u.Phys.fAllocated)
|
---|
561 | {
|
---|
562 | if (pMemSolaris->fLargePage)
|
---|
563 | rtR0MemObjSolLargePageFree(pMemSolaris->pvHandle, pMemSolaris->Core.cb);
|
---|
564 | else
|
---|
565 | rtR0SolMemFree(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
|
---|
566 | }
|
---|
567 | break;
|
---|
568 |
|
---|
569 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
570 | if (pMemSolaris->fIndivPages)
|
---|
571 | rtR0MemObjSolPagesFree(pMemSolaris->pvHandle, pMemSolaris->Core.cb);
|
---|
572 | else
|
---|
573 | rtR0SolMemFree(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
|
---|
574 | break;
|
---|
575 |
|
---|
576 | case RTR0MEMOBJTYPE_PAGE:
|
---|
577 | ddi_umem_free(pMemSolaris->Cookie);
|
---|
578 | break;
|
---|
579 |
|
---|
580 | case RTR0MEMOBJTYPE_LOCK:
|
---|
581 | rtR0MemObjSolUnlock(pMemSolaris->Core.pv, pMemSolaris->Core.cb, pMemSolaris->fAccess);
|
---|
582 | break;
|
---|
583 |
|
---|
584 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
585 | rtR0MemObjSolUnmap(pMemSolaris->Core.pv, pMemSolaris->Core.cb);
|
---|
586 | break;
|
---|
587 |
|
---|
588 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
589 | {
|
---|
590 | if (pMemSolaris->Core.u.ResVirt.R0Process == NIL_RTR0PROCESS)
|
---|
591 | vmem_xfree(heap_arena, pMemSolaris->Core.pv, pMemSolaris->Core.cb);
|
---|
592 | else
|
---|
593 | AssertFailed();
|
---|
594 | break;
|
---|
595 | }
|
---|
596 |
|
---|
597 | case RTR0MEMOBJTYPE_CONT: /* we don't use this type here. */
|
---|
598 | default:
|
---|
599 | AssertMsgFailed(("enmType=%d\n", pMemSolaris->Core.enmType));
|
---|
600 | return VERR_INTERNAL_ERROR;
|
---|
601 | }
|
---|
602 |
|
---|
603 | return VINF_SUCCESS;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
608 | {
|
---|
609 | /* Create the object. */
|
---|
610 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PAGE, NULL, cb);
|
---|
611 | if (RT_UNLIKELY(!pMemSolaris))
|
---|
612 | return VERR_NO_MEMORY;
|
---|
613 |
|
---|
614 | void *pvMem = ddi_umem_alloc(cb, DDI_UMEM_SLEEP, &pMemSolaris->Cookie);
|
---|
615 | if (RT_UNLIKELY(!pvMem))
|
---|
616 | {
|
---|
617 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
618 | return VERR_NO_PAGE_MEMORY;
|
---|
619 | }
|
---|
620 |
|
---|
621 | pMemSolaris->Core.pv = pvMem;
|
---|
622 | pMemSolaris->pvHandle = NULL;
|
---|
623 | *ppMem = &pMemSolaris->Core;
|
---|
624 | return VINF_SUCCESS;
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
629 | {
|
---|
630 | NOREF(fExecutable);
|
---|
631 |
|
---|
632 | /* Create the object */
|
---|
633 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOW, NULL, cb);
|
---|
634 | if (!pMemSolaris)
|
---|
635 | return VERR_NO_MEMORY;
|
---|
636 |
|
---|
637 | /* Allocate physically low page-aligned memory. */
|
---|
638 | uint64_t uPhysHi = _4G - 1;
|
---|
639 | void *pvMem = rtR0SolMemAlloc(uPhysHi, NULL /* puPhys */, cb, PAGE_SIZE, false /* fContig */);
|
---|
640 | if (RT_UNLIKELY(!pvMem))
|
---|
641 | {
|
---|
642 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
643 | return VERR_NO_LOW_MEMORY;
|
---|
644 | }
|
---|
645 | pMemSolaris->Core.pv = pvMem;
|
---|
646 | pMemSolaris->pvHandle = NULL;
|
---|
647 | *ppMem = &pMemSolaris->Core;
|
---|
648 | return VINF_SUCCESS;
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
653 | {
|
---|
654 | NOREF(fExecutable);
|
---|
655 | return rtR0MemObjNativeAllocPhys(ppMem, cb, _4G - 1, PAGE_SIZE /* alignment */);
|
---|
656 | }
|
---|
657 |
|
---|
658 |
|
---|
659 | DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
660 | {
|
---|
661 | #if HC_ARCH_BITS == 64
|
---|
662 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS_NC, NULL, cb);
|
---|
663 | if (RT_UNLIKELY(!pMemSolaris))
|
---|
664 | return VERR_NO_MEMORY;
|
---|
665 |
|
---|
666 | if (PhysHighest == NIL_RTHCPHYS)
|
---|
667 | {
|
---|
668 | uint64_t PhysAddr = UINT64_MAX;
|
---|
669 | void *pvPages = rtR0MemObjSolPagesAlloc(&PhysAddr, cb);
|
---|
670 | if (!pvPages)
|
---|
671 | {
|
---|
672 | LogRel(("rtR0MemObjNativeAllocPhysNC: rtR0MemObjSolPagesAlloc failed for cb=%u.\n", cb));
|
---|
673 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
674 | return VERR_NO_MEMORY;
|
---|
675 | }
|
---|
676 | Assert(PhysAddr != UINT64_MAX);
|
---|
677 | Assert(!(PhysAddr & PAGE_OFFSET_MASK));
|
---|
678 |
|
---|
679 | pMemSolaris->Core.pv = NULL;
|
---|
680 | pMemSolaris->pvHandle = pvPages;
|
---|
681 | pMemSolaris->fIndivPages = true;
|
---|
682 | *ppMem = &pMemSolaris->Core;
|
---|
683 | return VINF_SUCCESS;
|
---|
684 | }
|
---|
685 | else
|
---|
686 | {
|
---|
687 | /*
|
---|
688 | * If we must satisfy an upper limit constraint, it isn't feasible to grab individual pages.
|
---|
689 | * We fall back to using contig_alloc().
|
---|
690 | */
|
---|
691 | uint64_t PhysAddr = UINT64_MAX;
|
---|
692 | void *pvMem = rtR0SolMemAlloc(PhysHighest, &PhysAddr, cb, PAGE_SIZE, false /* fContig */);
|
---|
693 | if (!pvMem)
|
---|
694 | {
|
---|
695 | LogRel(("rtR0MemObjNativeAllocPhysNC: rtR0SolMemAlloc failed for cb=%u PhysHighest=%RHp.\n", cb, PhysHighest));
|
---|
696 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
697 | return VERR_NO_MEMORY;
|
---|
698 | }
|
---|
699 | Assert(PhysAddr != UINT64_MAX);
|
---|
700 | Assert(!(PhysAddr & PAGE_OFFSET_MASK));
|
---|
701 |
|
---|
702 | pMemSolaris->Core.pv = pvMem;
|
---|
703 | pMemSolaris->pvHandle = NULL;
|
---|
704 | pMemSolaris->fIndivPages = false;
|
---|
705 | *ppMem = &pMemSolaris->Core;
|
---|
706 | return VINF_SUCCESS;
|
---|
707 | }
|
---|
708 |
|
---|
709 | #else /* 32 bit: */
|
---|
710 | return VERR_NOT_SUPPORTED; /* see the RTR0MemObjAllocPhysNC specs */
|
---|
711 | #endif
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
|
---|
716 | {
|
---|
717 | AssertMsgReturn(PhysHighest >= 16 *_1M, ("PhysHigest=%RHp\n", PhysHighest), VERR_NOT_SUPPORTED);
|
---|
718 |
|
---|
719 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
720 | if (RT_UNLIKELY(!pMemSolaris))
|
---|
721 | return VERR_NO_MEMORY;
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Allocating one large page gets special treatment.
|
---|
725 | */
|
---|
726 | static uint32_t s_cbLargePage = UINT32_MAX;
|
---|
727 | if (s_cbLargePage == UINT32_MAX)
|
---|
728 | {
|
---|
729 | if (page_num_pagesizes() > 1)
|
---|
730 | ASMAtomicWriteU32(&s_cbLargePage, page_get_pagesize(1)); /* Page-size code 1 maps to _2M on Solaris x86/amd64. */
|
---|
731 | else
|
---|
732 | ASMAtomicWriteU32(&s_cbLargePage, 0);
|
---|
733 | }
|
---|
734 | uint64_t PhysAddr;
|
---|
735 | if ( cb == s_cbLargePage
|
---|
736 | && cb == uAlignment
|
---|
737 | && PhysHighest == NIL_RTHCPHYS)
|
---|
738 | {
|
---|
739 | /*
|
---|
740 | * Allocate one large page (backed by physically contiguous memory).
|
---|
741 | */
|
---|
742 | void *pvPages = rtR0MemObjSolLargePageAlloc(&PhysAddr, cb);
|
---|
743 | if (RT_LIKELY(pvPages))
|
---|
744 | {
|
---|
745 | AssertMsg(!(PhysAddr & (cb - 1)), ("%RHp\n", PhysAddr));
|
---|
746 | pMemSolaris->Core.pv = NULL;
|
---|
747 | pMemSolaris->Core.u.Phys.PhysBase = PhysAddr;
|
---|
748 | pMemSolaris->Core.u.Phys.fAllocated = true;
|
---|
749 | pMemSolaris->pvHandle = pvPages;
|
---|
750 | pMemSolaris->fLargePage = true;
|
---|
751 |
|
---|
752 | *ppMem = &pMemSolaris->Core;
|
---|
753 | return VINF_SUCCESS;
|
---|
754 | }
|
---|
755 | }
|
---|
756 | else
|
---|
757 | {
|
---|
758 | /*
|
---|
759 | * Allocate physically contiguous memory aligned as specified.
|
---|
760 | * Note: contig_alloc() can be agonizingly slow for large (e.g. >= _2M) contiguous allocations.
|
---|
761 | * So we shouldn't ideally be in this path for large-page allocations. .
|
---|
762 | */
|
---|
763 | AssertCompile(NIL_RTHCPHYS == UINT64_MAX); NOREF(RTASSERTVAR);
|
---|
764 | PhysAddr = PhysHighest;
|
---|
765 | void *pvMem = rtR0SolMemAlloc(PhysHighest, &PhysAddr, cb, uAlignment, true /* fContig */);
|
---|
766 | if (RT_LIKELY(pvMem))
|
---|
767 | {
|
---|
768 | Assert(!(PhysAddr & PAGE_OFFSET_MASK));
|
---|
769 | Assert(PhysAddr < PhysHighest);
|
---|
770 | Assert(PhysAddr + cb <= PhysHighest);
|
---|
771 |
|
---|
772 | pMemSolaris->Core.pv = pvMem;
|
---|
773 | pMemSolaris->Core.u.Phys.PhysBase = PhysAddr;
|
---|
774 | pMemSolaris->Core.u.Phys.fAllocated = true;
|
---|
775 | pMemSolaris->pvHandle = NULL;
|
---|
776 | pMemSolaris->fLargePage = false;
|
---|
777 |
|
---|
778 | *ppMem = &pMemSolaris->Core;
|
---|
779 | return VINF_SUCCESS;
|
---|
780 | }
|
---|
781 | }
|
---|
782 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
783 | return VERR_NO_CONT_MEMORY;
|
---|
784 | }
|
---|
785 |
|
---|
786 |
|
---|
787 | DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
|
---|
788 | {
|
---|
789 | AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
|
---|
790 |
|
---|
791 | /* Create the object. */
|
---|
792 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
793 | if (!pMemSolaris)
|
---|
794 | return VERR_NO_MEMORY;
|
---|
795 |
|
---|
796 | /* There is no allocation here, it needs to be mapped somewhere first. */
|
---|
797 | pMemSolaris->Core.u.Phys.fAllocated = false;
|
---|
798 | pMemSolaris->Core.u.Phys.PhysBase = Phys;
|
---|
799 | pMemSolaris->Core.u.Phys.uCachePolicy = uCachePolicy;
|
---|
800 | *ppMem = &pMemSolaris->Core;
|
---|
801 | return VINF_SUCCESS;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess,
|
---|
806 | RTR0PROCESS R0Process)
|
---|
807 | {
|
---|
808 | AssertReturn(R0Process == RTR0ProcHandleSelf(), VERR_INVALID_PARAMETER);
|
---|
809 | NOREF(fAccess);
|
---|
810 |
|
---|
811 | /* Create the locking object */
|
---|
812 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
|
---|
813 | if (!pMemSolaris)
|
---|
814 | return VERR_NO_MEMORY;
|
---|
815 |
|
---|
816 | /* Lock down user pages. */
|
---|
817 | int fPageAccess = S_READ;
|
---|
818 | if (fAccess & RTMEM_PROT_WRITE)
|
---|
819 | fPageAccess = S_WRITE;
|
---|
820 | if (fAccess & RTMEM_PROT_EXEC)
|
---|
821 | fPageAccess = S_EXEC;
|
---|
822 | int rc = rtR0MemObjSolLock((void *)R3Ptr, cb, fPageAccess);
|
---|
823 | if (RT_FAILURE(rc))
|
---|
824 | {
|
---|
825 | LogRel(("rtR0MemObjNativeLockUser: rtR0MemObjSolLock failed rc=%d\n", rc));
|
---|
826 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
827 | return rc;
|
---|
828 | }
|
---|
829 |
|
---|
830 | /* Fill in the object attributes and return successfully. */
|
---|
831 | pMemSolaris->Core.u.Lock.R0Process = R0Process;
|
---|
832 | pMemSolaris->pvHandle = NULL;
|
---|
833 | pMemSolaris->fAccess = fPageAccess;
|
---|
834 | *ppMem = &pMemSolaris->Core;
|
---|
835 | return VINF_SUCCESS;
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 | DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
|
---|
840 | {
|
---|
841 | NOREF(fAccess);
|
---|
842 |
|
---|
843 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
844 | if (!pMemSolaris)
|
---|
845 | return VERR_NO_MEMORY;
|
---|
846 |
|
---|
847 | /* Lock down kernel pages. */
|
---|
848 | int fPageAccess = S_READ;
|
---|
849 | if (fAccess & RTMEM_PROT_WRITE)
|
---|
850 | fPageAccess = S_WRITE;
|
---|
851 | if (fAccess & RTMEM_PROT_EXEC)
|
---|
852 | fPageAccess = S_EXEC;
|
---|
853 | int rc = rtR0MemObjSolLock(pv, cb, fPageAccess);
|
---|
854 | if (RT_FAILURE(rc))
|
---|
855 | {
|
---|
856 | LogRel(("rtR0MemObjNativeLockKernel: rtR0MemObjSolLock failed rc=%d\n", rc));
|
---|
857 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
858 | return rc;
|
---|
859 | }
|
---|
860 |
|
---|
861 | /* Fill in the object attributes and return successfully. */
|
---|
862 | pMemSolaris->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
|
---|
863 | pMemSolaris->pvHandle = NULL;
|
---|
864 | pMemSolaris->fAccess = fPageAccess;
|
---|
865 | *ppMem = &pMemSolaris->Core;
|
---|
866 | return VINF_SUCCESS;
|
---|
867 | }
|
---|
868 |
|
---|
869 |
|
---|
870 | DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
871 | {
|
---|
872 | PRTR0MEMOBJSOL pMemSolaris;
|
---|
873 |
|
---|
874 | /*
|
---|
875 | * Use xalloc.
|
---|
876 | */
|
---|
877 | void *pv = vmem_xalloc(heap_arena, cb, uAlignment, 0 /* phase */, 0 /* nocross */,
|
---|
878 | NULL /* minaddr */, NULL /* maxaddr */, VM_SLEEP);
|
---|
879 | if (RT_UNLIKELY(!pv))
|
---|
880 | return VERR_NO_MEMORY;
|
---|
881 |
|
---|
882 | /* Create the object. */
|
---|
883 | pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_RES_VIRT, pv, cb);
|
---|
884 | if (!pMemSolaris)
|
---|
885 | {
|
---|
886 | LogRel(("rtR0MemObjNativeReserveKernel failed to alloc memory object.\n"));
|
---|
887 | vmem_xfree(heap_arena, pv, cb);
|
---|
888 | return VERR_NO_MEMORY;
|
---|
889 | }
|
---|
890 |
|
---|
891 | pMemSolaris->Core.u.ResVirt.R0Process = NIL_RTR0PROCESS;
|
---|
892 | *ppMem = &pMemSolaris->Core;
|
---|
893 | return VINF_SUCCESS;
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment,
|
---|
898 | RTR0PROCESS R0Process)
|
---|
899 | {
|
---|
900 | return VERR_NOT_SUPPORTED;
|
---|
901 | }
|
---|
902 |
|
---|
903 |
|
---|
904 | DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
|
---|
905 | unsigned fProt, size_t offSub, size_t cbSub)
|
---|
906 | {
|
---|
907 | /* Fail if requested to do something we can't. */
|
---|
908 | AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
|
---|
909 | if (uAlignment > PAGE_SIZE)
|
---|
910 | return VERR_NOT_SUPPORTED;
|
---|
911 |
|
---|
912 | /*
|
---|
913 | * Use xalloc to get address space.
|
---|
914 | */
|
---|
915 | if (!cbSub)
|
---|
916 | cbSub = pMemToMap->cb;
|
---|
917 | void *pv = vmem_xalloc(heap_arena, cbSub, uAlignment, 0 /* phase */, 0 /* nocross */,
|
---|
918 | NULL /* minaddr */, NULL /* maxaddr */, VM_SLEEP);
|
---|
919 | if (RT_UNLIKELY(!pv))
|
---|
920 | return VERR_MAP_FAILED;
|
---|
921 |
|
---|
922 | /*
|
---|
923 | * Load the pages from the other object into it.
|
---|
924 | */
|
---|
925 | uint32_t fAttr = HAT_UNORDERED_OK | HAT_MERGING_OK | HAT_LOADCACHING_OK | HAT_STORECACHING_OK;
|
---|
926 | if (fProt & RTMEM_PROT_READ)
|
---|
927 | fAttr |= PROT_READ;
|
---|
928 | if (fProt & RTMEM_PROT_EXEC)
|
---|
929 | fAttr |= PROT_EXEC;
|
---|
930 | if (fProt & RTMEM_PROT_WRITE)
|
---|
931 | fAttr |= PROT_WRITE;
|
---|
932 | fAttr |= HAT_NOSYNC;
|
---|
933 |
|
---|
934 | int rc = VINF_SUCCESS;
|
---|
935 | size_t off = 0;
|
---|
936 | while (off < cbSub)
|
---|
937 | {
|
---|
938 | RTHCPHYS HCPhys = rtR0MemObjNativeGetPagePhysAddr(pMemToMap, (offSub + offSub) >> PAGE_SHIFT);
|
---|
939 | AssertBreakStmt(HCPhys != NIL_RTHCPHYS, rc = VERR_INTERNAL_ERROR_2);
|
---|
940 | pfn_t pfn = HCPhys >> PAGE_SHIFT;
|
---|
941 | AssertBreakStmt(((RTHCPHYS)pfn << PAGE_SHIFT) == HCPhys, rc = VERR_INTERNAL_ERROR_3);
|
---|
942 |
|
---|
943 | hat_devload(kas.a_hat, (uint8_t *)pv + off, PAGE_SIZE, pfn, fAttr, HAT_LOAD_LOCK);
|
---|
944 |
|
---|
945 | /* Advance. */
|
---|
946 | off += PAGE_SIZE;
|
---|
947 | }
|
---|
948 | if (RT_SUCCESS(rc))
|
---|
949 | {
|
---|
950 | /*
|
---|
951 | * Create a memory object for the mapping.
|
---|
952 | */
|
---|
953 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, cbSub);
|
---|
954 | if (pMemSolaris)
|
---|
955 | {
|
---|
956 | pMemSolaris->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
|
---|
957 | *ppMem = &pMemSolaris->Core;
|
---|
958 | return VINF_SUCCESS;
|
---|
959 | }
|
---|
960 |
|
---|
961 | LogRel(("rtR0MemObjNativeMapKernel failed to alloc memory object.\n"));
|
---|
962 | rc = VERR_NO_MEMORY;
|
---|
963 | }
|
---|
964 |
|
---|
965 | if (off)
|
---|
966 | hat_unload(kas.a_hat, pv, off, HAT_UNLOAD | HAT_UNLOAD_UNLOCK);
|
---|
967 | vmem_xfree(heap_arena, pv, cbSub);
|
---|
968 | return rc;
|
---|
969 | }
|
---|
970 |
|
---|
971 |
|
---|
972 | DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed,
|
---|
973 | size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
974 | {
|
---|
975 | /*
|
---|
976 | * Fend off things we cannot do.
|
---|
977 | */
|
---|
978 | AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
|
---|
979 | AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
|
---|
980 | if (uAlignment != PAGE_SIZE)
|
---|
981 | return VERR_NOT_SUPPORTED;
|
---|
982 |
|
---|
983 | /*
|
---|
984 | * Get parameters from the source object.
|
---|
985 | */
|
---|
986 | PRTR0MEMOBJSOL pMemToMapSolaris = (PRTR0MEMOBJSOL)pMemToMap;
|
---|
987 | void *pv = pMemToMapSolaris->Core.pv;
|
---|
988 | size_t cb = pMemToMapSolaris->Core.cb;
|
---|
989 | size_t cPages = (cb + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
---|
990 |
|
---|
991 | /*
|
---|
992 | * Create the mapping object
|
---|
993 | */
|
---|
994 | PRTR0MEMOBJSOL pMemSolaris;
|
---|
995 | pMemSolaris = (PRTR0MEMOBJSOL)rtR0MemObjNew(sizeof(*pMemSolaris), RTR0MEMOBJTYPE_MAPPING, pv, cb);
|
---|
996 | if (RT_UNLIKELY(!pMemSolaris))
|
---|
997 | return VERR_NO_MEMORY;
|
---|
998 |
|
---|
999 | int rc = VINF_SUCCESS;
|
---|
1000 | uint64_t *paPhysAddrs = kmem_zalloc(sizeof(uint64_t) * cPages, KM_SLEEP);
|
---|
1001 | if (RT_LIKELY(paPhysAddrs))
|
---|
1002 | {
|
---|
1003 | /*
|
---|
1004 | * Prepare the pages for mapping according to type.
|
---|
1005 | */
|
---|
1006 | if ( pMemToMapSolaris->Core.enmType == RTR0MEMOBJTYPE_PHYS_NC
|
---|
1007 | && pMemToMapSolaris->fIndivPages)
|
---|
1008 | {
|
---|
1009 | page_t **ppPages = pMemToMapSolaris->pvHandle;
|
---|
1010 | AssertPtr(ppPages);
|
---|
1011 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
1012 | paPhysAddrs[iPage] = rtR0MemObjSolPagePhys(ppPages[iPage]);
|
---|
1013 | }
|
---|
1014 | else if ( pMemToMapSolaris->Core.enmType == RTR0MEMOBJTYPE_PHYS
|
---|
1015 | && pMemToMapSolaris->fLargePage)
|
---|
1016 | {
|
---|
1017 | RTHCPHYS Phys = pMemToMapSolaris->Core.u.Phys.PhysBase;
|
---|
1018 | for (size_t iPage = 0; iPage < cPages; iPage++, Phys += PAGE_SIZE)
|
---|
1019 | paPhysAddrs[iPage] = Phys;
|
---|
1020 | }
|
---|
1021 | else
|
---|
1022 | {
|
---|
1023 | /*
|
---|
1024 | * Have kernel mapping, just translate virtual to physical.
|
---|
1025 | */
|
---|
1026 | AssertPtr(pv);
|
---|
1027 | rc = VINF_SUCCESS;
|
---|
1028 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
1029 | {
|
---|
1030 | paPhysAddrs[iPage] = rtR0MemObjSolVirtToPhys(pv);
|
---|
1031 | if (RT_UNLIKELY(paPhysAddrs[iPage] == -(uint64_t)1))
|
---|
1032 | {
|
---|
1033 | LogRel(("rtR0MemObjNativeMapUser: no page to map.\n"));
|
---|
1034 | rc = VERR_MAP_FAILED;
|
---|
1035 | break;
|
---|
1036 | }
|
---|
1037 | pv = (void *)((uintptr_t)pv + PAGE_SIZE);
|
---|
1038 | }
|
---|
1039 | }
|
---|
1040 | if (RT_SUCCESS(rc))
|
---|
1041 | {
|
---|
1042 | unsigned fPageAccess = PROT_READ;
|
---|
1043 | if (fProt & RTMEM_PROT_WRITE)
|
---|
1044 | fPageAccess |= PROT_WRITE;
|
---|
1045 | if (fProt & RTMEM_PROT_EXEC)
|
---|
1046 | fPageAccess |= PROT_EXEC;
|
---|
1047 |
|
---|
1048 | /*
|
---|
1049 | * Perform the actual mapping.
|
---|
1050 | */
|
---|
1051 | caddr_t UserAddr = NULL;
|
---|
1052 | rc = rtR0MemObjSolUserMap(&UserAddr, fPageAccess, paPhysAddrs, cb, PAGE_SIZE);
|
---|
1053 | if (RT_SUCCESS(rc))
|
---|
1054 | {
|
---|
1055 | pMemSolaris->Core.u.Mapping.R0Process = R0Process;
|
---|
1056 | pMemSolaris->Core.pv = UserAddr;
|
---|
1057 |
|
---|
1058 | *ppMem = &pMemSolaris->Core;
|
---|
1059 | kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
|
---|
1060 | return VINF_SUCCESS;
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | LogRel(("rtR0MemObjNativeMapUser: rtR0MemObjSolUserMap failed rc=%d.\n", rc));
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | rc = VERR_MAP_FAILED;
|
---|
1067 | kmem_free(paPhysAddrs, sizeof(uint64_t) * cPages);
|
---|
1068 | }
|
---|
1069 | else
|
---|
1070 | rc = VERR_NO_MEMORY;
|
---|
1071 | rtR0MemObjDelete(&pMemSolaris->Core);
|
---|
1072 | return rc;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
|
---|
1077 | {
|
---|
1078 | NOREF(pMem);
|
---|
1079 | NOREF(offSub);
|
---|
1080 | NOREF(cbSub);
|
---|
1081 | NOREF(fProt);
|
---|
1082 | return VERR_NOT_SUPPORTED;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
|
---|
1087 | {
|
---|
1088 | PRTR0MEMOBJSOL pMemSolaris = (PRTR0MEMOBJSOL)pMem;
|
---|
1089 |
|
---|
1090 | switch (pMemSolaris->Core.enmType)
|
---|
1091 | {
|
---|
1092 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
1093 | if ( pMemSolaris->Core.u.Phys.fAllocated
|
---|
1094 | || !pMemSolaris->fIndivPages)
|
---|
1095 | {
|
---|
1096 | uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
|
---|
1097 | return rtR0MemObjSolVirtToPhys(pb);
|
---|
1098 | }
|
---|
1099 | page_t **ppPages = pMemSolaris->pvHandle;
|
---|
1100 | return rtR0MemObjSolPagePhys(ppPages[iPage]);
|
---|
1101 |
|
---|
1102 | case RTR0MEMOBJTYPE_PAGE:
|
---|
1103 | case RTR0MEMOBJTYPE_LOW:
|
---|
1104 | case RTR0MEMOBJTYPE_LOCK:
|
---|
1105 | {
|
---|
1106 | uint8_t *pb = (uint8_t *)pMemSolaris->Core.pv + ((size_t)iPage << PAGE_SHIFT);
|
---|
1107 | return rtR0MemObjSolVirtToPhys(pb);
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /*
|
---|
1111 | * Although mapping can be handled by rtR0MemObjSolVirtToPhys(offset) like the above case,
|
---|
1112 | * request it from the parent so that we have a clear distinction between CONT/PHYS_NC.
|
---|
1113 | */
|
---|
1114 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
1115 | return rtR0MemObjNativeGetPagePhysAddr(pMemSolaris->Core.uRel.Child.pParent, iPage);
|
---|
1116 |
|
---|
1117 | case RTR0MEMOBJTYPE_CONT:
|
---|
1118 | case RTR0MEMOBJTYPE_PHYS:
|
---|
1119 | AssertFailed(); /* handled by the caller */
|
---|
1120 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
1121 | default:
|
---|
1122 | return NIL_RTHCPHYS;
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|