Пример #1
0
        private void SvcOutputDebugString(ARegisters Registers)
        {
            long Position = (long)Registers.X0;
            long Size     = (long)Registers.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);

            Logging.Info($"SvcOutputDebugString: {Str}");

            Registers.X0 = (int)SvcResult.Success;
        }
Пример #2
0
        private void SvcOutputDebugString(AThreadState ThreadState)
        {
            long Position = (long)ThreadState.X0;
            long Size     = (long)ThreadState.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, Size);

            Logging.Info(LogClass.KernelSvc, Str);

            ThreadState.X0 = 0;
        }
Пример #3
0
        private void SvcOutputDebugString(AThreadState ThreadState)
        {
            long Position = (long)ThreadState.X0;
            long Size     = (long)ThreadState.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);

            Logging.Info($"SvcOutputDebugString: {Str}");

            ThreadState.X0 = (int)SvcResult.Success;
        }
Пример #4
0
        public long SetAudioDeviceOutputVolume(ServiceCtx Context)
        {
            float Volume = Context.RequestData.ReadSingle();

            long Position = Context.Request.SendBuff[0].Position;
            long Size     = Context.Request.SendBuff[0].Size;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position, Size);

            return(0);
        }
Пример #5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="AAllocatedMemory" /> class.
 /// </summary>
 /// <param name="processPlus">The reference of the <see cref="AProcessSharp" /> object.</param>
 /// <param name="name"></param>
 /// <param name="size">The size of the allocated memory.</param>
 /// <param name="protection">The protection of the allocated memory.</param>
 /// <param name="mustBeDisposed">The allocated memory will be released when the finalizer collects the object.</param>
 public AAllocatedMemory(AProcessSharp processPlus, string name, int size,
                         MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite,
                         bool mustBeDisposed = true)
     : base(processPlus, AMemoryHelper.Allocate(processPlus.Handle, size, protection))
 {
     // Set local vars
     Identifier     = name;
     MustBeDisposed = mustBeDisposed;
     IsDisposed     = false;
     Size           = size;
 }
Пример #6
0
        private static void SvcOutputDebugString(Switch Ns, ARegisters Registers, AMemory Memory)
        {
            long Position = (long)Registers.X0;
            long Size     = (long)Registers.X1;

            string Str = AMemoryHelper.ReadAsciiString(Memory, Position, (int)Size);

            Console.WriteLine($"SvcOutputDebugString: {Str}");

            Registers.X0 = (int)SvcResult.Success;
        }
Пример #7
0
        private void SendShader(
            AMemory Memory,
            uint ShaderPrg,
            uint ShaderId,
            uint CodeAddr,
            uint ShaderType,
            uint CodeEnd)
        {
            long CodePos = Gpu.MemoryMgr.GetCpuAddr(CodeAddr);

            byte[] Data = AMemoryHelper.ReadBytes(Memory, CodePos, 0x300);
        }
Пример #8
0
        private void WriteData(
            long Position,
            byte[]      Data,
            MemoryType Type,
            AMemoryPerm Perm)
        {
            Memory.Manager.Map(Position, Data.Length, (int)Type, AMemoryPerm.Write);

            AMemoryHelper.WriteBytes(Memory, Position, Data);

            Memory.Manager.Reprotect(Position, Data.Length, Perm);
        }
Пример #9
0
        public long TransactParcelAuto(ServiceCtx Context)
        {
            int Id   = Context.RequestData.ReadInt32();
            int Code = Context.RequestData.ReadInt32();

            (long DataPos, long DataSize) = Context.Request.GetBufferType0x21();

            byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, DataSize);

            Data = Parcel.GetParcelData(Data);

            return Flinger.ProcessParcelRequest(Context, Data, Code);
        }
Пример #10
0
        public long GetActiveAudioDeviceName(ServiceCtx Context)
        {
            string Name = "FIXME";

            long Position = Context.Request.ReceiveBuff[0].Position;
            long Size     = Context.Request.ReceiveBuff[0].Size;

            byte[] Buffer = Encoding.ASCII.GetBytes(Name + '\0');

            AMemoryHelper.WriteBytes(Context.Memory, Position, Buffer);

            return(0);
        }
Пример #11
0
        public long SetAudioDeviceOutputVolume(ServiceCtx Context)
        {
            float Volume = Context.RequestData.ReadSingle();

            long Position = Context.Request.SendBuff[0].Position;
            long Size     = Context.Request.SendBuff[0].Size;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, Position, Size);

            Logging.Stub(LogClass.ServiceAudio, $"Volume = {Volume}, Position = {Position}, Size = {Size}");

            return(0);
        }
Пример #12
0
        public long Open(ServiceCtx Context)
        {
            long NamePtr = Context.Request.SendBuff[0].Position;

            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, NamePtr);

            int Fd = Fds.Add(Context.Process, new NvFd(Name));

            Context.ResponseData.Write(Fd);
            Context.ResponseData.Write(0);

            return(0);
        }
Пример #13
0
        private static int ZcullGetCtxSize(ServiceCtx Context)
        {
            long OutputPosition = Context.Request.GetBufferType0x22Position();

            NvGpuGpuZcullGetCtxSize Args = new NvGpuGpuZcullGetCtxSize();

            Args.Size = 1;

            AMemoryHelper.Write(Context.Memory, OutputPosition, Args);

            Context.Ns.Log.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return(NvResult.Success);
        }
Пример #14
0
        public long Get(ServiceCtx Context)
        {
            Context.Device.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");

            long Position = Context.Request.ReceiveBuff[0].Position;

            AMemoryHelper.FillWithZeros(Context.Memory, Position, 0x80);

            Context.Memory.WriteInt32(Position, 0);
            Context.Memory.WriteInt32(Position + 4, 1);
            Context.Memory.WriteInt64(Position + 8, 1);

            return(GetBase(Context));
        }
Пример #15
0
        private static int GetConfig(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21Position();
            long OutputPosition = Context.Request.GetBufferType0x22Position();

            string Nv   = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0, 0x41);
            string Name = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0x41, 0x41);

            Context.Memory.WriteByte(OutputPosition + 0x82, 0);

            Context.Ns.Log.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return(NvResult.Success);
        }
Пример #16
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="AMemoryProtection" /> class.
 /// </summary>
 /// <param name="handle">The reference of the <see cref="ASafeMemoryHandle" /> object.</param>
 /// <param name="baseAddress">The base address of the memory to change the protection.</param>
 /// <param name="size">The size of the memory to change.</param>
 /// <param name="protection">The new protection to apply.</param>
 /// <param name="mustBeDisposed">The resource will be automatically disposed when the finalizer collects the object.</param>
 public AMemoryProtection(ASafeMemoryHandle handle, IntPtr baseAddress, int size,
                          MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite,
                          bool mustBeDisposed = true)
 {
     // Save the parameters
     Handle         = handle;
     BaseAddress    = baseAddress;
     NewProtection  = protection;
     Size           = size;
     MustBeDisposed = mustBeDisposed;
     // Change the memory protection
     AMemoryHelper.ChangeProtection(Handle, baseAddress, size, protection, out var oldProtection);
     OldProtection = oldProtection;
 }
Пример #17
0
        public long TransactParcel(ServiceCtx Context)
        {
            int Id   = Context.RequestData.ReadInt32();
            int Code = Context.RequestData.ReadInt32();

            long DataPos  = Context.Request.SendBuff[0].Position;
            long DataSize = Context.Request.SendBuff[0].Size;

            byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, (int)DataSize);

            Data = Parcel.GetParcelData(Data);

            return(Flinger.ProcessParcelRequest(Context, Data, Code));
        }
Пример #18
0
        public long SetAudioDeviceOutputVolumeAuto(ServiceCtx Context)
        {
            float Volume = Context.RequestData.ReadSingle();

            (long Position, long Size) = Context.Request.GetBufferType0x21();

            byte[] DeviceNameBuffer = AMemoryHelper.ReadBytes(Context.Memory, Position, Size);

            string DeviceName = Encoding.UTF8.GetString(DeviceNameBuffer);

            Context.Ns.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");

            return(0);
        }
Пример #19
0
        private static int GetCharacteristics(ServiceCtx Context)
        {
            long InputPosition  = Context.Request.GetBufferType0x21Position();
            long OutputPosition = Context.Request.GetBufferType0x22Position();

            NvGpuGpuGetCharacteristics Args = AMemoryHelper.Read <NvGpuGpuGetCharacteristics>(Context.Memory, InputPosition);

            Args.BufferSize = 0xa0;

            Args.Arch                   = 0x120;
            Args.Impl                   = 0xb;
            Args.Rev                    = 0xa1;
            Args.NumGpc                 = 0x1;
            Args.L2CacheSize            = 0x40000;
            Args.OnBoardVideoMemorySize = 0x0;
            Args.NumTpcPerGpc           = 0x2;
            Args.BusType                = 0x20;
            Args.BigPageSize            = 0x20000;
            Args.CompressionPageSize    = 0x20000;
            Args.PdeCoverageBitCount    = 0x1b;
            Args.AvailableBigPageSizes  = 0x30000;
            Args.GpcMask                = 0x1;
            Args.SmArchSmVersion        = 0x503;
            Args.SmArchSpaVersion       = 0x503;
            Args.SmArchWarpCount        = 0x80;
            Args.GpuVaBitCount          = 0x28;
            Args.Reserved               = 0x0;
            Args.Flags                  = 0x55;
            Args.TwodClass              = 0x902d;
            Args.ThreedClass            = 0xb197;
            Args.ComputeClass           = 0xb1c0;
            Args.GpfifoClass            = 0xb06f;
            Args.InlineToMemoryClass    = 0xa140;
            Args.DmaCopyClass           = 0xb0b5;
            Args.MaxFbpsCount           = 0x1;
            Args.FbpEnMask              = 0x0;
            Args.MaxLtcPerFbp           = 0x2;
            Args.MaxLtsPerLtc           = 0x1;
            Args.MaxTexPerTpc           = 0x0;
            Args.MaxGpcCount            = 0x1;
            Args.RopL2EnMask0           = 0x21d70;
            Args.RopL2EnMask1           = 0x0;
            Args.ChipName               = 0x6230326d67;
            Args.GrCompbitStoreBaseHw   = 0x0;

            AMemoryHelper.Write(Context.Memory, OutputPosition, Args);

            return(NvResult.Success);
        }
Пример #20
0
        private long[] UploadShaders(AMemory Memory)
        {
            long[] Tags = new long[5];

            long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);

            for (int Index = 0; Index < 6; Index++)
            {
                int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
                int Offset  = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);

                //Note: Vertex Program (B) is always enabled.
                bool Enable = (Control & 1) != 0 || Index == 1;

                if (!Enable)
                {
                    continue;
                }

                long Tag = BasePosition + (uint)Offset;

                long Position = Gpu.GetCpuAddr(Tag);

                //TODO: Find a better way to calculate the size.
                int Size = 0x20000;

                byte[] Code = AMemoryHelper.ReadBytes(Memory, Position, (uint)Size);

                GalShaderType ShaderType = GetTypeFromProgram(Index);

                Tags[(int)ShaderType] = Tag;

                Gpu.Renderer.CreateShader(Tag, ShaderType, Code);
                Gpu.Renderer.BindShader(Tag);
            }

            int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX);
            int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY);

            float SX = BitConverter.Int32BitsToSingle(RawSX);
            float SY = BitConverter.Int32BitsToSingle(RawSY);

            float SignX = MathF.Sign(SX);
            float SignY = MathF.Sign(SY);

            Gpu.Renderer.SetUniform2F(GalConsts.FlipUniformName, SignX, SignY);

            return(Tags);
        }
Пример #21
0
        private static int GetConfig(ServiceCtx Context)
        {
            if (!IsProductionMode)
            {
                long InputPosition  = Context.Request.GetBufferType0x21().Position;
                long OutputPosition = Context.Request.GetBufferType0x22().Position;

                string Domain = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0, 0x41);
                string Name   = AMemoryHelper.ReadAsciiString(Context.Memory, InputPosition + 0x41, 0x41);

                if (Set.NxSettings.Settings.TryGetValue($"{Domain}!{Name}", out object NvSetting))
                {
                    byte[] SettingBuffer = new byte[0x101];

                    if (NvSetting is string StringValue)
                    {
                        if (StringValue.Length > 0x100)
                        {
                            Logger.PrintError(LogClass.ServiceNv, $"{Domain}!{Name} String value size is too big!");
                        }
                        else
                        {
                            SettingBuffer = Encoding.ASCII.GetBytes(StringValue + "\0");
                        }
                    }

                    if (NvSetting is int IntValue)
                    {
                        SettingBuffer = BitConverter.GetBytes(IntValue);
                    }
                    else if (NvSetting is bool BoolValue)
                    {
                        SettingBuffer[0] = BoolValue ? (byte)1 : (byte)0;
                    }
                    else
                    {
                        throw new NotImplementedException(NvSetting.GetType().Name);
                    }

                    Context.Memory.WriteBytes(OutputPosition + 0x82, SettingBuffer);

                    Logger.PrintDebug(LogClass.ServiceNv, $"Got setting {Domain}!{Name}");
                }

                return(NvResult.Success);
            }

            return(NvResult.NotAvailableInProduction);
        }
Пример #22
0
        private static int GetActiveSlotMask(ServiceCtx Context)
        {
            long OutputPosition = Context.Request.GetBufferType0x22Position();

            NvGpuGpuGetActiveSlotMask Args = new NvGpuGpuGetActiveSlotMask();

            Args.Slot = 0x07;
            Args.Mask = 0x01;

            AMemoryHelper.Write(Context.Memory, OutputPosition, Args);

            Context.Ns.Log.PrintStub(LogClass.ServiceNv, "Stubbed.");

            return(NvResult.Success);
        }
Пример #23
0
        public long DeleteDirectory(ServiceCtx Context)
        {
            long   Position = Context.Request.PtrBuff[0].Position;
            string Name     = AMemoryHelper.ReadAsciiString(Context.Memory, Position);
            string FileName = Context.Ns.VFs.GetFullPath(Path, Name);

            if (FileName != null)
            {
                Directory.Delete(FileName);
                return(0);
            }

            // TODO: Correct error code.
            return(-1);
        }
Пример #24
0
        public long SetAudioDeviceOutputVolume(ServiceCtx Context)
        {
            float Volume = Context.RequestData.ReadSingle();

            long Position = Context.Request.SendBuff[0].Position;
            long Size     = Context.Request.SendBuff[0].Size;

            byte[] DeviceNameBuffer = AMemoryHelper.ReadBytes(Context.Memory, Position, Size);

            string DeviceName = Encoding.ASCII.GetString(DeviceNameBuffer);

            Context.Ns.Log.PrintStub(LogClass.ServiceAudio, "Stubbed.");

            return(0);
        }
Пример #25
0
        private void SvcConnectToNamedPort(AThreadState ThreadState)
        {
            long StackPtr = (long)ThreadState.X0;
            long NamePtr  = (long)ThreadState.X1;

            string Name = AMemoryHelper.ReadAsciiString(Memory, NamePtr, 8);

            //TODO: Validate that app has perms to access the service, and that the service
            //actually exists, return error codes otherwise.

            HSession Session = new HSession(Name);

            ThreadState.X1 = (ulong)Ns.Os.Handles.GenerateId(Session);
            ThreadState.X0 = (int)SvcResult.Success;
        }
Пример #26
0
        public void LoadProgram(IExecutable Program)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(nameof(Process));
            }

            Logging.Info(LogClass.Loader, $"Image base at 0x{ImageBase:x16}.");

            Executable Executable = new Executable(Program, Memory, ImageBase);

            Executables.Add(Executable);

            ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
        }
        public long OpenLayer(ServiceCtx Context)
        {
            long LayerId = Context.RequestData.ReadInt64();
            long UserId  = Context.RequestData.ReadInt64();

            long ParcelPtr = Context.Request.ReceiveBuff[0].Position;

            byte[] Parcel = MakeIGraphicsBufferProducer(ParcelPtr);

            AMemoryHelper.WriteBytes(Context.Memory, ParcelPtr, Parcel);

            Context.ResponseData.Write((long)Parcel.Length);

            return(0);
        }
Пример #28
0
        public long Write(ServiceCtx Context)
        {
            long Position = Context.Request.SendBuff[0].Position;

            long Zero   = Context.RequestData.ReadInt64();
            long Offset = Context.RequestData.ReadInt64();
            long Size   = Context.RequestData.ReadInt64();

            byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, Position, (int)Size);

            BaseStream.Seek(Offset, SeekOrigin.Begin);
            BaseStream.Write(Data, 0, (int)Size);

            return(0);
        }
Пример #29
0
        public bool Run(bool NeedsHbAbi = false)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(nameof(Process));
            }

            this.NeedsHbAbi = NeedsHbAbi;

            if (Executables.Count == 0)
            {
                return(false);
            }

            MakeSymbolTable();

            MapRWMemRegion(
                MemoryRegions.MainStackAddress,
                MemoryRegions.MainStackSize,
                MemoryType.Normal);

            long StackTop = MemoryRegions.MainStackAddress + MemoryRegions.MainStackSize;

            int Handle = MakeThread(Executables[0].ImageBase, StackTop, 0, 44, 0);

            if (Handle == -1)
            {
                return(false);
            }

            MainThread = HandleTable.GetData <KThread>(Handle);

            if (NeedsHbAbi)
            {
                HbAbiDataPosition = AMemoryHelper.PageRoundUp(Executables[0].ImageEnd);

                string SwitchPath = Ns.VFs.SystemPathToSwitchPath(Executables[0].FilePath);

                Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle, SwitchPath);

                MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition;
                MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
            }

            Scheduler.StartThread(MainThread);

            return(true);
        }
Пример #30
0
        private void SvcConnectToNamedPort(AThreadState ThreadState)
        {
            long StackPtr = (long)ThreadState.X0;
            long NamePtr  = (long)ThreadState.X1;

            string Name = AMemoryHelper.ReadAsciiString(Memory, NamePtr, 8);

            //TODO: Validate that app has perms to access the service, and that the service
            //actually exists, return error codes otherwise.
            KSession Session = new KSession(ServiceFactory.MakeService(Name), Name);

            ulong Handle = (ulong)Process.HandleTable.OpenHandle(Session);

            ThreadState.X0 = 0;
            ThreadState.X1 = Handle;
        }