Interrupt Handlers in Java
Total Page:16
File Type:pdf, Size:1020Kb
Downloaded from orbit.dtu.dk on: Oct 06, 2021 Interrupt Handlers in Java Korsholm, Stephan; Schoeberl, Martin; Ravn, Anders P. Published in: Proceedings of the 11th IEEE International Symposium on Object/component/service-oriented Real-time distributed Computing (ISORC 2008) Link to article, DOI: 10.1109/ISORC.2008.68 Publication date: 2008 Document Version Early version, also known as pre-print Link back to DTU Orbit Citation (APA): Korsholm, S., Schoeberl, M., & Ravn, A. P. (2008). Interrupt Handlers in Java. In Proceedings of the 11th IEEE International Symposium on Object/component/service-oriented Real-time distributed Computing (ISORC 2008) (pp. 453-457) https://doi.org/10.1109/ISORC.2008.68 General rights Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. Users may download and print one copy of any publication from the public portal for the purpose of private study or research. You may not further distribute the material or use it for any profit-making activity or commercial gain You may freely distribute the URL identifying the publication in the public portal If you believe that this document breaches copyright please contact us providing details, and we will remove access to the work immediately and investigate your claim. Interrupt Handlers in Java Stephan Korsholm Martin Schoeberl Department of Computer Science Institute of Computer Engineering Aalborg University DK-9220 Aalborg Vienna University of Technology, Austria [email protected] [email protected] Anders P. Ravn Department of Computer Science Aalborg University DK-9220 Aalborg [email protected] Abstract both cases the interrupts are handled out-of-context. An advantage of this approach is that the execution of the An important part of implementing device drivers is to second level interrupt handler is controlled by the normal control the interrupt facilities of the hardware platform and thread scheduler and thus observes the priorities and schedul- to program interrupt handlers. Current methods for handling ing principles of it. Less desirable features are: interrupts in Java use a server thread waiting for the VM to signal an interrupt occurrence. It means that the interrupt Latency: A delay is introduced from the occurrence of the is handled at a later time, which has some disadvantages. interrupt until the point in time when it is handled. We present constructs that allow interrupts to be handled di- Out-of-context execution: The interrupt is handled out of rectly and not at a later point decided by a scheduler. A the context of the first level interrupt handler. This means that desirable feature of our approach is that we do not require any computation that requires this context cannot be done in a native middelware layer but can handle interrupts entirely Java, but must be done in native code. If the context depen- with Java code. We have implemented our approach using dent part can be kept stereotypical, this is less of a problem. an interpreter and a Java processor, and give an example In other cases where device dependent actions are needed, demonstrating its use. the native middelware layer becomes complex. The contribution of this paper consists in the design and 1 Introduction implementation of a mechanism for handling interrupts in Java. It does not hand over the interrupt to a sporadic thread, In an embedded system which is implemented as a collec- but handles the interrupt completely in the context of the first tion of cooperating threads, interrupt handlers are more simi- level interrupt handler. We call this mechanism in-context lar to subroutines than to threads. The handlers are not sched- interrupt handling. An important feature of in-context in- uled as threads; they may be invoked any time and even inter- terrupt handling is that actions that need to be done in the rupt the thread scheduler, because interrupts are dispatched context of the first level interrupt handler can now be done in by the hardware as response to external events. Java, in effect eliminating the need for a native middelware Current profiles for real-time Java [5, 2] attempt to hand layer. This mechanism does not preclude the standard way of over interrupts to normal sporadic threads as quickly as pos- handling interrupts in real-time Java, but complements it and sible. This can be done by installing a piece of native code, can be used in cases where the standard method is inadequate called the first level interrupt handler, to be invoked by the for one of the reasons given above. hardware, and let this code register the interrupt occurrence In Section 2 we will describe how interrupts are handled and then return. Then at each reschedule point the interrupt in legacy systems. In Section 3 we introduce our design for mark is checked and a waiting thread is unblocked to handle in-context interrupt handling, and discuss how the method the interrupt (this is sometimes called the second level inter- can be supported in existing Java execution environments. In rupt handler). Examples of this approach are found in e.g. Section 4 we demonstrate how the design has been imple- the Squawk VM [11] and the JamaicaVM from aicas [1]. mented for two different execution environments for Java: Squawk reports an average latency of 0.1 milliseconds and the JOP Java processor and the SimpleRTJ interpreter. Then a worst case latency of around 13 milliseconds. From [4] in Section 5 we show a simple example of using our inter- we see that the JamaicaVM has an average latency of 50 mi- rupt handling implementations. We conclude the paper in croseconds and a worst case latency of 250 microseconds. In Section 6. volatile uint16 P0_UART_RX_TX_REG @ 0xFFE032; interrupt the function void Uart RX Interrupt(void) becomes an volatile uint16 P0_CLEAR_RX_INT_REG @ 0xFFE036; interrupt routine. This basically means that exit and entry volatile uint16 RESET_INT_PENDING_REG @ 0xFFE202; code is generated by the compiler to save and restore the state #define CLR_UART_RX_INT_PENDING 0x0010 of the interrupted process. #define CLEAR_UART_RI_FLAG P0_CLEAR_RX_INT_REG = 0 The circular buffer UartRxBuffer can be read by user code #define CLEAR_PENDING_UART_RI_INTERRUPT \ outside the context of the interrupt to handle the bytes re- RESET_INT_PENDING_REG = CLR_UART_RX_INT_PENDING ceived. Some kind of mutual exclusion between user code and the interrupt handler may be required. This is typically __interrupt void Uart_RX_Interrupt(void) { implemented by disabling interrupts. UartRxBuffer[UartRxWrPtr++] = P0_UART_RX_TX_REG; if (UartRxWrPtr>=sizeof(UartRxBuffer)) { 3 In-context Interrupt Handling UartRxWrPtr=0; } Our goal is to be able to implement the interrupt handler CLEAR_UART_RI_FLAG; from Figure 1 in pure Java. The two important tasks that the CLEAR_PENDING_UART_RI_INTERRUPT; Uart RX Interrupt handler must do are: } 1. Retrieve the received byte from the proper device regis- Figure 1. An example interrupt handler in C ter and save the byte in a data structure. 2. Clean up from the interrupt, in this case clear the 2 Conventional Interrupt Handling UART receive interrupt flag (CLEAR UART RI FLAG; CLEAR PENDING UART RI INTERRUPT). Interrupts are used to signal external events for example, detecting that a button has been pressed. When an interrupt Using the RTSJ profile [2] for Java the above tasks can natu- occurs the processor simply stops executing the code it runs, rally be solved in the following manner: and jumps to an interrupt routine instead. The jump saves Ad 1) Using the raw memory access supplied by RTSJ the environment of the interrupted process so that it may be through the class RawMemoryAccess, it is possible to read the restored later; this includes saving the CPU registers and the received byte from the proper device register. A detailed ex- processor status register. This makes it possible to continue ample on how this looks is available in [3] (Chapter 15.5). the execution of the original code when the interrupt routine Ad 2) The suggested way to handle interrupts in RTSJ has been executed. is to use the AsyncEvent and AsyncEventHandler classes. Saving the interrupted context and setting up a new con- Again [3] includes a detailed example. In the RTSJ an inter- text for the interrupt handler is called a context switch. Some rupt occurring is equivalent to the fire method being called on hardware platforms implement a full context switch in hard- the AsyncEvent object. This in turn will call the run() method ware, where other platforms implements a partial context on all installed handlers. But, to handle the interrupt from switch in hardware. In the latter case the programmer must Figure 1 in pure Java, the fire method must be called in the save those parts of the interrupted context that he needs to context of the interrupt. The reason is that the receive in- overwrite in the interrupt handler. terrupt flag must be cleared before exiting the interrupt con- As an example interrupt handler, Figure 1 shows an ex- text. Failing to do so will cause the interrupt to recur. In all cerpt of code implementing the RS232 receive interrupt for implementations of RTSJ that we know of, handling of the an existing legacy system. The RS232 receive interrupt is AsyncEvent corresponding to the interrupt will be scheduled generated by the hardware when the RS232 interface re- outside the context of the interrupt. This does not allow us to ceives a byte on the serial line. It is the job of the interrupt implement the handler from Figure 1 in pure Java. handler to retrieve the byte from the proper device register As a complementary way to handle interrupts in Java we and clear the receive interrupt flag.