Procházet zdrojové kódy

0.437u-init: now we can write in C

rain před 5 roky
rodič
revize
02e0544a36
7 změnil soubory, kde provedl 120 přidání a 14 odebrání
  1. 2 1
      LINES
  2. 2 2
      Makefile
  3. 5 0
      README.md
  4. 15 5
      boot/loader.asm
  5. 6 1
      init/Makefile
  6. 49 5
      init/head.S
  7. 41 0
      init/main.c

+ 2 - 1
LINES

@@ -1,5 +1,6 @@
 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
+2021-5-30 0.427: 427 lines, 4 files, rain
+2021-5-30 0.437: 437 lines, 5 files, rain
 

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 OBJS = boot/bootsect 
-KOBJS = init/head.o
-KSRCS = init/head.S
+KOBJS = init/head.o init/main.o
+KSRCS = init/head.S init/main.c
 
 all: Makefile
 	make -C boot/

+ 5 - 0
README.md

@@ -25,3 +25,8 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &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. 
 
+0.437u-init:  
+&nbsp;&nbsp;&nbsp;&nbsp;`head.S` 'return' to init(void), then `init` put a message on screen and return to `head.S`.  
+&nbsp;&nbsp;&nbsp;&nbsp;and then `head.S` check return value of init, if not zero, it will put a message on screen. 
+&nbsp;&nbsp;&nbsp;&nbsp;if no error, it will halted the CPU. 
+

+ 15 - 5
boot/loader.asm

@@ -101,28 +101,38 @@ init_prot:
 	; now we switch to long-mode
 
 .to_long_mode:
+	; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
+	xor edi, edi
+	; mov edi, 0x00000000
+
+	xor eax, eax
+	; store for 4096 times
+	mov ecx, 4096
+
+	cld
+	rep stosd
 
 .init_paging:
 	; we set page map at 0x00000000 (a good address)
 
 	mov dword [0x00000000], 0x00001000 | PAGE_USER
-	mov dword [0x00000004], 0x00000000
+;	mov dword [0x00000004], 0x00000000
 
 	mov dword [0x00000800], 0x00001000 | PAGE_USER
-	mov dword [0x00000804], 0x00000000
+;	mov dword [0x00000804], 0x00000000
 
 	; and page directory pointer table
 
 	mov dword [0x00001000], 0x00002000 | PAGE_USER
-	mov dword [0x00001004], 0x00000000
+;	mov dword [0x00001004], 0x00000000
 
 	; page directory entry (2 MB)
 
 	mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
-	mov dword [0x00002004], 0x00000000
+;	mov dword [0x00002004], 0x00000000
 
 	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
-	mov dword [0x0000200c], 0x00000000
+;	mov dword [0x0000200c], 0x00000000
 
 .enable_long_mode:
 	lgdt [gdt64_desc]

+ 6 - 1
init/Makefile

@@ -1,4 +1,6 @@
-OBJS = head.o
+OBJS = head.o main.o
+
+CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -O3
 
 all: $(OBJS)
 
@@ -9,6 +11,9 @@ head.s: head.S Makefile
 head.o: head.s
 	as --64 head.s -o head.o
 
+%.o: %.c
+	gcc -c $*.c $(CFLAGS)
+
 clear: Makefile
 	rm head.s
 	rm $(OBJS)

+ 49 - 5
init/head.S

@@ -30,14 +30,52 @@
 .globl _start
 
 _start:
-	leaq message(%rip), %rsi
-	call print
+	/* we cannot use far-jmp in long mode. */
 
-die:	hlt
-	jmp die
+	/* 
+	 * 'call' will push address of next code, 
+	 * and 'ret' will pop it to %rip 
+	 * so we can push address, 'ret' will 
+	 * pop it and jump to it. 
+	 */
 
+	/* 64 bit code */
+
+	/* return address */
+	leaq init_done(%rip), %rax
+	pushq %rax
+
+	/* address will jump */
+	leaq init(%rip), %rax
+	pushq %rax
+
+	retq
+
+
+init_done:
+	/* andq %rax, %rax, but %rax won't change. */
+	testq %rax, %rax
+	
+	jnz init_panic
+
+init_die:
+	hlt
+	jmp init_die
+
+
+init_panic:
+	leaq panic_msg(%rip), %rdi
+	callq print
+
+	jmp init_die
+
+
+.globl print
 
 print:
+	/* C stores the first argument in %rdi */
+	movq %rdi, %rsi
+
 	movq framebuffer(%rip), %rdi
 
 	cld
@@ -62,12 +100,18 @@ print_ret:
 
 
 
-.data
+.section .rodata
 .align 8
 
 message:
 	.asciz "XNIX Kernel is starting..."
 
+panic_msg:
+	.asciz "panic in init: halted"
+
+
+.data
+
 .globl framebuffer
 
 framebuffer:

+ 41 - 0
init/main.c

@@ -0,0 +1,41 @@
+/* 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/>.  
+*/
+
+
+
+
+extern void print(char *s);
+
+
+/* 
+ * init(void): initial all modules and devices. 
+ *
+ * return value: error number. main will return after initial done. 
+ * if return value isn't 0, head.S will panic. else, it will halt. 
+ * */
+
+int init(void) {
+	print("XNIX kernel init");
+
+	return 0;
+}