Beispiel #1
0
        public void Detach()
        {
            if (CurrentState == State.Idle || CurrentState == State.Detached)
            {
                return;
            }

            Memory.UninitializeMapping();

            if (codeCacheHandle != null)
            {
                FileMapping.UnmapViewOfFile(codeCachePtr);
                codeCacheHandle.Close();
                codeCacheHandle = null;
            }
            if (memoryHandle != null)
            {
                memoryHandle.Close();
                memoryHandle = null;
            }

            if (socket != null)
            {
                socket.Close();
                socket = null;
            }

            OnStateChanged(State.Detached);
        }
Beispiel #2
0
        public bool InitializeMapping(FileMappingHandle mappingHandle)
        {
            ulong mappingBase = 0x100000000;

            foreach (MapInfo mapInfo in mapInfos)
            {
                uint targetAddressLow  = (uint)mapInfo.targetAddress;
                uint targetAddressHigh = (uint)(mapInfo.targetAddress >> 32);
                mapInfo.ptr = FileMapping.MapViewOfFileEx(
                    mappingHandle, FileMapAccess.FILE_MAP_ALL_ACCESS,
                    targetAddressHigh, targetAddressLow,
                    mapInfo.virtualAddressEnd - mapInfo.virtualAddressStart + 1,
                    mappingBase + mapInfo.virtualAddressStart);
                if (mapInfo.ptr == IntPtr.Zero)
                {
                    System.Diagnostics.Debug.Fail("Unable to place memory at target address");
                    return(false);
                }
            }
            VirtualMembase  = new UIntPtr(mappingBase);
            PhysicalMembase = new UIntPtr(mappingBase + 0x100000000);
            return(true);
        }
Beispiel #3
0
        public async Task Attach(CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(CurrentState == State.Idle);

            SocketPermission permission = new SocketPermission(
                NetworkAccess.Connect,
                TransportType.Tcp,
                kServerHostname,
                kServerPort);

            permission.Demand();

            IPAddress  ipAddress  = new IPAddress(new byte[] { 127, 0, 0, 1 });
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, kServerPort);

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                                ProtocolType.Tcp);
            socket.Blocking          = false;
            socket.NoDelay           = true;
            socket.ReceiveBufferSize = 1024 * 1024;
            socket.SendBufferSize    = 1024 * 1024;
            socket.ReceiveTimeout    = 0;
            socket.SendTimeout       = 0;

            OnStateChanged(State.Attaching);

            while (true)
            {
                Task task = Task.Factory.FromAsync(socket.BeginConnect, socket.EndConnect, ipEndPoint, null);
                try {
                    await task.WithCancellation(cancellationToken);
                } catch (OperationCanceledException) {
                    socket.Close();
                    socket = null;
                    OnStateChanged(State.Idle);
                    return;
                } catch (SocketException e) {
                    if (e.SocketErrorCode == SocketError.ConnectionRefused)
                    {
                        // Not found - emulator may still be starting.
                        System.Diagnostics.Debug.WriteLine("Connection refused; trying again...");
                        continue;
                    }
                    OnStateChanged(State.Idle);
                    return;
                }
                break;
            }

            // Start recv pump.
            Dispatch.Issue(() => ReceivePump());

            var fbb = BeginRequest();

            AttachRequest.StartAttachRequest(fbb);
            int requestDataOffset = AttachRequest.EndAttachRequest(fbb);
            var response          = await CommitRequest(fbb, RequestData.AttachRequest, requestDataOffset);

            System.Diagnostics.Debug.Assert(response.ResponseDataType ==
                                            ResponseData.AttachResponse);
            var attachResponse = new AttachResponse();

            response.GetResponseData(attachResponse);

            // Open mmap to share memory.
            memoryHandle = FileMapping.OpenFileMapping(
                FileMapAccess.FILE_MAP_ALL_ACCESS, false, attachResponse.MemoryFile);
            if (memoryHandle.IsInvalid)
            {
                System.Diagnostics.Debug.Fail("Unable to open target memory");
                Detach();
                return;
            }

            // Open mmap to code cache.
            codeCacheHandle =
                FileMapping.OpenFileMapping(FileMapAccess.FILE_MAP_ALL_ACCESS, false,
                                            attachResponse.CodeCacheFile);
            if (codeCacheHandle.IsInvalid)
            {
                System.Diagnostics.Debug.Fail("Unable to open target code cache");
                Detach();
                return;
            }
            codeCachePtr = FileMapping.MapViewOfFileEx(
                codeCacheHandle, FileMapAccess.FILE_MAP_ALL_ACCESS, 0, 0,
                attachResponse.CodeCacheSize, attachResponse.CodeCacheBase);

            // Setup the memory system. This maps the emulator memory into our address
            // space.
            if (!Memory.InitializeMapping(memoryHandle))
            {
                Detach();
                return;
            }

            OnStateChanged(State.Attached);
        }