Home | History | Annotate | Line # | Download | only in hpcboot
memory.cpp revision 1.4.2.1
      1  1.4.2.1  jdolecek /*	$NetBSD: memory.cpp,v 1.4.2.1 2002/02/11 20:07:59 jdolecek Exp $	*/
      2      1.1       uch 
      3      1.1       uch /*-
      4  1.4.2.1  jdolecek  * 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.4.2.1  jdolecek 	_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 	_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.4.2.1  jdolecek 	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.1       uch 
     84      1.1       uch 	if (size == 0)
     85      1.1       uch 		return FALSE;
     86      1.1       uch 
     87  1.4.2.1  jdolecek 	// 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.4.2.1  jdolecek 	// 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.3       uch 	    &_addr_table[_addr_table_idx++];
    190      1.1       uch 	vaddr = tab->vaddr;
    191      1.1       uch 	paddr = tab->paddr;
    192      1.1       uch 
    193      1.1       uch 	return TRUE;
    194      1.1       uch }
    195      1.1       uch 
    196      1.1       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.1       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 
    231      1.1       uch #endif
    232      1.1       uch 
    233      1.1       uch 	return vaddr_t(p) + vaddr_t(paddr - pstart);
    234      1.1       uch }
    235      1.1       uch 
    236      1.1       uch void
    237      1.1       uch MemoryManager::unmapPhysicalPage(vaddr_t vaddr)
    238      1.1       uch {
    239      1.1       uch 	int ok = VirtualFree(LPVOID(truncPage(vaddr)), 0, MEM_RELEASE);
    240      1.1       uch 	if (!ok)
    241      1.1       uch 		DPRINTF((TEXT("can't release memory\n")));
    242      1.1       uch }
    243      1.1       uch 
    244      1.1       uch u_int32_t
    245      1.1       uch MemoryManager::readPhysical4(paddr_t paddr)
    246      1.1       uch {
    247      1.1       uch 	vaddr_t v = mapPhysicalPage(paddr, 4, PAGE_READONLY);
    248      1.1       uch 	u_int32_t val = *(u_int32_t *)v;
    249      1.1       uch 	unmapPhysicalPage(v);
    250      1.1       uch 	return val;
    251      1.1       uch }
    252      1.1       uch 
    253      1.1       uch //
    254      1.1       uch //	Use LockPages()
    255      1.1       uch //
    256      1.1       uch MemoryManager_LockPages::MemoryManager_LockPages
    257      1.1       uch (BOOL(*lock_pages)(LPVOID, DWORD, PDWORD, int),
    258      1.3       uch     BOOL(*unlock_pages)(LPVOID, DWORD),
    259      1.3       uch     Console *&cons, size_t pagesize, int shift)
    260      1.1       uch 	:  MemoryManager(cons, pagesize)
    261      1.1       uch {
    262      1.1       uch 	_lock_pages	= lock_pages;
    263      1.1       uch 	_unlock_pages	= unlock_pages;
    264      1.1       uch 	_shift = shift;
    265  1.4.2.1  jdolecek 	DPRINTF((TEXT("MemoryManager: LockPages\n")));
    266      1.1       uch }
    267      1.1       uch 
    268      1.1       uch MemoryManager_LockPages::~MemoryManager_LockPages(void)
    269      1.1       uch {
    270      1.1       uch }
    271      1.1       uch 
    272      1.1       uch paddr_t
    273      1.1       uch MemoryManager_LockPages::searchPage(vaddr_t vaddr)
    274      1.1       uch {
    275      1.1       uch 	paddr_t paddr = ~0;
    276      1.1       uch 
    277      1.1       uch 	if (!_lock_pages(LPVOID(vaddr), _page_size, PDWORD(&paddr), 1))
    278      1.1       uch 		return paddr;
    279      1.1       uch 
    280      1.1       uch 	if (!_unlock_pages(LPVOID(vaddr), _page_size)) {
    281      1.1       uch 		DPRINTF((TEXT("can't unlock pages\n")));
    282      1.1       uch 	}
    283      1.1       uch 
    284      1.1       uch 	return(paddr >>(_page_shift - _shift)) << _page_shift;
    285      1.1       uch }
    286      1.1       uch 
    287      1.1       uch //
    288      1.1       uch //	Use VirtualCopy()
    289      1.1       uch //
    290      1.1       uch MemoryManager_VirtualCopy::MemoryManager_VirtualCopy(Console *&cons,
    291      1.3       uch     size_t pagesize)
    292      1.1       uch 	: MemoryManager(cons, pagesize)
    293      1.1       uch {
    294      1.2       uch 	_search_guess = 0;
    295  1.4.2.1  jdolecek 	DPRINTF((TEXT("MemoryManager: VirtualCopy\n")));
    296      1.1       uch }
    297      1.1       uch 
    298      1.1       uch MemoryManager_VirtualCopy::~MemoryManager_VirtualCopy(void)
    299      1.1       uch {
    300      1.1       uch }
    301      1.1       uch 
    302      1.1       uch paddr_t
    303      1.1       uch MemoryManager_VirtualCopy::searchPage(vaddr_t vaddr)
    304      1.1       uch {
    305      1.1       uch 	paddr_t paddr = ~0;
    306      1.1       uch 	int i;
    307      1.1       uch 
    308      1.1       uch 	// search all D-RAM bank.
    309      1.1       uch 	setMagic(vaddr);
    310      1.2       uch  retry:
    311      1.1       uch 	for (i = 0; i < _nbank; i++) {
    312      1.1       uch 		paddr = searchBank(i);
    313      1.1       uch 		if (paddr != ~0)
    314      1.1       uch 			break;
    315      1.1       uch 	}
    316      1.2       uch 	if (_search_guess != 0 && paddr == ~0) {
    317      1.2       uch 		_search_guess = 0;
    318      1.2       uch 		goto retry;
    319      1.2       uch 	}
    320      1.2       uch 
    321      1.1       uch 	clearMagic();
    322      1.1       uch 
    323      1.1       uch 	return paddr;
    324      1.1       uch }
    325      1.1       uch 
    326      1.1       uch paddr_t
    327      1.1       uch MemoryManager_VirtualCopy::searchBank(int banknum)
    328      1.1       uch {
    329      1.1       uch 	LPVOID ref;
    330      1.1       uch 	paddr_t paddr, pstart, pend, pfound = ~0;
    331      1.2       uch 	paddr_t bstart, bend;
    332      1.1       uch 	vaddr_t ofs;
    333      1.1       uch 
    334      1.2       uch 	bstart = _bank[banknum].addr;
    335      1.2       uch 	bend = _bank[banknum].addr + _bank[banknum].size;
    336      1.2       uch 
    337      1.2       uch 	pstart = _search_guess ? _search_guess : bstart;
    338      1.2       uch 	pend = bend;
    339      1.2       uch 
    340      1.2       uch 	if (pstart < bstart || pstart >= pend)
    341      1.2       uch 		return pfound;
    342      1.1       uch 
    343      1.1       uch 	// reserve physical reference region
    344      1.1       uch 	ref = VirtualAlloc(0, BLOCK_SIZE, MEM_RESERVE, PAGE_NOACCESS);
    345      1.1       uch 	if (ref == 0) {
    346      1.1       uch 		DPRINTF((TEXT("can't allocate virtual memory.\n")));
    347      1.1       uch 		return pfound;
    348      1.1       uch 	}
    349      1.1       uch 
    350      1.1       uch 	for (paddr = pstart; paddr < pend; paddr += BLOCK_SIZE) {
    351      1.1       uch 		if (!VirtualCopy(ref, LPVOID(paddr >> 8), BLOCK_SIZE,
    352      1.3       uch 		    PAGE_READONLY | PAGE_NOCACHE | PAGE_PHYSICAL)) {
    353      1.1       uch 			DPRINTF((TEXT("can't map physical addr 0x%08x(->0x%08x)\n"),
    354      1.3       uch 			    ref, paddr));
    355      1.1       uch 			goto release;
    356      1.1       uch 		}
    357      1.1       uch 
    358      1.1       uch 		// search magic in this region.
    359      1.1       uch 		ofs = checkMagicRegion(vaddr_t(ref), BLOCK_SIZE, _page_size);
    360      1.1       uch 
    361      1.1       uch 		// decommit reference region.
    362      1.1       uch 		if (!VirtualFree(ref, BLOCK_SIZE, MEM_DECOMMIT)) {
    363      1.1       uch 			DPRINTF((TEXT("can't decommit addr 0x%08x(->0x%08x)\n"),
    364      1.3       uch 			    ref, paddr));
    365      1.1       uch 			goto release;
    366      1.1       uch 		}
    367      1.1       uch 
    368      1.1       uch 		if (ofs != ~0) {
    369      1.1       uch 			pfound = paddr + ofs;
    370      1.2       uch 			_search_guess = paddr;
    371      1.1       uch 			break;
    372      1.1       uch 		}
    373      1.1       uch 	}
    374      1.1       uch  release:
    375      1.1       uch 	if (!VirtualFree(ref, 0, MEM_RELEASE))
    376      1.1       uch 		DPRINTF((TEXT("can't release memory\n")));
    377      1.1       uch 
    378      1.1       uch 	return pfound;
    379      1.1       uch }
    380