summaryrefslogtreecommitdiffstats
path: root/src/usr/initservice/baseinitsvc/initservice.C
blob: 2187b8cb0b2f35e4ac5b331c6bb65312ffb80315 (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
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/usr/initservice/baseinitsvc/initservice.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2011
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END

/**
 * @file    initservice.C
 *
 *  Implements Initialization Service for Host boot.
 *  See initservice.H for details
 *
 */
#define __HIDDEN_SYSCALL_SHUTDOWN

#include    <kernel/console.H>                  // printk status

#include    <sys/vfs.h>
#include    <vfs/vfs.H>
#include    <sys/task.h>
#include    <sys/misc.h>
#include    <trace/interface.H>
#include    <errl/errlentry.H>
#include    <errl/errlmanager.H>
#include    <sys/sync.h>
#include    <sys/mm.h>
#include    <vmmconst.h>

#include    <errl/errludstring.H>

#include    <initservice/taskargs.H>        // TASK_ENTRY_MACRO

#include    "initservice.H"
#include    "initsvctasks.H"


//  -----   namespace   SPLESS  -----------------------------------------------
namespace   SPLESS
{
    //  allocate space for SPLess Command regs in the base image.
    uint64_t    g_SPLess_Command_Reg    =   0;
    uint64_t    g_SPLess_Status_Reg     =   0;
    uint64_t    g_SPLess_IStepMode_Reg  =   0x123456789abcdef0;

}
//  -----   end namespace   SPLESS  ---------------------------------------


namespace   INITSERVICE
{

trace_desc_t *g_trac_initsvc = NULL;
TRAC_INIT(&g_trac_initsvc, "INITSVC", 2048 );

/**
 *  @brief  start() task entry procedure
 *  This one is "special" since we do not return anything to the kernel/vfs
 */
extern "C"
void _start(void *ptr)
{
    TRACFCOMP( g_trac_initsvc,
            "Executing Initialization Service module." );

    // initialize the base modules in Hostboot.
    InitService::getTheInstance().init( ptr );

    TRACFCOMP( g_trac_initsvc,
            "return from Initialization Service module." );

    task_end();
}



errlHndl_t  InitService::checkNLoadModule( const TaskInfo *i_ptask ) const
{
    errlHndl_t      l_errl  =   NULL;
    const   char    *l_modulename   =   NULL;

    assert(i_ptask->taskflags.task_type   ==  START_FN );

    do  {

        //  i_ptask->taskflags.task_type   ==  STARTFN
        l_modulename  =   VFS::module_find_name(
                reinterpret_cast<void*>(i_ptask->taskfn) );
        if  ( l_modulename  ==   NULL )
        {
            /*@     errorlog tag
             *  @errortype      ERRL_SEV_CRITICAL_SYS_TERM
             *  @moduleid       BASE_INITSVC_MOD_ID
             *  @reasoncode     INITSVC_LOAD_MODULE_FAILED
             *  @userdata1      0
             *  @userdata2      0
             *
             *  @devdesc        Initialization Service failed to load a
             *                  module needed to load a function or task.
             *                  UserDetails will contain the name of the
             *                  function or task.
             */
            l_errl = new ERRORLOG::ErrlEntry(
                    ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                    INITSERVICE::BASE_INITSVC_MOD_ID,
                    INITSERVICE::INITSVC_LOAD_MODULE_FAILED,
                    0,
                    0 );

            //  error, break out of do block
            break;
        }

        TRACDCOMP( g_trac_initsvc,
                "checkNLoadModule: found %s in module %s",
                i_ptask->taskname,
                ((l_modulename!=NULL)?l_modulename:"NULL???") );

        if (  !VFS::module_is_loaded( l_modulename )
        )
        {
            TRACDCOMP( g_trac_initsvc,
                    "loading module %s",
                    l_modulename );
            l_errl = VFS::module_load( l_modulename );
            if ( l_errl )
            {
                //  load module returned with errl set
                TRACFCOMP( g_trac_initsvc,
                        "module_load( %s ) returned with an error.",
                        l_modulename );

                //  break out of do block
                break;
            }
        }

    }   while( 0 );     // end do() block


    return  l_errl;
}


errlHndl_t InitService::startTask(
        const TaskInfo       *i_ptask,
        void                 *io_pargs ) const
{
    tid_t       l_tidlnchrc     =   0;
    tid_t       l_tidretrc      =   0;
    errlHndl_t  l_errl          =   NULL;
    int         l_childsts      =   0;
    void        *l_childerrl    =   NULL;


    assert( i_ptask != NULL );
    // assert( i_ptask->taskflags.task_type == START_TASK );

    do  {
        // Base modules have already been loaded and initialized,
        // extended modules have not.
        if ( i_ptask->taskflags.module_type == EXT_IMAGE )
        {
            // load module if necessary
            l_errl = VFS::module_load( i_ptask->taskname );
        }
        if ( l_errl )
        {
            TRACFCOMP(g_trac_initsvc,
                    "ERROR: failed to load module for task '%s'",
                    i_ptask->taskname);

            // drop out of do block with errl set
            break;
        }

        // launch a task and wait for it.
        l_tidlnchrc = task_exec( i_ptask->taskname, io_pargs );
        TRACDCOMP( g_trac_initsvc,
                "launch task %s returned %d",
                i_ptask->taskname,
                l_tidlnchrc );

        //  process the return - kernel returns a 16-bit signed # as a
        //  threadid/error
        if ( static_cast<int16_t> (l_tidlnchrc) < 0 )
        {
            // task failed to launch, post an errorlog and dump some trace
            TRACFCOMP(g_trac_initsvc,
                    "ERROR 0x%x: starting task '%s'",
                    l_tidlnchrc,
                    i_ptask->taskname);

            /*@     errorlog tag
             *  @errortype      ERRL_SEV_CRITICAL_SYS_TERM
             *  @moduleid       BASE_INITSVC_MOD_ID
             *  @reasoncode     START_TASK_FAILED
             *  @userdata1      0
             *  @userdata2      task id or task return code
             *
             *  @devdesc        Initialization Service failed to start a task.
             *
             */
            l_errl = new ERRORLOG::ErrlEntry(
                    ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                    INITSERVICE::BASE_INITSVC_MOD_ID,
                    INITSERVICE::START_TASK_FAILED,
                    0,
                    l_tidlnchrc );

            //  break out of do block
            break;
        } // endif tidlnchrc

        TRACDCOMP(g_trac_initsvc,
                            "Wait for tid %d '%s'",
                            l_tidlnchrc,
                            i_ptask->taskname);

        //  wait here for the task to end.
        //  status of the task ( OK or Crashed ) is returned in l_childsts
        //  if the task returns an errorlog, it will be returned
        //      (via task_end2) in l_childerrl
        l_tidretrc  =   task_wait_tid(
                                l_tidlnchrc,
                                &l_childsts,
                                &l_childerrl );
        if (    ( static_cast<int16_t>(l_tidretrc) < 0 )
             || ( l_childsts != TASK_STATUS_EXITED_CLEAN )
        )
        {
            // the launched task failed or crashed,
            //  post an errorlog and dump some trace
            /*@     errorlog tag
             *  @errortype      ERRL_SEV_CRITICAL_SYS_TERM
             *  @moduleid       BASE_INITSVC_MOD_ID
             *  @reasoncode     WAIT_TASK_FAILED
             *  @userdata1      task id or task return code
             *  @userdata2      returned status from task
             *
             *  @devdesc        Initialization Service launched a task and
             *                  the task returned an error.
             *
             */
            l_errl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                                    INITSERVICE::BASE_INITSVC_MOD_ID,
                                    INITSERVICE::WAIT_TASK_FAILED,
                                    l_tidretrc,
                                    l_childsts );

            //  break out of do block
            break;
        } // endif tidretrc

        //  check for returned errorlog
        if ( l_childerrl != NULL )
        {
            // cast to the correct type and return
            l_errl  =   reinterpret_cast<errlHndl_t>(l_childerrl);

            // break out of do block
            break;
        }

    }   while(0);   // end do block

    if ( l_errl )
    {
        // Add the task name as user detail data to any errorlog
        ERRORLOG::ErrlUserDetailsString(i_ptask->taskname).addToLog(l_errl);
    }

    //  return any errorlog to the caller
    return l_errl;
}   // startTask()


errlHndl_t InitService::executeFn(
                        const TaskInfo  *i_ptask,
                        void            *io_pargs ) const
{
    tid_t       l_tidlnchrc     =   0;
    tid_t       l_tidretrc      =   0;
    errlHndl_t  l_errl          =   NULL;
    int         l_childsts      =   0;
    void        *l_childerrl    =   NULL;

    assert( i_ptask != NULL );
    assert( i_ptask->taskfn != NULL ) ;


    do  {

        //
        l_errl  =   checkNLoadModule( i_ptask );
        if ( l_errl )
        {
            TRACFCOMP(g_trac_initsvc,
                    "ERROR: failed to load module for task '%s'",
                    i_ptask->taskname);

            //  break out with errorlog set
            break;
        }

        //  valid function, launch it
        l_tidlnchrc = task_create( i_ptask->taskfn, io_pargs);
        if (static_cast<int16_t> (l_tidlnchrc) < 0)
        {
            TRACFCOMP(g_trac_initsvc,
                    "ERROR %d: starting function in task'%s'",
                    l_tidlnchrc,
                    i_ptask->taskname);

            /*@     errorlog tag
             *  @errortype      ERRL_SEV_CRITICAL_SYS_TERM
             *  @moduleid       BASE_INITSVC_MOD_ID
             *  @reasoncode     START_FN_FAILED
             *  @userdata1      task return code
             *  @userdata2      0
             *
             *  @devdesc        Initialization Service attempted to start a
             *                  function within a module but the function
             *                  failed to launch
             */
            l_errl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                                    INITSERVICE::BASE_INITSVC_MOD_ID,
                                    INITSERVICE::START_FN_FAILED,
                                    l_tidlnchrc,
                                    0   );

            //  break out with errorlog set
            break;
        } // endif tidlnchrc

        //  wait here for the task to end.
        //  status of the task ( OK or Crashed ) is returned in l_childsts
        //  if the task returns an errorlog, it will be returned
        //      (via task_end2) in l_childerrl
        l_tidretrc  =   task_wait_tid(
                                l_tidlnchrc,
                                &l_childsts,
                                &l_childerrl );
        if (    ( static_cast<int16_t>(l_tidretrc) < 0 )
             || ( l_childsts != TASK_STATUS_EXITED_CLEAN )
            )
        {
            // the launched task failed or crashed
            //  post an errorlog and dump some trace
            /*@     errorlog tag
             *  @errortype      ERRL_SEV_CRITICAL_SYS_TERM
             *  @moduleid       BASE_INITSVC_MOD_ID
             *  @reasoncode     WAIT_TASK_FAILED
             *  @userdata1      task id or task return code
             *  @userdata2      returned status from task
             *
             *  @devdesc        Initialization Service launched a task and the task returned an error.
             *
             *
             */
            l_errl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                                    INITSERVICE::BASE_INITSVC_MOD_ID,
                                    INITSERVICE::WAIT_TASK_FAILED,
                                    l_tidretrc,
                                    l_childsts );

            TRACFCOMP(g_trac_initsvc,
                    "ERROR : task_wait_tid(0x%x). '%s', l_tidretrc=0x%x, l_childsts=0x%x",
                    l_tidlnchrc,
                    i_ptask->taskname,
                    l_tidretrc,
                    l_childsts );

            //  break out of do block
            break;
        } // endif tidretrc

        //  check for returned errorlog
        if ( l_childerrl != NULL )
        {
            TRACFCOMP(g_trac_initsvc,
                    "ERROR : task_wait_tid(0x%x). '%s', l_childerrl=%p",
                    l_tidlnchrc,
                    i_ptask->taskname,
                    l_childerrl  );

            // cast to the correct type and return
            l_errl  =   reinterpret_cast<errlHndl_t>(l_childerrl);

            // break out of do block
            break;
        }

    }   while( 0 );     // end do block

    if ( l_errl )
    {
        // Add the task name as user detail data to any errorlog that was
        //  posted.
        ERRORLOG::ErrlUserDetailsString(i_ptask->taskname).addToLog(l_errl);
    }

    return l_errl;
}


