<commit_before>#include "mmu.h"
#include "tinycstd.h"

#define VPN_2_BITMASK 0x7FC0000000ULL
#define VPN_1_BITMASK 0x3FE00000
#define VPN_0_BITMASK 0x1FF000

uint64_t create_pt_entry(struct pt_entry* table, uint64_t index, uint64_t ppn, char pt_at_ppn_addr, char u_mode_accessible) {
  struct pt_entry* entry = (table + index);

  entry->ppn = ppn;
  entry->d = 1;
  entry->a = 1;

  if (!pt_at_ppn_addr) {
    entry->x = 1;
    entry->w = 1;
    entry->r = 1;
  }
  
  if (u_mode_accessible)
    entry->u = 1;

  entry->v = 1;

  return ppn << 12;
}

uint64_t ppn_bump;
uint64_t kpalloc() {
    return ppn_bump++;
}

struct pt_entry* retrieve_pt_entry_from_table(struct pt_entry* table, uint64_t index) {
  return (struct pt_entry*) ((table + index)->ppn << 12);
}

void kmap_page(struct pt_entry* table, uint64_t vaddr, char u_mode_accessible) {
  uint64_t vpn_2 = vaddr & VPN_2_BITMASK;
  uint64_t vpn_1 = vaddr & VPN_1_BITMASK;
  uint64_t vpn_0 = vaddr & VPN_0_BITMASK;
  struct pt_entry* mid_pt;
  struct pt_entry* leaf_pt;

  mid_pt = retrieve_pt_entry_from_table(table, vpn_2);
  
  if (!mid_pt->v)
    mid_pt = (struct pt_entry*) create_pt_entry(table, vpn_2, (uint64_t) kpalloc() >> 12, 1, 0);
  
  leaf_pt = retrieve_pt_entry_from_table(mid_pt, vpn_1);

  if (!leaf_pt->v)
    leaf_pt = (struct pt_entry*) create_pt_entry(mid_pt, vpn_1, (uint64_t) kpalloc() >> 12, 1, 0);

  create_pt_entry(leaf_pt, vpn_0, (uint64_t) kpalloc() >> 12, 0, u_mode_accessible);
}

__attribute__((aligned(4096)))
struct pt_entry root_table[512];<commit_msg>Switch from page address to PPN in kmap_page [skip ci]<commit_after>#include "mmu.h"
#include "tinycstd.h"

#define VPN_2_BITMASK 0x7FC0000000ULL
#define VPN_1_BITMASK 0x3FE00000
#define VPN_0_BITMASK 0x1FF000

uint64_t create_pt_entry(struct pt_entry* table, uint64_t index, uint64_t ppn, char pt_at_ppn_addr, char u_mode_accessible) {
  struct pt_entry* entry = (table + index);

  entry->ppn = ppn;
  entry->d = 1;
  entry->a = 1;

  if (!pt_at_ppn_addr) {
    entry->x = 1;
    entry->w = 1;
    entry->r = 1;
  }
  
  if (u_mode_accessible)
    entry->u = 1;

  entry->v = 1;

  return ppn << 12;
}

uint64_t ppn_bump;
uint64_t kpalloc() {
    return ppn_bump++;
}

struct pt_entry* retrieve_pt_entry_from_table(struct pt_entry* table, uint64_t index) {
  return (struct pt_entry*) ((table + index)->ppn << 12);
}

void kmap_page(struct pt_entry* table, uint64_t vaddr, char u_mode_accessible) {
  uint64_t vpn_2 = vaddr & VPN_2_BITMASK;
  uint64_t vpn_1 = vaddr & VPN_1_BITMASK;
  uint64_t vpn_0 = vaddr & VPN_0_BITMASK;
  struct pt_entry* mid_pt;
  struct pt_entry* leaf_pt;

  mid_pt = retrieve_pt_entry_from_table(table, vpn_2);
  
  if (!mid_pt->v)
    mid_pt = (struct pt_entry*) create_pt_entry(table, vpn_2, kpalloc(), 1, 0);
  
  leaf_pt = retrieve_pt_entry_from_table(mid_pt, vpn_1);

  if (!leaf_pt->v)
    leaf_pt = (struct pt_entry*) create_pt_entry(mid_pt, vpn_1, kpalloc(), 1, 0);

  create_pt_entry(leaf_pt, vpn_0, kpalloc(), 0, u_mode_accessible);
}

__attribute__((aligned(4096)))
struct pt_entry root_table[512];
{'fraction_non_alphanumeric': 0.12329656067488644, 'fraction_numerical': 0.054510058403634, 'mean_word_length': 4.554954954954955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4350360', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1305, 'n_words': 280}