Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package emulator.editor;
- import emulator.editor.Export.MemoryImage;
- public abstract class ExportStructure {
- // TODO access:
- // fat pointer, string pointer (start,size)
- // array list
- //
- public static interface IField {
- }
- public final byte[] structure;
- public final int size;
- public ExportStructure(int size) {
- this.size = size;
- this.structure = new byte[size];
- }
- public static class InterruptDescriptorTableRegister extends ExportStructure {
- public enum Field implements IField {
- LIMIT, // in bytes
- BASE,
- }
- public InterruptDescriptorTableRegister() {
- super(6); // 48 bits
- //write(Field.LIMIT, 0x800); // 8 * 256
- }
- @Override
- public Object read(IField field, int start) {
- switch((Field) field) {
- case LIMIT:
- {
- int value = 0;
- value |= structure[0] << 0; // 0
- value |= structure[1] << 8; // 8
- return value;
- }
- case BASE:
- {
- int value = 0;
- value |= structure[2] << 0; // 16
- value |= structure[3] << 8; // 24
- value |= structure[4] << 16; // 32
- value |= structure[5] << 24; // 40
- return value;
- }
- default:
- assert false;
- return null;
- }
- }
- @Override
- public void write(IField field, Object object) {
- switch((Field) field) {
- case LIMIT:
- {
- int value = (Integer) object;
- structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0
- structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
- }
- break;
- case BASE:
- {
- int value = (Integer) object;
- structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16
- structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
- structure[4] = (byte) ((value & 0x00FF0000) >>> 16); // 32
- structure[5] = (byte) ((value & 0xFF000000) >>> 24); // 40
- }
- break;
- default:
- assert false;
- }
- }
- }
- public static class InterruptDescriptor extends ExportStructure {
- public enum Field implements IField {
- SEGMENT,
- OFFSET,
- TYPE_ATTR,
- }
- public static class TypeAttr {
- public int type = 0; // constant of Type
- public boolean storageSegment = false;
- public int privilege = 0; // 0 .. 3
- public boolean present = true;
- }
- public static class Type { // only 32-bit
- public static final int TASK_GATE = Integer.parseInt("00000101", 2); // 0x5
- public static final int INTERRUPT_GATE = Integer.parseInt("00001110", 2); // 0xE, interrupt gate, disable interrupts (fixed stack size)
- public static final int TRAP_GATE = Integer.parseInt("00001111", 2); // 0xF
- }
- public InterruptDescriptor() {
- super(8); // 64 bits
- structure[4] = 0; // 32
- }
- @Override
- public Object read(IField field, int start) {
- switch((Field) field) {
- case SEGMENT:
- {
- int value = 0;
- value |= structure[2] << 0; // 16
- value |= structure[3] << 8; // 24
- return value;
- }
- case OFFSET:
- {
- int value = 0;
- value |= structure[0] << 0; // 0: low offset
- value |= structure[1] << 8; // 8
- value |= structure[6] << 16; // 48: high offset
- value |= structure[7] << 24; // 56
- return value;
- }
- case TYPE_ATTR:
- {
- TypeAttr value = new TypeAttr();
- value.type = (structure[5] & (byte) Integer.parseInt("00001111", 2)) >>> 0; // 40 .. 43
- value.storageSegment = (structure[5] & (byte) Integer.parseInt("00010000", 2)) != 0; // 44
- value.privilege = (structure[5] & (byte) Integer.parseInt("01100000", 2)) >>> 5; // 45 .. 46
- value.present = (structure[5] & (byte) Integer.parseInt("10000000", 2)) != 0; // 47
- return value;
- }
- default:
- assert false;
- return null;
- }
- }
- @Override
- public void write(IField field, Object object) {
- switch((Field) field) {
- case SEGMENT:
- {
- int value = (Integer) object;
- structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16
- structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
- }
- break;
- case OFFSET:
- {
- int value = (Integer) object;
- structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0: low offset
- structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
- structure[6] = (byte) ((value & 0x00FF0000) >>> 16); // 48: high offset
- structure[7] = (byte) ((value & 0xFF000000) >>> 24); // 56
- }
- break;
- case TYPE_ATTR:
- {
- TypeAttr value = (TypeAttr) object;
- structure[5] = 0;
- structure[5] |= (value.type & 0x0000000F) << 0; // 40 .. 43
- structure[5] |= (value.storageSegment ? 1 : 0) << 4; // 44
- structure[5] |= (value.privilege & 0x00000003) << 5; // 45 .. 46
- structure[5] |= (value.present ? 1 : 0) << 7; // 47
- }
- break;
- default:
- assert false;
- }
- }
- }
- public static class GlobalDescriptorTableRegister extends ExportStructure {
- public enum Field implements IField {
- SIZE, // entry count
- LINEAR_ADDRESS,
- }
- public GlobalDescriptorTableRegister() {
- super(6); // 48 bits
- }
- @Override
- public Object read(IField field, int start) {
- switch((Field) field) {
- case SIZE:
- {
- int value = 0;
- value |= structure[0] << 0; // 0
- value |= structure[1] << 8; // 8
- value++;
- return value;
- }
- case LINEAR_ADDRESS:
- {
- int value = 0;
- value |= structure[2] << 0; // 16
- value |= structure[3] << 8; // 24
- value |= structure[4] << 16; // 32
- value |= structure[5] << 24; // 40
- return value;
- }
- default:
- assert false;
- return null;
- }
- }
- @Override
- public void write(IField field, Object object) {
- switch((Field) field) {
- case SIZE:
- {
- int value = (Integer) object;
- assert value > 0;
- value--;
- structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0
- structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
- }
- break;
- case LINEAR_ADDRESS:
- {
- int value = (Integer) object;
- structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16
- structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
- structure[4] = (byte) ((value & 0x00FF0000) >>> 16); // 32
- structure[5] = (byte) ((value & 0xFF000000) >>> 24); // 40
- }
- break;
- default:
- assert false;
- }
- }
- }
- public static class GlobalDescriptor extends ExportStructure {
- public enum Field implements IField {
- LIMIT,
- BASE,
- ACCESS_BYTE,
- FLAGS,
- }
- public static class AccessByte {
- public boolean accessed = false;
- public boolean readable_writable = false; // code can read (write disallowed regardless), data can write (read allowed regardless)
- public boolean direction_conforming = false; // data grows up, code specified privilege only
- public boolean executable = false;
- public int privilege = 3; // 0 .. 3
- public boolean present = true;
- }
- public static class Flags {
- public boolean size = true;
- public boolean granularity = true;
- }
- public GlobalDescriptor() {
- super(8); // 64 bits
- }
- @Override
- public Object read(IField field, int start) {
- switch((Field) field) {
- case LIMIT:
- {
- int value = 0;
- value |= structure[0] << 0; // 0: I
- value |= structure[1] << 8; // 8
- value |= (structure[6] & 0xF) << 16; // 48 .. 51: II
- return value;
- }
- case BASE:
- {
- int value = 0;
- value |= structure[2] << 0; // 16: I
- value |= structure[3] << 8; // 24
- value |= structure[4] << 16; // 32: II
- value |= structure[7] << 24; // 56: III
- return value;
- }
- case ACCESS_BYTE:
- {
- AccessByte value = new AccessByte();
- value.accessed = (structure[5] & (byte) Integer.parseInt("00000001", 2)) != 0; // 40
- value.readable_writable = (structure[5] & (byte) Integer.parseInt("00000010", 2)) != 0; // 41
- value.direction_conforming = (structure[5] & (byte) Integer.parseInt("00000100", 2)) != 0; // 42
- value.executable = (structure[5] & (byte) Integer.parseInt("00001000", 2)) != 0; // 43
- value.privilege = (structure[5] & (byte) Integer.parseInt("01100000", 2)) >>> 5; // 45 .. 46
- value.present = (structure[5] & (byte) Integer.parseInt("10000000", 2)) != 0; // 47
- return value;
- }
- case FLAGS:
- {
- Flags value = new Flags();
- value.size = (structure[6] & (byte) Integer.parseInt("01000000", 2)) != 0; // 54
- value.granularity = (structure[6] & (byte) Integer.parseInt("10000000", 2)) != 0; // 55
- return value;
- }
- default:
- assert false;
- return null;
- }
- }
- @Override
- public void write(IField field, Object object) {
- switch((Field) field) {
- case LIMIT:
- {
- int value = (Integer) object;
- structure[0] = (byte) ((value & 0x000000FF) >>> 0); // 0: I
- structure[1] = (byte) ((value & 0x0000FF00) >>> 8); // 8
- structure[6] &= 0xF0;
- structure[6] |= (byte) ((value & 0x000F0000) >>> 16); // 48 .. 51: II
- }
- break;
- case BASE:
- {
- int value = (Integer) object;
- structure[2] = (byte) ((value & 0x000000FF) >>> 0); // 16: I
- structure[3] = (byte) ((value & 0x0000FF00) >>> 8); // 24
- structure[4] = (byte) ((value & 0x00FF0000) >>> 16);// 32: II
- structure[7] = (byte) ((value & 0xFF000000) >>> 24); // 56: III
- }
- break;
- case ACCESS_BYTE:
- {
- AccessByte value = (AccessByte) object;
- structure[5] = 0;
- structure[5] |= (value.accessed ? 1 : 0) << 0; // 40
- structure[5] |= (value.readable_writable ? 1 : 0) << 1; // 41
- structure[5] |= (value.direction_conforming ? 1 : 0) << 2; // 42
- structure[5] |= (value.executable ? 1 : 0) << 3; // 43
- structure[5] |= 1 << 4; // 44
- structure[5] |= (value.privilege & 0x00000003) << 5; // 45 .. 46
- structure[5] |= (value.present ? 1 : 0) << 7; // 47
- }
- break;
- case FLAGS:
- {
- Flags value = (Flags) object;
- structure[6] &= 0x0F;
- structure[6] |= (value.size ? 1 : 0) << 6; // 54
- structure[6] |= (value.granularity ? 1 : 0) << 7; // 55
- }
- break;
- default:
- assert false;
- }
- }
- }
- public static class PageDirectoryEntry extends ExportStructure {
- public enum Field implements IField {
- FLAGS,
- CUSTOM,
- PHYSICAL_ADDRESS,
- }
- public static class Flags {
- public boolean present = true;
- public boolean read_write = false;
- public boolean user_supervisor = false;
- public boolean writeThrough = false;
- public boolean cacheDisabled = true;
- public boolean accessed = false;
- public boolean dirty = false;
- public boolean pageSize = false;
- public boolean global = false; // ignored for 4kb
- }
- public PageDirectoryEntry() {
- super(4); // 32 bits
- }
- @Override
- public Object read(IField field, int start) {
- switch((Field) field) {
- case FLAGS:
- {
- Flags value = new Flags();
- value.present = (structure[0] & (byte) Integer.parseInt("00000001", 2)) != 0; // 0
- value.read_write = (structure[0] & (byte) Integer.parseInt("00000010", 2)) != 0; // 1
- value.user_supervisor = (structure[0] & (byte) Integer.parseInt("00000100", 2)) != 0; // 2
- value.writeThrough = (structure[0] & (byte) Integer.parseInt("00001000", 2)) != 0; // 3
- value.cacheDisabled = (structure[0] & (byte) Integer.parseInt("00010000", 2)) != 0; // 4
- value.accessed = (structure[0] & (byte) Integer.parseInt("00100000", 2)) != 0; // 5
- value.dirty = (structure[0] & (byte) Integer.parseInt("01000000", 2)) != 0; // 6
- value.pageSize = (structure[0] & (byte) Integer.parseInt("10000000", 2)) != 0; // 7
- value.global = (structure[1] & (byte) Integer.parseInt("00000001", 2)) != 0; // 8
- return value;
- }
- case CUSTOM:
- {
- int value = 0;
- value |= (structure[1] & (byte) Integer.parseInt("00001110", 2)) >>> 1; // 9 .. 11
- return value;
- }
- case PHYSICAL_ADDRESS:
- {
- int value = 0;
- value |= (structure[1] & 0xF0) << 8; // 12 .. 15
- value |= structure[2] << 16; // 16
- value |= structure[3] << 24; // 24
- return value;
- }
- default:
- assert false;
- return null;
- }
- }
- @Override
- public void write(IField field, Object object) {
- switch((Field) field) {
- case FLAGS:
- {
- Flags value = (Flags) object;
- structure[0] = 0;
- structure[0] |= (value.present ? 1 : 0) << 0; // 0
- structure[0] |= (value.read_write ? 1 : 0) << 1; // 1
- structure[0] |= (value.user_supervisor ? 1 : 0) << 2; // 2
- structure[0] |= (value.writeThrough ? 1 : 0) << 3; // 3
- structure[0] |= (value.cacheDisabled ? 1 : 0) << 4; // 4
- structure[0] |= (value.accessed ? 1 : 0) << 5; // 5
- structure[0] |= (value.dirty ? 1 : 0) << 6; // 6
- structure[0] |= (value.pageSize ? 1 : 0) << 7; // 7
- structure[1] &= (byte) Integer.parseInt("11111110", 2);
- structure[1] |= (value.global ? 1 : 0) << 0; // 8
- }
- break;
- case CUSTOM:
- {
- int value = (Integer) object;
- structure[1] &= (byte) Integer.parseInt("11110001", 2);
- structure[1] |= (byte) ((value & (byte) Integer.parseInt("00000111", 2)) << 1); // 9 .. 11
- }
- break;
- case PHYSICAL_ADDRESS:
- {
- int value = (Integer) object;
- structure[1] &= 0x0F;
- structure[1] |= (byte) ((value & 0x0000F000) >>> 8); // 12 .. 15
- structure[2] = (byte) ((value & 0x00FF0000) >>> 16); // 16
- structure[3] = (byte) ((value & 0xFF000000) >>> 24); // 24
- }
- break;
- default:
- assert false;
- }
- }
- }
- /*public static class TaskSegmentDescriptor extends ExportStructure {
- public enum Field implements IField {
- LIMIT,
- BASE,
- FLAGS,
- }
- public TaskSegmentDescriptor(int size) {
- super(8);
- }
- @Override
- public Object read(IField field, int start) {
- return null;
- }
- @Override
- public void write(IField field, Object object) {
- }
- }
- public static class TaskSegment extends ExportStructure {
- public enum Field implements IField {
- PREVIOUS_TASK,
- ESP0,
- ESP1,
- ESP2,
- SS0,
- SS1,
- SS2,
- CR3,
- EIP,
- EFLAGS,
- EAX,
- ECX,
- EDX,
- EBX,
- ESP,
- EBP,
- ESI,
- EDI,
- ES,
- CS,
- SS,
- DS,
- FS,
- GS,
- LDT_SELECTOR,
- IO_MAP_BASE,
- }
- public TaskSegment(int size) {
- super(1234);
- }
- @Override
- public Object read(IField field, int start) {
- assert false;
- return null;
- }
- @Override
- public void write(IField field, Object object) {
- assert false;
- }
- }*/
- public abstract Object read(IField field, int start); // load structure field from memory
- public abstract void write(IField field, Object object); // write structure field to memory
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement