2019-09-14 14:41:00 +02:00
|
|
|
#include "KSharedMemory.h"
|
2020-01-01 14:11:25 +01:00
|
|
|
#include "KProcess.h"
|
2019-10-13 10:04:47 +02:00
|
|
|
#include <nce.h>
|
2019-11-15 19:14:40 +01:00
|
|
|
#include <android/sharedmem.h>
|
2019-09-14 14:41:00 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-09-24 22:54:27 +02:00
|
|
|
namespace skyline::kernel::type {
|
2019-10-13 10:04:47 +02:00
|
|
|
u64 MapSharedFunc(u64 address, size_t size, u64 perms, u64 fd) {
|
2019-09-14 14:41:00 +02:00
|
|
|
return reinterpret_cast<u64>(mmap(reinterpret_cast<void *>(address), size, static_cast<int>(perms), MAP_SHARED | ((address) ? MAP_FIXED : 0), static_cast<int>(fd), 0)); // NOLINT(hicpp-signed-bitwise)
|
|
|
|
}
|
|
|
|
|
2019-11-17 21:19:01 +01:00
|
|
|
KSharedMemory::KSharedMemory(const DeviceState &state, u64 address, size_t size, const memory::Permission permission, memory::Type type) : type(type), KObject(state, KType::KSharedMemory) {
|
|
|
|
fd = ASharedMemory_create("", size);
|
2019-09-24 22:54:27 +02:00
|
|
|
if (fd < 0)
|
2019-11-15 19:14:40 +01:00
|
|
|
throw exception("An error occurred while creating shared memory: {}", fd);
|
2019-11-17 21:19:01 +01:00
|
|
|
address = MapSharedFunc(address, size, static_cast<u64>(permission.Get()), static_cast<u64>(fd));
|
|
|
|
if (address == reinterpret_cast<u64>(MAP_FAILED)) // NOLINT(hicpp-signed-bitwise)
|
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
|
|
|
throw exception("An occurred while mapping shared region: {}", strerror(errno));
|
2020-01-01 14:11:25 +01:00
|
|
|
kernel = {address, size, permission};
|
2019-09-14 14:41:00 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 14:11:25 +01:00
|
|
|
u64 KSharedMemory::Map(u64 address, u64 size, memory::Permission permission) {
|
2019-10-13 10:04:47 +02:00
|
|
|
user_pt_regs fregs = {0};
|
|
|
|
fregs.regs[0] = address;
|
|
|
|
fregs.regs[1] = size;
|
2019-11-17 21:19:01 +01:00
|
|
|
fregs.regs[2] = static_cast<u64>(permission.Get());
|
2019-10-13 10:04:47 +02:00
|
|
|
fregs.regs[3] = static_cast<u64>(fd);
|
2020-01-01 14:11:25 +01:00
|
|
|
state.nce->ExecuteFunction(reinterpret_cast<void *>(MapSharedFunc), fregs, state.process->pid);
|
2019-10-13 10:04:47 +02:00
|
|
|
if (reinterpret_cast<void *>(fregs.regs[0]) == MAP_FAILED)
|
|
|
|
throw exception("An error occurred while mapping shared region in child process");
|
2020-01-01 14:11:25 +01:00
|
|
|
guest = {fregs.regs[0], size, permission};
|
2019-10-13 10:04:47 +02:00
|
|
|
return fregs.regs[0];
|
2019-09-14 14:41:00 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 10:04:47 +02:00
|
|
|
u64 UnmapSharedFunc(u64 address, size_t size) {
|
2019-09-14 14:41:00 +02:00
|
|
|
return static_cast<u64>(munmap(reinterpret_cast<void *>(address), size));
|
|
|
|
}
|
|
|
|
|
|
|
|
KSharedMemory::~KSharedMemory() {
|
2020-01-01 14:11:25 +01:00
|
|
|
try {
|
|
|
|
if (guest.valid()) {
|
|
|
|
user_pt_regs fregs = {0};
|
|
|
|
fregs.regs[0] = guest.address;
|
|
|
|
fregs.regs[1] = guest.size;
|
|
|
|
state.nce->ExecuteFunction(reinterpret_cast<void *>(UnmapSharedFunc), fregs, state.process->pid);
|
|
|
|
}
|
|
|
|
if (kernel.valid())
|
|
|
|
UnmapSharedFunc(kernel.address, kernel.size);
|
|
|
|
} catch (const std::exception &) {
|
2019-09-14 14:41:00 +02:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:04:47 +02:00
|
|
|
u64 RemapSharedFunc(u64 address, size_t oldSize, size_t size) {
|
2019-09-24 22:54:27 +02:00
|
|
|
return reinterpret_cast<u64>(mremap(reinterpret_cast<void *>(address), oldSize, size, 0));
|
2019-09-14 14:41:00 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 14:11:25 +01:00
|
|
|
void KSharedMemory::Resize(size_t size) {
|
|
|
|
if (guest.valid()) {
|
|
|
|
user_pt_regs fregs = {0};
|
|
|
|
fregs.regs[0] = guest.address;
|
|
|
|
fregs.regs[1] = guest.size;
|
|
|
|
fregs.regs[2] = size;
|
|
|
|
state.nce->ExecuteFunction(reinterpret_cast<void *>(RemapSharedFunc), fregs, state.process->pid);
|
|
|
|
if (reinterpret_cast<void *>(fregs.regs[0]) == MAP_FAILED)
|
|
|
|
throw exception("An error occurred while remapping shared region in child process");
|
|
|
|
guest.size = size;
|
|
|
|
}
|
|
|
|
if (kernel.valid()) {
|
|
|
|
if (RemapSharedFunc(kernel.address, kernel.size, size) == reinterpret_cast<u64>(MAP_FAILED))
|
|
|
|
throw exception("An occurred while remapping shared region: {}", strerror(errno));
|
|
|
|
kernel.size = size;
|
2019-09-14 14:41:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 10:04:47 +02:00
|
|
|
u64 UpdatePermissionSharedFunc(u64 address, size_t size, u64 perms) {
|
2019-09-14 14:41:00 +02:00
|
|
|
return static_cast<u64>(mprotect(reinterpret_cast<void *>(address), size, static_cast<int>(perms)));
|
|
|
|
}
|
|
|
|
|
2020-01-01 14:11:25 +01:00
|
|
|
void KSharedMemory::UpdatePermission(memory::Permission permission, bool host) {
|
|
|
|
if (guest.valid() && !host) {
|
|
|
|
user_pt_regs fregs = {0};
|
|
|
|
fregs.regs[0] = guest.address;
|
|
|
|
fregs.regs[1] = guest.size;
|
|
|
|
fregs.regs[2] = static_cast<u64>(guest.permission.Get());
|
|
|
|
state.nce->ExecuteFunction(reinterpret_cast<void *>(UpdatePermissionSharedFunc), fregs, state.process->pid);
|
|
|
|
if (static_cast<int>(fregs.regs[0]) == -1)
|
|
|
|
throw exception("An error occurred while updating shared region's permissions in child process");
|
|
|
|
guest.permission = permission;
|
|
|
|
}
|
|
|
|
if (kernel.valid() && host) {
|
|
|
|
if (UpdatePermissionSharedFunc(kernel.address, kernel.size, static_cast<u64>(permission.Get())) == reinterpret_cast<u64>(MAP_FAILED))
|
|
|
|
throw exception("An occurred while remapping shared region: {}", strerror(errno));
|
|
|
|
kernel.permission = permission;
|
2019-09-14 14:41:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 14:11:25 +01:00
|
|
|
memory::MemoryInfo KSharedMemory::GetInfo() {
|
2019-09-24 22:54:27 +02:00
|
|
|
memory::MemoryInfo info{};
|
2020-01-01 14:11:25 +01:00
|
|
|
info.baseAddress = guest.address;
|
|
|
|
info.size = guest.size;
|
|
|
|
info.type = static_cast<u32>(type);
|
2019-09-24 22:54:27 +02:00
|
|
|
info.memoryAttribute.isIpcLocked = (info.ipcRefCount > 0);
|
|
|
|
info.memoryAttribute.isDeviceShared = (info.deviceRefCount > 0);
|
2020-01-01 14:11:25 +01:00
|
|
|
info.r = guest.permission.r;
|
|
|
|
info.w = guest.permission.w;
|
|
|
|
info.x = guest.permission.x;
|
2019-09-24 22:54:27 +02:00
|
|
|
info.ipcRefCount = ipcRefCount;
|
|
|
|
info.deviceRefCount = deviceRefCount;
|
2019-09-14 14:41:00 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
};
|