Ver Fonte

evx: support mapping on i386, and later I relized I need a getfreepage

rain há 4 anos atrás
pai
commit
83d97fcd07

+ 1 - 0
arch/x86/.archcflags

@@ -0,0 +1 @@
+-m64 -I/root/evx/arch/x86/include -I/root/evx/arch/x86/include64 

+ 34 - 0
arch/x86/include/arch/asm.h

@@ -0,0 +1,34 @@
+/* arch/x86/include/arch/asm.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 __ARCH_ASM_H
+#define __ARCH_ASM_H
+
+
+#ifndef __ASSEMBLER__
+
+#define cpu_relax() 	__asm__ volatile ("hlt\n\t" :::)
+
+
+#endif
+#endif
+
+

+ 6 - 0
arch/x86/include/arch/mm.h

@@ -44,6 +44,12 @@ struct e820_entry {
 	__uint32_t 	type;
 } __attribute__ ((packed));
 
+#define LVL1_PT 		((__archptr_t *) 0)
+
+/* rwup */
+#define DEFAULT_PAGE_ATTR 	7
+
+void *mapto(void *src, void *dest);
 
 
 #if CONFIG_64BIT

+ 42 - 7
arch/x86/kernel/arch_mm.c

@@ -175,19 +175,54 @@ int early_free(void *p, int nbytes) {
 }
 
 
+/* map page s (virtual) to d */
 
-static inline void **rsp(void) {
-	register __archptr_t 	res;
 
-	__asm__ volatile (
-		"movq 	%%rsp, %0\n\t"
-		: "=r" (res)
-	);
+void *mapto(void *s, void *d) {
+#if CONFIG_64BIT
+	__archptr_t *lvl1;
+	__archptr_t dest_addr;
+
+	dest_addr = (__archptr_t) d;
+
+	lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 36) & 4095];
+	
+	if (!lvl1) 
+		/* we need 2m to do that, but now 
+		 * the low memory pool has only about 0.5m. 
+		 * abandon, man */
+
+		panic("mapto: what time can we support a getfreepage()?");
+
+	return d;
+#else
+
+	__archptr_t dest_addr = (__archptr_t) d;
+	__archptr_t *lvl1;
+	__archptr_t *lvl2;
 
-	return (void **) res;
+	dest_addr &= ~PAGE_MASK;
+
+	/* 10 lvl1    10 lvl2    12 offset */
+	lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22];
+
+	if (!lvl1) {
+		if (!(lvl1 = early_kmalloc(4096, 4096))) 
+			panic("mapto: Out of memory\n");
+
+		LVL1_PT[dest_addr >> 22] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
+		memset(lvl1, 0, 4096);
+	}
+
+	lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu);
+	lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | DEFAULT_PAGE_ATTR;
+
+	return (void *) dest_addr;
+#endif
 }
 
 
+
 arch_init(arch_mm_init);
 
 

+ 1 - 0
include/kernel/kernel.h

@@ -42,6 +42,7 @@
 
 int printk(char *, ...);
 int vsprintf(char *buf, const char *fmt, va_list);
+int panic(char *s);
 
 #define to_bool(cond) 	!!(cond)
 #define likely(c) 	__builtin_expect(to_bool(c), 1)

+ 3 - 24
init/main.c

@@ -25,6 +25,7 @@
 #include <kernel/mm.h>
 
 #include <arch/archinfo.h>
+#include <arch/mm.h>
 
 #include <sys/types.h>
 
@@ -33,33 +34,11 @@ static int do_init_calls(void);
 
 
 int __init init(void) {
-	void *p;
-
 	do_init_calls();
 
+	mapto((void *) 1048576, (void *) 0xffffffff);
 
-	p = kmalloc(1024);
-
-	printk("kmalloc(1024): %p\n", p);
-	free_s(p, 1024);
-	free(p);
-
-
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-	p = kmalloc(1024);
-
-	printk("kmalloc(256): %p\n", kmalloc(256));
-	printk("kmalloc(8): %p\n", kmalloc(8));
-	printk("kmalloc(8): %p\n", kmalloc(8));
-
-	kmalloc_report();
-	kmalloc_report();
+	printk("%x\n", *(unsigned long *) 0xfffff000);
 
 	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 
+obj-i = mm.c vsprintf.c tty.c mempool.c panic.c 
 
 target: $(obj-i)
 

+ 8 - 19
kernel/mempool.c

@@ -82,23 +82,18 @@ struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* po
 
 	pool = alloc(sizeof(struct mempool));
 
-	if (!pool) {
+	if (!pool) 
 		/* this printk is not safe, as you know */
-		printk("mempool_create: Out of memory\n");
+		panic("mempool_create: Out of memory\n");
 
-		return NULL;
-	}
 
 	pool->size = size;
 
 	/* we'll not free this */
 	pool->cache = pool_alloc(CACHE_SIZE);
 
-	if (!pool->cache) {
-		printk("mempool_create: Out of memory\n");
-
-		return NULL;
-	}
+	if (!pool->cache) 
+		panic("mempool_create: Out of memory\n");
 
 	e = pool->freeptr = (struct bucket_entry *) pool->cache;
 
@@ -154,11 +149,8 @@ void *kmalloc(int size) {
 		if (d->size >= size) 
 			break;
 	
-	if (!d->size) {
-		printk("WARNING: kmalloc: size is too big\n");
-
-		return NULL;
-	}
+	if (!d->size) 
+		panic("WARNING: kmalloc: size is too big\n");
 
 	return __mempool_alloc(d->pools);
 }
@@ -173,11 +165,8 @@ int free_s(void *p, int size) {
 		if (d->size >= size && d->pools->cache == cache) 
 			break;
 
-	if (!d->size) {
-		printk("WARNING: free_s: object is not in any mempool\n");
-
-		return -1;
-	}
+	if (!d->size) 
+		panic("WARNING: free_s: object is not in any mempool\n");
 
 	return __mempool_free(d->pools, p);
 }

+ 40 - 0
kernel/panic.c

@@ -0,0 +1,40 @@
+/* kernel/panic.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 <arch/asm.h>
+
+
+
+int panic(char *s) {
+	printk("Kernel panic: %s\n", s);
+
+	for (;;) 
+		cpu_relax();
+
+	return -1;
+}
+
+
+