ソースを参照

evx: add a new function kmlog to write logs before console initialize

rain 4 年 前
親
コミット
15bf4d4be9

+ 2 - 1
Kconfig

@@ -1,3 +1,4 @@
 [	
-	{"CONFIG_64BIT": "Enable / disable 64-bits kernel support"}
+	{"CONFIG_64BIT": "Enable / disable 64-bits kernel support"}, 
+	{"CONFIG_KMLOG_LEVEL": "Messages smaller than this level will be ignored"}
 ]

+ 6 - 6
Makefile

@@ -58,13 +58,9 @@ obj-i 			+= $(PWD)/modules/
 
 
 
-_all:
+_all: include/config.h 
 	@rm .objlist -f 
 
-	@if [ ! -f "include/config.h" ]; then 		\
-		scripts/do_mkconfig.py; 		\
-	fi;
-
 	@for d in $(obj-i); do 				\
 		$(MAKE) -C $$d D=$$d $(MAKEFLAGS); 	\
 	done
@@ -73,12 +69,16 @@ _all:
 	@$(MAKE) -C $(ARCHDIR)/ boot 
 
 
+include/config.h: 
+	@scripts/do_mkconfig.py 
+
+
 
 .PHONY clean:
 	@find ./ -name "*.o" \
 	       -o -name "*.bin"	| xargs rm -f
 	@$(MAKE) -C $(ARCHDIR)/ clean
-	@rm .objlist include/config.h -f 
+
 
 else
 

+ 14 - 0
arch/x86/include/arch/string.h

@@ -51,6 +51,20 @@
 })
 
 
+#define strcpy(dest, src) ({ 				\
+	__asm__ volatile (				\
+		"cld\n\t"				\
+		"1:\n\t"				\
+		"lodsb\n\t"				\
+		"stosb\n\t" 				\
+		"testb %%al, %%al\n\t" 			\
+		"jnz 1b\n\t"				\
+		: "D" (dest), "S" (src)			\
+		:: "ax"					\
+	);						\
+})
+
+
 #endif
 
 #endif

+ 43 - 16
arch/x86/kernel/arch_mm.c

@@ -23,6 +23,7 @@
 #include <kernel/module.h>
 #include <kernel/mm.h>
 #include <kernel/kernel.h>
+#include <kernel/kmlog.h>
 
 #include <arch/archinfo.h>
 #include <arch/mm.h>
@@ -33,6 +34,8 @@
 
 
 static int __init init_mapping_32m(void);
+static int __init init_kmlog_buf(void);
+
 __archptr_t freeptr;
 
 void *kernel_end;
@@ -44,7 +47,7 @@ void *kernel_end;
 int arch_mm_init(void) {
 	struct e820_entry 	*e;
 	struct zone 		*z;
-	struct page 		*p;
+	struct page 		*p, *last;
 
 	int 			i;
 
@@ -53,16 +56,14 @@ int arch_mm_init(void) {
 	total_pages = 0;
 
 	init_mapping_32m();
+	init_kmlog_buf();
+
 	kernel_end = (void *) (KERNEL_OFFSET | (__archptr_t) &_end);
 
 	/* put them there, after that, are the page structures. */
 	z = kmem_zones;
-	p = kmem_pages;
-
-	memset(z, 0, sizeof(struct zone) * 2);
 
-	z[0].type = ZONETYPE_RAM;
-	z[1].type = ZONETYPE_OTHER;
+	memset(z, 0, sizeof(struct zone) * 16);
 
 
 	for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
@@ -75,27 +76,44 @@ int arch_mm_init(void) {
 
 		addr = PAGE_ALIGN(HIGH_AND_LOW(e->addrh, e->addrl, 32));
 
-		if (z[e->type].start_addr > (void *) addr) 
-			z[e->type].start_addr = (void *) addr;
-
-
 		npages = PAGE_NR(HIGH_AND_LOW(e->lenh, e->lenl, 32));
-		z[e->type].total_pages += npages;
+		
+		z->start_addr = (void *) addr;
+		z->type = e->type;
+		z->total_pages += npages;
 
 		/* any page */
 		total_pages += npages;
 
-		/* now initiallize page structures */
+		if (z->type == ZONETYPE_RAM) 
+			nr_ram_pages += npages;
+
+		++z;
+	}
+
+	p = kmem_pages = (struct page *) z;
+	z = kmem_zones;
+
+	for (;;) {
+		if (z->type != ZONETYPE_RAM && z->type != ZONETYPE_OTHER) 
+			break;
+
+		z->pages = p;
 
-		for (i = 0; i < npages; i++) {
+		for (i = 0; i < z->total_pages; i++) {
+			last = p;
 			p->count = 0;
 			p->next = p + 1;
 
-			++p;
+			p++;
 		}
-	}
 
-	first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) (kmem_pages + total_pages)));
+		last->next = NULL;
+
+		z++;
+	}
+	
+	first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) p));
 
 	return 0;
 }
