Background of the Invention
1. Field of the Invention
The present invention generally relates to computer systems, and more particularly to a method of saving information from the central processor of the computer.
2. Description of the Prior Art
The basic structure of a conventional computer system 10 is shown in FIG. 1. The heart of computer system 10 is a central processing unit (CPU) or processor 12 which is connected to several peripheral devices, including input/output (I/O) devices 14 (such as a display monitor and keyboard) for the user interface, a permanent memory device 16 (such as a hard disk or floppy diskette) for storing the computer's operating system and user programs, and a temporary memory device 18 (such as random access memory or RAM) that is used by processor 12 in carrying out program instructions. Processor 12 communicates with the peripheral devices by various means, including a bus 20 or a direct channel 22. Computer system 10 may have many additional components which are not shown, such as serial and parallel ports for connection to, e.g., modems or printers. Those skilled in the art will further appreciate that there are other components that might be used in conjunction with those shown in the block diagram of FIG. 1; for example, a display adapter connected to processor 12 might be used to control a video display monitor. Computer system 10 also includes firmware 24 whose primary purpose is to seek out and load an operating system from one of the peripherals (usually permanent memory device 16) whenever the computer is first turned on.
An illustrative embodiment of processor 12 is further shown in FIG. 2, which depicts the architecture for a PowerPC.TM. microprocessor manufactured by International Business Machines Corp. In this embodiment, processor 12 operates according to reduced instruction set computing (RISC) techniques, and is a single integrated circuit superscalar microprocessor. The system bus 20 is connected to a bus interface unit (BIU) 30 of processor 12. It is understood that bus 20, as well as various other connections described, include more than one line or wire, e.g., the bus could be a 32-bit bus. BIU 30 is connected an instruction cache 32 and a data cache 34. The output of instruction cache 32 is connected to a sequencer unit 36. In response to the particular instructions received from instruction cache 32, sequencer unit 36 outputs instructions to other execution circuitry of processor 12, including six execution units, namely, a branch unit 38, a fixed-point unit A (FXUA) 40, a fixed-point unit B (FXUB) 42, a complex fixed-point unit (CFXU) 44, a load/store unit (LSU) 46, and a floating-point unit (FPU) 48.
The inputs of FXUA 40, FXUB 42, CFXU 44 and LSU 46 also receive source operand information from general-purpose registers (GPRs) 50 and fixed-point rename buffers 52. The outputs of FXUA 40, FXUB 42, CFXU 44 and LSU 46 send destination operand information for storage at selected entries in fixed-point rename buffers 52. CFXU 44 further has an input and an output connected to special-purpose registers (SPRs) 54 for receiving and sending source operand information and destination operand information, respectively. An input of FPU 48 receives source operand information from floating-point registers (FPRs) 56 and floating-point rename buffers 58. The output of FPU 48 sends destination operand information to selected entries in floating-point rename buffers 58. Processor 12 may include other registers, such as configuration registers, memory management registers, exception handling registers, and miscellaneous registers, which are not shown. Processor 12 carries out program instructions (from a user application or the operating system) by routing the instructions and data to the appropriate execution units, buffers and registers, and by sending the resulting output to the memory device 18, or some output device such as a display console.
During operation of the computer system, it frequently becomes necessary to save the context of the registers, for example, when swapping out processes (multi-tasking), or handling exceptions. The manner in which the context is saved depends upon size of the registers, e.g., whether they are 32-bit registers, 64-bit registers operating in 64-bit mode, or 64-bit registers operating in 32-bit mode, because different instructions must be used to save the information depending upon the nature of the registers. Accordingly, separate sets of code are required to perform the context-saving routine for different register architectures. The operating system must examine the processor version register (PVR, one of the configuration registers mentioned above) in order to determine which set of codes are to be used throughout its operation. At a given processor architecture version, whenever a new processor is available, the operating system must be modified to recognize the new PVR value since it may not be known previously. It would, therefore, be desirable to devise a method of saving a processor's register context which can be used for any processor version, present or future, of a given processor architecture version. Furthermore, when the processor architecture is changed to a newer version to include larger register size, it is desirable that the method could be easily expanded to cover the larger size registers with newly defined instructions which can handle them.
Summary of the Invention
It is therefore one object of the present invention to provide a routine for saving the register context of a computer processor.
It is another object of the present invention to provide a context-saving routine that can be used with processors having different register architectures.
It is yet another object of the present invention to provide such a context-saving routine that is adaptable to future processor versions of a given processor architecture or expandable to future changes of the processor architecture.
The foregoing objects are achieved in a method of saving the context of a plurality of registers in a computer processor, generally comprising the steps of determining whether the processor registers have a first size or a second size, and saving the contents of the registers in a buffer using a first set of instructions if the processor registers have the first size, and using a second set of instructions if the processor registers have the second size. If the processor registers having the first size, the method may further include the steps of determining whether the processor is operating in a first mode or a second mode, and then saving the contents of the registers in the buffer using the first set of instructions if the processor is operating in the first mode, but using the second set of instructions if the processor is operating in the second mode. In an exemplary embodiment, the first size is 64 bits and the second size is 32 bits, and the saving step is accomplished using either a first instruction in the first instruction set which retrieves 64 bits, or using a second instruction in the second instruction set which retrieves 32 bits. The method can additionally determine whether the processor registers have a third size, and use a third set of instructions if the processor registers have the third size. Similarly, if the processor can operate in a third mode, then the contents of the registers may be saved using a third set of instructions if the processor is operating in the third mode. The context-saving code can use a portion of the registers for its processing, such as by initially saving a first portion of the registers in the buffer, and thereafter using the first portion of the registers to temporarily store the contents of other registers. In such a case, the contents of the other registers can be later restored from the first portion of the registers, and thereafter saved in the buffer.
The above as well as additional objectives, features, and advantages of the present invention will become apparent in the following detailed written description.
Brief Description of the Drawings
The novel features believed characteristic of the invention are set forth in the appended claims. The invention itself, however, as well as a preferred mode of use, further objectives, and advantages thereof, will best be understood by reference to the following detailed description of an illustrative embodiment when read in conjunction with the accompanying drawings, wherein:
FIG. 1 is a block diagram of a conventional computer system;
FIG. 2 is a block diagram of a conventional computer processor, depicting execution units, buffers and registers;
FIG. 3 is a flow chart depicting the general steps performed according to the present invention; and
FIG. 4 is a flow chart depicting more specific steps performed according to the present invention for a PowerPC.TM. processor.
Description of the Preferred Embodiment
The present invention is directed to a method of saving the context of a computer processor's register. The computer system's hardware and the processor may include the various components shown in FIGS. 1 and 2, but the computer system is not necessarily conventional, i.e., it could include new hardware components as well, or have a novel interconnection architecture for existing components.
The general method of the present invention is shown in FIG. 3. Upon issuance of the command by the operating system to save the current register context (60), the context-saving code first checks to see if the processor registers are of a particular size, e.g., 64 bits (62). If not, then the routine simply uses the storage instructions for saving the contents of the registers which are associated with the other known processor size, e.g., 32 bits (64), and then exits the context-saving routine (66). If, however, the processor registers are of the larger 64-bit size, then a further determination is made as to whether the processor is using all of the register bits, i.e., operating in a 64-bit mode, or whether the processor is using a smaller number of the register bits, i.e., operating in a 32-bit mode (68). If the processor is operating in the lower 32-bit mode, then the contents of the registers are saved using the 32-bit storage instructions (64). Only if the processor has 64-bit registers and if it is operating in 64-bit mode will the context-saving routine use the 64-bit storage instructions (70).
Those skilled in the art will appreciate that this approach can be expanded for other types of processor architectures. For example, the context-saving code could be adapted to save the contents of 16-bit registers as well as 32-bit or 64-bit registers, using a third set of storage instructions. Similarly, if a 64-bit processor were designed to run in three modes (64-bit, 32-bit and 16-bit), then the context-saving code could examine a 64-bit processor to see which of the three modes it is operating in.
A more detailed procedure for saving register context according to the present invention is shown in FIG. 4. The flow chart of FIG. 4 depicts the steps used in saving the context of a processor, such as the PowerPC.TM. processor, which can have either 32-bit or 64-bit registers and which, if having 64-bit registers, can operate in either a 32-bit mode or a 64-bit mode. The procedure begins by initializing the buffer used to store the register context, i.e., by defining offsets within the buffer for each register to be saved (72). Registers which may be 64 bits long should have the offsets doubleword-aligned. In this example, only the GPRs, the condition register (CR), the fixed-point exception register (XER), and the DSISR register are saved. After initialization, the lower 32-bits of all the GPRs are saved in the buffer (74) using a 32-bit storage instruction ("stw"). The lower 32-bits of certain registers are then cleared, to be used by the context-saving code, e.g., three of the general purpose registers (76). The contents of certain other registers (such as the CR and XER registers) are stored in these other available registers (78).
The context-saving code is then prepared to determine whether the processor is 64-bits or 32-bits and, if 64-bits, to further determine what mode it is operating in. In this example the determination begins by examining the overflow field in the XER register, which can be used to indicate if the processor is both 64-bits and in 64-bit mode (80). If this is not the case, then the processor is either 32-bits, or is 64-bits operating is 32-bit mode but, in either case, the 32-bit storage instructions previously executed were sufficient to save the register context of the GPRs, and so all that remains is for the other registers to be saved. First, the CR and XER registers are restored (82), and then these registers, along with the DSISR register, are saved using the 32-bit storage instruction (84). A flag in the buffer may then be set (86) indicating a 32-bit context, to be used for saving other registers (88) as discussed further below.
Returning to step 80, if it is determined that the processor is 64-bits and operating in 64-bit mode, then the 64-bit registers are saved to the buffer (90) using a 64-bit storage instruction ("std"). For example, all of the GPRs, except the three that were set aside for local storage, are saved. Then, the CR and XER registers must be restored (92) and these registers along with the DSISR register saved (94), as with the other branch of FIG. 4. Next, since the lower 32-bits of the three GPRs used by the context-saving code have been modified, they must also be restored (96) prior to being saved (98). The flag in the buffer may then be set (100) indicating a 64-bit context. Other registers, such as the SPRs, can then be easily saved based upon the setting of the flag stored in the buffer (88). This flag can also be used by the code which restores the saved context from the buffer back to the registers.
Assembly language code for implementing the foregoing example is included herewith as an appendix. All rights to any copyright subsisting in this code which may exist separate and apart from any patentable subject matter are reserved.
The foregoing method provides a solution for saving the context of a computer processor's registers which efficiently performs the context-saving operation regardless of the particular register architecture and mode of operation. Additionally, it can easily be adapted to support future register architectures as well as existing ones. Finally, this approach does not cause or use any system exception.
Although the invention has been described with reference to specific embodiments, this description is not meant to be construed in a limiting sense. Various modifications of the disclosed embodiment, as well as alternative embodiments of the invention, will become apparent to persons skilled in the art upon reference to the description of the invention. It is therefore contemplated that such modifications can be made without departing from the spirit or scope of the present invention as defined in the appended claims.