Tics Realtime -----


 
Home Services Products Tutorials Contact Us
- - - - -

Virtual Machines for Real-time Systems

A virtual machine (VM) is an implementation of a CPU in software.
	/* Assume that instructions reside
	sequentially in a global byte array. */

	for (;;) {

		getNextInstruction(&opCode, &operand1, &operand2, &inc);

		switch (opCode) {

		case ADD:
			result = operand1 + operand2;
			break;

		case SUB:
			result = operand1 - operand2;
			break;

		case JMP:
			IP = operand1;
			break;

		case JGE:
			if (flags & GE_MASK) IP += operand1;
			break;

		/* And so forth... */
		}

		/* Point to next instruction */
		IP += inc;
	}

The code fragment above illustrates the basic concept of the VM. Each instruction is retrieved from a global array and executed just like a real machine. The VM maintains an instruction pointer (index into the global array), status flags, and so on. The VM has the following benefits.
  1. Greatly enhanced debugging ability.
  2. Better management of system crashes.
  3. Better security.
  4. Multi-tasking is easily implemented within the VM.
  5. Unlimited number of virtual interrupts.
  6. Portable to any microprocessor for which a C compiler is available.
  7. Virtual components.
Some of these benefits are detailed below.

Debugging

Debugging is greatly enhanced because software controls each instruction. This situation allows for ICE-like debugging features. ICE-like debugging features could be implemented by having the VM check a debug variable before executing each instruction. A non-zero value would indicate that debugging is enabled. In this way breakpoints, trace, address and data range monitoring, etc. are all easily implemented.

System Crashes

Within the VM world, the actual CPU never crashes. When a crash does occur it happens within the VM which is a software program. Consider an application running under the VM that jumps to an invalid memory location. This situation could be detected by the VM and handled in a number of ways. For example, the application could be terminated or a debug mode could be entered when a crash is detected.

Security

Security is an issue in many environments (the Internet for example). In secure environments the VM can act as a shield between the application and the actual CPU, disk, etc. One can envision virtual disks, security tags on each VM instruction, etc. This is a topic for a separate article; please let us know if you would like more information on this subject.

Multi-tasking

Multi-tasking is typically handled in software by saving the task's context and switching stacks. The flexibility of the VM allows for multi-tasking to be built into it. Each task is assigned its own context (status flags, IP, SP, etc.) which eliminates the need to save and restore context on each task switch. A task switch in a VM could be as simple as changing the "current context pointer" to point to the new task's context block.

Virtual Interrupts

An interrupt is a hardware signal that tells the processor to jump to a predetermined location (the address of the isr). Although a VM could implement interrupts in a number of ways, here is one example. The VM handles hardware interrupts by reading a 32 bit port after the execution of each VM instruction. External devices assert an interrupt by raising a pin on the 32 bit port. When any of the port bits are non-zero, the VM switches to the interrupt task by using the port bit number as an index into a table of interrupt tasks. Interrupt task priorities are always higher than the highest application or system task so that they cannot be interrupted by a non-interrupt task. However, since each interrupt task has a different priority (based on the port bit number), an interrupt task can be interrupted by a higher priority interrupt task. Software interrupts are handled in the same way by using a 32 bit global variable instead of an external port.

Remarks

Although much slower than a hardware machine, the VM may have applications in areas where the demands on the VM are minimal, and the benefits of the VM are deemed essential and not achievable by other means.


We welcome comments. Let us know what subjects you would like written up. Send comments to Mike@TicsRealtime.com

Copyright © 2000, Tics Realtime