12650Ssaidi@eecs.umich.edu/*
22650Ssaidi@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
32650Ssaidi@eecs.umich.edu * All rights reserved.
42650Ssaidi@eecs.umich.edu *
52650Ssaidi@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
62650Ssaidi@eecs.umich.edu * modification, are permitted provided that the following conditions are
72650Ssaidi@eecs.umich.edu * met: redistributions of source code must retain the above copyright
82650Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
92650Ssaidi@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
102650Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
112650Ssaidi@eecs.umich.edu * documentation and/or other materials provided with the distribution;
122650Ssaidi@eecs.umich.edu * neither the name of the copyright holders nor the names of its
132650Ssaidi@eecs.umich.edu * contributors may be used to endorse or promote products derived from
142650Ssaidi@eecs.umich.edu * this software without specific prior written permission.
152650Ssaidi@eecs.umich.edu *
162650Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172650Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182650Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192650Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202650Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212650Ssaidi@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222650Ssaidi@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232650Ssaidi@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242650Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252650Ssaidi@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262650Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
284070Ssaidi@eecs.umich.edu * Authors: Ali Saidi
292650Ssaidi@eecs.umich.edu */
302650Ssaidi@eecs.umich.edu
3111793Sbrandon.potter@amd.com#include "arch/sparc/vtophys.hh"
3211793Sbrandon.potter@amd.com
332650Ssaidi@eecs.umich.edu#include <string>
342650Ssaidi@eecs.umich.edu
358229Snate@binkert.org#include "arch/sparc/tlb.hh"
368229Snate@binkert.org#include "base/chunk_generator.hh"
374070Ssaidi@eecs.umich.edu#include "base/compiler.hh"
382650Ssaidi@eecs.umich.edu#include "base/trace.hh"
392680Sktlim@umich.edu#include "cpu/thread_context.hh"
408232Snate@binkert.org#include "debug/VtoPhys.hh"
418706Sandreas.hansson@arm.com#include "mem/port_proxy.hh"
422650Ssaidi@eecs.umich.edu
432650Ssaidi@eecs.umich.eduusing namespace std;
442650Ssaidi@eecs.umich.edu
455560Snate@binkert.orgnamespace SparcISA {
465560Snate@binkert.org
475560Snate@binkert.orgAddr
485560Snate@binkert.orgvtophys(Addr vaddr)
492650Ssaidi@eecs.umich.edu{
505560Snate@binkert.org    // In SPARC it's almost always impossible to turn a VA->PA w/o a
515560Snate@binkert.org    // context The only times we can kinda do it are if we have a
525560Snate@binkert.org    // SegKPM mapping and can find the real address in the tlb or we
535560Snate@binkert.org    // have a physical adddress already (beacuse we are looking at the
545560Snate@binkert.org    // hypervisor) Either case is rare, so we'll just panic.
554070Ssaidi@eecs.umich.edu
565560Snate@binkert.org    panic("vtophys() without context on SPARC largly worthless\n");
575560Snate@binkert.org    M5_DUMMY_RETURN;
585560Snate@binkert.org}
595560Snate@binkert.org
605560Snate@binkert.orgAddr
615560Snate@binkert.orgvtophys(ThreadContext *tc, Addr addr)
625560Snate@binkert.org{
635560Snate@binkert.org    // Here we have many options and are really implementing something like
645560Snate@binkert.org    // a fill handler to find the address since there isn't a multilevel
655560Snate@binkert.org    // table for us to walk around.
665560Snate@binkert.org    //
675560Snate@binkert.org    // 1. We are currently hyperpriv, return the address unmodified
685560Snate@binkert.org    // 2. The mmu is off return(ra->pa)
695560Snate@binkert.org    // 3. We are currently priv, use ctx0* tsbs to find the page
705560Snate@binkert.org    // 4. We are not priv, use ctxN0* tsbs to find the page
715560Snate@binkert.org    // For all accesses we check the tlbs first since it's possible that
725560Snate@binkert.org    // long standing pages (e.g. locked kernel mappings) won't be in the tsb
735560Snate@binkert.org    uint64_t tlbdata = tc->readMiscRegNoEffect(MISCREG_TLB_DATA);
745560Snate@binkert.org
755560Snate@binkert.org    bool hpriv = bits(tlbdata,0,0);
767741Sgblack@eecs.umich.edu    // bool priv = bits(tlbdata,2,2);
775560Snate@binkert.org    bool addr_mask = bits(tlbdata,3,3);
785560Snate@binkert.org    bool data_real = !bits(tlbdata,5,5);
795560Snate@binkert.org    bool inst_real = !bits(tlbdata,4,4);
805560Snate@binkert.org    bool ctx_zero  = bits(tlbdata,18,16) > 0;
815560Snate@binkert.org    int part_id = bits(tlbdata,15,8);
825560Snate@binkert.org    int pri_context = bits(tlbdata,47,32);
837741Sgblack@eecs.umich.edu    // int sec_context = bits(tlbdata,63,48);
845560Snate@binkert.org
858852Sandreas.hansson@arm.com    PortProxy &mem = tc->getPhysProxy();
8612406Sgabeblack@google.com    TLB* itb = dynamic_cast<TLB *>(tc->getITBPtr());
8712406Sgabeblack@google.com    TLB* dtb = dynamic_cast<TLB *>(tc->getDTBPtr());
885560Snate@binkert.org    TlbEntry* tbe;
895560Snate@binkert.org    PageTableEntry pte;
905560Snate@binkert.org    Addr tsbs[4];
915560Snate@binkert.org    Addr va_tag;
925560Snate@binkert.org    TteTag ttetag;
935560Snate@binkert.org
945560Snate@binkert.org    if (hpriv)
955560Snate@binkert.org        return addr;
965560Snate@binkert.org
975560Snate@binkert.org    if (addr_mask)
985560Snate@binkert.org        addr = addr & VAddrAMask;
995560Snate@binkert.org
1005560Snate@binkert.org    tbe = dtb->lookup(addr, part_id, data_real, ctx_zero ? 0 : pri_context ,
1015560Snate@binkert.org                      false);
1025560Snate@binkert.org    if (tbe)
1035560Snate@binkert.org        goto foundtbe;
1045560Snate@binkert.org
1055560Snate@binkert.org    tbe = itb->lookup(addr, part_id, inst_real, ctx_zero ? 0 : pri_context,
1065560Snate@binkert.org                      false);
1075560Snate@binkert.org    if (tbe)
1085560Snate@binkert.org        goto foundtbe;
1095560Snate@binkert.org
1105560Snate@binkert.org    // We didn't find it in the tlbs, so lets look at the TSBs
1115560Snate@binkert.org    dtb->GetTsbPtr(tc, addr, ctx_zero ? 0 : pri_context, tsbs);
1125560Snate@binkert.org    va_tag = bits(addr, 63, 22);
1135560Snate@binkert.org    for (int x = 0; x < 4; x++) {
1148852Sandreas.hansson@arm.com        ttetag = betoh(mem.read<uint64_t>(tsbs[x]));
1155560Snate@binkert.org        if (ttetag.valid() && ttetag.va() == va_tag) {
1168852Sandreas.hansson@arm.com            uint64_t entry = mem.read<uint64_t>(tsbs[x]) + sizeof(uint64_t);
1175560Snate@binkert.org            // I think it's sun4v at least!
1185560Snate@binkert.org            pte.populate(betoh(entry), PageTableEntry::sun4v);
1195560Snate@binkert.org            DPRINTF(VtoPhys, "Virtual(%#x)->Physical(%#x) found in TTE\n",
1205560Snate@binkert.org                    addr, pte.translate(addr));
1215560Snate@binkert.org            goto foundpte;
1225560Snate@binkert.org        }
1232650Ssaidi@eecs.umich.edu    }
1245560Snate@binkert.org    panic("couldn't translate %#x\n", addr);
1252650Ssaidi@eecs.umich.edu
1265560Snate@binkert.org  foundtbe:
1275560Snate@binkert.org    pte = tbe->pte;
1285560Snate@binkert.org    DPRINTF(VtoPhys, "Virtual(%#x)->Physical(%#x) found in TLB\n", addr,
1295560Snate@binkert.org            pte.translate(addr));
1305560Snate@binkert.org  foundpte:
1315560Snate@binkert.org    return pte.translate(addr);
1325560Snate@binkert.org}
1332650Ssaidi@eecs.umich.edu
1347811Ssteve.reinhardt@amd.com} // namespace SparcISA
135