123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /*
- * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
- * The President and Fellows of Harvard College.
- *
- * 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.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY 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 UNIVERSITY OR CONTRIBUTORS 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.
- */
- #include <types.h>
- #include <lib.h>
- #include <vm.h>
- #include <mainbus.h>
- vaddr_t firstfree; /* first free virtual address; set by start.S */
- static paddr_t firstpaddr; /* address of first free physical page */
- static paddr_t lastpaddr; /* one past end of last free physical page */
- // vm stuffs
- extern struct coremap map;
- static void vmem_bootstrap(void)
- {
- paddr_t min;
- paddr_t max;
- // size of ram available
- ram_getsize(&min, &max);
- // # of frames if we start here
- map.framecount = (max - min) / PAGE_SIZE;
- // start of the coremap array
- map.num = (int *) PADDR_TO_KVADDR(min);
- // min should start at a page start address (roundup), and be after the space for the array
- min += ROUNDUP(map.framecount * sizeof(int), PAGE_SIZE);
- // framecount needs to reflect the frames we took for the array of frames
- map.framecount = (max - min) / PAGE_SIZE;
- // set the start of actual virtual memory
- map.start = min;
- // set next frame to the start
- map.next = 0;
-
- // set all frames to empty
- for (int i = 0; i < map.framecount; ++i)
- {
- map.num[i] = -1;
- }
- }
- /*
- * Called very early in system boot to figure out how much physical
- * RAM is available.
- */
- void
- ram_bootstrap(void)
- {
- size_t ramsize;
-
- /* Get size of RAM. */
- ramsize = mainbus_ramsize();
- /*
- * This is the same as the last physical address, as long as
- * we have less than 508 megabytes of memory. If we had more,
- * various annoying properties of the MIPS architecture would
- * force the RAM to be discontiguous. This is not a case we
- * are going to worry about.
- */
- if (ramsize > 508*1024*1024) {
- ramsize = 508*1024*1024;
- }
- lastpaddr = ramsize;
- /*
- * Get first free virtual address from where start.S saved it.
- * Convert to physical address.
- */
- firstpaddr = firstfree - MIPS_KSEG0;
- kprintf("%uk physical memory available\n",
- (lastpaddr-firstpaddr)/1024);
-
- // initialize VM
- vmem_bootstrap();
- }
- /*
- * This function is for allocating physical memory prior to VM
- * initialization.
- *
- * The pages it hands back will not be reported to the VM system when
- * the VM system calls ram_getsize(). If it's desired to free up these
- * pages later on after bootup is complete, some mechanism for adding
- * them to the VM system's page management must be implemented.
- * Alternatively, one can do enough VM initialization early so that
- * this function is never needed.
- *
- * Note: while the error return value of 0 is a legal physical address,
- * it's not a legal *allocatable* physical address, because it's the
- * page with the exception handlers on it.
- *
- * This function should not be called once the VM system is initialized,
- * so it is not synchronized.
- */
- paddr_t
- ram_stealmem(unsigned long npages)
- {
- size_t size;
- paddr_t paddr;
- size = npages * PAGE_SIZE;
- if (firstpaddr + size > lastpaddr) {
- return 0;
- }
- paddr = firstpaddr;
- firstpaddr += size;
- return paddr;
- }
- /*
- * This function is intended to be called by the VM system when it
- * initializes in order to find out what memory it has available to
- * manage.
- *
- * This function should not be called once the VM system is initialized,
- * so it is not synchronized.
- */
- void
- ram_getsize(paddr_t *lo, paddr_t *hi)
- {
- *lo = firstpaddr;
- *hi = lastpaddr;
- firstpaddr = lastpaddr = 0;
- }
|