1 | /* $Id: alloc-r0drv-netbsd.c 63191 2016-08-09 03:01:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, NetBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*********************************************************************************************************************************
|
---|
7 | * Header Files *
|
---|
8 | *********************************************************************************************************************************/
|
---|
9 | #include "the-netbsd-kernel.h"
|
---|
10 | #include "internal/iprt.h"
|
---|
11 | #include <iprt/mem.h>
|
---|
12 |
|
---|
13 | #include <iprt/assert.h>
|
---|
14 | #include <iprt/err.h>
|
---|
15 | #include <iprt/param.h>
|
---|
16 |
|
---|
17 | #include "r0drv/alloc-r0drv.h"
|
---|
18 |
|
---|
19 |
|
---|
20 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
21 | {
|
---|
22 | size_t cbAllocated = cb;
|
---|
23 | PRTMEMHDR pHdr = NULL;
|
---|
24 |
|
---|
25 | if (fFlags & RTMEMHDR_FLAG_ZEROED)
|
---|
26 | pHdr = kmem_zalloc(cb + sizeof(RTMEMHDR), KM_NOSLEEP);
|
---|
27 | else
|
---|
28 | pHdr = kmem_alloc(cb + sizeof(RTMEMHDR), KM_NOSLEEP);
|
---|
29 |
|
---|
30 | if (RT_UNLIKELY(!pHdr))
|
---|
31 | return VERR_NO_MEMORY;
|
---|
32 |
|
---|
33 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
34 | pHdr->fFlags = fFlags;
|
---|
35 | pHdr->cb = cbAllocated;
|
---|
36 | pHdr->cbReq = cb;
|
---|
37 |
|
---|
38 | *ppHdr = pHdr;
|
---|
39 | return VINF_SUCCESS;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
44 | {
|
---|
45 | pHdr->u32Magic += 1;
|
---|
46 |
|
---|
47 | kmem_free(pHdr, pHdr->cb + sizeof(RTMEMHDR));
|
---|
48 | }
|
---|
49 |
|
---|
50 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
51 | {
|
---|
52 | if (pv)
|
---|
53 | {
|
---|
54 | cb = round_page(cb);
|
---|
55 |
|
---|
56 | paddr_t pa;
|
---|
57 | pmap_extract(pmap_kernel(), (vaddr_t)pv, &pa);
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Reconstruct pglist to free the physical pages
|
---|
61 | */
|
---|
62 | struct pglist rlist;
|
---|
63 | TAILQ_INIT(&rlist);
|
---|
64 |
|
---|
65 | for (paddr_t pa2 = pa ; pa2 < pa + cb ; pa2 += PAGE_SIZE) {
|
---|
66 | struct vm_page *page = PHYS_TO_VM_PAGE(pa2);
|
---|
67 | TAILQ_INSERT_TAIL(&rlist, page, pageq.queue);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* Unmap */
|
---|
71 | pmap_kremove((vaddr_t)pv, cb);
|
---|
72 |
|
---|
73 | /* Free the virtual space */
|
---|
74 | uvm_km_free(kernel_map, (vaddr_t)pv, cb, UVM_KMF_VAONLY);
|
---|
75 |
|
---|
76 | /* Free the physical pages */
|
---|
77 | uvm_pglistfree(&rlist);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
82 | {
|
---|
83 | /*
|
---|
84 | * Validate input.
|
---|
85 | */
|
---|
86 | AssertPtr(pPhys);
|
---|
87 | Assert(cb > 0);
|
---|
88 |
|
---|
89 | cb = round_page(cb);
|
---|
90 |
|
---|
91 | vaddr_t virt = uvm_km_alloc(kernel_map, cb, 0,
|
---|
92 | UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_CANFAIL);
|
---|
93 | if (virt == 0)
|
---|
94 | return NULL;
|
---|
95 |
|
---|
96 | struct pglist rlist;
|
---|
97 |
|
---|
98 | if (uvm_pglistalloc(cb, 0, (paddr_t)0xFFFFFFFF,
|
---|
99 | PAGE_SIZE, 0, &rlist, 1, 1) != 0)
|
---|
100 | {
|
---|
101 | uvm_km_free(kernel_map, virt, cb, UVM_KMF_VAONLY);
|
---|
102 | return NULL;
|
---|
103 | }
|
---|
104 |
|
---|
105 | struct vm_page *page;
|
---|
106 | vaddr_t virt2 = virt;
|
---|
107 | TAILQ_FOREACH(page, &rlist, pageq.queue)
|
---|
108 | {
|
---|
109 | pmap_kenter_pa(virt2, VM_PAGE_TO_PHYS(page),
|
---|
110 | VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 0);
|
---|
111 | virt2 += PAGE_SIZE;
|
---|
112 | }
|
---|
113 |
|
---|
114 | page = TAILQ_FIRST(&rlist);
|
---|
115 | *pPhys = VM_PAGE_TO_PHYS(page);
|
---|
116 |
|
---|
117 | return (void *)virt;
|
---|
118 | }
|
---|