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