⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 5 页
字号:
        }
		Print(OUTPUT_WINDOW,tempShell);
        dwCallOldGPFaultHandler = 1;
	}
	else if(dwReasonForBreak == REASON_CTRLF)
	{
        DPRINT((0,"REASON_CTRLF\n"));
        // nothing to do
    }
    else if(dwReasonForBreak == REASON_DOUBLE_FAULT)
    {
        DPRINT((0,"REASON_DOUBLE_FAULT\n"));

        PICE_sprintf(tempShell,"pICE: Breakpoint due to double fault at %.4X:%.8X\n",CurrentCS,CurrentEIP);
		Print(OUTPUT_WINDOW,tempShell);
    }
    else if(dwReasonForBreak == REASON_INTERNAL_ERROR)
    {
        DPRINT((0,"REASON_INTERNAL_ERROR\n"));

        Print(OUTPUT_WINDOW,"pICE: Please report this error to klauspg@diamondmm.com!\n");
//        Print(OUTPUT_WINDOW,"pICE: !!! SYSTEM HALTED !!!\n");
//        __asm__ __volatile__("hlt");
    }
    else
    {
        DPRINT((0,"REASON_UNKNOWN\n"));

        PICE_sprintf(tempShell,"pICE: Breakpoint due to unknown reason at %.4X:%.8X (code %x)\n",CurrentCS,CurrentEIP,dwReasonForBreak);
		Print(OUTPUT_WINDOW,tempShell);
        Print(OUTPUT_WINDOW,"pICE: Please report this error to klauspg@diamondmm.com!\n");
        Print(OUTPUT_WINDOW,"pICE: !!! SYSTEM HALTED !!!\n");
        __asm__ __volatile__("hlt");
    }

    // we don't single-step yet
    DPRINT((0,"RealIsr(): not stepping yet\n"));
	bSingleStep=FALSE;

    // process commands
    if(bSkipMainLoop == FALSE)
	{
        DPRINT((0,"RealIsr(): saving registers\n"));
	    // save the extended regs
	    __asm__ __volatile__
	    ("\n\t \
            pushl %eax\n\t \
		    movw %es,%ax\n\t \
		    movw %ax,_CurrentES\n\t \
		    //movw %fs,%ax\n\t \
		    //movw %ax,_CurrentFS\n\t \
		    movw %gs,%ax\n\t \
		    movw %ax,_CurrentGS\n\t \
		    movl %dr0,%eax\n\t \
		    movl %eax,_CurrentDR0\n\t \
		    movl %dr1,%eax\n\t \
		    movl %eax,_CurrentDR1\n\t \
		    movl %dr2,%eax\n\t \
		    movl %eax,_CurrentDR2\n\t \
		    movl %dr3,%eax\n\t \
		    movl %eax,_CurrentDR3\n\t \
		    movl %dr6,%eax\n\t \
		    movl %eax,_CurrentDR6\n\t \
		    movl %dr7,%eax\n\t \
		    movl %eax,_CurrentDR7\n\t \
		    movl %cr0,%eax\n\t \
		    movl %eax,_CurrentCR0\n\t \
		    movl %cr2,%eax\n\t \
		    movl %eax,_CurrentCR2\n\t \
		    movl %cr3,%eax\n\t \
		    movl %eax,_CurrentCR3\n\t \
            popl %eax"
	    );

		CurrentFS = OLD_PCR;
        DPRINT((0,"RealIsr(): adding colon to output()\n"));
        Print(OUTPUT_WINDOW,":");

        DPRINT((0,"RealIsr(): calling DebuggerShell()\n"));
        DebuggerShell();
	}

	// if there was a SW breakpoint at CS:EIP
    if(NeedToReInstallSWBreakpoints(GetLinearAddress(CurrentCS,CurrentEIP),TRUE) || ReinstallPermanentBp)
    {
        DPRINT((0,"need to reinstall INT3\n"));
		// remember how we restarted last time
        bPreviousCommandWasGo = !bSingleStep;
        // do a single step to reinstall breakpoint
	    // modify trace flag
	    CurrentEFL|=0x100; // set trace flag (TF)

	    bSingleStep=TRUE;
	    bNotifyToExit=TRUE;
    }

