VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/alloc-r0drv-netbsd.c

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1/* $Id: alloc-r0drv-netbsd.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, NetBSD.
4 */
5
6/*
7 * Copyright (C) 2014-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 * This code is based on:
38 *
39 * Copyright (c) 2014 Arto Huusko
40 *
41 * Permission is hereby granted, free of charge, to any person
42 * obtaining a copy of this software and associated documentation
43 * files (the "Software"), to deal in the Software without
44 * restriction, including without limitation the rights to use,
45 * copy, modify, merge, publish, distribute, sublicense, and/or sell
46 * copies of the Software, and to permit persons to whom the
47 * Software is furnished to do so, subject to the following
48 * conditions:
49 *
50 * The above copyright notice and this permission notice shall be
51 * included in all copies or substantial portions of the Software.
52 *
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
54 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
55 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
57 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
58 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
59 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
60 * OTHER DEALINGS IN THE SOFTWARE.
61 */
62
63
64/*********************************************************************************************************************************
65* Header Files *
66*********************************************************************************************************************************/
67#include "the-netbsd-kernel.h"
68#include "internal/iprt.h"
69#include <iprt/mem.h>
70
71#include <iprt/assert.h>
72#include <iprt/errcore.h>
73#include <iprt/param.h>
74
75#include "r0drv/alloc-r0drv.h"
76
77
78DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
79{
80 size_t cbAllocated = cb;
81 PRTMEMHDR pHdr = NULL;
82
83 if (fFlags & RTMEMHDR_FLAG_ZEROED)
84 pHdr = kmem_zalloc(cb + sizeof(RTMEMHDR), KM_NOSLEEP);
85 else
86 pHdr = kmem_alloc(cb + sizeof(RTMEMHDR), KM_NOSLEEP);
87
88 if (RT_UNLIKELY(!pHdr))
89 return VERR_NO_MEMORY;
90
91 pHdr->u32Magic = RTMEMHDR_MAGIC;
92 pHdr->fFlags = fFlags;
93 pHdr->cb = cbAllocated;
94 pHdr->cbReq = cb;
95
96 *ppHdr = pHdr;
97 return VINF_SUCCESS;
98}
99
100
101DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
102{
103 pHdr->u32Magic += 1;
104
105 kmem_free(pHdr, pHdr->cb + sizeof(RTMEMHDR));
106}
107
108RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
109{
110 if (pv)
111 {
112 cb = round_page(cb);
113
114 paddr_t pa;
115 pmap_extract(pmap_kernel(), (vaddr_t)pv, &pa);
116
117 /*
118 * Reconstruct pglist to free the physical pages
119 */
120 struct pglist rlist;
121 TAILQ_INIT(&rlist);
122
123 for (paddr_t pa2 = pa ; pa2 < pa + cb ; pa2 += PAGE_SIZE) {
124 struct vm_page *page = PHYS_TO_VM_PAGE(pa2);
125 TAILQ_INSERT_TAIL(&rlist, page, pageq.queue);
126 }
127
128 /* Unmap */
129 pmap_kremove((vaddr_t)pv, cb);
130
131 /* Free the virtual space */
132 uvm_km_free(kernel_map, (vaddr_t)pv, cb, UVM_KMF_VAONLY);
133
134 /* Free the physical pages */
135 uvm_pglistfree(&rlist);
136 }
137}
138
139RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
140{
141 /*
142 * Validate input.
143 */
144 AssertPtr(pPhys);
145 Assert(cb > 0);
146
147 cb = round_page(cb);
148
149 vaddr_t virt = uvm_km_alloc(kernel_map, cb, 0,
150 UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_CANFAIL);
151 if (virt == 0)
152 return NULL;
153
154 struct pglist rlist;
155
156 if (uvm_pglistalloc(cb, 0, (paddr_t)0xFFFFFFFF,
157 PAGE_SIZE, 0, &rlist, 1, 1) != 0)
158 {
159 uvm_km_free(kernel_map, virt, cb, UVM_KMF_VAONLY);
160 return NULL;
161 }
162
163 struct vm_page *page;
164 vaddr_t virt2 = virt;
165 TAILQ_FOREACH(page, &rlist, pageq.queue)
166 {
167 pmap_kenter_pa(virt2, VM_PAGE_TO_PHYS(page),
168 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 0);
169 virt2 += PAGE_SIZE;
170 }
171
172 page = TAILQ_FIRST(&rlist);
173 *pPhys = VM_PAGE_TO_PHYS(page);
174
175 return (void *)virt;
176}
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