refactor: Optimize the context switch overhead
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / interrupt.S
index e9419837ab5b6c07ea36b9a23173b4aec3d15ba4..3007d453a4c307eec7407c42504ad36ef2ad055c 100644 (file)
 #define __ASM__
 #include <arch/x86/interrupts.h>
 #define __ASM__
 #include <arch/x86/interrupts.h>
-#include <lunaix/common.h>
+#include <arch/x86/i386_asm.h>
+#include <arch/x86/tss.h>
 #include <lunaix/syscall.h>
 #define __ASM_INTR_DIAGNOSIS
 
 #include <lunaix/syscall.h>
 #define __ASM_INTR_DIAGNOSIS
 
-.macro isr_template vector, no_error_code=1
-    .global _asm_isr\vector
-    .type _asm_isr\vector, @function
-    _asm_isr\vector:
-        .if \no_error_code
-            pushl $0x0
-        .endif
-        pushl $\vector
-        jmp interrupt_wrapper
-.endm
-
 #ifdef __ASM_INTR_DIAGNOSIS
 .section .bss
     .global debug_resv
     debug_resv:
         .skip 16
 #ifdef __ASM_INTR_DIAGNOSIS
 .section .bss
     .global debug_resv
     debug_resv:
         .skip 16
+    tmp_store:
+        .skip 4
 #endif
 
 #endif
 
-.section .text
-    isr_template FAULT_DIVISION_ERROR
-    isr_template FAULT_GENERAL_PROTECTION, no_error_code=0
-    isr_template FAULT_PAGE_FAULT, no_error_code=0
-    isr_template FAULT_STACK_SEG_FAULT, no_error_code=0
-
-    isr_template LUNAIX_SYS_PANIC
-    isr_template LUNAIX_SYS_CALL
-
-    isr_template APIC_ERROR_IV
-    isr_template APIC_LINT0_IV
-    isr_template APIC_TIMER_IV
-    isr_template APIC_SPIV_IV
-    isr_template RTC_TIMER_IV
-    isr_template PC_KBD_IV
+.section .bss
+    .align 16
+    lo_tmp_stack:
+        .skip 256
+    tmp_stack:
+
+/*
+    This perhaps the ugliest part in the project. 
+    It contains code to handle arbitrary depth of 
+    nested interrupt and all those corner cases and 
+    nasty gotchas.
+
+    Be aware the twists, offsets and hidden dependencies!
+
+*/
+
+#define regsize 4
+
+/* stack layout: saved interrupt context */
+    .struct 0
+idepth:
+    .struct idepth + regsize
+ieax:
+    .struct ieax + regsize
+iebx:
+    .struct iebx + regsize
+iecx:
+    .struct iecx + regsize
+iedx:
+    .struct iedx + regsize
+iedi:
+    .struct iedi + regsize
+iebp:
+    .struct iebp + regsize
+iesi:
+    .struct iesi + regsize
+ids:
+    .struct ids + regsize
+ies:
+    .struct ies + regsize
+ifs:
+    .struct ifs + regsize
+igs:
+    .struct igs + regsize
+iesp:
+    .struct iesp + regsize
+isave_prev:
+    .struct isave_prev + regsize
+ivec:
+    .struct ivec + regsize
+iecode:
+    .struct iecode + regsize
+ieip:
+    .struct ieip + regsize
+ics:
+    .struct ics + regsize
+ieflags:
+    .struct ieflags + regsize
+iuesp:
+    .struct iuesp + regsize
+iuss:
+
+
+/* stack layout: execution (flow-control) state context */
+    .struct 0
+exsave_prev:
+    .struct exsave_prev + regsize
+exvec:
+    .struct exvec + regsize
+execode:
+    .struct execode + regsize
+exeip:
+    .struct exeip + regsize
+excs:
+    .struct excs + regsize
+exeflags:
+    .struct exeflags + regsize
+exuesp:
+    .struct exuesp + regsize
+exuss:
+
+/* struct layout: critical section of struct proc_info */
+    .struct 0
+proc_pid:
+    .struct proc_pid + regsize
+proc_parent:
+    .struct proc_parent + regsize
+proc_intr_ctx:
+    .struct proc_intr_ctx + regsize
+proc_ustack_top:
+    .struct proc_ustack_top + regsize
+proc_page_table:
+    .struct proc_page_table + regsize
+proc_fxstate:
 
 
+.section .text
+    .global interrupt_wrapper
     interrupt_wrapper:
         /*
          Stack layout (layout of struct isr_param)
     interrupt_wrapper:
         /*
          Stack layout (layout of struct isr_param)
-    msa:   [ss]
-           [esp]
-           eflags     > offset = 48 + 16 = 64
-           cs
-           eip
-           err_code   
-           vector     > offset = 28 + 16 + 4 = 48
-           esp
+    msa:   [ss]             > 76 -> 28
+           [esp]            > 72 -> 24
+           eflags           > 68 -> 20
+           cs               > 64 -> 16
+           eip              > 60 -> 12
+           err_code         > 56 -> 8
+           vector           > offset = 52 -> 4
+           [saved_prev_ctx] > offset = 0
+           ---
+           esp       > 12 * 4 = 48
            gs
            fs
            es
            gs
            fs
            es
-           ds         > offset = 7 * 4 = 28
+           ds         > offset = 8 * 4 = 32
            esi
            ebp
            edi
            edx
            ecx
            ebx
            esi
            ebp
            edi
            edx
            ecx
            ebx
-    lsa:   eax        > offset = 0
+           eax
+    lsa:   depth       > offset = 0
 
             las: Least Significant Address
             msa: Most Significant Address
         */
         cld
 
             las: Least Significant Address
             msa: Most Significant Address
         */
         cld
+
+        subl $4, %esp
         pushl %esp
 
         subl $16, %esp
         pushl %esp
 
         subl $16, %esp
         pushl %ebx
         pushl %eax
 
         pushl %ebx
         pushl %eax
 
-        movl 60(%esp), %eax   /* 取出 %cs */
+        movl __current, %eax
+        movl proc_intr_ctx(%eax), %eax
+        incl %eax
+        pushl %eax          # nested intr: current depth
+
+        movl ics(%esp), %eax   /* 取出 %cs */
         andl $0x3, %eax          /* 判断 RPL */
         jz 1f
 
         andl $0x3, %eax          /* 判断 RPL */
         jz 1f
 
         movw %ax, %ds
         movw %ax, %es
 
         movw %ax, %ds
         movw %ax, %es
 
-        # 保存用户栈顶指针。这是因为我们允许系统调用内进行上下文切换,而这样一来,我们就失去了用户栈的信息,
-        # 这样一来,就无法设置信号上下文。这主要是为了实现了pause()而做的准备
-        movl (__current), %eax  
-        movl 68(%esp), %ebx     # 取出esp
-        movl %ebx, 84(%eax)     # 存入__current->ustack_top
+        movl __current, %eax
+
+        # FIXME: Save x87 context to user stack, rather than kernel's memory.
+        # 保存x87FPU的状态
+        movl proc_fxstate(%eax), %ebx
+        fxsave (%ebx)
+
+        # 保存用户栈顶指针。因为我们允许同级中断的产生,所以需要该手段跟踪用户栈的地址。
+        movl iuesp(%esp), %ebx     # 取出esp
+        movl %ebx, proc_ustack_top(%eax)     # 存入__current->ustack_top
 
     1:
         movl %esp, %eax
 
     1:
         movl %esp, %eax
     soft_iret:
         movl %eax, %esp
 
     soft_iret:
         movl %eax, %esp
 
+#ifdef __ASM_INTR_DIAGNOSIS
+        movl %eax, (debug_resv + 8)
+        movl iesp(%esp), %eax
+        movl exeip(%eax), %eax
+        movl %eax, (debug_resv + 4) # eip
+#endif
+        movl __current, %eax
+        movl proc_fxstate(%eax), %eax
+        
+        test %eax, %eax     # do we have stored x87 context?
+        jz 1f 
+        fxrstor (%eax) 
+
+1:
+        popl %eax   # discard isr_param::depth
         popl %eax
         popl %ebx
         popl %ecx
         popl %eax
         popl %ebx
         popl %ecx
 
         movl 16(%esp), %esp
 
 
         movl 16(%esp), %esp
 
+        movl %eax, tmp_store
+        movl __current, %eax
+        
+
+        # nested intr: restore saved context
+        popl proc_intr_ctx(%eax)
+
         addl $8, %esp
 
 #ifdef __ASM_INTR_DIAGNOSIS
         addl $8, %esp
 
 #ifdef __ASM_INTR_DIAGNOSIS
-        pushl %eax
-        movl 4(%esp), %eax
+        movl (%esp), %eax
         movl %eax, debug_resv
         movl %eax, debug_resv
-        popl %eax
 #endif
 #endif
