<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- permission is obtained from the copyright holder. -->
|
<!-- permission is obtained from the copyright holder. -->
|
<HTML
|
<HTML
|
><HEAD
|
><HEAD
|
><TITLE
|
><TITLE
|
>Exception handling</TITLE
|
>Exception handling</TITLE
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
<META
|
<META
|
NAME="GENERATOR"
|
NAME="GENERATOR"
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
"><LINK
|
"><LINK
|
REL="HOME"
|
REL="HOME"
|
TITLE="eCos Reference Manual"
|
TITLE="eCos Reference Manual"
|
HREF="ecos-ref.html"><LINK
|
HREF="ecos-ref.html"><LINK
|
REL="UP"
|
REL="UP"
|
TITLE="The eCos Kernel"
|
TITLE="The eCos Kernel"
|
HREF="kernel.html"><LINK
|
HREF="kernel.html"><LINK
|
REL="PREVIOUS"
|
REL="PREVIOUS"
|
TITLE="Thread destructors"
|
TITLE="Thread destructors"
|
HREF="kernel-thread-destructors.html"><LINK
|
HREF="kernel-thread-destructors.html"><LINK
|
REL="NEXT"
|
REL="NEXT"
|
TITLE="Counters"
|
TITLE="Counters"
|
HREF="kernel-counters.html"></HEAD
|
HREF="kernel-counters.html"></HEAD
|
><BODY
|
><BODY
|
CLASS="REFENTRY"
|
CLASS="REFENTRY"
|
BGCOLOR="#FFFFFF"
|
BGCOLOR="#FFFFFF"
|
TEXT="#000000"
|
TEXT="#000000"
|
LINK="#0000FF"
|
LINK="#0000FF"
|
VLINK="#840084"
|
VLINK="#840084"
|
ALINK="#0000FF"
|
ALINK="#0000FF"
|
><DIV
|
><DIV
|
CLASS="NAVHEADER"
|
CLASS="NAVHEADER"
|
><TABLE
|
><TABLE
|
SUMMARY="Header navigation table"
|
SUMMARY="Header navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TH
|
><TH
|
COLSPAN="3"
|
COLSPAN="3"
|
ALIGN="center"
|
ALIGN="center"
|
>eCos Reference Manual</TH
|
>eCos Reference Manual</TH
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="kernel-thread-destructors.html"
|
HREF="kernel-thread-destructors.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="80%"
|
WIDTH="80%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="kernel-counters.html"
|
HREF="kernel-counters.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"></DIV
|
WIDTH="100%"></DIV
|
><H1
|
><H1
|
><A
|
><A
|
NAME="KERNEL-EXCEPTIONS">Exception handling</H1
|
NAME="KERNEL-EXCEPTIONS">Exception handling</H1
|
><DIV
|
><DIV
|
CLASS="REFNAMEDIV"
|
CLASS="REFNAMEDIV"
|
><A
|
><A
|
NAME="AEN782"
|
NAME="AEN782"
|
></A
|
></A
|
><H2
|
><H2
|
>Name</H2
|
>Name</H2
|
>cyg_exception_set_handler, cyg_exception_clear_handler, cyg_exception_call_handler -- Handle processor exceptions</DIV
|
>cyg_exception_set_handler, cyg_exception_clear_handler, cyg_exception_call_handler -- Handle processor exceptions</DIV
|
><DIV
|
><DIV
|
CLASS="REFSYNOPSISDIV"
|
CLASS="REFSYNOPSISDIV"
|
><A
|
><A
|
NAME="AEN787"><H2
|
NAME="AEN787"><H2
|
>Synopsis</H2
|
>Synopsis</H2
|
><DIV
|
><DIV
|
CLASS="FUNCSYNOPSIS"
|
CLASS="FUNCSYNOPSIS"
|
><A
|
><A
|
NAME="AEN788"><P
|
NAME="AEN788"><P
|
></P
|
></P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="FUNCSYNOPSISINFO"
|
CLASS="FUNCSYNOPSISINFO"
|
>#include <cyg/kernel/kapi.h>
|
>#include <cyg/kernel/kapi.h>
|
</PRE
|
</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_exception_set_handler</CODE
|
>void cyg_exception_set_handler</CODE
|
>(cyg_code_t exception_number, cyg_exception_handler_t* new_handler, cyg_addrword_t new_data, cyg_exception_handler_t** old_handler, cyg_addrword_t* old_data);</CODE
|
>(cyg_code_t exception_number, cyg_exception_handler_t* new_handler, cyg_addrword_t new_data, cyg_exception_handler_t** old_handler, cyg_addrword_t* old_data);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_exception_clear_handler</CODE
|
>void cyg_exception_clear_handler</CODE
|
>(cyg_code_t exception_number);</CODE
|
>(cyg_code_t exception_number);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_exception_call_handler</CODE
|
>void cyg_exception_call_handler</CODE
|
>(cyg_handle_t thread, cyg_code_t exception_number, cyg_addrword_t exception_info);</CODE
|
>(cyg_handle_t thread, cyg_code_t exception_number, cyg_addrword_t exception_info);</CODE
|
></P
|
></P
|
><P
|
><P
|
></P
|
></P
|
></DIV
|
></DIV
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="REFSECT1"
|
CLASS="REFSECT1"
|
><A
|
><A
|
NAME="AEN817"
|
NAME="AEN817"
|
></A
|
></A
|
><H2
|
><H2
|
>Description</H2
|
>Description</H2
|
><P
|
><P
|
>Sometimes code attempts operations that are not legal on the current
|
>Sometimes code attempts operations that are not legal on the current
|
hardware, for example dividing by zero, or accessing data through a
|
hardware, for example dividing by zero, or accessing data through a
|
pointer that is not properly aligned. When this happens the hardware
|
pointer that is not properly aligned. When this happens the hardware
|
will raise an exception. This is very similar to an interrupt, but
|
will raise an exception. This is very similar to an interrupt, but
|
happens synchronously with code execution rather than asynchronously
|
happens synchronously with code execution rather than asynchronously
|
and hence can be tied to the thread that is currently running.
|
and hence can be tied to the thread that is currently running.
|
</P
|
</P
|
><P
|
><P
|
>The exceptions that can be raised depend very much on the hardware,
|
>The exceptions that can be raised depend very much on the hardware,
|
especially the processor. The corresponding documentation should be
|
especially the processor. The corresponding documentation should be
|
consulted for more details. Alternatively the architectural HAL header
|
consulted for more details. Alternatively the architectural HAL header
|
file <TT
|
file <TT
|
CLASS="FILENAME"
|
CLASS="FILENAME"
|
>hal_intr.h</TT
|
>hal_intr.h</TT
|
>, or one of the
|
>, or one of the
|
variant or platform header files it includes, will contain appropriate
|
variant or platform header files it includes, will contain appropriate
|
definitions. The details of how to handle exceptions, including
|
definitions. The details of how to handle exceptions, including
|
whether or not it is possible to recover from them, also depend on the
|
whether or not it is possible to recover from them, also depend on the
|
hardware.
|
hardware.
|
</P
|
</P
|
><P
|
><P
|
>Exception handling is optional, and can be disabled through the
|
>Exception handling is optional, and can be disabled through the
|
configuration option <TT
|
configuration option <TT
|
CLASS="VARNAME"
|
CLASS="VARNAME"
|
>CYGPKG_KERNEL_EXCEPTIONS</TT
|
>CYGPKG_KERNEL_EXCEPTIONS</TT
|
>. If
|
>. If
|
an application has been exhaustively tested and is trusted never to
|
an application has been exhaustively tested and is trusted never to
|
raise a hardware exception then this option can be disabled and code
|
raise a hardware exception then this option can be disabled and code
|
and data sizes will be reduced somewhat. If exceptions are left
|
and data sizes will be reduced somewhat. If exceptions are left
|
enabled then the system will provide default handlers for the various
|
enabled then the system will provide default handlers for the various
|
exceptions, but these do nothing. Even the specific type of exception
|
exceptions, but these do nothing. Even the specific type of exception
|
is ignored, so there is no point in attempting to decode this and
|
is ignored, so there is no point in attempting to decode this and
|
distinguish between say a divide-by-zero and an unaligned access.
|
distinguish between say a divide-by-zero and an unaligned access.
|
If the application installs its own handlers and wants details of the
|
If the application installs its own handlers and wants details of the
|
specific exception being raised then the configuration option
|
specific exception being raised then the configuration option
|
<TT
|
<TT
|
CLASS="VARNAME"
|
CLASS="VARNAME"
|
>CYGSEM_KERNEL_EXCEPTIONS_DECODE</TT
|
>CYGSEM_KERNEL_EXCEPTIONS_DECODE</TT
|
> has to be enabled.
|
> has to be enabled.
|
</P
|
</P
|
><P
|
><P
|
>An alternative handler can be installed using
|
>An alternative handler can be installed using
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_set_handler</TT
|
>cyg_exception_set_handler</TT
|
>. This requires a code
|
>. This requires a code
|
for the exception, a function pointer for the new exception handler,
|
for the exception, a function pointer for the new exception handler,
|
and a parameter to be passed to this handler. Details of the
|
and a parameter to be passed to this handler. Details of the
|
previously installed exception handler will be returned via the
|
previously installed exception handler will be returned via the
|
remaining two arguments, allowing that handler to be reinstated, or
|
remaining two arguments, allowing that handler to be reinstated, or
|
null pointers can be used if this information is of no interest. An
|
null pointers can be used if this information is of no interest. An
|
exception handling function should take the following form:
|
exception handling function should take the following form:
|
</P
|
</P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="PROGRAMLISTING"
|
CLASS="PROGRAMLISTING"
|
>void
|
>void
|
my_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_t info)
|
my_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_t info)
|
{
|
{
|
…
|
…
|
}
|
}
|
</PRE
|
</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
>The data argument corresponds to the <TT
|
>The data argument corresponds to the <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>new_data</I
|
>new_data</I
|
></TT
|
></TT
|
>
|
>
|
parameter supplied to <TT
|
parameter supplied to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_set_handler</TT
|
>cyg_exception_set_handler</TT
|
>.
|
>.
|
The exception code is provided as well, in case a single handler is
|
The exception code is provided as well, in case a single handler is
|
expected to support multiple exceptions. The <TT
|
expected to support multiple exceptions. The <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>info</I
|
>info</I
|
></TT
|
></TT
|
>
|
>
|
argument will depend on the hardware and on the specific exception.
|
argument will depend on the hardware and on the specific exception.
|
</P
|
</P
|
><P
|
><P
|
><TT
|
><TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_clear_handler</TT
|
>cyg_exception_clear_handler</TT
|
> can be used to
|
> can be used to
|
restore the default handler, if desired. It is also possible for
|
restore the default handler, if desired. It is also possible for
|
software to raise an exception and cause the current handler to be
|
software to raise an exception and cause the current handler to be
|
invoked, but generally this is useful only for testing.
|
invoked, but generally this is useful only for testing.
|
</P
|
</P
|
><P
|
><P
|
>By default the system maintains a single set of global exception
|
>By default the system maintains a single set of global exception
|
handlers. However, since exceptions occur synchronously it is
|
handlers. However, since exceptions occur synchronously it is
|
sometimes useful to handle them on a per-thread basis, and have a
|
sometimes useful to handle them on a per-thread basis, and have a
|
different set of handlers for each thread. This behaviour can be
|
different set of handlers for each thread. This behaviour can be
|
obtained by disabling the configuration option
|
obtained by disabling the configuration option
|
<TT
|
<TT
|
CLASS="VARNAME"
|
CLASS="VARNAME"
|
>CYGSEM_KERNEL_EXCEPTIONS_GLOBAL</TT
|
>CYGSEM_KERNEL_EXCEPTIONS_GLOBAL</TT
|
>. If per-thread
|
>. If per-thread
|
exception handlers are being used then
|
exception handlers are being used then
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_set_handler</TT
|
>cyg_exception_set_handler</TT
|
> and
|
> and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_clear_handler</TT
|
>cyg_exception_clear_handler</TT
|
> apply to the current
|
> apply to the current
|
thread. Otherwise they apply to the global set of handlers.
|
thread. Otherwise they apply to the global set of handlers.
|
</P
|
</P
|
><DIV
|
><DIV
|
CLASS="CAUTION"
|
CLASS="CAUTION"
|
><P
|
><P
|
></P
|
></P
|
><TABLE
|
><TABLE
|
CLASS="CAUTION"
|
CLASS="CAUTION"
|
BORDER="1"
|
BORDER="1"
|
WIDTH="100%"
|
WIDTH="100%"
|
><TR
|
><TR
|
><TD
|
><TD
|
ALIGN="CENTER"
|
ALIGN="CENTER"
|
><B
|
><B
|
>Caution</B
|
>Caution</B
|
></TD
|
></TD
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
><P
|
><P
|
>In the current implementation
|
>In the current implementation
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_call_handler</TT
|
>cyg_exception_call_handler</TT
|
> can only be used on
|
> can only be used on
|
the current thread. There is no support for delivering an exception to
|
the current thread. There is no support for delivering an exception to
|
another thread.
|
another thread.
|
</P
|
</P
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="NOTE"
|
CLASS="NOTE"
|
><BLOCKQUOTE
|
><BLOCKQUOTE
|
CLASS="NOTE"
|
CLASS="NOTE"
|
><P
|
><P
|
><B
|
><B
|
>Note: </B
|
>Note: </B
|
>Exceptions at the eCos kernel level refer specifically to
|
>Exceptions at the eCos kernel level refer specifically to
|
hardware-related events such as unaligned accesses to memory or
|
hardware-related events such as unaligned accesses to memory or
|
division by zero. There is no relation with other concepts that are
|
division by zero. There is no relation with other concepts that are
|
also known as exceptions, for example the <TT
|
also known as exceptions, for example the <TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>throw</TT
|
>throw</TT
|
> and
|
> and
|
<TT
|
<TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>catch</TT
|
>catch</TT
|
> facilities associated with C++.
|
> facilities associated with C++.
|
</P
|
</P
|
></BLOCKQUOTE
|
></BLOCKQUOTE
|
></DIV
|
></DIV
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="REFSECT1"
|
CLASS="REFSECT1"
|
><A
|
><A
|
NAME="KERNEL-EXCEPTIONS-CONTEXT"
|
NAME="KERNEL-EXCEPTIONS-CONTEXT"
|
></A
|
></A
|
><H2
|
><H2
|
>Valid contexts</H2
|
>Valid contexts</H2
|
><P
|
><P
|
>If the system is configured with a single set of global exception
|
>If the system is configured with a single set of global exception
|
handlers then
|
handlers then
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_set_handler</TT
|
>cyg_exception_set_handler</TT
|
> and
|
> and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_clear_handler</TT
|
>cyg_exception_clear_handler</TT
|
> may be called during
|
> may be called during
|
initialization or from thread context. If instead per-thread exception
|
initialization or from thread context. If instead per-thread exception
|
handlers are being used then it is not possible to install new
|
handlers are being used then it is not possible to install new
|
handlers during initialization because the functions operate
|
handlers during initialization because the functions operate
|
implicitly on the current thread, so they can only be called from
|
implicitly on the current thread, so they can only be called from
|
thread context. <TT
|
thread context. <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_exception_call_handler</TT
|
>cyg_exception_call_handler</TT
|
> should
|
> should
|
only be called from thread context.
|
only be called from thread context.
|
</P
|
</P
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="NAVFOOTER"
|
CLASS="NAVFOOTER"
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"><TABLE
|
WIDTH="100%"><TABLE
|
SUMMARY="Footer navigation table"
|
SUMMARY="Footer navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel-thread-destructors.html"
|
HREF="kernel-thread-destructors.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="ecos-ref.html"
|
HREF="ecos-ref.html"
|
ACCESSKEY="H"
|
ACCESSKEY="H"
|
>Home</A
|
>Home</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel-counters.html"
|
HREF="kernel-counters.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
>Thread destructors</TD
|
>Thread destructors</TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel.html"
|
HREF="kernel.html"
|
ACCESSKEY="U"
|
ACCESSKEY="U"
|
>Up</A
|
>Up</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
>Counters</TD
|
>Counters</TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
></DIV
|
></DIV
|
></BODY
|
></BODY
|
></HTML
|
></HTML
|
|
|