1 | /* $Id: alloc-r0drv-solaris.c 56290 2015-06-09 14:01:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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/mem.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include "r0drv/alloc-r0drv.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*******************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *******************************************************************************/
|
---|
46 | static ddi_dma_attr_t s_rtR0SolDmaAttr =
|
---|
47 | {
|
---|
48 | DMA_ATTR_V0, /* Version Number */
|
---|
49 | (uint64_t)0, /* Lower limit */
|
---|
50 | (uint64_t)0, /* High limit */
|
---|
51 | (uint64_t)0xffffffff, /* Counter limit */
|
---|
52 | (uint64_t)PAGESIZE, /* Alignment */
|
---|
53 | (uint64_t)PAGESIZE, /* Burst size */
|
---|
54 | (uint64_t)PAGESIZE, /* Effective DMA size */
|
---|
55 | (uint64_t)0xffffffff, /* Max DMA xfer size */
|
---|
56 | (uint64_t)0xffffffff, /* Segment boundary */
|
---|
57 | 1, /* Scatter-gather list length (1 for contiguous) */
|
---|
58 | 1, /* Device granularity */
|
---|
59 | 0 /* Bus-specific flags */
|
---|
60 | };
|
---|
61 |
|
---|
62 | extern void *contig_alloc(size_t cb, ddi_dma_attr_t *pDmaAttr, size_t uAlign, int fCanSleep);
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * OS specific allocation function.
|
---|
67 | */
|
---|
68 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
69 | {
|
---|
70 | size_t cbAllocated = cb;
|
---|
71 | PRTMEMHDR pHdr;
|
---|
72 |
|
---|
73 | #ifdef RT_ARCH_AMD64
|
---|
74 | if (fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
75 | {
|
---|
76 | AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), VERR_NOT_SUPPORTED);
|
---|
77 | cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE) - sizeof(*pHdr);
|
---|
78 | pHdr = (PRTMEMHDR)segkmem_alloc(heaptext_arena, cbAllocated + sizeof(*pHdr), KM_SLEEP);
|
---|
79 | }
|
---|
80 | else
|
---|
81 | #endif
|
---|
82 | {
|
---|
83 | unsigned fKmFlags = fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC ? KM_NOSLEEP : KM_SLEEP;
|
---|
84 | if (fFlags & RTMEMHDR_FLAG_ZEROED)
|
---|
85 | pHdr = (PRTMEMHDR)kmem_zalloc(cb + sizeof(*pHdr), fKmFlags);
|
---|
86 | else
|
---|
87 | pHdr = (PRTMEMHDR)kmem_alloc(cb + sizeof(*pHdr), fKmFlags);
|
---|
88 | }
|
---|
89 | if (RT_UNLIKELY(!pHdr))
|
---|
90 | {
|
---|
91 | LogRel(("rtMemAllocEx(%u, %#x) failed\n", (unsigned)cb + sizeof(*pHdr), fFlags));
|
---|
92 | return VERR_NO_MEMORY;
|
---|
93 | }
|
---|
94 |
|
---|
95 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
96 | pHdr->fFlags = fFlags;
|
---|
97 | pHdr->cb = cbAllocated;
|
---|
98 | pHdr->cbReq = cb;
|
---|
99 |
|
---|
100 | *ppHdr = pHdr;
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * OS specific free function.
|
---|
107 | */
|
---|
108 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
109 | {
|
---|
110 | pHdr->u32Magic += 1;
|
---|
111 | #ifdef RT_ARCH_AMD64
|
---|
112 | if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
113 | segkmem_free(heaptext_arena, pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
114 | else
|
---|
115 | #endif
|
---|
116 | kmem_free(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Allocates physical memory which satisfy the given constraints.
|
---|
122 | *
|
---|
123 | * @param uPhysHi The upper physical address limit (inclusive).
|
---|
124 | * @param puPhys Where to store the physical address of the allocated
|
---|
125 | * memory. Optional, can be NULL.
|
---|
126 | * @param cb Size of allocation.
|
---|
127 | * @param uAlignment Alignment.
|
---|
128 | * @param fContig Whether the memory must be physically contiguous or
|
---|
129 | * not.
|
---|
130 | *
|
---|
131 | * @returns Virtual address of allocated memory block or NULL if allocation
|
---|
132 | * failed.
|
---|
133 | */
|
---|
134 | DECLHIDDEN(void *) rtR0SolMemAlloc(uint64_t uPhysHi, uint64_t *puPhys, size_t cb, uint64_t uAlignment, bool fContig)
|
---|
135 | {
|
---|
136 | if ((cb & PAGEOFFSET) != 0)
|
---|
137 | return NULL;
|
---|
138 |
|
---|
139 | size_t cPages = (cb + PAGESIZE - 1) >> PAGESHIFT;
|
---|
140 | if (!cPages)
|
---|
141 | return NULL;
|
---|
142 |
|
---|
143 | ddi_dma_attr_t DmaAttr = s_rtR0SolDmaAttr;
|
---|
144 | DmaAttr.dma_attr_addr_hi = uPhysHi;
|
---|
145 | DmaAttr.dma_attr_align = uAlignment;
|
---|
146 | if (!fContig)
|
---|
147 | DmaAttr.dma_attr_sgllen = cPages > INT_MAX ? INT_MAX - 1 : cPages;
|
---|
148 | else
|
---|
149 | AssertRelease(DmaAttr.dma_attr_sgllen == 1);
|
---|
150 |
|
---|
151 | void *pvMem = contig_alloc(cb, &DmaAttr, PAGESIZE, 1 /* can sleep */);
|
---|
152 | if (!pvMem)
|
---|
153 | {
|
---|
154 | LogRel(("rtR0SolMemAlloc failed. cb=%u Align=%u fContig=%d\n", (unsigned)cb, (unsigned)uAlignment, fContig));
|
---|
155 | return NULL;
|
---|
156 | }
|
---|
157 |
|
---|
158 | pfn_t PageFrameNum = hat_getpfnum(kas.a_hat, (caddr_t)pvMem);
|
---|
159 | AssertRelease(PageFrameNum != PFN_INVALID);
|
---|
160 | if (puPhys)
|
---|
161 | *puPhys = (uint64_t)PageFrameNum << PAGESHIFT;
|
---|
162 |
|
---|
163 | return pvMem;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Frees memory allocated using rtR0SolMemAlloc().
|
---|
169 | *
|
---|
170 | * @param pv The memory to free.
|
---|
171 | * @param cb Size of the memory block
|
---|
172 | */
|
---|
173 | DECLHIDDEN(void) rtR0SolMemFree(void *pv, size_t cb)
|
---|
174 | {
|
---|
175 | if (RT_LIKELY(pv))
|
---|
176 | g_pfnrtR0Sol_contig_free(pv, cb);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
181 | {
|
---|
182 | AssertPtrReturn(pPhys, NULL);
|
---|
183 | AssertReturn(cb > 0, NULL);
|
---|
184 | RT_ASSERT_PREEMPTIBLE();
|
---|
185 |
|
---|
186 | /* Allocate physically contiguous (< 4GB) page-aligned memory. */
|
---|
187 | uint64_t uPhys;
|
---|
188 | void *pvMem = rtR0SolMemAlloc((uint64_t)_4G - 1, &uPhys, cb, PAGESIZE, true /* fContig */);
|
---|
189 | if (RT_UNLIKELY(!pvMem))
|
---|
190 | {
|
---|
191 | LogRel(("RTMemContAlloc failed to allocate %u bytes\n", cb));
|
---|
192 | return NULL;
|
---|
193 | }
|
---|
194 |
|
---|
195 | Assert(uPhys < _4G);
|
---|
196 | *pPhys = uPhys;
|
---|
197 | return pvMem;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
202 | {
|
---|
203 | RT_ASSERT_PREEMPTIBLE();
|
---|
204 | rtR0SolMemFree(pv, cb);
|
---|
205 | }
|
---|
206 |
|
---|