Skip to content

Instantly share code, notes, and snippets.

@benvanik
Created August 2, 2014 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benvanik/c884fd3ce64cbf65bb11 to your computer and use it in GitHub Desktop.
Save benvanik/c884fd3ce64cbf65bb11 to your computer and use it in GitHub Desktop.
//
// main.cpp
// mem_test
//
// Created by Ben Vanik on 8/1/14.
// Copyright (c) 2014 Ben Vanik. All rights reserved.
//
#include <fcntl.h>
#include <type_traits>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>
uint8_t* mapping_base_;
union {
struct {
uint8_t* v00000000;
uint8_t* v40000000;
uint8_t* v80000000;
uint8_t* vA0000000;
uint8_t* vC0000000;
uint8_t* vE0000000;
};
uint8_t* all_views[6];
} views_;
int main(int argc, const char* argv[]) {
char mapping_path[] = "/xenia/mapping/XXXXXX";
mktemp(mapping_path);
int shm = shm_open(mapping_path, O_CREAT, 0);
int trunced = ftruncate(shm, 0x100000000);
mapping_base_ = (uint8_t*)0x200000000;
static struct {
uint64_t virtual_address_start;
uint64_t virtual_address_end;
uint64_t target_address;
} map_info[] = {
0x00000000, 0x3FFFFFFF, 0x00000000, // (1024mb) - virtual 4k pages
0x40000000, 0x7FFFFFFF, 0x40000000, // (1024mb) - virtual 64k pages
0x80000000, 0x9FFFFFFF, 0x80000000, // (512mb) - xex pages
0xA0000000, 0xBFFFFFFF, 0x00000000, // (512mb) - physical 64k pages
0xC0000000, 0xDFFFFFFF, 0x00000000, // - physical 16mb pages
0xE0000000, 0xFFFFFFFF, 0x00000000, // - physical 4k pages
};
for (size_t n = 0; n < std::extent<decltype(map_info)>::value; n++) {
views_.all_views[n] = (uint8_t*)mmap(
map_info[n].virtual_address_start + mapping_base_,
map_info[n].virtual_address_end - map_info[n].virtual_address_start + 1,
PROT_NONE, MAP_SHARED | MAP_FIXED, shm, map_info[n].target_address);
if (views_.all_views[n] == nullptr) {
printf("nope");
}
}
shm_unlink(mapping_path);
// GPU writeback.
// 0xC... is physical, 0x7F... is virtual. We may need to overlay these.
uint8_t* gpu_base = mapping_base_ + 0xC000000;
mprotect(gpu_base, 0x00100000, PROT_READ | PROT_WRITE);
gpu_base[5] = 123;
int x = gpu_base[5];
printf("%d", x);
// VirtualAlloc(
// Translate(0xC0000000),
// 0x00100000,
// MEM_COMMIT, PAGE_READWRITE);
// DWORD protect = PAGE_NOACCESS;
// if (!VirtualAlloc(Translate(address),
// size,
// MEM_COMMIT, protect)) {
// XELOGE("Unable to map range; commit/protect failed");
// return false;
// }
uint8_t* heap = mapping_base_ + 0xA0000000 + 0x1000;
mprotect(heap, 0x1000, PROT_READ | PROT_WRITE);
heap[0] = 100;
int y = heap[0];
printf("%d", y);
uint8_t* heap2 = mapping_base_ + 0xC0000000 + 0x1000;
mprotect(heap2, 0x1000, PROT_READ | PROT_WRITE);
int z = heap2[0];
printf("%d", z);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment