Kaynağa Gözat

5.011u-sched: create a new task

rain 5 yıl önce
ebeveyn
işleme
13563c33cc
17 değiştirilmiş dosya ile 413 ekleme ve 81 silme
  1. 1 1
      LINES
  2. 2 2
      Makefile
  3. 3 0
      README.md
  4. 2 2
      boot/boot.asm
  5. 26 0
      include/kernel/errno.h
  6. 3 0
      include/kernel/init.h
  7. 11 11
      include/kernel/int.h
  8. 32 4
      include/kernel/sched.h
  9. 45 0
      include/sys/syscall.h
  10. 4 0
      init/head.S
  11. 1 0
      init/main.c
  12. 1 1
      kernel/Makefile
  13. 3 16
      kernel/int.c
  14. 188 31
      kernel/sched.c
  15. 40 0
      kernel/sys.c
  16. 3 13
      kernel/timer.S
  17. 48 0
      kernel/trap.S

+ 1 - 1
LINES

@@ -20,4 +20,4 @@
 2021-7-18 3.913: 3913 lines, 35 files, rain
 2021-7-21 4.678: 4678 lines, 39 files, rain
 2021-7-21 4.682: 4682 lines, 39 files, rain
-
+2021-7-21 5.011: 5011 lines, 41 files, rain

+ 2 - 2
Makefile

@@ -1,8 +1,8 @@
 OBJS = boot/bootsect 
 RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c
 RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o font.o $(RLIB_OBJS)
-KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c $(RLIB_SRCS)
+KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o font.o $(RLIB_OBJS)
+KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c $(RLIB_SRCS)
 
 all: Makefile
 	make -C tools/

+ 3 - 0
README.md

@@ -87,3 +87,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 4.682u-mm:  
 &nbsp;&nbsp;&nbsp;&nbsp;`alloc_page` returns a wrong address that isn't that it get from `mapping()`. 
 
+5.011u-sched:  
+&nbsp;&nbsp;&nbsp;&nbsp;now `task_init()`, `ktask_create()`, `do_fork()` and `switch_to()` can work: create a new task and switch to it. 
+

+ 2 - 2
boot/boot.asm

@@ -53,7 +53,7 @@ _start:
 .set_vga:
 	; get mode info
 	mov ax, 0x4f01
-	mov cx, 0x4180
+	mov cx, 0x4118
 	mov di, 0x7000
 
 	int 0x10
@@ -73,7 +73,7 @@ die:	jc die
 	mov [FB_ADDR], eax
 
 	mov ax, 0x4f02
-	mov bx, 0x4180
+	mov bx, 0x4118
 	int 0x10
 
 	; get e820 memory

+ 26 - 0
include/kernel/errno.h

@@ -1,3 +1,29 @@
+/* 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/>.  
+*/
+
+
+
+
 #ifndef _ERRNO_H
 #define _ERRNO_H
 

+ 3 - 0
include/kernel/init.h

@@ -56,6 +56,9 @@
 #define KERNEL_CS 0x0008
 #define KERNEL_DS 0x0010
 
+#define USER_CS   0x0018
+#define USER_DS   0x0020
+
 
 #define SEEK_SET 0x0001
 #define SEEK_CUR 0x0002

+ 11 - 11
include/kernel/int.h

@@ -124,8 +124,7 @@ extern __uint64_t jiffies;
 	);
 
 
