The disclosed embodiments of the present invention relate to handling an operating system mode switching operation, and more particularly, to a method for handling mode switching with less unnecessary register data access and a related non-transitory machine readable medium.
Processors are key components required by a variety of electronic devices. For example, an operating system (OS) can be executed by a processor of an electronic device to control execution of application software for performing the user's desired data processing. The operating system may switch between different operating modes, such as a user mode and a kernel mode. It is desirable that the switching from a first operating system mode to a second operating system mode should be reversible in that when a return is made to the first operating system mode due to exit of the second operating system mode, such that the first operating system mode will continue as if it had not been interrupted. In order to achieve such reversibility, it is necessary that the contents of various registers (e.g., registers inside the processor) should be saved upon leaving the first operating system mode so that they can be restored after the second operating system mode has finished its use of the registers (e.g., registers inside the processor) and control is returned to the first operating system mode. This is conventionally achieved by saving the register data of processor registers in the first operating system mode to an area of a stack memory allocated in an external dynamic random access memory (DRAM) upon leaving the first operating system mode and then returning these saved register data from the area of the stack memory to the processor registers upon returning to the first operating system mode.
A conventional mode switching handling approach is to save and restore contents of all registers used by the processor. However, a problem with this conventional approach is that operations of writing to and subsequently reading from the stack memory are relatively slow, which inevitably degrades the performance of the processor. Thus, there is a need for an innovative mode switching handling approach which is capable of avoiding/reducing the unnecessary register data access to speed up the mode switching operation.
In accordance with exemplary embodiments of the present invention, a method for handling mode switching with less unnecessary register data access and a related non-transitory machine readable medium are proposed.
According to a first aspect of the present invention, an exemplary mode switching handling method is disclosed. The exemplary mode switching handling method includes: when an operating system mode is switched from a first mode to a second mode, saving only a portion of register data that are stored in registers into a storage device, wherein an M-bit register length is used in the first mode, an N-bit register length is used in the second mode, and M and N are different integers.
According to a second aspect of the present invention, an exemplary mode switching handling method is disclosed. The exemplary mode switching handling method includes: when an operating system mode is switched from a second mode to a first mode, restoring a saved register data set in a storage device to only a portion of a storage space of registers, wherein an M-bit register length is used in the first mode, an N-bit register length is used in the second mode, and M and N are different integers.
According to a third aspect of the present invention, an exemplary non-transitory machine readable medium having a program code stored therein is disclosed. When executed by a processor, the program code causes the processor to execute following step: when an operating system mode is switched from a first mode to a second mode, saving only a portion of register data that are stored in registers into a storage device, wherein an M-bit register length is used in the first mode, an N-bit register length is used in the second mode, and M and N are different integers.
According to a fourth aspect of the present invention, an exemplary non-transitory machine readable medium having a program code stored therein is disclosed. When executed by a processor, the program code causes the processor to execute following step: when an operating system mode is switched from a second mode to a first mode, restoring a saved register data set in a storage device to only a portion of a storage space of registers, wherein an M-bit register length is used in the first mode, an N-bit register length is used in the second mode, and M and N are different integers.
These and other objectives of the present invention will no doubt become obvious to those of ordinary skill in the art after reading the following detailed description of the preferred embodiment that is illustrated in the various figures and drawings.
Certain terms are used throughout the description and following claims to refer to particular components. As one skilled in the art will appreciate, manufacturers may refer to a component by different names. This document does not intend to distinguish between components that differ in name but not function. In the following description and in the claims, the terms “include” and “comprise” are used in an open-ended fashion, and thus should be interpreted to mean “include, but not limited to . . . ”. Also, the term “couple” is intended to mean either an indirect or direct electrical connection. Accordingly, if one device is coupled to another device, that connection may be through a direct electrical connection, or through an indirect electrical connection via other devices and connections.
The processor 102 may have a plurality of registers REG0-REGn included therein. When the processor 102 is an N-bit processor, most or all of the registers REG0-REGn may be N-bit registers. For example, the processor 102 may be a 64-bit ARM-based processor, and most of the registers REG0-REGn may be 64-bit registers. It should be noted that the number of registers REG0-REGn implemented in the same processor 102 may depend on the actual processor architecture of the processor 102. In this embodiment, the proposed method for handling mode switching with less unnecessary register data access may be applied to registers REG0-REGn inside the processor 102. However, this is for illustrative purposes only, and is not meant to be a limitation of the present invention. Alternatively, the proposed method for handling mode switching with less unnecessary register data access may be applied to registers used by any processing circuit and/or registers outside the processor 102.
The non-transitory machine readable medium 104 may be arranged to store a program code PROG. The program code PROG may be part of an operating system (OS) such as a Linux-based OS, and may be loaded and executed by the processor 102 to deal with a mode switching operation of the operating system mode. The storage device 106 may be arranged to have a stack memory allocated therein. Hence, when the operating system mode is switched from a first mode to a second mode, the instruction execution in the first mode may be interrupted, and the program code PROG running on the processor 102 may save register data of the processor 102 into the stack memory, such that a saved register data set DATAREG may be available in the storage device 106. When the operating system mode is switched from the second mode to the first mode, the program code PROG running on the processor 102 may restore the saved register data set DATAREG in the stack memory to the processor 102, thus enabling continued instruction execution in the first mode.
When the processor 102 is an N-bit processor, the processor 102 may be configured to operate in one of an N-bit mode, an N/2-bit mode, an N/4-bit mode, . . . , and an one-bit mode. Hence, it is possible that the processor 102 may operate in an N-bit mode (in which an N-bit register length may be used), and may operate in an M-bit mode (in which an M-bit register length may be used), where N and M may be different integers. Since the register utilization of the N-bit mode of the processor 102 may be different from the register utilization of the M-bit mode of the processor 102, saving/restoring the full register data of all registers REG0-REGn of the processor 102 may lead to unnecessary register data access as well as unnecessary storage device access. Compared to the conventional mode switching handling approach that saves/restores the full register data of all registers REG0-REGn of the processor 102, the proposed mode switching handling approach may avoid/reduce unnecessary register data access for achieving fast mode switching and reduced power consumption. Further details of the proposed mode switching handling approach are described as below.
By way of example, but not limitation, the processor 102 shown in
Please refer to
When the operating system mode is switched from the first mode (e.g., 32-bit user mode) to the second mode (e.g., 64-bit kernel mode), the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may save only a portion of register data that are stored in registers (e.g., registers REG0-REGn inside the processor 102) into the storage device 106 to serve as the saved register data set DATAREG.
With regard to several examples mentioned hereinafter, it is assumed that the general-purpose registers X0-X30 shown in
In a first exemplary register data saving scheme, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may save a portion of a register data stored in each of the registers REG0-REGn into the storage device 106 to thereby create the saved register data set DATAREG, where a remaining portion of the register data stored in each of the registers REG0-REGn may not be saved in the storage device 106. For example, the portion of the register data may be stored in the lower-half part P2 of the register as shown in
In a second exemplary register data saving scheme, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may save a plurality of register data stored in a portion of the registers REG0-REGn of the processor 102 into the storage device 106 to thereby create the saved register data set DATAREG, where a plurality of register data stored in a remaining portion of the registers REG0-REGn of the processor 102 may not be saved in the storage device 106. For example, the portion of the registers REG0-REGn may be allowed to be used in each of the first mode (e.g., user mode) and the second mode (e.g., kernel mode), and the remaining portion of the registers REG0-REGn may not be allowed to be used in the second mode (e.g., kernel mode) but not the first mode (e.g., user mode).
In a third exemplary register data saving scheme, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may save only a portion of a register data stored in each of a portion of the registers REG0-REGn of the processor 102 into the storage device 106 to thereby create the saved register data set DATAREG. In other words, the third exemplary register data saving scheme may be regarded as having technical features of the first exemplary register data saving scheme and the second exemplary register data saving scheme.
If the operating system mode is switched from the second mode (e.g., kernel mode) to the first mode (e.g., user mode) due to exit of the second mode (e.g., kernel mode), the program code PROG running on the processor 102 may restore the saved register data set DATAREG in the storage device 106 to the processor 102 for resuming the instruction execution of the 32-bit application in the first mode (e.g., user mode). Hence, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may further perform a restore operation when the operating system mode is switched from the second mode (e.g., kernel mode) to the first mode (e.g., user mode). For example, information recorded in program status registers (e.g., SPSRs) may be checked to decide whether the processor is operated in an N-bit instruction mode or an M-bit instruction mode and to decide whether the mode switching returning to a “USR” processor mode from an “SVC” processor mode occurs. However, this is for illustrative purposes only, and is not meant to be a limitation of the present invention. In practice, any means capable of checking if an operating system mode returns to a short-bit mode from a long-bit mode may be adopted by the proposed mode switching handling approach.
When the operating system mode is switched from the second mode (e.g., kernel mode) to the first mode (e.g., user mode), the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may restore the saved register data set DATAREG in the storage device 106 to only a portion of a storage space of registers (e.g., registers REG0-REGn inside the processor 102).
In a first exemplary register data restoring scheme, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may restore a register data included in the saved register data set DATAREG (which may be saved according to the first exemplary register data saving scheme) to a portion of each register in the registers REG0-REGn of the processor 102, where no register data included in the saved register data set DATAREG (which may be saved according to the first exemplary register data saving scheme) may be restored to a remaining portion of each register in the registers REG0-REGn of the processor 102. For example, the portion of the register may be the lower-half part P2 of the register as shown in
In a second exemplary register data restoring scheme, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may restore a plurality of register data included in the saved register data set DATAREG (which may be saved according to the second exemplary register data saving scheme) to a portion of the registers REG0-REGn of the processor 102, wherein no register data included in the saved register data set DATAREG (which may be saved according to the second exemplary register data saving scheme) may be restored to a remaining portion of the registers REG0-REGn of the processor 102. For example, the portion of the registers REG0-REGn may be allowed to be used in each of the first mode (e.g., user mode) and the second mode (e.g., kernel mode), and the remaining portion of the registers REG0-REGn may be allowed to be used in the second mode (e.g., kernel mode) but not the first mode (e.g., user mode).
In a third exemplary register data restoring scheme, the program code (e.g., mode switching handling program) PROG running on the processor (e.g., 64-bit processor) 102 may restore a plurality of register data included in the saved register data set DATAREG (which may be saved according to the third exemplary register data saving scheme) to only a portion of each register in a portion of the registers REG0-REGn of the processor 102. In other words, the third exemplary register data restoring scheme may be regarded as combining the technical features of the first exemplary register data restoring scheme and the second exemplary register data restoring scheme.
When one of the aforementioned exemplary register data saving schemes and one of the aforementioned exemplary register data restoring scheme are employed, some or all of the register access may be avoided to reduce the time needed for writing register data into the storage device 106 and reading register data from the storage device 106.
Step 1302: Is an operating system mode switched from a first mode (e.g., a user mode in which an M-bit register length is used) to a second mode (e.g., a kernel mode in which an N-bit register length is used, where N>M)? If yes, go to step 1304; otherwise, go to step 1302 to wait for occurrence of the operating system mode switching from a short-bit mode to a long-bit mode.
Step 1304: Save only a portion of register data that are stored in registers into a storage device, such that a saved register data set is available in the storage device.
Step 1306: If the operating system mode is switched from the second mode (e.g., kernel mode in which the N-bit register length is used) to the first mode (e.g., user mode in which the M-bit register length is used, where M<N)? If yes, go to step 1308; otherwise, go to step 1306 to wait for occurrence of the operating system mode switching from the long-bit mode to the short-bit mode.
Step 1308: Restore the saved register data set in the storage device to only a portion of a storage space of registers.
As a person skilled in the art can readily understand details of each step shown in
In above exemplary embodiments, the processor 102 may be a 64-bit processor (e.g., a 64-bit ARM-based processor), the first mode may be an M-bit operating system mode (e.g., a 32-bit user mode), and the second mode may be an N-bit operating system mode (e.g., a 64-bit kernel mode). However, these are for illustrative purposes only, and are not meant to be limitations of the present invention. In practice, the proposed mode switching handling approach may be applied to any mode switching between a short-bit mode and a high-bit mode for avoiding some or all unnecessary data access in a storage device during the save phase and the restore phase of the mode switching.
Those skilled in the art will readily observe that numerous modifications and alterations of the device and method may be made while retaining the teachings of the invention. Accordingly, the above disclosure should be construed as limited only by the metes and bounds of the appended claims.
This application claims the benefit of U.S. provisional application No. 62/045,082, filed on Sep. 3, 2014 and incorporated herein by reference.
Filing Document | Filing Date | Country | Kind |
---|---|---|---|
PCT/CN2015/088549 | 8/31/2015 | WO | 00 |
Number | Date | Country | |
---|---|---|---|
62045082 | Sep 2014 | US |