summaryrefslogtreecommitdiff
path: root/src/arch/arm/tracers/tarmac_record.cc
blob: 5dbb847e51beba7be93520c0b00fd2cd53571a10 (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
/*
 * Copyright (c) 2017-2018 ARM Limited
 * All rights reserved
 *
 * The license below extends only to copyright in the software and shall
 * not be construed as granting a license to any other intellectual
 * property including but not limited to intellectual property relating
 * to a hardware implementation of the functionality of the software
 * licensed hereunder.  You may use the software subject to the license
 * terms below provided that you ensure that this notice is replicated
 * unmodified and in its entirety in all distributions of the software,
 * modified or unmodified, in source code or in binary form.
 *
 * 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: Giacomo Travaglini
 */

#include "arch/arm/tracers/tarmac_record.hh"

#include "arch/arm/insts/static_inst.hh"
#include "tarmac_tracer.hh"

namespace Trace {

// TARMAC Instruction Record static variables
uint64_t TarmacTracerRecord::TraceInstEntry::instCount = 0;

std::string
iSetStateToStr(TarmacBaseRecord::ISetState isetstate)
{
    switch (isetstate) {
      case TarmacBaseRecord::ISET_ARM:
        return "A";
      case TarmacBaseRecord::ISET_THUMB:
        return "T";
      case TarmacBaseRecord::ISET_A64:
        return "O";
      default:
        return "Unsupported";
    }
}

std::string
opModeToStr(OperatingMode opMode)
{
    switch (opMode) {
      case MODE_EL0T:
        return "EL0t";
      case MODE_EL1T:
        return "EL1t";
      case MODE_EL1H:
        return "EL1h";
      case MODE_EL2T:
        return "EL2t";
      case MODE_EL2H:
        return "EL2h";
      case MODE_EL3T:
        return "EL3t";
      case MODE_EL3H:
        return "EL3h";
      case MODE_USER:
        return "usr";
      case MODE_FIQ:
        return "fiq";
      case MODE_IRQ:
        return "irq";
      case MODE_SVC:
        return "svc";
      case MODE_MON:
        return "mon";
      case MODE_ABORT:
        return "abt";
      case MODE_HYP:
        return "hyp";
      case MODE_UNDEFINED:
        return "und";
      case MODE_SYSTEM:
        return "sys";
      default:
        return "Unsupported";
    }
}

// TarmacTracerRecord ctor
TarmacTracerRecord::TarmacTracerRecord(Tick _when, ThreadContext *_thread,
                                     const StaticInstPtr _staticInst,
                                     PCState _pc,
                                     TarmacTracer& _tracer,
                                     const StaticInstPtr _macroStaticInst)
    : TarmacBaseRecord(_when, _thread, _staticInst,
                       _pc,  _macroStaticInst),
      tracer(_tracer)
{
}

TarmacTracerRecord::TraceInstEntry::TraceInstEntry(
    const TarmacContext& tarmCtx,
    bool predicate)
      : InstEntry(tarmCtx.thread, tarmCtx.pc, tarmCtx.staticInst, predicate)
{
    secureMode = inSecureState(tarmCtx.thread);

    auto arm_inst = static_cast<const ArmStaticInst*>(
        tarmCtx.staticInst.get()
    );

    // Get the instruction size as a number of bits:
    // (multiply byte size by 8)
    instSize = (arm_inst->instSize() << 3);

    // Mask the opcode using the instruction size: the
    // opcode field will otherwise be 32 bit wide even
    // for 16bit (Thumb) instruction.
    opcode = arm_inst->encoding();

    // Update the instruction count: number of executed
    // instructions.
    instCount++;
}

TarmacTracerRecord::TraceMemEntry::TraceMemEntry(
    const TarmacContext& tarmCtx,
    uint8_t _size, Addr _addr, uint64_t _data)
      :  MemEntry(_size, _addr, _data),
         loadAccess(tarmCtx.staticInst->isLoad())
{
}

TarmacTracerRecord::TraceRegEntry::TraceRegEntry(
    const TarmacContext& tarmCtx,
    const RegId& reg)
      : RegEntry(tarmCtx.pc),
        regValid(false),
        regClass(reg.classValue()),
        regRel(reg.index())
{
}

void
TarmacTracerRecord::TraceRegEntry::update(
    const TarmacContext& tarmCtx
)
{
    // Fill the register entry data, according to register
    // class.
    switch (regClass) {
      case CCRegClass:
        updateCC(tarmCtx, regRel);
        break;
      case FloatRegClass:
        updateFloat(tarmCtx, regRel);
        break;
      case IntRegClass:
        updateInt(tarmCtx, regRel);
        break;
      case MiscRegClass:
        updateMisc(tarmCtx, regRel);
        break;
      default:
        // If unsupported format, do nothing: non updating
        // the register will prevent it to be printed.
        break;
    }
}

void
TarmacTracerRecord::TraceRegEntry::updateMisc(
    const TarmacContext& tarmCtx,
    RegIndex regRelIdx
)
{
    auto thread = tarmCtx.thread;

    regValid = true;
    regName = miscRegName[regRelIdx];
    valueLo = thread->readMiscRegNoEffect(regRelIdx);

    // If it is the CPSR:
    // update the value of the CPSR register and add
    // the CC flags on top of the value
    if (regRelIdx == MISCREG_CPSR) {
        CPSR cpsr = thread->readMiscRegNoEffect(MISCREG_CPSR);
        cpsr.nz = thread->readCCReg(CCREG_NZ);
        cpsr.c = thread->readCCReg(CCREG_C);
        cpsr.v = thread->readCCReg(CCREG_V);
        cpsr.ge = thread->readCCReg(CCREG_GE);

        // update the entry value
        valueLo = cpsr;
    }
}

void
TarmacTracerRecord::TraceRegEntry::updateCC(
    const TarmacContext& tarmCtx,
    RegIndex regRelIdx
)
{
    auto thread = tarmCtx.thread;

    regValid = true;
    regName = ccRegName[regRelIdx];
    valueLo = thread->readCCReg(regRelIdx);
}

void
TarmacTracerRecord::TraceRegEntry::updateFloat(
    const TarmacContext& tarmCtx,
    RegIndex regRelIdx
)
{
    auto thread = tarmCtx.thread;

    regValid = true;
    regName  = "f" + std::to_string(regRelIdx);
    valueLo = bitsToFloat32(thread->readFloatRegBits(regRelIdx));
}

void
TarmacTracerRecord::TraceRegEntry::updateInt(
    const TarmacContext& tarmCtx,
    RegIndex regRelIdx
)
{
    auto thread = tarmCtx.thread;

    // Reading operating mode from CPSR.
    // This is needed when printing the register name in case
    // of banked register (e.g. lr_svc)
    CPSR cpsr = thread->readMiscRegNoEffect(MISCREG_CPSR);
    OperatingMode mode = (OperatingMode)(uint8_t)cpsr.mode;

    std::string reg_suffix;
    if (mode != MODE_USER) {
        reg_suffix = "_"  + opModeToStr(mode);
    }

    regValid = true;
    switch (regRelIdx) {
      case PCReg:
        regName = "pc";
        break;
      case StackPointerReg:
        regName = "sp" + reg_suffix ;
        break;
      case FramePointerReg:
        regName = "fp" + reg_suffix;
        break;
      case ReturnAddressReg:
        regName = "lr" + reg_suffix;
        break;
      default:
        regName  = "r" + std::to_string(regRelIdx);
        break;
    }
    valueLo = thread->readIntReg(regRelIdx);
}

void
TarmacTracerRecord::addInstEntry(std::vector<InstPtr>& queue,
                                 const TarmacContext& tarmCtx)
{
    // Generate an instruction entry in the record and
    // add it to the Instruction Queue
    queue.push_back(
        m5::make_unique<TraceInstEntry>(tarmCtx, predicate)
    );
}

void
TarmacTracerRecord::addMemEntry(std::vector<MemPtr>& queue,
                                const TarmacContext& tarmCtx)
{
    // Generate a memory entry in the record if the record
    // implies a valid memory access, and add it to the
    // Memory Queue
    if (getMemValid()) {
        queue.push_back(
            m5::make_unique<TraceMemEntry>(tarmCtx,
                                           static_cast<uint8_t>(getSize()),
                                           getAddr(), getIntData())
        );
    }
}

void
TarmacTracerRecord::addRegEntry(std::vector<RegPtr>& queue,
                                const TarmacContext& tarmCtx)
{
    // Generate an entry for every ARM register being
    // written by the current instruction
    for (auto reg = 0; reg < staticInst->numDestRegs(); ++reg) {

        RegId reg_id = staticInst->destRegIdx(reg);

        // Creating a single register change entry
        auto single_reg = genRegister<TraceRegEntry>(tarmCtx, reg_id);

        // Copying the entry and adding it to the "list"
        // of entries to be dumped to trace.
        queue.push_back(
            m5::make_unique<TraceRegEntry>(single_reg)
        );
    }

    // Gem5 is treating CPSR flags as separate registers (CC registers),
    // in contrast with Tarmac specification: we need to merge the gem5 CC
    // entries altogether with the CPSR register and produce a single entry.
    mergeCCEntry<TraceRegEntry>(queue, tarmCtx);
}

void
TarmacTracerRecord::dump()
{
    // Generate and print all the record's entries.
    auto &instQueue = tracer.instQueue;
    auto &memQueue = tracer.memQueue;
    auto &regQueue = tracer.regQueue;

    const TarmacContext tarmCtx(
        thread,
        staticInst->isMicroop()? macroStaticInst : staticInst,
        pc
    );

    if (!staticInst->isMicroop()) {
        // Current instruction is NOT a micro-instruction:
        // Generate Tarmac entries and dump them immediately

        // Generate Tarmac entries and add them to the respective
        // queues.
        addInstEntry(instQueue, tarmCtx);
        addMemEntry(memQueue, tarmCtx);
        addRegEntry(regQueue, tarmCtx);

        // Flush (print) any queued entry.
        flushQueues(instQueue, memQueue, regQueue);

    } else {
        // Current instruction is a micro-instruction:
        // save micro entries into dedicated queues and flush them
        // into the tracefile only when the MACRO-instruction
        // has completed.

        if (staticInst->isFirstMicroop()) {
            addInstEntry(instQueue, tarmCtx);
        }

        addRegEntry(regQueue, tarmCtx);
        addMemEntry(memQueue, tarmCtx);

        if (staticInst->isLastMicroop()) {
            // Flush (print) any queued entry.
            flushQueues(instQueue, memQueue, regQueue);
        }
    }
}

template<typename Queue>
void
TarmacTracerRecord::flushQueues(Queue& queue)
{
    std::ostream &outs = Trace::output();

    for (const auto &single_entry : queue) {
        single_entry->print(outs);
    }

    queue.clear();
}

template<typename Queue, typename... Args>
void
TarmacTracerRecord::flushQueues(Queue& queue, Args & ... args)
{
    flushQueues(queue);
    flushQueues(args...);
}

void
TarmacTracerRecord::TraceInstEntry::print(
    std::ostream& outs,
    int verbosity,
    const std::string &prefix) const
{
    // Pad the opcode
    std::string opcode_str = csprintf("%0*x", instSize >> 2, opcode);

    // Print the instruction record formatted according
    // to the Tarmac specification
    ccprintf(outs, "%s clk %s (%u) %08x %s %s %s_%s : %s\n",
             curTick(),                   /* Tick time */
             taken? "IT" : "IS",          /* Instruction taken/skipped */
             instCount,                   /* Instruction count */
             addr,                        /* Instruction address */
             opcode_str,                  /* Instruction opcode */
             iSetStateToStr(isetstate),   /* Instruction Set */
             opModeToStr(mode),           /* Exception level */
             secureMode? "s" : "ns",      /* Security */
             disassemble);                /* Instruction disass */
}

void
TarmacTracerRecord::TraceMemEntry::print(
    std::ostream& outs,
    int verbosity,
    const std::string &prefix) const
{
    // Print the memory record formatted according
    // to the Tarmac specification
    ccprintf(outs, "%s clk M%s%d %08x %0*x\n",
             curTick(),                 /* Tick time */
             loadAccess? "R" : "W",     /* Access type */
             size,                      /* Access size */
             addr,                      /* Memory address */
             size*2,                    /* Padding with access size */
             data);                     /* Memory data */
}

void
TarmacTracerRecord::TraceRegEntry::print(
    std::ostream& outs,
    int verbosity,
    const std::string &prefix) const
{
    // Print the register record formatted according
    // to the Tarmac specification
    if (regValid)
        ccprintf(outs, "%s clk R %s %08x\n",
                 curTick(),                 /* Tick time */
                 regName,                   /* Register name */
                 valueLo);                  /* Register value */
}

} // namespace Trace