COMP2300

Tutorial / Laboratory 08 - Virtual Memory in PeANUt

Semester 1, 2007         Week 9 (30 Apr - 4 May)

Note that for this session, there are some Preparation Exercises. Also for this session, there is a submitable laboratory exercise which is due by 10 am Monday 14 May (week 10), which will contribute up to 1% of your assessment (in the Tute/Lab mark).

Objectives

There are several objectives in this exercise:

Preparation

NOTE: This laboratory requires you to read the detailed description of the operation of PeANUt Virtual Memory that appears in Section 3 of the PeANUt Specification. It is highly desirable that you read this in advance of your laboratory. Reading the rest of this document in advance will be helpful too. You will waste your lab time and miss out on opportunities to ask your tutor relevant questions if you do not prepare adequately.

Preparation Exercises

Complete the following questions on a separate sheet of paper, with your name and student number clearly written. Please ensure your writing is legible. Hand in to your tutor at the beginning of your tutorial / laboratory session.
  1. What is meant by the terms page and page frame?
  2. What is a page fault? When does it happen?
  3. What is the role of the dirty bit? Where is it found?
  4. What is thrashing?

Tutorial Exercises

  1. What sequence of paging events take place when a program is first being run (on a real machine)?
  2. What are the possible chains of events that can happen in the PeANut machine during a load instruction between an address being loaded into the MAR and the data appearing in the MDR?
  3. It is said that the LRU page replacement policy is far better than the FIFO. What does this statement mean? Why would we expect it to be so?
  4. Suppose page 810 is present in physical memory page frame 5 and a store to address a401 has just been executed. Suppose also that 3 page faults have occurred since it was first loaded into physical memory. Write the corresponding page table entry for the page, in terms of a diagram and in terms of a binary and hexadecimal number.
  5. What is the largest working set (in number of pages as well as number of cells) possible for a PeANUt program before the system starts thrashing? Hint: how many pages must be reserved for VM?

Laboratory Exercises

It is expected that you work through Part 1 as well as Part 2 (up to at least Part 2a) in the supervised lab in week 9, and that you continue with Parts 2 and 3 in the unsupervised lab in week 10 (no new lab sheet will be distributed in week 10). It is also a (possibly the last!) chance to get some help from your tutor for Assignment 2, although it is not the intention for your to spend much of the session on this.

Preliminaries

  1. In your comp2300 folder, create a new directory called lab8.
  2. Copy the files /dept/dcs/comp2300/public/lab8/* into your lab8 folder.

Part 1: Experimenting with PeANUt Virtual Memory

Part 2: Further Experiments

The page fault service routine in vmfifo.ass is rather simplistic in a number of ways. There are a number of steps that can be taken to improve the page fault service routine.

Part 2a: Locking in Further Pages

As explained earlier, the page fault service routine of vmfifo.ass ensures that pages 0 and 1 are never swapped out (see definition of FstULPg). This is very important, because if either of those pages were swapped out, the PeANUt would not be able to either access the page table or call the page fault service routine, and so would come to a grinding halt very quickly. If you are unsure about this, try redefining FstULPg to 1 (or 0) and see what happens.

One of the limitations of the page fault service routine of vmfifo.ass is that it only has 26 memory cells of page one to fit into (the first six cells of page one are used to store registers and the number of the page to be swapped in). This space restriction means that the routine must be kept simple.

Part 2b: Using the Dirty Bit

You may have noticed that the Dirty bit has been largely ignored until now. In the <swap out page XR>; code, the page to be removed is copied from main memory to secondary memory (trap #13). This is necessary because something may have been written to that page since it was swapped in and that information should not just be thrown away. In fact, often pages are swapped out that have not had anything written to them. Remembering that on real systems, reading and writing between main memory and secondary memory is very slow, a lot of time could be saved if pages were only written back to main memory when really necessary.

The dirty bit is provided for just this reason. If a page is dirty it should be written back to main memory, otherwise it can be discarded (because the copy in main memory is identical). Now that page three is locked in, you should have enough space to add lines to the page fault service routine that check the dirty bit before writing the page back.

Part 2c: Different Paging Policies

So far you have only looked at the FIFO paging policy.


 

Code for vmfifo.ass

; vmfifo.ass: PeANUt assembler file for COMP2300 Lab 8, 2007
;
; This file demonstrates PeANUt Virtual Memory in operation. The paging
; algorithm here is a *basic* FIFO algorithm. This algorithm has the following
; weaknesses:
;	i)  It relies on all page frames being pre-loaded.
;	ii) It always writes back (no Dirty bit check).
; The program included here accesses each page in memory.
; As it accesses pages not swapped in, page faults occur. 
; As each page fault occurs, two characters are printed, a character corres-
; ponding to the page to be swapped out, and a character corresponding to the
; page to be swapped in. Page 0 is represented by "A", Page 1 "B" and
; Page 31 by "`".
;
; The page-fault handling routine starts at a46 (as it must). The test program
; starts at page 2.


; /* Define initial page table contents (pages 0-7 swapped in, remainder out) */
					; 	   /* PT must be at address 0 */
					; volatile short int PT[] = {
PT:	data %00000 000 111 0 1 000	; 	0x00e8,
	data %00000 000 110 0 1 001	;	0x00c9,
	data %00000 000 101 0 1 010	;	0x00aa,
	data %00000 000 100 0 1 011	;	0x008b,
	data %00000 000 011 0 1 100	;	0x006c,
	data %00000 000 010 0 1 101	;	0x004d,
	data %00000 000 001 0 1 110	;	0x002e,
	data %00000 000 000 0 1 111	; 	0x000f,
					; 
	block 	24			; 	0,0,0,0, 0,0,0,0, 0,0,0,0, 
					;	0,0,0,0, 0,0,0,0, 0,0,0,0
					; } /* PT */ 
			;  /* next 6 memory loc. are reserved for the OS*/
	block	5	;  		/* for saving registers on page faults*/
			;  		/* PgFtNum must be at address 37 = a45*/
