소스 검색

3.455u-int: support IRQ and all trap handlers

rain 5 년 전
부모
커밋
8cead0dffa
20개의 변경된 파일과 1188개의 추가작업 그리고 56개의 파일을 삭제
  1. 1 1
      LINES
  2. 2 2
      Makefile
  3. 5 1
      README.md
  4. 2 2
      boot/boot.asm
  5. 7 3
      include/arch/sys.h
  6. 3 2
      include/kernel/graphic.h
  7. 7 0
      include/kernel/init.h
  8. 131 0
      include/kernel/int.h
  9. 38 0
      include/kernel/panic.h
  10. 65 0
      include/kernel/schk.h
  11. 73 10
      init/head.S
  12. 5 0
      init/main.c
  13. 1 1
      kernel/Makefile
  14. 54 16
      kernel/graphic.c
  15. 265 11
      kernel/int.c
  16. 140 0
      kernel/irq.c
  17. 46 0
      kernel/kernel.c
  18. 46 0
      kernel/panic.c
  19. 288 7
      kernel/trap.S
  20. 9 0
      kernel/vsprintf.c

+ 1 - 1
LINES

@@ -15,4 +15,4 @@
 2021-7-04 2.164: 2164 lines, 25 files, rain
 2021-7-08 2.325: 2325 lines, 26 files, rain
 2021-7-08 2.327: 2327 lines, 26 files, rain
-
+2021-7-11 3.455: 3455 lines, 32 files, rain

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 OBJS = boot/bootsect 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o font.o 
-KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c 
+KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o kernel/irq.o kernel/panic.o font.o 
+KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c kernel/irq.c kernel/panic.c 
 
 all: Makefile
 	make -C tools/

+ 5 - 1
README.md

@@ -70,4 +70,8 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;NOTE that interrupts can also work now, but only divide-error and syscall. 
 
 2.327u-int:  
-&nbsp;&nbsp;&nbsp;&nbsp;write interrupt handlers again. 
+&nbsp;&nbsp;&nbsp;&nbsp;write interrupt handlers again.
+
+3.455u-int:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we support all 20 traps and IRQ. 
+

+ 2 - 2
boot/boot.asm

@@ -53,7 +53,7 @@ _start:
 .set_vga:
 	; get mode info
 	mov ax, 0x4f01
-	mov cx, 0x4118
+	mov cx, 0x4180
 	mov di, 0x7000
 
 	int 0x10
@@ -73,7 +73,7 @@ die:	jc die
 	mov [FB_ADDR], eax
 
 	mov ax, 0x4f02
-	mov bx, 0x4118
+	mov bx, 0x4180
 	int 0x10
 
 	jmp 0x0000:0x7e00

+ 7 - 3
include/arch/sys.h

@@ -36,9 +36,13 @@
 #define lidt(addr) __asm__ __volatile__ ("lidt %0" : : "m" (addr))
 
 
-#define pause() __asm__ __volatile__ ("pause");
-#define nop() __asm__ __volatile__ ("nop");
-#define hlt() __asm__ __volatile__ ("hlt");
+#define pause() __asm__ __volatile__ ("pause")
+#define nop() __asm__ __volatile__ ("nop")
+#define hlt() __asm__ __volatile__ ("hlt")
+
+
+#define cli() __asm__ __volatile__ ("cli")
+#define sti() __asm__ __volatile__ ("sti")
 
 
 #define getreg(regname) ({ \

+ 3 - 2
include/kernel/graphic.h

@@ -35,6 +35,7 @@ void putblock(__uint32_t, int, int, int, int);
 void putc(char, __uint32_t, __uint32_t);
 void print(__uint32_t, __uint32_t, char *);
 int printk(__uint32_t, __uint32_t, char *, ...);
+void syslog(const char *);
 
 
 /* 
@@ -85,8 +86,8 @@ extern __uint64_t font_size;
 
 /* this is default value */
 
-#define CHAR_HEIGHT 8
-#define CHAR_WIDTH  16
+#define CHAR_HEIGHT 16
+#define CHAR_WIDTH  8
 
 #define X_CHARS (VGA_X / CHAR_WIDTH)
 #define Y_LINES (VGA_Y / CHAR_HEIGHT)

