feat: add signal handling support (not tested!)
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / interrupt.S
1 #define __ASM__
2 #include <arch/x86/interrupts.h>
3 #include <lunaix/common.h>
4 #include <lunaix/syscall.h>
5 #define __ASM_INTR_DIAGNOSIS
6
7 .macro isr_template vector, no_error_code=1
8     .global _asm_isr\vector
9     .type _asm_isr\vector, @function
10     _asm_isr\vector:
11         .if \no_error_code
12             pushl $0x0
13         .endif
14         pushl $\vector
15         jmp interrupt_wrapper
16 .endm
17
18 #ifdef __ASM_INTR_DIAGNOSIS
19 .section .bss
20     .global debug_resv
21     debug_resv:
22         .skip 16
23 #endif
24
25 .section .text
26     isr_template FAULT_DIVISION_ERROR
27     isr_template FAULT_GENERAL_PROTECTION, no_error_code=0
28     isr_template FAULT_PAGE_FAULT, no_error_code=0
29
30     isr_template LUNAIX_SYS_PANIC
31     isr_template LUNAIX_SYS_CALL
32
33     isr_template APIC_ERROR_IV
34     isr_template APIC_LINT0_IV
35     isr_template APIC_TIMER_IV
36     isr_template APIC_SPIV_IV
37     isr_template RTC_TIMER_IV
38     isr_template PC_KBD_IV
39
40     interrupt_wrapper:
41         /*
42          Stack layout (layout of struct isr_param)
43     msa:   [ss]
44            [esp]
45            eflags     > offset = 48 + 16 = 64
46            cs
47            eip
48            err_code   
49            vector     > offset = 28 + 16 + 4 = 48
50            esp
51            gs
52            fs
53            es
54            ds         > offset = 7 * 4 = 28
55            esi
56            ebp
57            edi
58            edx
59            ecx
60            ebx
61     lsa:   eax        > offset = 0
62
63             las: Least Significant Address
64             msa: Most Significant Address
65         */
66         cld
67         pushl %esp
68
69         subl $16, %esp
70         movw %gs, 12(%esp)
71         movw %fs,  8(%esp)
72         movw %es,  4(%esp)
73         movw %ds,   (%esp)
74
75         pushl %esi
76         pushl %ebp
77         pushl %edi
78         pushl %edx
79         pushl %ecx
80         pushl %ebx
81         pushl %eax
82
83         movl 60(%esp), %eax   /* 取出 %cs */
84         andl $0x3, %eax          /* 判断 RPL */
85         jz 1f
86
87         movw $KDATA_SEG, %ax    /* 如果从用户模式转来,则切换至内核数据段 */
88         movw %ax, %gs
89         movw %ax, %fs
90         movw %ax, %ds
91         movw %ax, %es
92
93         # 保存用户栈顶指针。这是因为我们允许系统调用内进行上下文切换,而这样一来,我们就失去了用户栈的信息,
94         # 这样一来,就无法设置信号上下文。这主要是为了实现了pause()而做的准备
95         movl (__current), %eax  
96         movl 68(%esp), %ebx     # 取出esp
97         movl %ebx, 84(%eax)     # 存入__current->ustack_top
98
99     1:
100         movl %esp, %eax
101         andl $0xfffffff0, %esp
102         subl $16, %esp
103         movl %eax, (%esp)
104
105         call intr_handler
106
107     .global soft_iret
108     soft_iret:
109         cli
110         popl %esp
111
112         popl %eax
113         popl %ebx
114         popl %ecx
115         popl %edx
116         popl %edi
117         popl %ebp
118         popl %esi
119         
120         movw   (%esp), %ds
121         movw  4(%esp), %es
122         movw  8(%esp), %fs
123         movw 12(%esp), %gs
124
125         movl 16(%esp), %esp
126
127         addl $8, %esp
128
129 #ifdef __ASM_INTR_DIAGNOSIS
130         pushl %eax
131         movl 4(%esp), %eax
132         movl %eax, debug_resv
133         popl %eax
134 #endif
135         iret
136
137     handle_signal:
138         # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性!
139         # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈!
140         popl %eax               # arg1: addr of proc_sig structure in user stack
141         leal 8(%eax), %ebx
142
143         pushl 72(%ebx)          # proc_sig->prev_context.ss
144         pushl %eax              # esp
145         pushl 64(%ebx)          # proc_sig->prev_context.eflags
146         pushl $UCODE_SEG        # cs
147         pushl $sig_wrapper      # eip for sig wrapper
148
149         movw $UDATA_SEG, %cx    # switch data seg to user mode
150         movw %cx, %es
151         movw %cx, %ds
152         movw %cx, %fs
153         movw %cx, %gs
154
155         iret  
156
157     sig_wrapper:                # in user mode
158         movl %esp, %eax
159         and $0xfffffff0, %esp
160         subl $8, %esp
161         pushl %eax              # Addr to proc_sig structure 
162         pushl 4(%eax)           # proc_sig->sig_num     ---- 16 bytes aligned
163
164         call (%eax)             # invoke signal handler
165
166         # invoke the sigreturn syscall to exit the signal wrapper
167         movl $__SYSCALL_sigreturn, %eax
168         movl 4(%esp), %ebx
169         int $LUNAIX_SYS_CALL    
170
171         ud2                     # never reach!