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