Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
CSC3150-Instruction-A3:
Introduction
submission
- Due on: 23:59, 13 November, 2024
- Plagiarism is strictly forbidden. Please note that TAs may ask you to explain the meaning ofyour program to ensure that the codes are indeed written by yourself. Please also note that we would check whether your program is too similar to your fellow students' code andsolutions available on the internet using plagiarism detectors.
- Late submission: A late submission within 15 minutes will not induce any penalty on your grades. But 00:16 am-1:00 am: Reduced by 10%; 1:01 am-2:00 am: Reduced by 20%; 2:01am-3:00 am: Reduced by 30% and so on. (e.g. Li Hua submit a perfect attemp of assignment3 on 2:10 am. He will get (100+10 (bonus)) * 0.7 = 77 points for his assignment3.)
- You should submit a zip file to the Blackboard. The zip file structure is as follows.
Format guide
The project structure is illustrated below. You can also use ls command to check if your structure is fine. Structure mismatch would cause grade deduction.
For this assignment, you don't need a specific folder for the extra credit part. The source folder should contain four files: proc.c, proc.h, sysfile.c, trap.c
Please compress all files in the file structure root folder into a single zip file and name it using your student ID as the code shown below and above, for example, Assignment_3_xxxxxxxxx.zip. The report should be submitted in the format of pdf, together with your source code. Format mismatch would cause grade deduction. Here is the sample step for compressing your code.
Tips on interactions between host and virtual machine
In the terminal, you should not include "<" and ">". Here, they are just to present a custom string variable.
1. Copy the assignment folder to your virtual machine. You can copy the folder in the VSCode or use the scp command below.
$\newline$
If you have spaces in the path, use the double quote to include your path, e.g. cd "your host path" .
2. Unzip the assignment folder in your virtual machine.
In the virtual machine:
Then, you can browse the assignment folder.
After finishing the project, you should wrap your file following the format instructions. We prepare a script for you to generate the submission zip. This optional script is just for your convenience to wrap the files. You can wrap your file in your own way, only ensuring that you follow the format.
3. Suppose that you have already copied your Report.pdf to the virtual machine (like the way you copy the assignment zip from the host machine to the virtual machine).
In the virtual machine:
gen_submission.sh script will ask for your student id and path of your Report.pdf .
Then you can find your submission folder under ~/csc3150
project3/submission/Assignment_3_<your_student_id>.zip
4. You can use the following command to copy the submission zip to your host machine.
In the host machine:
Then you will get the submission zip in your_host_machine_folder_path . Don't forget to submit your zip file to the BlackBoard.
Instrction Guideline
Sections with (*) are introduction sections. These sections introduce tools and functions that will help you understand what this system is about and how the system works with these components. You might need to use some of the functions when implementing the TODO parts.
You are ONLY allowed to modify the TODO parts in these four files! And we will grade your project ONLY based on the implementation of the TODO parts. Any other modification will be
a. https://pdos.csail.mit.edu/6.828/2022/xv6/book-riscv-rev3.pdf
1. However, no sample code will be shown here. You need to figure out the implementation based on the logic and APIs provided in the introduction sections.
Arguments fetching*
The kernel functions argint , argaddr , and argfd retrieve the nth system call argument from the trap frame as an integer, pointer, or file descriptor. They all call argraw to retrieve the appropriate saved user register (kernel/syscall.c:34).
Proc*
Pages*
#define PTE_U (1L << 4) // user can access
Prots & Flags*
#define MAP_PRIVATE 0x02
(TODO) Traps
Hint:
- r_stval() provides trap value. (i.e. the address causing the exception)
- The swapping mechanism is not supported in the xv6 system. If the physical memory is filled, you are expected to kill the process. (You shall learn to use kalloc() and setkilled() functions)
- If there is spare space in physical memory, map one page of the file with the corresponding vma. ( mapfile() and mappages() )
File*
};
// Write to file f.
Struct "file" "inode" is presented for your information.
filewrite() will be invoked to write back when the memory map is over. i.e. Calling munmap or Calling exit of process. Similarly to fileclose() .
filedup() will be invoked when there is an increment of accessing file. ( mmap() , fork() )
// Defined in fs.c
If you have no idea what readi() is doing, think about read() or memcpy(), which deal with pointers and address.
ilock() and iunlock() are locks of inode, which are used to ensure consistency of the memory.
Hint
(TODO) VMA Struct
Explanation
Implementation
- Keep track of what mmap has mapped for each process.
- Define a structure corresponding to the VMA (virtual memory area), recording the address, length, permissions, file, etc. for a virtual memory range created by mmap.
- Since the xv6 kernel doesn't have a memory allocator in the kernel, it's OK to declare a fixedsize array of VMAs and allocate from that array as needed. A size of 16 should be sufficient. (I already define VMASIZE for you)
Hint
The VMA should contain a pointer to a struct file for the file being mapped;
If you would like to use more variables in VMA for further implementation, feel free to use them.
There is not only one correct answer.
(TODO) mmap()
}
- Arguments explanation:
In the mmaptest.c, we call 'char p = mmap(0, PGSIZE2, PROT_READ, MAP_PRIVATE, fd, 0);'.This call asks the kernel to map the content of file 'fd' into the address space. The first '0' argument indicates that the kernel should choose the virtual address (In this homework, you can assume that 'addr' will always be zero).The second argument 'length' indicates how many bytes to map.The third argument 'PROT_READ' indicates that the mapped memory should be read-only, i.e., modification is not allowed.The fourth argument 'MAP_PRIVATE' indicates that if the process modifies then mapped memory, the modification should not be written back to the file nor shared with other processes mapping the same file (of course, due to PROT_READ, updates are prohibited in this case).The fifth argument is the file description of the file to be mapped.The last argument 'offset' is the starting offset in the file.The return value indicates whethermmap succeeds or not.
- sys_xxx() function is the kernel's implementation of the xxx() system call. In the xv6 operating system, system calls are prefixed with sys_ to distinguish them from other functions and to indicate that they are system calls. The kernel functions argint , argaddr , and argfd retrieve the n'th system call argument from the trap frame as an integer, pointer, or a file descriptor. See the Arguments fetching section.
- Run mmaptest after mmap() implemented: the first mmap should succeed, but the first access to the mmap-ed memory will cause a page fault and kill mmaptest.
- Before mmap() implemented
- Page fault occurs after mmap() implemented(work correctly)
Progress chart
(TODO) PageFault Handle
- Add code to cause a page-fault in a mmap-ed region to allocate a page of physical memory.
- Find corresponding valid vma by fault address.
- Read 4096 bytes of the relevant file onto that page, and map it into the user address space.
- Read the file with readi, which takes an offset argument at which to read in the file (but you will have to lock/unlock the inode passed to readi).
- Set the permissions correctly on the page. Run mmaptest; it should get to the first munmap.See Section Trap
(TODO) munmap()
- find the VMA for the address range and unmap the specified pages (hint: use uvmunmap).
- If munmap removes all pages of a previous mmap, it should decrease the reference count of the corresponding struct file.
- If an unmapped page has been modified and the file is mapped MAP_SHARED, write the page back to the file. Look at filewrite for inspiration.
- Ideally your implementation would only write back MAP_SHARED pages that the program actually modified. The dirty bit (D) in the RISC-V PTE indicates whether a page has been written. However, mmaptest does not check that non-dirty pages are not written back; thus, you can get away with writing pages back without looking at D bits.
(TODO) Page Alignment
(EXTRA CREDITS) Fork Handle
- In your Assignment 1, you should already know that fork() creates a sub process with the same info. Therefore, you should handle how mmap() works when fork() is invoked.
- Ensure that the child has the same mapped regions as the parent. Don't forget to increment the reference count for a VMA's struct file. In the page fault handler of the child, it is OK to allocate a new physical page instead of sharing a page with the parent. The latter would be cooler, but it would require more implementation work.
Grading Rules
Program part 90' + extra credits
You can test the correctness of your code using the following commands under ~/csc3150- project3 directory.
mmaptest: all tests succeeded
function
|
points
|
mmap f
|
13p
|
mmap private
|
5p
|
mmap read-only
|
5p
|
mmap read/write
|
5p
|
mmap dirty
|
5p
|
mmap two files |
5p
|
not-mapped unmap
|
12p
|
mmap offset |
5p
|
mmap half page
|
15p
|
Compile Success
|
20p |
Report part 10'
LaTex Editor
- Create a new blank project.
- Click the following highlight bottom and upload the template we provide.
- Click Recompile and you will see your report in PDF format.