@@ -144,6 +162,15 @@ static int __init init_mapping_32m(void) {
 #endif
 }
 
+static int __init init_kmlog_buf(void) {
+	kmlog_buf = (char *) freeptr;
+	freeptr += KMLOG_SIZE;
+
+	*kmlog_buf = 0;
+
+	return 0;
+}
+
 
 /* stack - 8192 */
 #define END 	0x8e000

+ 1 - 1
docs/x86/memmap.md

@@ -207,7 +207,7 @@ Kernel memory map in x86-64
 0x00037000
 0x00036000
 0x00035000    Free Memory for Early kmalloc() 556Kib Usable
-0x00034000
+0x00034000                                    (Kernel Heap)
 0x00033000
 0x00032000
 0x00031000

+ 1 - 1
include/kernel/kernel.h

@@ -40,7 +40,7 @@
 #endif
 
 
-int printk(char *, ...);
+int kmlog(char *buf, ...);
 int vsprintf(char *buf, const char *fmt, va_list);
 int panic(char *s);
 

+ 62 - 0
include/kernel/kmlog.h

@@ -0,0 +1,62 @@
+/* include/kernel/kernel.h 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx 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) any later version. 
+ * 
+ * evx 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 evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __KMLOG_H
+#define __KMLOG_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+#include <config.h>
+#include <stdarg.h>
+
+int 			kmlog(char *buf, ...);
+
+/* i think this is enough in kernel and not so big, because kernel 
+ * heap in x86 has 512k+ */
+#define KMLOG_SIZE 	32768
+
+extern char 		*kmlog_buf;
+extern int 		kmlog_index;
+
+#define KERN_DEBUG 	"#0"
+#define KERN_INFO 	"#1"
+#define KERN_NOTICE 	"#2"
+#define KERN_WARN 	"#3"
+#define KERN_ERR 	"#4"
+#define KERN_EMERG 	"#5"
+#define KERN_ABANDON 	"#6"
+
+#ifndef CONFIG_KMLOG_LEVEL
+
+#define KMLOG_LEVEL 	-1
+
+#else
+
+#define KMLOG_LEVEL 	CONFIG_KMLOG_LEVEL
+
+#endif
+
+#endif
+
+#endif
+

+ 8 - 6
include/kernel/mm.h

@@ -101,19 +101,24 @@ int 			free_s(void *p, int size);
 int 			kmalloc_report(void);
 int 			dump_pool(struct mempool_dir *);
 
+void 			*early_kmalloc(int nbytes, int align);
+int 			early_free(void *p, int nbytes);
 
-#define free(p) 	
 
+#define free(p) 	early_free(p, 0)
+#define BUDDY_LEVELS 	5
 
 
+extern struct page  	*buddy_pages[BUDDY_LEVELS];
+extern int 		nr_ram_pages;
 
 
 /* sets by arch_mm_init */
 
-extern struct page 	*first_usable_ram;
+extern struct page 	*first_usable_ram, 
+       			*kmem_pages;
 
 #define kmem_zones 	((struct zone *) kernel_end)
-#define kmem_pages 	((struct page *) (kmem_zones + 2))
 
 
 /* they're really early but fast. actually, most free() did nothing
@@ -121,9 +126,6 @@ extern struct page 	*first_usable_ram;
  * WARNING that you should't use align, because we didn't save old 
  * freeptr and it will be cracked by it: not freeable */
 
