summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/attrrp.C
blob: b4d5ce155b6c4c47cf7f69b5f0f276d9ff668695 (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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/targeting/attrrp.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2017                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* 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                                                     */
/**
 *  @file targeting/attrrp.C
 *
 *  @brief Attribute resource provider implementation which establishes and
 *      initializes virtual memory ranges for attributes as needed, and works
 *      with other resource providers (such as the PNOR resource provider) to
 *      retrieve attribute data which it connot directly provide.
 */

#include <util/singleton.H>
#include <pnortargeting.H>
#include <pnor/pnorif.H>
#include <sys/mm.h>
#include <errno.h>
#include <string.h>
#include <algorithm>
#include <vmmconst.h>
#include <targeting/adapters/assertadapter.H>
#include <targeting/common/targreasoncodes.H>
#include <targeting/targplatreasoncodes.H>
#include <targeting/attrrp.H>
#include <targeting/common/trace.H>
#include <targeting/common/attributeTank.H>
#include <initservice/initserviceif.H>
#include <util/align.H>
#include <util/utilrsvdmem.H>
#include <sys/misc.h>
#include <fapi2/plat_attr_override_sync.H>
#include <targeting/attrPlatOverride.H>
#include <config.h>
#include <secureboot/service.H>
#include <kernel/bltohbdatamgr.H>
#include <bootloader/bootloaderif.H>
#include <sbeio/sbeioif.H>

using namespace INITSERVICE;
using namespace ERRORLOG;

#include "attrrp_common.C"

namespace TARGETING
{

    const char* ATTRRP_MSG_Q = "attrrpq";

    void* AttrRP::getBaseAddress(const NODE_ID i_nodeIdUnused)
    {
        return reinterpret_cast<void*>(VMM_VADDR_ATTR_RP);
    }

    void* AttrRP::startMsgServiceTask(void* i_pInstance)
    {
        // Call msgServiceTask loop on instance.
        TARG_ASSERT(i_pInstance, "No instance passed to startMsgServiceTask");
        static_cast<AttrRP*>(i_pInstance)->msgServiceTask();
        return NULL;
    }

    void AttrRP::startup(errlHndl_t& io_taskRetErrl, bool i_isMpipl)
    {
        errlHndl_t l_errl = NULL;

        do
        {
            iv_isMpipl = i_isMpipl;
            // Parse PNOR headers.
            l_errl = this->parseAttrSectHeader();
            if (l_errl)
            {
                break;
            }

            // Create corresponding VMM blocks for each section.
            l_errl = this->createVmmSections();
            if (l_errl)
            {
                break;
            }

            // Spawn daemon thread.
            task_create(&AttrRP::startMsgServiceTask, this);

            if(iv_isMpipl)
            {
                populateAttrsForMpipl();
            }



        } while (false);

        // If an error occurred, post to TaskArgs.
        if (l_errl)
        {
            l_errl->setSev(ERRORLOG::ERRL_SEV_UNRECOVERABLE);
        }

        //  return any errlogs to _start()
        io_taskRetErrl = l_errl;
    }

    void AttrRP::msgServiceTask() const
    {
        TRACFCOMP(g_trac_targeting, ENTER_MRK "AttrRP::msgServiceTask");

        // Daemon loop.
        while(1)
        {
            int rc = 0;

            // Wait for message.
            msg_t* msg = msg_wait(iv_msgQ);
            if (!msg) continue;

            // Parse message data members.
            uint64_t vAddr = 0;
            void*    pAddr = nullptr;
            ssize_t  section = -1;
            uint64_t offset = 0;
            uint64_t size = 0;

            TRACFCOMP(g_trac_targeting, INFO_MRK "AttrRP: Message recv'd: "
                        "0x%x");

            do {

                if (msg->type != MSG_MM_RP_RUNTIME_PREP)
                {
                    vAddr = msg->data[0];
                    pAddr = reinterpret_cast<void*>(msg->data[1]);

                    TRACFCOMP(g_trac_targeting,
                        INFO_MRK "AttrRP: vAddr=0x%lx pAddr=0x%p",
                        msg->type, vAddr, pAddr);

                    // Locate corresponding attribute section for message.
                    for (size_t i = 0; i < iv_sectionCount; ++i)
                    {
                        if ((vAddr >= iv_sections[i].vmmAddress) &&
                            (vAddr < iv_sections[i].vmmAddress +
                                                         iv_sections[i].size))
                        {
                            section = i;
                            break;
                        }
                    }

                    // Return EINVAL if no section was found.  Kernel bug?
                    if (section == -1)
                    {
                        rc = -EINVAL;
                        TRACFCOMP(g_trac_targeting,
                              ERR_MRK "AttrRP: Address given outside section "
                                      "ranges: %p",
                              vAddr);
                        break; // go to error handler
                    }

                    // Determine PNOR offset and page size.
                    offset = vAddr - iv_sections[section].vmmAddress;
                    size = std::min(PAGE_SIZE,
                                         iv_sections[section].vmmAddress +
                                         iv_sections[section].size -
                                         vAddr);
                    // We could be requested to read/write less than a page
                    // if the virtual address requested is at the end of the
                    // section and the section size is not page aligned.
                    //
                    // Example: Section size is 6k and vAddr = vmmAddr + 4k,
                    //          we should only operate on 2k of content.


                }

                // Process request.

                // Read / Write message behavior.
                switch(msg->type)
                {
                    case MSG_MM_RP_READ:
                        // HEAP_ZERO_INIT should never be requested for read
                        // because kernel should automatically get a zero page.
                        if ( (iv_sections[section].type ==
                              SECTION_TYPE_HEAP_ZERO_INIT) ||
                             (iv_sections[section].type ==
                              SECTION_TYPE_HB_HEAP_ZERO_INIT) )
                        {
                            TRACFCOMP(g_trac_targeting,
                                      ERR_MRK "AttrRP: Read request on "
                                              "HEAP_ZERO section.");
                            rc = -EINVAL;
                            break;
                        }
                        // if we are NOT in mpipl OR if this IS a r/w section,
                        // Do a memcpy from PNOR address into physical page.
                        if(!iv_isMpipl || (iv_sections[section].type == SECTION_TYPE_PNOR_RW)  )
                        {
                            memcpy(pAddr,
                                reinterpret_cast<void*>(
                                        iv_sections[section].pnorAddress + offset),
                                size);
                        }
                        else
                        {
                            // Do memcpy from real memory into physical page.
                            memcpy(pAddr,
                                   reinterpret_cast<void*>(
                                   iv_sections[section].realMemAddress + offset),
                                   size);
                        }
                        break;

                    case MSG_MM_RP_WRITE:
                        // Only PNOR_RW should ever be requested for write-back
                        // because others are not allowed to be pushed back to
                        // PNOR.
                        if (iv_sections[section].type !=
                            SECTION_TYPE_PNOR_RW)
                        {
                            TRACFCOMP(g_trac_targeting,
                                      ERR_MRK "AttrRP: Write request on "
                                              "non-PNOR_RW section.");
                            rc = -EINVAL;
                            break;
                        }
                        // Do memcpy from physical page into PNOR.
                        memcpy(reinterpret_cast<void*>(
                                    iv_sections[section].pnorAddress + offset),
                               pAddr,
                               size);
                        break;
                    case MSG_MM_RP_RUNTIME_PREP:
                    {
                        // used for security purposes to pin all the attribute
                        // memory just prior to copying to reserve memory
                        uint64_t l_access =
                            msg->data[0] == MSG_MM_RP_RUNTIME_PREP_BEGIN?
                                WRITABLE:
                            msg->data[0] == MSG_MM_RP_RUNTIME_PREP_END?
                                WRITE_TRACKED: 0;
                        if (!l_access)
                        {
                            rc = -EINVAL;
                            break;
                        }

                        for (size_t i = 0; i < iv_sectionCount; ++i)
                        {
                            if ( iv_sections[i].type == SECTION_TYPE_PNOR_RW)
                            {
                                rc = mm_set_permission(reinterpret_cast<void*>(
                                       iv_sections[i].vmmAddress),
                                       iv_sections[i].size,
                                       l_access);
                            }
                        }
                        break;
                    }
                    default:
                        TRACFCOMP(g_trac_targeting,
                                  ERR_MRK "AttrRP: Unhandled command type %d.",
                                  msg->type);
                        rc = -EINVAL;
                        break;
                }

            } while (0);

            // Log an error log if the AttrRP was unable to handle a message
            // for any reason.
            if (rc != 0)
            {
                /*@
                 *   @errortype         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                 *   @moduleid          TARG_MSG_SERVICE_TASK
                 *   @reasoncode        TARG_RC_ATTR_MSG_FAIL
                 *   @userdata1         Virtual Address
                 *   @userdata2         (Msg Type << 32) | Section #
                 *
                 *   @devdesc   The attribute resource provider was unable to
                 *              satisfy a message request from the VMM portion
                 *              of the kernel.  This was either due to an
                 *              address outside a valid range or a message
                 *              request that is invalid for the attribute
                 *              section containing the address.
                 *
                 *   @custdesc  Attribute Resource Provider failed to handle
                 *              request
                 */
                const bool hbSwError = true;
                errlHndl_t l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                                  TARG_MSG_SERVICE_TASK,
                                                  TARG_RC_ATTR_MSG_FAIL,
                                                  vAddr,
                                                  TWO_UINT32_TO_UINT64(
                                                        msg->type,
                                                        section),
                                                  hbSwError);
                errlCommit(l_errl,TARG_COMP_ID);
            }

            // Respond to request.
            msg->data[1] = rc;
            rc = msg_respond(iv_msgQ, msg);
            if (rc)
            {
                TRACFCOMP(g_trac_targeting,
                          ERR_MRK "AttrRP: Bad rc from msg_respond: %d", rc);
            }
        }
    }

    errlHndl_t AttrRP::parseAttrSectHeader()
    {
        errlHndl_t l_errl = NULL;

        do
        {
            #ifdef CONFIG_SECUREBOOT
            // Securely load HB_DATA section
            l_errl = PNOR::loadSecureSection(PNOR::HB_DATA);
            if (l_errl)
            {
                break;
            }
            #endif

            // Locate attribute section in PNOR.
            PNOR::SectionInfo_t l_pnorSectionInfo;
            TargetingHeader* l_header = nullptr;
            l_errl = PNOR::getSectionInfo(PNOR::HB_DATA,
                                          l_pnorSectionInfo);
            if(l_errl)
            {
                break;
            }

            if(!iv_isMpipl)
            {
                // Find attribute section header.
                l_header =
                reinterpret_cast<TargetingHeader*>(l_pnorSectionInfo.vaddr);
            }
            else
            {
                TRACFCOMP(g_trac_targeting,
                           "Reading attributes from memory, NOT PNOR");
                //Create a block map of the address space we used to store
                //attribute information on the initial IPL
                //Account HRMOR (non 0 base addr)

                ///////////////////////////////////////////////////////////////
                // This should change to get address from SBE.  Currently hack
                // to the start of ATTR data section on FSP systems
                uint64_t l_phys_attr_data_addr = 0;
                uint64_t l_attr_data_size = 0;

                // Setup physical TOC address
                uint64_t l_toc_addr = 0;

                Bootloader::keyAddrPair_t l_keyAddrPairs =
                    g_BlToHbDataManager.getKeyAddrPairs();

                for (uint8_t keyIndex = 0; keyIndex < MAX_ROW_COUNT; keyIndex++)
                {
                    if(l_keyAddrPairs.key[keyIndex] == SBEIO::RSV_MEM_ATTR_ADDR)
                    {
                        l_toc_addr = l_keyAddrPairs.addr[keyIndex];
                    }
                }

                if(!l_toc_addr)
                {
                    // Setup physical TOC address to hardcoded value
                    l_toc_addr = cpu_spr_value(CPU_SPR_HRMOR) +
                                        VMM_HB_DATA_TOC_START_OFFSET;
                }

                // Now map the TOC to find the ATTR label address & size
                Util::hbrtTableOfContents_t * l_toc_ptr =
                        reinterpret_cast<Util::hbrtTableOfContents_t *>(
                            mm_block_map(reinterpret_cast<void*>(l_toc_addr),
                            sizeof(Util::hbrtTableOfContents_t)));

                if (l_toc_ptr != 0)
                {
                    // read the TOC and look for ATTR data section
                    uint64_t l_attr_data_addr = Util::hb_find_rsvd_mem_label(
                                                    Util::HBRT_MEM_LABEL_ATTR,
                                                    l_toc_ptr,
                                                    l_attr_data_size);

                    // calculate the offset from the start of the TOC
                    uint64_t l_attr_offset = l_attr_data_addr -
                                        reinterpret_cast<uint64_t>(l_toc_ptr);

                    // Setup where the ATTR data can be found
                    l_phys_attr_data_addr = l_toc_addr + l_attr_offset;

                    // Clear the mapped memory for the TOC
                    int l_rc = mm_block_unmap(
                                    reinterpret_cast<void*>(l_toc_ptr));
                    if(l_rc)
                    {
                        TRACFCOMP( g_trac_targeting,
                           "parseAttrSectHeader. fail to unmap virt addr %p, "
                           " rc = %d",
                           reinterpret_cast<void*>(l_toc_ptr), l_rc);
                        //Error mm_block_unmap returned non-zero
                        /*@
                        *   @errortype         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                        *   @moduleid          TARG_PARSE_ATTR_SECT_HEADER
                        *   @reasoncode        TARG_RC_MM_BLOCK_UNMAP_FAIL
                        *   @userdata1         return code
                        *   @userdata2         Unmap virtual address
                        *
                        *   @devdesc   While attempting to unmap a virtual
                        *              addr for our targeting information the
                        *              kernel returned an error
                        *   @custdesc  Kernel failed to unblock mapped memory
                        */
                        l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                               TARG_PARSE_ATTR_SECT_HEADER,
                                               TARG_RC_MM_BLOCK_FAIL,
                                               l_rc,
                                               reinterpret_cast<uint64_t>
                                                (l_toc_ptr),
                                               true);
                        break;
                    }

                    // Now just map the ATTR data section
                    l_header = reinterpret_cast<TargetingHeader*>(
                        mm_block_map(
                            reinterpret_cast<void*>(l_phys_attr_data_addr),
                            l_attr_data_size));
                }
                else
                {
                    TRACFCOMP(g_trac_targeting,
                              "Failed mapping Table of Contents section");
                    l_header = 0;
                    l_phys_attr_data_addr = l_toc_addr;
                    l_attr_data_size = sizeof(Util::hbrtTableOfContents_t);
                }
                ///////////////////////////////////////////////////////////////

                if(l_header == 0)
                {
                    TRACFCOMP(g_trac_targeting,
                              "Failed mapping phys addr: %p for %lx bytes",
                              l_phys_attr_data_addr,
                              l_attr_data_size);
                    //Error mm_block_map returned invalid ptr
                    /*@
                    *   @errortype         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                    *   @moduleid          TARG_PARSE_ATTR_SECT_HEADER
                    *   @reasoncode        TARG_RC_MM_BLOCK_MAP_FAIL
                    *   @userdata1         physical address of target info
                    *   @userdata2         size we tried to map
                    *
                    *   @devdesc   While attempting to map a phys addr to a virtual
                    *              addr for our targeting information the kernel
                    *              returned an error
                    *   @custdesc  Kernel failed to block map memory
                    */
                    l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                           TARG_PARSE_ATTR_SECT_HEADER,
                                           TARG_RC_MM_BLOCK_FAIL,
                                           l_phys_attr_data_addr,
                                           l_attr_data_size,
                                           true);
                    break;
                }
                TRACFCOMP(g_trac_targeting,
                          "Mapped phys addr: %p to virt addr: %p",
                          reinterpret_cast<void*>(l_phys_attr_data_addr),
                          l_header);
            }


            // Validate eye catch.
            if (l_header->eyeCatcher != PNOR_TARG_EYE_CATCHER)
            {
                TRACFCOMP(g_trac_targeting,
                          "ATTR_DATA section in pnor header mismatch found"
                          " header: %d expected header: %d",
                          l_header->eyeCatcher,
                          PNOR_TARG_EYE_CATCHER);
                /*@
                 *   @errortype         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                 *   @moduleid          TARG_PARSE_ATTR_SECT_HEADER
                 *   @reasoncode        TARG_RC_BAD_EYECATCH
                 *   @userdata1         Observed Header Eyecatch Value
                 *   @userdata2         Expected Eyecatch Value
                 *
                 *   @devdesc   The eyecatch value observed in PNOR does not
                 *              match the expected value of
                 *              PNOR_TARG_EYE_CATCHER and therefore the
                 *              contents of the Attribute PNOR section are
                 *              unable to be parsed.
                 *   @custdesc  A problem occurred during the IPL of the
                 *              system.
                 *              The eyecatch value observed in memory does not
                 *              match the expected value and therefore the
                 *              contents of the attribute sections are unable
                 *              to be parsed.
                */
                l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                       TARG_PARSE_ATTR_SECT_HEADER,
                                       TARG_RC_BAD_EYECATCH,
                                       l_header->eyeCatcher,
                                       PNOR_TARG_EYE_CATCHER);
                break;
            }

            // Allocate section structures based on section count in header.
            iv_sectionCount = l_header->numSections;
            iv_sections = new AttrRP_Section[iv_sectionCount];

            TargetingSection* l_section = nullptr;
            if(!iv_isMpipl)
            {
                // Find start to first section:
                //          (PNOR addr + size of header + offset in header).
                l_section =
                        reinterpret_cast<TargetingSection*>(
                                l_pnorSectionInfo.vaddr + sizeof(TargetingHeader) +
                                l_header->offsetToSections
                        );
            }
            else
            {
                // Find start to first section:
                // (header address + size of header + offset in header)
                l_section =
                    reinterpret_cast<TargetingSection*>(
                        reinterpret_cast<uint64_t>(l_header) +
                        sizeof(TargetingHeader) + l_header->offsetToSections
                        );

            }

            //Keep a running offset of how far into our real memory section we are
            uint64_t l_realMemOffset = 0;

            // Parse each section.
            for (size_t i = 0; i < iv_sectionCount; i++, ++l_section)
            {
                iv_sections[i].type = l_section->sectionType;

                // Conversion cast for templated abstract pointer object only
                // works when casting to pointer of the templated type.  Since
                // cache is of a different type, we first cast to extract the
                // real pointer, then recast it into the cache
                iv_sections[i].vmmAddress =
                        static_cast<uint64_t>(
                            TARG_TO_PLAT_PTR(l_header->vmmBaseAddress)) +
                        l_header->vmmSectionOffset*i;


                iv_sections[i].pnorAddress =
                    l_pnorSectionInfo.vaddr + l_section->sectionOffset;

                #ifdef CONFIG_SECUREBOOT
                // RW targeting section is part of the unprotected payload
                // so use the normal PNOR virtual address space
                if(   l_pnorSectionInfo.secure
                   && iv_sections[i].type == SECTION_TYPE_PNOR_RW)
                {
                    iv_sections[i].pnorAddress -=
                        (VMM_VADDR_SPNOR_DELTA + VMM_VADDR_SPNOR_DELTA);
                }
                #endif

                if(iv_isMpipl)
                {
                    //For MPIPL we are reading from real memory,
                    //not pnor flash. Set the real memory address
                    iv_sections[i].realMemAddress =
                        reinterpret_cast<uint64_t>(l_header) + l_realMemOffset;
                }
                iv_sections[i].size = l_section->sectionSize;

                //Increment our offset variable by the size of this section
                l_realMemOffset += iv_sections[i].size;

                TRACFCOMP(g_trac_targeting,
                          "Decoded Attribute Section: %d, 0x%lx 0x%lx 0x%lx 0x%lx",
                          iv_sections[i].type,
                          iv_sections[i].vmmAddress,
                          iv_sections[i].pnorAddress,
                          iv_sections[i].realMemAddress,
                          iv_sections[i].size);

            }

        } while (false);

        return l_errl;

    }

    errlHndl_t AttrRP::createVmmSections()
    {
        errlHndl_t l_errl = NULL;

        do
        {
            // Allocate message queue for VMM requests.
            iv_msgQ = msg_q_create();

            // register it so it can be discovered by istep 21 and thus allow
            // secure runtime preparation of persistent r/w attributes
            int rc = msg_q_register(iv_msgQ, ATTRRP_MSG_Q);

            assert(rc == 0, "Bug! Unable to register message queue");

            // Create VMM block for each section, assign permissions.
            for (size_t i = 0; i < iv_sectionCount; ++i)
            {
                uint64_t l_perm = 0;
                switch(iv_sections[i].type)
                {
                    case SECTION_TYPE_PNOR_RO:
                        l_perm = READ_ONLY;
                        break;

                    case SECTION_TYPE_PNOR_RW:
                        l_perm = WRITABLE | WRITE_TRACKED;
                        break;

                    case SECTION_TYPE_HEAP_PNOR_INIT:
                        l_perm = WRITABLE;
                        break;

                    case SECTION_TYPE_HEAP_ZERO_INIT:
                    case SECTION_TYPE_HB_HEAP_ZERO_INIT:
                        l_perm = WRITABLE | ALLOCATE_FROM_ZERO;
                        break;

                    default:

                        /*@
                         *   @errortype  ERRORLOG::ERRL_SEV_UNRECOVERABLE
                         *   @moduleid   TARG_CREATE_VMM_SECTIONS
                         *   @reasoncode TARG_RC_UNHANDLED_ATTR_SEC_TYPE
                         *   @userdata1  Section type
                         *
                         *   @devdesc    Found unhandled attribute section type
                         *   @custdesc   FW error, unexpected Attribute section type
                         */
                        const bool hbSwError = true;
                        l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                               TARG_CREATE_VMM_SECTIONS,
                                               TARG_RC_UNHANDLED_ATTR_SEC_TYPE,
                                               iv_sections[i].type,
                                               0, hbSwError);
                        break;
                }

                if(l_errl)
                {
                    break;
                }

                int rc = 0;
                msg_q_t l_msgQ = iv_msgQ;

                if ( (iv_sections[i].type == SECTION_TYPE_HEAP_ZERO_INIT) ||
                     (iv_sections[i].type == SECTION_TYPE_HB_HEAP_ZERO_INIT) )
                {
                    l_msgQ = NULL;
                }

                rc = mm_alloc_block(l_msgQ,
                                    reinterpret_cast<void*>(iv_sections[i].vmmAddress),
                                    iv_sections[i].size);

                if (rc)
                {
                    /*@
                     *   @errortype         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                     *   @moduleid          TARG_CREATE_VMM_SECTIONS
                     *   @reasoncode        TARG_RC_MM_BLOCK_FAIL
                     *   @userdata1         vAddress attempting to allocate.
                     *   @userdata2         RC from kernel.
                     *
                     *   @devdesc   While attempting to allocate a virtual
                     *              memory block for an attribute section, the
                     *              kernel returned an error.
                     *   @custdesc  Kernel failed to block memory
                     */
                    const bool hbSwError = true;
                    l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                           TARG_CREATE_VMM_SECTIONS,
                                           TARG_RC_MM_BLOCK_FAIL,
                                           iv_sections[i].vmmAddress,
                                           rc, hbSwError);
                    break;
                }

                if(iv_sections[i].type == SECTION_TYPE_PNOR_RW)
                {
                    // TODO RTC:164480 For MPIPL we need to map the RW section
                    // in real memory to virtual address of the section in PNOR
                    /*
                     * Register this memory range to be FLUSHed during
                     * a shutdown.
                     */
                    INITSERVICE::registerBlock(
                        reinterpret_cast<void*>(iv_sections[i].vmmAddress),
                        iv_sections[i].size,ATTR_PRIORITY);
                }

                rc = mm_set_permission(reinterpret_cast<void*>(
                                       iv_sections[i].vmmAddress),
                                       iv_sections[i].size,
                                       l_perm);

                if (rc)
                {
                    /*@
                     *   @errortype         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                     *   @moduleid          TARG_CREATE_VMM_SECTIONS
                     *   @reasoncode        TARG_RC_MM_PERM_FAIL
                     *   @userdata1         vAddress attempting to allocate.
                     *   @userdata2         (kernel-rc << 32) | (Permissions)
                     *
                     *   @devdesc   While attempting to set permissions on
                     *              a virtual memory block for an attribute
                     *              section, the kernel returned an error.
                     *
                     *   @custdesc  Kernel failed to set permissions on
                     *              virtual memory block
                     */
                    const bool hbSwError = true;
                    l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                           TARG_CREATE_VMM_SECTIONS,
                                           TARG_RC_MM_PERM_FAIL,
                                           iv_sections[i].vmmAddress,
                                           TWO_UINT32_TO_UINT64(rc, l_perm),
                                           hbSwError);
                    break;
                }
            } // End iteration through each section

            if(l_errl)
            {
                break;
            }

        } while (false);

        return l_errl;
    }

    void AttrRP::populateAttrsForMpipl()
    {
        do
        {
            // Copy RW, Heap Zero Init sections because we are not
            // running the isteps that set these attrs during MPIPL
            for (size_t i = 0; i < iv_sectionCount; ++i)
            {
                // The volatile sections in MPIPL need to be copied because
                // on the MPIPL flow we will not run the HWPs that set these attrs
                // the RW section of the attribute data must be copied
                // into the vmmAddress in order to make future r/w come
                // from the pnor address, not real memory
                if(((iv_sections[i].type == SECTION_TYPE_HEAP_ZERO_INIT) ||
                    (iv_sections[i].type == SECTION_TYPE_HB_HEAP_ZERO_INIT) ||
                    (iv_sections[i].type == SECTION_TYPE_PNOR_RW)) &&
                    iv_isMpipl)
                {
                    memcpy(reinterpret_cast<void*>(iv_sections[i].vmmAddress),
                        reinterpret_cast<void*>(iv_sections[i].realMemAddress),
                        (iv_sections[i].size));
                }

            }
        }while(0);
    }

    void* AttrRP::save(uint64_t& io_addr)
    {
        // Call save on singleton instance.
        return Singleton<AttrRP>::instance()._save(io_addr);
    }

    errlHndl_t AttrRP::save( uint8_t* i_dest, size_t& io_size )
    {
        // Call save on singleton instance.
        return Singleton<AttrRP>::instance()._save(i_dest,io_size);
    }

    uint64_t  AttrRP::maxSize( )
    {
        // Find total size of the sections.
        uint64_t l_size = 0;
        for( size_t i = 0;
             i < Singleton<AttrRP>::instance().iv_sectionCount;
             ++i)
        {
            l_size += ALIGN_PAGE(Singleton<AttrRP>::
                          instance().iv_sections[i].size);
        }

        return(l_size);

    } // end maxSize


    errlHndl_t AttrRP::saveOverrides( uint8_t* i_dest, size_t& io_size )
    {
        // Call save on singleton instance.
        return Singleton<AttrRP>::instance()._saveOverrides(i_dest,io_size);
    }


    void* AttrRP::_save(uint64_t& io_addr)
    {
        TRACDCOMP(g_trac_targeting, "AttrRP::save: top @ 0x%lx", io_addr);

        void* region = reinterpret_cast<void*>(io_addr);
        uint8_t* pointer = reinterpret_cast<uint8_t*>(region);

        if (TARGETING::is_no_load())
        {
            // Find total size of the sections.
            uint64_t l_size = maxSize();

            io_addr = ALIGN_PAGE_DOWN(io_addr);
            // Determine bottom of the address region.
            io_addr = io_addr - l_size;
            // Align to 64KB for No Payload
            io_addr = ALIGN_DOWN_X(io_addr,64*KILOBYTE);
            // Map in region.
            region = mm_block_map(reinterpret_cast<void*>(io_addr),l_size);
            pointer = reinterpret_cast<uint8_t*>(region);
        }

        // Copy content.
        for (size_t i = 0; i < iv_sectionCount; ++i)
        {
            memcpy(pointer,
                   reinterpret_cast<void*>(iv_sections[i].vmmAddress),
                   iv_sections[i].size);

            pointer = &pointer[ALIGN_PAGE(iv_sections[i].size)];
        }

        TRACFCOMP(g_trac_targeting, "AttrRP::save: bottom @ 0x%lx", io_addr);
        return region;
    }

    errlHndl_t AttrRP::_save( uint8_t* i_dest, size_t& io_size )
    {
        TRACFCOMP( g_trac_targeting, ENTER_MRK"AttrRP::_save: i_dest=%p, io_size=%ld", i_dest, io_size );
        errlHndl_t l_err = nullptr;
        uint8_t* pointer = i_dest;
        uint64_t l_totalSize = 0;
        uint64_t l_maxSize = io_size;
        uint64_t l_filledSize = 0;

        // Copy content.
        for (size_t i = 0; i < iv_sectionCount; ++i)
        {
            l_totalSize += iv_sections[i].size;
            if (l_totalSize <= l_maxSize)
            {
                l_filledSize = l_totalSize;
                memcpy(pointer,
                       reinterpret_cast<void*>(iv_sections[i].vmmAddress),
                       iv_sections[i].size);

                pointer = &pointer[ALIGN_PAGE(iv_sections[i].size)];
            }
            else
            {
                // Need a larger buffer
                TRACFCOMP( g_trac_targeting, ERR_MRK"AttrRP::_save - max size %d exceeded, missing section %d, size %d",
                    io_size,i, iv_sections[i].size);
            }
        }

        if (l_totalSize > io_size)
        {
            // Need to increase size of the buffer
             /*@
                 *   @errortype
                 *   @moduleid          TARG_MOD_SAVE_ATTR_TANK
                 *   @reasoncode        TARG_SPACE_OVERRUN
                 *   @userdata1         Maximum Available size
                 *   @userdata2         Required size
                 *
                 *   @devdesc   Size of attribute data exceeds available
                 *              buffer space
                 *
                 *   @custdesc  Internal firmware error applying
                 *              custom configuration settings
                 */
                l_err = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                      TARG_MOD_SAVE_ATTR_TANK,
                                      TARG_SPACE_OVERRUN,
                                      io_size,
                                      l_totalSize,
                                      true /*SW Error */);
        }

        io_size = l_filledSize;

        TRACFCOMP(g_trac_targeting, EXIT_MRK"AttrRP::_save: i_dest=%p, io_size=%ld, size needed=%ld", i_dest, io_size, l_totalSize );
        return l_err;
    }



    errlHndl_t AttrRP::_saveOverrides( uint8_t* i_dest, size_t& io_size )
    {
        TRACFCOMP( g_trac_targeting, ENTER_MRK"AttrRP::_saveOverrides: i_dest=%p, io_size=%d", i_dest, io_size );
        errlHndl_t l_err = nullptr;

        do
        {
            size_t l_maxSize = io_size;
            io_size = 0;

            if (!SECUREBOOT::allowAttrOverrides())
            {
                TRACFCOMP( g_trac_targeting, "AttrRP::_saveOverrides: skipping "
                           "since Attribute Overrides are not allowed.");
            }

            // Save the fapi and temp overrides
            //   Note: no need to look at PERM because those were added to
            //         the base targeting model

            size_t l_tankSize = l_maxSize;
            uint8_t* l_dest = i_dest;

            // FAPI
            l_err = saveOverrideTank( l_dest,
                       l_tankSize,
                       &fapi2::theAttrOverrideSync().iv_overrideTank,
                       AttributeTank::TANK_LAYER_FAPI );
            if( l_err )
            {
                break;
            }
            l_maxSize -= l_tankSize;
            io_size += l_tankSize;

            // TARGETING
            l_tankSize = l_maxSize;
            l_dest = i_dest + io_size;
            l_err = saveOverrideTank( l_dest,
                                      l_tankSize,
                                      &Target::theTargOverrideAttrTank(),
                                      AttributeTank::TANK_LAYER_TARG );
            if( l_err )
            {
                break;
            }
            l_maxSize -= l_tankSize;
            io_size += l_tankSize;
        } while(0);

        TRACFCOMP( g_trac_targeting, EXIT_MRK"AttrRP::_saveOverrides: io_size=%d, l_err=%.8X", io_size, ERRL_GETRC_SAFE(l_err) );
        return l_err;
    }

    errlHndl_t AttrRP::saveOverrideTank( uint8_t* i_dest,
                                         size_t& io_size,
                                         AttributeTank* i_tank,
                                         AttributeTank::TankLayer i_layer )
    {
        TRACFCOMP( g_trac_targeting, ENTER_MRK"AttrRP::saveOverrideTank: i_dest=%p, io_size=%d, i_layer=%d", i_dest, io_size, i_layer );
        errlHndl_t l_err = nullptr;
        size_t l_maxSize = io_size;
        io_size = 0;

        // List of chunks we're going to save away
        std::vector<AttributeTank::AttributeSerializedChunk> l_chunks;
        i_tank->serializeAttributes(
                       TARGETING::AttributeTank::ALLOC_TYPE_MALLOC,
                       PAGESIZE, l_chunks );

        // Copy each chunk until we run out of space
        for( auto l_chunk : l_chunks )
        {
            // total size of data plus header for this chunk
            uint32_t l_chunkSize = l_chunk.iv_size;
            l_chunkSize += sizeof(AttrOverrideSection);
            // don't want to double-count the data payload...
            l_chunkSize -= sizeof(AttrOverrideSection::iv_chunk);

            // look for overflow, but only create 1 error
            if( (l_err == nullptr)
                && (io_size + l_chunkSize > l_maxSize) )
            {
                TRACFCOMP( g_trac_targeting, ERR_MRK"Size of chunk is too big" );
                /*@
                 *   @errortype
                 *   @moduleid          TARG_MOD_SAVE_OVERRIDE_TANK
                 *   @reasoncode        TARG_SPACE_OVERRUN
                 *   @userdata1[00:31]  Maximum Available size
                 *   @userdata1[32:63]  Required size
                 *   @userdata2[00:31]  Chunk Size
                 *   @userdata2[32:63]  Previous Size
                 *
                 *   @devdesc   Size of override data exceeds available
                 *              buffer space
                 *
                 *   @custdesc  Internal firmware error applying
                 *              custom configuration settings
                 */
                l_err = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                      TARG_CREATE_VMM_SECTIONS,
                                      TARG_RC_MM_PERM_FAIL,
                                      TWO_UINT32_TO_UINT64(l_maxSize,
                                            io_size + l_chunkSize),
                                      TWO_UINT32_TO_UINT64(l_chunkSize,
                                                           io_size),
                                      true /*SW Error */);
                //deliberately not breaking out here so that we can
                // compute the required size and free the memory in
                // one place
            }

            if( l_err == nullptr )
            {
                // fill in the header
                AttrOverrideSection* l_header =
                  reinterpret_cast<AttrOverrideSection*>(i_dest+io_size);
                l_header->iv_layer = i_layer;
                l_header->iv_size = l_chunk.iv_size;

                // add the data
                memcpy( l_header->iv_chunk,
                        l_chunk.iv_pAttributes,
                        l_chunk.iv_size );
            }

            io_size += l_chunkSize;

            // freeing data that was allocated by serializeAttributes()
            free( l_chunk.iv_pAttributes );
            l_chunk.iv_pAttributes = NULL;
        }

        // add a terminator at the end since the size might get lost
        //  but only if we found some overrides
        if( (io_size > 0)
            && (io_size + sizeof(AttributeTank::TankLayer) < l_maxSize) )
        {
            AttrOverrideSection* l_term =
              reinterpret_cast<AttrOverrideSection*>(i_dest+io_size);
            l_term->iv_layer = AttributeTank::TANK_LAYER_TERM;
            io_size += sizeof(AttributeTank::TankLayer);
        }

        TRACFCOMP( g_trac_targeting, ENTER_MRK"AttrRP::saveOverrideTank: io_size=%d", io_size );
        return l_err;
    }
};
OpenPOWER on IntegriCloud