tlb.hh revision 4070:74449a198a44
1/*
2 * Copyright (c) 2006 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: Ali Saidi
29 */
30
31#ifndef __ARCH_SPARC_TLB_HH__
32#define __ARCH_SPARC_TLB_HH__
33
34#include "arch/sparc/asi.hh"
35#include "arch/sparc/tlb_map.hh"
36#include "base/misc.hh"
37#include "mem/request.hh"
38#include "sim/faults.hh"
39#include "sim/sim_object.hh"
40
41class ThreadContext;
42class Packet;
43
44namespace SparcISA
45{
46
47class TLB : public SimObject
48{
49  protected:
50    TlbMap lookupTable;;
51    typedef TlbMap::iterator MapIter;
52
53    TlbEntry *tlb;
54
55    int size;
56    int usedEntries;
57    int lastReplaced;
58
59    uint64_t cacheState;
60    bool cacheValid;
61
62    std::list<TlbEntry*> freeList;
63
64    enum FaultTypes {
65        OtherFault = 0,
66        PrivViolation = 0x1,
67        SideEffect = 0x2,
68        AtomicToIo = 0x4,
69        IllegalAsi = 0x8,
70        LoadFromNfo = 0x10,
71        VaOutOfRange = 0x20,
72        VaOutOfRangeJmp = 0x40
73    };
74
75    enum ContextType {
76        Primary = 0,
77        Secondary = 1,
78        Nucleus = 2
79    };
80
81    enum TsbPageSize {
82        Ps0,
83        Ps1
84    };
85  public:
86    /** lookup an entry in the TLB based on the partition id, and real bit if
87     * real is true or the partition id, and context id if real is false.
88     * @param va the virtual address not shifted (e.g. bottom 13 bits are 0)
89     * @param paritition_id partition this entry is for
90     * @param real is this a real->phys or virt->phys translation
91     * @param context_id if this is virt->phys what context
92     * @param update_used should ew update the used bits in the entries on not
93     * useful if we are trying to do a va->pa without mucking with any state for
94     * a debug read for example.
95     * @return A pointer to a tlb entry
96     */
97    TlbEntry *lookup(Addr va, int partition_id, bool real, int context_id = 0,
98            bool update_used = true);
99  protected:
100    /** Insert a PTE into the TLB. */
101    void insert(Addr vpn, int partition_id, int context_id, bool real,
102            const PageTableEntry& PTE, int entry = -1);
103
104    /** Given an entry id, read that tlb entries' tag. */
105    uint64_t TagRead(int entry);
106
107    /** Remove all entries from the TLB */
108    void invalidateAll();
109
110    /** Remove all non-locked entries from the tlb that match partition id. */
111    void demapAll(int partition_id);
112
113    /** Remove all entries that match a given context/partition id. */
114    void demapContext(int partition_id, int context_id);
115
116    /** Remve all entries that match a certain partition id, (contextid), and
117     * va). */
118    void demapPage(Addr va, int partition_id, bool real, int context_id);
119
120    /** Checks if the virtual address provided is a valid one. */
121    bool validVirtualAddress(Addr va, bool am);
122
123    void writeSfsr(ThreadContext *tc, int reg, bool write, ContextType ct,
124            bool se, FaultTypes ft, int asi);
125
126    void clearUsedBits();
127
128
129    void writeTagAccess(ThreadContext *tc, int reg, Addr va, int context);
130
131  public:
132    TLB(const std::string &name, int size);
133
134    void dumpAll();
135
136    // Checkpointing
137    virtual void serialize(std::ostream &os);
138    virtual void unserialize(Checkpoint *cp, const std::string &section);
139
140    /** Give an entry id, read that tlb entries' tte */
141    uint64_t TteRead(int entry);
142
143};
144
145class ITB : public TLB
146{
147  public:
148    ITB(const std::string &name, int size) : TLB(name, size)
149    {
150        cacheEntry = NULL;
151    }
152
153    Fault translate(RequestPtr &req, ThreadContext *tc);
154  private:
155    void writeSfsr(ThreadContext *tc, bool write, ContextType ct,
156            bool se, FaultTypes ft, int asi);
157    void writeTagAccess(ThreadContext *tc, Addr va, int context);
158    TlbEntry *cacheEntry;
159    friend class DTB;
160};
161
162class DTB : public TLB
163{
164  public:
165    DTB(const std::string &name, int size) : TLB(name, size)
166    {
167        cacheEntry[0] = NULL;
168        cacheEntry[1] = NULL;
169    }
170
171    Fault translate(RequestPtr &req, ThreadContext *tc, bool write);
172    Tick doMmuRegRead(ThreadContext *tc, Packet *pkt);
173    Tick doMmuRegWrite(ThreadContext *tc, Packet *pkt);
174    void GetTsbPtr(ThreadContext *tc, Addr addr, int ctx, Addr *ptrs);
175
176  private:
177    void writeSfr(ThreadContext *tc, Addr a, bool write, ContextType ct,
178            bool se, FaultTypes ft, int asi);
179    void writeTagAccess(ThreadContext *tc, Addr va, int context);
180
181    uint64_t MakeTsbPtr(TsbPageSize ps, uint64_t tag_access, uint64_t c0_tsb,
182        uint64_t c0_config, uint64_t cX_tsb, uint64_t cX_config);
183
184
185    TlbEntry *cacheEntry[2];
186    ASI cacheAsi[2];
187};
188
189}
190
191#endif // __ARCH_SPARC_TLB_HH__
192