vtophys.cc revision 8232
16019Shines@cs.fsu.edu/*
27694SAli.Saidi@ARM.com * Copyright (c) 2010 ARM Limited
37694SAli.Saidi@ARM.com * All rights reserved
47694SAli.Saidi@ARM.com *
57694SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall
67694SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual
77694SAli.Saidi@ARM.com * property including but not limited to intellectual property relating
87694SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software
97694SAli.Saidi@ARM.com * licensed hereunder.  You may use the software subject to the license
107694SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated
117694SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software,
127694SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form.
137694SAli.Saidi@ARM.com *
146019Shines@cs.fsu.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
156019Shines@cs.fsu.edu * Copyright (c) 2007-2008 The Florida State University
166019Shines@cs.fsu.edu * All rights reserved.
176019Shines@cs.fsu.edu *
186019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without
196019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are
206019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright
216019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer;
226019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright
236019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the
246019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution;
256019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its
266019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
276019Shines@cs.fsu.edu * this software without specific prior written permission.
286019Shines@cs.fsu.edu *
296019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
306019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
316019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
326019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
336019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
346019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
356019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
366019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
376019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
386019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
396019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
406019Shines@cs.fsu.edu *
416019Shines@cs.fsu.edu * Authors: Ali Saidi
426019Shines@cs.fsu.edu *          Nathan Binkert
436019Shines@cs.fsu.edu *          Stephen Hines
446019Shines@cs.fsu.edu */
456019Shines@cs.fsu.edu
466019Shines@cs.fsu.edu#include <string>
476019Shines@cs.fsu.edu
487694SAli.Saidi@ARM.com#include "arch/arm/table_walker.hh"
497694SAli.Saidi@ARM.com#include "arch/arm/tlb.hh"
506019Shines@cs.fsu.edu#include "arch/arm/vtophys.hh"
516019Shines@cs.fsu.edu#include "base/chunk_generator.hh"
526019Shines@cs.fsu.edu#include "base/trace.hh"
536019Shines@cs.fsu.edu#include "cpu/thread_context.hh"
546019Shines@cs.fsu.edu#include "mem/vport.hh"
556019Shines@cs.fsu.edu
566019Shines@cs.fsu.eduusing namespace std;
576019Shines@cs.fsu.eduusing namespace ArmISA;
586019Shines@cs.fsu.edu
596019Shines@cs.fsu.eduAddr
606019Shines@cs.fsu.eduArmISA::vtophys(Addr vaddr)
616019Shines@cs.fsu.edu{
627694SAli.Saidi@ARM.com    fatal("VTOPHYS: Can't convert vaddr to paddr on ARM without a thread context");
636019Shines@cs.fsu.edu}
646019Shines@cs.fsu.edu
656019Shines@cs.fsu.eduAddr
666019Shines@cs.fsu.eduArmISA::vtophys(ThreadContext *tc, Addr addr)
676019Shines@cs.fsu.edu{
687694SAli.Saidi@ARM.com    SCTLR sctlr = tc->readMiscReg(MISCREG_SCTLR);
697694SAli.Saidi@ARM.com    if (!sctlr.m) {
707694SAli.Saidi@ARM.com        // Translation is currently disabled PA == VA
717694SAli.Saidi@ARM.com        return addr;
727694SAli.Saidi@ARM.com    }
737694SAli.Saidi@ARM.com    bool success;
747694SAli.Saidi@ARM.com    Addr pa;
757694SAli.Saidi@ARM.com    ArmISA::TLB *tlb;
767694SAli.Saidi@ARM.com
777694SAli.Saidi@ARM.com    // Check the TLBs far a translation
787694SAli.Saidi@ARM.com    // It's possible that there is a validy translation in the tlb
797694SAli.Saidi@ARM.com    // that is no loger valid in the page table in memory
807694SAli.Saidi@ARM.com    // so we need to check here first
817694SAli.Saidi@ARM.com    tlb = static_cast<ArmISA::TLB*>(tc->getDTBPtr());
827694SAli.Saidi@ARM.com    success = tlb->translateFunctional(tc, addr, pa);
837694SAli.Saidi@ARM.com    if (success)
847694SAli.Saidi@ARM.com        return pa;
857694SAli.Saidi@ARM.com
867694SAli.Saidi@ARM.com    tlb = static_cast<ArmISA::TLB*>(tc->getITBPtr());
877694SAli.Saidi@ARM.com    success = tlb->translateFunctional(tc, addr, pa);
887694SAli.Saidi@ARM.com    if (success)
897694SAli.Saidi@ARM.com        return pa;
907694SAli.Saidi@ARM.com
917694SAli.Saidi@ARM.com    // We've failed everything, so we need to do a
927694SAli.Saidi@ARM.com    // hardware tlb walk without messing with any
937694SAli.Saidi@ARM.com    // state
947694SAli.Saidi@ARM.com
957694SAli.Saidi@ARM.com    uint32_t N = tc->readMiscReg(MISCREG_TTBCR);
967694SAli.Saidi@ARM.com    Addr ttbr;
977694SAli.Saidi@ARM.com    if (N == 0 || !mbits(addr, 31, 32-N)) {
987694SAli.Saidi@ARM.com        ttbr = tc->readMiscReg(MISCREG_TTBR0);
997694SAli.Saidi@ARM.com    } else {
1007694SAli.Saidi@ARM.com        ttbr = tc->readMiscReg(MISCREG_TTBR1);
1017694SAli.Saidi@ARM.com        N = 0;
1027694SAli.Saidi@ARM.com    }
1037694SAli.Saidi@ARM.com
1047694SAli.Saidi@ARM.com    FunctionalPort *port = tc->getPhysPort();
1057694SAli.Saidi@ARM.com    Addr l1desc_addr = mbits(ttbr, 31, 14-N) | (bits(addr,31-N,20) << 2);
1067694SAli.Saidi@ARM.com
1077694SAli.Saidi@ARM.com    TableWalker::L1Descriptor l1desc;
1087694SAli.Saidi@ARM.com    l1desc.data = port->read<uint32_t>(l1desc_addr);
1097694SAli.Saidi@ARM.com    if (l1desc.type() == TableWalker::L1Descriptor::Ignore ||
1107694SAli.Saidi@ARM.com            l1desc.type() == TableWalker::L1Descriptor::Reserved) {
1117694SAli.Saidi@ARM.com        warn("Unable to translate virtual address: %#x\n", addr);
1127694SAli.Saidi@ARM.com        return -1;
1137694SAli.Saidi@ARM.com    }
1147694SAli.Saidi@ARM.com    if (l1desc.type() == TableWalker::L1Descriptor::Section)
1157694SAli.Saidi@ARM.com        return l1desc.paddr(addr);
1167694SAli.Saidi@ARM.com
1177694SAli.Saidi@ARM.com    // Didn't find it at the first level, try againt
1187694SAli.Saidi@ARM.com    Addr l2desc_addr = l1desc.l2Addr() | (bits(addr, 19, 12) << 2);
1197694SAli.Saidi@ARM.com    TableWalker::L2Descriptor l2desc;
1207694SAli.Saidi@ARM.com    l2desc.data = port->read<uint32_t>(l2desc_addr);
1217694SAli.Saidi@ARM.com
1227694SAli.Saidi@ARM.com    if (l2desc.invalid()) {
1237694SAli.Saidi@ARM.com        warn("Unable to translate virtual address: %#x\n", addr);
1247694SAli.Saidi@ARM.com        return -1;
1257694SAli.Saidi@ARM.com    }
1267694SAli.Saidi@ARM.com
1277694SAli.Saidi@ARM.com    return l2desc.paddr(addr);
1286019Shines@cs.fsu.edu}
1296019Shines@cs.fsu.edu
1307694SAli.Saidi@ARM.combool
1317694SAli.Saidi@ARM.comArmISA::virtvalid(ThreadContext *tc, Addr vaddr)
1327694SAli.Saidi@ARM.com{
1337694SAli.Saidi@ARM.com    if (vtophys(tc, vaddr) != -1)
1347694SAli.Saidi@ARM.com        return true;
1357694SAli.Saidi@ARM.com    return false;
1367694SAli.Saidi@ARM.com}
1377694SAli.Saidi@ARM.com
1387694SAli.Saidi@ARM.com
139