Quellcode durchsuchen

2.131 buggy: no error in bochs, but error qemu and on hardware. (* (&'XNIX kernel init') = 0 in qemu)

rain vor 5 Jahren
Ursprung
Commit
12d9f823c9
10 geänderte Dateien mit 154 neuen und 38 gelöschten Zeilen
  1. 16 1
      boot/loader.asm
  2. 64 0
      include/kernel/args.h
  3. 1 1
      init/Makefile
  4. 1 9
      init/main.c
  5. 1 1
      kernel.lds
  6. 1 1
      kernel/Makefile
  7. 15 2
      kernel/int.c
  8. 20 2
      kernel/trap.S
  9. 1 1
      modules/Makefile
  10. 34 20
      modules/tty.c

+ 16 - 1
boot/loader.asm

@@ -98,6 +98,15 @@ init_prot:
 
 	mov sp, 0x00007c00
 
+	; copy kernel to 0x100000
+
+	mov esi, 0x8000
+	mov edi, 0x100000
+
+	mov ecx, 64 * 512
+
+	call memcpy
+
 	; now we switch to long-mode
 
 .to_long_mode:
@@ -176,7 +185,13 @@ init_prot:
 
 	; okay! now set segment registers and init long mode
 	
-	jmp code64:0x0000000000008000
+	jmp code64:0x100000
+
+memcpy:
+	cld
+	rep movsb
+
+	ret
 
 
 

+ 64 - 0
include/kernel/args.h

@@ -0,0 +1,64 @@
+/* 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/>.  
+*/
+
+
+
+
+#ifndef INCLUDED_ARGS_H
+#define INCLUDED_ARGS_H
+
+#include <kernel/types.h>
+#include <arch/sys.h>
+
+/*
+ * arguments in 64 bit: 
+ * %rdi, %rsi, %rdx, %rcx, %r8, %9, and stack. 
+ * */
+
+#define get_arg(type, argno) ({		\
+	__uint64_t __res;			\
+	if (argno < 7) {			\
+		if (argno == 1) 		\
+			__res = get_rdi();	\
+		else if (argno == 2)		\
+			__res = get_rsi();	\
+		else if (argno == 3)		\
+			__res = get_rdx();	\
+		else if (argno == 4)		\
+			__res = get_rcx();	\
+		else if (argno == 5)		\
+			__res = get_r8();	\
+		else if (argno == 6)		\
+			__res = get_r9();	\
+	}					\
+	else {					\
+		argno -= 6;			\
+		__uint64_t *p = (__uint64_t *) 	\
+				get_rsp();	\
+		__res = p[argno];		\
+	}					\
+	(type) __res;				\
+})
+
+#endif
+

+ 1 - 1
init/Makefile

@@ -1,6 +1,6 @@
 OBJS = head.o main.o
 
-CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror 
+CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 all: $(OBJS)
 

+ 1 - 9
init/main.c

@@ -42,18 +42,10 @@
  * */
 
 int init(void) {
-	printf("XNIX kernel init\n");
+	printf("XNIX kernel init, 1 + 1 = %d\n", 1 + 1);
 
 	init_int();
 
-	/* divide by zero */
-	unsigned int i = 20;
-	unsigned int j = 1;
-
-	i = i / (j - 1);
-
-	printf("i = %d\n", i);
-
 	return 0;
 }
 

+ 1 - 1
kernel.lds

