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

📄 segmentfactory.java

📁 JPC: x86 PC Hardware Emulator. 牛津大学开发的一个纯JAVA的x86系统结构硬件模拟器。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }
    
    static final class ExecuteOnlyCodeSegment extends ReadOnlyProtectedModeSegment
    {
	public ExecuteOnlyCodeSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return DESCRIPTOR_TYPE_CODE_DATA | TYPE_CODE;
        }
    }

    static final class ExecuteReadAccessedCodeSegment extends DefaultProtectedModeSegment
    {
	public ExecuteReadAccessedCodeSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return DESCRIPTOR_TYPE_CODE_DATA | TYPE_CODE | TYPE_CODE_READABLE | TYPE_ACCESSED;
        }
    }

    static final class ExecuteReadCodeSegment extends DefaultProtectedModeSegment
    {
	public ExecuteReadCodeSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return DESCRIPTOR_TYPE_CODE_DATA | TYPE_CODE | TYPE_CODE_READABLE;
        }
    }

    static final class ExecuteOnlyConformingAccessedCodeSegment extends ReadOnlyProtectedModeSegment
    {
	public ExecuteOnlyConformingAccessedCodeSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return DESCRIPTOR_TYPE_CODE_DATA | TYPE_CODE | TYPE_CODE_CONFORMING | TYPE_ACCESSED;
        }
    }

    static final class ExecuteReadConformingAccessedCodeSegment extends DefaultProtectedModeSegment
    {
	public ExecuteReadConformingAccessedCodeSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return DESCRIPTOR_TYPE_CODE_DATA | TYPE_CODE | TYPE_CODE_CONFORMING | TYPE_CODE_READABLE | TYPE_ACCESSED;
        }
    }

    static final class ExecuteReadConformingCodeSegment extends DefaultProtectedModeSegment
    {
	public ExecuteReadConformingCodeSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return DESCRIPTOR_TYPE_CODE_DATA | TYPE_CODE | TYPE_CODE_CONFORMING | TYPE_CODE_READABLE;
        }
    }

    static public abstract class AbstractTSS extends ReadOnlyProtectedModeSegment
    {
        public AbstractTSS(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
        }

	public void saveCPUState(Processor cpu)
	{
	    int initialAddress = translateAddressWrite(0);
	    memory.setDoubleWord( initialAddress + 32,cpu.eip);
	    memory.setDoubleWord( initialAddress + 36,cpu.getEFlags());
	    memory.setDoubleWord( initialAddress + 40,cpu.eax);
	    memory.setDoubleWord( initialAddress + 44,cpu.ecx);
	    memory.setDoubleWord( initialAddress + 48,cpu.edx);
	    memory.setDoubleWord( initialAddress + 52,cpu.ebx);
	    memory.setDoubleWord( initialAddress + 56,cpu.esp);
	    memory.setDoubleWord( initialAddress + 60,cpu.ebp);
	    memory.setDoubleWord( initialAddress + 64,cpu.esi);
	    memory.setDoubleWord( initialAddress + 68,cpu.edi);
	    memory.setDoubleWord( initialAddress + 72,cpu.es.getSelector());
	    memory.setDoubleWord( initialAddress + 76,cpu.cs.getSelector());
	    memory.setDoubleWord( initialAddress + 80,cpu.ss.getSelector());
	    memory.setDoubleWord( initialAddress + 84,cpu.ds.getSelector());
	    memory.setDoubleWord( initialAddress + 88,cpu.fs.getSelector());
	    memory.setDoubleWord( initialAddress + 92,cpu.gs.getSelector());
	}
	
	public void restoreCPUState(Processor cpu)
	{
	    int initialAddress = translateAddressRead(0);
	    cpu.eip = memory.getDoubleWord( initialAddress + 32);
	    cpu.setEFlags(memory.getDoubleWord( initialAddress + 36));
	    cpu.eax = memory.getDoubleWord( initialAddress + 40);
	    cpu.ecx = memory.getDoubleWord( initialAddress + 44);
	    cpu.edx = memory.getDoubleWord( initialAddress + 48);
	    cpu.ebx = memory.getDoubleWord( initialAddress + 52);
	    cpu.esp = memory.getDoubleWord( initialAddress + 56);
	    cpu.ebp = memory.getDoubleWord( initialAddress + 60);
	    cpu.esi = memory.getDoubleWord( initialAddress + 64);
	    cpu.edi = memory.getDoubleWord( initialAddress + 68);
	    cpu.es = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 72));
	    cpu.cs = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 76));
	    cpu.ss = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 80));
	    cpu.ds = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 84));
	    cpu.fs = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 88));
	    cpu.gs = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 92));
	    // non dynamic fields
	    cpu.ldtr = cpu.getSegment(0xFFFF & memory.getDoubleWord( initialAddress + 96));
	    cpu.setCR3(memory.getDoubleWord( initialAddress + 28));
	}

        public byte getByte(int offset)
        {
	    boolean isSup = ((LinearAddressSpace)memory).isSupervisor();
	    try {
		((LinearAddressSpace)memory).setSupervisor(true);
		return super.getByte(offset);
	    } finally {
		((LinearAddressSpace)memory).setSupervisor(isSup);
	    }
        }
    
        public short getWord(int offset)
        {
	    boolean isSup = ((LinearAddressSpace)memory).isSupervisor();
	    try {
		((LinearAddressSpace)memory).setSupervisor(true);
		return super.getWord(offset);
	    } finally {
		((LinearAddressSpace)memory).setSupervisor(isSup);
	    }
        }

        public int getDoubleWord(int offset)
        {
	    boolean isSup = ((LinearAddressSpace)memory).isSupervisor();
	    try {
		((LinearAddressSpace)memory).setSupervisor(true);
		return super.getDoubleWord(offset);
	    } finally {
		((LinearAddressSpace)memory).setSupervisor(isSup);
	    }
        }

        public long getQuadWord(int offset)
        {
	    boolean isSup = ((LinearAddressSpace)memory).isSupervisor();
	    try {
		((LinearAddressSpace)memory).setSupervisor(true);
		return super.getQuadWord(offset);
	    } finally {
		((LinearAddressSpace)memory).setSupervisor(isSup);
	    }
        }

    }

    static final class Available32BitTSS extends AbstractTSS
    {
	public Available32BitTSS(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x09;
        }
    }

    static final class Busy32BitTSS extends AbstractTSS
    {
	public Busy32BitTSS(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x0b;
        }
    }

    static final class LDT extends ReadOnlyProtectedModeSegment
    {
	public LDT(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x02;
        }
    }

    public static class GateSegment extends ReadOnlyProtectedModeSegment
    {
        private int targetSegment, targetOffset;

        public GateSegment(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
            
            targetSegment = (int)((descriptor >> 16) & 0xffff);
            targetOffset = (int)((descriptor & 0xffff) | ((descriptor >>> 32) & 0xffff0000));
	}

	public int getTargetSegment()
	{
	    return targetSegment;
	}

	public int getTargetOffset()
	{
	    return targetOffset;
	}
    }

    static final class TaskGate extends GateSegment
    {
        public TaskGate(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

	public final int getTargetOffset()
	{
            throw new IllegalStateException();
	}

	public int getType()
	{
	    return 0x05;
	}
    }

    static final class InterruptGate32Bit extends GateSegment
    {
	public InterruptGate32Bit(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x0e;
        }
    }

    static final class InterruptGate16Bit extends GateSegment
    {
	public InterruptGate16Bit(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x06;
        }
    }

    static final class TrapGate32Bit extends GateSegment
    {
	public TrapGate32Bit(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x0f;
        }
    }

    static final class TrapGate16Bit extends GateSegment
    {
	public TrapGate16Bit(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
	}

        public int getType()
        {
            return 0x07;
        }
    }

    public static final class CallGate32Bit extends GateSegment
    {
        private int parameterCount;

	public CallGate32Bit(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
            parameterCount = (int) ((descriptor >> 32) & 0xF);
	}

        public int getType()
        {
            return 0x0c;
        }

        public final int getParameterCount()
        {
            return parameterCount;
        }
    }

    public static final class CallGate16Bit extends GateSegment
    {
        private int parameterCount;

	public CallGate16Bit(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
            parameterCount = (int) ((descriptor >> 32) & 0xF);
	}

        public int getType()
        {
            return 0x04;
        }

        public final int getParameterCount()
        {
            return parameterCount;
        }
    }

    static final class Available16BitTSS extends DefaultProtectedModeSegment
    {
        public Available16BitTSS(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
        }

        public int getType()
        {
            return 0x01;
        }
    }

    static final class Busy16BitTSS extends DefaultProtectedModeSegment
    {
        public Busy16BitTSS(Memory memory, int selector, long descriptor)
	{
            super(memory, selector, descriptor);
        }

        public int getType()
        {
            return 0x03;
        }
    }

    static final class NullSegment extends DefaultSegment
    {
	public NullSegment()
	{
	    super(null);
	}

        public int dumpState(DataOutput output)  throws IOException
        {
            output.writeInt(4); 
            return 0;
        }

        public void loadState(DataInput input) {}

	public int getType()
	{
	    throw new ProcessorException(Processor.PROC_EXCEPTION_GP, 0, true);
	}

	public int getSelector()
	{
	    return 0;
	}

        public void checkAddress(int offset)
	{
	    throw new ProcessorException(Processor.PROC_EXCEPTION_GP, 0, true);
	}

        public int translateAddressRead(int offset)
	{
	    throw new ProcessorException(Processor.PROC_EXCEPTION_GP, 0, true);
	}

        public int translateAddressWrite(int offset)
	{
	    throw new ProcessorException(Processor.PROC_EXCEPTION_GP, 0, true);
	}

        public void invalidateAddress(int offset)
	{
	    throw new ProcessorException(Processor.PROC_EXCEPTION_GP, 0, true);
	}
    }

    public static Segment createProtectedModeSegment(Memory memory, int selector, long descriptor)
    {
	switch ((int)((descriptor & (DESCRIPTOR_TYPE | SEGMENT_TYPE)) >>> 40)) {

	    // System Segments 
	default:
	case 0x00: //Reserved
	case 0x08: //Reserved
	case 0x0a: //Reserved
	case 0x0d: //Reserved
            System.out.println(Integer.toHexString(selector)+"  "+Long.toString(descriptor, 16));
	    throw new IllegalStateException("Attempted To Construct Reserved Segment Type");
	case 0x01: //System Segment: 16-bit TSS (Available)
            return new Available16BitTSS(memory, selector, descriptor);
	case 0x02: //System Segment: LDT
	    return new LDT(memory, selector, descriptor);
	case 0x03: //System Segment: 16-bit TSS (Busy)
            return new Busy16BitTSS(memory, selector, descriptor);
	case 0x04: //System Segment: 16-bit Call Gate
            return new CallGate16Bit(memory, selector, descriptor);
	case 0x05: //System Segment: Task Gate
            return new TaskGate(memory, selector, descriptor);
	case 0x06: //System Segment: 16-bit Interrupt Gate
            return new InterruptGate16Bit(memory, selector, descriptor);
	case 0x07: //System Segment: 16-bit Trap Gate
            return new TrapGate16Bit(memory, selector, descriptor);
	case 0x09: //System Segment: 32-bit TSS (Available)
	    return new Available32BitTSS(memory, selector, descriptor);
	case 0x0b: //System Segment: 32-bit TSS (Busy)
	    return new Busy32BitTSS(memory, selector, descriptor);
	case 0x0c: //System Segment: 32-bit Call Gate
            return new CallGate32Bit(memory, selector, descriptor);
	case 0x0e: //System Segment: 32-bit Interrupt Gate
	    return new InterruptGate32Bit(memory, selector, descriptor);
	case 0x0f: //System Segment: 32-bit Trap Gate
	    return new TrapGate32Bit(memory, selector, descriptor);
	
	    // Code and Data Segments
	case 0x10: //Data Segment: Read-Only
	    return new ReadOnlyDataSegment(memory, selector, descriptor);
	case 0x11: //Data Segment: Read-Only, Accessed
	    return new ReadOnlyAccessedDataSegment(memory, selector, descriptor);
	case 0x12: //Data Segment: Read/Write
	    return new ReadWriteDataSegment(memory, selector, descriptor);
	case 0x13: //Data Segment: Read/Write, Accessed
            return new ReadWriteAccessedDataSegment(memory, selector, descriptor);
	case 0x14: //Data Segment: Read-Only, Expand-Down
	    throw new IllegalStateException("Unimplemented Data Segment: Read-Only, Expand-Down");
	case 0x15: //Data Segment: Read-Only, Expand-Down, Accessed
	    throw new IllegalStateException("Unimplemented Data Segment: Read-Only, Expand-Down, Accessed");
	case 0x16: //Data Segment: Read/Write, Expand-Down
	    throw new IllegalStateException("Unimplemented Data Segment: Read/Write, Expand-Down");
	case 0x17: //Data Segment: Read/Write, Expand-Down, Accessed
	    throw new IllegalStateException("Unimplemented Data Segment: Read/Write, Expand-Down, Accessed");

	case 0x18: //Code, Execute-Only
	    return new ExecuteOnlyCodeSegment(memory, selector, descriptor);
	case 0x19: //Code, Execute-Only, Accessed
	    throw new IllegalStateException("Unimplemented Code Segment: Execute-Only, Accessed");
	case 0x1a: //Code, Execute/Read
            return new ExecuteReadCodeSegment(memory, selector, descriptor);
	case 0x1b: //Code, Execute/Read, Accessed
            return new ExecuteReadAccessedCodeSegment(memory, selector, descriptor);
	case 0x1c: //Code: Execute-Only, Conforming
	    throw new IllegalStateException("Unimplemented Code Segment: Execute-Only, Conforming");
	case 0x1d: //Code: Execute-Only, Conforming, Accessed
	    return new ExecuteOnlyConformingAccessedCodeSegment(memory, selector, descriptor);
	case 0x1e: //Code: Execute/Read, Conforming
	    return new ExecuteReadConformingCodeSegment(memory, selector, descriptor);
	case 0x1f: //Code: Execute/Read, Conforming, Accessed
	    return new ExecuteReadConformingAccessedCodeSegment(memory, selector, descriptor);
	}
    }
}

⌨️ 快捷键说明

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