The second week of the Linux kernel analysis experiment

Source: Internet
Author: User
Tags volatile

Wang Yi Original Works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

The core of this lesson is to complete the scheduling of the process through the interrupt mechanism, in this course, __init My_start_kernel as an entry function, define the T-PCB structure of process No. 0, copy to make other process TPCB data structure, The interrupt Time function is My_timer_handler periodic call to modify the value of the my_need_sched, and the No. 0 process has been detecting the state of my_need_sched, and when the state changes, call the My_schedule () function to switch the process, During the process switching process, the assembly code is used. The following analysis of the code:

1, Mypcb.h

struct Thread

{

Unsigned long IP; //

Unsigned long SP; In the current process, the address that ESP points to

}

typedef struct PCB

{

int pid;

volatile long state; /-1 unrunnable, 0 runnable,>0 stopped*/

Char Stack[kernel_stack_size];

struct thread thread;

unsigned long task_entry;

struct Pcb*next;

}TPCB;

void My_schedule (void);//Scheduler

In this header file, a thread structure and a TPCB structure are defined, and the function of the thread struct is to store the address of the ESP and EIP for the corresponding process. Its role is to save the current process of ESP and EIP, when the current process will be loaded into the corresponding ESP and EIP, when switching out, will be in the ESP and EIP values stored in the structure. The role of TPCB is to store information about the corresponding process.

2, MYMAIN.C

void __init My_start_kernel (void)

{int PID = 0;

int i;

/* Initialize Process 0*/

Task[pid].pid = pid; Initialize process 0 Here

Task[pid].state = 0;/*-1 unrunnable, 0 runnable, >0 stopped * *

Task[pid].task_entry = Task[pid].thread.ip = (unsigned long) my_process; Ingress handler function

task[pid].thread.sp= (unsigned long) &task[pid].stack[KERNEL_STACK_SIZE-1]; The top of the stack

Task[pid].next = &task[pid]; Ring Linked List

/*fork More Process */

for (i=1;i<max_task_num;i++)

{

memcpy (&task[i],&task[0],sizeof (TPCB)); Making a process from replication

Task[i].pid = i;

TASK[I].THREAD.SP = (unsigned long) &task[i].stack[KERNEL_STACK_SIZE-1];

Task[i].next = Task[i-1].next; Join Process List

Task[i-1].next = &task[i];

}

/* START process 0 by task[0] */

PID = 0;

My_current_task = &task[pid];

ASM volatile (

"MOVL%1,%%esp\n\t"/* Set TASK[PID].THREAD.SP to ESP, loading process sp*/

"PUSHL%1\n\t"/* Push EBP */

"PUSHL%0\n\t"/* push Task[pid].thread.ip, using these two statements to load IP, point to function entry */

"Ret\n\t"/* pop task[pid].thread.ip to EIP */

"Popl%%ebp\n\t"

:

: "C" (Task[pid].thread.ip), "D" (TASK[PID].THREAD.SP)/* input C or D mean%ecx/%edx*/

); Start Process # No. 0

void my_process (void)

{

int i = 0;

while (1)

{

i++;

if (i%10000000 = = 0)

{

PRINTK (Kern_notice "This is process%d-\n", my_current_task->pid);

if (my_need_sched = = 1)

{

my_need_sched = 0;

My_schedule ();

}

PRINTK (Kern_notice "This is process%d +n", my_current_task->pid);

}

The above code is the initialization process and to start the function of the 0 good process, in which the following assembly part is through the TPCB structure of the SP loaded into the ESP,EBP and IP loaded into the EIP to start the thread, because the EIP can not be directly assigned, through the push and RET structure to load the EIP. The following my_process function is the handler for all processes to verify the my_need_sched state to invoke the process switch function.

3. Process switching

void My_schedule (void)

{

TPCB * NEXT;

TPCB * PREV;

if (My_current_task = = NULL

|| My_current_task->next = = NULL)

{

Return

}

PRINTK (Kern_notice ">>>my_schedule<<<\n");

/* Schedule */

Next = my_current_task->next;

prev = My_current_task;

if (next->state = = 0)/*-1 unrunnable, 0 runnable, >0 stopped * *

{

My_current_task = Next;

PRINTK (kern_notice ">>>switch%d to%d<<<\n", prev->pid,next->pid);

/* Switch to Next process */

ASM volatile (

"PUSHL%%ebp\n\t"/* Save EBP */

"Movl%%esp,%0\n\t"/* Save ESP */

"Movl%2,%%esp\n\t"/* Restore ESP */

"Movl $1f,%1\n\t"/* Save EIP */

"Pushl%3\n\t"

"Ret\n\t"/* Restore EIP */

"1:\t"/* Next process start here */

"Popl%%ebp\n\t"

: "=m" (PREV->THREAD.SP), "=m" (PREV->THREAD.IP)

: "M" (NEXT->THREAD.SP), "M" (NEXT->THREAD.IP)

);

My_current_task = Next;//switch to the next task

PRINTK (kern_notice "switch from%d process to%d process\n >>>process%d running!!! <<<\n\n ", prev->pid,next->pid,next->pid);

}

Else

{

next->state = 0;

My_current_task = Next;

PRINTK (kern_notice "switch from%d process to%d process\n >>>process%d running!! !<<<\n\n\n ", prev->pid,next->pid,next->pid);

/* Switch to New process */

ASM volatile (

"PUSHL%%ebp\n\t"/* Save EBP */

"Movl%%esp,%0\n\t"/* Save ESP */

"Movl%2,%%esp\n\t"/* Restore ESP */

"Movl%2,%%ebp\n\t"/* Restore EBP */

"Movl $1f,%1\n\t"/* Save EIP */

"Pushl%3\n\t"

"Ret\n\t"/* Restore EIP */

: "=m" (PREV->THREAD.SP), "=m" (PREV->THREAD.IP)

: "M" (NEXT->THREAD.SP), "M" (NEXT->THREAD.IP)

);

}

Return

}//end of My_schedule

In the process switching function, it is judged by the state of the process to be switched to, depending on whether the process has been run, if it has been run, to directly save the EBP,ESP and EIP of the previous process, to load the EIP and ESP of the next process. If it is not running, add ESP and EBP to the address of the SP.

4, the computer's three magic weapon

Stored program computers

function Call stack

Interrupt

5, the operating system two swords

Interrupt Handling

Process switching

Is the result of the run:

The second week of the Linux kernel analysis experiment

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.