Procházet zdrojové kódy

evx: add kmalloc(), by early_kmalloc but there is a small bug

rain před 4 roky
rodič
revize
ed250edad4
10 změnil soubory, kde provedl 314 přidání a 19 odebrání
  1. 2 0
      arch/x86/include/arch/mm.h
  2. 13 0
      arch/x86/kernel/arch_mm.c
  3. 55 10
      include/kernel/mm.h
  4. 0 4
      include/lib/klist.h
  5. 25 1
      init/main.c
  6. 1 1
      kernel/Makefile
  7. 209 0
      kernel/mempool.c
  8. 4 1
      kernel/mm.c
  9. 5 0
      kernel/vsprintf.c
  10. 0 2
      lib/klist.c

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

@@ -44,6 +44,8 @@ struct e820_entry {
 	__uint32_t 	type;
 } __attribute__ ((packed));
 
+
+
 #if CONFIG_64BIT
 static inline u64 HIGH_AND_LOW(u32 high, u32 low, int bits) {
 	u64 res = (u64) high;

+ 13 - 0
arch/x86/kernel/arch_mm.c

@@ -22,6 +22,7 @@
 #include <kernel/init.h>
 #include <kernel/module.h>
 #include <kernel/mm.h>
+#include <kernel/kernel.h>
 
 #include <arch/archinfo.h>
 #include <arch/mm.h>
@@ -175,6 +176,18 @@ int early_free(void *p, int nbytes) {
 
 
 
+static inline void **rsp(void) {
+	register __archptr_t 	res;
+
+	__asm__ volatile (
+		"movq 	%%rsp, %0\n\t"
+		: "=r" (res)
+	);
+
+	return (void **) res;
+}
+
+
 arch_init(arch_mm_init);
 
 

+ 55 - 10
include/kernel/mm.h

@@ -41,28 +41,73 @@
 
 struct page {
 	/* for cow, the reference count. we won't free if count-- */
-	__uint32_t 	count;
+	__uint32_t 		count;
 
-	struct page 	*next;
+	struct page 		*next;
 };
 
 
 /* okay, that's may only in the x86, but... */
 
 struct zone {
-	__uint32_t 	total_pages;
+	__uint32_t 		total_pages;
 
-#define ZONETYPE_RAM 	1
-#define ZONETYPE_OTHER 	0
+#define ZONETYPE_RAM 		1
+#define ZONETYPE_OTHER 		0
 
-	__uint32_t 	type;
+	__uint32_t 		type;
 
-	void 		*start_addr;
+	void 			*start_addr;
 	
-	struct page 	*pages;
+	struct page 		*pages;
 };
 
 
+
+/* the bucket_entry are for free objects. next_free points to next free
+ * object. we replace the freeptr pointer in struct mempool and let that
+ * points to old freeptr */
+
+struct bucket_entry {
+	struct bucket_entry 	*next;
+};
+
+
+#define CACHE_SIZE 		4096
+
+
+struct mempool {
+	int 			size, used;
+	void 			*cache;
+	struct bucket_entry 	*freeptr;
+
+	/* it has the same size */
+	struct mempool 		*next_pool;
+};
+
+
+struct mempool_dir {
+	int 			size;
+	struct mempool 		*pools;
+};
+
+
+void 			*__mempool_alloc(struct mempool *);
+int 			__mempool_free(struct mempool *, void *);
+
+void 			*kmalloc(int size);
+int 			free_s(void *p, int size);
+
+int 			kmalloc_report(void);
+int 			dump_pool(struct mempool_dir *);
+
+
+#define free(p) 	
+
+
+
+
+
 /* sets by arch_mm_init */
 
 extern struct page 	*first_usable_ram;
@@ -76,8 +121,8 @@ 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);
+void 			*early_kmalloc(int nbytes, int align);
+int 			early_free(void *p, int nbytes);
 
 
 #endif

+ 0 - 4
include/lib/klist.h

@@ -27,8 +27,6 @@
 
 struct klist_node {
 	long 		value;
-	int 		xmask;
-	void 		*payload;
 	void 		*data;
 
 	struct klist_node *next, *prev;
@@ -47,9 +45,7 @@ void klist_del(struct klist_node *head, struct klist_node *will_del);
 
 #define KLIST_NODE_INIT(__x) { \
 	.value = 0, \
-	.xmask = 0, \
 	.payload = NULL, \
-	.data = NULL, \
 	.next = __x, \
 	.prev = __x, \
 }

+ 25 - 1
init/main.c

@@ -33,9 +33,33 @@ static int do_init_calls(void);
 
 
 int __init init(void) {
+	void *p;
+
 	do_init_calls();
 
-	printk("%d\n", first_usable_ram - kmem_pages);
+
+	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();
 
 	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 
+obj-i = mm.c vsprintf.c tty.c mempool.c 
 
 target: $(obj-i)
 

+ 209 - 0
kernel/mempool.c

@@ -0,0 +1,209 @@
+/* kernel/mempool.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 <kernel/kernel.h>
+
+#include <sys/types.h>
+
+
+/* early memory pools are for klist nodes because most early_free cannot 
+ * release memory.
+ *
+ * of course you can use them for some other structures such as struct task 
+ * etc. */
+
+static struct mempool *mempool_create(int size, void *(*) (int), void *(*) (int));
+
+static void *__early_alloc(int size) {
+	return early_kmalloc(size, 1);
+}
+
+
+static void *__pool_alloc(int size) {
+	return early_kmalloc(size, CACHE_SIZE);
+}
+
+
+struct mempool_dir mempools[] = {
+	/* you can join a structure in here too */
+
+	{ 16, NULL }, 
+	{ 32, NULL }, 
+	{ 64, NULL }, 
+	{ 128, NULL }, 
+	{ 256, NULL }, 
+	{ 512, NULL }, 
+	{ 1024, NULL }, 
+	{ 2048, NULL }, 
+	{ 4096, NULL }, 
+
+	{ 0, NULL }
+};
+
+
+
+int early_mempool_init(void) {
+	struct mempool_dir *dir;
+
+	for (dir = mempools; dir->size; dir++) 
+		dir->pools = mempool_create(dir->size, __early_alloc, __pool_alloc);
+
+	return 0;
+}
+
+
+struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* pool_alloc) (int)) {
+	struct mempool *pool;
+	struct bucket_entry *e;
+	int i;
+
+	/* we don't support multiple-pools now */
+
+	pool = alloc(sizeof(struct mempool));
+
+	if (!pool) {
+		/* this printk is not safe, as you know */
+		printk("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;
+	}
+
+	e = pool->freeptr = (struct bucket_entry *) pool->cache;
+
+	for (i = 0; i < CACHE_SIZE / size; i++) {
+		e->next = e + 1;
+
+		++e;
+	}
+
+	pool->next_pool = NULL;
+	pool->used = 0;
+
+	return pool;
+}
+
+
+void *__mempool_alloc(struct mempool *p) {
+	struct bucket_entry *e = p->freeptr;
+
+	if (!e) {
+		printk("WARNING: __mempool_alloc: no objects left\n");
+
+		return NULL;
+	}
+
+	p->freeptr = e->next;
+	p->used++;
+	
+	return (void *) e; 
+}
+
+
+int __mempool_free(struct mempool *pool, void *p) {
+	struct bucket_entry *old, *e;
+
+	e = (struct bucket_entry *) p;
+	old = pool->freeptr;
+
+	pool->freeptr = e;
+	e->next = old;
+
+	pool->used--;
+
+	return 0;
+}
+
+
+
+void *kmalloc(int size) {
+	struct mempool_dir 	*d;
+
+	for (d = mempools; d->size; d++) 
+		if (d->size >= size) 
+			break;
+	
+	if (!d->size) {
+		printk("WARNING: kmalloc: size is too big\n");
+
+		return NULL;
+	}
+
+	return __mempool_alloc(d->pools);
+}
+
+
+int free_s(void *p, int size) {
+	struct mempool_dir 	*d;
+
+	void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
+
+	for (d = mempools; d->size; d++) 
+		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;
+	}
+
+	return __mempool_free(d->pools, p);
+}
+
+
+int dump_pool(struct mempool_dir *d) {
+	struct mempool *p;
+	int total;
+
+	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);
+
+	return 0;
+}
+
+
+
+int kmalloc_report(void) {
+	struct mempool_dir 	*d;
+
+	for (d = mempools; d->size; d++) 
+		dump_pool(d);
+
+	return 0;
+}
+
+

+ 4 - 1
kernel/mm.c

@@ -21,17 +21,20 @@
 
 #include <kernel/init.h>
 #include <kernel/module.h>
+#include <kernel/mm.h>
 
 #include <arch/archinfo.h>
 
 #include <sys/types.h>
 
 
-
+int early_mempool_init(void);
 struct page *first_usable_ram;
 
 
 int mm_init(void) {
+	early_mempool_init();
+
 	return 0;
 }
 

+ 5 - 0
kernel/vsprintf.c

@@ -178,6 +178,11 @@ int vsprintf(char *buf, const char *s, va_list args) {
 
 			break;
 
+		case '%':
+			output('%');
+
+			break;
+
 		default:
 			output('%');
 			output(*fmt);

+ 0 - 2
lib/klist.c

@@ -36,11 +36,9 @@ struct klist_node *klist_find_value(struct klist_node *head, int value) {
 
 
 void klist_init(struct klist_node *head) {
-	head->xmask = 0;
 	head->next = head;
 	head->prev = head;
 	head->value = 0;
-	head->payload = NULL;
 	head->data = NULL;
 
 	return;