Home | History | Annotate | Line # | Download | only in hpcboot
memory.cpp revision 1.1.4.1
      1  1.1.4.1  nathanw /*	$NetBSD: memory.cpp,v 1.1.4.1 2001/04/09 01:52:40 nathanw Exp $	*/
      2      1.1      uch 
      3      1.1      uch /*-
      4      1.1      uch  * Copyright (c) 2001 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.1      uch 	_page_size =pagesize;
     47      1.1      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 	DPRINTF((TEXT("Page size %dbyte %dpages/region\n"),
     55      1.1      uch 		 _page_size , _page_per_region));
     56      1.1      uch 	_addr_table_idx = 0;
     57      1.1      uch 	_addr_table = 0;
     58      1.1      uch 	_memory = 0;
     59      1.1      uch }
     60      1.1      uch 
     61      1.1      uch MemoryManager::~MemoryManager(void)
     62      1.1      uch {
     63      1.1      uch 	if (_memory)
     64      1.1      uch 		VirtualFree(LPVOID(_memory), 0, MEM_RELEASE);
     65      1.1      uch }
     66      1.1      uch 
     67      1.1      uch void
     68      1.1      uch MemoryManager::loadBank(paddr_t paddr, psize_t psize)
     69      1.1      uch {
     70      1.1      uch 	struct MemoryManager::bank *b = &_bank[_nbank++];
     71      1.1      uch 	b->addr = paddr;
     72      1.1      uch 	b->size = psize;
     73      1.1      uch 	DPRINTF((TEXT("Bank#%d 0x%08x size 0x%08x\n"), _nbank - 1,
     74      1.1      uch 		 b->addr, b->size));
     75      1.1      uch }
     76      1.1      uch 
     77      1.1      uch BOOL
     78      1.1      uch MemoryManager::reservePage(vsize_t size, BOOL page_commit)
     79      1.1      uch {
     80      1.1      uch 	// My virtual memory space
     81      1.1      uch 	vaddr_t vbase;
     82      1.1      uch 	vsize_t vsize;
     83      1.1      uch 
     84      1.1      uch 	int i, npage;
     85      1.1      uch 
     86      1.1      uch 	if (size == 0)
     87      1.1      uch 		return FALSE;
     88      1.1      uch 
     89      1.1      uch   // reserve all virtual memory.
     90      1.1      uch 	vsize = roundRegion(size);
     91      1.1      uch 	npage = roundPage(size) / _page_size;
     92      1.1      uch 
     93      1.1      uch 	size_t tabsz = sizeof(struct AddressTranslationTable) * npage;
     94      1.1      uch 	_addr_table = static_cast <struct AddressTranslationTable *>
     95      1.1      uch 		(malloc(tabsz));
     96      1.1      uch 	if (_addr_table == NULL) {
     97      1.1      uch 		DPRINTF((TEXT("can't allocate memory for translation table.\n")));
     98      1.1      uch 		return FALSE;
     99      1.1      uch 	}
    100      1.1      uch 	DPRINTF((TEXT("address translation table %d pages.(%d byte)\n"), npage,
    101      1.1      uch 		 tabsz));
    102      1.1      uch 
    103      1.1      uch 	if (page_commit)
    104      1.1      uch 		vbase = vaddr_t(VirtualAlloc(0, vsize, MEM_RESERVE,
    105      1.1      uch 					     PAGE_NOACCESS));
    106      1.1      uch 	else
    107      1.1      uch 		vbase = vaddr_t(VirtualAlloc(0, vsize, MEM_COMMIT,
    108      1.1      uch 					     PAGE_READWRITE | PAGE_NOCACHE));
    109      1.1      uch 
    110      1.1      uch 	if (vbase == 0) {
    111      1.1      uch 		DPRINTF((TEXT("can't allocate memory\n")));
    112      1.1      uch 		return FALSE;
    113      1.1      uch 	}
    114      1.1      uch 	_memory = vbase;
    115      1.1      uch 
    116      1.1      uch   // find physical address of allocated page.
    117      1.1      uch 	AddressTranslationTable *tab = _addr_table;
    118      1.1      uch 	_naddr_table = 0;
    119      1.1      uch 	for (i = 0; i < npage; i++) {
    120      1.1      uch 		vaddr_t vaddr;
    121      1.1      uch 		paddr_t paddr = ~0;
    122      1.1      uch 
    123      1.1      uch 		if (page_commit)
    124      1.1      uch 			// now map to physical page.
    125      1.1      uch 			vaddr = vaddr_t(VirtualAlloc(
    126      1.1      uch 				LPVOID(vbase + _page_size * i),
    127      1.1      uch 				_page_size, MEM_COMMIT,
    128      1.1      uch 				PAGE_READWRITE | PAGE_NOCACHE));
    129      1.1      uch 		else
    130      1.1      uch 			vaddr = vbase + _page_size * i;
    131      1.1      uch 
    132      1.1      uch 		paddr = searchPage(vaddr);
    133      1.1      uch 
    134      1.1      uch 		if (paddr == ~0) {
    135      1.1      uch 			DPRINTF((TEXT("page#%d not found\n"), i));
    136      1.1      uch 			break;
    137      1.1      uch 		} else {
    138      1.1      uch #ifdef MEMORY_MAP_DEBUG
    139      1.1      uch 			DPRINTF((TEXT("page %d vaddr=0x%08x paddr=0x%08x\n"),
    140      1.1      uch 				 _naddr_table, vaddr, paddr));
    141      1.1      uch #endif
    142      1.1      uch 			tab->vaddr = vaddr;
    143      1.1      uch 			tab->paddr = paddr;
    144      1.1      uch 			++tab;
    145      1.1      uch 			++_naddr_table;
    146      1.1      uch 		}
    147      1.1      uch 	}
    148      1.1      uch 
    149      1.1      uch #ifdef MEMORY_MAP_DEBUG
    150      1.1      uch 	// dump virtual <-> physical address table
    151      1.1      uch 	tab = _addr_table;
    152      1.1      uch 	for (i = 0; i < _naddr_table;) {
    153      1.1      uch 		for (int j = 0; j < 4; j++, i++, tab++)
    154      1.1      uch 			DPRINTF((TEXT("%08x=%08x "), tab->vaddr, tab->paddr));
    155      1.1      uch 		DPRINTF((TEXT("\n")));
    156      1.1      uch 	}
    157      1.1      uch #endif
    158      1.1      uch 	DPRINTF((TEXT("allocated %d page. mapped %d page.\n"), npage,
    159      1.1      uch 		 _naddr_table));
    160      1.1      uch 
    161      1.1      uch 	return TRUE;
    162      1.1      uch }
    163      1.1      uch 
    164      1.1      uch BOOL
    165      1.1      uch MemoryManager::getPage(vaddr_t &vaddr, paddr_t &paddr)
    166      1.1      uch {
    167      1.1      uch 	/* get plain page from the top */
    168      1.1      uch 	int idx = --_naddr_table;
    169      1.1      uch 
    170      1.1      uch 	if (idx < 0 || _addr_table == NULL)
    171      1.1      uch 		return FALSE;
    172      1.1      uch 
    173      1.1      uch 	AddressTranslationTable *tab = &_addr_table[idx];
    174      1.1      uch 	vaddr = tab->vaddr;
    175      1.1      uch 	paddr = tab->paddr;
    176      1.1      uch 
    177      1.1      uch 	return TRUE;
    178      1.1      uch }
    179      1.1      uch 
    180      1.1      uch BOOL
    181      1.1      uch MemoryManager::getTaggedPage(vaddr_t &vaddr, paddr_t &paddr)
    182      1.1      uch {
    183      1.1      uch 	/* get tagged page from the bottom */
    184      1.1      uch 	if (_addr_table_idx >= _naddr_table ||
    185      1.1      uch 	    _addr_table == NULL) {
    186      1.1      uch 		DPRINTF((TEXT("page insufficient.\n")));
    187      1.1      uch 		return FALSE;
    188      1.1      uch 	}
    189      1.1      uch 	AddressTranslationTable *tab =
    190      1.1      uch 		&_addr_table[_addr_table_idx++];
    191      1.1      uch 	vaddr = tab->vaddr;
    192      1.1      uch 	paddr = tab->paddr;
    193      1.1      uch 
    194      1.1      uch 	return TRUE;
    195      1.1      uch }
    196      1.1      uch 
    197      1.1      uch BOOL
    198      1.1      uch MemoryManager::getTaggedPage(vaddr_t &v, paddr_t &p,
    199      1.1      uch 			     struct PageTag **pvec, paddr_t &pvec_paddr)
    200      1.1      uch {
    201      1.1      uch 	if (!getTaggedPage(v, p))
    202      1.1      uch 		return FALSE;
    203      1.1      uch 
    204      1.1      uch 	*pvec =(struct PageTag *)v;
    205      1.1      uch 	memset(*pvec, 0, sizeof(struct PageTag));
    206      1.1      uch 	v += sizeof(struct PageTag);
    207      1.1      uch 	pvec_paddr = p;
    208      1.1      uch 	p += sizeof(struct PageTag);
    209      1.1      uch 
    210      1.1      uch 	return TRUE;
    211      1.1      uch }
    212      1.1      uch 
    213      1.1      uch vaddr_t
    214      1.1      uch MemoryManager::mapPhysicalPage(paddr_t paddr, psize_t size, u_int32_t flags)
    215      1.1      uch {
    216      1.1      uch 	paddr_t pstart = truncPage(paddr);
    217      1.1      uch 	paddr_t pend = roundPage(paddr + size);
    218      1.1      uch 	psize_t psize = pend - pstart;
    219      1.1      uch 
    220      1.1      uch 	LPVOID p = VirtualAlloc(0, psize, MEM_RESERVE, PAGE_NOACCESS);
    221      1.1      uch 
    222      1.1      uch 	int ok = VirtualCopy(p, LPVOID(pstart >> 8), psize,
    223      1.1      uch 			     flags | PAGE_NOCACHE | PAGE_PHYSICAL);
    224      1.1      uch 	if (!ok) {
    225      1.1      uch 		DPRINTF((TEXT("can't map physical address 0x%08x\n"), paddr));
    226      1.1      uch 		return ~0;
    227      1.1      uch 	}
    228      1.1      uch #if 0
    229      1.1      uch 	DPRINTF((TEXT("start=0x%08x end=0x%08x size=0x%08x return=0x%08x\n"),
    230      1.1      uch 		 pstart, pend, psize, vaddr_t(p) + vaddr_t(paddr - pstart)));
    231      1.1      uch 
    232      1.1      uch #endif
    233      1.1      uch 
    234      1.1      uch 	return vaddr_t(p) + vaddr_t(paddr - pstart);
    235      1.1      uch }
    236      1.1      uch 
    237      1.1      uch void
    238      1.1      uch MemoryManager::unmapPhysicalPage(vaddr_t vaddr)
    239      1.1      uch {
    240      1.1      uch 	int ok = VirtualFree(LPVOID(truncPage(vaddr)), 0, MEM_RELEASE);
    241      1.1      uch 	if (!ok)
    242      1.1      uch 		DPRINTF((TEXT("can't release memory\n")));
    243      1.1      uch }
    244      1.1      uch 
    245      1.1      uch u_int32_t
    246      1.1      uch MemoryManager::readPhysical4(paddr_t paddr)
    247      1.1      uch {
    248      1.1      uch 	vaddr_t v = mapPhysicalPage(paddr, 4, PAGE_READONLY);
    249      1.1      uch 	u_int32_t val = *(u_int32_t *)v;
    250      1.1      uch 	unmapPhysicalPage(v);
    251      1.1      uch 	return val;
    252      1.1      uch }
    253      1.1      uch 
    254      1.1      uch //
    255      1.1      uch //	Use LockPages()
    256      1.1      uch //
    257      1.1      uch MemoryManager_LockPages::MemoryManager_LockPages
    258      1.1      uch (BOOL(*lock_pages)(LPVOID, DWORD, PDWORD, int),
    259      1.1      uch  BOOL(*unlock_pages)(LPVOID, DWORD),
    260      1.1      uch  Console *&cons, size_t pagesize, int shift)
    261      1.1      uch 	:  MemoryManager(cons, pagesize)
    262      1.1      uch {
    263      1.1      uch 	_lock_pages	= lock_pages;
    264      1.1      uch 	_unlock_pages	= unlock_pages;
    265      1.1      uch 	_shift = shift;
    266      1.1      uch 	DPRINTF((TEXT("use LockPages method.\n")));
    267      1.1      uch }
    268      1.1      uch 
    269      1.1      uch MemoryManager_LockPages::~MemoryManager_LockPages(void)
    270      1.1      uch {
    271      1.1      uch }
    272      1.1      uch 
    273      1.1      uch paddr_t
    274      1.1      uch MemoryManager_LockPages::searchPage(vaddr_t vaddr)
    275      1.1      uch {
    276      1.1      uch 	paddr_t paddr = ~0;
    277      1.1      uch 
    278      1.1      uch 	if (!_lock_pages(LPVOID(vaddr), _page_size, PDWORD(&paddr), 1))
    279      1.1      uch 		return paddr;
    280      1.1      uch 
    281      1.1      uch 	if (!_unlock_pages(LPVOID(vaddr), _page_size)) {
    282      1.1      uch 		DPRINTF((TEXT("can't unlock pages\n")));
    283      1.1      uch 	}
    284      1.1      uch 
    285      1.1      uch 	return(paddr >>(_page_shift - _shift)) << _page_shift;
    286      1.1      uch }
    287      1.1      uch 
    288      1.1      uch //
    289      1.1      uch //	Use VirtualCopy()
    290      1.1      uch //
    291      1.1      uch MemoryManager_VirtualCopy::MemoryManager_VirtualCopy(Console *&cons,
    292      1.1      uch 						     size_t pagesize)
    293      1.1      uch 	: MemoryManager(cons, pagesize)
    294      1.1      uch {
    295  1.1.4.1  nathanw 	_search_guess = 0;
    296      1.1      uch 	DPRINTF((TEXT("use VirtualCopy method.\n")));
    297      1.1      uch }
    298      1.1      uch 
    299      1.1      uch MemoryManager_VirtualCopy::~MemoryManager_VirtualCopy(void)
    300      1.1      uch {
    301      1.1      uch }
    302      1.1      uch 
    303      1.1      uch paddr_t
    304      1.1      uch MemoryManager_VirtualCopy::searchPage(vaddr_t vaddr)
    305      1.1      uch {
    306      1.1      uch 	paddr_t paddr = ~0;
    307      1.1      uch 	int i;
    308      1.1      uch 
    309      1.1      uch 	// search all D-RAM bank.
    310      1.1      uch 	setMagic(vaddr);
    311  1.1.4.1  nathanw  retry:
    312      1.1      uch 	for (i = 0; i < _nbank; i++) {
    313      1.1      uch 		paddr = searchBank(i);
    314      1.1      uch 		if (paddr != ~0)
    315      1.1      uch 			break;
    316      1.1      uch 	}
    317  1.1.4.1  nathanw 	if (_search_guess != 0 && paddr == ~0) {
    318  1.1.4.1  nathanw 		_search_guess = 0;
    319  1.1.4.1  nathanw 		goto retry;
    320  1.1.4.1  nathanw 	}
    321  1.1.4.1  nathanw 
    322      1.1      uch 	clearMagic();
    323      1.1      uch 
    324      1.1      uch 	return paddr;
    325      1.1      uch }
    326      1.1      uch 
    327      1.1      uch paddr_t
    328      1.1      uch MemoryManager_VirtualCopy::searchBank(int banknum)
    329      1.1      uch {
    330      1.1      uch 	LPVOID ref;
    331      1.1      uch 	paddr_t paddr, pstart, pend, pfound = ~0;
    332  1.1.4.1  nathanw 	paddr_t bstart, bend;
    333      1.1      uch 	vaddr_t ofs;
    334      1.1      uch 
    335  1.1.4.1  nathanw 	bstart = _bank[banknum].addr;
    336  1.1.4.1  nathanw 	bend = _bank[banknum].addr + _bank[banknum].size;
    337  1.1.4.1  nathanw 
    338  1.1.4.1  nathanw 	pstart = _search_guess ? _search_guess : bstart;
    339  1.1.4.1  nathanw 	pend = bend;
    340  1.1.4.1  nathanw 
    341  1.1.4.1  nathanw 	if (pstart < bstart || pstart >= pend)
    342  1.1.4.1  nathanw 		return pfound;
    343      1.1      uch 
    344      1.1      uch 	// reserve physical reference region
    345      1.1      uch 	ref = VirtualAlloc(0, BLOCK_SIZE, MEM_RESERVE, PAGE_NOACCESS);
    346      1.1      uch 	if (ref == 0) {
    347      1.1      uch 		DPRINTF((TEXT("can't allocate virtual memory.\n")));
    348      1.1      uch 		return pfound;
    349      1.1      uch 	}
    350      1.1      uch 
    351      1.1      uch 	for (paddr = pstart; paddr < pend; paddr += BLOCK_SIZE) {
    352      1.1      uch 		if (!VirtualCopy(ref, LPVOID(paddr >> 8), BLOCK_SIZE,
    353      1.1      uch 				 PAGE_READONLY | PAGE_NOCACHE |
    354      1.1      uch 				 PAGE_PHYSICAL)) {
    355      1.1      uch 			DPRINTF((TEXT("can't map physical addr 0x%08x(->0x%08x)\n"),
    356      1.1      uch 				 ref, paddr));
    357      1.1      uch 			goto release;
    358      1.1      uch 		}
    359      1.1      uch 
    360      1.1      uch 		// search magic in this region.
    361      1.1      uch 		ofs = checkMagicRegion(vaddr_t(ref), BLOCK_SIZE, _page_size);
    362      1.1      uch 
    363      1.1      uch 		// decommit reference region.
    364      1.1      uch 		if (!VirtualFree(ref, BLOCK_SIZE, MEM_DECOMMIT)) {
    365      1.1      uch 			DPRINTF((TEXT("can't decommit addr 0x%08x(->0x%08x)\n"),
    366      1.1      uch 				 ref, paddr));
    367      1.1      uch 			goto release;
    368      1.1      uch 		}
    369      1.1      uch 
    370      1.1      uch 		if (ofs != ~0) {
    371      1.1      uch 			pfound = paddr + ofs;
    372  1.1.4.1  nathanw 			_search_guess = paddr;
    373      1.1      uch 			break;
    374      1.1      uch 		}
    375      1.1      uch 	}
    376      1.1      uch  release:
    377      1.1      uch 	if (!VirtualFree(ref, 0, MEM_RELEASE))
    378      1.1      uch 		DPRINTF((TEXT("can't release memory\n")));
    379      1.1      uch 
    380      1.1      uch 	return pfound;
    381      1.1      uch }
    382