1 | /* $Id: alloc-r0drv-haiku.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, Haiku.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 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-haiku-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 | #include <iprt/mem.h>
|
---|
44 | #include <iprt/log.h>
|
---|
45 |
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 | #include <iprt/thread.h>
|
---|
49 | #include "r0drv/alloc-r0drv.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * OS specific allocation function.
|
---|
54 | */
|
---|
55 | int rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
56 | {
|
---|
57 | if (RT_UNLIKELY(fFlags & RTMEMHDR_FLAG_ANY_CTX))
|
---|
58 | return VERR_NOT_SUPPORTED;
|
---|
59 |
|
---|
60 | PRTMEMHDR pHdr = (PRTMEMHDR)malloc(cb + sizeof(*pHdr));
|
---|
61 | if (RT_UNLIKELY(!pHdr))
|
---|
62 | {
|
---|
63 | LogRel(("rtR0MemAllocEx(%u, %#x) failed\n",(unsigned)cb + sizeof(*pHdr), fFlags));
|
---|
64 | return VERR_NO_MEMORY;
|
---|
65 | }
|
---|
66 |
|
---|
67 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
68 | pHdr->fFlags = fFlags;
|
---|
69 | pHdr->cb = cb;
|
---|
70 | pHdr->cbReq = cb;
|
---|
71 | *ppHdr = pHdr;
|
---|
72 | return VINF_SUCCESS;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * OS specific free function.
|
---|
78 | */
|
---|
79 | void rtR0MemFree(PRTMEMHDR pHdr)
|
---|
80 | {
|
---|
81 | pHdr->u32Magic += 1;
|
---|
82 | free(pHdr);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb) RT_NO_THROW_DEF
|
---|
87 | {
|
---|
88 | /*
|
---|
89 | * Validate input.
|
---|
90 | */
|
---|
91 | AssertPtr(pPhys);
|
---|
92 | Assert(cb > 0);
|
---|
93 | RT_ASSERT_PREEMPTIBLE();
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Allocate the memory and ensure that the API is still providing
|
---|
97 | * memory that's always below 4GB.
|
---|
98 | */
|
---|
99 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
100 | void *pv;
|
---|
101 | area_id area = create_area("VirtualBox Contig Alloc", &pv, B_ANY_KERNEL_ADDRESS, cb, B_32_BIT_CONTIGUOUS,
|
---|
102 | B_READ_AREA | B_WRITE_AREA);
|
---|
103 | if (area >= 0)
|
---|
104 | {
|
---|
105 | physical_entry physMap[2];
|
---|
106 | if (get_memory_map(pv, cb, physMap, 2)>= B_OK)
|
---|
107 | {
|
---|
108 | *pPhys = physMap[0].address;
|
---|
109 | return pv;
|
---|
110 | }
|
---|
111 | delete_area(area);
|
---|
112 | AssertMsgFailed(("Cannot get_memory_map for contig alloc! cb=%u\n",(unsigned)cb));
|
---|
113 | }
|
---|
114 | else
|
---|
115 | AssertMsgFailed(("Cannot create_area for contig alloc! cb=%u error=0x%08lx\n",(unsigned)cb, area));
|
---|
116 | return NULL;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb) RT_NO_THROW_DEF
|
---|
121 | {
|
---|
122 | RT_ASSERT_PREEMPTIBLE();
|
---|
123 | if (pv)
|
---|
124 | {
|
---|
125 | Assert(cb > 0);
|
---|
126 |
|
---|
127 | area_id area = area_for(pv);
|
---|
128 | if (area >= B_OK)
|
---|
129 | delete_area(area);
|
---|
130 | else
|
---|
131 | AssertMsgFailed(("Cannot find area to delete! cb=%u error=0x%08lx\n",(unsigned)cb, area));
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|