Methods and systems for applications to interact with hardware

A method of providing Java application layer access to hardware peripheral memory mapped registers is provided together with a processor adapted to implement such a method. A fixed memory address space for a hardware peripheral's memory mapped registers is identified, and a Java object is constructed having elements which occupy this fixed memory address space. This allows a Java application to be provided with access to the hardware peripheral's memory mapped registers directly through the Java object. A new Java class is defined having base address and length parameters and in some cases also having a type parameter. This is used in constructing the Java object. When a Java object has an object descriptor which is effectively an object header and a pointer to where the object data is located, constructing the Java object may be done by creating an object descriptor, and then creating an object handle for the Java object which points to the object descriptor. Alternatively, a level of indirection may be removed, and the object handle created to point directly to the object created so as to exist in memory mapped register space.

FIELD OF THE INVENTION

The invention relates to methods and systems for applications to interact with hardware, and more particularly for applications in languages such as Java which lack an ability to access particular memories directly, to interact with hardware peripherals.

BACKGROUND OF THE INVENTION

Typical software systems include an application program run by an operating system on a processor connected to a number of hardware peripherals. In some systems, such as those where the Java language is employed, there is additionally a virtual machine such as a Java virtual machine (JVM) situated between the operating system and the application program.

In order to facilitate communication with the hardware peripherals, nearly all complex software systems include a device driver which is specific to each hardware peripheral. These device drivers provide a layer of abstraction to their clients (the operating system and ultimately the application program) while allowing them to use the underlying hardware peripherals.

However, device drivers are notoriously difficult to debug or troubleshoot due to the asynchronous nature of their coupling with interrupts and due the lack of debugging features. A faulty driver can also inhibit user input and/or user output. At interrupt levels, the system cannot provide support for the common user state input/output functionality. Furthermore, since timing is often critical, it is impossible to stop a processor and trace device driver code in a non-destructive way. Also, because of the way systems are developed, frequently different development teams are responsible for different layers of a design. When interface problems develop, it is often difficult to determine where the problem originated and hence which team should fix the problem.

Consequently, device drivers take more time to develop and their opacity makes them more error-prone. One of the most common errors which may occur during driver development is an error in pointer arithmetic which instructs the processor to access an erroneous location. Systems generally allow this initially and operation resumes without any apparent disturbance until a later point in time where the value in question is used.

While this is not a big problem for large computing platforms with standardized peripheral interfaces, and a standardized layered architecture, it becomes a very serious problem for application specific hardware and devices where for each new design, the application peripheral path must be debugged from scratch.

Common safeguard measures against pointer arithmetic errors include software range checking. Some languages, such as Java, have inherent measures which prevent invalid memory accesses. However, using the built-in range checking of standard Java to develop device drivers is currently impossible as one of the fundamental characteristics of Java is that any client machine should be protected from corruption/bugs in Java, i.e. any bug in a Java application should only effect the Java application and should have no effect on other applications and memory unrelated to the Java application. To achieve this level of security, Java applications running on the JVM are not given direct access to memory. Instead, memory access is done through an indirection mechanism through the JVM.

Referring now toFIG. 1, a conventional embedded environment3typically has hardware2, software in the form of native code4(or assembly language), and software in the form of a Java application6. Also shown are externally connected hardware peripherals8. The hardware2consists of a processor core10, memory in the form of RAM12and/or ROM14and one or more physical interfaces18including for example a serial port27. The software4running on the processor10includes an operating system20over top of which is run a Java virtual machine22as a task, and also over which other tasks such as an event dispatcher task23is run. The Java application6uses the resources and features of the Java virtual machine22.

FIG. 1also shows the details of a typical path from the Java application6to and from a particular hardware peripheral8which for the purpose of this example we will assume is the serial port peripheral26connected through the serial port27. The Java application6includes functionality28for either generating data ultimately for output to the serial port27, or for processing data ultimately received from the serial port27. Of course the functionality28does not interact with the serial port27directly. The Java virtual machine22has a Java native interface30through which the Java application6communicates with the serial port physical interface27. The operating system20has a serial port device driver32which has an input queue34and an output queue36, through which it communicates with the underlying hardware2. The serial port device driver32is typically run at interrupt level, or through a deferred procedure call within the operating system kernel (not shown). More specifically, the serial port device driver32communicates with the serial port27through serial port memory mapped registers56to an input queue38and an output queue40and on to the hardware peripheral26. The operating system20also has an IRQ (interrupt request) handler33for each interrupt from any hardware peripheral.

When the Java application6has to communicate with the hardware peripherals8and in this case the serial port peripheral26, a path such as that consisting of the serial port communications28→Java virtual machine22→Java native interface30→operating system20→serial port device driver32→serial port physical interface27→serial port hardware peripheral26must be established and debugged for each different hardware peripheral. More specifically, when the Java application6has data to send to the serial port peripheral28, the Java application6communicates with the device driver32using the Java native interfaces30. The JNI30takes the data, formats it and passes it on to the device driver32by copying it into the output buffer34. The serial port device driver32transfers the data to the serial port memory mapped registers56of the serial port27. These are copied into the hardware queue38in the serial port27for output.

When the serial port27receives data destined for the Java application6, an even more complicated path is taken. For communication originating from the hardware, the process typically goes as follows. To begin, the arrival of data at the serial port27triggers the assertion of a hardware interrupt. When this occurs, the program flow is interrupted, and the IRQ handler33starts an interrupt service routine. The interrupt service routine calls the serial port device driver32which reads the data from the hardware input queue40in the serial port27and copies it into the input queue36which is one of the device driver's data structures. The serial port device driver32then posts an event to the event dispatcher task23. The interrupt service routine returns and normal Java operation resumes. The event dispatcher task23sends an event to one of the destination threads6to read from the input queue36of the serial port device driver32, for example through a piping mechanism provided by the JNI30.

It can be clearly seen that there are a large number of areas where bugs may make their way into the design of such a Java application—hardware peripheral interaction. Furthermore, each copying stage forces power consuming and processor intensive operations which are inevitable due to the abstractions of the operating system20and the JVM22.

SUMMARY OF THE INVENTION

It is an object of the invention to obviate or mitigate one or more of the above-identified disadvantages.

One embodiment of the invention provides a method of providing Java application layer access to hardware peripheral memory mapped registers. A fixed memory address space for a hardware peripheral's memory mapped registers is identified, and a Java object is constructed having elements which occupy this fixed memory address space. This allows a Java application to be provided with access to the hardware peripheral's memory mapped registers directly through the Java object.

A new Java class may be defined having base address and length parameters and in some cases also having an element length parameter. This is used in constructing the Java object.

In an embodiment applicable when a Java object has an object descriptor which is effectively an object header and a pointer to where the object data is located, constructing the Java object may be done by creating an object descriptor, and then creating an object handle for the Java object which points to the object descriptor.

Preferably, the new Java class having a class name <class name>, for example, “AnchoredArray” is defined as follows:

where <class name> is the name assigned to the new class. Optionally, a parameter “type” might also be provided which specifies the type of object to be created. If multiple types are not contemplated, then this parameter would not be required. In the examples which follow, a default type of integer array is assumed. The parameter “base” specifies a beginning address, and length is a parameter specifying a number of elements in the object, which when constructed, generates an object descriptor having the specified type, base, length, and also generates a handle to the object descriptor.

In another embodiment, a level of indirection is removed, and the object handle points directly to the object created so as to exist in memory mapped register space. In so doing, preferably, a memory map is defined having a predetermined address space for the peripheral, and having at least one additional address space allocated contiguous with the predetermined address space. Object header information for the Java object is stored directly in the additional address space.

Other embodiments provide a Java object defined such that it overlaps with a predetermined address space; a Java class which enables a Java object to be defined such that it overlaps with a predetermined address space; and a Java virtual machine featuring such a class. Yet another embodiment provides an integrated circuit having a plurality of peripheral memory mapped registers and a Java virtual machine which has Java objects anchored to said peripheral memory mapped registers.

Advantageously, the embodiments of the invention permit portions of memory space defined by the object descriptor or header to be anchored to the hardware peripheral. Java application software can then use the object using standard Java methods and procedures to control the hardware peripheral with all the benefits of hardware protection and abstraction that are provided in a normal Java virtual machine. Advantageously, faster development and integration can be realized using such anchored arrays.

DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

Communications from a processor to hardware peripherals are typically done through registers which are mapped to a predetermined address space of the processor. Embodiments of the invention provide systems and methods for anchoring an object in Java, such as an array object, such that it overlaps with the register area of a hardware peripheral. By way of example,FIG. 2illustrates a typical memory map showing an entire address space running from a lowest address470x0000 to a highest address480xFFFF for a 64 kB memory address space. Usually, a first portion50of the address space is reserved for ROM, a second portion52of the address is reserved for RAM, and a third portion54of memory address space is reserved for registers used by hardware peripherals. The mapping for a given peripheral, such as a serial port peripheral for example, has been expanded, as generally indicated by56. Typically, there are a number of addresses58(four in the illustrated example) which map to control registers which are used to control the peripheral. There are a number of addresses60(two in the illustrated example) mapped to data registers through which the data flow per se occurs. These data registers would not exist for peripherals to/from which data flow is not to occur. Finally, there are typically a number of addresses62(two in the illustrated example) mapped to registers for accessing status information. There is a separate portion of the peripheral address space54for each hardware peripheral.

FIGS. 3A to 3Cillustrate some of the internal memory structures of a conventional Java virtual machine such as JVM22ofFIG. 1. InFIG. 3A, generally indicated at80is a list of object handles82. Each of the handles82is an address which points to an object descriptor such as shown inFIG. 3Bgenerally indicated by84which includes a header85defining an object. In the case of an array, the header85contains a type field86(identifying the object to be an array and defining the size of each element in the array and possibly other type information, and defining what type of Java garbage collection is to be performed on the object), a base address field88, and a length field90in units of array elements. The base address field88contains the start address of array elements in memory such as array elements92illustrated inFIG. 3C. During normal operation, the length field90is compared with indexes into the array92to determine whether an exception must be thrown to signal an out of bounds access. In conventional systems the elements of the array structure92are abstracted from Java applications which therefore can never refer to them directly. Rather, as shown inFIG. 3Da section94of memory (typically in RAM) between the addresses heap.start and heap.end is set aside for the dynamic allocation to objects. The creation of an array at the application level would allocate a region of memory96selected from the available object memory94. With the allocation, the JVM would fill in the elements of structure84. Both the object handles82and the objects to which they refer are created and destroyed by the JVM22.

According to an embodiment of the invention, a method is provided for anchoring a Java object, such as an array, to a specific area of memory, for example to a predetermined portion of memory address space for example space mapped to a hardware peripheral. While the examples provided are Java specific, the invention can also be applied to other application layer models which would otherwise restrict access to the specific memory mapped locations. To achieve this, a new Java class is defined, referred to herein as “class AnchoredArray” although of course other names may be used.

Referring toFIG. 4, the new AnchoredArray class has a parameter list100containing the elements base104, length106which are used to characterize a particular hardware peripheral in the sense that the base104is selected to be the base address of the registers in memory assigned to the particular hardware peripheral, and length106specifies how many elements there are in the hardware peripheral's memory mapped registers. The class might optionally be designed to include a type element for specifying an array having a certain structure. In the absence of a type, the class would need to assume a default type, for example an array of integers. This class would, upon construction (which would normally occur during system initialization), use the parameter list100to generate an array object descriptor84having an object header85(seeFIG. 3B). (As an aside, it is noted that this differs from the construction of a normal array which is done by allocating a memory region96from the object memory94and then filling in the header85after the fact.) More specifically, a default value specifying an integer array, and the parameters base104, and length106would be copied into corresponding type86, base address88, and length90fields in the header85of an array object descriptor84. Once created, the array object descriptor is indistinguishable from a normal Java array object descriptor. The class has associated native code specifically written to achieve this function.

The array object descriptor84thus created would be stored in the object memory94, and a handle82to the array object descriptor84is added to the list of object handles80. At this point the memory space defined by the object descriptor84is anchored to the hardware peripheral and the Java application software can access the array using standard Java methods and procedures to control the hardware peripheral with all the benefits of hardware protection and abstraction that are provided in a normal Java virtual machine.

It is noted that the list of handles80and the object descriptor84can take many shapes and forms. The initialization can also take place when a class is loaded, or at boot time. Alternatively, a system class could let the system allocate a normal array and then replace the pointers. A specific example of pseudocode for implementing the AnchoredArray class is provided further below.

A second embodiment of the invention is provided for use when the memory structure representing an object differs from that ofFIGS. 3B and 3C. Referring toFIG. 5, the memory structure120for this embodiment has a type122and a length124. Instead of a base address however, this is immediately followed by a plurality of data elements126which make up the body of the array. By taking away the base address, a level of indirection has been removed. This second embodiment requires peripheral memory mapped address space to conform to the defined object structure.

In order to map hardware peripheral address space directly to such an array object, the memory map must include the additional fields length and type either adjacent to or as part of the normal hardware peripheral memory mapped registers as depicted inFIG. 6which is similar to the memory map ofFIG. 2except that additional registers130,132are provided for type and length respectively. Thus a slight change to the memory map for the hardware peripherals is required.

Hence, the object descriptor of the structure120must forcefully prepend the data elements, whereas the use of structure84provided an additional level of indirection. The absence of this additional level of indirection makes accesses to elements of the array120faster, but also forces the hardware peripheral address space shown inFIG. 6to contain additional registers for the type and length fields130,132.

These two additional registers130,132may be hardcoded in hardware or initialized using a dedicated base class. At initialization time, the dedicated base class ensures an object handle72refers directly to the base of the peripheral registers, at which point application software can use the array using standard Java methods and procedures to control the hardware devices, with all the benefits of hardware protection and abstraction that are provided in a Java virtual machine.

In another embodiment, rather than having two additional registers130,132for each peripheral, only two additional registers are provided for the peripheral address space collectively. Java threads reading or writing to the single composite object thus created would need to know which subgroup of registers to use. While this protects non-peripheral memory space from incorrect access, it does not prevent a thread working with one peripheral from erroneously accessing registers belonging to another peripheral.

In the Java environment, a consideration which might need to be dealt with during the construction of objects, and for our purposes the construction of objects which map to peripheral address spaces, is garbage collection. Garbage collection is the process through which the JVM performs housekeeping on the object memory. The details of Java garbage collection are well known and will not be repeated here. In order to prevent objects being garbage collected during their creation, the objects should be created as static objects using static initialization blocks. In the event dynamic objects are used, then garbage collection issues would need to be addressed. The dynamic objects might be created using the static root mechanism for example, which is also well known.

By way of example, the following is Pseudocode for an implementation of the AnchoredArray class for the case where the object structure ofFIGS. 3B and 3Cis employed and where the default is that the array is an integer array. The Java class is compiled with the rest of the system. Device driver classes can make use of it to access hardware.

//element is an array handle which will be mapped directly onto the peripheral.

//The constructor takes two parameters, a base address and a length

//The function of the constructor is to initialize element [ ] using those parameters.

//The action of mapping an array onto a specific area of memory is done at the native

//level. Since a Java constructor cannot be native, the constructor calls a native

//This native function instantiates an array on a fixed memory area. It is made static

//because it does not use the class instance.

//first, we get the two parameters off the stack. All the parameters in Java are

//pushed onto the stack by the caller, and popped from the stack by the native

//method. Similarly, the native functions push the result onto the stack for the caller

//in the case where there is an indirection, we would normally allocate space for array

//elements but in this case we don't because the base indicates where the elements

//are. We just create the object that points to the elements.

//That's it. We are now passing the handle to Java.

Pseudocode follows for an example implementation of the AnchoredArray class and associated lockDownElements native function for the case where objects have the structure ofFIG. 5, i.e. the length, base information is to be stored in the memory mapped registers adjacent the remaining registers.
Class AnchoredArray
{
public int element[ ];
public AnchoredArray(int baseAddress)

//first, we get the parameter off the stack. All the parameters in Java are

//pushed onto the stack by the caller, and popped from the stack by the native

//method. Similarly, the native functions pushes the result onto the stack for the caller

//the beginning of the peripheral. In that case, the length field is not used.

//that's it. We are now passing the handle to the hardcoded object to Java.

Referring back to the memory map example ofFIG. 2and in particular to the memory map details for the serial port, the base address for the serial port's address space is SPORT_BASE=0xFF00, the length is SPORT_LENGTH=8. These values would be used in the parameter list100when constructing the AnchoredArray class for the serial port.

The JVM would be designed to include the above discussed AnchoredArray class as part of its system classes, and for the serial port peripheral, there would be a constructor:

where SPORT_BASE is a constant in this case equal to 0xFF00, and SPORT_LENGTH is a constant in this case equal to 8 which would result in a handle82pointing to the serial_port anchored array (i.e. pointing to 0xFF00) being added to the list of handles80at initialization. Subsequently, indexes into the array may be done for example through expressions such as serial_port.element[C1], where C1 is a constant which indexes into the array to the register occupied by the first of the peripheral's constants64, which would in turn provide access to that register. The Java range checking functionality would permit access through this particular handle only to the specific range of addresses defined. An attempt to access an index which would point to memory addresses outside this space would result in an exception.

Optionally, the AnchoredArray class can be declared so that application classes cannot call it if they are not part of the same Java package.

Optionally, the AnchoredArray class can be made public to allow driver classes to be put in any package and use the AnchoredArray class nonetheless.

Optionally, the AnchoredArray constructor can be protected so it can only be invoked by derived classes.

Numerous modifications and variations of the present invention are possible in light of the above teachings. It is therefore to be understood that within the scope of the appended claims, the invention may be practised otherwise than as specifically described herein.