Friday, September 7, 2012

context switch



Kernel can run for itself. It cannot access the user space.
PTBR will point to swapper_pg_dir.

It can also run on the behalf of a user process.
In this case kernel can access the user space of that particular process.
PTBR has the pgd of that particular process.

A process will have two stacks.
one in user space and one in kernel space.
If process does system call then kernel space stack is used.

context switch is not needed when prev == next;
ie.. no switch is needed when switching between 2 kernel threads.

kernel -> kernel ---> no switch
user1 -> user2   ---> full context switch
kernel -> user   ---> kernel->active = NULL
user -> kernel   ---> kernel->active = prev->active

Basically when switching from user to kernel , there is no need of full switch.
Use the previous process mm and do lazy_tlb.
While swtiching from kernel to user , make the active_mm to NULL.
active_mm is always used by the arch specific code for pagetable operations.

code after switch_to is executed only after the process is selected to run next time.
barrier() is present after switch_to to prevent any compiler interleavings.

when switch_to returns prev will be pointing to the real previous task.

A->B , B->C , C->A

              b4 switch_to  after switch_to
prev=a prev=b prev=b | prev=c
next=b next=c next=a | next=a


To switch between tasks requires the following steps:
1. Save the active task context and place the task in a dormant state.
2. Flush the caches; possibly clean the D-cache if using a writeback policy.
3. Flush the TLB to remove translations for the retiring task.
4. Configure the MMU to use new page tables translating the virtual memory execution
area to the awakening task’s location in physical memory.
5. Restore the context of the awakening task.
6. Resume execution of the restored task.

No comments:

Post a Comment