refactor: keep in mind the stack layout is crucial. Move context switching & signal...
[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         movl (%esp), %eax
108
109     .global soft_iret
110     soft_iret:
111         movl %eax, %esp
112
113         popl %eax
114         popl %ebx
115         popl %ecx
116         popl %edx
117         popl %edi
118         popl %ebp
119         popl %esi
120         
121         movw   (%esp), %ds
122         movw  4(%esp), %es
123         movw  8(%esp), %fs
124         movw 12(%esp), %gs
125
126         movl 16(%esp), %esp
127
128         addl $8, %esp
129
130 #ifdef __ASM_INTR_DIAGNOSIS
131         pushl %eax
132         movl 4(%esp), %eax
133         movl %eax, debug_resv
134         popl %eax
135 #endif
136         iret
137
138     .global switch_to
139     switch_to:
140         # 约定
141         # arg1: 目标进程PCB地址 (next)
142         popl %ecx               # next
143
144         call signal_dispatch    # kernel/signal.c
145         movl %eax, %edx
146
147         movl __current, %eax    
148         movl 88(%eax), %ebx     # __current->pagetable
149         movl 88(%ecx), %eax     # next->pagetable
150         
151         cmpl %ebx, %eax         # if(next->pagtable != __current->pagetable) {
152         jz 1f
153         movl %eax, %cr3         #   cpu_lcr3(next->pagetable)
154                                 # }
155     1:
156         movl %ecx, __current    # __current = next
157
158         test %edx, %edx         # do we have signal to handle?
159         jz 1f
160         movl %edx, %eax
161         jmp handle_signal
162     1:
163         leal 8(%ecx), %eax
164         jmp soft_iret
165
166     .global handle_signal
167     handle_signal:
168         # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性!
169         # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈!
170         leal 8(%eax), %ebx      # arg1 in %eax: addr of proc_sig structure in user stack
171
172         pushl 72(%ebx)          # proc_sig->prev_context.ss
173         pushl %eax              # esp
174         pushl 64(%ebx)          # proc_sig->prev_context.eflags
175         pushl $UCODE_SEG        # cs
176         pushl $sig_wrapper      # eip for sig wrapper
177
178         movw $UDATA_SEG, %cx    # switch data seg to user mode
179         movw %cx, %es
180         movw %cx, %ds
181         movw %cx, %fs
182         movw %cx, %gs
183
184         iret  
185
186     sig_wrapper:                # in user mode
187         movl %esp, %eax
188         and $0xfffffff0, %esp
189         subl $8, %esp
190         pushl %eax              # Addr to proc_sig structure 
191         pushl 4(%eax)           # proc_sig->sig_num     ---- 16 bytes aligned
192
193         call (%eax)             # invoke signal handler
194
195         # invoke the sigreturn syscall to exit the signal wrapper
196         movl $__SYSCALL_sigreturn, %eax
197         movl 4(%esp), %ebx
198         int $LUNAIX_SYS_CALL    
199
200         ud2                     # never reach!