summaryrefslogtreecommitdiff
path: root/src/cpu/ozone/inorder_back_end_impl.hh
blob: 2d4d225c7494ff605f011a2d7937b7ed57882649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 * Copyright (c) 2006 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Kevin Lim
 */

#include "sim/faults.hh"
#include "arch/types.hh"
#include "config/the_isa.hh"
#include "cpu/ozone/inorder_back_end.hh"
#include "cpu/ozone/thread_state.hh"

template <class Impl>
InorderBackEnd<Impl>::InorderBackEnd(Params *params)
    : squashPending(false),
      squashSeqNum(0),
      squashNextPC(0),
      faultFromFetch(NoFault),
      interruptBlocked(false),
      cacheCompletionEvent(this),
      dcacheInterface(params->dcacheInterface),
      width(params->backEndWidth),
      latency(params->backEndLatency),
      squashLatency(params->backEndSquashLatency),
      numInstsToWB(0, latency + 1)
{
    instsAdded = numInstsToWB.getWire(latency);
    instsToExecute = numInstsToWB.getWire(0);

    memReq = new MemReq;
    memReq->data = new uint8_t[64];
    status = Running;
}

template <class Impl>
std::string
InorderBackEnd<Impl>::name() const
{
    return cpu->name() + ".inorderbackend";
}

template <class Impl>
void
InorderBackEnd<Impl>::setXC(ExecContext *xc_ptr)
{
    xc = xc_ptr;
    memReq->xc = xc;
}

template <class Impl>
void
InorderBackEnd<Impl>::setThreadState(OzoneThreadState<Impl> *thread_ptr)
{
    thread = thread_ptr;
    thread->setFuncExeInst(0);
}

#if FULL_SYSTEM
template <class Impl>
void
InorderBackEnd<Impl>::checkInterrupts()
{
    //Check if there are any outstanding interrupts
    //Handle the interrupts
    int ipl = 0;
    int summary = 0;


    if (thread->readMiscRegNoEffect(IPR_ASTRR))
        panic("asynchronous traps not implemented\n");

    if (thread->readMiscRegNoEffect(IPR_SIRR)) {
        for (int i = INTLEVEL_SOFTWARE_MIN;
             i < INTLEVEL_SOFTWARE_MAX; i++) {
            if (thread->readMiscRegNoEffect(IPR_SIRR) & (ULL(1) << i)) {
                // See table 4-19 of the 21164 hardware reference
                ipl = (i - INTLEVEL_SOFTWARE_MIN) + 1;
                summary |= (ULL(1) << i);
            }
        }
    }

    uint64_t interrupts = cpu->intr_status();

    if (interrupts) {
        for (int i = INTLEVEL_EXTERNAL_MIN;
             i < INTLEVEL_EXTERNAL_MAX; i++) {
            if (interrupts & (ULL(1) << i)) {
                // See table 4-19 of the 21164 hardware reference
                ipl = i;
                summary |= (ULL(1) << i);
            }
        }
    }

    if (ipl && ipl > thread->readMiscRegNoEffect(IPR_IPLR)) {
        thread->inSyscall = true;

        thread->setMiscRegNoEffect(IPR_ISR, summary);
        thread->setMiscRegNoEffect(IPR_INTID, ipl);
        Fault(new InterruptFault)->invoke(xc);
        DPRINTF(Flow, "Interrupt! IPLR=%d ipl=%d summary=%x\n",
                thread->readMiscRegNoEffect(IPR_IPLR), ipl, summary);

        // May need to go 1 inst prior
        squashPending = true;

        thread->inSyscall = false;

        setSquashInfoFromXC();
    }
}
#endif

template <class Impl>
void
InorderBackEnd<Impl>::tick()
{
    // Squash due to an external source
    // Not sure if this or an interrupt has higher priority
    if (squashPending) {
        squash(squashSeqNum, squashNextPC);
        return;
    }

    // if (interrupt) then set thread PC, stall front end, record that
    // I'm waiting for it to drain.  (for now just squash)
#if FULL_SYSTEM
    if (interruptBlocked || cpu->checkInterrupts(tc)) {
        if (!robEmpty()) {
            interruptBlocked = true;
        //AlphaDep
        } else if (robEmpty() && (PC & 0x3)) {
            // Will need to let the front end continue a bit until
            // we're out of pal mode.  Hopefully we never get into an
            // infinite loop...
            interruptBlocked = false;
        } else {
            interruptBlocked = false;
            checkInterrupts();
            return;
        }
    }
#endif

    if (status != DcacheMissLoadStall &&
        status != DcacheMissStoreStall) {
        for (int i = 0; i < width && (*instsAdded) < width; ++i) {
            DynInstPtr inst = frontEnd->getInst();

            if (!inst)
                break;

            instList.push_back(inst);

            (*instsAdded)++;
        }

#if FULL_SYSTEM
        if (faultFromFetch && robEmpty() && frontEnd->isEmpty()) {
            handleFault();
        } else {
            executeInsts();
        }
#else
        executeInsts();
#endif
    }
}

