request.hh revision 6133:5af0a83d9021
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: Ron Dreslinski
29 *          Steve Reinhardt
30 *          Ali Saidi
31 */
32
33/**
34 * @file
35 * Declaration of a request, the overall memory request consisting of
36 the parts of the request that are persistent throughout the transaction.
37 */
38
39#ifndef __MEM_REQUEST_HH__
40#define __MEM_REQUEST_HH__
41
42#include <cassert>
43
44#include "base/fast_alloc.hh"
45#include "base/flags.hh"
46#include "base/misc.hh"
47#include "sim/host.hh"
48#include "sim/core.hh"
49
50class Request;
51
52typedef Request* RequestPtr;
53
54class Request : public FastAlloc
55{
56  public:
57    typedef uint32_t FlagsType;
58    typedef ::Flags<FlagsType> Flags;
59
60    /** ASI information for this request if it exists. */
61    static const FlagsType ASI_BITS                    = 0x000000FF;
62    /** The request was an instruction fetch. */
63    static const FlagsType INST_FETCH                  = 0x00000100;
64    /** The virtual address is also the physical address. */
65    static const FlagsType PHYSICAL                    = 0x00000200;
66    /** The request is an ALPHA VPTE pal access (hw_ld). */
67    static const FlagsType VPTE                        = 0x00000400;
68    /** Use the alternate mode bits in ALPHA. */
69    static const FlagsType ALTMODE                     = 0x00000800;
70    /** The request is to an uncacheable address. */
71    static const FlagsType UNCACHEABLE                 = 0x00001000;
72    /** This request is to a memory mapped register. */
73    static const FlagsType MMAPED_IPR                  = 0x00002000;
74
75    /** The request should not cause a page fault. */
76    static const FlagsType NO_FAULT                    = 0x00010000;
77    /** The request should ignore unaligned access faults */
78    static const FlagsType NO_ALIGN_FAULT              = 0x00020000;
79    /** The request should ignore unaligned access faults */
80    static const FlagsType NO_HALF_WORD_ALIGN_FAULT    = 0x00040000;
81    /** The request should not cause a memory access. */
82    static const FlagsType NO_ACCESS                   = 0x00080000;
83    /** This request will lock or unlock the accessed memory. When used with
84     * a load, the access locks the particular chunk of memory. When used
85     * with a store, it unlocks. The rule is that locked accesses have to be
86     * made up of a locked load, some operation on the data, and then a locked
87     * store.
88     */
89    static const FlagsType LOCKED                      = 0x00100000;
90    /** The request is a Load locked/store conditional. */
91    static const FlagsType LLSC                        = 0x00200000;
92    /** This request is for a memory swap. */
93    static const FlagsType MEM_SWAP                    = 0x00400000;
94    static const FlagsType MEM_SWAP_COND               = 0x00800000;
95
96    /** The request is a prefetch. */
97    static const FlagsType PREFETCH                    = 0x01000000;
98    /** The request should be prefetched into the exclusive state. */
99    static const FlagsType PF_EXCLUSIVE                = 0x02000000;
100    /** The request should be marked as LRU. */
101    static const FlagsType EVICT_NEXT                  = 0x04000000;
102
103    /** These flags are *not* cleared when a Request object is reused
104       (assigned a new address). */
105    static const FlagsType STICKY_FLAGS = INST_FETCH;
106
107  private:
108    typedef uint8_t PrivateFlagsType;
109    typedef ::Flags<PrivateFlagsType> PrivateFlags;
110
111    /** Whether or not the size is valid. */
112    static const PrivateFlagsType VALID_SIZE           = 0x00000001;
113    /** Whether or not paddr is valid (has been written yet). */
114    static const PrivateFlagsType VALID_PADDR          = 0x00000002;
115    /** Whether or not the vaddr & asid are valid. */
116    static const PrivateFlagsType VALID_VADDR          = 0x00000004;
117    /** Whether or not the pc is valid. */
118    static const PrivateFlagsType VALID_PC             = 0x00000010;
119    /** Whether or not the context ID is valid. */
120    static const PrivateFlagsType VALID_CONTEXT_ID     = 0x00000020;
121    static const PrivateFlagsType VALID_THREAD_ID      = 0x00000040;
122    /** Whether or not the sc result is valid. */
123    static const PrivateFlagsType VALID_EXTRA_DATA     = 0x00000080;
124
125    /** These flags are *not* cleared when a Request object is reused
126       (assigned a new address). */
127    static const PrivateFlagsType STICKY_PRIVATE_FLAGS =
128        VALID_CONTEXT_ID | VALID_THREAD_ID;
129
130  private:
131    /**
132     * The physical address of the request. Valid only if validPaddr
133     * is set.
134     */
135    Addr paddr;
136
137    /**
138     * The size of the request. This field must be set when vaddr or
139     * paddr is written via setVirt() or setPhys(), so it is always
140     * valid as long as one of the address fields is valid.
141     */
142    int size;
143
144    /** Flag structure for the request. */
145    Flags flags;
146
147    /** Private flags for field validity checking. */
148    PrivateFlags privateFlags;
149
150    /**
151     * The time this request was started. Used to calculate
152     * latencies. This field is set to curTick any time paddr or vaddr
153     * is written.
154     */
155    Tick time;
156
157    /** The address space ID. */
158    int asid;
159
160    /** The virtual address of the request. */
161    Addr vaddr;
162
163    /**
164     * Extra data for the request, such as the return value of
165     * store conditional or the compare value for a CAS. */
166    uint64_t extraData;
167
168    /** The context ID (for statistics, typically). */
169    int _contextId;
170    /** The thread ID (id within this CPU) */
171    int _threadId;
172
173    /** program counter of initiating access; for tracing/debugging */
174    Addr pc;
175
176  public:
177    /** Minimal constructor.  No fields are initialized. */
178    Request()
179    {}
180
181    /**
182     * Constructor for physical (e.g. device) requests.  Initializes
183     * just physical address, size, flags, and timestamp (to curTick).
184     * These fields are adequate to perform a request.
185     */
186    Request(Addr paddr, int size, Flags flags)
187    {
188        setPhys(paddr, size, flags);
189    }
190
191    Request(int asid, Addr vaddr, int size, Flags flags, Addr pc,
192            int cid, int tid)
193    {
194        setVirt(asid, vaddr, size, flags, pc);
195        setThreadContext(cid, tid);
196    }
197
198    ~Request() {}  // for FastAlloc
199
200    /**
201     * Set up CPU and thread numbers.
202     */
203    void
204    setThreadContext(int context_id, int thread_id)
205    {
206        _contextId = context_id;
207        _threadId = thread_id;
208        privateFlags.set(VALID_CONTEXT_ID|VALID_THREAD_ID);
209    }
210
211    /**
212     * Set up a physical (e.g. device) request in a previously
213     * allocated Request object.
214     */
215    void
216    setPhys(Addr _paddr, int _size, Flags _flags)
217    {
218        assert(_size >= 0);
219        paddr = _paddr;
220        size = _size;
221        time = curTick;
222
223        flags.clear(~STICKY_FLAGS);
224        flags.set(_flags);
225        privateFlags.clear(~STICKY_PRIVATE_FLAGS);
226        privateFlags.set(VALID_PADDR|VALID_SIZE);
227    }
228
229    /**
230     * Set up a virtual (e.g., CPU) request in a previously
231     * allocated Request object.
232     */
233    void
234    setVirt(int _asid, Addr _vaddr, int _size, Flags _flags, Addr _pc)
235    {
236        assert(_size >= 0);
237        asid = _asid;
238        vaddr = _vaddr;
239        size = _size;
240        flags = _flags;
241        pc = _pc;
242        time = curTick;
243
244        flags.clear(~STICKY_FLAGS);
245        flags.set(_flags);
246        privateFlags.clear(~STICKY_PRIVATE_FLAGS);
247        privateFlags.set(VALID_VADDR|VALID_SIZE|VALID_PC);
248    }
249
250    /**
251     * Set just the physical address.  This should only be used to
252     * record the result of a translation, and thus the vaddr must be
253     * valid before this method is called.  Otherwise, use setPhys()
254     * to guarantee that the size and flags are also set.
255     */
256    void
257    setPaddr(Addr _paddr)
258    {
259        assert(privateFlags.isSet(VALID_VADDR));
260        paddr = _paddr;
261        privateFlags.set(VALID_PADDR);
262    }
263
264    /**
265     * Generate two requests as if this request had been split into two
266     * pieces. The original request can't have been translated already.
267     */
268    void splitOnVaddr(Addr split_addr, RequestPtr &req1, RequestPtr &req2)
269    {
270        assert(privateFlags.isSet(VALID_VADDR));
271        assert(privateFlags.noneSet(VALID_PADDR));
272        assert(split_addr > vaddr && split_addr < vaddr + size);
273        req1 = new Request;
274        *req1 = *this;
275        req2 = new Request;
276        *req2 = *this;
277        req1->size = split_addr - vaddr;
278        req2->vaddr = split_addr;
279        req2->size = size - req1->size;
280    }
281
282    /**
283     * Accessor for paddr.
284     */
285    bool
286    hasPaddr()
287    {
288        return privateFlags.isSet(VALID_PADDR);
289    }
290
291    Addr
292    getPaddr()
293    {
294        assert(privateFlags.isSet(VALID_PADDR));
295        return paddr;
296    }
297
298    /**
299     *  Accessor for size.
300     */
301    bool
302    hasSize()
303    {
304        return privateFlags.isSet(VALID_SIZE);
305    }
306
307    int
308    getSize()
309    {
310        assert(privateFlags.isSet(VALID_SIZE));
311        return size;
312    }
313
314    /** Accessor for time. */
315    Tick
316    getTime()
317    {
318        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
319        return time;
320    }
321
322    /** Accessor for flags. */
323    Flags
324    getFlags()
325    {
326        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
327        return flags;
328    }
329
330    void
331    setFlags(Flags _flags)
332    {
333        assert(privateFlags.isSet(VALID_PADDR|VALID_VADDR));
334        flags.set(_flags);
335    }
336
337    /** Accessor function for vaddr.*/
338    Addr
339    getVaddr()
340    {
341        assert(privateFlags.isSet(VALID_VADDR));
342        return vaddr;
343    }
344
345    /** Accessor function for asid.*/
346    int
347    getAsid()
348    {
349        assert(privateFlags.isSet(VALID_VADDR));
350        return asid;
351    }
352
353    /** Accessor function for asi.*/
354    uint8_t
355    getAsi()
356    {
357        assert(privateFlags.isSet(VALID_VADDR));
358        return flags & ASI_BITS;
359    }
360
361    /** Accessor function for MMAPED_IPR flag. */
362    bool
363    isMmapedIpr()
364    {
365        assert(privateFlags.isSet(VALID_PADDR));
366        return flags.isSet(MMAPED_IPR);
367    }
368
369    void
370    setMmapedIpr(bool r)
371    {
372        assert(VALID_VADDR);
373        flags.set(MMAPED_IPR);
374    }
375
376    /** Accessor function to check if sc result is valid. */
377    bool
378    extraDataValid()
379    {
380        return privateFlags.isSet(VALID_EXTRA_DATA);
381    }
382
383    /** Accessor function for store conditional return value.*/
384    uint64_t
385    getExtraData() const
386    {
387        assert(privateFlags.isSet(VALID_EXTRA_DATA));
388        return extraData;
389    }
390
391    /** Accessor function for store conditional return value.*/
392    void
393    setExtraData(uint64_t _extraData)
394    {
395        extraData = _extraData;
396        privateFlags.set(VALID_EXTRA_DATA);
397    }
398
399    bool
400    hasContextId() const
401    {
402        return privateFlags.isSet(VALID_CONTEXT_ID);
403    }
404
405    /** Accessor function for context ID.*/
406    int
407    contextId() const
408    {
409        assert(privateFlags.isSet(VALID_CONTEXT_ID));
410        return _contextId;
411    }
412
413    /** Accessor function for thread ID. */
414    int
415    threadId() const
416    {
417        assert(privateFlags.isSet(VALID_THREAD_ID));
418        return _threadId;
419    }
420
421    bool
422    hasPC() const
423    {
424        return privateFlags.isSet(VALID_PC);
425    }
426
427    /** Accessor function for pc.*/
428    Addr
429    getPC() const
430    {
431        assert(privateFlags.isSet(VALID_PC));
432        return pc;
433    }
434
435    /** Accessor Function to Check Cacheability. */
436    bool isUncacheable() const { return flags.isSet(UNCACHEABLE); }
437    bool isInstFetch() const { return flags.isSet(INST_FETCH); }
438    bool isPrefetch() const { return flags.isSet(PREFETCH); }
439    bool isLLSC() const { return flags.isSet(LLSC); }
440    bool isLocked() const { return flags.isSet(LOCKED); }
441    bool isSwap() const { return flags.isSet(MEM_SWAP|MEM_SWAP_COND); }
442    bool isCondSwap() const { return flags.isSet(MEM_SWAP_COND); }
443
444    bool
445    isMisaligned() const
446    {
447        if (flags.isSet(NO_ALIGN_FAULT))
448            return false;
449
450        if ((vaddr & 0x1))
451            return true;
452
453        if (flags.isSet(NO_HALF_WORD_ALIGN_FAULT))
454            return false;
455
456        if ((vaddr & 0x2))
457            return true;
458
459        return false;
460    }
461};
462
463#endif // __MEM_REQUEST_HH__
464