Fork me on GitHub

root/kernel/sched.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. context_switch
  2. set_tss
  3. do_sched
  4. sched_init

   1 /*
   2  * fiwix/kernel/sched.c
   3  *
   4  * Copyright 2018-2021, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/asm.h>
   9 #include <fiwix/kernel.h>
  10 #include <fiwix/sched.h>
  11 #include <fiwix/process.h>
  12 #include <fiwix/sleep.h>
  13 #include <fiwix/segments.h>
  14 #include <fiwix/timer.h>
  15 #include <fiwix/pic.h>
  16 #include <fiwix/stdio.h>
  17 #include <fiwix/string.h>
  18 
  19 extern struct seg_desc gdt[NR_GDT_ENTRIES];
  20 int need_resched = 0;
  21 
  22 static void context_switch(struct proc *next)
  23 {
  24         struct proc *prev;
  25 
  26         CLI();
  27         kstat.ctxt++;
  28         prev = current;
  29         set_tss(next);
  30         current = next;
  31         do_switch(&prev->tss.esp, &prev->tss.eip, next->tss.esp, next->tss.eip, next->tss.cr3, TSS);
  32         STI();
  33 }
  34 
  35 void set_tss(struct proc *p)
  36 {
  37         struct seg_desc *g;
  38 
  39         g = &gdt[TSS / sizeof(struct seg_desc)];
  40 
  41         g->sd_lobase = (unsigned int)p;
  42         g->sd_loflags = SD_TSSPRESENT;
  43         g->sd_hibase = (char)(((unsigned int)p) >> 24);
  44 }
  45 
  46 /* Round Robin algorithm */
  47 void do_sched(void)
  48 {
  49         int count;
  50         struct proc *p, *selected;
  51 
  52         /* let the current running process consume its time slice */
  53         if(!need_resched && current->state == PROC_RUNNING && current->cpu_count > 0) {
  54                 return;
  55         }
  56 
  57         need_resched = 0;
  58         for(;;) {
  59                 count = -1;
  60                 selected = &proc_table[IDLE];
  61 
  62                 FOR_EACH_PROCESS_RUNNING(p) {
  63                         if(p->cpu_count > count) {
  64                                 count = p->cpu_count;
  65                                 selected = p;
  66                         }
  67                         p = p->next_run;
  68                 }
  69                 if(count) {
  70                         break;
  71                 }
  72 
  73                 /* reassigns new quantum to all running processes */
  74                 FOR_EACH_PROCESS_RUNNING(p) {
  75                         p->cpu_count = p->priority;
  76                         p = p->next_run;
  77                 }
  78         }
  79         if(current != selected) {
  80                 context_switch(selected);
  81         }
  82 }
  83 
  84 void sched_init(void)
  85 {
  86         get_system_time();
  87 
  88         /* this should be more unpredictable */
  89         kstat.random_seed = CURRENT_TIME;
  90 }

/* [previous][next][first][last][top][bottom][index][help] */