template <class Impl>
void
InorderBackEnd<Impl>::executeInsts()
{
    bool completed_last_inst = true;
    int insts_to_execute = *instsToExecute;
    int freed_regs = 0;

    while (insts_to_execute > 0) {
        assert(!instList.empty());
        DynInstPtr inst = instList.front();

        commitPC = inst->readPC();

        thread->setPC(commitPC);
        thread->setNextPC(inst->readNextPC());

#if FULL_SYSTEM
        int count = 0;
        Addr oldpc;
        do {
            if (count == 0)
                assert(!thread->inSyscall && !thread->trapPending);
            oldpc = thread->readPC();
            cpu->system->pcEventQueue.service(
                thread->getXCProxy());
            count++;
        } while (oldpc != thread->readPC());
        if (count > 1) {
            DPRINTF(IBE, "PC skip function event, stopping commit\n");
            completed_last_inst = false;
            squashPending = true;
            break;
        }
#endif

        Fault inst_fault = NoFault;

        if (status == DcacheMissComplete) {
            DPRINTF(IBE, "Completing inst [sn:%lli]\n", inst->seqNum);
            status = Running;
        } else if (inst->isMemRef() && status != DcacheMissComplete &&
            (!inst->isDataPrefetch() && !inst->isInstPrefetch())) {
            DPRINTF(IBE, "Initiating mem op inst [sn:%lli] PC: %#x\n",
                    inst->seqNum, inst->readPC());

            cacheCompletionEvent.inst = inst;
            inst_fault = inst->initiateAcc();
            if (inst_fault == NoFault &&
                status != DcacheMissLoadStall &&
                status != DcacheMissStoreStall) {
                inst_fault = inst->completeAcc();
            }
            ++thread->funcExeInst;
        } else {
            DPRINTF(IBE, "Executing inst [sn:%lli] PC: %#x\n",
                    inst->seqNum, inst->readPC());
            inst_fault = inst->execute();
            ++thread->funcExeInst;
        }

        // Will need to be able to break this loop in case the load
        // misses.  Split access/complete ops would be useful here
        // with writeback events.
        if (status == DcacheMissLoadStall) {
            *instsToExecute = insts_to_execute;

            completed_last_inst = false;
            break;
        } else if (status == DcacheMissStoreStall) {
            // Figure out how to fix this hack.  Probably have DcacheMissLoad
            // vs DcacheMissStore.
            *instsToExecute = insts_to_execute;
            completed_last_inst = false;
/*
            instList.pop_front();
            --insts_to_execute;
            if (inst->traceData) {
                inst->traceData->finalize();
            }
*/

            // Don't really need to stop for a store stall as long as
            // the memory system is able to handle store forwarding
            // and such.  Breaking out might help avoid the cache
            // interface becoming blocked.
            break;
        }

        inst->setExecuted();
        inst->setResultReady();
        inst->setCanCommit();

        instList.pop_front();

        --insts_to_execute;
        --(*instsToExecute);

        if (inst->traceData) {
            inst->traceData->finalize();
            inst->traceData = NULL;
        }

        if (inst_fault != NoFault) {
#if FULL_SYSTEM
            DPRINTF(IBE, "Inst [sn:%lli] PC %#x has a fault\n",
                    inst->seqNum, inst->readPC());

            assert(!thread->inSyscall);

            thread->inSyscall = true;

            // Hack for now; DTB will sometimes need the machine instruction
            // for when faults happen.  So we will set it here, prior to the
            // DTB possibly needing it for this translation.
            thread->setInst(
                static_cast<TheISA::MachInst>(inst->staticInst->machInst));

            // Consider holding onto the trap and waiting until the trap event
            // happens for this to be executed.
            inst_fault->invoke(xc);

            // Exit state update mode to avoid accidental updating.
            thread->inSyscall = false;

            squashPending = true;

            // Generate trap squash event.
//            generateTrapEvent(tid);
            completed_last_inst = false;
            break;
#else // !FULL_SYSTEM
            panic("fault (%d) detected @ PC %08p", inst_fault,
                  inst->PC);
#endif // FULL_SYSTEM
        }

        for (int i = 0; i < inst->numDestRegs(); ++i) {
            renameTable[inst->destRegIdx(i)] = inst;
            thread->renameTable[inst->destRegIdx(i)] = inst;
            ++freed_regs;
        }

        inst->clearDependents();

        comm->access(0)->doneSeqNum = inst->seqNum;

        if (inst->mispredicted()) {
            squash(inst->seqNum, inst->readNextPC());

            thread->setNextPC(inst->readNextPC());

            break;
        } else if (squashPending) {
            // Something external happened that caused the CPU to squash.
            // Break out of commit and handle the squash next cycle.
            break;
        }
        // If it didn't mispredict, then it executed fine.  Send back its
        // registers and BP info?  What about insts that may still have
        // latency, like loads?  Probably can send back the information after
        // it is completed.

        // keep an instruction count
        cpu->numInst++;
        thread->numInsts++;
    }

    frontEnd->addFreeRegs(freed_regs);

    assert(insts_to_execute >= 0);

    // Should only advance this if I have executed all instructions.
    if (insts_to_execute == 0) {
        numInstsToWB.advance();
    }

    // Should I set the PC to the next PC here?  What do I set next PC to?
    if (completed_last_inst) {
        thread->setPC(thread->readNextPC());
        thread->setNextPC(thread->readPC() + sizeof(MachInst));
    }

    if (squashPending) {
        setSquashInfoFromXC();
    }
}

