memory.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/assert.h"
  10. #include "common/common_types.h"
  11. #include "common/logging/log.h"
  12. #include "core/hle/kernel/memory.h"
  13. #include "core/hle/kernel/process.h"
  14. #include "core/hle/kernel/vm_manager.h"
  15. #include "core/memory.h"
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. namespace Kernel {
  18. MemoryRegionInfo memory_regions[3];
  19. /// Size of the APPLICATION, SYSTEM and BASE memory regions (respectively) for each system
  20. /// memory configuration type.
  21. static const u32 memory_region_sizes[8][3] = {
  22. // Old 3DS layouts
  23. {0x04000000, 0x02C00000, 0x01400000}, // 0
  24. {/* This appears to be unused. */}, // 1
  25. {0x06000000, 0x00C00000, 0x01400000}, // 2
  26. {0x05000000, 0x01C00000, 0x01400000}, // 3
  27. {0x04800000, 0x02400000, 0x01400000}, // 4
  28. {0x02000000, 0x04C00000, 0x01400000}, // 5
  29. // New 3DS layouts
  30. {0x07C00000, 0x06400000, 0x02000000}, // 6
  31. {0x0B200000, 0x02E00000, 0x02000000}, // 7
  32. };
  33. void MemoryInit(u32 mem_type) {
  34. // TODO(yuriks): On the n3DS, all o3DS configurations (<=5) are forced to 6 instead.
  35. ASSERT_MSG(mem_type <= 5, "New 3DS memory configuration aren't supported yet!");
  36. ASSERT(mem_type != 1);
  37. // The kernel allocation regions (APPLICATION, SYSTEM and BASE) are laid out in sequence, with
  38. // the sizes specified in the memory_region_sizes table.
  39. VAddr base = 0;
  40. for (int i = 0; i < 3; ++i) {
  41. memory_regions[i].base = base;
  42. memory_regions[i].size = memory_region_sizes[mem_type][i];
  43. memory_regions[i].used = 0;
  44. memory_regions[i].linear_heap_memory = std::make_shared<std::vector<u8>>();
  45. // Reserve enough space for this region of FCRAM.
  46. // We do not want this block of memory to be relocated when allocating from it.
  47. memory_regions[i].linear_heap_memory->reserve(memory_regions[i].size);
  48. base += memory_regions[i].size;
  49. }
  50. // We must've allocated the entire FCRAM by the end
  51. ASSERT(base == Memory::FCRAM_SIZE);
  52. }
  53. void MemoryShutdown() {
  54. for (auto& region : memory_regions) {
  55. region.base = 0;
  56. region.size = 0;
  57. region.used = 0;
  58. region.linear_heap_memory = nullptr;
  59. }
  60. }
  61. MemoryRegionInfo* GetMemoryRegion(MemoryRegion region) {
  62. switch (region) {
  63. case MemoryRegion::APPLICATION:
  64. return &memory_regions[0];
  65. case MemoryRegion::SYSTEM:
  66. return &memory_regions[1];
  67. case MemoryRegion::BASE:
  68. return &memory_regions[2];
  69. default:
  70. UNREACHABLE();
  71. }
  72. }
  73. void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mapping) {}
  74. void MapSharedPages(VMManager& address_space) {}
  75. } // namespace Kernel