fix: previous interrupt context lost after a nested interrupt
[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 #ifdef __ASM_INTR_DIAGNOSIS
8 .section .bss
9     .global debug_resv
10     debug_resv:
11         .skip 16
12     tmp_store:
13         .skip 4
14 #endif
15
16 .section .bss
17     .align 16
18     lo_tmp_stack:
19         .skip 128
20     tmp_stack:
21
22 .section .text
23     .global interrupt_wrapper
24     interrupt_wrapper:
25         /*
26          Stack layout (layout of struct isr_param)
27     msa:   [ss]             > 76
28            [esp]            > 72
29            eflags           > 68
30            cs               > 64
31            eip              > 60
32            err_code         > 56
33            vector           > offset = 52
34            [saved_prev_ctx] > offset = 0
35            ---
36            esp
37            gs
38            fs
39            es
40            ds         > offset = 7 * 4 = 28 + 4
41            esi
42            ebp
43            edi
44            edx
45            ecx
46            ebx
47            eax
48     lsa:   depth       > offset = 0
49
50             las: Least Significant Address
51             msa: Most Significant Address
52         */
53         cld
54
55         subl $52, %esp
56         pushl %esp
57
58         subl $16, %esp
59         movw %gs, 12(%esp)
60         movw %fs,  8(%esp)
61         movw %es,  4(%esp)
62         movw %ds,   (%esp)
63
64         pushl %esi
65         pushl %ebp
66         pushl %edi
67         pushl %edx
68         pushl %ecx
69         pushl %ebx
70         pushl %eax
71
72         movl __current, %eax
73         movl 8(%eax), %eax
74         incl %eax
75         pushl %eax          # nested intr: current depth
76
77         movl 116(%esp), %eax   /* 取出 %cs */
78         andl $0x3, %eax          /* 判断 RPL */
79         jz 1f
80
81         movw $KDATA_SEG, %ax    /* 如果从用户模式转来,则切换至内核数据段 */
82         movw %ax, %gs
83         movw %ax, %fs
84         movw %ax, %ds
85         movw %ax, %es
86
87         # 保存用户栈顶指针。这是因为我们允许系统调用内进行上下文切换,而这样一来,我们就失去了用户栈的信息,
88         # 这样一来,就无法设置信号上下文。这主要是为了实现了pause()而做的准备
89         movl __current, %eax
90
91         # 保存x87FPU的状态
92         movl 68(%eax), %ebx
93         fxsave (%ebx)
94
95         movl 124(%esp), %ebx     # 取出esp
96         movl %ebx, 60(%eax)     # 存入__current->ustack_top
97
98     1:
99         movl %esp, %eax
100         andl $0xfffffff0, %esp
101         subl $16, %esp
102         movl %eax, (%esp)
103
104         call intr_handler
105
106         movl (%esp), %eax
107
108     .global soft_iret
109     soft_iret:
110         movl %eax, %esp
111
112 #ifdef __ASM_INTR_DIAGNOSIS
113         movl %eax, (debug_resv + 8)
114         movl 48(%esp), %eax
115         movl 60(%eax), %eax
116         movl %eax, (debug_resv + 4) # eip
117 #endif
118         movl __current, %eax
119         movl 68(%eax), %eax
120         
121         test %eax, %eax     # do we have stored x87 context?
122         jz 1f 
123         fxrstor (%eax)      
124 1:
125         popl %eax   # discard isr_param::depth
126         popl %eax
127         popl %ebx
128         popl %ecx
129         popl %edx
130         popl %edi
131         popl %ebp
132         popl %esi
133         
134         movw   (%esp), %ds
135         movw  4(%esp), %es
136         movw  8(%esp), %fs
137         movw 12(%esp), %gs
138
139         movl 16(%esp), %esp
140
141         movl %eax, tmp_store
142         movl __current, %eax
143         # nested intr: restore saved context
144         popl 8(%eax)       # depth
145         popl 12(%eax)      # eax
146         popl 16(%eax)      # ebx
147         popl 20(%eax)      # ecx
148         popl 24(%eax)      # edx
149         popl 28(%eax)      # edi
150         popl 32(%eax)      # ebp
151         popl 36(%eax)      # esi
152         popl 40(%eax)      # ds
153         popl 44(%eax)      # es
154         popl 48(%eax)      # fs
155         popl 52(%eax)      # gs
156         popl 56(%eax)      # esp
157
158         addl $8, %esp
159
160 #ifdef __ASM_INTR_DIAGNOSIS
161         movl (%esp), %eax
162         movl %eax, debug_resv
163 #endif
164         # 处理TSS.ESP的一些边界条件。如果是正常iret(即从内核模式*优雅地*退出)
165         # 那么TSS.ESP0应该为iret进行弹栈后,%esp的值。
166         # 所以这里的边界条件是:如返回用户模式,iret会额外弹出8个字节(ss,esp)
167         movl 4(%esp), %eax
168         andl $3, %eax
169         setnz %al
170         shll $3, %eax
171         addl $12, %eax
172         addl %esp, %eax
173         movl %eax, (_tss + 4)
174         movl tmp_store, %eax
175         iret
176
177     .global switch_to
178     switch_to:
179         # 约定
180         # arg1: 目标进程PCB地址 (next
181
182         popl %ebx               # next
183         movl __current, %eax    
184         movl 64(%eax), %ecx     # __current->pagetable
185         movl 64(%ebx), %eax     # next->pagetable
186         
187         cmpl %ecx, %eax         # if(next->pagtable != __current->pagetable) {
188         jz 1f
189         movl %eax, %cr3         #   cpu_lcr3(next->pagetable)
190                                 # }
191     1:
192         movl %ebx, __current    # __current = next
193
194         # 我们已经处在了新的地址空间,为了避免影响其先前的栈布局
195         # 需要使用一个临时的栈空间
196         movl $tmp_stack, %esp
197         call signal_dispatch    # kernel/signal.c
198
199         test %eax, %eax         # do we have signal to handle?
200         jz 1f
201         jmp handle_signal
202     1:
203         leal 8(%ebx), %eax
204         jmp soft_iret
205
206     .global handle_signal
207     handle_signal:
208         # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性!
209         # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈!
210         leal 8(%eax), %ebx      # arg1 in %eax: addr of proc_sig structure in user stack
211
212         pushl $UDATA_SEG        # proc_sig->prev_context.proc_regs.ss
213         pushl %eax              # esp
214         movl 48(%ebx), %ebx
215         pushl 68(%ebx)          # proc_sig->prev_context.proc_regs.execp->eflags
216         pushl $UCODE_SEG        # cs
217         pushl $sig_wrapper      # eip for sig wrapper
218
219         movw $UDATA_SEG, %cx    # switch data seg to user mode
220         movw %cx, %es
221         movw %cx, %ds
222         movw %cx, %fs
223         movw %cx, %gs
224
225         iret  
226
227 .section .usrtext
228     sig_wrapper:                # in user mode
229         movl %esp, %eax
230         and $0xfffffff0, %esp
231         subl $8, %esp
232         pushl %eax              # Addr to proc_sig structure 
233         pushl 4(%eax)           # proc_sig->sig_num     ---- 16 bytes aligned
234
235         call *(%eax)             # invoke signal handler
236
237         # invoke the sigreturn syscall to exit the signal wrapper
238         movl $__SYSCALL_sigreturn, %eax
239         movl 4(%esp), %ebx
240         int $LUNAIX_SYS_CALL    
241
242         ud2                     # never reach!