summaryrefslogtreecommitdiff
path: root/kernel/sched.c
blob: 8469005f8427c71dc06a11fa75b8440f881a2b02 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <asm/system.h>
#include <kernel/memory.h>
#include <kernel/sched.h>
#include <string.h>
#include <sys/types.h>

/*
 * WARNING!
 * don't change this without also updating
 * the number of TSS descriptors in the GDT
 * (located in boot.s)
 */
#define NRTASKS 64

#define USRSTART ((void*) 0x40000000)

#define pages(n) \
  ((n / 4096) + 1)

#define tables(n) \
  ((n / 1024) + 1)

#define getpage(name, p) ({\
  void **_next; \
  _next = map_page(p); \
  name = p; \
  p = *_next; \
  })

extern char _usrbin_start;
extern char _usrbin_size;

struct task_state {
  uint16_t ds;
  uint32_t edi;
  uint32_t esi;
  uint32_t ebp;
  uint32_t esp_garbage;
  uint32_t ebx;
  uint32_t edx;
  uint32_t ecx;
  uint32_t eax;
  uint32_t eip;
  uint32_t cs;
  uint32_t eflags;
  uint32_t esp;
  uint32_t ss;
} __attribute__((packed));

struct task_struct *ctask;
uint32_t ctaskn;
struct task_state *cstate;

static struct task_struct tasks[NRTASKS];

static pid_t nextpid;

static inline void empty_table(void *t) {
  int i;

  for(i = 0; i < PGENT; i++)
    ((uint32_t*) t)[i] = 0;
}

struct task_struct *create_proc(void *bin, size_t len) {
  int i, j;
  int c;
  size_t totpages, npages, ntables;
  void *p, *page, *tab, *map;
  void *pdir, *stackt, *stackp;
  struct task_struct *task = NULL;

  npages = pages(len);
  ntables = tables(pages(len));
  totpages = npages + ntables + 3;

  /* find an unused task structure */
  for(i = 0; i < NRTASKS; i++) {
    if(!tasks[i].pid) {
      task = &tasks[i];
      /* update the GDT */
      set_tss(i, &tasks[i].state);
      break;
    }
  }
  if(task == NULL)
    return NULL;

  /* find an unused PID */
  for(i = 0; i < NRTASKS; i++) {
    if(tasks[i].pid == nextpid) {
      /* TODO: handle PID exhaustion */
      nextpid++;
      if(nextpid == 0)
        nextpid++;
      i = 0;
    }
  }

  /* here we allocate pages to setup the process'
   * address space. in addition to the pages that
   * will hold the binary image, the following
   * pages are allocated:
   *  - page directory (1 page)
   *  - page tables
   *  - stack page (1 page)
   *  - stack table (1 page)
   */
  p = alloc_physical_pages(totpages);
  if(p == NULL)
    return NULL;

  getpage(pdir, p);
  getpage(stackt, p);
  getpage(stackp, p);

  /* populate the page directory */
  c = npages;
  map = map_page(pdir);
  empty_table(map);
  for(i = 0; i < ntables; i++) {
    /* add the table entry to the directory */
    getpage(tab, p);
    map = map_page(pdir);
    ((uint32_t*) map)[i + (PGENT / 4)] = (uint32_t) tab | 0x007;

    /* populate the page table */
    map = map_page(tab);
    for(j = 0; j < PGENT; j++) {
      ((uint32_t*) map)[j] = 0;
      if(c <= 0)
        continue;

      getpage(page, p);
      map = map_page(tab);
      ((uint32_t*) map)[j] = (uint32_t) page | 0x007;
      c--;
    }
  }

  /* populate the stack table */
  map = map_page(stackt);
  empty_table(map);
  ((uint32_t*) map)[PGENT - 1] = (uint32_t) stackp | 0x007;

  /* add the stack page table to the directory */
  map = map_page(pdir);
  ((uint32_t*) map)[PGENT - 1] = (uint32_t) stackt | 0x007;
  /* add the kernel's page table to the directory */
  ((uint32_t*) map)[0] = (uint32_t) ktab | 0x003;

  __asm__ volatile ("mov %%eax, %%cr3" :: "a" (pdir));

  /* load in the user binary */
  memcpy(USRSTART, bin, len);

  /*
   * here we initialize the task struct.
   * currently, we only setup state information
   * necessary to begin execution.
   */
  memset(task, 0, sizeof(struct task_struct));
  task->pid = nextpid;
  task->state.cr3 = (uint32_t) pdir;
  task->state.cs = 0x1B;
  task->state.ds = 0x23;
  task->state.es = 0x23;
  task->state.fs = 0x23;
  task->state.gs = 0x23;
  task->state.ss = 0x23;
  task->state.eip = (uint32_t) USRSTART;
  task->state.esp = 0xFFFFFFFF;
  task->state.esp0 = 0x80000;
  task->state.ss0 = 0x10;
  __asm__ (
      "pushf\n" \
      "pop %%eax" \
      : "=a" (task->state.eflags) \
      ::);

  return task;
}

void sched_init(void) {
  int i;
  struct task_struct *task;

  memset(&tasks, 0, sizeof(tasks));
  for(i = 0; i < NRTASKS; i++)
    clear_tss(i);

  nextpid = 1;
  ctask = NULL;
  cstate = NULL;

  create_proc(&_usrbin_start, (size_t) &_usrbin_size);
  create_proc(&_usrbin_start, (size_t) &_usrbin_size);
  create_proc(&_usrbin_start, (size_t) &_usrbin_size);

  switch_to(0, &tasks[0]);
}

void save_state(void) {
  if(ctask == NULL || cstate == NULL)
    return;

  ctask->state.eax = cstate->eax;
  ctask->state.ebx = cstate->ebx;
  ctask->state.ecx = cstate->ecx;
  ctask->state.edx = cstate->edx;
  ctask->state.esi = cstate->esi;
  ctask->state.edi = cstate->edi;
  ctask->state.esp = cstate->esp;
  ctask->state.ebp = cstate->ebp;
  ctask->state.cs = cstate->cs;
  ctask->state.ds = cstate->ds;
  ctask->state.es = cstate->ds;
  ctask->state.fs = cstate->ds;
  ctask->state.gs = cstate->ds;
  ctask->state.ss = cstate->ss;
  ctask->state.eflags = cstate->eflags;
  ctask->state.eip = cstate->eip;
}

void reschedule(void) {
  int i;
  uint32_t n;

  if(ctask == NULL)
    return;

  n = ctaskn;
  for(i = 0; i < NRTASKS; i++) {
    n = (n + 1) % NRTASKS;
    if(tasks[n].pid)
      switch_to(n, &tasks[n]);
  }
}

void sched_tick(void) {
  reschedule();
}