瀏覽代碼

evx: prepare to use a boot-loader

rain 4 年之前
父節點
當前提交
7041f5f199

+ 1 - 0
Makefile

@@ -51,6 +51,7 @@ ifeq 	($D,)
 # that inside object, and we don't support other objects at this time :-)
 
 obj-i 			+= $(ARCHDIR)/ 
+obj-i 			+= $(ARCHDIR)/kernel/ 
 obj-i 			+= $(PWD)/init/ 
 obj-i 			+= $(PWD)/kernel/ 
 obj-i 			+= $(PWD)/modules/ 

+ 2 - 2
arch/x86/boot/boot32.S

@@ -134,10 +134,10 @@ read_failed:
 
 
 setup_e820:
-	xorl %ebx, %ebx
+	movl $0x9000, %ebx
 	movl %ebx, %es
 
-	movl $0x7000, %edi
+	movl $0, %edi
 
 sel:	movl $0xe820, %eax
 	movl $20, %ecx

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

@@ -134,10 +134,11 @@ read_failed:
 
 
 setup_e820:
-	xorl %ebx, %ebx
+	movl $0x9000, %ebx
 	movl %ebx, %es
 
-	movl $0x7000, %edi
+	movl $0, %edi
+	xorl %ebx, %ebx
 
 sel:	movl $0xe820, %eax
 	movl $20, %ecx

+ 1 - 1
arch/x86/boot/setup64.S

@@ -81,7 +81,7 @@ rsetpt:	rep
 	movl $0x00003003, 0x2000
 	
 	movl $0x3000, %edi
-	movl $0x00000083, %eax
+	movl $3, %eax
 
 rsetp:	movl %eax, (%edi)
 	movl $0, 4(%edi)

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

@@ -0,0 +1,49 @@
+/* arch/x86/include/arch/mm.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_MM_H
+#define __ARCH_MM_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <arch/archinfo.h>
+
+
+
+#define E820_ADDR 	((struct e820_entry *) 0x90000)
+
+#define E820_TYPE_RAM 	1
+#define E820_TYPE_ROM 	2
+
+#define E820_TYPE_MAX 	5
+
+
+struct e820_entry {
+	__archptr_t 	addr;
+	__archptr_t 	len;
+	__uint32_t 	type;
+} __attribute__ ((packed));
+
+
+#endif
+
+#endif

+ 56 - 0
arch/x86/include/arch/string.h

@@ -0,0 +1,56 @@
+/* arch/x86/include/arch/string.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_STRING_H
+#define __ARCH_STRING_H
+
+
+#ifndef __ASSEMBLER__
+
+
+
+/* well... but in kernel, these are only required. */
+
+
+#define memset(dest, v, n) ({				\
+	__asm__ volatile (				\
+		"cld\n\t"				\
+		"rep\n\t"				\
+		"stosb\n\t"				\
+		:: "c" (n), "a" (v), "D" (dest)		\
+	);						\
+})
+
+
+
+#define memcpy(dest, src, n) ({				\
+	__asm__ volatile (				\
+		"cld\n\t"				\
+		"rep\n\t"				\
+		"movsb\n\t"				\
+		:: "c" (n), "S" (src), "D" (dest)	\
+	);						\
+})
+
+
+#endif
+
+#endif

+ 3 - 0
arch/x86/include32/arch/archinfo.h

@@ -31,8 +31,11 @@ typedef __uint32_t __archptr_t;
 
 #define PAGE_SIZE 	4096lu
 #define PAGE_MASK 	4095lu
+#define PAGE_SHIFT 	12lu
 #define PAGE_ALIGN(a) 	(((a) + PAGE_MASK) & (~PAGE_MASK))
 
+#define PAGE_NR(addr) 	((addr) >> PAGE_SHIFT)
+
 #define PTRSIZE 	4lu
 #define PTRSHIFT 	2lu
 

+ 3 - 0
arch/x86/include64/arch/archinfo.h

@@ -31,8 +31,11 @@ typedef __uint64_t __archptr_t;
 
 #define PAGE_SIZE 	4096lu
 #define PAGE_MASK 	4095lu
+#define PAGE_SHIFT 	12lu
 #define PAGE_ALIGN(a) 	(((a) + PAGE_MASK) & ~(PAGE_MASK))
 
+#define PAGE_NR(addr) 	((addr) >> PAGE_SHIFT)
+
 #define PTRSIZE 	8lu
 #define PTRSHIFT 	3lu
 

+ 76 - 1
arch/x86/init/head64.S

@@ -28,7 +28,10 @@ _start:
 	/* we are now in the long-mode, just 
 	 * do some initializes, and call main */
 
-	lgdt gdt_descr(%rip)
+	call setup_stack
+	call init_gdt
+	call setup_paging
+
 
 	movq $0xffff800000000000, %rax
 	leaq 1f(%rip), %rcx
@@ -50,6 +53,66 @@ _start:
 	jmp 1b
 
 
