📄 processor.java
字号:
if (alignmentChecking) { if ((result.getType() & 0x18) == 0x10) // Should make this a data segment result.setAddressSpace(alignmentCheckedMemory); } return result; } finally { linearMemory.setSupervisor(isSup); } } public Segment getSegment(int segmentSelector, Segment local, Segment global) { boolean isSup = linearMemory.isSupervisor(); try { long segmentDescriptor = 0; linearMemory.setSupervisor(true); if ((segmentSelector & 0x4) != 0) segmentDescriptor = local.getQuadWord(segmentSelector & 0xfff8); else { if (segmentSelector < 0x4) return SegmentFactory.NULL_SEGMENT; segmentDescriptor = global.getQuadWord(segmentSelector & 0xfff8); } Segment result = SegmentFactory.createProtectedModeSegment(linearMemory, segmentSelector, segmentDescriptor); if (alignmentChecking) { if ((result.getType() & 0x18) == 0x10) // Should make this a data segment result.setAddressSpace(alignmentCheckedMemory); } return result; } finally { linearMemory.setSupervisor(isSup); } } public void reset() { resetTime = System.currentTimeMillis(); eax = ebx = ecx = edx = 0; edi = esi = ebp = esp = 0; edx = 0x00000633; // Pentium II Model 3 Stepping 3 interruptFlags = 0; currentPrivilegeLevel = 0; linearMemory.reset(); alignmentChecking = false; eip = 0x0000fff0; cr0 = CR0_CACHE_DISABLE | CR0_NOT_WRITETHROUGH | 0x10; cr2 = cr3 = cr4 = 0x0; dr0 = dr1 = dr2 = dr3 = 0x0; dr6 = 0xffff0ff0; dr7 = 0x00000700; eflagsCarry = eflagsParity = eflagsAuxiliaryCarry = eflagsZero = eflagsSign = eflagsTrap = eflagsInterruptEnable = false; carryCalculated = parityCalculated = auxiliaryCarryCalculated = zeroCalculated = signCalculated = true; eflagsDirection = eflagsOverflow = eflagsNestedTask = eflagsResume = eflagsVirtual8086Mode = false; overflowCalculated = true; eflagsAlignmentCheck = eflagsVirtualInterrupt = eflagsVirtualInterruptPending = eflagsID = false; eflagsIOPrivilegeLevel = 0; eflagsZero = true; //eflags = 0x00000020; eflagsInterruptEnableSoon = false; cs = createRealModeSegment(0xf000); ds = createRealModeSegment(0); ss = createRealModeSegment(0); es = createRealModeSegment(0); fs = createRealModeSegment(0); gs = createRealModeSegment(0); idtr = SegmentFactory.createDescriptorTableSegment(physicalMemory, 0, 0xFFFF); ldtr = SegmentFactory.NULL_SEGMENT; gdtr = SegmentFactory.createDescriptorTableSegment(physicalMemory, 0, 0xFFFF); tss = SegmentFactory.NULL_SEGMENT; modelSpecificRegisters.clear(); //Will need to set any MSRs here fpu.init(); } public long getClockCount() { return (System.currentTimeMillis() - resetTime) * 1000 * Processor.CLOCK_SPEED; } public final int getInstructionPointer() { return cs.translateAddressRead(eip); } public final void processRealModeInterrupts() { if (eflagsInterruptEnable) { if ((interruptFlags & IFLAGS_RESET_REQUEST) != 0) { reset(); return; } if ((interruptFlags & IFLAGS_HARDWARE_INTERRUPT) != 0) { interruptFlags &= ~IFLAGS_HARDWARE_INTERRUPT; int vector = interruptController.cpuGetInterrupt(); handleRealModeInterrupt(vector); } } eflagsInterruptEnable = eflagsInterruptEnableSoon; } public final void processProtectedModeInterrupts() { if (eflagsInterruptEnable) { if ((interruptFlags & IFLAGS_RESET_REQUEST) != 0) { reset(); return; } if ((interruptFlags & IFLAGS_HARDWARE_INTERRUPT) != 0) { interruptFlags &= ~IFLAGS_HARDWARE_INTERRUPT; handleHardProtectedModeInterrupt(interruptController.cpuGetInterrupt()); } } eflagsInterruptEnable = eflagsInterruptEnableSoon; } public final void processVirtual8086ModeInterrupts() { if (eflagsInterruptEnable) { if ((interruptFlags & IFLAGS_RESET_REQUEST) != 0) { reset(); return; } if ((interruptFlags & IFLAGS_HARDWARE_INTERRUPT) != 0) { interruptFlags &= ~IFLAGS_HARDWARE_INTERRUPT; if ((getCR4() & CR4_VIRTUAL8086_MODE_EXTENSIONS) != 0) throw new IllegalStateException(); else handleHardVirtual8086ModeInterrupt(interruptController.cpuGetInterrupt()); } } eflagsInterruptEnable = eflagsInterruptEnableSoon; } public final void handleRealModeException(int vector) { if (vector <= PROC_EXCEPTION_MAX) handleRealModeInterrupt(vector); } private final void handleRealModeInterrupt(int vector) { //System.out.println("Real Mode execption " + Integer.toHexString(vector)); // TODO: the following is for real-mode interrupts. // probably need to check mode here and act accordingly. //if ((esp & 0xffff) < 6 && (esp & 0xffff) != 0) if (((esp & 0xffff) < 6) && ((esp & 0xffff) > 0)) { throw new IllegalStateException("SS Processor Exception Thrown in \"handleInterrupt("+vector+")\""); //throw new ProcessorException(ProcessorException.SS); //maybe just change vector value } // hmm...an exception in an interrupt handler? maybe not... vector *= 4; int newEip = 0xffff & idtr.getWord(vector); int newSelector = 0xffff & idtr.getWord(vector+2); short sesp = (short) esp; sesp -= 2; int eflags = getEFlags() & 0xffff; ss.setWord(sesp & 0xffff, (short)eflags); eflagsInterruptEnable = false; eflagsInterruptEnableSoon = false; eflagsTrap = false; eflagsAlignmentCheck = false; sesp -= 2; ss.setWord(sesp & 0xffff, (short)cs.getSelector()); sesp -= 2; ss.setWord(sesp & 0xffff, (short)eip); esp = (0xFFFF0000 & esp) | (sesp & 0xFFFF); // read interrupt vector eip = newEip; if (!cs.setSelector(newSelector)) { cs = createRealModeSegment(newSelector); setCPL(0); } } public final void handleProtectedModeException(int vector, boolean hasErrorCode, int errorCode) { int savedESP = esp; int savedEIP = eip; Segment savedCS = cs; Segment savedSS = ss; try { followProtectedModeException(vector, hasErrorCode, errorCode, false, false); } catch (ProcessorException e) { e.printStackTrace(); //return cpu to original state esp = savedESP; eip = savedEIP; cs = savedCS; ss = savedSS; if (vector == PROC_EXCEPTION_DF) { System.err.println("Triple-Fault: Unhandleable, machine will halt!"); throw new IllegalStateException("Triple Fault " + e); } else if (e.combinesToDoubleFault(vector)){ System.err.println(vector); handleProtectedModeException(PROC_EXCEPTION_DF, true, 0); } else handleProtectedModeException(e.getVector(), e.hasErrorCode(), e.getErrorCode()); } } public final void handleSoftProtectedModeInterrupt(int vector) { int savedESP = esp; int savedEIP = eip; Segment savedCS = cs; Segment savedSS = ss; try { followProtectedModeException(vector, false, 0, false, true); } catch (ProcessorException e) { //return cpu to original state esp = savedESP; eip = savedEIP; cs = savedCS; ss = savedSS; //if (e.getVector() == PROC_EXCEPTION_DF) { //System.err.println("Triple-Fault: Unhandleable, machine will halt!"); //throw new IllegalStateException("Triple Fault"); //else handleProtectedModeException(e.getVector(), e.hasErrorCode(), e.getErrorCode()); } } public final void handleHardProtectedModeInterrupt(int vector) { int savedESP = esp; int savedEIP = eip; Segment savedCS = cs; Segment savedSS = ss; try { followProtectedModeException(vector, false, 0, true, false); } catch (ProcessorException e) { //return cpu to original state esp = savedESP; eip = savedEIP; cs = savedCS; ss = savedSS; //if (e.getVector() == PROC_EXCEPTION_DF) { //System.err.println("Triple-Fault: Unhandleable, machine will halt!"); //throw new IllegalStateException("Triple Fault"); //else handleProtectedModeException(e.getVector(), e.hasErrorCode(), e.getErrorCode()); } } private final void checkGate(Segment gate, int selector, boolean software) { if (software) { if (gate.getDPL() < currentPrivilegeLevel) throw new ProcessorException(PROC_EXCEPTION_GP, selector + 2, true); } if (!gate.isPresent()) throw new ProcessorException(PROC_EXCEPTION_NP, selector, true); } private final void followProtectedModeException(int vector, boolean hasErrorCode, int errorCode, boolean hardware, boolean software) { //System.out.println("protected Mode exception " + Integer.toHexString(vector)); //System.out.println(Integer.toHexString(cs.getBase()) +":" +Integer.toHexString(eip)); //Thread.dumpStack(); if (vector == PROC_EXCEPTION_PF) setCR2(linearMemory.getLastWalkedAddress()); int selector = vector << 3; //multiply by 8 to get offset into idt int EXT = hardware ? 1 : 0; Segment gate; boolean isSup = linearMemory.isSupervisor(); try { linearMemory.setSupervisor(true); long descriptor = idtr.getQuadWord(selector); gate = SegmentFactory.createProtectedModeSegment(linearMemory, selector, descriptor); } catch (ProcessorException e) { throw new ProcessorException(PROC_EXCEPTION_GP, selector + 2 + EXT, true); } finally { linearMemory.setSupervisor(isSup); } switch (gate.getType()) { default: System.err.println("Invalid Gate Type For Throwing Interrupt: 0x" + Integer.toHexString(gate.getType())); throw new ProcessorException(PROC_EXCEPTION_GP, selector + 2 + EXT, true); case 0x05: //Interrupt Handler: Task Gate System.out.println("Unimplemented Interrupt Handler: Task Gate"); throw new IllegalStateException("Unimplemented Interrupt Handler: Task Gate"); case 0x06: //Interrupt Handler: 16-bit Interrupt Gate System.out.println("Unimplemented Interrupt Handler: 16-bit Interrupt Gate"); throw new IllegalStateException("Unimplemented Interrupt Handler: 16-bit Interrupt Gate"); case 0x07: //Interrupt Handler: 16-bit Trap Gate System.out.println("Unimplemented Interrupt Handler: 16-bit Trap Gate"); throw new IllegalStateException("Unimplemented Interrupt Handler: 16-bit Trap Gate"); case 0x0e: //Interrupt Handler: 32-bit Interrupt Gate { SegmentFactory.InterruptGate32Bit interruptGate = ((SegmentFactory.InterruptGate32Bit)gate); checkGate(gate, selector, software); int targetSegmentSelector = interruptGate.getTargetSegment(); Segment targetSegment; try { targetSegment = getSegment(targetSegmentSelector); } catch (ProcessorException e) { throw new ProcessorException(PROC_EXCEPTION_GP, targetSegmentSelector + EXT, true); } if (targetSegment.getDPL() > currentPrivilegeLevel) throw new ProcessorException(PROC_EXCEPTION_GP, targetSegmentSelector + EXT, true); switch(targetSegment.getType()) { default: throw new ProcessorException(PROC_EXCEPTION_GP, targetSegmentSelector + EXT, true); case 0x18: //Code, Execute-Only case 0x19: //Code, Execute-Only, Accessed case 0x1a: //Code, Execute/Read case 0x1b: //Code, Execute/Read, Accessed { if (!targetSegment.isPresent()) throw new ProcessorException(PROC_EXCEPTION_NP, targetSegmentSelector + EXT, true); if (targetSegment.getDPL() < currentPrivilegeLevel) { //INTER-PRIVILEGE-LEVEL int newStackSelector = 0; int newESP = 0; if ((tss.getType() & 0x8) != 0) { int tssStackAddress = (targetSegment.getDPL() * 8) + 4; if ((tssStackAddress + 7) > tss.getLimit()) throw new ProcessorException(PROC_EXCEPTION_TS, tss.getSelector(), true); isSup = linearMemory.isSupervisor(); try { linearMemory.setSupervisor(true); newStackSelector = 0xffff & tss.getWord(tssStackAddress + 4); newESP = tss.getDoubleWord(tssStackAddress); } finally { linearMemory.setSupervisor(isSup);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -