Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include #include student _ code.h bool is _ entry _ valid ( PageTableEntry pte ) { return ( pte &

#include
#include
#include
#include
#include
#include "student_code.h"
bool is_entry_valid(PageTableEntry pte){
return (pte & single_bit_mask(VALID_BIT))!=0;
}
bool is_read_enabled(PageTableEntry pte){
// todo
// Question: Implement this function. Hint: it can take as little as 1 line.
return is_bit_set(pte, READ_BIT);
}
bool is_write_enabled(PageTableEntry pte){
// todo
return pte & single_bit_mask(WRITE_BIT);
}
bool is_execute_enabled(PageTableEntry pte){
// todo
return pte & single_bit_mask(EXEC_BIT);
}
PFN find_free_page(MMU m){
for (PFN i =0; i NUM_FRAMES; i++){
if (!m.page_used[i]){// Check if the page is not used
return i; // Return the first free page index
}
}
return -1; // Indicate no free pages found
}
PFN convert_PageTableEntry_to_PFN(PageTableEntry pte){
// todo
PageTableEntry mask = ~((PageTableEntry)0); // All bits set to 1
mask = mask >>(NUM_BITS_IN_BYTE * sizeof(PageTableEntry)-4); // Shift to get 4 least significant bits
return pte & mask;
}
/*
* Page Table Functions
*/
PageTableEntry get_pagetableentry__MMU_pagetable(MMU m, VPN vpn){
// todo
return m.page_pointer[vpn];
}
PFN map_page__MMU_pagetable(MMU* m, VPN vpn, bool can_read, bool can_write, bool can_exec){
// todo
PFN pfn = find_free_page(*m);
if (pfn == NUM_FRAMES){
// No free page found
return pfn; // Handle this case accordingly
}
// Set the page as used
m->page_used[pfn]= true;
PageTableEntry entry =0; // Initialize to zero
if (can_read) entry |= single_bit_mask(READ_BIT);
if (can_write) entry |= single_bit_mask(WRITE_BIT);
if (can_exec) entry |= single_bit_mask(EXEC_BIT);
// Set PFN bits
entry |= pfn;
// Assuming page_pointer is a 1D array representing the page table
m->page_pointer[vpn]= entry;
return pfn;
}
Page* get_page(MMU m, VirtualAddress va, bool for_read, bool for_write, bool for_execute){
// todo
VPN vpn = va >> NUM_OFFSET_BITS; // Extract the VPN from the VirtualAddress
PageTableEntry pte = m.get_pagetableentry(m, vpn);
if (!is_entry_valid(pte)) return NULL; // Page not present
if (for_read && !is_read_enabled(pte)) return NULL; // No read permission
if (for_write && !is_write_enabled(pte)) return NULL; // No write permission
if (for_execute && !is_execute_enabled(pte)) return NULL; // No execute permission
PFN pfn = convert_PageTableEntry_to_PFN(pte);
return (Page*)(m.physical_memory + pfn * PAGE_SIZE);
}
char read_byte(MMU m, VirtualAddress va){
// todo
Page* page = get_page(m, va, true, false, false);
if (page){
OFFSET offset = va & get_mask(NUM_OFFSET_BITS);
return page->data[offset];
}
// Handle error if page is NULL, e.g., no read permission or page not present
return -1;
}
void write_byte(MMU m, VirtualAddress va, char val){
// todo
Page* page = get_page(m, va, false, true, false);
if (page){
OFFSET offset = va & get_mask(NUM_OFFSET_BITS);
page->data[offset]= val;
}
}
My two test cases are failing.
These are the taest cases.
"FAILED": [
"map_page__MMU_pagetable",
"end_to_end"
]
These are the test cases.
Test(MMU_PageTable, map_page__MMU_pagetable){
// See the random number
srand( time(NULL));
// Create a new MMU
MMU m = new__MMU_pagetable();
// Create the page pointer (aka the page pointer), so we can use it
m.page_pointer = calloc(NUM_PAGES, sizeof(PageTableEntry));
m.page_used = calloc(NUM_FRAMES, sizeof(bool));
for (int i =0; i NUMBER_OF_REPEATS; i++){
int page_to_test = rand()% NUM_PAGES;
map_page__MMU_pagetable(
&m,// The MMU we created
page_to_test, // The page number we're testing
page_to_test %2%3,// Something random but repeatable
page_to_test %2%5,// Something random but repeatable
page_to_test %2%7// Something random but repeatable
);
PageTableEntry pte = m.page_pointer[page_to_test];
// Check that the PageTableEntry is pointing to the right place
cr_assert(
(get_mask(NUM_PFN_BITS + NUM_OFFSET_BITS) & pte)
==
(i)
);
cr_assert(is_entry_valid(pte));
cr_assert(is_read_enabled(pte)== page_to_test %2%3);
cr_assert(is_write_enabled(pte)== page_to_test %2%5);
cr_assert(is_execute_enabled(pte)== page_to_test %2%6);
}
cleanup_MMU(m);
}
void write_string(MMU m, VirtualAddress va, char* str){
int i;
for (i =0; str[i]!='\0'; i++){
write_byte(m, va+i, str[i]);
}
write_byte(m, va+i,'\0');
}
Test(MMU_PageTable, end_to_end){
// Create a new MMU
MMU m = new__MMU_pagetable();
for (VPN vpn =0; vpn NUM_PAGES ; vpn++){
if (vpn == NUM_PAGES -1){
m.map_page(&m, vpn, true, true, true);
} else {
m.map_page(&m, vpn, vpn %
Can you solve the problem?
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

Which questions should be placed first on a questionnaire?

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago