Przeglądaj źródła

5.039u-sched: fix a bug of kmalloc(), and write do_fork() again

rain 5 lat temu
rodzic
commit
6fd3352cdc
12 zmienionych plików z 124 dodań i 91 usunięć
  1. 2 0
      LINES
  2. 14 11
      README.md
  3. 9 0
      include/arch/sys.h
  4. 3 0
      include/kernel/init.h
  5. 7 0
      include/kernel/mm.h
  6. 1 1
      include/kernel/sched.h
  7. 6 1
      include/sys/syscall.h
  8. 1 19
      init/main.c
  9. 3 0
      kernel/int.c
  10. 46 45
      kernel/mm.c
  11. 31 13
      kernel/sched.c
  12. 1 1
      kernel/trap.S

+ 2 - 0
LINES

@@ -21,3 +21,5 @@
 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
+2021-7-26 5.039: 5039 lines, 41 files, rain
+

+ 14 - 11
README.md

@@ -9,29 +9,29 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 ### Changes
 
 0.066u-boot:  
-&nbsp;&nbsp;&nbsp;&nbsp;upload `boot.asm` -- it puts 'Loading System' on screen, and die. 
-&nbsp;&nbsp;&nbsp;&nbsp;see 'How XNIX and other operating systems startup' if you are 
+&nbsp;&nbsp;&nbsp;&nbsp;upload `boot.asm` -- it puts 'Loading System' on screen, and die. <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;see 'How XNIX and other operating systems startup' if you are <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;interested in it. 
 
 
 0.228u-boot:  
-&nbsp;&nbsp;&nbsp;&nbsp;update `boot.asm`: it reads the second sector of boot-driver (loader) and jumps to it. 
+&nbsp;&nbsp;&nbsp;&nbsp;update `boot.asm`: it reads the second sector of boot-driver (loader) and jumps to it. <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;`loader.asm` puts a message 'Starting Loader' on screen, and die. 
 
 0.431u-boot:  
 &nbsp;&nbsp;&nbsp;&nbsp;`loader.asm` switching to long mode. And it set page map to address 0x00000000. It mapped 4MB memories. 
 
 0.427u-init:  
-&nbsp;&nbsp;&nbsp;&nbsp;`boot.asm` read 64 sectors to `0x00007e00`, and `loader.asm` jumps to it after switching to long-mode. 
+&nbsp;&nbsp;&nbsp;&nbsp;`boot.asm` read 64 sectors to `0x00007e00`, and `loader.asm` jumps to it after switching to long-mode. <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;then, `head.S` displays a message on screen. 
 
 0.437u-init:  
-&nbsp;&nbsp;&nbsp;&nbsp;`head.S` 'return' to init(void), then `init` put a message on screen and return to `head.S`.  
-&nbsp;&nbsp;&nbsp;&nbsp;and then `head.S` check return value of init, if not zero, it will put a message on screen. 
+&nbsp;&nbsp;&nbsp;&nbsp;`head.S` 'return' to init(void), then `init` put a message on screen and return to `head.S`. <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;and then `head.S` check return value of init, if not zero, it will put a message on screen. <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;if no error, it will halted the CPU. 
 
 1.611s-modules:  
-&nbsp;&nbsp;&nbsp;&nbsp;first stable version! 
+&nbsp;&nbsp;&nbsp;&nbsp;first stable version! <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;copy some files from Cunix, now XNIX can put a formated message on screen. 
 
 1.640u-modules:  
@@ -44,11 +44,11 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;new `int.c` intial `idt_table[0x80]`, and set handler to `syscall`. it put a message and die. 
 
 2.015u-traps:  
-&nbsp;&nbsp;&nbsp;&nbsp;add handler for divide-error (always divide by zero). `do_divide_error` write in C, it pucs a message and return. 
+&nbsp;&nbsp;&nbsp;&nbsp;add handler for divide-error (always divide by zero). `do_divide_error` write in C, it pucs a message and return. <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;`divide_error` is an assembly function. it saves all registers, and call `do_divide_error`. then it recover all registers and iretq. 
 
 2.156u-modules:  
-&nbsp;&nbsp;&nbsp;&nbsp;now we can support colouring printf, and I think it's beautifuller than VGA (well...). 
+&nbsp;&nbsp;&nbsp;&nbsp;now we can support colouring printf, and I think it's beautifuller than VGA (well...). <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;...and, loader loaded kernel to 0x100000 (1MB). 
 
 2.040u-modules:  
