GNU Linux-libre 4.9.337-gnu1
[releases.git] / kernel / gcov / gcc_4_7.c
1 /*
2  *  This code provides functions to handle gcc's profiling data format
3  *  introduced with gcc 4.7.
4  *
5  *  This file is based heavily on gcc_3_4.c file.
6  *
7  *  For a better understanding, refer to gcc source:
8  *  gcc/gcov-io.h
9  *  libgcc/libgcov.c
10  *
11  *  Uses gcc-internal data definitions.
12  */
13
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/seq_file.h>
18 #include <linux/vmalloc.h>
19 #include "gcov.h"
20
21 #if (__GNUC__ >= 10)
22 #define GCOV_COUNTERS                   8
23 #elif (__GNUC__ >= 7)
24 #define GCOV_COUNTERS                   9
25 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
26 #define GCOV_COUNTERS                   10
27 #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
28 #define GCOV_COUNTERS                   9
29 #else
30 #define GCOV_COUNTERS                   8
31 #endif
32
33 #define GCOV_TAG_FUNCTION_LENGTH        3
34
35 /* Since GCC 12.1 sizes are in BYTES and not in WORDS (4B). */
36 #if (__GNUC__ >= 12)
37 #define GCOV_UNIT_SIZE                          4
38 #else
39 #define GCOV_UNIT_SIZE                          1
40 #endif
41
42 static struct gcov_info *gcov_info_head;
43
44 /**
45  * struct gcov_ctr_info - information about counters for a single function
46  * @num: number of counter values for this type
47  * @values: array of counter values for this type
48  *
49  * This data is generated by gcc during compilation and doesn't change
50  * at run-time with the exception of the values array.
51  */
52 struct gcov_ctr_info {
53         unsigned int num;
54         gcov_type *values;
55 };
56
57 /**
58  * struct gcov_fn_info - profiling meta data per function
59  * @key: comdat key
60  * @ident: unique ident of function
61  * @lineno_checksum: function lineo_checksum
62  * @cfg_checksum: function cfg checksum
63  * @ctrs: instrumented counters
64  *
65  * This data is generated by gcc during compilation and doesn't change
66  * at run-time.
67  *
68  * Information about a single function.  This uses the trailing array
69  * idiom. The number of counters is determined from the merge pointer
70  * array in gcov_info.  The key is used to detect which of a set of
71  * comdat functions was selected -- it points to the gcov_info object
72  * of the object file containing the selected comdat function.
73  */
74 struct gcov_fn_info {
75         const struct gcov_info *key;
76         unsigned int ident;
77         unsigned int lineno_checksum;
78         unsigned int cfg_checksum;
79         struct gcov_ctr_info ctrs[0];
80 };
81
82 /**
83  * struct gcov_info - profiling data per object file
84  * @version: gcov version magic indicating the gcc version used for compilation
85  * @next: list head for a singly-linked list
86  * @stamp: uniquifying time stamp
87  * @checksum: unique object checksum
88  * @filename: name of the associated gcov data file
89  * @merge: merge functions (null for unused counter type)
90  * @n_functions: number of instrumented functions
91  * @functions: pointer to pointers to function information
92  *
93  * This data is generated by gcc during compilation and doesn't change
94  * at run-time with the exception of the next pointer.
95  */
96 struct gcov_info {
97         unsigned int version;
98         struct gcov_info *next;
99         unsigned int stamp;
100  /* Since GCC 12.1 a checksum field is added. */
101 #if (__GNUC__ >= 12)
102         unsigned int checksum;
103 #endif
104         const char *filename;
105         void (*merge[GCOV_COUNTERS])(gcov_type *, unsigned int);
106         unsigned int n_functions;
107         struct gcov_fn_info **functions;
108 };
109
110 /**
111  * gcov_info_filename - return info filename
112  * @info: profiling data set
113  */
114 const char *gcov_info_filename(struct gcov_info *info)
115 {
116         return info->filename;
117 }
118
119 /**
120  * gcov_info_version - return info version
121  * @info: profiling data set
122  */
123 unsigned int gcov_info_version(struct gcov_info *info)
124 {
125         return info->version;
126 }
127
128 /**
129  * gcov_info_next - return next profiling data set
130  * @info: profiling data set
131  *
132  * Returns next gcov_info following @info or first gcov_info in the chain if
133  * @info is %NULL.
134  */
135 struct gcov_info *gcov_info_next(struct gcov_info *info)
136 {
137         if (!info)
138                 return gcov_info_head;
139
140         return info->next;
141 }
142
143 /**
144  * gcov_info_link - link/add profiling data set to the list
145  * @info: profiling data set
146  */
147 void gcov_info_link(struct gcov_info *info)
148 {
149         info->next = gcov_info_head;
150         gcov_info_head = info;
151 }
152
153 /**
154  * gcov_info_unlink - unlink/remove profiling data set from the list
155  * @prev: previous profiling data set
156  * @info: profiling data set
157  */
158 void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
159 {
160         if (prev)
161                 prev->next = info->next;
162         else
163                 gcov_info_head = info->next;
164 }
165
166 /* Symbolic links to be created for each profiling data file. */
167 const struct gcov_link gcov_link[] = {
168         { OBJ_TREE, "gcno" },   /* Link to .gcno file in $(objtree). */
169         { 0, NULL},
170 };
171
172 /*
173  * Determine whether a counter is active. Doesn't change at run-time.
174  */
175 static int counter_active(struct gcov_info *info, unsigned int type)
176 {
177         return info->merge[type] ? 1 : 0;
178 }
179
180 /* Determine number of active counters. Based on gcc magic. */
181 static unsigned int num_counter_active(struct gcov_info *info)
182 {
183         unsigned int i;
184         unsigned int result = 0;
185
186         for (i = 0; i < GCOV_COUNTERS; i++) {
187                 if (counter_active(info, i))
188                         result++;
189         }
190         return result;
191 }
192
193 /**
194  * gcov_info_reset - reset profiling data to zero
195  * @info: profiling data set
196  */
197 void gcov_info_reset(struct gcov_info *info)
198 {
199         struct gcov_ctr_info *ci_ptr;
200         unsigned int fi_idx;
201         unsigned int ct_idx;
202
203         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
204                 ci_ptr = info->functions[fi_idx]->ctrs;
205
206                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
207                         if (!counter_active(info, ct_idx))
208                                 continue;
209
210                         memset(ci_ptr->values, 0,
211                                         sizeof(gcov_type) * ci_ptr->num);
212                         ci_ptr++;
213                 }
214         }
215 }
216
217 /**
218  * gcov_info_is_compatible - check if profiling data can be added
219  * @info1: first profiling data set
220  * @info2: second profiling data set
221  *
222  * Returns non-zero if profiling data can be added, zero otherwise.
223  */
224 int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
225 {
226         return (info1->stamp == info2->stamp);
227 }
228
229 /**
230  * gcov_info_add - add up profiling data
231  * @dest: profiling data set to which data is added
232  * @source: profiling data set which is added
233  *
234  * Adds profiling counts of @source to @dest.
235  */
236 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
237 {
238         struct gcov_ctr_info *dci_ptr;
239         struct gcov_ctr_info *sci_ptr;
240         unsigned int fi_idx;
241         unsigned int ct_idx;
242         unsigned int val_idx;
243
244         for (fi_idx = 0; fi_idx < src->n_functions; fi_idx++) {
245                 dci_ptr = dst->functions[fi_idx]->ctrs;
246                 sci_ptr = src->functions[fi_idx]->ctrs;
247
248                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
249                         if (!counter_active(src, ct_idx))
250                                 continue;
251
252                         for (val_idx = 0; val_idx < sci_ptr->num; val_idx++)
253                                 dci_ptr->values[val_idx] +=
254                                         sci_ptr->values[val_idx];
255
256                         dci_ptr++;
257                         sci_ptr++;
258                 }
259         }
260 }
261
262 /**
263  * gcov_info_dup - duplicate profiling data set
264  * @info: profiling data set to duplicate
265  *
266  * Return newly allocated duplicate on success, %NULL on error.
267  */
268 struct gcov_info *gcov_info_dup(struct gcov_info *info)
269 {
270         struct gcov_info *dup;
271         struct gcov_ctr_info *dci_ptr; /* dst counter info */
272         struct gcov_ctr_info *sci_ptr; /* src counter info */
273         unsigned int active;
274         unsigned int fi_idx; /* function info idx */
275         unsigned int ct_idx; /* counter type idx */
276         size_t fi_size; /* function info size */
277         size_t cv_size; /* counter values size */
278
279         dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
280         if (!dup)
281                 return NULL;
282
283         dup->next = NULL;
284         dup->filename = NULL;
285         dup->functions = NULL;
286
287         dup->filename = kstrdup(info->filename, GFP_KERNEL);
288         if (!dup->filename)
289                 goto err_free;
290
291         dup->functions = kcalloc(info->n_functions,
292                                  sizeof(struct gcov_fn_info *), GFP_KERNEL);
293         if (!dup->functions)
294                 goto err_free;
295
296         active = num_counter_active(info);
297         fi_size = sizeof(struct gcov_fn_info);
298         fi_size += sizeof(struct gcov_ctr_info) * active;
299
300         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
301                 dup->functions[fi_idx] = kzalloc(fi_size, GFP_KERNEL);
302                 if (!dup->functions[fi_idx])
303                         goto err_free;
304
305                 *(dup->functions[fi_idx]) = *(info->functions[fi_idx]);
306
307                 sci_ptr = info->functions[fi_idx]->ctrs;
308                 dci_ptr = dup->functions[fi_idx]->ctrs;
309
310                 for (ct_idx = 0; ct_idx < active; ct_idx++) {
311
312                         cv_size = sizeof(gcov_type) * sci_ptr->num;
313
314                         dci_ptr->values = vmalloc(cv_size);
315
316                         if (!dci_ptr->values)
317                                 goto err_free;
318
319                         dci_ptr->num = sci_ptr->num;
320                         memcpy(dci_ptr->values, sci_ptr->values, cv_size);
321
322                         sci_ptr++;
323                         dci_ptr++;
324                 }
325         }
326
327         return dup;
328 err_free:
329         gcov_info_free(dup);
330         return NULL;
331 }
332
333 /**
334  * gcov_info_free - release memory for profiling data set duplicate
335  * @info: profiling data set duplicate to free
336  */
337 void gcov_info_free(struct gcov_info *info)
338 {
339         unsigned int active;
340         unsigned int fi_idx;
341         unsigned int ct_idx;
342         struct gcov_ctr_info *ci_ptr;
343
344         if (!info->functions)
345                 goto free_info;
346
347         active = num_counter_active(info);
348
349         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
350                 if (!info->functions[fi_idx])
351                         continue;
352
353                 ci_ptr = info->functions[fi_idx]->ctrs;
354
355                 for (ct_idx = 0; ct_idx < active; ct_idx++, ci_ptr++)
356                         vfree(ci_ptr->values);
357
358                 kfree(info->functions[fi_idx]);
359         }
360
361 free_info:
362         kfree(info->functions);
363         kfree(info->filename);
364         kfree(info);
365 }
366
367 #define ITER_STRIDE     PAGE_SIZE
368
369 /**
370  * struct gcov_iterator - specifies current file position in logical records
371  * @info: associated profiling data
372  * @buffer: buffer containing file data
373  * @size: size of buffer
374  * @pos: current position in file
375  */
376 struct gcov_iterator {
377         struct gcov_info *info;
378         void *buffer;
379         size_t size;
380         loff_t pos;
381 };
382
383 /**
384  * store_gcov_u32 - store 32 bit number in gcov format to buffer
385  * @buffer: target buffer or NULL
386  * @off: offset into the buffer
387  * @v: value to be stored
388  *
389  * Number format defined by gcc: numbers are recorded in the 32 bit
390  * unsigned binary form of the endianness of the machine generating the
391  * file. Returns the number of bytes stored. If @buffer is %NULL, doesn't
392  * store anything.
393  */
394 static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
395 {
396         u32 *data;
397
398         if (buffer) {
399                 data = buffer + off;
400                 *data = v;
401         }
402
403         return sizeof(*data);
404 }
405
406 /**
407  * store_gcov_u64 - store 64 bit number in gcov format to buffer
408  * @buffer: target buffer or NULL
409  * @off: offset into the buffer
410  * @v: value to be stored
411  *
412  * Number format defined by gcc: numbers are recorded in the 32 bit
413  * unsigned binary form of the endianness of the machine generating the
414  * file. 64 bit numbers are stored as two 32 bit numbers, the low part
415  * first. Returns the number of bytes stored. If @buffer is %NULL, doesn't store
416  * anything.
417  */
418 static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
419 {
420         u32 *data;
421
422         if (buffer) {
423                 data = buffer + off;
424
425                 data[0] = (v & 0xffffffffUL);
426                 data[1] = (v >> 32);
427         }
428
429         return sizeof(*data) * 2;
430 }
431
432 /**
433  * convert_to_gcda - convert profiling data set to gcda file format
434  * @buffer: the buffer to store file data or %NULL if no data should be stored
435  * @info: profiling data set to be converted
436  *
437  * Returns the number of bytes that were/would have been stored into the buffer.
438  */
439 static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
440 {
441         struct gcov_fn_info *fi_ptr;
442         struct gcov_ctr_info *ci_ptr;
443         unsigned int fi_idx;
444         unsigned int ct_idx;
445         unsigned int cv_idx;
446         size_t pos = 0;
447
448         /* File header. */
449         pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
450         pos += store_gcov_u32(buffer, pos, info->version);
451         pos += store_gcov_u32(buffer, pos, info->stamp);
452
453 #if (__GNUC__ >= 12)
454         /* Use zero as checksum of the compilation unit. */
455         pos += store_gcov_u32(buffer, pos, 0);
456 #endif
457
458         for (fi_idx = 0; fi_idx < info->n_functions; fi_idx++) {
459                 fi_ptr = info->functions[fi_idx];
460
461                 /* Function record. */
462                 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
463                 pos += store_gcov_u32(buffer, pos,
464                         GCOV_TAG_FUNCTION_LENGTH * GCOV_UNIT_SIZE);
465                 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
466                 pos += store_gcov_u32(buffer, pos, fi_ptr->lineno_checksum);
467                 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
468
469                 ci_ptr = fi_ptr->ctrs;
470
471                 for (ct_idx = 0; ct_idx < GCOV_COUNTERS; ct_idx++) {
472                         if (!counter_active(info, ct_idx))
473                                 continue;
474
475                         /* Counter record. */
476                         pos += store_gcov_u32(buffer, pos,
477                                               GCOV_TAG_FOR_COUNTER(ct_idx));
478                         pos += store_gcov_u32(buffer, pos,
479                                 ci_ptr->num * 2 * GCOV_UNIT_SIZE);
480
481                         for (cv_idx = 0; cv_idx < ci_ptr->num; cv_idx++) {
482                                 pos += store_gcov_u64(buffer, pos,
483                                                       ci_ptr->values[cv_idx]);
484                         }
485
486                         ci_ptr++;
487                 }
488         }
489
490         return pos;
491 }
492
493 /**
494  * gcov_iter_new - allocate and initialize profiling data iterator
495  * @info: profiling data set to be iterated
496  *
497  * Return file iterator on success, %NULL otherwise.
498  */
499 struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
500 {
501         struct gcov_iterator *iter;
502
503         iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
504         if (!iter)
505                 goto err_free;
506
507         iter->info = info;
508         /* Dry-run to get the actual buffer size. */
509         iter->size = convert_to_gcda(NULL, info);
510         iter->buffer = vmalloc(iter->size);
511         if (!iter->buffer)
512                 goto err_free;
513
514         convert_to_gcda(iter->buffer, info);
515
516         return iter;
517
518 err_free:
519         kfree(iter);
520         return NULL;
521 }
522
523
524 /**
525  * gcov_iter_get_info - return profiling data set for given file iterator
526  * @iter: file iterator
527  */
528 void gcov_iter_free(struct gcov_iterator *iter)
529 {
530         vfree(iter->buffer);
531         kfree(iter);
532 }
533
534 /**
535  * gcov_iter_get_info - return profiling data set for given file iterator
536  * @iter: file iterator
537  */
538 struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
539 {
540         return iter->info;
541 }
542
543 /**
544  * gcov_iter_start - reset file iterator to starting position
545  * @iter: file iterator
546  */
547 void gcov_iter_start(struct gcov_iterator *iter)
548 {
549         iter->pos = 0;
550 }
551
552 /**
553  * gcov_iter_next - advance file iterator to next logical record
554  * @iter: file iterator
555  *
556  * Return zero if new position is valid, non-zero if iterator has reached end.
557  */
558 int gcov_iter_next(struct gcov_iterator *iter)
559 {
560         if (iter->pos < iter->size)
561                 iter->pos += ITER_STRIDE;
562
563         if (iter->pos >= iter->size)
564                 return -EINVAL;
565
566         return 0;
567 }
568
569 /**
570  * gcov_iter_write - write data for current pos to seq_file
571  * @iter: file iterator
572  * @seq: seq_file handle
573  *
574  * Return zero on success, non-zero otherwise.
575  */
576 int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
577 {
578         size_t len;
579
580         if (iter->pos >= iter->size)
581                 return -EINVAL;
582
583         len = ITER_STRIDE;
584         if (iter->pos + len > iter->size)
585                 len = iter->size - iter->pos;
586
587         seq_write(seq, iter->buffer + iter->pos, len);
588
589         return 0;
590 }