Kaynağa Gözat

2.327u-int: write interrupt handlers again

rain 5 yıl önce
ebeveyn
işleme
8738793c62
6 değiştirilmiş dosya ile 90 ekleme ve 85 silme
  1. 1 0
      LINES
  2. 2 0
      README.md
  3. 4 1
      init/head.S
  4. 7 2
      kernel/graphic.c
  5. 3 3
      kernel/int.c
  6. 73 79
      kernel/trap.S

+ 1 - 0
LINES

@@ -14,4 +14,5 @@
 2021-7-04 2.154: 2154 lines, 25 files, rain
 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
 

+ 2 - 0
README.md

@@ -69,3 +69,5 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 &nbsp;&nbsp;&nbsp;&nbsp;now we can use 1024x768 VBE and vsprintf. this is a big change. 
 &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. 

+ 4 - 1
init/head.S

@@ -116,7 +116,10 @@ init_done:
 	testq %rax, %rax
 	jnz init_panic
 
-	int $0x80
+	xorq %rax, %rax
+	xorq %rbx, %rbx
+
+	divq %rbx
 
 init_die:
 	hlt

+ 7 - 2
kernel/graphic.c

@@ -30,9 +30,14 @@
 #include <kernel/graphic.h>
 #include <kernel/vsprintf.h>
 
+#include <lib/string.h>
+
 #include <stdarg.h>
 
 
+static char buffer[256];
+
+
 int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
 	va_list args;
 	int i;
@@ -43,14 +48,14 @@ int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
 	if (s[255]) 
 		return 0;
 
-	static char buffer[256] = {0};
-
 	va_start(args, s);
 
 	/* it maybe overflow, but that's in .data section and we did check. */
 
 	i = vsprintf(buffer, s, args);
 	print(fg, bg, buffer);
+	
+	memset(buffer, 0, 256);
 
 	va_end(args);
 

+ 3 - 3
kernel/int.c

@@ -86,14 +86,15 @@ void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
 
 
 void syscall(void) {
-	print(0x00ff0000, 0x00, "int #0x80: system call. \n");
+	print(0x00ff0000, 0x00000000, "int #0x80: system call. \n");
 
 	return;
 }
 
 
 void die(char *s, __uint64_t *rsp, char nr) {
-	print(0x00ff0000, 0x00, "int #%b: %s. \n");
+	printk(0x00ff0000, 0x00000000, "int #%b: %s. \n", nr, s);
+	printk(0x00ff0000, 0x00000000, "rsp: %q\n", rsp);
 
 	for (;;);
 }
@@ -105,4 +106,3 @@ void do_divide_error(__uint64_t *rsp) {
 	return;
 }
 
-

+ 73 - 79
kernel/trap.S

@@ -24,112 +24,106 @@
 
 
 
-#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;	\
+.globl __stack_chk_fail
+
+__stack_chk_fail:
+	movq schkfail_msg(%rip), %rdi
+
+scf_die:
+	hlt
+	jmp scf_die
+
+
+.globl divide_error
+
+divide_error:
 	pushq %rax
+	leaq do_divide_error(%rip), %rax
+
+no_error_code:
+	/* and save other registers seriously now */
+	pushq %rcx
+	pushq %rdx
+	pushq %rbp
+	pushq %rsi
+	pushq %rdi
 	
-	
+	pushq %r8
+	pushq %r9
+	pushq %r10
+	pushq %r11
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+
+	/* segments also */
+
+	movq %ds, %rcx
+	pushq %rcx
+
+	movq %es, %rcx
+	pushq %rcx
+
+	movq %fs, %rcx
+	pushq %rcx
+
+	movq %gs, %rcx
+	pushq %rcx
+
+	/* that fake error code */
+	pushq $0
+
+	/* KERNEL_DS */
+	movq $0x10, %rcx
+	movq %ds, %rcx
+	movq %es, %rcx
+	movq %fs, %rcx
+	movq %gs, %rcx
+
+	/* call that interrupt handler */
+
+	callq *%rax
+
 
 restore_all:
-	popq %rax
-	movq %rax, %ss
+	/* now we restore and return */
+
+	/* ignore that "error code" */
+	addq $8, %rsp
 
 	popq %rax
-	movq %rax, %gs
+	movq %gs, %rax
 
 	popq %rax
-	movq %rax, %fs
-	
+	movq %fs, %rax
+
 	popq %rax
-	movq %rax, %es
-	
+	movq %es, %rax
+
 	popq %rax
-	movq %rax, %ds
+	movq %ds, %rax
 
 	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 __stack_chk_fail
-
-__stack_chk_fail:
-	movq schkfail_msg(%rip), %rdi
-
-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
-
-	SAVE_REGS();
-
-	/* first argument for do_divide_error */
-	movq %rsp, %rdi
-	
-	leaq do_divide_error(%rip), %rcx
-	callq *%rcx
-
-	/* return */
+	popq %rax
 
-	sti
+	/* then say goodbye to  */
 
-	jmp restore_all
-	
 
 .section .rodata