/*- * Copyright (c) 2003-2004 Hiten Pandya * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS, CONTRIBUTORS OR VOICES IN HITEN PANDYA'S HEAD * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Module that allocates vast amounts of contiguous memory at load-time, * it uses contigmalloc(9)/contigfree(9) API. * */ #include #include #include #include #include #include #include #include #define NUM_CONTIG_PG 20 #define NUM_CONTIG_SIZ ctob(NUM_CONTIG_PG) struct vm_page *pga; char *addr; /* * vm_contig_alloc_test - just as the name says... */ static int vm_contig_alloc_test(void) { char *p; int n; pga = vm_page_array; addr = contigmalloc((u_long)NUM_CONTIG_SIZ, M_DEVBUF, M_WAITOK, 0, ~0ul, PAGE_SIZE, 0); if (addr == NULL) { uprintf("contigmalloc did not succeed!\n"); return EINVAL; } uprintf("Number of VM Contig Pages: %d (start addr = %p)\n", NUM_CONTIG_SIZ, addr); for (p = addr, n = 0 ; p < (addr + (NUM_CONTIG_SIZ)); p += PAGE_SIZE, n++) { if (p == addr) { uprintf("First page: va=%p\n", p); } else if (n == NUM_CONTIG_PG - 1) { uprintf("Last page: va=%p\n", p); } else { uprintf("Page: size = %d, number = %2d, addr = %p\n", (vm_offset_t)(vtophys(p) - vtophys(p-PAGE_SIZE)), n+1, p); } } return (0); } /* * The function called at load/unload. */ static int vmcontig_modevent(struct module *module, int cmd, void *arg) { int error; error = 0; switch (cmd) { case MOD_LOAD: error = vm_contig_alloc_test(); break; case MOD_UNLOAD: case MOD_SHUTDOWN: contigfree(addr, NUM_CONTIG_SIZ, M_DEVBUF); uprintf("Contig memory deallocated\n"); break; default: error = EINVAL; break; } return error; } static moduledata_t mod_data = { "vmcontig", vmcontig_modevent, 0 }; DECLARE_MODULE(vmcontig, mod_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);