From cfcb02e713f9b0bbfa268fdcc34ba0aae820e12b Mon Sep 17 00:00:00 2001 From: hinus Date: Mon, 14 Jun 2021 23:21:29 +0800 Subject: [PATCH] Title: Setup Interruption Descrition Table Issue: https://gitee.com/hinus/linux_kernel_011/issues/I3V4FC Description: macro 'set_intr_gate' to setup IDT. --- include/asm/system.h | 15 +++++++++++++++ include/linux/head.h | 11 +++++++++++ include/linux/sched.h | 3 +++ kernel/Makefile | 5 ++++- kernel/main.c | 5 ++++- kernel/sched.c | 10 ++++++++++ kernel/sys_call.S | 30 ++++++++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 include/linux/head.h create mode 100644 kernel/sys_call.S diff --git a/include/asm/system.h b/include/asm/system.h index 5090d22..001b687 100644 --- a/include/asm/system.h +++ b/include/asm/system.h @@ -6,4 +6,19 @@ #define nop() __asm__("nop"::) #define iret() __asm__("iret"::) +#define _set_gate(gate_addr, type, dpl, addr) \ +__asm__("movw %%dx, %%ax\n\t" \ + "movw %0, %%dx\n\t" \ + "movl %%eax, %1\n\t" \ + "movl %%edx, %2" \ + : \ + :"i"((short)(0x8000 + (dpl << 13) + (type << 8))), \ + "o"(*((char*)(gate_addr))), \ + "o"(*(4 + (char*)(gate_addr))), \ + "d"((char*)(addr)), "a" (0x00080000)) + + +#define set_intr_gate(n, addr) \ + _set_gate(&idt[n], 14, 0, addr) + #endif diff --git a/include/linux/head.h b/include/linux/head.h new file mode 100644 index 0000000..7bf5f67 --- /dev/null +++ b/include/linux/head.h @@ -0,0 +1,11 @@ +#ifndef _HEAD_H +#define _HEAD_H + +typedef struct desc_struct { + unsigned long a, b; +} desc_table[256]; + +extern unsigned long pg_dir[1024]; +extern desc_table idt,gdt; + +#endif diff --git a/include/linux/sched.h b/include/linux/sched.h index 79cbdbd..e7f1a39 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1,6 +1,9 @@ #ifndef _SCHED_H #define _SCHED_H +#include #include +void sched_init(); + #endif diff --git a/kernel/Makefile b/kernel/Makefile index caaa841..f8f04ec 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,7 +2,7 @@ GCC := gcc CCFLAG := -I../include -nostdinc -Wall -fomit-frame-pointer -fno-stack-protector -c -m32 LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386 INCDIR := ../include -OBJS := head.o main.o sched.o printk.o vsprintf.o chr_drv/chr_drv.a mm/mm.o +OBJS := head.o main.o sys_call.o sched.o printk.o vsprintf.o chr_drv/chr_drv.a mm/mm.o system: $(OBJS) $(LD) $(LDFLAG) -e startup_32 -o $@ $^ @@ -10,6 +10,9 @@ system: $(OBJS) head.o : head.S $(GCC) -m32 -traditional -c -o $@ $< +sys_call.o : sys_call.S + $(GCC) -m32 -traditional -c -o $@ $< + main.o : main.c $(GCC) $(CCFLAG) -o $@ $< diff --git a/kernel/main.c b/kernel/main.c index 22038da..c563dcc 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -2,6 +2,7 @@ #include #include +#include extern void mem_init(long start, long end); @@ -36,11 +37,13 @@ void main(void) main_memory_start = buffer_memory_end; mem_init(main_memory_start, memory_end); + sched_init(); tty_init(); - printk("memory start: %d, end: %d", main_memory_start, memory_end); + printk("memory start: %d, end: %d\n\r" , main_memory_start, memory_end); __asm__ __volatile__( + "int $0x80\n\r" "loop:\n\r" "jmp loop" ::); diff --git a/kernel/sched.c b/kernel/sched.c index 97a9e99..7be08e4 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -1,5 +1,11 @@ +#include + +#include + #define PAGE_SIZE 4096 +extern int system_call(); + long user_stack[PAGE_SIZE >> 2]; struct @@ -8,3 +14,7 @@ struct short b; } stack_start = {&user_stack[PAGE_SIZE >> 2], 0x10}; +void sched_init() { + set_intr_gate(0x80, &system_call); +} + diff --git a/kernel/sys_call.S b/kernel/sys_call.S new file mode 100644 index 0000000..a71dab3 --- /dev/null +++ b/kernel/sys_call.S @@ -0,0 +1,30 @@ +.code32 +.text +.globl system_call +int_msg: + .asciz "In kernel interrupt" + +system_call: + /* we do not have function _printk now, so trick it */ + pushl %eax + pushl %ecx + pushl %edx + pushw %ds + pushw %es + pushw %fs + movl $0x10, %eax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + /* call _printk */ + pushl $int_msg + call printk + popl %eax + popw %fs + popw %es + popw %ds + popl %edx + popl %ecx + popl %eax + iret + -- Gitee