2014-08-01 18:27:48 +02:00
|
|
|
#include "stdafx.h"
|
2014-08-31 11:54:12 +02:00
|
|
|
#include "Memory.h"
|
2015-01-07 03:14:00 +01:00
|
|
|
#include "Emu/CPU/CPUThread.h"
|
|
|
|
|
#include "Emu/Cell/PPUThread.h"
|
2015-02-01 14:52:34 +01:00
|
|
|
#include "Emu/ARMv7/ARMv7Thread.h"
|
2014-08-31 11:54:12 +02:00
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
namespace vm
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
void* const g_base_addr = VirtualAlloc(nullptr, 0x100000000, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/mman.h>
|
2014-08-31 11:54:12 +02:00
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
/* OS X uses MAP_ANON instead of MAP_ANONYMOUS */
|
|
|
|
|
#ifndef MAP_ANONYMOUS
|
|
|
|
|
#define MAP_ANONYMOUS MAP_ANON
|
|
|
|
|
#endif
|
2014-08-31 11:54:12 +02:00
|
|
|
|
2014-11-20 20:41:04 +01:00
|
|
|
void* const g_base_addr = mmap(nullptr, 0x100000000, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
2014-11-19 15:16:30 +01:00
|
|
|
#endif
|
2014-08-01 18:27:48 +02:00
|
|
|
|
|
|
|
|
bool check_addr(u32 addr)
|
|
|
|
|
{
|
2014-08-31 01:06:43 +02:00
|
|
|
// Checking address before using it is unsafe.
|
|
|
|
|
// The only safe way to check it is to protect both actions (checking and using) with mutex that is used for mapping/allocation.
|
2014-08-01 18:27:48 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 01:06:43 +02:00
|
|
|
//TODO
|
2014-08-01 18:27:48 +02:00
|
|
|
bool map(u32 addr, u32 size, u32 flags)
|
|
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
return Memory.Map(addr, size);
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool unmap(u32 addr, u32 size, u32 flags)
|
|
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
return Memory.Unmap(addr);
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
u32 alloc(u32 addr, u32 size, memory_location location)
|
2014-08-01 18:27:48 +02:00
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
return g_locations[location].fixed_allocator(addr, size);
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
u32 alloc(u32 size, memory_location location)
|
|
|
|
|
{
|
|
|
|
|
return g_locations[location].allocator(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dealloc(u32 addr, memory_location location)
|
|
|
|
|
{
|
|
|
|
|
return g_locations[location].deallocator(addr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 20:41:31 +01:00
|
|
|
u32 get_addr(const void* real_pointer)
|
|
|
|
|
{
|
|
|
|
|
const u64 diff = (u64)real_pointer - (u64)g_base_addr;
|
|
|
|
|
const u32 res = (u32)diff;
|
|
|
|
|
|
|
|
|
|
if (res == diff)
|
|
|
|
|
{
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(!real_pointer);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-29 16:48:05 +01:00
|
|
|
void error(const u64 addr, const char* func)
|
|
|
|
|
{
|
2015-02-02 10:14:49 +01:00
|
|
|
throw fmt::format("%s(): failed to cast 0x%llx (too big value)", func, addr);
|
2015-01-29 16:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
namespace ps3
|
|
|
|
|
{
|
|
|
|
|
u32 main_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.MainMem.AllocAlign(size, 1);
|
|
|
|
|
}
|
|
|
|
|
u32 main_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.MainMem.AllocFixed(addr, size) ? addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void main_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.MainMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 g_stack_offset = 0;
|
|
|
|
|
|
|
|
|
|
u32 stack_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.StackMem.AllocAlign(size, 0x10);
|
|
|
|
|
}
|
|
|
|
|
u32 stack_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.StackMem.AllocFixed(addr, size) ? addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void stack_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.StackMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 sprx_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.SPRXMem.AllocAlign(size, 1);
|
|
|
|
|
}
|
|
|
|
|
u32 sprx_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.SPRXMem.AllocFixed(Memory.SPRXMem.GetStartAddr() + addr, size) ? Memory.SPRXMem.GetStartAddr() + addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void sprx_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.SPRXMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 user_space_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.PRXMem.AllocAlign(size, 1);
|
|
|
|
|
}
|
|
|
|
|
u32 user_space_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.PRXMem.AllocFixed(addr, size) ? addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void user_space_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.PRXMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
Memory.Init(Memory_PS3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace psv
|
|
|
|
|
{
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
Memory.Init(Memory_PSV);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace psp
|
|
|
|
|
{
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
Memory.Init(Memory_PSP);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location_info g_locations[memory_location_count] =
|
|
|
|
|
{
|
|
|
|
|
{ 0x00010000, 0x2FFF0000, ps3::main_alloc, ps3::main_fixed_alloc, ps3::main_dealloc },
|
2014-11-24 19:12:04 +01:00
|
|
|
{ 0xD0000000, 0x10000000, ps3::stack_alloc, ps3::stack_fixed_alloc, ps3::stack_dealloc },
|
|
|
|
|
|
|
|
|
|
//remove me
|
2014-11-19 15:16:30 +01:00
|
|
|
{ 0x00010000, 0x2FFF0000, ps3::sprx_alloc, ps3::sprx_fixed_alloc, ps3::sprx_dealloc },
|
2014-11-24 19:12:04 +01:00
|
|
|
|
|
|
|
|
{ 0x30000000, 0x10000000, ps3::user_space_alloc, ps3::user_space_fixed_alloc, ps3::user_space_dealloc },
|
2014-11-19 15:16:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void close()
|
2014-08-01 18:27:48 +02:00
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
Memory.Close();
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
2015-01-07 03:14:00 +01:00
|
|
|
|
|
|
|
|
u32 stack_push(CPUThread& CPU, u32 size, u32 align_v, u32& old_pos)
|
|
|
|
|
{
|
2015-02-01 14:52:34 +01:00
|
|
|
assert(align_v);
|
|
|
|
|
|
2015-01-07 03:14:00 +01:00
|
|
|
switch (CPU.GetType())
|
|
|
|
|
{
|
|
|
|
|
case CPU_THREAD_PPU:
|
|
|
|
|
{
|
|
|
|
|
PPUThread& PPU = static_cast<PPUThread&>(CPU);
|
|
|
|
|
|
|
|
|
|
old_pos = (u32)PPU.GPR[1];
|
|
|
|
|
PPU.GPR[1] -= align(size, 8); // room minimal possible size
|
|
|
|
|
PPU.GPR[1] &= ~(align_v - 1); // fix stack alignment
|
|
|
|
|
|
|
|
|
|
if (PPU.GPR[1] < CPU.GetStackAddr())
|
|
|
|
|
{
|
|
|
|
|
// stack overflow
|
|
|
|
|
PPU.GPR[1] = old_pos;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (u32)PPU.GPR[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CPU_THREAD_SPU:
|
|
|
|
|
case CPU_THREAD_RAW_SPU:
|
|
|
|
|
{
|
|
|
|
|
assert(!"stack_push(): SPU not supported");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CPU_THREAD_ARMv7:
|
|
|
|
|
{
|
2015-02-01 14:52:34 +01:00
|
|
|
ARMv7Context& context = static_cast<ARMv7Thread&>(CPU).context;
|
|
|
|
|
|
|
|
|
|
old_pos = context.SP;
|
|
|
|
|
context.SP -= align(size, 4); // room minimal possible size
|
|
|
|
|
context.SP &= ~(align_v - 1); // fix stack alignment
|
|
|
|
|
|
|
|
|
|
if (context.SP < CPU.GetStackAddr())
|
|
|
|
|
{
|
|
|
|
|
// stack overflow
|
|
|
|
|
context.SP = old_pos;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return context.SP;
|
|
|
|
|
}
|
2015-01-07 03:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
assert(!"stack_push(): invalid thread type");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stack_pop(CPUThread& CPU, u32 addr, u32 old_pos)
|
|
|
|
|
{
|
|
|
|
|
switch (CPU.GetType())
|
|
|
|
|
{
|
|
|
|
|
case CPU_THREAD_PPU:
|
|
|
|
|
{
|
|
|
|
|
PPUThread& PPU = static_cast<PPUThread&>(CPU);
|
|
|
|
|
|
|
|
|
|
assert(PPU.GPR[1] == addr);
|
|
|
|
|
PPU.GPR[1] = old_pos;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CPU_THREAD_SPU:
|
|
|
|
|
case CPU_THREAD_RAW_SPU:
|
|
|
|
|
{
|
|
|
|
|
assert(!"stack_pop(): SPU not supported");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case CPU_THREAD_ARMv7:
|
|
|
|
|
{
|
2015-02-02 10:14:49 +01:00
|
|
|
ARMv7Context& context = static_cast<ARMv7Thread&>(CPU).context;
|
|
|
|
|
|
|
|
|
|
assert(context.SP == addr);
|
|
|
|
|
context.SP = old_pos;
|
2015-01-07 03:14:00 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
assert(!"stack_pop(): invalid thread type");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|