common_return_point:

    // reset the global flags
    bNotifyToExit = FALSE;
    bSkipMainLoop = FALSE;

    // not in handler anymore
    bInDebuggerShell = FALSE;

    LEAVE_FUNC();
	DPRINT((0,"common return-----------------------------------------------------------------\n"));
}


__asm__(".global NewInt31Handler\n\t \
NewInt31Handler:\n\t \
	cli\n\t \
    cld\n\t \
\n\t \
	pushl %eax\n\t \
	pushl %ds\n\t \
\n\t \
	movw %ss,%ax\n\t \
	mov %ax,%ds\n\t \
\n\t \
	mov 0x4(%esp),%eax\n\t \
	movl %eax,_CurrentEAX\n\t \
	movl %ebx,_CurrentEBX\n\t \
	movl %ecx,_CurrentECX\n\t \
	movl %edx,_CurrentEDX\n\t \
	movl %esi,_CurrentESI\n\t \
	movl %edi,_CurrentEDI\n\t \
	movl %ebp,_CurrentEBP\n\t \
	movl (%esp),%eax\n\t \
	movw %ax,_CurrentDS\n\t \
\n\t \
    // test for V86 mode\n\t \
	testl $0x20000,5*4(%esp)\n\t \
	jz notV86\n\t \
\n\t \
	int $0x03\n\t \
\n\t \
notV86:\n\t \
    // test if stack switched (ring3->ring0 transition)\n\t \
    // stack is switched if orig. SS is not global kernel code segment\n\t \
    movl 4*4(%esp),%eax\n\t \
    cmpw $" STR(GLOBAL_CODE_SEGMENT) ",%ax\n\t \
	je notswitched\n\t \
\n\t \
	// switched stack\n\t \
	movl 6*4(%esp),%eax\n\t \
	mov %eax,_CurrentESP\n\t \
	mov 7*4(%esp),%eax\n\t \
	movzwl %ax,%eax\n\t \
	mov %ax,_CurrentSS\n\t \
	jmp afterswitch\n\t \
\n\t \
notswitched:\n\t \
    // didn't switch stack\n\t \
	movl %esp,_CurrentESP\n\t \
	addl $24,_CurrentESP\n\t \
	movw %ss,%ax\n\t \
	movzwl %ax,%eax\n\t \
	mov %ax,_CurrentSS\n\t \
\n\t \
afterswitch:\n\t \
    // save EIP\n\t \
	mov 3*4(%esp),%eax\n\t \
	mov %eax,_CurrentEIP\n\t \
    //save CS\n\t \
	mov 4*4(%esp),%eax\n\t \
	movzwl %ax,%eax\n\t \
	movw %ax,_CurrentCS\n\t \
    // save flags\n\t \
	movl 5*4(%esp),%eax\n\t \
	andl $0xFFFFFEFF,%eax\n\t \
	movl %eax,_CurrentEFL\n\t \
\n\t \
	pushal\n\t \
\n\t \
    // get reason code\n\t \
    mov 0x28(%esp),%ebx\n\t \
\n\t \
	/*\n\t \
	 * Load the PCR selector.\n\t \
	 */\n\t \
\n\t \
	movl 	%fs, %eax\n\t \
	movl	%eax, _OLD_PCR\n\t \
	movl	_PCR_SEL, %eax\n\t \
	movl	%eax, %fs\n\t \
\n\t \
    // setup a large work stack\n\t \
	movl %esp,%eax\n\t \
	movl %eax,_ulRealStackPtr\n\t \
\n\t \
    pushl %ebx\n\t \
	call _RealIsr\n\t \
    addl $4,%esp\n\t \
\n\t \
	pushl 	%eax\n\t \
	movl	_OLD_PCR, %eax\n\t \
	movl	%eax, %fs\n\t \
	popl	%eax\n\t \
\n\t \
	// restore all regs\n\t \
	popal\n\t \
\n\t \
	// do an EOI to IRQ controller (because we definitely pressed some key)\n\t \
	// TODO: SMP APIC support\n\t \
	movb $0x20,%al\n\t \
	outb %al,$0x20\n\t \
\n\t \
	popl %ds\n\t \
	popl %eax\n\t \
\n\t \
    // remove reason code\n\t \
    addl $4,%esp\n\t \
\n\t \
    // make EAX available\n\t \
	pushl %eax\n\t \
\n\t \
	// modify or restore EFLAGS\n\t \
	.byte 0x2e\n\t \
	mov _CurrentEFL,%eax\n\t \
	mov %eax,3*4(%esp)\n\t \
	.byte 0x2e\n\t \
	movzwl _CurrentCS,%eax\n\t \
	mov %eax,2*4(%esp)\n\t \
	.byte 0x2e\n\t \
	mov _CurrentEIP,%eax\n\t \
	mov %eax,1*4(%esp)\n\t \
\n\t \
    // restore EAX\n\t \
	popl %eax\n\t \
\n\t \
	// do we need to call old INT1 handler\n\t \
    .byte 0x2e\n\t \
     cmp $0,_dwCallOldInt1Handler\n\t \
     je do_iret2\n\t \
\n\t \
    // call INT3 handler\n\t \
    .byte 0x2e\n\t \
     jmp *_OldInt1Handler\n\t \
\n\t \
do_iret2:\n\t \
    // do we need to call old INT3 handler\n\t \
    .byte 0x2e\n\t \
    cmp $0,_dwCallOldInt3Handler\n\t \
    je do_iret1\n\t \
\n\t \
    // call INT3 handler\n\t \
    .byte 0x2e\n\t \
    jmp *_OldInt3Handler\n\t \
\n\t \
do_iret1:\n\t \
    // do we need to call old pagefault handler\n\t \
    .byte 0x2e\n\t \
    cmp $0,_dwCallOldIntEHandler\n\t \
    je do_iret3\n\t \
\n\t \
    // call old pagefault handler\n\t \
	.byte 0x2e\n\t \
    pushl _error_code\n\t \
	.byte 0x2e\n\t \
    jmp *_OldIntEHandler\n\t \
\n\t \
do_iret3:\n\t \
    // do we need to call old general protection fault handler\n\t \
    .byte 0x2e\n\t \
    cmp $0,_dwCallOldGPFaultHandler\n\t \
    je do_iret\n\t \
\n\t \
    // call old pagefault handler\n\t \
	.byte 0x2e\n\t \
    pushl _error_code\n\t \
	.byte 0x2e\n\t \
    jmp *_OldGPFaultHandler\n\t \
\n\t \
do_iret:\n\t \
	//ei\n\t \
	//int3\n\t \
	iretl ");

