packet.cc (5650:d2782c951841) packet.cc (5735:a88e8e7dec75)
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 * Steve Reinhardt
30 */
31
32/**
33 * @file
34 * Definition of the Packet Class, a packet is a transaction occuring
35 * between a single level of the memory heirarchy (ie L1->L2).
36 */
37
38#include <iostream>
39#include <cstring>
40#include "base/cprintf.hh"
41#include "base/misc.hh"
42#include "base/trace.hh"
43#include "mem/packet.hh"
44
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 * Steve Reinhardt
30 */
31
32/**
33 * @file
34 * Definition of the Packet Class, a packet is a transaction occuring
35 * between a single level of the memory heirarchy (ie L1->L2).
36 */
37
38#include <iostream>
39#include <cstring>
40#include "base/cprintf.hh"
41#include "base/misc.hh"
42#include "base/trace.hh"
43#include "mem/packet.hh"
44
45using namespace std;
46
45// The one downside to bitsets is that static initializers can get ugly.
46#define SET1(a1) (1 << (a1))
47#define SET2(a1, a2) (SET1(a1) | SET1(a2))
48#define SET3(a1, a2, a3) (SET2(a1, a2) | SET1(a3))
49#define SET4(a1, a2, a3, a4) (SET3(a1, a2, a3) | SET1(a4))
50#define SET5(a1, a2, a3, a4, a5) (SET4(a1, a2, a3, a4) | SET1(a5))
51#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
52
53const MemCmd::CommandInfo
54MemCmd::commandInfo[] =
55{
56 /* InvalidCmd */
57 { 0, InvalidCmd, "InvalidCmd" },
58 /* ReadReq */
59 { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
60 /* ReadResp */
61 { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
62 /* ReadRespWithInvalidate */
63 { SET4(IsRead, IsResponse, HasData, IsInvalidate),
64 InvalidCmd, "ReadRespWithInvalidate" },
65 /* WriteReq */
66 { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
67 WriteResp, "WriteReq" },
68 /* WriteResp */
69 { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
70 /* Writeback */
71 { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
72 InvalidCmd, "Writeback" },
73 /* SoftPFReq */
74 { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
75 SoftPFResp, "SoftPFReq" },
76 /* HardPFReq */
77 { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
78 HardPFResp, "HardPFReq" },
79 /* SoftPFResp */
80 { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
81 InvalidCmd, "SoftPFResp" },
82 /* HardPFResp */
83 { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
84 InvalidCmd, "HardPFResp" },
85 /* WriteInvalidateReq */
86 { SET6(IsWrite, NeedsExclusive, IsInvalidate,
87 IsRequest, HasData, NeedsResponse),
88 WriteInvalidateResp, "WriteInvalidateReq" },
89 /* WriteInvalidateResp */
90 { SET3(IsWrite, NeedsExclusive, IsResponse),
91 InvalidCmd, "WriteInvalidateResp" },
92 /* UpgradeReq */
93 { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse),
94 UpgradeResp, "UpgradeReq" },
95 /* UpgradeResp */
96 { SET2(NeedsExclusive, IsResponse),
97 InvalidCmd, "UpgradeResp" },
98 /* ReadExReq */
99 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
100 ReadExResp, "ReadExReq" },
101 /* ReadExResp */
102 { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
103 InvalidCmd, "ReadExResp" },
104 /* LoadLockedReq: note that we use plain ReadResp as response, so that
105 * we can also use ReadRespWithInvalidate when needed */
106 { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
107 ReadResp, "LoadLockedReq" },
108 /* StoreCondReq */
109 { SET6(IsWrite, NeedsExclusive, IsLocked,
110 IsRequest, NeedsResponse, HasData),
111 StoreCondResp, "StoreCondReq" },
112 /* StoreCondResp */
113 { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
114 InvalidCmd, "StoreCondResp" },
115 /* SwapReq -- for Swap ldstub type operations */
116 { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
117 SwapResp, "SwapReq" },
118 /* SwapResp -- for Swap ldstub type operations */
119 { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
120 InvalidCmd, "SwapResp" },
121 /* IntReq -- for interrupts */
122 { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
123 MessageReq, "MessageReq" },
124 /* IntResp -- for interrupts */
125 { SET2(IsWrite, IsResponse), MessageResp, "MessageResp" },
126 /* NetworkNackError -- nacked at network layer (not by protocol) */
127 { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
128 /* InvalidDestError -- packet dest field invalid */
129 { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
130 /* BadAddressError -- memory address invalid */
131 { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
132 /* PrintReq */
133 { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
134};
135
47// The one downside to bitsets is that static initializers can get ugly.
48#define SET1(a1) (1 << (a1))
49#define SET2(a1, a2) (SET1(a1) | SET1(a2))
50#define SET3(a1, a2, a3) (SET2(a1, a2) | SET1(a3))
51#define SET4(a1, a2, a3, a4) (SET3(a1, a2, a3) | SET1(a4))
52#define SET5(a1, a2, a3, a4, a5) (SET4(a1, a2, a3, a4) | SET1(a5))
53#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
54
55const MemCmd::CommandInfo
56MemCmd::commandInfo[] =
57{
58 /* InvalidCmd */
59 { 0, InvalidCmd, "InvalidCmd" },
60 /* ReadReq */
61 { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
62 /* ReadResp */
63 { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
64 /* ReadRespWithInvalidate */
65 { SET4(IsRead, IsResponse, HasData, IsInvalidate),
66 InvalidCmd, "ReadRespWithInvalidate" },
67 /* WriteReq */
68 { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
69 WriteResp, "WriteReq" },
70 /* WriteResp */
71 { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
72 /* Writeback */
73 { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
74 InvalidCmd, "Writeback" },
75 /* SoftPFReq */
76 { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
77 SoftPFResp, "SoftPFReq" },
78 /* HardPFReq */
79 { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
80 HardPFResp, "HardPFReq" },
81 /* SoftPFResp */
82 { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
83 InvalidCmd, "SoftPFResp" },
84 /* HardPFResp */
85 { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
86 InvalidCmd, "HardPFResp" },
87 /* WriteInvalidateReq */
88 { SET6(IsWrite, NeedsExclusive, IsInvalidate,
89 IsRequest, HasData, NeedsResponse),
90 WriteInvalidateResp, "WriteInvalidateReq" },
91 /* WriteInvalidateResp */
92 { SET3(IsWrite, NeedsExclusive, IsResponse),
93 InvalidCmd, "WriteInvalidateResp" },
94 /* UpgradeReq */
95 { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse),
96 UpgradeResp, "UpgradeReq" },
97 /* UpgradeResp */
98 { SET2(NeedsExclusive, IsResponse),
99 InvalidCmd, "UpgradeResp" },
100 /* ReadExReq */
101 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
102 ReadExResp, "ReadExReq" },
103 /* ReadExResp */
104 { SET4(IsRead, NeedsExclusive, IsResponse, HasData),
105 InvalidCmd, "ReadExResp" },
106 /* LoadLockedReq: note that we use plain ReadResp as response, so that
107 * we can also use ReadRespWithInvalidate when needed */
108 { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
109 ReadResp, "LoadLockedReq" },
110 /* StoreCondReq */
111 { SET6(IsWrite, NeedsExclusive, IsLocked,
112 IsRequest, NeedsResponse, HasData),
113 StoreCondResp, "StoreCondReq" },
114 /* StoreCondResp */
115 { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
116 InvalidCmd, "StoreCondResp" },
117 /* SwapReq -- for Swap ldstub type operations */
118 { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
119 SwapResp, "SwapReq" },
120 /* SwapResp -- for Swap ldstub type operations */
121 { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
122 InvalidCmd, "SwapResp" },
123 /* IntReq -- for interrupts */
124 { SET4(IsWrite, IsRequest, NeedsResponse, HasData),
125 MessageReq, "MessageReq" },
126 /* IntResp -- for interrupts */
127 { SET2(IsWrite, IsResponse), MessageResp, "MessageResp" },
128 /* NetworkNackError -- nacked at network layer (not by protocol) */
129 { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
130 /* InvalidDestError -- packet dest field invalid */
131 { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
132 /* BadAddressError -- memory address invalid */
133 { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
134 /* PrintReq */
135 { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
136};
137
136
137/** delete the data pointed to in the data pointer. Ok to call to matter how
138 * data was allocted. */
139void
140Packet::deleteData()
141{
142 assert(staticData || dynamicData);
143 if (staticData)
144 return;
145
146 if (arrayData)
147 delete [] data;
148 else
149 delete data;
150}
151
152/** If there isn't data in the packet, allocate some. */
153void
154Packet::allocate()
155{
156 if (data)
157 return;
158 assert(!staticData);
159 dynamicData = true;
160 arrayData = true;
161 data = new uint8_t[getSize()];
162}
163
164
165bool
166Packet::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
167{
168 Addr func_start = getAddr();
169 Addr func_end = getAddr() + getSize() - 1;
170 Addr val_start = addr;
171 Addr val_end = val_start + size - 1;
172
173 if (func_start > val_end || val_start > func_end) {
174 // no intersection
175 return false;
176 }
177
178 // check print first since it doesn't require data
179 if (isPrint()) {
180 dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
181 return false;
182 }
183
184 // if there's no data, there's no need to look further
185 if (!data) {
186 return false;
187 }
188
189 // offset of functional request into supplied value (could be
190 // negative if partial overlap)
191 int offset = func_start - val_start;
192
193 if (isRead()) {
194 if (func_start >= val_start && func_end <= val_end) {
195 allocate();
138bool
139Packet::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
140{
141 Addr func_start = getAddr();
142 Addr func_end = getAddr() + getSize() - 1;
143 Addr val_start = addr;
144 Addr val_end = val_start + size - 1;
145
146 if (func_start > val_end || val_start > func_end) {
147 // no intersection
148 return false;
149 }
150
151 // check print first since it doesn't require data
152 if (isPrint()) {
153 dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
154 return false;
155 }
156
157 // if there's no data, there's no need to look further
158 if (!data) {
159 return false;
160 }
161
162 // offset of functional request into supplied value (could be
163 // negative if partial overlap)
164 int offset = func_start - val_start;
165
166 if (isRead()) {
167 if (func_start >= val_start && func_end <= val_end) {
168 allocate();
196 std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
169 memcpy(getPtr(), data + offset, getSize());
197 makeResponse();
198 return true;
199 } else {
200 // In this case the timing packet only partially satisfies
201 // the request, so we would need more information to make
202 // this work. Like bytes valid in the packet or
203 // something, so the request could continue and get this
204 // bit of possibly newer data along with the older data
205 // not written to yet.
206 panic("Memory value only partially satisfies the functional "
207 "request. Now what?");
208 }
209 } else if (isWrite()) {
210 if (offset >= 0) {
170 makeResponse();
171 return true;
172 } else {
173 // In this case the timing packet only partially satisfies
174 // the request, so we would need more information to make
175 // this work. Like bytes valid in the packet or
176 // something, so the request could continue and get this
177 // bit of possibly newer data along with the older data
178 // not written to yet.
179 panic("Memory value only partially satisfies the functional "
180 "request. Now what?");
181 }
182 } else if (isWrite()) {
183 if (offset >= 0) {
211 std::memcpy(data + offset, getPtr<uint8_t>(),
212 (std::min(func_end, val_end) - func_start) + 1);
213 } else { // val_start > func_start
214 std::memcpy(data, getPtr<uint8_t>() - offset,
215 (std::min(func_end, val_end) - val_start) + 1);
184 memcpy(data + offset, getPtr(),
185 (min(func_end, val_end) - func_start) + 1);
186 } else {
187 // val_start > func_start
188 memcpy(data, getPtr<uint8_t>() - offset,
189 (min(func_end, val_end) - val_start) + 1);
216 }
217 } else {
218 panic("Don't know how to handle command %s\n", cmdString());
219 }
220
221 // keep going with request by default
222 return false;
223}
224
190 }
191 } else {
192 panic("Don't know how to handle command %s\n", cmdString());
193 }
194
195 // keep going with request by default
196 return false;
197}
198
225
226void
199void
227Packet::print(std::ostream &o, const int verbosity,
228 const std::string &prefix) const
200Packet::print(ostream &o, const int verbosity, const string &prefix) const
229{
230 ccprintf(o, "%s[%x:%x] %s\n", prefix,
231 getAddr(), getAddr() + getSize() - 1, cmdString());
232}
233
201{
202 ccprintf(o, "%s[%x:%x] %s\n", prefix,
203 getAddr(), getAddr() + getSize() - 1, cmdString());
204}
205
234
235Packet::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity)
236 : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity)
206Packet::PrintReqState::PrintReqState(ostream &_os, int _verbosity)
207 : curPrefixPtr(new string("")), os(_os), verbosity(_verbosity)
237{
238 labelStack.push_back(LabelStackEntry("", curPrefixPtr));
239}
240
208{
209 labelStack.push_back(LabelStackEntry("", curPrefixPtr));
210}
211
241
242Packet::PrintReqState::~PrintReqState()
243{
244 labelStack.pop_back();
245 assert(labelStack.empty());
246 delete curPrefixPtr;
247}
248
212Packet::PrintReqState::~PrintReqState()
213{
214 labelStack.pop_back();
215 assert(labelStack.empty());
216 delete curPrefixPtr;
217}
218
249
250Packet::PrintReqState::
219Packet::PrintReqState::
251LabelStackEntry::LabelStackEntry(const std::string &_label,
252 std::string *_prefix)
220LabelStackEntry::LabelStackEntry(const string &_label, string *_prefix)
253 : label(_label), prefix(_prefix), labelPrinted(false)
254{
255}
256
221 : label(_label), prefix(_prefix), labelPrinted(false)
222{
223}
224
257
258void
225void
259Packet::PrintReqState::pushLabel(const std::string &lbl,
260 const std::string &prefix)
226Packet::PrintReqState::pushLabel(const string &lbl, const string &prefix)
261{
262 labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
227{
228 labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
263 curPrefixPtr = new std::string(*curPrefixPtr);
229 curPrefixPtr = new string(*curPrefixPtr);
264 *curPrefixPtr += prefix;
265}
266
267void
268Packet::PrintReqState::popLabel()
269{
270 delete curPrefixPtr;
271 curPrefixPtr = labelStack.back().prefix;
272 labelStack.pop_back();
273 assert(!labelStack.empty());
274}
275
276void
277Packet::PrintReqState::printLabels()
278{
279 if (!labelStack.back().labelPrinted) {
280 LabelStack::iterator i = labelStack.begin();
281 LabelStack::iterator end = labelStack.end();
282 while (i != end) {
283 if (!i->labelPrinted) {
284 ccprintf(os, "%s%s\n", *(i->prefix), i->label);
285 i->labelPrinted = true;
286 }
287 i++;
288 }
289 }
290}
291
292
293void
294Packet::PrintReqState::printObj(Printable *obj)
295{
296 printLabels();
297 obj->print(os, verbosity, curPrefix());
298}
230 *curPrefixPtr += prefix;
231}
232
233void
234Packet::PrintReqState::popLabel()
235{
236 delete curPrefixPtr;
237 curPrefixPtr = labelStack.back().prefix;
238 labelStack.pop_back();
239 assert(!labelStack.empty());
240}
241
242void
243Packet::PrintReqState::printLabels()
244{
245 if (!labelStack.back().labelPrinted) {
246 LabelStack::iterator i = labelStack.begin();
247 LabelStack::iterator end = labelStack.end();
248 while (i != end) {
249 if (!i->labelPrinted) {
250 ccprintf(os, "%s%s\n", *(i->prefix), i->label);
251 i->labelPrinted = true;
252 }
253 i++;
254 }
255 }
256}
257
258
259void
260Packet::PrintReqState::printObj(Printable *obj)
261{
262 printLabels();
263 obj->print(os, verbosity, curPrefix());
264}