Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: profile.c,v 1.17 2016/07/07 06:55:38 msaitoh Exp $	*/

/*
 * Copyright 1997
 * Digital Equipment Corporation. All rights reserved.
 *
 * This software is furnished under license and may be used and
 * copied only in accordance with the following terms and conditions.
 * Subject to these conditions, you may download, copy, install,
 * use, modify and distribute this software in source and/or binary
 * form. No title or ownership is transferred hereby.
 *
 * 1) Any source code used, modified or distributed must reproduce
 *    and retain this copyright notice and list of conditions as
 *    they appear in the source file.
 *
 * 2) No right is granted to use any trade name, trademark, or logo of
 *    Digital Equipment Corporation. Neither the "Digital Equipment
 *    Corporation" name nor any trademark or logo of Digital Equipment
 *    Corporation may be used to endorse or promote products derived
 *    from this software without the prior written permission of
 *    Digital Equipment Corporation.
 *
 * 3) This software is provided "AS-IS" and any express or implied
 *    warranties, including but not limited to, any implied warranties
 *    of merchantability, fitness for a particular purpose, or
 *    non-infringement are disclaimed. In no event shall DIGITAL be
 *    liable for any damages whatsoever, and in particular, DIGITAL
 *    shall not be liable for special, indirect, consequential, or
 *    incidental damages or damages for lost profits, loss of
 *    revenue or loss of use, whether such damages arise in contract,
 *    negligence, tort, under statute, in equity, at law or otherwise,
 *    even if advised of the possibility of such damage.
 */

/*
 * The fiq based profiler.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: profile.c,v 1.17 2016/07/07 06:55:38 msaitoh Exp $");

#include "profiler.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/ioctl.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <sys/malloc.h>

#include <shark/shark/hat.h>
#include <machine/profileio.h>
#include <dev/ic/i8253reg.h>

#define PROFILER_DEBUG 1

#define countPerTick 500 /* TIMER_FREQ/10000   10 kHz timer */

/* Processor Status Defines */
#define STATUS_MODE_MASK 0x1f
#define USER_MODE        0x10
#define FIQ_MODE         0x11
#define IRQ_MODE         0x12
#define SVC_MODE         0x13
#define ABORT_MODE       0x17
#define UNDEF_MODE       0x1b
#define SYS_MODE         0x1f

/* software controller
 */
struct profiler_sc 
{
    int state;
#define PROF_OPEN 0x01
#define PROF_PROFILING 0x02
} prof_sc;

/*
 * GLOBAL DATA
 */

/* I need my own stack space for the hat */
#define HATSTACKSIZE 1024       /* size of stack used during a FIQ */
static unsigned char hatStack[HATSTACKSIZE]; /* actual stack used 
					      * during a FIQ 
					      */
/* Pointer to the list of hash tables.
 * A backup table is created for every table malloced, this
 * is used so that we don't miss samples while copying the 
 * data out. Thus the actual number of tables in the array is twice
 * what nhashTables says.
 */
struct profHashTable *profTable;
struct profHashTable *phashTables[2];
int nhashTables;

/*
 * FORWARD DECLARATIONS
 */
static void profFiq(int  x);
static void profHatWedge(int nFIQs);
void profStop(void);
void profStart(struct profStartInfo *);
static void profEnter(struct profHashTable * , unsigned int);
void displayTable(struct profHashTable * );

dev_type_open(profopen);
dev_type_close(profclose);
dev_type_read(profread);
dev_type_ioctl(profioctl);