-void 			*early_kmalloc(int nbytes, int align);
-int 			early_free(void *p, int nbytes);
-
 
 #endif
 

+ 2 - 1
include/stdarg.h

@@ -31,7 +31,8 @@ typedef __builtin_va_list	va_list;
 
 #define va_start(v, l)	 	__builtin_va_start(v, l)
 #define va_arg(v, l) 		__builtin_va_arg(v, l)
-#define va_end(v) 		__builtin_va_end(v);
+#define va_end(v) 		__builtin_va_end(v)
+#define va_copy(d, s) 		__builtin_va_copy(d, s)
 
 #endif
 

+ 1 - 3
init/main.c

@@ -36,9 +36,7 @@ static int do_init_calls(void);
 int __init init(void) {
 	do_init_calls();
 
-	mapto((void *) 1048576, (void *) 0xffffffff);
-
-	printk("%x\n", *(unsigned long *) 0xfffff000);
+	kmlog("first usable page is the %d'th page. \n", first_usable_ram - kmem_pages);
 
 	return 0;
 }

+ 1 - 1
kernel/Makefile

@@ -1,7 +1,7 @@
 # this was called from the Makefile in /, 
 # and we just tell it what objects that he should make. 
 
-obj-i = mm.c vsprintf.c tty.c mempool.c panic.c 
+obj-i = mm.c vsprintf.c tty.c mempool.c panic.c page.c kmlog.c 
 
 target: $(obj-i)
 

+ 86 - 0
kernel/kmlog.c

@@ -0,0 +1,86 @@
+/* kernel/kmlog.c 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx 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) any later version. 
+ * 
+ * evx 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 evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/module.h>
+#include <kernel/kernel.h>
+#include <kernel/kmlog.h>
+
+#include <arch/archinfo.h>
+#include <arch/string.h>
+
+#include <sys/types.h>
+#include <stdarg.h>
+
+
+/* write log to memory, before printk is ready.
+ *
+ * kmlog buffer sets by arch_mm_init (why it is always this 
+ * function...) and **MUST** be cleared by it. 
+ *
+ * the buffer is always in the kernel heap, and it's always 
+ * KMLOG_SIZE bytes long.*/
+
+
+char 	*kmlog_buf;
+int 	kmlog_index = 1;
+
+
+int kmlog(char *s, ...) {
+	int len, i, level;
+	char buf[512];
+	va_list args;
+
+
+	memset(buf, 0, 512);
+	va_start(args, s);
+
+	if (*s == '#') {
+		level = atoi(s[1]);
+
+		if (level < KMLOG_LEVEL) 
+			return 0;
+
+		s += 2;
+	}
+
+
+	len = vsprintf(buf, s, args);
+
+	/* replace last zero */
+	kmlog_index--;
+
+	/* <= len copies a zero from buf */
+
+	for (i = 0; i <= len; i++) {
+		if (kmlog_index > KMLOG_SIZE) 
+			kmlog_index = 0;
+
+		kmlog_buf[kmlog_index++] = buf[i];
+	}
+
+	va_end(args);
+
+	return 0;
+}
+
+
+

+ 9 - 3
kernel/mempool.c

@@ -23,6 +23,7 @@
 #include <kernel/module.h>
 #include <kernel/mm.h>
 #include <kernel/kernel.h>
+#include <kernel/kmlog.h>
 
 #include <sys/types.h>
 
