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