Kernel/x86_64 - Implemented UserRIP/CS
authorJohn Hodge <[email protected]>
Thu, 28 Jul 2011 13:11:36 +0000 (21:11 +0800)
committerJohn Hodge <[email protected]>
Thu, 28 Jul 2011 13:11:36 +0000 (21:11 +0800)
Kernel/arch/x86_64/desctab.asm
Kernel/arch/x86_64/include/mm_virt.h
Kernel/arch/x86_64/include/proc.h
Kernel/arch/x86_64/proc.c

index c575dd6..7ad93eb 100644 (file)
@@ -289,8 +289,10 @@ DEFIRQ     i
 [global IrqCommon]
 IrqCommon:
        PUSH_GPR
 [global IrqCommon]
 IrqCommon:
        PUSH_GPR
+       push gs
+       push fs
        
        
-       mov rbx, [rsp+16*8]     ; Calculate address
+       mov rbx, [rsp+(16+2)*8] ; Calculate address
        shr rbx, 3+2    ; *8*4
        mov rax, gaIRQ_Handlers
        add rbx, rax
        shr rbx, 3+2    ; *8*4
        mov rax, gaIRQ_Handlers
        add rbx, rax
@@ -321,6 +323,8 @@ IrqCommon:
        mov dx, 0x0020
        out dx, al
        
        mov dx, 0x0020
        out dx, al
        
+       pop fs
+       pop gs
        POP_GPR
        add rsp, 8*2
        ;xchg bx, bx
        POP_GPR
        add rsp, 8*2
        ;xchg bx, bx
@@ -328,8 +332,15 @@ IrqCommon:
 
 [extern Proc_Scheduler]
 [global SchedulerIRQ]
 
 [extern Proc_Scheduler]
 [global SchedulerIRQ]
+;
+; NOTE: Proc_Scheduler makes assumptions about the stack state when called 
+;
 SchedulerIRQ:
 SchedulerIRQ:
+       push 0  ; Error code
+       push 0  ; IRQNum
        PUSH_GPR
        PUSH_GPR
+       push gs
+       push fs
        ;PUSH_FPU
        ;PUSH_XMM
        
        ;PUSH_FPU
        ;PUSH_XMM
        
@@ -365,7 +376,10 @@ SchedulerIRQ:
        
        ;POP_XMM
        ;POP_FPU
        
        ;POP_XMM
        ;POP_FPU
+       pop fs
+       pop gs
        POP_GPR
        POP_GPR
+       add rsp, 2*8    ; Dummy error code and IRQ num
        iretq
 
 [section .data]
        iretq
 
 [section .data]
index dd520a2..ebb6923 100644 (file)
@@ -61,7 +61,7 @@
 #define MM_HWMAP_TOP   (MM_KERNEL_RANGE|(0xD000##00000000))
 #define MM_PPD_BASE    (MM_KERNEL_RANGE|(0xD000##00000000))
 #define MM_PPD_CFG     MM_PPD_BASE
 #define MM_HWMAP_TOP   (MM_KERNEL_RANGE|(0xD000##00000000))
 #define MM_PPD_BASE    (MM_KERNEL_RANGE|(0xD000##00000000))
 #define MM_PPD_CFG     MM_PPD_BASE
-#define MM_PPD_VFS     (MM_KERNEL_RANGE|(0xD008##00000000))
+#define MM_PPD_HANDLES         (MM_KERNEL_RANGE|(0xD008##00000000))
 #define MM_USER_CODE   (MM_KERNEL_RANGE|(0xD080##00000000))
 
 #define MM_PAGE_COUNTS (MM_KERNEL_RANGE|(0xE000##00000000))
 #define MM_USER_CODE   (MM_KERNEL_RANGE|(0xD080##00000000))
 
 #define MM_PAGE_COUNTS (MM_KERNEL_RANGE|(0xE000##00000000))
index 94d2127..93926f7 100644 (file)
@@ -16,12 +16,12 @@ typedef struct {
        Uint    FS, GS;
        
        Uint    RAX, RCX, RDX, RBX;
        Uint    FS, GS;
        
        Uint    RAX, RCX, RDX, RBX;
-    Uint       KernelRSP, RBP, RSI, RDI;
+       Uint    KernelRSP, RBP, RSI, RDI;
        Uint    R8,  R9,  R10, R11;
        Uint    R12, R13, R14, R15;
        
        Uint    R8,  R9,  R10, R11;
        Uint    R12, R13, R14, R15;
        
-    Uint       IntNum, ErrorCode;
-    Uint       RIP, CS;
+       Uint    IntNum, ErrorCode;
+       Uint    RIP, CS;
        Uint    RFlags, RSP, SS;
 } tRegs;
 
        Uint    RFlags, RSP, SS;
 } tRegs;
 
@@ -39,6 +39,7 @@ typedef struct sMemoryState
 typedef struct sTaskState
 {
        Uint    RIP, RSP, RBP;
 typedef struct sTaskState
 {
        Uint    RIP, RSP, RBP;
+       Uint64  UserRIP, UserCS;
 }      tTaskState;
 
 // === CONSTANTS ===
 }      tTaskState;
 
 // === CONSTANTS ===
index 99fa7e6..f2abb28 100644 (file)
@@ -742,6 +742,7 @@ void Proc_CallFaultHandler(tThread *Thread)
 
 void Proc_DumpThreadCPUState(tThread *Thread)
 {
 
 void Proc_DumpThreadCPUState(tThread *Thread)
 {
+       Log("  At %04x:%016llx", Thread->SavedState.UserCS, Thread->SavedState.UserRIP);
 }
 
 /**
 }
 
 /**
@@ -758,22 +759,31 @@ void Proc_Scheduler(int CPU)
        
        // Get current thread
        thread = gaCPUs[CPU].Current;
        
        // Get current thread
        thread = gaCPUs[CPU].Current;
-       
-       // Reduce remaining quantum and continue timeslice if non-zero
-       if(thread->Remaining--) return;
-       // Reset quantum for next call
-       thread->Remaining = thread->Quantum;
-       
-       // Get machine state
-       __asm__ __volatile__ ("mov %%rsp, %0":"=r"(rsp));
-       __asm__ __volatile__ ("mov %%rbp, %0":"=r"(rbp));
-       rip = GetRIP();
-       if(rip == SWITCH_MAGIC) return; // Check if a switch happened
-       
-       // Save machine state
-       thread->SavedState.RSP = rsp;
-       thread->SavedState.RBP = rbp;
-       thread->SavedState.RIP = rip;
+
+       if( thread )
+       {
+               tRegs   *regs;
+               // Reduce remaining quantum and continue timeslice if non-zero
+               if(thread->Remaining--) return;
+               // Reset quantum for next call
+               thread->Remaining = thread->Quantum;
+       
+               // Get machine state
+               __asm__ __volatile__ ("mov %%rsp, %0":"=r"(rsp));
+               __asm__ __volatile__ ("mov %%rbp, %0":"=r"(rbp));
+               rip = GetRIP();
+               if(rip == SWITCH_MAGIC) return; // Check if a switch happened
+               
+               // Save machine state
+               thread->SavedState.RSP = rsp;
+               thread->SavedState.RBP = rbp;
+               thread->SavedState.RIP = rip;
+               
+               // TODO: Make this more stable somehow
+               regs = (tRegs*)(rbp+(2+1)*8);   // RBP,Ret + CurThread
+               thread->SavedState.UserCS = regs->CS;
+               thread->SavedState.UserRIP = regs->RIP;
+       }
        
        // Get next thread
        thread = Threads_GetNextToRun(CPU, thread);
        
        // Get next thread
        thread = Threads_GetNextToRun(CPU, thread);

UCC git Repository :: git.ucc.asn.au