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 / userspace / ffi / ctype.c
1 #include "../../include/ktap_types.h"
2 #include "../../include/ktap_opcodes.h"
3 #include "../ktapc.h"
4 #include "../cparser.h"
5
6
7 /* for ktap vm */
8 cp_csymbol_state csym_state;
9
10 #define cs_nr (csym_state.cs_nr)
11 #define cs_arr_size (csym_state.cs_arr_size)
12 #define cs_arr (csym_state.cs_arr)
13
14 csymbol *cp_id_to_csym(int id)
15 {
16         return &cs_arr[id];
17 }
18
19
20 typedef struct cp_ctype_entry {
21         char name[MAX_TYPE_NAME_LEN];
22         struct cp_ctype ct;
23 } cp_ctype_entry;
24
25 #define DEFAULT_CTYPE_ARR_SIZE 100
26 static int cte_nr;
27 static int cte_arr_size;
28 static cp_ctype_entry *cte_arr;
29
30
31 /* stack to help maintain state during parsing */
32 typedef struct cp_ctype_stack {
33         int size;
34         int top;
35         cp_ctype_entry *stack;
36 } ctype_stack;
37
38
39 static ctype_stack cts;
40
41 #define ct_stack(id) (&(cts.stack[id]))
42 #define ct_stack_ct(id) (&(cts.stack[id].ct))
43
44
45
46 int cp_ctype_reg_csymbol(csymbol *cs);
47
48
49 size_t ctype_size(const struct cp_ctype *ct)
50 {
51         if (ct->pointers - ct->is_array) {
52                 return sizeof(void*) * (ct->is_array ? ct->array_size : 1);
53
54         } else if (!ct->is_defined || ct->type == VOID_TYPE) {
55                 cp_error("can't calculate size of an undefined type");
56                 return 0;
57         } else if (ct->variable_size_known) {
58                 assert(ct->is_variable_struct && !ct->is_array);
59                 return ct->base_size + ct->variable_increment;
60         } else if (ct->is_variable_array || ct->is_variable_struct) {
61                 cp_error("internal error: calc size of variable type with "
62                                 "unknown size");
63                 return 0;
64         } else {
65                 return ct->base_size * (ct->is_array ? ct->array_size : 1);
66         }
67 }
68
69 #define MAX_STACK_SIZE 100
70 int ctype_stack_grow(int size)
71 {
72         struct cp_ctype_entry *new_st;
73
74         assert(cts.size + size < MAX_STACK_SIZE);
75
76         new_st = realloc(cts.stack, (cts.size+size)*sizeof(cp_ctype_entry));
77         if (new_st)
78                 cts.stack = new_st;
79         else
80                 return -1;
81
82         cts.size += size;
83
84         return size;
85 }
86
87 int ctype_stack_free_space()
88 {
89         return cts.size - cts.top;
90 }
91
92 void ctype_stack_reset()
93 {
94         cts.top = 0;
95 }
96
97 /* push ctype to stack, create new csymbol if needed */
98 void cp_push_ctype_with_name(struct cp_ctype *ct, const char *name, int nlen)
99 {
100         int i;
101         struct cp_ctype *nct;
102
103         if (ctype_stack_free_space() < 1)
104                 ctype_stack_grow(4);
105
106         /* we have to check pointer here because does type lookup by name
107          * before parsing '*', and for pointers, ct will always be the
108          * original type */
109         if (ct->pointers) {
110                 for (i = 0; i < cte_nr; i++) {
111                         nct = &(cte_arr[i].ct);
112                         if (nct->type == ct->type &&
113                                         nct->pointers == ct->pointers) {
114                                 break;
115                         }
116                 }
117
118                 if (i == cte_nr) {
119                         /* pointer type not found
120                          * create a new pointer symbol for this type */
121                         /* associate ctype with new csymbol */
122                         ct->ffi_cs_id = cp_symbol_build_pointer(ct);
123                         /* register wit new pointer name */
124                         cp_ctype_reg_type(csym_name(ct_ffi_cs(ct)), ct);
125                 } else {
126                         /* pointer type already registered, reinstantiate ct */
127                         *ct = cte_arr[i].ct;
128                 }
129         }
130         memset(ct_stack(cts.top), 0, sizeof(cp_ctype_entry));
131         ct_stack(cts.top)->ct = *ct;
132         if (name)
133                 strncpy(ct_stack(cts.top)->name, name, nlen);
134         cts.top++;
135 }
136
137 void cp_push_ctype(struct cp_ctype *ct)
138 {
139         cp_push_ctype_with_name(ct, NULL, 0);
140 }
141
142 void cp_set_defined(struct cp_ctype *ct)
143 {
144         ct->is_defined = 1;
145
146         /* @TODO: update ctypes and cdatas that were created before the
147          * definition came in */
148 }
149
150 void cp_ctype_dump_stack()
151 {
152         int i;
153         struct cp_ctype *ct;
154
155         printf("---------------------------\n");
156         printf("start of ctype stack (%d) dump: \n", cts.top);
157         for (i = 0; i < cts.top; i++) {
158                 ct = ct_stack_ct(i);
159                 printf("[%d] -> cp_ctype: %d, sym_type: %d, pointer: %d "
160                         "symbol_id: %d, name: %s\n",
161                         i, ct->type,
162                         csym_type(ct_ffi_cs(ct)), ct->pointers, ct->ffi_cs_id,
163                         ct_stack(i)->name);
164         }
165 }
166
167 int ctype_reg_table_grow()
168 {
169         cp_ctype_entry *new_arr;
170
171         new_arr = realloc(cte_arr, sizeof(cp_ctype_entry)*cte_arr_size*2);
172         if (!new_arr)
173                 cp_error("failed to allocate memory for ctype array\n");
174
175         cte_arr_size = cte_arr_size * 2;
176         return 0;
177 }
178
179 /* return index in csymbol array */
180 int cp_ctype_reg_csymbol(csymbol *cs)
181 {
182         if (cs_nr >= cs_arr_size) {
183                 cs_arr_size *= 2;
184                 cs_arr = realloc(cs_arr, cs_arr_size*sizeof(csymbol));
185                 if (!cs_arr)
186                         cp_error("failed to extend csymbol array!\n");
187         }
188
189         cs_arr[cs_nr] = *cs;
190         cs_nr++;
191
192         return cs_nr-1;
193 }
194
195 void __cp_symbol_dump_struct(csymbol *cs)
196 {
197         int i;
198         csymbol *ncs;
199         csymbol_struct *stcs = csym_struct(cs);
200
201         printf("=== [%s] definition ==================\n", csym_name(cs));
202         for (i = 0; i < stcs->memb_nr; i++) {
203                 printf("\t(%d) ", i);
204                 printf("csym_id: %d, ", stcs->members[i].id);
205                 ncs = &cs_arr[stcs->members[i].id];
206                 printf("name: %s, ffi_ctype: %d, %s\n",
207                         stcs->members[i].name, ncs->type, csym_name(ncs));
208         }
209 }
210
211 void cp_symbol_dump_struct(int id)
212 {
213         __cp_symbol_dump_struct(&cs_arr[id]);
214 }
215
216 int cp_symbol_build_struct(const char *stname)
217 {
218         int i, id, memb_size;
219         cp_ctype_entry *cte;
220         csymbol nst;
221         struct_member *st_membs;
222         csymbol_struct *stcs;
223
224         if (cts.top <= 0 || !stname) {
225                 cp_error("invalid struct definition.\n");
226         }
227
228         memb_size = cts.top;
229         st_membs = malloc(memb_size*sizeof(struct_member));
230         if (!st_membs)
231                 cp_error("failed to allocate memory for struct members.\n");
232         memset(st_membs, 0, memb_size*sizeof(struct_member));
233
234         nst.type = FFI_STRUCT;
235         strcpy(nst.name, stname);
236
237         stcs = csym_struct(&nst);
238         stcs->memb_nr = memb_size;
239         stcs->members = st_membs;
240
241         for (i = 0; i < memb_size; i++) {
242                 assert(i < cts.top);
243                 cte = ct_stack(i);
244                 if (cte->name)
245                         strcpy(st_membs[i].name, cte->name);
246                 st_membs[i].id = ct_stack_ct(i)->ffi_cs_id;
247         }
248
249         id = cp_ctype_reg_csymbol(&nst);
250
251         ctype_stack_reset();
252
253         return id;
254 }
255
256 /* build pointer symbol from given csymbol */
257 int cp_symbol_build_pointer(struct cp_ctype *ct)
258 {
259         int id, ret;
260         csymbol ncspt;
261         csymbol *ref_cs = ct_ffi_cs(ct);
262
263         /* TODO: Check correctness of multi-level pointer 24.11.2013(unihorn) */
264         memset(&ncspt, 0, sizeof(csymbol));
265         ncspt.type = FFI_PTR;
266         ret = sprintf(ncspt.name, "%s *", csym_name(ref_cs));
267         assert(ret < MAX_TYPE_NAME_LEN);
268
269         csym_set_ptr_deref_id(&ncspt, ct->ffi_cs_id);
270         id = cp_ctype_reg_csymbol(&ncspt);
271
272         return id;
273 }
274
275 void __cp_symbol_dump_func(csymbol *cs)
276 {
277         int i;
278         csymbol *ncs;
279         csymbol_func *fcs = csym_func(cs);
280
281         printf("=== [%s] function definition =============\n", csym_name(cs));
282         ncs = cp_csymf_ret(fcs);
283         printf("address: %p\n", fcs->addr);
284         printf("return type: \n");
285         printf("\tcsym_id: %d, ffi_ctype: %d, %s\n",
286                         fcs->ret_id, ncs->type, csym_name(ncs));
287         printf("args type (%d): \n", fcs->arg_nr);
288         for (i = 0; i < csymf_arg_nr(fcs); i++) {
289             printf("\t (%d) ", i);
290             printf("csym_id: %d, ", fcs->arg_ids[i]);
291             ncs = cp_csymf_arg(fcs, i);
292             printf("ffi_ctype: %d, %s\n", ncs->type, csym_name(ncs));
293         }
294 }
295
296 void cp_symbol_dump_func(int id)
297 {
298         __cp_symbol_dump_func(&cs_arr[id]);
299 }
300
301 int cp_symbol_build_func(struct cp_ctype *type, const char *fname, int fn_size)
302 {
303         int i = 1, arg_nr, id;
304         int *argsym_id_arr;
305         csymbol nfcs;
306         csymbol_func *fcs;
307
308         if (cts.top == 0 || fn_size < 0 || !fname) {
309                 cp_error("invalid function definition.\n");
310         }
311
312         argsym_id_arr = NULL;
313         memset(&nfcs, 0, sizeof(csymbol));
314         csym_type(&nfcs) = FFI_FUNC;
315
316         strncpy(csym_name(&nfcs), fname, fn_size);
317
318         fcs = csym_func(&nfcs);
319         fcs->has_var_arg = type->has_var_arg;
320         /* Type needed for handling variable args handle */
321         if (fcs->has_var_arg && !ctype_lookup_type("void *"))
322                 cp_symbol_build_pointer(ctype_lookup_type("void"));
323
324         /* Fetch start address of function  */
325         fcs->addr = (void *)find_kernel_symbol(csym_name(&nfcs));
326         if (!fcs->addr)
327                 cp_error("wrong function address for %s\n", csym_name(&nfcs));
328
329         /* bottom of the stack is return type */
330         fcs->ret_id = ct_stack_ct(0)->ffi_cs_id;
331
332         /* the rest is argument type */
333         if (cts.top == 1) {
334                 /* function takes no argument */
335                 arg_nr = 0;
336         } else {
337                 arg_nr = cts.top - 1;
338                 argsym_id_arr = malloc(arg_nr * sizeof(int));
339                 if (!argsym_id_arr)
340                         cp_error("failed to allocate memory for function args.\n");
341                 for (i = 0; i < arg_nr; i++) {
342                         argsym_id_arr[i] = ct_stack_ct(i+1)->ffi_cs_id;
343                 }
344         }
345         fcs->arg_nr = arg_nr;
346         fcs->arg_ids = argsym_id_arr;
347
348         id = cp_ctype_reg_csymbol(&nfcs);
349
350         /* clear stack since we have consumed all the ctypes */
351         ctype_stack_reset();
352
353         return id;
354 }
355
356 struct cp_ctype *cp_ctype_reg_type(char *name, struct cp_ctype *ct)
357 {
358         if (cte_nr >= cte_arr_size)
359                 ctype_reg_table_grow();
360
361         memset(cte_arr[cte_nr].name, 0, MAX_TYPE_NAME_LEN);
362         strcpy(cte_arr[cte_nr].name, name);
363
364         cte_arr[cte_nr].ct = *ct;
365         cte_nr++;
366
367         return &(cte_arr[cte_nr-1].ct);
368 }
369
370 #if 0
371 /* TODO: used for size calculation */
372 static ffi_type ffi_int_type(ktap_state *ks, int size, bool sign)
373 {
374         switch(size) {
375         case 1:
376                 if (!sign)
377                         return FFI_UINT8;
378                 else
379                         return FFI_INT8;
380         case 2:
381                 if (!sign)
382                         return FFI_UINT16;
383                 else
384                         return FFI_INT16;
385         case 4:
386                 if (!sign)
387                         return FFI_UINT32;
388                 else
389                         return FFI_INT32;
390         case 8:
391                 if (!sign)
392                         return FFI_UINT64;
393                 else
394                         return FFI_INT64;
395         default:
396                 kp_error(ks, "Error: Have not support int type of size %d\n", size);
397                 return FFI_UNKNOWN;
398         }
399
400         /* NEVER reach here, silence compiler */
401         return -1;
402 }
403 #endif
404
405
406 static inline void ct_set_type(struct cp_ctype *ct, int type, int is_unsigned)
407 {
408         ct->type = type;
409         ct->is_unsigned = is_unsigned;
410 }
411
412 static void init_builtin_type(struct cp_ctype *ct, ffi_type ftype)
413 {
414         csymbol cs;
415         int cs_id;
416
417         csym_type(&cs) = ftype;
418         strncpy(csym_name(&cs), ffi_type_name(ftype), CSYM_NAME_MAX_LEN);
419         cs_id = cp_ctype_reg_csymbol(&cs);
420
421         memset(ct, 0, sizeof(*ct));
422         ct->ffi_cs_id = cs_id;
423         switch (ftype) {
424         case FFI_VOID:          ct_set_type(ct, VOID_TYPE, 0); break;
425         case FFI_UINT8:         ct_set_type(ct, INT8_TYPE, 1); break;
426         case FFI_INT8:          ct_set_type(ct, INT8_TYPE, 0); break;
427         case FFI_UINT16:        ct_set_type(ct, INT16_TYPE, 1); break;
428         case FFI_INT16:         ct_set_type(ct, INT16_TYPE, 0); break;
429         case FFI_UINT32:        ct_set_type(ct, INT32_TYPE, 1); break;
430         case FFI_INT32:         ct_set_type(ct, INT32_TYPE, 0); break;
431         case FFI_UINT64:        ct_set_type(ct, INT64_TYPE, 1); break;
432         case FFI_INT64:         ct_set_type(ct, INT64_TYPE, 0); break;
433         default:                break;
434         }
435         ct->base_size = ffi_type_size(ftype);
436         ct->align_mask = ffi_type_align(ftype) - 1;
437         ct->is_defined = 1;
438 }
439
440 /*
441  * lookup and register builtin C type on demand
442  * You should ensure that the type with name doesn't appear in
443  * csymbol table before calling.
444  */
445 struct cp_ctype *ctype_lookup_builtin_type(char *name)
446 {
447         struct cp_ctype ct;
448
449         if (!strncmp(name, "void", sizeof("void"))) {
450                 init_builtin_type(&ct, FFI_VOID);
451                 return cp_ctype_reg_type("void", &ct);
452         } else if (!strncmp(name, "int8_t", sizeof("int8_t"))) {
453                 init_builtin_type(&ct, FFI_INT8);
454                 return cp_ctype_reg_type("int8_t", &ct);
455         } else if (!strncmp(name, "uint8_t", sizeof("uint8_t"))) {
456                 init_builtin_type(&ct, FFI_UINT8);
457                 return cp_ctype_reg_type("uint8_t", &ct);
458         } else if (!strncmp(name, "int16_t", sizeof("int16_t"))) {
459                 init_builtin_type(&ct, FFI_INT16);
460                 return cp_ctype_reg_type("int16_t", &ct);
461         } else if (!strncmp(name, "uint16_t", sizeof("uint16_t"))) {
462                 init_builtin_type(&ct, FFI_UINT16);
463                 return cp_ctype_reg_type("uint16_t", &ct);
464         } else if (!strncmp(name, "int32_t", sizeof("int32_t"))) {
465                 init_builtin_type(&ct, FFI_INT32);
466                 return cp_ctype_reg_type("int32_t", &ct);
467         } else if (!strncmp(name, "uint32_t", sizeof("uint32_t"))) {
468                 init_builtin_type(&ct, FFI_UINT32);
469                 return cp_ctype_reg_type("uint32_t", &ct);
470         } else if (!strncmp(name, "int64_t", sizeof("int64_t"))) {
471                 init_builtin_type(&ct, FFI_INT64);
472                 return cp_ctype_reg_type("int64_t", &ct);
473         } else if (!strncmp(name, "uint64_t", sizeof("uint64_t"))) {
474                 init_builtin_type(&ct, FFI_UINT64);
475                 return cp_ctype_reg_type("uint64_t", &ct);
476         } else {
477                 /* no builtin type matched */
478                 return NULL;
479         }
480 }
481
482 /* start ctype reg table */
483 struct cp_ctype *ctype_lookup_type(char *name)
484 {
485         int i;
486         struct cp_ctype *ct;
487
488         for (i = 0; i < cte_nr; i++) {
489                 ct = &cte_arr[i].ct;
490                 if (!strcmp(name, cte_arr[i].name))
491                         return ct;
492         }
493
494         /* see if it's a builtin C type
495          * return NULL if still no match */
496         return ctype_lookup_builtin_type(name);
497 }
498
499 cp_csymbol_state *ctype_get_csym_state(void)
500 {
501         return &csym_state;
502 }
503
504 #define DEFAULT_STACK_SIZE 20
505 #define DEFAULT_SYM_ARR_SIZE 20
506 int cp_ctype_init()
507 {
508         cts.size = DEFAULT_STACK_SIZE;
509         cts.top = 0;
510         cts.stack = malloc(sizeof(cp_ctype_entry)*DEFAULT_STACK_SIZE);
511
512         cs_nr = 0;
513         cs_arr_size = DEFAULT_SYM_ARR_SIZE;
514         cs_arr = malloc(sizeof(csymbol)*DEFAULT_SYM_ARR_SIZE);
515         memset(cs_arr, 0, sizeof(csymbol)*DEFAULT_SYM_ARR_SIZE);
516
517         cte_nr = 0;
518         cte_arr_size = DEFAULT_CTYPE_ARR_SIZE;
519         cte_arr = malloc(sizeof(cp_ctype_entry)*DEFAULT_CTYPE_ARR_SIZE);
520
521         return 0;
522 }
523
524 int cp_ctype_free()
525 {
526         int i;
527         csymbol *cs;
528
529         if (cts.stack)
530                 free(cts.stack);
531
532         if (cs_arr) {
533                 for (i = 0; i < cs_nr; i++) {
534                         cs = &cs_arr[i];
535                         if (csym_type(cs) == FFI_FUNC) {
536                                 if (csym_func(cs)->arg_ids)
537                                         free(csym_func(cs)->arg_ids);
538                         } else if (csym_type(cs) == FFI_STRUCT) {
539                                 if (csym_struct(cs)->members)
540                                         free(csym_struct(cs)->members);
541                         }
542                 }
543                 free(cs_arr);
544         }
545
546         if (cte_arr) {
547                 free(cte_arr);
548         }
549
550         return 0;
551 }