<commit_before>#include "nemu.h"
#include "device/mmio.h"

#define PMEM_SIZE (128 * 1024 * 1024)
#define PGSIZE    4096    // Bytes mapped by a page
#define PGMASK          (PGSIZE - 1)    // Mask for bit ops
#define PGROUNDUP(sz)   (((sz)+PGSIZE-1) & ~PGMASK)
#define PGROUNDDOWN(a)  (((a)) & ~PGMASK)

// Control Register flags
#define CR0_PE    0x00000001  // Protection Enable
#define CR0_PG    0x80000000  // Paging

// Page directory and page table constants
#define NR_PDE    1024    // # directory entries per page directory
#define NR_PTE    1024    // # PTEs per page table
#define PGSHFT    12      // log2(PGSIZE)
#define PTXSHFT   12      // Offset of PTX in a linear address
#define PDXSHFT   22      // Offset of PDX in a linear address

// Page table/directory entry flags
#define PTE_P     0x001     // Present
#define PTE_W     0x002     // Writeable
#define PTE_U     0x004     // User
#define PTE_PWT   0x008     // Write-Through
#define PTE_PCD   0x010     // Cache-Disable
#define PTE_A     0x020     // Accessed
#define PTE_D     0x040     // Dirty

typedef uint32_t PTE;
typedef uint32_t PDE;
#define PDX(va)     (((uint32_t)(va) >> PDXSHFT) & 0x3ff)
#define PTX(va)     (((uint32_t)(va) >> PTXSHFT) & 0x3ff)
#define OFF(va)     ((uint32_t)(va) & 0xfff)

// construct virtual address from indexes and offset
#define PGADDR(d, t, o) ((uint32_t)((d) << PDXSHFT | (t) << PTXSHFT | (o)))

// Address in page table or page directory entry
#define PTE_ADDR(pte)   ((uint32_t)(pte) & ~0xfff)

#define pmem_rw(addr, type) *(type *)({\
    Assert(addr < PMEM_SIZE, "physical address(0x%08x) is out of bound, EIP = 0x%08x", addr, cpu.eip); \
    guest_to_host(addr); \
    })

uint8_t pmem[PMEM_SIZE];

static paddr_t page_translate(vaddr_t va) {
  if (cpu.cr0.val & CR0_PG) {
    uint32_t pde_base = cpu.cr3.val;
    uint32_t pdx = PDX(va);
    PDE pde = paddr_read(pde_base + pdx * sizeof(uint32_t), sizeof(uint32_t));
    Log("va = 0x%08x, pde = 0x%08x, pdx = %d", va, pde, pdx);
    Assert(pde & PTE_P, "Can't find %d th page directory entry", pdx);

    uint32_t pte_base = PTE_ADDR(pde);
    uint32_t ptx = PTX(va);
    PTE pte = paddr_read(pte_base + ptx * sizeof(uint32_t), sizeof(uint32_t));
    Assert(pte & PTE_P, "Can't find %d th page directory entry", ptx);

    uint32_t pa_base = PTE_ADDR(pte);
    uint32_t pa_offset = PTE_ADDR(va);
    Assert((pa_base + pa_offset) == va, "page translation failure: (0x%08x + 0x%08x) != 0x%08x", pa_base, pa_offset, va);
    return pa_base + pa_offset;
  } else {
    return va;
  }
}

/* Memory accessing interfaces */

uint32_t paddr_read(paddr_t addr, int len) {
  int map_NO = is_mmio(addr);

  if (map_NO == -1) {
    return pmem_rw(addr, uint32_t) & (~0u >> ((4 - len) << 3));
  } else {
    return mmio_read(addr, len, map_NO);
  }
}

