packet.cc revision 10376:28c63d075e0c
16019Shines@cs.fsu.edu/*
27093Sgblack@eecs.umich.edu * Copyright (c) 2011-2014 ARM Limited
37093Sgblack@eecs.umich.edu * All rights reserved
47093Sgblack@eecs.umich.edu *
57093Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
67093Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
77093Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
87093Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
97093Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
107093Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
117093Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
127093Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
137093Sgblack@eecs.umich.edu *
146019Shines@cs.fsu.edu * Copyright (c) 2006 The Regents of The University of Michigan
156019Shines@cs.fsu.edu * Copyright (c) 2010 Advanced Micro Devices, Inc.
166019Shines@cs.fsu.edu * All rights reserved.
176019Shines@cs.fsu.edu *
186019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without
196019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are
206019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright
216019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer;
226019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright
236019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the
246019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution;
256019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its
266019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
276019Shines@cs.fsu.edu * this software without specific prior written permission.
286019Shines@cs.fsu.edu *
296019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
306019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
316019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
326019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
336019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
346019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
356019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
366019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
376019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
386019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
396019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
406019Shines@cs.fsu.edu *
416735Sgblack@eecs.umich.edu * Authors: Ali Saidi
426735Sgblack@eecs.umich.edu *          Steve Reinhardt
436019Shines@cs.fsu.edu */
446019Shines@cs.fsu.edu
456019Shines@cs.fsu.edu/**
466019Shines@cs.fsu.edu * @file
476019Shines@cs.fsu.edu * Definition of the Packet Class, a packet is a transaction occuring
486019Shines@cs.fsu.edu * between a single level of the memory heirarchy (ie L1->L2).
496019Shines@cs.fsu.edu */
506019Shines@cs.fsu.edu
516019Shines@cs.fsu.edu#include <cstring>
526019Shines@cs.fsu.edu#include <iostream>
536735Sgblack@eecs.umich.edu
546735Sgblack@eecs.umich.edu#include "base/cprintf.hh"
556019Shines@cs.fsu.edu#include "base/misc.hh"
566735Sgblack@eecs.umich.edu#include "base/trace.hh"
576735Sgblack@eecs.umich.edu#include "mem/packet.hh"
586019Shines@cs.fsu.edu
596735Sgblack@eecs.umich.eduusing namespace std;
606735Sgblack@eecs.umich.edu
616019Shines@cs.fsu.edu// The one downside to bitsets is that static initializers can get ugly.
626735Sgblack@eecs.umich.edu#define SET1(a1)                     (1 << (a1))
636735Sgblack@eecs.umich.edu#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
646019Shines@cs.fsu.edu#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
656735Sgblack@eecs.umich.edu#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
666735Sgblack@eecs.umich.edu#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
676019Shines@cs.fsu.edu#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
686735Sgblack@eecs.umich.edu
696735Sgblack@eecs.umich.educonst MemCmd::CommandInfo
706019Shines@cs.fsu.eduMemCmd::commandInfo[] =
716735Sgblack@eecs.umich.edu{
726735Sgblack@eecs.umich.edu    /* InvalidCmd */
736019Shines@cs.fsu.edu    { 0, InvalidCmd, "InvalidCmd" },
746735Sgblack@eecs.umich.edu    /* ReadReq */
756735Sgblack@eecs.umich.edu    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
766735Sgblack@eecs.umich.edu    /* ReadResp */
776735Sgblack@eecs.umich.edu    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
786019Shines@cs.fsu.edu    /* ReadRespWithInvalidate */
796735Sgblack@eecs.umich.edu    { SET4(IsRead, IsResponse, HasData, IsInvalidate),
806735Sgblack@eecs.umich.edu            InvalidCmd, "ReadRespWithInvalidate" },
816735Sgblack@eecs.umich.edu    /* WriteReq */
826735Sgblack@eecs.umich.edu    { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
836735Sgblack@eecs.umich.edu            WriteResp, "WriteReq" },
846735Sgblack@eecs.umich.edu    /* WriteResp */
856735Sgblack@eecs.umich.edu    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
866735Sgblack@eecs.umich.edu    /* Writeback */
876735Sgblack@eecs.umich.edu    { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
886019Shines@cs.fsu.edu            InvalidCmd, "Writeback" },
896019Shines@cs.fsu.edu    /* SoftPFReq */
906019Shines@cs.fsu.edu    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
916735Sgblack@eecs.umich.edu            SoftPFResp, "SoftPFReq" },
926735Sgblack@eecs.umich.edu    /* HardPFReq */
936735Sgblack@eecs.umich.edu    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
946735Sgblack@eecs.umich.edu            HardPFResp, "HardPFReq" },
956019Shines@cs.fsu.edu    /* SoftPFResp */
966735Sgblack@eecs.umich.edu    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
976735Sgblack@eecs.umich.edu            InvalidCmd, "SoftPFResp" },
986735Sgblack@eecs.umich.edu    /* HardPFResp */
996019Shines@cs.fsu.edu    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
1006735Sgblack@eecs.umich.edu            InvalidCmd, "HardPFResp" },
1016735Sgblack@eecs.umich.edu    /* WriteInvalidateReq */
1026735Sgblack@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsInvalidate,
1036735Sgblack@eecs.umich.edu           IsRequest, HasData, NeedsResponse),
1046735Sgblack@eecs.umich.edu            WriteInvalidateResp, "WriteInvalidateReq" },
1056735Sgblack@eecs.umich.edu    /* WriteInvalidateResp */
1066735Sgblack@eecs.umich.edu    { SET3(IsWrite, NeedsExclusive, IsResponse),
1076735Sgblack@eecs.umich.edu            InvalidCmd, "WriteInvalidateResp" },
1086735Sgblack@eecs.umich.edu    /* UpgradeReq */
1096735Sgblack@eecs.umich.edu    { SET5(IsInvalidate, NeedsExclusive, IsUpgrade, IsRequest, NeedsResponse),
1107093Sgblack@eecs.umich.edu            UpgradeResp, "UpgradeReq" },
1116735Sgblack@eecs.umich.edu    /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */
1126735Sgblack@eecs.umich.edu    { SET6(IsInvalidate, NeedsExclusive, IsUpgrade, IsLlsc,
1136735Sgblack@eecs.umich.edu           IsRequest, NeedsResponse),
1146735Sgblack@eecs.umich.edu            UpgradeResp, "SCUpgradeReq" },
1156735Sgblack@eecs.umich.edu    /* UpgradeResp */
1166735Sgblack@eecs.umich.edu    { SET3(NeedsExclusive, IsUpgrade, IsResponse),
1176735Sgblack@eecs.umich.edu            InvalidCmd, "UpgradeResp" },
1186735Sgblack@eecs.umich.edu    /* SCUpgradeFailReq: generates UpgradeFailResp but still gets the data */
1196735Sgblack@eecs.umich.edu    { SET6(IsRead, NeedsExclusive, IsInvalidate,
1206735Sgblack@eecs.umich.edu           IsLlsc, IsRequest, NeedsResponse),
1216735Sgblack@eecs.umich.edu            UpgradeFailResp, "SCUpgradeFailReq" },
1226735Sgblack@eecs.umich.edu    /* UpgradeFailResp - Behaves like a ReadExReq, but notifies an SC
1236735Sgblack@eecs.umich.edu     * that it has failed, acquires line as Dirty*/
1246735Sgblack@eecs.umich.edu    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
1256735Sgblack@eecs.umich.edu            InvalidCmd, "UpgradeFailResp" },
1266735Sgblack@eecs.umich.edu    /* ReadExReq */
1276735Sgblack@eecs.umich.edu    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
1286735Sgblack@eecs.umich.edu            ReadExResp, "ReadExReq" },
1296735Sgblack@eecs.umich.edu    /* ReadExResp */
1306735Sgblack@eecs.umich.edu    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
1316735Sgblack@eecs.umich.edu            InvalidCmd, "ReadExResp" },
1326735Sgblack@eecs.umich.edu    /* LoadLockedReq: note that we use plain ReadResp as response, so that
1336735Sgblack@eecs.umich.edu     *                we can also use ReadRespWithInvalidate when needed */
1346735Sgblack@eecs.umich.edu    { SET4(IsRead, IsLlsc, IsRequest, NeedsResponse),
1356735Sgblack@eecs.umich.edu            ReadResp, "LoadLockedReq" },
1367093Sgblack@eecs.umich.edu    /* StoreCondReq */
1377093Sgblack@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsLlsc,
1387093Sgblack@eecs.umich.edu           IsRequest, NeedsResponse, HasData),
1397093Sgblack@eecs.umich.edu            StoreCondResp, "StoreCondReq" },
1407093Sgblack@eecs.umich.edu    /* StoreCondFailReq: generates failing StoreCondResp */
1417093Sgblack@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsLlsc,
1427093Sgblack@eecs.umich.edu           IsRequest, NeedsResponse, HasData),
1437093Sgblack@eecs.umich.edu            StoreCondResp, "StoreCondFailReq" },
1446019Shines@cs.fsu.edu    /* StoreCondResp */
1456019Shines@cs.fsu.edu    { SET4(IsWrite, NeedsExclusive, IsLlsc, IsResponse),
1466019Shines@cs.fsu.edu            InvalidCmd, "StoreCondResp" },
1476735Sgblack@eecs.umich.edu    /* SwapReq -- for Swap ldstub type operations */
1486019Shines@cs.fsu.edu    { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
1496019Shines@cs.fsu.edu        SwapResp, "SwapReq" },
1506019Shines@cs.fsu.edu    /* SwapResp -- for Swap ldstub type operations */
1516019Shines@cs.fsu.edu    { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
1526019Shines@cs.fsu.edu            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        safe_cast<PrintReqState*>(senderState)->printObj(obj);
193        return false;
194    }
195
196    // if there's no data, there's no need to look further
197    if (!data) {
198        return false;
199    }
200
201    // offset of functional request into supplied value (could be
202    // negative if partial overlap)
203    int offset = func_start - val_start;
204
205    if (isRead()) {
206        if (func_start >= val_start && func_end <= val_end) {
207            allocate();
208            memcpy(getPtr<uint8_t>(), data + offset, getSize());
209            return true;
210        } else {
211            // Offsets and sizes to copy in case of partial overlap
212            int func_offset;
213            int val_offset;
214            int overlap_size;
215
216            // calculate offsets and copy sizes for the two byte arrays
217            if (val_start < func_start && val_end <= func_end) {
218                val_offset = func_start - val_start;
219                func_offset = 0;
220                overlap_size = val_end - func_start;
221            } else if (val_start >= func_start && val_end > func_end) {
222                val_offset = 0;
223                func_offset = val_start - func_start;
224                overlap_size = func_end - val_start;
225            } else if (val_start >= func_start && val_end <= func_end) {
226                val_offset = 0;
227                func_offset = val_start - func_start;
228                overlap_size = size;
229            } else {
230                panic("BUG: Missed a case for a partial functional request");
231            }
232
233            // Figure out how much of the partial overlap should be copied
234            // into the packet and not overwrite previously found bytes.
235            if (bytesValidStart == 0 && bytesValidEnd == 0) {
236                // No bytes have been copied yet, just set indices
237                // to found range
238                bytesValidStart = func_offset;
239                bytesValidEnd = func_offset + overlap_size;
240            } else {
241                // Some bytes have already been copied. Use bytesValid
242                // indices and offset values to figure out how much data
243                // to copy and where to copy it to.
244
245                // Indice overlap conditions to check
246                int a = func_offset - bytesValidStart;
247                int b = (func_offset + overlap_size) - bytesValidEnd;
248                int c = func_offset - bytesValidEnd;
249                int d = (func_offset + overlap_size) - bytesValidStart;
250
251                if (a >= 0 && b <= 0) {
252                    // bytes already in pkt data array are superset of
253                    // found bytes, will not copy any bytes
254                    overlap_size = 0;
255                } else if (a < 0 && d >= 0 && b <= 0) {
256                    // found bytes will move bytesValidStart towards 0
257                    overlap_size = bytesValidStart - func_offset;
258                    bytesValidStart = func_offset;
259                } else if (b > 0 && c <= 0 && a >= 0) {
260                    // found bytes will move bytesValidEnd
261                    // towards end of pkt data array
262                    overlap_size =
263                        (func_offset + overlap_size) - bytesValidEnd;
264                    val_offset += bytesValidEnd - func_offset;
265                    func_offset = bytesValidEnd;
266                    bytesValidEnd += overlap_size;
267                } else if (a < 0 && b > 0) {
268                    // Found bytes are superset of copied range. Will move
269                    // bytesValidStart towards 0 and bytesValidEnd towards
270                    // end of pkt data array.  Need to break copy into two
271                    // pieces so as to not overwrite previously found data.
272
273                    // copy the first half
274                    uint8_t *dest = getPtr<uint8_t>() + func_offset;
275                    uint8_t *src = data + val_offset;
276                    memcpy(dest, src, (bytesValidStart - func_offset));
277
278                    // re-calc the offsets and indices to do the copy
279                    // required for the second half
280                    val_offset += (bytesValidEnd - func_offset);
281                    bytesValidStart = func_offset;
282                    overlap_size =
283                        (func_offset + overlap_size) - bytesValidEnd;
284                    func_offset = bytesValidEnd;
285                    bytesValidEnd += overlap_size;
286                } else if ((c > 0 && b > 0)
287                           || (a < 0 && d < 0)) {
288                    // region to be copied is discontiguous! Not supported.
289                    panic("BUG: Discontiguous bytes found"
290                          "for functional copying!");
291                }
292            }
293            assert(bytesValidEnd <= getSize());
294
295            // copy partial data into the packet's data array
296            uint8_t *dest = getPtr<uint8_t>() + func_offset;
297            uint8_t *src = data + val_offset;
298            memcpy(dest, src, overlap_size);
299
300            // check if we're done filling the functional access
301            bool done = (bytesValidStart == 0) && (bytesValidEnd == getSize());
302            return done;
303        }
304    } else if (isWrite()) {
305        if (offset >= 0) {
306            memcpy(data + offset, getPtr<uint8_t>(),
307                   (min(func_end, val_end) - func_start) + 1);
308        } else {
309            // val_start > func_start
310            memcpy(data, getPtr<uint8_t>() - offset,
311                   (min(func_end, val_end) - val_start) + 1);
312        }
313    } else {
314        panic("Don't know how to handle command %s\n", cmdString());
315    }
316
317    // keep going with request by default
318    return false;
319}
320
321void
322Packet::pushSenderState(Packet::SenderState *sender_state)
323{
324    assert(sender_state != NULL);
325    sender_state->predecessor = senderState;
326    senderState = sender_state;
327}
328
329Packet::SenderState *
330Packet::popSenderState()
331{
332    assert(senderState != NULL);
333    SenderState *sender_state = senderState;
334    senderState = sender_state->predecessor;
335    sender_state->predecessor = NULL;
336    return sender_state;
337}
338
339void
340Packet::print(ostream &o, const int verbosity, const string &prefix) const
341{
342    ccprintf(o, "%s[%x:%x] %s\n", prefix,
343             getAddr(), getAddr() + getSize() - 1, cmdString());
344}
345
346std::string
347Packet::print() const {
348    ostringstream str;
349    print(str);
350    return str.str();
351}
352
353Packet::PrintReqState::PrintReqState(ostream &_os, int _verbosity)
354    : curPrefixPtr(new string("")), os(_os), verbosity(_verbosity)
355{
356    labelStack.push_back(LabelStackEntry("", curPrefixPtr));
357}
358
359Packet::PrintReqState::~PrintReqState()
360{
361    labelStack.pop_back();
362    assert(labelStack.empty());
363    delete curPrefixPtr;
364}
365
366Packet::PrintReqState::
367LabelStackEntry::LabelStackEntry(const string &_label, string *_prefix)
368    : label(_label), prefix(_prefix), labelPrinted(false)
369{
370}
371
372void
373Packet::PrintReqState::pushLabel(const string &lbl, const string &prefix)
374{
375    labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
376    curPrefixPtr = new string(*curPrefixPtr);
377    *curPrefixPtr += prefix;
378}
379
380void
381Packet::PrintReqState::popLabel()
382{
383    delete curPrefixPtr;
384    curPrefixPtr = labelStack.back().prefix;
385    labelStack.pop_back();
386    assert(!labelStack.empty());
387}
388
389void
390Packet::PrintReqState::printLabels()
391{
392    if (!labelStack.back().labelPrinted) {
393        LabelStack::iterator i = labelStack.begin();
394        LabelStack::iterator end = labelStack.end();
395        while (i != end) {
396            if (!i->labelPrinted) {
397                ccprintf(os, "%s%s\n", *(i->prefix), i->label);
398                i->labelPrinted = true;
399            }
400            i++;
401        }
402    }
403}
404
405
406void
407Packet::PrintReqState::printObj(Printable *obj)
408{
409    printLabels();
410    obj->print(os, verbosity, curPrefix());
411}
412