PgFtNum:block	1	;  volatile short int PgFtNum;
			;	/*on trap #11, OS puts # of page causing fault*/

			;  /* VM-related macros for PeANUt */
	PageSz	= 32	;  #define PageSz 	32		 /* page size */
	LastPg	= 31	;  #define LastPg 	31 	/* number of last page*/
	SwapOut	= 13	;  #define SwapOut 	13	/* trap # to swap out */
	SwapIn	= 12	;  #define SwapIn 	12	 /* trap # to swap in */

			; 	  /* FIFOPgSwap() must be at address 38 = a46 */
FIFOPgSwap:		;  void FIFOPgSwap(void) { 
			;    /* post: implements a FIFO page swap algorithm   */
	FstULPg	= 2	;  #define FstULPg 2	    /* # of 1st unlocked page */
	setxr	#FstULPg;    short int XR = FstULPg;
FIFOwh:	load	*PT	;    while ( (PT[XR] 
	and	SwPrMsk	;             & SwPrMsk)
	cmp 	SwCtOPr	; 	     != SwCtOPr ) {
	beq  	FIFOewh	;      /* seek PT entry with Swap Count=5 & Present=1 */
	incxr	#1	;      XR = XR + 1;
	load	#LastPg	;      if (XR > LastPg) 
	cmpxr		;         break; /* an error, to be trapped later;... */
	ble	FIFOwh	;                /* ... prevents an inf. loop here    */
FIFOewh:		;    } /* while */
	load	*PT	;    PT[PgFtNum] = (PT[XR] & FramMsk);
	and  	FramMsk	; 	/* fill in frame # of PT entry for new page   */
	store	@PgFtNum; 	/* new page goes to same frame as page to be swapped out */
	loadxr		;    <swap out page XR>;
	trap	#SwapOut; 		
	add	#'A'	;    putchar('A'+XR);
	trap	#3	; 			/*letter corresp. to this page*/
	load	PgFtNum ;    <swap in page PgFtNum>;
	trap	#SwapIn	;    	  
	add	#'A'	;    putchar('A'+PgFtNum); 
	trap	#3	; 
	load	#'\n'	;    putchar("\n');
	trap	#3	; 
	ret		;  } /* FIFOPgSw() */     
			; Bit masks used in page fault service routine.
SwPrMsk:data 	%00000 000 111  0 1 000	; const short int SwPrMsk = 0x00e8,
					;      /*Swap count & Present bit mask*/
SwCtOPr:data 	%00000 000 101  0 1 000 ;                 SwCtOPr = 0x00a8, 
					;  /*Swap count=5 (Oldest), Present=1 */
FramMsk:data	%00000 000 000  0 0 111 ;                 FramMsk = 0x0007; 
					; 		 /* frame number mask */

LstPg:	data	991 	;  const int LstPg = 991; /*addr. of start last page-1*/
			;  int main() {
main:	setxr	#0	;    short int *XR = NULL;	   /* implement via XR*/
			;    do {
mloop:	load	*0	;      <access *XR>;
	incxr	#PageSz	;      XR += PageSz;
	load	LstPg	; 
	cmpxr 		;    } while (XR <= (int *) LstPg);  
	ble 	mloop	; 
	trap	#1	;    exit(0);
	end 	main	;  } /* main() */


Last modified: Mon Apr 30 10:42:51 EST 2007