template <class Impl>
void
InorderBackEnd<Impl>::handleFault()
{
    DPRINTF(Commit, "Handling fault from fetch\n");

    assert(!thread->inSyscall);

    thread->inSyscall = true;

    // Consider holding onto the trap and waiting until the trap event
    // happens for this to be executed.
    faultFromFetch->invoke(xc);

    // Exit state update mode to avoid accidental updating.
    thread->inSyscall = false;

    squashPending = true;

    setSquashInfoFromXC();
}

template <class Impl>
void
InorderBackEnd<Impl>::squash(const InstSeqNum &squash_num, const Addr &next_PC)
{
    DPRINTF(IBE, "Squashing from [sn:%lli], setting PC to %#x\n",
            squash_num, next_PC);

    InstListIt squash_it = --(instList.end());

    int freed_regs = 0;

    while (!instList.empty() && (*squash_it)->seqNum > squash_num) {
        DynInstPtr inst = *squash_it;

        DPRINTF(IBE, "Squashing instruction PC %#x, [sn:%lli].\n",
                inst->readPC(),
                inst->seqNum);

        // May cause problems with misc regs
        freed_regs+= inst->numDestRegs();
        inst->clearDependents();
        squash_it--;
        instList.pop_back();
    }

    frontEnd->addFreeRegs(freed_regs);

    for (int i = 0; i < latency+1; ++i) {
        numInstsToWB.advance();
    }

    squashPending = false;

    // Probably want to make sure that this squash is the one that set the
    // thread into inSyscall mode.
    thread->inSyscall = false;

    // Tell front end to squash, reset PC to new one.
    frontEnd->squash(squash_num, next_PC);

    faultFromFetch = NULL;
}

template <class Impl>
void
InorderBackEnd<Impl>::squashFromXC()
{
    // Record that I need to squash
    squashPending = true;

    thread->inSyscall = true;
}

template <class Impl>
void
InorderBackEnd<Impl>::setSquashInfoFromXC()
{
    // Need to handle the case of the instList being empty.  In that case
    // probably any number works, except maybe with stores in the store buffer.
    squashSeqNum = instList.empty() ? 0 : instList.front()->seqNum - 1;

    squashNextPC = thread->PC;
}

template <class Impl>
void
InorderBackEnd<Impl>::fetchFault(Fault &fault)
{
    faultFromFetch = fault;
}

template <class Impl>
void
InorderBackEnd<Impl>::dumpInsts()
{
    int num = 0;
    int valid_num = 0;

    InstListIt inst_list_it = instList.begin();

    cprintf("Inst list size: %i\n", instList.size());

    while (inst_list_it != instList.end())
    {
        cprintf("Instruction:%i\n",
                num);
        if (!(*inst_list_it)->isSquashed()) {
            if (!(*inst_list_it)->isIssued()) {
                ++valid_num;
                cprintf("Count:%i\n", valid_num);
            } else if ((*inst_list_it)->isMemRef() &&
                       !(*inst_list_it)->memOpDone) {
                // Loads that have not been marked as executed still count
                // towards the total instructions.
                ++valid_num;
                cprintf("Count:%i\n", valid_num);
            }
        }

        cprintf("PC:%#x\n[sn:%lli]\n[tid:%i]\n"
                "Issued:%i\nSquashed:%i\n",
                (*inst_list_it)->readPC(),
                (*inst_list_it)->seqNum,
                (*inst_list_it)->threadNumber,
                (*inst_list_it)->isIssued(),
                (*inst_list_it)->isSquashed());

        if ((*inst_list_it)->isMemRef()) {
            cprintf("MemOpDone:%i\n", (*inst_list_it)->memOpDone);
        }

        cprintf("\n");

        inst_list_it++;
        ++num;
    }
}

template <class Impl>
InorderBackEnd<Impl>::DCacheCompletionEvent::DCacheCompletionEvent(
    InorderBackEnd *_be)
    : Event(&mainEventQueue, CPU_Tick_Pri), be(_be)
{
//    this->setFlags(Event::AutoDelete);
}

template <class Impl>
void
InorderBackEnd<Impl>::DCacheCompletionEvent::process()
{
    inst->completeAcc();
    be->status = DcacheMissComplete;
}

template <class Impl>
const char *
InorderBackEnd<Impl>::DCacheCompletionEvent::description() const
{
    return "DCache completion";
}