packet.cc revision 10883
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 /* CleanEvict */ 90 { SET2(IsWrite, IsRequest), InvalidCmd, "CleanEvict" }, 91 /* SoftPFReq */ 92 { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse), 93 SoftPFResp, "SoftPFReq" }, 94 /* HardPFReq */ 95 { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse), 96 HardPFResp, "HardPFReq" }, 97 /* SoftPFResp */ 98 { SET4(IsRead, IsResponse, IsSWPrefetch, HasData), 99 InvalidCmd, "SoftPFResp" }, 100 /* HardPFResp */ 101 { SET4(IsRead, IsResponse, IsHWPrefetch, HasData), 102 InvalidCmd, "HardPFResp" }, 103 /* WriteInvalidateReq */ 104 { SET6(IsWrite, NeedsExclusive, IsInvalidate, 105 IsRequest, HasData, NeedsResponse), 106 WriteInvalidateResp, "WriteInvalidateReq" }, 107 /* WriteInvalidateResp */ 108 { SET3(IsWrite, NeedsExclusive, IsResponse), 109 InvalidCmd, "WriteInvalidateResp" }, 110 /* UpgradeReq */ 111 { SET5(IsInvalidate, NeedsExclusive, IsUpgrade, IsRequest, NeedsResponse), 112 UpgradeResp, "UpgradeReq" }, 113 /* SCUpgradeReq: response could be UpgradeResp or UpgradeFailResp */ 114 { SET6(IsInvalidate, NeedsExclusive, IsUpgrade, IsLlsc, 115 IsRequest, NeedsResponse), 116 UpgradeResp, "SCUpgradeReq" }, 117 /* UpgradeResp */ 118 { SET3(NeedsExclusive, IsUpgrade, IsResponse), 119 InvalidCmd, "UpgradeResp" }, 120 /* SCUpgradeFailReq: generates UpgradeFailResp but still gets the data */ 121 { SET6(IsRead, NeedsExclusive, IsInvalidate, 122 IsLlsc, IsRequest, NeedsResponse), 123 UpgradeFailResp, "SCUpgradeFailReq" }, 124 /* UpgradeFailResp - Behaves like a ReadExReq, but notifies an SC 125 * that it has failed, acquires line as Dirty*/ 126 { SET4(IsRead, NeedsExclusive, IsResponse, HasData), 127 InvalidCmd, "UpgradeFailResp" }, 128 /* ReadExReq */ 129 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse), 130 ReadExResp, "ReadExReq" }, 131 /* ReadExResp */ 132 { SET4(IsRead, NeedsExclusive, IsResponse, HasData), 133 InvalidCmd, "ReadExResp" }, 134 /* LoadLockedReq: note that we use plain ReadResp as response, so that 135 * we can also use ReadRespWithInvalidate when needed */ 136 { SET4(IsRead, IsLlsc, IsRequest, NeedsResponse), 137 ReadResp, "LoadLockedReq" }, 138 /* StoreCondReq */ 139 { SET6(IsWrite, NeedsExclusive, IsLlsc, 140 IsRequest, NeedsResponse, HasData), 141 StoreCondResp, "StoreCondReq" }, 142 /* StoreCondFailReq: generates failing StoreCondResp */ 143 { SET6(IsWrite, NeedsExclusive, IsLlsc, 144 IsRequest, NeedsResponse, HasData), 145 StoreCondResp, "StoreCondFailReq" }, 146 /* StoreCondResp */ 147 { SET4(IsWrite, NeedsExclusive, IsLlsc, IsResponse), 148 InvalidCmd, "StoreCondResp" }, 149 /* SwapReq -- for Swap ldstub type operations */ 150 { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse), 151 SwapResp, "SwapReq" }, 152 /* SwapResp -- for Swap ldstub type operations */ 153 { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData), 154 InvalidCmd, "SwapResp" }, 155 /* IntReq -- for interrupts */ 156 { SET4(IsWrite, IsRequest, NeedsResponse, HasData), 157 MessageResp, "MessageReq" }, 158 /* IntResp -- for interrupts */ 159 { SET2(IsWrite, IsResponse), InvalidCmd, "MessageResp" }, 160 /* InvalidDestError -- packet dest field invalid */ 161 { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" }, 162 /* BadAddressError -- memory address invalid */ 163 { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" }, 164 /* FunctionalReadError */ 165 { SET3(IsRead, IsResponse, IsError), InvalidCmd, "FunctionalReadError" }, 166 /* FunctionalWriteError */ 167 { SET3(IsWrite, IsResponse, IsError), InvalidCmd, "FunctionalWriteError" }, 168 /* PrintReq */ 169 { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }, 170 /* Flush Request */ 171 { SET3(IsRequest, IsFlush, NeedsExclusive), InvalidCmd, "FlushReq" }, 172 /* Invalidation Request */ 173 { SET3(NeedsExclusive, IsInvalidate, IsRequest), 174 InvalidCmd, "InvalidationReq" }, 175}; 176 177bool 178Packet::checkFunctional(Printable *obj, Addr addr, bool is_secure, int size, 179 uint8_t *_data) 180{ 181 Addr func_start = getAddr(); 182 Addr func_end = getAddr() + getSize() - 1; 183 Addr val_start = addr; 184 Addr val_end = val_start + size - 1; 185 186 if (is_secure != _isSecure || func_start > val_end || 187 val_start > func_end) { 188 // no intersection 189 return false; 190 } 191 192 // check print first since it doesn't require data 193 if (isPrint()) { 194 assert(!_data); 195 safe_cast<PrintReqState*>(senderState)->printObj(obj); 196 return false; 197 } 198 199 // we allow the caller to pass NULL to signify the other packet 200 // has no data 201 if (!_data) { 202 return false; 203 } 204 205 // offset of functional request into supplied value (could be 206 // negative if partial overlap) 207 int offset = func_start - val_start; 208 209 if (isRead()) { 210 if (func_start >= val_start && func_end <= val_end) { 211 memcpy(getPtr<uint8_t>(), _data + offset, getSize()); 212 if (bytesValid.empty()) 213 bytesValid.resize(getSize(), true); 214 // complete overlap, and as the current packet is a read 215 // we are done 216 return true; 217 } else { 218 // Offsets and sizes to copy in case of partial overlap 219 int func_offset; 220 int val_offset; 221 int overlap_size; 222 223 // calculate offsets and copy sizes for the two byte arrays 224 if (val_start < func_start && val_end <= func_end) { 225 // the one we are checking against starts before and 226 // ends before or the same 227 val_offset = func_start - val_start; 228 func_offset = 0; 229 overlap_size = val_end - func_start; 230 } else if (val_start >= func_start && val_end > func_end) { 231 // the one we are checking against starts after or the 232 // same, and ends after 233 val_offset = 0; 234 func_offset = val_start - func_start; 235 overlap_size = func_end - val_start; 236 } else if (val_start >= func_start && val_end <= func_end) { 237 // the one we are checking against is completely 238 // subsumed in the current packet, possibly starting 239 // and ending at the same address 240 val_offset = 0; 241 func_offset = val_start - func_start; 242 overlap_size = size; 243 } else if (val_start < func_start && val_end > func_end) { 244 // the current packet is completely subsumed in the 245 // one we are checking against 246 val_offset = func_start - val_start; 247 func_offset = 0; 248 overlap_size = func_end - func_start; 249 } else { 250 panic("Missed a case for checkFunctional with " 251 " %s 0x%x size %d, against 0x%x size %d\n", 252 cmdString(), getAddr(), getSize(), addr, size); 253 } 254 255 // copy partial data into the packet's data array 256 uint8_t *dest = getPtr<uint8_t>() + func_offset; 257 uint8_t *src = _data + val_offset; 258 memcpy(dest, src, overlap_size); 259 260 // initialise the tracking of valid bytes if we have not 261 // used it already 262 if (bytesValid.empty()) 263 bytesValid.resize(getSize(), false); 264 265 // track if we are done filling the functional access 266 bool all_bytes_valid = true; 267 268 int i = 0; 269 270 // check up to func_offset 271 for (; all_bytes_valid && i < func_offset; ++i) 272 all_bytes_valid &= bytesValid[i]; 273 274 // update the valid bytes 275 for (i = func_offset; i < func_offset + overlap_size; ++i) 276 bytesValid[i] = true; 277 278 // check the bit after the update we just made 279 for (; all_bytes_valid && i < getSize(); ++i) 280 all_bytes_valid &= bytesValid[i]; 281 282 return all_bytes_valid; 283 } 284 } else if (isWrite()) { 285 if (offset >= 0) { 286 memcpy(_data + offset, getConstPtr<uint8_t>(), 287 (min(func_end, val_end) - func_start) + 1); 288 } else { 289 // val_start > func_start 290 memcpy(_data, getConstPtr<uint8_t>() - offset, 291 (min(func_end, val_end) - val_start) + 1); 292 } 293 } else { 294 panic("Don't know how to handle command %s\n", cmdString()); 295 } 296 297 // keep going with request by default 298 return false; 299} 300 301void 302Packet::pushSenderState(Packet::SenderState *sender_state) 303{ 304 assert(sender_state != NULL); 305 sender_state->predecessor = senderState; 306 senderState = sender_state; 307} 308 309Packet::SenderState * 310Packet::popSenderState() 311{ 312 assert(senderState != NULL); 313 SenderState *sender_state = senderState; 314 senderState = sender_state->predecessor; 315 sender_state->predecessor = NULL; 316 return sender_state; 317} 318 319void 320Packet::print(ostream &o, const int verbosity, const string &prefix) const 321{ 322 ccprintf(o, "%s[%x:%x] %s\n", prefix, 323 getAddr(), getAddr() + getSize() - 1, cmdString()); 324} 325 326std::string 327Packet::print() const { 328 ostringstream str; 329 print(str); 330 return str.str(); 331} 332 333Packet::PrintReqState::PrintReqState(ostream &_os, int _verbosity) 334 : curPrefixPtr(new string("")), os(_os), verbosity(_verbosity) 335{ 336 labelStack.push_back(LabelStackEntry("", curPrefixPtr)); 337} 338 339Packet::PrintReqState::~PrintReqState() 340{ 341 labelStack.pop_back(); 342 assert(labelStack.empty()); 343 delete curPrefixPtr; 344} 345 346Packet::PrintReqState:: 347LabelStackEntry::LabelStackEntry(const string &_label, string *_prefix) 348 : label(_label), prefix(_prefix), labelPrinted(false) 349{ 350} 351 352void 353Packet::PrintReqState::pushLabel(const string &lbl, const string &prefix) 354{ 355 labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr)); 356 curPrefixPtr = new string(*curPrefixPtr); 357 *curPrefixPtr += prefix; 358} 359 360void 361Packet::PrintReqState::popLabel() 362{ 363 delete curPrefixPtr; 364 curPrefixPtr = labelStack.back().prefix; 365 labelStack.pop_back(); 366 assert(!labelStack.empty()); 367} 368 369void 370Packet::PrintReqState::printLabels() 371{ 372 if (!labelStack.back().labelPrinted) { 373 LabelStack::iterator i = labelStack.begin(); 374 LabelStack::iterator end = labelStack.end(); 375 while (i != end) { 376 if (!i->labelPrinted) { 377 ccprintf(os, "%s%s\n", *(i->prefix), i->label); 378 i->labelPrinted = true; 379 } 380 i++; 381 } 382 } 383} 384 385 386void 387Packet::PrintReqState::printObj(Printable *obj) 388{ 389 printLabels(); 390 obj->print(os, verbosity, curPrefix()); 391} 392