2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 09:53:10 +00:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
2019-03-28 22:06:50 +00:00
|
|
|
#include <stratosphere.hpp>
|
2018-04-24 09:53:10 +00:00
|
|
|
|
2019-06-17 23:29:09 +00:00
|
|
|
class MapUtils {
|
2018-04-24 09:53:10 +00:00
|
|
|
public:
|
2018-04-27 00:04:30 +00:00
|
|
|
struct AddressSpaceInfo {
|
|
|
|
u64 heap_base;
|
|
|
|
u64 heap_size;
|
|
|
|
u64 heap_end;
|
|
|
|
u64 map_base;
|
|
|
|
u64 map_size;
|
|
|
|
u64 map_end;
|
|
|
|
u64 addspace_base;
|
|
|
|
u64 addspace_size;
|
|
|
|
u64 addspace_end;
|
|
|
|
};
|
|
|
|
static Result GetAddressSpaceInfo(AddressSpaceInfo *out, Handle process_h);
|
2018-04-24 09:53:10 +00:00
|
|
|
static Result LocateSpaceForMapDeprecated(u64 *out, u64 out_size);
|
|
|
|
static Result LocateSpaceForMapModern(u64 *out, u64 out_size);
|
|
|
|
static Result LocateSpaceForMap(u64 *out, u64 out_size);
|
2018-04-27 01:13:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AutoCloseMap {
|
|
|
|
private:
|
2018-07-02 14:26:03 +00:00
|
|
|
void *mapped_address = nullptr;
|
|
|
|
u64 base_address = 0;
|
|
|
|
u64 size = 0;
|
|
|
|
Handle process_handle = 0;
|
2018-04-27 01:13:55 +00:00
|
|
|
public:
|
|
|
|
~AutoCloseMap() {
|
|
|
|
Close();
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-27 01:13:55 +00:00
|
|
|
void *GetMappedAddress() {
|
|
|
|
return this->mapped_address;
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-27 01:13:55 +00:00
|
|
|
Result Open(Handle process_h, u64 address, u64 size) {
|
|
|
|
u64 try_address;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
|
|
|
/* Find an address to map at. */
|
|
|
|
R_TRY(MapUtils::LocateSpaceForMap(&try_address, size));
|
|
|
|
|
|
|
|
/* Actually map at address. */
|
|
|
|
void *try_map_address = reinterpret_cast<void *>(try_address);
|
|
|
|
R_TRY(svcMapProcessMemory(try_map_address, process_h, address, size));
|
|
|
|
|
|
|
|
this->mapped_address = try_map_address;
|
2018-04-27 01:13:55 +00:00
|
|
|
this->process_handle = process_h;
|
|
|
|
this->base_address = address;
|
|
|
|
this->size = size;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-27 01:13:55 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-27 01:13:55 +00:00
|
|
|
void Close() {
|
|
|
|
if (this->mapped_address) {
|
|
|
|
if (R_FAILED(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->base_address, this->size))) {
|
2019-05-10 10:28:18 +00:00
|
|
|
std::abort();
|
2018-04-27 01:13:55 +00:00
|
|
|
}
|
2018-04-27 01:43:26 +00:00
|
|
|
this->mapped_address = NULL;
|
2018-04-27 01:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-27 02:27:52 +00:00
|
|
|
};
|