void paddr_write(paddr_t addr, int len, uint32_t data) {
  int map_NO = is_mmio(addr);

  if (map_NO == -1) {
    memcpy(guest_to_host(addr), &data, len);
  } else {
    mmio_write(addr, len, data, map_NO);<commit_msg>>  compile U201414800 sabertazimi Linux 17aa0d73d5e3 4.10.0-33-generic #37~16.04.1-Ubuntu SMP Fri Aug 11 14:07:24 UTC 2017 x86_64 GNU/Linux  03:31:36 up 18:43,  3 users,  load average: 0.23, 0.27, 0.25 43813fa45e9e4ff66006815e4a4d3043f5ea99e7<commit_after>#include "nemu.h"
#include "device/mmio.h"

#define PMEM_SIZE (128 * 1024 * 1024)
#define PGSIZE    4096    // Bytes mapped by a page
#define PGMASK          (PGSIZE - 1)    // Mask for bit ops
#define PGROUNDUP(sz)   (((sz)+PGSIZE-1) & ~PGMASK)
#define PGROUNDDOWN(a)  (((a)) & ~PGMASK)

// Control Register flags
#define CR0_PE    0x00000001  // Protection Enable
#define CR0_PG    0x80000000  // Paging

// Page directory and page table constants
#define NR_PDE    1024    // # directory entries per page directory
#define NR_PTE    1024    // # PTEs per page table
#define PGSHFT    12      // log2(PGSIZE)
#define PTXSHFT   12      // Offset of PTX in a linear address
#define PDXSHFT   22      // Offset of PDX in a linear address

// Page table/directory entry flags
#define PTE_P     0x001     // Present
#define PTE_W     0x002     // Writeable
#define PTE_U     0x004     // User
#define PTE_PWT   0x008     // Write-Through
#define PTE_PCD   0x010     // Cache-Disable
#define PTE_A     0x020     // Accessed
#define PTE_D     0x040     // Dirty

typedef uint32_t PTE;
typedef uint32_t PDE;
#define PDX(va)     (((uint32_t)(va) >> PDXSHFT) & 0x3ff)
#define PTX(va)     (((uint32_t)(va) >> PTXSHFT) & 0x3ff)
#define OFF(va)     ((uint32_t)(va) & 0xfff)

// construct virtual address from indexes and offset
#define PGADDR(d, t, o) ((uint32_t)((d) << PDXSHFT | (t) << PTXSHFT | (o)))

// Address in page table or page directory entry
#define PTE_ADDR(pte)   ((uint32_t)(pte) & ~0xfff)

#define pmem_rw(addr, type) *(type *)({\
    Assert(addr < PMEM_SIZE, "physical address(0x%08x) is out of bound, EIP = 0x%08x", addr, cpu.eip); \
    guest_to_host(addr); \
    })

uint8_t pmem[PMEM_SIZE];

static paddr_t page_translate(vaddr_t va) {
  if (cpu.cr0.val & CR0_PG) {
    uint32_t pde_base = cpu.cr3.val;
    uint32_t pdx = PDX(va);
    PDE pde = paddr_read(pde_base + pdx * sizeof(uint32_t), sizeof(uint32_t));
    Log("va = 0x%08x, pde = 0x%08x, pdx = %d", va, pde, pdx);
    Assert(pde & PTE_P, "Can't find %d th page directory entry", pdx);

    uint32_t pte_base = PTE_ADDR(pde);
    uint32_t ptx = PTX(va);
    PTE pte = paddr_read(pte_base + ptx * sizeof(uint32_t), sizeof(uint32_t));
    Log("va = 0x%08x, pte = 0x%08x, ptx = %d", va, pte, ptx);
    Assert(pte & PTE_P, "Can't find %d th page directory entry", ptx);

    uint32_t pa_base = PTE_ADDR(pte);
    uint32_t pa_offset = PTE_ADDR(va);
    Assert((pa_base + pa_offset) == va, "page translation failure: (0x%08x + 0x%08x) != 0x%08x", pa_base, pa_offset, va);
    return pa_base + pa_offset;
  } else {
    return va;
  }
}

/* Memory accessing interfaces */

uint32_t paddr_read(paddr_t addr, int len) {
  int map_NO = is_mmio(addr);

  if (map_NO == -1) {
    return pmem_rw(addr, uint32_t) & (~0u >> ((4 - len) << 3));
  } else {
    return mmio_read(addr, len, map_NO);
  }
}

void paddr_write(paddr_t addr, int len, uint32_t data) {
  int map_NO = is_mmio(addr);

  if (map_NO == -1) {
    memcpy(guest_to_host(addr), &data, len);
  } else {
    mmio_write(addr, len, data, map_NO);
{'fraction_non_alphanumeric': 0.13751774168112285, 'fraction_numerical': 0.06465857120328024, 'mean_word_length': 3.2027833001988073, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '481864', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2554, 'n_words': 675}