Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a MMU simulator to help us better understand virtual memory management. Please read mmu_lib.c and mmu.c . Before diving into the code, read the

Implement a MMU simulator to help us better understand virtual memory management. Please read mmu_lib.c and mmu.c. Before diving into the code, read the comments in mmu_lib.c first. After understanding the code, fix the FIXMEs in mmu_lib.c.

sorry it long please help!

mmu.c

image text in transcribed

mmu_lib.c

#include
#include
// This file implements a naive 32-bits memory management unit(MMU) simulator
// to do virtual memory control.
//
// NOTE1: The number of bits of virtual (offset) for each layer may be different
// from the real Intel CPU. We are creating our own MMU.
//
// NOTE2: Please treat all unsigned long long as 4 bytes here because we are using
// a 64-bit program to simulate a 32-bit MMU.
//
// APIs:
// init_root(): Initialize the L0 page table (root page table). In modern
// OS, a similar API is called in the early stage of the
// booting process of the OS.
//
// page_fault_get_physical_page(address): Gets a physical memory page.
//
// translate(address): Translates a virtual address to the corresponding
// physical address.
//
//
//
#define BITS_IN_L0 (3)
#define ENTRIES_IN_L0 (1
#define BITS_IN_L1 (4)
#define ENTRIES_IN_L1 (1
#define BITS_IN_L2 (5)
#define ENTRIES_IN_L2 (1
#define BITS_IN_OFFSETS (20)
unsigned long long root_page_table[ENTRIES_IN_L0];
unsigned long long page_fault_count = 0;
void init_root()
{
for (int i = 0; i
root_page_table[i] = 0;
}
}
// NOTE: You don't need to understand how this API generates a physical memory
// page (because it's fake). But you need to understand why we need this
// API.
unsigned int page_fault_get_physical_page(unsigned int address)
{
unsigned long long addr = address;
return (unsigned int)((((addr >> BITS_IN_OFFSETS)
}
unsigned int translate(unsigned int address)
{
unsigned long long* l1_page_table;
unsigned long long* l2_page_table;
// Layer 0, root
int index_in_l0 = address >> (32 - BITS_IN_L0);
printf("Index in L0(root) page table : %X ", index_in_l0);
if (root_page_table[index_in_l0] == 0) {
// page fault
printf("L0 page fault ");
page_fault_count += 1;
root_page_table[index_in_l0] = (unsigned long long)malloc(sizeof(unsigned long long) * ENTRIES_IN_L1);
l1_page_table = (unsigned long long*)root_page_table[index_in_l0];
for (int i = 0; i
l1_page_table[i] = 0;
}
} else {
l1_page_table = (unsigned long long*)root_page_table[index_in_l0];
}
// Layer 1
// FIXME: index_in_l1 is incorrect.
int index_in_l1 = (address > (32 - BITS_IN_L1); // I tried to do this but I don't know if it is right
printf("Index in L1 page table : %X ", index_in_l1);
if (l1_page_table[index_in_l1] == 0) {
// page fault
page_fault_count += 1;
printf("L1 page fault ");

// FIXME: Constructs the missing L2 page table (yes, it's L2 not L1). I tried this too but not sure what to do..

l2_page_table = (unsigned long long*)root_page_table[index_in_l1];

for (int i = 0; i

l2_page_table[i] = 0; }

} else {

// FIXME: Gets the existing L2 page table (yes, it's L2 not L1).

l2_page_table = (unsigned long long*)root_page_table[index_in_l1];

}
// Layer 2
int index_in_l2 = (address > (32 - BITS_IN_L2);
printf("Index in L2 page table : %X ", index_in_l2);
if (l2_page_table[index_in_l2] == 0) {
printf("L2 page fault ");
// page fault
page_fault_count += 1;
l2_page_table[index_in_l2] = page_fault_get_physical_page(address);
}
// FIXME: page_offset is incorrect.
int page_offset = 0;
printf("Page offset : %X ", page_offset);
// FIXME: return value is incorrect;
return 0;
}
unsigned long long get_page_fault_count()
{
return page_fault_count;
}
1 I You are not allowed to modify this file. #includestdio.h> #include #include 2 4 6 void init_root); 7 unsigned long long get_ page fault_count ); 8 unsigned int translate(unsigned int address); 10 int main(int argc, char** argv) 12 13 init root): assert ( translate (0xFFFF0001) = 0x1FFF0001); assert (get-page-fault-count() = 3); printf(" assert ( translate (0x5E300109) 0x7E300109); assert (get-page-fault-count() = 6); printf(" assert ( translate (0x5E4F0000) 0x7E4F0000); assert (get-page-fault-count() = 7); printf(" assert ( translate(0x5E4FF200) 0x7E4FF200); assert (get-page-fault-count() = 7); printf(" assert ( translate(0x8) 0x20000000); assert (get-page-fault-count() = 10); printf("n); assert ( translate(0x1) 0x20000001); assert (get-page-fault-count() 10); 15 16 18 19 20 21 23 24 25 26 27 28 29 30 31 32 assert ( translate(0xf) = 0x2000000F); assert (get-page-fault-count() = 10); 34 35 36 37 38 39 assert ( translate(0xFFFFFFFF) = 0x1FFFFFFF); assert (get-page-fault-count() = 10); return 0

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_2

Step: 3

blur-text-image_3

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

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions

Question

4.5 Write equipment specifications.

Answered: 1 week ago

Question

explain what is meant by redundancy

Answered: 1 week ago