ram.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  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. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <lib.h>
  31. #include <vm.h>
  32. #include <mainbus.h>
  33. vaddr_t firstfree; /* first free virtual address; set by start.S */
  34. static paddr_t firstpaddr; /* address of first free physical page */
  35. static paddr_t lastpaddr; /* one past end of last free physical page */
  36. // vm stuffs
  37. extern struct coremap map;
  38. static void vmem_bootstrap(void)
  39. {
  40. paddr_t min;
  41. paddr_t max;
  42. // size of ram available
  43. ram_getsize(&min, &max);
  44. // # of frames if we start here
  45. map.framecount = (max - min) / PAGE_SIZE;
  46. // start of the coremap array
  47. map.num = (int *) PADDR_TO_KVADDR(min);
  48. // min should start at a page start address (roundup), and be after the space for the array
  49. min += ROUNDUP(map.framecount * sizeof(int), PAGE_SIZE);
  50. // framecount needs to reflect the frames we took for the array of frames
  51. map.framecount = (max - min) / PAGE_SIZE;
  52. // set the start of actual virtual memory
  53. map.start = min;
  54. // set next frame to the start
  55. map.next = 0;
  56. // set all frames to empty
  57. for (int i = 0; i < map.framecount; ++i)
  58. {
  59. map.num[i] = -1;
  60. }
  61. }
  62. /*
  63. * Called very early in system boot to figure out how much physical
  64. * RAM is available.
  65. */
  66. void
  67. ram_bootstrap(void)
  68. {
  69. size_t ramsize;
  70. /* Get size of RAM. */
  71. ramsize = mainbus_ramsize();
  72. /*
  73. * This is the same as the last physical address, as long as
  74. * we have less than 508 megabytes of memory. If we had more,
  75. * various annoying properties of the MIPS architecture would
  76. * force the RAM to be discontiguous. This is not a case we
  77. * are going to worry about.
  78. */
  79. if (ramsize > 508*1024*1024) {
  80. ramsize = 508*1024*1024;
  81. }
  82. lastpaddr = ramsize;
  83. /*
  84. * Get first free virtual address from where start.S saved it.
  85. * Convert to physical address.
  86. */
  87. firstpaddr = firstfree - MIPS_KSEG0;
  88. kprintf("%uk physical memory available\n",
  89. (lastpaddr-firstpaddr)/1024);
  90. // initialize VM
  91. vmem_bootstrap();
  92. }
  93. /*
  94. * This function is for allocating physical memory prior to VM
  95. * initialization.
  96. *
  97. * The pages it hands back will not be reported to the VM system when
  98. * the VM system calls ram_getsize(). If it's desired to free up these
  99. * pages later on after bootup is complete, some mechanism for adding
  100. * them to the VM system's page management must be implemented.
  101. * Alternatively, one can do enough VM initialization early so that
  102. * this function is never needed.
  103. *
  104. * Note: while the error return value of 0 is a legal physical address,
  105. * it's not a legal *allocatable* physical address, because it's the
  106. * page with the exception handlers on it.
  107. *
  108. * This function should not be called once the VM system is initialized,
  109. * so it is not synchronized.
  110. */
  111. paddr_t
  112. ram_stealmem(unsigned long npages)
  113. {
  114. size_t size;
  115. paddr_t paddr;
  116. size = npages * PAGE_SIZE;
  117. if (firstpaddr + size > lastpaddr) {
  118. return 0;
  119. }
  120. paddr = firstpaddr;
  121. firstpaddr += size;
  122. return paddr;
  123. }
  124. /*
  125. * This function is intended to be called by the VM system when it
  126. * initializes in order to find out what memory it has available to
  127. * manage.
  128. *
  129. * This function should not be called once the VM system is initialized,
  130. * so it is not synchronized.
  131. */
  132. void
  133. ram_getsize(paddr_t *lo, paddr_t *hi)
  134. {
  135. *lo = firstpaddr;
  136. *hi = lastpaddr;
  137. firstpaddr = lastpaddr = 0;
  138. }