atomic.cc revision 2623
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
29#include "arch/utility.hh"
30#include "cpu/exetrace.hh"
31#include "cpu/simple/atomic.hh"
32#include "mem/packet_impl.hh"
33#include "sim/builder.hh"
34
35using namespace std;
36using namespace TheISA;
37
38AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU *c)
39    : Event(&mainEventQueue, CPU_Tick_Pri), cpu(c)
40{
41}
42
43
44void
45AtomicSimpleCPU::TickEvent::process()
46{
47    cpu->tick();
48}
49
50const char *
51AtomicSimpleCPU::TickEvent::description()
52{
53    return "AtomicSimpleCPU tick event";
54}
55
56
57void
58AtomicSimpleCPU::init()
59{
60    //Create Memory Ports (conect them up)
61    Port *mem_dport = mem->getPort("");
62    dcachePort.setPeer(mem_dport);
63    mem_dport->setPeer(&dcachePort);
64
65    Port *mem_iport = mem->getPort("");
66    icachePort.setPeer(mem_iport);
67    mem_iport->setPeer(&icachePort);
68
69    BaseCPU::init();
70#if FULL_SYSTEM
71    for (int i = 0; i < execContexts.size(); ++i) {
72        ExecContext *xc = execContexts[i];
73
74        // initialize CPU, including PC
75        TheISA::initCPU(xc, xc->readCpuId());
76    }
77#endif
78}
79
80bool
81AtomicSimpleCPU::CpuPort::recvTiming(Packet &pkt)
82{
83    panic("AtomicSimpleCPU doesn't expect recvAtomic callback!");
84    return true;
85}
86
87Tick
88AtomicSimpleCPU::CpuPort::recvAtomic(Packet &pkt)
89{
90    panic("AtomicSimpleCPU doesn't expect recvAtomic callback!");
91    return curTick;
92}
93
94void
95AtomicSimpleCPU::CpuPort::recvFunctional(Packet &pkt)
96{
97    panic("AtomicSimpleCPU doesn't expect recvFunctional callback!");
98}
99
100void
101AtomicSimpleCPU::CpuPort::recvStatusChange(Status status)
102{
103    panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
104}
105
106Packet *
107AtomicSimpleCPU::CpuPort::recvRetry()
108{
109    panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
110    return NULL;
111}
112
113
114AtomicSimpleCPU::AtomicSimpleCPU(Params *p)
115    : BaseSimpleCPU(p), tickEvent(this),
116      width(p->width), simulate_stalls(p->simulate_stalls),
117      icachePort(this), dcachePort(this)
118{
119    _status = Idle;
120
121    ifetch_req = new Request(true);
122    ifetch_req->setAsid(0);
123    // @todo fix me and get the real cpu iD!!!
124    ifetch_req->setCpuNum(0);
125    ifetch_req->setSize(sizeof(MachInst));
126    ifetch_pkt = new Packet;
127    ifetch_pkt->cmd = Read;
128    ifetch_pkt->dataStatic(&inst);
129    ifetch_pkt->req = ifetch_req;
130    ifetch_pkt->size = sizeof(MachInst);
131    ifetch_pkt->dest = Packet::Broadcast;
132
133    data_read_req = new Request(true);
134    // @todo fix me and get the real cpu iD!!!
135    data_read_req->setCpuNum(0);
136    data_read_req->setAsid(0);
137    data_read_pkt = new Packet;
138    data_read_pkt->cmd = Read;
139    data_read_pkt->dataStatic(&dataReg);
140    data_read_pkt->req = data_read_req;
141    data_read_pkt->dest = Packet::Broadcast;
142
143    data_write_req = new Request(true);
144    // @todo fix me and get the real cpu iD!!!
145    data_write_req->setCpuNum(0);
146    data_write_req->setAsid(0);
147    data_write_pkt = new Packet;
148    data_write_pkt->cmd = Write;
149    data_write_pkt->req = data_write_req;
150    data_write_pkt->dest = Packet::Broadcast;
151}
152
153
154AtomicSimpleCPU::~AtomicSimpleCPU()
155{
156}
157
158void
159AtomicSimpleCPU::serialize(ostream &os)
160{
161    BaseSimpleCPU::serialize(os);
162    SERIALIZE_ENUM(_status);
163    nameOut(os, csprintf("%s.tickEvent", name()));
164    tickEvent.serialize(os);
165}
166
167void
168AtomicSimpleCPU::unserialize(Checkpoint *cp, const string &section)
169{
170    BaseSimpleCPU::unserialize(cp, section);
171    UNSERIALIZE_ENUM(_status);
172    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
173}
174
175void
176AtomicSimpleCPU::switchOut(Sampler *s)
177{
178    sampler = s;
179    if (status() == Running) {
180        _status = SwitchedOut;
181
182        tickEvent.squash();
183    }
184    sampler->signalSwitched();
185}
186
187
188void
189AtomicSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
190{
191    BaseCPU::takeOverFrom(oldCPU);
192
193    assert(!tickEvent.scheduled());
194
195    // if any of this CPU's ExecContexts are active, mark the CPU as
196    // running and schedule its tick event.
197    for (int i = 0; i < execContexts.size(); ++i) {
198        ExecContext *xc = execContexts[i];
199        if (xc->status() == ExecContext::Active && _status != Running) {
200            _status = Running;
201            tickEvent.schedule(curTick);
202            break;
203        }
204    }
205}
206
207
208void
209AtomicSimpleCPU::activateContext(int thread_num, int delay)
210{
211    assert(thread_num == 0);
212    assert(cpuXC);
213
214    assert(_status == Idle);
215    assert(!tickEvent.scheduled());
216
217    notIdleFraction++;
218    tickEvent.schedule(curTick + cycles(delay));
219    _status = Running;
220}
221
222
223void
224AtomicSimpleCPU::suspendContext(int thread_num)
225{
226    assert(thread_num == 0);
227    assert(cpuXC);
228
229    assert(_status == Running);
230    assert(tickEvent.scheduled());
231
232    notIdleFraction--;
233    tickEvent.deschedule();
234    _status = Idle;
235}
236
237
238template <class T>
239Fault
240AtomicSimpleCPU::read(Addr addr, T &data, unsigned flags)
241{
242    data_read_req->setVaddr(addr);
243    data_read_req->setSize(sizeof(T));
244    data_read_req->setFlags(flags);
245    data_read_req->setTime(curTick);
246
247    if (traceData) {
248        traceData->setAddr(addr);
249    }
250
251    // translate to physical address
252    Fault fault = cpuXC->translateDataReadReq(data_read_req);
253
254    // Now do the access.
255    if (fault == NoFault) {
256        data_read_pkt->reset();
257        data_read_pkt->addr = data_read_req->getPaddr();
258        data_read_pkt->size = sizeof(T);
259
260        dcache_complete = dcachePort.sendAtomic(*data_read_pkt);
261        dcache_access = true;
262
263        assert(data_read_pkt->result == Success);
264        data = data_read_pkt->get<T>();
265
266    }
267
268    // This will need a new way to tell if it has a dcache attached.
269    if (data_read_req->getFlags() & UNCACHEABLE)
270        recordEvent("Uncached Read");
271
272    return fault;
273}
274
275#ifndef DOXYGEN_SHOULD_SKIP_THIS
276
277template
278Fault
279AtomicSimpleCPU::read(Addr addr, uint64_t &data, unsigned flags);
280
281template
282Fault
283AtomicSimpleCPU::read(Addr addr, uint32_t &data, unsigned flags);
284
285template
286Fault
287AtomicSimpleCPU::read(Addr addr, uint16_t &data, unsigned flags);
288
289template
290Fault
291AtomicSimpleCPU::read(Addr addr, uint8_t &data, unsigned flags);
292
293#endif //DOXYGEN_SHOULD_SKIP_THIS
294
295template<>
296Fault
297AtomicSimpleCPU::read(Addr addr, double &data, unsigned flags)
298{
299    return read(addr, *(uint64_t*)&data, flags);
300}
301
302template<>
303Fault
304AtomicSimpleCPU::read(Addr addr, float &data, unsigned flags)
305{
306    return read(addr, *(uint32_t*)&data, flags);
307}
308
309
310template<>
311Fault
312AtomicSimpleCPU::read(Addr addr, int32_t &data, unsigned flags)
313{
314    return read(addr, (uint32_t&)data, flags);
315}
316
317
318template <class T>
319Fault
320AtomicSimpleCPU::write(T data, Addr addr, unsigned flags, uint64_t *res)
321{
322    data_write_req->setVaddr(addr);
323    data_write_req->setTime(curTick);
324    data_write_req->setSize(sizeof(T));
325    data_write_req->setFlags(flags);
326
327    if (traceData) {
328        traceData->setAddr(addr);
329    }
330
331    // translate to physical address
332    Fault fault = cpuXC->translateDataWriteReq(data_write_req);
333
334    // Now do the access.
335    if (fault == NoFault) {
336        data_write_pkt->reset();
337        data = htog(data);
338        data_write_pkt->dataStatic(&data);
339        data_write_pkt->addr = data_write_req->getPaddr();
340        data_write_pkt->size = sizeof(T);
341
342        dcache_complete = dcachePort.sendAtomic(*data_write_pkt);
343        dcache_access = true;
344
345        assert(data_write_pkt->result == Success);
346    }
347
348    if (res && (fault == NoFault))
349        *res = data_write_pkt->result;
350
351    // This will need a new way to tell if it's hooked up to a cache or not.
352    if (data_write_req->getFlags() & UNCACHEABLE)
353        recordEvent("Uncached Write");
354
355    // @todo this is a hack and only works on uniprocessor systems
356    // some one else can implement LL/SC.
357    if (data_write_req->getFlags() & LOCKED)
358        *res = 1;
359
360    // If the write needs to have a fault on the access, consider calling
361    // changeStatus() and changing it to "bad addr write" or something.
362    return fault;
363}
364
365
366#ifndef DOXYGEN_SHOULD_SKIP_THIS
367template
368Fault
369AtomicSimpleCPU::write(uint64_t data, Addr addr,
370                       unsigned flags, uint64_t *res);
371
372template
373Fault
374AtomicSimpleCPU::write(uint32_t data, Addr addr,
375                       unsigned flags, uint64_t *res);
376
377template
378Fault
379AtomicSimpleCPU::write(uint16_t data, Addr addr,
380                       unsigned flags, uint64_t *res);
381
382template
383Fault
384AtomicSimpleCPU::write(uint8_t data, Addr addr,
385                       unsigned flags, uint64_t *res);
386
387#endif //DOXYGEN_SHOULD_SKIP_THIS
388
389template<>
390Fault
391AtomicSimpleCPU::write(double data, Addr addr, unsigned flags, uint64_t *res)
392{
393    return write(*(uint64_t*)&data, addr, flags, res);
394}
395
396template<>
397Fault
398AtomicSimpleCPU::write(float data, Addr addr, unsigned flags, uint64_t *res)
399{
400    return write(*(uint32_t*)&data, addr, flags, res);
401}
402
403
404template<>
405Fault
406AtomicSimpleCPU::write(int32_t data, Addr addr, unsigned flags, uint64_t *res)
407{
408    return write((uint32_t)data, addr, flags, res);
409}
410
411
412void
413AtomicSimpleCPU::tick()
414{
415    Tick latency = cycles(1); // instruction takes one cycle by default
416
417    for (int i = 0; i < width; ++i) {
418        numCycles++;
419
420        ifetch_req->resetMin();
421        ifetch_pkt->reset();
422        Fault fault = setupFetchPacket(ifetch_pkt);
423
424        if (fault == NoFault) {
425            Tick icache_complete = icachePort.sendAtomic(*ifetch_pkt);
426            // ifetch_req is initialized to read the instruction directly
427            // into the CPU object's inst field.
428
429            dcache_access = false; // assume no dcache access
430            preExecute();
431            fault = curStaticInst->execute(this, traceData);
432            postExecute();
433
434            if (traceData) {
435                traceData->finalize();
436            }
437
438            if (simulate_stalls) {
439                // This calculation assumes that the icache and dcache
440                // access latencies are always a multiple of the CPU's
441                // cycle time.  If not, the next tick event may get
442                // scheduled at a non-integer multiple of the CPU
443                // cycle time.
444                Tick icache_stall = icache_complete - curTick - cycles(1);
445                Tick dcache_stall =
446                    dcache_access ? dcache_complete - curTick - cycles(1) : 0;
447                latency += icache_stall + dcache_stall;
448            }
449
450        }
451
452        advancePC(fault);
453    }
454
455    tickEvent.schedule(curTick + latency);
456}
457
458
459////////////////////////////////////////////////////////////////////////
460//
461//  AtomicSimpleCPU Simulation Object
462//
463BEGIN_DECLARE_SIM_OBJECT_PARAMS(AtomicSimpleCPU)
464
465    Param<Counter> max_insts_any_thread;
466    Param<Counter> max_insts_all_threads;
467    Param<Counter> max_loads_any_thread;
468    Param<Counter> max_loads_all_threads;
469    SimObjectParam<MemObject *> mem;
470
471#if FULL_SYSTEM
472    SimObjectParam<AlphaITB *> itb;
473    SimObjectParam<AlphaDTB *> dtb;
474    SimObjectParam<System *> system;
475    Param<int> cpu_id;
476    Param<Tick> profile;
477#else
478    SimObjectParam<Process *> workload;
479#endif // FULL_SYSTEM
480
481    Param<int> clock;
482
483    Param<bool> defer_registration;
484    Param<int> width;
485    Param<bool> function_trace;
486    Param<Tick> function_trace_start;
487    Param<bool> simulate_stalls;
488
489END_DECLARE_SIM_OBJECT_PARAMS(AtomicSimpleCPU)
490
491BEGIN_INIT_SIM_OBJECT_PARAMS(AtomicSimpleCPU)
492
493    INIT_PARAM(max_insts_any_thread,
494               "terminate when any thread reaches this inst count"),
495    INIT_PARAM(max_insts_all_threads,
496               "terminate when all threads have reached this inst count"),
497    INIT_PARAM(max_loads_any_thread,
498               "terminate when any thread reaches this load count"),
499    INIT_PARAM(max_loads_all_threads,
500               "terminate when all threads have reached this load count"),
501    INIT_PARAM(mem, "memory"),
502
503#if FULL_SYSTEM
504    INIT_PARAM(itb, "Instruction TLB"),
505    INIT_PARAM(dtb, "Data TLB"),
506    INIT_PARAM(system, "system object"),
507    INIT_PARAM(cpu_id, "processor ID"),
508    INIT_PARAM(profile, ""),
509#else
510    INIT_PARAM(workload, "processes to run"),
511#endif // FULL_SYSTEM
512
513    INIT_PARAM(clock, "clock speed"),
514    INIT_PARAM(defer_registration, "defer system registration (for sampling)"),
515    INIT_PARAM(width, "cpu width"),
516    INIT_PARAM(function_trace, "Enable function trace"),
517    INIT_PARAM(function_trace_start, "Cycle to start function trace"),
518    INIT_PARAM(simulate_stalls, "Simulate cache stall cycles")
519
520END_INIT_SIM_OBJECT_PARAMS(AtomicSimpleCPU)
521
522
523CREATE_SIM_OBJECT(AtomicSimpleCPU)
524{
525    AtomicSimpleCPU::Params *params = new AtomicSimpleCPU::Params();
526    params->name = getInstanceName();
527    params->numberOfThreads = 1;
528    params->max_insts_any_thread = max_insts_any_thread;
529    params->max_insts_all_threads = max_insts_all_threads;
530    params->max_loads_any_thread = max_loads_any_thread;
531    params->max_loads_all_threads = max_loads_all_threads;
532    params->deferRegistration = defer_registration;
533    params->clock = clock;
534    params->functionTrace = function_trace;
535    params->functionTraceStart = function_trace_start;
536    params->width = width;
537    params->simulate_stalls = simulate_stalls;
538    params->mem = mem;
539
540#if FULL_SYSTEM
541    params->itb = itb;
542    params->dtb = dtb;
543    params->system = system;
544    params->cpu_id = cpu_id;
545    params->profile = profile;
546#else
547    params->process = workload;
548#endif
549
550    AtomicSimpleCPU *cpu = new AtomicSimpleCPU(params);
551    return cpu;
552}
553
554REGISTER_SIM_OBJECT("AtomicSimpleCPU", AtomicSimpleCPU)
555
556