Răsfoiți Sursa

7.783u-sched: schedule() is now working

rain 5 ani în urmă
părinte
comite
655eb8fcaf

+ 1 - 0
LINES

@@ -30,4 +30,5 @@
 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
+2021-8-13 7.783: 7783 lines, 55 files, rain
 

+ 3 - 0
README.md

@@ -128,3 +128,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &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/>
 
+7.783u-sched:  
+&nbsp;&nbsp;&nbsp;&nbsp;`schedule()` is now working! 
+

+ 2 - 2
include/kernel/init.h

@@ -34,14 +34,14 @@
 
 /* fastest, but no syslog and schk */
 
-#define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
+// #define DEBUG_LEVEL 		DEFAULT_DEBUG_LEVEL
 
 /* standard */
 
 // #define DEBUG_LEVEL		DEV_DEBUG_LEVEL
 
 /* slowest: schk will display function info in this mode */
-// #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
+#define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 
 #include <modules/tty.h>
 

+ 2 - 2
include/kernel/int.h

@@ -59,11 +59,11 @@ void virtualization(void);
 
 
 
-void register_irq(int, void *, void *);
+void register_irq(int, void *, const void *);
 void unregister_irq(int);
 
 /* kernel/timer.S */
-void do_irq(__uint64_t *, __uint64_t);
+void do_irq(const __uint64_t *, __uint64_t);
 
 void do_timer(void);
 void do_keyboard(void);

+ 59 - 50
include/kernel/sched.h

@@ -117,43 +117,50 @@
 	"addq $16, %%rsp\n\t"
 
 
