; $NetBSD$

;
; Copyright (c) 1999 David Daniels
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
; 1. Redistributions of source code must retain the above copyright
;    notice, this list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright
;    notice, this list of conditions and the following disclaimer in the
;    documentation and/or other materials provided with the distribution.
; 3. All advertising materials mentioning features or use of this software
;    must display the following acknowledgement:
;	This product includes software developed by David Daniels
; 4. The name of the company nor the name of the author may be used to
;    endorse or promote products derived from this software without specific
;    prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY DD ``AS IS'' AND ANY EXPRESS OR IMPLIED
; WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
; IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
; INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
; SUCH DAMAGE.
;
; RiscBSD kernel project
;
; pregrow.s
;
;
; Created      : 08/09/99
; Last updated : 08/09/99
;
;    $Id$
;

	GET	h.asmregs
	GET	h.arm6

        AREA |C$$code|, CODE, READONLY

	IMPORT	first_page

        EXPORT	pregrow

; Pregrow handler used when creating the dynamic area for the NetBSD kernel
; when running under RISCOS 4

; On entry:
;	R0 = 0	(pregrow)
;	R1 -> page block
;	R2 = number of entries in page block
;	R3 = Amount area is growing by in bytes
;	R4 = Current size of area in bytes
;	R5 = Page size
;	R12 = pointer to workspace

; On exit:
;	All registers preserved
;	'first page' set to next page number to use

pregrow
	TEQ	R0,#0		;Pregrow call?
	MOVNES	PC,LR		;No, so return
	TEQ	R2,#0		;Ensure the page block contains at least one entry
	MOVEQS	PC,LR		;Oops...
	STMFD	R13!,{R0-R2,R5}
	LDR	R5,pageno
	LDR	R0,[R5]		;Number of page to allocate to dynamic area
allocate
	STR	R0,page_number	;Store the number of the page we want to use
	ADD	R0,R0,#1	;Bump up page number as per OS_Memory 12 docs
	ADD	R1,R1,#ptesize	;Point at next entry in page block
	SUBS	R2,R2,#1
	BNE	allocate
	STR	R0,[R5]		;Save the final page number
	LDMFD	R13!,{R0-R2,R5}
	MOVS	PC, LR

; 'first_page' is the page number returned by OS_Memory 12

pageno	DCD	first_page

; Layout of page block entry (based area using R1)

		^	0,R1
page_number	#	4
logical_addr	#	4
physical_addr	#	4

ptesize		EQU	@-page_number

        END