/**
 * @todo    this will make a system call to post the progress code.
 *
 */
void InitService::setProgressCode( uint64_t i_progresscode ) const
{

    // do nothing for now
}


errlHndl_t  InitService::dispatchTask( const TaskInfo   *i_ptask,
                                       void             *io_pargs ) const
{
    errlHndl_t   l_errl  =   NULL;


    //  dispatch tasks...
    switch ( i_ptask->taskflags.task_type)
    {
    case NONE:
        //  task is a place holder, skip
        TRACDCOMP( g_trac_initsvc,
                   "task_type==NONE : %s",
                   i_ptask->taskname );
        break;
    case START_TASK:
        TRACDCOMP( g_trac_initsvc,
                  "task_type==START_TASK: %s",
                  i_ptask->taskname );
        l_errl = startTask( i_ptask,
                            io_pargs );
        break;
    case INIT_TASK:
        TRACDCOMP( g_trac_initsvc,
                   "task_type==INIT_TASK: %s",
                   i_ptask->taskname);

        l_errl = VFS::module_load( i_ptask->taskname );
        break;
    case START_FN:
        TRACDCOMP( g_trac_initsvc,
                   "task_type==START_FN : %s %p",
                   i_ptask->taskname,
                   i_ptask->taskfn );
        l_errl = executeFn( i_ptask,
                            io_pargs );
        break;
    case UNINIT_TASK:
        TRACDCOMP( g_trac_initsvc,
                  "task_type=UNINIT_TASK : %s ",
                  i_ptask->taskname );
        l_errl = VFS::module_unload( i_ptask->taskname );
        break;
    default:
        /**
         * @note    If there is a bad TaskInfo struct we just stop here.
         */
        TRACFCOMP( g_trac_initsvc,
                   "Invalid task_type %d: ABORT",
                   i_ptask->taskflags.task_type );
        assert( 0 );
        break;

    } //  endswitch

    return  l_errl;
}


