<commit_before>#include <arch/x64/control_registers.h>
#include <arch/x64/paging.h>
#include <truth/heap.h>
#include <truth/log.h>
#include <truth/physical_allocator.h>
#include <truth/panic.h>
#include <truth/slab.h>
#include <truth/string.h>
#include <truth/types.h>

extern void invalidate_tlb(void);
extern uint64_t invalidate_page(void *);

#define pl1_Count 512
#define pl2_Count 512
#define pl3_Count 512
#define pl4_Count 512

#define pl1_offset 12
#define pl1_mask   0777
#define pl2_offset 21
#define pl2_mask   0777
#define pl3_offset 30
#define pl3_mask   0777
#define pl4_offset 39
#define pl4_mask   0777

#define align_page(x) (x & ~0xfff)

// Any even number will do
#define free_page_entry 0xccccccccccccccul

#define Phys_Addr_Mask 0x000ffffffffff000

typedef uint64_t pl4_entry;
typedef uint64_t pl3_entry;
typedef uint64_t pl2_entry;
typedef uint64_t pl1_entry;


phys_addr page_entry_to_phys(uint64_t entry) {
    return entry & Phys_Addr_Mask;
}

uint64_t page_entry_clone(phys_addr new_entry, uint64_t old_entry) {
    return (old_entry & ~Phys_Addr_Mask) | new_entry;
}

static inline size_t pl4_index(void *address) {
    return (uintptr_t)address >> pl4_offset & pl4_mask;
}

static inline size_t pl3_index(void *address) {
    return ((uintptr_t)address >> pl3_offset) & pl3_mask;
}

static inline size_t pl2_index(void *address) {
    return ((uintptr_t)address >> pl2_offset) & pl2_mask;
}

static inline size_t pl1_index(void *address) {
    return ((uintptr_t)address >> pl1_offset) & pl1_mask;
}

static pl4_entry *get_pl4(void) {
    return (pl4_entry *)01777777777777777770000;
}

static pl3_entry *get_pl3_index(size_t pl4_index) {
    return (pl3_entry *)(01777777777777770000000 | (pl4_index << 12));
}

static pl2_entry *get_pl2_index(size_t pl4_index, size_t pl3_index) {
    return (pl2_entry *)(01777777777770000000000 |
                   (pl4_index << 21) |
                   (pl3_index << 12));
}

static pl1_entry *get_pl1_index(size_t pl4_index, size_t pl3_index,
                          size_t pl2_index) {
    return (pl1_entry *)(01777777770000000000000 |
                   (pl4_index << 30) |
                   (pl3_index << 21) |
                   (pl2_index << 12));
}

static pl3_entry *get_pl3(void *address) {
    return get_pl3_index(pl4_index(address));
}

static pl2_entry *get_pl2(void *address) {
    return get_pl2_index(pl4_index(address), pl3_index(address));
}

static pl1_entry *get_pl1(void *address) {
    return get_pl1_index(pl4_index(address), pl3_index(address), pl2_index(address));
}

static inline bool is_pl3_present(pl4_entry *pl4,
                                  void *address) {
    return (pl4[pl4_index(address)] & Memory_Present) == 1;
}

static inline bool is_pl2_present(pl3_entry *level_three, void *address) {
    return level_three[pl3_index(address)] & Memory_Present;
}

static inline bool is_pl1_present(pl2_entry *level_two, void *address) {
    return level_two[pl2_index(address)] & Memory_Present;
}

static inline bool is_Memory_Present(pl1_entry *level_one, void *address) {
    return level_one[pl1_index(address)] & Memory_Present;
}

void *virt_from_indices(size_t i4, size_t i3, size_t i2, size_t i1) {
    return (void *)((i4 << pl4_offset) |
           (i3 << pl3_offset) |
           (i2 << pl2_offset) |
           (i1 << pl1_offset));
}

void debug_paging(void) {
    pl4_entry *pl4 = get_pl4();
    for (size_t i = 0; i < pl4_Count; ++i) {
        if (pl4[i] * Memory_Present) {
            logf(Log_Debug, "%zu has %lx\n", i, pl4[i]);
        }
    }
    logf(Log_Debug, "kernel start: %p\n", Kernel_Virtual_Start);
    logf(Log_Debug, "kernel end: %p\n", Kernel_Virtual_End);
}

enum status checked map_page(void *virtual_address, phys_addr phys_address,
                             enum memory_attributes permissions) {

    pl4_entry *pl4 = get_pl4();
    logf(Log_Debug, "Mapping: %p -> %lx\n", virtual_address, phys_address);
    if (!is_pl3_present(pl4, virtual_address)) {
        phys_addr phys_address = physical_alloc(1);
        pl4[pl4_index(virtual_address)] =
            (phys_address | permissions | Memory_User_Access |
             Memory_Present);
        invalidate_tlb();
    }

    pl3_entry *level_three = get_pl3(virtual_address);
    if (!is_pl2_present(level_three, virtual_address)) {
        phys_addr phys_address = physical_alloc(1);
        level_three[pl3_index(virtual_address)] =
            (phys_address | permissions | Memory_User_Access |
             Memory_Present);
        invalidate_tlb();
    }

    pl2_entry *level_two = get_pl2(virtual_address);
    if (!is_pl1_present(level_two, virtual_address)) {
        phys_addr phys_address = physical_alloc(1);
        level_two[pl2_index(virtual_address)] =
            (phys_address | permissions | Memory_User_Access |
             Memory_Present);
        invalidate_tlb();
    }

    pl1_entry *level_one = get_pl1(virtual_address);
    if (is_Memory_Present(level_one, virtual_address)) {
        logf(Log_Debug, "The virtual address %p is already present\n",
             virtual_address);
        return Error_Present;
    }

    level_one[pl1_index(virtual_address)] =
        (phys_address | permissions | Memory_Present);
    invalidate_tlb();

    int *test = virtual_address;
    logf(Log_Debug, "Testing: %x\n", test[0]);

    return Ok;
}

void unmap_page(void *address, bool free_physical_memory) {
    pl4_entry *pl4 = get_pl4();
    if (is_pl3_present(pl4, address) &&
        is_pl2_present(get_pl3(address), address) &&
        is_pl1_present(get_pl2(address), address)) {
        pl1_entry *level_one = get_pl1(address);


        if (free_physical_memory) {
            physical_free(align_page(level_one[pl1_index(address)]), 1);
        }
        level_one[pl1_index(address)] = free_page_entry;<commit_msg>Get rid of annoying paging debug message<commit_after>#include <arch/x64/control_registers.h>
#include <arch/x64/paging.h>
#include <truth/heap.h>
#include <truth/log.h>
#include <truth/physical_allocator.h>
#include <truth/panic.h>
#include <truth/slab.h>
#include <truth/string.h>
#include <truth/types.h>

extern void invalidate_tlb(void);
extern uint64_t invalidate_page(void *);

#define pl1_Count 512
#define pl2_Count 512
#define pl3_Count 512
#define pl4_Count 512

#define pl1_offset 12
#define pl1_mask   0777
#define pl2_offset 21
#define pl2_mask   0777
#define pl3_offset 30
#define pl3_mask   0777
#define pl4_offset 39
#define pl4_mask   0777

#define align_page(x) (x & ~0xfff)

// Any even number will do
#define free_page_entry 0xccccccccccccccul

#define Phys_Addr_Mask 0x000ffffffffff000

typedef uint64_t pl4_entry;
typedef uint64_t pl3_entry;
typedef uint64_t pl2_entry;
typedef uint64_t pl1_entry;


phys_addr page_entry_to_phys(uint64_t entry) {
    return entry & Phys_Addr_Mask;
}

uint64_t page_entry_clone(phys_addr new_entry, uint64_t old_entry) {
    return (old_entry & ~Phys_Addr_Mask) | new_entry;
}

static inline size_t pl4_index(void *address) {
    return (uintptr_t)address >> pl4_offset & pl4_mask;
}

static inline size_t pl3_index(void *address) {
    return ((uintptr_t)address >> pl3_offset) & pl3_mask;
}

static inline size_t pl2_index(void *address) {
    return ((uintptr_t)address >> pl2_offset) & pl2_mask;
}

static inline size_t pl1_index(void *address) {
    return ((uintptr_t)address >> pl1_offset) & pl1_mask;
}

static pl4_entry *get_pl4(void) {
    return (pl4_entry *)01777777777777777770000;
}

static pl3_entry *get_pl3_index(size_t pl4_index) {
    return (pl3_entry *)(01777777777777770000000 | (pl4_index << 12));
}

static pl2_entry *get_pl2_index(size_t pl4_index, size_t pl3_index) {
    return (pl2_entry *)(01777777777770000000000 |
                   (pl4_index << 21) |
                   (pl3_index << 12));
}

static pl1_entry *get_pl1_index(size_t pl4_index, size_t pl3_index,
                          size_t pl2_index) {
    return (pl1_entry *)(01777777770000000000000 |
                   (pl4_index << 30) |
                   (pl3_index << 21) |
                   (pl2_index << 12));
}

static pl3_entry *get_pl3(void *address) {
    return get_pl3_index(pl4_index(address));
}

static pl2_entry *get_pl2(void *address) {
    return get_pl2_index(pl4_index(address), pl3_index(address));
}

static pl1_entry *get_pl1(void *address) {
    return get_pl1_index(pl4_index(address), pl3_index(address), pl2_index(address));
}

static inline bool is_pl3_present(pl4_entry *pl4,
                                  void *address) {
    return (pl4[pl4_index(address)] & Memory_Present) == 1;
}

static inline bool is_pl2_present(pl3_entry *level_three, void *address) {
    return level_three[pl3_index(address)] & Memory_Present;
}

static inline bool is_pl1_present(pl2_entry *level_two, void *address) {
    return level_two[pl2_index(address)] & Memory_Present;
}

static inline bool is_Memory_Present(pl1_entry *level_one, void *address) {
    return level_one[pl1_index(address)] & Memory_Present;
}

void *virt_from_indices(size_t i4, size_t i3, size_t i2, size_t i1) {
    return (void *)((i4 << pl4_offset) |
           (i3 << pl3_offset) |
           (i2 << pl2_offset) |
           (i1 << pl1_offset));
}

void debug_paging(void) {
    pl4_entry *pl4 = get_pl4();
    for (size_t i = 0; i < pl4_Count; ++i) {
        if (pl4[i] * Memory_Present) {
            logf(Log_Debug, "%zu has %lx\n", i, pl4[i]);
        }
    }
    logf(Log_Debug, "kernel start: %p\n", Kernel_Virtual_Start);
    logf(Log_Debug, "kernel end: %p\n", Kernel_Virtual_End);
}

enum status checked map_page(void *virtual_address, phys_addr phys_address,
                             enum memory_attributes permissions) {

    pl4_entry *pl4 = get_pl4();
    if (!is_pl3_present(pl4, virtual_address)) {
        phys_addr phys_address = physical_alloc(1);
        pl4[pl4_index(virtual_address)] =
            (phys_address | permissions | Memory_User_Access |
             Memory_Present);
        invalidate_tlb();
    }

    pl3_entry *level_three = get_pl3(virtual_address);
    if (!is_pl2_present(level_three, virtual_address)) {
        phys_addr phys_address = physical_alloc(1);
        level_three[pl3_index(virtual_address)] =
            (phys_address | permissions | Memory_User_Access |
             Memory_Present);
        invalidate_tlb();
    }

    pl2_entry *level_two = get_pl2(virtual_address);
    if (!is_pl1_present(level_two, virtual_address)) {
        phys_addr phys_address = physical_alloc(1);
        level_two[pl2_index(virtual_address)] =
            (phys_address | permissions | Memory_User_Access |
             Memory_Present);
        invalidate_tlb();
    }

    pl1_entry *level_one = get_pl1(virtual_address);
    if (is_Memory_Present(level_one, virtual_address)) {
        logf(Log_Debug, "The virtual address %p is already present\n",
             virtual_address);
        return Error_Present;
    }

    level_one[pl1_index(virtual_address)] =
        (phys_address | permissions | Memory_Present);
    invalidate_tlb();

    return Ok;
}

void unmap_page(void *address, bool free_physical_memory) {
    pl4_entry *pl4 = get_pl4();
    if (is_pl3_present(pl4, address) &&
        is_pl2_present(get_pl3(address), address) &&
        is_pl1_present(get_pl2(address), address)) {
        pl1_entry *level_one = get_pl1(address);


        if (free_physical_memory) {
            physical_free(align_page(level_one[pl1_index(address)]), 1);
        }
        level_one[pl1_index(address)] = free_page_entry;
{'fraction_non_alphanumeric': 0.10771230502599653, 'fraction_numerical': 0.05277296360485269, 'mean_word_length': 3.576130055511499, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 18, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2849426', 'n_tokens_mistral': 4433, 'n_tokens_neox': 4121, 'n_words': 814}