瀏覽代碼

evx: add supporting on x86 arch: just boot

rain 5 年之前
父節點
當前提交
b34e595d19
共有 10 個文件被更改,包括 398 次插入 和 5 次删除
  1. 3 1
      Makefile
  2. 10 2
      arch/x86/Makefile
  3. 0 0
      arch/x86/b.c
  4. 35 0
      arch/x86/boot/Makefile
  5. 12 0
      arch/x86/boot/boot.lds
  6. 197 0
      arch/x86/boot/boot32.S
  7. 141 0
      arch/x86/boot/setup32.S
  8. 0 2
      kernel/Makefile
  9. 0 0
      kernel/a.c
  10. 0 0
      kernel/foo/b.c

+ 3 - 1
Makefile

@@ -52,7 +52,9 @@ _all:
 
 
 .PHONY clean:
-	@find ./ -name "*.o" | xargs rm -f 
+	@find ./ -name "*.o" \
+	       -o -name "*.bin"	| xargs rm -f
+	@$(MAKE) -C $(ARCHDIR)/ clean
 
 else
 

+ 10 - 2
arch/x86/Makefile

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

+ 0 - 0
arch/x86/b.c


+ 35 - 0
arch/x86/boot/Makefile

@@ -0,0 +1,35 @@
+# this is calling from the /evx/arch/x86/Makefile and we need to 
+# compile these assembly codes those there isn't a rule to compile 
+# it. 
+
+# 's' is the asm object
+BITS = 32
+obj-s = boot$(BITS).S.o.bin setup$(BITS).S.o.bin 
+
+all: 	Image
+
+%.S.o: 		%.S
+	@$(CPP)	$*.S | $(AS) --32 -o $*.S.o.l 
+	@echo "AS                $*.S.o.l"
+	@$(LD) 	$*.S.o.l -o $*.S.o -T boot.lds 
+	@rm $*.S.o.l 
+	@echo "LD                $*.S.o"
+
+%.S.o.bin:	%.S.o
+	@objcopy -I elf32-i386 -S -j .text -O binary $*.S.o $*.S.o.bin
+	@rm $*.S.o 
+	@echo "OBJCOPY           $*.S.o.bin"
+
+
+Image:	$(obj-s)
+	@dd if=/dev/zero of=Image bs=1048576 count=64 >& /dev/zero 
+	@dd if=boot$(BITS).S.o.bin of=Image conv=notrunc >& /dev/zero 
+	@dd if=setup$(BITS).S.o.bin of=Image conv=notrunc bs=512 seek=1 >& /dev/zero 
+	@rm *.bin 
+	@echo "MKIMG             Image"
+
+
+.PHONY clean:
+	@rm Image -f  
+	@echo "CLEAN             Image"
+

+ 12 - 0
arch/x86/boot/boot.lds

@@ -0,0 +1,12 @@
+OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_ARCH(i386)
+ENTRY(_start)
+
+SECTIONS {
+	. = 0x7e00;
+	.text : {
+		_text = .;
+		*(.text)
+		_etext = .;
+	}
+}

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

@@ -0,0 +1,197 @@
+/* arch/x86/boot/boot32.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
+

+ 141 - 0
arch/x86/boot/setup32.S

@@ -0,0 +1,141 @@
+/* arch/x86/boot/setup32.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 protected-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 idt and gdt */
+	lgdt gdt32_descr
+
+	movl %cr0, %eax
+	orl $1, %eax
+
+	/* that's it! */
+	movl %eax, %cr0
+
+	/* setup paging */
+
+	movl $0, %edi
+	movl $512, %ecx
+	xorl %eax, %eax
+
+	cld
+
+rsetpt:	rep
+	stosl
+
+	movl $0x00001007, 0
+	movl $0x00001007, 0xc00
+	
+	movl $0x1000, %edi
+	movl $1024, %ecx
+	movl $0x00000007, %eax
+
+rsetp:	stosl
+	addl $4096, %eax
+
+	loop rsetp
+
+	xorl %eax, %eax
+	movl %eax, %cr3
+
+	movl %cr0, %eax
+	orl $0x80000000, %eax
+	movl %eax, %cr0
+
+	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 $0x8400, %esi
+
+	movl $131072, %ecx
+
+	cld
+
+	rep
+	movsl
+
+	ljmp $0x08, $1048576
+
+
+.globl gdt32
+
+gdt32:	.long 0x00000000, 0x00000000
+	.long 0x0000ffff, 0x00cf9a00
+	.long 0x0000ffff, 0x00cf9200
+
+
+.globl gdt32_descr
+
+gdt32_descr:
+	.word . - gdt32
+	.long gdt32
+

+ 0 - 2
kernel/Makefile

@@ -2,8 +2,6 @@
 # and we just tell it what objects that he should make. 
 
 obj-i = 
-obj-i += a.c 
-obj-i += foo/b.c 
 
 target:
 

+ 0 - 0
kernel/a.c


+ 0 - 0
kernel/foo/b.c