@@ -66,8 +67,13 @@ struct mempool_dir mempools[] = {
 int early_mempool_init(void) {
 	struct mempool_dir *dir;
 
-	for (dir = mempools; dir->size; dir++) 
+	for (dir = mempools; dir->size; dir++) {
+		kmlog(KERN_DEBUG "creating mempool%d ...\n", dir - mempools);
+
 		dir->pools = mempool_create(dir->size, __early_alloc, __pool_alloc);
+	}
+
+	kmalloc_report();
 
 	return 0;
 }
@@ -114,7 +120,7 @@ void *__mempool_alloc(struct mempool *p) {
 	struct bucket_entry *e = p->freeptr;
 
 	if (!e) {
-		printk("WARNING: __mempool_alloc: no objects left\n");
+		kmlog("WARNING: __mempool_alloc: no objects left\n");
 
 		return NULL;
 	}
@@ -179,7 +185,7 @@ int dump_pool(struct mempool_dir *d) {
 	p = d->pools;
 	total = CACHE_SIZE / p->size;
 
-	printk("dump_pool(%d): \t%d / %d: %d%%\n", d->size, p->used, total, p->used * 100 / total);
+	kmlog(KERN_INFO "dump_pool(%d): \t%d / %d: %d\n", d->size, p->used, total, p->used / total);
 
 	return 0;
 }

+ 6 - 0
kernel/mm.c

@@ -29,11 +29,17 @@
 
 
 int early_mempool_init(void);
+int pages_init(void);
+
 struct page *first_usable_ram;
+struct page *kmem_pages;
+
+int nr_ram_pages = 0;
 
 
 int mm_init(void) {
 	early_mempool_init();
+	pages_init();
 
 	return 0;
 }

+ 46 - 0
kernel/page.c

@@ -0,0 +1,46 @@
+/* kernel/page.c 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx 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) any later version. 
+ * 
+ * evx 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 evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/module.h>
+#include <kernel/mm.h>
+
+#include <arch/archinfo.h>
+
+#include <sys/types.h>
+
+
+
+/* I think, well, it looks like a buddy, but it's not... */
+struct page *buddy_pages[BUDDY_LEVELS];
+
+
+int pages_init(void) {
+	return 0;
+}
+
+
+void *get_page_from(struct page *list) {
+	return NULL;
+}
+
+
+

+ 1 - 1
kernel/panic.c

@@ -28,7 +28,7 @@
 
 
 int panic(char *s) {
-	printk("Kernel panic: %s\n", s);
+	kmlog("Kernel panic: %s\n", s);
 
 	for (;;) 
 		cpu_relax();

+ 5 - 30
kernel/vsprintf.c

@@ -34,35 +34,10 @@
  * words to say. 
  *
  * and you should know, we removed a lot of useless things (useful in 
- * userspace) in kernel. */
-
-int cano_putc(int);
-
-int printk(char *s, ...) {
-	/* 512 bytes are enough for kernel, because kernelers are 
-	 * very very good, I think :-)
-	 *
-	 * but you must know this truth, because more long 's' can 
-	 * make a stack-overflow */
-
-	char buf[512] = { 0 };
-	va_list args;
-	int i;
-
-	memset(buf, 0, 512);
-
-	va_start(args, s);
-
-	i = vsprintf(buf, s, args);
-
-	va_end(args);
-
-	for (i = 0; buf[i]; i++) 
-		/* will be changed by tty_write etc. in the future */
-		cano_putc(buf[i]);
-
-	return i;
-}
+ * userspace) in kernel. 
+ * i don't know why but va_arg can only get first 6 arguments on x86_64 
+ * and 6 is also the number of arguments store in registers. I'm trying 
+ * i386 */
 
 
 static const char digits[] = "0123456789abcdef";
@@ -193,7 +168,7 @@ int vsprintf(char *buf, const char *s, va_list args) {
 		}
 	}
 
-	return i;
+	return bufindex;
 }
 
 

+ 19 - 0
modules/console.c

@@ -23,6 +23,7 @@
 
 #include <kernel/init.h>
 #include <kernel/module.h>
+#include <kernel/kmlog.h>
 
 #include <modules/console.h>
 
@@ -40,6 +41,8 @@ static void update_cursor(void);
 void raw_putc(__uint16_t, __uint8_t, char);
 void cano_putc(char);
 
+static void put_kmlog(void);
+
 
 int __init init_console(void) {
 	pos = read_cursor();
@@ -48,6 +51,8 @@ int __init init_console(void) {
 
 	cano_putc('\n');
 
+	put_kmlog();
+
 	return 0;
 }
 
@@ -134,6 +139,20 @@ void update_cursor(void) {
 }
 
 
+static void put_kmlog(void) {
+	char *s;
+
+	for (s = kmlog_buf; *s; s++) {
+		if (s > kmlog_buf + KMLOG_SIZE) 
+			s = kmlog_buf;
+
+		cano_putc(*s);
+	}
+
+	return;
+}
+
+
 module_init(init_console);