VirtualBox

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

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