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