Przeglądaj źródła

7.689u-sched: we are now using the array, not the list (max tasks 128)

rain 5 lat temu
rodzic
commit
c630773811
5 zmienionych plików z 129 dodań i 93 usunięć
  1. 1 0
      LINES
  2. 6 0
      README.md
  3. 59 63
      include/kernel/sched.h
  4. 1 10
      kernel/mm.c
  5. 62 20
      kernel/sched.c

+ 1 - 0
LINES

@@ -29,4 +29,5 @@
 2021-8-08 7.274: 7274 lines, 50 files, rain
 2021-8-09 7.411: 7411 lines, 50 files, rain
 2021-8-12 7.660: 7660 lines, 55 files, rain
+2021-8-12 7.689: 7689 lines, 55 files, rain
 

+ 6 - 0
README.md

@@ -122,3 +122,9 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 7.660u-keyboard:  
 &nbsp;&nbsp;&nbsp;&nbsp;now we use a fifo-buffer, to store key scan codes. (but we don't put it to screen)
 
+7.689u-sched:  
+&nbsp;&nbsp;&nbsp;&nbsp;`switch_to` is now working! it can switch in A and B. (but it is not a `schedule()`) :-), and <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;registers will not change! and we are using the array, not the list now because this is simpiler. <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;the max tasks is default to be 128, but you can make it bigger anyway (because it's a `**` pointer, not a <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;`struct task *` pointer. so it saves the address, and `struct task` allocates when `do_fork()`, not the `task_init()` <br/>
+

+ 59 - 63
include/kernel/sched.h

@@ -34,6 +34,7 @@
 #include <arch/sys.h>
 
 
+#define MAX_TASKS 128
 
 
 /* 
@@ -59,70 +60,67 @@
  * 	(maybe doesn't), so we also should save/restore them. 
  * */
 
-#define RESTOREREGS 			\
-	"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, %%ds\n\t"		\
-	"popq %%r15\n\t"		\
-	"popq %%r14\n\t"		\
-	"popq %%r13\n\t"		\
-	"popq %%r12\n\t"		\
-	"popq %%r11\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 the rip, cs, ss, rsp */	\
-	"addq $16, %%rsp\n\t"		\
-	"popfq\n\t"			\
-	"addq $16, %%rsp\n\t"
-
-
-#define SAVEREGS 			\
-	"subq $16, %%rsp\n\t"		\
-	"pushfq\n\t"			\
-	"subq $16, %%rsp\n\t"		\
-	"pushq %%rax\n\t"		\
-	"pushq %%rcx\n\t"		\
-	"pushq %%rdx\n\t"		\
-	"pushq %%rbx\n\t"		\
-	"pushq %%rbp\n\t"		\
-	"pushq %%rsi\n\t"		\
-	"pushq %%rdi\n\t"		\
-	"pushq %%r8\n\t"		\
-	"pushq %%r9\n\t"		\
-	"pushq %%r10\n\t"		\
-	"pushq %%r11\n\t"		\
-	"pushq %%r12\n\t"		\
-	"pushq %%r13\n\t"		\
-	"pushq %%r14\n\t"		\
-	"pushq %%r15\n\t"		\
-	"movq %%ds, %%rax\n\t"		\
-	"pushq %%rax\n\t"		\
-	"movq %%es, %%rax\n\t"		\
-	"pushq %%rax\n\t"		\
-	"movq %%fs, %%rax\n\t"		\
-	"pushq %%rax\n\t"		\
-	"movq %%gs, %%rax\n\t"		\
+#define SAVE_REGS 		\
+	"subq $16, %%rsp\n\t"	\
+	"pushfq\n\t"		\
+	"subq $16, %%rsp\n\t"	\
+	"pushq %%rax\n\t"	\
+	"pushq %%rcx\n\t"	\
+	"pushq %%rdx\n\t"	\
+	"pushq %%rbx\n\t"	\
+	"pushq %%rbp\n\t"	\
+	"pushq %%rsi\n\t"	\
+	"pushq %%rdi\n\t"	\
+	"pushq %%r8\n\t"	\
+	"pushq %%r9\n\t"	\
+	"pushq %%r10\n\t"	\
+	"pushq %%r11\n\t"	\
+	"pushq %%r12\n\t"	\
+	"pushq %%r13\n\t"	\
+	"pushq %%r14\n\t"	\
+	"pushq %%r15\n\t"	\
+	"movq %%ds, %%rax\n\t"	\
+	"pushq %%rax\n\t"	\
+	"movq %%es, %%rax\n\t"	\
+	"pushq %%rax\n\t"	\
+	"movq %%fs, %%rax\n\t"	\
+	"pushq %%rax\n\t"	\
+	"movq %%gs, %%rax\n\t"	\
 	"pushq %%rax\n\t"
 
+#define RESTORE_REGS 		\
+	"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, %%ds\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"	\
+	"addq $16, %%rsp\n\t"	\
+	"popfq\n\t"		\
+	"addq $16, %%rsp\n\t"
+
 
 #define switch_to(next) 			\
 	__asm__ __volatile__ (			\
 		"cli\n\t"			\
-	/* save frame to the old stack */	\
-		"pushq %%rbp\n\t"		\
-		"pushq %%rax\n\t"		\
+		SAVE_REGS			\
 		"movq %%rsp, %0\n\t"		\
 		"movq %2, %%rsp\n\t"		\
 		"leaq 1f(%%rip), %%rax\n\t"	\
@@ -148,14 +146,14 @@
 	/* else, we will return to 1f */	\
 		"retq\n\t"			\
 		"1:\n\t"			\
-		"popq %%rax\n\t"		\
-		"popq %%rbp\n\t"		\
+		RESTORE_REGS			\
 		: "=m" (current->rsp), 		\
 		  "=m" (current->rip)		\
 		: "m" ((next)->rsp), 		\
 		  "m" ((next)->rip), 		\
 		  "r" ((long) (next)->dataseg),	\
 		  "r" ((next))			\
+		: "ax"				\
 	)
 
 
@@ -181,7 +179,7 @@ struct task {
 	/* you can schedule this task any time */
 #define STATE_READY		4
 
-	__uint32_t state;
+	__uint16_t state;
 
 	/* total run time in kernel space (jiffies) */
 	__uint32_t ktime;
@@ -222,8 +220,6 @@ struct task {
 	__uint64_t cr3;
 
 	__uint16_t dataseg, codeseg;
-
-	struct task *next;
 };
 
 

+ 1 - 10
kernel/mm.c

@@ -185,20 +185,11 @@ void *mapping(void *addr, __uint64_t attr) {
 		if (!PT_ADDR[i]) {
 			PT_ADDR[i] = (__uint64_t) addr | attr;
 			invalidate();
-			goto get_vaddr;
+			return (void *) (i << PAGE_SHIFT);
 		}
 	}
 
 	return NULL;
-
-get_vaddr:
-	/* get virtual address */
-	/* drunk, do it later. */
-
-	__uint64_t vaddr = i << PAGE_SHIFT;
-
-	return (void *) vaddr;
-
 #undef PT_ADDR
 }
 

+ 62 - 20
kernel/sched.c

@@ -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;
 }