Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "console.h"
- #include "stdint.h"
- #include "int.h"
- #include "memory.h"
- struct task {
- struct cpu_state* cpu_state;
- struct task* next;
- };
- static struct task* first_task = 0;
- static struct task* current_task = 0;
- void task_a(void) {
- while (1) {
- kprintf("A");
- }
- }
- void task_b(void) {
- while (1) {
- kprintf("B");
- }
- }
- /* Jeder Task braucht seinen eigenen Stack und Einsprungspunkt */
- struct task* init_task(void* entry) {
- uint8_t* stack = (uint8_t*)pmm_malloc();
- uint8_t* user_stack = (uint8_t*)pmm_malloc();
- struct cpu_state new_state = {
- .eax = 0,
- .ebx = 0,
- .ecx = 0,
- .edx = 0,
- .esi = 0,
- .edi = 0,
- .ebp = 0,
- .esp = (uint32_t) user_stack +4096,
- .eip = (uint32_t) entry,
- /*Ring 0 Segmentregister */
- .cs = 0x18 | 0x03,
- .ss = 0x20 | 0x03,
- // IRQs einschalten (IF = 1)
- .eflags = 0x202,
- };
- // Den angelegten CPU-Zustand auf den Stack des Tasks kopieren, damit es am
- // Ende so aussieht als waere der Task durch einen Interrupt unterbrochen
- // worden. So kann man dem Interrupthandler den neuen Task unterschieben
- //und er stellt einfach den neuen Prozessorzustand "wieder her"
- struct cpu_state* state = (void*) (stack + 4096 - sizeof(new_state));
- *state = new_state;
- /*
- * Neue Taskstruktur anlegen und in die Liste einhaengen
- */
- struct task* task = (struct task*)pmm_malloc();
- task->cpu_state = state;
- task->next = first_task;
- first_task = task;
- return task;
- }
- void init_multitasking(void) {
- init_task(task_a);
- init_task(task_b);
- current_task = first_task;
- asm volatile ("ltr %%ax" : : "a" (5 << 3));
- }
- /*
- * Gibt den Prozessorzustand des naechsten Tasks zurueck. Der aktuelle
- * Prozessorzustand wird als Parameter uebergeben und gespeichert, damit er
- * beim naechsten Aufruf des Tasks wiederhergestellt werden kann
- */
- struct cpu_state* schedule(struct cpu_state* cpu)
- {
- if (!current_task)
- return cpu;
- // Zustand sichern
- current_task->cpu_state = cpu;
- // Einen Eintrag in der Task-Liste weiter gehen.
- if (current_task->next)
- current_task = current_task->next;
- else
- current_task = first_task;
- // Prozessorzustand des neuen Tasks laden
- cpu = current_task->cpu_state;
- return cpu;
- }
Advertisement
Add Comment
Please, Sign In to add comment