packet.cc revision 10723:b1d90d88420e
1/*
2 * Copyright (c) 2011-2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Ali Saidi
42 *          Steve Reinhardt
43 */
44
45/**
46 * @file
47 * Definition of the Packet Class, a packet is a transaction occuring
48 * between a single level of the memory heirarchy (ie L1->L2).
49 */
50
51#include <cstring>
52#include <iostream>
53
54#include "base/cprintf.hh"
55#include "base/misc.hh"
56#include "base/trace.hh"
57#include "mem/packet.hh"
58
59using namespace std;
60
61// The one downside to bitsets is that static initializers can get ugly.
62#define SET1(a1)                     (1 << (a1))
63#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
64#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
65#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
66#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
67#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
68
69const MemCmd::CommandInfo
70MemCmd::commandInfo[] =
71{
72    /* InvalidCmd */
73    { 0, InvalidCmd, "InvalidCmd" },
74    /* ReadReq */
75    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
76    /* ReadResp */
77    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
78    /* ReadRespWithInvalidate */
79    { SET4(IsRead, IsResponse, HasData, IsInvalidate),
80            InvalidCmd, "ReadRespWithInvalidate" },
81    /* WriteReq */
82    { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
83            WriteResp, "WriteReq" },
84    /* WriteResp */
85    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
86    /* Writeback */
87    { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
88            InvalidCmd, "Writeback" },
89    /* SoftPFReq */
90    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
91            SoftPFResp, "SoftPFReq" },
92    /* HardPFReq */
93    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
94            HardPFResp, "HardPFReq" },
95    /* SoftPFResp */
96    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
97            InvalidCmd, "SoftPFResp" },
98    /* HardPFResp */
99    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
100            InvalidCmd, "HardPFResp" },
101    /* WriteInvalidateReq */
102    { SET6(IsWrite, NeedsExclusive, IsInvalidate,
103           IsRequest, HasData, NeedsResponse),
104            WriteInvalidateResp, "WriteInvalidateReq" },
105    /* WriteInvalidateResp */
106    { SET3(IsWrite, NeedsExclusive, IsResponse),
107            InvalidCmd, "WriteInvalidateResp" },
108    /* UpgradeReq */
109    { SET5(IsInvalidate, NeedsExclusive, IsUpgrade, IsRequest, NeedsResponse),
110            UpgradeResp, "UpgradeReq" },
111    /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */
112    { SET6(IsInvalidate, NeedsExclusive, IsUpgrade, IsLlsc,
113           IsRequest, NeedsResponse),
114            UpgradeResp, "SCUpgradeReq" },
115    /* UpgradeResp */
116    { SET3(NeedsExclusive, IsUpgrade, IsResponse),
117            InvalidCmd, "UpgradeResp" },
118    /* SCUpgradeFailReq: generates UpgradeFailResp but still gets the data */
119    { SET6(IsRead, NeedsExclusive, IsInvalidate,
120           IsLlsc, IsRequest, NeedsResponse),
121            UpgradeFailResp, "SCUpgradeFailReq" },
122    /* UpgradeFailResp - Behaves like a ReadExReq, but notifies an SC
123     * that it has failed, acquires line as Dirty*/
124    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
125            InvalidCmd, "UpgradeFailResp" },
126    /* ReadExReq */
127    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
128            ReadExResp, "ReadExReq" },
129    /* ReadExResp */
130    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
131            InvalidCmd, "ReadExResp" },
132    /* LoadLockedReq: note that we use plain ReadResp as response, so that
133     *                we can also use ReadRespWithInvalidate when needed */
134    { SET4(IsRead, IsLlsc, IsRequest, NeedsResponse),
135            ReadResp, "LoadLockedReq" },
136    /* StoreCondReq */
137    { SET6(IsWrite, NeedsExclusive, IsLlsc,
138           IsRequest, NeedsResponse, HasData),
139            StoreCondResp, "StoreCondReq" },
140    /* StoreCondFailReq: generates failing StoreCondResp */
141    { SET6(IsWrite, NeedsExclusive, IsLlsc,
142           IsRequest, NeedsResponse, HasData),
143            StoreCondResp, "StoreCondFailReq" },
144    /* StoreCondResp */
145    { SET4(IsWrite, NeedsExclusive, IsLlsc, IsResponse),
146            InvalidCmd, "StoreCondResp" },
147    /* SwapReq -- for Swap ldstub type operations */
148    { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
149        SwapResp, "SwapReq" },
150    /* SwapResp -- for Swap ldstub type operations */
151    { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
152            InvalidCmd, "SwapResp" },
153    /* IntReq -- for interrupts */
154    { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
155        MessageResp, "MessageReq" },
156    /* IntResp -- for interrupts */
157    { SET2(IsWrite, IsResponse), InvalidCmd, "MessageResp" },
158    /* InvalidDestError  -- packet dest field invalid */
159    { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
160    /* BadAddressError   -- memory address invalid */
161    { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
162    /* FunctionalReadError */
163    { SET3(IsRead, IsResponse, IsError), InvalidCmd, "FunctionalReadError" },
164    /* FunctionalWriteError */
165    { SET3(IsWrite, IsResponse, IsError), InvalidCmd, "FunctionalWriteError" },
166    /* PrintReq */
167    { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" },
168    /* Flush Request */
169    { SET3(IsRequest, IsFlush, NeedsExclusive), InvalidCmd, "FlushReq" },
170    /* Invalidation Request */
171    { SET3(NeedsExclusive, IsInvalidate, IsRequest),
172      InvalidCmd, "InvalidationReq" },
173};
174
175bool
176Packet::checkFunctional(Printable *obj, Addr addr, bool is_secure, int size,
177                        uint8_t *_data)
178{
179    Addr func_start = getAddr();
180    Addr func_end   = getAddr() + getSize() - 1;
181    Addr val_start  = addr;
182    Addr val_end    = val_start + size - 1;
183
184    if (is_secure != _isSecure || func_start > val_end ||
185        val_start > func_end) {
186        // no intersection
187        return false;
188    }
189
190    // check print first since it doesn't require data
191    if (isPrint()) {
192        assert(!_data);
193        safe_cast<PrintReqState*>(senderState)->printObj(obj);
194        return false;
195    }
196
197    // we allow the caller to pass NULL to signify the other packet
198    // has no data
199    if (!_data) {
200        return false;
201    }
202
203    // offset of functional request into supplied value (could be
204    // negative if partial overlap)
205    int offset = func_start - val_start;
206
207    if (isRead()) {
208        if (func_start >= val_start && func_end <= val_end) {
209            memcpy(getPtr<uint8_t>(), _data + offset, getSize());
210            if (bytesValid.empty())
211                bytesValid.resize(getSize(), true);
212            // complete overlap, and as the current packet is a read
213            // we are done
214            return true;
215        } else {
216            // Offsets and sizes to copy in case of partial overlap
217            int func_offset;
218            int val_offset;
219            int overlap_size;
220
221            // calculate offsets and copy sizes for the two byte arrays
222            if (val_start < func_start && val_end <= func_end) {
223                // the one we are checking against starts before and
224                // ends before or the same
225                val_offset = func_start - val_start;
226                func_offset = 0;
227                overlap_size = val_end - func_start;
228            } else if (val_start >= func_start && val_end > func_end) {
229                // the one we are checking against starts after or the
230                // same, and ends after
231                val_offset = 0;
232                func_offset = val_start - func_start;
233                overlap_size = func_end - val_start;
234            } else if (val_start >= func_start && val_end <= func_end) {
235                // the one we are checking against is completely
236                // subsumed in the current packet, possibly starting
237                // and ending at the same address
238                val_offset = 0;
239                func_offset = val_start - func_start;
240                overlap_size = size;
241            } else if (val_start < func_start && val_end > func_end) {
242                // the current packet is completely subsumed in the
243                // one we are checking against
244                val_offset = func_start - val_start;
245                func_offset = 0;
246                overlap_size = func_end - func_start;
247            } else {
248                panic("Missed a case for checkFunctional with "
249                      " %s 0x%x size %d, against 0x%x size %d\n",
250                      cmdString(), getAddr(), getSize(), addr, size);
251            }
252
253            // copy partial data into the packet's data array
254            uint8_t *dest = getPtr<uint8_t>() + func_offset;
255            uint8_t *src = _data + val_offset;
256            memcpy(dest, src, overlap_size);
257
258            // initialise the tracking of valid bytes if we have not
259            // used it already
260            if (bytesValid.empty())
261                bytesValid.resize(getSize(), false);
262
263            // track if we are done filling the functional access
264            bool all_bytes_valid = true;
265
266            int i = 0;
267
268            // check up to func_offset
269            for (; all_bytes_valid && i < func_offset; ++i)
270                all_bytes_valid &= bytesValid[i];
271
272            // update the valid bytes
273            for (i = func_offset; i < func_offset + overlap_size; ++i)
274                bytesValid[i] = true;
275
276            // check the bit after the update we just made
277            for (; all_bytes_valid && i < getSize(); ++i)
278                all_bytes_valid &= bytesValid[i];
279
280            return all_bytes_valid;
281        }
282    } else if (isWrite()) {
283        if (offset >= 0) {
284            memcpy(_data + offset, getConstPtr<uint8_t>(),
285                   (min(func_end, val_end) - func_start) + 1);
286        } else {
287            // val_start > func_start
288            memcpy(_data, getConstPtr<uint8_t>() - offset,
289                   (min(func_end, val_end) - val_start) + 1);
290        }
291    } else {
292        panic("Don't know how to handle command %s\n", cmdString());
293    }
294
295    // keep going with request by default
296    return false;
297}
298
299void
300Packet::pushSenderState(Packet::SenderState *sender_state)
301{
302    assert(sender_state != NULL);
303    sender_state->predecessor = senderState;
304    senderState = sender_state;
305}
306
307Packet::SenderState *
308Packet::popSenderState()
309{
310    assert(senderState != NULL);
311    SenderState *sender_state = senderState;
312    senderState = sender_state->predecessor;
313    sender_state->predecessor = NULL;
314    return sender_state;
315}
316
317void
318Packet::print(ostream &o, const int verbosity, const string &prefix) const
319{
320    ccprintf(o, "%s[%x:%x] %s\n", prefix,
321             getAddr(), getAddr() + getSize() - 1, cmdString());
322}
323
324std::string
325Packet::print() const {
326    ostringstream str;
327    print(str);
328    return str.str();
329}
330
331Packet::PrintReqState::PrintReqState(ostream &_os, int _verbosity)
332    : curPrefixPtr(new string("")), os(_os), verbosity(_verbosity)
333{
334    labelStack.push_back(LabelStackEntry("", curPrefixPtr));
335}
336
337Packet::PrintReqState::~PrintReqState()
338{
339    labelStack.pop_back();
340    assert(labelStack.empty());
341    delete curPrefixPtr;
342}
343
344Packet::PrintReqState::
345LabelStackEntry::LabelStackEntry(const string &_label, string *_prefix)
346    : label(_label), prefix(_prefix), labelPrinted(false)
347{
348}
349
350void
351Packet::PrintReqState::pushLabel(const string &lbl, const string &prefix)
352{
353    labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
354    curPrefixPtr = new string(*curPrefixPtr);
355    *curPrefixPtr += prefix;
356}
357
358void
359Packet::PrintReqState::popLabel()
360{
361    delete curPrefixPtr;
362    curPrefixPtr = labelStack.back().prefix;
363    labelStack.pop_back();
364    assert(!labelStack.empty());
365}
366
367void
368Packet::PrintReqState::printLabels()
369{
370    if (!labelStack.back().labelPrinted) {
371        LabelStack::iterator i = labelStack.begin();
372        LabelStack::iterator end = labelStack.end();
373        while (i != end) {
374            if (!i->labelPrinted) {
375                ccprintf(os, "%s%s\n", *(i->prefix), i->label);
376                i->labelPrinted = true;
377            }
378            i++;
379        }
380    }
381}
382
383
384void
385Packet::PrintReqState::printObj(Printable *obj)
386{
387    printLabels();
388    obj->print(os, verbosity, curPrefix());
389}
390