From: <mic...@us...> - 2007-04-11 21:25:11
|
Revision: 25 http://svn.sourceforge.net/pearcolator/?rev=25&view=rev Author: michael_baer Date: 2007-04-11 14:25:11 -0700 (Wed, 11 Apr 2007) Log Message: ----------- Moved memory access functions from interface LinuxSystemCallGenerator to the Memory class Modified Paths: -------------- src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java src/org/binarytranslator/arch/ppc/os/process/PPC_ProcessSpace.java src/org/binarytranslator/arch/ppc/os/process/linux/PPC_LinuxProcessSpace.java src/org/binarytranslator/arch/x86/os/process/linux/X86_LinuxProcessSpace.java src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCallGenerator.java src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCalls.java src/org/binarytranslator/generic/os/process/ProcessSpace.java Modified: src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java =================================================================== --- src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java 2007-04-11 21:25:11 UTC (rev 25) @@ -231,7 +231,7 @@ */ public PPC_InstructionDecoder interpretInstruction(PPC_ProcessSpace ps) throws BadInstructionException { - ps.currentInstruction = ps.memoryLoad32(ps.getCurrentInstructionAddress()); + ps.currentInstruction = ps.memory.load32(ps.getCurrentInstructionAddress()); try { return getDecoder(ps.currentInstruction).interpretInstruction(ps); } catch (NullPointerException e) { @@ -246,7 +246,7 @@ protected static PPC_InstructionDecoder moveInstructionOnAndReturnDecoder( PPC_ProcessSpace ps) { ps.setCurrentInstructionAddress(ps.getCurrentInstructionAddress() + 4); - ps.currentInstruction = ps.memoryLoad32(ps.getCurrentInstructionAddress()); + ps.currentInstruction = ps.memory.load32(ps.getCurrentInstructionAddress()); return findDecoder(ps.currentInstruction); } @@ -264,7 +264,7 @@ */ public static int translateInstruction(PPC2IR ppc2ir, PPC_ProcessSpace ps, PPC_Laziness lazy, int pc) { - int instr = ps.memoryLoad32(pc); + int instr = ps.memory.load32(pc); if (DBT_Options.debugInstr) { System.out.println(lazy.makeKey(pc) + PPC_Disassembler.disasm(instr, pc) @@ -3593,7 +3593,7 @@ int d = EXTS(bits(ps.currentInstruction, 16, 31), 16); int EA = (rA == 0) ? d : (ps.getRegister(rA) + d); int value = Float.floatToIntBits((float) ps.getFPregister(frS)); - ps.memoryStore32(EA, value); + ps.memory.store32(EA, value); return moveInstructionOnAndReturnDecoder(ps); } @@ -3654,7 +3654,7 @@ int d = EXTS(bits(ps.currentInstruction, 16, 31), 16); int EA = (rA == 0) ? d : (ps.getRegister(rA) + d); int value = Float.floatToIntBits((float) ps.getFPregister(frS)); - ps.memoryStore32(EA, value); + ps.memory.store32(EA, value); ps.setRegister(rA, EA); return moveInstructionOnAndReturnDecoder(ps); } @@ -3965,7 +3965,7 @@ } } ps.setCurrentInstructionAddress(target_address); - ps.currentInstruction = ps.memoryLoad32(target_address); + ps.currentInstruction = ps.memory.load32(target_address); return findDecoder(ps.currentInstruction); } @@ -4626,7 +4626,7 @@ } } ps.setCurrentInstructionAddress(target_address); - ps.currentInstruction = ps.memoryLoad32(target_address); + ps.currentInstruction = ps.memory.load32(target_address); return findDecoder(ps.currentInstruction); } @@ -11662,7 +11662,7 @@ } } ps.setCurrentInstructionAddress(target_address); - ps.currentInstruction = ps.memoryLoad32(target_address); + ps.currentInstruction = ps.memory.load32(target_address); return findDecoder(ps.currentInstruction); } @@ -11806,7 +11806,7 @@ .getCurrentInstructionAddress() + 4, target_address); } ps.setCurrentInstructionAddress(target_address); - ps.currentInstruction = ps.memoryLoad32(target_address); + ps.currentInstruction = ps.memory.load32(target_address); return findDecoder(ps.currentInstruction); } Modified: src/org/binarytranslator/arch/ppc/os/process/PPC_ProcessSpace.java =================================================================== --- src/org/binarytranslator/arch/ppc/os/process/PPC_ProcessSpace.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/arch/ppc/os/process/PPC_ProcessSpace.java 2007-04-11 21:25:11 UTC (rev 25) @@ -992,7 +992,7 @@ * Run a single instruction */ public void runOneInstruction() throws BadInstructionException { - currentInstruction = memoryLoad32(pc); + currentInstruction = memory.load32(pc); try { PPC_InstructionDecoder.findDecoder(currentInstruction) .interpretInstruction(this); Modified: src/org/binarytranslator/arch/ppc/os/process/linux/PPC_LinuxProcessSpace.java =================================================================== --- src/org/binarytranslator/arch/ppc/os/process/linux/PPC_LinuxProcessSpace.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/arch/ppc/os/process/linux/PPC_LinuxProcessSpace.java 2007-04-11 21:25:11 UTC (rev 25) @@ -16,6 +16,7 @@ import org.binarytranslator.generic.os.abi.linux.LinuxSystemCallGenerator; import org.binarytranslator.generic.os.abi.linux.LinuxSystemCalls; import org.binarytranslator.generic.os.loader.Loader; +import org.binarytranslator.generic.os.process.ProcessSpace; /** * Simulate the process address space for our PowerPC ELF binary. Also keep @@ -203,4 +204,8 @@ return false; } + public ProcessSpace getProcessSpace() { + return this; + } + } Modified: src/org/binarytranslator/arch/x86/os/process/linux/X86_LinuxProcessSpace.java =================================================================== --- src/org/binarytranslator/arch/x86/os/process/linux/X86_LinuxProcessSpace.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/arch/x86/os/process/linux/X86_LinuxProcessSpace.java 2007-04-11 21:25:11 UTC (rev 25) @@ -16,6 +16,7 @@ import org.binarytranslator.generic.os.abi.linux.LinuxSystemCallGenerator; import org.binarytranslator.generic.os.abi.linux.LinuxSystemCalls; import org.binarytranslator.generic.os.loader.Loader; +import org.binarytranslator.generic.os.process.ProcessSpace; /** * Linux specific parts of the process @@ -166,4 +167,8 @@ public byte[] getPlatformString() { return new byte[] {'\0', '6', '8', '6', 'i'}; } + + public ProcessSpace getProcessSpace() { + return this; + } } Modified: src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCallGenerator.java =================================================================== --- src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCallGenerator.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCallGenerator.java 2007-04-11 21:25:11 UTC (rev 25) @@ -9,6 +9,7 @@ package org.binarytranslator.generic.os.abi.linux; import org.binarytranslator.generic.memory.MemoryMapException; +import org.binarytranslator.generic.os.process.ProcessSpace; /** * Class encapsulating the interface between Linux system calls and @@ -56,39 +57,13 @@ * @param address where to write * @param data value to store */ - public void memoryStore32(int address, int data); + /** - * Write to the memory of the system call generator an 8bit value - * @param address where to write - * @param data value to store - */ - public void memoryStore8(int address, byte data); + * Returns the process space that this call originated from. + */ + public ProcessSpace getProcessSpace(); + /** - * Load from memory of the system call generator an 8bit value - * @param address where to read - * @return value read - */ - public byte memoryLoad8(int address); - /** - * Load from memory of the system call generator a 32bit value - * @param address where to read - * @return value read - */ - public int memoryLoad32(int address); - /** - * Load an ASCIIZ string from the memory of the system call - * generator and return it as a Java String. - * @param address where to read - * @return the String read - */ - public String memoryReadString(int address); - /** - * Store an ASCIIZ string to the memory of the system call generator - * @param address where to read - * @param data the String to write - */ - public void memoryWriteString(int address, String data); - /** * Get the top of the BSS segment (the heap that reside below the * stack in memory) * @return top of BSS segment @@ -100,13 +75,4 @@ * @param address new top of BSS segment */ public void setBrk(int address); - /** - * Map an anonymous page of memory - * @param addr the address to map or NULL if don't care - * @param len the amount of memory to map - * @param read is the page readable - * @param write is the page writable - * @param exec is the page executable - */ - public int memoryMap(int addr, int len, boolean read, boolean write, boolean exec) throws MemoryMapException; } Modified: src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCalls.java =================================================================== --- src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCalls.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/generic/os/abi/linux/LinuxSystemCalls.java 2007-04-11 21:25:11 UTC (rev 25) @@ -10,6 +10,7 @@ import java.io.*; import org.binarytranslator.DBT_Options; +import org.binarytranslator.generic.memory.Memory; import org.binarytranslator.generic.memory.MemoryMapException; import java.util.ArrayList; import java.net.InetAddress; @@ -62,7 +63,44 @@ * List of (RandomAccessFile(s)) files currently open */ private ArrayList<Object> files; + + /** + * Load an ASCIIZ string from the memory of the system call + * generator and return it as a Java String. + * @param address where to read + * @return the String read + */ + private String memoryReadString(int address) { + Memory m = src.getProcessSpace().memory; + + StringBuffer str = new StringBuffer(); + char c; + + while ((c = (char) m.loadUnsigned8(address++)) != 0) + str.append(c); + + return str.toString(); + } + + /** + * Store an ASCIIZ string to the memory of the system call generator + * @param address where to read + * @param data the String to write + */ + public void memoryWriteString(int address, String data) { + Memory m = src.getProcessSpace().memory; + + if (data != null) { + for (int i = 0; i < data.length(); i++) { + m.store8(address + i, (byte) data.charAt(i)); + } + + m.store8(address + data.length(), (byte) 0); + } + } + + /** * Convert integer file descriptor into Java RandomAccessFile */ private RandomAccessFile getRAFile(int fd) { @@ -489,13 +527,15 @@ int fd = arguments.nextInt(); int buf = arguments.nextInt(); int count = arguments.nextInt(); + + Memory mem = src.getProcessSpace().memory; if(fd == 0) { // read from stdin byte[] b = new byte[256]; try { int len = System.in.read(b); for (int i=0; i < len; i++) { - src.memoryStore32(buf + i, b[i]); + mem.store32(buf + i, b[i]); } src.setSysCallReturn(len); } @@ -518,7 +558,7 @@ while((b < count) && ((i = raFile.read()) != -1)) { byte by = (byte)i; b++; - src.memoryStore8(addr++, by); + mem.store8(addr++, by); } src.setSysCallReturn(b); // Return number of bytes read. } @@ -538,15 +578,17 @@ int fd = arguments.nextInt(); int buf = arguments.nextInt(); int count = arguments.nextInt(); + + Memory mem = src.getProcessSpace().memory; if(fd == 1) { // stdout for(int c = 0 ; c < count; c++) { - System.out.print((char) src.memoryLoad8(buf + c)); + System.out.print((char) mem.loadUnsigned8(buf + c)); } src.setSysCallReturn(count); } else if(fd == 2) { // sterr for(int c = 0 ; c < count ; c++) { - System.err.print((char) src.memoryLoad8(buf + c)); + System.err.print((char) mem.loadUnsigned8(buf + c)); } src.setSysCallReturn(count); } else { @@ -564,7 +606,7 @@ try { for(b = 1 ; b <= count ; b++) { - by = src.memoryLoad8(addr++); + by = (byte) mem.loadUnsigned8(addr++); raFile.write(by); } // Return number of bytes written, having accounted for b @@ -588,20 +630,21 @@ int fd = arguments.nextInt(); int vector = arguments.nextInt(); int count = arguments.nextInt(); + Memory mem = src.getProcessSpace().memory; if((fd == 1)||(fd == 2)) { // stdout || stderr - int base = src.memoryLoad32(vector); - int len = src.memoryLoad32(vector+4); + int base = mem.load32(vector); + int len = mem.load32(vector+4); int currentVector = 0; int curVectorPos = 0; for(int c = 0 ; c < count; c++) { if(curVectorPos == len) { currentVector++; - base = src.memoryLoad32(vector+(currentVector*8)); - len = src.memoryLoad32(vector+(currentVector*8)+4); + base = mem.load32(vector+(currentVector*8)); + len = mem.load32(vector+(currentVector*8)+4); curVectorPos = 0; } - System.out.print((char) src.memoryLoad8(base + curVectorPos)); + System.out.print((char) mem.loadUnsigned8(base + curVectorPos)); curVectorPos++; } src.setSysCallReturn(count); @@ -618,7 +661,7 @@ // Examine the flags argument and open read or read-write // accordingly. args[0] points to the file name. - String fileName = src.memoryReadString(pathname); + String fileName = memoryReadString(pathname); // Create a File object so we can test for the existance and // properties of the file. @@ -760,12 +803,12 @@ hostName = localhostString.substring(0,index); } // Fill in utsname struct - see /usr/include/sys/utsname.h - src.memoryWriteString (addr, getSysName()); // sysname - src.memoryWriteString (addr+65, hostName); // nodename - src.memoryWriteString (addr+130, getRelease()); // release - src.memoryWriteString (addr+195, getVersion()); // version - src.memoryWriteString (addr+260, getMachine()); // machine - src.memoryWriteString (addr+325, domainName); // __domainname + memoryWriteString (addr, getSysName()); // sysname + memoryWriteString (addr+65, hostName); // nodename + memoryWriteString (addr+130, getRelease()); // release + memoryWriteString (addr+195, getVersion()); // version + memoryWriteString (addr+260, getMachine()); // machine + memoryWriteString (addr+325, domainName); // __domainname src.setSysCallReturn(0); } else { @@ -786,7 +829,9 @@ int offset = arguments.nextInt(); if((flags & mman.MAP_ANONYMOUS) != 0 ) { try { - src.setSysCallReturn(src.memoryMap(start, length, + Memory mem = src.getProcessSpace().memory; + + src.setSysCallReturn( mem.map(start, length, (prot & mman.PROT_READ) != 0, (prot & mman.PROT_WRITE) != 0, (prot & mman.PROT_EXEC) != 0)); Modified: src/org/binarytranslator/generic/os/process/ProcessSpace.java =================================================================== --- src/org/binarytranslator/generic/os/process/ProcessSpace.java 2007-04-11 13:39:17 UTC (rev 24) +++ src/org/binarytranslator/generic/os/process/ProcessSpace.java 2007-04-11 21:25:11 UTC (rev 25) @@ -67,34 +67,6 @@ public Memory memory; /** - * Load a 32bit value from memory - */ - public int memoryLoad32(int wordAddr) { - return memory.load32(wordAddr); - } - - /** - * Store a 32bit value to memory - */ - public void memoryStore32(int address, int data) { - memory.store32(address, data); - } - - /** - * Load a 16bit value from memory - */ - public int memoryLoad16(int hwAddr) { - return memory.loadSigned16(hwAddr); - } - - /** - * Store a 16bit value to memory - */ - public void memoryStore16(int hwAddr, int iValue) { - memory.store16(hwAddr, iValue); - } - - /** * Load a 8bit value from memory */ public byte memoryLoad8(int address) { @@ -108,63 +80,6 @@ memory.store8(address, data); } - /** - * Read an ASCIIZ string from the process' memory into a Java String - */ - public String memoryReadString(int address) { - StringBuffer str = new StringBuffer(); - char c; - - while ((c = (char) memoryLoad8(address++)) != 0) - str.append(c); - - return str.toString(); - } - - /** - * Write a Java string (crudely) to an ASCIIZ string in the process' memory - */ - public void memoryWriteString(int byteAddr, String value) { - if (value != null) { - for (int i = 0; i < value.length(); i++) { - memoryStore8(byteAddr + i, (byte) value.charAt(i)); - } - memoryStore8(byteAddr + value.length(), (byte) 0); - } - } - - /** - * Map an anonymous page of memory - * - * @param addr - * the address to map or NULL if don't care - * @param len - * the amount of memory to map - * @param read - * is the page readable - * @param write - * is the page writable - * @param exec - * is the page executable - */ - public int memoryMap(int addr, int len, boolean read, boolean write, - boolean exec) throws MemoryMapException { - return memory.map(addr, len, read, write, exec); - } - - /** - * Simulate an munmap system call. - * - * @param start - * start of memory area to unmap. - * @param length - * length of area. - */ - public int munmap(int start, int length) { - memory.unmap(start, length); - return 0; - } - /* * Utility functions */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |