1 | /* $Id: alloc-r0drv-solaris.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-solaris-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 | #include <iprt/mem.h>
|
---|
44 |
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/errcore.h>
|
---|
47 | #include <iprt/log.h>
|
---|
48 | #include <iprt/param.h>
|
---|
49 | #include <iprt/thread.h>
|
---|
50 | #include "r0drv/alloc-r0drv.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | static ddi_dma_attr_t s_rtR0SolDmaAttr =
|
---|
57 | {
|
---|
58 | DMA_ATTR_V0, /* Version Number */
|
---|
59 | (uint64_t)0, /* Lower limit */
|
---|
60 | (uint64_t)0, /* High limit */
|
---|
61 | (uint64_t)0xffffffff, /* Counter limit */
|
---|
62 | (uint64_t)PAGESIZE, /* Alignment */
|
---|
63 | (uint64_t)PAGESIZE, /* Burst size */
|
---|
64 | (uint64_t)PAGESIZE, /* Effective DMA size */
|
---|
65 | (uint64_t)0xffffffff, /* Max DMA xfer size */
|
---|
66 | (uint64_t)0xffffffff, /* Segment boundary */
|
---|
67 | 1, /* Scatter-gather list length (1 for contiguous) */
|
---|
68 | 1, /* Device granularity */
|
---|
69 | 0 /* Bus-specific flags */
|
---|
70 | };
|
---|
71 |
|
---|
72 | extern void *contig_alloc(size_t cb, ddi_dma_attr_t *pDmaAttr, size_t uAlign, int fCanSleep);
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * OS specific allocation function.
|
---|
77 | */
|
---|
78 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
79 | {
|
---|
80 | size_t cbAllocated = cb;
|
---|
81 | PRTMEMHDR pHdr;
|
---|
82 |
|
---|
83 | #ifdef RT_ARCH_AMD64
|
---|
84 | if (fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
85 | {
|
---|
86 | AssertReturn(!(fFlags & RTMEMHDR_FLAG_ANY_CTX), VERR_NOT_SUPPORTED);
|
---|
87 | cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE) - sizeof(*pHdr);
|
---|
88 | pHdr = (PRTMEMHDR)segkmem_alloc(heaptext_arena, cbAllocated + sizeof(*pHdr), KM_SLEEP);
|
---|
89 | }
|
---|
90 | else
|
---|
91 | #endif
|
---|
92 | {
|
---|
93 | unsigned fKmFlags = fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC ? KM_NOSLEEP : KM_SLEEP;
|
---|
94 | if (fFlags & RTMEMHDR_FLAG_ZEROED)
|
---|
95 | pHdr = (PRTMEMHDR)kmem_zalloc(cb + sizeof(*pHdr), fKmFlags);
|
---|
96 | else
|
---|
97 | pHdr = (PRTMEMHDR)kmem_alloc(cb + sizeof(*pHdr), fKmFlags);
|
---|
98 | }
|
---|
99 | if (RT_UNLIKELY(!pHdr))
|
---|
100 | {
|
---|
101 | LogRel(("rtMemAllocEx(%u, %#x) failed\n", (unsigned)cb + sizeof(*pHdr), fFlags));
|
---|
102 | return VERR_NO_MEMORY;
|
---|
103 | }
|
---|
104 |
|
---|
105 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
106 | pHdr->fFlags = fFlags;
|
---|
107 | pHdr->cb = cbAllocated;
|
---|
108 | pHdr->cbReq = cb;
|
---|
109 |
|
---|
110 | *ppHdr = pHdr;
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * OS specific free function.
|
---|
117 | */
|
---|
118 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
119 | {
|
---|
120 | pHdr->u32Magic += 1;
|
---|
121 | #ifdef RT_ARCH_AMD64
|
---|
122 | if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
123 | segkmem_free(heaptext_arena, pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
124 | else
|
---|
125 | #endif
|
---|
126 | kmem_free(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Allocates physical memory which satisfy the given constraints.
|
---|
132 | *
|
---|
133 | * @param uPhysHi The upper physical address limit (inclusive).
|
---|
134 | * @param puPhys Where to store the physical address of the allocated
|
---|
135 | * memory. Optional, can be NULL.
|
---|
136 | * @param cb Size of allocation.
|
---|
137 | * @param uAlignment Alignment.
|
---|
138 | * @param fContig Whether the memory must be physically contiguous or
|
---|
139 | * not.
|
---|
140 | *
|
---|
141 | * @returns Virtual address of allocated memory block or NULL if allocation
|
---|
142 | * failed.
|
---|
143 | */
|
---|
144 | DECLHIDDEN(void *) rtR0SolMemAlloc(uint64_t uPhysHi, uint64_t *puPhys, size_t cb, uint64_t uAlignment, bool fContig)
|
---|
145 | {
|
---|
146 | if ((cb & PAGEOFFSET) != 0)
|
---|
147 | return NULL;
|
---|
148 |
|
---|
149 | size_t cPages = (cb + PAGESIZE - 1) >> PAGESHIFT;
|
---|
150 | if (!cPages)
|
---|
151 | return NULL;
|
---|
152 |
|
---|
153 | ddi_dma_attr_t DmaAttr = s_rtR0SolDmaAttr;
|
---|
154 | DmaAttr.dma_attr_addr_hi = uPhysHi;
|
---|
155 | DmaAttr.dma_attr_align = uAlignment;
|
---|
156 | if (!fContig)
|
---|
157 | DmaAttr.dma_attr_sgllen = cPages > INT_MAX ? INT_MAX - 1 : cPages;
|
---|
158 | else
|
---|
159 | AssertRelease(DmaAttr.dma_attr_sgllen == 1);
|
---|
160 |
|
---|
161 | void *pvMem = contig_alloc(cb, &DmaAttr, PAGESIZE, 1 /* can sleep */);
|
---|
162 | if (!pvMem)
|
---|
163 | {
|
---|
164 | LogRel(("rtR0SolMemAlloc failed. cb=%u Align=%u fContig=%d\n", (unsigned)cb, (unsigned)uAlignment, fContig));
|
---|
165 | return NULL;
|
---|
166 | }
|
---|
167 |
|
---|
168 | pfn_t PageFrameNum = hat_getpfnum(kas.a_hat, (caddr_t)pvMem);
|
---|
169 | AssertRelease(PageFrameNum != PFN_INVALID);
|
---|
170 | if (puPhys)
|
---|
171 | *puPhys = (uint64_t)PageFrameNum << PAGESHIFT;
|
---|
172 |
|
---|
173 | return pvMem;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Frees memory allocated using rtR0SolMemAlloc().
|
---|
179 | *
|
---|
180 | * @param pv The memory to free.
|
---|
181 | * @param cb Size of the memory block
|
---|
182 | */
|
---|
183 | DECLHIDDEN(void) rtR0SolMemFree(void *pv, size_t cb)
|
---|
184 | {
|
---|
185 | if (RT_LIKELY(pv))
|
---|
186 | g_pfnrtR0Sol_contig_free(pv, cb);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
191 | {
|
---|
192 | AssertPtrReturn(pPhys, NULL);
|
---|
193 | AssertReturn(cb > 0, NULL);
|
---|
194 | RT_ASSERT_PREEMPTIBLE();
|
---|
195 |
|
---|
196 | /* Allocate physically contiguous (< 4GB) page-aligned memory. */
|
---|
197 | uint64_t uPhys;
|
---|
198 | void *pvMem = rtR0SolMemAlloc((uint64_t)_4G - 1, &uPhys, cb, PAGESIZE, true /* fContig */);
|
---|
199 | if (RT_UNLIKELY(!pvMem))
|
---|
200 | {
|
---|
201 | LogRel(("RTMemContAlloc failed to allocate %u bytes\n", cb));
|
---|
202 | return NULL;
|
---|
203 | }
|
---|
204 |
|
---|
205 | Assert(uPhys < _4G);
|
---|
206 | *pPhys = uPhys;
|
---|
207 | return pvMem;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
212 | {
|
---|
213 | RT_ASSERT_PREEMPTIBLE();
|
---|
214 | rtR0SolMemFree(pv, cb);
|
---|
215 | }
|
---|
216 |
|
---|