packet.cc revision 5314
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" }, 624022Sstever@eecs.umich.edu /* WriteReq */ 634626Sstever@eecs.umich.edu { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData), 644022Sstever@eecs.umich.edu WriteResp, "WriteReq" }, 654022Sstever@eecs.umich.edu /* WriteResp */ 664626Sstever@eecs.umich.edu { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" }, 674022Sstever@eecs.umich.edu /* Writeback */ 684628Sstever@eecs.umich.edu { SET4(IsWrite, NeedsExclusive, IsRequest, HasData), 694628Sstever@eecs.umich.edu InvalidCmd, "Writeback" }, 704022Sstever@eecs.umich.edu /* SoftPFReq */ 714022Sstever@eecs.umich.edu { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse), 724022Sstever@eecs.umich.edu SoftPFResp, "SoftPFReq" }, 734022Sstever@eecs.umich.edu /* HardPFReq */ 744022Sstever@eecs.umich.edu { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse), 754022Sstever@eecs.umich.edu HardPFResp, "HardPFReq" }, 764022Sstever@eecs.umich.edu /* SoftPFResp */ 774022Sstever@eecs.umich.edu { SET4(IsRead, IsResponse, IsSWPrefetch, HasData), 784022Sstever@eecs.umich.edu InvalidCmd, "SoftPFResp" }, 794022Sstever@eecs.umich.edu /* HardPFResp */ 804022Sstever@eecs.umich.edu { SET4(IsRead, IsResponse, IsHWPrefetch, HasData), 814022Sstever@eecs.umich.edu InvalidCmd, "HardPFResp" }, 824022Sstever@eecs.umich.edu /* WriteInvalidateReq */ 834626Sstever@eecs.umich.edu { SET6(IsWrite, NeedsExclusive, IsInvalidate, 844626Sstever@eecs.umich.edu IsRequest, HasData, NeedsResponse), 854022Sstever@eecs.umich.edu WriteInvalidateResp, "WriteInvalidateReq" }, 864022Sstever@eecs.umich.edu /* WriteInvalidateResp */ 874626Sstever@eecs.umich.edu { SET4(IsWrite, NeedsExclusive, IsInvalidate, IsResponse), 884022Sstever@eecs.umich.edu InvalidCmd, "WriteInvalidateResp" }, 894022Sstever@eecs.umich.edu /* UpgradeReq */ 904628Sstever@eecs.umich.edu { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse), 914628Sstever@eecs.umich.edu UpgradeResp, "UpgradeReq" }, 924628Sstever@eecs.umich.edu /* UpgradeResp */ 934628Sstever@eecs.umich.edu { SET3(IsInvalidate, NeedsExclusive, IsResponse), 944628Sstever@eecs.umich.edu InvalidCmd, "UpgradeResp" }, 954022Sstever@eecs.umich.edu /* ReadExReq */ 964626Sstever@eecs.umich.edu { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse), 974022Sstever@eecs.umich.edu ReadExResp, "ReadExReq" }, 984022Sstever@eecs.umich.edu /* ReadExResp */ 994626Sstever@eecs.umich.edu { SET5(IsRead, NeedsExclusive, IsInvalidate, IsResponse, HasData), 1004040Ssaidi@eecs.umich.edu InvalidCmd, "ReadExResp" }, 1014626Sstever@eecs.umich.edu /* LoadLockedReq */ 1024626Sstever@eecs.umich.edu { SET4(IsRead, IsLocked, IsRequest, NeedsResponse), 1034880Sstever@eecs.umich.edu LoadLockedResp, "LoadLockedReq" }, 1044626Sstever@eecs.umich.edu /* LoadLockedResp */ 1054626Sstever@eecs.umich.edu { SET4(IsRead, IsLocked, IsResponse, HasData), 1064626Sstever@eecs.umich.edu InvalidCmd, "LoadLockedResp" }, 1074626Sstever@eecs.umich.edu /* StoreCondReq */ 1084626Sstever@eecs.umich.edu { SET6(IsWrite, NeedsExclusive, IsLocked, 1094626Sstever@eecs.umich.edu IsRequest, NeedsResponse, HasData), 1104626Sstever@eecs.umich.edu StoreCondResp, "StoreCondReq" }, 1114626Sstever@eecs.umich.edu /* StoreCondResp */ 1124626Sstever@eecs.umich.edu { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse), 1134626Sstever@eecs.umich.edu InvalidCmd, "StoreCondResp" }, 1144040Ssaidi@eecs.umich.edu /* SwapReq -- for Swap ldstub type operations */ 1154626Sstever@eecs.umich.edu { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse), 1164040Ssaidi@eecs.umich.edu SwapResp, "SwapReq" }, 1174040Ssaidi@eecs.umich.edu /* SwapResp -- for Swap ldstub type operations */ 1184626Sstever@eecs.umich.edu { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData), 1194870Sstever@eecs.umich.edu InvalidCmd, "SwapResp" }, 1204870Sstever@eecs.umich.edu /* NetworkNackError -- nacked at network layer (not by protocol) */ 1214986Ssaidi@eecs.umich.edu { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" }, 1224870Sstever@eecs.umich.edu /* InvalidDestError -- packet dest field invalid */ 1234986Ssaidi@eecs.umich.edu { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" }, 1244870Sstever@eecs.umich.edu /* BadAddressError -- memory address invalid */ 1255314Sstever@gmail.com { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" }, 1265314Sstever@gmail.com /* PrintReq */ 1275314Sstever@gmail.com { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" } 1284022Sstever@eecs.umich.edu}; 1292592SN/A 1302811Srdreslin@umich.edu 1312592SN/A/** delete the data pointed to in the data pointer. Ok to call to matter how 1322592SN/A * data was allocted. */ 1332592SN/Avoid 1342641Sstever@eecs.umich.eduPacket::deleteData() 1352641Sstever@eecs.umich.edu{ 1362592SN/A assert(staticData || dynamicData); 1372592SN/A if (staticData) 1382592SN/A return; 1392592SN/A 1402592SN/A if (arrayData) 1412592SN/A delete [] data; 1422592SN/A else 1432592SN/A delete data; 1442592SN/A} 1452592SN/A 1462592SN/A/** If there isn't data in the packet, allocate some. */ 1472592SN/Avoid 1482641Sstever@eecs.umich.eduPacket::allocate() 1492641Sstever@eecs.umich.edu{ 1502592SN/A if (data) 1512592SN/A return; 1522592SN/A assert(!staticData); 1532592SN/A dynamicData = true; 1542592SN/A arrayData = true; 1552641Sstever@eecs.umich.edu data = new uint8_t[getSize()]; 1562592SN/A} 1572592SN/A 1583607Srdreslin@umich.edu 1593607Srdreslin@umich.edubool 1605314Sstever@gmail.comPacket::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data) 1612641Sstever@eecs.umich.edu{ 1624626Sstever@eecs.umich.edu Addr func_start = getAddr(); 1634626Sstever@eecs.umich.edu Addr func_end = getAddr() + getSize() - 1; 1644626Sstever@eecs.umich.edu Addr val_start = addr; 1654626Sstever@eecs.umich.edu Addr val_end = val_start + size - 1; 1663260Ssaidi@eecs.umich.edu 1674626Sstever@eecs.umich.edu if (func_start > val_end || val_start > func_end) { 1684626Sstever@eecs.umich.edu // no intersection 1694626Sstever@eecs.umich.edu return false; 1704626Sstever@eecs.umich.edu } 1713260Ssaidi@eecs.umich.edu 1725314Sstever@gmail.com // check print first since it doesn't require data 1735314Sstever@gmail.com if (isPrint()) { 1745314Sstever@gmail.com dynamic_cast<PrintReqState*>(senderState)->printObj(obj); 1755314Sstever@gmail.com return false; 1765314Sstever@gmail.com } 1775314Sstever@gmail.com 1785314Sstever@gmail.com // if there's no data, there's no need to look further 1795314Sstever@gmail.com if (!data) { 1805314Sstever@gmail.com return false; 1815314Sstever@gmail.com } 1825314Sstever@gmail.com 1834626Sstever@eecs.umich.edu // offset of functional request into supplied value (could be 1844626Sstever@eecs.umich.edu // negative if partial overlap) 1854626Sstever@eecs.umich.edu int offset = func_start - val_start; 1863260Ssaidi@eecs.umich.edu 1874626Sstever@eecs.umich.edu if (isRead()) { 1884626Sstever@eecs.umich.edu if (func_start >= val_start && func_end <= val_end) { 1894626Sstever@eecs.umich.edu allocate(); 1904626Sstever@eecs.umich.edu std::memcpy(getPtr<uint8_t>(), data + offset, getSize()); 1914870Sstever@eecs.umich.edu makeResponse(); 1924626Sstever@eecs.umich.edu return true; 1933260Ssaidi@eecs.umich.edu } else { 1944489Sstever@eecs.umich.edu // In this case the timing packet only partially satisfies 1954489Sstever@eecs.umich.edu // the request, so we would need more information to make 1964489Sstever@eecs.umich.edu // this work. Like bytes valid in the packet or 1974489Sstever@eecs.umich.edu // something, so the request could continue and get this 1984489Sstever@eecs.umich.edu // bit of possibly newer data along with the older data 1994489Sstever@eecs.umich.edu // not written to yet. 2004626Sstever@eecs.umich.edu panic("Memory value only partially satisfies the functional " 2014626Sstever@eecs.umich.edu "request. Now what?"); 2023260Ssaidi@eecs.umich.edu } 2034626Sstever@eecs.umich.edu } else if (isWrite()) { 2044626Sstever@eecs.umich.edu if (offset >= 0) { 2054626Sstever@eecs.umich.edu std::memcpy(data + offset, getPtr<uint8_t>(), 2064626Sstever@eecs.umich.edu (std::min(func_end, val_end) - func_start) + 1); 2074626Sstever@eecs.umich.edu } else { // val_start > func_start 2084626Sstever@eecs.umich.edu std::memcpy(data, getPtr<uint8_t>() - offset, 2094626Sstever@eecs.umich.edu (std::min(func_end, val_end) - val_start) + 1); 2103260Ssaidi@eecs.umich.edu } 2115314Sstever@gmail.com } else { 2124626Sstever@eecs.umich.edu panic("Don't know how to handle command %s\n", cmdString()); 2135314Sstever@gmail.com } 2145314Sstever@gmail.com 2155314Sstever@gmail.com // keep going with request by default 2165314Sstever@gmail.com return false; 2172641Sstever@eecs.umich.edu} 2183260Ssaidi@eecs.umich.edu 2193260Ssaidi@eecs.umich.edu 2205314Sstever@gmail.comvoid 2215314Sstever@gmail.comPacket::print(std::ostream &o, const int verbosity, 2225314Sstever@gmail.com const std::string &prefix) const 2233260Ssaidi@eecs.umich.edu{ 2245314Sstever@gmail.com ccprintf(o, "%s[%x:%x] %s\n", prefix, 2255314Sstever@gmail.com getAddr(), getAddr() + getSize() - 1, cmdString()); 2263260Ssaidi@eecs.umich.edu} 2273260Ssaidi@eecs.umich.edu 2285314Sstever@gmail.com 2295314Sstever@gmail.comPacket::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity) 2305314Sstever@gmail.com : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity) 2315314Sstever@gmail.com{ 2325314Sstever@gmail.com labelStack.push_back(LabelStackEntry("", curPrefixPtr)); 2335314Sstever@gmail.com} 2345314Sstever@gmail.com 2355314Sstever@gmail.com 2365314Sstever@gmail.comPacket::PrintReqState::~PrintReqState() 2375314Sstever@gmail.com{ 2385314Sstever@gmail.com labelStack.pop_back(); 2395314Sstever@gmail.com assert(labelStack.empty()); 2405314Sstever@gmail.com delete curPrefixPtr; 2415314Sstever@gmail.com} 2425314Sstever@gmail.com 2435314Sstever@gmail.com 2445314Sstever@gmail.comPacket::PrintReqState:: 2455314Sstever@gmail.comLabelStackEntry::LabelStackEntry(const std::string &_label, 2465314Sstever@gmail.com std::string *_prefix) 2475314Sstever@gmail.com : label(_label), prefix(_prefix), labelPrinted(false) 2485314Sstever@gmail.com{ 2495314Sstever@gmail.com} 2505314Sstever@gmail.com 2515314Sstever@gmail.com 2525314Sstever@gmail.comvoid 2535314Sstever@gmail.comPacket::PrintReqState::pushLabel(const std::string &lbl, 2545314Sstever@gmail.com const std::string &prefix) 2555314Sstever@gmail.com{ 2565314Sstever@gmail.com labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr)); 2575314Sstever@gmail.com curPrefixPtr = new std::string(*curPrefixPtr); 2585314Sstever@gmail.com *curPrefixPtr += prefix; 2595314Sstever@gmail.com} 2605314Sstever@gmail.com 2615314Sstever@gmail.comvoid 2625314Sstever@gmail.comPacket::PrintReqState::popLabel() 2635314Sstever@gmail.com{ 2645314Sstever@gmail.com delete curPrefixPtr; 2655314Sstever@gmail.com curPrefixPtr = labelStack.back().prefix; 2665314Sstever@gmail.com labelStack.pop_back(); 2675314Sstever@gmail.com assert(!labelStack.empty()); 2685314Sstever@gmail.com} 2695314Sstever@gmail.com 2705314Sstever@gmail.comvoid 2715314Sstever@gmail.comPacket::PrintReqState::printLabels() 2725314Sstever@gmail.com{ 2735314Sstever@gmail.com if (!labelStack.back().labelPrinted) { 2745314Sstever@gmail.com LabelStack::iterator i = labelStack.begin(); 2755314Sstever@gmail.com LabelStack::iterator end = labelStack.end(); 2765314Sstever@gmail.com while (i != end) { 2775314Sstever@gmail.com if (!i->labelPrinted) { 2785314Sstever@gmail.com ccprintf(os, "%s%s\n", *(i->prefix), i->label); 2795314Sstever@gmail.com i->labelPrinted = true; 2805314Sstever@gmail.com } 2815314Sstever@gmail.com i++; 2825314Sstever@gmail.com } 2835314Sstever@gmail.com } 2845314Sstever@gmail.com} 2855314Sstever@gmail.com 2865314Sstever@gmail.com 2875314Sstever@gmail.comvoid 2885314Sstever@gmail.comPacket::PrintReqState::printObj(Printable *obj) 2895314Sstever@gmail.com{ 2905314Sstever@gmail.com printLabels(); 2915314Sstever@gmail.com obj->print(os, verbosity, curPrefix()); 2925314Sstever@gmail.com} 293