Quellcode durchsuchen

2.015u-traps: add handler for interrupt #0x00 divide error

rain vor 5 Jahren
Ursprung
Commit
ac3de23fcd
9 geänderte Dateien mit 183 neuen und 16 gelöschten Zeilen
  1. 2 0
      LINES
  2. 2 2
      Makefile
  3. 4 0
      README.md
  4. 26 0
      include/arch/x86/int.h
  5. 1 1
      init/Makefile
  6. 8 0
      init/main.c
  7. 1 1
      kernel/Makefile
  8. 19 12
      kernel/int.c
  9. 120 0
      kernel/trap.S

+ 2 - 0
LINES

@@ -7,4 +7,6 @@
 2021-5-30 1.640: 1640 lines, 18 files, rain
 2021-5-30 1.706: 1706 lines, 18 files, rain
 2021-6-02 1.854: 1854 lines, 20 files, rain
+2021-6-02 2.015: 2015 lines, 21 files, rain
+
 

+ 2 - 2
Makefile

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

+ 4 - 0
README.md

@@ -43,3 +43,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 1.854u-int:  
 &nbsp;&nbsp;&nbsp;&nbsp;new `int.c` intial `idt_table[0x80]`, and set handler to `syscall`. it put a message and die. 
 
+2.015u-traps:  
+&nbsp;&nbsp;&nbsp;&nbsp;add handler for divide-error (always divide by zero). `do_divide_error` write in C, it pucs a message and return. 
+&nbsp;&nbsp;&nbsp;&nbsp;`divide_error` is an assembly function. it saves all registers, and call `do_divide_error`. then it recover all registers and iretq. 
+

+ 26 - 0
include/arch/x86/int.h

@@ -48,9 +48,35 @@ int init_int(void);
 void set_gate(struct idt_entry *, char, char, void *);
 void syscall(void);
 
+/* functions in int.S */
+void divide_error(void);
+
 
 #define set_int_gate(nr, ist, handler)	set_gate(nr, 0x8e, ist, handler)
 #define set_trap_gate(nr, ist, handler)	set_gate(nr, 0x8f, ist, handler)
 #define set_sys_gate(nr, ist, handler)	set_gate(nr, 0xef, ist, handler)
 
+
+/* control word */
+#define PIC0_ICW1 0x0020
+#define PIC0_OCW2 0x0020
+
+/* interrupt mask register */
+#define PIC0_IMR  0x0021
+
+#define PIC0_ICW2 0X0021
+#define PIC0_ICW3 0X0021
+#define PIC0_ICW4 0X0021
+
+#define PIC1_ICW1 0x00a0
+#define PIC1_OCW2 0x00a0
+
+
+#define PIC1_IMR  0x00a1
+#define PIC1_ICW2 0x00a1
+#define PIC1_ICW3 0x00a1
+#define PIC1_ICW4 0x00a1
+
+
+
 #endif

+ 1 - 1
init/Makefile

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

+ 8 - 0
init/main.c

@@ -46,6 +46,14 @@ int init(void) {
 
 	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/Makefile

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

+ 19 - 12
kernel/int.c

@@ -40,26 +40,27 @@ int init_int(void) {
 	/* disable all irqs */
 
 	/* IMR: interrupt mask register */
-//	outb(0xff, PIC0_IMR);
-//	outb(0xff, PIC1_IMR);
+	outb(0xff, PIC0_IMR);
+	outb(0xff, PIC1_IMR);
 
 	/* ICW: initial control word */
-//	outb(0x11, PIC0_ICW1);
-//	outb(0x20, PIC0_ICW2);
-//	outb(0x04, PIC0_ICW3);
-//	outb(0x01, PIC0_ICW4);
+	outb(0x11, PIC0_ICW1);
+	outb(0x20, PIC0_ICW2);
+	outb(0x04, PIC0_ICW3);
+	outb(0x01, PIC0_ICW4);
 
-//	outb(0x11, PIC1_ICW1);
-//	outb(0x28, PIC1_ICW2);
-//	outb(0x02, PIC1_ICW3);
-//	outb(0x01, PIC1_ICW4);
+	outb(0x11, PIC1_ICW1);
+	outb(0x28, PIC1_ICW2);
+	outb(0x02, PIC1_ICW3);
+	outb(0x01, PIC1_ICW4);
 
 
 	/* enable all interrupts */
 
-//	outb(0x00, PIC0_IMR);
-//	outb(0x00, PIC1_IMR);
+	outb(0x00, PIC0_IMR);
+	outb(0x00, PIC1_IMR);
 
+	set_int_gate(idt_table + 0x00, 0, divide_error);
 	set_int_gate(idt_table + 0x80, 0, syscall);
 
 	return 0;
@@ -89,3 +90,9 @@ void syscall(void) {
 }
 
 
+void do_divide_error(__uint64_t *rsp) {
+	printf("int #0x00: divide error. \n");
+	printf("RSP: %q\n", rsp);
+
+	return;
+}

+ 120 - 0
kernel/trap.S

@@ -0,0 +1,120 @@
+/* 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/>.  
+*/
+
+
+
+
+#define SAVE_REGS() 	\
+	pushq %rax; 	\
+	pushq %rcx;	\
+	pushq %rdx;	\
+	pushq %rbx;	\
+	/* we shouldn't push it because it's really in stack */	\
+	pushq %rsp;	\
+	pushq %rbp;	\
+	pushq %rsi;	\
+	pushq %rdi;	\
+	pushq %r8;	\
+	pushq %r9;	\
+	pushq %r10;	\
+	pushq %r11;	\
+	pushq %r12;	\
+	pushq %r13;	\
+	pushq %r14;	\
+	pushq %r15;	\
+	/* and, segment registers */ \
+	movq %ds, %rax;	\
+	pushq %rax;	\
+	movq %es, %rax;	\
+	pushq %rax;	\
+	movq %fs, %rax;	\
+	pushq %rax;	\
+	movq %gs, %rax;	\
+	pushq %rax;	\
+	movq %ss, %rax;	\
+	pushq %rax
+	
+	
+
+restore_all:
+	popq %rax
+	movq %rax, %ss
+
+	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 %rsp
+	popq %rbx
+	popq %rdx
+	popq %rcx
+	popq %rax
+
+	/* add it because we don't want errcode or func */
+	addq $0x10, %rsp
+
+	/* iretq will pop ss if it need */
+	iretq
+
+
+.globl divide_error
+
+divide_error:
+	/* error code: 0 */
+	pushq $0
+	pushq %rax
+
+	SAVE_REGS();
+
+	/* first argument for do_divide_error */
+	movq %rsp, %rdi
+	
+	leaq do_divide_error(%rip), %rcx
+	callq *%rcx
+
+	/* return */
+
+die:	hlt
+	jmp die	
+
+	jmp restore_all
+