summaryrefslogtreecommitdiff
path: root/src/kernel/task.c
blob: 79b2f09774619423a7758836518f300b45e20e16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "panic.h"
#include "task.h"
#include "paging.h"

struct tss {
  struct tss *prev;

  uint32_t esp0;
  uint32_t ss0;
  uint32_t esp1;
  uint32_t ss1;
  uint32_t esp2;
  uint32_t ss2;

  uint32_t cr3;
  uint32_t eip;
  uint32_t eflags;

  uint32_t eax;
  uint32_t ecx;
  uint32_t edx;
  uint32_t ebx;
  uint32_t esp;
  uint32_t ebp;
  uint32_t esi;
  uint32_t edi;

  uint32_t es;
  uint32_t cs;
  uint32_t ss;
  uint32_t ds;
  uint32_t fs;
  uint32_t gs;

  uint32_t ldt;
  uint16_t trap;
  uint16_t iomp;
} __attribute__ ((packed));

#define TSS ((struct tss *)0x00004f98)

#define MAX_TASKS 64

struct task_state tasks[MAX_TASKS];
struct task_state *active_task;

void init_tasks() {
  active_task = tasks;

  for (uint8_t i = 0; i < MAX_TASKS; ++i)
    tasks[i].page_directory = 0;

  TSS->ss0 = 0x18;
  TSS->esp0 = 0x00040000;
//TSS->cs = 0x13;
//TSS->ds = 0x1b;
//TSS->ss = 0x1b;
  TSS->iomp = sizeof(struct tss);

  asm volatile (
    "mov $0x08, %%ax\n"
    "ltr %%ax"
  : : : "ax");
}

void new_task(struct task_state state) {
  for (uint8_t n = 0; n < MAX_TASKS; ++n)
    if (!tasks[n].page_directory) {
      tasks[n] = state;
      return;
    }
  panic("Maximum number of tasks reached.");
}

void advance_active_task() {
  struct task_state *prev_task = active_task;
  do {
    if (++active_task == tasks + MAX_TASKS)
      active_task = tasks;
    if (active_task == prev_task) {
      logsz("No active tasks.\nHalting.");
      while (1)
        asm ("hlt");
    }
  }
  while (!active_task->page_directory);
}

void delete_task(struct task_state *state) {
  free_task_pd(state->page_directory);
  state->page_directory = 0;
}