+        # 处理TSS.ESP的一些边界条件。如果是正常iret(即从内核模式*优雅地*退出)
+        # 那么TSS.ESP0应该为iret进行弹栈后,%esp的值。
+        # 所以这里的边界条件是:如返回用户模式,iret会额外弹出8个字节(ss,esp)
+        movl 4(%esp), %eax
+        andl $3, %eax
+        setnz %al
+        shll $3, %eax
+        addl $12, %eax
+        addl %esp, %eax
+        movl %eax, (_tss + tss_esp0_off)
+        movl tmp_store, %eax
+
         iret
 
     .global switch_to
     switch_to:
         # 约定
         iret
 
     .global switch_to
     switch_to:
         # 约定
-        # arg1: 目标进程PCB地址 (next)
-        popl %ecx               # next
-
-        call signal_dispatch    # kernel/signal.c
-        movl %eax, %edx
+        # arg1: 目标进程PCB地址 (next
 
 
+        popl %ebx               # next
         movl __current, %eax    
         movl __current, %eax    
-        movl 88(%eax), %ebx     # __current->pagetable
-        movl 88(%ecx), %eax     # next->pagetable
+        movl proc_page_table(%eax), %ecx     # __current->pagetable
+        movl proc_page_table(%ebx), %eax     # next->pagetable
         
         
-        cmpl %ebx, %eax         # if(next->pagtable != __current->pagetable) {
+        cmpl %ecx, %eax         # if(next->pagtable != __current->pagetable) {
         jz 1f
         movl %eax, %cr3         #   cpu_lcr3(next->pagetable)
                                 # }
     1:
         jz 1f
         movl %eax, %cr3         #   cpu_lcr3(next->pagetable)
                                 # }
     1:
-        movl %ecx, __current    # __current = next
+        movl %ebx, __current    # __current = next
+
+        # 我们已经处在了新的地址空间,为了避免影响其先前的栈布局
+        # 需要使用一个临时的栈空间
+        movl $tmp_stack, %esp
+        
+        # 更新 tss
+        movl proc_intr_ctx(%ebx), %eax      # proc->intr_ctx
+        movl iesp(%eax), %eax      # intr_ctx->esp
+        movl %eax, (tss_esp0_off + _tss)
 
 
-        test %edx, %edx         # do we have signal to handle?
+        call signal_dispatch    # kernel/signal.c
+
+        test %eax, %eax         # do we have signal to handle?
         jz 1f
         jz 1f
-        movl %edx, %eax
         jmp handle_signal
     1:
         jmp handle_signal
     1:
-        leal 8(%ecx), %eax
+        movl proc_intr_ctx(%ebx), %eax
         jmp soft_iret
 
     .global handle_signal
     handle_signal:
         # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性!
         # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈!
         jmp soft_iret
 
     .global handle_signal
     handle_signal:
         # 注意1:任何对proc_sig的布局改动,都须及时的保证这里的一致性!
         # 注意2:handle_signal在调用之前,须确保proc_sig已经写入用户栈!
-        leal 8(%eax), %ebx      # arg1 in %eax: addr of proc_sig structure in user stack
+        # arg1 in %eax: addr of proc_sig structure in user stack
+        leal 12(%eax), %ebx      # %ebx = &proc_sig->prev_context
 
 
-        pushl 72(%ebx)          # proc_sig->prev_context.ss
+        pushl $UDATA_SEG        # proc_sig->prev_context.proc_regs.ss
         pushl %eax              # esp
         pushl %eax              # esp
-        pushl 64(%ebx)          # proc_sig->prev_context.eflags
+
+        movl 48(%ebx), %ebx
+        pushl 68(%ebx)          # proc_sig->prev_context.proc_regs.execp->eflags
+        
         pushl $UCODE_SEG        # cs
         pushl $UCODE_SEG        # cs
-        pushl $sig_wrapper      # eip for sig wrapper
+        pushl 4(%eax)           # %eip = proc_sig->sigact
 
         movw $UDATA_SEG, %cx    # switch data seg to user mode
         movw %cx, %es
 
         movw $UDATA_SEG, %cx    # switch data seg to user mode
         movw %cx, %es
         movw %cx, %fs
         movw %cx, %gs
 
         movw %cx, %fs
         movw %cx, %gs
 
-        iret  
-
-.section .usrtext
-    sig_wrapper:                # in user mode
-        movl %esp, %eax
-        and $0xfffffff0, %esp
-        subl $8, %esp
-        pushl %eax              # Addr to proc_sig structure 
-        pushl 4(%eax)           # proc_sig->sig_num     ---- 16 bytes aligned
-
-        call (%eax)             # invoke signal handler
-
-        # invoke the sigreturn syscall to exit the signal wrapper
-        movl $__SYSCALL_sigreturn, %eax
-        movl 4(%esp), %ebx
-        int $LUNAIX_SYS_CALL    
-
-        ud2                     # never reach!
\ No newline at end of file
+        iret
\ No newline at end of file