|
|
@@ -53,10 +53,7 @@ extern void ret_intr(void);
|
|
|
struct task init_task;
|
|
|
struct task *current;
|
|
|
|
|
|
-struct task *task_lists[16];
|
|
|
-
|
|
|
-__uint32_t next_pid = 0;
|
|
|
-
|
|
|
+struct task *tasks[MAX_TASKS] = {0};
|
|
|
|
|
|
int do_exit(__uint64_t exitcode) {
|
|
|
printk(white, "do_exit: %q\n", exitcode);
|
|
|
@@ -64,6 +61,37 @@ int do_exit(__uint64_t exitcode) {
|
|
|
for (;;);
|
|
|
}
|
|
|
|
|
|
+__uint64_t task_a(__uint64_t arg) {
|
|
|
+ register __uint64_t a = 12345678;
|
|
|
+ printk(white, "task a is running: %x\n", arg);
|
|
|
+
|
|
|
+ for (;;) {
|
|
|
+ if (a != 12345678) {
|
|
|
+ printk(white, "a is now %d\n", a);
|
|
|
+ panic("task_a: a changed. ");
|
|
|
+ }
|
|
|
+
|
|
|
+ putc('A', white);
|
|
|
+ switch_to(tasks[1]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+__uint64_t task_b(__uint64_t arg) {
|
|
|
+ register __uint64_t b = 90123456;
|
|
|
+
|
|
|
+ printk(white, "task b is running: %x\n", arg);
|
|
|
+
|
|
|
+ for (;;) {
|
|
|
+ if (b != 90123456) {
|
|
|
+ printk(white, "b is now %d\n", b);
|
|
|
+ panic("task_b: b changed. ");
|
|
|
+ }
|
|
|
+
|
|
|
+ putc('B', white);
|
|
|
+ switch_to(tasks[0]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
int task_init(void) {
|
|
|
/* initial the init-tss */
|
|
|
@@ -75,9 +103,15 @@ int task_init(void) {
|
|
|
|
|
|
set_tss(&init_tss, 0);
|
|
|
|
|
|
- /* initial the task struct for init task */
|
|
|
+ if (ktask_create(task_a, 0x12345678))
|
|
|
+ panic("task_init: failed to create task a. \n");
|
|
|
+
|
|
|
+ if (ktask_create(task_b, 0x23456789))
|
|
|
+ panic("task_init: failed to create task b. \n");
|
|
|
|
|
|
- current = task_lists[0];
|
|
|
+ current = &init_task;
|
|
|
+
|
|
|
+ switch_to(tasks[0]);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -155,6 +189,17 @@ int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
|
|
|
}
|
|
|
|
|
|
|
|
|
+static __uint32_t find_free_pid(void) {
|
|
|
+ __uint32_t freepid;
|
|
|
+
|
|
|
+ for (freepid = 0; freepid < MAX_TASKS; freepid++)
|
|
|
+ if (!tasks[freepid])
|
|
|
+ return freepid;
|
|
|
+
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/*
|
|
|
* we use this function to create a new task.
|
|
|
@@ -174,7 +219,6 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
|
|
|
if (!start_stack)
|
|
|
return -ENOMEM;
|
|
|
|
|
|
-
|
|
|
/* += stack_size: point to the top of stack. */
|
|
|
|
|
|
start_stack += 65536;
|
|
|
@@ -200,7 +244,13 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
|
|
|
|
|
|
child->exit_code = 0;
|
|
|
|
|
|
- child->pid = next_pid++;
|
|
|
+ child->pid = find_free_pid();
|
|
|
+
|
|
|
+ if (child->pid == -1)
|
|
|
+ return -EAGAIN;
|
|
|
+
|
|
|
+ printk(white, "new task pid: %d\n", child->pid);
|
|
|
+
|
|
|
child->father = current->pid;
|
|
|
|
|
|
child->cr3 = 0x0000000000000000;
|
|
|
@@ -224,9 +274,11 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
|
|
|
|
|
|
child->nice = 0;
|
|
|
|
|
|
+ /* bullshit! -> ktask_entry will popq it in the stack. */
|
|
|
/* we don't need to set ss in regs, because
|
|
|
* ktask_entry will ignore them
|
|
|
* */
|
|
|
+ regs->gs = regs->fs = regs->es = regs->ds = KERNEL_DS;
|
|
|
}
|
|
|
|
|
|
/* dpl = 3 */
|
|
|
@@ -244,7 +296,7 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
|
|
|
child->codeseg = USER_CS;
|
|
|
child->nice = 1;
|
|
|
|
|
|
- regs->ss = USER_DS;
|
|
|
+ regs->ss = regs->fs = regs->es = regs->ds = regs->gs = USER_DS;
|
|
|
regs->cs = USER_CS;
|
|
|
regs->rflags = 1 << 9;
|
|
|
regs->rip = (__uint64_t) ret_intr;
|
|
|
@@ -258,19 +310,9 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
|
|
|
:: "S" (regs), "D" (child->rsp), "c" (sizeof(struct registers))
|
|
|
);
|
|
|
|
|
|
- child->next = NULL;
|
|
|
-
|
|
|
child->state = STATE_READY;
|
|
|
|
|
|
- struct task *t;
|
|
|
-
|
|
|
- for (t = task_lists[child->nice]; t && t->next; t = t->next);
|
|
|
-
|
|
|
- if (!t)
|
|
|
- task_lists[child->nice] = child;
|
|
|
-
|
|
|
- else
|
|
|
- t->next = child;
|
|
|
+ tasks[child->pid] = child;
|
|
|
|
|
|
return 0;
|
|
|
}
|