ソースを参照

1.854u-int: initial idt_table[0x80]

rain 5 年 前
親
コミット
694ae8de54
10 ファイル変更、170 行追加、12 行削除
  1. 1 0
      LINES
  2. 2 2
      Makefile
  3. 4 0
      README.md
  4. 56 0
      include/arch/x86/int.h
  5. 2 0
      include/kernel/errno.h
  6. 3 8
      init/head.S
  7. 4 0
      init/main.c
  8. 1 1
      kernel.lds
  9. 6 1
      kernel/Makefile
  10. 91 0
      kernel/int.c

+ 1 - 0
LINES

@@ -6,4 +6,5 @@
 2021-5-30 1.708: 1611 lines, 18 files, rain
 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
 

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 OBJS = boot/bootsect 
-KOBJS = init/head.o init/main.o modules/tty.o kernel/tty.o
-KSRCS = init/head.S init/main.c modules/tty.c kernel/tty.S
+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
 
 all: Makefile
 	make -C boot/

+ 4 - 0
README.md

@@ -39,3 +39,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 
 1.706u-int:  
 &nbsp;&nbsp;&nbsp;&nbsp;initial IDT in `head.S`, it set all handlers at `defualt_int_handler`
+
+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. 
+

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

@@ -0,0 +1,56 @@
+/* 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_INT_H
+#define INCLUDED_INT_H
+
+
+
+struct idt_entry {
+	__uint16_t off_0_16;
+	__uint16_t selector;
+	__uint8_t ist;
+	/* attr: P + DPL + TYPE */
+	__uint8_t attr;
+	__uint16_t off_16_32;
+
+	__uint32_t off_32_64;
+	__uint32_t zero;
+} __attribute__ ((packed));
+
+
+/* 16B per entry, 256 entries */
+extern struct idt_entry idt_table[];
+
+int init_int(void);
+void set_gate(struct idt_entry *, char, char, void *);
+void syscall(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)
+
+#endif

+ 2 - 0
include/kernel/errno.h

@@ -27,6 +27,8 @@
 #ifndef INCLUDED_ERRNO_H
 #define INCLUDED_ERRNO_H
 
+#include <kernel/types.h>
+
 typedef __uint32_t errno_t;
 
 

+ 3 - 8
init/head.S

@@ -69,6 +69,7 @@ rep_set_idt:
 	dec %rcx
 	jnz rep_set_idt
 
+to_init:
 
 	/* we cannot use far-jmp in long mode. */
 
@@ -81,19 +82,12 @@ rep_set_idt:
 
 	/* 64 bit code */
 
-	/* virtual base address */
-	movq $0xffff800000000000, %rbp
-
 	/* return address */
 	leaq init_done(%rip), %rax
-
-	addq %rbp, %rax
 	pushq %rax
 
 	/* address will jump */
 	leaq init(%rip), %rax
-	addq %rbp, %rax
-	
 	pushq %rax
 
 	retq
@@ -102,9 +96,10 @@ rep_set_idt:
 init_done:
 	/* andq %rax, %rax, but %rax won't change. */
 	testq %rax, %rax
-	
 	jnz init_panic
 
+	int $0x80
+
 init_die:
 	hlt
 	jmp init_die

+ 4 - 0
init/main.c

@@ -28,8 +28,10 @@
 #include <kernel/types.h>
 
 #include <modules/tty.h>
+
 #include <arch/io.h>
 #include <arch/sys.h>
+#include <arch/x86/int.h>
 
 
 /* 
@@ -42,6 +44,8 @@
 int init(void) {
 	printf("XNIX kernel init\n");
 
+	init_int();
+
 	return 0;
 }
 

+ 1 - 1
kernel.lds

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

+ 6 - 1
kernel/Makefile

@@ -1,9 +1,14 @@
-all: tty.o 
+CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror 
+all: tty.o int.o
+
 
 %.o: %.S
 	gcc -E $*.S -I ../include > $*.s
 	as --64 $*.s -o $*.o 
 
+%.o: %.c
+	gcc -c $*.c -o $*.o $(CFLAGS)
+
 clear:
 	rm *.o *.s
 

+ 91 - 0
kernel/int.c

@@ -0,0 +1,91 @@
+/* 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 <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+#include <modules/tty.h>
+
+
+int init_int(void) {
+	/* init PIC */
+
+	/* disable all irqs */
+
+	/* IMR: interrupt mask register */
+//	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, 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);
+
+	set_int_gate(idt_table + 0x80, 0, syscall);
+
+	return 0;
+}
+
+
+void set_gate(struct idt_entry *entry, char attr, char ist, void *addr) {
+#define handler ((__uint64_t) addr)
+
+	entry->selector = KERNEL_CS;
+	entry->ist = ist;
+	entry->attr = attr;
+
+	entry->off_0_16 = handler & 0xffff;
+	entry->off_16_32 = handler >> 16;
+	entry->off_32_64 = handler >> 32;
+	entry->zero = 0x00000000;
+
+	return;
+}
+
+
+
+void syscall(void) {
+	printf("int #0x80: system call. \n");
+	for (;;);
+}
+
+