|
@@ -32,6 +32,7 @@
|
|
|
#include <kernel/int.h>
|
|
#include <kernel/int.h>
|
|
|
#include <kernel/sched.h>
|
|
#include <kernel/sched.h>
|
|
|
|
|
|
|
|
|
|
+#include <kernel/mm.h>
|
|
|
#include <arch/x86/int.h>
|
|
#include <arch/x86/int.h>
|
|
|
#include <arch/io.h>
|
|
#include <arch/io.h>
|
|
|
#include <arch/sys.h>
|
|
#include <arch/sys.h>
|
|
@@ -42,14 +43,33 @@
|
|
|
#include <lib/string.h>
|
|
#include <lib/string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+/* kernel/trap.S */
|
|
|
|
|
+
|
|
|
|
|
+extern void ret_intr(void);
|
|
|
|
|
+
|
|
|
struct task init_task;
|
|
struct task init_task;
|
|
|
struct task *current;
|
|
struct task *current;
|
|
|
|
|
|
|
|
-struct task *task_queues[16];
|
|
|
|
|
|
|
+struct task *task_lists[16];
|
|
|
|
|
|
|
|
__uint32_t next_pid = 0;
|
|
__uint32_t next_pid = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+__uint64_t ktask(__uint64_t arg) {
|
|
|
|
|
+ printk(0x00ffffff, 0x00000000, "main is now running. arg: %q\n", arg);
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+int do_exit(__uint64_t exitcode) {
|
|
|
|
|
+ printk(0x00ffffff, 0x00000000, "do_exit: %q\n", exitcode);
|
|
|
|
|
+
|
|
|
|
|
+ for (;;);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
int task_init(void) {
|
|
int task_init(void) {
|
|
|
/* initial the init-tss */
|
|
/* initial the init-tss */
|
|
|
|
|
|
|
@@ -62,58 +82,195 @@ int task_init(void) {
|
|
|
|
|
|
|
|
/* initial the task struct for init task */
|
|
/* initial the task struct for init task */
|
|
|
|
|
|
|
|
- memset(&init_task, 0x00, sizeof(struct task));
|
|
|
|
|
|
|
+ ktask_create(ktask, 0x12345678);
|
|
|
|
|
|
|
|
- next_pid++;
|
|
|
|
|
|
|
+ switch_to(task_lists[0]);
|
|
|
|
|
|
|
|
- init_task.state = STATE_RUNNING;
|
|
|
|
|
- init_task.ktime = init_task.utime = 0;
|
|
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- /* min priority */
|
|
|
|
|
- init_task.priority = 15;
|
|
|
|
|
|
|
|
|
|
- init_task.signal = 0;
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ * entry for all kernel tasks. (execute only one time)
|
|
|
|
|
+ * get registers from stack (store in do_fork, memcpy).
|
|
|
|
|
+ * and call function %rcx with argument %rdi, then call
|
|
|
|
|
+ * do_exit.
|
|
|
|
|
+ * */
|
|
|
|
|
|
|
|
- /* init_task can't have any signals */
|
|
|
|
|
- init_task.signal_mask = 0xffffffff;
|
|
|
|
|
|
|
+__asm__(
|
|
|
|
|
+ "ktask_entry:\n\t"
|
|
|
|
|
+ "popq %rax\n\t"
|
|
|
|
|
+ "movq %rax, %gs\n\t"
|
|
|
|
|
|
|
|
- init_task.exit_code = 0;
|
|
|
|
|
|
|
+ "popq %rax\n\t"
|
|
|
|
|
+ "movq %rax, %fs\n\t"
|
|
|
|
|
|
|
|
- init_task.pid = init_task.father = INIT_PID;
|
|
|
|
|
- init_task.tss = &init_tss;
|
|
|
|
|
|
|
+ "popq %rax\n\t"
|
|
|
|
|
+ "movq %rax, %es\n\t"
|
|
|
|
|
|
|
|
- init_task.cr3 = PAGE_TABLE_BASE;
|
|
|
|
|
- init_task.rsp0 = get_rsp();
|
|
|
|
|
- init_task.rsp = get_rsp();
|
|
|
|
|
|
|
+ "popq %rax\n\t"
|
|
|
|
|
+ "movq %rax, %es\n\t"
|
|
|
|
|
|
|
|
- init_task.dataseg = KERNEL_DS;
|
|
|
|
|
- init_task.codeseg = KERNEL_CS;
|
|
|
|
|
|
|
+ "popq %r15\n\t"
|
|
|
|
|
+ "popq %r14\n\t"
|
|
|
|
|
+ "popq %r13\n\t"
|
|
|
|
|
+ "popq %r12\n\t"
|
|
|
|
|
|
|
|
- init_task.rip = 0;
|
|
|
|
|
-
|
|
|
|
|
- /* copy registers to top of its stack */
|
|
|
|
|
|
|
+ "popq %r11\n\t"
|
|
|
|
|
+ "popq %r10\n\t"
|
|
|
|
|
+ "popq %r9\n\t"
|
|
|
|
|
+ "popq %r8\n\t"
|
|
|
|
|
|
|
|
- init_task.next = NULL;
|
|
|
|
|
|
|
+ "popq %rdi\n\t"
|
|
|
|
|
+ "popq %rsi\n\t"
|
|
|
|
|
+ "popq %rbp\n\t"
|
|
|
|
|
|
|
|
- current = &init_task;
|
|
|
|
|
- task_queues[15] = &init_task;
|
|
|
|
|
|
|
+ "popq %rbx\n\t"
|
|
|
|
|
+ "popq %rdx\n\t"
|
|
|
|
|
+ "popq %rcx\n\t"
|
|
|
|
|
+ "popq %rax\n\t"
|
|
|
|
|
|
|
|
-// switch_to(&init_task);
|
|
|
|
|
|
|
+ /* ignore ss, rip etc. */
|
|
|
|
|
+ "addq $40, %rsp\n\t"
|
|
|
|
|
+ "callq *%rcx\n\t"
|
|
|
|
|
|
|
|
- return 0;
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ /* return code -> first argument */
|
|
|
|
|
+ "movq %rax, %rdi\n\t"
|
|
|
|
|
+ "callq do_exit\n\t"
|
|
|
|
|
|
|
|
|
|
+ /* it never here */
|
|
|
|
|
+);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/*
|
|
|
|
|
+ * create a kernel task.
|
|
|
|
|
+ * (just set some registers)
|
|
|
|
|
+ * */
|
|
|
|
|
|
|
|
int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
|
|
int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
|
|
|
- return 0;
|
|
|
|
|
|
|
+ void *stack = alloc_page();
|
|
|
|
|
+
|
|
|
|
|
+ if (!stack)
|
|
|
|
|
+ return -ENOMEM;
|
|
|
|
|
+
|
|
|
|
|
+ struct registers regs;
|
|
|
|
|
+
|
|
|
|
|
+ memset(®s, 0x00, sizeof(regs));
|
|
|
|
|
+
|
|
|
|
|
+ /* entry function */
|
|
|
|
|
+ regs.rcx = (__uint64_t) entry;
|
|
|
|
|
+
|
|
|
|
|
+ /* argument */
|
|
|
|
|
+ regs.rdi = arg;
|
|
|
|
|
+
|
|
|
|
|
+ return do_fork(®s, (__uint64_t) stack, PAGE_SIZE, 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-void do_clock(void) {
|
|
|
|
|
- printk(0x00ff0000, 0x00000000, "%d\n", jiffies / 100);
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ * we use this function to create a new task.
|
|
|
|
|
+ *
|
|
|
|
|
+ * it doesn't like that function that we often use in linux (no argument),
|
|
|
|
|
+ * it needs some arguments, and info about stack.
|
|
|
|
|
+ *
|
|
|
|
|
+ * if now is a user task, its child should be a user task too.
|
|
|
|
|
+ *
|
|
|
|
|
+ * we store reg on the top of stack, because ret_intr (or ktask_entry) will
|
|
|
|
|
+ * popq all these registers when the task runs.
|
|
|
|
|
+ * */
|
|
|
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+int do_fork(struct registers *regs, __uint64_t start_stack, __uint64_t stacksize, __uint64_t dpl) {
|
|
|
|
|
+ struct task *child = kmalloc(&pool, sizeof(struct task));
|
|
|
|
|
+
|
|
|
|
|
+ if (!child)
|
|
|
|
|
+ return -ENOMEM;
|
|
|
|
|
+
|
|
|
|
|
+ /* current is this task's father */
|
|
|
|
|
+ memcpy(child, current, sizeof(struct task));
|
|
|
|
|
+
|
|
|
|
|
+ child->state = STATE_UNINTERRUPTIABLE;
|
|
|
|
|
+
|
|
|
|
|
+ child->utime = child->ktime = 0;
|
|
|
|
|
+
|
|
|
|
|
+ child->start_time = jiffies;
|
|
|
|
|
+
|
|
|
|
|
+ child->signal = 0;
|
|
|
|
|
+
|
|
|
|
|
+ /* allow all signals */
|
|
|
|
|
+ child->signal_mask = 0xffffffff;
|
|
|
|
|
+
|
|
|
|
|
+ child->exit_code = 0;
|
|
|
|
|
+
|
|
|
|
|
+ child->pid = next_pid++;
|
|
|
|
|
+ child->father = current->pid;
|
|
|
|
|
+
|
|
|
|
|
+ child->cr3 = 0x0000000000000000;
|
|
|
|
|
+
|
|
|
|
|
+ /* kernel task */
|
|
|
|
|
+ if (!dpl) {
|
|
|
|
|
+ __asm__ __volatile__ (
|
|
|
|
|
+ "leaq ktask_entry(%%rip), %%rax\n\t"
|
|
|
|
|
+ "movq %%rax, %0\n\t"
|
|
|
|
|
+
|
|
|
|
|
+ : "=m" (child->rip)
|
|
|
|
|
+ :: "ax"
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ /* I don't know why this doesn't work. . . */
|
|
|
|
|
+
|
|
|
|
|
+// child->rip = (__uint64_t) &ktask_entry;
|
|
|
|
|
+
|
|
|
|
|
+ child->dataseg = KERNEL_DS;
|
|
|
|
|
+ child->codeseg = KERNEL_CS;
|
|
|
|
|
+
|
|
|
|
|
+ child->nice = 0;
|
|
|
|
|
+
|
|
|
|
|
+ /* we don't need to set ss in regs, because
|
|
|
|
|
+ * ktask_entry will ignore them
|
|
|
|
|
+ * */
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /* dpl = 3 */
|
|
|
|
|
+ else {
|
|
|
|
|
+ __asm__ __volatile__ (
|
|
|
|
|
+ "leaq ret_intr(%%rip), %%rax\n\t"
|
|
|
|
|
+ "movq %%rax, %0\n\t"
|
|
|
|
|
+
|
|
|
|
|
+ : "=m" (child->rip)
|
|
|
|
|
+ :: "ax"
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+// child->rip = (__uint64_t) &ret_intr;
|
|
|
|
|
+ child->dataseg = USER_DS;
|
|
|
|
|
+ child->codeseg = USER_CS;
|
|
|
|
|
+ child->nice = 1;
|
|
|
|
|
+
|
|
|
|
|
+ regs->ss = USER_DS;
|
|
|
|
|
+ regs->cs = USER_CS;
|
|
|
|
|
+ regs->rflags = 1 << 9;
|
|
|
|
|
+ regs->rip = (__uint64_t) ret_intr;
|
|
|
|
|
+ regs->rsp = start_stack - sizeof(struct registers);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ child->rsp = start_stack - sizeof(struct registers);
|
|
|
|
|
+ memcpy((char *) child->rsp, regs, 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;
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|