Bläddra i källkod

0.427u-init: jump to kernel

rain 5 år sedan
förälder
incheckning
e014d3db09
8 ändrade filer med 163 tillägg och 30 borttagningar
  1. 1 0
      LINES
  2. 16 7
      Makefile
  3. 6 2
      README.md
  4. 16 6
      boot/boot.asm
  5. 1 15
      boot/loader.asm
  6. 15 0
      init/Makefile
  7. 75 0
      init/head.S
  8. 33 0
      kernel.lds

+ 1 - 0
LINES

@@ -1,4 +1,5 @@
 2021-5-21 0.066: 66 lines, 1 files, rain
 2021-5-29 0.228: 228 lines, 2 files, rain
 2021-5-29 0.431: 431 lines, 3 files, rain
+2021-5-29 0.427: 427 lines, 4 files, rain
 

+ 16 - 7
Makefile

@@ -1,23 +1,32 @@
-OBJS = boot/bootsect
+OBJS = boot/bootsect 
+KOBJS = init/head.o
+KSRCS = init/head.S
 
 all: Makefile
 	make -C boot/
+	make -C init/
 	make xnix.img
 
+kernel: $(KSRCS) boot/bootsect Makefile
+	ld -b elf64-x86-64 -o kernel.o $(KOBJS) -T kernel.lds
+	objcopy -I elf64-x86-64 -S -R ".comment" -R ".eh_frame" -O binary kernel.o kernel
 
-xnix.img: $(OBJS) Makefile
+
+xnix.img: boot/bootsect $(KSRCS) Makefile kernel
 	dd if=/dev/zero of=xnix.img bs=1024 count=1440
-	dd if=$(OBJS) of=xnix.img conv=notrunc seek=0
+	dd if=boot/bootsect of=xnix.img conv=notrunc seek=0
+	dd if=kernel of=xnix.img conv=notrunc bs=512 seek=2
 
 
-lines: 
+lines: Makefile
 	wc -l `find ./ -name *.asm`
 
 
-clear: 
+clear: Makefile
 	make -C boot/ clear
-	rm $(OBJS)
-	rm xnix.img
+	make -C init/ clear
+	rm boot/bootsect
+	rm xnix.img kernel kernel.o
 
 
 

+ 6 - 2
README.md

@@ -3,8 +3,7 @@
 ### Overview
 
 XNIX is a UNIX-like operating system uses the GPLv3 license. It is a free and open 
-source software, you can get its source code from 
-<https://github.com/pengruiyang-cpu/xnix> or <https://gitee.com/pengruiyang-cpu/xnix>. 
+source software, you can get its source code from <https://gitee.com/pengruiyang-cpu/xnix>. 
 
 
 ### Changes
@@ -21,3 +20,8 @@ source software, you can get its source code from
 
 0.431u-boot:  
 &nbsp;&nbsp;&nbsp;&nbsp;`loader.asm` switching to long mode. And it set page map to address 0x00000000. It mapped 4MB memories. 
+
+0.427u-init:  
+&nbsp;&nbsp;&nbsp;&nbsp;`boot.asm` read 64 sectors to `0x00007e00`, and `loader.asm` jumps to it after switching to long-mode. 
+&nbsp;&nbsp;&nbsp;&nbsp;then, `head.S` displays a message on screen. 
+

+ 16 - 6
boot/boot.asm

@@ -41,19 +41,29 @@ _start:
 	mov si, boot_msg
 	call print
 
-	mov ax, 1
-	mov cl, 1
 
+	xor ax, ax
 	mov bx, 0x7e00
 
+.read_it:
+	inc ax
+	mov cl, 1
+
+	push ax
+	push bx
+
 	call read_sector
 
-	jmp 0x0000:0x7e00
+	pop bx
+	pop ax
 
+	add bx, 512
 
-.die:
-	hlt
-	jmp .die
+	cmp ax, 64
+
+	jne .read_it
+
+	jmp 0x0000:0x7e00
 
 
 

+ 1 - 15
boot/loader.asm

@@ -166,23 +166,9 @@ init_prot:
 
 	; okay! now set segment registers and init long mode
 	
-	jmp dword code64:init_long_mode
+	jmp code64:0x0000000000008000
 
 
-bits 64
-
-init_long_mode:
-	mov ax, data64
-
-	mov es, ax
-	mov ds, ax
-	mov fs, ax
-	mov gs, ax
-	mov ss, ax
-
-.die:	hlt
-	jmp .die
-
 
 message: db "Starting Loader", 0x0d, 0x0a, 0x00
 

+ 15 - 0
init/Makefile

@@ -0,0 +1,15 @@
+OBJS = head.o
+
+all: $(OBJS)
+
+
+head.s: head.S Makefile
+	gcc -E head.S > head.s
+
+head.o: head.s
+	as --64 head.s -o head.o
+
+clear: Makefile
+	rm head.s
+	rm $(OBJS)
+

+ 75 - 0
init/head.S

@@ -0,0 +1,75 @@
+/* Copyright (C) 2021 Rain */
+
+/* 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) and 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/>.  
+*/
+
+
+
+.text
+
+.align 8
+
+.globl _start
+
+_start:
+	leaq message(%rip), %rsi
+	call print
+
+die:	hlt
+	jmp die
+
+
+print:
+	movq framebuffer(%rip), %rdi
+
+	cld
+
+print_loop:
+	lodsb
+
+	testb %al, %al
+	jz print_ret
+
+	movb $0x07, %ah
+	stosw
+
+	jmp print_loop
+
+print_ret:
+	/* update frame buffer */
+	movq %rdi, framebuffer(%rip)
+
+	ret
+
+
+
+
+.data
+.align 8
+
+message:
+	.asciz "XNIX Kernel is starting..."
+
+.globl framebuffer
+
+framebuffer:
+	.quad 0x00000000000b8000
+

+ 33 - 0
kernel.lds

@@ -0,0 +1,33 @@
+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 = .;
+	}
+
+	. = ALIGN(8);
+	.data : {
+		_data = .;
+		*(.data);
+		_edata = .;
+	}
+
+	.rodata : {
+		_rodata = .;
+		*(.rodata)
+		.erodata = .;
+	}
+
+	.bss : {
+		_bss = .;
+		*(.bss)
+		_ebss = .;
+	}
+
+	_end = .;
+}