1
// Copyright (c) 2014- PPSSPP Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
18
#include "Common/ChunkFile.h"
19
#include "Core/MemMapHelpers.h"
20
#include "Core/HLE/HLE.h"
21
#include "Core/HLE/HLEHelperThread.h"
22
#include "Core/HLE/sceKernelThread.h"
23
#include "Core/HLE/sceKernelMemory.h"
24
#include "Core/MIPS/MIPSCodeUtils.h"
26
HLEHelperThread::HLEHelperThread() : id_(-1), entry_(0) {
29
HLEHelperThread::HLEHelperThread(const char *threadName, u32 instructions[], u32 instrCount, u32 prio, int stacksize) {
30
u32 instrBytes = instrCount * sizeof(u32);
31
u32 totalBytes = instrBytes + sizeof(u32) * 2;
32
AllocEntry(totalBytes);
33
Memory::Memcpy(entry_, instructions, instrBytes);
35
// Just to simplify things, we add the return here.
36
Memory::Write_U32(MIPS_MAKE_JR_RA(), entry_ + instrBytes + 0);
37
Memory::Write_U32(MIPS_MAKE_NOP(), entry_ + instrBytes + 4);
39
Create(threadName, prio, stacksize);
42
HLEHelperThread::HLEHelperThread(const char *threadName, const char *module, const char *func, u32 prio, int stacksize) {
43
const u32 bytes = sizeof(u32) * 2;
45
Memory::Write_U32(MIPS_MAKE_JR_RA(), entry_ + 0);
46
Memory::Write_U32(MIPS_MAKE_SYSCALL(module, func), entry_ + 4);
48
Create(threadName, prio, stacksize);
51
HLEHelperThread::~HLEHelperThread() {
52
__KernelDeleteThread(id_, SCE_KERNEL_ERROR_THREAD_TERMINATED, "helper deleted");
53
kernelMemory.Free(entry_);
56
void HLEHelperThread::AllocEntry(u32 size) {
57
entry_ = kernelMemory.Alloc(size);
58
currentMIPS->InvalidateICache(entry_, size);
61
void HLEHelperThread::Create(const char *threadName, u32 prio, int stacksize) {
62
id_ = __KernelCreateThreadInternal(threadName, __KernelGetCurThreadModuleId(), entry_, prio, stacksize, 0);
65
void HLEHelperThread::DoState(PointerWrap &p) {
66
auto s = p.Section("HLEHelperThread", 1);
75
void HLEHelperThread::Start(u32 a0, u32 a1) {
76
__KernelStartThread(id_, a0, a1, true);
79
void HLEHelperThread::Terminate() {
80
__KernelStopThread(id_, SCE_KERNEL_ERROR_THREAD_TERMINATED, "helper terminated");