memory.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cinttypes>
  6. #include <map>
  7. #include <memory>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/assert.h"
  11. #include "common/common_types.h"
  12. #include "common/logging/log.h"
  13. #include "core/hle/config_mem.h"
  14. #include "core/hle/kernel/memory.h"
  15. #include "core/hle/kernel/vm_manager.h"
  16. #include "core/hle/result.h"
  17. #include "core/hle/shared_page.h"
  18. #include "core/memory.h"
  19. #include "core/memory_setup.h"
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////
  21. namespace Kernel {
  22. MemoryRegionInfo memory_regions[3];
  23. /// Size of the APPLICATION, SYSTEM and BASE memory regions (respectively) for each system
  24. /// memory configuration type.
  25. static const u32 memory_region_sizes[8][3] = {
  26. // Old 3DS layouts
  27. {0x04000000, 0x02C00000, 0x01400000}, // 0
  28. {/* This appears to be unused. */}, // 1
  29. {0x06000000, 0x00C00000, 0x01400000}, // 2
  30. {0x05000000, 0x01C00000, 0x01400000}, // 3
  31. {0x04800000, 0x02400000, 0x01400000}, // 4
  32. {0x02000000, 0x04C00000, 0x01400000}, // 5
  33. // New 3DS layouts
  34. {0x07C00000, 0x06400000, 0x02000000}, // 6
  35. {0x0B200000, 0x02E00000, 0x02000000}, // 7
  36. };
  37. void MemoryInit(u32 mem_type) {
  38. // TODO(yuriks): On the n3DS, all o3DS configurations (<=5) are forced to 6 instead.
  39. ASSERT_MSG(mem_type <= 5, "New 3DS memory configuration aren't supported yet!");
  40. ASSERT(mem_type != 1);
  41. // The kernel allocation regions (APPLICATION, SYSTEM and BASE) are laid out in sequence, with
  42. // the sizes specified in the memory_region_sizes table.
  43. VAddr base = 0;
  44. for (int i = 0; i < 3; ++i) {
  45. memory_regions[i].base = base;
  46. memory_regions[i].size = memory_region_sizes[mem_type][i];
  47. memory_regions[i].used = 0;
  48. memory_regions[i].linear_heap_memory = std::make_shared<std::vector<u8>>();
  49. // Reserve enough space for this region of FCRAM.
  50. // We do not want this block of memory to be relocated when allocating from it.
  51. memory_regions[i].linear_heap_memory->reserve(memory_regions[i].size);
  52. base += memory_regions[i].size;
  53. }
  54. // We must've allocated the entire FCRAM by the end
  55. ASSERT(base == Memory::FCRAM_SIZE);
  56. using ConfigMem::config_mem;
  57. config_mem.app_mem_type = mem_type;
  58. // app_mem_malloc does not always match the configured size for memory_region[0]: in case the
  59. // n3DS type override is in effect it reports the size the game expects, not the real one.
  60. config_mem.app_mem_alloc = memory_region_sizes[mem_type][0];
  61. config_mem.sys_mem_alloc = static_cast<u32_le>(memory_regions[1].size);
  62. config_mem.base_mem_alloc = static_cast<u32_le>(memory_regions[2].size);
  63. }
  64. void MemoryShutdown() {
  65. for (auto& region : memory_regions) {
  66. region.base = 0;
  67. region.size = 0;
  68. region.used = 0;
  69. region.linear_heap_memory = nullptr;
  70. }
  71. }
  72. MemoryRegionInfo* GetMemoryRegion(MemoryRegion region) {
  73. switch (region) {
  74. case MemoryRegion::APPLICATION:
  75. return &memory_regions[0];
  76. case MemoryRegion::SYSTEM:
  77. return &memory_regions[1];
  78. case MemoryRegion::BASE:
  79. return &memory_regions[2];
  80. default:
  81. UNREACHABLE();
  82. }
  83. }
  84. void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mapping) {}
  85. void MapSharedPages(VMManager& address_space) {}
  86. } // namespace Kernel