+ 7 - 0
include/kernel/init.h

@@ -30,9 +30,16 @@
 #include <kernel/types.h>
 #include <kernel/errno.h>
 
+
+/* that address that used to save boot.s in linux 0.01 */
+/* but we use it to save syslog */
+#define SYSLOG_BUFFER ((char *) 0x90000)
+
+
 #define KERNEL_CS 0x0008
 #define KERNEL_DS 0x0010
 
+
 #define SEEK_SET 0x0001
 #define SEEK_CUR 0x0002
 #define SEEK_END 0x0003

+ 131 - 0
include/kernel/int.h

@@ -0,0 +1,131 @@
+/* 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 _INT_H
+#define _INT_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+
+
+/* kernel/trap.S */
+
+void divide_error(void);
+void debug(void);
+void nmi(void);
+void int3(void);
+void overflow(void);
+void bound(void);
+void invalid_code(void);
+void device_not_avaliable(void);
+void double_fault(void);
+void fpu_segment_overrun(void);
+void invalid_tss(void);
+void segment_not_present(void);
+void stack_error(void);
+void general_protection(void);
+void page_fault(void);
+void reserved(void);
+void fpu_error(void);
+void alignment(void);
+void machine(void);
+void simd(void);
+void virtualization(void);
+
+
+
+void register_irq(int, void *, void *);
+void unregister_irq(int);
+
+void do_irq(__uint64_t *, __uint64_t);
+
+void do_timer(void);
+void do_keyboard(void);
+
+
+#define IRQ_SAVE_ALL()		\
+	"pushq %rax;\n\t"	\
+	"pushq %rcx;\n\t"	\
+	"pushq %rbx;\n\t"	\
+	"pushq %rdx;\n\t"	\
+	"pushq %rbp;\n\t"	\
+	"pushq %rsi;\n\t"	\
+	"pushq %rdi;\n\t"	\
+	"pushq %r8; \n\t"	\
+	"pushq %r9; \n\t"	\
+	"pushq %r10;\n\t"	\
+	"pushq %r11;\n\t"	\
+	"pushq %r12;\n\t"	\
+	"pushq %r13;\n\t"	\
+	"pushq %r14;\n\t"	\
+	"pushq %r15;\n\t"	\
+	"movq %ds, %rax;\n\t"	\
+	"pushq %rax;    \n\t"	\
+	"movq %es, %rax;\n\t"	\
+	"pushq %rax;    \n\t"	\
+	"movq %fs, %rax;\n\t"	\
+	"pushq %rax;    \n\t"	\
+	"movq %gs, %rax;\n\t"	\
+	"pushq %rax;"		\
+	"movq $0x10, %rax;\n\t"	\
+	"movq %rax, %ds;\n\t"	\
+	"movq %rax, %es;\n\t"	\
+	"movq %rax, %fs;\n\t"	\
+	"movq %rax, %gs;\n\t"
+
+
+#define IRQ_NAMEN(nr) nr##_interrupt(void)
+#define IRQ_NAME(nr)  IRQ_NAMEN(irq##nr)
+
+
+#define build_irq(nr)				\
+	void IRQ_NAME(nr);			\
+	__asm__ (				\
+		".globl irq"#nr"_interrupt\n\t;"\
+		"irq"#nr"_interrupt:	\n\t"	\
+		IRQ_SAVE_ALL()			\
+		/* arguments for do_irq */	\
+		"movq %rsp, %rdi\n\t"		\
+		"movq $"#nr", %rsi\n\t"		\
+		"leaq ret_intr(%rip), %rax\n\t"	\
+		"pushq %rax\n\t"		\
+		"jmp do_irq\n\t"		\
+	);
+
+
+#define MAX_IRQ_NR 24
+
+
+struct irq_entry {
+	void (*handler) (void);
+};
+
+extern struct irq_entry irq_table[MAX_IRQ_NR];
+
+
+#endif
+

+ 38 - 0
include/kernel/panic.h

@@ -0,0 +1,38 @@
+/* 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 _PANIC_H
+#define _PANIC_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+
+void panic(char *);
+
+
+#endif
+

+ 65 - 0
include/kernel/schk.h

@@ -0,0 +1,65 @@
+/* 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 _SCHK_H
+#define _SCHK_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+#include <kernel/panic.h>
+
+#include <kernel/graphic.h>
+#include <arch/sys.h>
+
+
+void __stack_chk_fail(void);
+
+
+#define schk_init() 					\
+	__uint64_t old_sp = get_rsp();			\
+	syslog("  entering function ");			\
+	syslog(__FUNCTION__);				\
+	if (old_sp < 0x3000) {				\
+		syslog("#1#PANIC: SP < 0x3000\n");	\
+		__stack_chk_fail();			\
+	}						\
+	if (old_sp < 0x4000) 				\
+		syslog("#2#WARNING: SP < 0x4000 now");	\
+	else if (old_sp < 0x5000)			\
+		syslog("#3#WARNING: SP < 0x5000 now");	\
+	else if (old_sp < 0x6000)			\
+		syslog("#4#WARNING: SP < 0x6000 now");
+	
+	
+
+
+#define schk_leave()			\
+	if (old_sp != get_rsp())	\
+		__stack_chk_fail();
+
+
+#endif
+

+ 73 - 10
init/head.S

@@ -49,6 +49,7 @@
 .globl _start
 
 _start:
+	movq $0x0000000000007c00, %rsp
 	/* setup IDT */
 	lidt idt_desc(%rip)
 
@@ -116,16 +117,9 @@ init_done:
 	testq %rax, %rax
 	jnz init_panic
 
-	xorq %rax, %rax
-	xorq %rbx, %rbx
-
-	divq %rbx
-
 init_die:
-	hlt
 	jmp init_die
 
-
 init_panic:
 	WARNING(panic_msg(%rip))
 
@@ -133,15 +127,84 @@ init_panic:
 
 
 default_int_handler:
+	/* save all registers */
+	pushq %rax
+	pushq %rcx
+	pushq %rbx
+	pushq %rdx
+
+	pushq %rbp
+	pushq %rsi
+	pushq %rdi
+
+	pushq %r8
+	pushq %r9
+	pushq %r10
+	pushq %r11
+
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+
+	movq %ds, %rax
+	pushq %rax
+	
+	movq %es, %rax
+	pushq %rax
+
+	movq %fs, %rax
+	pushq %rax
+
+	movq %gs, %rax
+	pushq %rax
+
+	movq $0x10, %rax
+	movq %rax, %ds
+	movq %rax, %es
+	movq %rax, %fs
+	movq %rax, %gs
+
 	/* put a message on screen, and die */
 	WARNING(int_msg(%rip))
 
-dih_die:
-	hlt
-	jmp dih_die
+	popq %rax
+	movq %rax, %gs
+	
+	popq %rax
+	movq %rax, %fs
+
+	popq %rax
+	movq %rax, %es
 
+	popq %rax
+	movq %rax, %ds
 
+	popq %r15
+	popq %r14
+	popq %r13
+	popq %r12
 
+	popq %r11
+	popq %r10
+	popq %r9
+	popq %r8
+	
+	popq %rdi
+	popq %rsi
+	popq %rbp
+
+	popq %rdx
+	popq %rbx
+	popq %rcx
+	popq %rax
+
+
+	/* goodbye! */
+	sti
+	iretq
+	
+	
 .data
 .align 16
 

+ 5 - 0
init/main.c

@@ -29,6 +29,9 @@
 
 #include <kernel/graphic.h>
 #include <kernel/vsprintf.h>
+#include <kernel/schk.h>
+
+#include <lib/string.h>
 
 #include <arch/io.h>
 #include <arch/sys.h>
@@ -54,6 +57,8 @@ int init(void) {
 
 	printk(0x00ffffff, 0x00, "hello, can you see this? %d, and this? %q\n", 123, 0x0123456789abcdef);
 
+	sti();
+
 	return 0;
 }
 

+ 1 - 1
kernel/Makefile

@@ -1,5 +1,5 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
-all: int.o trap.o graphic.o vsprintf.o
+all: int.o trap.o graphic.o vsprintf.o irq.o panic.o 
 
 
 %.o: %.S

+ 54 - 16
kernel/graphic.c

@@ -29,6 +29,7 @@
 
 #include <kernel/graphic.h>
 #include <kernel/vsprintf.h>
+#include <kernel/schk.h>
 
 #include <lib/string.h>
 
@@ -38,16 +39,36 @@
 static char buffer[256];
 
 
+static int syslog_index = 0;
+
+
+void syslog(const char *s) {
+	/* we can't use schk in this function, */
+	/* because this will make a die-loop (schk_init calls this function) */
+	int i;
+
+	for (i = 0; s[i]; i++) {
+		if (syslog_index + i == 32768) 
+			syslog_index = 0;
+	
+		*(syslog_index + SYSLOG_BUFFER) = s[i];			
+		syslog_index++;
+	}
+
+
+	return;
+}
+
+
 int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
+	schk_init();
+
 	va_list args;
 	int i;
 
 	/* I think 256 is enough in kernel */
 	/* too long, return it. */
 
-	if (s[255]) 
-		return 0;
-
 	va_start(args, s);
 
 	/* it maybe overflow, but that's in .data section and we did check. */
@@ -59,6 +80,8 @@ int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
 
 	va_end(args);
 
+	schk_leave();
+
 	return i;
 }
 
@@ -67,10 +90,20 @@ static int x = 0, y = 0;
 
 
 void putc(char c, __uint32_t fg, __uint32_t bg) {
+	schk_init();
+
 	if (c == '\n') {
 		/* it's '\n', but we use '\r\n' */
+
+		if (y + CHAR_HEIGHT > VGA_Y) 
+			y = 0;
+
+		else 
+			y += 16;
+
 		x = 0;
-		y += 16;
+
+		/* and clear next line */
 
 		return;
 	}
@@ -80,12 +113,6 @@ void putc(char c, __uint32_t fg, __uint32_t bg) {
 		return;
 	}
 
-	if (c == ' ') {
-		x += 8;
-		return;
-	}
-
-
 	struct psf_header *psf = (struct psf_header *) &font_start;
 
 	/* 
@@ -96,6 +123,18 @@ void putc(char c, __uint32_t fg, __uint32_t bg) {
 	__uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size + 
 		(c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
 
+	/* need next line? */
+	if (x >= VGA_X) {
+		x = 0;
+		y += CHAR_HEIGHT;
+	}
+
+	/* no enough space for this character */
+	/* VGA_Y % CHAR_HEIGHT != 0, so we cannot use y == VGA_Y */
+
+	if (y + CHAR_HEIGHT > VGA_Y) 
+		y = 0;
+
 	/* okay, now we put it on the screen */
 
 	int i;
@@ -124,21 +163,20 @@ void putc(char c, __uint32_t fg, __uint32_t bg) {
 
 	x += 8;
 
-	if (x >= VGA_X) {
-		x = 0;
-		y += 16;
-
-		/* but y will > Y_LINES */
-	}
+	schk_leave();
 
 	return;
 }
 
 
 void print(__uint32_t fg, __uint32_t bg, char *s) {
+	schk_init();
+
 	for (; *s; s++) 
 		putc(*s, fg, bg);
 
+	schk_leave();
+
 	return;
 }
 

+ 265 - 11
kernel/int.c

@@ -29,20 +29,23 @@
 #include <kernel/types.h>
 
 #include <kernel/graphic.h>
+#include <kernel/int.h>
+#include <kernel/schk.h>
 
 #include <arch/x86/int.h>
 #include <arch/io.h>
 #include <arch/sys.h>
 
 
+build_irq(0x20);
+build_irq(0x21);
+
+
 int init_int(void) {
-	/* init PIC */
+	schk_init();
 
-	/* disable all irqs */
+	/* init PIC */
 
-	/* IMR: interrupt mask register */
-	outb(0xff, PIC0_IMR);
-	outb(0xff, PIC1_IMR);
 
 	/* ICW: initial control word */
 	outb(0x11, PIC0_ICW1);
@@ -55,20 +58,47 @@ int init_int(void) {
 	outb(0x02, PIC1_ICW3);
 	outb(0x01, PIC1_ICW4);
 
+	/* disable all IRQs */
 
-	/* enable all interrupts */
-
-	outb(0x00, PIC0_IMR);
-	outb(0x00, PIC1_IMR);
+	outb(0xff, PIC0_IMR);
+	outb(0xff, PIC1_IMR);
 
-	set_int_gate(idt_table + 0x00, 0, divide_error);
-	set_int_gate(idt_table + 0x80, 0, syscall);
+	set_trap_gate(idt_table + 0x00, 0, &divide_error);
+	set_trap_gate(idt_table + 0x01, 0, &debug);
+	set_trap_gate(idt_table + 0x02, 0, &nmi);
+	set_trap_gate(idt_table + 0x03, 0, &int3);
+	set_trap_gate(idt_table + 0x04, 0, &overflow);
+	set_trap_gate(idt_table + 0x05, 0, &bound);
+	set_trap_gate(idt_table + 0x06, 0, &invalid_code);
+	set_trap_gate(idt_table + 0x07, 0, &device_not_avaliable);
+	set_trap_gate(idt_table + 0x08, 0, &double_fault);
+	set_trap_gate(idt_table + 0x09, 0, &fpu_segment_overrun);
+	set_trap_gate(idt_table + 0x0a, 0, &invalid_tss);
+	set_trap_gate(idt_table + 0x0b, 0, &segment_not_present);
+	set_trap_gate(idt_table + 0x0c, 0, &stack_error);
+	set_trap_gate(idt_table + 0x0d, 0, &general_protection);
+	set_trap_gate(idt_table + 0x0e, 0, &page_fault);
+	set_trap_gate(idt_table + 0x0f, 0, &reserved);
+	set_trap_gate(idt_table + 0x10, 0, &fpu_error);
+	set_trap_gate(idt_table + 0x11, 0, &alignment);
+	set_trap_gate(idt_table + 0x12, 0, &machine);
+	set_trap_gate(idt_table + 0x13, 0, &simd);
+	set_trap_gate(idt_table + 0x14, 0, &virtualization);
+
+	set_int_gate(idt_table + 0x80, 0, &syscall);
+
+	register_irq(0x00, irq0x20_interrupt, do_timer);
+	register_irq(0x01, irq0x21_interrupt, do_keyboard);
+
+	schk_leave();
 
 	return 0;
 }
 
 
 void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
+	schk_init();
+
 #define handler ((__uint64_t) addr)
 
 	entry->selector = KERNEL_CS;
@@ -80,19 +110,28 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 	entry->off_32_64 = handler >> 32;
 	entry->zero = 0x00000000;
 
+	schk_leave();
+
 	return;
 }
 
 
 
+
 void syscall(void) {
+	schk_init();
+
 	print(0x00ff0000, 0x00000000, "int #0x80: system call. \n");
 
+	schk_leave();
+
 	return;
 }
 
 
 void die(char *s, __uint64_t *rsp, char nr) {
+	/* should we use schk here? */
+
 	printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
 	printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
 
@@ -101,8 +140,223 @@ void die(char *s, __uint64_t *rsp, char nr) {
 
 
 void do_divide_error(__uint64_t *rsp) {
+	schk_init();
+
 	die("divide error", rsp, 0);
 
+	schk_leave();
+
+	return;
+}
+
+
+void do_debug(__uint64_t *rsp) {
+	schk_init();
+
+	die("debug", rsp, 1);
+
+	schk_leave();
+
+	return;
+}
+
+void do_nmi(__uint64_t *rsp) {
+	schk_init();
+
+	die("nmi", rsp, 2);
+
+	schk_leave();
+
+	return;
+}
+
+void do_int3(__uint64_t *rsp) {
+	schk_init();
+
+	die("int3", rsp, 3);
+
+	schk_leave();
+
+	return;
+}
+
+void do_overflow(__uint64_t *rsp) {
+	schk_init();
+
+	die("overflow", rsp, 4);
+
+	schk_leave();
+
+	return;
+}
+
+void do_bound(__uint64_t *rsp) {
+	schk_init();
+
+	die("bound", rsp, 5);
+
+	schk_leave();
+
+	return;
+}
+
+void do_ud(__uint64_t *rsp) {
+	schk_init();
+
+	die("undefined instruction", rsp, 6);
+
+	schk_leave();
+
+	return;
+}
+
+void do_no_fpu(__uint64_t *rsp) {
+	schk_init();
+
+	die("fpu not present", rsp, 7);
+
+	schk_leave();
+
+	return;
+}
+
+void do_double_fault(__uint64_t *rsp) {
+	schk_init();
+
+	die("double fault", rsp, 8);
+
+	schk_leave();
+
+	return;
+}
+
+void do_fpu_overrun(__uint64_t *rsp) {
+	schk_init();
+
+	die("fpu overrun", rsp, 9);
+
+	schk_leave();
+
+	return;
+}
+
+void do_invalid_tss(__uint64_t *rsp) {
+	schk_init();
+
+	die("invalid tss", rsp, 10);
+
+	schk_leave();
+
+	return;
+}
+
+void do_no_segment(__uint64_t *rsp) {
+	schk_init();
+
+	die("no such segment", rsp, 11);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_stack(__uint64_t *rsp) {
+	schk_init();
+
+	die("stack error", rsp, 12);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_gp(__uint64_t *rsp) {
+	schk_init();
+
+	die("general protection", rsp, 13);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_page_fault(__uint64_t *rsp) {
+	schk_init();
+
+	die("page fault", rsp, 14);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_reserved(__uint64_t *rsp) {
+	schk_init();
+
+	die("reserved", rsp, 15);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_fpu_error(__uint64_t *rsp) {
+	schk_init();
+
+	die("fpu error", rsp, 16);
+
+	schk_leave();
+
 	return;
 }
 
+
+void do_alignment(__uint64_t *rsp) {
+	schk_init();
+
+	die("alignment error", rsp, 17);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_machine(__uint64_t *rsp) {
+	schk_init();
+
+	die("machine check", rsp, 18);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_simd(__uint64_t *rsp) {
+	schk_init();
+
+	die("simd error", rsp, 19);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_virtualization(__uint64_t *rsp) {
+	schk_init();
+
+	die("virtualization error", rsp, 20);
+
+	schk_leave();
+
+	return;
+}
+
+

+ 140 - 0
kernel/irq.c

@@ -0,0 +1,140 @@
+/* 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/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/graphic.h>
+#include <kernel/int.h>
+#include <kernel/schk.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+
+struct irq_entry irq_table[MAX_IRQ_NR];
+
+
+void register_irq(int nr, void *entry, void *handler) {
+	schk_init();
+
+	__uint8_t mask;
+	
+	if (nr < 8) {
+		mask = inb(0x21) & ~(1 << nr);
+		outb(mask, 0x21);
+	}
+
+	else {
+		mask = inb(0xa1) & ~(1 << (nr-8));
+		outb(mask, 0xa1);
+	}
+
+
+	irq_table[nr].handler = handler;
+
+	/* IRQ nr starts at 0x20 */
+	set_int_gate(idt_table + 0x20 + nr, 0, entry);
+
+	schk_leave();
+
+	return;
+}
+
+void unregister_irq(int nr) {
+	schk_init();
+
+	__uint8_t mask;
+
+	irq_table[nr].handler = NULL;
+
+	if (nr < 8) {
+		mask = inb(0x21) | 1 << nr;
+		outb(mask, 0x21);
+	}
+
+	else {
+		mask = inb(0xa1) | 1 << (nr - 8);
+		outb(mask, 0xa1);
+	}
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_irq(__uint64_t *rsp, __uint64_t nr) {
+	schk_init();
+
+	printk(0x00ff0000, 0x00000000, "do_irq #%b\n", nr);
+
+	void (*fn) (void) = irq_table[nr - 0x20].handler;
+
+	if (fn) 
+		fn();
+
+	else 
+		printk(0x00ff0000, 0x00000000, "unknow irq %x\n", nr - 0x20);
+		
+	outb(0x20, 0x20);
+
+	schk_leave();
+
+	return;
+}
+
+
+void do_timer(void) {
+	schk_init();
+
+	printk(0x00ff0000, 0x00000000, "irq 0x20: PIT timer. \n");
+
+	schk_leave();
+
+	return;
+}
+
+
+#define KEYBOARD_DATA 0x0060
+
+void do_keyboard(void) {
+	schk_init();
+
+	printk(0x00ff0000, 0x00000000, "irq 0x21: keyboard interrupt. \n");
+
+	/* get keyboard scan code */
+
+	char code = inb(KEYBOARD_DATA);
+
+	printk(0x00ff0000, 0x00000000, "key code: %b\n", code & 0xff);
+
+	schk_leave();
+
+	return;
+}

+ 46 - 0
kernel/kernel.c

@@ -0,0 +1,46 @@
+/* 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/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/graphic.h>
+#include <kernel/int.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+
+
+void panic(char *s) {
+	printk(0x00ff0000, 0x00000000, "kernel panic: %s\n", s);
+	printk(0x00ff0000, 0x00000000, "for more debug information, see syslog 0x90000\n");
+
+	for (;;) hlt();
+}
+

+ 46 - 0
kernel/panic.c

@@ -0,0 +1,46 @@
+/* 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/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/graphic.h>
+#include <kernel/int.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+
+
+void panic(char *s) {
+	printk(0x00ff0000, 0x00000000, "kernel panic: %s\n", s);
+	printk(0x00ff0000, 0x00000000, "for more debug information, see syslog 0x90000\n");
+
+	for (;;) hlt();
+}
+

+ 288 - 7
kernel/trap.S

@@ -27,7 +27,9 @@
 .globl __stack_chk_fail
 
 __stack_chk_fail:
+	/* panic and do stack trace */
 	movq schkfail_msg(%rip), %rdi
+	call panic
 
 scf_die:
 	hlt
@@ -71,9 +73,6 @@ no_error_code:
 	movq %gs, %rcx
 	pushq %rcx
 
-	/* that fake error code */
-	pushq $0
-
 	/* KERNEL_DS */
 	movq $0x10, %rcx
 	movq %ds, %rcx
@@ -89,9 +88,6 @@ no_error_code:
 restore_all:
 	/* now we restore and return */
 
-	/* ignore that "error code" */
-	addq $8, %rsp
-
 	popq %rax
 	movq %gs, %rax
 
@@ -122,7 +118,292 @@ restore_all:
 
 	popq %rax
 
-	/* then say goodbye to  */
+	/* then say goodbye to this interrupt */
+	iretq
+
+.globl debug
+
+debug:
+	pushq %rax
+	leaq do_debug(%rip), %rax
+
+	jmp no_error_code
+
+.globl nmi
+
+nmi:
+	pushq %rax
+	leaq do_nmi(%rip), %rax
+
+	jmp no_error_code
+
+.globl int3
+
+int3:
+	pushq %rax
+	leaq do_int3(%rip), %rax
+
+	jmp no_error_code
+
+.globl overflow
+
+overflow:
+	pushq %rax
+	leaq do_overflow(%rip), %rax
+
+	jmp no_error_code
+
+.globl bound
+
+bound:
+	pushq %rax
+	leaq do_bound(%rip), %rax
+
+	jmp no_error_code
+
+.globl invalid_code
+
+invalid_code:
+	pushq %rax
+	leaq do_ud(%rip), %rax
+
+	jmp no_error_code
+
+.globl device_not_avaliable
+
+device_not_avaliable:
+	pushq %rax
+	leaq do_no_fpu(%rip), %rax
+
+	jmp no_error_code
+
+.globl double_fault
+
+double_fault:
+	pushq %rax
+	leaq do_double_fault(%rip), %rax
+
+
+error_code:
+
+	/* save registers, and get that error-code */
+	/* now error-code is on the top of stack */
+
+	pushq %rcx
+	pushq %rbx
+	pushq %rdx
+
+	pushq %rbp
+	pushq %rsi
+	pushq %rdi
+
+	pushq %r8
+	pushq %r9
+	pushq %r10
+	pushq %r11
+
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+
+	movq %ds, %rcx
+	pushq %rcx
+
+	movq %es, %rcx
+	pushq %rcx
+
+	movq %fs, %rcx
+	pushq %rcx
+
+	movq %gs, %rcx
+	pushq %rcx
+
+	movq $0x10, %rcx
+	movq %rcx, %ds
+	movq %rcx, %es
+	movq %rcx, %fs
+	movq %rcx, %gs
+
+	/* error code is the second argument */
+
+	movq %rsp, %rdi
+
+	call *%rax
+
+	/* restore and return */
+	popq %rax
+	movq %rax, %gs
+
+	popq %rax
+	movq %rax, %fs
+
+	popq %rax
+	movq %rax, %es
+
+	popq %rax
+	movq %rax, %ds
+
+	popq %r15
+	popq %r14
+	popq %r13
+	popq %r12
+
+	popq %r11
+	popq %r10
+	popq %r9
+	popq %r8
+
+	popq %rdi
+	popq %rsi
+	popq %rbp
+
+	popq %rdx
+	popq %rbx
+	popq %rcx
+	popq %rax
+
+	iretq
+
+.globl fpu_segment_overrun
+
+fpu_segment_overrun:
+	pushq %rax
+	leaq do_fpu_overrun(%rip), %rax
+
+	jmp no_error_code
+
+.globl invalid_tss
+
+invalid_tss:
+	pushq %rax
+	leaq do_invalid_tss(%rip), %rax
+	
+	jmp error_code
+
+.globl segment_not_present
+
+segment_not_present:
+	pushq %rax
+	leaq do_no_segment(%rip), %rax
+
+	jmp error_code
+
+
+.globl stack_error
+
+stack_error:
+	pushq %rax
+	leaq do_stack(%rip), %rax
+
+	jmp error_code
+
+
+.globl general_protection
+
+general_protection:
+	pushq %rax
+	leaq do_gp(%rip), %rax
+
+	jmp error_code
+
+.globl page_fault
+
+page_fault:
+	pushq %rax
+	leaq do_page_fault(%rip), %rax
+
+	jmp error_code
+
+.globl reserved
+
+reserved:
+	pushq %rax
+	leaq do_reserved(%rip), %rax
+
+	jmp no_error_code
+
+.globl fpu_error
+
+fpu_error:
+	pushq %rax
+	leaq do_fpu_error(%rip), %rax
+
+	jmp no_error_code
+
+.globl alignment
+
+alignment:
+	pushq %rax
+	leaq do_alignment(%rip), %rax
+
+	jmp error_code
+
+.globl machine
+
+machine:
+	pushq %rax
+	leaq do_machine(%rip), %rax
+
+	jmp no_error_code
+
+.globl simd
+
+simd:
+	pushq %rax
+	leaq do_simd(%rip), %rax
+
+	jmp no_error_code
+
+.globl virtualization
+
+virtualization:
+	pushq %rax
+	leaq do_virtualization(%rip), %rax
+
+	jmp no_error_code
+
+
+.globl ret_intr
+
+ret_intr:
+	/* this is different with restore_all, but they do same things. */
+	/* but interrupts haven't got an error-code */
+
+	popq %rax
+	movq %rax, %gs
+
+	popq %rax
+	movq %rax, %fs
+
+	popq %rax
+	movq %rax, %es
+
+	popq %rax
+	movq %rax, %ds
+
+	popq %r15
+	popq %r14
+	popq %r13
+	popq %r12
+
+	popq %r11
+	popq %r10
+	popq %r9
+	popq %r8
+
+	popq %rdi
+	popq %rsi
+	popq %rbp
+
+	popq %rdx
+	popq %rbx
+	popq %rcx
+	popq %rax
+
+	sti
+
+	iretq
 
 
 .section .rodata

+ 9 - 0
kernel/vsprintf.c

@@ -30,6 +30,7 @@
 
 #include <kernel/graphic.h>
 #include <kernel/vsprintf.h>
+#include <kernel/schk.h>
 
 #include <arch/x86/int.h>
 #include <arch/io.h>
@@ -63,6 +64,8 @@
  * */
 
 int vsprintf(char *buf, char *fmt, va_list args) {
+	schk_init();
+
 	/* ri: real index in buf */
 	__uint64_t i, ri;
 	char state, c;
@@ -132,12 +135,16 @@ int vsprintf(char *buf, char *fmt, va_list args) {
 		}
 	}
 
+	schk_leave();
+
 	return ri;
 }
 
 
 
 int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
+	schk_init();
+
 	static char digits[] = "0123456789abcdef";
 
 	char tbuf[32];
@@ -178,5 +185,7 @@ int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
 	while (--j >= 0) 
 		buffer[i - j - 1] = tbuf[j];
 
+	schk_leave();
+
 	return i;
 }