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, 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/const.h>
  11 #include <fiwix/sched.h>
  12 #include <fiwix/process.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         kstat.ctxt++;
  27         prev = current;
  28         set_tss(next);
  29         current = next;
  30         do_switch(&prev->tss.esp, &prev->tss.eip, next->tss.esp, next->tss.eip, next->tss.cr3, TSS);
  31 }
  32 
  33 void set_tss(struct proc *p)
  34 {
  35         struct seg_desc *g;
  36 
  37         g = &gdt[TSS / sizeof(struct seg_desc)];
  38 
  39         g->sd_lobase = (unsigned int)p;
  40         g->sd_loflags = SD_TSSPRESENT;
  41         g->sd_hibase = (char)(((unsigned int)p) >> 24);
  42 }
  43 
  44 /* Round Robin algorithm */
  45 void do_sched(void)
  46 {
  47         int count;
  48         struct proc *p, *selected;
  49 
  50         /* let the current running process consume its time slice */
  51         if(!need_resched && current->state == PROC_RUNNING && current->cpu_count > 0) {
  52                 return;
  53         }
  54 
  55         need_resched = 0;
  56         for(;;) {
  57                 count = -1;
  58                 selected = &proc_table[IDLE];
  59 
  60                 FOR_EACH_PROCESS(p) {
  61                         if(p->state == PROC_RUNNING && p->cpu_count > count) {
  62                                 count = p->cpu_count;
  63                                 selected = p;
  64                         }
  65                 }
  66                 if(count) {
  67                         break;
  68                 }
  69 
  70                 /* reassigns new quantum to all processes */
  71                 FOR_EACH_PROCESS(p) {
  72                         if(p->state) {
  73                                 p->cpu_count = p->priority;
  74                         }
  75                 }
  76         }
  77         if(current != selected) {
  78                 context_switch(selected);
  79         }
  80 }
  81 
  82 void sched_init(void)
  83 {
  84         get_system_time();
  85 
  86         /* this should be more unpredictable */
  87         kstat.random_seed = CURRENT_TIME;
  88 }

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