From 3db69946b488555840d2c56422d491e2167e04a2 Mon Sep 17 00:00:00 2001 From: f0tt3r Date: Sun, 31 May 2026 23:50:47 +0200 Subject: [PATCH] first commit --- projects/bus/bus-02-memory/mmem.h | 41 ++++++++++ projects/bus/bus-02-memory/mmem_clock.c | 33 ++++++++ projects/bus/bus-02-memory/mmem_clock.h | 37 +++++++++ projects/bus/bus-02-memory/mmem_fifo.c | 33 ++++++++ projects/bus/bus-02-memory/mmem_fifo.h | 35 +++++++++ projects/bus/bus-02-memory/mmem_lru_aging.c | 42 ++++++++++ projects/bus/bus-02-memory/mmem_lru_aging.h | 45 +++++++++++ projects/bus/bus-02-memory/page_walk.c | 69 +++++++++++++++++ projects/bus/bus-02-memory/page_walk.h | 85 +++++++++++++++++++++ 9 files changed, 420 insertions(+) create mode 100644 projects/bus/bus-02-memory/mmem.h create mode 100644 projects/bus/bus-02-memory/mmem_clock.c create mode 100644 projects/bus/bus-02-memory/mmem_clock.h create mode 100644 projects/bus/bus-02-memory/mmem_fifo.c create mode 100644 projects/bus/bus-02-memory/mmem_fifo.h create mode 100644 projects/bus/bus-02-memory/mmem_lru_aging.c create mode 100644 projects/bus/bus-02-memory/mmem_lru_aging.h create mode 100644 projects/bus/bus-02-memory/page_walk.c create mode 100644 projects/bus/bus-02-memory/page_walk.h diff --git a/projects/bus/bus-02-memory/mmem.h b/projects/bus/bus-02-memory/mmem.h new file mode 100644 index 0000000..a805a2b --- /dev/null +++ b/projects/bus/bus-02-memory/mmem.h @@ -0,0 +1,41 @@ +#ifndef MMEM_H__ +#define MMEM_H__ + +#include +#include + +#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 \ No newline at end of file diff --git a/projects/bus/bus-02-memory/mmem_clock.c b/projects/bus/bus-02-memory/mmem_clock.c new file mode 100644 index 0000000..4e25f55 --- /dev/null +++ b/projects/bus/bus-02-memory/mmem_clock.c @@ -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; +} \ No newline at end of file diff --git a/projects/bus/bus-02-memory/mmem_clock.h b/projects/bus/bus-02-memory/mmem_clock.h new file mode 100644 index 0000000..21d73bd --- /dev/null +++ b/projects/bus/bus-02-memory/mmem_clock.h @@ -0,0 +1,37 @@ +#ifndef MMEM_CLOCK_H__ +#define MMEM_CLOCK_H__ + +// You do not need to change anything here + +#include "mmem.h" +#include + +/** + * \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 \ No newline at end of file diff --git a/projects/bus/bus-02-memory/mmem_fifo.c b/projects/bus/bus-02-memory/mmem_fifo.c new file mode 100644 index 0000000..98f0789 --- /dev/null +++ b/projects/bus/bus-02-memory/mmem_fifo.c @@ -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; +} \ No newline at end of file diff --git a/projects/bus/bus-02-memory/mmem_fifo.h b/projects/bus/bus-02-memory/mmem_fifo.h new file mode 100644 index 0000000..505c4d1 --- /dev/null +++ b/projects/bus/bus-02-memory/mmem_fifo.h @@ -0,0 +1,35 @@ +#ifndef MMEM_FIFO_H__ +#define MMEM_FIFO_H__ + +// You do not need to change anything here + +#include "mmem.h" +#include + +/** + * \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 \ No newline at end of file diff --git a/projects/bus/bus-02-memory/mmem_lru_aging.c b/projects/bus/bus-02-memory/mmem_lru_aging.c new file mode 100644 index 0000000..dc66975 --- /dev/null +++ b/projects/bus/bus-02-memory/mmem_lru_aging.c @@ -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; +} \ No newline at end of file diff --git a/projects/bus/bus-02-memory/mmem_lru_aging.h b/projects/bus/bus-02-memory/mmem_lru_aging.h new file mode 100644 index 0000000..33cef06 --- /dev/null +++ b/projects/bus/bus-02-memory/mmem_lru_aging.h @@ -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 + +/** + * \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 \ No newline at end of file diff --git a/projects/bus/bus-02-memory/page_walk.c b/projects/bus/bus-02-memory/page_walk.c new file mode 100644 index 0000000..7b3ae8b --- /dev/null +++ b/projects/bus/bus-02-memory/page_walk.c @@ -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; +} diff --git a/projects/bus/bus-02-memory/page_walk.h b/projects/bus/bus-02-memory/page_walk.h new file mode 100644 index 0000000..8713826 --- /dev/null +++ b/projects/bus/bus-02-memory/page_walk.h @@ -0,0 +1,85 @@ +#ifndef PAGE_WALK_H__ +#define PAGE_WALK_H__ + +#include +#include +#include + +// 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__