Skip to content

Commit d6f8ec2

Browse files
committed
add struct irpt_regs
1 parent e0e99aa commit d6f8ec2

File tree

6 files changed

+257
-18
lines changed

6 files changed

+257
-18
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
CFLAGS=-I ./include -I ./include/boot -fno-builtin
22

3-
cobjects = lib/kstdlib.o lib/printk.o src/kernel.o src/protect.o src/irpts.o
3+
cobjects = lib/kstdlib.o lib/kstdio.o src/kernel.o src/protect.o src/irpts.o
44

5-
objects = $(cobjects) loader.o arch/i386/irpts.o
5+
objects = $(cobjects) boot/i386/loader.o arch/i386/irpts.o
66

77
all: kernel
88

9-
loader.o:
10-
nasm -f elf -o loader.o ./boot/i386/loader.asm
9+
boot/i386/loader.o:
10+
nasm -f elf -o boot/i386/loader.o ./boot/i386/loader.asm
1111
arch/i386/irpts.o:
1212
nasm -f elf -o ./arch/i386/irpts.o ./arch/i386/irpts.asm
1313

arch/i386/irpts.asm

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,137 @@
2323

2424
[BITS 32]
2525

26-
;除0异常
27-
global divide0_error, general_protection
28-
26+
; int0 除0异常
27+
global divide0_error
2928
extern _do_divide0_error
3029
divide0_error:
3130
push 0x00 ; 当做错误码 push 入栈
3231
push _do_divide0_error
3332
jmp exception_handler
3433

34+
; int1 单步调试
35+
global debug
36+
extern _do_debug
37+
debug:
38+
push 0x00
39+
push _do_debug
40+
jmp exception_handler
41+
42+
; int2 NMI
43+
global nmi
44+
extern _do_nmi
45+
nmi:
46+
push 0x00
47+
push _do_nmi
48+
jmp exception_handler
49+
50+
; int3 断点
51+
global debug_break
52+
extern _do_debug_break
53+
debug_break:
54+
push 0x00
55+
push _do_debug_break
56+
jmp exception_handler
57+
58+
; int4 溢出
59+
global overflow
60+
extern _do_overflow
61+
overflow:
62+
push 0x00
63+
push _do_overflow
64+
jmp exception_handler
65+
66+
; int5 寻址到有效地址以外
67+
global bounds_check
68+
extern _do_bounds_check
69+
bounds_check:
70+
push 0x00
71+
push _do_bounds_check
72+
jmp exception_handler
73+
74+
; int6 非法操作码
75+
global invalid_op
76+
extern _do_invalid_op
77+
invalid_op:
78+
push 0x00
79+
push _do_invalid_op
80+
jmp exception_handler
81+
82+
; int7 设备故障
83+
global device_fail
84+
extern _do_device_fail
85+
device_fail:
86+
push _do_device_fail
87+
jmp exception_handler
88+
89+
; int8 双故障出错
90+
global double_fault
91+
extern _do_double_fault
92+
double_fault:
93+
push _do_double_fault
94+
jmp exception_handler
95+
96+
; int9 协处理器段错误
97+
global cop_segment
98+
extern _do_cop_segment
99+
cop_segment:
100+
push _do_cop_segment
101+
jmp exception_handler
102+
103+
; int10 TSS无效
104+
global tss_inval
105+
extern _do_tss_inval
106+
tss_inval:
107+
push _do_tss_inval
108+
jmp exception_handler
109+
110+
; int11 段不存在
111+
global segment_unpresent
112+
extern _do_segment_unpresent
113+
segment_unpresent:
114+
push _do_segment_unpresent
115+
jmp exception_handler
116+
117+
; int12 堆栈段不存在或者越界
118+
global stack_segment
119+
extern _do_stack_segment
120+
stack_segment:
121+
push _do_stack_segment
122+
jmp exception_handler
123+
124+
; int13 不符合386保护机制
125+
global general_protection
35126
extern _do_general_protection
36127
general_protection:
37128
push _do_general_protection
38129
jmp exception_handler
39130

131+
; int14 页不在内存
132+
global page_fault
133+
extern _do_page_fault
134+
page_fault:
135+
push _do_page_fault
136+
jmp exception_handler
137+
138+
; int15 保留
139+
global reserved
140+
extern _do_reserved
141+
reserved:
142+
push 0x00
143+
push _do_reserved
144+
jmp exception_handler
145+
146+
; int16 协处理器出错信号
147+
global cop_error
148+
extern _do_cop_error
149+
cop_error:
150+
push _do_cop_error
151+
jmp exception_handler
152+
40153
extern kernel_data_selector
41154
exception_handler:
42155
xchg [esp+4], eax ; exchange errcode <-> eax
43-
xchg [esp], ebx ; exchange function <-> ebx
156+
xchg [esp], ebx ; exchange function <-> ebx
44157
push ecx
45158
push edx
46159
push edi

include/sys/irpts.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#ifndef _ISR_H_
22
#define _ISR_H_
33

4+
#include <sys/system.h>
5+
46
#define cli() __asm__ __volatile__ ("cli" ::)
57
#define sti() __asm__ __volatile__ ("sti" ::)
68
#define nop() __asm__ __volatile__ ("nop" ::)
79

10+
void _do_divide0_error(struct irpt_regs* regs);
11+
void _do_debug(struct irpt_regs* regs);
12+
void _do_nmi(struct irpt_regs* regs);
13+
void _do_debug_break(struct irpt_regs* regs);
14+
815
void setup_irqts();
916
#endif

include/sys/system.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2013 Wang Jing <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/*
19+
* Author: wangjild <[email protected]>
20+
* Github: https://github.com/wangjild/OS_Hacking
21+
* File: system.h
22+
* Lauguage: c/cpp
23+
* Date: 13-12-15 22:12:35
24+
* Descripton:
25+
*/
26+
27+
#include <sys/types.h>
28+
struct irpt_regs
29+
{
30+
uint32_t err_code; /* push */
31+
uint32_t fs, es, ds; /* 压入上一个段。 */
32+
uint32_t ebp, esi, edi, edx, ecx; /* 由push 压入*/
33+
uint32_t ebx, eax; /* 由exchange 压入*/
34+
uint32_t eip, cs, eflags, uesp, ss; /* 由处理器自动压入。 */
35+
};

lib/printk.c renamed to lib/kstdio.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
/*
2-
CopyRight
3-
*/
2+
* Copyright (C) 2013 Wang Jing <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
/*
19+
* Author: wangjild <[email protected]>
20+
* Github: https://github.com/wangjild/OS_Hacking
21+
* File: kstdio.c
22+
* Lauguage: c/cpp
23+
* Date: 13-12-15 20:09:48
24+
* Descripton:
25+
*/
26+
427
#include <kstdio.h>
528
#include <kstdlib.h>
629

src/irpts.c

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,81 @@
2727
#include <sys/protect.h>
2828
#include <sys/irpts.h>
2929

30-
static void FATAL(const char* str, uint32_t* esp, uint32_t errcode) {
31-
printk("EIP:\t%04x:%x\nEFLAGS:\t%04x\nESP:\t%04x:%x\n",
32-
esp[1], esp[0], esp[2], esp[4], esp[3]);
30+
static void FATAL(const char* str, struct irpt_regs* regs) {
31+
printk("EIP: %x:%x\nEFLAGS: %x\nESP: %x:%x\n",
32+
regs->cs, regs->eip, regs->eflags, regs->es, regs->uesp);
3333
for (;;) ;
3434
}
3535

36-
void _do_divide0_error(uint32_t esp, uint32_t errcode) {
37-
FATAL("divide 0 error", (uint32_t*) esp, errcode);
36+
void _do_divide0_error(struct irpt_regs* regs) {
37+
FATAL("int0: divide 0 error", regs);
3838
}
3939

40-
void _do_general_protection(uint32_t esp, uint32_t errcode) {
41-
FATAL("general protection", (uint32_t*) esp, errcode);
40+
void _do_debug(struct irpt_regs* regs) {
41+
FATAL("int1: debug", regs);
4242
}
4343

44+
void _do_nmi(struct irpt_regs* regs) {
45+
FATAL("int2: nmi", regs);
46+
}
47+
48+
void _do_debug_break(struct irpt_regs* regs) {
49+
FATAL("int3: break", regs);
50+
}
51+
52+
void _do_overflow(struct irpt_regs* regs) {
53+
FATAL("int4: overflow", regs);
54+
}
55+
56+
void _do_bounds_check(struct irpt_regs* regs) {
57+
FATAL("int5: bounds_check", regs);
58+
}
59+
60+
void _do_invalid_op(struct irpt_regs* regs) {
61+
FATAL("int6: invalid op", regs);
62+
}
63+
64+
void _do_device_fail(struct irpt_regs* regs) {
65+
FATAL("int7: device fail", regs);
66+
}
67+
68+
void _do_double_fault(struct irpt_regs* regs) {
69+
FATAL("int8: double fault", regs);
70+
}
71+
72+
void _do_cop_segment(struct irpt_regs* regs) {
73+
FATAL("int9: coprocesser segment", regs);
74+
}
75+
76+
void _do_tss_inval(struct irpt_regs* regs) {
77+
FATAL("int10: tss invalid", regs);
78+
}
79+
80+
void _do_segment_unpresent(struct irpt_regs* regs) {
81+
FATAL("int11: segment unpresent", regs);
82+
}
83+
84+
void _do_stack_segment(struct irpt_regs* regs) {
85+
FATAL("int12: stack segment unpresent or overflow", regs);
86+
}
87+
88+
void _do_general_protection(struct irpt_regs* regs) {
89+
FATAL("int13: general protection", regs);
90+
}
91+
92+
void _do_page_fault(struct irpt_regs* regs) {
93+
FATAL("int14: page fault", regs);
94+
}
95+
96+
void _do_reserved(struct irpt_regs* regs) {
97+
FATAL("int15: reserved", regs);
98+
}
99+
100+
void _do_cop_error(struct irpt_regs* regs) {
101+
FATAL("int16: coprocesser error", regs);
102+
}
103+
104+
44105
#define IDT_OFFSET 256
45106
struct idt_entry idts[IDT_OFFSET];
46107
struct idt_ptr idtptr;
@@ -69,7 +130,7 @@ void setup_idt() {
69130
idtptr.limit = sizeof(idts) - 1;
70131

71132
set_idt(0, (uint32_t) &divide0_error, kernel_code_selector, IDT_DPL0 | IDT_TRAP);
133+
set_idt(13, (uint32_t) &general_protection, kernel_code_selector, IDT_DPL0 | IDT_IRPT);
72134

73135
idt_load();
74-
//set_idt(13, (uint32_t) &general_protection, kernel_code_selector, IDT_DPL0 | IDT_IRPT);
75136
}

0 commit comments

Comments
 (0)