d8df554cd7160335f702dfb322c5886390a898c7
[lunaix-os.git] / lunaix-os / arch / i386 / exceptions / interrupt.S
1 #define __ASM__
2 #include <sys/interrupts.h>
3 #include <sys/abi.h>
4 #include <sys/interrupt.S.inc>
5
6 #include <lunaix/syscall.h>
7
8 #define __ASM_INTR_DIAGNOSIS
9
10 #ifdef __ASM_INTR_DIAGNOSIS
11 .section .bss
12     .global debug_resv
13     debug_resv:
14         .skip 16
15     tmp_store:
16         .skip 4
17 #endif
18
19 .section .bss
20     .align 16
21     lo_tmp_stack:
22         .skip 256
23     tmp_stack:
24
25 /*
26     This perhaps the ugliest part in the project. 
27     It contains code to handle arbitrary depth of 
28     nested interrupt and all those corner cases and 
29     nasty gotchas.
30
31     Be aware the twists, offsets and hidden dependencies!
32
33 */
34
35 .section .text
36     .type interrupt_wrapper, @function
37     .global interrupt_wrapper
38     interrupt_wrapper:
39         cld
40
41         subl $4, %esp
42         pushl %esp
43
44         subl $16, %esp
45         movw %gs, 12(%esp)
46         movw %fs,  8(%esp)
47         movw %es,  4(%esp)
48         movw %ds,   (%esp)
49
50         pushl %esi
51         pushl %ebp
52         pushl %edi
53         pushl %edx
54         pushl %ecx
55         pushl %ebx
56         pushl %eax
57
58         pushl $0      // placeholder for depth accounting
59
60         movl ics(%esp), %eax   /* 取出 %cs */
61         andl $0x3, %eax          /* 判断 RPL */
62         jz 1f
63
64     /* crossing the user/kernel boundary */
65         movw $KDATA_SEG, %ax
66         movw %ax, %gs
67         movw %ax, %fs
68         movw %ax, %ds
69         movw %ax, %es
70
71         movl current_thread, %ebx
72         movl iuesp(%esp), %eax
73
74         # Save x87 context to user stack, rather than kernel's memory.
75         # XXX what will happen if we triggered a page fault during fxsave?
76         # FIXME I think we should defer this to scheduler, and pratice lazy save/load
77         #       Doing this will also make it safe from nested interrupt due to potential 
78         #       page fault when saving
79         # FIXME Also, generalise it to any FPU context, without constraining it to x87.
80
81         #andl $stack_alignment, %eax 
82         #subl $512, %eax
83         #fxsave (%eax)
84
85         # 保存用户栈顶指针。因为我们允许同级中断的产生,所以需要该手段跟踪用户栈的地址。
86         movl %eax, thread_ustack_top(%ebx)     # 存入__current->ustack_top
87
88     /* kernel space same-level switch */
89     1:
90         movl %esp, %eax
91         andl $stack_alignment, %esp
92         subl $16, %esp
93         movl %eax, (%esp)
94
95         xorl %ebp, %ebp     # marks the boundary of stack walking
96         call intr_handler
97
98         movl (%esp), %eax
99
100     .global soft_iret
101     soft_iret:
102         movl %eax, %esp
103
104 #ifdef __ASM_INTR_DIAGNOSIS
105         movl %eax, (debug_resv + 8)
106         movl iesp(%esp), %eax
107         movl exeip(%eax), %eax
108         movl %eax, (debug_resv + 4) # eip
109 #endif
110
111         movl ics(%esp), %eax
112         andl $3, %eax
113         jz 1f 
114
115         ## FIXME x87 fpu context 
116         movl current_thread, %eax
117         movl thread_ustack_top(%eax), %eax
118         test %eax, %eax
119         jz 1f
120         # fxrstor (%eax)
121
122 1:
123         popl %eax   # discard isr_param::depth
124         popl %eax
125         popl %ebx
126         popl %ecx
127         popl %edx
128         popl %edi
129         popl %ebp
130         popl %esi
131         
132         movw   (%esp), %ds
133         movw  4(%esp), %es
134         movw  8(%esp), %fs
135         movw 12(%esp), %gs
136
137         movl 16(%esp), %esp
138
139         movl %eax, tmp_store
140         movl current_thread, %eax
141
142         # nested intr: restore saved context
143         popl thread_intr_ctx(%eax)
144
145         addl $8, %esp
146
147 #ifdef __ASM_INTR_DIAGNOSIS
148         movl (%esp), %eax
149         movl %eax, debug_resv
150 #endif
151         # 处理TSS.ESP的一些边界条件。如果是正常iret(即从内核模式*优雅地*退出)
152         # 那么TSS.ESP0应该为iret进行弹栈后,%esp的值。
153         # 所以这里的边界条件是:如返回用户模式,iret会额外弹出8个字节(ss,esp)
154         movl 4(%esp), %eax
155         andl $3, %eax
156         setnz %al
157         shll $3, %eax
158         addl $12, %eax
159         addl %esp, %eax
160         movl %eax, (_tss + tss_esp0_off)
161         movl tmp_store, %eax
162
163         iret
164
165     .type do_switch, @function
166     .global do_switch
167     do_switch:
168         # Assumption: __current already hold the target process
169
170         call proc_vmroot
171
172         movl %eax, %ebx
173         movl %cr3, %eax
174         xorl %ebx, %eax         # avoid setting cr3 if just local thread switch.
175         jz 1f
176
177         movl %ebx, %cr3         
178
179     1:
180         # the address space could be changed. A temporary stack
181         # is required to prevent corrupt existing stack
182         movl $tmp_stack, %esp
183
184         call signal_dispatch    # kernel/signal.c
185
186         movl current_thread, %ebx
187         test %eax, %eax         # do we have signal to handle?
188         jz 1f
189
190         /*
191             将tss.esp0设置为上次调度前的esp值。
192             当处理信号时,上下文信息是不会恢复的,而是保存在用户栈中,然后直接跳转进位于用户空间的sig_wrapper进行
193             信号的处理。当用户自定义的信号处理函数返回时,sigreturn的系统调用才开始进行上下文的恢复(或者说是进行
194             另一次调度。
195             由于这中间没有进行地址空间的交换,所以第二次跳转使用的是同一个内核栈,而之前默认tss.esp0的值是永远指向最顶部
196             这样一来就有可能会覆盖更早的上下文信息(比如嵌套的信号捕获函数)
197         */
198         movl thread_intr_ctx(%ebx), %ecx      # __current->intr_ctx
199         movl %ecx, (tss_esp0_off + _tss)
200
201         jmp handle_signal
202
203     1:
204         movl thread_intr_ctx(%ebx), %eax
205         jmp soft_iret
206
207     .type handle_signal, @function 
208     .global handle_signal
209     handle_signal:
210         # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性!
211         # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈!
212         # arg1 in %eax: addr of proc_sig structure in user stack
213         movl psig_saved_ictx(%eax), %ebx      # %ebx = &proc_sig->saved_ictx
214
215         pushl $UDATA_SEG
216         pushl %eax              # esp
217
218         movl iexecp(%ebx), %ebx
219         pushl exeflags(%ebx)          # proc_sig->saved_ictx->execp->eflags
220         
221         pushl $UCODE_SEG        # cs
222         pushl psig_sigact(%eax)           # %eip = proc_sig->sigact
223
224         movw $UDATA_SEG, %cx    # switch data seg to user mode
225         movw %cx, %es
226         movw %cx, %ds
227         movw %cx, %fs
228         movw %cx, %gs
229
230         iret