-#define MAX_IRQ_NR 24
-
+#define MAX_IRQ_NR 0x60
 
 struct irq_entry {
 	void (*handler) (void);
@@ -152,19 +151,20 @@ extern struct irq_entry irq_table[MAX_IRQ_NR];
 #define RDI	96
 #define RSI 	104
 #define RBP 	112
-#define RDX 	120
-#define RCX 	128
-#define RAX 	136
+#define RBX 	120
+#define RDX 	128
+#define RCX 	136
+#define RAX 	144
 
-#define HANDLER 144
-#define ERRCODE 152
+#define HANDLER 152
+#define ERRCODE 160
 
-#define RIP 	160
-#define RFLAGS 	168
+#define RIP 	168
+#define RFLAGS 	176
 
 /* maybe, maybe, well... */
-#define RSP 	176
-#define SS 	184
+#define RSP 	184
+#define SS 	192
 
 
 #endif

+ 32 - 4
include/kernel/sched.h

@@ -118,13 +118,16 @@ struct task {
 	/* total run time in user space (0 in kernel tasks) */
 	__uint32_t utime;
 
+	/* start time (jiffies) */
+	__uint32_t start_time;
+
 	/* 
 	 * 0 - 16 
 	 * 8 is the default priorty for all user tasks,  
 	 * 0 is for all kernel tasks. 
 	 * */
 
-	__uint32_t priority;
+	__uint32_t nice;
 
 	/* signal (a small bitmap) */
 	__uint32_t signal;
@@ -142,7 +145,6 @@ struct task {
 	/* point to its tss */
 	struct tss *tss;
 
-	__uint64_t rsp0;
 	__uint64_t rsp;
 	__uint64_t rip;
 
@@ -156,8 +158,6 @@ struct task {
 
 extern __uint32_t next_pid;
 
-extern struct task init_task;
-
 /* unlike linux we use a pointer to save current task */  
 extern struct task *current;
 
@@ -168,8 +168,36 @@ int task_init(void);
 void set_tss(struct tss *, __uint64_t);
 
 
+/* registers in interrupt-handler's stack */
+
+struct registers {
+	__uint64_t gs, fs, es, ds;
+
+	__uint64_t r15, r14, r13, r12;
+	__uint64_t r11, r10, r9, r8;
+
+	__uint64_t rdi, rsi, rbp;
+	__uint64_t rbx, rdx, rcx, rax;
+
+	/* user task will push this when interrupt happens (for iretq) */
+	__uint64_t rip;
+	__uint64_t cs;
+
+	__uint64_t rflags;
+
+	__uint64_t rsp;
+	__uint64_t ss;
+};
+
+
 #define ltr(nr) __asm__ __volatile__ ("ltr %%ax" :: "a" (nr))
 
 
+/* kernel/sched.c */
+void ktask_entry(void);
+int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t);
+int do_fork(struct registers *, __uint64_t, __uint64_t, __uint64_t);
+
+
 #endif
 

+ 45 - 0
include/sys/syscall.h

@@ -0,0 +1,45 @@
+/* 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/>.  
+*/
+
+
+
+
+#ifndef _SYSCALL_H
+#define _SYSCALL_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#define SYSCALL_NR 128
+
+
+extern int no_syscall(void);
+
+
+void *syscall_table[SYSCALL_NR] = {
+	[0 ... SYSCALL_NR - 1] = no_syscall
+};
+
+
+#endif
+

+ 4 - 0
init/head.S

@@ -123,6 +123,10 @@ init_done:
 	testq %rax, %rax
 	jnz init_panic
 
+	movq $0, %rax
+
+	int $0x80
+
 init_die:
 	jmp init_die
 

+ 1 - 0
init/main.c

@@ -37,6 +37,7 @@
 
 #include <kernel/sched.h>
 #include <lib/string.h>
+#include <sys/syscall.h>
 
 #include <arch/io.h>
 #include <arch/sys.h>

+ 1 - 1
kernel/Makefile

@@ -1,6 +1,6 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
 
-all: int.o trap.o graphic.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o 
+all: int.o trap.o graphic.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o sys.o 
 
 
 %.o: %.S

+ 3 - 16
kernel/int.c

@@ -85,8 +85,6 @@ int int_init(void) {
 	set_trap_gate(idt_table + 0x13, 0, &simd);
 	set_trap_gate(idt_table + 0x14, 0, &virtualization);
 
-	set_int_gate(idt_table + 0x80, 0, &syscall);
-
 	/* initial PIT timer */
 	outb(0x34, 0x43);
 
@@ -102,6 +100,8 @@ int int_init(void) {
 	register_irq(0x00, irq0x20_interrupt, do_timer);
 	register_irq(0x01, irq0x21_interrupt, do_keyboard);
 
+	set_int_gate(idt_table + 0x80, 1, &syscall);
+
 	schk_leave();
 
 	return 0;
@@ -128,26 +128,13 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 }
 
 
-
-
-void syscall(void) {
-	schk_init();
-
-	print(0x00ff0000, 0x00000000, "int #0x80: system call. \n");
-
-	schk_leave();
-
-	return;
-}
-
-
 void die(char *s, __uint64_t *rsp, char nr) {
 	/* should we use schk here? */
 
 	printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
 	printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
 
-	printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp - RIP);
+	printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp + RIP);
 
 	for (;;) hlt();
 

+ 188 - 31
kernel/sched.c

@@ -32,6 +32,7 @@
 #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>
@@ -42,14 +43,33 @@
 #include <lib/string.h>
 
 
+
+/* kernel/trap.S */
+
+extern void ret_intr(void);
+
 struct task init_task;
 struct task *current;
 
-struct task *task_queues[16];
+struct task *task_lists[16];
 
 __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) {
 	/* initial the init-tss */
 
@@ -62,58 +82,195 @@ int task_init(void) {
 
 	/* 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) {
-	return 0;
+	void *stack = alloc_page();
+
+	if (!stack) 
+		return -ENOMEM;
+	
+	struct registers regs;
+
+	memset(&regs, 0x00, sizeof(regs));
+
+	/* entry function */
+	regs.rcx = (__uint64_t) entry;
+	
+	/* argument */
+	regs.rdi = arg;
+
+	return do_fork(&regs, (__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;
 }
 
 

+ 40 - 0
kernel/sys.c

@@ -0,0 +1,40 @@
+/* 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 <kernel/graphic.h>
+
+
+int no_syscall(void) {
+	printk(0x00ffffff, 0x00000000, "no such system call. ");
+
+	return -ENOSYS;
+}
+
+

+ 3 - 13
kernel/timer.S

@@ -30,19 +30,9 @@ do_timer:
 
 	incq jiffies(%rip)
 
-	movq time_to_sched(%rip), %rax
-	decq %rax
+//	callq do_clock
 
-	cmpq $0, %rax
-	
-	jne dt_ret
-
-	callq do_clock
-
-	movq $2, %rax
-
-dt_ret:
-	movq %rax, time_to_sched(%rip)
+	movq $1, time_to_sched(%rip)
 
 	ret
 
@@ -60,5 +50,5 @@ jiffies:
 /* schedule time: 0.0200 s = 20 jiffies */
 
 time_to_sched:
-	.quad 100
+	.quad 1
 

+ 48 - 0
kernel/trap.S

@@ -64,6 +64,7 @@ error_code:
 	pushq %rax
 	pushq %rcx
 	pushq %rdx
+	pushq %rbx
 
 	pushq %rbp
 	pushq %rsi
@@ -137,6 +138,8 @@ restore_all:
 	popq %rdi
 	popq %rsi
 	popq %rbp
+
+	popq %rbx
 	popq %rdx
 	popq %rcx
 
@@ -420,6 +423,51 @@ ret_intr:
 	iretq
 
 
+.globl syscall
+
+syscall:
+	/* we can't use error_code here because error_code will change %rax */
+
+	pushq %rax
+	pushq %rcx
+	pushq %rbx
+	pushq %rdx
+
+	pushq %rbp
+	pushq %rsi
+	pushq %rdi
+
+	pushq %r8
+	pushq %r9
+	pushq %r10
+	pushq %r11
+
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+
+	movq %rbx, %rdi
+	movq %rcx, %rsi
+
+	movq %ds, %rcx
+	pushq %rcx
+
+	movq %es, %rcx
+	pushq %rcx
+
+	movq %fs, %rcx
+	pushq %rcx
+
+	movq %gs, %rcx
+	pushq %rcx
+
+	callq syscall_table(, %rax, 8)
+
+	jmp ret_intr
+
+
+
 .section .rodata
 
 schkfail_msg: