memory.cpp revision 1.7.2.1 1 /* $NetBSD: memory.cpp,v 1.7.2.1 2006/03/01 09:27:54 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <memory.h>
40 #include <console.h>
41
42 MemoryManager::MemoryManager(Console *&cons, size_t pagesize)
43 : _cons(cons)
44 {
45 _debug = FALSE;
46 _page_size = pagesize;
47
48 int mask = _page_size;
49 for (_page_shift = 0; !(mask & 1); _page_shift++)
50 mask >>= 1;
51
52 _page_per_region = WCE_REGION_SIZE / _page_size;
53 _nbank = 0;
54 _addr_table_idx = 0;
55 _addr_table = 0;
56 _memory = 0;
57 }
58
59 MemoryManager::~MemoryManager(void)
60 {
61 if (_memory)
62 VirtualFree(LPVOID(_memory), 0, MEM_RELEASE);
63 }
64
65 void
66 MemoryManager::loadBank(paddr_t paddr, psize_t psize)
67 {
68 struct MemoryManager::bank *b = &_bank[_nbank++];
69 b->addr = paddr;
70 b->size = psize;
71 DPRINTF((TEXT("[%d] 0x%08x size 0x%08x\n"), _nbank - 1,
72 b->addr, b->size));
73 }
74
75 BOOL
76 MemoryManager::reservePage(vsize_t size, BOOL page_commit)
77 {
78 // My virtual memory space
79 vaddr_t vbase;
80 vsize_t vsize;
81
82 int i, npage;
83
84 if (size == 0)
85 return FALSE;
86
87 // reserve all virtual memory.
88 vsize = roundRegion(size);
89 npage = roundPage(size) / _page_size;
90
91 size_t tabsz = sizeof(struct AddressTranslationTable) * npage;
92 _addr_table = static_cast <struct AddressTranslationTable *>
93 (malloc(tabsz));
94 if (_addr_table == NULL) {
95 DPRINTF((TEXT("can't allocate memory for translation table.\n")));
96 return FALSE;
97 }
98 DPRINTF((TEXT("address translation table %d pages. (0x%x bytes)\n"),
99 npage, tabsz));
100
101 if (page_commit)
102 vbase = vaddr_t(VirtualAlloc(0, vsize, MEM_RESERVE,
103 PAGE_NOACCESS));
104 else
105 vbase = vaddr_t(VirtualAlloc(0, vsize, MEM_COMMIT,
106 PAGE_READWRITE | PAGE_NOCACHE));
107
108 if (vbase == 0) {
109 DPRINTF((TEXT("can't allocate memory\n")));
110 return FALSE;
111 }
112 _memory = vbase;
113
114 // find physical address of allocated page.
115 AddressTranslationTable *tab = _addr_table;
116 _naddr_table = 0;
117 for (i = 0; i < npage; i++) {
118 vaddr_t vaddr;
119 paddr_t paddr = ~0;
120
121 if (page_commit)
122 // now map to physical page.
123 vaddr = vaddr_t(VirtualAlloc(
124 LPVOID(vbase + _page_size * i),
125 _page_size, MEM_COMMIT,
126 PAGE_READWRITE | PAGE_NOCACHE));
127 else
128 vaddr = vbase + _page_size * i;
129
130 paddr = searchPage(vaddr);
131
132 if (paddr == ~0) {
133 DPRINTF((TEXT("page#%d not found\n"), i));
134 break;
135 } else {
136 #ifdef MEMORY_MAP_DEBUG
137 DPRINTF((TEXT("page %d vaddr=0x%08x paddr=0x%08x\n"),
138 _naddr_table, vaddr, paddr));
139 #endif
140 tab->vaddr = vaddr;
141 tab->paddr = paddr;
142 ++tab;
143 ++_naddr_table;
144 }
145 }
146
147 #ifdef MEMORY_MAP_DEBUG
148 // dump virtual <-> physical address table
149 tab = _addr_table;
150 for (i = 0; i < _naddr_table;) {
151 for (int j = 0; j < 4; j++, i++, tab++)
152 DPRINTF((TEXT("%08x=%08x "), tab->vaddr, tab->paddr));
153 DPRINTF((TEXT("\n")));
154 }
155 #endif
156 DPRINTF((TEXT("allocated %d page. mapped %d page.\n"), npage,
157 _naddr_table));
158
159 return TRUE;
160 }
161
162 BOOL
163 MemoryManager::getPage(vaddr_t &vaddr, paddr_t &paddr)
164 {
165 /* get plain page from the top */
166 if (_addr_table_idx >= _naddr_table ||
167 _addr_table == NULL)
168 return FALSE;
169
170 int idx = --_naddr_table;
171
172 AddressTranslationTable *tab = &_addr_table[idx];
173 vaddr = tab->vaddr;
174 paddr = tab->paddr;
175
176 return TRUE;
177 }
178
179 BOOL
180 MemoryManager::getTaggedPage(vaddr_t &vaddr, paddr_t &paddr)
181 {
182 /* get tagged page from the bottom */
183 if (_addr_table_idx >= _naddr_table ||
184 _addr_table == NULL) {
185 DPRINTF((TEXT("page insufficient.\n")));
186 return FALSE;
187 }
188 AddressTranslationTable *tab =
189 &_addr_table[_addr_table_idx++];
190 vaddr = tab->vaddr;
191 paddr = tab->paddr;
192
193 return TRUE;
194 }
195
196 BOOL
197 MemoryManager::getTaggedPage(vaddr_t &v, paddr_t &p,
198 struct PageTag **pvec, paddr_t &pvec_paddr)
199 {
200 if (!getTaggedPage(v, p))
201 return FALSE;
202
203 *pvec =(struct PageTag *)v;
204 memset(*pvec, 0, sizeof(struct PageTag));
205 v += sizeof(struct PageTag);
206 pvec_paddr = p;
207 p += sizeof(struct PageTag);
208
209 return TRUE;
210 }
211
212 vaddr_t
213 MemoryManager::mapPhysicalPage(paddr_t paddr, psize_t size, u_int32_t flags)
214 {
215 paddr_t pstart = truncPage(paddr);
216 paddr_t pend = roundPage(paddr + size);
217 psize_t psize = pend - pstart;
218
219 LPVOID p = VirtualAlloc(0, psize, MEM_RESERVE, PAGE_NOACCESS);
220
221 int ok = VirtualCopy(p, LPVOID(pstart >> 8), psize,
222 flags | PAGE_NOCACHE | PAGE_PHYSICAL);
223 if (!ok) {
224 DPRINTF((TEXT("can't map physical address 0x%08x\n"), paddr));
225 return ~0;
226 }
227 #if 0
228 DPRINTF((TEXT("start=0x%08x end=0x%08x size=0x%08x return=0x%08x\n"),
229 pstart, pend, psize, vaddr_t(p) + vaddr_t(paddr - pstart)));
230 #endif
231 return vaddr_t(p) + vaddr_t(paddr - pstart);
232 }
233
234 void
235 MemoryManager::unmapPhysicalPage(vaddr_t vaddr)
236 {
237 int ok = VirtualFree(LPVOID(truncPage(vaddr)), 0, MEM_RELEASE);
238 if (!ok)
239 DPRINTF((TEXT("can't release memory\n")));
240 }
241
242 u_int32_t
243 MemoryManager::readPhysical4(paddr_t paddr)
244 {
245 vaddr_t v = mapPhysicalPage(paddr, 4, PAGE_READONLY);
246 u_int32_t val = *(u_int32_t *)v;
247 unmapPhysicalPage(v);
248 return val;
249 }
250
251 //
252 // Use LockPages()
253 //
254 MemoryManager_LockPages::MemoryManager_LockPages
255 (BOOL(*lock_pages)(LPVOID, DWORD, PDWORD, int),
256 BOOL(*unlock_pages)(LPVOID, DWORD),
257 Console *&cons, size_t pagesize, int shift)
258 : MemoryManager(cons, pagesize)
259 {
260 _lock_pages = lock_pages;
261 _unlock_pages = unlock_pages;
262 _shift = shift;
263 DPRINTF((TEXT("MemoryManager: LockPages\n")));
264 }
265
266 MemoryManager_LockPages::~MemoryManager_LockPages(void)
267 {
268 }
269
270 paddr_t
271 MemoryManager_LockPages::searchPage(vaddr_t vaddr)
272 {
273 paddr_t paddr = ~0;
274
275 if (!_lock_pages(LPVOID(vaddr), _page_size, PDWORD(&paddr), 1))
276 return paddr;
277
278 if (!_unlock_pages(LPVOID(vaddr), _page_size)) {
279 DPRINTF((TEXT("can't unlock pages\n")));
280 }
281
282 return(paddr >>(_page_shift - _shift)) << _page_shift;
283 }
284
285 //
286 // Use VirtualCopy()
287 //
288 MemoryManager_VirtualCopy::MemoryManager_VirtualCopy(Console *&cons,
289 size_t pagesize)
290 : MemoryManager(cons, pagesize)
291 {
292 _search_guess = 0;
293 DPRINTF((TEXT("MemoryManager: VirtualCopy\n")));
294 }
295
296 MemoryManager_VirtualCopy::~MemoryManager_VirtualCopy(void)
297 {
298 }
299
300 paddr_t
301 MemoryManager_VirtualCopy::searchPage(vaddr_t vaddr)
302 {
303 paddr_t paddr = ~0;
304 int i;
305
306 // search all D-RAM bank.
307 setMagic(vaddr);
308 retry:
309 for (i = 0; i < _nbank; i++) {
310 paddr = searchBank(i);
311 if (paddr != ~0)
312 break;
313 }
314 if (_search_guess != 0 && paddr == ~0) {
315 _search_guess = 0;
316 goto retry;
317 }
318
319 clearMagic();
320
321 return paddr;
322 }
323
324 paddr_t
325 MemoryManager_VirtualCopy::searchBank(int banknum)
326 {
327 LPVOID ref;
328 paddr_t paddr, pstart, pend, pfound = ~0;
329 paddr_t bstart, bend;
330 vaddr_t ofs;
331
332 bstart = _bank[banknum].addr;
333 bend = _bank[banknum].addr + _bank[banknum].size;
334
335 pstart = _search_guess ? _search_guess : bstart;
336 pend = bend;
337
338 if (pstart < bstart || pstart >= pend)
339 return pfound;
340
341 // reserve physical reference region
342 ref = VirtualAlloc(0, BLOCK_SIZE, MEM_RESERVE, PAGE_NOACCESS);
343 if (ref == 0) {
344 DPRINTF((TEXT("can't allocate virtual memory.\n")));
345 return pfound;
346 }
347
348 for (paddr = pstart; paddr < pend; paddr += BLOCK_SIZE) {
349 if (!VirtualCopy(ref, LPVOID(paddr >> 8), BLOCK_SIZE,
350 PAGE_READONLY | PAGE_NOCACHE | PAGE_PHYSICAL)) {
351 DPRINTF((TEXT("can't map physical addr 0x%08x(->0x%08x)\n"),
352 ref, paddr));
353 goto release;
354 }
355
356 // search magic in this region.
357 ofs = checkMagicRegion(vaddr_t(ref), BLOCK_SIZE, _page_size);
358
359 // decommit reference region.
360 if (!VirtualFree(ref, BLOCK_SIZE, MEM_DECOMMIT)) {
361 DPRINTF((TEXT("can't decommit addr 0x%08x(->0x%08x)\n"),
362 ref, paddr));
363 goto release;
364 }
365
366 if (ofs != ~0) {
367 pfound = paddr + ofs;
368 _search_guess = paddr;
369 break;
370 }
371 }
372 release:
373 if (!VirtualFree(ref, 0, MEM_RELEASE))
374 DPRINTF((TEXT("can't release memory\n")));
375
376 return pfound;
377 }
378