Explorar el Código

evx: well, also, write a lot, fix them later

rain hace 4 años
padre
commit
adb2429b81
Se han modificado 12 ficheros con 255 adiciones y 33 borrados
  1. 2 0
      arch/x86/include/arch/mm.h
  2. 56 15
      arch/x86/kernel/arch_mm.c
  3. 4 0
      include/kernel/init.h
  4. 9 2
      include/kernel/mm.h
  5. 2 0
      include/kernel/tty.h
  6. 20 1
      init/main.c
  7. 4 0
      kernel/kmlog.c
  8. 19 8
      kernel/mempool.c
  9. 72 2
      kernel/page.c
  10. 22 5
      kernel/tty.c
  11. 25 0
      modules/console.c
  12. 20 0
      modules/serial.c

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

@@ -74,6 +74,8 @@ static inline u64 HIGH_AND_LOW(u32 high, u32 low, int bits) {
 
 #define real_addr(a) 			((a) & ~KERNEL_OFFSET)
 #define page_descr(addr) 		(kmem_pages + PAGE_NR(real_addr(addr) & ~PAGE_MASK))
+#define page_nr(page) 			((page) - kmem_pages)
+#define page_addr(p) 			((void *) (page_nr(p) << PAGE_SHIFT))
 
 #endif
 #endif

+ 56 - 15
arch/x86/kernel/arch_mm.c

@@ -36,6 +36,8 @@
 static int __init init_mapping_32m(void);
 static int __init init_kmlog_buf(void);
 
+int setpageattr(void *p, int attr);
+
 __archptr_t freeptr;
 
 void *kernel_end;
@@ -94,7 +96,7 @@ int arch_mm_init(void) {
 	p = kmem_pages = (struct page *) z;
 	z = kmem_zones;
 
-	for (;;) {
+	while (z < (struct zone *) kmem_pages) {
 		if (z->type != ZONETYPE_RAM && z->type != ZONETYPE_OTHER) 
 			break;
 
@@ -102,7 +104,8 @@ int arch_mm_init(void) {
 
 		for (i = 0; i < z->total_pages; i++) {
 			last = p;
-			p->count = 0;
+
+			p->info = (z - kmem_zones) << 24;
 			p->next = p + 1;
 
 			p++;
@@ -203,24 +206,43 @@ int early_free(void *p, int nbytes) {
 }
 
 
-/* map page s (virtual) to d */
-
+/* map page s (virtual) to d... okay, what am i doing? */
 
-void *mapto(void *s, void *d) {
+void *__mapto(void *s, void *d, int attr) {
 #if CONFIG_64BIT
-	__archptr_t *lvl1;
+	__archptr_t *lvl1, *lvl2, *lvl3, *p;
 	__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 */
+	lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 39) & 4095];
+	
+	if (!lvl1) {
+		if (!(lvl1 = early_kmalloc(4096, 4096))) 
+			panic("mapto: Out of memory\n");
+
+		LVL1_PT[(dest_addr >> 39) & 4095] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
+		memset(lvl1, 0, 4096);
+	}
+
+	p = (__archptr_t *) ((__archptr_t) lvl1 & ~0xfffu);
+	lvl2 = (__archptr_t *) p[(dest_addr >> 30) & 4095];
+
+	if (!lvl2) {
+		if (!(lvl2 = early_kmalloc(4096, 4096))) 
+			panic("mapto: Out of memory\n");
+
+		p[(dest_addr >> 30) & 4095] = (__archptr_t) lvl2 | DEFAULT_PAGE_ATTR;
+		memset(lvl2, 0, 4096);
+	}
+
+	lvl3 = (__archptr_t *) ((__archptr_t) lvl2 & ~0xfffu);
+
+	if (s) 
+		lvl3[(dest_addr >> 21) & 4095] = (__archptr_t) s | attr;
 
-		panic("mapto: what time can we support a getfreepage()?");
+	else 
+		lvl3[(dest_addr >> 21) & 4095] &= ~0xfffu | attr;
 
 	return d;
 #else
@@ -230,7 +252,7 @@ void *mapto(void *s, void *d) {
 	__archptr_t *lvl2;
 
 	dest_addr &= ~PAGE_MASK;
-
+	
 	/* 10 lvl1    10 lvl2    12 offset */
 	lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22];
 
@@ -243,13 +265,32 @@ void *mapto(void *s, void *d) {
 	}
 
 	lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu);
-	lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | DEFAULT_PAGE_ATTR;
+
+	if (!s) 
+		/* clear attributes and then set */
+
+		lvl2[(dest_addr >> 12) & 1023] &= ~0xff | attr;
+
+	else 
+		lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | attr;
+
 
 	return (void *) dest_addr;
 #endif
 }
 
 
+void *mapto(void *s, void *d) {
+	return __mapto(s, d, DEFAULT_PAGE_ATTR);
+}
+
+
+int setpageattr(void *p, int attr) {
+	__mapto(NULL, p, attr);
+
+	return 0;
+}
+
 
 arch_init(arch_mm_init);
 

+ 4 - 0
include/kernel/init.h

@@ -44,6 +44,10 @@ extern void *_einit_call_arch, *_einit_call_kernel, *_einit_call_modules;
 extern int _end __attribute__ ((__section__(".end")));
 extern void *kernel_end;
 
+#define STATE_INIT_CALLS 	1
+#define STATE_READY 		2
+extern int init_state;
+
 
 #define arch_init(fn) 	\
 	void *__initcall1_##fn __attribute__ ((__section__ (".initcalls.arch"))) = fn;

+ 9 - 2
include/kernel/mm.h

@@ -41,8 +41,12 @@
  * */
 
 struct page {
-	/* for cow, the reference count. we won't free if count-- */
-	__uint32_t 		count;
+	/* for cow, the reference count. we won't free if count-- 
+	 * id of this page, can let freepage faster */
+	__uint32_t 		info;
+
+#define page_count(__p) 	((__p)->info & 0xffu)
+#define page_id(__p) 		((__p)->info >> 24u)
 
 	struct page 		*next;
 };
@@ -105,6 +109,9 @@ int 			dump_pool(struct mempool_dir *);
 void 			*early_kmalloc(int nbytes, int align);
 int 			early_free(void *p, int nbytes);
 
+struct page 		*getfreepage(int order, int gfp_mask);
+int 			freepage(struct page *, int order);
+
 
 #define free(p) 	early_free(p, 0)
 #define BUDDY_LEVELS 	5

+ 2 - 0
include/kernel/tty.h

@@ -44,6 +44,8 @@ struct tty {
 };
 
 int tty_register(struct tty *);
+int tty_write(struct tty *, char *);
+int tty_write_all(char *);
 
 extern struct klist_node ttys;
 

+ 20 - 1
init/main.c

@@ -30,13 +30,32 @@
 #include <sys/types.h>
 
 
+int init_state;
+
 static int do_init_calls(void);
 
 
 int __init init(void) {
+	struct page *p;
+
+	init_state = STATE_INIT_CALLS;
+
 	do_init_calls();
 
-	kmlog("first usable page is the %d'th page. \n", first_usable_ram - kmem_pages);
+	init_state = STATE_READY;
+
+	p = getfreepage(0, 0);
+	freepage(p, 0);
+
+	p = getfreepage(0, 0);
+	freepage(p, 0);
+
+	p = getfreepage(0, 0);
+	freepage(p, 0);
+
+	p = getfreepage(0, 0);
+
+	kmlog("page number %d\n", page_nr(p));
 
 	return 0;
 }

+ 4 - 0
kernel/kmlog.c

@@ -23,6 +23,7 @@
 #include <kernel/module.h>
 #include <kernel/kernel.h>
 #include <kernel/kmlog.h>
+#include <kernel/tty.h>
 
 #include <arch/archinfo.h>
 #include <arch/string.h>
@@ -77,6 +78,9 @@ int kmlog(char *s, ...) {
 		kmlog_buf[kmlog_index++] = buf[i];
 	}
 
+	if (likely(init_state == STATE_READY))
+		tty_write_all(buf);
+
 	va_end(args);
 
 	return 0;

+ 19 - 8
kernel/mempool.c

@@ -99,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 = (struct bucket_entry *) ((char *) e) + size;
+		e->next = (struct bucket_entry *) ((char *) e + size);
 
 		++e;
 	}
@@ -111,13 +111,14 @@ struct mempool *mempool_create(int size, void *(* alloc) (int size), void *(* po
 }
 
 
-void *__mempool_alloc(struct mempool *p) {
-	struct bucket_entry *e = p->freeptr;
+void *__mempool_alloc(struct mempool *pool) {
+	struct bucket_entry *e = pool->freeptr;
+	struct mempool *p = pool;
 
 	if (!e) {
 		kmlog(KERN_INFO "__mempool_alloc: creating new pool ...\n");
 
-		p = mempool_create(p->size, __early_alloc, __pool_alloc);
+		pool->next_pool = p = mempool_create(p->size, __early_alloc, __pool_alloc);
 	}
 
 	p->freeptr = e->next;
@@ -159,17 +160,27 @@ void *kmalloc(int size) {
 
 int free_s(void *p, int size) {
 	struct mempool_dir *d;
+	struct mempool *pool;
 
 	void *cache = (void *) ((__archptr_t) p & ~CACHE_SIZE);
 
-	for (d = mempools; d->size; d++) 
-		if (d->size >= size && d->pools->cache == cache) 
-			break;
+	for (d = mempools; d->size; d++) {
+		if (d->size < size) 
+			continue;
+
+		pool = d->pools;
+
+		while (pool && pool->cache != cache) 
+			pool = pool->next_pool;
+
+		if (!pool) 
+			panic("WARNING: free_s: object is not in any mempool\n");
+	}
 
 	if (!d->size) 
 		panic("WARNING: free_s: object is not in any mempool\n");
 
-	return __mempool_free(d->pools, p);
+	return __mempool_free(pool, p);
 }
 
 

+ 72 - 2
kernel/page.c

@@ -40,7 +40,7 @@ static int pages_in_levels_init(void);
 struct klist_node buddy_pages[BUDDY_LEVELS];
 int pages_in_levels[BUDDY_LEVELS];
 
-int pages_init(void) {
+int __init pages_init(void) {
 	struct zone *z = kmem_zones;
 	struct page *p;
 	struct klist_node *n;
@@ -52,6 +52,8 @@ int pages_init(void) {
 		n = NULL;
 		klist_init(buddy_pages + i);
 
+		/* '->data' in buddy_pages' entry points to the first usable page */
+
 		for (j = 0; j < pages_in_levels[i]; j++) {
 			if (unlikely(!p)) {
 				do {
@@ -69,6 +71,8 @@ int pages_init(void) {
 
 				if (unlikely(!p)) 
 					panic("pages_init: broken zone descriptor");
+
+				buddy_pages[i].data = n;
 			}
 
 			if (!n) {
@@ -98,7 +102,7 @@ int pages_init(void) {
 }
 
 
-int pages_in_levels_init(void) {
+int __init pages_in_levels_init(void) {
 	/* a bit math, very healthy :-) */
 	register int an, d, i, check = 0;
 	
@@ -124,4 +128,70 @@ int pages_in_levels_init(void) {
 }
 
 
+/* this is not interesting, and we will not get so better, so just use
+ * this and we can do other things i think more interesting such as 
+ * writing some drivers 
+ *
+ * WARNING that the returned value maybe unmapped. you should map it by 
+ * yourself. 
+ * */
+
+
+struct page *getfreepage(int order, int gfp_mask) {
+	struct klist_node *list = buddy_pages + order, *n;
+	struct page *page, *t;
+
+	/* find the next. if no page else, get from its parent */
+
+	if (!(n = list->data)) 
+		return NULL;
+
+	/* find next usable */
+	page = n->data;
+
+	for (n = list->next; n != list; n = n->next) {
+		t = n->data;
+
+		while (t && page_count(t) && t != page) 
+			t = t->next;
+	
+		if (!t) 
+			/* change it later */
+			panic("getfreepage: no pages left");
+	}
+
+	page->info |= 0xff;
+	list->data = t;
+
+	return page;
+}
+
+
+int freepage(struct page *p, int order) {
+	struct klist_node *list = buddy_pages + order, *n;
+	struct page *t;
+
+	for (n = list->next; n != list; n = n->next) 
+		if (page_id(p) == page_id((struct page *) n->data)) 
+			break;
+
+	if (n == list) 
+		panic("freepage: page not found");
+	
+	/* find the struct page */
+
+	for (t = n->data; t; t = t->next) 
+		if (t == p) 
+			break;
+
+	if (!t) 
+		panic("freepage: broken system");
+
+	if (!page_count(t)) 
+		panic("freepage: trying to free a free-page");
+
+	t->info &= ~0xff;
+
+	return 0;
+}
 

+ 22 - 5
kernel/tty.c

@@ -21,6 +21,7 @@
 
 #include <kernel/init.h>
 #include <kernel/module.h>
+#include <kernel/kernel.h>
 #include <kernel/tty.h>
 #include <kernel/mm.h>
 
@@ -34,9 +35,17 @@ struct klist_node ttys = KLIST_NODE_INIT(&ttys);
 int tty_register(struct tty *tty) {
 	struct klist_node *n = kmalloc(sizeof(struct klist_node));
 
+	if (!n) 
+		panic("tty_register: Out of memory");
+
+	klist_init(n);
+
 	n->data = tty;
 	klist_add(&ttys, n);
 
+	tty_write(tty, tty->dev_name);
+	tty_write(tty, "\n\n");
+
 	return 0;
 }
 
@@ -44,25 +53,33 @@ int tty_register(struct tty *tty) {
 int tty_write(struct tty *tty, char *s) {
 	int err;
 
-	while (*s++) 
+	while (*s) {
 		if ((err = tty->putc(*s))) 
 			return err;
 
+		s++;
+	}
+
 	return 0;
 }
 
-int tty_write_all(char *s) {
+int tty_write_all(char *p) {
 	struct tty *tty;
 	struct klist_node *n;
+	char *s;
 	int err;
 
-	while (*s++) 
-		for (n = ttys.next; n != &ttys; n = n->next) {
-			tty = (struct tty *) n->data;
+	for (n = ttys.next; n != &ttys; n = n->next) {
+		s = p;
+		tty = (struct tty *) n->data;
 
+		while (*s) {
 			if ((err = tty->putc(*s))) 
 				return err;
+
+			s++;
 		}
+	}
 	
 	return 0;
 }

+ 25 - 0
modules/console.c

@@ -28,6 +28,7 @@
 
 #include <modules/console.h>
 
+#include <arch/string.h>
 #include <arch/io.h>
 
 
@@ -38,6 +39,7 @@ __uint16_t x = 0, y = 0;
 
 static __uint16_t read_cursor(void);
 static void update_cursor(void);
+static void roll_screen(void);
 
 void raw_putc(__uint16_t, __uint8_t, char);
 int cano_putc(char);
@@ -134,6 +136,9 @@ void raw_putc(__uint16_t pos, __uint8_t color, char c) {
 
 
 void update_cursor(void) {
+	if (y > LINES) 
+		roll_screen();
+
 	pos = y * COLMS + x;
 
 	outb(CMD_POS_LO, VGA_INDEX);
@@ -145,6 +150,26 @@ void update_cursor(void) {
 	return;
 }
 
+void roll_screen(void) {
+	/* well, in old versions of xnix this was implemented 
+	 * in assembly, that's really fast. 
+	 *
+	 * but ... now we can only use C. */
+
+	__uint16_t *s;
+
+	memcpy(FB_ADDR, FB_ADDR + 80, 7680);
+
+	/* fill the last line with '0720', white space */
+
+	for (s = FB_ADDR + 1920; s < (__uint16_t *) 0xb9000; s++) 
+		*s = 0x0720;
+
+	y = 24;
+
+	return;
+}
+
 
 static void put_kmlog(void) {
 	char *s;

+ 20 - 0
modules/serial.c

@@ -24,6 +24,7 @@
 #include <kernel/init.h>
 #include <kernel/module.h>
 #include <kernel/tty.h>
+#include <kernel/kmlog.h>
 
 #include <modules/serial.h>
 
@@ -32,6 +33,7 @@
 
 
 int serial_putc(char);
+static int put_kmlog(void);
 
 struct tty serial0_tty = {
 	.dev_name = "ttyS0", 
@@ -60,6 +62,7 @@ static int com_init(__io_t p) {
 
 int __init init_serial(void) {
 	com_init(COM0);
+	put_kmlog();
 
 	return tty_register(&serial0_tty);
 }
@@ -74,10 +77,27 @@ int serial_write(__io_t p, char c) {
 
 
 int serial_putc(char c) {
+	if (c == '\n') 
+		serial_write(COM0, '\r');
+
 	return serial_write(COM0, c);
 }
 
 
+static int put_kmlog(void) {
+	char *s;
+
+	for (s = kmlog_buf; *s; s++) {
+		if (s > kmlog_buf + KMLOG_SIZE) 
+			s = kmlog_buf;
+
+		serial_putc(*s);
+	}
+
+	return 0;
+}
+
+
 module_init(init_serial);