-#define switch_to(next) 			\
-	__asm__ __volatile__ (			\
-		"cli\n\t"			\
-		SAVE_REGS			\
-		"movq %%rsp, %0\n\t"		\
-		"movq %2, %%rsp\n\t"		\
-		"leaq 1f(%%rip), %%rax\n\t"	\
-		"movq %%rax, %1\n\t"		\
-	/* fake return address */		\
-		"pushq %3\n\t"			\
-	/* update the current */		\
-		"movq %5, current\n\t"		\
-	/* get registers in the new stack 	\
-	 * note that do_fork also do things  	\
-	 * looks like SAVEREGS (copy 		\
-	 * struct registers to stack) */	\
-		"movq %4, %%rax\n\t"		\
-		"movq %%rax, %%ds\n\t"		\
-		"movq %%rax, %%es\n\t"		\
-		"movq %%rax, %%fs\n\t"		\
-		"movq %%rax, %%gs\n\t"		\
-		"sti\n\t"			\
-	/* note that if this is the first 	\
-	 * run time for this task, we will  	\
-	 * return to 'ret_intr' or 		\
-	 * ktask_entry */			\
-	/* else, we will return to 1f */	\
-		"retq\n\t"			\
-		"1:\n\t"			\
-		RESTORE_REGS			\
-		: "=m" (current->rsp), 		\
-		  "=m" (current->rip)		\
-		: "m" ((next)->rsp), 		\
-		  "m" ((next)->rip), 		\
-		  "r" ((long) (next)->dataseg),	\
-		  "r" ((next))			\
-		: "ax"				\
+#define switch_to(next) 				\
+	__asm__ __volatile__ (				\
+		"cli\n\t"				\
+		SAVE_REGS				\
+		"movq %%rsp, %0\n\t"			\
+		"movq %2, %%rsp\n\t"			\
+		"leaq 1f(%%rip), %%rax\n\t"		\
+		"movq %%rax, %1\n\t"			\
+	/* fake return address */			\
+		"pushq %3\n\t"				\
+	/* get registers in the new stack 		\
+	 * note that do_fork also do things  		\
+	 * looks like SAVEREGS (copy 			\
+	 * struct registers to stack) */		\
+		"movq %4, %%rax\n\t"			\
+		"movq %%rax, %%ds\n\t"			\
+		"movq %%rax, %%es\n\t"			\
+		"movq %%rax, %%fs\n\t"			\
+		"movq %%rax, %%gs\n\t"			\
+		"cmpq $0, need_open_clock(%%rip)\n\t"	\
+		"je 2f\n\t"				\
+		"movq $0, need_open_clock(%%rip)\n\t"	\
+		"movq $0x20, %%rax\n\t"			\
+		"movq $0x20, %%rdx\n\t"			\
+		"outb %%al, %%dx\n\t"			\
+		"2:\n\t"				\
+		"sti\n\t"				\
+	/* note that if this is the first 		\
+	 * run time for this task, we will  		\
+	 * return to 'ret_intr' or 			\
+	 * ktask_entry */				\
+	/* else, we will return to 1f */		\
+	/* update the current */			\
+		"movq %5, current\n\t"			\
+		"retq\n\t"				\
+		"1:\n\t"				\
+		RESTORE_REGS				\
+		: "=m" (current->rsp), 			\
+		  "=m" (current->rip)			\
+		: "m" ((next)->rsp), 			\
+		  "m" ((next)->rip), 			\
+		  "r" ((long) (next)->dataseg),		\
+		  "r" ((next))				\
+		: "ax"					\
 	)
 
 
@@ -165,19 +172,22 @@
 struct task {
 
 	/* died */
-#define STATE_ZOMBIE		0
+#define STATE_ZOMBIE	0
 
 	/* it's running on cpu */
-#define STATE_RUNNING 		1
+#define STATE_RUNNING 	1
 
-	/* waiting, but unlike STATE_UNINTERRUPTIBLE, you can wake_up() */
-#define STATE_INTERRUPTIBLE 	2
-
-	/* waiting, and can't wake_up() */
-#define STATE_UNINTERRUPTIABLE	3
+	/* waiting for some resource, and can't be schedule() */
+#define STATE_WAIT 	3
 
 	/* you can schedule this task any time */
-#define STATE_READY		4
+#define STATE_READY	4
+
+	/* you can't run this task, in this time */
+#define STATE_FINISH 	5
+
+	/* task is creating */
+#define STATE_IDE	6
 
 	__uint16_t state;
 
@@ -190,11 +200,10 @@ struct task {
 	/* start time (jiffies) */
 	__uint32_t start_time;
 
-	/* 
-	 * 0 - 16 
-	 * 8 is the default priorty for all user tasks,  
-	 * 0 is for all kernel tasks. 
-	 * */
+	/* by nice, 0.01 - 0.1 sec, default 2 (nice 2) */
+	__uint8_t counter;
+
+	/* 0 - 16 */
 
 	__uint32_t nice;
 

+ 1 - 1
include/kernel/schk.h

@@ -57,7 +57,7 @@ void __stack_chk_fail(void);
 
 #elif DEBUG_LEVEL == DEV_DEBUG_LEVEL
 
-#define schk_init() __uint64_t old_sp = get_rsp();
+#define schk_init() 	__uint64_t old_sp = get_rsp();
 
 #elif DEBUG_LEVEL == DEFAULT_DEBUG_LEVEL
 

+ 1 - 1
include/kernel/vsprintf.h

@@ -36,7 +36,7 @@
 
 /* kernel/vsprintf.c */
 
-int vsprintf(char *, char *, va_list);
+int vsprintf(char *, const char *, va_list);
 int itos(char *, unsigned long, int, int, int, int);
 
 

+ 2 - 2
init/main.c

@@ -64,10 +64,10 @@ int init(void) {
 	int_init();
 	mm_init();
 
-	task_init();
-
 	sti();
 
+	task_init();
+
 	printk(white, "System boots from the device %b: ", bootarg->bootdev);
 
 	if (bootarg->bootdev & 0x80) 

+ 22 - 23
kernel/int.c

@@ -129,9 +129,8 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 }
 
 
-void die(char *s, __uint64_t *rsp, char nr) {
+void die(const char *s, const __uint64_t *rsp, char nr) {
 	/* should we use schk here? */
-
 	printk(red, "int #%b: %s. \n", nr, s);
 
 	printk(red, "rsp now: %q\n", rsp);
@@ -167,7 +166,7 @@ void die(char *s, __uint64_t *rsp, char nr) {
 }
 
 
-void do_divide_error(__uint64_t *rsp, __uint64_t errcode) {
+void do_divide_error(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("divide error", rsp, 0);
@@ -178,7 +177,7 @@ void do_divide_error(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_debug(__uint64_t *rsp, __uint64_t errcode) {
+void do_debug(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("debug", rsp, 1);
@@ -188,7 +187,7 @@ void do_debug(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_nmi(__uint64_t *rsp, __uint64_t errcode) {
+void do_nmi(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("nmi", rsp, 2);
@@ -198,7 +197,7 @@ void do_nmi(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_int3(__uint64_t *rsp, __uint64_t errcode) {
+void do_int3(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("int3", rsp, 3);
@@ -208,7 +207,7 @@ void do_int3(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_overflow(__uint64_t *rsp, __uint64_t errcode) {
+void do_overflow(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("overflow", rsp, 4);
@@ -218,7 +217,7 @@ void do_overflow(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_bound(__uint64_t *rsp, __uint64_t errcode) {
+void do_bound(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("bound", rsp, 5);
@@ -228,7 +227,7 @@ void do_bound(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_ud(__uint64_t *rsp, __uint64_t errcode) {
+void do_ud(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("undefined instruction", rsp, 6);
@@ -238,7 +237,7 @@ void do_ud(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_no_fpu(__uint64_t *rsp, __uint64_t errcode) {
+void do_no_fpu(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("fpu not present", rsp, 7);
@@ -248,7 +247,7 @@ void do_no_fpu(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_double_fault(__uint64_t *rsp, __uint64_t errcode) {
+void do_double_fault(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("double fault", rsp, 8);
@@ -258,7 +257,7 @@ void do_double_fault(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_fpu_overrun(__uint64_t *rsp, __uint64_t errcode) {
+void do_fpu_overrun(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("fpu overrun", rsp, 9);
@@ -268,7 +267,7 @@ void do_fpu_overrun(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_invalid_tss(__uint64_t *rsp, __uint64_t errcode) {
+void do_invalid_tss(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("invalid tss", rsp, 10);
@@ -278,7 +277,7 @@ void do_invalid_tss(__uint64_t *rsp, __uint64_t errcode) {
 	return;
 }
 
-void do_no_segment(__uint64_t *rsp, __uint64_t errcode) {
+void do_no_segment(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("no such segment", rsp, 11);
@@ -289,7 +288,7 @@ void do_no_segment(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_stack(__uint64_t *rsp, __uint64_t errcode) {
+void do_stack(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("stack error", rsp, 12);
@@ -300,7 +299,7 @@ void do_stack(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_gp(__uint64_t *rsp, __uint64_t errcode) {
+void do_gp(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("general protection", rsp, 13);
@@ -311,7 +310,7 @@ void do_gp(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_page_fault(__uint64_t *rsp, __uint64_t errcode) {
+void do_page_fault(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("page fault", rsp, 14);
@@ -322,7 +321,7 @@ void do_page_fault(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_reserved(__uint64_t *rsp, __uint64_t errcode) {
+void do_reserved(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("reserved", rsp, 15);
@@ -333,7 +332,7 @@ void do_reserved(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_fpu_error(__uint64_t *rsp, __uint64_t errcode) {
+void do_fpu_error(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("fpu error", rsp, 16);
@@ -344,7 +343,7 @@ void do_fpu_error(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_alignment(__uint64_t *rsp, __uint64_t errcode) {
+void do_alignment(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("alignment error", rsp, 17);
@@ -355,7 +354,7 @@ void do_alignment(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_machine(__uint64_t *rsp, __uint64_t errcode) {
+void do_machine(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("machine check", rsp, 18);
@@ -366,7 +365,7 @@ void do_machine(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_simd(__uint64_t *rsp, __uint64_t errcode) {
+void do_simd(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("simd error", rsp, 19);
@@ -377,7 +376,7 @@ void do_simd(__uint64_t *rsp, __uint64_t errcode) {
 }
 
 
-void do_virtualization(__uint64_t *rsp, __uint64_t errcode) {
+void do_virtualization(const __uint64_t *rsp, __uint64_t errcode) {
 	schk_init();
 
 	die("virtualization error", rsp, 20);

+ 2 - 2
kernel/irq.c

@@ -40,7 +40,7 @@
 struct irq_entry irq_table[MAX_IRQ_NR];
 
 
-void register_irq(int nr, void *entry, void *handler) {
+void register_irq(int nr, void *entry, const void *handler) {
 	schk_init();
 
 	__uint8_t mask;
@@ -89,7 +89,7 @@ void unregister_irq(int nr) {
 }
 
 
-void do_irq(__uint64_t *rsp, __uint64_t nr) {
+void do_irq(const __uint64_t *rsp, __uint64_t nr) {
 	schk_init();
 
 	void (*fn) (void) = irq_table[nr - 0x20].handler;

+ 6 - 0
kernel/keyboard.c

@@ -31,6 +31,7 @@
 #include <modules/tty.h>
 #include <lib/string.h>
 #include <lib/fifo.h>
+#include <kernel/schk.h>
 
 #include <kernel/mm.h>
 #include <kernel/keyboard.h>
@@ -45,11 +46,16 @@ struct fifo kbd_fifo;
 
 
 errno_t kbd_init(void) {
+	schk_init();
+
 	__fifo_buf_t *key_buf = kmalloc(&pool, MAX_KEYS * sizeof(__fifo_buf_t));
+
 	memset(key_buf, 0x00, MAX_KEYS * sizeof(__uint64_t));
 
 	fifo_init(&kbd_fifo, key_buf, MAX_KEYS);
 
+	schk_leave();
+
 	return 0;
 }
 

+ 1 - 3
kernel/mm.c

@@ -34,6 +34,7 @@
 
 #include <lib/bitmap.h>
 #include <lib/string.h>
+#include <kernel/schk.h>
 
 #include <arch/x86/int.h>
 #include <arch/x86/paging.h>
@@ -135,10 +136,7 @@ void mm_init(void) {
 	/* 2MB */
 	bitmap_set(mm->bitmap, 1, 1);
 
-	/* so next free page is 10MB (0xa00000) */
-
 	/* now we setup that memory pool */
-
 	/* default pool size: 2MB (1 page) */
 
 	pool.size = 0x200000;

+ 1 - 1
kernel/panic.c

@@ -37,7 +37,7 @@
 
 
 
-void panic(char *s) {
+void panic(const char *s) {
 	printk(red, "kernel panic: %s\n", s);
 	printk(red, "for more debug information, see syslog 0x90000\n");
 

+ 12 - 20
kernel/rlib/bitman.c

@@ -25,39 +25,31 @@
 
 
 #include <lib/rlib.h>
+#include <kernel/schk.h>
 
-
-/*
-	find a new bit in bm->bitmap. 
-
-	STEPS: 
-		1. save free position. 
-		2. find a new free position. 
-		3. save new free position. 
-*/
 unsigned int bitman_alloc(struct bitmap_manager *bm) {
+	schk_init();
+
 	unsigned int p = bm->next_free;
 	bitmap_set(bm->bitmap, bm->next_free, 1);
 
 	for (; bitmap_read(bm->bitmap, bm->next_free) == 1; bm->next_free++);
 
+	schk_leave();
+	
 	return p;
 }
 
-
-/*
-	free a bit. 
-
-	STEPS: 
-		1. free bit pos. 
-		2. if pos is lowwer than bm->next_free
-			2a. set bm->next_free = pos. 
-*/
 void bitman_free(struct bitmap_manager *bm, unsigned int pos) {
+	schk_init();
+
 	bitmap_set(bm->bitmap, pos, 0);
 
-	if (pos < bm->next_free) {
+	if (pos < bm->next_free) 
 		bm->next_free = pos;
-	}
+
+	schk_leave();
+
 	return;
 }
+

+ 15 - 5
kernel/rlib/bitmap.c

@@ -1,16 +1,16 @@
 /* Copyright (C) 2021 Rain */
 
-/* This file is part of Cunix. */
+/* This file is part of XNIX. */
 
 /* 
-  Cunix is free software: you can redistribute it and/or modify 
+  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. 
 */
 
 /*
-  Cunix is distributed in the hope that it will be useful, 
+  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. 
@@ -18,15 +18,19 @@
 
 /*
   You should have received a copy of the GNU General Public License 
-   along with Cunix. If not, see <https://www.gnu.org/licenses/>.  
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
 */
 
 
 
 
-#include <kernel/types.h>
+#include <lib/rlib.h>
+#include <kernel/schk.h>
+
 
 void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value) {
+	schk_init();
+
 	unsigned char mask = 0x80 >> (pos & 0x7);
 
 	if (value) 
@@ -34,13 +38,19 @@ void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value) {
 	
 	else 
 		bitmap[pos >> 3] &= ~mask;
+
+	schk_leave();
 	
 	return;
 }
 
 char bitmap_read(unsigned char *bitmap, unsigned int pos) {
+	schk_init();
+
 	unsigned char mask = 0x80 >> (pos & 0x7);
 
+	schk_leave();
+
 	return (mask & bitmap[pos >> 3]) == mask ? 1 : 0;
 }
 

+ 13 - 1
kernel/rlib/fifo.c

@@ -25,39 +25,51 @@
 
 
 #include <kernel/init.h>
-#include <kernel/errno.h>
 #include <kernel/types.h>
+#include <kernel/schk.h>
 
 #include <lib/fifo.h>
 
 
 void fifo_init(struct fifo *fifo, __fifo_buf_t *buf, int total) {
+	schk_init();
+
 	fifo->buf = buf;
 	fifo->total = total;
 	
 	fifo->next_wr = fifo->next_rd = 0;
 	fifo->need_read = 0;
 
+	schk_leave();
+
 	return;
 }
 
 
 __fifo_buf_t fifo_get(struct fifo *fifo) {
+	schk_init();
+
 	if (!fifo->need_read) 
 		return 0;
 
 	fifo->need_read--;
 
+	schk_leave();
+
 	return fifo->buf[fifo->next_rd++];
 }
 
 
 void fifo_put(struct fifo *fifo, __fifo_buf_t value) {
+	schk_init();
+
 	fifo->buf[fifo->next_wr] = value;
 	fifo->need_read++;
 
 	if (fifo->next_wr++ == fifo->total) 
 		fifo->next_wr = 0;
 
+	schk_leave();
+
 	return;
 }

+ 84 - 6
kernel/sched.c

@@ -33,6 +33,7 @@
 #include <kernel/panic.h>
 #include <kernel/int.h>
 #include <kernel/sched.h>
+#include <kernel/schk.h>
 
 #include <kernel/mm.h>
 #include <arch/x86/int.h>
@@ -72,7 +73,6 @@ __uint64_t task_a(__uint64_t arg) {
 		}
 
 		putc('A', white);
-		switch_to(tasks[1]);
 	}
 }
 
@@ -88,7 +88,21 @@ __uint64_t task_b(__uint64_t arg) {
 		}
 
 		putc('B', white);
-		switch_to(tasks[0]);
+	}
+}
+
+__uint64_t task_c(__uint64_t arg) {
+	register __uint64_t c = 12345670;
+
+	printk(white, "task b is running: %x\n", arg);
+
+	for (;;) {
+		if (c != 12345670) {
+			printk(white, "c is now %d\n", c);
+			panic("task_c: c changed. ");
+		}
+
+		putc('C', white);
 	}
 }
 
@@ -109,6 +123,9 @@ int task_init(void) {
 	if (ktask_create(task_b, 0x23456789)) 
 		panic("task_init: failed to create task b. \n");
 
+	if (ktask_create(task_c, 0x34567890)) 
+		panic("task_init: failed to create task c. \n");
+
 	current = &init_task;
 
 	switch_to(tasks[0]);
@@ -157,6 +174,7 @@ __asm__(
 		"addq $16, %rsp\n\t"
 		"popfq\n\t"
 		"addq $16, %rsp\n\t"
+		"sti\n\t"
 
 		"callq *%rcx\n\t"
 
@@ -231,7 +249,7 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 	/* current is this task's father */
 	memcpy(child, current, sizeof(struct task));
 
-	child->state = STATE_UNINTERRUPTIABLE;
+	child->state = STATE_IDE;
 
 	child->utime = child->ktime = 0;
 
@@ -272,8 +290,6 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 		child->dataseg = KERNEL_DS;
 		child->codeseg = KERNEL_CS;
 
-		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
@@ -294,7 +310,6 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 //		child->rip = (__uint64_t) &ret_intr;
 		child->dataseg = USER_DS;
 		child->codeseg = USER_CS;
-		child->nice = 1;
 
 		regs->ss = regs->fs = regs->es = regs->ds = regs->gs = USER_DS;
 		regs->cs = USER_CS;
@@ -303,6 +318,10 @@ int do_fork(struct registers *regs, __uint64_t dpl) {
 		regs->rsp = start_stack - sizeof(struct registers);
 	}
 
+	/* 20 ms, default */
+	child->counter = 2;
+
+	child->nice = 2;
 	child->rsp = start_stack - sizeof(struct registers);
 
 	__asm__ __volatile__ (
@@ -339,3 +358,62 @@ void set_tss(struct tss *table, __uint64_t dpl) {
 
 	return;
 }
+
+/* well... but this is ok */
+static char need_open_clock = 0;
+
+void schedule(void) {
+	/* find and switch to a new task */
+
+	__uint32_t pid;
+	__uint32_t best = 17;
+	__uint32_t bestpid = 0;
+
+	for (pid = 0; pid < MAX_TASKS; pid++) {
+		if (tasks[pid] && tasks[pid]->state == STATE_READY 
+				&& tasks[pid]->nice < best) {
+			bestpid = pid;
+			best = tasks[pid]->nice;
+		}
+	}
+
+	if (best == 17) {
+		/* there must are some STATE_FINISH, change them to STATE_READY 
+		 * and get bestpid again. 
+		 * */
+		
+		int i;
+		best = 17;
+
+		for (i = 0; i < MAX_TASKS; i++) {
+			if (tasks[i] && tasks[i]->state == STATE_FINISH) {
+				tasks[i]->state = STATE_READY;
+				tasks[i]->counter = tasks[i]->nice;
+
+				if (tasks[i]->nice < best) {
+					bestpid = i;
+					best = tasks[pid]->nice;
+				}
+			}
+		}
+	}
+
+	/* will sti() in switch_to() */
+
+	switch_to(tasks[bestpid]);
+
+	return;
+}
+
+void do_clock(void) {
+	current->counter--;
+
+	if (!current->counter) {
+		current->state = STATE_FINISH;
+		need_open_clock = 1;
+
+		schedule();
+	}
+
+	return;
+}

+ 1 - 11
kernel/timer.S

@@ -30,9 +30,7 @@ do_timer:
 
 	incq jiffies(%rip)
 
-//	callq do_clock
-
-	movq $1, time_to_sched(%rip)
+	callq do_clock
 
 	ret
 
@@ -44,11 +42,3 @@ do_timer:
 jiffies:
 	.quad 0
 
-.globl time_to_sched
-
-/* time to next schedule */
-/* schedule time: 0.0200 s = 20 jiffies */
-
-time_to_sched:
-	.quad 1
-

+ 2 - 2
kernel/vsprintf.c

@@ -61,7 +61,7 @@
  *	64:	%q: a 16-based unsigned long  number. 
  * */
 
-int vsprintf(char *buf, char *fmt, va_list args) {
+int vsprintf(char *buf, const char *fmt, va_list args) {
 	schk_init();
 
 	/* ri: real index in buf */
@@ -91,7 +91,7 @@ int vsprintf(char *buf, char *fmt, va_list args) {
 				/* it's not so difficult, I think */
 
 				/* get pointer and save in s */
-				char *s = va_arg(args, char *);
+				const char *s = va_arg(args, char *);
 
 				while (*s) {
 					buf[ri++] = *s;

+ 1 - 1
modules/tty.c

@@ -65,7 +65,7 @@ void syslog(const char *s) {
 	int i;
 
 	for (i = 0; s[i]; i++) {
-		if (syslog_index + i == 32768) 
+		if (syslog_index == 32768) 
 			syslog_index = 0;
 	
 		*(syslog_index + SYSLOG_BUFFER) = s[i];