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