summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/attrrp.C
blob: fbf61b89292ea31fa3653bce405f059809496cf5 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/targeting/attrrp.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2015                        */
/* [+] 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/attrrp.H>
#include <targeting/common/trace.H>
#include <initservice/initserviceif.H>
#include <util/align.H>

using namespace INITSERVICE;
using namespace ERRORLOG;

#include "attrrp_common.C"

namespace TARGETING
{
    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);
        static_cast<AttrRP*>(i_pInstance)->msgServiceTask();
        return NULL;
    }

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

        do
        {
            // 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);

        } 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 = msg->data[0];
            void*    pAddr = reinterpret_cast<void*>(msg->data[1]);

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

            // Locate corresponding attribute section for message.
            ssize_t section = -1;
            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);
            }
            else // Process request.
            {

                // Determine PNOR offset and page size.
                uint64_t offset = vAddr - iv_sections[section].vmmAddress;
                uint64_t 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.


                // 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;
                        }
                        // Do memcpy from PNOR into physical page.
                        memcpy(pAddr,
                               reinterpret_cast<void*>(
                                    iv_sections[section].pnorAddress + 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;

                    default:
                        TRACFCOMP(g_trac_targeting,
                                  ERR_MRK "AttrRP: Unhandled command type %d.",
                                  msg->type);
                        rc = -EINVAL;
                        break;
                }
            }

            // Log an error log if the AttrRP was unable to handle a message
            // for any reason.
            if (rc != 0)
            {
                /*@
                 *   @errortype
                 *   @moduleid          TARG_MOD_ATTRRP
                 *   @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.
                 */
                const bool hbSwError = true;
                errlHndl_t l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                                  TARG_MOD_ATTRRP,
                                                  TARG_RC_ATTR_MSG_FAIL,
                                                  vAddr,
                                                  TWO_UINT32_TO_UINT64(
                                                        msg->type,
                                                        section),
                                                  hbSwError);
                errlCommit(l_errl,TARG_COMP_ID);
            }

            // Respond to kernel 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
        {
            // Locate attribute section in PNOR.
            PNOR::SectionInfo_t l_pnorSectionInfo;
            l_errl = PNOR::getSectionInfo(PNOR::HB_DATA,
                                          l_pnorSectionInfo);
            if (l_errl) break;

            // Find attribute section header.
            TargetingHeader* l_header =
                reinterpret_cast<TargetingHeader*>(l_pnorSectionInfo.vaddr);

            // Validate eye catch.
            if (l_header->eyeCatcher != PNOR_TARG_EYE_CATCHER)
            {
                /*@
                 *   @errortype
                 *   @moduleid          TARG_MOD_ATTRRP
                 *   @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_MOD_ATTRRP,
                                       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];

            // Find start to first section:
            //          (PNOR addr + size of header + offset in header).
            TargetingSection* l_section =
                    reinterpret_cast<TargetingSection*>(
                            l_pnorSectionInfo.vaddr + sizeof(TargetingHeader) +
                            l_header->offsetToSections
                    );

            // 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;
                iv_sections[i].size = l_section->sectionSize;

                TRACFCOMP(g_trac_targeting,
                          "Decoded Attribute Section: %d, 0x%lx 0x%lx 0x%lx",
                          iv_sections[i].type,
                          iv_sections[i].vmmAddress,
                          iv_sections[i].pnorAddress,
                          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();

            // 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
                         *   @moduleid   TARG_MOD_ATTRRP
                         *   @reasoncode TARG_RC_UNHANDLED_ATTR_SEC_TYPE
                         *   @userdata1  Section type
                         *
                         *   @devdesc    Found unhandled attribute section type
                         */
                        const bool hbSwError = true;
                        l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                               TARG_MOD_ATTRRP,
                                               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
                     *   @moduleid          TARG_MOD_ATTRRP
                     *   @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.
                     */
                    const bool hbSwError = true;
                    l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                           TARG_MOD_ATTRRP,
                                           TARG_RC_MM_BLOCK_FAIL,
                                           iv_sections[i].vmmAddress,
                                           rc, hbSwError);
                    break;
                }

                if(iv_sections[i].type == SECTION_TYPE_PNOR_RW)
                {
                    /*
                     * 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
                     *   @moduleid          TARG_MOD_ATTRRP
                     *   @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.
                     */
                    const bool hbSwError = true;
                    l_errl = new ErrlEntry(ERRL_SEV_UNRECOVERABLE,
                                           TARG_MOD_ATTRRP,
                                           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::save(uint64_t& io_addr)
    {
        // Call save on singleton instance.
        return Singleton<AttrRP>::instance()._save(io_addr);
    }

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

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

        // Determine bottom of the address region.
        io_addr = io_addr - l_size;
        // Align to 64KB for Opal
        io_addr = ALIGN_DOWN_X(io_addr,64*KILOBYTE);


        // Map in region.
        void* region = mm_block_map(reinterpret_cast<void*>(io_addr),
                                    l_size);
        uint8_t* 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;
    }

};
OpenPOWER on IntegriCloud