summaryrefslogtreecommitdiffstats
path: root/src/usr/pore/poreve/model/poremodel.C
blob: 0ba57f41cdb0efce93c72a16f7bdb94430a734d6 (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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pore/poreve/model/poremodel.C $                       */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
// $Id: poremodel.C,v 1.25 2013/05/30 00:30:43 bcbrock Exp $

/// \file poremodel.C
/// \brief The PORE hardware engine model and interface to the virtual
/// environment. 

#include "poremodel.H"

using namespace vsbe;


////////////////////////////////////////////////////////////////////////////
// PoreModel
////////////////////////////////////////////////////////////////////////////

/////////////// Common Creation/Control Interface ////////////////////

/// \bug The underlying PORE model does not model reset correctly.  This is
/// fixed here.

int 
PoreModel::restart()
{
    flushReset();
    iv_modelError = ME_SUCCESS;
    iv_instructions = 0;
    iv_stopCode = 0;

    // Begin bug workaround

    registerWriteRaw(PORE_STATUS, 0x0001000000000000ull); // Stack empty

    if (iv_ibufId == PORE_SLW) {
        registerWriteRaw(PORE_CONTROL,0x8005ffffffffffffull);
    } else {
        registerWriteRaw(PORE_CONTROL,0x8000ffffffffffffull);
    }
        
    registerWriteRaw(PORE_RESET,0);

    registerWriteRaw(PORE_ERROR_MASK,0x00bff00000000000ull);

    registerWriteRaw(PORE_PRV_BASE_ADDR0,0);
    registerWriteRaw(PORE_PRV_BASE_ADDR1,0);
    registerWriteRaw(PORE_OCI_MEMORY_BASE_ADDR0,0);
    registerWriteRaw(PORE_OCI_MEMORY_BASE_ADDR1,0);

    if (iv_ibufId == PORE_SBE) {
        registerWriteRaw(PORE_TABLE_BASE_ADDR, 0x0000000100040028ull);
    } else {
        registerWriteRaw(PORE_TABLE_BASE_ADDR, 0);
    }
        
    registerWriteRaw(PORE_EXE_TRIGGER,0);
    registerWriteRaw(PORE_SCRATCH0,0);
    registerWriteRaw(PORE_SCRATCH1,0);
    registerWriteRaw(PORE_SCRATCH2,0);
    registerWriteRaw(PORE_IBUF_01,0);
    registerWriteRaw(PORE_IBUF_2,0);
    registerWriteRaw(PORE_DBG0,0);
    registerWriteRaw(PORE_DBG1,0);
    registerWriteRaw(PORE_PC_STACK0,0);
    registerWriteRaw(PORE_PC_STACK1,0);
    registerWriteRaw(PORE_PC_STACK2,0);

    registerWriteRaw(PORE_ID_FLAGS, iv_ibufId);

    registerWriteRaw(PORE_DATA0,0);
    registerWriteRaw(PORE_MEM_RELOC,0);

    registerWriteRaw(PORE_I2C_E0_PARAM,0x0000000f00000000ull);

    registerWriteRaw(PORE_I2C_E1_PARAM,0);
    registerWriteRaw(PORE_I2C_E2_PARAM,0);

    // End bug workaround

    if (iv_ibufId == PORE_SBE) {
        registerWrite(PORE_EXE_TRIGGER,0);
    }
    
    return getStatus();
}


// run() refuses to do anything if the status shows a model error or an error
// halt. Otherwise the status is used to determine how to restart the machine
// if it is currently stopped. Then the model is stepped until the number of
// instructions has been executed or the status is non-0.  If a debug stop was
// requested this condition is recorded so that PORE_STATUS_DEBUG_STOP can be
// added back to the status at the end.

// Note: The system model or hooks may call modelError() to update the private
// iv_modelError field to record errors that are not otherwise visible to the
// PORE hardware model. This will cause the PORE_STATUS_MODEL_ERROR to be set
// and terminate the run loop.  However note that the instruction will
// complete on the PORE.

// Note: run(0, ran) simply has the effect of making a stopped machine
// runnable.

// Note: This method will need to be overridden/reimplemented in a derived
// class that controls actual PORE hardware or a simulation model.

int
PoreModel::run(const uint64_t i_instructions, uint64_t& o_ran)
{
    uint64_t n = i_instructions;
    uint64_t control;
    ModelError me = ME_SUCCESS;
    bool stepped;
    bool writeControl = false;
    int status;

    do {
        
        o_ran = 0;
        iv_stopCode = 0;
        status = getStatus();

        if (status & (PORE_STATUS_ERROR_HALT | PORE_STATUS_MODEL_ERROR)) {
            break;
        }

        me = registerRead(PORE_CONTROL, control);
        if (me != 0) break;

        if (status & PORE_STATUS_HARDWARE_STOP) {
            control &= ~BE64_BIT(0);
            writeControl = true;
        }
        if (status & (PORE_STATUS_BREAKPOINT | PORE_STATUS_TRAP)) {
            control |= BE64_BIT(1);
            writeControl = true;
        }

        if (writeControl) {
            me = registerWrite(PORE_CONTROL, control);
        }
        if (me != 0) break;
    
        while (n--) {
            me = step(stepped);
            if ((me != 0) || !stepped) {
                break;
            }
            o_ran++;
            iv_instructions++;
            if ((me = getModelError()) != 0) {
                break;
            }
        }

    } while (0);

    if (me != 0) {
        modelError(me);
    }

    return getStatus();
}
   

ModelError
PoreModel::stop(const int i_stopCode)
{
    uint64_t control;
    ModelError me;

    do {
        me = iv_modelError;
        if (me) break;
        me = registerRead(PORE_CONTROL, control);
        if (me) break;
        me = registerWrite(PORE_CONTROL, control | BE64_BIT(0));
        if (me) break;
        if (i_stopCode != 0) {
            iv_stopCode = i_stopCode;
        }
    } while (0);

    return me;
}

ModelError
PoreModel::modelError(const ModelError i_modelError)
{
    if (i_modelError != 0) {
        iv_modelError = i_modelError;
    } 
    return i_modelError;
}


void
PoreModel::clearModelError()
{
    iv_modelError = ME_SUCCESS;
}


ModelError
PoreModel::clearHardwareErrors()
{
    ModelError me;

    me = registerWrite(PORE_DBG0, 0);
    if (me == 0) {
        me = registerWrite(PORE_DBG1, 0);
    }
    return me;
}


// Set the PC as specified, leaving the engine in the stopped state. By
// hardware specification, if the control register is written with the set_pc
// bit set, then the only action is to update the PC, which comes in as the
// low-order 48 bits of the CONTROL register.

ModelError
PoreModel::setPc(const PoreAddress& i_pc)
{
    do {

        if (iv_modelError != 0) break;
        stop(STOP_CODE_SET_PC);
        if (iv_modelError != 0) break;

        iv_modelError = 
            registerWrite(PORE_CONTROL, (uint64_t)i_pc | BE64_BIT(3));

    } while (0);
    return iv_modelError;
}


// The breakpoint address occupies the lower 48 bits of the control register.

ModelError
PoreModel::setBreakpoint(const PoreAddress& i_address)
{
    uint64_t control, address;
    ModelError me;

    do {

        me = registerRead(PORE_CONTROL, control);
        if (me != 0) break;

        address = i_address;
        control &= ~BE64_MASK(16, 63);
        control |= address;

        me = registerWrite(PORE_CONTROL, control);
        if (me != 0) break;

    } while (0);
    return me;
}


// Bit 11 of the PORE CONTROL register is the TRAP enable bit.

ModelError
PoreModel::enableTrap(const bool i_enable)
{
    ModelError me;
    uint64_t control;

    me = registerRead(PORE_CONTROL, control);
    if (me == 0) {
        if (i_enable) {
            control |= BE64_BIT(11); 
        } else {
            control &= ~BE64_BIT(11);
        }
        me = registerWrite(PORE_CONTROL, control);
    }
    return me;
}


// Abstract status bits are set as documented.  If a ModelError is present the
// state is assumed to be corrupted so PORE_STATUS_MODEL_ERROR is the only
// status returned.

int
PoreModel::getStatus()
{

    ModelError me;
    uint64_t control, status, ibuf01, dbg1;
    int finalStatus;
    const unsigned PORE_OPCODE_WAIT = 1;
    const unsigned PORE_OPCODE_TRAP = 2;
    const unsigned PORE_STATE_ABR = 0xb; // PORE HW State machine state

    do {

        finalStatus = 0;
        me = iv_modelError;
        if (me != 0) break;

        me = registerRead(PORE_CONTROL, control);
        if (me != 0) break;
        me = registerRead(PORE_STATUS, status);
        if (me != 0) break;
        me = registerRead(PORE_IBUF_01, ibuf01);
        if (me != 0) break;
        me = registerRead(PORE_DBG1, dbg1);
        if (me != 0) break;

        // Status associated with the hardware stop condition

        if (control & BE64_BIT(0)) {

            finalStatus |= PORE_STATUS_HARDWARE_STOP;

            if ((BE64_GET_FIELD(ibuf01, 0, 6) == PORE_OPCODE_WAIT) &&
                (BE64_GET_FIELD(ibuf01, 8, 31) == 0)) {
                finalStatus |= PORE_STATUS_HALTED;
            }

            if (dbg1 & BE64_BIT(63)) {
                finalStatus |= PORE_STATUS_ERROR_HALT;
            }
        }

        // Status associated with the engine being in the ABR (Address
        // BReakpoint) state.  We need to disambiguate TRAP, BREAKPOINT, and
        // TRAP+BREAKPOINT. This is needlesssly complicated due to lack of
        // direct hardware status.

        if (BE64_GET_FIELD(status, 3, 6) == PORE_STATE_ABR) {
            
            if ((BE64_GET_FIELD(ibuf01, 0, 6) == PORE_OPCODE_TRAP) &&
                BE64_GET_FIELD(control, 11, 11)) {
                finalStatus |= PORE_STATUS_TRAP;
            }

            if (BE64_GET_FIELD(control, 16, 63) == 
                BE64_GET_FIELD(status, 16, 63)) {
                finalStatus |= PORE_STATUS_BREAKPOINT;
            } else {
                if (!(finalStatus & PORE_STATUS_TRAP)) {
                    finalStatus |= PORE_STATUS_BREAKPOINT;
                }
            }
        }
    } while (0);

    if (iv_stopCode != 0) {
        finalStatus |= PORE_STATUS_DEBUG_STOP;
    }

    if (me != 0) {
        modelError(me);
        finalStatus = PORE_STATUS_MODEL_ERROR;
    }

    return finalStatus;
}


ModelError
PoreModel::getModelError()
{
    return iv_modelError;
}


uint64_t
PoreModel::getInstructions()
{
    return iv_instructions;
}


int
PoreModel::getStopCode()
{
    return iv_stopCode;
}


// 'Ram' a load or store instruction without modifying the final state of the
// PORE. 

static ModelError
_ramLoadStore(PoreModel& io_pore, 
     uint32_t i_instruction, uint64_t i_immediate,
     uint64_t& io_d0, uint64_t i_a0, uint8_t i_p0)
{
    ModelError me;
    PoreState state;
    uint64_t control;
    bool stateExtracted;

    do {

        // Extract the state, then load up the caller's register state

        stateExtracted = false;

        me = io_pore.extractState(state);
        if (me) break;

        stateExtracted = true;

        me = io_pore.registerWrite(PORE_SCRATCH1, io_d0);
        if (me) break;
        me = io_pore.registerWrite(PORE_OCI_MEMORY_BASE_ADDR0, i_a0);
        if (me) break;
        me = io_pore.registerWrite(PORE_PRV_BASE_ADDR0, i_p0);
        if (me) break;


        // Stop the engine (set control register bit 0) and ram the
        // instruction by writing PORE_IBUF_01.  The model executes the
        // instruction immediately w/o the necessity of run()-ing it.

        me = io_pore.registerRead(PORE_CONTROL, control);
        if (me) break;
        me = io_pore.registerWrite(PORE_CONTROL, control | BE64_BIT(0));
        if (me) break;

        me = io_pore.registerWrite(PORE_IBUF_2, i_immediate << 32);
        if (me) break;
        me = io_pore.registerWrite(PORE_IBUF_01, 
                                   (((uint64_t)i_instruction) << 32) |
                                   i_immediate >> 32);
        if (me) break;

        
        // Extract the return value of D0

        me = io_pore.registerRead(PORE_SCRATCH1, io_d0);
        if (me) break;

    } while (0);

    if (stateExtracted) {
        me = io_pore.installState(state);
    }

    return me;
}
        

ModelError
PoreModel::getmemInteger(const PoreAddress& i_address, 
                         uint64_t& o_data,
                         const size_t i_size)
{
    PoreAddress address;
    int byte;
    ModelError me;

    union {
        uint64_t u64;
        uint32_t u32[2];
        uint16_t u16[4];
        uint8_t u8[8];
    } data;

    do {

        // Check the address legality and alignment vis-a-vis the size, and
        // align the address to an 8-byte boundary.

        address = i_address;

        if (!(address.iv_memorySpace & 0x8000) ||
            ((address.iv_offset & (i_size - 1)) != 0)) {
            me = ME_INVALID_ARGUMENT;
            break;
        }

        byte = address.iv_offset % 8;
        address.iv_offset -= byte;

        
        // Ram "LD D0, 0, A0", then pull out the requested bytes in host
        // format.

        me = _ramLoadStore(*this, 
                           0x64800000, 0, 
                           data.u64, (uint64_t)address, 0);
        if (me) break;

#ifndef _BIG_ENDIAN
        byte = 7 - byte;
#endif

        switch (i_size) {
            
        case 1: o_data = data.u8[byte]; break;
        case 2: o_data = data.u16[byte / 2]; break;
        case 4: o_data = data.u32[byte / 4]; break;
        case 8: o_data = data.u64; break;
        default: me = ME_INVALID_ARGUMENT;

        }

        if (me) break;

    } while (0);

    return me;
}


ModelError
PoreModel::putmemInteger(const PoreAddress& i_address, 
                         const uint64_t i_data,
                         const size_t i_size)
{
    PoreAddress address;
    int byte;
    uint64_t d0;
    ModelError me;

    do {

        // Check the address and size for legality.

        address = i_address;

        if ((i_size != 8) ||
            !(address.iv_memorySpace & 0x8000) ||
            ((address.iv_offset & (i_size - 1)) != 0)) {
            me = ME_INVALID_ARGUMENT;
            break;
        }

        byte = address.iv_offset % 8;
        address.iv_offset -= byte;


        // Ram "STD D0, 0, A0"

        d0 = i_data;
        me = _ramLoadStore(*this, 
                           0x72800000, 0, 
                           d0, (uint64_t)address, 0);
        if (me) break;

    } while (0);

    return me;
}


////////////////////  PoreInterface Methods  /////////////////////////

// The interface methods are responsible for ensuring that any errors are
// captured in the iv_modelError member of the PoreModel.

void
PoreModel::pibMaster(PibTransaction& transaction)
{
    iv_interface->pibMaster(transaction);
}


void
PoreModel::ociMaster(OciTransaction& transaction)
{
    iv_interface->ociMaster(transaction);
}


void
PoreModel::wait(const uint32_t i_count) 
{
    iv_interface->wait(i_count);
}


void
PoreModel::hookInstruction(const PoreAddress& i_address, 
                           const uint32_t i_hook,
                           const uint64_t i_parameter) 
{
    iv_interface->hookInstruction(i_address, i_hook, i_parameter);
}


void
PoreModel::hookRead(const PoreAddress& i_address) 
{
    iv_interface->hookRead(i_address);
}


void
PoreModel::hookWrite(const PoreAddress& i_address) 
{
    iv_interface->hookWrite(i_address);
}


void
PoreModel::hookFetch(const PoreAddress& i_address) 
{
    iv_interface->hookFetch(i_address);
}


void
PoreModel::errorIntr(void)
{
    iv_interface->errorIntr();
}


void
PoreModel::fatalErrorIntr(void)
{
    iv_interface->fatalErrorIntr();
}


////////////////////////////// Creators //////////////////////////////

PoreModel::PoreModel(PoreIbufId i_id, PoreInterface *i_interface) :
    iv_ibufId(i_id),
    iv_modelError(ME_PORE_UNINITIALIZED),
    iv_instructions(0),
    iv_stopCode(0),
    iv_interface(i_interface)
{
}


PoreModel::~PoreModel()
{
}



        
OpenPOWER on IntegriCloud