summaryrefslogtreecommitdiffstats
path: root/src/occ_405/trac/trac_interface.c
blob: a0b4d3a4053b5278b3dd87f9c4aecd3606ea5216 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/occ_405/trac/trac_interface.c $                           */
/*                                                                        */
/* OpenPOWER OnChipController 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                                                     */

//*************************************************************************/
// Includes
//*************************************************************************/
#include "ssx.h"

#include <trac_interface.h>
#include <trac_service_codes.h>
#include <occ_common.h>
#include <comp_ids.h>
//*************************************************************************/
// Externs
//*************************************************************************/

//*************************************************************************/
// Macros
//*************************************************************************/

//*************************************************************************/
// Defines/Enums
//*************************************************************************/
#define TRAC_END_BUFFER                 "&"

#define TRAC_INTF_MUTEX_TIMEOUT         SSX_SECONDS(5)

#define     TRACE_BUF_VERSION   0x01;    /*!< Trace buffer version            */
#define     TRACE_FIELDTRACE    0x4654;  /*!< Field Trace - "FT"              */
#define     TRACE_FIELDBIN      0x4644   /*!< Binary Field Trace - "FD"       */

#define TRAC_TIME_REAL   0 // upper 32 = seconds, lower 32 = microseconds
#define TRAC_TIME_50MHZ  1
#define TRAC_TIME_200MHZ 2
#define TRAC_TIME_167MHZ 3 // 166666667Hz

//*************************************************************************/
// Structures
//*************************************************************************/

//*************************************************************************/
// Globals
//*************************************************************************/

/// Instantiate the buffers for the traces.
///
/// It may be beneficial to add the attribute:
///     __attribute__ ((section (".noncacheable")))
/// when debugging on real HW, in case the OCC hangs and we can't access
/// the cache to get coherent data.
uint8_t g_trac_inf_buffer[TRACE_BUFFER_SIZE];
uint8_t g_trac_err_buffer[TRACE_BUFFER_SIZE];
uint8_t g_trac_imp_buffer[TRACE_BUFFER_SIZE];

#if SIMICS_ENVIRONMENT
// Necessary for use in Simics (to get address)
uint8_t* g_trac_inf_buffer_ptr = g_trac_inf_buffer;
uint8_t* g_trac_err_buffer_ptr = g_trac_err_buffer;
uint8_t* g_trac_imp_buffer_ptr = g_trac_imp_buffer;
#endif

// Need to modify the addTraceToErrl() function in errl.c when new trace buffer is added/removed
tracDesc_t g_trac_inf = (tracDesc_t) &g_trac_inf_buffer;
tracDesc_t g_trac_err = (tracDesc_t) &g_trac_err_buffer;
tracDesc_t g_trac_imp = (tracDesc_t) &g_trac_imp_buffer;

SsxSemaphore    g_trac_mutex_inf;
SsxSemaphore    g_trac_mutex_err;
SsxSemaphore    g_trac_mutex_imp;

// There are #defines for the indicies of this array in the header file
const trace_descriptor_array_t g_des_array[] =
{
    {&g_trac_inf,"INF", &g_trac_mutex_inf},
    {&g_trac_err,"ERR", &g_trac_mutex_err},
    {&g_trac_imp,"IMP", &g_trac_mutex_imp}
};

static bool circular_full_flag = FALSE;
circular_buf_header_t g_isr_circular_header;
circular_entire_data_t g_isr_circular_buf[CIRCULAR_BUFFER_SIZE];

//*************************************************************************/
// Function Prototypes
//*************************************************************************/
/*
 *  Initialize all header values of a trace buffer
 *
 *  This function will initialize all of the values in the trace buffer
 *  header so that it is ready for tracing.
 *
 *  param o_buf Pointer to trace buffer which will be initialized.
 *  param i_comp Component who is getting buffer initialized.
 *
 *  return Non-zero return code on error.
 */
UINT trac_init_values_buffer(tracDesc_t *o_buf,const CHAR *i_comp);


/*
 *  Raw buffer write function
 *
 *  This function assumes i_td has been initialized and it also assume
 *  the critical region of the input trace descriptor has been locked.
 *
 *  param io_td Initialized trace descriptor pointer to buffer to trace to.
 *  param i_ptr Pointer to data to write to trace buffer
 *  param i_size Size of i_ptr
 *
 *  return Non-zero return code on error.
 */
