Your Cart
Loading

Operating Systems Project 4 Memory Management∗

On Sale
$25.00 (% off)
$19.00
Added to cart

Introduction


The outcome of this project is to implement a series of Linux kernel modules to report memory management statistics. The objective of this project is to get familiar with the following memory management components:


  1. Process virtual address space.


  1. Page tables.


The free book The Linux Kernel Module Programming Guide will always be your friend.


Project submission


For each project, please create a zipped file containing the following items, and submit it to Canvas.


  1. A report that includes (1) the (printed) full names of the project members, and the statement: We have neither given nor received unauthorized assistance on this work; (2) the location and name of your virtual machine (VM), and the password for the root usera solution (that I don’t recommend for security reasons).


    Project


    This project reviews the key memory management concepts we studied in class: virtual memory and page tables. Virtual memory often refers to the process address space assigned to user-space processes. Such virtual addresses need to be translated into physical addresses with the help of page tables. You may read Section 10.4 in our textbook to get an overview of Linux’s memory management.


    Part 0.0: Use the provided template for report (5 points)


    Part 0.1: Preparation


    Like in Project 2, please boot the newer version of the kernel by choosing it at boot time.blog post (also shown as Figure this page if you like to know. The reason is a bit complicated but well worth your time.


    Part 2: The page table walk (50 points)


    Write a module called va status to report the current status of a specific virtual address. The module takes a virtual address of a process (whose process ID is pid) and the pid as



    its input, then outputs whether this address is in memory or on disk. The virtual address will be passed in as a string, and pid as an integer.


    HINT: The command pmap report virtual memory map of a process. So to get a virtual address of a process, you can take the PID of bash in Part 1, and pass the PID to command pmap to get a listing of the virtual addresses. Pass one of the virtual addresses from pmap as a string input to your module. While the free book The Linux Kernel Module Programming Guide is great, it has incorrect information how to pass a string to a module. Please refer to yet another blog post how to correctly pass a string to a kernel module:


    “When a string variable is passed to the kernel module – the string should be enclosed with double quotes and externally with single quotes. The single quotes are used by the shell in which the insmod command is invoked and the double quotes string literal is passed on to the kernel module.”


    NOW THE ACTUAL HINT: The page descriptor page defined in linux/mm types.h contains information about a page. You need to figure out how to obtain a reference to the page descriptor given a virtual address and read information from the page descriptor. Note that Linux uses multi-level page tables, so you might need multiple steps to reach the page table entry (PTE) of a given virtual address.



    On slide 30 of Lecture 10, we introduced how to navigate the page directories. Each process has its own pointer to what is called a page global directory (PGD). To navigate the page directories, several macros are provided by the kernel to break up a virtual address into its component parts. For example, pgd offset() takes a virtual address and the



    1. struct for the process (the mm field of task struct) and returns the PGD entry that covers the requested address. pud offset() takes a PGD entry and an address and returns the relevant PUD entry. And pmd offset() takes a PUD entry and an address and returns the relevant PMD entry. More information can be found on this page (make sure read it).


    Note: The output of pmap are hexadecimal strings. To use pgd offset(mm, address), pmd offset(mm,address), and pte offset kernel(mm,address), the argument address has to be converted from a hexadecimal string to unsigned long. Unlike user-space programs that can use sscanf or strtoul, kernel modules use kstrtoul. Please look up how to use this function.


    Finally, we need to get the PTE from the PMD entry. This step can be a little tricky. pte offset kernel(pmd, address) could work, but you may get a kernel oops. The reason is that pte offset kernel() returns the address of a page table entry, but the entry itself could be changed by the process at runtime. Therefore, you should try the following:



    • #i n c l u d e <l i n u x /mm. h>


    • #i n c l u d e <l i n u x /highmem . h>


    3

    4

    s p i n l o c k t ∗ l o c k ;

    5

    ptep = p t e o f f s e t m a p l o c k (mm, pmd , a d d r e s s , &l o c k ) ;

    • . . .


    • pte unmap unlock ( ptep , l o c k ) ;



    To test if an address is in memory, you need to use the macro pte present() to test if the corresponding PTE have the PRESENT bit set. Note that pte present()’s argument is a PTE, not the address of a PTE (pte offset map lock() above returns a PTE pointer).



    Unlike Part 1, we have no way of validating the result in Part 2. The result can be changed at any moment (therefore the need for lock/unlock above).






    4


You will get a ZIP (461KB) file