+init_gdt:
+	lgdt gdt_descr(%rip)
+
+	movq $0x10, %rax
+
+	movq %rax, %ds
+	movq %rax, %es
+	movq %rax, %fs
+	movq %rax, %gs
+	movq %rax, %ss
+
+	ret
+
+setup_stack:
+	/* `setup_paging` used 0~0x80000, so we must 
+	 * find a new space for stack (old stack at 0x7c00). 
+	 * 
+	 * but sadly, we can't use address above 1048576 because 
+	 * the `arch_mm_init` will use them as the page and zone 
+	 * structures. so, would you like to use someplace in 
+	 * .data? :-) */
+
+	movq (%rsp), %rax
+
+	movq $0x90000, %rsp
+
+	jmp *%rax
+
+
+setup_paging:
+	/* setup pde, and reload segment registers */
+
+	movq $3, %rax
+	movq $0x3000, %rdi
+
+	cld
+
+rspde: 	stosq
+	addq $4096, %rax
+
+	cmpq $0x80000, %rdi
+	jbe rspde
+
+	/* now pdpte */
+	
+	movq $0x3003, %rax
+	movq $0x2000, %rdi
+
+rsppdp:	stosq
+	addq $4096, %rax
+	cmpq $0x80003, %rax
+
+	jbe rsppdp
+
+	xorq %rax, %rax
+	movq %rax, %cr3
+
+	ret
+	
+
 .data
 
 .align 16
@@ -68,3 +131,15 @@ gdt_descr:
 	.quad gdt
 
 
+.globl idt
+
+idt: 	.fill 512, 8, 0
+
+idt_descr:
+	.word . - idt
+	.quad idt
+
+
+.data
+
+

+ 16 - 0
arch/x86/kernel/Makefile

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

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

@@ -0,0 +1,99 @@
+/* arch/x86/kernel/archx86_mm32.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 <arch/archinfo.h>
+#include <arch/mm.h>
+#include <arch/string.h>
+
+
+#include <sys/types.h>
+
+
+extern char cano_putc(int);
+
+
+/* the most important work is: get total size of ram, 
+ * and initiallize page structures */
+
+int arch_mm_init(void) {
+	struct e820_entry 	*e;
+	struct zone 		*z;
+	struct page 		*p;
+
+	int 			i;
+
+	__archptr_t		addr;
+
+
+	/* put them there, after that, are the page structures. */
+	z = (struct zone *) &_end;
+	p = (struct page *) ((struct zone *) &_end + 5);
+
+	
+	memset(z, 0, sizeof(struct zone) * 5);
+
+	z[0].type = ZONETYPE_RAM;
+
+
+	for (i = 1; i < 5; i++) 
+		z[i].type = ZONETYPE_OTHER;
+
+
+	for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
+		if (e->type > E820_TYPE_MAX || !e->type) 
+			break;
+
+		addr = PAGE_ALIGN(e->addr);
+
+
+		if (z[e->type].start_addr > (void *) addr) 
+			z[e->type].start_addr = (void *) addr;
+
+
+		z[e->type].total_pages++;
+
+
+		/* now initiallize page structures */
+
+		for (i = 0; i < PAGE_NR(e->len); i++) {
+			p->pnr = PAGE_NR(addr);
+			p->count = 0;
+			p->next = p + 1;
+
+			++p;
+		}
+	}
+
+	cano_putc('O');
+
+
+	return 0;
+}
+
+
+
+arch_init(arch_mm_init);
+
+

+ 0 - 0
include/config.h


+ 3 - 0
include/kernel/init.h

@@ -39,6 +39,9 @@
 extern void *_init_call_arch, *_init_call_kernel, *_init_call_modules;
 extern void *_einit_call_arch, *_einit_call_kernel, *_einit_call_modules;
 
+extern void *_end;
+
+
 #define arch_init(fn) 	\
 	void *__initcall1_##fn __attribute__ ((__section__ (".initcalls.arch"))) = fn;
 

+ 73 - 0
include/kernel/mm.h

@@ -0,0 +1,73 @@
+/* include/kernel/mm.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 __MM_H
+#define __MM_H
+
+
+#ifndef __ASSEMBLER__
+
+
+#include <sys/types.h>
+#include <arch/archinfo.h>
+
+
+
+/* a descriptor for each page to describe a page, hopely be smallest
+ * and this is create only for the buddy memory management system. we 
+ * can't implement that without this, because we need more memory when 
+ * inserting. 
+ *
+ *
+ * we should initiallize this as early as we can. 
+ * */
+
+struct page {
+	/* number of this page */
+	__archptr_t 	pnr;
+
+	/* for cow, the reference count. we won't free if count-- */
+	__uint32_t 	count;
+
+	struct page 	*next;
+};
+
+
+/* okay, that's may only in the x86, but... */
+
+struct zone {
+	__uint32_t 	total_pages;
+
+#define ZONETYPE_RAM 	1
+#define ZONETYPE_OTHER 	2
+
+	__uint32_t 	type;
+
+	void 		*start_addr;
+	
+	struct page 	*pages;
+};
+
+
+#endif
+
+#endif
+

+ 5 - 2
init/main.c

@@ -29,14 +29,17 @@
 
 int do_init_calls(void);
 
+extern int cano_putc(int);
 
 
 int __init init(void) {
+	cano_putc('A');
+
 	return do_init_calls();
 }
 
 
-static void *init_call_levels[] = {
+void *init_call_levels[] = {
 	&_init_call_arch, &_einit_call_arch, 
 	&_init_call_kernel, &_einit_call_kernel, 
 	&_init_call_modules, &_einit_call_modules
@@ -65,7 +68,7 @@ int do_init_calls(void) {
 
 	
 	for (i = 0; i < ARRAYSIZE(init_call_levels); i++) {
-		start = init_call_levels[++i];
+		start = init_call_levels[i];
 		end = init_call_levels[++i];
 
 		int ncalls = end - start;