const struct cdevsw prof_cdevsw = {
	.d_open = profopen,
	.d_close = profclose,
	.d_read = profread,
	.d_write = nowrite,
	.d_ioctl = profioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

void 
profilerattach(int n)
{
    /* reset the profiler state */
    prof_sc.state = 0;
}

/*
 * Open the profiling devicee.  
 * Returns 
 *       ENXIO for illegal minor device 
 *             ie. if the minor device number is not 0.
 *       EBUSY if file is open by another process.
 *       EROFS if attempt to open in write mode.
 */
int
profopen(dev_t dev, int flag, int mode, struct proc *p)
{

    /* check that the minor number is correct. */
    if (minor(dev) >= NPROFILER)
    {
	return ENXIO;
    }

    /* check that the device is not already open. */
    if (prof_sc.state && PROF_OPEN)
    {
	return EBUSY;
    }

    /* check that the flag is set to read only. */
    if (!(flag && FWRITE))
    {
	return EROFS;
    }
    /* flag the device as open. */
    prof_sc.state |= PROF_OPEN;
    nhashTables = 0;
    phashTables[0] = phashTables[1] = NULL;
    return 0;
}

/*
 * Close the descriptor.
 * 
 */
int
profclose(dev_t dev, int flag, int mode, struct proc *p)
{
    /* clear the state, and stop profiling if 
     * it is happening.
     */
    profStop();
    prof_sc.state &= ~PROF_OPEN;
    return 0;
}

int
profread(dev_t dev, struct uio *uio, int flags)
{
    int error;
    int real, backup;
    
    /* must be profiling to read */
    if (!(prof_sc.state & PROF_PROFILING))
    {
	error = EINVAL;
    }
    else
    {
	if (uio->uio_resid != sizeof(struct profHashHeader) + 
	    profTable->hdr.tableSize * sizeof(struct profHashEntry))
	{
	    printf("profile read size is incorrect!");
	    error = EINVAL;
	}
	else
	{
	    /* first work out which table is currently being used.
	     */
	    if (profTable == phashTables[0])
	    {
		real = 0;
		backup = 1;
	    }
	    else
	    {
		if (profTable == phashTables[1])
		{
		    real = 1;
		    backup = 0;
		}
		else
		{
		    panic("profiler lost buffer");
		}
	    }
	    /* now initialise the backup copy before switching over.
	     */   
	    memset(phashTables[backup]->entries, 0,
		  profTable->hdr.tableSize * sizeof(struct profHashEntry));

	    
	    /* now initialise the header */
	    phashTables[backup]->hdr.tableSize = phashTables[real]->hdr.tableSize;
	    phashTables[backup]->hdr.entries = phashTables[backup]->hdr.last 
		= phashTables[real]->hdr.entries;
	    phashTables[backup]->hdr.samples = 0;
	    phashTables[backup]->hdr.missed = 0;
	    phashTables[backup]->hdr.fiqs = 0;
	    phashTables[backup]->hdr.pid = phashTables[real]->hdr.pid;
	    phashTables[backup]->hdr.mode = phashTables[real]->hdr.mode;
	    
	    /* ok now swap over. 
	     * I don't worry about locking the fiq while I change
	     * this, at this point it won't matter which table the 
	     * fiq reads.
	     */
	    profTable = phashTables[backup];
	    
	    /* don't want to send the pointer,
	     * make assumption that table follows the header.
	     */
	    if ( (error = uiomove(phashTables[real], 
				  sizeof(struct profHashHeader), uio))
		!= 0)
	    {
		printf("uiomove failed error is %d\n", error);
	    }
	    else
	    {
		if ( (error = uiomove(phashTables[real]->entries, 
				      phashTables[real]->hdr.tableSize * 
				      sizeof(struct profHashEntry), uio))
		    != 0)
		{
		    printf("uiomove failed error is %d\n", error);
		}
	    }
	}
    }
    return error;
}

/*
 *  PROFIOSTART	  Start Profiling
 *  PROFIOSTOP	  Stop Profiling
 */
static int profcount = 0;
static int ints = 0;
int
profioctl(dev_t dev, u_long cmd, void *data, int flag, struct proc *p)
{
    int error = 0;
    struct profStartInfo *info = (struct profStartInfo *) data;

    switch (cmd)
    {
	case PROFIOSTART :
	    profStart(info);
	    break;
	case PROFIOSTOP :
	    profStop();
	    break;
	default :
	    error = EINVAL;
	    break;
    }
    return error;
}

/* start profiling, returning status information in the 
 * profStartInfo structure.
 * 
 * presumes pid is running, does no checks here.
 */
void 
profStart(struct profStartInfo *info)
{
    unsigned int savedInts;
    char *buffer;

    /* can't already be sampling */
    if ( prof_sc.state & PROF_PROFILING )
    {
	info->status = ALREADY_SAMPLING;
	return ;
    }

    /* sanity check that the table sizes are logical */
    if (info->entries > info->tableSize)
    {
	info->status = BAD_TABLE_SIZE;
	return ;
    }
    
    /* now sanity check that we are sampling either the 
     * kernel or a pid or both.
     */
    if ( !(info->mode & SAMPLE_MODE_MASK) )
    {
	info->status = ILLEGAL_COMMAND;
	return ;
    }

    /* alloc two hash tables. */
    buffer  = malloc(sizeof(struct profHashTable) + 
		     info->tableSize * sizeof(struct profHashEntry), 
		     M_DEVBUF, M_NOWAIT);
    if ( (buffer == NULL) )
    {
	info->status = NO_MEMORY;
	return;
    }
    phashTables[0] = (struct profHashTable *) buffer;
    phashTables[0]->entries = (struct profHashEntry *) 
	( buffer + sizeof(struct profHashTable));

    buffer  = malloc(sizeof(struct profHashTable) + 
		     info->tableSize * sizeof(struct profHashEntry), 
		     M_DEVBUF, M_NOWAIT);
    if ( (buffer == NULL) )
    {
	free(phashTables[0], M_DEVBUF);
	info->status = NO_MEMORY;
	return;
    }
    phashTables[1] = (struct profHashTable *) buffer;
    phashTables[1]->entries = (struct profHashEntry *) 
	( buffer + sizeof(struct profHashTable));

    memset(phashTables[0]->entries, 0,
	  info->tableSize * sizeof(struct profHashEntry));
    memset(phashTables[1]->entries, 0,
	  info->tableSize * sizeof(struct profHashEntry));

    /* now initialise the header */
    profTable = phashTables[0];
    profTable->hdr.tableSize = info->tableSize;
    profTable->hdr.entries = profTable->hdr.last = info->entries;
    profTable->hdr.samples = 0;
    profTable->hdr.missed = 0;
    profTable->hdr.fiqs = 0;
    profTable->hdr.pid = info->pid;
    profTable->hdr.mode = info->mode;

    /* now let the pigeons loose. */
    savedInts = disable_interrupts(I32_bit | F32_bit);
    prof_sc.state |= PROF_PROFILING;
    hatClkOn(countPerTick,
	     profFiq,
	     (int)&prof_sc,
	     hatStack + HATSTACKSIZE - sizeof(unsigned),
	     profHatWedge);
    restore_interrupts(savedInts);
}

void
profStop(void)
{
    unsigned int savedInts;
    int spl;

    savedInts = disable_interrupts(I32_bit | F32_bit);
    hatClkOff();
    restore_interrupts(savedInts);

    spl = splbio();
    /* only free the buffer's if we were profiling,
     * who cares if we were not, won't alert any one.
     */
    if (prof_sc.state & PROF_PROFILING)
    {
	/* now free both buffers. */
	free(phashTables[0], M_DEVBUF);
	free(phashTables[1], M_DEVBUF);
    }
    phashTables[0] = phashTables[1] = NULL;
    prof_sc.state &= ~PROF_PROFILING;
    splx(spl);

}
    
/*
**++
**  FUNCTIONAL DESCRIPTION:
**
**      profFiq
**
**      This is what the HAT clock calls.   This call drives
**      the timeout queues, which in turn drive the state machines 
**      
**      Be very carefully when calling a timeout as the function
**      that is called may in turn do timeout/untimeout calls 
**      before returning 
**
**  FORMAL PARAMETERS:
**
**      int x       - not used 
**
**  IMPLICIT INPUTS:
**
**      nill
**
**  IMPLICIT OUTPUTS:
**
**      nill
**
**  FUNCTION VALUE:
**
**      nill
**
**  SIDE EFFECTS:
**
**      a timeout may be called if it is due
**--
*/
static void 
profFiq(int  x)
{
    int i;
    int *ip;           /* the fiq stack pointer */
    unsigned int spsr, stacklr;   /* the link register, off the stack. */ 

    
    /* get the link register and see where we came from.
     * We do this by getting the stack pointer using,
     * an inline assembler instruction and then going 9 
     * words up to get the return address from the fiq.
     * 
     * NOTE: the stack will change if more local variables 
     * are added so beware of modifications to this 
     * function.
     * the fiq.S handler puts the following on the stack 
     *          stmfd	sp!, {r0-r3, lr}
     * then this function does
     *          mov     ip, sp
     *          stmfd	sp!, {r4, fp, ip, lr, pc} 
     * or some variant of this.
     *
     * instead of using sp we can use ip, the saved stack pointer
     * and be done with the chance of sp changing around on us.
     *
     * so by the time we get here we have a stack that looks like.
     * (see pg 4-23, ARM programming Techniques doco for description
     * on stm instructions.)
     *         lr-fiq  (we want this one).
     *         r3-fiq
     *         r2-fiq
     *         r1-fiq
     * ip-->   r0-fiq
     *         pc-prof
     *         lr-prof
     *         ip-prof
     *         fp-prof
     * sp-->   r4-prof
     * the sp by the time we get to it will point to r4 at the 
     * bottom of the stack. So we go 9 up to get the lr we want.
     * or even better we have ip pointing to r0 and we can go 4 up
     * to get the saved link register.
     *
     * We are safer this way because fiq.S is coded assembler, we are 
     * at the mercy of the assembler for our stack.
     *
     */
    __asm("mov %0, ip" : "=r" (ip) : );
    stacklr = *(ip+4);
    
    /* get the spsr register
     */
    __asm("mrs %0, spsr" : "=r" (spsr) : );

    /* now check whether we want this sample.
     * NB. We place kernel and user level samples in the 
     * same table.
     */
    if ( (profTable->hdr.mode & SAMPLE_PROC) &&
	((spsr & STATUS_MODE_MASK) == USER_MODE) )
    {
	if ( curlwp->p_pid == profTable->hdr.pid )
	{
	    profEnter(profTable, stacklr-4);
	}
    }

    if ( profTable->hdr.mode & SAMPLE_KERN )
    {
	if ( ((spsr & STATUS_MODE_MASK) == SVC_MODE)/* || 
	    ((spsr & STATUS_MODE_MASK) == IRQ_MODE)*/ )
	{
	    /* Note: the link register will be two instructions,
	     * ahead of the "blamed" instruction. This is actually 
	     * a most likely case and might not actually highlight the 
	     * exact cause of problems, some post processing intelligence
	     * will be required to make use of this data.
	     */
	    profEnter(profTable, stacklr-4);
	}
    }
    /* increment the samples counter */
    profTable->hdr.fiqs++;
}

/*
**++
**  FUNCTIONAL DESCRIPTION:
**
**      profHatWedge
**
**      Called if the HAT timer becomes clogged/wedged.  Not
**      used by this driver, we let upper layers recover
**      from this condition
**
**  FORMAL PARAMETERS:
**
**      int nFIQs - not used
**
**  IMPLICIT INPUTS:
**
**      nill
**
**  IMPLICIT OUTPUTS:
**
**      nill
**
**  FUNCTION VALUE:
**
**      nill
**
**  SIDE EFFECTS:
**
**      nill
**--
*/
static void 
profHatWedge(int nFIQs)
{
    #ifdef PROFILER_DEBUG
        printf("profHatWedge: nFIQ = %d\n",nFIQs);
    #endif    
}

/* Enter the data in the table.
 *
 * To reduce the time taken to find samples with time
 * an eviction algorithm is implemented.
 * When a new entry in the overflow area is required 
 * the first entry in the hash table is copied there
 * and the new entry placed as the hash table entry. The 
 * displaced entry will then be the first entry accessed in 
 * the table.
 */
static void 
profEnter(struct profHashTable *table, unsigned int lr)
{
    unsigned int entries, hashShift, index, count;
    struct profHashEntry *sample;
    struct profHashEntry *first;
    struct profHashEntry *prev;
    struct profHashEntry tmpEntry;
    int tmpIndex;

    /* work out how many bits
     * are required to hash the given size.
     */
    entries = table->hdr.entries - 1;
    hashShift = 0;
    do
    {
	entries = entries << 1;
	hashShift++;
    } while (!(entries & 0x80000000));
    
    /* enter the pc in the table. */
    /* remove redundant bits. 
     * and save the count offset bits
     */
    lr = lr >> REDUNDANT_BITS;
    count = lr & COUNT_BIT_MASK;
    lr = lr >> COUNT_BITS;

    /* this is easier than working out how
     * many bits to or, based on the hashShift.
     * maybe it would be better to work out at 
     * the start and save time during the fiq.
     */
    index = (lr << hashShift) >> hashShift;
    
    first = sample = &table->entries[index];
    /* now loop until we either find the entry 
     * or the next free space.
     */
    while ( (sample->pc != lr) && (table->hdr.last < table->hdr.tableSize) )
    {
	if (sample->pc == 0)
	{
	    /* go ahead and stick it in */
	    sample->pc = lr;
	}
	else
	{
	    if (sample->next != 0)
	    {
		/* move along and continue */
		prev = sample;
		sample = &table->entries[sample->next];
	    }
	    else
	    {
		/* create a new entry if available */
		if (table->hdr.last < table->hdr.tableSize)
		{
		    sample = &table->entries[table->hdr.last];
		    /* copy the first sample into the new
		     * field.
		     */
		    memcpy(sample, first, sizeof(struct profHashEntry));
		    /* now update the new entry in the first position.
		     */
		    first->pc = lr;
		    first->next = table->hdr.last;
		    first->counts[0] = 0;
		    first->counts[1] = 0;
		    first->counts[2] = 0;
		    first->counts[3] = 0;
		    table->hdr.last++;
		    /* update the sample pointer so that we
		     * can insert the count.
		     */
		    sample = first;
		}
	    }
	}
    }
	
    /* check if we need to do an eviction. */
    if (sample != first)
    {
	/* copy the sample out of the table. */
	memcpy(&tmpEntry, sample, sizeof(struct profHashEntry));
	/* remove the sample from the chain. */
	tmpIndex = prev->next;
	prev->next = sample->next;
	/* now insert it at the beginning. */
	memcpy(sample, first, sizeof(struct profHashEntry));
	memcpy(first, &tmpEntry, sizeof(struct profHashEntry));
	/* now make the new first entry point to the old
	 * first entry.
	 */
	first->next = tmpIndex;
    }
	
    /* must now check the lr
     * to see if the table is full.
     */
    if (sample->pc == lr)
    {
	/* update the count */
	sample->counts[count]++;
	table->hdr.samples++;
    }
    else
    {
	table->hdr.missed++;
    }
}

void 
displayTable(struct profHashTable *table)
{
    int i;
    struct profHashEntry *sample;
    char buff[100] = ".............................................\n";
    
    for (i=0; i < table->hdr.tableSize; i++)
    {
	sample = &table->entries[i];
	if ((i * table->hdr.tableSize) >= table->hdr.entries)
	{
	    printf("%s", buff);
	    buff[0] = '\0';
	}
	printf("i = %d, pc = 0x%x, next = %d, counts %d %d %d %d\n",
	       i, sample->pc, sample->next, sample->counts[0], 
	       sample->counts[1], sample->counts[2], sample->counts[3]);
    }
    return;
}