packet.cc revision 5507
12568SN/A/*
22568SN/A * Copyright (c) 2006 The Regents of The University of Michigan
32568SN/A * All rights reserved.
42568SN/A *
52568SN/A * Redistribution and use in source and binary forms, with or without
62568SN/A * modification, are permitted provided that the following conditions are
72568SN/A * met: redistributions of source code must retain the above copyright
82568SN/A * notice, this list of conditions and the following disclaimer;
92568SN/A * redistributions in binary form must reproduce the above copyright
102568SN/A * notice, this list of conditions and the following disclaimer in the
112568SN/A * documentation and/or other materials provided with the distribution;
122568SN/A * neither the name of the copyright holders nor the names of its
132568SN/A * contributors may be used to endorse or promote products derived from
142568SN/A * this software without specific prior written permission.
152568SN/A *
162568SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172568SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182568SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192568SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202568SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212568SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222568SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232568SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242568SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252568SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262568SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302568SN/A */
312568SN/A
322568SN/A/**
332568SN/A * @file
342568SN/A * Definition of the Packet Class, a packet is a transaction occuring
352568SN/A * between a single level of the memory heirarchy (ie L1->L2).
362568SN/A */
373260Ssaidi@eecs.umich.edu
383260Ssaidi@eecs.umich.edu#include <iostream>
393918Ssaidi@eecs.umich.edu#include <cstring>
405314Sstever@gmail.com#include "base/cprintf.hh"
412590SN/A#include "base/misc.hh"
423348Sbinkertn@umich.edu#include "base/trace.hh"
432568SN/A#include "mem/packet.hh"
442568SN/A
454022Sstever@eecs.umich.edu// The one downside to bitsets is that static initializers can get ugly.
464022Sstever@eecs.umich.edu#define SET1(a1)                     (1 << (a1))
474022Sstever@eecs.umich.edu#define SET2(a1, a2)                 (SET1(a1) | SET1(a2))
484022Sstever@eecs.umich.edu#define SET3(a1, a2, a3)             (SET2(a1, a2) | SET1(a3))
494022Sstever@eecs.umich.edu#define SET4(a1, a2, a3, a4)         (SET3(a1, a2, a3) | SET1(a4))
504022Sstever@eecs.umich.edu#define SET5(a1, a2, a3, a4, a5)     (SET4(a1, a2, a3, a4) | SET1(a5))
514022Sstever@eecs.umich.edu#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
522641Sstever@eecs.umich.edu
534022Sstever@eecs.umich.educonst MemCmd::CommandInfo
544022Sstever@eecs.umich.eduMemCmd::commandInfo[] =
552641Sstever@eecs.umich.edu{
564022Sstever@eecs.umich.edu    /* InvalidCmd */
574022Sstever@eecs.umich.edu    { 0, InvalidCmd, "InvalidCmd" },
584022Sstever@eecs.umich.edu    /* ReadReq */
594022Sstever@eecs.umich.edu    { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
604473Sstever@eecs.umich.edu    /* ReadResp */
614473Sstever@eecs.umich.edu    { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
625319Sstever@gmail.com    /* ReadRespWithInvalidate */
635319Sstever@gmail.com    { SET4(IsRead, IsResponse, HasData, IsInvalidate),
645319Sstever@gmail.com            InvalidCmd, "ReadRespWithInvalidate" },
654022Sstever@eecs.umich.edu    /* WriteReq */
664626Sstever@eecs.umich.edu    { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
674022Sstever@eecs.umich.edu            WriteResp, "WriteReq" },
684022Sstever@eecs.umich.edu    /* WriteResp */
694626Sstever@eecs.umich.edu    { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
704022Sstever@eecs.umich.edu    /* Writeback */
714628Sstever@eecs.umich.edu    { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
724628Sstever@eecs.umich.edu            InvalidCmd, "Writeback" },
734022Sstever@eecs.umich.edu    /* SoftPFReq */
744022Sstever@eecs.umich.edu    { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
754022Sstever@eecs.umich.edu            SoftPFResp, "SoftPFReq" },
764022Sstever@eecs.umich.edu    /* HardPFReq */
774022Sstever@eecs.umich.edu    { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
784022Sstever@eecs.umich.edu            HardPFResp, "HardPFReq" },
794022Sstever@eecs.umich.edu    /* SoftPFResp */
804022Sstever@eecs.umich.edu    { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
814022Sstever@eecs.umich.edu            InvalidCmd, "SoftPFResp" },
824022Sstever@eecs.umich.edu    /* HardPFResp */
834022Sstever@eecs.umich.edu    { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
844022Sstever@eecs.umich.edu            InvalidCmd, "HardPFResp" },
854022Sstever@eecs.umich.edu    /* WriteInvalidateReq */
864626Sstever@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsInvalidate,
874626Sstever@eecs.umich.edu           IsRequest, HasData, NeedsResponse),
884022Sstever@eecs.umich.edu            WriteInvalidateResp, "WriteInvalidateReq" },
894022Sstever@eecs.umich.edu    /* WriteInvalidateResp */
905319Sstever@gmail.com    { SET3(IsWrite, NeedsExclusive, IsResponse),
914022Sstever@eecs.umich.edu            InvalidCmd, "WriteInvalidateResp" },
924022Sstever@eecs.umich.edu    /* UpgradeReq */
934628Sstever@eecs.umich.edu    { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse),
944628Sstever@eecs.umich.edu            UpgradeResp, "UpgradeReq" },
954628Sstever@eecs.umich.edu    /* UpgradeResp */
965319Sstever@gmail.com    { SET2(NeedsExclusive, IsResponse),
974628Sstever@eecs.umich.edu            InvalidCmd, "UpgradeResp" },
984022Sstever@eecs.umich.edu    /* ReadExReq */
994626Sstever@eecs.umich.edu    { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
1004022Sstever@eecs.umich.edu            ReadExResp, "ReadExReq" },
1014022Sstever@eecs.umich.edu    /* ReadExResp */
1025319Sstever@gmail.com    { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
1034040Ssaidi@eecs.umich.edu            InvalidCmd, "ReadExResp" },
1045507Sstever@gmail.com    /* LoadLockedReq: note that we use plain ReadResp as response, so that
1055507Sstever@gmail.com     *                we can also use ReadRespWithInvalidate when needed */
1064626Sstever@eecs.umich.edu    { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
1075507Sstever@gmail.com            ReadResp, "LoadLockedReq" },
1084626Sstever@eecs.umich.edu    /* StoreCondReq */
1094626Sstever@eecs.umich.edu    { SET6(IsWrite, NeedsExclusive, IsLocked,
1104626Sstever@eecs.umich.edu           IsRequest, NeedsResponse, HasData),
1114626Sstever@eecs.umich.edu            StoreCondResp, "StoreCondReq" },
1124626Sstever@eecs.umich.edu    /* StoreCondResp */
1134626Sstever@eecs.umich.edu    { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
1144626Sstever@eecs.umich.edu            InvalidCmd, "StoreCondResp" },
1154040Ssaidi@eecs.umich.edu    /* SwapReq -- for Swap ldstub type operations */
1164626Sstever@eecs.umich.edu    { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
1174040Ssaidi@eecs.umich.edu        SwapResp, "SwapReq" },
1184040Ssaidi@eecs.umich.edu    /* SwapResp -- for Swap ldstub type operations */
1194626Sstever@eecs.umich.edu    { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
1204870Sstever@eecs.umich.edu            InvalidCmd, "SwapResp" },
1214870Sstever@eecs.umich.edu    /* NetworkNackError  -- nacked at network layer (not by protocol) */
1224986Ssaidi@eecs.umich.edu    { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
1234870Sstever@eecs.umich.edu    /* InvalidDestError  -- packet dest field invalid */
1244986Ssaidi@eecs.umich.edu    { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
1254870Sstever@eecs.umich.edu    /* BadAddressError   -- memory address invalid */
1265314Sstever@gmail.com    { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
1275314Sstever@gmail.com    /* PrintReq */
1285314Sstever@gmail.com    { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
1294022Sstever@eecs.umich.edu};
1302592SN/A
1312811Srdreslin@umich.edu
1322592SN/A/** delete the data pointed to in the data pointer. Ok to call to matter how
1332592SN/A * data was allocted. */
1342592SN/Avoid
1352641Sstever@eecs.umich.eduPacket::deleteData()
1362641Sstever@eecs.umich.edu{
1372592SN/A    assert(staticData || dynamicData);
1382592SN/A    if (staticData)
1392592SN/A        return;
1402592SN/A
1412592SN/A    if (arrayData)
1422592SN/A        delete [] data;
1432592SN/A    else
1442592SN/A        delete data;
1452592SN/A}
1462592SN/A
1472592SN/A/** If there isn't data in the packet, allocate some. */
1482592SN/Avoid
1492641Sstever@eecs.umich.eduPacket::allocate()
1502641Sstever@eecs.umich.edu{
1512592SN/A    if (data)
1522592SN/A        return;
1532592SN/A    assert(!staticData);
1542592SN/A    dynamicData = true;
1552592SN/A    arrayData = true;
1562641Sstever@eecs.umich.edu    data = new uint8_t[getSize()];
1572592SN/A}
1582592SN/A
1593607Srdreslin@umich.edu
1603607Srdreslin@umich.edubool
1615314Sstever@gmail.comPacket::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
1622641Sstever@eecs.umich.edu{
1634626Sstever@eecs.umich.edu    Addr func_start = getAddr();
1644626Sstever@eecs.umich.edu    Addr func_end   = getAddr() + getSize() - 1;
1654626Sstever@eecs.umich.edu    Addr val_start  = addr;
1664626Sstever@eecs.umich.edu    Addr val_end    = val_start + size - 1;
1673260Ssaidi@eecs.umich.edu
1684626Sstever@eecs.umich.edu    if (func_start > val_end || val_start > func_end) {
1694626Sstever@eecs.umich.edu        // no intersection
1704626Sstever@eecs.umich.edu        return false;
1714626Sstever@eecs.umich.edu    }
1723260Ssaidi@eecs.umich.edu
1735314Sstever@gmail.com    // check print first since it doesn't require data
1745314Sstever@gmail.com    if (isPrint()) {
1755314Sstever@gmail.com        dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
1765314Sstever@gmail.com        return false;
1775314Sstever@gmail.com    }
1785314Sstever@gmail.com
1795314Sstever@gmail.com    // if there's no data, there's no need to look further
1805314Sstever@gmail.com    if (!data) {
1815314Sstever@gmail.com        return false;
1825314Sstever@gmail.com    }
1835314Sstever@gmail.com
1844626Sstever@eecs.umich.edu    // offset of functional request into supplied value (could be
1854626Sstever@eecs.umich.edu    // negative if partial overlap)
1864626Sstever@eecs.umich.edu    int offset = func_start - val_start;
1873260Ssaidi@eecs.umich.edu
1884626Sstever@eecs.umich.edu    if (isRead()) {
1894626Sstever@eecs.umich.edu        if (func_start >= val_start && func_end <= val_end) {
1904626Sstever@eecs.umich.edu            allocate();
1914626Sstever@eecs.umich.edu            std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
1924870Sstever@eecs.umich.edu            makeResponse();
1934626Sstever@eecs.umich.edu            return true;
1943260Ssaidi@eecs.umich.edu        } else {
1954489Sstever@eecs.umich.edu            // In this case the timing packet only partially satisfies
1964489Sstever@eecs.umich.edu            // the request, so we would need more information to make
1974489Sstever@eecs.umich.edu            // this work.  Like bytes valid in the packet or
1984489Sstever@eecs.umich.edu            // something, so the request could continue and get this
1994489Sstever@eecs.umich.edu            // bit of possibly newer data along with the older data
2004489Sstever@eecs.umich.edu            // not written to yet.
2014626Sstever@eecs.umich.edu            panic("Memory value only partially satisfies the functional "
2024626Sstever@eecs.umich.edu                  "request. Now what?");
2033260Ssaidi@eecs.umich.edu        }
2044626Sstever@eecs.umich.edu    } else if (isWrite()) {
2054626Sstever@eecs.umich.edu        if (offset >= 0) {
2064626Sstever@eecs.umich.edu            std::memcpy(data + offset, getPtr<uint8_t>(),
2074626Sstever@eecs.umich.edu                        (std::min(func_end, val_end) - func_start) + 1);
2084626Sstever@eecs.umich.edu        } else { // val_start > func_start
2094626Sstever@eecs.umich.edu            std::memcpy(data, getPtr<uint8_t>() - offset,
2104626Sstever@eecs.umich.edu                        (std::min(func_end, val_end) - val_start) + 1);
2113260Ssaidi@eecs.umich.edu        }
2125314Sstever@gmail.com    } else {
2134626Sstever@eecs.umich.edu        panic("Don't know how to handle command %s\n", cmdString());
2145314Sstever@gmail.com    }
2155314Sstever@gmail.com
2165314Sstever@gmail.com    // keep going with request by default
2175314Sstever@gmail.com    return false;
2182641Sstever@eecs.umich.edu}
2193260Ssaidi@eecs.umich.edu
2203260Ssaidi@eecs.umich.edu
2215314Sstever@gmail.comvoid
2225314Sstever@gmail.comPacket::print(std::ostream &o, const int verbosity,
2235314Sstever@gmail.com              const std::string &prefix) const
2243260Ssaidi@eecs.umich.edu{
2255314Sstever@gmail.com    ccprintf(o, "%s[%x:%x] %s\n", prefix,
2265314Sstever@gmail.com             getAddr(), getAddr() + getSize() - 1, cmdString());
2273260Ssaidi@eecs.umich.edu}
2283260Ssaidi@eecs.umich.edu
2295314Sstever@gmail.com
2305314Sstever@gmail.comPacket::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity)
2315314Sstever@gmail.com    : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity)
2325314Sstever@gmail.com{
2335314Sstever@gmail.com    labelStack.push_back(LabelStackEntry("", curPrefixPtr));
2345314Sstever@gmail.com}
2355314Sstever@gmail.com
2365314Sstever@gmail.com
2375314Sstever@gmail.comPacket::PrintReqState::~PrintReqState()
2385314Sstever@gmail.com{
2395314Sstever@gmail.com    labelStack.pop_back();
2405314Sstever@gmail.com    assert(labelStack.empty());
2415314Sstever@gmail.com    delete curPrefixPtr;
2425314Sstever@gmail.com}
2435314Sstever@gmail.com
2445314Sstever@gmail.com
2455314Sstever@gmail.comPacket::PrintReqState::
2465314Sstever@gmail.comLabelStackEntry::LabelStackEntry(const std::string &_label,
2475314Sstever@gmail.com                                 std::string *_prefix)
2485314Sstever@gmail.com    : label(_label), prefix(_prefix), labelPrinted(false)
2495314Sstever@gmail.com{
2505314Sstever@gmail.com}
2515314Sstever@gmail.com
2525314Sstever@gmail.com
2535314Sstever@gmail.comvoid
2545314Sstever@gmail.comPacket::PrintReqState::pushLabel(const std::string &lbl,
2555314Sstever@gmail.com                                 const std::string &prefix)
2565314Sstever@gmail.com{
2575314Sstever@gmail.com    labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
2585314Sstever@gmail.com    curPrefixPtr = new std::string(*curPrefixPtr);
2595314Sstever@gmail.com    *curPrefixPtr += prefix;
2605314Sstever@gmail.com}
2615314Sstever@gmail.com
2625314Sstever@gmail.comvoid
2635314Sstever@gmail.comPacket::PrintReqState::popLabel()
2645314Sstever@gmail.com{
2655314Sstever@gmail.com    delete curPrefixPtr;
2665314Sstever@gmail.com    curPrefixPtr = labelStack.back().prefix;
2675314Sstever@gmail.com    labelStack.pop_back();
2685314Sstever@gmail.com    assert(!labelStack.empty());
2695314Sstever@gmail.com}
2705314Sstever@gmail.com
2715314Sstever@gmail.comvoid
2725314Sstever@gmail.comPacket::PrintReqState::printLabels()
2735314Sstever@gmail.com{
2745314Sstever@gmail.com    if (!labelStack.back().labelPrinted) {
2755314Sstever@gmail.com        LabelStack::iterator i = labelStack.begin();
2765314Sstever@gmail.com        LabelStack::iterator end = labelStack.end();
2775314Sstever@gmail.com        while (i != end) {
2785314Sstever@gmail.com            if (!i->labelPrinted) {
2795314Sstever@gmail.com                ccprintf(os, "%s%s\n", *(i->prefix), i->label);
2805314Sstever@gmail.com                i->labelPrinted = true;
2815314Sstever@gmail.com            }
2825314Sstever@gmail.com            i++;
2835314Sstever@gmail.com        }
2845314Sstever@gmail.com    }
2855314Sstever@gmail.com}
2865314Sstever@gmail.com
2875314Sstever@gmail.com
2885314Sstever@gmail.comvoid
2895314Sstever@gmail.comPacket::PrintReqState::printObj(Printable *obj)
2905314Sstever@gmail.com{
2915314Sstever@gmail.com    printLabels();
2925314Sstever@gmail.com    obj->print(os, verbosity, curPrefix());
2935314Sstever@gmail.com}
294