Home | History | Annotate | Line # | Download | only in arm
      1  1.6  martin /* -*-C++-*-	$NetBSD: arm_mmu.cpp,v 1.6 2008/04/28 20:23:20 martin 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  *
     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 <arm/arm_mmu.h>
     33  1.1     uch #include <console.h>
     34  1.1     uch 
     35  1.1     uch MemoryManager_ArmMMU::MemoryManager_ArmMMU(Console *&cons,
     36  1.2     uch     size_t pagesize)
     37  1.1     uch 	: MemoryManager(cons, pagesize)
     38  1.1     uch {
     39  1.1     uch 	DPRINTF((TEXT("Use ARM software MMU.\n")));
     40  1.1     uch }
     41  1.1     uch 
     42  1.1     uch MemoryManager_ArmMMU::~MemoryManager_ArmMMU(void)
     43  1.1     uch {
     44  1.1     uch 	SetKMode(_kmode);
     45  1.1     uch }
     46  1.1     uch 
     47  1.1     uch BOOL
     48  1.1     uch MemoryManager_ArmMMU::init(void)
     49  1.1     uch {
     50  1.5     uwe 	uint32_t reg;
     51  1.1     uch 
     52  1.1     uch 	_kmode = SetKMode(1);
     53  1.1     uch 	// Check system mode
     54  1.1     uch 	if ((GetCPSR() & 0x1f) != 0x1f) {
     55  1.1     uch 		DPRINTF((TEXT("not System mode\n")));
     56  1.1     uch 		return FALSE;
     57  1.1     uch 	}
     58  1.1     uch 	// Domain access control.(full access)
     59  1.1     uch 	SetCop15Reg3(~0);
     60  1.1     uch 
     61  1.1     uch 	// Get Translation table base.
     62  1.1     uch 	reg = GetCop15Reg2();
     63  1.1     uch 	_table_base =  reg & ARM_MMU_TABLEBASE_MASK;
     64  1.1     uch 	DPRINTF((TEXT("page directory address=0x%08x->0x%08x(0x%08x)\n"),
     65  1.2     uch 	    _table_base, readPhysical4(_table_base), reg));
     66  1.1     uch 
     67  1.1     uch 	return TRUE;
     68  1.1     uch }
     69  1.1     uch 
     70  1.1     uch paddr_t
     71  1.1     uch MemoryManager_ArmMMU::searchPage(vaddr_t vaddr)
     72  1.1     uch {
     73  1.1     uch 	paddr_t daddr, paddr = ~0;
     74  1.5     uwe 	uint32_t desc1, desc2;
     75  1.1     uch 
     76  1.1     uch 	// set marker.
     77  1.1     uch 	memset(LPVOID(vaddr), 0xa5, _page_size);
     78  1.1     uch 
     79  1.1     uch 	// PID virtual address mapping.
     80  1.1     uch 	DPRINTF((TEXT("Virtual Address 0x%08x"), vaddr));
     81  1.1     uch 	vaddr |= GetCop15Reg13();
     82  1.1     uch 	DPRINTF((TEXT("(+PID)-> 0x%08x\n"), vaddr));
     83  1.1     uch 
     84  1.1     uch 	daddr = _table_base | ARM_MMU_TABLEINDEX(vaddr);
     85  1.1     uch 	desc1 = readPhysical4(daddr);
     86  1.1     uch 	DPRINTF((TEXT("1st level descriptor 0x%08x(addr 0x%08x)\n"),
     87  1.2     uch 	    desc1, daddr));
     88  1.3     uch 
     89  1.1     uch 	switch(ARM_MMU_LEVEL1DESC_TRANSLATE_TYPE(desc1)) {
     90  1.1     uch 	default:
     91  1.1     uch 		DPRINTF((TEXT("1st level descriptor fault.\n")));
     92  1.1     uch 		break;
     93  1.1     uch 	case ARM_MMU_LEVEL1DESC_TRANSLATE_SECTION:
     94  1.1     uch 		paddr = ARM_MMU_SECTION_BASE(desc1) |
     95  1.2     uch 		    ARM_MMU_VADDR_SECTION_INDEX(vaddr);
     96  1.1     uch 		DPRINTF((TEXT("section Physical Address 0x%08x\n"), paddr));
     97  1.1     uch 		break;
     98  1.1     uch 	case ARM_MMU_LEVEL1DESC_TRANSLATE_PAGE:
     99  1.1     uch 		DPRINTF((TEXT("-> Level2 page descriptor.\n")));
    100  1.1     uch 		daddr = ARM_MMU_PTE_BASE(desc1) |
    101  1.2     uch 		    ARM_MMU_VADDR_PTE_INDEX(vaddr);
    102  1.1     uch 		desc2 = readPhysical4(daddr);
    103  1.1     uch 		DPRINTF((TEXT("2nd level descriptor 0x%08x(addr 0x%08x)\n"),
    104  1.2     uch 		    desc2, daddr));
    105  1.1     uch 		switch(desc2 & 0x3) {
    106  1.1     uch 		default:
    107  1.1     uch 			DPRINTF((TEXT("2nd level descriptor fault.\n")));
    108  1.1     uch 			break;
    109  1.1     uch 		case 2: // 4Kpage
    110  1.1     uch 			paddr =(desc2 & 0xfffff000) |(vaddr & 0x00000fff);
    111  1.1     uch 			break;
    112  1.1     uch 		case 1: // 64Kpage
    113  1.1     uch 			paddr =(desc2 & 0xffff0000) |(vaddr & 0x0000ffff);
    114  1.1     uch 			break;
    115  1.1     uch 		}
    116  1.1     uch 		break;
    117  1.1     uch 	}
    118  1.3     uch 
    119  1.1     uch 	return paddr;
    120  1.1     uch }
    121