[ From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 22-NOV-1990 17:07:22.65  To:	MRGATE::"ARISIA::EVERHART" CC:	1 Subj:	The RIGHT version of RAMDRIVER, part 3 of 5   * Received:  by crdgw1.ge.com (5.57/GE 1.78)) 	 id AA04404; Thu, 22 Nov 90 16:42:29 EST Y Received: From UCBVAX.BERKELEY.EDU by CRVAX.SRI.COM with TCP; Thu, 22 NOV 90 07:34:39 PST , Received: by ucbvax.Berkeley.EDU (5.63/1.42)* 	id AA06238; Thu, 22 Nov 90 07:20:01 -08009 Received: from USENET by ucbvax.Berkeley.EDU with netnews . 	for info-vax@kl.sri.com (info-vax@kl.sri.com); 	(contact usenet@ucbvax.Berkeley.EDU if you have questions)  Date: 21 Nov 90 00:37:51 GMT& From: gkn@ucsd.edu  (Gerard K. Newman), Organization: San Diego Supercomputer Center4 Subject: The RIGHT version of RAMDRIVER, part 3 of 5 Message-Id: <23374@ucsd.Edu># Sender: info-vax-request@kl.sri.com  To: info-vax@kl.sri.com     1 -+-+-+-+-+-+-+-+ START OF PART 3 -+-+-+-+-+-+-+-+ 0 X`009MOVW`009#SS$_NORMAL,R0`009`009`009 ;Success2 X`009CLRL`009R1`009`009`009`009 ;No further status< X`009BITW`009#<IRP$M_PAGIO!IRP$M_SWAPIO>,-`009 ;Special caseC X`009`009IRP$W_STS(R3)`009`009`009 ; paging/swapping I/O completion ) X`009BNEQ`00940$`009`009`009`009 ;`009... 5 X`009FORKLOCK LOCK=UCB$B_FLCK(R5),-`009`009 ;Grab the / X`009`009SAVIPL=-(SP),-`009`009`009 ; fork lock . X`009`009PRESERVE=YES`009`009`009 ;  to modify1 X`009MOVL`009R3,UCB$L_IRP(R5)`009`009 ;   the UCB  X M X30$:`009JSB`009G`094IOC$REQCOM`009`009 ;Post the request complete (& look fo 
 Vr more work) 2 X`009FORKUNLOCK LOCK=UCB$B_FLCK(R5),- ;Release the+ X`009`009NEWIPL=(SP)+,-`009`009 ; fork lock ( X`009`009PRESERVE=YES,-`009`009 ;`009...' X`009`009CONDITION=RESTORE`009 ;`009...  X`009RSB`009`009`009`009 ;Done X F X; Here for paging I/O completion -- we already own the fork lock, and( X; IOC$INSIOQC expects to release it ... X M X40$:`009BSBW`009RAMD_FREE_SPTES`009`009;Release the system page table entrie  Vs used to map the buffer 7 X`009MOVL`009R3,UCB$L_IRP(R5)`009;Stash the IRP address M X`009JMP`009G`094IOC$REQCOM`009`009;Post this request complete (& look for mo 	 Vre work)  X 
 X`009.Page3 X`009.Subtitle`009RAMD_READ`009- Process a read IRP  X  X;+  X;' X; ----- RAMD_READ:  Process a read IRP  X; X;J X; This routine is called to process a read IRP, which involves a transfer( X; from the "disk" to the user's buffer. X; X; Environment:  X;# X;`009IPL$_ASTDEL, process context.  X;
 X; Inputs: X; X;`009R4`009- PCB address  X;`009R3`009- IRP address ! X;`009R2`009- User buffer address ! X;`009R1`009- Disk buffer address  X; X; Outputs:  X;' X;`009Data read into the user's buffer.  X; X;-  X / XRAMD_READ:`009`009`009`009 ;Process a read IRP  X C X`009BSBW`009RAMD_LOCK_READ`009`009 ;Lock the MUTEX for read access 5 X`009MOVC3`009IRP$L_BCNT(R3),(R1),(R2) ;Read the data  X`009RSB`009`009`009`009 ;Done X 
 X`009.Page5 X`009.Subtitle`009RAMD_WRITE`009- Process a write IRP  X  X;+  X;) X; ----- RAMD_WRITE:  Process a write IRP  X; X;K X; This routine is called to process a write IRP, which involves a transfer H X; from the user's buffer to the "disk".  Erase requests also come here. X; X; Environment:  X;# X;`009IPL$_ASTDEL, process context.  X;
 X; Inputs: X; X;`009R4`009- PCB address  X;`009R3`009- IRP address ! X;`009R2`009- User buffer address ! X;`009R1`009- Disk buffer address  X; X; Outputs:  X;) X;`009Data written from the user's buffer  X; X;-  X 4 XRAMD_WRITE:`009`009`009`009`009;Process a write IRP X H X`009BSBW`009RAMD_LOCK_WRITE`009`009`009;Lock the MUTEX for write access5 X`009BBS`009#IO$V_ERASE,IRP$W_FUNC(R3),10$`009;Erase? 0 X`009MOVC3`009IRP$L_BCNT(R3),(R2),(R1)`009;Nope.! X`009RSB`009`009`009`009`009;Done  X C X10$:`009MOVC5`009#0,(SP),#0,IRP$L_BCNT(R3),(R1)`009;Erase the data ! X`009RSB`009`009`009`009`009;Done  X 
 X`009.PageE X`009.Subtitle`009RAMD_LOCK_READ`009- Lock our MUTEX for read  access F X`009.Subtitle`009RAMD_LOCK_WRITE`009- Lock our MUTEX for write access X;+  X;: X; ----- RAMD_LOCK_READ:   Lock our MUTEX for read  access: X; ----- RAMD_LOCK_WRITE:  Lock our MUTEX for write access X; X;G X; These routines will  lock our MUTEX for either read or write access, G X; and are sensitive to whether or not we can receive ASTs.  If we can, M X; the standard SCH$LOCK`123R,W`125  routines are called.  Failure to acquire ; X; the lock in this case will suspend the process in MWAIT.  X;M X; If we can't receive ASTs (paging I/O) then we call SCH$LOCK`123R,W`125EXEC G X; routines to  lock  the MUTEX  without process  context.  Failure  to G X; acquire the lock in this  case complicates things quite a bit.  What G X; we'd really like to  do is to drop IPL back  down to IPL$_ASTDEL and G X; stick the process  in MWAIT,  but I'm  not convinced  I can  do that G X; safely.  The problem is the length of the call stack that eventually G X; got us  here -- we came  thru  EXE$INSIOQC  prior to  the start  I/O G X; routine, and  hit EXE$BUILDPKTR at some  point before that, and from G X; there the  path gets a bit fuzzy.  So, what we do instead is to look G X; at the kind of I/O request we're processing.  If it's a read request G X; (the usual case) we simply drive  on as if we had acquired the lock. G X; If it's a write  request then we  complete the  I/O request  with an G X; error.  The idea here is that the only way to get a page fault write G X; on a ram disk is to either have  a paging file open on is (dumb), or G X; for someone to  have a global  section with  backing store set  to a G X; file on the  ram disk (dumb).  In either  case I just can't convince D X; myself that the extra work to make this work "right" is worth it. X; X; Pie-in-the-sky-department:  X;F X; It would be nice to come up with an interlock which had granularity1 X; of, say, 1 block, rather than the entire disk.  X;
 X; Inputs: X; X;`009R4`009- PCB address  X;`009R3`009- IRP address ! X;`009R2`009- User buffer address ! X;`009R1`009- Disk buffer address  X;`009R0`009- MUTEX address  X; X; Outputs:  X; X;`009MUTEX locked if possible.  X; X;-  X @ XRAMD_LOCK_READ:`009`009`009`009;Lock our MUTEX for read  access X 8 X`009BITW`009#<IRP$M_PAGIO!IRP$M_SWAPIO>,-`009;Paging or1 X`009`009IRP$W_STS(R3)`009`009`009; swapping I/O? 9 X`009BNEQ`00910$`009`009`009`009;If NEQ yes, special case @ X`009JMP`009G`094SCH$LOCKR`009`009`009;Lock the MUTEX and return X  X; Here for a page fault read. X D X10$:`009JSB`009G`094SCH$LOCKREXEC`009`009;Attempt to lock the MUTEX# X`009BLBS`009R0,20$`009`009`009;Win 7 X`009INCW`009UCB$W_ERRCNT(R5)`009;Count up these errors 1 X`009BBSS`009#IRP$V_MBXIO,-`009`009;Flag the fact < X`009`009IRP$W_STS(R3),20$`009; that we don't have to unlock X ! X20$:`009RSB`009`009`009`009;Done  X = XRAMD_LOCK_WRITE:`009`009`009;Lock our MUTEX for write access  X 8 X`009BITW`009#<IRP$M_PAGIO!IRP$M_SWAPIO>,-`009;Paging orG X`009`009IRP$W_STS(R3)`009`009`009; swapping I/O (dumb and double dumb) 9 X`009BNEQ`00910$`009`009`009`009;If NEQ yes, special case F X`009JMP`009G`094SCH$LOCKW`009`009`009;Nope, lock the MUTEX and return X & X; Here for a page fault write (dumb). X D X10$:`009JSB`009G`094SCH$LOCKWEXEC`009`009;Attempt to lock the MUTEX$ X`009BLBC`009R0,20$`009`009`009;Lose X`009RSB`009`009`009`009;Win X $ X; Lossage.  Defeat the I/O request. X 4 X20$:`009CMPL`009(SP)+,(SP)+`009`009;Clean the stack; X`009POPR`009#`094m<R3,R4,R5>`009`009;Restore the registers J X`009BSBW`009RAMD_FREE_SPTES`009`009;Release the system page table entries0 X`009MOVL`009IRP$L_WCBSAVE(R3),-`009;Restore the/ X`009`009IRP$L_WIND(R3)`009`009; window pointer 7 X`009MOVL`009R3,UCB$L_IRP(R5)`009;Stash the IRP address 8 X`009INCW`009UCB$W_ERRCNT(R5)`009;Count up these errors.3 X`009MOVQ`009#SS$_CTRLERR,R0`009`009;Indicate error 6 X`009JMP`009G`094IOC$REQCOM`009`009;Punt this request. X 
 X`009.Page3 X`009.Subtitle`009RAMD_UNLOCK`009- Unlock our MUTEX  X  X;+  X;' X; ----- RAMD_UNLOCK:  Unlock our MUTEX  X; X;I X; This routine  is called to unlock  our MUTEX.  We are sensitive to our I X; ability to receive ASTs.   If we can,  then we simply call SCH$UNLOCK. I X; If we  can't  (paging I/O), we call  SCH$UNLOCKEXEC  instead.  We alsoEI X; use IRP$V_MBXIO  for paging I/O  unlocks to  indicate that  we ignoredEI X; the error return from SCH$LOCKREXEC and therefore don't need to unlock 
 X; the MUTEX.  X;
 X; Inputs: X; X;`009R5`009- UCB address  X;`009R4`009- PCB address  X;`009R3`009- IRP addressK X; X; Outputs:. X;& X;`009MUTEX unlocked if it was locked. X; X;-v X . XRAMD_UNLOCK:`009`009`009`009;Unlock our MUTEX Xu? X`009MOVAB`009UCB$L_RAMD_MUTEX(R5),R0`009`009;Address our MUTEXl8 X`009BITW`009#<IRP$M_PAGIO!IRP$M_SWAPIO>,-`009;Paging or1 X`009`009IRP$W_STS(R3)`009`009`009; swapping I/O? % X`009BNEQ`00910$`009`009`009`009;Yep.0- X`009JMP`009G`094SCH$UNLOCK`009`009`009;Nope.a X B X; Here for paging I/O -- check the MBXIO bit to see if we ignored  X; the error from SCH$LOCKREXEC. XgM X10$:`009BBSC`009#IRP$V_MBXIO,IRP$W_STS(R3),20$`009;Did we ignore an error frr Vom SCH$LOCKREXEC?? X`009JMP`009G`094SCH$UNLOCKEXEC`009`009;Nope, unlock and returnM XR. X20$:`009RSB`009`009`009`009;Nothing to unlock X`
 X`009.PageM X`009.Subtitle`009SVAPTE_TO_VA`009- Convert a SVAPTE to a usable virtual addr` Vess XS X;+) X;D X; ----- SVAPTE_TO_VA:  Convert a SVAPTE to a usable virtual address X; X;L X; This routine will take the  SVAPTE from an IRP and convert it to a usableD X; virtual address.  The virtual address can be P0, P1, or S0 space. X;L X; A  special case is made for paging I/O:  We use system space addresses toL X; deal  with paging I/O.  RAMD_STARTIO has  allocated and mapped a range ofL X; SPTs  on top of  the user's  buffer.  We do this for two reasons.  First,L X; user  PTEs involved  in page  read operations  don't have PTE$M_VALID setL X; until after the I/O request completes.  To use the user virtual addressesL X; specified by  these PTEs we  would have to set PTE$M_VALID and also allowL X; KW access  to the pages temporarily.  The  second reason is that we wouldL X; have to run  at IPL$_SYNCH to  prevent the swapper from moving the user'sL X; process header while we were mucking about with the PTEs in it.  By usingL X; system virtual addresses  we only have to run at IPL$_SYNCH for two briefL X; periods - one to synchronize access to the SPTE bitmap in the UCB and the* X; other to synchornize access to the SPT. X;
 X; Note bene:p X;L X; This  routine  assumes  that the  system page table (SPT)  is at a higherL X; virtual address  than  all of the  page tables for  processes (PHDs), andL X; that  the P1  page table is at  a higher virtual  address in the PHD thanL X; the P0 page table.  We must  briefly execute at IPL$_SYNCH to prevent the X; PHD from being outswapped.u X; X; Environment:; X;# X;`009IPL$_ASTDEL, process context.e X;
 X; Inputs: X; X;`009R4`009- PCB address` X;`009R3`009- IRP address  X; X; Outputs:0 X; X;`009R2`009- Virtual address; X; X;-d X M XSVAPTE_TO_VA:`009`009`009`009`009;Convert a SVAPTE to a usable virtual addre0 Vssr Xs< X`009MOVL`009IRP$L_PAGE_VA(R3),R2`009`009;Presume paging I/O8 X`009BITW`009#<IRP$M_PAGIO!IRP$M_SWAPIO>,-`009;Check our/ X`009`009IRP$W_STS(R3)`009`009`009; presumptionh7 X`009BNEQ`00910$`009`009`009`009;Branch if we're right.h8 X`009MOVL`009IRP$L_SVAPTE(R3),R2`009`009;Grab the SVAPTE4 X`009LOCK`009LOCKNAME=MMG,-`009`009`009;Keep the PHD4 X`009`009SAVIPL=-(SP),-`009`009`009; from moving and9 X`009`009PRESERVE=YES`009`009`009;  the SPT from changingO Xu X; S0 space address? Xt3 X`009CMPL`009R2,G`094MMG$GL_SPTBASE`009;S0 address?:E X`009BGEQU`00930$`009`009`009;If GEQU yes, use a different page tableI X0M X; Nope -- it's a process virtual address.  Check to see if it's P0 or P1 spaR Vce. X9: X`009MOVL`009PCB$L_PHD(R4),R1`009 ;Grab our process headerM X`009MULL3`009#4,PHD$L_P0LRASTL(R1),R0 ;Get the length of the P0 page table i( Vn bytesM X`009ADDL`009PHD$L_P0BR(R1),R0`009 ;Compute the ending address of the P0 pageu
 V table +12 X`009CMPL`009R2,R0`009`009`009 ;Is this a P0 page?9 X`009BGEQU`00920$`009`009`009 ;If GEQU no, it's a P1 pagerL X`009SUBL`009PHD$L_P0BR(R1),R2`009 ;Else compute the byte offset into the P0 V page tableF X`009MULL`009#128,R2`009`009`009 ;Convert same into a VA (128*4 = 512)2 X`009UNLOCK`009LOCKNAME=MMG,-`009`009 ;Release the* X`009`009NEWIPL=(SP)+,-`009`009 ; MMG lock& X`009`009PRESERVE=YES`009`009 ;`009... Xe" X10$:`009RSB`009`009`009`009 ;Done X ( X; Here when we have a P1 space address. X M X20$:`009SUBL`009PHD$L_P1BR(R1),R2`009;Compute the byte offset into the P1 paT	 Vge table/M X`009MULL`009#128,R2`009`009`009;Convert same into a virtual page number (128o
 V*4 = 512)B X`009BISL`009#VA$M_P1,R2`009`009;Bias it into a P1 virtual address1 X`009UNLOCK`009LOCKNAME=MMG,-`009`009;Release theo) X`009`009NEWIPL=(SP)+,-`009`009; MMG lockE% X`009`009PRESERVE=YES`009`009;`009...  X`009RSB`009`009`009`009;Done  Xt3 X; Here when we have a system virtual address (eh?)l XtL X30$:`009SUBL`009G`094MMG$GL_SPTBASE,R2`009;Get the byte offset into the SPTM X`009MULL`009#128,R2`009`009`009;Convert same to a virtual page number (128*4, V = 512)C X`009BISL`009#VA$M_SYSTEM,R2`009`009;Convert same into a S0 addressl1 X`009UNLOCK`009LOCKNAME=MMG,-`009`009;Release theg) X`009`009NEWIPL=(SP)+,-`009`009; MMG locka% X`009`009PRESERVE=YES`009`009;`009...q X`009RSB`009`009`009`009;Donew Xe
 X`009.PageL X`009.Subtitle`009RAMD_SMAP`009- Map pages into system virtual address space Xo X;+a X;@ X; ----- RAMD_SMAP:  Map pages into system virtual address space X; X;M X; This routine is called to map user pages into system space.  These pages an VrerL X; part of a page fault read and hence the valid bit in the PTEs is not set. V WeM X; could just set the valid bit, but then we would also have to change the pa; VgeiM X; protection to allow kernel  write to the desired pages,  and it becomes moi VredM X; complicated.  In addition, to properly synchronize with the swapper we wou: Vld L X; have to own the MMG interlock  (to keep the PHD from moving) and thus run V atL X; IPL$_SYNCH,  negating any  performance advantage we might  otherwise  get V by  X; doing all of these gyrations. X; X; Environment:  X; X;`009IPL$_SYNCH, fork context.L X;
 X; Inputs: X; X;`009R5`009- UCB address0 X;`009R3`009- IRP address> X; X; Outputs:  X;L X;`009System address space allocated and mapped on top of the user's buffer. X; X;-  X,G XRAMD_SMAP:`009`009`009`009;Map pages into system virtual address space  Xu9 X`009MOVZWL`009IRP$L_BCNT(R3),R2`009;Fetch the byte count9: X`009MOVZWL`009IRP$W_BOFF(R3),R0`009;Fetch the byte offsetM X`009MOVAB`009511(R2)`091R0`093,R2`009`009;Combine offset and count & round ue Vp: X`009ASHL`009#-VA$S_BYTE,R2,R2`009;Convert to a page countF X`009JSB`009G`094LDR$ALLOC_PT`009`009;Allocate some page table entries( X`009BLBC`009R0,20$`009`009`009;Lossage! XIM X; We've allocated some page table entries;  map them to the specified physicO ValMM X; pages.  We unconditionally map the pages as KW since the only way we can gb VetdF X; here is for page reads (well, page writes, too, but KW won't hurt). X`A X`009MOVL`009R1,IRP$L_SPTE(R3)`009;Save the starting SPTE addressnM X`009MOVL`009R2,IRP$L_NSPTES(R3)`009;Save the number of system page table entW Vries9L X`009SUBL3`009G`094MMG$GL_SPTBASE,R1,R0`009;Get the byte offset into the SPTM X`009MULL`009#128,R0`009`009`009;Convert same to a virtual page # (128*4 = 51P V2)(7 X`009BISL3`009#VA$M_SYSTEM,R0,-`009;Convert same into a36 X`009`009IRP$L_PAGE_VA(R3)`009; system virtual addressL X`009MOVL`009IRP$L_SVAPTE(R3),R0`009;Fetch the address of the first user PTE6 X`009PUSHL`009R3`009`009`009;Keep the IRP address safe X`B X; Note that we don't invalidate the translation buffer here;  I'mB X; presuming that the last user of this system virtual address did X; so. X`1 +-+-+-+-+-+-+-+-  END  OF PART 3 +-+-+-+-+-+-+-+-P -- `3 gkn	Gerard K. Newman		gkn@sds.sdsc.edu	619.534.5076aA 	San Diego Supercomputer Center	gkn@sdsc.bitnet		619.534.5152 FAX % 	PO Box 85608			sdsc::gkn (27.1/span)U# 	San Diego, CA  92186-9784	ucsd!gknl  