packet.cc (5012:c0a28154d002) packet.cc (5314:e902f12a3af1)
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>
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"
40#include "base/misc.hh"
41#include "base/trace.hh"
42#include "mem/packet.hh"
43
44// The one downside to bitsets is that static initializers can get ugly.
45#define SET1(a1) (1 << (a1))
46#define SET2(a1, a2) (SET1(a1) | SET1(a2))
47#define SET3(a1, a2, a3) (SET2(a1, a2) | SET1(a3))
48#define SET4(a1, a2, a3, a4) (SET3(a1, a2, a3) | SET1(a4))
49#define SET5(a1, a2, a3, a4, a5) (SET4(a1, a2, a3, a4) | SET1(a5))
50#define SET6(a1, a2, a3, a4, a5, a6) (SET5(a1, a2, a3, a4, a5) | SET1(a6))
51
52const MemCmd::CommandInfo
53MemCmd::commandInfo[] =
54{
55 /* InvalidCmd */
56 { 0, InvalidCmd, "InvalidCmd" },
57 /* ReadReq */
58 { SET3(IsRead, IsRequest, NeedsResponse), ReadResp, "ReadReq" },
59 /* ReadResp */
60 { SET3(IsRead, IsResponse, HasData), InvalidCmd, "ReadResp" },
61 /* WriteReq */
62 { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
63 WriteResp, "WriteReq" },
64 /* WriteResp */
65 { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
66 /* Writeback */
67 { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
68 InvalidCmd, "Writeback" },
69 /* SoftPFReq */
70 { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
71 SoftPFResp, "SoftPFReq" },
72 /* HardPFReq */
73 { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
74 HardPFResp, "HardPFReq" },
75 /* SoftPFResp */
76 { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
77 InvalidCmd, "SoftPFResp" },
78 /* HardPFResp */
79 { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
80 InvalidCmd, "HardPFResp" },
81 /* WriteInvalidateReq */
82 { SET6(IsWrite, NeedsExclusive, IsInvalidate,
83 IsRequest, HasData, NeedsResponse),
84 WriteInvalidateResp, "WriteInvalidateReq" },
85 /* WriteInvalidateResp */
86 { SET4(IsWrite, NeedsExclusive, IsInvalidate, IsResponse),
87 InvalidCmd, "WriteInvalidateResp" },
88 /* UpgradeReq */
89 { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse),
90 UpgradeResp, "UpgradeReq" },
91 /* UpgradeResp */
92 { SET3(IsInvalidate, NeedsExclusive, IsResponse),
93 InvalidCmd, "UpgradeResp" },
94 /* ReadExReq */
95 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
96 ReadExResp, "ReadExReq" },
97 /* ReadExResp */
98 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsResponse, HasData),
99 InvalidCmd, "ReadExResp" },
100 /* LoadLockedReq */
101 { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
102 LoadLockedResp, "LoadLockedReq" },
103 /* LoadLockedResp */
104 { SET4(IsRead, IsLocked, IsResponse, HasData),
105 InvalidCmd, "LoadLockedResp" },
106 /* StoreCondReq */
107 { SET6(IsWrite, NeedsExclusive, IsLocked,
108 IsRequest, NeedsResponse, HasData),
109 StoreCondResp, "StoreCondReq" },
110 /* StoreCondResp */
111 { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
112 InvalidCmd, "StoreCondResp" },
113 /* SwapReq -- for Swap ldstub type operations */
114 { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
115 SwapResp, "SwapReq" },
116 /* SwapResp -- for Swap ldstub type operations */
117 { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
118 InvalidCmd, "SwapResp" },
119 /* NetworkNackError -- nacked at network layer (not by protocol) */
120 { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
121 /* InvalidDestError -- packet dest field invalid */
122 { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
123 /* BadAddressError -- memory address invalid */
41#include "base/misc.hh"
42#include "base/trace.hh"
43#include "mem/packet.hh"
44
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 /* WriteReq */
63 { SET5(IsWrite, NeedsExclusive, IsRequest, NeedsResponse, HasData),
64 WriteResp, "WriteReq" },
65 /* WriteResp */
66 { SET3(IsWrite, NeedsExclusive, IsResponse), InvalidCmd, "WriteResp" },
67 /* Writeback */
68 { SET4(IsWrite, NeedsExclusive, IsRequest, HasData),
69 InvalidCmd, "Writeback" },
70 /* SoftPFReq */
71 { SET4(IsRead, IsRequest, IsSWPrefetch, NeedsResponse),
72 SoftPFResp, "SoftPFReq" },
73 /* HardPFReq */
74 { SET4(IsRead, IsRequest, IsHWPrefetch, NeedsResponse),
75 HardPFResp, "HardPFReq" },
76 /* SoftPFResp */
77 { SET4(IsRead, IsResponse, IsSWPrefetch, HasData),
78 InvalidCmd, "SoftPFResp" },
79 /* HardPFResp */
80 { SET4(IsRead, IsResponse, IsHWPrefetch, HasData),
81 InvalidCmd, "HardPFResp" },
82 /* WriteInvalidateReq */
83 { SET6(IsWrite, NeedsExclusive, IsInvalidate,
84 IsRequest, HasData, NeedsResponse),
85 WriteInvalidateResp, "WriteInvalidateReq" },
86 /* WriteInvalidateResp */
87 { SET4(IsWrite, NeedsExclusive, IsInvalidate, IsResponse),
88 InvalidCmd, "WriteInvalidateResp" },
89 /* UpgradeReq */
90 { SET4(IsInvalidate, NeedsExclusive, IsRequest, NeedsResponse),
91 UpgradeResp, "UpgradeReq" },
92 /* UpgradeResp */
93 { SET3(IsInvalidate, NeedsExclusive, IsResponse),
94 InvalidCmd, "UpgradeResp" },
95 /* ReadExReq */
96 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsRequest, NeedsResponse),
97 ReadExResp, "ReadExReq" },
98 /* ReadExResp */
99 { SET5(IsRead, NeedsExclusive, IsInvalidate, IsResponse, HasData),
100 InvalidCmd, "ReadExResp" },
101 /* LoadLockedReq */
102 { SET4(IsRead, IsLocked, IsRequest, NeedsResponse),
103 LoadLockedResp, "LoadLockedReq" },
104 /* LoadLockedResp */
105 { SET4(IsRead, IsLocked, IsResponse, HasData),
106 InvalidCmd, "LoadLockedResp" },
107 /* StoreCondReq */
108 { SET6(IsWrite, NeedsExclusive, IsLocked,
109 IsRequest, NeedsResponse, HasData),
110 StoreCondResp, "StoreCondReq" },
111 /* StoreCondResp */
112 { SET4(IsWrite, NeedsExclusive, IsLocked, IsResponse),
113 InvalidCmd, "StoreCondResp" },
114 /* SwapReq -- for Swap ldstub type operations */
115 { SET6(IsRead, IsWrite, NeedsExclusive, IsRequest, HasData, NeedsResponse),
116 SwapResp, "SwapReq" },
117 /* SwapResp -- for Swap ldstub type operations */
118 { SET5(IsRead, IsWrite, NeedsExclusive, IsResponse, HasData),
119 InvalidCmd, "SwapResp" },
120 /* NetworkNackError -- nacked at network layer (not by protocol) */
121 { SET2(IsResponse, IsError), InvalidCmd, "NetworkNackError" },
122 /* InvalidDestError -- packet dest field invalid */
123 { SET2(IsResponse, IsError), InvalidCmd, "InvalidDestError" },
124 /* BadAddressError -- memory address invalid */
124 { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" }
125 { SET2(IsResponse, IsError), InvalidCmd, "BadAddressError" },
126 /* PrintReq */
127 { SET2(IsRequest, IsPrint), InvalidCmd, "PrintReq" }
125};
126
127
128/** delete the data pointed to in the data pointer. Ok to call to matter how
129 * data was allocted. */
130void
131Packet::deleteData()
132{
133 assert(staticData || dynamicData);
134 if (staticData)
135 return;
136
137 if (arrayData)
138 delete [] data;
139 else
140 delete data;
141}
142
143/** If there isn't data in the packet, allocate some. */
144void
145Packet::allocate()
146{
147 if (data)
148 return;
149 assert(!staticData);
150 dynamicData = true;
151 arrayData = true;
152 data = new uint8_t[getSize()];
153}
154
155
156bool
128};
129
130
131/** delete the data pointed to in the data pointer. Ok to call to matter how
132 * data was allocted. */
133void
134Packet::deleteData()
135{
136 assert(staticData || dynamicData);
137 if (staticData)
138 return;
139
140 if (arrayData)
141 delete [] data;
142 else
143 delete data;
144}
145
146/** If there isn't data in the packet, allocate some. */
147void
148Packet::allocate()
149{
150 if (data)
151 return;
152 assert(!staticData);
153 dynamicData = true;
154 arrayData = true;
155 data = new uint8_t[getSize()];
156}
157
158
159bool
157Packet::checkFunctional(Addr addr, int size, uint8_t *data)
160Packet::checkFunctional(Printable *obj, Addr addr, int size, uint8_t *data)
158{
159 Addr func_start = getAddr();
160 Addr func_end = getAddr() + getSize() - 1;
161 Addr val_start = addr;
162 Addr val_end = val_start + size - 1;
163
164 if (func_start > val_end || val_start > func_end) {
165 // no intersection
166 return false;
167 }
168
161{
162 Addr func_start = getAddr();
163 Addr func_end = getAddr() + getSize() - 1;
164 Addr val_start = addr;
165 Addr val_end = val_start + size - 1;
166
167 if (func_start > val_end || val_start > func_end) {
168 // no intersection
169 return false;
170 }
171
172 // check print first since it doesn't require data
173 if (isPrint()) {
174 dynamic_cast<PrintReqState*>(senderState)->printObj(obj);
175 return false;
176 }
177
178 // if there's no data, there's no need to look further
179 if (!data) {
180 return false;
181 }
182
169 // offset of functional request into supplied value (could be
170 // negative if partial overlap)
171 int offset = func_start - val_start;
172
173 if (isRead()) {
174 if (func_start >= val_start && func_end <= val_end) {
175 allocate();
176 std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
177 makeResponse();
178 return true;
179 } else {
180 // In this case the timing packet only partially satisfies
181 // the request, so we would need more information to make
182 // this work. Like bytes valid in the packet or
183 // something, so the request could continue and get this
184 // bit of possibly newer data along with the older data
185 // not written to yet.
186 panic("Memory value only partially satisfies the functional "
187 "request. Now what?");
188 }
189 } else if (isWrite()) {
190 if (offset >= 0) {
191 std::memcpy(data + offset, getPtr<uint8_t>(),
192 (std::min(func_end, val_end) - func_start) + 1);
193 } else { // val_start > func_start
194 std::memcpy(data, getPtr<uint8_t>() - offset,
195 (std::min(func_end, val_end) - val_start) + 1);
196 }
183 // offset of functional request into supplied value (could be
184 // negative if partial overlap)
185 int offset = func_start - val_start;
186
187 if (isRead()) {
188 if (func_start >= val_start && func_end <= val_end) {
189 allocate();
190 std::memcpy(getPtr<uint8_t>(), data + offset, getSize());
191 makeResponse();
192 return true;
193 } else {
194 // In this case the timing packet only partially satisfies
195 // the request, so we would need more information to make
196 // this work. Like bytes valid in the packet or
197 // something, so the request could continue and get this
198 // bit of possibly newer data along with the older data
199 // not written to yet.
200 panic("Memory value only partially satisfies the functional "
201 "request. Now what?");
202 }
203 } else if (isWrite()) {
204 if (offset >= 0) {
205 std::memcpy(data + offset, getPtr<uint8_t>(),
206 (std::min(func_end, val_end) - func_start) + 1);
207 } else { // val_start > func_start
208 std::memcpy(data, getPtr<uint8_t>() - offset,
209 (std::min(func_end, val_end) - val_start) + 1);
210 }
197 // we always want to keep going with a write
198 return false;
199 } else
211 } else {
200 panic("Don't know how to handle command %s\n", cmdString());
212 panic("Don't know how to handle command %s\n", cmdString());
213 }
214
215 // keep going with request by default
216 return false;
201}
202
203
217}
218
219
204std::ostream &
205operator<<(std::ostream &o, const Packet &p)
220void
221Packet::print(std::ostream &o, const int verbosity,
222 const std::string &prefix) const
206{
223{
224 ccprintf(o, "%s[%x:%x] %s\n", prefix,
225 getAddr(), getAddr() + getSize() - 1, cmdString());
226}
207
227
208 o << "[0x";
209 o.setf(std::ios_base::hex, std::ios_base::showbase);
210 o << p.getAddr();
211 o.unsetf(std::ios_base::hex| std::ios_base::showbase);
212 o << ":";
213 o.setf(std::ios_base::hex, std::ios_base::showbase);
214 o << p.getAddr() + p.getSize() - 1 << "] ";
215 o.unsetf(std::ios_base::hex| std::ios_base::showbase);
216
228
217 if (p.isRead())
218 o << "Read ";
219 if (p.isWrite())
220 o << "Write ";
221 if (p.isInvalidate())
222 o << "Invalidate ";
223 if (p.isRequest())
224 o << "Request ";
225 if (p.isResponse())
226 o << "Response ";
227 if (p.hasData())
228 o << "w/Data ";
229Packet::PrintReqState::PrintReqState(std::ostream &_os, int _verbosity)
230 : curPrefixPtr(new std::string("")), os(_os), verbosity(_verbosity)
231{
232 labelStack.push_back(LabelStackEntry("", curPrefixPtr));
233}
229
234
230 o << std::endl;
231 return o;
235
236Packet::PrintReqState::~PrintReqState()
237{
238 labelStack.pop_back();
239 assert(labelStack.empty());
240 delete curPrefixPtr;
232}
233
241}
242
243
244Packet::PrintReqState::
245LabelStackEntry::LabelStackEntry(const std::string &_label,
246 std::string *_prefix)
247 : label(_label), prefix(_prefix), labelPrinted(false)
248{
249}
250
251
252void
253Packet::PrintReqState::pushLabel(const std::string &lbl,
254 const std::string &prefix)
255{
256 labelStack.push_back(LabelStackEntry(lbl, curPrefixPtr));
257 curPrefixPtr = new std::string(*curPrefixPtr);
258 *curPrefixPtr += prefix;
259}
260
261void
262Packet::PrintReqState::popLabel()
263{
264 delete curPrefixPtr;
265 curPrefixPtr = labelStack.back().prefix;
266 labelStack.pop_back();
267 assert(!labelStack.empty());
268}
269
270void
271Packet::PrintReqState::printLabels()
272{
273 if (!labelStack.back().labelPrinted) {
274 LabelStack::iterator i = labelStack.begin();
275 LabelStack::iterator end = labelStack.end();
276 while (i != end) {
277 if (!i->labelPrinted) {
278 ccprintf(os, "%s%s\n", *(i->prefix), i->label);
279 i->labelPrinted = true;
280 }
281 i++;
282 }
283 }
284}
285
286
287void
288Packet::PrintReqState::printObj(Printable *obj)
289{
290 printLabels();
291 obj->print(os, verbosity, curPrefix());
292}