Procházet zdrojové kódy

evx: now starting kernel on i386 and x86_64

rain před 5 roky
rodič
revize
b112764a4a

+ 3 - 0
Kconfig

@@ -0,0 +1,3 @@
+[	
+	{}
+]

+ 0 - 1
Makefile

@@ -53,7 +53,6 @@ obj-i 			+= $(PWD)/kernel/
 obj-i 			+= $(PWD)/init/ 
 
 obj-i 			+= $(ARCHDIR)/ 
-obj-i 			+= $(ARCHDIR)/init/ 
 
 
 _all:

+ 1 - 1
arch/x86/.archcflags

@@ -1 +1 @@
--m32
+-m64

+ 8 - 4
arch/x86/Makefile

@@ -1,8 +1,10 @@
 # this was called from the Makefile in /, 
 # and we just tell it what objects that he should make. 
 
-asm 	= boot/boot32.S boot/setup32.S 
-asmobj 	= boot/boot32.S.o boot/setup32.S.o 
+BITS = 64
+
+asm 	= boot/boot$(BITS).S boot/setup$(BITS).S 
+asmobj 	= boot/boot$(BITS).S.o boot/setup$(BITS).S.o 
 obj-i 	+= 
 
 target: 
@@ -11,12 +13,14 @@ ifeq ($(D), )
 	@echo "this should call from the Makefile in directory /, but not typing make in kernel/. :-(" >&2 
 	@exit 1
 endif
+	@echo "-m$(BITS)" > .archcflags
 
 # $(PWD) is the / directory of kernel, e.g. /root/evx/
 	@$(MAKE) -C $(PWD) D=$(shell pwd) obj-i="$(obj-i)"
 
 vmevx.o: $(PWD)/.objlist 
-	@cat $(PWD)/.objlist | xargs $(LD) -o vmevx.o -T vmevx.lds 
+	@$(MAKE) -C init/ BITS=$(BITS)
+	@cat $(PWD)/.objlist | xargs $(LD) -o vmevx.o -T vmevx$(BITS).lds 
 	@echo "LD                vmevx.o"
 
 vmevx.bin: vmevx.o
@@ -24,7 +28,7 @@ vmevx.bin: vmevx.o
 
 
 boot: 	vmevx.bin  
-	@$(MAKE) -C boot/ 
+	@$(MAKE) -C boot/ BITS=$(BITS)
 
 
 .PHONY clean:

+ 0 - 1
arch/x86/boot/Makefile

@@ -3,7 +3,6 @@
 # it. 
 
 # 's' is the asm object
-BITS = 32
 obj-s = boot$(BITS).S.o.bin setup$(BITS).S.o.bin 
 
 all: 	Image

+ 197 - 0
arch/x86/boot/boot64.S

@@ -0,0 +1,197 @@
+/* arch/x86/boot/boot64.S 
+ * 
+ * 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/>
+ */ 
+
+
+
+
+.code16
+
+
+/* load system to 0x8000, and boot */
+
+.globl _start
+
+_start:	xorw %ax, %ax
+
+	movw %ax, %ds
+	movw %ax, %es
+	movw %ax, %ss
+
+	movw $0x7c00, %sp
+
+	/* move self to 0x7e00 */
+	movw $0x7e00, %di
+	movw $0x7c00, %si
+
+	movw $512, %cx
+
+	cld
+
+	rep
+	movsb
+
+	ljmp $0x0000, $real_start
+
+
+real_start:
+	leaw loading_msg, %si
+	call print
+
+	call load_sys
+
+	call setup_e820
+
+	ljmp $0, $0x8000
+
+die:	hlt
+	jmp die
+
+
+load_sys:
+	/* load the system to 0x8000, and jump */
+
+	movw $0x0800, %ax
+	movw %ax, %es
+
+	xorw %bx, %bx
+	xorl %eax, %eax
+
+lloop:	incl %eax
+	movw $16, %cx
+
+	call read_lba
+
+	movw %es, %cx
+	addw $512, %cx
+	movw %cx, %es
+
+	cmpl $64, %eax
+	jb cont
+
+	ret
+
+cont:	pushal
+	/* put a '.' on screen */
+	movb $'.', %al
+	movb $0x0e, %ah
+
+	movw $0x000f, %bx
+
+	int $0x10
+
+	popal
+
+	jmp lloop
+
+
+read_lba:
+	pushaw
+	pushl $0
+	pushl %eax
+
+	pushw %es
+	pushw %bx
+
+	pushw %cx
+	pushw $16
+
+	movb $0x42, %ah
+	/* dx didn't change */
+
+	movw %sp, %si
+
+	int $0x13
+
+	jc read_failed
+
+	addw $16, %sp
+
+	popaw
+
+	ret
+
+
+read_failed:
+	leaw read_fail_msg, %si
+	call print
+
+	jmp die
+
+
+setup_e820:
+	xorl %ebx, %ebx
+	movl %ebx, %es
+
+	movl $0x7000, %edi
+
+sel:	movl $0xe820, %eax
+	movl $20, %ecx
+	movl $0x534d4150, %edx
+
+	int $0x15
+
+	jc e820_failed
+
+	addl $20, %edi
+
+	cmpl $0, %ebx
+	jne sel
+
+	ret
+
+e820_failed:
+	leaw e820_fail_msg, %si
+
+	pushw $die
+
+
+print:	cld
+	lodsb
+	
+	testb %al, %al
+	jz pret
+
+	movb $0x0e, %ah
+	movw $0x000f, %bx
+
+	int $0x10
+
+	jmp print
+
+pret:	ret
+
+
+read_fail_msg:
+	.ascii "read /dev/sda failed. "
+	.byte 0x0d, 0x0a, 0x00
+
+loading_msg:
+	.asciz "Loading System"
+
+e820_fail_msg:
+	.asciz "get e820 meminfo failed. "
+
+
+.org 0x1be
+
+part0:
+
+.org 510
+
+.word 0xaa55
+

+ 185 - 0
arch/x86/boot/setup64.S

@@ -0,0 +1,185 @@
+/* arch/x86/boot/setup64.S 
+ * 
+ * This file is part of xnix. 
+ * 
+ * XNIX 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. 
+ * 
+ * XNIX 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 XNIX. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+.code16
+
+
+/* switch to the long-mode, and to real system */
+
+.globl _start
+
+_start:	xorw %ax, %ax
+
+	movw %ax, %ds
+	movw %ax, %es
+	movw %ax, %ss
+
+	/* yeah, we should move it again. */
+	movw $0x7e00, %di
+	movw $0x8000, %si
+
+	movw $512, %cx
+
+	cld
+	rep
+	movsb
+
+	movw $0x7c00, %sp
+
+	ljmp $0, $real_start
+
+real_start:
+	/* open a20 */
+	cli
+
+	inb $0x92, %al
+	orb $0x02, %al
+	outb %al, $0x92
+
+	/* setup gdt */
+	lgdt gdt32_descr
+
+	movl %cr0, %eax
+	orl $1, %eax
+
+	/* that's it! */
+	movl %eax, %cr0
+
+	/* setup paging (for long mode) */
+
+	movl $0, %edi
+	movl $512, %ecx
+	xorl %eax, %eax
+
+	cld
+
+rsetpt:	rep
+	stosl
+
+	movl $0x00001003, 0
+	movl $0x00001003, 0x800
+
+	movl $0x00002003, 0x1000
+	movl $0x00003003, 0x2000
+	
+	movl $0x3000, %edi
+	movl $0x00000083, %eax
+
+rsetp:	movl %eax, (%edi)
+	movl $0, 4(%edi)
+
+	addl $8, %edi
+	addl $4096, %eax
+
+	cmpl $0x800000, %eax
+	jbe rsetp
+
+	ljmp $0x08, $init_prot
+
+
+.code32
+
+
+init_prot:
+	movl $0x10, %eax
+
+	movl %eax, %ds
+	movl %eax, %es
+	movl %eax, %fs
+	movl %eax, %gs
+
+	movl %eax, %ss
+
+	movl $0x7c00, %esp
+
+	/* move it to 1mb */
+
+	movl $1048576, %edi
+	movl $0x8200, %esi
+
+	movl $131072, %ecx
+
+	cld
+
+	rep
+	movsl
+
+to_long:
+	lgdt gdt64_descr
+	
+	movl $0x000000a0, %eax
+	movl %eax, %cr4
+
+	xorl %eax, %eax
+	movl %eax, %cr3
+
+	movl $0xc0000080, %ecx
+	rdmsr
+
+	orl $0x100, %eax
+	wrmsr
+
+	/* PG and PE */
+	movl %cr0, %eax
+	orl $0x80000001, %eax
+	movl %eax, %cr0
+
+	ljmp $0x08, $init_lm
+
+
+.code64
+
+init_lm:
+	movq $0x10, %rax
+
+	movq %rax, %ds
+	movq %rax, %es
+	movq %rax, %fs
+	movq %rax, %gs
+	movq %rax, %ss
+	
+	movq $0x7c00, %rsp
+
+	pushq $1048576
+	retq
+
+
+.globl gdt32
+
+gdt32:	.long 0x00000000, 0x00000000
+	.long 0x0000ffff, 0x00cf9a00
+	.long 0x0000ffff, 0x00cf9200
+
+
+.globl gdt32_descr
+
+gdt32_descr:
+	.word . - gdt32
+	.long gdt32
+
+gdt64:	.quad 0x0000000000000000
+	.quad 0x0020980000000000
+	.quad 0x0000920000000000
+
+gdt64_descr:
+	.word . - gdt64
+	.long gdt64
+

+ 2 - 2
arch/x86/init/Makefile

@@ -1,10 +1,10 @@
-obj-s = head.S.o 
+obj-s = head$(BITS).S.o 
 
 target: $(obj-s)
 	@echo `pwd`/$(obj-s) >> $(PWD)/.objlist 
 
 %.S.o: %.S
-	@$(CPP) $*.S | $(AS) --32 -o $*.S.o 
+	@$(CPP) $*.S | $(AS) --$(BITS) -o $*.S.o 
 	@echo "AS                $*.S.o"
 
 

+ 1 - 1
arch/x86/init/head.S → arch/x86/init/head32.S

@@ -1,4 +1,4 @@
-/* arch/x86/init/head.S 
+/* arch/x86/init/head32.S 
  * 
  * This file is part of evx. 
  * 

+ 70 - 0
arch/x86/init/head64.S

@@ -0,0 +1,70 @@
+/* arch/x86/init/head64.S 
+ * 
+ * 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/>
+ */ 
+
+
+
+
+.code64
+
+
+.globl _start
+
+_start:
+	/* we are now in the long-mode, just 
+	 * do some initializes, and call main */
+
+	lgdt gdt_descr(%rip)
+
+	movq $0xffff800000000000, %rax
+	leaq 1f(%rip), %rcx
+
+	orq %rcx, %rax
+
+	pushq %rax
+
+	movq $0xffff800000000000, %rax
+	leaq init(%rip), %rcx
+
+	orq %rcx, %rax
+
+	pushq %rax
+
+	retq
+
+1:	hlt
+	jmp 1b
+
+
+.data
+
+.align 16
+
+.globl gdt
+
+gdt:	.quad 0x0000000000000000
+	.quad 0x0020980000000000
+	.quad 0x0000920000000000
+
+
+.globl gdt_descr
+
+gdt_descr:
+	.word . - gdt
+	.quad gdt
+
+

+ 0 - 0
arch/x86/vmevx.lds → arch/x86/vmevx32.lds


+ 50 - 0
arch/x86/vmevx64.lds

@@ -0,0 +1,50 @@
+OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64")
+OUTPUT_ARCH(i386:x86-64)
+ENTRY(_start)
+
+SECTIONS {
+	. = 0xffff800000000000 + 0x100000;
+
+	.text : {
+		_text = .;
+		*(.text)
+		_etext = .;
+	}
+
+	.init.text : {
+		*(.init.text)
+	}
+
+	.initcalls.arch : {
+		_init_call_arch = .;
+		*(.initcalls.arch)
+		_einit_call_arch = .;
+	}
+
+	.initcalls.kernel : {
+		_init_call_kernel = .;
+		*(.initcalls.kernel)
+		_einit_call_kernel = .;
+	}
+
+	.initcalls.modules : {
+		_init_call_modules = .;
+		*(.initcalls.modules)
+		_einit_call_modules = .;
+	}
+
+	.data : {
+		_data = .;
+		*(.data)
+		_edata = .;
+	}
+
+	.rodata : {
+		_rodata = .;
+		*(.rodata)
+		_erodata = .;
+	}
+
+	_end = .;
+}
+

+ 73 - 0
scripts/do_mkconfig.py

@@ -0,0 +1,73 @@
+#!/usr/bin/python3 
+
+# this script is using to create a .config configuration file
+
+import sys
+import os
+import json
+
+
+argc = 0
+
+for name in sys.argv:
+    argc += 1
+
+def fatal(msg):
+    sys.stderr.write("%s: fatal: %s\n" % (sys.argv[0], msg))
+    exit(1)
+
+
+if argc != 1:
+    fatal("no argument this requires")
+
+if os.getcwd()[-7 : -1] == "script":
+    fatal("call this as scripts/do_mkconfig.py, not ./do_mkconfig.py")
+
+
+basedir = os.getcwd()
+config_filename = basedir + "/include/config.h"
+kconfig_filename = basedir + "/Kconfig"
+
+# unlike that in linux, Kconfig is just a json likes that: 
+# [
+#   {
+#       "name": "descr"
+#   }, 
+#   {
+#        "DISABLE_A": "will you disable A"
+#   }
+# ]
+
+
+try:
+    kconfig_fd = open(kconfig_filename, "r")
+
+except FileNotFoundError:
+    fatal("Kconfig not exists")
+
+out_fd = open(config_filename, "w")
+
+
+kconfig_jsonobj = json.load(kconfig_fd)
+kconfig_fd.close()
+
+if type(kconfig_jsonobj) != list:
+    fatal("Kconfig should be a json list object, but %s gave" % type(kconfig_jsonobj))
+
+
+for config in kconfig_jsonobj:
+    for key, descr in config.items():
+        print("CONFIG %s: " % key)
+        print("\t(" + descr + ")")
+
+        value = input("> (int) ")
+        
+        try:
+            value = int(value)
+
+        except ValueError:
+            fatal("an integer requires for %s" % key)
+
+        out_fd.write("#define %s \t\t\t(%d)\n" % (key, value))
+        print("\n")
+