UINT trac_write_data(const trace_descriptor_array_t *io_td,
                    const void *i_ptr,
                    const ULONG i_size);

/*
 *  Write data to circular buffer
 *
 *  param i_ptr
 *
 *  return Non-zero return code on error.
 */
uint16_t trac_write_data_to_circular(circular_entire_data_t *i_ptr);


 /**
 *  Get data from circular buffer
 *
 *  param o_ptr
 *
 *  return Non-zero return code on error.
 */
uint16_t get_trac_entry_data_from_circular(circular_entire_data_t *o_ptr);

//*************************************************************************/
// Functions
//*************************************************************************/

// Function Specification
//
// Name: TRAC_init_buffers
//
// Description:
//
// End Function Specification
UINT TRAC_init_buffers()
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    INT             l_rc = 0;
    UINT            l_num_des = sizeof(g_des_array) / sizeof(trace_descriptor_array_t);
    UINT            i=0;
    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/


    // Initialize trace mutexes
    for(i = 0;i < l_num_des; i++)
    {
        l_rc = ssx_semaphore_create(g_des_array[i].sem, 1, 1);
        if(SSX_OK != l_rc) break;
    }

    if(SSX_OK != l_rc)
    {
        // Badness, don't continue
        FIELD("TRAC_init_buffers: Failed to create mutex");
    }
    else
    {
        // Initialize trace buffers
        for(i=0;i<l_num_des;i++)
        {
            // Initialize the buffer
            l_rc = trac_init_values_buffer(g_des_array[i].entry,
                                           g_des_array[i].comp);
            if(l_rc)
            {
                FIELD1("TRAC_init_buffers: Failed to initialize buffer: ",
                       (unsigned char)i);
                break;
            }
        }
    }

    // Initialize isr circular buffer it to all 0's
    g_isr_circular_header.head = 0;
    g_isr_circular_header.tail = 0;
    g_isr_circular_header.entryCount = 0;

    memset(g_isr_circular_buf, 0 , CIRCULAR_BUFFER_SIZE * sizeof(circular_entire_data_t));

    return(l_rc);
}

// Function Specification
//
// Name: trac_init_values_buffer
//
// Description:
//
// End Function Specification
UINT trac_init_values_buffer(tracDesc_t *o_buf,const CHAR *i_comp)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT16             l_rc = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    // Initialize it to all 0's
    memset(*o_buf,0,(size_t)TRACE_BUFFER_SIZE);

    (*o_buf)->ver = TRACE_BUF_VERSION;
    (*o_buf)->hdr_len = sizeof(trace_buf_head_t);
    (*o_buf)->time_flg = TRAC_TIME_REAL;
    (*o_buf)->endian_flg = 'B';  // Big Endian
    memcpy((*o_buf)->comp,i_comp,(size_t)COMP_NAME_SIZE);
    (*o_buf)->size = TRACE_BUFFER_SIZE;
    (*o_buf)->times_wrap = 0;
    (*o_buf)->next_free = sizeof(trace_buf_head_t);

    return(l_rc);
}

// Function Specification
//
// Name: trace_adal_write_all
//
// Description: In order to leverage the tracepp, need to add this function. It will call
//              trac_write_int finally
//
// End Function Specification
UINT trace_adal_write_all(const trace_descriptor_array_t *io_td,const trace_hash_val i_hash,
                     const char *i_fmt,const ULONG i_line, const ULONG i_type,...)
{
    UINT rc = 0, i = 0;
    UINT l_num_args = 0;
    ULONG l_i_param[TRACE_MAX_ARGS] = {0};

    // Calculate the number of optional parameters by looking at the i_fmt.
    // i_fmt will store something like "%d,%f,%u"
    if(i_fmt != NULL)
    {
        l_num_args = 1;
        for(i=0;i_fmt[i] != 0;i++)
        {
            if( i_fmt[i] == ',')
            {
                l_num_args++;
            }
        }
    }


    // Get the optional parameters
    va_list l_argptr;     //will hold optional parameters
    va_start(l_argptr,i_type);

    // Check the number of optional parameters
    if(TRACE_MAX_ARGS < l_num_args)
    {
        l_num_args = TRACE_MAX_ARGS;
    }

    for (i=0;i<l_num_args;i++)
    {
        l_i_param[i] = va_arg(l_argptr,ULONG);
    }

    va_end(l_argptr);



    rc = trac_write_int(io_td,
                        i_hash, i_line,
                        l_num_args,
                        l_i_param[0],
                        l_i_param[1],
                        l_i_param[2],
                        l_i_param[3],
                        l_i_param[4]   );
    return rc;
}


// Function Specification
//
// Name: trac_write_int
//
// Description:
//
// End Function Specification
UINT trac_write_int(const trace_descriptor_array_t *io_td,const trace_hash_val i_hash,
                     const ULONG i_line,
                     const UINT i_num_args,
                     const ULONG i_1,const ULONG i_2,const ULONG i_3,
                     const ULONG i_4,const ULONG i_5)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT                 l_rc = 0;
    ULONG                l_entry_size = 0;
    trace_entire_entry_t l_entry;
    SsxMachineContext    l_ctx = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    if(io_td != NULL)
    {

        // Calculate total space needed
        l_entry_size = sizeof(trace_entry_stamp_t);
        l_entry_size += sizeof(trace_entry_head_t);

        // We always add the size of the entry at the end of the trace entry
        // so the parsing tool can easily walk the trace buffer stack so we
        // need to add that on to total size
        l_entry_size += sizeof(ULONG);

        // Now add on size for actual number of arguments we're tracing
        l_entry_size += (i_num_args * sizeof(ULONG));

        // Word align the entry
        l_entry_size = (l_entry_size + 3) & ~3;

        // Fill in the entry structure
        //l_entry.stamp.tid = (ULONG)tx_thread_identify();   // What is response to this in AME code?
        l_entry.stamp.tid = (__ssx_kernel_context_thread() ? 1 : 0);  //context thread or ISR

        // Capture the time.  Note the time stamp is split into tbh (upper) and
        // tbl (lower), both of which are 32 bits each.  The ssx_timebase_get
        // call returns a uint64_t

        uint64_t l_time = ssx_timebase_get();
        l_entry.stamp.tbh = l_time / SSX_TIMEBASE_FREQUENCY_HZ;                // seconds
        l_entry.stamp.tbl = ((l_time % SSX_TIMEBASE_FREQUENCY_HZ)*1000000000)  // nanoseconds
                          /SSX_TIMEBASE_FREQUENCY_HZ;

        // Length is equal to size of data
        l_entry.head.length = (i_num_args * sizeof(ULONG));
        l_entry.head.tag = TRACE_FIELDTRACE;
        l_entry.head.hash = i_hash;
        l_entry.head.line = i_line;

        switch (i_num_args)
        {
            case 5: l_entry.args[4] = i_5;  // Intentional Fall Through
            case 4: l_entry.args[3] = i_4;  // Intentional Fall Through
            case 3: l_entry.args[2] = i_3;  // Intentional Fall Through
            case 2: l_entry.args[1] = i_2;  // Intentional Fall Through
            case 1: l_entry.args[0] = i_1;  // Intentional Fall Through
            default: ;
        }

        // Now put total size at end of buffer
        l_entry.args[i_num_args] = l_entry_size;

        // Disable non-critical interrupts if thread context
        if (__ssx_kernel_context_thread())
            ssx_critical_section_enter(SSX_NONCRITICAL, &l_ctx);

        // Check if context thread or ISR get semaphore or not
        // If ISR did not get semaphore, will add trace log into circular buffer.
        // Context thread will check circular buffer, and add log back into trace buffer.
        // Prevent ISR did not get semaphore, and lost trace log.
        l_rc = ssx_semaphore_pend(io_td->sem,
                                  __ssx_kernel_context_thread()? \
                                  TRAC_INTF_MUTEX_TIMEOUT : SSX_NO_WAIT);

        if(l_rc == SSX_OK)
        {
            // Either this is thread context and mutex was locked within
            // timeout or this is interrupt context and mutex was immediately
            // available, regardless, mutex is now locked.
            l_rc = SUCCESS;

            // Update the entry count
            (*io_td->entry)->te_count++;

            l_rc = trac_write_data(io_td, (void *)&l_entry, l_entry_size);

            if(l_rc != SUCCESS)
            {
                // Badness - Not much we can do on trace failure.  Can't log error
                // because of recursion concerns.  Luckily a trace error is not critical.
                FIELD("trac_write_int: Failed in call to trac_write_data()");
            }

            // Always try to release even if error above
            ssx_semaphore_post(io_td->sem);
        }
        else if(!__ssx_kernel_context_thread())
        {
            // Tracing in interrupt context and mutex was locked, SSX
            // returned -SSX_SEMAPHORE_PEND_NO_WAIT

            // Failed to get semaphore in ISR
            // Create trace in ISR circular buffer
            circular_entire_data_t l_cir_data_in;
            l_cir_data_in.len = l_entry_size;
            memcpy(&l_cir_data_in.comp, io_td->comp, (size_t)COMP_NAME_SIZE);
            l_cir_data_in.entry = l_entry;

            if(g_isr_circular_header.entryCount >= CIRCULAR_BUFFER_SIZE)
            {
                FIELD("trac_write_int: Circular Buffer size insufficient!\n");
                circular_full_flag = TRUE;
                l_rc = TRAC_CIRCULAR_BUFF_FULL;
                // Always try to release even if error above
                ssx_semaphore_post(io_td->sem);
                return(l_rc);
            }

            // Save to Circular Buffer
            l_rc = trac_write_data_to_circular(&l_cir_data_in);
            g_isr_circular_header.head = (g_isr_circular_header.head + 1) % CIRCULAR_BUFFER_SIZE;
            g_isr_circular_header.entryCount++;

            if(l_rc != SUCCESS)
            {
                // Badness - Not much we can do on trace failure.  Can't log error
                // because of recursion concerns.  Luckily a trace error is not critical.
                FIELD("trac_write_int: Failed in call to trac_write_data_to_circular()");
            }
        }
        else
        {
            // Failed to get mutex in thread
            FIELD("trac_write_int: Failed to get mutex");
        }

        // Re-enable non-critical interrupts if thread context
        if (__ssx_kernel_context_thread())
            ssx_critical_section_exit(&l_ctx);


        //2nd. Check caller from thread?
        if(__ssx_kernel_context_thread() && (g_isr_circular_header.entryCount > 0))
        {
            if(circular_full_flag)
            {
                // If ISR circular buffer is full, create a trace in IMP
                // Use existed trace structure to create new trace

                // re-calculate size of the new trace entry
                l_entry_size = l_entry_size + ((1 - i_num_args)*4);

                // fill trace field
                l_entry.head.hash = trace_adal_hash("IMP: ISR Circular Buffer is full, %d entries lost", -1);
                l_entry.head.line = __LINE__;

                // one argument for this trace
                l_entry.head.length = sizeof(ULONG);
                l_entry.args[0] = circular_full_flag;
                l_entry.args[1] = l_entry_size;

                // Disable non-critical interrupts in thread context
                ssx_critical_section_enter(SSX_NONCRITICAL, &l_ctx);

                // Get IMP trace buffer / mutex
                const trace_descriptor_array_t *imp_td = TRAC_get_td("IMP");

                //Write to IMP trace buffer
                l_rc = ssx_semaphore_pend(imp_td->sem,TRAC_INTF_MUTEX_TIMEOUT);
                if(l_rc == SSX_OK)
                {
                    // Update the entry count
                    (*imp_td->entry)->te_count++;

                    l_rc = trac_write_data(imp_td, (void *)&l_entry, l_entry_size);
                    if(l_rc != SUCCESS)
                    {
                        // Badness - Not much we can do on trace failure.  Can't log error
                        // because of recursion concerns.  Luckily a trace error is not critical.
                        FIELD("trac_write_int: Failed in call to trac_write_data()");
                    }
                    ssx_semaphore_post(imp_td->sem);
                }
                else
                {
                    // Failed to get mutex in thread
                    FIELD("trac_write_int: Failed to get mutex");
                }

                // Re-enable non-critical interrupts
                ssx_critical_section_exit(&l_ctx);

                // Reset full flag
                circular_full_flag = FALSE;
                l_rc = TRAC_CIRCULAR_BUFF_FULL;
            }

            circular_entire_data_t l_cir_data_out;

            do
            {
                // Thread context here, disable non-critical
                // interrupts while unloading circular buffer
                ssx_critical_section_enter(SSX_NONCRITICAL, &l_ctx);

                // Get tail position
                g_isr_circular_header.tail = g_isr_circular_header.tail % CIRCULAR_BUFFER_SIZE;
                //Copy One trace entity from circular buffer
                get_trac_entry_data_from_circular(&l_cir_data_out);

                //Write to trace buffer
                const trace_descriptor_array_t *i_td = TRAC_get_td((const char *)l_cir_data_out.comp);
                l_rc = ssx_semaphore_pend(i_td->sem,TRAC_INTF_MUTEX_TIMEOUT);
                if(l_rc == SSX_OK)
                {
                    // Update the entry count
                    (*i_td->entry)->te_count++;

                    l_rc = trac_write_data(i_td,
                                          (const void *)&l_cir_data_out.entry,
                                          (const ULONG)l_cir_data_out.len);

                    if(l_rc == SUCCESS)
                    {
                        if(g_isr_circular_header.tail == g_isr_circular_header.head )
                            g_isr_circular_header.entryCount = 0;
                        else
                        {
                            g_isr_circular_header.tail++;
                            g_isr_circular_header.entryCount--;
                        }
                    }
                    else
                    {
                        // Badness - Not much we can do on trace failure.  Can't log error
                        // because of recursion concerns.  Luckily a trace error is not critical.
                        FIELD("trac_write_int: Failed in call to trac_write_data()");
                    }

                    ssx_semaphore_post(i_td->sem);
                }
                else
                {
                    // Failed to get mutex in thread
                    FIELD("trac_write_int: Failed to get mutex");
                }
                // Re-enable non-critical interrupts
                ssx_critical_section_exit(&l_ctx);
            }
            while(g_isr_circular_header.entryCount > 0);
        }
    }
    else
    {
        l_rc = TRAC_INVALID_PARM;
        FIELD("trac_write_int: User passed invalid parameter");
    }

    return(l_rc);
}

