Guest User

arbu

a guest
Mar 5th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include "console.h"
  2. #include "stdint.h"
  3. #include "int.h"
  4. #include "memory.h"
  5.  
  6. struct task {
  7.     struct cpu_state*   cpu_state;
  8.     struct task*        next;
  9. };
  10.  
  11. static struct task* first_task = 0;
  12. static struct task* current_task = 0;
  13.  
  14. void task_a(void) {
  15.     while (1) {
  16.         kprintf("A");
  17.     }
  18. }
  19.  
  20. void task_b(void) {
  21.     while (1) {
  22.         kprintf("B");
  23.     }
  24. }
  25.  
  26.  
  27. /* Jeder Task braucht seinen eigenen Stack und Einsprungspunkt */
  28. struct task* init_task(void* entry) {
  29.     uint8_t* stack          = (uint8_t*)pmm_malloc();
  30.     uint8_t* user_stack     = (uint8_t*)pmm_malloc();
  31.  
  32.     struct cpu_state new_state = {
  33.         .eax = 0,
  34.         .ebx = 0,
  35.         .ecx = 0,
  36.         .edx = 0,
  37.         .esi = 0,
  38.         .edi = 0,
  39.         .ebp = 0,
  40.         .esp = (uint32_t) user_stack +4096,
  41.         .eip = (uint32_t) entry,
  42.    
  43.         /*Ring 0 Segmentregister */
  44.         .cs = 0x18 | 0x03,
  45.         .ss = 0x20 | 0x03,
  46.    
  47.         // IRQs einschalten (IF = 1)
  48.         .eflags = 0x202,
  49.     };
  50.    
  51.    
  52.     // Den angelegten CPU-Zustand auf den Stack des Tasks kopieren, damit es am
  53.     // Ende so aussieht als waere der Task durch einen Interrupt unterbrochen
  54.     // worden. So kann man dem Interrupthandler den neuen Task unterschieben
  55.     //und er stellt einfach den neuen Prozessorzustand "wieder her"
  56.    
  57.     struct cpu_state* state = (void*) (stack + 4096 - sizeof(new_state));
  58.     *state = new_state;
  59.    
  60.     /*
  61.      * Neue Taskstruktur anlegen und in die Liste einhaengen
  62.      */
  63.     struct task* task = (struct task*)pmm_malloc();
  64.     task->cpu_state = state;
  65.     task->next = first_task;
  66.     first_task = task;
  67.     return task;
  68. }
  69.  
  70. void init_multitasking(void) {
  71.     init_task(task_a);
  72.     init_task(task_b);
  73.     current_task = first_task;
  74.     asm volatile ("ltr %%ax" : : "a" (5 << 3));
  75. }
  76.  
  77. /*
  78.  * Gibt den Prozessorzustand des naechsten Tasks zurueck. Der aktuelle
  79.  * Prozessorzustand wird als Parameter uebergeben und gespeichert, damit er
  80.  * beim naechsten Aufruf des Tasks wiederhergestellt werden kann
  81.  */
  82. struct cpu_state* schedule(struct cpu_state* cpu)
  83. {
  84.    
  85.     if (!current_task)
  86.         return cpu;
  87.        
  88.     // Zustand sichern
  89.     current_task->cpu_state = cpu;
  90.  
  91.      // Einen Eintrag in der Task-Liste weiter gehen.
  92.     if (current_task->next)
  93.         current_task = current_task->next;
  94.     else
  95.         current_task = first_task;
  96.  
  97.     // Prozessorzustand des neuen Tasks laden
  98.     cpu = current_task->cpu_state;
  99.     return cpu;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment