SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
¯

(context switch)   –4
¯
         Linux- (context switch)
                     Linux-
                     Linux-




)c2003   (              ¯   -   4       2
?
                        quot;       - quot;           ¯
                            ¯
                   quot;   quot;–
                                                                  ¯
         (execution context) quot;                            quot;               ¯
                                                              ¯       ¯
                        , ¯         ¯ ,               ,       ,
                   ¯

               quot;              ¯ –                     ¯
                                                                  quot;
)c2003     (                         ¯    -       4                           3
Linux-
                   ¯                                  Linux
                                .kernel mode-
          ,
         kernel-
         kernel
                      .user mode-                     ,mode
         ,non-preemptive    Linux           kernel-
                                        :
                          context switch-
                             context switch


)c2003      (               ¯   -   4                         4
context switch
          time slice-         ¯              –                     .1
                                   .(scheduler_tick()          )
                                          –                        .2
                   ¯,          ¯      ¯            )
                                                         (     ¯
 ¯,                                                ¯               .3
                                   .
.sched_yield()   ¯ ¯                                           ¯   .4
      need_resched
               .user mode                                ,
                                                        4- 3


 )c2003   (                            ¯   -   4                        5
,Linux-
                                .schedule()
                                                schedule()
                                            .
                                                schedule()
                                                       ¯
                         context_switch()
                                                   .
                 -
         ,
                     ¯
                                                       ¯


)c2003       (                  ¯   -   4                    6
,Linux-
                                             .
         :
                      . ¯                                .1
                 )                                       .2
                            .(
                             (           ¯
                 .      ¯                                .3
             ¯                                   ¯       .4
                                                     .




)c2003       (                   ¯   -   4                    7
Linux-
  ¯                                      Linux-
             .                   ¯                 quot;       quot; quot;          quot;

                                                           –
                                                                        thread
                     ¯                            ¯
                     ¯                         :User Mode-

         ,               ¯                                          ,

cs, )                                ¯
                     .       ¯                 ¯                 ,(ds, ss, es
                                                       ¯

)c2003           (                         ¯   -   4                             8
(1) Task State Segment
          ,                ¯                    Task State Segment – TSS
               .                            ¯                                            ¯
                                        .         TSS                 Linux-                 ¯
                               (IA32)
TSS-                   ¯                                               ¯
                                                               Kernel Mode-
         I/O                                ¯                                     TSS-
                        .init_tss                          ¯                  ¯    TSS-
                       .TSS-                       ¯           ,




)c2003             (                               ¯   -        4                                9
(2) Task State Segment

                   include/asm/processor.h
struct tss_struct {
..
unsigned long esp0;                             ¯
                        (kernel mode- ¯   esp       )
..
};
struct tss_struct init_tss[NR_CPUS];


)c2003   (             ¯   -   4                        10
thread

                           thread_struct
                               :include/asm/processor.h
struct thread_struct {
   unsigned long esp0;                                ¯
   unsigned long eip;                            ¯
   unsigned long esp;            (       )
   unsigned long fs;
                          gs- fs
   unsigned long gs;
   ..
   union i387_union i387;
   ..
};
 )c2003   (                  ¯   -   4                    11
context_switch()
context_switch()
                                        kernel/sched.c
switch_to                ¯

         .                              context_switch()
                                           t t    it h()
                   switch_to           2.4.X

inline task_t *context_switch(task_t *prev, task_t *next) {
  switch_mm.... ¯
   switch_to(prev, next, prev);
   return prev;
}


)c2003      (                  ¯   -     4                 12
(1) switch_to
            include/asm/system.h
         switch_to(prev, next, last) :
                            , ¯                     prev
                              ,                     next
             .                               ¯       last
          switch_to-                         ,     2.4.X
                    ¯      switch_to
movl prev, %eax
movl next, %edx
pushl %esi                                 (gcc)
                         ¯ switch_to
pushl %edi
pushl %ebp
)c2003     (                 ¯    -    4                    13
(2) switch_to
movl %esp, prev->thread.esp                 – next        prev
movl next->thread.esp, %esp
movl $1f, prev->thread.eip                  ¯          prev
                                                        ¯ quot; 1quot;
pushl next->thread.eip                  ¯            next
jmp __switch_to __switch_to                     __switch_to
1:
popl %ebp
popl %edi                   switch_to           next
popl %esi



 )c2003   (                ¯   -   4                             14
(1) __switch_to()

                                arch/i386/kernel/process.c
                                         :
void FASTCALL(__switch_to(struct task_struct *prev_p,
            struct task_struct *next_p));
                                                  FASTCALL

                               :(C               ) GCC-
     #define FASTCALL __attribute__(regparm(3))
         (             ) eax, edx, ecx                          3
                          edx      next_p- eax            prev_p , ¯
                 ?            __switch_to
                       .             , ret_from_fork –
)c2003       (                       ¯   -   4                         15
(2) __switch_to()
     switch_to
                     ¯ (context_switch()                        ,            )
                                                        .                    ¯
jmp __switch_to
               ,                  ¯               __switch_to()-         ¯
    .                                              ¯        ret
                 .        ¯
                         ¯       ¯ next->thread.eip                  ¯
                        '1'            ¯     ,              .switch_to
                           .context_switch()                 switch_to
             ¯                ¯ ,                             next
                                                               .

)c2003   (                            ¯   -   4                                  16
(3) __switch_to()
struct thread_struct *prev = &prev_p->thread, *next = &next_p->thread;
struct tss_struct *tss = init_tss + smp_processor_id();

unlazy_fpu(prev_p);

tss->esp0 = next->esp0;                        ¯
..                             TSS-   ¯
movl %fs, prev->fs
movl %gs, prev->gs
if (prev->fs | prev->gs | next->fs | next->gs) {
   ..
   movl next->fs, %fs                                      gs- fs
   movl next->gs, %gs
   ..
}
/* load debug registers */
/* load IO permission bitmap */
return;

  )c2003   (                      ¯   -   4                         17
....
                         ¯            context switch
                                                 ?
                   ¯C               - context switch()
                                      context_switch()
                                  .kernel-
         ,           ,                     – switch_to
                 .i386
             .           ,C                – __switch_to


)c2003       (                ¯   -    4                   18
prev
context_switch:                                                        task_struct
                                        context_switch()
       ……

                                                              $1f      thread.eip
                                              esi
                                                                       thread.esp
 switch_to:                        prev       edi
                                              ebp
   movl $1f, prev->thread.eip                                       next
                                                                           task_struct
                                        context_switch()
   pushl next->thread.eip
                                              esi               $1f        thread.eip
       __switch_to:
         s tc to:                             edi                          thread.esp
                                                                           thread esp
                                   next
        ………




                                              ebp
                                    esp       $1f
       ret

                            next
                                                           context_switch()
 1:                                                              esi
                                                 eip
                                                                 edi
                                                                 ebp
                                                                 $1f
  19                         4       -       ¯                  2003            (c)
User Mode
          User Mode-
     (2                                  ) Kernel Mode-
                                                     ¯
                                         quot;                            ss:esp
    kernel
                 mode
                                                                      eflags

                                                         ¯             cs:eip

                                   eflags
                                                                  es,ds,eax,
                        SAVE_ALL
                                                             ebp,edi,esi,edx,
                                                                     ecx,ebx
)c2003       (                       ¯       -   4                        20
Kernel Mode
                                 ?
,context_switch()                       ¯                          switch_to

              .switch_to                                             - ebp, esi, edi
     ¯                                  switch_to-                 ebx      – ebx
                                     (context_switch() quot;                  ¯)
                    schedule()               quot;                      - ecx, edx, eax
                                                               switch_to
    caller-               context_switch()                 schedule()
               ¯ ,(2               ¯,                       /            ) callee
              ebx, ebp, esi, edi-                        ¯            schedule()
  .switch_to                                thread_struct                  eip- esp
                    ¯
                    ¯   ¯        quot;                                  ,gs- fs
              TSS           thread_struct.esp0
 )c2003        (                                 ¯   -    4                            21
(1) Linux-
                 ¯                             ¯
                                              (fork()       ¯)
                                            do_fork()
                                  kernel/fork.c
                                        do_fork()

                     ,(file descriptors)
                                                        ¯
             –                          ¯       ¯


)c2003   (                ¯   -     4                            22
(2) Linux-
                                        do_fork()
                     copy_thread()                      quot;
             quot;   ¯
                     SET_LINKS                   quot; quot;
                                          pid-
                     hash_pid()-                    quot;
  -      ¯ TASK_RUNNING
          wake_up_process()-                 quot; runqueue
           pid-         do_fork()                  ,
                                               ,

)c2003   (                  ¯   -   4                       23
Linux-

                 (1) copy_thread()
         ¯            (arch/i386/kernel/process.c               )
             .             thread

                                                                        p
                                                                     regs
                     kernel mode-                   SAVE_ALL -
                              ¯             ¯            struct pt_regs
                 SAVE_ALL -
struct pt_regs* childregs;
                                                                      childregs
childregs = ((struct pt_regs *)(8192 + (unsigned long) p)) – 1;

struct_cpy(childregs, regs);
                       fork()       0                           ,2          ¯¯
childregs->eax = 0;


)c2003       (                      ¯   -       4                            24
(2) copy_thread()
                                                               p->thread.esp
p->thread.esp = (unsigned long) childregs;
                                                              p->thread.esp0
p->thread.esp0 = (unsigned long) (childregs + 1);
                              ret_from_fork
p->thread.eip = (unsigned long) ret_from_fork;
          pushl next->thread.eip                         switch_to     ¯
                                (16           ) .__switch_to
/* save fs and gs registers in thread field */
/* copy saved math regs in thread field from father to son */



 )c2003     (                         ¯   -   4                         25
ret_from_fork()
                                        ¯

     arch/i386/kernel/entry.S
ret_from_fork:
  check need_resched
   …
  jmp ret_from_sys_call
               ret_from_sys_call      ,2             ¯
                      ¯             user mode-

fork()                 ret_from_fork-            ,
                               0

)c2003   (                  ¯   -   4                    26
ss
 user -                                                               p
               mode
                              esp
                             eflags                                         task_struct

                              cs                                  .
                              eip                                 .
           ¯          eax
                            orig_eax                              .
                              es                                            thread.esp0

                                           struct pt_regs
                              ds                            ret_from_fork   thread.eip

                             eax=0                                          thread.esp
                              ebp
SAVE_ALL
                                                                  .
                              edi
                                                                  .
                              esi                                 .
                              edx
                              ecx
                              ebx
  )c2003          (                    ¯   -    4                                27
(1)
 exit() ¯                    quot;
                  ,exit()-
         main()                       exit()-
                                     libc           ,
         -         __libc_start_main()
                                  ¯               main()
                                              ¯

                        ¯,
                                             0-     ¯
                                         -
)c2003       (                   ¯   -   4                 28
(2)
                      ¯                 do_exit()

                                  kernel/exit.c
         :                                        ,

                 ¯     exit_code                      ¯
                            exit() quot;
       ¯                   ¯:                         ¯
                                  init
             TASK_ZOMBIE-
   runqueue-                  ,schedule()-
                    .
                           switch_to
)c2003       (            ¯   -     4                     29
(3)
                                ¯
         wait()   ¯                            ,
                        quot;
                                                release_task()
                                        kernel/exit.c
                            ,        ,
 pid-                 (REMOVE_LINKS) ¯
                               ,(unhash_pid())



)c2003   (                  ¯       -     4                      30

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Kürzlich hochgeladen (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Empfohlen

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Empfohlen (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

R4 Context Switch

  • 2. ¯ Linux- (context switch) Linux- Linux- )c2003 ( ¯ - 4 2
  • 3. ? quot; - quot; ¯ ¯ quot; quot;– ¯ (execution context) quot; quot; ¯ ¯ ¯ , ¯ ¯ , , , ¯ quot; ¯ – ¯ quot; )c2003 ( ¯ - 4 3
  • 4. Linux- ¯ Linux .kernel mode- , kernel- kernel .user mode- ,mode ,non-preemptive Linux kernel- : context switch- context switch )c2003 ( ¯ - 4 4
  • 5. context switch time slice- ¯ – .1 .(scheduler_tick() ) – .2 ¯, ¯ ¯ ) ( ¯ ¯, ¯ .3 . .sched_yield() ¯ ¯ ¯ .4 need_resched .user mode , 4- 3 )c2003 ( ¯ - 4 5
  • 6. ,Linux- .schedule() schedule() . schedule() ¯ context_switch() . - , ¯ ¯ )c2003 ( ¯ - 4 6
  • 7. ,Linux- . : . ¯ .1 ) .2 .( ( ¯ . ¯ .3 ¯ ¯ .4 . )c2003 ( ¯ - 4 7
  • 8. Linux- ¯ Linux- . ¯ quot; quot; quot; quot; – thread ¯ ¯ ¯ :User Mode- , ¯ , cs, ) ¯ . ¯ ¯ ,(ds, ss, es ¯ )c2003 ( ¯ - 4 8
  • 9. (1) Task State Segment , ¯ Task State Segment – TSS . ¯ ¯ . TSS Linux- ¯ (IA32) TSS- ¯ ¯ Kernel Mode- I/O ¯ TSS- .init_tss ¯ ¯ TSS- .TSS- ¯ , )c2003 ( ¯ - 4 9
  • 10. (2) Task State Segment include/asm/processor.h struct tss_struct { .. unsigned long esp0; ¯ (kernel mode- ¯ esp ) .. }; struct tss_struct init_tss[NR_CPUS]; )c2003 ( ¯ - 4 10
  • 11. thread thread_struct :include/asm/processor.h struct thread_struct { unsigned long esp0; ¯ unsigned long eip; ¯ unsigned long esp; ( ) unsigned long fs; gs- fs unsigned long gs; .. union i387_union i387; .. }; )c2003 ( ¯ - 4 11
  • 12. context_switch() context_switch() kernel/sched.c switch_to ¯ . context_switch() t t it h() switch_to 2.4.X inline task_t *context_switch(task_t *prev, task_t *next) { switch_mm.... ¯ switch_to(prev, next, prev); return prev; } )c2003 ( ¯ - 4 12
  • 13. (1) switch_to include/asm/system.h switch_to(prev, next, last) : , ¯ prev , next . ¯ last switch_to- , 2.4.X ¯ switch_to movl prev, %eax movl next, %edx pushl %esi (gcc) ¯ switch_to pushl %edi pushl %ebp )c2003 ( ¯ - 4 13
  • 14. (2) switch_to movl %esp, prev->thread.esp – next prev movl next->thread.esp, %esp movl $1f, prev->thread.eip ¯ prev ¯ quot; 1quot; pushl next->thread.eip ¯ next jmp __switch_to __switch_to __switch_to 1: popl %ebp popl %edi switch_to next popl %esi )c2003 ( ¯ - 4 14
  • 15. (1) __switch_to() arch/i386/kernel/process.c : void FASTCALL(__switch_to(struct task_struct *prev_p, struct task_struct *next_p)); FASTCALL :(C ) GCC- #define FASTCALL __attribute__(regparm(3)) ( ) eax, edx, ecx 3 edx next_p- eax prev_p , ¯ ? __switch_to . , ret_from_fork – )c2003 ( ¯ - 4 15
  • 16. (2) __switch_to() switch_to ¯ (context_switch() , ) . ¯ jmp __switch_to , ¯ __switch_to()- ¯ . ¯ ret . ¯ ¯ ¯ next->thread.eip ¯ '1' ¯ , .switch_to .context_switch() switch_to ¯ ¯ , next . )c2003 ( ¯ - 4 16
  • 17. (3) __switch_to() struct thread_struct *prev = &prev_p->thread, *next = &next_p->thread; struct tss_struct *tss = init_tss + smp_processor_id(); unlazy_fpu(prev_p); tss->esp0 = next->esp0; ¯ .. TSS- ¯ movl %fs, prev->fs movl %gs, prev->gs if (prev->fs | prev->gs | next->fs | next->gs) { .. movl next->fs, %fs gs- fs movl next->gs, %gs .. } /* load debug registers */ /* load IO permission bitmap */ return; )c2003 ( ¯ - 4 17
  • 18. .... ¯ context switch ? ¯C - context switch() context_switch() .kernel- , , – switch_to .i386 . ,C – __switch_to )c2003 ( ¯ - 4 18
  • 19. prev context_switch: task_struct context_switch() …… $1f thread.eip esi thread.esp switch_to: prev edi ebp movl $1f, prev->thread.eip next task_struct context_switch() pushl next->thread.eip esi $1f thread.eip __switch_to: s tc to: edi thread.esp thread esp next ……… ebp esp $1f ret next context_switch() 1: esi eip edi ebp $1f 19 4 - ¯ 2003 (c)
  • 20. User Mode User Mode- (2 ) Kernel Mode- ¯ quot; ss:esp kernel mode eflags ¯ cs:eip eflags es,ds,eax, SAVE_ALL ebp,edi,esi,edx, ecx,ebx )c2003 ( ¯ - 4 20
  • 21. Kernel Mode ? ,context_switch() ¯ switch_to .switch_to - ebp, esi, edi ¯ switch_to- ebx – ebx (context_switch() quot; ¯) schedule() quot; - ecx, edx, eax switch_to caller- context_switch() schedule() ¯ ,(2 ¯, / ) callee ebx, ebp, esi, edi- ¯ schedule() .switch_to thread_struct eip- esp ¯ ¯ ¯ quot; ,gs- fs TSS thread_struct.esp0 )c2003 ( ¯ - 4 21
  • 22. (1) Linux- ¯ ¯ (fork() ¯) do_fork() kernel/fork.c do_fork() ,(file descriptors) ¯ – ¯ ¯ )c2003 ( ¯ - 4 22
  • 23. (2) Linux- do_fork() copy_thread() quot; quot; ¯ SET_LINKS quot; quot; pid- hash_pid()- quot; - ¯ TASK_RUNNING wake_up_process()- quot; runqueue pid- do_fork() , , )c2003 ( ¯ - 4 23
  • 24. Linux- (1) copy_thread() ¯ (arch/i386/kernel/process.c ) . thread p regs kernel mode- SAVE_ALL - ¯ ¯ struct pt_regs SAVE_ALL - struct pt_regs* childregs; childregs childregs = ((struct pt_regs *)(8192 + (unsigned long) p)) – 1; struct_cpy(childregs, regs); fork() 0 ,2 ¯¯ childregs->eax = 0; )c2003 ( ¯ - 4 24
  • 25. (2) copy_thread() p->thread.esp p->thread.esp = (unsigned long) childregs; p->thread.esp0 p->thread.esp0 = (unsigned long) (childregs + 1); ret_from_fork p->thread.eip = (unsigned long) ret_from_fork; pushl next->thread.eip switch_to ¯ (16 ) .__switch_to /* save fs and gs registers in thread field */ /* copy saved math regs in thread field from father to son */ )c2003 ( ¯ - 4 25
  • 26. ret_from_fork() ¯ arch/i386/kernel/entry.S ret_from_fork: check need_resched … jmp ret_from_sys_call ret_from_sys_call ,2 ¯ ¯ user mode- fork() ret_from_fork- , 0 )c2003 ( ¯ - 4 26
  • 27. ss user - p mode esp eflags task_struct cs . eip . ¯ eax orig_eax . es thread.esp0 struct pt_regs ds ret_from_fork thread.eip eax=0 thread.esp ebp SAVE_ALL . edi . esi . edx ecx ebx )c2003 ( ¯ - 4 27
  • 28. (1) exit() ¯ quot; ,exit()- main() exit()- libc , - __libc_start_main() ¯ main() ¯ ¯, 0- ¯ - )c2003 ( ¯ - 4 28
  • 29. (2) ¯ do_exit() kernel/exit.c : , ¯ exit_code ¯ exit() quot; ¯ ¯: ¯ init TASK_ZOMBIE- runqueue- ,schedule()- . switch_to )c2003 ( ¯ - 4 29
  • 30. (3) ¯ wait() ¯ , quot; release_task() kernel/exit.c , , pid- (REMOVE_LINKS) ¯ ,(unhash_pid()) )c2003 ( ¯ - 4 30