// NOTE: Removed trac_write_bin because it was unused and unsafe

// Function Specification
//
// Name: trac_write_data
//
// Description:
//
// End Function Specification
UINT trac_write_data(const trace_descriptor_array_t *io_td,
                    const void *i_ptr,
                    const ULONG i_size)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT                l_rc = 0;
    ULONG               l_total_size = i_size;
    void                *l_buf_ptr = NULL;
    ULONG               l_offset = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    do
    {

        if(i_size > TRACE_BUFFER_SIZE)
        {
            FIELD("trac_write_data: Input size too large!");
            l_rc = TRAC_DATA_SIZE_TOO_LARGE;
            break;
        }

        if(((*io_td->entry)->next_free + l_total_size) > TRACE_BUFFER_SIZE)
        {
            // copy what we can to end
            l_buf_ptr = (char *)(*io_td->entry) + (*io_td->entry)->next_free;
            l_buf_ptr = (void *) ( ((ULONG) l_buf_ptr + 3) & ~3);
            l_offset = TRACE_BUFFER_SIZE - (*io_td->entry)->next_free;
            memcpy(l_buf_ptr,i_ptr,(size_t)l_offset);

            l_total_size -= l_offset;

            // Now adjust the main header of buffer
            (*io_td->entry)->times_wrap++;
            (*io_td->entry)->next_free = (*io_td->entry)->hdr_len;
        }

        l_buf_ptr = (char *)(*io_td->entry) + (*io_td->entry)->next_free;

        // Word align the write - total size includes this alignment
        l_buf_ptr = (void *) ( ((ULONG) l_buf_ptr + 3) & ~3);

        memcpy(l_buf_ptr,(char *)i_ptr + l_offset,l_total_size);

        // Make sure size is correct for word alignment
        // Note that this works with binary trace because only the binary data
        // has the potential to be un-word aligned.  If two parts of the binary
        // trace had this problem then this code would not work.
        l_total_size = (l_total_size + 3) & ~3;
        (*io_td->entry)->next_free += l_total_size;

    }while(FALSE);

    return(l_rc);

}