@@ -3,7 +3,7 @@ OUTPUT_ARCH(i386:x86-64)
 ENTRY(_start)
 
 SECTIONS {
-	. = 0x8000;
+	. = 0x100000;
 	.text : {
 		_text = .;
 		*(.text)

+ 1 - 1
kernel/Makefile

@@ -1,4 +1,4 @@
-CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror 
+CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
 all: tty.o int.o trap.o
 
 

+ 15 - 2
kernel/int.c

@@ -90,9 +90,22 @@ void syscall(void) {
 }
 
 
+void die(char *s, __uint64_t *rsp, char nr) {
+	printf("int #%b: %s. \n");
+
+	int i;
+
+	for (i = 0; i < 50; i++) 
+		printf("stack: %q\n", rsp[i]);
+
+	for (;;);
+}
+
+
 void do_divide_error(__uint64_t *rsp) {
-	printf("int #0x00: divide error. \n");
-	printf("RSP: %q\n", rsp);
+	die("divide error", rsp, 0);
 
 	return;
 }
+
+

+ 20 - 2
kernel/trap.S

@@ -96,9 +96,23 @@ restore_all:
 	iretq
 
 
+.globl __stack_chk_fail
+
+__stack_chk_fail:
+	movq schkfail_msg(%rip), %rdi
+	callq printf
+
+scf_die:
+	hlt
+	jmp scf_die
+
+
 .globl divide_error
 
 divide_error:
+	/* at fact, we don't need cli because CPU will do it. */
+	cli
+
 	/* error code: 0 */
 	pushq $0
 	pushq %rax
@@ -113,8 +127,12 @@ divide_error:
 
 	/* return */
 
-die:	hlt
-	jmp die	
+	sti
 
 	jmp restore_all
 	
+
+.section .rodata
+
+schkfail_msg:
+	.asciz "kernel panic: stack check fail. \n"

+ 1 - 1
modules/Makefile

@@ -1,5 +1,5 @@
 OBJS = tty.o
-CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -O3 -fno-stack-protector
+CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include -Wall -Werror -g
 
 all: $(OBJS)
 

+ 34 - 20
modules/tty.c

@@ -34,7 +34,7 @@
 
 #include <lib/string.h>
 
-#include <stdarg.h>
+#include <kernel/args.h>
 
 
 
@@ -88,15 +88,16 @@ __uint64_t printf(const char *s, ...) {
 	char state, c;
 	/* fmt_s only use in %s, but we cannot define variables in switch-case */
 	char *fmt_s;
-	va_list values;
+
+	int argno;
 
 	int newlines;
 
-	va_start(values, s);
 	state = 0;
 	newlines = 0;
 
-	for (i = 0, l = 0; s[i]; i++) {
+	/* argno = 2 because first argument is 's' */
+	for (i = 0, l = 0, argno = 2; s[i]; i++) {
 		c = s[i];
 		/* check state */
 		if (!state) {
@@ -125,37 +126,51 @@ __uint64_t printf(const char *s, ...) {
 		/* it's a formater */
 		else {
 			if (c == 'd') {
-				l += printint((int) va_arg(values, int), 10, 1);
+				l += printint((int) get_arg(int, argno), 10, 1);
+				argno++;
 			}
 
 			else if (c == 'x') {
 				/* `x` is 64-bits, I think it's */
-				l += printint((long) va_arg(values, long), 16, 0);
+				l += printint((long) get_arg(long, argno), 16, 0);
+				argno++;
 			}
 
-			else if (c == 'p') 
+			else if (c == 'p') {
 				/* zeros at high bits should display, not like `%x` or `%d` */
-				l += printptr((__uint64_t) va_arg(values, __uint64_t), 16);
+				l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16);
+				argno++;
+			}
 			
 
 			/* put a byte */
-			else if (c == 'b') 
-				l += printptr((__uint64_t) va_arg(values, __uint32_t), 2);
+			else if (c == 'b') {
+				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 2);
+				argno++;
+			}
 
 			/* put a word */
-			else if (c == 'w') 
-				l += printptr((__uint64_t) va_arg(values, __uint32_t), 4);
+			else if (c == 'w') {
+				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 4);
+				argno++;
+			}
 
-			else if (c == 'l') 
-				l += printptr((__uint64_t) va_arg(values, __uint32_t), 8);
+			else if (c == 'l') {
+				l += printptr((__uint64_t) get_arg(__uint32_t, argno), 8);
+				argno++;
+			}
 
-			else if (c == 'q') 
-				l += printptr((__uint64_t) va_arg(values, __uint64_t), 16);
+			else if (c == 'q') {
+				l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16);
+				argno++;
+			}
 			
 			
 
 			else if (c == 's') {
-				fmt_s = (char *) va_arg(values, char *);
+				fmt_s = (char *) get_arg(char *, argno);
+				argno++;
+
 				if (!fmt_s) 
 					fmt_s = "(null)";
 						
@@ -169,7 +184,8 @@ __uint64_t printf(const char *s, ...) {
 			
 			else if (c == 'c') {
 				/* `char` aligned to `int` in GCC */
-				putc((char) va_arg(values, int));
+				putc((char) get_arg(int, argno));
+				argno++;
 				l++;
 			}
 
@@ -210,8 +226,6 @@ __uint64_t printf(const char *s, ...) {
 
 	update_cursor();
 
-	va_end(values);
-
 	return i;
 }