![]() |
ANU College of Engineering and Computer Science
Research School of Computer Science
|
|
|
Laboratory 3This is the longest of the labs. This laboratory should take you less than 4 hours to complete. Make sure you ask your tutor for help if you get stuck. You are expected to print out and read through these instruction before the lab time. Also your are expected to do about 1 hour of preparation.
Learning ObjectivesThis lab has two main parts. The first involving memory management and the second involving the very very simple file system. The learning objectives for this lab are to:
PreparationRead through the manual pages for mmap, mprotect, signal. Read through the documentation on the virtual file system(/usr/src/linux/Documentation/filesystems/vfs.txt) Also draft your code for the programs your are required to write during the lab.
During the LabStep 1 - File copying using mmapSee the description of the mmap() system call at the end of these notes and in the mmap man page.An interesting way to copy a file is to memory map the source and destination files then to use memcpy() to copy the data between them. This allows you to copy a file without a read() or write() system call. Write a mmap_copy program that copies a file. It should take 3 parameters on the command line, the source file name, the destination file name and the size of the source file in bytes. Your program will need to perform the following steps:
mmap()The mmap() system call provides a way for a user process to ask the kernel to map some pages from a file or device into the memory space of the process. The return value from mmap() is a pointer that points to an area of memory that, when accessed, will cause a page fault and a load of the corresponding page of data from the device or file. By using mmap() a process can treat data in a file or device as memory, which provides a very convenient programming interface for some tasks. The mmap() call takes the following parameters:void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
memalignmemalign() is a library call that allocates aligned memory. It is very similar to malloc() except that it takes an additional parameter that determines the minimum memory alignment required for the returned memory. In this lab you will use an alignment of 4096 so that the returned memory is guaranteed to be on a page boundary. This needs to be done because the memory management hardware works in units of one page and cannot set permissions on sub-page units.The prototype for memalign is: void *memalign(size_t alignment, size_t size); Note that, you should include "malloc.h". Step 2 - Setting memory protectionsThe mprotect() system call allows you to set the protections on a set of pages in your processes address space. This allows you to set pages as read-only or to allow the pages to be executed. Have a look at the mprotect() man page. In this exercise you will allocate some memory then set the memory non-writeable and install a signal handler to catch a segmentation fault. You will then attempt to write to the memory and your signal handler will be called as the memory management hardware detects a fault. Inside your signal handler you will use mprotect to make the memory writeable again then return. The write to the memory will then continue. Your program should perform the following steps:
Step 3 - Direct GraphicsThe kernel provides a special memory device /dev/mem that can be memory mapped to give direct access to the physical memory in your machine. In this exercise you will use mmap() to map in the physical memory of the graphics card in your machine and you will then perform some simple graphics operations by writing directly to that memory. You will need to be very careful with this program. If you write to the wrong memory location then you will crash the machine or corrupt files. Use "lspci -vv" will also help find this address of the graphics memory. The address is 0xe0000000 and is 256M on the machines currently in the lab (this will save you looking around for it). Each pixel is an 'unsigned int' and the screen starts at offset 0x1000000 (in bytes from the start of video memory) into this 256M region of memory. Also each line is 2048 ints before it wraps around (although you only see the first part of this). Your program should perform the following steps:
Each 32-bit unsigned int of mapped memory corresponds to one pixel on the display. The first 2048 ints will be the top line of the display(this includes some padding on right side) and the next 2048 shorts will be the next line, etc. To draw a rectangle you will need to work out the locations of the pixels in the rectangle and set them in the mapped memory(this can be done with two for loops). The value stored will determine the color of the pixel. Why don't all programs use this method to display graphics? Optional - Down-loading and mounting the vvsfsObtain a copy of the vvsfs code and Makefile. To mount and check the file system do the following:
Optional - Your first bug to debugAs the number of entries in the sramfs directory increases the size of this file (directories are just special files in UNIX) also should increase. However, a mounted vvsfs shows that the number of bytes taken up by the directory is zero. This is a bug. It is basically due to the fact that the vfs caches a copy of the inode information and the sramfs also maintains a copy of the inode for this directory. When a new file is added to the directory the vvsfs inode's size is updated, but the size in the vfs inode is not updated, hence, when you do a "ls -als /mnt" the size of the vvsfs directory is zero.Fix this bug. (Hint, you need to add one line to the end of the "vvsfs_add_entry" routine.) Check that this modification worked.
Optional - Adding the ablity to "rm" a fileTo be able to delete files you need to add an extra routine called "vvsfs_unlink" You need to add the pointer to this routine in the sturcture "vvsfs_dir_inode_operations". What are the parameters of this routine? (look in /usr/include/linux/fs.h or have a look at how another fs (eg minix, ext2) does this.)The unlink routine is most like the "ramfs_lookup" use this routine as a starting point for your code. Note that, you have:
Check your solution. Attempt to change user id and permission with the vvsfs. What happens? Why is this the case? Is there anypoint in storing this information in the vvsfs?
| |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Please direct all enquiries to: ericm@cs.anu.edu.au Page authorised by: Head of School, RSoCS |
| The Australian National University — CRICOS Provider Number 00120C |