// Function Specification
//
// Name: TRAC_get_td
//
// Description:
//
// End Function Specification
const trace_descriptor_array_t* TRAC_get_td(const char *i_comp)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT                        l_num_des = 0;
    UINT                        i=0;
    const trace_descriptor_array_t*   l_td = NULL;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    l_num_des = sizeof(g_des_array) / sizeof(trace_descriptor_array_t);

    for(i=0;i<l_num_des;i++)
    {
        if(memcmp(i_comp,(*(g_des_array[i].entry))->comp,(size_t)COMP_NAME_SIZE) == 0)
        {
            // Found the component
            l_td = &(g_des_array[i]);
            break;
        }
    }

    return(l_td);
}

// Function Specification
//
// Name: TRAC_get_buffer
//
// Description:
//
// End Function Specification
UINT TRAC_get_buffer(const trace_descriptor_array_t *i_td_ptr,
                    void *o_data)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT            l_rc = 0;
    SsxMachineContext   l_ctx = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    if((i_td_ptr) && (o_data != NULL))
    {
        // Disable non-critical interrupts if thread context
        if (__ssx_kernel_context_thread())
            ssx_critical_section_enter(SSX_NONCRITICAL, &l_ctx);

        // Get the lock
        l_rc = ssx_semaphore_pend(i_td_ptr->sem,TRAC_INTF_MUTEX_TIMEOUT);
        if(l_rc != SSX_OK)
        {
            // Badness
            FIELD("TRAC_get_buffer: Failed to get mutex");
        }
        else
        {
            l_rc = SUCCESS;

            // Copy it's buffer into temp one
            memcpy(o_data,*i_td_ptr->entry,(size_t)TRACE_BUFFER_SIZE);

            // Always try to release even if error above
            ssx_semaphore_post(i_td_ptr->sem);

            // Re-enable non-critical interrupts if thread context
            if (__ssx_kernel_context_thread())
                ssx_critical_section_exit(&l_ctx);
        }
    }
    else
    {
        FIELD("TRAC_get_buffer: Invalid parameter passed by caller");
        l_rc = TRAC_INVALID_PARM;
    }

    return(l_rc);
}

