timing.cc (3172:2c84db071850) timing.cc (3184:8edaf4539e05)
1/*
2 * Copyright (c) 2002-2005 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: Steve Reinhardt
29 */
30
31#include "arch/locked_mem.hh"
32#include "arch/utility.hh"
33#include "cpu/exetrace.hh"
34#include "cpu/simple/timing.hh"
35#include "mem/packet_impl.hh"
36#include "sim/builder.hh"
37#include "sim/system.hh"
38
39using namespace std;
40using namespace TheISA;
41
42Port *
43TimingSimpleCPU::getPort(const std::string &if_name, int idx)
44{
45 if (if_name == "dcache_port")
46 return &dcachePort;
47 else if (if_name == "icache_port")
48 return &icachePort;
49 else
50 panic("No Such Port\n");
51}
52
53void
54TimingSimpleCPU::init()
55{
56 BaseCPU::init();
57#if FULL_SYSTEM
58 for (int i = 0; i < threadContexts.size(); ++i) {
59 ThreadContext *tc = threadContexts[i];
60
61 // initialize CPU, including PC
62 TheISA::initCPU(tc, tc->readCpuId());
63 }
64#endif
65}
66
67Tick
68TimingSimpleCPU::CpuPort::recvAtomic(Packet *pkt)
69{
70 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
71 return curTick;
72}
73
74void
75TimingSimpleCPU::CpuPort::recvFunctional(Packet *pkt)
76{
1/*
2 * Copyright (c) 2002-2005 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: Steve Reinhardt
29 */
30
31#include "arch/locked_mem.hh"
32#include "arch/utility.hh"
33#include "cpu/exetrace.hh"
34#include "cpu/simple/timing.hh"
35#include "mem/packet_impl.hh"
36#include "sim/builder.hh"
37#include "sim/system.hh"
38
39using namespace std;
40using namespace TheISA;
41
42Port *
43TimingSimpleCPU::getPort(const std::string &if_name, int idx)
44{
45 if (if_name == "dcache_port")
46 return &dcachePort;
47 else if (if_name == "icache_port")
48 return &icachePort;
49 else
50 panic("No Such Port\n");
51}
52
53void
54TimingSimpleCPU::init()
55{
56 BaseCPU::init();
57#if FULL_SYSTEM
58 for (int i = 0; i < threadContexts.size(); ++i) {
59 ThreadContext *tc = threadContexts[i];
60
61 // initialize CPU, including PC
62 TheISA::initCPU(tc, tc->readCpuId());
63 }
64#endif
65}
66
67Tick
68TimingSimpleCPU::CpuPort::recvAtomic(Packet *pkt)
69{
70 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
71 return curTick;
72}
73
74void
75TimingSimpleCPU::CpuPort::recvFunctional(Packet *pkt)
76{
77 panic("TimingSimpleCPU doesn't expect recvFunctional callback!");
77 //No internal storage to update, jusst return
78 return;
78}
79
80void
81TimingSimpleCPU::CpuPort::recvStatusChange(Status status)
82{
83 if (status == RangeChange)
84 return;
85
86 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
87}
88
89
90void
91TimingSimpleCPU::CpuPort::TickEvent::schedule(Packet *_pkt, Tick t)
92{
93 pkt = _pkt;
94 Event::schedule(t);
95}
96
97TimingSimpleCPU::TimingSimpleCPU(Params *p)
98 : BaseSimpleCPU(p), icachePort(this, p->clock), dcachePort(this, p->clock),
99 cpu_id(p->cpu_id)
100{
101 _status = Idle;
102 ifetch_pkt = dcache_pkt = NULL;
103 drainEvent = NULL;
104 fetchEvent = NULL;
105 changeState(SimObject::Running);
106}
107
108
109TimingSimpleCPU::~TimingSimpleCPU()
110{
111}
112
113void
114TimingSimpleCPU::serialize(ostream &os)
115{
116 SimObject::State so_state = SimObject::getState();
117 SERIALIZE_ENUM(so_state);
118 BaseSimpleCPU::serialize(os);
119}
120
121void
122TimingSimpleCPU::unserialize(Checkpoint *cp, const string &section)
123{
124 SimObject::State so_state;
125 UNSERIALIZE_ENUM(so_state);
126 BaseSimpleCPU::unserialize(cp, section);
127}
128
129unsigned int
130TimingSimpleCPU::drain(Event *drain_event)
131{
132 // TimingSimpleCPU is ready to drain if it's not waiting for
133 // an access to complete.
134 if (status() == Idle || status() == Running || status() == SwitchedOut) {
135 changeState(SimObject::Drained);
136 return 0;
137 } else {
138 changeState(SimObject::Draining);
139 drainEvent = drain_event;
140 return 1;
141 }
142}
143
144void
145TimingSimpleCPU::resume()
146{
147 if (_status != SwitchedOut && _status != Idle) {
148 // Delete the old event if it existed.
149 if (fetchEvent) {
150 if (fetchEvent->scheduled())
151 fetchEvent->deschedule();
152
153 delete fetchEvent;
154 }
155
156 fetchEvent =
157 new EventWrapper<TimingSimpleCPU, &TimingSimpleCPU::fetch>(this, false);
158 fetchEvent->schedule(curTick);
159 }
160
161 assert(system->getMemoryMode() == System::Timing);
162 changeState(SimObject::Running);
163}
164
165void
166TimingSimpleCPU::switchOut()
167{
168 assert(status() == Running || status() == Idle);
169 _status = SwitchedOut;
170
171 // If we've been scheduled to resume but are then told to switch out,
172 // we'll need to cancel it.
173 if (fetchEvent && fetchEvent->scheduled())
174 fetchEvent->deschedule();
175}
176
177
178void
179TimingSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
180{
181 BaseCPU::takeOverFrom(oldCPU);
182
183 // if any of this CPU's ThreadContexts are active, mark the CPU as
184 // running and schedule its tick event.
185 for (int i = 0; i < threadContexts.size(); ++i) {
186 ThreadContext *tc = threadContexts[i];
187 if (tc->status() == ThreadContext::Active && _status != Running) {
188 _status = Running;
189 break;
190 }
191 }
192}
193
194
195void
196TimingSimpleCPU::activateContext(int thread_num, int delay)
197{
198 assert(thread_num == 0);
199 assert(thread);
200
201 assert(_status == Idle);
202
203 notIdleFraction++;
204 _status = Running;
205 // kick things off by initiating the fetch of the next instruction
206 fetchEvent =
207 new EventWrapper<TimingSimpleCPU, &TimingSimpleCPU::fetch>(this, false);
208 fetchEvent->schedule(curTick + cycles(delay));
209}
210
211
212void
213TimingSimpleCPU::suspendContext(int thread_num)
214{
215 assert(thread_num == 0);
216 assert(thread);
217
218 assert(_status == Running);
219
220 // just change status to Idle... if status != Running,
221 // completeInst() will not initiate fetch of next instruction.
222
223 notIdleFraction--;
224 _status = Idle;
225}
226
227
228template <class T>
229Fault
230TimingSimpleCPU::read(Addr addr, T &data, unsigned flags)
231{
232 Request *req =
233 new Request(/* asid */ 0, addr, sizeof(T), flags, thread->readPC(),
234 cpu_id, /* thread ID */ 0);
235
236 if (traceData) {
237 traceData->setAddr(req->getVaddr());
238 }
239
240 // translate to physical address
241 Fault fault = thread->translateDataReadReq(req);
242
243 // Now do the access.
244 if (fault == NoFault) {
245 Packet *pkt =
246 new Packet(req, Packet::ReadReq, Packet::Broadcast);
247 pkt->dataDynamic<T>(new T);
248
249 if (!dcachePort.sendTiming(pkt)) {
250 _status = DcacheRetry;
251 dcache_pkt = pkt;
252 } else {
253 _status = DcacheWaitResponse;
254 // memory system takes ownership of packet
255 dcache_pkt = NULL;
256 }
257 }
258
259 // This will need a new way to tell if it has a dcache attached.
260 if (req->isUncacheable())
261 recordEvent("Uncached Read");
262
263 return fault;
264}
265
266#ifndef DOXYGEN_SHOULD_SKIP_THIS
267
268template
269Fault
270TimingSimpleCPU::read(Addr addr, uint64_t &data, unsigned flags);
271
272template
273Fault
274TimingSimpleCPU::read(Addr addr, uint32_t &data, unsigned flags);
275
276template
277Fault
278TimingSimpleCPU::read(Addr addr, uint16_t &data, unsigned flags);
279
280template
281Fault
282TimingSimpleCPU::read(Addr addr, uint8_t &data, unsigned flags);
283
284#endif //DOXYGEN_SHOULD_SKIP_THIS
285
286template<>
287Fault
288TimingSimpleCPU::read(Addr addr, double &data, unsigned flags)
289{
290 return read(addr, *(uint64_t*)&data, flags);
291}
292
293template<>
294Fault
295TimingSimpleCPU::read(Addr addr, float &data, unsigned flags)
296{
297 return read(addr, *(uint32_t*)&data, flags);
298}
299
300
301template<>
302Fault
303TimingSimpleCPU::read(Addr addr, int32_t &data, unsigned flags)
304{
305 return read(addr, (uint32_t&)data, flags);
306}
307
308
309template <class T>
310Fault
311TimingSimpleCPU::write(T data, Addr addr, unsigned flags, uint64_t *res)
312{
313 Request *req =
314 new Request(/* asid */ 0, addr, sizeof(T), flags, thread->readPC(),
315 cpu_id, /* thread ID */ 0);
316
317 // translate to physical address
318 Fault fault = thread->translateDataWriteReq(req);
319
320 // Now do the access.
321 if (fault == NoFault) {
322 assert(dcache_pkt == NULL);
323 dcache_pkt = new Packet(req, Packet::WriteReq, Packet::Broadcast);
324 dcache_pkt->allocate();
325 dcache_pkt->set(data);
326
327 bool do_access = true; // flag to suppress cache access
328
329 if (req->isLocked()) {
330 do_access = TheISA::handleLockedWrite(thread, req);
331 }
332
333 if (do_access) {
334 if (!dcachePort.sendTiming(dcache_pkt)) {
335 _status = DcacheRetry;
336 } else {
337 _status = DcacheWaitResponse;
338 // memory system takes ownership of packet
339 dcache_pkt = NULL;
340 }
341 }
342 }
343
344 // This will need a new way to tell if it's hooked up to a cache or not.
345 if (req->isUncacheable())
346 recordEvent("Uncached Write");
347
348 // If the write needs to have a fault on the access, consider calling
349 // changeStatus() and changing it to "bad addr write" or something.
350 return fault;
351}
352
353
354#ifndef DOXYGEN_SHOULD_SKIP_THIS
355template
356Fault
357TimingSimpleCPU::write(uint64_t data, Addr addr,
358 unsigned flags, uint64_t *res);
359
360template
361Fault
362TimingSimpleCPU::write(uint32_t data, Addr addr,
363 unsigned flags, uint64_t *res);
364
365template
366Fault
367TimingSimpleCPU::write(uint16_t data, Addr addr,
368 unsigned flags, uint64_t *res);
369
370template
371Fault
372TimingSimpleCPU::write(uint8_t data, Addr addr,
373 unsigned flags, uint64_t *res);
374
375#endif //DOXYGEN_SHOULD_SKIP_THIS
376
377template<>
378Fault
379TimingSimpleCPU::write(double data, Addr addr, unsigned flags, uint64_t *res)
380{
381 return write(*(uint64_t*)&data, addr, flags, res);
382}
383
384template<>
385Fault
386TimingSimpleCPU::write(float data, Addr addr, unsigned flags, uint64_t *res)
387{
388 return write(*(uint32_t*)&data, addr, flags, res);
389}
390
391
392template<>
393Fault
394TimingSimpleCPU::write(int32_t data, Addr addr, unsigned flags, uint64_t *res)
395{
396 return write((uint32_t)data, addr, flags, res);
397}
398
399
400void
401TimingSimpleCPU::fetch()
402{
403 checkForInterrupts();
404
405 Request *ifetch_req = new Request();
406 ifetch_req->setThreadContext(cpu_id, /* thread ID */ 0);
407 Fault fault = setupFetchRequest(ifetch_req);
408
409 ifetch_pkt = new Packet(ifetch_req, Packet::ReadReq, Packet::Broadcast);
410 ifetch_pkt->dataStatic(&inst);
411
412 if (fault == NoFault) {
413 if (!icachePort.sendTiming(ifetch_pkt)) {
414 // Need to wait for retry
415 _status = IcacheRetry;
416 } else {
417 // Need to wait for cache to respond
418 _status = IcacheWaitResponse;
419 // ownership of packet transferred to memory system
420 ifetch_pkt = NULL;
421 }
422 } else {
423 // fetch fault: advance directly to next instruction (fault handler)
424 advanceInst(fault);
425 }
426}
427
428
429void
430TimingSimpleCPU::advanceInst(Fault fault)
431{
432 advancePC(fault);
433
434 if (_status == Running) {
435 // kick off fetch of next instruction... callback from icache
436 // response will cause that instruction to be executed,
437 // keeping the CPU running.
438 fetch();
439 }
440}
441
442
443void
444TimingSimpleCPU::completeIfetch(Packet *pkt)
445{
446 // received a response from the icache: execute the received
447 // instruction
448 assert(pkt->result == Packet::Success);
449 assert(_status == IcacheWaitResponse);
450
451 _status = Running;
452
453 delete pkt->req;
454 delete pkt;
455
456 if (getState() == SimObject::Draining) {
457 completeDrain();
458 return;
459 }
460
461 preExecute();
462 if (curStaticInst->isMemRef() && !curStaticInst->isDataPrefetch()) {
463 // load or store: just send to dcache
464 Fault fault = curStaticInst->initiateAcc(this, traceData);
465 if (_status != Running) {
466 // instruction will complete in dcache response callback
467 assert(_status == DcacheWaitResponse || _status == DcacheRetry);
468 assert(fault == NoFault);
469 } else {
470 if (fault == NoFault) {
471 // early fail on store conditional: complete now
472 assert(dcache_pkt != NULL);
473 fault = curStaticInst->completeAcc(dcache_pkt, this,
474 traceData);
475 delete dcache_pkt->req;
476 delete dcache_pkt;
477 dcache_pkt = NULL;
478 }
479 postExecute();
480 advanceInst(fault);
481 }
482 } else {
483 // non-memory instruction: execute completely now
484 Fault fault = curStaticInst->execute(this, traceData);
485 postExecute();
486 advanceInst(fault);
487 }
488}
489
490void
491TimingSimpleCPU::IcachePort::ITickEvent::process()
492{
493 cpu->completeIfetch(pkt);
494}
495
496bool
497TimingSimpleCPU::IcachePort::recvTiming(Packet *pkt)
498{
499 // delay processing of returned data until next CPU clock edge
500 Tick time = pkt->req->getTime();
501 while (time < curTick)
502 time += lat;
503
504 if (time == curTick)
505 cpu->completeIfetch(pkt);
506 else
507 tickEvent.schedule(pkt, time);
508
509 return true;
510}
511
512void
513TimingSimpleCPU::IcachePort::recvRetry()
514{
515 // we shouldn't get a retry unless we have a packet that we're
516 // waiting to transmit
517 assert(cpu->ifetch_pkt != NULL);
518 assert(cpu->_status == IcacheRetry);
519 Packet *tmp = cpu->ifetch_pkt;
520 if (sendTiming(tmp)) {
521 cpu->_status = IcacheWaitResponse;
522 cpu->ifetch_pkt = NULL;
523 }
524}
525
526void
527TimingSimpleCPU::completeDataAccess(Packet *pkt)
528{
529 // received a response from the dcache: complete the load or store
530 // instruction
531 assert(pkt->result == Packet::Success);
532 assert(_status == DcacheWaitResponse);
533 _status = Running;
534
535 if (getState() == SimObject::Draining) {
536 completeDrain();
537
538 delete pkt->req;
539 delete pkt;
540
541 return;
542 }
543
544 Fault fault = curStaticInst->completeAcc(pkt, this, traceData);
545
546 if (pkt->isRead() && pkt->req->isLocked()) {
547 TheISA::handleLockedRead(thread, pkt->req);
548 }
549
550 delete pkt->req;
551 delete pkt;
552
553 postExecute();
554 advanceInst(fault);
555}
556
557
558void
559TimingSimpleCPU::completeDrain()
560{
561 DPRINTF(Config, "Done draining\n");
562 changeState(SimObject::Drained);
563 drainEvent->process();
564}
565
566bool
567TimingSimpleCPU::DcachePort::recvTiming(Packet *pkt)
568{
569 // delay processing of returned data until next CPU clock edge
570 Tick time = pkt->req->getTime();
571 while (time < curTick)
572 time += lat;
573
574 if (time == curTick)
575 cpu->completeDataAccess(pkt);
576 else
577 tickEvent.schedule(pkt, time);
578
579 return true;
580}
581
582void
583TimingSimpleCPU::DcachePort::DTickEvent::process()
584{
585 cpu->completeDataAccess(pkt);
586}
587
588void
589TimingSimpleCPU::DcachePort::recvRetry()
590{
591 // we shouldn't get a retry unless we have a packet that we're
592 // waiting to transmit
593 assert(cpu->dcache_pkt != NULL);
594 assert(cpu->_status == DcacheRetry);
595 Packet *tmp = cpu->dcache_pkt;
596 if (sendTiming(tmp)) {
597 cpu->_status = DcacheWaitResponse;
598 // memory system takes ownership of packet
599 cpu->dcache_pkt = NULL;
600 }
601}
602
603
604////////////////////////////////////////////////////////////////////////
605//
606// TimingSimpleCPU Simulation Object
607//
608BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU)
609
610 Param<Counter> max_insts_any_thread;
611 Param<Counter> max_insts_all_threads;
612 Param<Counter> max_loads_any_thread;
613 Param<Counter> max_loads_all_threads;
614 Param<Tick> progress_interval;
615 SimObjectParam<MemObject *> mem;
616 SimObjectParam<System *> system;
617 Param<int> cpu_id;
618
619#if FULL_SYSTEM
620 SimObjectParam<AlphaITB *> itb;
621 SimObjectParam<AlphaDTB *> dtb;
622 Param<Tick> profile;
623#else
624 SimObjectParam<Process *> workload;
625#endif // FULL_SYSTEM
626
627 Param<int> clock;
628
629 Param<bool> defer_registration;
630 Param<int> width;
631 Param<bool> function_trace;
632 Param<Tick> function_trace_start;
633 Param<bool> simulate_stalls;
634
635END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU)
636
637BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU)
638
639 INIT_PARAM(max_insts_any_thread,
640 "terminate when any thread reaches this inst count"),
641 INIT_PARAM(max_insts_all_threads,
642 "terminate when all threads have reached this inst count"),
643 INIT_PARAM(max_loads_any_thread,
644 "terminate when any thread reaches this load count"),
645 INIT_PARAM(max_loads_all_threads,
646 "terminate when all threads have reached this load count"),
647 INIT_PARAM(progress_interval, "Progress interval"),
648 INIT_PARAM(mem, "memory"),
649 INIT_PARAM(system, "system object"),
650 INIT_PARAM(cpu_id, "processor ID"),
651
652#if FULL_SYSTEM
653 INIT_PARAM(itb, "Instruction TLB"),
654 INIT_PARAM(dtb, "Data TLB"),
655 INIT_PARAM(profile, ""),
656#else
657 INIT_PARAM(workload, "processes to run"),
658#endif // FULL_SYSTEM
659
660 INIT_PARAM(clock, "clock speed"),
661 INIT_PARAM(defer_registration, "defer system registration (for sampling)"),
662 INIT_PARAM(width, "cpu width"),
663 INIT_PARAM(function_trace, "Enable function trace"),
664 INIT_PARAM(function_trace_start, "Cycle to start function trace"),
665 INIT_PARAM(simulate_stalls, "Simulate cache stall cycles")
666
667END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU)
668
669
670CREATE_SIM_OBJECT(TimingSimpleCPU)
671{
672 TimingSimpleCPU::Params *params = new TimingSimpleCPU::Params();
673 params->name = getInstanceName();
674 params->numberOfThreads = 1;
675 params->max_insts_any_thread = max_insts_any_thread;
676 params->max_insts_all_threads = max_insts_all_threads;
677 params->max_loads_any_thread = max_loads_any_thread;
678 params->max_loads_all_threads = max_loads_all_threads;
679 params->progress_interval = progress_interval;
680 params->deferRegistration = defer_registration;
681 params->clock = clock;
682 params->functionTrace = function_trace;
683 params->functionTraceStart = function_trace_start;
684 params->mem = mem;
685 params->system = system;
686 params->cpu_id = cpu_id;
687
688#if FULL_SYSTEM
689 params->itb = itb;
690 params->dtb = dtb;
691 params->profile = profile;
692#else
693 params->process = workload;
694#endif
695
696 TimingSimpleCPU *cpu = new TimingSimpleCPU(params);
697 return cpu;
698}
699
700REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU)
701
79}
80
81void
82TimingSimpleCPU::CpuPort::recvStatusChange(Status status)
83{
84 if (status == RangeChange)
85 return;
86
87 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
88}
89
90
91void
92TimingSimpleCPU::CpuPort::TickEvent::schedule(Packet *_pkt, Tick t)
93{
94 pkt = _pkt;
95 Event::schedule(t);
96}
97
98TimingSimpleCPU::TimingSimpleCPU(Params *p)
99 : BaseSimpleCPU(p), icachePort(this, p->clock), dcachePort(this, p->clock),
100 cpu_id(p->cpu_id)
101{
102 _status = Idle;
103 ifetch_pkt = dcache_pkt = NULL;
104 drainEvent = NULL;
105 fetchEvent = NULL;
106 changeState(SimObject::Running);
107}
108
109
110TimingSimpleCPU::~TimingSimpleCPU()
111{
112}
113
114void
115TimingSimpleCPU::serialize(ostream &os)
116{
117 SimObject::State so_state = SimObject::getState();
118 SERIALIZE_ENUM(so_state);
119 BaseSimpleCPU::serialize(os);
120}
121
122void
123TimingSimpleCPU::unserialize(Checkpoint *cp, const string &section)
124{
125 SimObject::State so_state;
126 UNSERIALIZE_ENUM(so_state);
127 BaseSimpleCPU::unserialize(cp, section);
128}
129
130unsigned int
131TimingSimpleCPU::drain(Event *drain_event)
132{
133 // TimingSimpleCPU is ready to drain if it's not waiting for
134 // an access to complete.
135 if (status() == Idle || status() == Running || status() == SwitchedOut) {
136 changeState(SimObject::Drained);
137 return 0;
138 } else {
139 changeState(SimObject::Draining);
140 drainEvent = drain_event;
141 return 1;
142 }
143}
144
145void
146TimingSimpleCPU::resume()
147{
148 if (_status != SwitchedOut && _status != Idle) {
149 // Delete the old event if it existed.
150 if (fetchEvent) {
151 if (fetchEvent->scheduled())
152 fetchEvent->deschedule();
153
154 delete fetchEvent;
155 }
156
157 fetchEvent =
158 new EventWrapper<TimingSimpleCPU, &TimingSimpleCPU::fetch>(this, false);
159 fetchEvent->schedule(curTick);
160 }
161
162 assert(system->getMemoryMode() == System::Timing);
163 changeState(SimObject::Running);
164}
165
166void
167TimingSimpleCPU::switchOut()
168{
169 assert(status() == Running || status() == Idle);
170 _status = SwitchedOut;
171
172 // If we've been scheduled to resume but are then told to switch out,
173 // we'll need to cancel it.
174 if (fetchEvent && fetchEvent->scheduled())
175 fetchEvent->deschedule();
176}
177
178
179void
180TimingSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
181{
182 BaseCPU::takeOverFrom(oldCPU);
183
184 // if any of this CPU's ThreadContexts are active, mark the CPU as
185 // running and schedule its tick event.
186 for (int i = 0; i < threadContexts.size(); ++i) {
187 ThreadContext *tc = threadContexts[i];
188 if (tc->status() == ThreadContext::Active && _status != Running) {
189 _status = Running;
190 break;
191 }
192 }
193}
194
195
196void
197TimingSimpleCPU::activateContext(int thread_num, int delay)
198{
199 assert(thread_num == 0);
200 assert(thread);
201
202 assert(_status == Idle);
203
204 notIdleFraction++;
205 _status = Running;
206 // kick things off by initiating the fetch of the next instruction
207 fetchEvent =
208 new EventWrapper<TimingSimpleCPU, &TimingSimpleCPU::fetch>(this, false);
209 fetchEvent->schedule(curTick + cycles(delay));
210}
211
212
213void
214TimingSimpleCPU::suspendContext(int thread_num)
215{
216 assert(thread_num == 0);
217 assert(thread);
218
219 assert(_status == Running);
220
221 // just change status to Idle... if status != Running,
222 // completeInst() will not initiate fetch of next instruction.
223
224 notIdleFraction--;
225 _status = Idle;
226}
227
228
229template <class T>
230Fault
231TimingSimpleCPU::read(Addr addr, T &data, unsigned flags)
232{
233 Request *req =
234 new Request(/* asid */ 0, addr, sizeof(T), flags, thread->readPC(),
235 cpu_id, /* thread ID */ 0);
236
237 if (traceData) {
238 traceData->setAddr(req->getVaddr());
239 }
240
241 // translate to physical address
242 Fault fault = thread->translateDataReadReq(req);
243
244 // Now do the access.
245 if (fault == NoFault) {
246 Packet *pkt =
247 new Packet(req, Packet::ReadReq, Packet::Broadcast);
248 pkt->dataDynamic<T>(new T);
249
250 if (!dcachePort.sendTiming(pkt)) {
251 _status = DcacheRetry;
252 dcache_pkt = pkt;
253 } else {
254 _status = DcacheWaitResponse;
255 // memory system takes ownership of packet
256 dcache_pkt = NULL;
257 }
258 }
259
260 // This will need a new way to tell if it has a dcache attached.
261 if (req->isUncacheable())
262 recordEvent("Uncached Read");
263
264 return fault;
265}
266
267#ifndef DOXYGEN_SHOULD_SKIP_THIS
268
269template
270Fault
271TimingSimpleCPU::read(Addr addr, uint64_t &data, unsigned flags);
272
273template
274Fault
275TimingSimpleCPU::read(Addr addr, uint32_t &data, unsigned flags);
276
277template
278Fault
279TimingSimpleCPU::read(Addr addr, uint16_t &data, unsigned flags);
280
281template
282Fault
283TimingSimpleCPU::read(Addr addr, uint8_t &data, unsigned flags);
284
285#endif //DOXYGEN_SHOULD_SKIP_THIS
286
287template<>
288Fault
289TimingSimpleCPU::read(Addr addr, double &data, unsigned flags)
290{
291 return read(addr, *(uint64_t*)&data, flags);
292}
293
294template<>
295Fault
296TimingSimpleCPU::read(Addr addr, float &data, unsigned flags)
297{
298 return read(addr, *(uint32_t*)&data, flags);
299}
300
301
302template<>
303Fault
304TimingSimpleCPU::read(Addr addr, int32_t &data, unsigned flags)
305{
306 return read(addr, (uint32_t&)data, flags);
307}
308
309
310template <class T>
311Fault
312TimingSimpleCPU::write(T data, Addr addr, unsigned flags, uint64_t *res)
313{
314 Request *req =
315 new Request(/* asid */ 0, addr, sizeof(T), flags, thread->readPC(),
316 cpu_id, /* thread ID */ 0);
317
318 // translate to physical address
319 Fault fault = thread->translateDataWriteReq(req);
320
321 // Now do the access.
322 if (fault == NoFault) {
323 assert(dcache_pkt == NULL);
324 dcache_pkt = new Packet(req, Packet::WriteReq, Packet::Broadcast);
325 dcache_pkt->allocate();
326 dcache_pkt->set(data);
327
328 bool do_access = true; // flag to suppress cache access
329
330 if (req->isLocked()) {
331 do_access = TheISA::handleLockedWrite(thread, req);
332 }
333
334 if (do_access) {
335 if (!dcachePort.sendTiming(dcache_pkt)) {
336 _status = DcacheRetry;
337 } else {
338 _status = DcacheWaitResponse;
339 // memory system takes ownership of packet
340 dcache_pkt = NULL;
341 }
342 }
343 }
344
345 // This will need a new way to tell if it's hooked up to a cache or not.
346 if (req->isUncacheable())
347 recordEvent("Uncached Write");
348
349 // If the write needs to have a fault on the access, consider calling
350 // changeStatus() and changing it to "bad addr write" or something.
351 return fault;
352}
353
354
355#ifndef DOXYGEN_SHOULD_SKIP_THIS
356template
357Fault
358TimingSimpleCPU::write(uint64_t data, Addr addr,
359 unsigned flags, uint64_t *res);
360
361template
362Fault
363TimingSimpleCPU::write(uint32_t data, Addr addr,
364 unsigned flags, uint64_t *res);
365
366template
367Fault
368TimingSimpleCPU::write(uint16_t data, Addr addr,
369 unsigned flags, uint64_t *res);
370
371template
372Fault
373TimingSimpleCPU::write(uint8_t data, Addr addr,
374 unsigned flags, uint64_t *res);
375
376#endif //DOXYGEN_SHOULD_SKIP_THIS
377
378template<>
379Fault
380TimingSimpleCPU::write(double data, Addr addr, unsigned flags, uint64_t *res)
381{
382 return write(*(uint64_t*)&data, addr, flags, res);
383}
384
385template<>
386Fault
387TimingSimpleCPU::write(float data, Addr addr, unsigned flags, uint64_t *res)
388{
389 return write(*(uint32_t*)&data, addr, flags, res);
390}
391
392
393template<>
394Fault
395TimingSimpleCPU::write(int32_t data, Addr addr, unsigned flags, uint64_t *res)
396{
397 return write((uint32_t)data, addr, flags, res);
398}
399
400
401void
402TimingSimpleCPU::fetch()
403{
404 checkForInterrupts();
405
406 Request *ifetch_req = new Request();
407 ifetch_req->setThreadContext(cpu_id, /* thread ID */ 0);
408 Fault fault = setupFetchRequest(ifetch_req);
409
410 ifetch_pkt = new Packet(ifetch_req, Packet::ReadReq, Packet::Broadcast);
411 ifetch_pkt->dataStatic(&inst);
412
413 if (fault == NoFault) {
414 if (!icachePort.sendTiming(ifetch_pkt)) {
415 // Need to wait for retry
416 _status = IcacheRetry;
417 } else {
418 // Need to wait for cache to respond
419 _status = IcacheWaitResponse;
420 // ownership of packet transferred to memory system
421 ifetch_pkt = NULL;
422 }
423 } else {
424 // fetch fault: advance directly to next instruction (fault handler)
425 advanceInst(fault);
426 }
427}
428
429
430void
431TimingSimpleCPU::advanceInst(Fault fault)
432{
433 advancePC(fault);
434
435 if (_status == Running) {
436 // kick off fetch of next instruction... callback from icache
437 // response will cause that instruction to be executed,
438 // keeping the CPU running.
439 fetch();
440 }
441}
442
443
444void
445TimingSimpleCPU::completeIfetch(Packet *pkt)
446{
447 // received a response from the icache: execute the received
448 // instruction
449 assert(pkt->result == Packet::Success);
450 assert(_status == IcacheWaitResponse);
451
452 _status = Running;
453
454 delete pkt->req;
455 delete pkt;
456
457 if (getState() == SimObject::Draining) {
458 completeDrain();
459 return;
460 }
461
462 preExecute();
463 if (curStaticInst->isMemRef() && !curStaticInst->isDataPrefetch()) {
464 // load or store: just send to dcache
465 Fault fault = curStaticInst->initiateAcc(this, traceData);
466 if (_status != Running) {
467 // instruction will complete in dcache response callback
468 assert(_status == DcacheWaitResponse || _status == DcacheRetry);
469 assert(fault == NoFault);
470 } else {
471 if (fault == NoFault) {
472 // early fail on store conditional: complete now
473 assert(dcache_pkt != NULL);
474 fault = curStaticInst->completeAcc(dcache_pkt, this,
475 traceData);
476 delete dcache_pkt->req;
477 delete dcache_pkt;
478 dcache_pkt = NULL;
479 }
480 postExecute();
481 advanceInst(fault);
482 }
483 } else {
484 // non-memory instruction: execute completely now
485 Fault fault = curStaticInst->execute(this, traceData);
486 postExecute();
487 advanceInst(fault);
488 }
489}
490
491void
492TimingSimpleCPU::IcachePort::ITickEvent::process()
493{
494 cpu->completeIfetch(pkt);
495}
496
497bool
498TimingSimpleCPU::IcachePort::recvTiming(Packet *pkt)
499{
500 // delay processing of returned data until next CPU clock edge
501 Tick time = pkt->req->getTime();
502 while (time < curTick)
503 time += lat;
504
505 if (time == curTick)
506 cpu->completeIfetch(pkt);
507 else
508 tickEvent.schedule(pkt, time);
509
510 return true;
511}
512
513void
514TimingSimpleCPU::IcachePort::recvRetry()
515{
516 // we shouldn't get a retry unless we have a packet that we're
517 // waiting to transmit
518 assert(cpu->ifetch_pkt != NULL);
519 assert(cpu->_status == IcacheRetry);
520 Packet *tmp = cpu->ifetch_pkt;
521 if (sendTiming(tmp)) {
522 cpu->_status = IcacheWaitResponse;
523 cpu->ifetch_pkt = NULL;
524 }
525}
526
527void
528TimingSimpleCPU::completeDataAccess(Packet *pkt)
529{
530 // received a response from the dcache: complete the load or store
531 // instruction
532 assert(pkt->result == Packet::Success);
533 assert(_status == DcacheWaitResponse);
534 _status = Running;
535
536 if (getState() == SimObject::Draining) {
537 completeDrain();
538
539 delete pkt->req;
540 delete pkt;
541
542 return;
543 }
544
545 Fault fault = curStaticInst->completeAcc(pkt, this, traceData);
546
547 if (pkt->isRead() && pkt->req->isLocked()) {
548 TheISA::handleLockedRead(thread, pkt->req);
549 }
550
551 delete pkt->req;
552 delete pkt;
553
554 postExecute();
555 advanceInst(fault);
556}
557
558
559void
560TimingSimpleCPU::completeDrain()
561{
562 DPRINTF(Config, "Done draining\n");
563 changeState(SimObject::Drained);
564 drainEvent->process();
565}
566
567bool
568TimingSimpleCPU::DcachePort::recvTiming(Packet *pkt)
569{
570 // delay processing of returned data until next CPU clock edge
571 Tick time = pkt->req->getTime();
572 while (time < curTick)
573 time += lat;
574
575 if (time == curTick)
576 cpu->completeDataAccess(pkt);
577 else
578 tickEvent.schedule(pkt, time);
579
580 return true;
581}
582
583void
584TimingSimpleCPU::DcachePort::DTickEvent::process()
585{
586 cpu->completeDataAccess(pkt);
587}
588
589void
590TimingSimpleCPU::DcachePort::recvRetry()
591{
592 // we shouldn't get a retry unless we have a packet that we're
593 // waiting to transmit
594 assert(cpu->dcache_pkt != NULL);
595 assert(cpu->_status == DcacheRetry);
596 Packet *tmp = cpu->dcache_pkt;
597 if (sendTiming(tmp)) {
598 cpu->_status = DcacheWaitResponse;
599 // memory system takes ownership of packet
600 cpu->dcache_pkt = NULL;
601 }
602}
603
604
605////////////////////////////////////////////////////////////////////////
606//
607// TimingSimpleCPU Simulation Object
608//
609BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU)
610
611 Param<Counter> max_insts_any_thread;
612 Param<Counter> max_insts_all_threads;
613 Param<Counter> max_loads_any_thread;
614 Param<Counter> max_loads_all_threads;
615 Param<Tick> progress_interval;
616 SimObjectParam<MemObject *> mem;
617 SimObjectParam<System *> system;
618 Param<int> cpu_id;
619
620#if FULL_SYSTEM
621 SimObjectParam<AlphaITB *> itb;
622 SimObjectParam<AlphaDTB *> dtb;
623 Param<Tick> profile;
624#else
625 SimObjectParam<Process *> workload;
626#endif // FULL_SYSTEM
627
628 Param<int> clock;
629
630 Param<bool> defer_registration;
631 Param<int> width;
632 Param<bool> function_trace;
633 Param<Tick> function_trace_start;
634 Param<bool> simulate_stalls;
635
636END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU)
637
638BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU)
639
640 INIT_PARAM(max_insts_any_thread,
641 "terminate when any thread reaches this inst count"),
642 INIT_PARAM(max_insts_all_threads,
643 "terminate when all threads have reached this inst count"),
644 INIT_PARAM(max_loads_any_thread,
645 "terminate when any thread reaches this load count"),
646 INIT_PARAM(max_loads_all_threads,
647 "terminate when all threads have reached this load count"),
648 INIT_PARAM(progress_interval, "Progress interval"),
649 INIT_PARAM(mem, "memory"),
650 INIT_PARAM(system, "system object"),
651 INIT_PARAM(cpu_id, "processor ID"),
652
653#if FULL_SYSTEM
654 INIT_PARAM(itb, "Instruction TLB"),
655 INIT_PARAM(dtb, "Data TLB"),
656 INIT_PARAM(profile, ""),
657#else
658 INIT_PARAM(workload, "processes to run"),
659#endif // FULL_SYSTEM
660
661 INIT_PARAM(clock, "clock speed"),
662 INIT_PARAM(defer_registration, "defer system registration (for sampling)"),
663 INIT_PARAM(width, "cpu width"),
664 INIT_PARAM(function_trace, "Enable function trace"),
665 INIT_PARAM(function_trace_start, "Cycle to start function trace"),
666 INIT_PARAM(simulate_stalls, "Simulate cache stall cycles")
667
668END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU)
669
670
671CREATE_SIM_OBJECT(TimingSimpleCPU)
672{
673 TimingSimpleCPU::Params *params = new TimingSimpleCPU::Params();
674 params->name = getInstanceName();
675 params->numberOfThreads = 1;
676 params->max_insts_any_thread = max_insts_any_thread;
677 params->max_insts_all_threads = max_insts_all_threads;
678 params->max_loads_any_thread = max_loads_any_thread;
679 params->max_loads_all_threads = max_loads_all_threads;
680 params->progress_interval = progress_interval;
681 params->deferRegistration = defer_registration;
682 params->clock = clock;
683 params->functionTrace = function_trace;
684 params->functionTraceStart = function_trace_start;
685 params->mem = mem;
686 params->system = system;
687 params->cpu_id = cpu_id;
688
689#if FULL_SYSTEM
690 params->itb = itb;
691 params->dtb = dtb;
692 params->profile = profile;
693#else
694 params->process = workload;
695#endif
696
697 TimingSimpleCPU *cpu = new TimingSimpleCPU(params);
698 return cpu;
699}
700
701REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU)
702