          <<< NOTED::DISK$NOTES7:[NOTES$LIBRARY_7OF4]HACKERS.NOTE;1 >>>
                               -< ** Hackers ** >-
================================================================================
Note 1780.2          PFNMAP Section With No Backing Storage?             2 of 15
CSC32::M_MORRIS                                     149 lines   2-OCT-1995 10:28
                         -< Try Using MMG$ALLOCONTIG >-
--------------------------------------------------------------------------------
I am in a class this week and did not have alot of time to study your
mail message or the referenced notes, but I was wondering if the PFN Map 
HAS be to a high memory or if you could simply allocate contiguous free 
pages for it (ie, shortly after boot for example).  I wrote the following 
skeleton program for someone here at the Customer Support Center some
time ago and it MIGHT be useful to you. 

						mark morris

	.title		do PFN mapping to global section for testing idea
; *****************************************************************************
;                            COPYRIGHT (C) 1994 BY
;                      DIGITAL EQUIPMENT CORPORATION, MAYNARD
;                       MASSACHUSETTS.  ALL RIGHTS RESERVED.
;
;    THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
;    ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE INCLUSION
;    OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE OR ANY OTHER COPIES
;    THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER
;    PERSON.  NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY TRANSFERRED.
;
;    THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
;    SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
;
;    DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
;    SOFTWARE ON EQUIPMENT THAT IS NOT SUPPLIED BY DIGITAL.
;
;    NO RESPONSIBILITY IS ASSUMED FOR THE USE OR RELIABILITY OF SOFTWARE
;    ON EQUIPMENT THAT IS NOT SUPPLIED BY DIGITAL EQUIPMENT CORPORATION.
;
;    SUPPORT FOR THIS SOFTWARE IS NOT COVERED UNDER ANY DIGITAL SOFTWARE
;    PRODUCT SUPPORT CONTRACT, BUT MAY BE PROVIDED UNDER THE TERMS OF THE
;    CONSULTING AGREEMENT UNDER WHICH THIS SOFTWARE WAS DEVELOPED.
;
; *****************************************************************************
;
; This example program is UNSUPPORTED and provided AS IS.  No warranty,
; implied or otherwise, is offered by DIGITAL EQUIPMENT CORPORATION or
; any of its employees.  Any loss of services or damages incurred by 
; the use of this program are the sole responsibility of those authorizing
; it's execution.
;
; This program does the following actions:
;
; a) it locks down into the process working set the Kernel Mode code
;    used to allocate contiguous global pages for a large global section
;    (typically executed during startup when there are a large number of
;    contiguous free pages).  If we were to pagefault in this code segment
;    we could crash VMS, thus we want to make sure the code/data used are
;    memory resident before proceeding.
;
; b) it then goes to a kernel mode routine, ALLOC_MEM, that calls the
;    VMS routine MMG$ALLOCONTIG to allocate the requested number of
;    contiguous pages from the longword NUMBER_PAGES.  In order to do
;    this successfully several items must be meet:
;    
;    i) there must be enough contiguous free pages to allocate the request,
;       that is why executing it for a large number of global pages just
;       after a reboot is suggested.
;
;    ii) there must be enough SPTREQ (System Page Table Required) PTEs to
;       map the pages.  For every one SPTREQ we can map 128 additional
;       pages of memory.
;
;    iii) there must be enough contiguous SPTREQ free PTEs, so you may have
;       to make SPTREQ slightly larger than what you may think my be needed.
;
; c) it then creates a PFN mapped section to these allocated free pages,
;    thus allowing other processes to be able to map to the global section
;    as well.  Being a PFN mapped global section we do NOT have any backing
;    storage on disk, and the pages are not counted as part of the working
;    set and are always memory resident.
;
;    To do this the SYSGEN paramater GBLPAGES may have to be large enough
;    to map this as a Global Section.
;
	.library	/sys$share:lib.mlb/	; System macro library
	.link		/sys$system:sys.stb/	; System symbols for linktime

	$ipldef
	$secdef
	$ssdef

	.entry		alloc,^m<>
	nop						; for stepping thru debugger

	$lkwset_s	inadr=lock_code			; lock kernel mode code
	blbs		r0,10$				;
	$exit_s		r0				;
10$:							;

	$cmkrnl_s	routin=alloc_mem		; allocate PFNs
	tstl		r0				; see if we got any PFNs
	bgtr		20$				;
	movl		#ss$_insfmem,r0			;
	$exit_s		r0				;
20$:							;
		
        clrl            section_flags			; set up section flags
        bisl2           #sec$m_gbl,section_flags	;
        bisl2           #sec$m_wrt,section_flags	;
	bisl2		#sec$m_expreg,section_flags	;
        bisl2           #sec$m_perm,section_flags	;
        bisl2           #sec$m_sysgbl,section_flags	;
	bisl2		#sec$m_pfnmap,section_flags	;

	$crmpsc_s	inadr=input_address,-		; create & map section
			retadr=return_address,-		; 
			flags=section_flags,-		;
			gsdnam=section_name,-		;
			pagcnt=number_pages,-		;
			vbn=start_pfn			;
	blbs		r0,30$				; check for error
	$exit_s		r0				;
30$:							;
	
madeit:	nop						; 
	$exit_s						; 

lock_code_start:
	.entry		alloc_mem,^m<>			; Kernel Mode Code
	lock		MMG				;
	movl		number_pages,r1			; number of pages to try
	jsb		g^mmg$allocontig		; allocate cont pages
	movl		r0,start_pfn			; save first PFN
	unlock		MMG				;
	ret						;

number_pages:		.long	10			; map 10 pages
start_pfn:		.long	0			; return PFN

lock_code_end:

lock_code:						; lock kernel mode code
	.address	lock_code_start			; into working set so
	.address	lock_code_end			; we can't pagefault at

input_address:						; used w/ Expand Region
	.long		0				;   to expand process 
	.long		0				;   PTEs in P0 space
	
return_address:						; virtual address of section
	.long		0				;
	.long		0				;

section_flags:		.long	0			; flags longword
section_name:		.ascid	/MorrisTestPFN0/	; name of section
							; elevated IPL
	.end	alloc			