// Function Specification
//
// Name: TRAC_get_buffer_partial
//
// Description:
//
// End Function Specification
UINT TRAC_get_buffer_partial(const trace_descriptor_array_t *i_td_ptr,
                    void *io_data,
                    UINT *io_size)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT            l_rc = 0;
    char            *l_full_buf = NULL;
    tracDesc_t      l_head = NULL;
    UINT            l_part_size = 0;
    bool            l_lock_get = FALSE;
    SsxMachineContext   l_ctx = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    do
    {
        if((i_td_ptr == NULL) || (io_data == NULL) || (io_size == NULL))
        {
            FIELD("TRAC_get_buffer_partial: Invalid parameter passed by caller");
            l_rc = TRAC_INVALID_PARM;
            if(io_size != NULL)
            {
                *io_size = 0;
            }
            break;
        }

        // We can't even fit in first part of buffer
        // Make sure data size is larger than header length
        // Otherwise, we will be accessing beyond memory
        if(*io_size < sizeof(trace_buf_head_t))
        {
            // Need to at least have enough space for the header
            FIELD("TRAC_get_buffer_partial: *io_size too small");
            l_rc = TRAC_DATA_SIZE_LESS_THAN_HEADER_SIZE;
            *io_size = 0;
            break;
        }

        // CRITICAL REGION START
        // Disable non-critical interrupts if thread context
        if (__ssx_kernel_context_thread())
            ssx_critical_section_enter(SSX_NONCRITICAL, &l_ctx);

        // Get the lock
        l_rc = ssx_semaphore_pend(i_td_ptr->sem,TRAC_INTF_MUTEX_TIMEOUT);
        if(l_rc != SSX_OK)
        {
            // Badness
            FIELD("TRAC_get_buffer_partial: Failed to get mutex");
        }
        else
        {
            // Now that we have full buffer, adjust it to be requested size
            memset(io_data,0,(size_t)*io_size);

            l_lock_get = TRUE;
            l_full_buf = (char*)(*i_td_ptr->entry);
            if(*io_size >= TRACE_BUFFER_SIZE)
            {
                // It fits
                *io_size = TRACE_BUFFER_SIZE;
                memcpy(io_data,l_full_buf,(size_t)*io_size);
                break;
            }

            // copy the header of the trace buffer to io_data
            l_head = (tracDesc_t)l_full_buf;
            memcpy(io_data,l_full_buf,(size_t)(l_head->hdr_len));

            // Reuse the l_head to point to the io_data and fill in the data
            l_head = (tracDesc_t)io_data;

            if((l_head->next_free == l_head->hdr_len) && (l_head->times_wrap == 0))
            {
                // No data in buffer so just return what we have
                *io_size = 0;
                break;
            }

            if(l_head->next_free > *io_size)
            {
                l_part_size = *io_size - l_head->hdr_len;

                memcpy((UCHAR *)io_data+l_head->hdr_len,
                       l_full_buf+l_head->next_free-l_part_size,
                       (size_t)l_part_size);

                // We don't need to update *io_size, all data copied.
                l_head->size = *io_size;

                // Set pointer at beginning because this will be a
                // "just wrapped" buffer.
                l_head->next_free = l_head->hdr_len;

                // Buffer is now wrapped because we copied max data into it.
                if(!l_head->times_wrap)
                {
                    l_head->times_wrap = 1;
                }
            }
            else
            {
                // First part of buffer fits fine
                memcpy((UCHAR *)io_data+l_head->hdr_len,
                       l_full_buf+l_head->hdr_len,
                       (size_t)(l_head->next_free - l_head->hdr_len));


                // If it's wrapped then pick up some more data
                if(l_head->times_wrap)
                {
                    // Figure out how much room we have left
                    l_part_size = *io_size - l_head->next_free;

                    memcpy((UCHAR *)io_data+l_head->next_free,
                           l_full_buf+TRACE_BUFFER_SIZE-l_part_size,
                           (size_t)l_part_size);

                    // We don't need to update *io_size, all data copied.
                    l_head->size = *io_size;
                }
                else
                {
                    // Update copied length which is what we have in trace buffer
                    l_head->size = l_head->next_free;
                    *io_size = l_head->next_free;
                }
            }
        }
        // CRITICAL REGION END
    }
    while(FALSE);

    // Always try to release even if error above
    if(l_lock_get)
    {
        ssx_semaphore_post(i_td_ptr->sem);
        // Re-enable non-critical interrupts if thread context
        if (__ssx_kernel_context_thread())
            ssx_critical_section_exit(&l_ctx);
    }

    return(l_rc);
}

