Disclosure of Invention
In order to accelerate the task context switching speed of an operating system, the invention provides an operating system task context management method based on a TriCore architecture processor, and the technical scheme adopted by the method comprises two steps of task context initialization and task context switching:
(1) Task context initialization
Constructing a task context structure body according to a register format of the TriCore architecture processor, dividing 32 registers into two groups of contexts, and storing 16 register data in each group of contexts;
two context storage areas are allocated for each task, and a stack field of a task control block is set to be a base address of a second context storage area; the link word of the second context memory area points to the first context memory area;
when a task is initialized, a task stack address is stored in a first set of contexts, and a return address is stored in a second set of contexts;
(2) The context switching of the current task and the new task is executed, and the specific flow is as follows:
(2.1) automatically saving register data for a first set of contexts to a first context store of a current task when the operating system invokes the task context switch function;
(2.2) executing the svlcx instruction to save register data for a second set of contexts to a second context storage area of the current task;
(2.3) writing the PCXI register data into a task control block stack field of the current task;
(2.4) writing the task control block stack field data of the new task into the PCXI register;
(2.5) executing the rslcx instruction to fetch the register data of the second set of contexts from the second context memory area of the new task, writing into the corresponding registers of the second set of contexts, the PCXI register value becoming the first context memory area base address;
(2.6) executing rfe the instruction fetches the register data of the first set of contexts from the first context memory region of the new task, writes into the corresponding registers of the first set of contexts, reads the return address register, and jumps to the return address to begin executing the new task.
Further, only the PCXI register data needs to be modified by software in the task context switching process, the register stores the task context storage area base address, and the register values required by the running of other tasks are updated by hardware quickly.
Further, the return address of the second set of contexts is set to a task entry function during task context initialization, to store the program run address of the task when the task is preempted, and to jump to the address for continued execution when the task context is restored by executing rfe instructions.
Further, when the task runs, two sets of context register data are written into corresponding processor registers, two context storage areas are released, and the PCXI register value is invalid.
Further, when the task is preempted, two context storage areas are reassigned to store two sets of contexts of the task respectively, and a task control block stack field of the preempted task is set to a base address of the second context storage area.
Further, assembler instructions svlcx, rslcx and rfe complete the saving and restoring of task contexts according to the task context storage area pointed to by the PCXI register.
Further, the first set of contexts of the task may also store the address of a security check function, where the security check function automatically jumps to execute when the task ends, and the function releases the system resources occupied by the task and executes corresponding security check.
Further, the first set of context registers of the task includes a program running state PSW register, which can manage the processor privilege level of the task as required during task switching.
The beneficial effects of the invention are as follows: by adopting the method to manage the task objects of the operating system running on the processor of the English-flying-ice TriCore architecture, the supporting characteristic of the processor for switching the task context of the operating system can be fully utilized, the most efficient task switching operation is realized by using the least task context storage space, the real-time performance of the operating system is improved, and the application occasion with higher real-time requirement can be met.
Detailed Description
In order that the above objects, features and advantages of the invention will be readily understood, a more particular description of the invention will be rendered by reference to the appended drawings.
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present invention, but the present invention may be practiced in other ways other than those described herein, and persons skilled in the art will readily appreciate that the present invention is not limited to the specific embodiments disclosed below.
As shown in fig. 1, the method for managing task context of an operating system based on a TriCore architecture processor according to the embodiment of the present application includes two steps of task context initialization and task context switching:
(1) Task context initialization
Constructing a task context structure body according to a register format of the TriCore architecture processor, dividing 32 registers into two groups of contexts, and storing 16 register data in each group of contexts;
two context storage areas are allocated for each task, and a stack field of a task control block is set to be a base address of a second context storage area; the link word of the second context memory area points to the first context memory area;
when a task is initialized, a task stack address is stored in a first set of contexts, and a return address is stored in a second set of contexts;
(2) When the operating system enters a task scheduling point, a new task is selected to be subjected to context switching with the current task, and the specific flow is as follows:
(2.1) automatically saving register data for a first set of contexts to a first context store of a current task when the operating system invokes the task context switch function;
(2.2) executing the svlcx instruction to save register data for a second set of contexts to a second context storage area of the current task;
(2.3) writing the PCXI register data into a task control block stack field of the current task;
(2.4) writing the task control block stack field data of the new task into the PCXI register;
(2.5) executing the rslcx instruction to fetch the register data of the second set of contexts from the second context memory area of the new task, writing into the corresponding registers of the second set of contexts, the PCXI register value becoming the first context memory area base address;
(2.6) executing rfe the instruction fetches the register data of the first set of contexts from the first context memory region of the new task, writes into the corresponding registers of the first set of contexts, reads the return address register, and jumps to the return address to begin executing the new task.
In this embodiment, the return address of the second set of contexts is set as a task entry function during the task context initialization process, and is set to store the program running address of the task when the task is preempted, and the execution rfe instruction jumps to the address to continue execution when the task context is restored.
In the embodiment of the application, when the task runs, two sets of context register data are written into corresponding processor registers, two context storage areas are released, and the PCXI register value is invalid. When the task is preempted, the two context storage areas are reassigned to store two groups of contexts of the task respectively, and the task control block stack field of the preempted task is set as the base address of the second context storage area.
As shown in fig. 2, the stack field in the task control block is set at the start address of the task control block structure, and is set as the base address of the second context storage area in the process of initializing the task; the two context memory areas are linked by a link word, and the link word of the second context memory area points to the first context memory area.
As shown in FIG. 3, the two sets of task contexts store processor register data necessary for the execution of a portion of the task, respectively, the first set of contexts stores PCXI, PSW, A, A11, D8, D9, D10, D11, A12, A13, A14, A15, D12, D13, D14 and D15, wherein A10 stores the task stack address, A11 can store the security check function address, and the processor privilege level of the task can be managed by the PSW. The second set of contexts stores PCXI, a11, A2, A3, D0, D1, D2, D3, A4, A5, A6, A7, D4, D5, D6, and D7, where a11 stores the last run-time program address of the task, set to the task entry function address at task initialization.
As shown in fig. 4, the task context switching function is implemented by an assembler instruction, when the operating system needs to perform task switching, a new task is selected according to a preset policy, in this embodiment, a task control block pointer of the new task is written into an A5 register as a parameter, a task control block pointer of a current task is written into an A4 register, and then the current task context is saved according to a flow, and execution of the new task is started.
The following provides an implementation procedure of a specific example of the present application:
1) And modifying the task control block structure body, setting a task stack field as a first field of the structure body, and constructing two groups of task context data structures.
2) Two task context storage areas are respectively allocated for each task in a task context initializing function, a first group of contexts A10 is set as a task stack address, A11 is set as a security check function address, and the function jumps when a return instruction is executed after the task is executed, so that the security detection of an operating system is performed. Setting the task entry function address in the second set of contexts a11, and jumping to the task entry function by the task switch function execution return instruction when the task is first run.
3) Modifying the task switch function according to fig. 4, introducing assembler instructions provided by TriCore: svlcx, rslcx and rfe make full use of the support mechanism of the processor for task switching.
The previous description of the embodiments is provided to facilitate a person of ordinary skill in the art in order to make and use the present invention. It will be apparent to those having ordinary skill in the art that various modifications to the above-described embodiments may be readily made and the generic principles described herein may be applied to other embodiments without the use of inventive faculty. Therefore, the present invention is not limited to the above-described embodiments, and those skilled in the art, based on the present disclosure, should make improvements and modifications within the scope of the present invention.