vtophys.cc revision 8852
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 *          Ali Saidi
31 */
32
33#include <string>
34
35#include "arch/alpha/ev5.hh"
36#include "arch/alpha/vtophys.hh"
37#include "base/chunk_generator.hh"
38#include "base/trace.hh"
39#include "cpu/thread_context.hh"
40#include "debug/VtoPhys.hh"
41#include "mem/port_proxy.hh"
42
43using namespace std;
44
45namespace AlphaISA {
46
47PageTableEntry
48kernel_pte_lookup(PortProxy &mem, Addr ptbr, VAddr vaddr)
49{
50    Addr level1_pte = ptbr + vaddr.level1();
51    PageTableEntry level1 = mem.read<uint64_t>(level1_pte);
52    if (!level1.valid()) {
53        DPRINTF(VtoPhys, "level 1 PTE not valid, va = %#\n", vaddr);
54        return 0;
55    }
56
57    Addr level2_pte = level1.paddr() + vaddr.level2();
58    PageTableEntry level2 = mem.read<uint64_t>(level2_pte);
59    if (!level2.valid()) {
60        DPRINTF(VtoPhys, "level 2 PTE not valid, va = %#x\n", vaddr);
61        return 0;
62    }
63
64    Addr level3_pte = level2.paddr() + vaddr.level3();
65    PageTableEntry level3 = mem.read<uint64_t>(level3_pte);
66    if (!level3.valid()) {
67        DPRINTF(VtoPhys, "level 3 PTE not valid, va = %#x\n", vaddr);
68        return 0;
69    }
70    return level3;
71}
72
73Addr
74vtophys(Addr vaddr)
75{
76    Addr paddr = 0;
77    if (IsUSeg(vaddr))
78        DPRINTF(VtoPhys, "vtophys: invalid vaddr %#x", vaddr);
79    else if (IsK0Seg(vaddr))
80        paddr = K0Seg2Phys(vaddr);
81    else
82        panic("vtophys: ptbr is not set on virtual lookup");
83
84    DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr);
85
86    return paddr;
87}
88
89Addr
90vtophys(ThreadContext *tc, Addr addr)
91{
92    VAddr vaddr = addr;
93    Addr ptbr = tc->readMiscRegNoEffect(IPR_PALtemp20);
94    Addr paddr = 0;
95    //@todo Andrew couldn't remember why he commented some of this code
96    //so I put it back in. Perhaps something to do with gdb debugging?
97    if (PcPAL(vaddr) && (vaddr < PalMax)) {
98        paddr = vaddr & ~ULL(1);
99    } else {
100        if (IsK0Seg(vaddr)) {
101            paddr = K0Seg2Phys(vaddr);
102        } else if (!ptbr) {
103            paddr = vaddr;
104        } else {
105            PageTableEntry pte =
106                kernel_pte_lookup(tc->getPhysProxy(), ptbr, vaddr);
107            if (pte.valid())
108                paddr = pte.paddr() | vaddr.offset();
109        }
110    }
111
112
113    DPRINTF(VtoPhys, "vtophys(%#x) -> %#x\n", vaddr, paddr);
114
115    return paddr;
116}
117
118} // namespace AlphaISA
119