1    /* 
     2     * Copyright (c) 2011, Texas Instruments Incorporated
     3     * All rights reserved.
     4     *
     5     * Redistribution and use in source and binary forms, with or without
     6     * modification, are permitted provided that the following conditions
     7     * are met:
     8     *
     9     * *  Redistributions of source code must retain the above copyright
    10     *    notice, this list of conditions and the following disclaimer.
    11     *
    12     * *  Redistributions in binary form must reproduce the above copyright
    13     *    notice, this list of conditions and the following disclaimer in the
    14     *    documentation and/or other materials provided with the distribution.
    15     *
    16     * *  Neither the name of Texas Instruments Incorporated nor the names of
    17     *    its contributors may be used to endorse or promote products derived
    18     *    from this software without specific prior written permission.
    19     *
    20     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    22     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    23     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    27     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    28     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    29     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    30     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31     * */
    32    /*
    33     *  ======== Exception.xdc ========
    34     *
    35     */
    36    
    37    package ti.sysbios.family.c64p;
    38    
    39    import xdc.rov.ViewInfo;
    40    import xdc.runtime.Diags;
    41    import xdc.runtime.Log;
    42    
    43    /*!
    44     *  ======== Exception ========
    45     *  Exception Module
    46     *
    47     *  The Exception module is a basic C64+ exception handler.  It is generally
    48     *  considered to be a program endpoint, since an exception usually
    49     *  indicates something fatal to the system.
    50     *
    51     *  During initialization, the Exception module sets TSR.GEE and TSR.XEN to
    52     *  enable the CPU to recognize the EXECP input.
    53     *
    54     *  @p(html)
    55     *  <h3> Calling Context </h3>
    56     *  <table border="1" cellpadding="3">
    57     *    <colgroup span="1"></colgroup> <colgroup span="5" align="center"></colgroup>
    58     *
    59     *    <tr><th> Function                 </th><th>  Hwi   </th><th>  Swi   </th><th>  Task  </th><th>  Main  </th><th>  Startup  </th></tr>
    60     *    <!--                                                                                                                 -->
    61     *    <tr><td> {@link #clearLastStatus}         </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
    62     *    <tr><td> {@link #evtEvtClear}             </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
    63     *    <tr><td> {@link #evtExpMaskEnable}        </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
    64     *    <tr><td> {@link #getLastStatus}           </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
    65     *    <tr><td> {@link #setReturnPtr}            </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
    66     *    <tr><td colspan="6"> Definitions: <br />
    67     *       <ul>
    68     *         <li> <b>Hwi</b>: API is callable from a Hwi thread. </li>
    69     *         <li> <b>Swi</b>: API is callable from a Swi thread. </li>
    70     *         <li> <b>Task</b>: API is callable from a Task thread. </li>
    71     *         <li> <b>Main</b>: API is callable during any of these phases: </li>
    72     *           <ul>
    73     *             <li> In your module startup after this module is started (e.g. Mod_Module_startupDone() returns TRUE). </li>
    74     *             <li> During xdc.runtime.Startup.lastFxns. </li>
    75     *             <li> During main().</li>
    76     *             <li> During BIOS.startupFxns.</li>
    77     *           </ul>
    78     *         <li> <b>Startup</b>: API is callable during any of these phases:</li>
    79     *           <ul>
    80     *             <li> During xdc.runtime.Startup.firstFxns.</li>
    81     *             <li> In your module startup before this module is started (e.g. Mod_Module_startupDone() returns FALSE).</li>
    82     *           </ul>
    83     *       </ul>
    84     *    </td></tr>
    85     *
    86     *  </table>
    87     *  @p
    88     */
    89    
    90    @ModuleStartup         /* generate a call to Exception at startup */
    91    
    92    module Exception
    93    {
    94    
    95        @Facet
    96        metaonly config ViewInfo.Instance rovViewInfo =
    97            ViewInfo.create({
    98                viewMap: [
    99                     ['Exception',
   100                        {
   101                            type: ViewInfo.TREE,
   102                            viewInitFxn: 'viewInitException',
   103                            structName: 'Context'
   104                        }
   105                    ]
   106                ]
   107            });
   108            
   109        // -------- Module Types --------
   110    
   111        /*! FuncPtr - Hook function type definition. */
   112        typedef Void (*FuncPtr)(void);
   113    
   114        /*! @_nodoc 
   115         *  Context - Register contents at the time of the exception.
   116         *  dispatch() creates a Context structure on the Hwi ISR stack and
   117         *  fills it before calling handler.  A pointer to this
   118         *  Context structure is returned by getLastStatus().
   119         */
   120        struct Context {
   121            Ptr ILC;        //! register ILC
   122            Ptr RILC;       //! register RILC
   123            Ptr AMR;        //! register AMR
   124            Ptr SSR;        //! register SSR
   125            Ptr IRP;        //! register IRP
   126            Ptr NRP;        //! register NRP
   127            Ptr ITSR;       //! register ITSR
   128            Ptr NTSR;       //! register NTSR
   129            Ptr EFR;        //! register EFR
   130            Ptr IERR;       //! register IERR
   131            Ptr B30;        //! general purpose register b30
   132            Ptr B31;        //! general purpose register b31
   133            Ptr B28;        //! general purpose register b28
   134            Ptr B29;        //! general purpose register b29
   135            Ptr B26;        //! general purpose register b26
   136            Ptr B27;        //! general purpose register b27
   137            Ptr B24;        //! general purpose register b24
   138            Ptr B25;        //! general purpose register b25
   139            Ptr B22;        //! general purpose register b22
   140            Ptr B23;        //! general purpose register b23
   141            Ptr B20;        //! general purpose register b20
   142            Ptr B21;        //! general purpose register b21
   143            Ptr B18;        //! general purpose register b18
   144            Ptr B19;        //! general purpose register b19
   145            Ptr B16;        //! general purpose register b16
   146            Ptr B17;        //! general purpose register b17
   147            Ptr B14;        //! general purpose register b14
   148            Ptr B15;        //! general purpose register b15
   149            Ptr B12;        //! general purpose register b12
   150            Ptr B13;        //! general purpose register b13
   151            Ptr B10;        //! general purpose register b10
   152            Ptr B11;        //! general purpose register b11
   153            Ptr B8;         //! general purpose register b8
   154            Ptr B9;         //! general purpose register b9
   155            Ptr B6;         //! general purpose register b6
   156            Ptr B7;         //! general purpose register b7
   157            Ptr B4;         //! general purpose register b4
   158            Ptr B5;         //! general purpose register b5
   159            Ptr B2;         //! general purpose register b2
   160            Ptr B3;         //! general purpose register b3
   161            Ptr B0;         //! general purpose register b0
   162            Ptr B1;         //! general purpose register b1
   163            Ptr A30;        //! general purpose register a30
   164            Ptr A31;        //! general purpose register a31
   165            Ptr A28;        //! general purpose register a28
   166            Ptr A29;        //! general purpose register a29
   167            Ptr A26;        //! general purpose register a26
   168            Ptr A27;        //! general purpose register a27
   169            Ptr A24;        //! general purpose register a24
   170            Ptr A25;        //! general purpose register a25
   171            Ptr A22;        //! general purpose register a22
   172            Ptr A23;        //! general purpose register a23
   173            Ptr A20;        //! general purpose register a20
   174            Ptr A21;        //! general purpose register a21
   175            Ptr A18;        //! general purpose register a18
   176            Ptr A19;        //! general purpose register a19
   177            Ptr A16;        //! general purpose register a16
   178            Ptr A17;        //! general purpose register a17
   179            Ptr A14;        //! general purpose register a14
   180            Ptr A15;        //! general purpose register a15
   181            Ptr A12;        //! general purpose register a12
   182            Ptr A13;        //! general purpose register a13
   183            Ptr A10;        //! general purpose register a10
   184            Ptr A11;        //! general purpose register a11
   185            Ptr A8;         //! general purpose register a8
   186            Ptr A9;         //! general purpose register a9
   187            Ptr A6;         //! general purpose register a6
   188            Ptr A7;         //! general purpose register a7
   189            Ptr A4;         //! general purpose register a4
   190            Ptr A5;         //! general purpose register a5
   191            Ptr A2;         //! general purpose register a2
   192            Ptr A3;         //! general purpose register a3
   193            Ptr A0;         //! general purpose register a0
   194            Ptr A1;         //! general purpose register a1
   195        };
   196    
   197        /*! Status - structure filled by getLastStatus(). */
   198        struct Status {
   199            Bits32  efr;            //! EFR register
   200            Bits32  nrp;            //! NRP register
   201            Bits32  ntsr;           //! NTSR register
   202            Bits32  ierr;           //! IERR register
   203            Context *excContext;    //! Context structure filled by last exception
   204        };
   205    
   206        // -------- Module Constants --------
   207    
   208        /*
   209         *  Bitmasks for C64+ Exception Flag Register (EFR).
   210         */
   211        const Bits32 EFRSXF = 0x00000001;   //! Software exception flag
   212        const Bits32 EFRIXF = 0x00000002;   //! Internal exception flag
   213        const Bits32 EFREXF = 0x40000000;   //! EXCEP flag
   214        const Bits32 EFRNXF = 0x80000000;   //! NMI exception flag
   215    
   216        /*
   217         *  Bitmasks for C64+ Exception Clear Register (ECR).
   218         */
   219        const Bits32 ECRSXF = EFRSXF;       //! Software exception flag
   220        const Bits32 ECRIXF = EFRIXF;       //! Internal exception flag
   221        const Bits32 ECREXF = EFREXF;       //! EXCEP flag
   222        const Bits32 ECRNXF = EFRNXF;       //! NMI exception flag
   223    
   224        /*
   225         *  Bitmasks for C64+ Internal Exception Report Register (IERR).
   226         */
   227        const Bits32 IERRIFX = 0x00000001;  //! Instruction fetch exception
   228        const Bits32 IERRFPX = 0x00000002;  //! Fetch packet exception
   229        const Bits32 IERREPX = 0x00000004;  //! Execute packet exception
   230        const Bits32 IERROPX = 0x00000008;  //! Opcode exception
   231        const Bits32 IERRRCX = 0x00000010;  //! Resource conflict exception
   232        const Bits32 IERRRAX = 0x00000020;  //! Resource access exeption
   233        const Bits32 IERRPRX = 0x00000040;  //! Priviledge exception
   234        const Bits32 IERRLBX = 0x00000080;  //! Loop buffer exception
   235        const Bits32 IERRMSX = 0x00000100;  //! Missed stall exception
   236    
   237        /*
   238         *  Exception-related bitmasks for C64+ Task State Register (TSR).
   239         */
   240        const Bits32 TSRGEE = 0x00000004;   //! Global exception enable
   241        const Bits32 TSRXEN = 0x00000008;   //! External exception enable
   242        const Bits32 TSREXC = 0x00000400;   //! Exception processing
   243        
   244        /* TSR Privilege Mode bits */
   245        const Bits32 TSRCXM   = 0x000000C0; //! Current execution mode bits
   246        const Bits32 TSRCXMSHIFT = 6;       //! TSR.CXM left shift value
   247        const Bits32 TSRCXMSV = 0x00000000; //! Supervisor mode
   248        const Bits32 TSRCXMUS = 0x00000040; //! User mode
   249    
   250        // -------- Module Parameters --------
   251    
   252        /*!
   253         *  enableExternalMPC - Enable handling of Memory Protection
   254         *  Controller (MPC) exceptions.
   255         */
   256        config Bool enableExternalMPC = false;
   257    
   258        /*!
   259         *  enablePrint - Enable print of exception details and Register values
   260         */
   261        config Bool enablePrint = true;
   262    
   263        /*!
   264         *  exceptionHook - Function hook called by handler
   265         *  This is called anytime an exception occurs.
   266         */
   267        config FuncPtr exceptionHook = null;
   268    
   269        /*!
   270         *  internalHook - Function hook called by internalHandler
   271         *  Function is only called when an internal exception has occurred.
   272         */
   273        config FuncPtr internalHook = null;
   274    
   275        /*!
   276         *  externalHook - Function hook called by externalHandler
   277         *  Function is only called when an external exception has occurred.
   278         */
   279        config FuncPtr externalHook = null;
   280    
   281        /*!
   282         *  nmiHook - Function hook called by nmiHandler
   283         *  Function is called for legacy NMI exceptions only
   284         */
   285        config FuncPtr nmiHook = null;
   286    
   287        /*! returnHook - Function hook called at the end of Exception_dispatch */
   288        config FuncPtr returnHook = null;
   289    
   290        /*!
   291         *  getLastStatus - Fills passed status structure with the Status
   292         *  fields that were recorded by the last invocation of
   293         *  dispatch(), handler() and internalHandler().
   294         *  The 'excContext' is valid only in the scope of sub-handler
   295         *  "Hook" functions, since the Context structure is stored on the
   296         *  Hwi ISR stack.
   297         */
   298        @DirectCall
   299        Void getLastStatus(Status *status);
   300    
   301        /*!
   302         *  clearLastStatus - Clears internal Status structure.
   303         */
   304        @DirectCall
   305        Void clearLastStatus();
   306    
   307        /*!
   308         *  setReturnPtr - Configures dispatch() to "return" (branch) to the
   309         *  passed ptr.
   310         */
   311        @DirectCall
   312        FuncPtr setReturnPtr(FuncPtr ptr);
   313    
   314        /*!
   315         *  evtEvtClear - Clear a C64+ event from the EVTFLAG register.
   316         */
   317        @DirectCall
   318        Void evtEvtClear(UInt event);
   319    
   320        /*!
   321         *  evtExpMaskEnable - Enable a C64+ event to generate an exception.
   322         */
   323        @DirectCall
   324        Void evtExpMaskEnable(UInt event);
   325    
   326        /*! @_nodoc 
   327         *  dispatch - The default low-level dispatcher, plugged into the
   328         *  C64+ NMI vector.
   329         */
   330        @DirectCall
   331        Void dispatch();
   332    
   333    internal:
   334    
   335        /* MPC CPU Access Memory Protecton Fault Event numbers */
   336        const UInt EVTPMCCMPA = 120;        /* PMC memory protection fault event */
   337        const UInt EVTDMCCMPA = 122;        /* DMC memory protection fault event */
   338        const UInt EVTUMCCMPA = 124;        /* UMC memory protection fault event */
   339        const UInt EVTEMCCMPA = 126;        /* EMC memory protection fault event */
   340    
   341        /*!
   342         *  handler - The high-level dispatcher, called by dispatch().
   343         *  Performs the following steps in order:
   344         *    a. records EFR/NRP/NTSR in a Status structure
   345         *    b. logs EFR/NRP/NTSR.CXM with text output to module's logger
   346         *    c. calls exceptionHook
   347         *    d. clears EFR
   348         *    e. calls into subhandlers
   349         *    f. aborts system
   350         */
   351        Void handler(Bool abortFlag);
   352    
   353        /*!
   354         *  internalHandler - Internal exception handler called by
   355         *  handler().  Performs the following steps in order:
   356         *    a. records IERR in a Status structure
   357         *    b. logs IERR with text output to module's logger
   358         *    c. calls internalHook
   359         *    d. clears IERR
   360         */
   361        Void internalHandler();
   362    
   363        /*!
   364         *  externalHandler - External exception handler called by
   365         *  handler().  Performs the following steps in order:
   366         *    a. logs text output to module's logger
   367         *    b. calls externalHook
   368         */
   369        Void externalHandler();
   370    
   371        /*!
   372         *  nmiHandler - Legacy NMI handler called by handler().
   373         *  Performs the following steps in order:
   374         *    a. logs text output to module's logger
   375         *    b. calls nmiHook
   376         */
   377        Void nmiHandler();
   378    
   379        struct Module_State {
   380            Bits32  efr;
   381            Bits32  nrp;
   382            Bits32  ntsr;
   383            Bits32  ierr;
   384            FuncPtr returnHook;
   385            Context *excContext;
   386            Char    scratch[16];    // sratch memory used by dispatch
   387            Char    *isrStack;      // for ROM - Points to isrStack address
   388        };
   389    
   390    }
   391    
   392    /*
   393     *  @(#) ti.sysbios.family.c64p; 2, 0, 0, 0,453; 1-20-2011 12:27:52; /db/vtree/library/trees/avala/avala-o26x/src/ xlibrary
   394    
   395     */
   396