fix: corner case for x87 context restore on execve
authorMinep <lunaixsky@qq.com>
Sun, 23 Jul 2023 17:45:53 +0000 (18:45 +0100)
committerMinep <lunaixsky@qq.com>
Sun, 23 Jul 2023 17:46:05 +0000 (18:46 +0100)
chore: clean up unused fields

lunaix-os/includes/lunaix/process.h
lunaix-os/kernel/asm/x86/interrupt.S
lunaix-os/kernel/exe/exec.c
lunaix-os/kernel/process/signal.c

index 7d385a472d899283bb2773cc581513245df4df1e..1453e71634a72b5ee562f97131899693928ebb12 100644 (file)
@@ -45,8 +45,6 @@
 #define proc_hanged(proc) (((proc)->state) & PS_BLOCKED)
 #define proc_runnable(proc) (((proc)->state) & PS_PAUSED)
 
 #define proc_hanged(proc) (((proc)->state) & PS_BLOCKED)
 #define proc_runnable(proc) (((proc)->state) & PS_PAUSED)
 
-#define PROC_FINPAUSE 1
-
 struct sigact
 {
     struct sigact* prev;
 struct sigact
 {
     struct sigact* prev;
@@ -76,17 +74,17 @@ struct proc_info
 {
     /*
         Any change to *critical section*, including layout, size
 {
     /*
         Any change to *critical section*, including layout, size
-        must be reflected in kernel/asm/x86/interrupt.S to avoid
+        must be reflected in arch/x86/interrupt.S.inc to avoid
         disaster!
      */
 
     /* ---- critical section start ---- */
 
         disaster!
      */
 
     /* ---- critical section start ---- */
 
-    pid_t pid;                // offset = 0
-    struct proc_info* parent; // offset = 4
-    isr_param* intr_ctx;      // offset = 8
-    ptr_t ustack_top;         // offset = 84 -> 56 -> 60 -> 12
-    ptr_t page_table;         // offset = 88 -> 60 -> 64 -> 16
+    pid_t pid;
+    struct proc_info* parent;
+    isr_param* intr_ctx;
+    ptr_t ustack_top;
+    ptr_t page_table;
 
     /* ---- critical section end ---- */
 
 
     /* ---- critical section end ---- */
 
@@ -108,7 +106,6 @@ struct proc_info
     u8_t state;
     int32_t exit_code;
     int32_t k_status;
     u8_t state;
     int32_t exit_code;
     int32_t k_status;
-    int flags;
     struct sighail sigctx;
     struct v_fdtable* fdtable;
     struct v_dnode* cwd;
     struct sighail sigctx;
     struct v_fdtable* fdtable;
     struct v_dnode* cwd;
@@ -195,4 +192,7 @@ get_process(pid_t pid);
 void
 proc_setsignal(struct proc_info* proc, int signum);
 
 void
 proc_setsignal(struct proc_info* proc, int signum);
 
+void
+proc_clear_signal(struct proc_info* proc);
+
 #endif /* __LUNAIX_PROCESS_H */
 #endif /* __LUNAIX_PROCESS_H */
index 80dee1aff37a88af8ba536fc08a82471dbfd3dbe..f308503c7aac3722a39f721afcae32f1462259e4 100644 (file)
 .section .text
     .global interrupt_wrapper
     interrupt_wrapper:
 .section .text
     .global interrupt_wrapper
     interrupt_wrapper:
-        /*
-         Stack layout (layout of struct isr_param)
-    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
-           ds         > offset = 8 * 4 = 32
-           esi
-           ebp
-           edi
-           edx
-           ecx
-           ebx
-           eax
-    lsa:   depth       > offset = 0
-
-            las: Least Significant Address
-            msa: Most Significant Address
-        */
         cld
 
         subl $4, %esp
         cld
 
         subl $4, %esp
         movw %ax, %ds
         movw %ax, %es
 
         movw %ax, %ds
         movw %ax, %es
 
-        movl __current, %eax
-
-        # 保存用户栈顶指针。因为我们允许同级中断的产生,所以需要该手段跟踪用户栈的地址。
-        movl iuesp(%esp), %ebx     # 取出esp
-        movl %ebx, proc_ustack_top(%eax)     # 存入__current->ustack_top
+        movl __current, %ebx
 
         # Save x87 context to user stack, rather than kernel's memory.
 
         # Save x87 context to user stack, rather than kernel's memory.
-        # XXX: what will happen if we triggered a page fault during fxsave?
+        # XXX what will happen if we triggered a page fault during fxsave?
+        # FIXME can we remove this overhead?
         movl iuesp(%esp), %eax
         andl $stack_alignment, %eax 
         subl $512, %eax
         fxsave (%eax)
 
         movl iuesp(%esp), %eax
         andl $stack_alignment, %eax 
         subl $512, %eax
         fxsave (%eax)
 
+        # 保存用户栈顶指针。因为我们允许同级中断的产生,所以需要该手段跟踪用户栈的地址。
+        movl %eax, proc_ustack_top(%ebx)     # 存入__current->ustack_top
+
     /* kernel space same-level switch */
     1:
         movl %esp, %eax
     /* kernel space same-level switch */
     1:
         movl %esp, %eax
         movl exeip(%eax), %eax
         movl %eax, (debug_resv + 4) # eip
 #endif
         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?
 
         movl ics(%esp), %eax
         andl $3, %eax
         jz 1f 
 
 
         movl ics(%esp), %eax
         andl $3, %eax
         jz 1f 
 
-        movl iuesp(%esp), %eax
-        andl $stack_alignment, %eax 
-        subl $512, %eax
+        movl __current, %eax
+        movl proc_ustack_top(%eax), %eax
+        test %eax, %eax
+        jz 1f
         fxrstor (%eax)
 
 1:
         fxrstor (%eax)
 
 1:
 
         movl %eax, tmp_store
         movl __current, %eax
 
         movl %eax, tmp_store
         movl __current, %eax
-        
 
         # nested intr: restore saved context
         popl proc_intr_ctx(%eax)
 
         # nested intr: restore saved context
         popl proc_intr_ctx(%eax)
         movl %ecx, (tss_esp0_off + _tss)
 
         jmp handle_signal
         movl %ecx, (tss_esp0_off + _tss)
 
         jmp handle_signal
+
     1:
         movl proc_intr_ctx(%ebx), %eax
         jmp soft_iret
     1:
         movl proc_intr_ctx(%ebx), %eax
         jmp soft_iret
index a8461269ec3d15d24431c8fb4bbe8e99764c12ab..21814d16c042ffb50e96fbe8d7ebb89cee846a80 100644 (file)
@@ -224,6 +224,10 @@ __DEFINE_LXSYSCALL3(int,
     execp->esp = container.stack_top;
     execp->eip = container.exe.entry;
 
     execp->esp = container.stack_top;
     execp->eip = container.exe.entry;
 
+    // these become meaningless once execved!
+    __current->ustack_top = 0;
+    proc_clear_signal(__current);
+
 done:
     // set return value
     store_retval(DO_STATUS(errno));
 done:
     // set return value
     store_retval(DO_STATUS(errno));
index d00e75755cf16136481e6e1213ebe7865d70abd4..8c4322f442a9fd33680b4ebbb6ba327fb91170c5 100644 (file)
@@ -77,6 +77,12 @@ signal_dispatch()
     return sigframe;
 }
 
     return sigframe;
 }
 
+void
+proc_clear_signal(struct proc_info* proc)
+{
+    memset(&proc->sigctx, 0, sizeof(proc->sigctx));
+}
+
 void
 proc_setsignal(struct proc_info* proc, int signum)
 {
 void
 proc_setsignal(struct proc_info* proc, int signum)
 {