packet.cc revision 5650
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" },
1215650Sgblack@eecs.umich.edu    /* IntReq -- for interrupts */
1225650Sgblack@eecs.umich.edu    { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
1235650Sgblack@eecs.umich.edu        MessageReq, "MessageReq" },
1245650Sgblack@eecs.umich.edu    /* IntResp -- for interrupts */
1255650Sgblack@eecs.umich.edu    { SET2(IsWrite, IsResponse), MessageResp, "MessageResp" },
1264870Sstever@eecs.umich.edu    /* NetworkNackError  -- nacked at network layer (not by protocol) */
1274986Ssaidi@eecs.umich.edu    { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
1284870Sstever@eecs.umich.edu    /* InvalidDestError  -- packet dest field invalid */
1294986Ssaidi@eecs.umich.edu    { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
1304870Sstever@eecs.umich.edu    /* BadAddressError   -- memory address invalid */
1315314Sstever@gmail.com    { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
1325314Sstever@gmail.com    /* PrintReq */
1335314Sstever@gmail.com    { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
1344022Sstever@eecs.umich.edu};
1352592SN/A
1362811Srdreslin@umich.edu
1372592SN/A/** delete the data pointed to in the data pointer. Ok to call to matter how
1382592SN/A * data was allocted. */
1392592SN/Avoid
1402641Sstever@eecs.umich.eduPacket::deleteData()
1412641Sstever@eecs.umich.edu{
1422592SN/A    assert(staticData || dynamicData);
1432592SN/A    if (staticData)
1442592SN/A        return;
1452592SN/A
1462592SN/A    if (arrayData)
1472592SN/A        delete [] data;
1482592SN/A    else
1492592SN/A        delete data;
1502592SN/A}
1512592SN/A
1522592SN/A/** If there isn't data in the packet, allocate some. */
1532592SN/Avoid
1542641Sstever@eecs.umich.eduPacket::allocate()
1552641Sstever@eecs.umich.edu{
1562592SN/A    if (data)
1572592SN/A        return;
1582592SN/A    assert(!staticData);
1592592SN/A    dynamicData = true;
1602592SN/A    arrayData = true;
1612641Sstever@eecs.umich.edu    data = new uint8_t[getSize()];
1622592SN/A}
1632592SN/A
1643607Srdreslin@umich.edu
1653607Srdreslin@umich.edubool
1665314Sstever@gmail.comPacket::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
1672641Sstever@eecs.umich.edu{
1684626Sstever@eecs.umich.edu    Addr func_start = getAddr();
1694626Sstever@eecs.umich.edu    Addr func_end   = getAddr() + getSize() - 1;
1704626Sstever@eecs.umich.edu    Addr val_start  = addr;
1714626Sstever@eecs.umich.edu    Addr val_end    = val_start + size - 1;
1723260Ssaidi@eecs.umich.edu
1734626Sstever@eecs.umich.edu    if (func_start > val_end || val_start > func_end) {
1744626Sstever@eecs.umich.edu        // no intersection
1754626Sstever@eecs.umich.edu        return false;
1764626Sstever@eecs.umich.edu    }
1773260Ssaidi@eecs.umich.edu
1785314Sstever@gmail.com    // check print first since it doesn't require data
1795314Sstever@gmail.com    if (isPrint()) {
1805314Sstever@gmail.com        dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
1815314Sstever@gmail.com        return false;
1825314Sstever@gmail.com    }
1835314Sstever@gmail.com
1845314Sstever@gmail.com    // if there's no data, there's no need to look further
1855314Sstever@gmail.com    if (!data) {
1865314Sstever@gmail.com        return false;
1875314Sstever@gmail.com    }
1885314Sstever@gmail.com
1894626Sstever@eecs.umich.edu    // offset of functional request into supplied value (could be
1904626Sstever@eecs.umich.edu    // negative if partial overlap)
1914626Sstever@eecs.umich.edu    int offset = func_start - val_start;
1923260Ssaidi@eecs.umich.edu
1934626Sstever@eecs.umich.edu    if (isRead()) {
1944626Sstever@eecs.umich.edu        if (func_start >= val_start && func_end <= val_end) {
1954626Sstever@eecs.umich.edu            allocate();
1964626Sstever@eecs.umich.edu            std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
1974870Sstever@eecs.umich.edu            makeResponse();
1984626Sstever@eecs.umich.edu            return true;
1993260Ssaidi@eecs.umich.edu        } else {
2004489Sstever@eecs.umich.edu            // In this case the timing packet only partially satisfies
2014489Sstever@eecs.umich.edu            // the request, so we would need more information to make
2024489Sstever@eecs.umich.edu            // this work.  Like bytes valid in the packet or
2034489Sstever@eecs.umich.edu            // something, so the request could continue and get this
2044489Sstever@eecs.umich.edu            // bit of possibly newer data along with the older data
2054489Sstever@eecs.umich.edu            // not written to yet.
2064626Sstever@eecs.umich.edu            panic("Memory value only partially satisfies the functional "
2074626Sstever@eecs.umich.edu                  "request. Now what?");
2083260Ssaidi@eecs.umich.edu        }
2094626Sstever@eecs.umich.edu    } else if (isWrite()) {
2104626Sstever@eecs.umich.edu        if (offset >= 0) {
2114626Sstever@eecs.umich.edu            std::memcpy(data + offset, getPtr<uint8_t>(),
2124626Sstever@eecs.umich.edu                        (std::min(func_end, val_end) - func_start) + 1);
2134626Sstever@eecs.umich.edu        } else { // val_start > func_start
2144626Sstever@eecs.umich.edu            std::memcpy(data, getPtr<uint8_t>() - offset,
2154626Sstever@eecs.umich.edu                        (std::min(func_end, val_end) - val_start) + 1);
2163260Ssaidi@eecs.umich.edu        }
2175314Sstever@gmail.com    } else {
2184626Sstever@eecs.umich.edu        panic("Don't know how to handle command %s\n", cmdString());
2195314Sstever@gmail.com    }
2205314Sstever@gmail.com
2215314Sstever@gmail.com    // keep going with request by default
2225314Sstever@gmail.com    return false;
2232641Sstever@eecs.umich.edu}
2243260Ssaidi@eecs.umich.edu
2253260Ssaidi@eecs.umich.edu
2265314Sstever@gmail.comvoid
2275314Sstever@gmail.comPacket::print(std::ostream &o, const int verbosity,
2285314Sstever@gmail.com              const std::string &prefix) const
2293260Ssaidi@eecs.umich.edu{
2305314Sstever@gmail.com    ccprintf(o, "%s[%x:%x] %s\n", prefix,
2315314Sstever@gmail.com             getAddr(), getAddr() + getSize() - 1, cmdString());
2323260Ssaidi@eecs.umich.edu}
2333260Ssaidi@eecs.umich.edu
2345314Sstever@gmail.com
2355314Sstever@gmail.comPacket::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity)
2365314Sstever@gmail.com    : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity)
2375314Sstever@gmail.com{
2385314Sstever@gmail.com    labelStack.push_back(LabelStackEntry("", curPrefixPtr));
2395314Sstever@gmail.com}
2405314Sstever@gmail.com
2415314Sstever@gmail.com
2425314Sstever@gmail.comPacket::PrintReqState::~PrintReqState()
2435314Sstever@gmail.com{
2445314Sstever@gmail.com    labelStack.pop_back();
2455314Sstever@gmail.com    assert(labelStack.empty());
2465314Sstever@gmail.com    delete curPrefixPtr;
2475314Sstever@gmail.com}
2485314Sstever@gmail.com
2495314Sstever@gmail.com
2505314Sstever@gmail.comPacket::PrintReqState::
2515314Sstever@gmail.comLabelStackEntry::LabelStackEntry(const std::string &_label,
2525314Sstever@gmail.com                                 std::string *_prefix)
2535314Sstever@gmail.com    : label(_label), prefix(_prefix), labelPrinted(false)
2545314Sstever@gmail.com{
2555314Sstever@gmail.com}
2565314Sstever@gmail.com
2575314Sstever@gmail.com
2585314Sstever@gmail.comvoid
2595314Sstever@gmail.comPacket::PrintReqState::pushLabel(const std::string &lbl,
2605314Sstever@gmail.com                                 const std::string &prefix)
2615314Sstever@gmail.com{
2625314Sstever@gmail.com    labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
2635314Sstever@gmail.com    curPrefixPtr = new std::string(*curPrefixPtr);
2645314Sstever@gmail.com    *curPrefixPtr += prefix;
2655314Sstever@gmail.com}
2665314Sstever@gmail.com
2675314Sstever@gmail.comvoid
2685314Sstever@gmail.comPacket::PrintReqState::popLabel()
2695314Sstever@gmail.com{
2705314Sstever@gmail.com    delete curPrefixPtr;
2715314Sstever@gmail.com    curPrefixPtr = labelStack.back().prefix;
2725314Sstever@gmail.com    labelStack.pop_back();
2735314Sstever@gmail.com    assert(!labelStack.empty());
2745314Sstever@gmail.com}
2755314Sstever@gmail.com
2765314Sstever@gmail.comvoid
2775314Sstever@gmail.comPacket::PrintReqState::printLabels()
2785314Sstever@gmail.com{
2795314Sstever@gmail.com    if (!labelStack.back().labelPrinted) {
2805314Sstever@gmail.com        LabelStack::iterator i = labelStack.begin();
2815314Sstever@gmail.com        LabelStack::iterator end = labelStack.end();
2825314Sstever@gmail.com        while (i != end) {
2835314Sstever@gmail.com            if (!i->labelPrinted) {
2845314Sstever@gmail.com                ccprintf(os, "%s%s\n", *(i->prefix), i->label);
2855314Sstever@gmail.com                i->labelPrinted = true;
2865314Sstever@gmail.com            }
2875314Sstever@gmail.com            i++;
2885314Sstever@gmail.com        }
2895314Sstever@gmail.com    }
2905314Sstever@gmail.com}
2915314Sstever@gmail.com
2925314Sstever@gmail.com
2935314Sstever@gmail.comvoid
2945314Sstever@gmail.comPacket::PrintReqState::printObj(Printable *obj)
2955314Sstever@gmail.com{
2965314Sstever@gmail.com    printLabels();
2975314Sstever@gmail.com    obj->print(os, verbosity, curPrefix());
2985314Sstever@gmail.com}
299