@@ -65,7 +65,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 
 2.325u-modules:  
 
-&nbsp;&nbsp;&nbsp;&nbsp;now we can use 1024x768 VBE and vsprintf. this is a big change. 
+&nbsp;&nbsp;&nbsp;&nbsp;now we can use 1024x768 VBE and vsprintf. this is a big change. <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;NOTE that interrupts can also work now, but only divide-error and syscall. 
 
 2.327u-int:  
@@ -75,7 +75,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now we support all 20 traps and IRQ. 
 
 3.831u-mm:  
-&nbsp;&nbsp;&nbsp;&nbsp;we can use `alloc_page` and `free_page` to allocate/free memory. 
+&nbsp;&nbsp;&nbsp;&nbsp;we can use `alloc_page` and `free_page` to allocate/free memory. <br/>
 &nbsp;&nbsp;&nbsp;&nbsp;and don't forget to call `mm_init` first. 
 
 3.913u-pci:  
@@ -90,3 +90,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 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. 
 
+5.039u-sched:  
+&nbsp;&nbsp;&nbsp;&nbsp;write `do_fork()` again and do it easier (and also fixed a bug of kmalloc() - didn't be zero before used). default <br/>
+&nbsp;&nbsp;&nbsp;&nbsp;stack size is 64KB. 

+ 9 - 0
include/arch/sys.h

@@ -71,5 +71,14 @@
 #define get_r14() getreg("r14")
 #define get_r15() getreg("r15")
 
+#define get_cr3() ({			\
+	__uint64_t res;			\
+	__asm__ __volatile__ (		\
+		"movq %%cr3, %0\n\t"	\
+		: "=a" (res)		\
+	);				\
+	res;				\
+})
+
 
 #endif

+ 3 - 0
include/kernel/init.h

@@ -43,6 +43,9 @@
 /* slowest: schk will display function info in this mode */
 #define DEBUG_LEVEL		TEST_DEBUG_LEVEL
 
+#define trappoint() 			\
+	printk(0x0000ff00, 0x00000000, "system runs at %s:%s.%d\n", __FILE__, __FUNCTION__, __LINE__);
+
 
 #include <kernel/types.h>
 #include <kernel/errno.h>

+ 7 - 0
include/kernel/mm.h

@@ -139,5 +139,12 @@ void kfree(struct mempool *, void *, __uint64_t);
 void *mapping(void *, __uint64_t);
 
 
+/* put memory info */
+
+void trace_mm(void);
+
+#define invalidate() __asm__ __volatile__ ("movq %%cr3, %%rax\n\tmovq %%rax, %%cr3\n\t" ::: "ax")
+
+
 #endif
 

+ 1 - 1
include/kernel/sched.h

@@ -196,7 +196,7 @@ struct registers {
 /* 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);
+int do_fork(struct registers *, __uint64_t);
 
 
 #endif

+ 6 - 1
include/sys/syscall.h

@@ -34,10 +34,15 @@
 
 
 extern int no_syscall(void);
+extern int do_exit(__uint64_t);
 
 
 void *syscall_table[SYSCALL_NR] = {
-	[0 ... SYSCALL_NR - 1] = no_syscall
+	[0 ... SYSCALL_NR - 1] = no_syscall, 
+
+#define mksyscall(fn, nr) [nr] = fn, 
+
+	mksyscall(do_exit, 0)
 };
 
 

+ 1 - 19
init/main.c

@@ -61,29 +61,11 @@ int init(void) {
 
 	int_init();
 	mm_init();
+
 	task_init();
 
 	sti();
 
-	int *i = kmalloc(&pool, 128);
-
-	*i = 0;
-
-	printk(0x00ffffff, 0x000000, "kmalloc: %q\n", i);
-
-	kfree(&pool, i, 128);
-
-	i = kmalloc(&pool, 128);
-	int *p = kmalloc(&pool, 128);
-
-	printk(0x00ffffff, 0x00000000, "kmalloc: %q, %q\n", i, p);
-
-	kfree(&pool, p, 256);
-
-	i = kmalloc(&pool, 266);
-
-	printk(0x00ffffff, 0x00000000, "kmalloc: %q\n", i);
-
 	return 0;
 }
 

+ 3 - 0
kernel/int.c

@@ -31,6 +31,7 @@
 #include <kernel/graphic.h>
 #include <kernel/int.h>
 #include <kernel/schk.h>
+#include <kernel/mm.h>
 
 #include <arch/x86/int.h>
 #include <arch/io.h>
@@ -136,6 +137,8 @@ void die(char *s, __uint64_t *rsp, char nr) {
 
 	printk(0x00ff0000, 0x00000000, "rip: %q\n", rsp + RIP);
 
+	trace_mm();
+
 	for (;;) hlt();
 
 	return;

+ 46 - 45
kernel/mm.c

@@ -161,6 +161,8 @@ void mm_init(void) {
 
 	pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
 
+	memset(pool.bitmap, 0, 0x200000 / 64 / 8);
+
 	pool.page = alloc_page();
 
 	pool.next = NULL;
@@ -175,62 +177,29 @@ void mm_init(void) {
 void *mapping(void *addr, __uint64_t attr) {
 	__uint64_t i;
 
-	/* well... */
-
-#define PML4E_ADDR ((__uint64_t *) 0x0000)
-#define PDPT_ADDR  ((__uint64_t *) 0x1000)
-#define PT_ADDR    ((__uint64_t *) 0x2000);
-
-	__uint64_t *pml4e = PML4E_ADDR;
-	__uint64_t *pdpt = PDPT_ADDR;
-	__uint64_t *pt = PT_ADDR;
-
-#define TABLE_ENTRIES 512
+	/* we just use the (max) 1 GB memory in kernel (512 pages) */
 
-	for (i = 0; ((__uint64_t *) pt)[i]; i++) {
-		if (i > TABLE_ENTRIES) {
-			/* reload pt */
-			pt = (__uint64_t *) *(++pdpt);
+#define PT_ADDR    ((__uint64_t *) 0x2000)
 
-			if (pdpt == PDPT_ADDR  + TABLE_ENTRIES) {
-				pdpt = (__uint64_t *) *(++pml4e);
-				pt = (__uint64_t *) *pdpt;
-
-				if (pml4e == PML4E_ADDR + TABLE_ENTRIES) 
-					return 0;
-			}
+	for (i = 0; i < 512; i++) {
+		if (!PT_ADDR[i]) {
+			PT_ADDR[i] = (__uint64_t) addr | attr;
+			invalidate();
+			goto get_vaddr;
 		}
 	}
 
-	/* map it with attribute attr */
-
-	pt[i] = (__uint64_t) addr | attr;
+	return NULL;
 
+get_vaddr:
 	/* get virtual address */
+	/* drunk, do it later. */
 
-#define PT_NR 		i
-#define PDPT_NR 	((pdpt - PDPT_ADDR) >> 3)
-#define PML4E_NR	((pml4e - PML4E_ADDR) >> 3)
-
-#define TABLE_SHIFT 9ul
-
-	__uint64_t vaddr = (PT_NR << PAGE_SHIFT)
-	       		+ (PDPT_NR << TABLE_SHIFT << PAGE_SHIFT)
-			+ (PML4E_NR << TABLE_SHIFT << PAGE_SHIFT << TABLE_SHIFT << PAGE_SHIFT);
+	__uint64_t vaddr = i << PAGE_SHIFT;
 
 	return (void *) vaddr;
 
-#undef TABLE_SHIFT
-
-#undef PML4E_NR
-#undef PDPT_NR
-#undef PT_NR
-
-#undef TABLE_ENTRIES
-
 #undef PT_ADDR
-#undef PDPT_ADDR
-#undef PML4E_ADDR
 }
 
 
@@ -288,6 +257,8 @@ void *kmalloc(struct mempool *pool, __uint64_t size) {
 		return pool->page + (i << POOL_ENTRY_SHIFT);
 	}
 
+	printk(0x00ffffff, 0x00000000, "need %d points. \n", points);
+
 	/* it's not so good... */
 
 	for (i = 0; i < pool->entries; i++) {
@@ -299,6 +270,10 @@ repeat:
 		for (j = 0; j < points; j++) {
 			if (bitmap_read(pool->bitmap, i + j)) {
 				i++;
+
+				if (i == pool->entries) 
+					goto panic;
+
 				goto repeat;
 			}
 		}
@@ -306,13 +281,18 @@ repeat:
 		/* it will here when we found enough */
 
 		for (j = 0; j < points; j++) 
-			bitmap_set(pool->bitmap, j, 1);
+			bitmap_set(pool->bitmap, i + j, 1);
 
 		pool->used += points;
 
 		return pool->page + (i << POOL_ENTRY_SHIFT);
 	}
 
+panic:
+
+	printk(0x00ff0000, 0x00000000, "kmalloc: no enough memory (total %d KB, %d bytes used, requested %d bytes)\n", 
+		pool->size >> 10, pool->used << 6, points << 6);
+
 	return NULL;
 }
 
@@ -330,3 +310,24 @@ void kfree(struct mempool *pool, void *p, __uint64_t size) {
 
 	return;
 }
+
+
+void trace_mm(void) {
+	printk(0x0000ff00, 0x00000000, "tracing system memory: \n");
+
+	__uint64_t *cr3 = (__uint64_t *) get_cr3();
+
+	printk(0x0000ff00, 0x00000000, "pml4: 0x%q  pdp: 0x%q  pt: 0x%q\n", 
+		cr3, *cr3 & ~0xff, *(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
+
+	__uint64_t *pt = (__uint64_t *) (*(__uint64_t *) (*cr3 & ~0xff) & ~0xff);
+	int i;
+
+	for (i = 0; i < 512; i++) {
+		if (pt[i]) 
+			printk(0x0000ff00, 0x00000000, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
+	}
+		
+
+	return;
+}

+ 31 - 13
kernel/sched.c

@@ -82,7 +82,13 @@ int task_init(void) {
 
 	/* initial the task struct for init task */
 
-	ktask_create(ktask, 0x12345678);
+	current = task_lists[0];
+
+	if (ktask_create(ktask, 0x12345678) == -ENOMEM) {
+		printk(0x00ff0000, 0x00000000, "ktask_create returns -ENOMEM (no enough memory). ");
+		return 1;
+	}
+		
 
 	switch_to(task_lists[0]);
 
@@ -134,9 +140,10 @@ __asm__(
 		"addq $40, %rsp\n\t"
 		"callq *%rcx\n\t"
 
-		/* return code -> first argument */
-		"movq %rax, %rdi\n\t"
-		"callq do_exit\n\t"
+		/* syscall 0: do_exit */
+		"movq %rax, %rbx\n\t"
+		"movq $0, %rax\n\t"
+		"int $0x80\n\t"
 
 		/* it never here */
 );
@@ -148,11 +155,6 @@ __asm__(
  * */
 
 int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
-	void *stack = alloc_page();
-
-	if (!stack) 
-		return -ENOMEM;
-	
 	struct registers regs;
 
 	memset(&regs, 0x00, sizeof(regs));
@@ -163,7 +165,7 @@ int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
 	/* argument */
 	regs.rdi = arg;
 
-	return do_fork(&regs, (__uint64_t) stack, PAGE_SIZE, 0);
+	return do_fork(&regs, 0);
 }
 
 
@@ -172,7 +174,7 @@ int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
  * 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. 
+ * it needs some arguments. 
  *
  * if now is a user task, its child should be a user task too. 
  *
@@ -180,7 +182,17 @@ int ktask_create(__uint64_t (* entry) (__uint64_t), __uint64_t arg) {
  * popq all these registers when the task runs. 
  * */
 
-int do_fork(struct registers *regs, __uint64_t start_stack, __uint64_t stacksize, __uint64_t dpl) {
+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) 
@@ -254,7 +266,11 @@ int do_fork(struct registers *regs, __uint64_t start_stack, __uint64_t stacksize
 	}
 
 	child->rsp = start_stack - sizeof(struct registers);
-	memcpy((char *) child->rsp, regs, sizeof(struct registers));
+
+	__asm__ __volatile__ (
+		"cld;rep;movsb;\n\t"
+		:: "S" (regs), "D" (child->rsp), "c" (sizeof(struct registers))
+	);
 
 	child->next = NULL;
 
@@ -270,6 +286,8 @@ int do_fork(struct registers *regs, __uint64_t start_stack, __uint64_t stacksize
 	else 
 		t->next = child;
 
+	printk(0x00ffffff, 0x00000000, "task creates: rsp %q, rip %q\n", child->rsp, child->rip);
+
 	return 0;
 }
 

+ 1 - 1
kernel/trap.S

@@ -462,7 +462,7 @@ syscall:
 	movq %gs, %rcx
 	pushq %rcx
 
-	callq syscall_table(, %rax, 8)
+	callq *syscall_table(, %rax, 8)
 
 	jmp ret_intr