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