first commit

This commit is contained in:
f0tt3r 2026-05-31 23:50:47 +02:00
commit 3db69946b4
9 changed files with 420 additions and 0 deletions

View file

@ -0,0 +1,41 @@
#ifndef MMEM_H__
#define MMEM_H__
#include <stdbool.h>
#include <stdint.h>
#define NO_PAGE_FRAMES 20
#define LRU_AGING_BITS 8
// The page_frame struct contains everything that is related to a page_frame
typedef struct page_frame {
// the virtual page that is in this page frame
uint64_t virtual_address;
bool referenced;
uint8_t age;
} page_frame;
// The memory struct manages all page frames of a given system
// You may organize your pages in the array of page_frames however you like
// (as long as you evict and load the right pages for the strategy of course :) )
typedef struct memory {
struct page_frame pages[NO_PAGE_FRAMES];
bool is_full;
uint8_t first;
uint8_t last;
} memory;
// Evicts a page with the virtual address virtual_address
// You do not need to implement this function,
// you should however call this function, whenever you evict a page in your code.
void evict_page(uint64_t virtual_address);
// Loads a page with the virtual address virtual_address
// You do not need to implement this function,
// you should however call this function, whenever you load a page in your code.
void load_page(uint64_t virtual_address);
#endif

View file

@ -0,0 +1,33 @@
#include "mmem_clock.h"
/**
* \brief Initializes the list with the page frames
*
* \return Returns a pointer to the created memory struct
*/
memory* stud_clock_init_list(){
return 0;
}
/**
* \brief maps a page to a page frame
*
* \param mem - the memory struct in which the page should be mapped in
* \param virtual_address - the virtual address of the page
*/
void stud_clock_map_page(memory* mem, uint64_t virtual_address){
return;
}
/**
* \brief function is called, when a page is accessed
* The page that is accessed may or may not be in a page frame
*
* \param virtual_address - the virtual address of the page
* \param mem - the memory struct in which the page should be accessed
*/
void stud_clock_access_page(memory* mem, uint64_t virtual_address){
return;
}

View file

@ -0,0 +1,37 @@
#ifndef MMEM_CLOCK_H__
#define MMEM_CLOCK_H__
// You do not need to change anything here
#include "mmem.h"
#include <stdlib.h>
/**
* \brief Initializes the list with the page frames
*
* \return Returns a pointer to the created memory struct
*/
memory* stud_clock_init_list();
/**
* \brief maps a page to a page frame
*
* \param mem - the memory struct in which the page should be mapped in
* \param virtual_address - the virtual address of the page
*/
void stud_clock_map_page(memory* mem, uint64_t virtual_address);
/**
* \brief function is called, when a page is accessed
* The page that is accessed may or may not be in a page frame
*
* \param virtual_address - the virtual address of the page
* \param mem - the memory struct in which the page should be accessed
*/
void stud_clock_access_page(memory* mem, uint64_t virtual_address);
#endif

View file

@ -0,0 +1,33 @@
#include "mmem_fifo.h"
/**
* \brief Initializes the list with the page frames
*
* \return Returns a pointer to the created memory struct
*/
memory* stud_fifo_init_list(){
return 0;
}
/**
* \brief maps a page to a page frame
*
* \param mem - the memory struct in which the page should be mapped in
* \param virtual_address - the virtual address of the page
*/
void stud_fifo_map_page(memory* mem, uint64_t virtual_address){
return;
}
/**
* \brief function is called, when a page is accessed
* The page that is accessed may or may not be in a page frame
*
* \param virtual_address - the virtual address of the page
* \param mem - the memory struct in which the page should be accessed
*/
void stud_fifo_access_page(memory* mem, uint64_t virtual_address){
return;
}

View file

@ -0,0 +1,35 @@
#ifndef MMEM_FIFO_H__
#define MMEM_FIFO_H__
// You do not need to change anything here
#include "mmem.h"
#include <stdlib.h>
/**
* \brief Initializes the list with the page frames
*
* \return Returns a pointer to the created memory struct
*/
memory* stud_fifo_init_list();
/**
* \brief maps a page to a page frame
*
* \param mem - the memory struct in which the page should be mapped in
* \param virtual_address - the virtual address of the page
*/
void stud_fifo_map_page(memory* mem, uint64_t virtual_address);
/**
* \brief function is called, when a page is accessed
* The page that is accessed may or may not be in a page frame
*
* \param virtual_address - the virtual address of the page
* \param mem - the memory struct in which the page should be accessed
*/
void stud_fifo_access_page(memory* mem, uint64_t virtual_address);
#endif

View file

@ -0,0 +1,42 @@
#include "mmem_lru_aging.h"
/**
* \brief Initializes the list with the page frames
*
* \return Returns a pointer to the created memory struct
*/
memory* stud_lrua_init_list(){
return 0;
}
/**
* \brief maps a page to a page frame
*
* \param mem - the memory struct in which the page should be mapped in
* \param virtual_address - the virtual address of the page
*/
void stud_lrua_map_page(memory* mem, uint64_t virtual_address){
return;
}
/**
* \brief function is called, when a page is accessed
* The page that is accessed may or may not be in a page frame
*
* \param virtual_address - the virtual address of the page
* \param mem - the memory struct in which the page should be accessed
*/
void stud_lrua_access_page(memory* mem, uint64_t virtual_address){
return;
}
/**
* \brief function is called, when the clock is advanced
*
* \param mem - the memory struct on which the algorithm operates on
*/
void stud_lrua_clock_tick(memory* mem){
return;
}

