Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/arch/mips/vm/ram.c
2098 views
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
30
#include <types.h>
31
#include <lib.h>
32
#include <vm.h>
33
#include <mainbus.h>
34
35
36
vaddr_t firstfree; /* first free virtual address; set by start.S */
37
paddr_t firstpaddr; /* address of first free physical page */
38
paddr_t lastpaddr; /* one past end of last free physical page */
39
40
/*
41
* Called very early in system boot to figure out how much physical
42
* RAM is available.
43
*/
44
void
45
ram_bootstrap(void)
46
{
47
size_t ramsize;
48
49
/* Get size of RAM. */
50
ramsize = mainbus_ramsize();
51
52
/*
53
* This is the same as the last physical address, as long as
54
* we have less than 508 megabytes of memory. If we had more,
55
* various annoying properties of the MIPS architecture would
56
* force the RAM to be discontiguous. This is not a case we
57
* are going to worry about.
58
*/
59
if (ramsize > 508*1024*1024) {
60
ramsize = 508*1024*1024;
61
}
62
63
lastpaddr = ramsize;
64
65
/*
66
* Get first free virtual address from where start.S saved it.
67
* Convert to physical address.
68
*/
69
firstpaddr = firstfree - MIPS_KSEG0;
70
71
kprintf("%uk physical memory available\n",
72
(lastpaddr-firstpaddr)/1024);
73
}
74
75
/*
76
* This function is for allocating physical memory prior to VM
77
* initialization.
78
*
79
* The pages it hands back will not be reported to the VM system when
80
* the VM system calls ram_getsize(). If it's desired to free up these
81
* pages later on after bootup is complete, some mechanism for adding
82
* them to the VM system's page management must be implemented.
83
* Alternatively, one can do enough VM initialization early so that
84
* this function is never needed.
85
*
86
* Note: while the error return value of 0 is a legal physical address,
87
* it's not a legal *allocatable* physical address, because it's the
88
* page with the exception handlers on it.
89
*
90
* This function should not be called once the VM system is initialized,
91
* so it is not synchronized.
92
*/
93
paddr_t
94
ram_stealmem(unsigned long npages)
95
{
96
size_t size;
97
paddr_t paddr;
98
99
size = npages * PAGE_SIZE;
100
101
if (firstpaddr + size > lastpaddr) {
102
return 0;
103
}
104
105
paddr = firstpaddr;
106
firstpaddr += size;
107
108
return paddr;
109
}
110
111
/*
112
* This function is intended to be called by the VM system when it
113
* initializes in order to find out what memory it has available to
114
* manage.
115
*
116
* This function should not be called once the VM system is initialized,
117
* so it is not synchronized.
118
*/
119
void
120
ram_getsize(paddr_t *lo, paddr_t *hi)
121
{
122
*lo = firstpaddr;
123
*hi = lastpaddr;
124
firstpaddr = lastpaddr = 0;
125
}
126
127