void InitService::init( void *io_ptr )
{
    errlHndl_t      l_errl              =   NULL;
    uint64_t        l_task              =   0;
    const TaskInfo  *l_ptask            =   NULL;
    //  init shutdown status to good.
    uint64_t        l_shutdownStatus    =   SHUTDOWN_STATUS_GOOD;

    //  @todo detach from parent.
    // $$ task_detach();

    printk( "InitService entry.\n" );

    TRACFCOMP( g_trac_initsvc,
               "Initialization Service is starting, io_ptr=%p.", io_ptr );

    //  loop through the task list and start up any tasks necessary
    for ( l_task=0;
          l_task <  ( sizeof(g_taskinfolist)/sizeof(TaskInfo) ) ;
          l_task++ )
    {
        //  make a local copy of the base image task
        l_ptask = &(g_taskinfolist[ l_task]);
        if ( l_ptask->taskflags.task_type == END_TASK_LIST )
        {
            TRACDCOMP( g_trac_initsvc,
                    "End of Initialization Service task list." );
            break;
        }

        //  dispatch the task and return good or errorlog
        l_errl  =   dispatchTask( l_ptask ,
                                  NULL );

        //  process errorlogs returned from the task that was launched
        if ( l_errl )
        {
            TRACFCOMP( g_trac_initsvc,
                       "ERROR: dispatching task, errorlog=0x%p",
                       l_errl );
            //  drop out with the error
            break;
        }

    } // endfor

    //  die if we drop out with an error
    if ( l_errl )
    {

        //  commit the log first, then shutdown.
        TRACFCOMP( g_trac_initsvc,
                "InitService: Committing errorlog %p",
                l_errl );
        errlCommit( l_errl, INITSVC_COMP_ID );

        //  post bad shutdown status
        l_shutdownStatus   = SHUTDOWN_STATUS_INITSVC_FAILED;
    }

    //  =====================================================================
    //  -----   Shutdown all CPUs   -----------------------------------------
    //  =====================================================================

    TRACFCOMP( g_trac_initsvc,
            "InitService finished, shutdown = 0x%x.",
            l_shutdownStatus );

    //  Tell kernel to perform shutdown sequence
    InitService::getTheInstance().doShutdown( l_shutdownStatus );

    printk( "InitService exit.\n" );
    // return to _start() to exit the task.
}


