Updating logging messages, call DCFlushRange on the actual heap instead of a hardcoded address, general clean up.

This commit is contained in:
Maschell 2021-01-10 13:17:47 +01:00
parent 7154824192
commit f0b78caec3
7 changed files with 41 additions and 43 deletions

View File

@ -118,28 +118,29 @@ void PluginManagement::unloadPlugins(plugin_information_t *gPluginInformation, M
if (freePluginData) { if (freePluginData) {
if (plugin->data.buffer != nullptr) { if (plugin->data.buffer != nullptr) {
if (plugin->data.memoryType == eMemTypeMEM2) { if (plugin->data.memoryType == eMemTypeMEM2) {
DEBUG_FUNCTION_LINE("free %08X", plugin->data.buffer); DEBUG_FUNCTION_LINE("Free plugin data buffer for %s [%08X]", plugin->meta.name, plugin->data.buffer);
free(plugin->data.buffer); free(plugin->data.buffer);
} else if (plugin->data.memoryType == eMemTypeExpHeap) { } else if (plugin->data.memoryType == eMemTypeExpHeap) {
DEBUG_FUNCTION_LINE("free %08X on EXP heap %08X", plugin->data.buffer, plugin->data.heapHandle); DEBUG_FUNCTION_LINE("Free plugin data buffer for %s [%08X on heap %08X]", plugin->meta.name, plugin->data.buffer, plugin->data.heapHandle);
MEMFreeToExpHeap((MEMHeapHandle) plugin->data.heapHandle, plugin->data.buffer); MEMFreeToExpHeap((MEMHeapHandle) plugin->data.heapHandle, plugin->data.buffer);
} else { } else {
DEBUG_FUNCTION_LINE("########################"); DEBUG_FUNCTION_LINE("########################");
DEBUG_FUNCTION_LINE("Failed to free memory from plugin"); DEBUG_FUNCTION_LINE("Failed to free memory from plugin");
DEBUG_FUNCTION_LINE("########################"); DEBUG_FUNCTION_LINE("########################");
} }
plugin->data.buffer = nullptr;
plugin->data.bufferLength = 0; plugin->data.bufferLength = 0;
} else { } else {
DEBUG_FUNCTION_LINE("Plugin has no copy of elf save in memory, can't free it"); DEBUG_FUNCTION_LINE("Plugin has no copy of elf saved in memory, can't free it");
} }
} }
if (plugin->info.allocatedTextMemoryAddress != nullptr) { if (plugin->info.allocatedTextMemoryAddress != nullptr) {
MEMFreeToExpHeap((MEMHeapHandle) pluginHeap, plugin->info.allocatedTextMemoryAddress); MEMFreeToExpHeap((MEMHeapHandle) pluginHeap, plugin->info.allocatedTextMemoryAddress);
DEBUG_FUNCTION_LINE("Freed %08X", plugin->info.allocatedTextMemoryAddress); DEBUG_FUNCTION_LINE("Deleted allocated .text section for plugin %s [%08X]", plugin->meta.name, plugin->info.allocatedTextMemoryAddress);
} }
if (plugin->info.allocatedDataMemoryAddress != nullptr) { if (plugin->info.allocatedDataMemoryAddress != nullptr) {
MEMFreeToExpHeap((MEMHeapHandle) pluginHeap, plugin->info.allocatedDataMemoryAddress); MEMFreeToExpHeap((MEMHeapHandle) pluginHeap, plugin->info.allocatedDataMemoryAddress);
DEBUG_FUNCTION_LINE("Freed %08X", plugin->info.allocatedDataMemoryAddress); DEBUG_FUNCTION_LINE("Deleted allocated .data section for plugin %s [%08X]", plugin->meta.name, plugin->info.allocatedDataMemoryAddress);
} }
for (auto &trampoline : gPluginInformation->trampolines) { for (auto &trampoline : gPluginInformation->trampolines) {

View File

@ -3,5 +3,6 @@
MEMHeapHandle pluginDataHeap __attribute__((section(".data"))) = nullptr; MEMHeapHandle pluginDataHeap __attribute__((section(".data"))) = nullptr;
plugin_information_t *gPluginInformation __attribute__((section(".data"))) = nullptr; plugin_information_t *gPluginInformation __attribute__((section(".data"))) = nullptr;
plugin_information_on_reload_t gLinkOnReload __attribute__((section(".data"))); plugin_information_on_reload_t gLinkOnReload __attribute__((section(".data")));
module_information_t *gModuleData __attribute__((section(".data"))) = nullptr;
module_information_t *gModuleData __attribute__((section(".data"))) = nullptr; uint32_t gPluginDataHeapSize = 0;

View File

@ -7,6 +7,7 @@
extern plugin_information_t *gPluginInformation; extern plugin_information_t *gPluginInformation;
extern MEMHeapHandle pluginDataHeap; extern MEMHeapHandle pluginDataHeap;
extern uint32_t gPluginDataHeapSize;
extern plugin_information_on_reload_t gLinkOnReload; extern plugin_information_on_reload_t gLinkOnReload;
extern module_information_t *gModuleData; extern module_information_t *gModuleData;

View File

@ -8,14 +8,11 @@
#include "plugin/PluginDataPersistence.h" #include "plugin/PluginDataPersistence.h"
#include "plugin/PluginContainerPersistence.h" #include "plugin/PluginContainerPersistence.h"
#include "PluginManagement.h" #include "PluginManagement.h"
#include "utils/logger.h"
#include "hooks.h" #include "hooks.h"
#include "patcher/hooks_patcher_static.h" #include "patcher/hooks_patcher_static.h"
WUMS_MODULE_EXPORT_NAME("homebrew_wupsbackend"); WUMS_MODULE_EXPORT_NAME("homebrew_wupsbackend");
std::vector<PluginContainer> loadPlugins(const std::vector<PluginData> &pluginList, MEMHeapHandle heapHandle);
WUMS_INITIALIZE(args) { WUMS_INITIALIZE(args) {
WHBLogUdpInit(); WHBLogUdpInit();
@ -65,24 +62,25 @@ WUMS_APPLICATION_STARTS() {
// If this address is 0, make sure the header common match the one // If this address is 0, make sure the header common match the one
// in the SetupPayload repo. (I know that's a bad idea) // in the SetupPayload repo. (I know that's a bad idea)
endAddress = (endAddress + 0x100) & 0xFFFFFF00; endAddress = (endAddress + 0x100) & 0xFFFFFF00;
DEBUG_FUNCTION_LINE("endAddress: %08X", endAddress);
DEBUG_FUNCTION_LINE("Create heap"); gPluginDataHeapSize = 0x00FFF000 - endAddress;
pluginDataHeap = MEMCreateExpHeapEx((void *) (endAddress), 0x00FFF000 - endAddress, 0);
if (pluginDataHeap != NULL) { DEBUG_FUNCTION_LINE("Create heap to store plugins");
if (gPluginInformation == NULL) { pluginDataHeap = MEMCreateExpHeapEx((void *) (endAddress), gPluginDataHeapSize, 0);
if (pluginDataHeap != nullptr) {
if (gPluginInformation == nullptr) {
DEBUG_FUNCTION_LINE("Allocate gPluginInformation on heap %08X (size: %d bytes)", pluginDataHeap, sizeof(plugin_information_t));
gPluginInformation = (plugin_information_t *) MEMAllocFromExpHeapEx(pluginDataHeap, sizeof(plugin_information_t), 4); gPluginInformation = (plugin_information_t *) MEMAllocFromExpHeapEx(pluginDataHeap, sizeof(plugin_information_t), 4);
if (gPluginInformation == NULL) { if (gPluginInformation == nullptr) {
DEBUG_FUNCTION_LINE("Failed to allocate global plugin information"); DEBUG_FUNCTION_LINE("Failed to allocate global plugin information");
return; return;
} }
memset((void *) gPluginInformation, 0, sizeof(plugin_information_t)); memset((void *) gPluginInformation, 0, sizeof(plugin_information_t));
} }
DEBUG_FUNCTION_LINE("MEMGetAllocatableSizeForExpHeapEx %d kb", MEMGetAllocatableSizeForExpHeapEx(pluginDataHeap, 4) / 1024); DEBUG_FUNCTION_LINE("Available memory for storing plugins: %d kb", MEMGetAllocatableSizeForExpHeapEx(pluginDataHeap, 4) / 1024);
std::vector<PluginData> pluginList = PluginDataFactory::loadDir("fs:/vol/external01/wiiu/plugins/", pluginDataHeap); std::vector<PluginData> pluginList = PluginDataFactory::loadDir("fs:/vol/external01/wiiu/plugins/", pluginDataHeap);
DEBUG_FUNCTION_LINE("Loaded %d plugin data", pluginList.size()); DEBUG_FUNCTION_LINE("Loaded data for %d plugins.", pluginList.size());
std::vector<PluginContainer> plugins = PluginManagement::loadPlugins(pluginList, pluginDataHeap, gPluginInformation->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); std::vector<PluginContainer> plugins = PluginManagement::loadPlugins(pluginList, pluginDataHeap, gPluginInformation->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
@ -98,7 +96,7 @@ WUMS_APPLICATION_STARTS() {
} }
} }
if (gLinkOnReload.loadOnReload) { if (gLinkOnReload.loadOnReload) {
DEBUG_FUNCTION_LINE("We would now swap the plugins"); DEBUG_FUNCTION_LINE("Reload with new plugin list.");
std::vector<PluginData> pluginDataList; std::vector<PluginData> pluginDataList;
for (int32_t i = 0; i < gLinkOnReload.number_used_plugins; i++) { for (int32_t i = 0; i < gLinkOnReload.number_used_plugins; i++) {
auto pluginData = PluginDataPersistence::load(&gLinkOnReload.plugin_data[i]); auto pluginData = PluginDataPersistence::load(&gLinkOnReload.plugin_data[i]);
@ -108,42 +106,40 @@ WUMS_APPLICATION_STARTS() {
for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) { for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) {
plugin_information_single_t *plugin = &(gPluginInformation->plugin_data[plugin_index]); plugin_information_single_t *plugin = &(gPluginInformation->plugin_data[plugin_index]);
BOOL doDelete = true; BOOL doDelete = true;
DEBUG_FUNCTION_LINE("Check if we can delete %08X", plugin->data.buffer);
for (auto &pluginData: pluginDataList) { for (auto &pluginData: pluginDataList) {
if (pluginData.buffer == plugin->data.buffer) { if (pluginData.buffer == plugin->data.buffer) {
DEBUG_FUNCTION_LINE("We can keep buffer %08X", plugin->data.buffer);
doDelete = false; doDelete = false;
break; break;
} }
} }
if (doDelete) { if (doDelete) {
DEBUG_FUNCTION_LINE("We need to delete the plugin data for plugin %s", plugin->meta.name);
if (plugin->data.buffer != nullptr) { if (plugin->data.buffer != nullptr) {
if (plugin->data.memoryType == eMemTypeMEM2) { if (plugin->data.memoryType == eMemTypeMEM2) {
DEBUG_FUNCTION_LINE("free %08X", plugin->data.buffer); DEBUG_FUNCTION_LINE("Free plugin data buffer for %s [%08X]", plugin->meta.name, plugin->data.buffer);
free(plugin->data.buffer); free(plugin->data.buffer);
} else if (plugin->data.memoryType == eMemTypeExpHeap) { } else if (plugin->data.memoryType == eMemTypeExpHeap) {
DEBUG_FUNCTION_LINE("free %08X on EXP heap %08X", plugin->data.buffer, plugin->data.heapHandle); DEBUG_FUNCTION_LINE("Free plugin data buffer for %s [%08X on heap %08X]", plugin->meta.name, plugin->data.buffer, plugin->data.heapHandle);
MEMFreeToExpHeap((MEMHeapHandle) plugin->data.heapHandle, plugin->data.buffer); MEMFreeToExpHeap((MEMHeapHandle) plugin->data.heapHandle, plugin->data.buffer);
} else { } else {
DEBUG_FUNCTION_LINE("########################"); DEBUG_FUNCTION_LINE("########################");
DEBUG_FUNCTION_LINE("Failed to free memory from plugin"); DEBUG_FUNCTION_LINE("Failed to free memory from plugin");
DEBUG_FUNCTION_LINE("########################"); DEBUG_FUNCTION_LINE("########################");
} }
plugin->data.buffer = NULL; plugin->data.buffer = nullptr;
plugin->data.bufferLength = 0; plugin->data.bufferLength = 0;
} else { } else {
DEBUG_FUNCTION_LINE("Plugin has no copy of elf save in memory, can't free it"); DEBUG_FUNCTION_LINE("Plugin %s has no copy of elf saved in memory, can't free it", plugin->meta.name);
} }
} }
} }
DEBUG_FUNCTION_LINE("unloadPlugins");
PluginManagement::unloadPlugins(gPluginInformation, pluginDataHeap, false); PluginManagement::unloadPlugins(gPluginInformation, pluginDataHeap, false);
std::vector<PluginContainer> plugins = PluginManagement::loadPlugins(pluginDataList, pluginDataHeap, gPluginInformation->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); std::vector<PluginContainer> plugins = PluginManagement::loadPlugins(pluginDataList, pluginDataHeap, gPluginInformation->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
for (auto &pluginContainer : plugins) { for (auto &pluginContainer : plugins) {
DEBUG_FUNCTION_LINE("Saving %s from %s", pluginContainer.getMetaInformation().getName().c_str(), pluginContainer.getMetaInformation().getAuthor().c_str()); DEBUG_FUNCTION_LINE("Stored information for plugin %s ; %s", pluginContainer.getMetaInformation().getName().c_str(), pluginContainer.getMetaInformation().getAuthor().c_str());
if (!PluginContainerPersistence::savePlugin(gPluginInformation, pluginContainer)) { if (!PluginContainerPersistence::savePlugin(gPluginInformation, pluginContainer)) {
DEBUG_FUNCTION_LINE("Failed to save plugin"); DEBUG_FUNCTION_LINE("Failed to save plugin");
} }
@ -152,21 +148,18 @@ WUMS_APPLICATION_STARTS() {
initNeeded = true; initNeeded = true;
} }
if (pluginDataHeap != NULL) { if (pluginDataHeap != nullptr) {
std::vector<PluginContainer> plugins = PluginContainerPersistence::loadPlugins(gPluginInformation); std::vector<PluginContainer> plugins = PluginContainerPersistence::loadPlugins(gPluginInformation);
PluginManagement::doRelocations(plugins, gPluginInformation->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); PluginManagement::doRelocations(plugins, gPluginInformation->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
// PluginManagement::memsetBSS(plugins); // PluginManagement::memsetBSS(plugins);
DCFlushRange((void *) 0x00800000, 0x00800000); DCFlushRange((void *) pluginDataHeap, gPluginDataHeapSize);
ICInvalidateRange((void *) 0x00800000, 0x00800000); ICInvalidateRange((void *) pluginDataHeap, gPluginDataHeapSize);
if (initNeeded) { if (initNeeded) {
PluginManagement::callInitHooks(gPluginInformation); PluginManagement::callInitHooks(gPluginInformation);
initNeeded = false;
} }
PluginManagement::PatchFunctionsAndCallHooks(gPluginInformation); PluginManagement::PatchFunctionsAndCallHooks(gPluginInformation);
} }
return;
} }

View File

@ -19,10 +19,12 @@ void PluginData::freeMemory() {
switch (memoryType) { switch (memoryType) {
default: default:
case eMemTypeExpHeap: case eMemTypeExpHeap:
DEBUG_FUNCTION_LINE("Free PluginData buffer %08X on heap %08X", buffer, this->heapHandle);
MEMFreeToExpHeap(this->heapHandle, buffer); MEMFreeToExpHeap(this->heapHandle, buffer);
this->buffer = nullptr; this->buffer = nullptr;
break; break;
case eMemTypeMEM2: case eMemTypeMEM2:
DEBUG_FUNCTION_LINE("Free PluginData buffer %08X on default heap", buffer);
free(this->buffer); free(this->buffer);
this->buffer = nullptr; this->buffer = nullptr;
break; break;

View File

@ -33,7 +33,7 @@ enum eMemoryTypes {
class PluginData { class PluginData {
public: public:
~PluginData() = default; ~PluginData()= default;
void freeMemory(); void freeMemory();

View File

@ -21,9 +21,9 @@ void fillPluginInformation(plugin_information *out, PluginMetaInformation *metaI
extern "C" int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size) { extern "C" int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size) {
gLinkOnReload.number_used_plugins = 0; gLinkOnReload.number_used_plugins = 0;
if (plugin_data_handle_list != NULL && plugin_data_handle_list_size != 0) { if (plugin_data_handle_list != nullptr && plugin_data_handle_list_size != 0) {
for (uint32_t i = 0; i < plugin_data_handle_list_size; i++) { for (uint32_t i = 0; i < plugin_data_handle_list_size; i++) {
auto handle = plugin_data_handle_list[i]; plugin_data_handle handle = plugin_data_handle_list[i];
PluginData *pluginData = (PluginData *) handle; PluginData *pluginData = (PluginData *) handle;
DEBUG_FUNCTION_LINE("Saving plugin data %08X", pluginData); DEBUG_FUNCTION_LINE("Saving plugin data %08X", pluginData);
PluginDataPersistence::save(&gLinkOnReload.plugin_data[gLinkOnReload.number_used_plugins], *pluginData); PluginDataPersistence::save(&gLinkOnReload.plugin_data[gLinkOnReload.number_used_plugins], *pluginData);
@ -41,7 +41,7 @@ extern "C" int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_
} }
extern "C" int32_t WUPSDeletePluginContainer(const plugin_container_handle *handle_list, uint32_t handle_list_size) { extern "C" int32_t WUPSDeletePluginContainer(const plugin_container_handle *handle_list, uint32_t handle_list_size) {
if (handle_list != NULL && handle_list_size != 0) { if (handle_list != nullptr && handle_list_size != 0) {
for (uint32_t i = 0; i < handle_list_size; i++) { for (uint32_t i = 0; i < handle_list_size; i++) {
auto handle = handle_list[i]; auto handle = handle_list[i];
PluginContainer *pluginContainer = (PluginContainer *) handle; PluginContainer *pluginContainer = (PluginContainer *) handle;
@ -57,7 +57,7 @@ extern "C" int32_t WUPSDeletePluginData(const plugin_data_handle *plugin_data_ha
for (uint32_t i = 0; i < plugin_data_handle_list_size; i++) { for (uint32_t i = 0; i < plugin_data_handle_list_size; i++) {
auto handle = plugin_data_handle_list[i]; auto handle = plugin_data_handle_list[i];
auto *pluginData = (PluginData *) handle; auto *pluginData = (PluginData *) handle;
DEBUG_FUNCTION_LINE("delete plugin data %08X", pluginData); DEBUG_FUNCTION_LINE("Delete plugin data: %08X", pluginData);
delete pluginData; delete pluginData;
} }
} }
@ -66,9 +66,9 @@ extern "C" int32_t WUPSDeletePluginData(const plugin_data_handle *plugin_data_ha
extern "C" int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_data_handle *out) { extern "C" int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_data_handle *out) {
std::optional<PluginData> pluginData; std::optional<PluginData> pluginData;
if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_PATH) { if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_PATH && path != nullptr) {
pluginData = PluginDataFactory::load(path, pluginDataHeap); pluginData = PluginDataFactory::load(path, pluginDataHeap);
} else if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_BUFFER) { } else if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_BUFFER && buffer != nullptr && size > 0) {
std::vector<uint8_t> data(size); std::vector<uint8_t> data(size);
memcpy(&data[0], buffer, size); memcpy(&data[0], buffer, size);
pluginData = PluginDataFactory::load(data, pluginDataHeap); pluginData = PluginDataFactory::load(data, pluginDataHeap);
@ -86,7 +86,7 @@ extern "C" int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType,
return ERROR_INVALID_ARG; return ERROR_INVALID_ARG;
} else { } else {
auto *pluginDataHandle = new PluginData(pluginData.value()); auto *pluginDataHandle = new PluginData(pluginData.value());
DEBUG_FUNCTION_LINE("Saving handle %08X", pluginDataHandle); DEBUG_FUNCTION_LINE("Saving plugin data handle: %08X", pluginDataHandle);
*out = (uint32_t) pluginDataHandle; *out = (uint32_t) pluginDataHandle;
} }
@ -115,11 +115,11 @@ extern "C" int32_t WUPSGetPluginMetaInformation(GetPluginInformationInputType in
} }
if (!pluginInfo) { if (!pluginInfo) {
DEBUG_FUNCTION_LINE("Failed to load plugin"); DEBUG_FUNCTION_LINE("Failed to load plugin meta information");
return ERROR_FILE_NOT_FOUND; return ERROR_FILE_NOT_FOUND;
} }
DEBUG_FUNCTION_LINE("loaded plugin"); DEBUG_FUNCTION_LINE("Loaded plugin meta information");
if (output == nullptr) { if (output == nullptr) {
return ERROR_INVALID_ARG; return ERROR_INVALID_ARG;