View file

@ -0,0 +1,45 @@
#ifndef MMEM_LRU_AGING_H__
#define MMEM_LRU_AGING_H__
// You do not need to change anything here
#include "mmem.h"
#include <stdlib.h>
/**
* \brief Initializes the list with the page frames
*
* \return Returns a pointer to the created memory struct
*/
memory* stud_lrua_init_list();
/**
* \brief maps a page to a page frame
*
* \param mem - the memory struct in which the page should be mapped in
* \param virtual_address - the virtual address of the page
*/
void stud_lrua_map_page(memory* mem, uint64_t virtual_address);
/**
* \brief function is called, when a page is accessed
* The page that is accessed may or may not be in a page frame
*
* \param virtual_address - the virtual address of the page
* \param mem - the memory struct in which the page should be accessed
*/
void stud_lrua_access_page(memory* mem, uint64_t virtual_address);
/**
* \brief function is called, when the clock is advanced
*
* \param mem - the memory struct on which the algorithm operates on
*/
void stud_lrua_clock_tick(memory* mem);
#endif

View file

@ -0,0 +1,69 @@
#include "page_walk.h"
/**
* \brief Sets the nth bit (flag) to 1 in a 64-bit integer.
*
* \param value The 64-bit integer to modify.
* \param flag The bit position to set (0-63).
*
* \returns The modified 64-bit integer with the nth bit set to 1.
*/
uint64_t stud_set_bit(uint64_t value, unsigned int flag){
return 0;
}
/**
* \brief Tests the nth (flag) bit in a 64-bit integer.
*
* \param value The 64-bit integer to test.
* \param flag The bit position to test (0-63).
*
* \returns true if the nth bit is set to 1, false otherwise.
*/
bool stud_test_bit(uint64_t value, unsigned int flag){
return false;
}
/**
* \brief Clears the nth (flag) bit in a 64-bit integer.
*
* \param value The 64-bit integer to modify.
* \param flag The bit position to clear (0-63).
*
* \returns The modified 64-bit integer with the nth bit cleared.
*/
uint64_t stud_clear_bit(uint64_t value, unsigned int flag){
return 0;
}
/**
* \brief Calculates the offset of the levels in the page table
*
* @param virtual_address: virtual address associated
*
* @return offset of the levels in the page table
*/
uint64_t stud_index_page_level_1(uint64_t virtual_address){
return 0;
}
uint64_t stud_index_page_level_2(uint64_t virtual_address){
return 0;
}
uint64_t stud_index_page_level_3(uint64_t virtual_address){
return 0;
}
uint64_t stud_index_page_level_4(uint64_t virtual_address){
return 0;
}
/**
* \brief Converts a PTE to physical address
*
* @param pte: page table entry
* @param virtual_address: virtual address associated with the PTE
*
* @return the physical address
*/
uint64_t stud_pte_to_physical(uint64_t pte, uint64_t virtual_address){
return 0;
}

View file

@ -0,0 +1,85 @@
#ifndef PAGE_WALK_H__
#define PAGE_WALK_H__
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
// The number of bits used to represent the level index in a page table entry.
#define LEVEL_INDEX_BITS 9
// The number of bits used to represent the page offset in a virtual address.
#define OFFSET_BITS 12
// Define the possible flags for a page table entry
enum flags {
PRESENT, // 1 if page frame present in memory
READWRITE, // 1 if read/write, 0 if readonly
USERSUPERVISOR, // 1 if accessible in user/supervisor, 0 if only supervisor mode
};
enum operation {
READ,
WRITE,
};
enum mode {
USER,
SUPERVISOR,
};
/**
* \brief Sets the nth bit (flag) to 1 in a 64-bit integer.
*
* \param value The 64-bit integer to modify.
* \param flag The bit position to set (0-63).
*
* \returns The modified 64-bit integer with the nth bit set to 1.
*/
uint64_t stud_set_bit(uint64_t value, unsigned int flag);
/**
* \brief Tests the nth (flag) bit in a 64-bit integer.
*
* \param value The 64-bit integer to test.
* \param flag The bit position to test (0-63).
*
* \returns true if the nth bit is set to 1, false otherwise.
*/
bool stud_test_bit(uint64_t value, unsigned int flag);
/**
* \brief Clears the nth (flag) bit in a 64-bit integer.
*
* \param value The 64-bit integer to modify.
* \param flag The bit position to clear (0-63).
*
* \returns The modified 64-bit integer with the nth bit cleared.
*/
uint64_t stud_clear_bit(uint64_t value, unsigned int flag);
/**
* \brief Calculates the offset of the levels in the page table
*
* @param virtual_address: virtual address associated
*
* @return offset of the levels in the page table
*/
uint64_t stud_index_page_level_1(uint64_t virtual_address);
uint64_t stud_index_page_level_2(uint64_t virtual_address);
uint64_t stud_index_page_level_3(uint64_t virtual_address);
uint64_t stud_index_page_level_4(uint64_t virtual_address);
/**
* \brief Converts a PTE to physical address
*
* @param pte: page table entry
* @param virtual_address: virtual address associated with the PTE
*
* @return the physical address
*/
uint64_t stud_pte_to_physical(uint64_t pte, uint64_t virtual_address);
#endif // PAGE_WALK_H__