fix: passing intr_ctx to soft_iret corrupts the stack of next proccess when performin...
[lunaix-os.git] / lunaix-os / kernel / asm / x86 / interrupt.S
index 471a8e643f44a204b23c3928f5269cc76cd69ddf..a03bce613f3897b5fe65c54d15be68a9e9670453 100644 (file)
@@ -1,6 +1,7 @@
 #define __ASM__
 #include <arch/x86/interrupts.h>
-// #define __ASM_INTR_DIAGNOSIS
+#include <lunaix/common.h>
+#define __ASM_INTR_DIAGNOSIS
 
 .macro isr_template vector, no_error_code=1
     .global _asm_isr\vector
     isr_template PC_KBD_IV
 
     interrupt_wrapper:
+        /*
+         Stack layout
+    msa:   [ss]
+           [esp]
+           eflags
+           cs
+           eip
+           err_code   
+           vector     > offset = 28 + 16 + 4 = 48
+           esp
+           gs
+           fs
+           es
+           ds         > offset = 7 * 4 = 28
+           esi
+           ebp
+           edi
+           edx
+           ecx
+           ebx
+    lsa:   eax        > offset = 0
+
+            las: Least Significant Address
+            msa: Most Significant Address
+        */
         pushl %esp
 
+        subl $16, %esp
+        movw %gs, 12(%esp)
+        movw %fs,  8(%esp)
+        movw %es,  4(%esp)
+        movw %ds,   (%esp)
+
         pushl %esi
         pushl %ebp
         pushl %edi
         pushl %ebx
         pushl %eax
 
+        movl 60(%esp), %eax   /* 取出 %cs */
+        andl $0x3, %eax          /* 判断 RPL */
+        jz 1f
+
+        movw $KDATA_SEG, %ax    /* 如果从用户模式转来,则切换至内核数据段 */
+        movw %ax, %gs
+        movw %ax, %fs
+        movw %ax, %ds
+        movw %ax, %es
+
+    1:
         movl %esp, %eax
         andl $0xfffffff0, %esp
         subl $16, %esp
 
         call intr_handler
 
+        movl (%esp), %eax
+
     .global soft_iret
     soft_iret:
-        popl %esp
+        movl %eax, %esp
 
         popl %eax
         popl %ebx
         popl %edi
         popl %ebp
         popl %esi
-        popl %esp
+        
+        movw   (%esp), %ds
+        movw  4(%esp), %es
+        movw  8(%esp), %fs
+        movw 12(%esp), %gs
+
+        movl 16(%esp), %esp
 
         addl $8, %esp
 
 #ifdef __ASM_INTR_DIAGNOSIS
-        cmpl $0, (%esp)
-        jz 1f
-        iret
-1:
-        movl $__current, %eax
-        movl  (%esp), %ebx
-        movl $debug_resv, %ecx
-        ud2
-#else
-        iret
+        pushl %eax
+        movl 4(%esp), %eax
+        movl %eax, debug_resv
+        popl %eax
 #endif
+        iret