Ver Fonte

evx: i think i'll implement a buddy-mm system tomorrow

rain há 4 anos atrás
pai
commit
3905596075
12 ficheiros alterados com 157 adições e 27 exclusões
  1. 1 0
      Makefile
  2. 1 0
      arch/x86/kernel/arch_mm.c
  3. 4 2
      include/kernel/mm.h
  4. 2 0
      include/kernel/tty.h
  5. 1 1
      include/lib/klist.h
  6. 6 11
      kernel/mempool.c
  7. 1 1
      kernel/mm.c
  8. 85 4
      kernel/page.c
  9. 27 2
      kernel/tty.c
  10. 15 0
      lib/Makefile
  11. 1 0
      lib/klist.c
  12. 13 6
      modules/console.c

+ 1 - 0
Makefile

@@ -55,6 +55,7 @@ obj-i 			+= $(ARCHDIR)/kernel/
 obj-i 			+= $(PWD)/init/ 
 obj-i 			+= $(PWD)/kernel/ 
 obj-i 			+= $(PWD)/modules/ 
+obj-i 			+= $(PWD)/lib/ 
 
 
 

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

@@ -114,6 +114,7 @@ int arch_mm_init(void) {
 	}
 	
 	first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) p));
+	usable_ram_pages = nr_ram_pages - (first_usable_ram - kmem_pages);
 
 	return 0;
 }

+ 4 - 2
include/kernel/mm.h

@@ -27,6 +27,7 @@
 
 
 #include <sys/types.h>
+#include <lib/klist.h>
 #include <arch/archinfo.h>
 
 
@@ -108,9 +109,10 @@ int 			early_free(void *p, int nbytes);
 #define free(p) 	early_free(p, 0)
 #define BUDDY_LEVELS 	5
 
+extern struct klist_node buddy_pages[BUDDY_LEVELS];
 
-extern struct page  	*buddy_pages[BUDDY_LEVELS];
-extern int 		nr_ram_pages;
+/* nr_ram_pages is the total */
+extern int nr_ram_pages, usable_ram_pages;
 
 
 /* sets by arch_mm_init */

+ 2 - 0
include/kernel/tty.h

@@ -45,6 +45,8 @@ struct tty {
 
 int tty_register(struct tty *);
 
+extern struct klist_node ttys;
+
 
 #endif
 #endif

+ 1 - 1
include/lib/klist.h

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

+ 6 - 11
kernel/mempool.c

@@ -67,13 +67,8 @@ struct mempool_dir mempools[] = {
 int early_mempool_init(void) {
 	struct mempool_dir *dir;
 
-	for (dir = mempools; dir->size; dir++) {
-		kmlog(KERN_DEBUG "creating mempool%d ...\n", dir - mempools);
-
+	for (dir = mempools; dir->size; dir++) 
 		dir->pools = mempool_create(dir->size, __early_alloc, __pool_alloc);
-	}
-
-	kmalloc_report();
 
 	return 0;
 }
@@ -104,7 +99,7 @@ struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* po
 	e = pool->freeptr = (struct bucket_entry *) pool->cache;
 
 	for (i = 0; i < CACHE_SIZE / size; i++) {
-		e->next = e + 1;
+		e->next = (struct bucket_entry *) ((char *) e) + size;
 
 		++e;
 	}
@@ -120,9 +115,9 @@ void *__mempool_alloc(struct mempool *p) {
 	struct bucket_entry *e = p->freeptr;
 
 	if (!e) {
-		kmlog("WARNING: __mempool_alloc: no objects left\n");
+		kmlog(KERN_INFO "__mempool_alloc: creating new pool ...\n");
 
-		return NULL;
+		p = mempool_create(p->size, __early_alloc, __pool_alloc);
 	}
 
 	p->freeptr = e->next;
@@ -149,7 +144,7 @@ int __mempool_free(struct mempool *pool, void *p) {
 
 
 void *kmalloc(int size) {
-	struct mempool_dir 	*d;
+	struct mempool_dir *d;
 
 	for (d = mempools; d->size; d++) 
 		if (d->size >= size) 
@@ -163,7 +158,7 @@ void *kmalloc(int size) {
 
 
 int free_s(void *p, int size) {
-	struct mempool_dir 	*d;
+	struct mempool_dir *d;
 
 	void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
 

+ 1 - 1
kernel/mm.c

@@ -34,7 +34,7 @@ int pages_init(void);
 struct page *first_usable_ram;
 struct page *kmem_pages;
 
-int nr_ram_pages = 0;
+int nr_ram_pages = 0, usable_ram_pages = 0;
 
 
 int mm_init(void) {

+ 85 - 4
kernel/page.c

@@ -22,24 +22,105 @@
 #include <kernel/init.h>
 #include <kernel/module.h>
 #include <kernel/mm.h>
+#include <kernel/kmlog.h>
+#include <kernel/kernel.h>
 
 #include <arch/archinfo.h>
 
+#include <lib/klist.h>
 #include <sys/types.h>
 
 
 
-/* I think, well, it looks like a buddy, but it's not... */
-struct page *buddy_pages[BUDDY_LEVELS];
+static int pages_in_levels_init(void);
 
+/* I think, well, it looks like a buddy, but it's not...
+ *
+ * each '->data' points to the start of pages */
+struct klist_node buddy_pages[BUDDY_LEVELS];
+int pages_in_levels[BUDDY_LEVELS];
 
 int pages_init(void) {
+	struct zone *z = kmem_zones;
+	struct page *p;
+	struct klist_node *n;
+	int i, j;
+
+	pages_in_levels_init();
+
+	for (i = 0; i < BUDDY_LEVELS; i++) {
+		n = NULL;
+		klist_init(buddy_pages + i);
+
+		for (j = 0; j < pages_in_levels[i]; j++) {
+			if (unlikely(!p)) {
+				do {
+					++z; 
+				} while (z->type != ZONETYPE_RAM && 
+						z < (struct zone *) kmem_pages);
+
+				if (z == (struct zone *) kmem_pages) 
+					panic("pages_init: broken zone descriptor");
+
+				p = z->pages;
+
+				if (p < first_usable_ram) 
+					p = first_usable_ram;
+
+				if (unlikely(!p)) 
+					panic("pages_init: broken zone descriptor");
+			}
+
+			if (!n) {
+				n = kmalloc(sizeof(struct klist_node));
+
+				if (unlikely(!n)) 
+					panic("pages_init: out of memory");
+
+				klist_init(n);
+
+				n->value = 0;
+				n->data = p;
+
+				klist_add(buddy_pages + i, n);
+			}
+
+			p = p->next;
+
+			/* this stores the number of pages */
+			++n->value;
+		}
+	}
+
+	kmlog(KERN_INFO "%d pages total, %d pages usable\n", nr_ram_pages, usable_ram_pages);
+
 	return 0;
 }
 
 
-void *get_page_from(struct page *list) {
-	return NULL;
+int pages_in_levels_init(void) {
+	/* a bit math, very healthy :-) */
+	register int an, d, i, check = 0;
+	
+	/* first we must get 'd', by: 
+	 * S = (a1 + an) * n / 2 
+	 * an = 1 + a1 + (n - 1) * d
+	 *
+	 * a1 = 1 (levels[BUDDY_LEVELS - 1])
+	 * n is BUDDY_LEVELS
+	 * */
+
+	an = (usable_ram_pages << 1) / BUDDY_LEVELS - 1;
+	d = (an - 1) / (BUDDY_LEVELS - 1);
+
+	for (i = BUDDY_LEVELS; i > 0; i--) 
+		check += pages_in_levels[i - 1] = 1 + d * (BUDDY_LEVELS - i);
+
+	if (check != usable_ram_pages) 
+		/* this thing always happens */
+		pages_in_levels[0] += usable_ram_pages - check;
+
+	return 0;
 }
 
 

+ 27 - 2
kernel/tty.c

@@ -22,13 +22,22 @@
 #include <kernel/init.h>
 #include <kernel/module.h>
 #include <kernel/tty.h>
+#include <kernel/mm.h>
 
 #include <sys/types.h>
 #include <lib/klist.h>
 
 
+struct klist_node ttys = KLIST_NODE_INIT(&ttys);
+
+
 int tty_register(struct tty *tty) {
-	return /* until the mem isn't okay */ -1;
+	struct klist_node *n = kmalloc(sizeof(struct klist_node));
+
+	n->data = tty;
+	klist_add(&ttys, n);
+
+	return 0;
 }
 
 
@@ -36,10 +45,26 @@ int tty_write(struct tty *tty, char *s) {
 	int err;
 
 	while (*s++) 
-		if ((err = tty->putc(*s) < 0)) 
+		if ((err = tty->putc(*s))) 
 			return err;
 
 	return 0;
 }
 
+int tty_write_all(char *s) {
+	struct tty *tty;
+	struct klist_node *n;
+	int err;
+
+	while (*s++) 
+		for (n = ttys.next; n != &ttys; n = n->next) {
+			tty = (struct tty *) n->data;
+
+			if ((err = tty->putc(*s))) 
+				return err;
+		}
+	
+	return 0;
+}
+
 

+ 15 - 0
lib/Makefile

@@ -0,0 +1,15 @@
+# this was called from the Makefile in /, 
+# and we just tell it what objects that he should make. 
+
+obj-i = klist.c 
+
+target: $(obj-i)
+
+ifeq ($(D), )
+	@echo "this should call from the Makefile in directory /, but not typing make in lib/. :-(" >&2 
+	@exit 1
+endif
+
+# $(PWD) is the / directory of kernel, e.g. /root/evx/
+	@$(MAKE) -C $(PWD) D=$(shell pwd) obj-i="$(obj-i)"
+

+ 1 - 0
lib/klist.c

@@ -19,6 +19,7 @@
 
 
 
+#include <kernel/init.h>
 #include <sys/types.h>
 #include <lib/klist.h>
 

+ 13 - 6
modules/console.c

@@ -24,6 +24,7 @@
 #include <kernel/init.h>
 #include <kernel/module.h>
 #include <kernel/kmlog.h>
+#include <kernel/tty.h>
 
 #include <modules/console.h>
 
@@ -39,21 +40,27 @@ static __uint16_t read_cursor(void);
 static void update_cursor(void);
 
 void raw_putc(__uint16_t, __uint8_t, char);
-void cano_putc(char);
+int cano_putc(char);
 
 static void put_kmlog(void);
 
+struct tty console_tty = {
+	.dev_name = "console", 
+	.priv_data = 0, 
+	.flags = FLAGS_COLORS, 
+	.putc = cano_putc, 
+	.getc = NULL, 
+};
+
 
 int __init init_console(void) {
 	pos = read_cursor();
-
 	POS2XY(pos);
 
 	cano_putc('\n');
-
 	put_kmlog();
 
-	return 0;
+	return tty_register(&console_tty);
 }
 
 
@@ -70,7 +77,7 @@ static __uint16_t read_cursor(void) {
 }
 
 
-void cano_putc(char c) {
+int cano_putc(char c) {
 	if (c == '\n') {
 		y++;
 		x = 0;
@@ -113,7 +120,7 @@ void cano_putc(char c) {
 out:
 	update_cursor();
 
-	return;
+	return 0;
 }