packet.cc revision 5319:13cb690ba6d6
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 *          Steve Reinhardt
30 */
31
32/**
33 * @file
34 * Definition of the Packet Class, a packet is a transaction occuring
35 * between a single level of the memory heirarchy (ie L1->L2).
36 */
37
38#include <iostream>
39#include <cstring>
40#include "base/cprintf.hh"
41#include "base/misc.hh"
42#include "base/trace.hh"
43#include "mem/packet.hh"
44
45// The one downside to bitsets is that static initializers can get ugly.
46#define SET1(a1)                     (1 << (a1))
47#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
48#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
49#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
50#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
51#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
52
53const MemCmd::CommandInfo
54MemCmd::commandInfo[] =
55{
56    /* InvalidCmd */
57    { 0, InvalidCmd, "InvalidCmd" },
58    /* ReadReq */
59    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
60    /* ReadResp */
61    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
62    /* ReadRespWithInvalidate */
63    { SET4(IsRead, IsResponse, HasData, IsInvalidate),
64            InvalidCmd, "ReadRespWithInvalidate" },
65    /* WriteReq */
66    { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
67            WriteResp, "WriteReq" },
68    /* WriteResp */
69    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
70    /* Writeback */
71    { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
72            InvalidCmd, "Writeback" },
73    /* SoftPFReq */
74    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
75            SoftPFResp, "SoftPFReq" },
76    /* HardPFReq */
77    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
78            HardPFResp, "HardPFReq" },
79    /* SoftPFResp */
80    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
81            InvalidCmd, "SoftPFResp" },
82    /* HardPFResp */
83    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
84            InvalidCmd, "HardPFResp" },
85    /* WriteInvalidateReq */
86    { SET6(IsWrite, NeedsExclusive, IsInvalidate,
87           IsRequest, HasData, NeedsResponse),
88            WriteInvalidateResp, "WriteInvalidateReq" },
89    /* WriteInvalidateResp */
90    { SET3(IsWrite, NeedsExclusive, IsResponse),
91            InvalidCmd, "WriteInvalidateResp" },
92    /* UpgradeReq */
93    { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse),
94            UpgradeResp, "UpgradeReq" },
95    /* UpgradeResp */
96    { SET2(NeedsExclusive, IsResponse),
97            InvalidCmd, "UpgradeResp" },
98    /* ReadExReq */
99    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
100            ReadExResp, "ReadExReq" },
101    /* ReadExResp */
102    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
103            InvalidCmd, "ReadExResp" },
104    /* LoadLockedReq */
105    { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
106            LoadLockedResp, "LoadLockedReq" },
107    /* LoadLockedResp */
108    { SET4(IsRead, IsLocked, IsResponse, HasData),
109            InvalidCmd, "LoadLockedResp" },
110    /* StoreCondReq */
111    { SET6(IsWrite, NeedsExclusive, IsLocked,
112           IsRequest, NeedsResponse, HasData),
113            StoreCondResp, "StoreCondReq" },
114    /* StoreCondResp */
115    { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
116            InvalidCmd, "StoreCondResp" },
117    /* SwapReq -- for Swap ldstub type operations */
118    { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
119        SwapResp, "SwapReq" },
120    /* SwapResp -- for Swap ldstub type operations */
121    { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
122            InvalidCmd, "SwapResp" },
123    /* NetworkNackError  -- nacked at network layer (not by protocol) */
124    { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
125    /* InvalidDestError  -- packet dest field invalid */
126    { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
127    /* BadAddressError   -- memory address invalid */
128    { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
129    /* PrintReq */
130    { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
131};
132
133
134/** delete the data pointed to in the data pointer. Ok to call to matter how
135 * data was allocted. */
136void
137Packet::deleteData()
138{
139    assert(staticData || dynamicData);
140    if (staticData)
141        return;
142
143    if (arrayData)
144        delete [] data;
145    else
146        delete data;
147}
148
149/** If there isn't data in the packet, allocate some. */
150void
151Packet::allocate()
152{
153    if (data)
154        return;
155    assert(!staticData);
156    dynamicData = true;
157    arrayData = true;
158    data = new uint8_t[getSize()];
159}
160
161
162bool
163Packet::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
164{
165    Addr func_start = getAddr();
166    Addr func_end   = getAddr() + getSize() - 1;
167    Addr val_start  = addr;
168    Addr val_end    = val_start + size - 1;
169
170    if (func_start > val_end || val_start > func_end) {
171        // no intersection
172        return false;
173    }
174
175    // check print first since it doesn't require data
176    if (isPrint()) {
177        dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
178        return false;
179    }
180
181    // if there's no data, there's no need to look further
182    if (!data) {
183        return false;
184    }
185
186    // offset of functional request into supplied value (could be
187    // negative if partial overlap)
188    int offset = func_start - val_start;
189
190    if (isRead()) {
191        if (func_start >= val_start && func_end <= val_end) {
192            allocate();
193            std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
194            makeResponse();
195            return true;
196        } else {
197            // In this case the timing packet only partially satisfies
198            // the request, so we would need more information to make
199            // this work.  Like bytes valid in the packet or
200            // something, so the request could continue and get this
201            // bit of possibly newer data along with the older data
202            // not written to yet.
203            panic("Memory value only partially satisfies the functional "
204                  "request. Now what?");
205        }
206    } else if (isWrite()) {
207        if (offset >= 0) {
208            std::memcpy(data + offset, getPtr<uint8_t>(),
209                        (std::min(func_end, val_end) - func_start) + 1);
210        } else { // val_start > func_start
211            std::memcpy(data, getPtr<uint8_t>() - offset,
212                        (std::min(func_end, val_end) - val_start) + 1);
213        }
214    } else {
215        panic("Don't know how to handle command %s\n", cmdString());
216    }
217
218    // keep going with request by default
219    return false;
220}
221
222
223void
224Packet::print(std::ostream &o, const int verbosity,
225              const std::string &prefix) const
226{
227    ccprintf(o, "%s[%x:%x] %s\n", prefix,
228             getAddr(), getAddr() + getSize() - 1, cmdString());
229}
230
231
232Packet::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity)
233    : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity)
234{
235    labelStack.push_back(LabelStackEntry("", curPrefixPtr));
236}
237
238
239Packet::PrintReqState::~PrintReqState()
240{
241    labelStack.pop_back();
242    assert(labelStack.empty());
243    delete curPrefixPtr;
244}
245
246
247Packet::PrintReqState::
248LabelStackEntry::LabelStackEntry(const std::string &_label,
249                                 std::string *_prefix)
250    : label(_label), prefix(_prefix), labelPrinted(false)
251{
252}
253
254
255void
256Packet::PrintReqState::pushLabel(const std::string &lbl,
257                                 const std::string &prefix)
258{
259    labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
260    curPrefixPtr = new std::string(*curPrefixPtr);
261    *curPrefixPtr += prefix;
262}
263
264void
265Packet::PrintReqState::popLabel()
266{
267    delete curPrefixPtr;
268    curPrefixPtr = labelStack.back().prefix;
269    labelStack.pop_back();
270    assert(!labelStack.empty());
271}
272
273void
274Packet::PrintReqState::printLabels()
275{
276    if (!labelStack.back().labelPrinted) {
277        LabelStack::iterator i = labelStack.begin();
278        LabelStack::iterator end = labelStack.end();
279        while (i != end) {
280            if (!i->labelPrinted) {
281                ccprintf(os, "%s%s\n", *(i->prefix), i->label);
282                i->labelPrinted = true;
283            }
284            i++;
285        }
286    }
287}
288
289
290void
291Packet::PrintReqState::printObj(Printable *obj)
292{
293    printLabels();
294    obj->print(os, verbosity, curPrefix());
295}
296