Przeglądaj źródła

evx: memory okay. we can use early_kmalloc() to allocate :-)

rain 4 lat temu
rodzic
commit
52e3d3dd0d

+ 3 - 3
Makefile

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

+ 0 - 1
arch/x86/.archcflags

@@ -1 +0,0 @@
--m32 -I/root/evx/arch/x86/include -I/root/evx/arch/x86/include32 

+ 1 - 1
arch/x86/Makefile

@@ -1,7 +1,7 @@
 # this was called from the Makefile in /, 
 # and we just tell it what objects that he should make. 
 
-BITS 	:= 64
+BITS 	= 64
 
 asm 	= boot/boot$(BITS).S boot/setup$(BITS).S 
 asmobj 	= boot/boot$(BITS).S.o boot/setup$(BITS).S.o 

+ 4 - 3
arch/x86/boot/boot32.S

@@ -69,10 +69,9 @@ load_sys:
 	movw %ax, %es
 
 	xorw %bx, %bx
-	xorl %eax, %eax
+	movl $1, %eax
 
-lloop:	incl %eax
-	movw $16, %cx
+lloop:	movw $16, %cx
 
 	call read_lba
 
@@ -80,6 +79,8 @@ lloop:	incl %eax
 	addw $512, %cx
 	movw %cx, %es
 
+	addl $16, %eax
+
 	cmpl $64, %eax
 	jb cont
 

+ 4 - 3
arch/x86/boot/boot64.S

@@ -69,10 +69,9 @@ load_sys:
 	movw %ax, %es
 
 	xorw %bx, %bx
-	xorl %eax, %eax
+	movl $1, %eax
 
-lloop:	incl %eax
-	movw $16, %cx
+lloop:	movw $16, %cx
 
 	call read_lba
 
@@ -80,6 +79,8 @@ lloop:	incl %eax
 	addw $512, %cx
 	movw %cx, %es
 
+	addl $16, %eax
+
 	cmpl $64, %eax
 	jb cont
 

+ 8 - 1
arch/x86/include/arch/mm.h

@@ -45,7 +45,14 @@ struct e820_entry {
 } __attribute__ ((packed));
 
 #if CONFIG_64BIT
-#define HIGH_AND_LOW(high, low, bits) 	((((u64) high) << bits) + (u64) low)
+static inline u64 HIGH_AND_LOW(u32 high, u32 low, int bits) {
+	u64 res = (u64) high;
+
+	res <<= bits;
+	res += low;
+
+	return res;
+}
 #else
 #define HIGH_AND_LOW(high, low, bits) 	(low)
 #endif

+ 19 - 23
arch/x86/kernel/arch_mm.c

@@ -27,11 +27,12 @@
 #include <arch/mm.h>
 #include <arch/string.h>
 
-
 #include <sys/types.h>
+#include <config.h>
 
 
-static int __init init_mapping_16m(void);
+static int __init init_mapping_32m(void);
+static __archptr_t freeptr;
 
 
 /* the most important work is: get total size of ram, 
@@ -46,11 +47,11 @@ int arch_mm_init(void) {
 
 	__archptr_t		addr;
 
-	init_mapping_16m();
+	init_mapping_32m();
 
 	/* put them there, after that, are the page structures. */
 	z = (struct zone *) &_end;
-	p = (struct page *) ((struct zone *) &_end + 2);
+	p = (struct page *) (z + 2);
 
 	
 	memset(z, 0, sizeof(struct zone) * 2);
@@ -69,7 +70,6 @@ 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;
 
@@ -95,53 +95,49 @@ int arch_mm_init(void) {
 
 /* for a 4gb memory space, 12m used to create struct page */
 
-static int __init init_mapping_16m(void) {
+static int __init init_mapping_32m(void) {
 #if CONFIG_64BIT
+	freeptr = 0x3000;
+
 	/* already mapped to 1g */
+
 	return 0;
 #else
 
 	__archptr_t *lvl1 = (void *) 0, 
 		    *lvl2 = (void *) 4096;
 
-	__archptr_t addr = 7;
+	__archptr_t addr = 4096 + 7;
 	int i, j;
 
-	lvl1[0] = 4096 + 7;
-	lvl1[1] = 8192 + 7;
-	lvl1[2] = 4096 * 3 + 7;
-	lvl1[3] = 4096 * 4 + 7;
+	for (i = 0; i < 6; i++, addr += 4096) {
+		lvl1[i] = addr;
 
-	lvl1[0xc00] = 4096 + 7;
-	lvl1[0xc01] = 8192 + 7;
-	lvl1[0xc02] = 4096 * 3 + 7;
-	lvl1[0xc03] = 4096 * 4 + 7;
+		lvl1[0x300 + i] = addr;
+	}
 
+	addr = 7;
 	
-	for (i = 0; i < 4; i++) {
+	for (i = 0; i < 6; i++) {
 		lvl2 = (void *) (lvl1[i] & ~0xff);
 
 		for (j = 0; j < 1024; j++, addr += 4096) 
 			lvl2[j] = addr;
 	}
 
-	invalidate();
+	freeptr = (__archptr_t) lvl2 + j;
+
+	/* no need to invalidate() */
 
 	return 0;
 #endif
 }
 
 
-/* pmap */
-#define START 	0x3000
-
 /* stack - 8192 */
 #define END 	0x8e000
 
 
-__archptr_t 	freeptr = START;
-
-
 void *early_kmalloc(int nbytes, int align) {
 	void *p;
 

+ 4 - 0
arch/x86/vmevx32.lds

@@ -15,6 +15,10 @@ SECTIONS {
 		*(.init.text)
 	}
 
+	.init.data : {
+		*(.init.data)
+	}
+
 	.initcalls.arch : {
 		_init_call_arch = .;
 		*(.initcalls.arch)

+ 5 - 1
arch/x86/vmevx64.lds

@@ -15,6 +15,10 @@ SECTIONS {
 		*(.init.text)
 	}
 
+	.init.data : {
+		*(.init.data)
+	}
+
 	.initcalls.arch : {
 		_init_call_arch = .;
 		*(.initcalls.arch)
@@ -45,6 +49,6 @@ SECTIONS {
 		_erodata = .;
 	}
 
-	_end = .;
+	_end = _erodata;
 }
 

+ 267 - 0
docs/x86/memmap.md

@@ -0,0 +1,267 @@
+Kernel memory map in x86-64
+
+-----------------------------------------------------------
+`End of Zones`               Kernel memory page descriptors
+`&_end`                      Kernel memory zone descriptors
+0x00100000                                           Kernel
+0x000ff000
+0x000fe000
+0x000fd000
+0x000fc000
+0x000fb000
+0x000fa000
+0x000f9000
+0x000f8000
+0x000f7000
+0x000f6000
+0x000f5000
+0x000f4000
+0x000f3000
+0x000f2000
+0x000f1000
+0x000f0000
+0x000ef000
+0x000ee000
+0x000ed000
+0x000ec000
+0x000eb000
+0x000ea000
+0x000e9000
+0x000e8000             
+0x000e7000
+0x000e6000
+0x000e5000
+0x000e4000
+0x000e3000
+0x000e2000
+0x000e1000
+0x000e0000
+0x000df000
+0x000de000
+0x000dd000
+0x000dc000
+0x000db000
+0x000da000
+0x000d9000
+0x000d8000
+0x000d7000                 HIGH MEMORY FOR BIOS AND DEVICES
+0x000d6000
+0x000d5000
+0x000d4000
+0x000d3000
+0x000d2000
+0x000d1000
+0x000d0000
+0x000cf000
+0x000ce000
+0x000cd000
+0x000cc000
+0x000cb000
+0x000ca000
+0x000c9000
+0x000c8000
+0x000c7000
+0x000c6000
+0x000c5000
+0x000c4000
+0x000c3000
+0x000c2000
+0x000c1000
+0x000c0000
+0x000bf000
+0x000be000
+0x000bd000
+0x000bc000
+0x000bb000
+0x000ba000
+0x000b9000
+0x000b8000                    VGA Frame Buffer in Text Mode
+0x000b7000
+0x000b6000
+0x000b5000
+0x000b4000
+0x000b3000
+0x000b2000
+0x000b1000
+0x000b0000               VGA Frame Buffer in Mono Text Mode
+0x000af000
+0x000ae000
+0x000ad000
+0x000ac000
+0x000ab000
+0x000aa000
+0x000a9000
+0x000a8000
+0x000a7000
+0x000a6000
+0x000a5000
+0x000a4000
+0x000a3000
+0x000a2000
+0x000a1000
+0x000a0000                 VGA Frame Buffer in Graphic Mode
+0x0009f000
+0x0009e000
+0x0009d000
+0x0009c000
+0x0009b000
+0x0009a000
+0x00099000
+0x00098000
+0x00097000
+0x00096000
+0x00095000
+0x00094000
+0x00093000
+0x00092000
+0x00091000
+0x00090000               Kernel Stack   &   E820 Memory Map
+0x0008f000
+0x0008e000         Early kmalloc() end & Bottom Kernel Stack
+0x0008d000
+0x0008c000
+0x0008b000
+0x0008a000
+0x00089000
+0x00088000
+0x00087000
+0x00086000
+0x00085000
+0x00084000
+0x00083000
+0x00082000
+0x00081000
+0x00080000
+0x0007f000
+0x0007e000
+0x0007d000
+0x0007c000
+0x0007b000
+0x0007a000
+0x00079000
+0x00078000
+0x00077000
+0x00076000
+0x00075000
+0x00074000
+0x00073000
+0x00072000
+0x00071000
+0x00070000
+0x0006f000
+0x0006e000
+0x0006d000
+0x0006c000
+0x0006b000
+0x0006a000
+0x00069000
+0x00068000
+0x00067000
+0x00066000
+0x00065000
+0x00064000
+0x00063000
+0x00062000
+0x00061000
+0x00060000
+0x0005f000
+0x0005e000
+0x0005d000
+0x0005c000
+0x0005b000
+0x0005a000
+0x00059000
+0x00058000
+0x00057000
+0x00056000
+0x00055000
+0x00054000
+0x00053000
+0x00052000
+0x00051000
+0x00050000
+0x0004f000
+0x0004e000
+0x0004d000
+0x0004c000
+0x0004b000
+0x0004a000
+0x00049000
+0x00048000
+0x00047000
+0x00046000
+0x00045000
+0x00044000
+0x00043000
+0x00042000
+0x00041000
+0x00040000
+0x0003f000
+0x0003e000
+0x0003d000
+0x0003c000
+0x0003b000
+0x0003a000
+0x00039000
+0x00038000
+0x00037000
+0x00036000
+0x00035000    Free Memory for Early kmalloc() 556Kib Usable
+0x00034000
+0x00033000
+0x00032000
+0x00031000
+0x00030000
+0x0002f000
+0x0002e000
+0x0002d000
+0x0002c000
+0x0002b000
+0x0002a000
+0x00029000
+0x00028000
+0x00027000
+0x00026000
+0x00025000
+0x00024000
+0x00023000
+0x00022000
+0x00021000
+0x00020000
+0x0001f000
+0x0001e000
+0x0001d000
+0x0001c000
+0x0001b000
+0x0001a000
+0x00019000
+0x00018000
+0x00017000
+0x00016000
+0x00015000
+0x00014000
+0x00013000
+0x00012000
+0x00011000
+0x00010000
+0x0000f000
+0x0000e000
+0x0000d000
+0x0000c000
+0x0000b000
+0x0000a000
+0x00009000
+    0x00008200                      (Before Copying) System
+0x00008000                          (Before Copying) Loader
+    0x00007e00              (After Copying) Boot and Loader
+    0x00007c00                        (Before Copying) Boot
+0x00007000
+0x00006000
+0x00005000
+0x00004000            
+0x00003000  Early kmalloc() &  End of 2M Level 3 Page Table
+0x00002000                           2M  Level 3 Page Table
+0x00001000                               Level 2 Page Table
+0x00000000                               Level 1 Page Table
+
+-----------------------------------------------------------

+ 32 - 0
docs/x86/memmap.md.back

@@ -0,0 +1,32 @@
+### memory used in boot-loader for evx
+
+#### 64-bits
+-------------------------------------------------
+
++ End of zones     Kernel mrmory page descriptors
++ `&_end`          Kernel memory zone descriptors
++ 0x00100000                               System
++ 0x00090000                      E820 Memory Map
++ 0x00090000                     Stack for kernel
++ 0x00011000
++ 0x00010000
++ 0x0000f000
++ 0x0000e000
++ 0x0000d000
++ 0x0000c000
++ 0x0000b000
++ 0x0000a000
++ 0x00009000
++ 0x00008000                    Loader and System
++  0x00007e00           (By boot.lds) Boot Loader
++  0x00007c00                          (Was) Boot
++  0x00007c00                               Stack
++ 0x00007000
++ 0x00006000
++ 0x00005000
++ 0x00004000
++ 0x00003000           End of 2M Page table lvl 3
++ 0x00002000                  2M Page table lvl 3
++ 0x00001000                     Page table lvl 2
++ 0x00000000                     Page table lvl 1
+

+ 0 - 0
include/config.h


+ 1 - 0
include/kernel/init.h

@@ -50,6 +50,7 @@ extern void *_end;
 
 
 #define __init		__attribute__((__section__(".init.text")))
+#define __init_data 	__attribute__((__section__(".init.data")))
 
 #define ARRAYSIZE(arr) 	(sizeof(arr) / sizeof(arr[0]))
 

+ 1 - 1
include/kernel/mm.h

@@ -57,7 +57,7 @@ struct zone {
 	__uint32_t 	total_pages;
 
 #define ZONETYPE_RAM 	1
-#define ZONETYPE_OTHER 	2
+#define ZONETYPE_OTHER 	0
 
 	__uint32_t 	type;
 

+ 9 - 6
init/main.c

@@ -29,21 +29,24 @@
 #include <sys/types.h>
 
 
-int do_init_calls(void);
-
-extern int cano_putc(int);
+static int do_init_calls(void);
 
 
 int __init init(void) {
+	u64 npages;
+
 	do_init_calls();
 
-	printk("total ram: %d kib\n", kmem_zones[ZONETYPE_RAM].total_pages << PAGE_SHIFT >> 10);
+	npages = kmem_zones[ZONETYPE_RAM].total_pages;
+
+	printk("number %p\n", 0x1000);
+	printk("total ram: %d kib\n", npages << PAGE_SHIFT >> 10);
 	
 	return 0;
 }
 
 
-void *init_call_levels[] = {
+void *__init_data init_call_levels[] = {
 	&_init_call_arch, &_einit_call_arch, 
 	&_init_call_kernel, &_einit_call_kernel, 
 	&_init_call_modules, &_einit_call_modules
@@ -63,7 +66,7 @@ void *init_call_levels[] = {
 typedef int (* __initcall_t) (void);
 
 
-int do_init_calls(void) {
+static int do_init_calls(void) {
 	int error;
 	int i;
 

+ 28 - 9
kernel/vsprintf.c

@@ -23,6 +23,9 @@
 #include <kernel/module.h>
 #include <kernel/kernel.h>
 
+#include <arch/string.h>
+#include <arch/archinfo.h>
+
 #include <sys/types.h>
 #include <stdarg.h>
 
@@ -42,10 +45,12 @@ int printk(char *s, ...) {
 	 * but you must know this truth, because more long 's' can 
 	 * make a stack-overflow */
 
-	char buf[512];
+	char buf[512] = { 0 };
 	va_list args;
 	int i;
 
+	memset(buf, 0, 512);
+
 	va_start(args, s);
 
 	i = vsprintf(buf, s, args);
@@ -68,7 +73,9 @@ static const char digits[] = "0123456789abcdef";
 
 static int number(char *buf, long nr, int base, int flags, int width) {
 	int neg = 0, i = 0, j;
-	char t[16] = { 0 };
+	char t[PTRSIZE * 2];
+
+	memset(t, 0, PTRSIZE * 2);
 
 	if (nr < 0) {
 		nr = -nr;
@@ -77,9 +84,14 @@ static int number(char *buf, long nr, int base, int flags, int width) {
 			neg = 1;
 	}
 
-	do {
+	if (!nr) 
+		t[i++] = '0';
+
+	else while (nr) {
 		t[i++] = digits[nr % base];
-	} while (nr /= base);
+
+		nr /= base;
+	}
 
 
 	if (neg) 
@@ -87,13 +99,19 @@ static int number(char *buf, long nr, int base, int flags, int width) {
 	
 	if (flags & ZERO) {
 		if (width < 0) 
-			width = 16;
+			width = PTRSIZE * 2;
+
+		for (j = width - 1; j; j--) {
+			if (t[j]) 
+				break;
+			
+			t[j] = '0';
+		}
 
-		for (i = 0; i <= width; i++) 
-			if (!t[i]) 
-				t[i] = 0;
+		i = PTRSIZE * 2;
 	}
 
+	/* t[i] is always 0 */
 	j = i;
 
 	while (--j >= 0) 
@@ -153,7 +171,8 @@ int vsprintf(char *buf, const char *s, va_list args) {
 		case 'p':
 			/* ignore upcase */
 
-			bufindex += number(buf + bufindex, va_arg(args, int), 16, flag | UNSIGNED, 16);
+			bufindex += number(buf + bufindex, va_arg(args, int), 16, 
+					flag | UNSIGNED | ZERO, PTRSIZE * 2);
 
 			break;