Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ktap / include / ktap_types.h
1 #ifndef __KTAP_TYPES_H__
2 #define __KTAP_TYPES_H__
3
4 #ifdef __KERNEL__
5 #include <linux/perf_event.h>
6 #else
7 typedef char u8;
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdint.h>
12 #endif
13
14 /*
15  * The first argument type of kdebug.probe_by_id()
16  * The value is a userspace memory pointer.
17  */
18 typedef struct ktap_eventdef_info {
19         int nr;
20         int *id_arr;
21         char *filter;
22 } ktap_eventdef_info;
23
24 typedef struct ktap_parm {
25         char *trunk; /* __user */
26         int trunk_len;
27         int argc;
28         char **argv; /* __user */
29         int verbose;
30         int trace_pid;
31         int workload;
32         int trace_cpu;
33         int print_timestamp;
34         int quiet;
35 } ktap_parm;
36
37 /*
38  * Ioctls that can be done on a ktap fd:
39  * todo: use _IO macro in include/uapi/asm-generic/ioctl.h
40  */
41 #define KTAP_CMD_IOC_VERSION            ('$' + 0)
42 #define KTAP_CMD_IOC_RUN                ('$' + 1)
43 #define KTAP_CMD_IOC_EXIT               ('$' + 3)
44
45 #define KTAP_ENV        "_ENV"
46
47 #define KTAP_VERSION_MAJOR       "0"
48 #define KTAP_VERSION_MINOR       "4"
49
50 #define KTAP_VERSION    "ktap " KTAP_VERSION_MAJOR "." KTAP_VERSION_MINOR
51 #define KTAP_AUTHOR    "Jovi Zhangwei <jovi.zhangwei@gmail.com>"
52 #define KTAP_COPYRIGHT  KTAP_VERSION "  Copyright (C) 2012-2013, " KTAP_AUTHOR
53
54 #define MYINT(s)        (s[0] - '0')
55 #define VERSION         (MYINT(KTAP_VERSION_MAJOR) * 16 + MYINT(KTAP_VERSION_MINOR))
56 #define FORMAT          0 /* this is the official format */
57
58 #define KTAP_SIGNATURE  "\033ktap"
59
60 /* data to catch conversion errors */
61 #define KTAPC_TAIL      "\x19\x93\r\n\x1a\n"
62
63 /* size in bytes of header of binary files */
64 #define KTAPC_HEADERSIZE        (sizeof(KTAP_SIGNATURE) - sizeof(char) + 2 + \
65                                  6 + sizeof(KTAPC_TAIL) - sizeof(char))
66
67 typedef long ktap_number;
68 #define kp_number2int(i, n)     ((i) = (int)(n))
69
70 typedef int ktap_instruction;
71
72 typedef union ktap_gcobject ktap_gcobject;
73
74 #define CommonHeader ktap_gcobject *next; u8 tt;
75
76 typedef union ktap_string {
77         int dummy;  /* ensures maximum alignment for strings */
78         struct {
79                 CommonHeader;
80                 u8 extra;  /* reserved words for short strings; "has hash" for longs */
81                 unsigned int hash;
82                 size_t len;  /* number of characters in string */
83         } tsv;
84         /* short string is stored here, just after tsv */
85 } ktap_string;
86
87
88 struct ktap_state;
89 typedef int (*ktap_cfunction) (struct ktap_state *ks);
90
91 typedef struct ktap_value {
92         union {
93                 ktap_gcobject *gc;    /* collectable objects */
94                 void *p;         /* light userdata */
95                 int b;           /* booleans */
96                 ktap_cfunction f; /* light C functions */
97                 ktap_number n;         /* numbers */
98         } val;
99         int type;
100 } ktap_value;
101
102 typedef ktap_value * StkId;
103
104
105 /*
106  * Description of an upvalue for function prototypes
107  */
108 typedef struct ktap_upvaldesc {
109         ktap_string *name;  /* upvalue name (for debug information) */
110         u8 instack;  /* whether it is in stack */
111         u8 idx;  /* index of upvalue (in stack or in outer function's list) */
112 } ktap_upvaldesc;
113
114 /*
115  * Description of a local variable for function prototypes
116  * (used for debug information)
117  */
118 typedef struct ktap_locvar {
119         ktap_string *varname;
120         int startpc;  /* first point where variable is active */
121         int endpc;    /* first point where variable is dead */
122 } ktap_locvar;
123
124
125 typedef struct ktap_upval {
126         CommonHeader;
127         ktap_value *v;  /* points to stack or to its own value */
128         union {
129                 ktap_value value;  /* the value (when closed) */
130                 struct {  /* double linked list (when open) */
131                         struct ktap_upval *prev;
132                         struct ktap_upval *next;
133                 } l;
134         } u;
135 } ktap_upval;
136
137
138 #define KTAP_MAX_STACK_ENTRIES 100
139
140 typedef struct ktap_btrace {
141         CommonHeader;
142         unsigned int nr_entries;
143         /* entries stored in here, after nr_entries */
144 } ktap_btrace;
145
146 typedef struct ktap_closure {
147         CommonHeader;
148         u8 nupvalues;
149         struct ktap_proto *p;
150         struct ktap_upval *upvals[1];  /* list of upvalues */
151         ktap_gcobject *gclist;
152 } ktap_closure;
153
154 typedef struct ktap_proto {
155         CommonHeader;
156         ktap_value *k;  /* constants used by the function */
157         ktap_instruction *code;
158         struct ktap_proto **p;  /* functions defined inside the function */
159         int *lineinfo;  /* map from opcodes to source lines (debug information) */
160         struct ktap_locvar *locvars;  /* information about local variables (debug information) */
161         struct ktap_upvaldesc *upvalues;  /* upvalue information */
162         ktap_closure *cache;  /* last created closure with this prototype */
163         ktap_string  *source;  /* used for debug information */
164         int sizeupvalues;  /* size of 'upvalues' */
165         int sizek;  /* size of `k' */
166         int sizecode;
167         int sizelineinfo;
168         int sizep;  /* size of `p' */
169         int sizelocvars;
170         int linedefined;
171         int lastlinedefined;
172         u8 numparams;  /* number of fixed parameters */
173         u8 is_vararg;
174         u8 maxstacksize;  /* maximum stack used by this function */
175 } ktap_proto;
176
177
178 /*
179  * information about a call
180  */
181 typedef struct ktap_callinfo {
182         StkId func;  /* function index in the stack */
183         StkId top;  /* top for this function */
184         struct ktap_callinfo *prev, *next;  /* dynamic call link */
185         short nresults;  /* expected number of results from this function */
186         u8 callstatus;
187         int extra;
188         union {
189                 struct {  /* only for ktap functions */
190                         StkId base;  /* base for this function */
191                         const unsigned int *savedpc;
192                 } l;
193                 struct {  /* only for C functions */
194                         int ctx;  /* context info. in case of yields */
195                         u8 status;
196                 } c;
197         } u;
198 } ktap_callinfo;
199
200
201 /*
202  * ktap_tab
203  */
204 typedef struct ktap_tkey {
205         struct ktap_tnode *next;  /* for chaining */
206         ktap_value tvk;
207 } ktap_tkey;
208
209
210 typedef struct ktap_tnode {
211         ktap_value i_val;
212         ktap_tkey i_key;
213 } ktap_tnode;
214
215
216 typedef struct ktap_stat_data {
217         int count;
218         int sum;
219         int min, max;
220 } ktap_stat_data;
221
222
223 typedef struct ktap_tab {
224         CommonHeader;
225 #ifdef __KERNEL__
226         arch_spinlock_t lock;
227 #endif
228         u8 flags;  /* 1<<p means tagmethod(p) is not present */
229         u8 lsizenode;  /* log2 of size of `node' array */
230         int sizearray;  /* size of `array' array */
231         ktap_value *array;  /* array part */
232         ktap_tnode *node;
233         ktap_tnode *lastfree;  /* any free position is before this position */
234
235         int with_stats;  /* for aggregation table: ptable */
236         ktap_stat_data *sd_arr;
237         ktap_stat_data *sd_rec;
238
239         ktap_tnode *sorted;  /* sorted table, with linked node list */
240         ktap_tnode *sort_head;
241
242         ktap_gcobject *gclist;
243 } ktap_tab;
244
245 #define lmod(s,size)    ((int)((s) & ((size)-1)))
246
247 /* parallel table */
248 typedef struct ktap_ptab {
249         CommonHeader;
250         ktap_tab **tbl; /* percpu table */
251         ktap_tab *agg;
252 } ktap_ptab;
253
254 typedef struct ktap_stringtable {
255         ktap_gcobject **hash;
256         int nuse;
257         int size;
258 } ktap_stringtable;
259
260 #ifdef CONFIG_KTAP_FFI
261 typedef int csymbol_id;
262 typedef struct csymbol csymbol;
263
264 /* global ffi state maintained in each ktap vm instance */
265 typedef struct ffi_state {
266         ktap_tab *ctable;
267         int csym_nr;
268         csymbol *csym_arr;
269 } ffi_state;
270
271 /* instance of csymbol */
272 typedef struct ktap_cdata {
273         CommonHeader;
274         csymbol_id id;
275         union {
276                 uint64_t i;
277                 void *p;        /* pointer address */
278                 void *st;       /* struct member data */
279         } u;
280 } ktap_cdata;
281 #endif
282
283 typedef struct ktap_stats {
284         int mem_allocated;
285         int nr_mem_allocate;
286         int nr_mem_free;
287         int events_hits;
288         int events_missed;
289 } ktap_stats;
290
291 #define KTAP_STATS(ks)  this_cpu_ptr(G(ks)->stats)
292
293 enum {
294         KTAP_PERCPU_DATA_STATE,
295         KTAP_PERCPU_DATA_STACK,
296         KTAP_PERCPU_DATA_BUFFER,
297         KTAP_PERCPU_DATA_BUFFER2,
298         KTAP_PERCPU_DATA_BTRACE,
299
300         KTAP_PERCPU_DATA_MAX
301 };
302
303 typedef struct ktap_global_state {
304         ktap_stringtable strt;  /* hash table for strings */
305         ktap_value registry;
306         unsigned int seed; /* randonized seed for hashes */
307
308         ktap_gcobject *allgc; /* list of all collectable objects */
309
310         ktap_upval uvhead; /* head of double-linked list of all open upvalues */
311
312         struct ktap_state *mainthread;
313 #ifdef __KERNEL__
314         /* global percpu data(like stack) */
315         void __percpu *pcpu_data[KTAP_PERCPU_DATA_MAX][PERF_NR_CONTEXTS];
316
317         int __percpu *recursion_context[PERF_NR_CONTEXTS];
318
319         arch_spinlock_t str_lock; /* string opertion lock */
320
321         ktap_parm *parm;
322         pid_t trace_pid;
323         struct task_struct *trace_task;
324         cpumask_var_t cpumask;
325         struct ring_buffer *buffer;
326         struct dentry *trace_pipe_dentry;
327         int nr_builtin_cfunction;
328         ktap_value *cfunction_tbl;
329         struct task_struct *task;
330         int trace_enabled;
331         struct list_head timers;
332         struct list_head probe_events_head;
333         int exit;
334         int wait_user;
335         ktap_closure *trace_end_closure;
336         struct ktap_stats __percpu *stats;
337         struct kmem_cache *pevent_cache;
338 #ifdef CONFIG_KTAP_FFI
339         ffi_state  ffis;
340 #endif
341 #endif
342         int error;
343 } ktap_global_state;
344
345 typedef struct ktap_state {
346         CommonHeader;
347         ktap_global_state *g;
348         int stop;
349         StkId top;
350         ktap_callinfo *ci;
351         const unsigned long *oldpc;
352         StkId stack_last;
353         StkId stack;
354         ktap_gcobject *openupval;
355         ktap_callinfo baseci;
356
357         /* list of temp collectable objects, free when thread exit */
358         ktap_gcobject *gclist;
359
360 #ifdef __KERNEL__
361         struct ktap_event *current_event;
362 #endif
363 } ktap_state;
364
365
366 typedef struct gcheader {
367         CommonHeader;
368 } gcheader;
369
370 /*
371  * Union of all collectable objects
372  */
373 union ktap_gcobject {
374         gcheader gch;  /* common header */
375         union ktap_string ts;
376         struct ktap_closure cl;
377         struct ktap_tab h;
378         struct ktap_ptab ph;
379         struct ktap_proto p;
380         struct ktap_upval uv;
381         struct ktap_state th;  /* thread */
382         struct ktap_btrace bt;  /* backtrace object */
383 #ifdef CONFIG_KTAP_FFI
384         struct ktap_cdata cd;
385 #endif
386 };
387
388 #define gch(o)                  (&(o)->gch)
389
390 /* macros to convert a GCObject into a specific value */
391 #define rawgco2ts(o)            (&((o)->ts))
392
393 #define gco2ts(o)               (&rawgco2ts(o)->tsv)
394 #define gco2uv(o)               (&((o)->uv))
395 #define obj2gco(v)              ((ktap_gcobject *)(v))
396 #define check_exp(c, e)         (e)
397
398
399 /* predefined values in the registry */
400 #define KTAP_RIDX_MAINTHREAD     1
401 #define KTAP_RIDX_GLOBALS        2
402 #define KTAP_RIDX_LAST           KTAP_RIDX_GLOBALS
403
404 #define KTAP_TNONE              (-1)
405
406 #define KTAP_TNIL               0
407 #define KTAP_TBOOLEAN           1
408 #define KTAP_TLIGHTUSERDATA     2
409 #define KTAP_TNUMBER            3
410 #define KTAP_TSTRING            4
411 #define KTAP_TSHRSTR            (KTAP_TSTRING | (0 << 4))  /* short strings */
412 #define KTAP_TLNGSTR            (KTAP_TSTRING | (1 << 4))  /* long strings */
413 #define KTAP_TTABLE             5
414 #define KTAP_TFUNCTION          6
415 #define KTAP_TCLOSURE           (KTAP_TFUNCTION | (0 << 4))  /* closure */
416 #define KTAP_TCFUNCTION         (KTAP_TFUNCTION | (1 << 4))  /* light C function */
417 #define KTAP_TTHREAD            7
418 #define KTAP_TPROTO             8
419 #define KTAP_TUPVAL             9
420 #define KTAP_TEVENT             10
421 #define KTAP_TBTRACE            11
422 #define KTAP_TPTABLE            12
423 #define KTAP_TSTATDATA          13
424 #define KTAP_TCDATA             14
425 /*
426  * type number is ok so far, but it may collide later between
427  * 16+ and | (1 << 4), so be careful on this.
428  */
429
430 #define ttype(o)                ((o->type) & 0x3F)
431 #define settype(obj, t)         ((obj)->type = (t))
432
433 /* raw type tag of a TValue */
434 #define rttype(o)               ((o)->type)
435
436 /* tag with no variants (bits 0-3) */
437 #define novariant(x)            ((x) & 0x0F)
438
439 /* type tag of a TValue with no variants (bits 0-3) */
440 #define ttypenv(o)              (novariant(rttype(o)))
441
442 #define val_(o)                 ((o)->val)
443 #define gcvalue(o)              (val_(o).gc)
444
445 #define bvalue(o)               (val_(o).b)
446 #define nvalue(o)               (val_(o).n)
447 #define hvalue(o)               (&val_(o).gc->h)
448 #define phvalue(o)              (&val_(o).gc->ph)
449 #define clvalue(o)              (&val_(o).gc->cl)
450
451 #define getstr(ts)              (const char *)((ts) + 1)
452 #define eqshrstr(a, b)          ((a) == (b))
453 #define rawtsvalue(o)           (&val_(o).gc->ts)
454 #define svalue(o)               getstr(rawtsvalue(o))
455
456 #define pvalue(o)               (&val_(o).p)
457 #define sdvalue(o)              ((ktap_stat_data *)val_(o).p)
458 #define fvalue(o)               (val_(o).f)
459 #define evalue(o)               (val_(o).p)
460 #define btvalue(o)              (&val_(o).gc->bt)
461 #define cdvalue(o)              (&val_(o).gc->cd)
462
463 #define is_nil(o)               ((o)->type == KTAP_TNIL)
464 #define is_boolean(o)           ((o)->type == KTAP_TBOOLEAN)
465 #define is_false(o)             (is_nil(o) || (is_boolean(o) && bvalue(o) == 0))
466 #define is_shrstring(o)         ((o)->type == KTAP_TSHRSTR)
467 #define is_string(o)            (((o)->type & 0x0F) == KTAP_TSTRING)
468 #define is_number(o)            ((o)->type == KTAP_TNUMBER)
469 #define is_table(o)             ((o)->type == KTAP_TTABLE)
470 #define is_ptable(o)            ((o)->type == KTAP_TPTABLE)
471 #define is_statdata(o)          ((o)->type == KTAP_TSTATDATA)
472 #define is_event(o)             ((o)->type == KTAP_TEVENT)
473 #define is_btrace(o)            ((o)->type == KTAP_TBTRACE)
474 #define is_needclone(o)         is_btrace(o)
475 #ifdef CONFIG_KTAP_FFI
476 #define is_cdata(o)             ((o)->type == KTAP_TCDATA)
477 #endif
478
479
480 #define set_nil(obj) \
481         { ktap_value *io = (obj); io->val.n = 0; settype(io, KTAP_TNIL); }
482
483 #define set_boolean(obj, x) \
484         { ktap_value *io = (obj); io->val.b = (x); settype(io, KTAP_TBOOLEAN); }
485
486 #define set_number(obj, x) \
487         { ktap_value *io = (obj); io->val.n = (x); settype(io, KTAP_TNUMBER); }
488
489 #define set_statdata(obj, x) \
490         { ktap_value *io = (obj); \
491           io->val.p = (x); settype(io, KTAP_TSTATDATA); }
492
493 #define set_string(obj, x) \
494         { ktap_value *io = (obj); \
495           ktap_string *x_ = (x); \
496           io->val.gc = (ktap_gcobject *)x_; settype(io, x_->tsv.tt); }
497
498 #define set_closure(obj, x) \
499         { ktap_value *io = (obj); \
500           io->val.gc = (ktap_gcobject *)x; settype(io, KTAP_TCLOSURE); }
501
502 #define set_cfunction(obj, x) \
503         { ktap_value *io = (obj); val_(io).f = (x); settype(io, KTAP_TCFUNCTION); }
504
505 #define set_table(obj, x) \
506         { ktap_value *io = (obj); \
507           val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TTABLE); }
508
509 #define set_ptable(obj, x) \
510         { ktap_value *io = (obj); \
511           val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TPTABLE); }
512
513 #define set_thread(obj, x) \
514         { ktap_value *io = (obj); \
515           val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TTHREAD); }
516
517 #define set_event(obj, x) \
518         { ktap_value *io = (obj); val_(io).p = (x); settype(io, KTAP_TEVENT); }
519
520 #define set_btrace(obj, x) \
521         { ktap_value *io = (obj); \
522           val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TBTRACE); }
523
524 #ifdef CONFIG_KTAP_FFI
525 #define set_cdata(obj, x) \
526         { ktap_value *io=(obj); \
527           val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TCDATA); }
528 #endif
529
530 #define set_obj(obj1, obj2) \
531         { const ktap_value *io2 = (obj2); ktap_value *io1 = (obj1); \
532           io1->val = io2->val; io1->type = io2->type; }
533
534 #define rawequalobj(t1, t2) \
535         (((t1)->type == (t2)->type) && kp_equalobjv(NULL, t1, t2))
536
537 #define incr_top(ks) {ks->top++;}
538
539 #define NUMADD(a, b)    ((a) + (b))
540 #define NUMSUB(a, b)    ((a) - (b))
541 #define NUMMUL(a, b)    ((a) * (b))
542 #define NUMDIV(a, b)    ((a) / (b))
543 #define NUMUNM(a)       (-(a))
544 #define NUMEQ(a, b)     ((a) == (b))
545 #define NUMLT(a, b)     ((a) < (b))
546 #define NUMLE(a, b)     ((a) <= (b))
547 #define NUMISNAN(a)     (!NUMEQ((a), (a)))
548
549 /* todo: floor and pow in kernel */
550 #define NUMMOD(a, b)    ((a) % (b))
551 #define NUMPOW(a, b)    (pow(a, b))
552
553 #define ktap_assert(s)
554
555 #define kp_realloc(ks, v, osize, nsize, t) \
556         ((v) = (t *)kp_reallocv(ks, v, osize * sizeof(t), nsize * sizeof(t)))
557
558 #define kp_error(ks, args...) \
559         do { \
560                 kp_printf(ks, "error: "args);   \
561                 G(ks)->error = 1; \
562                 kp_exit(ks);    \
563         } while(0)
564
565 #ifdef __KERNEL__
566 #define G(ks)   (ks->g)
567
568 void kp_printf(ktap_state *ks, const char *fmt, ...);
569 extern void __kp_puts(ktap_state *ks, const char *str);
570 extern void __kp_bputs(ktap_state *ks, const char *str);
571
572 #define kp_puts(ks, str) ({                                             \
573         static const char *trace_printk_fmt                             \
574                 __attribute__((section("__trace_printk_fmt"))) =        \
575                 __builtin_constant_p(str) ? str : NULL;                 \
576                                                                         \
577         if (__builtin_constant_p(str))                                  \
578                 __kp_bputs(ks, trace_printk_fmt);               \
579         else                                                            \
580                 __kp_puts(ks, str);             \
581 })
582
583 #else
584 /*
585  * this is used for ktapc tstring operation, tstring need G(ks)->strt
586  * and G(ks)->seed, so ktapc need to init those field
587  */
588 #define G(ks)   (&dummy_global_state)
589 extern ktap_global_state dummy_global_state;
590
591 #define kp_printf(ks, args...)                  printf(args)
592 #define kp_puts(ks, str)                        printf("%s", str)
593 #define kp_exit(ks)                             exit(EXIT_FAILURE)
594
595 #endif
596
597 #define __maybe_unused  __attribute__((unused))
598
599 /*
600  * KTAP_QL describes how error messages quote program elements.
601  * CHANGE it if you want a different appearance.
602  */
603 #define KTAP_QL(x)      "'" x "'"
604 #define KTAP_QS         KTAP_QL("%s")
605
606 #define STRINGIFY(type) #type
607
608 #endif /* __KTAP_TYPES_H__ */
609