vmm_mem.c revision 242163
1/*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD$"); 31 32#include <sys/param.h> 33#include <sys/lock.h> 34#include <sys/mutex.h> 35#include <sys/linker.h> 36#include <sys/systm.h> 37#include <sys/malloc.h> 38#include <sys/kernel.h> 39#include <sys/sysctl.h> 40 41#include <vm/vm.h> 42#include <vm/pmap.h> 43#include <vm/vm_page.h> 44#include <vm/vm_pageout.h> 45 46#include <machine/md_var.h> 47#include <machine/metadata.h> 48#include <machine/pc/bios.h> 49#include <machine/vmparam.h> 50#include <machine/pmap.h> 51 52#include "vmm_util.h" 53#include "vmm_mem.h" 54 55SYSCTL_DECL(_hw_vmm); 56 57static u_long pages_allocated; 58SYSCTL_ULONG(_hw_vmm, OID_AUTO, pages_allocated, CTLFLAG_RD, 59 &pages_allocated, 0, "4KB pages allocated"); 60 61static void 62update_pages_allocated(int howmany) 63{ 64 pages_allocated += howmany; /* XXX locking? */ 65} 66 67int 68vmm_mem_init(void) 69{ 70 71 return (0); 72} 73 74vm_paddr_t 75vmm_mem_alloc(size_t size) 76{ 77 int flags; 78 vm_page_t m; 79 vm_paddr_t pa; 80 81 if (size != PAGE_SIZE) 82 panic("vmm_mem_alloc: invalid allocation size %lu", size); 83 84 flags = VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | 85 VM_ALLOC_ZERO; 86 87 while (1) { 88 /* 89 * XXX need policy to determine when to back off the allocation 90 */ 91 m = vm_page_alloc(NULL, 0, flags); 92 if (m == NULL) 93 VM_WAIT; 94 else 95 break; 96 } 97 98 pa = VM_PAGE_TO_PHYS(m); 99 100 if ((m->flags & PG_ZERO) == 0) 101 pagezero((void *)PHYS_TO_DMAP(pa)); 102 m->valid = VM_PAGE_BITS_ALL; 103 104 update_pages_allocated(1); 105 106 return (pa); 107} 108 109void 110vmm_mem_free(vm_paddr_t base, size_t length) 111{ 112 vm_page_t m; 113 114 if (base & PAGE_MASK) { 115 panic("vmm_mem_free: base 0x%0lx must be aligned on a " 116 "0x%0x boundary\n", base, PAGE_SIZE); 117 } 118 119 if (length != PAGE_SIZE) 120 panic("vmm_mem_free: invalid length %lu", length); 121 122 m = PHYS_TO_VM_PAGE(base); 123 m->wire_count--; 124 vm_page_free(m); 125 atomic_subtract_int(&cnt.v_wire_count, 1); 126 127 update_pages_allocated(-1); 128} 129 130vm_paddr_t 131vmm_mem_maxaddr(void) 132{ 133 134 return (ptoa(Maxmem)); 135} 136