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