//
// stub for entering via CTRL-F
//
// IDTs keyboard IRQ points here
//
__asm__ ("\n\t \
NewGlobalInt31Handler:\n\t \
		.byte 0x2e\n\t \
		cmpb $0,_bEnterNow\n\t \
		jne dotheenter\n\t \
\n\t \
        // chain to old handler\n\t \
		.byte 0x2e\n\t \
		jmp *_OldGlobalInt31Handler\n\t \
\n\t \
dotheenter:\n\t \
        pushl $" STR(REASON_CTRLF) "\n\t \
        jmp NewInt31Handler "
);

void InstallGlobalKeyboardHook(void)
{
	ULONG LocalNewGlobalInt31Handler;

	ENTER_FUNC();

	MaskIrqs();
	if(!OldGlobalInt31Handler)
	{
		__asm__("mov $NewGlobalInt31Handler,%0"
			:"=r" (LocalNewGlobalInt31Handler)
			:
			:"eax");
		OldGlobalInt31Handler=SetGlobalInt(KeyboardIRQL,(ULONG)LocalNewGlobalInt31Handler);
	}
	UnmaskIrqs();

    LEAVE_FUNC();
}

void DeInstallGlobalKeyboardHook(void)
{
    ENTER_FUNC();

	MaskIrqs();
	if(OldGlobalInt31Handler)
	{
		SetGlobalInt(KeyboardIRQL,(ULONG)OldGlobalInt31Handler);
		OldGlobalInt31Handler=0;
	}
	UnmaskIrqs();

    LEAVE_FUNC();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -