| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- /* Copyright (C) 2021 Rain */
- /* This file is part of XNIX. */
- /*
- XNIX is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) and later version.
- */
- /*
- XNIX is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- */
- /*
- You should have received a copy of the GNU General Public License
- along with XNIX. If not, see <https://www.gnu.org/licenses/>.
- */
- #include <kernel/init.h>
- #include <kernel/errno.h>
- #include <kernel/types.h>
- #include <modules/tty.h>
- #include <kernel/int.h>
- #include <kernel/sched.h>
- #include <kernel/mm.h>
- #include <arch/x86/int.h>
- #include <arch/io.h>
- #include <arch/sys.h>
- #include <arch/x86/tss.h>
- #include <arch/x86/paging.h>
- #include <lib/string.h>
- /* kernel/trap.S */
- extern void ret_intr(void);
- struct task init_task;
- struct task *current;
- struct task *task_lists[16];
- __uint32_t next_pid = 0;
- __uint64_t ktask(__uint64_t arg) {
- printk(white, "main is now running. arg: %q\n", arg);
- return 1;
- }
- int do_exit(__uint64_t exitcode) {
- printk(white, "do_exit: %q\n", exitcode);
- for (;;);
- }
- int task_init(void) {
- /* initial the init-tss */
- init_tss.rsp0 = init_tss.rsp1 = init_tss.rsp2 = get_rsp();
- init_tss.ist1 = init_tss.ist2 = init_tss.ist3 = get_rsp();
- init_tss.ist4 = init_tss.ist5 = init_tss.ist6 = get_rsp();
- init_tss.ist7 = get_rsp();
- set_tss(&init_tss, 0);
- /* initial the task struct for init task */
- current = task_lists[0];
- if (ktask_create(ktask, 0x12345678) == -ENOMEM) {
- printk(red, "ktask_create returns -ENOMEM (no enough memory). ");
- return 1;
- }
-
- switch_to(task_lists[0]);
- return 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.
- * */
- __asm__(
- "ktask_entry:\n\t"
- "popq %rax\n\t"
- "movq %rax, %gs\n\t"
- "popq %rax\n\t"
- "movq %rax, %fs\n\t"
- "popq %rax\n\t"
- "movq %rax, %es\n\t"
- "popq %rax\n\t"
- "movq %rax, %es\n\t"
- "popq %r15\n\t"
- "popq %r14\n\t"
- "popq %r13\n\t"
- "popq %r12\n\t"
- "popq %r11\n\t"
- "popq %r10\n\t"
- "popq %r9\n\t"
- "popq %r8\n\t"
- "popq %rdi\n\t"
- "popq %rsi\n\t"
- "popq %rbp\n\t"
- "popq %rbx\n\t"
- "popq %rdx\n\t"
- "popq %rcx\n\t"
- "popq %rax\n\t"
- /* ignore ss, rip etc. */
- "addq $40, %rsp\n\t"
- "callq *%rcx\n\t"
- /* syscall 0: do_exit */
- "movq %rax, %rbx\n\t"
- "movq $0, %rax\n\t"
- "int $0x80\n\t"
- /* it never here */
- );
- /*
- * create a kernel task.
- * (just set some registers)
- * */
- int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
- struct registers regs;
- memset(®s, 0x00, sizeof(regs));
- /* entry function */
- regs.rcx = (__uint64_t) entry;
-
- /* argument */
- regs.rdi = arg;
- return do_fork(®s, 0);
- }
- /*
- * 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.
- *
- * 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.
- * */
- int do_fork(struct registers *regs, __uint64_t dpl) {
- __uint64_t start_stack = (__uint64_t) kmalloc(&pool, 65536);
-
- if (!start_stack)
- return -ENOMEM;
- /* += stack_size: point to the top of stack. */
- start_stack += 65536;
- 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);
- __asm__ __volatile__ (
- "cld;rep;movsb;\n\t"
- :: "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;
- return 0;
- }
- void set_tss(struct tss *table, __uint64_t dpl) {
- __uint64_t tss = (__uint64_t) table;
- /* create a descriptor (in gdt) and register it */
- struct tss_entry *entry = (struct tss_entry *) (gdt_table + 5);
- entry->low = entry->high = 0x0000000000000000;
- /* `103` is the size of a tss table */
- entry->low = 103ul | ((tss & 0xffff) << 16) | (((tss >> 16) & 0xff) << 32)
- | ((0x80 + dpl + 0x09) << 40) | (tss << 56 & 0xff);
- entry->high = (tss >> 32) & 0xffffffff;
- /* load it to tr */
- ltr(40);
- return;
- }
|