// Function Specification
//
// Name: TRAC_reset_buf
//
// Description:
//
// End Function Specification
UINT TRAC_reset_buf()
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    UINT            l_rc = 0;
    UINT            l_num_des = sizeof(g_des_array) / sizeof(trace_descriptor_array_t);
    UINT            i=0;
    SsxMachineContext   l_ctx = 0;
    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    // Disable non-critical interrupts if thread context
    if (__ssx_kernel_context_thread())
        ssx_critical_section_enter(SSX_NONCRITICAL, &l_ctx);

    for(i=0;i<l_num_des;i++)
    {
        // Get mutex so no one traces
        l_rc = ssx_semaphore_pend(g_des_array[i].sem,TRAC_INTF_MUTEX_TIMEOUT);
        if(l_rc != SSX_OK)
        {
            // Badness
            FIELD("TRAC_reset_buf: Failure trying to get mutex");
        }
        else
        {
            // Initialize the buffer
            l_rc = trac_init_values_buffer(g_des_array[i].entry,
                                           g_des_array[i].comp);
            if(l_rc)
            {
                FIELD("TRAC_reset_buf: Failure in call to trac_init_values_buffer()");
                break;
            }
        }

        // Always try to release even if fail above
        ssx_semaphore_post(g_des_array[i].sem);
    }

    // Re-enable non-critical interrupts if thread context
    if (__ssx_kernel_context_thread())
        ssx_critical_section_exit(&l_ctx);

    return(l_rc);
}


// Function Specification
//
// Name: trac_write_data_to_circular
//
// Description:
//
// End Function Specification
uint16_t trac_write_data_to_circular(circular_entire_data_t *i_ptr)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t             l_rc = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    memcpy((void *)&g_isr_circular_buf[g_isr_circular_header.head],
           (void *)i_ptr,
           sizeof(circular_entire_data_t));

    return(l_rc);
}

// Function Specification
//
// Name: get_trac_entry_data_from_circular
//
// Description:
//
// End Function Specification
uint16_t get_trac_entry_data_from_circular(circular_entire_data_t *o_ptr)
{
    /*------------------------------------------------------------------------*/
    /*  Local Variables                                                       */
    /*------------------------------------------------------------------------*/
    uint16_t            l_rc = 0;

    /*------------------------------------------------------------------------*/
    /*  Code                                                                  */
    /*------------------------------------------------------------------------*/

    memcpy((void *)o_ptr,
           (void *)&g_isr_circular_buf[g_isr_circular_header.tail],
           sizeof(circular_entire_data_t));

    return(l_rc);
}
OpenPOWER on IntegriCloud