Ejemplo n.º 1
0
        public void SvcCall(IExecutionContext context, ulong address, int id)
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            if (currentThread.Owner != null &&
                currentThread.GetUserDisableCount() != 0 &&
                currentThread.Owner.PinnedThreads[currentThread.CurrentCore] == null)
            {
                _context.CriticalSection.Enter();

                currentThread.Owner.PinThread(currentThread);

                currentThread.SetUserInterruptFlag();

                _context.CriticalSection.Leave();
            }

            if (context.IsAarch32)
            {
                SyscallDispatch.Dispatch32(_context.Syscall, context, id);
            }
            else
            {
                SyscallDispatch.Dispatch64(_context.Syscall, context, id);
            }

            currentThread.HandlePostSyscall();
        }
Ejemplo n.º 2
0
        private void InterruptHandler(IExecutionContext context)
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            if (currentThread.Context.Running &&
                currentThread.Owner != null &&
                currentThread.GetUserDisableCount() != 0 &&
                currentThread.Owner.PinnedThreads[currentThread.CurrentCore] == null)
            {
                KernelContext.CriticalSection.Enter();

                currentThread.Owner.PinThread(currentThread);

                currentThread.SetUserInterruptFlag();

                KernelContext.CriticalSection.Leave();
            }

            if (currentThread.IsSchedulable)
            {
                KernelContext.Schedulers[currentThread.CurrentCore].Schedule();
            }

            currentThread.HandlePostSyscall();
        }
Ejemplo n.º 3
0
        private void InterruptHandler(object sender, EventArgs e)
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            if (currentThread.Owner != null &&
                currentThread.GetUserDisableCount() != 0 &&
                currentThread.Owner.PinnedThreads[currentThread.CurrentCore] == null)
            {
                KernelContext.CriticalSection.Enter();

                currentThread.Owner.PinThread(currentThread);

                currentThread.SetUserInterruptFlag();

                if (currentThread.IsSchedulable)
                {
                    KernelContext.Schedulers[currentThread.CurrentCore].Schedule();
                }

                KernelContext.CriticalSection.Leave();
            }
            else if (currentThread.IsSchedulable)
            {
                KernelContext.Schedulers[currentThread.CurrentCore].Schedule();
            }

            currentThread.HandlePostSyscall();
        }
Ejemplo n.º 4
0
        private void InterruptHandler(object sender, EventArgs e)
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            if (currentThread.IsSchedulable)
            {
                KernelContext.Schedulers[currentThread.CurrentCore].Schedule();
            }

            currentThread.HandlePostSyscall();
        }
Ejemplo n.º 5
0
        public void SvcCall(object sender, InstExceptionEventArgs e)
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            if (currentThread.Owner != null &&
                currentThread.GetUserDisableCount() != 0 &&
                currentThread.Owner.PinnedThreads[currentThread.CurrentCore] == null)
            {
                _context.CriticalSection.Enter();

                currentThread.Owner.PinThread(currentThread);

                currentThread.SetUserInterruptFlag();

                _context.CriticalSection.Leave();
            }

            ExecutionContext context = (ExecutionContext)sender;

            if (context.IsAarch32)
            {
                var svcFunc = SyscallTable.SvcTable32[e.Id];

                if (svcFunc == null)
                {
                    throw new NotImplementedException($"SVC 0x{e.Id:X4} is not implemented.");
                }

                svcFunc(_syscall32, context);
            }
            else
            {
                var svcFunc = SyscallTable.SvcTable64[e.Id];

                if (svcFunc == null)
                {
                    throw new NotImplementedException($"SVC 0x{e.Id:X4} is not implemented.");
                }

                svcFunc(_syscall64, context);
            }

            currentThread.HandlePostSyscall();
        }
Ejemplo n.º 6
0
        private void ServerLoop()
        {
            _selfProcess = KernelStatic.GetCurrentProcess();

            if (SmObjectFactory != null)
            {
                _context.Syscall.ManageNamedPort("sm:", 50, out int serverPortHandle);

                AddPort(serverPortHandle, SmObjectFactory);

                InitDone.Set();
            }
            else
            {
                InitDone.Dispose();
            }

            KThread thread     = KernelStatic.GetCurrentThread();
            ulong   messagePtr = thread.TlsAddress;

            _context.Syscall.SetHeapSize(0x200000, out ulong heapAddr);

            _selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
            _selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
            _selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));

            int replyTargetHandle = 0;

            while (true)
            {
                int[] portHandles    = _portHandles.ToArray();
                int[] sessionHandles = _sessionHandles.ToArray();
                int[] handles        = new int[portHandles.Length + sessionHandles.Length];

                portHandles.CopyTo(handles, 0);
                sessionHandles.CopyTo(handles, portHandles.Length);

                // We still need a timeout here to allow the service to pick up and listen new sessions...
                var rc = _context.Syscall.ReplyAndReceive(handles, replyTargetHandle, 1000000L, out int signaledIndex);

                thread.HandlePostSyscall();

                if (!thread.Context.Running)
                {
                    break;
                }

                replyTargetHandle = 0;

                if (rc == KernelResult.Success && signaledIndex >= portHandles.Length)
                {
                    // We got a IPC request, process it, pass to the appropriate service if needed.
                    int signaledHandle = handles[signaledIndex];

                    if (Process(signaledHandle, heapAddr))
                    {
                        replyTargetHandle = signaledHandle;
                    }
                }
                else
                {
                    if (rc == KernelResult.Success)
                    {
                        // We got a new connection, accept the session to allow servicing future requests.
                        if (_context.Syscall.AcceptSession(handles[signaledIndex], out int serverSessionHandle) == KernelResult.Success)
                        {
                            IpcService obj = _ports[handles[signaledIndex]].Invoke();

                            AddSessionObj(serverSessionHandle, obj);
                        }
                    }

                    _selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
                    _selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
                    _selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));
                }
            }
        }
Ejemplo n.º 7
0
        private void PostSvcHandler()
        {
            KThread currentThread = _system.Scheduler.GetCurrentThread();

            currentThread.HandlePostSyscall();
        }
Ejemplo n.º 8
0
        private void ServerLoop()
        {
            if (SmObject != null)
            {
                _context.Syscall.ManageNamedPort("sm:", 50, out int serverPortHandle);

                AddPort(serverPortHandle, SmObject);

                InitDone.Set();
            }
            else
            {
                InitDone.Dispose();
            }

            KThread thread     = _context.Scheduler.GetCurrentThread();
            ulong   messagePtr = thread.TlsAddress;

            _context.Syscall.SetHeapSize(0x200000, out ulong heapAddr);

            _selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
            _selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
            _selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));

            int replyTargetHandle = 0;

            while (true)
            {
                int[] handles = _portHandles.ToArray();

                for (int i = 0; i < handles.Length; i++)
                {
                    if (_context.Syscall.AcceptSession(handles[i], out int serverSessionHandle) == KernelResult.Success)
                    {
                        AddSessionObj(serverSessionHandle, _ports[handles[i]]);
                    }
                }

                handles = _sessionHandles.ToArray();

                var rc = _context.Syscall.ReplyAndReceive(handles, replyTargetHandle, 1000000L, out int signaledIndex);

                thread.HandlePostSyscall();

                if (!thread.Context.Running)
                {
                    break;
                }

                replyTargetHandle = 0;

                if (rc == KernelResult.Success && signaledIndex != -1)
                {
                    int signaledHandle = handles[signaledIndex];

                    if (Process(signaledHandle, heapAddr))
                    {
                        replyTargetHandle = signaledHandle;
                    }
                }
                else
                {
                    _selfProcess.CpuMemory.Write(messagePtr + 0x0, 0);
                    _selfProcess.CpuMemory.Write(messagePtr + 0x4, 2 << 10);
                    _selfProcess.CpuMemory.Write(messagePtr + 0x8, heapAddr | ((ulong)PointerBufferSize << 48));
                }
            }
        }
Ejemplo n.º 9
0
        private void PostSvcHandler()
        {
            KThread currentThread = KernelStatic.GetCurrentThread();

            currentThread.HandlePostSyscall();
        }