memory.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include <memory>
  6. #include <utility>
  7. #include <vector>
  8. #include "audio_core/audio_core.h"
  9. #include "common/common_types.h"
  10. #include "common/logging/log.h"
  11. #include "core/hle/config_mem.h"
  12. #include "core/hle/kernel/memory.h"
  13. #include "core/hle/kernel/vm_manager.h"
  14. #include "core/hle/result.h"
  15. #include "core/hle/shared_page.h"
  16. #include "core/memory.h"
  17. #include "core/memory_setup.h"
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. namespace Kernel {
  20. static MemoryRegionInfo memory_regions[3];
  21. /// Size of the APPLICATION, SYSTEM and BASE memory regions (respectively) for each sytem
  22. /// memory configuration type.
  23. static const u32 memory_region_sizes[8][3] = {
  24. // Old 3DS layouts
  25. {0x04000000, 0x02C00000, 0x01400000}, // 0
  26. {/* This appears to be unused. */}, // 1
  27. {0x06000000, 0x00C00000, 0x01400000}, // 2
  28. {0x05000000, 0x01C00000, 0x01400000}, // 3
  29. {0x04800000, 0x02400000, 0x01400000}, // 4
  30. {0x02000000, 0x04C00000, 0x01400000}, // 5
  31. // New 3DS layouts
  32. {0x07C00000, 0x06400000, 0x02000000}, // 6
  33. {0x0B200000, 0x02E00000, 0x02000000}, // 7
  34. };
  35. void MemoryInit(u32 mem_type) {
  36. // TODO(yuriks): On the n3DS, all o3DS configurations (<=5) are forced to 6 instead.
  37. ASSERT_MSG(mem_type <= 5, "New 3DS memory configuration aren't supported yet!");
  38. ASSERT(mem_type != 1);
  39. // The kernel allocation regions (APPLICATION, SYSTEM and BASE) are laid out in sequence, with
  40. // the sizes specified in the memory_region_sizes table.
  41. VAddr base = 0;
  42. for (int i = 0; i < 3; ++i) {
  43. memory_regions[i].base = base;
  44. memory_regions[i].size = memory_region_sizes[mem_type][i];
  45. memory_regions[i].used = 0;
  46. memory_regions[i].linear_heap_memory = std::make_shared<std::vector<u8>>();
  47. // Reserve enough space for this region of FCRAM.
  48. // We do not want this block of memory to be relocated when allocating from it.
  49. memory_regions[i].linear_heap_memory->reserve(memory_regions[i].size);
  50. base += memory_regions[i].size;
  51. }
  52. // We must've allocated the entire FCRAM by the end
  53. ASSERT(base == Memory::FCRAM_SIZE);
  54. using ConfigMem::config_mem;
  55. config_mem.app_mem_type = mem_type;
  56. // app_mem_malloc does not always match the configured size for memory_region[0]: in case the
  57. // n3DS type override is in effect it reports the size the game expects, not the real one.
  58. config_mem.app_mem_alloc = memory_region_sizes[mem_type][0];
  59. config_mem.sys_mem_alloc = memory_regions[1].size;
  60. config_mem.base_mem_alloc = memory_regions[2].size;
  61. }
  62. void MemoryShutdown() {
  63. for (auto& region : memory_regions) {
  64. region.base = 0;
  65. region.size = 0;
  66. region.used = 0;
  67. region.linear_heap_memory = nullptr;
  68. }
  69. }
  70. MemoryRegionInfo* GetMemoryRegion(MemoryRegion region) {
  71. switch (region) {
  72. case MemoryRegion::APPLICATION:
  73. return &memory_regions[0];
  74. case MemoryRegion::SYSTEM:
  75. return &memory_regions[1];
  76. case MemoryRegion::BASE:
  77. return &memory_regions[2];
  78. default:
  79. UNREACHABLE();
  80. }
  81. }
  82. }
  83. namespace Memory {
  84. namespace {
  85. struct MemoryArea {
  86. u32 base;
  87. u32 size;
  88. const char* name;
  89. };
  90. // We don't declare the IO regions in here since its handled by other means.
  91. static MemoryArea memory_areas[] = {
  92. {VRAM_VADDR, VRAM_SIZE, "VRAM"}, // Video memory (VRAM)
  93. };
  94. }
  95. void Init() {
  96. InitMemoryMap();
  97. LOG_DEBUG(HW_Memory, "initialized OK");
  98. }
  99. void InitLegacyAddressSpace(Kernel::VMManager& address_space) {
  100. using namespace Kernel;
  101. for (MemoryArea& area : memory_areas) {
  102. auto block = std::make_shared<std::vector<u8>>(area.size);
  103. address_space
  104. .MapMemoryBlock(area.base, std::move(block), 0, area.size, MemoryState::Private)
  105. .Unwrap();
  106. }
  107. auto cfg_mem_vma = address_space
  108. .MapBackingMemory(CONFIG_MEMORY_VADDR, (u8*)&ConfigMem::config_mem,
  109. CONFIG_MEMORY_SIZE, MemoryState::Shared)
  110. .MoveFrom();
  111. address_space.Reprotect(cfg_mem_vma, VMAPermission::Read);
  112. auto shared_page_vma = address_space
  113. .MapBackingMemory(SHARED_PAGE_VADDR, (u8*)&SharedPage::shared_page,
  114. SHARED_PAGE_SIZE, MemoryState::Shared)
  115. .MoveFrom();
  116. address_space.Reprotect(shared_page_vma, VMAPermission::Read);
  117. AudioCore::AddAddressSpace(address_space);
  118. }
  119. } // namespace