Typically, there are three uses of physical memory in the kernel: used to hold a virtual page of some process, used by the kernel to hold kernel's own data structures, and used by the kernel to cache file data, file i-nodes and file-related information (the cache is called file buffer cache).
In the Linux kernel, there is a structure called "mem_map" which is an array of "struct page". Each entry in "mem_map" corresponds to a physical page. However, Linux did not maintain information on the use of a physical page, that is, whether it is used by kernelto hold kernel data structure, or used by file buffer cache to hold file data, or used by the virtual memory system to hold a virtual page. In other words, there is no field in "struct page" that contains this information.
The assignment asks you to modify the "struct page" data structure to include two more fields: int pid, and unsigned long virtaddr. The two fields have the following semantics: if pid == -1, it means that the page is used by kernel, and virtaddr should be 0; if pid == -2, it means that the page is used by file buffer cache, and virtaddr should be 0; if pid == 0, it means that the page is free (not used for any purpose); if pid>0, then pid and virtaddr describe the virtual page that is mapped to this physical page.
The assignment asks you to find all places where the physical pages are
allocated/freed and modify the code, so that the appropriate values of
the two fields are set when the page is allocated, and cleared when the page
is freed. Furthermore, the assignment asks to implement a routine called
"dump_mem_map", which prints out the usage of the all physical pages. The
print-out should print clearly whether the page is used by kernel, by file
buffer cache, or by virtual memory, and in the third case, the (pid, virtaddr)
of the virtual page that is mapped to the physical page.
Implementation
You can either implement the assignment as a user-program, or as a kernel
modification.
File "page_alloc.c" contains routines that allocate a page and free a page. It is a vast simplification of the original page_alloc.c in Linux. __get_free_page allocates a free page; free_page frees a page. You need to insert calls to your routines in free_page in order to clear the mapping.
Finally, you need to implement "dump_mem_map". The main() in main.c will call "dump_mem_map" periodically.
One important caveat: similar to the function prototypes in the Linux kernel, the return value of __get_free_page is the starting address of the physical page in bytes. In other words, __get_free_page does not return the page number of the page. This is so that future support for larger pages can be easier. Similar, the virtaddr passed in calls to do_no_page and do_wp_page is the virtual address in bytes. Thus, you need to perform the appropriate conversion from the page address to page number when indexing the mem_map array.
Of course, you also have to change mm.h in linux/include/linux/ to include the appropriate definition of the fields. Note that the caveat described above applies in the kernel source too.
You should also implement a function called dump_mem_map, that prints out the reverse mapping for each page on the console. You use "printk" instead of "printf" to print things on the console. To test dump_mem_map, you can insert some code in free_pages to call it after free_pages have been called for a number of times (for example, 10000 times).
Driver routines for the user-program implementation can be found in ~cs537-2/public/project4/user-program-driver/.
About Linux
There are many good online sources and books about Linux. See the emails
I sent out on the subject.
Suggestions
I would not recommend you doing the kernel implementation unless you have
a fair amount of programming experience, for example, have written programs
that are at least 2000 lines long.
Handin
If you decide to do the user-program implementation, simply handin your code
and README file and test runs.
If you decide to do the kernel implementation, handin the file "diff"s
between your code and the original Linux source (preferable Linux 2.1.54 or
later), and a README file. I will compile and test your code on the "crash
and burn" lab.
Grading
If you decide to implement this project as a user program, the grade on this
project will count for 5\% in your final grade, and the grade on Project 5
will count for 19\% in your final grade.
If you decide to implement this project through modifying the kernel, the grade on this project will count for 12\% in your final grade, and the grade on Project 5 will count for 12\% in your final grade.
Other Stuff
On Thursday, Apr 16th, I'll hold a discussion and demo session on building
Linux kernel during the discussion session.