From 154a2c57911b30b410c25dc3f0cf81b0c51966a3 Mon Sep 17 00:00:00 2001 From: Yourtion Date: Wed, 4 May 2016 18:11:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=BC=82=E5=B8=B8=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 21_day/bootpack.h | 5 +++- 21_day/console.c | 7 +++++ 21_day/dsctbl.c | 1 + 21_day/naskfunc.nas | 67 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/21_day/bootpack.h b/21_day/bootpack.h index a1c20ad..b5ae31d 100644 --- a/21_day/bootpack.h +++ b/21_day/bootpack.h @@ -24,14 +24,16 @@ void load_idtr(int limit, int addr); int load_cr0(void); void store_cr0(int cr0); void load_tr(int tr); +void asm_inthandler0d(void); void asm_inthandler20(void); void asm_inthandler21(void); void asm_inthandler27(void); void asm_inthandler2c(void); unsigned int memtest_sub(unsigned int start, unsigned int end); void farjmp(int eip, int cs); -void start_app(int eip, int cs, int esp, int ds); +void farcall(int eip, int cs); void asm_hrb_api(void); +void start_app(int eip, int cs, int esp, int ds); /* fifo.c */ struct FIFO32 { @@ -246,6 +248,7 @@ void cmd_dir(struct CONSOLE *cons); void cmd_type(struct CONSOLE *cons, int *fat, char *cmdline); int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline); void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax); +int inthandler0d(int *esp); /* file.c */ struct FILEINFO { diff --git a/21_day/console.c b/21_day/console.c index 7cd6a79..1cf569c 100644 --- a/21_day/console.c +++ b/21_day/console.c @@ -320,3 +320,10 @@ void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int } return; } + +int inthandler0d(int *esp) +{ + struct CONSOLE *cons = (struct CONSOLE *) *((int *) 0x0fec); + cons_putstr0(cons, "¥nINT 0D :¥n General Protected Exception.¥n"); + return 1; /*强制结束程序*/ +} diff --git a/21_day/dsctbl.c b/21_day/dsctbl.c index 1887cde..e5d7a40 100644 --- a/21_day/dsctbl.c +++ b/21_day/dsctbl.c @@ -23,6 +23,7 @@ void init_gdtidt(void) load_idtr(LIMIT_IDT, ADR_IDT); /* IDT设置*/ + set_gatedesc(idt + 0x0d, (int) asm_inthandler0d, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x20, (int) asm_inthandler20, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32); set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, AR_INTGATE32); diff --git a/21_day/naskfunc.nas b/21_day/naskfunc.nas index a3aecf1..cc59a61 100644 --- a/21_day/naskfunc.nas +++ b/21_day/naskfunc.nas @@ -15,11 +15,13 @@ GLOBAL _load_tr GLOBAL _asm_inthandler20, _asm_inthandler21 GLOBAL _asm_inthandler27, _asm_inthandler2c + GLOBAL _asm_inthandler0d GLOBAL _memtest_sub - GLOBAL _farjmp, _start_app - GLOBAL _asm_hrb_api + GLOBAL _farjmp, _farcall + GLOBAL _asm_hrb_api, _start_app EXTERN _inthandler20, _inthandler21 EXTERN _inthandler27, _inthandler2c + EXTERN _inthandler0d EXTERN _hrb_api [SECTION .text] @@ -273,6 +275,67 @@ _asm_inthandler2c: POP ES IRETD +_asm_inthandler0d: + STI + PUSH ES + PUSH DS + PUSHAD + MOV AX,SS + CMP AX,1*8 + JNE .from_app +; 当操作系统活动时产生中断的情况和之前差不多 + MOV EAX,ESP + PUSH SS ; 保存中断时的SS + PUSH EAX ; 保存中断时的ESP + MOV AX,SS + MOV DS,AX + MOV ES,AX + CALL _inthandler0d + ADD ESP,8 + POPAD + POP DS + POP ES + ADD ESP,4 ; 在INT 0x0d中需要这句 + IRETD +.from_app: +; 当应用程序活动时产生中断 + CLI + MOV EAX,1*8 + MOV DS,AX ; 先仅将DS设定为操作系统用 + MOV ECX,[0xfe4] ; 操作系统的ESP + ADD ECX,-8 + MOV [ECX+4],SS ; 保存产生中断时的SS + MOV [ECX ],ESP ; 保存产生中断时的ESP + MOV SS,AX + MOV ES,AX + MOV ESP,ECX + STI + CALL _inthandler0d + CLI + CMP EAX,0 + JNE .kill + POP ECX + POP EAX + MOV SS,AX ; 将SS恢复为应用程序用 + MOV ESP,ECX ; 将ESP恢复为应用程序用 + POPAD + POP DS + POP ES + ADD ESP,4 ; INT 0x0d需要这句 + IRETD +.kill: +; 将应用程序强制结束 + MOV EAX,1*8 ; 操作系统用的DS/SS + MOV ES,AX + MOV SS,AX + MOV DS,AX + MOV FS,AX + MOV GS,AX + MOV ESP,[0xfe4] ; 强制返回到start_app时的ESP + STI ; 切换完成后恢复中断请求 + POPAD ; 恢复事先保存的寄存器值 + RET + _memtest_sub: ; unsigned int memtest_sub(unsigned int start, unsigned int end) PUSH EDI ; (由于还要使用EBX, ESI, EDI) PUSH ESI