Simple kernel scheduling design--based on the implementation of TQ2440 (ARM9) (3) __ Kernel

Source: Internet
Author: User

Author: Jiang Guangwei
Date: 2010-12-28
Email: guangwei.jiang@gmail.com/jgw2008@126.com 1. Introduction

The theme of this chapter is "The design and implementation of Kernel Scheduler", "Kernel scheduling" is the necessary module of all multitasking kernel, the design of scheduling algorithm has a direct impact on the efficiency of the whole system.
Examples of this chapter, do not consider the system "performance", just to show you how to design and implement a simple kernel scheduler, we use the simplest "rotation scheduling", we can try to use other algorithms to optimize the performance of the kernel scheduling.

First, let's take a look at the function of this program (kernelsched):
Task1, flashing LED1 lamp;
Task2, flashing LED2 lights.
The scheduler schedules the TASK1/TASK2 according to the interrupt generated by the clock.

Experimental Environment Description:
The Operation platform of this experiment is TQ2440 (ARM9) Development Board, the program code runs in the 4KB internal ram of s3c2440 "Steppingstone". The program is to use this 4KB of space to achieve multitask switching.
Please select start from NAND flash and burn the generated binaries into NAND flash.
When you choose to start the CPU from NAND flash, the CPU copies the 4KB byte data from NAND flash to the 4KB internal ram of "Steppingstone" via internal hardware (at which point the starting address for internal RAM is 0), and then jumps to address 0 to begin execution.

Let's take a look at the code for TASK1 and Task2,

2. Detailed design/implementation Section

The program is divided into 3 parts,
– Initialize
– Storage model
– Kernel dispatch 2.1 initialization

After the CPU first power up, jump to the interrupt vector table to perform the reset branch.
A. Because to call the C function, you must first set the stack in the SVC mode (after the CPU reset, the default into the SVC mode);
B. In order to prevent the CPU continuously automatically restart, need to close watchdog;
C. Set the stack pointer in the interrupt/system mode respectively;
D. Initialization of CLOCK/LED/TIMER0/IRQ, open IRQ interrupt;
E. Initialize the "next task" to point "Task 1", about the scheduling algorithm, which will be mentioned later;
F. Finally, start the first task Task1.

The initialization code (_START) is as follows,

2.2 Storage Model

Code Storage Area: 0X00000000~0X000007FF (2KB);
IRQ Stack Area: 0x00000800~0x000009ff (512B);
Task2 Stack Area: 0x00000c00~0x00000e03 (516B);
Task1 Stack Area: 0x00000e04~0x00000fa3 (416B);
TCB Pointer Storage area: 0X00000FA4~0X00000FFF (92B).

Attention:
A. The growth direction of the stack from high address to low address;
B. TCB pointer storage area, storing the task stack top pointer, such as 0X0000FF4 save Task1 stack top pointer, 0x00000ff0 save TASK2 stack top pointer.

The storage partitioning chart is as follows,

2.3 Kernel Scheduling

Before you run a new task, you must initialize the PCB (Process control block) for each new task. The PCB is a data structure that remains in RAM and is used to hold a copy of all arm registers (see table below).

Task control Blocks (PCB, Process controls block)

In the test code, Task 1 is the main program and runs first and does not need to initialize the PCB.
Task 2 before running, you need to initialize the PCB, initialization code as follows,

The 3 parameters of the function "Taskcreate" have the following meanings:
void (*task) (void): Task address
unsigned long *p_stack:task stack pointer
unsigned long *task_stk_ptr: Saves the address of the top pointer on the Task stack

first, explain the principle of task scheduling.
We use the simplest "rotation method", ie task1->task2->task3-> ...
Before running the first task, first initialize the next task, pointing to Task 1;
During task switching, you calculate the point of current task and next task:
"Current Task" is the "next task" before the previous dispatch;
The next task is the ID plus 1 for current task, and if current task is Task 1, next task is Task 2. If the ID of the next task is greater than the total number of tasks (we can also call it "out of Bounds"), then "next task" points to the first task.

then, the function of two variables is introduced.
Taskcur_stk_ptr: Saves the address of the current task stack pointer;
Tasknext_stk_ptr: Saves the address of the next task stack pointer.

then, describe the address saved by the task x stack pointer.
In our practice, in order to simplify, the task x stack pointer is kept in a contiguous address space.
For example, the address that holds the TASK1 stack pointer is 0x00000ff4; The address of the save Task2 stack pointer is 0x00000ff0.

Here is the concrete implementation,
the first stage, dealing with Timer0 interrupts. We require that after the TIME0 interrupt occurs, the kernelsched is executed instead of returning the function before the interrupt occurred.
First, the register of the former mode is pressed stack;
Then, save the PC pointer before the interruption, for later use;
Then, in the stack, replace the PC pointer before the interrupt occurs with kernelsched, so that the interrupt can jump directly to the kernelsched;
Finally, register value out stack, PC jumps to kernelsched.

In the second phase, you save the context in which the current task runs.
A. First of all, the "current task" register value pressure stack;
B. Then, remove the PC value before the interruption occurs and press it into the stack;
C. Then compute the address that holds the "current task" stack pointer;
D. Finally, save the top of the stack pointer for the current task.

In the third phase, restore the running context of the next task.
A. First, compute the "next task" stack pointer;
B. Then, remove the "next task" on the top of the stack pointer;
C. Finally, the registers are sequentially out of the stack.

This article source code can download from author's github, link below
Https://github.com/GuangweiJiang/diy_kernel/tree/master/03_KernelSched

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.