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