InitService& InitService::getTheInstance( )
{
    return Singleton<InitService>::instance();
}


InitService::InitService( )
{ }


InitService::~InitService( )
{ }

void registerBlock(void* i_vaddr, uint64_t i_size, BlockPriority i_priority)
{
    Singleton<InitService>::instance().registerBlock(i_vaddr,i_size,i_priority);
}

void InitService::registerBlock(void* i_vaddr, uint64_t i_size,
                                BlockPriority i_priority)
{
    //Order priority from largest to smallest upon inserting
    std::vector<regBlock_t*>::iterator regBlock_iter = iv_regBlock.begin();
    for (; regBlock_iter!=iv_regBlock.end(); ++regBlock_iter)
    {
        if ((uint64_t)i_priority >= (*regBlock_iter)->priority)
        {
            iv_regBlock.insert(regBlock_iter,
                               new regBlock_t(i_vaddr,i_size,
                                              (uint64_t)i_priority));
            regBlock_iter=iv_regBlock.begin();
            break;
        }
    }
    if (regBlock_iter == iv_regBlock.end())
    {
        iv_regBlock.push_back(new regBlock_t(i_vaddr,i_size,
                                             (uint64_t)i_priority));
    }
}

void InitService::doShutdown(uint64_t i_status)
{
    int l_rc = 0;
    errlHndl_t l_err = NULL;
    std::vector<regBlock_t*>::iterator l_rb_iter = iv_regBlock.begin();
    //FLUSH each registered block in order
    while (l_rb_iter!=iv_regBlock.end())
    {
        l_rc = mm_remove_pages(FLUSH,(*l_rb_iter)->vaddr,(*l_rb_iter)->size);
        if (l_rc)
        {
            TRACFCOMP(g_trac_initsvc, "ERROR: flushing virtual memory");
            /*
             * @errorlog tag
             * @errortype       ERRL_SEV_CRITICAL_SYS_TERM
             * @moduleid        BASE_INITSVC_MOD_ID
             * @reasoncode      SHUTDOWN_FLUSH_FAILED
             * @userdata1       returncode from mm_remove_pages()
             * @userdata2       0
             *
             * @defdesc         Could not FLUSH virtual memory.
             *
             */
            l_err = new ERRORLOG::ErrlEntry(
                        ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
                        INITSERVICE::BASE_INITSVC_MOD_ID,
                        INITSERVICE::SHUTDOWN_FLUSH_FAILED,l_rc,0);
            //Commit and attempt flushing other registered blocks
            errlCommit( l_err, INITSVC_COMP_ID );
            l_err = NULL;
        }
        l_rb_iter++;
    }
    shutdown(i_status);
}

} // namespace
OpenPOWER on IntegriCloud