source: svn/trunk/newcon3bcm2_21bu/nexus/build/tools/kernelproxy/bapi_common.pm

Last change on this file was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 31.8 KB
Line 
1#!/usr/bin/perl
2#     (c)2003-2011 Broadcom Corporation
3#
4#  This program is the proprietary software of Broadcom Corporation and/or its licensors,
5#  and may only be used, duplicated, modified or distributed pursuant to the terms and
6#  conditions of a separate, written license agreement executed between you and Broadcom
7#  (an "Authorized License").  Except as set forth in an Authorized License, Broadcom grants
8#  no license (express or implied), right to use, or waiver of any kind with respect to the
9#  Software, and Broadcom expressly reserves all rights in and to the Software and all
10#  intellectual property rights therein.  IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU
11#  HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY
12#  NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE.
13#
14#  Except as expressly set forth in the Authorized License,
15#
16#  1.     This program, including its structure, sequence and organization, constitutes the valuable trade
17#  secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof,
18#  and to use this information only in connection with your use of Broadcom integrated circuit products.
19#
20#  2.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
21#  AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR
22#  WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
23#  THE SOFTWARE.  BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES
24#  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE,
25#  LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION
26#  OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF
27#  USE OR PERFORMANCE OF THE SOFTWARE.
28#
29#  3.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS
30#  LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR
31#  EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR
32#  USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF
33#  THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT
34#  ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE
35#  LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF
36#  ANY LIMITED REMEDY.
37#
38# $brcm_Workfile: bapi_common.pm $
39# $brcm_Revision: 46 $
40# $brcm_Date: 12/7/11 10:41a $
41#
42# File Description:
43#
44# Revision History:
45#
46# $brcm_Log: /nexus/build/tools/kernelproxy/bapi_common.pm $
47#
48# 46   12/7/11 10:41a erickson
49# SW7420-2141: merge as much duplicated kernelmode proxy/usermode ipc
50#  perl code as possible
51#
52# 45   11/29/11 11:31a erickson
53# SW7420-2129: remove null_allowed=trusted attribute
54#
55# 44   11/7/11 3:59p bandrews
56# SW7231-446: merge to main
57#
58# SW7231-446/2   11/4/11 9:44p bandrews
59# SW7231-446: fix based on review
60#
61# SW7231-446/1   11/3/11 9:27p bandrews
62# SW7231-446: implemented
63#
64# 43   9/13/11 1:23p erickson
65# SW7420-1148: change client modes to unprotected/protected/untrusted
66#
67# 42   7/29/11 12:15p erickson
68# SW7420-1991: reorganize to allow ipcthunk/bapi_common.pm layout to be
69#  closer
70#
71# 41   7/28/11 3:28p erickson
72# SW7425-1029: fix NEXUS_CallbackDesc proxy for functions with enums, not
73#  handles
74#
75# 40   7/26/11 12:07p erickson
76# SW7420-1992: callback database must take client id so that callbacks
77#  can be disconnected when a client releases
78#
79# 39   7/26/11 9:38a erickson
80# SW7405-5438: fix warning
81#
82# 38   7/25/11 2:46p erickson
83# SW7420-1992: allow class with secondary_destructor and release, no
84#  destructor
85#
86# 37   7/21/11 7:06p jtna
87# SW7405-5438: convert address to offset correctly in multi-element array
88#
89# 36   7/12/11 1:26p erickson
90# SW7420-1979: add support for new shutdown attribute
91#
92# 35   5/13/11 12:17p erickson
93# SW7420-1873: remove shutdown attribute
94#
95# 34   5/3/11 5:08p erickson
96# SW7420-1819: repartition so NFE is a standard feature
97#
98# 33   4/14/11 12:01p erickson
99# SW7420-1679: clean up NEXUS_Memory_Allocate using
100#  attr{dealloc=NEXUS_Memory_Free}
101#
102# 32   3/17/11 11:59a erickson
103# SW7346-101: fix NEXUS_StopCallbacks for secondary destructors
104#  (specifically, NEXUS_Playpump_ClosePidChannel)
105#
106# 31   3/1/11 9:31a erickson
107# SW7420-1123: add skip_thunk to centralize test for filenames that
108#  should not be thunked
109#
110# 30   2/16/11 4:00p erickson
111# SW7420-1507: fix callback registration in Open functions
112#
113# 29   2/16/11 1:03p erickson
114# SW7420-1491: fix last check in. improve error checking.
115#
116# 28   2/15/11 2:16p erickson
117# SW7420-1491: consolidate to nelem and nelem_out
118#
119# 27   2/3/11 6:37p vsilyaev
120# SW7420-1441: Use local (non proxied) implementation for function that
121#  flush CPU cache
122#
123# 26   1/28/11 9:04a erickson
124# SW7420-1440: refactor secondary_destructor
125#
126# 25   1/27/11 3:05p erickson
127# SW7208-152: fix and standardize get_destructor_hash
128#
129# 24   1/19/11 3:42p erickson
130# SW7420-1123: refactor kernelproxy to correspond to usermode changes
131#
132# 23   12/7/10 5:47p erickson
133# SW7420-1148: refactor kernel mode proxy for process isolation
134#
135# 22   10/14/10 12:34p erickson
136# SW7420-1148: add secondary_destructor to support
137#  NEXUS_Playpump_ClosePidChannel, verify function attr in one place,
138#  support multiple param verifications per ioctl
139#
140# 21   10/12/10 4:29p erickson
141# SW7420-1148: refactor kernel mode proxy for process isolation
142#
143# 20   6/10/10 4:56p vsilyaev
144# SW7405-4011: Added StopCallbacks into the driver ioctl code
145#
146# 19   3/19/10 10:07a erickson
147# SW7405-3832: rename variable to avoid conflict
148#
149# 18   8/4/08 4:50p vsilyaev
150# PR 45397: Added support to handle address remapping in variable size
151#  structure, this fixes dma example
152#
153# 17   6/10/08 3:29p vsilyaev
154# PR 43184: Added support for Start/Stop callbacks in linux kernel/user
155#  proxy mode
156#
157# 16   5/28/08 10:50a vsilyaev
158# PR 42158: Added hot-fix for variable size complex function arguments
159#
160# 15   2/6/08 4:50p vsilyaev
161# PR 38682: Fixed typo
162#
163# 14   2/6/08 4:37p vsilyaev
164# PR 38682: Added attributes to set array size for 'void *' type
165#
166# 13   2/4/08 4:24p vsilyaev
167# PR 38682: Added more tags for the linux kernel/user proxy mode
168#
169# 12   2/4/08 3:31p vsilyaev
170# PR 38682: Added more cases in  support of variable size arrays
171#
172# 11   2/4/08 2:39p vsilyaev
173# PR 38682: Added support for pointers with variable size
174#
175# 10   2/1/08 5:02p vsilyaev
176# PR 38682: Added tracking of open/close calls
177#
178# 9   1/31/08 5:02p vsilyaev
179# PR 38682: Added function level attributes
180#
181# 8   1/30/08 6:18p vsilyaev
182# PR 38682: Added support for callbacks that are passed directly as
183#  function arguments
184#
185# 7   1/30/08 4:28p vsilyaev
186# PR 38682: Improved handling of callbacks traversing user/kernel
187#  boundary
188#
189# 6   1/30/08 11:49a vsilyaev
190# PR 38682: Added routing of callbacks into the user space
191#
192# 5   1/29/08 5:55p vsilyaev
193# PR 38682: Support of callbacks in the linux kernel/user proxy
194#  environment
195#
196# 4   1/28/08 12:51p vsilyaev
197# PR 38682: Fixed redundant mapping of addreses inside structures
198#
199# 3   1/28/08 12:33p vsilyaev
200# PR 38682: Added support for attribute tags applied to structure
201#  members.
202#
203# 2   1/25/08 2:35p vsilyaev
204# PR 38682: Added handling of 'attr' to the function arguments
205#
206# 1   1/18/08 2:15p jgarrett
207# PR 38808: Merging to main branch
208#
209# Nexus_Devel/2   1/18/08 1:13p vsilyaev
210# PR 38682: Updated nexus kernel environment
211#
212#############################################################################
213use strict;
214use bapi_util;
215
216package bapi_common;
217
218sub version_value
219{
220    my $version = shift;
221    sprintf " 0x%08Xul ", ($version & 0xFFFFFFFF);
222}
223
224sub version_ioctl
225{
226    my $module = shift;
227    return "IOCTL_${module}_NEXUS_INIT";
228}
229
230sub version_define
231{
232    my $module = shift;
233    return "NEXUS_${module}_MODULE_VERSION";
234}
235
236sub ioctl_name
237{
238    my $module = shift;
239    my $func = shift;
240
241    return "IOCTL_${module}_$func->{FUNCNAME}";
242}
243
244sub ioctl_struct
245{
246    my $module = shift;
247    my $func = shift;
248    return "${module}_$func->{FUNCNAME}_data";
249}
250
251sub ioctl_header
252{
253    my $module = shift;
254    my $module_lc = lc $module;
255
256    return "nexus_${module_lc}_ioctl.h";
257}
258
259sub process_function_attributes {
260    my $func = shift;
261    my $structs = shift;
262    my $funcs = shift;
263    my $params = $func->{PARAMS};
264    my @proxy_post_success;
265    my @proxy_pre_call;
266    my @driver_pre_call;
267    my @driver_post_success;
268    my @driver_post_error;
269    my @driver_post_always;
270    my @proxy_vars;
271    my @driver_ioctl_reserved;
272    my @driver_ioctl_data;
273    my %driver_overwrite;
274    my ($attr,$value);
275    my $param;
276    my $field;
277    my $arg_no=0;
278    my $driver_arg = "module->args.$func->{FUNCNAME}";
279
280    if(exists $func->{ATTR}) {
281        while (($attr, $value) = each %{$func->{ATTR}} ) {
282            next if($attr eq 'destructor');
283            next if($attr eq 'secondary_destructor');
284            next if($attr eq 'release');
285            next if($attr eq 'local');
286            next if($attr eq 'dealloc');
287            next if($attr eq 'shutdown');
288            if($attr eq 'chain') {
289                my @args;
290                for $param (@$params) {
291                    push @args, $param->{NAME};
292                }
293                push @proxy_post_success, "/* call local proxy function */";
294                push @proxy_post_success, ($value . " ( " . (join( ' , ', @args)) . ' );');
295
296                @args=();
297                for $param (@$params) {
298                    push @args, "$driver_arg.ioctl." . $param->{NAME};
299                }
300                push @driver_post_success, "/* call local driver function */";
301                push @driver_post_success, ($value . " ( " . (join( ' , ', @args)) . ' );');
302                next;
303            }
304            print STDERR "ERROR: Unsupported attribute $attr = $value for $func->{FUNCNAME}\n";
305        }
306    }
307
308    for $param (@$params) {
309        my $name = $param->{NAME};
310        my $name_copy = "$name\_copy";
311        $arg_no ++;
312       
313        if (!exists $param->{ATTR}->{'nelem'} &&
314            !exists $param->{ATTR}->{'fixed_length'})
315        {
316            # ordinary params
317            if ($param->{ISREF} && $param->{BASETYPE} ne 'void') {
318                if ($param->{INPARAM}) {
319                    push @driver_pre_call, "if ($driver_arg.ioctl.$param->{NAME}) {";
320                    push @driver_pre_call, "  if (copy_from_user_small(&$driver_arg.$name, $driver_arg.ioctl.$name, sizeof($driver_arg.$name))!=0) {BERR_TRACE(-1);goto err_fault;}";
321                    push @driver_pre_call, "}";
322                    my $null_allowed = $param->{ATTR}->{'null_allowed'};
323                    if (!defined $null_allowed) {
324                        push @driver_pre_call, "else {";
325                        push @driver_pre_call, "  BDBG_ERR((\"NULL not allowed for $name param in $func->{FUNCNAME}: %d\", __LINE__));";
326                        push @driver_pre_call, "  goto err_fault;";
327                        push @driver_pre_call, "}";
328                    }
329                } 
330                else {
331                    push @driver_pre_call, "NEXUS_IOCTL_CLEAR($driver_arg.ioctl.$name);";
332                }
333            }
334        }
335       
336        # must exchange eInvalid and NULL for protected clients for Timebase and ParserBand as direct params
337        # this code only handles output param conversion post call
338        # pre-call, post-verification NULL->eInvalid input conversion is done in bapi_driver_ioctl.pm
339        if ($param->{ISREF})
340        {
341            if ($param->{BASETYPE} eq 'NEXUS_Timebase' || $param->{BASETYPE} eq 'NEXUS_ParserBand')
342            {
343                if (!$param->{INPARAM}) 
344                {
345                    push @driver_post_success, "if (nexus_driver_client_id(context)->config.mode != NEXUS_ClientMode_eUnprotected) {";
346                    push @driver_post_success, "  if ($driver_arg.ioctl.$name != NULL) {"; # check pointer
347                    push @driver_post_success, "    if (*$driver_arg.ioctl.$name == $param->{TYPE}_eInvalid) {"; # check pointed to value
348                    push @driver_post_success, "      *$driver_arg.ioctl.$name = ($param->{TYPE})NULL;";
349                    push @driver_post_success, "    }";
350                    push @driver_post_success, "  }";
351                    push @driver_post_success, "}";
352                }
353            }
354        }
355           
356        if($param->{BASETYPE} eq 'NEXUS_CallbackDesc') {
357            if ($param->{INPARAM}) {
358                my $handle = "$driver_arg.ioctl.$$params[0]->{NAME}";
359                my $id = sprintf("0x%05x", ((bapi_util::func_id $funcs, $func)*256 + $arg_no + 0x10000));
360                my $notnull_test = "$driver_arg.ioctl.$name !=  NULL";
361                push @driver_pre_call, "if($notnull_test) {";
362                push @driver_pre_call, "   /* MAP callback from proxy space to the driver space */";
363                push @driver_pre_call, "   NEXUS_DRIVER_CALLBACK_TO_DRIVER(&$driver_arg.$name, $handle, $id);";
364                push @driver_pre_call, "}";
365                push @driver_post_success, "if($notnull_test) {";
366                push @driver_post_success, "    /* after function succeded, commit changes */";
367                push @driver_post_success, "    NEXUS_DRIVER_CALLBACK_TO_DRIVER_COMMIT(&$driver_arg.$name, $handle, $id);";
368                push @driver_post_success, "}";
369                push @driver_post_error, "if($notnull_test) {";
370                push @driver_post_error, "    /* if error occured, then cancel changes in the callback mapping */";
371                push @driver_post_error, "    NEXUS_DRIVER_CALLBACK_TO_DRIVER_CANCEL(&$driver_arg.$name, $handle, $id);";
372                push @driver_post_error, "}";
373            } else {
374                print STDERR "ERROR: pointer to NEXUS_CallbackDesc could be only input parameter\n";
375            }
376        }
377
378        if (exists $structs->{$param->{BASETYPE}}) {
379            my $field_no = 0;
380            my @pre_update_proxy;
381            my @pre_update_driver;
382            my @post_update_proxy;
383            my @post_update_driver;
384            my @post_error_driver;
385
386#            bapi_parse_c::print_struct $structs->{$param->{BASETYPE}}
387            for $field (@{$structs->{$param->{BASETYPE}}}) {
388                my $field_driver = "$driver_arg.$name.$field->{NAME}";
389
390                $field_no ++;
391                if($field->{TYPE} eq 'NEXUS_CallbackDesc') {
392                    my $id = sprintf("0x%04x", ((bapi_util::struct_id $structs, $param->{BASETYPE})*256 + $field_no));
393                    my $handle;
394                    if ($func->{RETTYPE_ISHANDLE}) {
395                        $handle = 'NULL'; # update after success
396                    }
397                    elsif ($$params[0]->{ISHANDLE}) {
398                        $handle = "$driver_arg.ioctl.$$params[0]->{NAME}";
399                    }
400                    elsif (!$$params[0]->{ISREF} && $param != $$params[0]) {
401                        # enum instead of handle.
402                        # use enum type + enum value as psuedo handle
403                        # hardcoded for a max of 128 enum values per type
404                        # value<<2+1 to use non-4 byte aligned value to ensure no conflict with actual handle
405                        $handle = "(($id<<9) + ($driver_arg.ioctl.$$params[0]->{NAME}<<2) + 1)";
406                    }
407                    if (defined $handle && $param->{ISREF}) {
408                        if ($param->{INPARAM}) {
409                            push @pre_update_driver, "/* MAP callback from proxy space to the driver space */";
410                            push @pre_update_driver, "NEXUS_DRIVER_CALLBACK_TO_DRIVER(&$field_driver, $handle, $id);";
411                            if($handle eq 'NULL') {
412                                push @post_update_driver, "/* since callback wasn't avaliable before calling function, use the return result to update callback */";
413                                push @post_update_driver, "NEXUS_DRIVER_CALLBACK_UPDATE(&$field_driver, $handle, $id, $driver_arg.ioctl.__retval);";
414                            } else {
415                                push @post_update_driver, "/* after function succeded, commit changes */";
416                                push @post_update_driver, "NEXUS_DRIVER_CALLBACK_TO_DRIVER_COMMIT(&$field_driver, $handle, $id);";
417                            }
418                            push @post_error_driver, "/* if error occured, then cancel changes in the callback mapping */";
419                            push @post_error_driver, "NEXUS_DRIVER_CALLBACK_TO_DRIVER_CANCEL(&$field_driver, $handle, $id);";
420                        } else {
421                            push @post_update_driver, "/* MAP callback from the driver space to proxy space */";
422                            push @post_update_driver, "NEXUS_DRIVER_CALLBACK_TO_USER(&$field_driver, $handle, $id);";
423                        }
424                    }
425                }
426                # must change eInvalid back to NULL for protected clients for Timebase and ParserBand as struct members
427                # this code only handles output param conversion post call
428                # pre-call, post-verification NULL->eInvalid input conversion is done in bapi_driver_ioctl.pm
429                # TODO: is field as pointer supported?
430                elsif ($field->{TYPE} eq 'NEXUS_Timebase' || $field->{TYPE} eq 'NEXUS_ParserBand')
431                {
432                    if (!$param->{INPARAM})
433                    {
434                        push @driver_post_success, "if (nexus_driver_client_id(context)->config.mode != NEXUS_ClientMode_eUnprotected) {";
435                        push @driver_post_success, "  if ($field_driver == $field->{TYPE}_eInvalid) {";
436                        push @driver_post_success, "    $field_driver = ($field->{TYPE})NULL;";
437                        push @driver_post_success, "  }";
438                        push @driver_post_success, "}";
439                    }
440                }
441
442                if (exists $field->{ATTR}) {
443#                   print " $func->{FUNCNAME} :";
444#                   print "  field: $field->{TYPE} $field->{NAME} -> ";
445#                   bapi_parse_c::print_attr $field->{ATTR};
446                    while (($attr, $value) = each %{$field->{ATTR}} ) {
447                        my $field_proxy = "$name->$field->{NAME}";
448                        my $field_copy = "$name_copy.$field->{NAME}";
449                        if($attr eq 'memory') {
450                            if($value eq 'cached') {
451                                if ($param->{ISREF}) {
452                                    if ($param->{INPARAM}) {
453                                        next if (exists $param->{ATTR}->{'nelem'});
454                                        push @pre_update_proxy, "/* convert address to the device offset */";
455                                        push @pre_update_proxy, "$field_copy = $field_copy ? (void *) NEXUS_AddrToOffset($field_copy) : $field_copy;";
456
457                                        push @pre_update_driver, "/* convert offset (substituted by the proxy ) to the cached address */";
458                                        push @pre_update_driver, "NEXUS_DRIVER_RECV_ADDR($field_driver,cached);";
459                                        next;
460                                    } else {
461                                        push @post_update_proxy, "/* convert offset (substituted by the driver) to the cached address */";
462                                        push @post_update_proxy, "$field_proxy = $field_proxy ? NEXUS_OffsetToCachedAddr((uint32_t)$field_proxy) : $field_proxy ;";
463
464                                        push @post_update_driver, "/* convert offset (substituted by the proxy) to the cached address */";
465                                        push @post_update_driver, "NEXUS_DRIVER_SEND_ADDR($field_driver,cached);";
466                                        next;
467                                    }
468                                }
469                            }
470                        }
471                        print STDERR "ERROR: Unsupported attribute $attr = $value for $field->{NAME} in $param->{BASETYPE}\n";
472                    }
473                }
474            }
475            if(scalar @pre_update_proxy) {
476                push @proxy_vars, "$param->{BASETYPE} $name_copy; /* placeholder for modified parameters */ ";
477                push @proxy_vars, "const $param->{BASETYPE} *$name\_old; /* copy of old pointer */ ";
478                push @proxy_pre_call, "BSTD_UNUSED($name\_old);\n";
479                push @proxy_pre_call, "if($name) {";
480                push @proxy_pre_call, "     /* replace the original pointer with local copy */";
481                push @proxy_pre_call, "     $name\_old=$name;$name\_copy=*$name;$name=&$name\_copy;";
482                push @proxy_pre_call, "     /* update values ... */";
483                bapi_util::append_code \@proxy_pre_call, \@pre_update_proxy, "     ";
484                push @proxy_pre_call, "}";
485            }
486            bapi_util::append_if \@driver_pre_call, "$driver_arg.ioctl.$name", \@pre_update_driver;
487            bapi_util::append_if \@driver_post_error, "$driver_arg.ioctl.$name", \@post_error_driver;
488            bapi_util::append_if \@driver_post_success, "$driver_arg.ioctl.$name", \@post_update_driver;
489            bapi_util::append_if \@proxy_post_success , "$name", \@post_update_proxy;
490        }
491        if(exists $param->{ATTR}) {
492            while (($attr, $value) = each %{$param->{ATTR}} ) {
493                if($attr eq 'nelem_convert' || $attr eq 'nelem_out') {
494                    if( !exists $param->{ATTR}->{'nelem'}) {
495                        print STDERR "ERROR: $attr requires nelem: $func->{FUNCNAME}\n";
496                    }
497                    next; # handled with nelem
498                }
499                next if($attr eq 'dealloc'); # skip here
500                next if($attr eq 'null_allowed'); # skip here
501
502                if($attr eq 'reserved') {
503                    if($param->{BASETYPE} ne 'void') {
504                        push @driver_ioctl_reserved, "$param->{BASETYPE} $name\[$value];";
505                    } else {
506                        push @driver_ioctl_reserved, "uint8_t $name\[$value];";
507                    }
508                }
509                elsif($attr eq 'fixed_length' && $param->{ISREF}) {
510                    my $length = $value;
511                    push @driver_ioctl_reserved, "uint8_t $name\[$value];";
512                    push @driver_ioctl_data, "void * $name;";
513                    push @driver_pre_call, "$driver_arg.$name=$driver_arg.ioctl.$name?$driver_arg.reserved.$name:NULL;";
514                    if($param->{INPARAM}) {
515                        push @driver_pre_call, "if (copy_from_user_small($driver_arg.$name, $driver_arg.ioctl.$name, $value) !=0) {NEXUS_IOCTL_FAULT($func->{FUNCNAME}, $name);goto err_fault;}";
516                    }  else {
517                        push @driver_post_success, "if ($driver_arg.ioctl.$name!=NULL && copy_to_user_small($driver_arg.ioctl.$name, $driver_arg.$name, $value) !=0) {NEXUS_IOCTL_FAULT($func->{FUNCNAME}, $name);goto err_fault;}";
518                    }
519                    $driver_overwrite{$name} = 1;
520                }
521                elsif($attr eq 'nelem' && $param->{ISREF}) {
522                    my $length = "$driver_arg.ioctl.$value";
523                    my $scale = "";
524                    if( exists $param->{ATTR}->{'nelem_convert'}) {
525                        $length =  $param->{ATTR}->{'nelem_convert'} . "($length)";
526                    }
527                    if($param->{BASETYPE} ne 'void') {
528                        push @driver_ioctl_data, "$param->{BASETYPE} * $name;";
529                        $scale = "sizeof(*$driver_arg.ioctl.$name) *";
530                    } else {
531                        push @driver_ioctl_data, "void * $name;";
532                    }
533                    $length = "($scale $length)";
534                    push @driver_pre_call, "if($driver_arg.ioctl.$name!=NULL) {";
535                    push @driver_pre_call, "    /* allocate space for the $name */";
536                    push @driver_post_always, "/* free space allocated for $name */";
537                    if( exists $param->{ATTR}->{'reserved'}) {
538                        push @driver_pre_call, "    if( $length <= $scale $param->{ATTR}->{'reserved'} ) {";
539                        push @driver_pre_call, "        $driver_arg.$name = $driver_arg.reserved.$name;";
540                        push @driver_pre_call, "    } else {";
541                        push @driver_pre_call, "        $driver_arg.$name = NEXUS_DRIVER_ALLOC($length);";
542                        push @driver_pre_call, "        if($driver_arg.$name == NULL) {goto err_alloc;}";
543                        push @driver_pre_call, "    }";
544
545                        push @driver_post_always, "if($driver_arg.$name != $driver_arg.reserved.$name) {";
546                        push @driver_post_always, "    NEXUS_DRIVER_FREE($driver_arg.$name);";
547                        push @driver_post_always, "}";
548
549                    } else {
550                        push @driver_pre_call, "    $driver_arg.$name = NEXUS_DRIVER_ALLOC($length);";
551                        push @driver_post_always, "NEXUS_DRIVER_FREE($driver_arg.$name);";
552                    }
553                    if($param->{INPARAM}) {
554                        push @driver_pre_call, "    if (copy_from_user_small($driver_arg.$name, $driver_arg.ioctl.$name, $length) !=0) {NEXUS_IOCTL_FAULT($func->{FUNCNAME}, $name);goto err_fault;}";
555                        # Handle address translation
556                        my @driver_update_structure;
557                        my @proxy_update_structure;
558                        if (exists $structs->{$param->{BASETYPE}}) {
559                            my $field;
560                            for $field (@{$structs->{$param->{BASETYPE}}}) {
561                                if (exists $field->{ATTR}) {
562                                    my ($attr,$value);
563                                    while (($attr, $value) = each %{$field->{ATTR}} ) {
564                                        if($attr eq 'memory' && $value eq 'cached' && $param->{ISREF} && $param->{INPARAM}) {
565                                            my $field_copy = "$name_copy->$field->{NAME}";
566
567                                            push @proxy_update_structure, "/* convert address to the device offset */";
568                                            push @proxy_update_structure, "$field_copy = $field_copy ? (void *) NEXUS_AddrToOffset($field_copy) : $field_copy;";
569
570                                            push @driver_update_structure, "/* convert offset (substituted by the proxy) to the cached address */";
571                                            push @driver_update_structure, "NEXUS_DRIVER_RECV_ADDR($driver_arg.$name [i].$field->{NAME},cached);";
572                                            next;
573                                        }
574                                        print STDERR "ERROR: Unsupported attribute $attr = $value for $field->{NAME} in $param->{BASETYPE}\n";
575                                    }
576                                }
577                            }
578                        }
579                        if (scalar @driver_update_structure) {
580                            push @driver_pre_call, "    {unsigned i;for(i=0;i<$driver_arg.ioctl.$value;i++) {";
581                            bapi_util::append_code \@driver_pre_call, \@driver_update_structure, "        ";
582                            push @driver_pre_call, "    }}";
583                        }
584                        if (scalar @proxy_update_structure) {
585                            # Approximate handling
586                            push @proxy_pre_call, "    unsigned i;";
587                            push @proxy_pre_call, "    $param->{BASETYPE} * $name_copy = __builtin_alloca(sizeof($param->{BASETYPE}) * $value);";
588                            push @proxy_pre_call, "    if($name!=NULL) {";
589                            push @proxy_pre_call, "        void *_save_data = $name_copy;";
590                            push @proxy_pre_call, "        BKNI_Memcpy($name_copy, $name, sizeof($param->{BASETYPE}) * $value);";
591                            push @proxy_pre_call, "        for(i=0;i<$value;i++) {";
592                            bapi_util::append_code \@proxy_pre_call, \@proxy_update_structure, "            ";
593                            push @proxy_pre_call, "            $name_copy++;";
594                            push @proxy_pre_call, "        }";
595                            push @proxy_pre_call, "        $name = _save_data;";
596                            push @proxy_pre_call, "    }";
597                        }
598                    }
599                    else {
600                        my $length_out = $length;
601
602                        # nelem_out is an optimization for out params. only copy the data actually populated.
603                        if( exists $param->{ATTR}->{'nelem_out'}) {
604                            $length_out = "$driver_arg.$param->{ATTR}->{'nelem_out'}";
605                            if( exists $param->{ATTR}->{'nelem_convert'}) {
606                                $length_out =  $param->{ATTR}->{'nelem_convert'} . "($length_out)";
607                            }
608                            $length_out = "($length_out * sizeof(*$driver_arg.ioctl.$name))";
609                        }
610                        push @driver_post_success, "if ($driver_arg.ioctl.$name!=NULL && copy_to_user_small($driver_arg.ioctl.$name, $driver_arg.$name, $length_out ) !=0) {NEXUS_IOCTL_FAULT($func->{FUNCNAME}, $name);goto err_fault;}";
611                    }
612                    push @driver_pre_call, "} else {";
613                    push @driver_pre_call, "    $driver_arg.$name = NULL;";
614                    push @driver_pre_call, "}";
615                    $driver_overwrite{$name} = 1;
616                }
617                elsif($attr eq 'memory' && $value eq 'cached') {
618                    if ($param->{INPARAM}) {
619                        push @proxy_pre_call, "/* convert offset (substituted by the driver) to the cached address */";
620                        push @proxy_pre_call, "$name = $name ? (void *)NEXUS_AddrToOffset($name) : $name;";
621                        push @driver_pre_call, "/* convert address to the device offset */";
622                        push @driver_pre_call, "NEXUS_DRIVER_RECV_ADDR($driver_arg.ioctl.$name, cached);";
623                    }
624                    else {
625                        # handle return value. if client passes NULL outparam, only the client crashes
626                        push @proxy_post_success, "/* convert offset (substituted by the driver) to the cached address */";
627                        push @proxy_post_success, "*$name = *$name ? NEXUS_OffsetToCachedAddr((uint32_t)*$name) : *$name;";
628                        push @driver_post_success, "/* convert address to the device offset */";
629                        push @driver_post_success, "NEXUS_DRIVER_SEND_ADDR($driver_arg.$name, cached);";
630                    }
631                }
632                else {
633                    print STDERR "ERROR: Unsupported attribute $attr = $value for $param->{NAME} in $func->{FUNCNAME}\n";
634                }
635            }
636        }
637    }
638   
639    my %result;
640    if (scalar @proxy_post_success) {
641        $result{'proxy_post_success'} = \@proxy_post_success;
642    }
643    if (scalar @driver_post_success) {
644        $result{'driver_post_success'} = \@driver_post_success;
645    }
646    if (scalar @proxy_pre_call) {
647        $result{'proxy_pre_call'} = \@proxy_pre_call;
648    }
649    if (scalar @driver_pre_call) {
650        $result{'driver_pre_call'} = \@driver_pre_call;
651    }
652    if (scalar @proxy_vars) {
653        $result{'proxy_vars'} = \@proxy_vars;
654    }
655    if (scalar @driver_post_error) {
656        $result{'driver_post_error'} = \@driver_post_error;
657    }
658    if (scalar @driver_post_always) {
659        $result{'driver_post_always'} = \@driver_post_always;
660    }
661
662    if(scalar @driver_ioctl_reserved) {
663        push @driver_ioctl_data, "struct {";
664        bapi_util::append_code \@driver_ioctl_data, \@driver_ioctl_reserved, "    ";
665        push @driver_ioctl_data, "} reserved;";
666    }
667    if(scalar @driver_ioctl_data) {
668        $result{'driver_ioctl_data'} = \@driver_ioctl_data;
669    }
670    $result{'driver_overwrite'} = \%driver_overwrite;
671    return \%result;
672}
673
674
6751;
676
Note: See TracBrowser for help on using the repository browser.