ChangeDyn 265 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
        TTL   => ChangeDyn

17 18
        ; OS_ChangeDynamicArea optimisations:

Jeffrey Lee's avatar
Jeffrey Lee committed
19 20
        GBLL  FastCDA_Bulk ; Do all cache/TLB maintenance in bulk instead of on a per-page basis
FastCDA_Bulk SETL {TRUE}
21 22 23 24

        GBLL  FastCDA_FIQs ; Don't thrash ClaimFIQ/ReleaseFIQ in DoTheGrowPagesSpecified
FastCDA_FIQs SETL {TRUE}

Jeffrey Lee's avatar
Jeffrey Lee committed
25
        GBLL  FastCDA_Unnecessary ; Avoid unnecessary cache cleaning in DoTheGrowPagesSpecified
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
FastCDA_Unnecessary SETL {TRUE}

        ; DoTheGrowPagesSpecified profiling code
        ; Written to use Cortex-A8 cycle count performance counter - will need modifying for other CPUs!

        GBLL  FastCDA_Prof
FastCDA_Prof SETL {FALSE}

      [ FastCDA_Prof
        ; Squeeze profiling workspace into "free space after envstring"
                                 ^ ExtendedROMFooter+4
        ! 0, "FastCDA_Prof workspace at ":CC::STR:@
FastCDA_Prof_DoTheGrowInit           # 4
FastCDA_Prof_MarkRequired            # 4
FastCDA_Prof_PagesUnsafe             # 4
Jeffrey Lee's avatar
Jeffrey Lee committed
41
FastCDA_Prof_DoublyRemoveCacheability # 4
42 43 44 45 46 47 48 49 50 51 52 53
FastCDA_Prof_DoublyMovePages         # 4
FastCDA_Prof_FindSpare               # 4
FastCDA_Prof_ClaimFIQ                # 4
FastCDA_Prof_AccessPhysical          # 4
FastCDA_Prof_CopyPage                # 4
FastCDA_Prof_ReleasePhysical         # 4
FastCDA_Prof_MoveReplacement         # 4
FastCDA_Prof_MoveNeeded              # 4
FastCDA_Prof_ReleaseFIQ              # 4
FastCDA_Prof_PagesSafe               # 4
FastCDA_Prof_CallPreGrow             # 4
FastCDA_Prof_CallPostGrow            # 4
Jeffrey Lee's avatar
Jeffrey Lee committed
54 55
FastCDA_Prof_MMUChangingCached       # 4 ; MMU_ChangingUncached followed by Cache_CleanInvalidateaAll
FastCDA_Prof_MMUChangingUncached     # 4 ; MMU_ChangingUncached followed by nothing
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
FastCDA_Prof_ChangingEntry           # 4
        ASSERT @ <= &500
      ]

        MACRO
        FastCDA_ProfInit $temp
      [ FastCDA_Prof
        MVN     $temp,#0
        MCR     p15,0,$temp,c9,c12,2
        MOV     $temp,#1<<31
        MCR     p15,0,$temp,c9,c12,1
        MOV     $temp,#7
        MCR     p15,0,$temp,c9,c12,0
      ]
        MEND

        MACRO
        FastCDA_ProfStart $var,$temp,$temp2,$temp3,$cc
      [ FastCDA_Prof
        LDR$cc  $temp,=ZeroPage+FastCDA_Prof_$var
        LDR$cc  $temp2,[$temp]
        MRC$cc  p15,0,$temp3,c9,c13,0
        SUB$cc  $temp2,$temp2,$temp3
        STR$cc  $temp2,[$temp]
      ]
        MEND

        MACRO
        FastCDA_ProfEnd $var,$temp,$temp2,$temp3,$cc
      [ FastCDA_Prof
        MRC$cc  p15,0,$temp3,c9,c13,0
        LDR$cc  $temp,=ZeroPage+FastCDA_Prof_$var
        LDR$cc  $temp2,[$temp]
        ADD$cc  $temp2,$temp2,$temp3
        STR$cc  $temp2,[$temp]
      ]
        MEND

Neil Turton's avatar
Neil Turton committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
;******************************************************************************
; ChangeDynamic SWI
; In  : R0 =  0 => System Heap,
;             1 => RMA
;             2 => Screen
;             3 => Sprite area
;             4 => Font cache
;             5 => RAM disc
;             6 => Free pool
;       R1 = no of bytes to change by
;
; Out : V set if CAO in AplWork or couldn't move all the bytes requested.
;       R1 set to bytes moved.
;******************************************************************************

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
; OS access privileges (OSAP_ to differentiate from AP_ used in Hdr:MEMM.*)
OSAP_Full * 0 ; user r/w/x, priv r/w/x
OSAP_Read * 1 ; user r/x, priv r/w/x
OSAP_None * 2 ; user none, priv r/w/x
OSAP_ROM  * 3 ; user r/x, priv r/x

; Corresponding values for OS_Memory 24
; (n.b. - XN flag is inverted)
CMA_ROM  * CMA_Partially_UserR+CMA_Partially_UserXN+CMA_Partially_PrivR+CMA_Partially_PrivXN
CMA_Read * CMA_ROM+CMA_Partially_PrivW
CMA_Full * CMA_Read+CMA_Partially_UserW
CMA_None * CMA_Partially_PrivR+CMA_Partially_PrivW+CMA_Partially_PrivXN

; Convenience macro for defining DA/page flags and corresponding CMA value
        MACRO
$area   DefAreaFlags $ap, $extra
AreaFlags_$area * OSAP_$ap :OR: ($extra + 0)
CMA_$area * CMA_$ap
        MEND

; Flags for kernel-managed DAs
AppSpace         DefAreaFlags Full
SysHeap          DefAreaFlags Full
RMA              DefAreaFlags Full
Screen           DefAreaFlags Full, DynAreaFlags_NotCacheable :OR: DynAreaFlags_DoublyMapped :OR: DynAreaFlags_NeedsSpecificPages
Sprites          DefAreaFlags Full
FontArea         DefAreaFlags None
RAMDisc          DefAreaFlags None, DynAreaFlags_NotCacheable
RAMDisc_SA       DefAreaFlags None ; StrongARM-specific (~CB gives poor performance for current StrongARMs)
FreePool         DefAreaFlags None, DynAreaFlags_NotCacheable :OR: DynAreaFlags_NotBufferable :OR: DynAreaFlags_PMP

; Flags for other kernel managed areas

Duff                 DefAreaFlags None, DynAreaFlags_NotCacheable :OR: DynAreaFlags_NotBufferable
CursorChunkCacheable DefAreaFlags Read, PageFlags_Unavailable ; Should be OSAP_None?
CursorChunk          DefAreaFlags Read, PageFlags_Unavailable :OR: DynAreaFlags_NotCacheable
145
PageTablesAccess     DefAreaFlags None ; n.b. just the AP value, for full page flags use PageTable_PageFlags workspace var
146 147 148 149 150 151
HALWorkspace         DefAreaFlags Read, PageFlags_Unavailable
HALWorkspaceNCNB     DefAreaFlags None, DynAreaFlags_NotCacheable :OR: DynAreaFlags_NotBufferable :OR: PageFlags_Unavailable
ZeroPage             DefAreaFlags Read, PageFlags_Unavailable
ScratchSpace         DefAreaFlags Read, PageFlags_Unavailable
DCacheClean          DefAreaFlags None ; ideally, svc read only, user none but hey ho
CAM                  DefAreaFlags None, PageFlags_Unavailable
152
SVCStack             DefAreaFlags None, PageFlags_Unavailable
153 154 155 156
IRQStack             DefAreaFlags None, PageFlags_Unavailable
ABTStack             DefAreaFlags None, PageFlags_Unavailable
UNDStack             DefAreaFlags None, PageFlags_Unavailable
Kbuffs               DefAreaFlags Read, PageFlags_Unavailable
157 158 159
  [ EmulateAP1
DebuggerSpace        DefAreaFlags Full, PageFlags_Unavailable ; Downgrade from read-only to full access in usermode, to avoid LongDesc AP1 emulation causing it to become non-executable in usermode
  |
160
DebuggerSpace        DefAreaFlags Read, PageFlags_Unavailable
161
  ]
Jeffrey Lee's avatar
Jeffrey Lee committed
162
RWArea               DefAreaFlags None, PageFlags_Unavailable
Neil Turton's avatar
Neil Turton committed
163

164 165 166
  [ DA_Batman
ChangeDyn_Batcall    * -3               ; special DA number to select Batman usage of OS_ChangeDynamicArea
  ]
167
; -2 was an internal value, now no longer used
168
; -> See hdr.OSMem for other area definitions
Neil Turton's avatar
Neil Turton committed
169

170
; Page block format, 32 bit log & phys addrs
171 172 173 174 175 176
                        ^ 0
MemPageBlock32_PageNum  # 4
MemPageBlock32_LogAddr  # 4
MemPageBlock32_PhysAddr # 4
MemPageBlock32_Size     # 0

177 178 179 180 181 182 183 184 185
; Page block format, 64 bit log & phys addrs
                        ^ 0
MemPageBlock64_PageNum  # 4
MemPageBlock64_LogLow   # 4
MemPageBlock64_LogHigh  # 4
MemPageBlock64_PhysLow  # 4
MemPageBlock64_PhysHigh # 4
MemPageBlock64_Size     # 0

Neil Turton's avatar
Neil Turton committed
186 187
; Number of entries in page block on stack

188
NumPageBlockEntries *   63
189 190
PageBlockSize64 *       NumPageBlockEntries * MemPageBlock64_Size
PageBlockSize32 *       NumPageBlockEntries * MemPageBlock32_Size
Neil Turton's avatar
Neil Turton committed
191 192
PageBlockChunk  *       NumPageBlockEntries * 4096

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
;
; mjs - performance enhancements (from Ursula, merged into HALised kernel June 2001)
; Workspace for acceleration of operations on a DA, by reducing need to traverse DA list.
;
; - accelerates allocating non-quick DA numbers to O(n) instead of laughable O(n*n), where n is no. of DAs
; - accelerates enumeration to O(n) instead of laughable O(n*n)
; - allocation of a quick handle (DA number) is O(1)
; - access of a DA node from a quick handle is O(1)
; - access of a DA node from a non-quick handle is O(1), if it repeats the most recent non-quick handle access (else O(n))
;
; - creation of a DA still has some O(n) work (requires search for address space), but is now rather quicker
; - removal of a DA is still O(n) (requires traversal of list in order to get previous node)
; - other uses of a DA with a quick handle (eg. get info, change size) avoid any O(n) work
;
; - all system handles will be quick.
; - non-system handles will be quick, except for very large numbers of DAs, or silly modules like the Wimp who insist on
;   their own silly DA number (the latter can still benefit from LastTreacleHandle - see below)
;
; Limitations:
; - does not allow anyone to choose their own DA number that clashes with the quick handle set - should not
;   be a problem since choosing own number reserved for Acorn use
; - does not allow anyone to renumber a DA with a quick handle - again, reserved for system use
; - DA names will be truncated to a maximum of 31 characters (or as defined below)
;
                                  GBLL DynArea_QuickHandles
DynArea_QuickHandles              SETL {TRUE}
;
      ;various bad things happen if DynArea_QuickHandles is FALSE (eg. some new API disappears)
      ;should remove FALSE build option to simplify source code next time kernel is updated (kept for reference/testing now)
      ASSERT DynArea_QuickHandles

                                  GBLL DynArea_NullNamePtrMeansHexString
DynArea_NullNamePtrMeansHexString SETL {TRUE} :LAND: DynArea_QuickHandles
;
  [ DynArea_QuickHandles
DynArea_MaxNameLength     * 31                      ;maximum length of DA name, excluding terminator (multiple of 4, -1)
DynArea_NumQHandles       * 256                     ;maximum no. of non-system quick handles available simultaneously
230 231 232
DynArea_AddrLookupBits    * 8                       ;LUT covers entire 4G logical space, so 4G>>8 = 16M granularity
DynArea_AddrLookupSize    * 1<<(32-DynArea_AddrLookupBits) ; Address space covered by each entry
DynArea_AddrLookupMask    * &FFFFFFFF-(DynArea_AddrLookupSize-1)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
;
                          ^  0,R11
DynArea_TreacleGuess      # 4                       ;guess for next non-quick handle to allocate, if needed, is TreacleGuess+1
DynArea_CreatingHandle    # 4                       ;handle proposed but not yet committed, during DynArea_Create, or -1 if none
DynArea_CreatingPtr       # 4                       ;ptr to proposed DANode during DynArea_Create (invalid if CreatingHandle = -1)
DynArea_LastTreacleHandle # 4                       ;last non-quick handle accessed by a client (usually the Wimp), or -1 if none
DynArea_LastTreaclePtr    # 4                       ;ptr to DANode for last non-quick handle accessed (invalid if LastTreacleHandle = -1)
DynArea_LastEnumHandle    # 4                       ;last handle enumerated, or -1 if none
DynArea_LastEnumPtr       # 4                       ;ptr to DANode for last handle enumerated (invalid if LastEnumHandle = -1)
DynArea_ShrinkableSubList # 4                       ;sub list of dynamic areas that are Shrinkable (0 if none)
DynArea_OD6Signature      # 4                       ;signature of changes to non-system DAs since last call to OS_DynamicArea 6
                                                    ;bit  0 = 1 if any DAs have been created
                                                    ;bit  1 = 1 if any DAs have been removed
                                                    ;bit  2 = 1 if any DAs have been resized (excluding grow or shrink at creation or removal)
                                                    ;bit  3 = 1 if any DAs have been renumbered
                                                    ;bits 4-30   reserved (0)
                                                    ;bit 31 = 1 if next resize is not to update signature (used during create, remove)
DynArea_OD6PrevSignature  # 4                       ;previous signature, used to distinguish single from multiple changes
DynArea_OD6Handle         # 4                       ;handle of last DA that affected signature
DynArea_OD8Clamp1         # 4                       ;clamp value on area max size for OS_DynamicArea 0 with R5 = -1
                                                    ;(default -1, set by R1 of OS_DynamicArea 8)
DynArea_OD8Clamp2         # 4                       ;clamp value on area max size for OS_DynamicArea 0 with R5 > 0 (not Sparse)
                                                    ;(default -1, set by R2 of OS_DynamicArea 8)
DynArea_OD8Clamp3         # 4                       ;clamp value on area max size for OS_DynamicArea 0 for a Sparse area
                                                    ;(default 4G-4k, set by R3 of OS_DynamicArea 8)
DynArea_SortedList        # 4                       ;alphabetically sorted list of non-system areas, or 0 if none
DynArea_SysQHandleArray   # 4*(ChangeDyn_MaxArea+1) ;for system areas 0..MaxArea, word = ptr to DANode, or 0 if not created yet
DynArea_FreeQHandles      # 4                       ;index of first free quick handle, starting at 1 (or 0 for none)
DynArea_QHandleArray      # 4*DynArea_NumQHandles   ;1 word per quick handle
                                                    ; - if free, word = index of next free quick handle (or 0 if none)
                                                    ; - if used, word = ptr to DANode (must be > DynArea_NumQHandles)
264
DynArea_AddrLookup        # 4<<DynArea_AddrLookupBits ; Lookup table for fast logaddr -> dynarea lookup
265 266 267 268 269 270 271 272
;
DynArea_ws_size           *  :INDEX:@               ;must be multiple of 4
;
            ASSERT DynArea_QHandleArray = DynArea_FreeQHandles +4
  ]
;


Kevin Bracey's avatar
Kevin Bracey committed
273
;        InsertDebugRoutines
Neil Turton's avatar
Neil Turton committed
274 275 276 277

; Exit from ChangeDynamicArea with error Not all moved

failure_IRQgoingClearSemaphore
Jeffrey Lee's avatar
Jeffrey Lee committed
278 279
        LDR     r0, =ZeroPage
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
280
        STR     r0, [r0, #CDASemaphore]
Jeffrey Lee's avatar
Jeffrey Lee committed
281 282 283 284
      |
        MOV     r10, #0
        STR     r10, [r0, #CDASemaphore]
      ]
Neil Turton's avatar
Neil Turton committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
failure_IRQgoing
        ADR     r0, ErrorBlock_ChDynamNotAllMoved
ChangeDynamic_Error
        MOV     r10, #0
        STR     r0, [stack]
        LDR     lr, [stack, #4*10]
        ORR     lr, lr, #V_bit
        STR     lr, [stack, #4*10]
CDS_PostServiceWithRestore
      [ International
        LDR     r0, [stack]
        BL      TranslateError
        STR     r0, [stack]
      ]

; and drop thru to ...

CDS_PostService
        MOV     r1, #Service_MemoryMoved
        MOV     r0, r10                 ; amount moved
        MOVS    r2, r11                 ; which way was transfer?
        BMI     %FT47                   ; [definitely a grow]
        CMP     r11, #ChangeDyn_FreePool
        BNE     %FT48                   ; [definitely a shrink]
        CMP     r12, #ChangeDyn_AplSpace
        BEQ     %FT48                   ; [a shrink]
47
        RSB     r0, r0, #0             ; APLwork or free was source
        MOV     r2, r12                ; r2 = area indicator
48
        BL      Issue_Service

        MOV     r1, r10                ; amount moved

        Pull    "r0, r2-r9, r10, lr"
        ExitSWIHandler

        MakeErrorBlock ChDynamNotAllMoved

324 325 326 327 328
; Call_CAM_Mapping
; in:   r2 = physical page number
;       r3 = logical address (2nd copy of doubly mapped area)
;       r9 = offset from 1st to 2nd copy of doubly mapped area (either source or dest, but not both)
;       r11 = PPL + CB bits
Neil Turton's avatar
Neil Turton committed
329 330
Call_CAM_Mapping
        Push    "r0, r1, r4, r6, lr"
331
        PTOp    BangCamUpdate
Neil Turton's avatar
Neil Turton committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        Pull    "r0, r1, r4, r6, pc"

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r0 bits 0..6 = area number
;       r0 bit 7 set => return max area size in r2 (implemented 13 Jun 1990)
;                       this will return an error if not implemented
; Out   r0 = address of area
;       r1 = current size of area
;       r2 = max size of area if r0 bit 7 set on entry (preserved otherwise)

; TMD 19-May-93: When this is updated to new CDA list, change meaning as follows:

; r0 in range 0..&7F    return address, size of area r0
;             &80..&FF  return address, size, maxsize of area (r0-&80)
;             &100..    return address, size, maxsize of area r0

; TMD 20-Aug-93: New bit added - if r0 = -1 on entry, then returns info on application space
; r0 = base address (&8000)
; r1 = current size (for current task)
; r2 = maximum size (eg 16M-&8000)

ReadDynamicArea ROUT

355
        ASSERT  ChangeDyn_MaxArea < ReadDyn_ReturnMax
Neil Turton's avatar
Neil Turton committed
356

357
        CMP     r0, #ChangeDyn_AplSpace         ; if finding out about app space
Jeffrey Lee's avatar
Jeffrey Lee committed
358
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
359
        LDREQ   r1, [r0, #AplWorkSize+1]        ; then r1 = current size
360
        LDREQ   r2, [r0, #SoftAplWorkMaxSize+1] ; and r2 = max size
Jeffrey Lee's avatar
Jeffrey Lee committed
361
      |
362 363 364
        LDREQ   r2, =ZeroPage
        LDREQ   r1, [r2, #AplWorkSize]          ; then r1 = current size
        LDREQ   r2, [r2, #SoftAplWorkMaxSize]   ; and r2 = max size
Jeffrey Lee's avatar
Jeffrey Lee committed
365
      ]
Neil Turton's avatar
Neil Turton committed
366 367 368 369 370 371 372 373 374 375
        MOVEQ   r0, #&8000                      ; r0 = base address
        SUBEQ   r1, r1, r0                      ; adjust size and maxsize
        SUBEQ   r2, r2, r0                      ; to remove bottom 32K
        ExitSWIHandler EQ

; first check if it's one of the new ones

        Push    "r1,lr"
        CMP     r0, #&100                       ; if area >= &100
        MOVCS   r1, r0                          ; then just use area
376
        BICCC   r1, r0, #ReadDyn_ReturnMax      ; else knock off bit 7
377 378 379
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber                ; out: r10 -> node
  |
Neil Turton's avatar
Neil Turton committed
380
        BL      CheckAreaNumber                 ; out: r10 -> node
381
  ]
Neil Turton's avatar
Neil Turton committed
382 383 384
        Pull    "r1,lr"
        BCC     %FT05                           ; [not a new one, so use old code]

385 386 387
        LDR     r11, [r10, #DANode_Flags]
        TST     r11, #DynAreaFlags_PMP
        BNE     %FT01
Neil Turton's avatar
Neil Turton committed
388 389 390 391 392 393
        CMP     r0, #&80                        ; CS => load maxsize into R2
                                                ; (do this either if bit 7 set, or area >=&100)
        LDRCS   r2, [r10, #DANode_MaxSize]
        LDR     r1, [r10, #DANode_Size]         ; r1 = current size
        LDR     r0, [r10, #DANode_Base]         ; r0 -> base
        TST     r11, #DynAreaFlags_DoublyMapped
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        SUBNE   r0, r0, r1                      ; if doubly mapped then return start of 1st copy for compatibility
        ExitSWIHandler
01
        ; Convert physical parameters into byte counts
        CMP     r0, #&80
        BCC     %FT02
        LDR     r2, [r10, #DANode_PMPMaxSize]
        CMP     r2, #DynArea_PMP_BigPageCount
        MOVLO   r2, r2, LSL #12
        LDRHS   r2, =DynArea_PMP_BigByteCount
02
        LDR     r1, [r10, #DANode_PMPSize]
        CMP     r1, #DynArea_PMP_BigPageCount
        MOVLO   r1, r1, LSL #12
        LDRHS   r1, =DynArea_PMP_BigByteCount
        LDR     r0, [r10, #DANode_Base]
Neil Turton's avatar
Neil Turton committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        ExitSWIHandler
05
        ADRL    r0, ErrorBlock_BadDynamicArea
      [ International
        Push    "lr"
        BL      TranslateError
        Pull    "lr"
      ]
        B       SLVK_SetV

        MakeErrorBlock  BadDynamicArea

; *************************************************************************
; User access to CAM mapping
; ReadMemMapInfo:
; returns R0 = pagsize
;         R1 = number of pages in use  (= R2 returned from SetEnv/Pagesize)
; *************************************************************************

ReadMemMapInfo_Code
Jeffrey Lee's avatar
Jeffrey Lee committed
430
      LDR      R10, =ZeroPage
Neil Turton's avatar
Neil Turton committed
431
      LDR      R0, [R10, #Page_Size]
432
      LDR      R1, [R10, #RAMLIMIT]    ; = total number of pages
Neil Turton's avatar
Neil Turton committed
433 434 435 436 437 438 439 440 441 442 443
      ExitSWIHandler

; ************************************************************************
; SWI ReadMemMapEntries: R0 pointer to list.
;  Entries are three words long, the first of which is the CAM page number.
;  List terminated by -1.
; Returns pagenumber (unaltered)/address/PPL triads as below
; ************************************************************************

ReadMemMapEntries_Code  ROUT
        Push    "r0,r14"
Jeffrey Lee's avatar
Jeffrey Lee committed
444
        LDR     r14, =ZeroPage
Neil Turton's avatar
Neil Turton committed
445 446 447
        LDR     r10, [r14, #CamEntriesPointer]
        LDR     r14, [r14, #MaxCamEntry]
01
448
        LDR     r12, [r0], #4                   ; page number
Neil Turton's avatar
Neil Turton committed
449 450 451
        CMP     r12, r14
        Pull    "r0,r14", HI
        ExitSWIHandler HI
452

453
   [ AMB_LazyMapIn
454 455 456 457 458 459 460
        ;may need AMB to make mapping honest (as if not lazy), if page is in currently mapped app
        Push    "r0, lr"
        MOV     r0, r12                         ; page number to make honest
        BL      AMB_MakeHonestPN
        Pull    "r0, lr"
   ]

461 462 463
        ADD     r11, r10, r12, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
Neil Turton's avatar
Neil Turton committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
        LDMIA   r11, {r11, r12}
        STMIA   r0!, {r11, r12}
        B       %BT01

; ************************************************************************
; SWI FindMemMapEntries:
; In:  R0 -> table of 12-byte page entries
;       +0      4       probable page number (0..npages-1) (use 0 if no idea)
;       +4      4       logical address to match with
;       +8      4       undefined
;       terminated by a single word containing -1
;
; Out: table of 12-byte entries updated:
;       +0      4       actual page number (-1 => not found)
;       +4      4       address (preserved)
;       +8      4       page protection level (3 if not found)
;       terminator preserved
;
; ************************************************************************

FindMemMapEntries_Code  ROUT

; Code for expanded CAM map version

488
        Push    "r0, r3-r5,r7-r9, r14"
Jeffrey Lee's avatar
Jeffrey Lee committed
489
        LDR     r14, =ZeroPage
490 491
        LDR     r7, [r14, #MaxCamEntry]
        LDR     r12, [r14, #CamEntriesPointer]  ; r12 -> start of cam map
Neil Turton's avatar
Neil Turton committed
492
10
493 494 495
        LDR     r3, [r0, #0]                    ; r3 = guess page number (or -1)
        CMP     r3, #-1                         ; if -1 then end of list
        Pull    "r0, r3-r5,r7-r9, r14", EQ      ; so restore registers
Neil Turton's avatar
Neil Turton committed
496 497
        ExitSWIHandler EQ                       ; and exit

498
        LDR     r4, [r0, #4]                    ; r4 = logical address
499

500
   [ AMB_LazyMapIn
501
        ;may need AMB to make mapping honest (as if not lazy), if page is in currently mapped app
502 503
        Push    "r0"
        MOV     r0, r4                          ; logical address to make honest
504
        BL      AMB_MakeHonestLA                ; note, quickly dismisses non app space addresses
505
        Pull    "r0"
506 507
   ]

508
        CMP     r3, r7                          ; if off end of CAM
Neil Turton's avatar
Neil Turton committed
509
        BHI     %FT20                           ; then don't try to use the guess
510 511 512 513
        
        ADD     r10, r12, r3, LSL #CAM_EntrySizeLog2
        LDR     lr, [r10, #CAM_LogAddr]         ; load address from guessed page
        TEQ     r4, lr                          ; compare address
Neil Turton's avatar
Neil Turton committed
514 515 516
        BEQ     %FT60                           ; if equal, then guessed page was OK
20

517 518
; use the page tables to perform a logical -> physical (-> page no.) conversion

519
        PTOp    logical_to_physical             ; r5 corrupt, r8,r9 = phys
520 521 522 523 524 525 526 527
        BLCC    physical_to_ppn                 ; r5,r10-r11 corrupt, r3 = page
        ; If we found a page, double-check that the logical address is a match
        ; (means we can never find the second mapping of doubly-mapped regions
        ; - is that intentional?)
        ADDCC   r10, r12, r3, LSL #CAM_EntrySizeLog2
        LDRCC   lr, [r10, #CAM_LogAddr]
        SUBCS   lr, r4, #1
        TEQ     r4, lr                          ; page found?
Neil Turton's avatar
Neil Turton committed
528
60
529
        LDREQ   lr, [r10, #CAM_PageFlags]       ; if match, then lr = PPL
Neil Turton's avatar
Neil Turton committed
530

531 532
        MOVNE   r3, #-1                         ; else unknown page number indicator
        MOVNE   lr, #3                          ; and PPL=3 (no user access)
Neil Turton's avatar
Neil Turton committed
533

534
        STMIA   r0!, {r3,r4,lr}                 ; store all 3 words
Neil Turton's avatar
Neil Turton committed
535 536 537 538 539
        B       %BT10                           ; and go back for another one

;**************************************************************************
; SWI SetMemMapEntries: R0 pointer to list of CAM page/address/PPL triads,
;  terminated by -1.
540
; address of -1 means "put the page out of the way"
Neil Turton's avatar
Neil Turton committed
541 542
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

543 544 545 546 547
; note, if ChocolateAMB, no MakeHonest consideration here, this SWI just
; changes the mapping of pages regardless of their current mapping, and
; assumes the caller knows what he is doing (ho ho)
;

Neil Turton's avatar
Neil Turton committed
548 549 550 551
SetMemMapEntries_Code  ROUT
        Push    "r0-r6, r9, lr"
        MOV     r12, r0

552
; BangCamUpdate takes entry no in r2, logaddr to set to in r3, r11 = PPL
Neil Turton's avatar
Neil Turton committed
553 554
; corrupts r0,r1,r4,r6

Jeffrey Lee's avatar
Jeffrey Lee committed
555
        LDR     r9, =ZeroPage
Neil Turton's avatar
Neil Turton committed
556
        LDR     r5, [r9, #MaxCamEntry]
557 558
        LDR     r9, [r9, #CamEntriesPointer]
        ADD     r9, r9, #CAM_PageFlags
Neil Turton's avatar
Neil Turton committed
559 560 561 562 563 564 565
01
        LDR     r2, [r12], #4
        CMP     r2, r5
        BHI     %FT02                   ; finished
        LDMIA   r12!, {r3, r11}
        CMP     r3, #-1
        LDRHS   r3, =DuffEntry
566
        MOVHS   r11, #AreaFlags_Duff
567 568
        ; Ensure PMP membership flag is retained - just in case caller doesn't
        ; know what he's doing
569
        LDR     r6, =StickyPageFlags+DynAreaFlags_PMP
570
        LDR     r0, [r9, r2, LSL #CAM_EntrySizeLog2]
571 572
        AND     r0, r0, r6
        BIC     r11, r11, r6
573
        ORR     r11, r11, r0
574
        PTOp    BangCamUpdate
Neil Turton's avatar
Neil Turton committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
        B       %BT01
02
        Pull    "r0-r6, r9, lr"
        ExitSWIHandler

        LTORG



;**************************************************************************
;
;       DynamicAreaSWI - Code to handle SWI OS_DynamicArea
;
; in:   r0 = reason code
;       Other registers depend on reason code
;
; out:  Depends on reason code
;

594 595 596

DynArea_NewAreas *      &100            ; Allocated area numbers start here
DynArea_NewAreasBase *  &04000000       ; Allocated area addresses start here
597 598
DynArea_PMP_BigPageCount * 1:SHL:(31-12) ; If PMP has >= this many pages...
DynArea_PMP_BigByteCount * &7FFFF000     ; Then convert to this byte value
Neil Turton's avatar
Neil Turton committed
599

600
;
601
; Internal page flags (note - may overlap DA flags)
602 603
;
TempUncacheableShift            * 16
604
PageFlags_TempUncacheableBits   * 15 :SHL: TempUncacheableShift    ; temporary count of uncacheability, used by DMA mgr (via OS_Memory 0)
605 606
PageFlags_Required              *  1 :SHL: 21                      ; physical page asked for by handler (only set temporarily)

607
;
608
; Temporary flags only used by kernel (note - may overlap DA flags)
609
;
610 611
PageFlags_Unsafe                *  1 :SHL: 31                      ; skip cache/TLB maintenance in BangCamUpdate. flag not saved to CAM map.

612
; Mask to convert DANode_Flags to page flags (i.e. flags that are common between the two)
613 614 615 616 617
DynAreaFlags_AccessMask * DynAreaFlags_APBits :OR: DynAreaFlags_NotBufferable :OR: DynAreaFlags_NotCacheable :OR: DynAreaFlags_DoublyMapped :OR: DynAreaFlags_CPBits :OR: DynAreaFlags_PMP
; PMP LogOp can specify these flags
DynAreaFlags_PMPLogOpAccessMask * (DynAreaFlags_AccessMask :OR: PageFlags_Unavailable) :AND: :NOT: (DynAreaFlags_DoublyMapped :OR: DynAreaFlags_PMP)
; PMP PhysOp can specify these flags
DynAreaFlags_PMPPhysOpAccessMask * PageFlags_Unavailable
Neil Turton's avatar
Neil Turton committed
618

619 620 621 622 623 624 625 626 627
; Sticky page flags - they're preserved over most operations, including changes
; in ownership
StickyPageFlags * PageFlags_Reserved

; Ensure user can't specify any sticky flags for the main calls
; If this changes, lots of code will need checking/updating!
        ASSERT  (DynAreaFlags_AccessMask :AND: StickyPageFlags) = 0
        ASSERT  (DynAreaFlags_PMPLogOpAccessMask :AND: StickyPageFlags) = 0
        ASSERT  (DynAreaFlags_PMPPhysOpAccessMask :AND: StickyPageFlags) = 0
Neil Turton's avatar
Neil Turton committed
628

629
DynamicAreaSWI Entry
Neil Turton's avatar
Neil Turton committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643
        BL      DynAreaSub
        PullEnv
        ORRVS   lr, lr, #V_bit
        ExitSWIHandler

DynAreaSub
        CMP     r0, #DAReason_Limit
        ADDCC   pc, pc, r0, LSL #2
        B       DynArea_Unknown
        B       DynArea_Create
        B       DynArea_Remove
        B       DynArea_GetInfo
        B       DynArea_Enumerate
        B       DynArea_Renumber
644 645
 [ ShrinkableDAs
        B       DynArea_ReturnFree
646 647
 |
        B       DynArea_Unknown
Neil Turton's avatar
Neil Turton committed
648
 ]
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
 [ DynArea_QuickHandles
        B       DynArea_GetChangeInfo
        B       DynArea_EnumerateInfo
        B       DynArea_SetClamps
 |
        B       DynArea_Unknown
        B       DynArea_Unknown
        B       DynArea_Unknown
 ]
 [ DA_Batman
        B       DynArea_SparseClaim
        B       DynArea_SparseRelease
 |
        B       DynArea_Unknown
        B       DynArea_Unknown
 ]
665 666 667 668 669 670 671 672 673 674
        B       DynArea_Unknown ; 11
        B       DynArea_Unknown ; |
        B       DynArea_Unknown ; |
        B       DynArea_Unknown ; |
        B       DynArea_Unknown ; |--Reserved for ROL
        B       DynArea_Unknown ; |
        B       DynArea_Unknown ; |
        B       DynArea_Unknown ; |
        B       DynArea_Unknown ; 19
        B       DynArea_Locate
675 676 677 678 679
        B       DynArea_PMP_PhysOp
        B       DynArea_PMP_LogOp
        B       DynArea_PMP_Resize
        B       DynArea_PMP_GetInfo
        B       DynArea_PMP_GetPages
680
        B       DynArea_AplSpaceLimit
681 682 683 684 685 686 687 688 689 690
 [ ShrinkableDAs
        B       DynArea_ReturnFreePages
 |
        B       DynArea_Unknown
 ]
 [ DynArea_QuickHandles
        B       DynArea_EnumerateInfo2
 |
        B       DynArea_Unknown
 ]
691

692
;
Neil Turton's avatar
Neil Turton committed
693
; unknown OS_DynamicArea reason code
694
;
Neil Turton's avatar
Neil Turton committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
DynArea_Unknown
        ADRL    r0, ErrorBlock_HeapBadReason
DynArea_TranslateAndReturnError
      [ International
        Push    lr
        BL      TranslateError
        Pull    lr
      ]
DynArea_ReturnError
        SETV
        MOV     pc, lr

;**************************************************************************
;
;       DynArea_Create - Create a dynamic area
;
;       Internal routine called by DynamicAreaSWI and by reset code
;
; in:   r0 = reason code (0)
;       r1 = new area number, or -1 => RISC OS allocates number
;       r2 = initial size of area (in bytes)
;       r3 = base logical address of area, or -1 => RISC OS allocates address space
;       r4 = area flags
;               bits 0..3 = access privileges
;               bit  4 = 1 => not bufferable
;               bit  5 = 1 => not cacheable
;               bit  6 = 0 => area is singly mapped
;                      = 1 => area is doubly mapped
;               bit  7 = 1 => area is not user draggable in TaskManager window
724 725 726 727 728
;               bit  8 = 1 => area may require specific physical pages
;               bit  9 = 1 => area is shrinkable (only implemented if ShrinkableDAs)
;               bit 10 = 1 => area may be sparsely mapped (only implemented if DA_Batman)
;               bit 11 = 1 => area is bound to client application (allows areas to be overlayed in address space)
;                             not implemented yet, but declared in public API for Ursula
729 730
;               bits 12..14 => cache policy (if bits 4 or 5 set)
;               bit 15 = 1 => area requires DMA capable pages (is bit 12 in ROL's OS!)
Jeffrey Lee's avatar
Jeffrey Lee committed
731 732
;               bit 16 = 1 => "abortable" DA which is integrated with the AbortTrap system
;               bits 17-19 used by ROL
733 734
;               bit 20 = 1 => area is backed by physical memory pool
;               other bits reserved for future expansion + internal page flags (should be 0)
Neil Turton's avatar
Neil Turton committed
735
;
736
;       r5 = maximum size of logical area, or -1 for total RAM size
Neil Turton's avatar
Neil Turton committed
737 738 739
;       r6 -> area handler routine
;       r7 = workspace pointer for area handler (-1 => use base address)
;       r8 -> area description string (null terminated) (gets copied)
740
;       r9 = initial physical area size limit, in pages (physical memory pools), otherwise ignored
Neil Turton's avatar
Neil Turton committed
741 742 743 744 745 746 747 748
;
; out:  r1 = given or allocated area number
;       r3 = given or allocated base address of area
;       r5 = given or allocated maximum size
;       r0, r2, r4, r6-r9 preserved
;       r10-r12 may be corrupted
;

749
DynArea_Create Entry "r2,r6-r8"
Neil Turton's avatar
Neil Turton committed
750 751 752
        CMP     r1, #-1         ; do we have to allocate a new area number
        BEQ     %FT10

753 754 755 756 757 758 759 760 761 762 763
  [ DynArea_QuickHandles
        CMP     r1, #DynArea_NewAreas
        BLO     %FT06
        CMP     r1, #DynArea_NewAreas+DynArea_NumQHandles
        BLO     %FT08           ; can't choose your own quick handle
  ]

06
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber ; see if area number is unique
  |
Neil Turton's avatar
Neil Turton committed
764
        BL      CheckAreaNumber ; see if area number is unique
765
  ]
Neil Turton's avatar
Neil Turton committed
766 767
        BCC     %FT20           ; didn't find it, so OK

768
08
Neil Turton's avatar
Neil Turton committed
769 770 771 772 773 774 775 776 777 778 779
        ADR     r0, ErrorBlock_AreaAlreadyExists
DynArea_ErrorTranslateAndExit
        PullEnv
        B       DynArea_TranslateAndReturnError

        MakeErrorBlock  AreaAlreadyExists
        MakeErrorBlock  AreaNotOnPageBdy
        MakeErrorBlock  OverlappingAreas
        MakeErrorBlock  CantAllocateArea
        MakeErrorBlock  CantAllocateLevel2
        MakeErrorBlock  UnknownAreaHandler
780 781
        MakeErrorBlock  BadDynamicAreaOptions
        MakeErrorBlock  BadPageNumber
Neil Turton's avatar
Neil Turton committed
782 783 784

; we have to allocate an area number for him

785 786
  [ DynArea_QuickHandles
10
Jeffrey Lee's avatar
Jeffrey Lee committed
787
        LDR     r11, =ZeroPage
788 789 790 791 792 793 794 795 796 797 798 799 800
        LDR     r11, [r11, #DynArea_ws ]
        LDR     r1, DynArea_FreeQHandles          ;get index of next available quick handle, if any free
        CMP     r1, #0
        ADDNE   r1, r1, #DynArea_NewAreas-1       ;compute quick handle from index
        BNE     %FT20
        LDR     r1, DynArea_TreacleGuess          ;last non-quick number allocated
12
        ADD     r1, r1, #1                        ; increment for next guess (collisions should be *very* rare)
        CMP     r1, #DynArea_NewAreas+DynArea_NumQHandles
        MOVLO   r1, #DynArea_NewAreas+DynArea_NumQHandles
        BL      QCheckAreaNumber
        BCS     %BT12                             ; and try again
  |
Neil Turton's avatar
Neil Turton committed
801 802 803 804 805 806
10
        MOV     r1, #DynArea_NewAreas
12
        BL      CheckAreaNumber
        ADDCS   r1, r1, #1      ; that area number already exists, so increment
        BCS     %BT12           ; and try again
807 808
  ]

Neil Turton's avatar
Neil Turton committed
809 810
20

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
; Check PMP settings
; If PMP is requested:
; * Must require specific pages
; * Mustn't be sparse
; * Mustn't be doubly mapped
; * Mustn't be requesting DMA auto-alloc
; * Must be zero initial size
; * Must have handler code
; Some of these restrictions may be lifted in future (e.g. if not requesting specific pages, kernel could implement OS_ChangeDynamicArea?)
        TST     r4, #DynAreaFlags_PMP
        BEQ     %FT21
        LDR     r11, =DynAreaFlags_DoublyMapped+DynAreaFlags_NeedsSpecificPages+DynAreaFlags_SparseMap+DynAreaFlags_NeedsDMA
        AND     r11, r4, r11
        TEQ     r11, #DynAreaFlags_NeedsSpecificPages
        TEQEQ   r2, #0
        ADRNE   r0, ErrorBlock_BadDynamicAreaOptions
        BNE     DynArea_ErrorTranslateAndExit
        TEQ     r6, #0
        ADREQ   r0, ErrorBlock_BadDynamicAreaOptions
        BEQ     DynArea_ErrorTranslateAndExit
21

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
; Check cacheable doubly-mapped area restrictions
; * On ARMv5 and below we disallow cacheable doubly-mapped areas outright, because the virtually tagged caches simply can't deal with them (at least when it comes to writes)
; * ARMv6 can support cacheable doubly-mapped areas, but only if we comply with the page colouring restrictions, which is something we currently don't do. So disallow there as well.
; * ARMv7+ doesn't have page colouring restrictions, but due to the potential of virtually tagged instruction caches (which would complicate OS_SynchroniseCodeAreas), for simplicity we only allow the area to be cacheable if it's non-executable
        AND     r11, r4, #DynAreaFlags_DoublyMapped+DynAreaFlags_NotCacheable
        TEQ     r11, #DynAreaFlags_DoublyMapped
        BNE     %FT22
        ; Cheesy architecture check: check the identified cache/ARMop type
        LDR     r11, =ZeroPage
        LDRB    lr, [r11, #Cache_Type]
        TEQ     lr, #CT_ctype_WB_CR7_Lx
        ADRNE   r0, ErrorBlock_BadDynamicAreaOptions
        BNE     DynArea_ErrorTranslateAndExit
        ; Check the supplied access policy is XN
        LDR     r11, [r11, #MMU_PPLAccess]
        AND     lr, r4, #DynAreaFlags_APBits
        LDR     r11, [r11, lr, LSL #2]
        TST     r11, #CMA_Partially_UserXN+CMA_Partially_PrivXN ; n.b. XN flag sense is inverted in PPLAccess, so NE means executable
        ADRNE   r0, ErrorBlock_BadDynamicAreaOptions
        BNE     DynArea_ErrorTranslateAndExit
22

Jeffrey Lee's avatar
Jeffrey Lee committed
855 856 857 858 859 860 861 862
; Abortable DAs must have a handler
        TST     r4, #DynAreaFlags_Abortable
        BEQ     %FT23
        CMP     r6, #0
        ADREQ   r0, ErrorBlock_BadDynamicAreaOptions
        BEQ     DynArea_ErrorTranslateAndExit
23        

Neil Turton's avatar
Neil Turton committed
863 864
; now validate maximum size of area

865 866 867 868
  [ DynArea_QuickHandles
    ;
    ; apply clamps on max size, as set by last call to OS_DynamicArea 8
    ;
Jeffrey Lee's avatar
Jeffrey Lee committed
869
        LDR     r11, =ZeroPage
870 871 872 873 874 875 876 877 878 879
        LDR     r11, [r11, #DynArea_ws]
    [ DA_Batman
        TST     r4, #DynAreaFlags_SparseMap
        BEQ     DAC_notsparse
        LDR     r10, DynArea_OD8Clamp3   ; clamp for sparse dynamic area
        CMP     r5, r10                  ; if requested max size is > relevant clamp
        MOVHI   r5, r10                  ; then clamp it!
        LDR     r10, [sp]                ; requested initial size, from stack
        CMP     r5, r10
        MOVLO   r5, r10                  ; we must try to honour initial size (allowed to break clamp)
Jeffrey Lee's avatar
Jeffrey Lee committed
880
        LDR     r10, =ZeroPage
881 882 883 884 885 886 887 888 889 890 891 892 893 894
        LDR     r11, [r10, #Page_Size]
        B       DAC_roundup
DAC_notsparse
    ]
        CMP     r5, #-1
        LDREQ   r10, DynArea_OD8Clamp1   ; clamp for max size requested of -1
        LDRNE   r10, DynArea_OD8Clamp2   ; clamp for max size requested of some specific value
        CMP     r5, r10                  ; if requested max size is > relevant clamp
        MOVHI   r5, r10                  ; then clamp it!
        LDR     r10, [sp]                ; requested initial size, from stack
        CMP     r5, r10
        MOVLO   r5, r10                  ; we must try to honour initial size (allowed to break clamp)
  ]

Jeffrey Lee's avatar
Jeffrey Lee committed
895
        LDR     r10, =ZeroPage
Neil Turton's avatar
Neil Turton committed
896 897
        LDR     r11, [r10, #Page_Size]
        LDR     r10, [r10, #RAMLIMIT]   ; get total RAM size
898 899
        CMP     r10, r5, LSR #Log2PageSize ; if requested maximum size is > total
        MOVLS   r5, r10, LSL #Log2PageSize ; then set max to total. Note no special handling of R5=-1 is needed (R5=-1 will get treated as 4GB-1. If RAMLIMIT < 4GB then R5 will be clamped correctly, if RAMLIMIT >= 4GB then the request will fail regardless because we only have limited logical address space to work with)
Neil Turton's avatar
Neil Turton committed
900

901
DAC_roundup
Neil Turton's avatar
Neil Turton committed
902 903 904 905 906
        SUB     r10, r11, #1            ; also round up to a page multiple
        ADD     r5, r5, r10
        BIC     r5, r5, r10

; now see if we have to allocate a logical address space
907 908 909
        TEQ     r5, #0                  ; If no logical size (i.e. purely physical PMP)
        MOVEQ   r3, #0                  ; Then set base addr to 0
        BEQ     %FT41                   ; And skip straight to claiming the DANode
Neil Turton's avatar
Neil Turton committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

        CMP     r3, #-1                 ; if we are to allocate the address space
        BEQ     %FT30                   ; then go do it

; otherwise we must check that the address does not clash with anything else

        TST     r3, r10                         ; does it start on a page boundary
        ADRNE   r0, ErrorBlock_AreaNotOnPageBdy ; if not then error
        BNE     DynArea_ErrorTranslateAndExit

        BL      CheckForOverlappingAreas        ; in: r3 = address, r4 = flags, r5 = size; out: if error, r0->error, V=1
        BVC     %FT40
25
        PullEnv
        B       DynArea_ReturnError

30
        BL      AllocateAreaAddress             ; in: r4 = flags, r5 = size of area needed; out: r3, or V=1, r0->error
        BVS     %BT25
40
930
        PTOp    AllocateBackingLevel2           ; in: r3 = address, r4 = flags, r5 = size; out: VS if error
Neil Turton's avatar
Neil Turton committed
931 932
        BVS     %BT25

933
41
Neil Turton's avatar
Neil Turton committed
934
        Push    "r0,r1,r3"
935 936 937 938 939 940
  [ DynArea_QuickHandles
    ;we save work and reduce stress on system heap by claiming only one block, consisting of node followed by
    ;string space (always maximum length, but typically not overly wasteful compared to 2nd block overhead)
    ;
        MOV     r3, #DANode_NodeSize + DynArea_MaxNameLength + 1
  |
Neil Turton's avatar
Neil Turton committed
941
        MOV     r3, #DANode_NodeSize
942
  ]
Neil Turton's avatar
Neil Turton committed
943 944 945 946 947 948 949 950 951 952 953 954
        BL      ClaimSysHeapNode                ; out: r2 -> node
        STRVS   r0, [sp]
        Pull    "r0,r1,r3"
        BVS     %BT25                           ; failed to claim node

; now store data in node (could probably use STM if we shuffled things around)

        CMP     r7, #-1                         ; if workspace ptr = -1
        MOVEQ   r7, r3                          ; then use base address

        STR     r1, [r2, #DANode_Number]
        STR     r3, [r2, #DANode_Base]
955 956
  [ DA_Batman
        ;disallow some awkward flag options if SparseMap set (no error), and temporarily create as not sparse
Jeffrey Lee's avatar
Jeffrey Lee committed
957
        ;also disallow a DA handler (unless it's an Abortable DA)
958 959
        TST     r4, #DynAreaFlags_SparseMap
        STREQ   r4, [r2, #DANode_Flags]
Jeffrey Lee's avatar
Jeffrey Lee committed
960 961 962 963 964
        BICNE   lr, r4, #DynAreaFlags_DoublyMapped + DynAreaFlags_NeedsSpecificPages + DynAreaFlags_Shrinkable + DynAreaFlags_SparseMap
        ORRNE   lr, lr, #DynAreaFlags_NotUserDraggable
        STRNE   lr, [r2, #DANode_Flags]
        ANDNE   lr, r4, #DynAreaFlags_Abortable
        TEQNE   lr, #DynAreaFlags_Abortable
965 966 967
        MOVNE   r6, #0
        MOVNE   r7, #0
  |
Neil Turton's avatar
Neil Turton committed
968
        STR     r4, [r2, #DANode_Flags]
969
  ]
Neil Turton's avatar
Neil Turton committed
970 971 972 973 974
        STR     r5, [r2, #DANode_MaxSize]
        STR     r6, [r2, #DANode_Handler]
        STR     r7, [r2, #DANode_Workspace]
        MOV     r7, #0                          ; initial size is zero
        STR     r7, [r2, #DANode_Size]          ; before we grow it
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
        TST     r4, #DynAreaFlags_PMP
        STR     r7, [r2, #DANode_PMP]
        STREQ   r7, [r2, #DANode_PMPMaxSize]
        STR     r7, [r2, #DANode_PMPSize]
        BEQ     %FT44
        STR     r3, [r2, #DANode_SparseHWM]
        TEQ     r9, #0
        STR     r9, [r2, #DANode_PMPMaxSize]
        BEQ     %FT44
        ; Allocate and initialise PMP for this DA
        Push    "r0-r3"
        LDR     r0, =ZeroPage
        LDR     r0, [r0, #MaxCamEntry]
        CMP     r9, r0
        ADDHI   r9, r0, #1
        MOV     r3, r9, LSL #2
        BL      ClaimSysHeapNode
        BVS     %FT43
        MOV     r10, r2
        MOV     r0, #-1
42
        SUBS    r3, r3, #4
        STR     r0, [r2], #4
        BNE     %BT42
        Pull    "r0-r3"
        STR     r10, [r2, #DANode_PMP]
        B       %FT44
43
        STR     r0, [sp]
        LDR     r2, [sp, #8]
        BL      FreeSysHeapNode
        Pull    "r0-r3"
        SETV
        B       %BT25
44
Neil Turton's avatar
Neil Turton committed
1010

1011
        ; update lower limit on IO space growth, if this DA exceeds previous limit
Jeffrey Lee's avatar
Jeffrey Lee committed
1012 1013 1014
      [ ZeroPage <> 0
        LDR     r7, =ZeroPage
      ]
1015 1016 1017 1018 1019
        LDR     r6, [r7, #IOAllocLimit]
        ADD     lr, r3, r5
        CMP     lr, r6
        STRHI   lr, [r7, #IOAllocLimit]

Neil Turton's avatar
Neil Turton committed
1020 1021
; now make copy of string - first find out length of string

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  [ DynArea_QuickHandles

        ADD     r7, r2, #DANode_NodeSize
        STR     r7, [r2, #DANode_Title]
        Push    "r0"
        MOV     r0, #DynArea_MaxNameLength
        TEQ     r8, #0
    [ DynArea_NullNamePtrMeansHexString
        ASSERT  DynArea_MaxNameLength > 8
        BNE     %FT45
        Push    "r1, r2"
        MOV     r0, r1                          ;string is 8-digit hex of DA number
        MOV     r1, r7
        MOV     r2, #DynArea_MaxNameLength+1
        SWI     XOS_ConvertHex8
        Pull    "r1, r2"
        B       %FT55
    |
        BEQ     %FT50                           ;assume NULL ptr to mean no DA name
    ]
45
        LDRB    r6, [r8], #1
        STRB    r6, [r7], #1
        SUB     r0, r0, #1
        TEQ     r6, #0
        TEQNE   r0, #0
        BNE     %BT45
50
        MOV     r0, #0
        STRB    r0, [r7], #1
55
        Pull    "r0"

  |

Neil Turton's avatar
Neil Turton committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
        MOV     r7, r8
45
        LDRB    r6, [r7], #1
        TEQ     r6, #0
        BNE     %BT45

        Push    "r0-r3"
        SUB     r3, r7, r8                      ; r3 = length inc. term.
        BL      ClaimSysHeapNode
        STRVS   r0, [sp]
        MOV     r7, r2
        Pull    "r0-r3"
        BVS     StringNodeClaimFailed

        STR     r7, [r2, #DANode_Title]
50
        LDRB    r6, [r8], #1                    ; copy string into claimed block
        STRB    r6, [r7], #1
        TEQ     r6, #0
        BNE     %BT50

1078 1079
 ] ;DynArea_QuickHandles

Neil Turton's avatar
Neil Turton committed
1080 1081
; now put node on list - list is sorted in ascending base address order

Jeffrey Lee's avatar
Jeffrey Lee committed
1082
        LDR     r8, =ZeroPage+DAList
Neil Turton's avatar
Neil Turton committed
1083 1084 1085
        LDR     r6, [r2, #DANode_Base]
60
        MOV     r7, r8
1086
        ASSERT  DANode_Link = 0                 ; For picking up pointer to first real node
Neil Turton's avatar
Neil Turton committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
        LDR     r8, [r7, #DANode_Link]          ; get next node
        TEQ     r8, #0                          ; if no more
        BEQ     %FT70                           ; then put it on here
        LDR     lr, [r8, #DANode_Base]
        CMP     lr, r6                          ; if this one is before ours
        BCC     %BT60                           ; then loop

70
        STR     r8, [r2, #DANode_Link]
        STR     r2, [r7, #DANode_Link]

1098
  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
1099
        LDR     r11, =ZeroPage
1100
        LDR     r11, [r11, #DynArea_ws]
1101
        BL      AddDAToAddrLookupTable
1102 1103 1104 1105 1106 1107 1108 1109 1110
        ;so XOS_ChangeDynamicArea can pick up the node we are still creating
        STR     r1, DynArea_CreatingHandle
        STR     r2, DynArea_CreatingPtr
        ;so initial grow won't leave resize signature
        LDR     lr, DynArea_OD6Signature
        ORR     lr, lr, #&80000000
        STR     lr, DynArea_OD6Signature
  ]

Neil Turton's avatar
Neil Turton committed
1111 1112
; now we need to grow the area to its requested size

1113
        Push    "r0, r1, r2"
Neil Turton's avatar
Neil Turton committed
1114
        LDR     r0, [r2, #DANode_Number]
1115
        LDR     r1, [sp, #3*4]                  ; reload requested size off stack
1116 1117
        CMP     r1, #0                          ; skip redundant SWI
        SWINE   XOS_ChangeDynamicArea           ; deal with error - r0,r1,r2 still stacked
Neil Turton's avatar
Neil Turton committed
1118
        BVS     %FT90
1119 1120 1121 1122 1123 1124 1125
        Pull    "r0, r1, r2"

  [ DynArea_QuickHandles
;
; Now put node on alphabetically sorted list
;
        Push    "r3,r4,r5,r7,r8,r9"
Jeffrey Lee's avatar
Jeffrey Lee committed
1126
        LDR     r11, =ZeroPage
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
        LDR     r11, [r11, #DynArea_ws]
        ADR     r8, DynArea_SortedList - DANode_SortLink ;so that [r8, #DANode_SortLink] addresses list header)
75
        MOV     r7, r8                       ; previous
        LDR     r8, [r7, #DANode_SortLink]
        TEQ     r8, #0
        BEQ     %FT78
        ;ho hum, UK case insensitive string compare
        LDR     r3, [r2, #DANode_Title]
        LDR     r9, [r8, #DANode_Title]
76
        LDRB    r4, [r3],#1
        uk_LowerCase r4,r11
        LDRB    r5, [r9],#1
        uk_LowerCase r5,r11
        CMP     r4, r5
        BNE     %FT77
        CMP     r4, #0
        BNE     %BT76
77
        BHI     %BT75
78
        STR     r2, [r7, #DANode_SortLink]
        STR     r8, [r2, #DANode_SortLink]
79
        Pull    "r3,r4,r5,r7,r8,r9"
  ] ;DynArea_QuickHandles

  [ DA_Batman
        TST     r4, #DynAreaFlags_SparseMap
        LDRNE   r11, [r2, #DANode_Flags]
        ORRNE   r11, r11, #DynAreaFlags_SparseMap ; set this in node now (after initial grow)
        STRNE   r11, [r2, #DANode_Flags]
        LDRNE   r11, [r2, #DANode_Size]
        LDRNE   lr,  [r2, #DANode_Base]
        ADDNE   r11, r11, lr
        STRNE   r11, [r2, #DANode_SparseHWM]      ; initial high water mark
  ]

  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
1167
        LDR     r11, =ZeroPage
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
        LDR     r11, [r11, #DynArea_ws]

        TST     r4, #DynAreaFlags_Shrinkable
        LDRNE   lr, DynArea_ShrinkableSubList
        STRNE   lr, [r2, #DANode_SubLink]
        STRNE   r2, DynArea_ShrinkableSubList   ;link onto front of Shrinkable sublist if Shrinkable

        MOV     lr, #-1
        STR     lr, DynArea_CreatingHandle      ;invalidate this now
        CMP     r1, #ChangeDyn_MaxArea
        BHI     %FT72
        ADR     lr, DynArea_SysQHandleArray
        STR     r2, [lr, r1, LSL #2]            ;system handle - store ptr to node for quick reference
        B       %FT80
72
        LDR     lr, DynArea_OD6Signature
        STR     lr, DynArea_OD6PrevSignature
        ORR     lr, lr, #1
        STR     lr, DynArea_OD6Signature
        STR     r1, DynArea_OD6Handle
        CMP     r1, #DynArea_NewAreas
        BLO     %FT80
        CMP     r1, #DynArea_NewAreas+DynArea_NumQHandles
        BHS     %FT74
        SUB     r10, r1, #DynArea_NewAreas
        ADR     lr, DynArea_QHandleArray
        LDR     r6, [lr, r10, LSL #2]           ;pick up index of next free quick handle
        STR     r6, DynArea_FreeQHandles        ;store as index of first free quick handle
        STR     r2, [lr, r10, LSL #2]           ;store ptr to node for quick reference
        B       %FT80
74
        LDR     r10, DynArea_TreacleGuess
        ADD     r10, r10, #1
        STR     r10, DynArea_TreacleGuess       ;non-quick handle allocated, increment for next allocate
80
  ] ;DynArea_QuickHandles

Jeffrey Lee's avatar
Jeffrey Lee committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
; Register the AbortTrap handler for any abortable DA
        LDR     r10, [r2, #DANode_Flags]
        TST     r10, #DynAreaFlags_Abortable
        BEQ     %FT85
        Push    "r0-r4"
        MOV     r4, r2
        MOV     r0, #OSAbortTrap_Register
        LDR     r1, [r2, #DANode_Base]
        LDR     r2, [r2, #DANode_MaxSize]
        TST     r10, #DynAreaFlags_DoublyMapped
        SUBNE   r1, r1, r2
        ADDNE   r2, r1, r2, LSL #1
        ADDEQ   r2, r1, r2
        ADRL    r3, DynArea_AbortTrapHandler
        SWI     XOS_AbortTrap
        Pull    "r0-r4",VC
        BVC     %FT85
        ; Yuck, something went wrong. Kill the area.
        BIC     r10, r10, #DynAreaFlags_Abortable
        STR     r10, [r2, #DANode_Flags]
        STR     r0, [sp]
        MOV     r0, #DAReason_Remove
        LDR     r1, [sp, #4]
        SWI     XOS_DynamicArea
        Pull    "r0-r4"
        PullEnv
        B       DynArea_ReturnError
85
Neil Turton's avatar
Neil Turton committed
1233 1234 1235

; Now issue service to tell TaskManager about it

1236 1237
        Push    "r0, r1, r2"
        MOV     r2, r1                          ; r2 = area number
Neil Turton's avatar
Neil Turton committed
1238 1239
        MOV     r1, #Service_DynamicAreaCreate
        BL      Issue_Service
1240
        Pull    "r0, r1, r2"
Neil Turton's avatar
Neil Turton committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

        CLRV
        EXIT

90

; The dynamic area is not being created, because we failed to grow the area to the required size.
; The area itself will have no memory allocated to it (since if grow fails it doesn't move any).
; We must delink the node from our list, free the string node, and then the area node itself.

        STR     r0, [sp, #0*4]                  ; remember error pointer in stacked r0
        STR     r8, [r7, #DANode_Link]          ; delink area
1253 1254
  [ :LNOT: DynArea_QuickHandles
        LDR     r2, [r2, #DANode_Title]
Neil Turton's avatar
Neil Turton committed
1255
        BL      FreeSysHeapNode                 ; free title string node
1256 1257
  ]
        Pull    "r0, r1, r2"                    ; pull stacked registers, and drop thru to...
Neil Turton's avatar
Neil Turton committed
1258

1259
  [ :LNOT: DynArea_QuickHandles
Neil Turton's avatar
Neil Turton committed
1260 1261 1262 1263 1264 1265 1266
; The dynamic area is not being created, because there is no room to allocate space for the title string
; We must free the DANode we have allocated
; It would be nice to also free the backing L2, but we'll leave that for now.

; in: r2 -> DANode

StringNodeClaimFailed
1267 1268
  ]

Neil Turton's avatar
Neil Turton committed
1269 1270 1271
        Push    "r0, r1"
        BL      FreeSysHeapNode
        Pull    "r0, r1"
1272
  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
1273
        LDR     r11, =ZeroPage
1274 1275 1276 1277
        LDR     r11, [r11, #DynArea_ws]
        MOV     lr, #-1
        STR     lr, DynArea_CreatingHandle
  ]
Neil Turton's avatar
Neil Turton committed
1278 1279 1280
        PullEnv
        B       DynArea_ReturnError

Jeffrey Lee's avatar
Jeffrey Lee committed
1281 1282
        LTORG

1283 1284 1285 1286
; Add a dynamic area to the quick address lookup table
; In:
;  R2 = DANode ptr
;  R11 = DynArea_ws
1287
AddDAToAddrLookupTable ROUT
1288
        Entry   "r0-r1,r3,r6"
1289
        LDR     r3, [r2, #DANode_MaxSize]
1290
        ADRL    r0, DynArea_AddrLookup
1291
        TEQ     r3, #0
1292
        LDR     r1, [r2, #DANode_Flags]
1293
        BEQ     %FT90
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
        LDR     r6, [r2, #DANode_Base]
        TST     r1, #DynAreaFlags_DoublyMapped
        SUBNE   r6, r6, r3                      ; Get true start address
        MOVNE   r3, r3, LSL #1
        AND     r1, r6, #DynArea_AddrLookupMask ; Round down start address
        ADD     lr, r6, r3
        AND     r3, lr, #DynArea_AddrLookupMask
        TEQ     lr, r3
        ADDNE   r3, r3, #DynArea_AddrLookupSize ; Round up end address
        SUB     r3, r3, r1
        ADD     r0, r0, r1, LSR #30-DynArea_AddrLookupBits
71
        LDR     lr, [r0], #4
        TEQ     lr, #0
        STREQ   r2, [r0, #-4]
        BEQ     %FT72
        LDR     lr, [lr, #DANode_Base]
        CMP     lr, r6
        STRHI   r2, [r0, #-4]                   ; Update LUT if current entry starts after us
72
        SUBS    r3, r3, #DynArea_AddrLookupSize
        BNE     %BT71
1316
90
1317 1318
        EXIT

Neil Turton's avatar
Neil Turton committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
;**************************************************************************
;
;       DynArea_Remove - Remove a dynamic area
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (1)
;       r1 = area number
;
; out:  r10-r12 may be corrupted
;       All other registers preserved
;

1332
DynArea_Remove Entry
1333 1334

        ;*MUST NOT USE QCheckAreaNumber* (need r11 = previous)
Neil Turton's avatar
Neil Turton committed
1335 1336 1337
        BL      CheckAreaNumber         ; check that area is there
        BCC     UnknownDyn              ; [not found]

Jeffrey Lee's avatar
Jeffrey Lee committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
        ; Deregister any AbortTrap handler first, to (hopefully) stop things
        ; messing with the area while we're trying to remove it
        LDR     r12, [r10, #DANode_Flags]
        TST     r12, #DynAreaFlags_Abortable
        BEQ     DAR_notabortable
        Push    "r0-r4"
        MOV     r4, r10
        MOV     r0, #OSAbortTrap_Deregister
        LDR     r1, [r10, #DANode_Base]
        LDR     r2, [r10, #DANode_MaxSize]
        TST     r12, #DynAreaFlags_DoublyMapped
        SUBNE   r1, r1, r2
        ADDNE   r2, r1, r2, LSL #1
        ADDEQ   r2, r1, r2
        ADRL    r3, DynArea_AbortTrapHandler
        SWI     XOS_AbortTrap
        STRVS   r0, [sp]
        Pull    "r0-r4"
        EXIT    VS
        BIC     r12, r12, #DynAreaFlags_Abortable
        STR     r12, [r10, #DANode_Flags]
        
DAR_notabortable
1361
  [ DA_Batman
1362 1363
        LDR     lr,[r10,#DANode_Flags]
        TST     lr,#DynAreaFlags_SparseMap
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
        BEQ     DAR_notsparse
        Push    "r0,r2-r3"
        MOV     r0,#DAReason_SparseRelease
        LDR     r2,[r10,#DANode_Base]
        LDR     r3,[r10,#DANode_MaxSize]
        SWI     XOS_DynamicArea            ;release all pages in sparse area
        STRVS   r0,[SP]
        Pull    "r0,r2-r3"
        EXIT    VS
        B       DAR_delink
DAR_notsparse
1375 1376 1377 1378 1379 1380
        TST     lr, #DynAreaFlags_PMP
        BEQ     DAR_notPMP
        ; Unmap all pages from logical space
        ; This is a bit of a simplistic approach - request for everything to
        ; be unmapped and leave it to PMP_LogOp to detect which pages are and
        ; aren't there
1381
        Push    "r2-r8"
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
        LDR     r6, [r10, #DANode_Base]
        MOV     r3, #0
        LDR     r4, [r10, #DANode_SparseHWM] ; Assume HWM valid
        MOV     r5, #-1
        SUB     r4, r4, r6
        MOV     r6, #0
        MOV     r4, r4, LSR #12
        MOV     r8, sp
DAR_PMP_logloop
        SUBS    r4, r4, #1
        BLT     DAR_PMP_logunmap
        STMDB   sp!, {r4-r6}
        ADD     r3, r3, #1
        CMP     r3, #85 ; Limit to 1K stack
        BLT     DAR_PMP_logloop
DAR_PMP_logunmap
        MOV     r0, #DAReason_PMP_LogOp
        MOV     r2, sp
        SWI     XOS_DynamicArea
        MOV     sp, r8
1402
        Pull    "r2-r8", VS
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
        EXIT    VS
        CMP     r4, #0
        MOV     r3, #0
        BGT     DAR_PMP_logloop
        ; Pages are all unmapped, now release them from the PMP
        LDR     r4, [r10, #DANode_PMPMaxSize]
        LDR     r7, [r10, #DANode_PMP]
        MOV     r3, #0
DAR_PMP_physloop
        SUBS    r4, r4, #1
        BLT     DAR_PMP_physunmap
        LDR     lr, [r7, r4, LSL #2]
        CMP     lr, #-1                 ; Save some effort and only release pages which exist
        STMNEDB sp!, {r4-r6}
        ADDNE   r3, r3, #1
        CMP     r3, #85 ; Limit to 1K stack
        BLT     DAR_PMP_physloop
DAR_PMP_physunmap
        MOV     r0, #DAReason_PMP_PhysOp
        MOV     r2, sp
        SWI     XOS_DynamicArea
        MOV     sp, r8
1425
        Pull    "r2-r8", VS
1426 1427 1428 1429 1430
        EXIT    VS
        CMP     r4, #0
        MOV     r3, #0
        BGT     DAR_PMP_physloop
        ; All pages released, safe to free area
1431
        Pull    "r2-r8"
1432 1433 1434
        MOV     r0, #DAReason_Remove
        B       DAR_delink
DAR_notPMP
1435 1436 1437 1438
  ]

  [ DynArea_QuickHandles
        Push    "r11"
Jeffrey Lee's avatar
Jeffrey Lee committed
1439
        LDR     r11, =ZeroPage
1440 1441 1442 1443 1444 1445 1446 1447
        LDR     r11, [r11, #DynArea_ws]
        ;so final shrink won't leave resize signature
        LDR     lr, DynArea_OD6Signature
        ORR     lr, lr, #&80000000
        STR     lr, DynArea_OD6Signature
        Pull    "r11"
  ]

Neil Turton's avatar
Neil Turton committed
1448 1449 1450 1451 1452
; First try to shrink area to zero size

        Push    "r0-r2"
        MOV     r0, r1                  ; area number
        LDR     r2, [r10, #DANode_Size] ; get current size
1453 1454
        RSBS    r1, r2, #0              ; negate it
        SWINE   XOS_ChangeDynamicArea
Neil Turton's avatar
Neil Turton committed
1455 1456 1457
        BVS     %FT80
        Pull    "r0-r2"

1458
DAR_delink
Neil Turton's avatar
Neil Turton committed
1459

1460 1461 1462 1463
  [ DynArea_QuickHandles
;
; delink from sorted list
;
1464
        Push    "r0-r4,r7,r8,r11"
Jeffrey Lee's avatar
Jeffrey Lee committed
1465
        LDR     r11, =ZeroPage
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
        LDR     r11, [r11, #DynArea_ws]
        ADR     r8, DynArea_SortedList - DANode_SortLink ;so that [r8, #DANode_SortLink] addresses list header)
DAR_sdloop
        MOV     r7, r8                      ;previous
        LDR     r8, [r7, #DANode_SortLink]
        TEQ     r8, #0                      ;just in case not on list, shouldn't happen
        BEQ     DAR_sddone
        TEQ     r8, r10
        BNE     DAR_sdloop
        LDR     r8, [r8, #DANode_SortLink]
        STR     r8, [r7, #DANode_SortLink]
DAR_sddone
1478
; Delink from address lookup table
1479
        LDR     r3, [r10, #DANode_MaxSize]
1480
        ADRL    r0, DynArea_AddrLookup
1481
        TEQ     r3, #0
1482
        LDR     r1, [r10, #DANode_Flags]
1483
        BEQ     DAR_addone
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
        LDR     r2, [r10, #DANode_Base]
        TST     r1, #DynAreaFlags_DoublyMapped
        SUBNE   r2, r2, r3                      ; Get true start address
        MOVNE   r3, r3, LSL #1
        AND     r1, r2, #DynArea_AddrLookupMask ; Round down start address
        ADD     lr, r2, r3
        AND     r3, lr, #DynArea_AddrLookupMask
        TEQ     lr, r3
        ADDNE   r3, r3, #DynArea_AddrLookupSize ; Round up end address
        SUB     r3, r3, r1
        ADD     r0, r0, r1, LSR #30-DynArea_AddrLookupBits
DAR_adloop
        LDR     lr, [r0], #4
        TEQ     lr, r10
        BNE     DAR_adnext
        ; Update to point to next DA, or null if next is outside this chunk
        LDR     lr, [lr, #DANode_Link]
        TEQ     lr, #0
        STREQ   lr, [r0, #-4]
        BEQ     DAR_adnext
        LDR     r4, [lr, #DANode_Flags]
        LDR     r2, [lr, #DANode_Base]
        TST     r4, #DynAreaFlags_DoublyMapped
        LDRNE   r4, [lr, #DANode_MaxSize]
        SUBNE   r2, r2, r4
        AND     r2, r2, #DynArea_AddrLookupMask
        TEQ     r2, r1
        MOVNE   lr, #0
        STR     lr, [r0, #-4]
DAR_adnext
        SUBS    r3, r3, #DynArea_AddrLookupSize
        ADD     r1, r1, #DynArea_AddrLookupSize
        BNE     DAR_adloop
1517
DAR_addone
1518
        Pull    "r0-r4,r7,r8,r11"
1519 1520

  ] ;DynArea_QuickHandles
Neil Turton's avatar
Neil Turton committed
1521 1522 1523 1524 1525 1526

; Now just de-link from list (r10 -> node, r11 -> prev)

        LDR     lr, [r10, #DANode_Link] ; store our link
        STR     lr, [r11, #DANode_Link] ; in prev link

1527 1528 1529 1530 1531 1532
  [ DynArea_QuickHandles
        ;if it is a Shrinkable area, find on Shrinkable sublist and remove it
        Push    "r0-r2, r11"
        LDR     r0, [r10, #DANode_Flags]
        TST     r0, #DynAreaFlags_Shrinkable
        BEQ     %FT06
Jeffrey Lee's avatar
Jeffrey Lee committed
1533
        LDR     r11, =ZeroPage
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
        LDR     r11, [r11, #DynArea_ws]
        ADR     r1, DynArea_ShrinkableSubList
        LDR     r2, [r1]
04
        CMP     r2, r10
        LDREQ   r2, [r2, #DANode_SubLink]
        STREQ   r2, [r1]
        BEQ     %FT06
        ADD     r1, r2, #DANode_SubLink
        LDR     r2, [r2, #DANode_SubLink]
        B       %BT04
06
        Pull    "r0-r2, r11"
  ]

Neil Turton's avatar
Neil Turton committed
1549
        Push    "r0-r2"
1550
  [ :LNOT: DynArea_QuickHandles
Neil Turton's avatar
Neil Turton committed
1551 1552
        LDR     r2, [r10, #DANode_Title]        ; free title string block
        BL      FreeSysHeapNode
1553
  ]
1554 1555 1556
        LDR     r2, [r10, #DANode_PMP]
        CMP     r2, #0
        BLNE    FreeSysHeapNode
Neil Turton's avatar
Neil Turton committed
1557 1558 1559
        MOV     r2, r10                         ; and free node block
        BL      FreeSysHeapNode
        Pull    "r0-r2"
1560 1561 1562

  [ DynArea_QuickHandles
        Push    "r2, r3"
Jeffrey Lee's avatar
Jeffrey Lee committed
1563
        LDR     r11, =ZeroPage
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
        LDR     r11, [r11, #DynArea_ws]
        MOV     r2,#-1
        STR     r2, DynArea_CreatingHandle                ; invalidate these, just in case
        STR     r2, DynArea_LastTreacleHandle
        STR     r2, DynArea_LastEnumHandle
        CMP     r1, #ChangeDyn_MaxArea
        BLS     %FT08                                     ; system area being removed
        LDR     r2, DynArea_OD6Signature
        STR     r2, DynArea_OD6PrevSignature
        ORR     r2, r2, #2
        STR     r2, DynArea_OD6Signature
        STR     r1, DynArea_OD6Handle
        CMP     r1, #DynArea_NewAreas
        BLO     %FT10
        CMP     r1, #DynArea_NewAreas+DynArea_NumQHandles
        BHS     %FT10
        SUB     r2, r1, #DynArea_NewAreas-1               ; index of quick handle
        ADR     r10, DynArea_FreeQHandles                 ; so we can index array from 1
        LDR     r3, DynArea_FreeQHandles
        STR     r3, [r10, r2, LSL #2]
        STR     r2, DynArea_FreeQHandles                  ; put on front of free list
        B       %FT10
08
        ADR     r10, DynArea_SysQHandleArray
        MOV     r2,  #0
        STR     r2,  [r10, r1, LSL #2]                    ; reset system Qhandle
10
        Pull    "r2, r3"

  ]

; Issue service to tell TaskManager

        Push    "r0, r1, r2"
        MOV     r2, r1
        MOV     r1, #Service_DynamicAreaRemove
        BL      Issue_Service
        Pull    "r0, r1, r2"

Neil Turton's avatar
Neil Turton committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
        CLRV
        EXIT

; come here if shrink failed - r0-r2 stacked

80
        STR     r0, [sp]                ; overwrite stacked r0 with error pointer
        LDR     r0, [sp, #1*4]          ; reload area number
        LDR     r1, [r10, #DANode_Size] ; get size after failed shrink
        SUB     r1, r2, r1              ; change needed to restore original size
        SWI     XOS_ChangeDynamicArea   ; ignore any error from this
1614
        Pull    "r0-r2"
Neil Turton's avatar
Neil Turton committed
1615 1616 1617 1618 1619 1620 1621
        SETV
        EXIT

UnknownDyn
        ADRL    r0, ErrorBlock_BadDynamicArea
 [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
1622
 |
Neil Turton's avatar
Neil Turton committed
1623
        SETV
Robert Sprowson's avatar
Robert Sprowson committed
1624
 ]
Neil Turton's avatar
Neil Turton committed
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
        EXIT

;**************************************************************************
;
;       DynArea_GetInfo - Get info on a dynamic area
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (2)
;       r1 = area number
;
; out:  r2 = current size of area
;       r3 = base logical address
;       r4 = area flags
;       r5 = maximum size of area
;       r6 -> area handler routine
;       r7 = workspace pointer
;       r8 -> title string
;       r10-r12 may be corrupted
;       All other registers preserved
;

DynArea_GetInfo ALTENTRY
1648 1649 1650
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber        ; check area exists
  |
Neil Turton's avatar
Neil Turton committed
1651
        BL      CheckAreaNumber         ; check area exists
1652
  ]
Neil Turton's avatar
Neil Turton committed
1653 1654 1655 1656 1657
        BCC     UnknownDyn              ; [it doesn't]

; r10 -> node, so get info

        LDR     r4, [r10, #DANode_Flags]
1658
        LDR     r3, [r10, #DANode_Base]
Neil Turton's avatar
Neil Turton committed
1659
        LDR     r6, [r10, #DANode_Handler]
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
        TST     r4, #DynAreaFlags_PMP
        LDREQ   r2, [r10, #DANode_Size]
        LDREQ   r5, [r10, #DANode_MaxSize]
        BEQ     %FT10
        LDR     r2, [r10, #DANode_PMPSize]
        LDR     r5, [r10, #DANode_PMPMaxSize]
        CMP     r2, #DynArea_PMP_BigPageCount
        MOVLO   r2, r2, LSL #12
        LDRHS   r2, =DynArea_PMP_BigByteCount
        CMP     r5, #DynArea_PMP_BigPageCount
        MOVLO   r5, r5, LSL #12
        LDRHS   r5, =DynArea_PMP_BigByteCount
10
Neil Turton's avatar
Neil Turton committed
1673 1674 1675 1676 1677
        LDR     r7, [r10, #DANode_Workspace]
        LDR     r8, [r10, #DANode_Title]
        CLRV
        EXIT

1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
;**************************************************************************
;
;       DynArea_GetChangeInfo
;
;       Get info on changes to *non-system* dynamic areas
;       Reserved for Acorn use (intended for TaskManager, can only serve one client)
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (6)
;
; out:  r1 = Number of affected area, if a single change has occurred since last call,
;          = -1, if no changes or more than one change have occurred
;       r2 = signature of changes to non-system dynamic areas since last call to
;            OS_DynamicArea 6
;            bit 0 = 1 if any non-system areas have been created
;            bit 1 = 1 if any non-system areas have been removed
;            bit 2 = 1 if any non-system areas have been resized
;            bit 3 = 1 if any non-system areas have been renumbered
;            bits 4-31 reserved (undefined)
;
;Notes:
; (1) bit 2 of r2 excludes the initial grow of a created area, and the final
;     shrink of a removed area
; (2) if a single renumber has occurred, r1 is the old number

  [ DynArea_QuickHandles

DynArea_GetChangeInfo ROUT
        Push    "lr"
Jeffrey Lee's avatar
Jeffrey Lee committed
1708
        LDR     r11, =ZeroPage
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
        LDR     r11, [r11, #DynArea_ws]
        LDR     r1, DynArea_OD6Handle
        LDR     r2, DynArea_OD6Signature
        LDR     lr, DynArea_OD6PrevSignature
        CMP     lr, #0
        MOVNE   r1, #-1
        CMP     r2, #0
        MOVEQ   r1, #-1
        MOV     lr, #0
        STR     lr, DynArea_OD6Signature
        STR     lr, DynArea_OD6PrevSignature
        CLRV
        Pull    "PC"

  ] ;DynArea_QuickHandles

1725 1726
  [ DynArea_QuickHandles

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
;**************************************************************************
;
;       DynArea_EnumerateInfo
;
;       Enumerate *non-system* dynamic areas, returning selected info
;       Reserved for Acorn use (intended for TaskManager)
;
; in:   r0 = reason code (7)
;       r1 = -1 to start enumeration, or area number to continue from
;
; out:  r1 = number of next area found, or -1 if no more areas
1738
;       r2 = current size of area (in bytes), if area found
1739 1740
;       r3 = base logical address, if area found
;       r4 = area flags, if area found
1741
;       r5 = maximum size of area (in bytes), if area found
1742 1743 1744 1745 1746 1747 1748
;       r6 -> title string, if area found
;
;Notes:
; (1) r2-r6 on exit are undefined if r1 = -1
;

DynArea_EnumerateInfo ROUT
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
        Push    "r2,lr"
        MOV     r2, #0
        BL      DynArea_EnumerateInfo2
        Pull    "r2,pc",VS
        CMP     r1, #-1
        Pull    "r2,pc",EQ
        ; Clamp at safe values
        CMP     r2, #DynArea_PMP_BigPageCount
        MOVLO   r2, r2, LSL #12
        LDRHS   r2, =DynArea_PMP_BigByteCount
        CMP     r5, #DynArea_PMP_BigPageCount
        MOVLO   r5, r5, LSL #12
        LDRHS   r5, =DynArea_PMP_BigByteCount
        ADD     sp, sp, #4
        Pull    "pc"

;**************************************************************************
;
;       DynArea_EnumerateInfo2
;
;       Enumerate *non-system* dynamic areas, returning selected info
;       Reserved for Acorn use (intended for TaskManager)
;
; in:   r0 = reason code (28)
;       r1 = -1 to start enumeration, or area number to continue from
;       r2 = flags (reserved)
;
; out:  r1 = number of next area found, or -1 if no more areas
;       r2 = current size of area (in pages), if area found
;       r3 = base logical address, if area found
;       r4 = area flags, if area found
;       r5 = maximum size of area (in pages), if area found
;       r6 -> title string, if area found
;
;Notes:
; (1) r2-r6 on exit are undefined if r1 = -1
;

DynArea_EnumerateInfo2 ROUT
1788 1789
        Push    "lr"

1790 1791 1792
        CMP     r2, #0
        BNE     %FT90

Jeffrey Lee's avatar
Jeffrey Lee committed
1793
        LDR     r11, =ZeroPage
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
        LDR     r11, [r11, #DynArea_ws]

        CMP     r1, #-1                         ; if starting from beginning
        LDREQ   r10, DynArea_SortedList         ; then load pointer to 1st node on sorted list
        BEQ     %FT10                           ; and skip

        LDR     r10, DynArea_LastEnumHandle
        CMP     r10, r1
        LDREQ   r10, DynArea_LastEnumPtr        ; pick up where we left off
        BEQ     %FT08
        BL      QCheckAreaNumber                ; else check valid area number
        BCC     %FT14

08
        LDR     r10, [r10, #DANode_SortLink]    ; find next one
10
        TEQ     r10, #0                         ; if at end
        MOVEQ   r1, #-1                         ; then return -1
        BEQ     %FT12
        LDR     r1, [r10, #DANode_Number]       ; else return number
        CMP     r1, #ChangeDyn_MaxArea
        BLS     %BT08                           ; skip if system area

1817
        LDR     r4, [r10, #DANode_Flags]        ; return rest of info
1818 1819
        LDR     r3, [r10, #DANode_Base]
        LDR     r6, [r10, #DANode_Title]
1820
        TST     r4, #DynAreaFlags_PMP
1821 1822 1823 1824 1825 1826 1827
        LDRNE   r2, [r10, #DANode_PMPSize]
        LDRNE   r5, [r10, #DANode_PMPMaxSize]
        BNE     %FT11
        LDR     r2, [r10, #DANode_Size]
        LDR     r5, [r10, #DANode_MaxSize]
        MOV     r2, r2, LSR #12
        MOV     r5, r5, LSR #12
1828
11
1829

Jeffrey Lee's avatar
Jeffrey Lee committed
1830
        LDR     r11, =ZeroPage
1831 1832 1833 1834 1835 1836 1837 1838
        LDR     r11, [r11, #DynArea_ws]
        STR     r1, DynArea_LastEnumHandle
        STR     r10, DynArea_LastEnumPtr        ; save a lot of messing about on next call
12
        CLRV
        Pull    "PC"

14
1839 1840 1841 1842 1843 1844 1845 1846
        ADRL    r0, ErrorBlock_BadDynamicArea
        B       %FT90
80
        ADRL    r0, ErrorBlock_BadParameters
90
 [ International
        BL      TranslateError
 |
1847
        SETV
1848
 ]
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
        Pull    "PC"

  ] ;DynArea_QuickHandles

;**************************************************************************
;
;       DynArea_SetClamps
;
;       Set clamps on max size of dynamic areas created by subsequent
;       calls to OS_DynamicArea 0
;
;   On entry
;       R0 = 8 (reason code)
;       R1 = limit on maximum size of (non-Sparse) areas created by
;            OS_DynamicArea 0 with R5 = -1, or 0 to read only
;       R2 = limit on maximum size of (non-Sparse) areas created by
;            OS_DynamicArea 0 with R5 > 0, or 0 to read only
;       R3 = limit on maximum size of Sparse areas created by
;            OS_DynamicArea 0 with R4 bit 10 set, or 0 to read only
;
;   On exit
;       R1 = previous limit for OS_DynamicArea 0 with R5 = -1
;       R2 = previous limit for OS_DynamicArea 0 with R5 > 0
;       R3 = previous limit for OS_DynamicArea 0 with R4 bit 10 set
;
;       Specifying -1 in R1 or R2 means the respective limit
;       is the RAM limit of the machine (this is the default).
;       Specifiying larger than the RAM limit in R1 or R2 is
;       equivalent to specifiying -1.
;
  [ DynArea_QuickHandles

DynArea_SetClamps ROUT
        Push    "r9,lr"

Jeffrey Lee's avatar
Jeffrey Lee committed
1884
        LDR     r11, =ZeroPage
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
        LDR     r11, [r11, #DynArea_ws]

        LDR     r10, DynArea_OD8Clamp1
        LDR     lr,  DynArea_OD8Clamp2
        LDR     r9,  DynArea_OD8Clamp3

;insist on at least 1M for a clamp value (ignore anything lower)
;
        CMP     r1, #&100000
        STRHS   r1, DynArea_OD8Clamp1
        CMP     r2, #&100000
        STRHS   r2, DynArea_OD8Clamp2
        CMP     r3, #&100000
        STRHS   r3, DynArea_OD8Clamp3

        MOV     r1, r10
        MOV     r2, lr
        MOV     r3, r9

        CLRV
        Pull    "r9,PC"

  ] ;DynArea_QuickHandles

;**************************************************************************

;       DynArea_SparseClaim
;
;  Ensure region of sparse dynamic area is mapped to valid memory
;
; in:   r0 = reason code (9)
;       r1 = area number
;       r2 = base of region to claim
;       r3 = size of region to claim
;
; out:  r0-r3 preserved (error if not all of region successfully mapped)
;       r10-r12 may be corrupted
;
; action: - round base and size to page granularity
;         - scan the L2PT for each page covered
;         - find contiguous fragments of 1 or more pages that are not yet mapped-in
;         - pass each of these fragments as a psuedo DANode to OS_ChangeDynamicArea
;           via special Batcall), to 'grow' pages into each fragment
;
  [ DA_Batman

DynArea_SparseClaim ROUT
        Push    "r0-r9,lr"
        MOV     r4,#1                   ; flags operation as a claim

DynArea_SparseChange                    ; common entry point for claim and release

  [ DynArea_QuickHandles
        BL      QCheckAreaNumber        ; check area exists
  |
        BL      CheckAreaNumber         ; check area exists
  ]
        BCC     DA_naffsparse           ; area not there

        LDR     r9,[r10,#DANode_Flags]
        TST     r9,#DynAreaFlags_SparseMap
        BEQ     DA_naffsparse           ; area not sparse

        MOV     r9,#&1000               ;page size
        SUB     r9,r9,#1
        ADD     r3,r3,r2                ;base+size
        CMP     r4,#1
        BICEQ   r2,r2,r9                ;round base down to page granularity for claim
        ADDEQ   r3,r3,r9
        BICEQ   r3,r3,r9                ;round base+size up to page granularity for claim
        ADDNE   r2,r2,r9
        BICNE   r2,r2,r9                ;round base up to page granularity for release
        BICNE   r3,r3,r9                ;round base+size down to page granularity for release
        SUB     r3,r3,r2                ;rounded size

        ADD     r9,r3,r2
        LDR     r5,[r10,#DANode_Base]
        LDR     r6,[r10,#DANode_MaxSize]
        ADD     r6,r6,r5
        CMP     r2,r5
        CMPHS   r9,r5
        BLO     DA_naffsparse
        CMP     r2,r6
        CMPLS   r9,r6
        BHI     DA_naffsparse

        ADD     r5,r2,r3                   ;base+size of mapping
        LDR     r6,[r10,#DANode_SparseHWM] ;high water mark = highest claim base+size seen
        CMP     r4,#1
        BEQ     %FT08
        CMP     r5,r6
        SUBHI   r3,r6,r2                   ;for release we can save work by trimming to high water mark
        B       %FT09                      ;r3 is now trimmed size (may be <=0 for trim to nothing)
08
        CMP     r5,r6
        STRHI   r5,[r10,#DANode_SparseHWM] ;for claim remember highest base+size as high water mark
09
        SUB     SP,SP,#DANode_NodeSize  ;room for temporary DANode on stack
        MOV     r9,r10                  ;actual sparse area DANode
        MOV     r5,SP
        MOV     r6,#DANode_NodeSize
10
        LDR     r7,[r9],#4           ;copy sparse area node to temp node
        STR     r7,[r5],#4
        SUBS    r6,r6,#4
        BNE     %BT10
        ADD     r3,r2,r3             ;stop address
;
Jeffrey Lee's avatar
Jeffrey Lee committed
1993
        MOV     r8,r2                ;start address
1994
20
1995
        PTOp    ScanSparse
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
        CMP     r8,r3
        BHS     %FT50                ;done
;set up pseudo DA and do Batcall to change mapping of fragment we have found
40
        MOV     r2,SP                  ;temp DANode
        STR     r8,[r2,#DANode_Base]
        ADD     r8,r8,r1
        ADD     r8,r8,#&1000           ;next address to check after fragment
        CMP     r4,#1
        MOVEQ   r9,#0                  ;start size of 0 for claim
        MOVNE   r9,r1                  ;start size of fragment size for release
        STR     r9,[r2,#DANode_Size]
        STR     r1,[r2,#DANode_MaxSize]
        MOV     r0,#ChangeDyn_Batcall
        CMP     r4,#0
        RSBEQ   r1,r1,#0               ;batshrink for release, batgrow for claim
        SWI     XOS_ChangeDynamicArea
2013
        TEQ     r4,#0
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
        RSBEQ   r1,r1,#0
        LDR     r9,[r10,#DANode_Size]
        ADD     r9,r9,r1
        STR     r9,[r10,#DANode_Size]
        BVC     %BT20
;
50
        ADD     SP,SP,#DANode_NodeSize   ;drop temp DANode
        BVS     %FT52
        BL      DA_sparse_serviceandsig
        Pull    "r0-r9,PC"
;
52
        BL      DA_sparse_serviceandsig
        SETV
        STR     r0,[SP]
        Pull    "r0-r9,PC"

DA_naffsparse
        ADR     r0,DA_naffsparseinnit
        SETV
        STR     r0,[SP]
        Pull    "r0-r9,PC"
DA_naffsparseinnit
        DCD     0
        DCB     "invalid OS_DynamicArea sparse claim/release",0
        ALIGN

DA_sparse_serviceandsig ROUT
        Push    "r0,LR"
        LDR     r1,[r10,#DANode_Number]
   [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
2046
        LDR     r11,=ZeroPage
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
        LDR     r11,[r11, #DynArea_ws]
        LDR     r5,DynArea_OD6Signature
        STR     r5,DynArea_OD6PrevSignature
        ORR     r5,r5,#4                     ;signal a resize
        STR     r5,DynArea_OD6Signature
        STR     r1,DynArea_OD6Handle
   ]
        MOV     r2,r1                    ;area number
        MOV     r0,#0                    ;nominal 'grow/shrink' of 0 for service call
        MOV     r1,#Service_MemoryMoved
        BL      Issue_Service
        Pull    "r0,PC"

  ] ;DA_Batman

;**************************************************************************
;
;       DynArea_SparseRelease
;
;  Allow region of sparse dynamic area to release memory to free pool
;
; in:   r0 = reason code (10)
;       r1 = area number
;       r2 = base of region to release
;       r3 = size of region to release
;
; out:  r0-r3 preserved (error if not all of region successfully released)
;       r10-r12 may be corrupted
;
;
; action: - similar to DynArea_SparseClaim, but does 'shrinks' on fragments
;           that are mapped in

  [ DA_Batman

DynArea_SparseRelease ROUT
        Push    "r0-r9,lr"
        MOV     r4,#0                   ; flags operation as a release
        B       DynArea_SparseChange    ; jump to common code

  ] ;DA_Batman

Neil Turton's avatar
Neil Turton committed
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
;**************************************************************************
;
;       DynArea_Enumerate - Enumerate dynamic areas
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (3)
;       r1 = -1 to start enumeration, or area number to continue from
;
; out:  r1 = next area number or -1 if no next
;       r10-r12 may be corrupted
;       All other registers preserved

DynArea_Enumerate ALTENTRY
2103
  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
2104
        LDR     r11, =ZeroPage
2105 2106
        LDR     r11, [r11, #DynArea_ws]
  ]
Neil Turton's avatar
Neil Turton committed
2107
        CMP     r1, #-1                         ; if starting from beginning
2108 2109 2110
  [ DynArea_QuickHandles
        LDREQ   r10, DynArea_SortedList         ; then load pointer to 1st node on sorted list
  |
Jeffrey Lee's avatar
Jeffrey Lee committed
2111
    [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
2112
        LDREQ   r10, [r1, #DAList+1]            ; then load pointer to 1st node
Jeffrey Lee's avatar
Jeffrey Lee committed
2113 2114 2115 2116
    |
        LDREQ   r10, =ZeroPage
        LDREQ   r10, [r10, #DAList]
    ]
2117
  ]
Neil Turton's avatar
Neil Turton committed
2118 2119
        BEQ     %FT10                           ; and skip

2120 2121 2122 2123 2124 2125 2126
  [ DynArea_QuickHandles
        LDR     r10, DynArea_LastEnumHandle
        CMP     r10, r1
        LDREQ   r10, DynArea_LastEnumPtr        ; pick up where we left off
        BEQ     %FT08
        BL      QCheckAreaNumber                ; else check valid area number
  |
Neil Turton's avatar
Neil Turton committed
2127
        BL      CheckAreaNumber                 ; else check valid area number
2128
  ]
Neil Turton's avatar
Neil Turton committed
2129 2130
        BCC     UnknownDyn                      ; complain if passed in duff area number

2131 2132 2133 2134
08
  [ DynArea_QuickHandles
        LDR     r10, [r10, #DANode_SortLink]    ; find next one
  |
Neil Turton's avatar
Neil Turton committed
2135
        LDR     r10, [r10, #DANode_Link]        ; find next one
2136
  ]
Neil Turton's avatar
Neil Turton committed
2137 2138 2139
10
        TEQ     r10, #0                         ; if at end
        MOVEQ   r1, #-1                         ; then return -1
2140 2141 2142 2143
        BEQ     %FT12
        LDR     r1, [r10, #DANode_Number]       ; else return number

  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
2144
        LDR     r11, =ZeroPage
2145 2146 2147 2148 2149
        LDR     r11, [r11, #DynArea_ws]
        STR     r1, DynArea_LastEnumHandle
        STR     r10, DynArea_LastEnumPtr        ; save a lot of messing about on next call
  ]
12
Neil Turton's avatar
Neil Turton committed
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
        CLRV
        EXIT

;**************************************************************************
;
;       DynArea_Renumber - Renumber dynamic area
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (4)
;       r1 = old area number
;       r2 = new area number
;

DynArea_Renumber ALTENTRY
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
        CMP     r1, #ChangeDyn_MaxArea
        BLS     %FT92                           ; can't renumber a system area
        CMP     r1, #ChangeDyn_MaxArea
        BLS     %FT92
  [ DynArea_QuickHandles
        CMP     r1, #DynArea_NewAreas
        BLO     %FT10
        CMP     r1, #DynArea_NewAreas+DynArea_NumQHandles
        BLO     %FT92                           ; can't renumber a quick handle
10
        CMP     r2, #DynArea_NewAreas
        BLO     %FT12
        CMP     r2, #DynArea_NewAreas+DynArea_NumQHandles
        BLO     %FT92                           ; can't choose your own quick handle
12
        BL      QCheckAreaNumber
  |
Neil Turton's avatar
Neil Turton committed
2182
        BL      CheckAreaNumber                 ; check valid area number
2183
  ]
Neil Turton's avatar
Neil Turton committed
2184 2185 2186 2187 2188
        BCC     UnknownDyn                      ; [it's not]

        Push    "r1"
        MOV     r12, r10                        ; save pointer to node
        MOV     r1, r2
2189 2190 2191
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber                ; check area r2 doesn't already exist
  |
Neil Turton's avatar
Neil Turton committed
2192
        BL      CheckAreaNumber                 ; check area r2 doesn't already exist
2193
  ]
Neil Turton's avatar
Neil Turton committed
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
        Pull    "r1"
        BCS     %FT90                           ; [area r2 already exists]

        STR     r2, [r12, #DANode_Number]

; Now issue service to tell TaskManager

        Push    "r1-r3"
        MOV     r3, r2                          ; new number
        MOV     r2, r1                          ; old number
        MOV     r1, #Service_DynamicAreaRenumber
        BL      Issue_Service
        Pull    "r1-r3"

2208
  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
2209
        LDR     r11, =ZeroPage                         ; we know system areas cannot be renumbered
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
        LDR     r11, [r11, #DynArea_ws]
        MOV     r10, #-1
        STR     r10, DynArea_CreatingHandle     ; invalidate these, just in case
        STR     r10, DynArea_LastTreacleHandle
        STR     r10, DynArea_LastEnumHandle
        LDR     lr, DynArea_OD6Signature
        STR     lr, DynArea_OD6PrevSignature
        ORR     lr, lr, #8
        STR     lr, DynArea_OD6Signature
        STR     r1, DynArea_OD6Handle
  ]

Neil Turton's avatar
Neil Turton committed
2222 2223 2224 2225 2226 2227 2228
        CLRV
        EXIT

90
        ADRL    r0, ErrorBlock_AreaAlreadyExists
 [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
2229
 |
Neil Turton's avatar
Neil Turton committed
2230
        SETV
Robert Sprowson's avatar
Robert Sprowson committed
2231
 ]
Neil Turton's avatar
Neil Turton committed
2232 2233
        EXIT

2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
;if you think this is worth internationalising, I pity you
92
        ADR     r0, DynArea_NaughtyRenum
        SETV
        EXIT
DynArea_NaughtyRenum
        DCD     0
        DCB     "illegal DA renumber",0
        ALIGN

2244 2245
        LTORG

2246
 [ ShrinkableDAs
Neil Turton's avatar
Neil Turton committed
2247 2248
;**************************************************************************
;
2249
;       DynArea_ReturnFree - Return total free space, including shrinkables
Neil Turton's avatar
Neil Turton committed
2250
;
2251
;       Internal routine called by DynamicAreaSWI
Neil Turton's avatar
Neil Turton committed
2252
;
2253 2254
; in:   r0 = reason code (5)
;       r1 = area number to exclude, or -1 to include all shrinkable areas
Neil Turton's avatar
Neil Turton committed
2255
;
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
; out:  r2 = total amount of free memory, bytes
;

DynArea_ReturnFree
        Push    "lr"
        BL      DynArea_ReturnFreePages
        Pull    "pc",VS
        ; Clamp at safe value
        CMP     r2, #DynArea_PMP_BigPageCount
        MOVLO   r2, r2, LSL #12
        LDRHS   r2, =DynArea_PMP_BigByteCount
        Pull    "pc"

;**************************************************************************
;
;       DynArea_ReturnFreePages - Return total free space, including shrinkables
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (27)
;       r1 = area number to exclude, or -1 to include all shrinkable areas
;
; out:  r2 = total amount of free memory, in pages
Neil Turton's avatar
Neil Turton committed
2279 2280
;

2281
DynArea_ReturnFreePages ALTENTRY
2282 2283 2284
        CMP     r1, #-1                         ; if no excluded area,
        MOVEQ   r10, r1                         ; then point r10 nowhere
        BEQ     %FT10
Neil Turton's avatar
Neil Turton committed
2285

2286 2287 2288
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber                ; else check area number is valid
  |
2289
        BL      CheckAreaNumber                 ; else check area number is valid
2290
  ]
2291
        BCC     UnknownDyn                      ; [unknown area]
Neil Turton's avatar
Neil Turton committed
2292
10
Jeffrey Lee's avatar
Jeffrey Lee committed
2293
        LDR     r2, =ZeroPage
2294
        LDR     r2, [r2, #FreePoolDANode + DANode_PMPSize] ; start with current size of free pool
2295 2296
  [ DynArea_QuickHandles
        ;traverse the Shrinkable sublist
Jeffrey Lee's avatar
Jeffrey Lee committed
2297
        LDR     r11, =ZeroPage
2298 2299 2300 2301
        LDR     r11, [r11, #DynArea_ws]
        LDR     r11, DynArea_ShrinkableSubList
        B       %FT21
  |
Jeffrey Lee's avatar
Jeffrey Lee committed
2302
        LDR     r11, =ZeroPage+DAList
2303
  ]
Neil Turton's avatar
Neil Turton committed
2304
20
2305 2306 2307
  [ DynArea_QuickHandles
        LDR     r11, [r11, #DANode_SubLink]     ; load next area on sublist
  |
2308
        LDR     r11, [r11, #DANode_Link]        ; load next area
2309 2310
  ]
21
2311
        TEQ     r11, #0                         ; if end of list
2312
        BEQ     %FT90                           ; then exit, with r2 = correct value
Neil Turton's avatar
Neil Turton committed
2313

2314 2315 2316
  [ DynArea_QuickHandles
        TEQ     r11, r10                        ; must not be the excluded area
  |
2317 2318 2319
        LDR     lr, [r11, #DANode_Flags]        ; load area flags
        TST     lr, #DynAreaFlags_Shrinkable    ; must be shrinkable
        TEQNE   r11, r10                        ; and not excluded area
2320
  ]
2321
        BEQ     %BT20                           ; [don't try this one]
Neil Turton's avatar
Neil Turton committed
2322 2323

        Push    r3
2324
        BL      CallTestShrink
2325
        ADD     r2, r2, r3, LSR #12             ; add on amount if any
2326 2327
        Pull    r3
        B       %BT20                           ; then go back for more
2328 2329 2330

90
        EXIT
Neil Turton's avatar
Neil Turton committed
2331 2332
 ]

2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
;**************************************************************************
;
;       DynArea_Locate - Return area number given an address
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (20)
;       r1 = logical address to locate
;
; out:  r0 = area type (0=dynamic, 1=system, others reserved)
;       r1 = area number
;       r10-r12 may be corrupted
;       All other registers preserved
;

DynArea_Locate Entry "r2-r5"
        MOV     r5, r1

        MOV     r4, #1
10
        MOV     r0, r4, LSL #8
        BL      MemoryAreaInfo
        BVS     %FT20                           ; no more system areas

        ADD     r2, r1, r2                      ; r1:=base r2:=top
        CMP     r5, r1
        CMPCS   r2, r5
        MOVHI   r1, r4                          ; number
        MOVHI   r0, #1                          ; system area
        EXIT    HI

        ADD     r4, r4, #1
        B       %BT10
20
  [ DynArea_QuickHandles
        LDR     r11, =ZeroPage
        LDR     r11, [r11, #DynArea_ws]

        ADR     r3, DynArea_SysQHandleArray
        MOV     r4, #0
30
        LDR     r10, [r3, r4, LSL #2]
        TEQ     r10, #0
        BLNE    %FT60                           ; check system DA node

        ADD     r4, r4, #1
        CMP     r4, #ChangeDyn_MaxArea
        BLS     %BT30

        ADR     r3, DynArea_QHandleArray
        MOV     r4, #0
40
        LDR     r10, [r3, r4, LSL #2]
        CMP     r10, #DynArea_NumQHandles
        BLHI    %FT60                           ; check quick DA node

        ADD     r4, r4, #1
        CMP     r4, #DynArea_NumQHandles
        BCC     %BT40
  ]
        LDR     r10, =ZeroPage+DAList
        ASSERT  DANode_Link = 0                 ; because DAList has only link
50
        LDR     r10, [r10, #DANode_Link]
        TEQ     r10, #0
        PullEnv EQ
        ADREQL  r0, ErrorBlock_BadAddress
2400
        BEQ     DynArea_TranslateAndReturnError
2401 2402 2403 2404

        BL      %FT60                           ; check treacle DA node
        B       %BT50
60
2405
        LDR     r1, [r10, #DANode_Flags]
2406
        LDR     r0, [r10, #DANode_MaxSize]
2407
        TST     r1, #DynAreaFlags_DoublyMapped
2408
        LDR     r1, [r10, #DANode_Base]
2409 2410
        ADD     r2, r1, r0                      ; r1:=base r2:=top
        SUBNE   r1, r1, r0                      ; doubly mapped is special
2411 2412 2413 2414 2415 2416 2417 2418
        CMP     r5, r1
        CMPCS   r2, r5
        LDRHI   r1, [r10, #DANode_Number]       ; number
        MOVHI   r0, #0                          ; dynamic area
        EXIT    HI

        MOV     pc, lr                          ; not within this one

Neil Turton's avatar
Neil Turton committed
2419 2420
;**************************************************************************
;
2421
;       DynArea_PMP_PhysOp
Neil Turton's avatar
Neil Turton committed
2422
;
2423
;  Claim/release physical memory pages in physical memory pool
Neil Turton's avatar
Neil Turton committed
2424
;
2425 2426
; in:   r0 = reason code (21)
;       r1 = area number
2427
;       r2 = pointer to array of (PMP page index, phys page index, page flag) tuples
2428 2429 2430
;            phys page index -1 to release
;            phys page index -2 to let kernel pick page
;            otherwise page number to use
2431 2432 2433 2434 2435
;
;            page flags are defined by DynAreaFlags_PMPPhysOpAccessMask, and
;            PageFlags_Reserved to allow claiming of pages previously reserved
;            via OS_Memory 23
;
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
;       r3 = number of entries
;
; out:  r0-r1 preserved (error if not all of region successfully updated)
;       r2 advanced to first entry not processed (or end of list)
;       r3 updated to number of entries not processed (or 0)
;       r10-r12 may be corrupted
;
DynArea_PMP_PhysOp ROUT
        ; Strategy:
        ; - Check free pool has enough pages to satisfy claim
        ; - Walk page list, comparing against current PMP state
        ; - If free required, check not in use and push straight into free pool
        ; - If kernel auto-alloc required, grab last page from free pool
        ; - If specific page required, batcall into OS_ChangeDynamicArea to let it deal with page reclaiming
        Entry   "r0-r9"
2451
  [ DynArea_QuickHandles
2452 2453 2454
        BL      QCheckAreaNumber        ; check area exists
  |
        BL      CheckAreaNumber         ; check area exists
2455
  ]
2456
        BCC     %FT90                   ; [it doesn't]
2457
01
2458
        ; r10 -> DANode
Jeffrey Lee's avatar
Jeffrey Lee committed
2459 2460 2461 2462 2463
        LDR     lr, [r10, #DANode_Flags]
        TST     lr, #DynAreaFlags_PMP
        BEQ     %FT90
        CMP     r3, #0                  ; early-exit for empty requests
        EXIT    EQ
2464 2465 2466 2467 2468
        LDR     r8, [r10, #DANode_PMP]
        LDR     r9, [r10, #DANode_PMPMaxSize]
        BL      ClaimCDASemaphore
        BNE     %FT91
        ; Before we begin, check to see if there's enough space in the free pool to satisfy any map in requests
2469 2470
        LDR     r7, =ZeroPage
        LDR     r11, [r7, #MaxCamEntry]
2471
        LDR     r0, [r7, #FreePoolDANode+DANode_PMPSize]
2472
        LDR     r7, [r7, #CamEntriesPointer]
2473
        CMP     r0, r3
2474
        BHS     %FT30
2475 2476 2477 2478
        ; Free pool may be too small - scan the page list and the PMP to work out exactly how many pages are required
        ; If there aren't enough, try growing the free pool (shrink app space, shrink shrinkables, etc.)
        MOV     r12, #0
        MOV     r1, #0
2479
10
2480 2481 2482 2483 2484 2485 2486
        SUBS    r3, r3, #1
        BLO     %FT20
        LDMIA   r2!, {r4-r6}
        ; Check for silly PMP page index
        CMP     r4, r9
        BHS     %FT92
        ; Check for silly phys page index
2487 2488 2489
        CMP     r5, #-2
        CMPLO   r11, r5
        BLO     %FT92
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
        ; Look up the page that's currently in the PMP
        LDR     r0, [r8, r4, LSL #2]
        TEQ     r0, r5
        BEQ     %BT10
        ; Do we need to release the existing page?
        CMP     r0, #-1
        BEQ     %FT15
        CMP     r5, #-2
        BEQ     %BT10                   ; Page is currently there, and we want a kernel picked page -> no action required
        ; A page is currently there, but we either want to release it or to swap in a different page.
        SUB     r12, r12, #1            ; Count page release
15
        ; Map in new page if required
        CMP     r5, #-2
        BHI     %BT10 ; i.e. -1
        ADD     r12, r12, #1            ; Count page claim
        ; Because we process list entries in order, we actually need to keep track of the maximum number of pages needed in the free pool at any point in the list, rather than just the delta we'll have at the end
        CMP     r12, r1
        MOVGT   r1, r12
        B       %BT10
20
        ; r1 = number of pages needed in free pool
      [ PMPDebug
        DebugReg r1, "Want this many pages: "
      ]
        MOV     r12, r10
        BL      GrowFreePool
        BCC     %FT94
        ; Enough space is available, reset r2 & r3 and process the list properly
        FRAMLDR r2
        FRAMLDR r3
Neil Turton's avatar
Neil Turton committed
2521

2522
30
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
        LDR     r12, [r10, #DANode_PMPSize]
        ; Usage in main loop:
        ; r2 -> input page list
        ; r3 = length
        ; r4 = current entry PMP index
        ; r5 = current entry phys page index
        ; r6 = current entry flags
        ; r7 -> CAM
        ; r8 -> PMP
        ; r9 = PMPMaxSize
        ; r10 -> DANode
        ; r11 = MaxCamEntry
        ; r12 = current PMPSize
        ; r0, r1 temp
2537
40
2538 2539 2540 2541 2542 2543 2544
        SUBS    r3, r3, #1
        BLO     %FT80
        LDMIA   r2!, {r4-r6}
        ; Check for silly PMP page index
        CMP     r4, r9
        BHS     %FT93
        ; Check for silly phys page index
2545 2546 2547
        CMP     r5, #-2
        CMPLO   r11, r5
        BLO     %FT93
2548
42
2549 2550 2551 2552
        AND     r6, r6, #DynAreaFlags_PMPPhysOpAccessMask
        ; Look up the page that's currently in the PMP
        LDR     r0, [r8, r4, LSL #2]
        TEQ     r0, r5
2553 2554 2555 2556
        BNE     %FT50
        CMP     r0, #-1
        BEQ     %BT40
45
2557 2558 2559 2560 2561 2562
        ; Page is there - check/update flags
        ADD     r0, r7, r0, LSL #CAM_EntrySizeLog2
        LDR     r1, [r0, #CAM_PageFlags]
        BIC     lr, r1, #DynAreaFlags_PMPPhysOpAccessMask
        ORR     lr, lr, r6
        TEQ     r1, lr
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
        BEQ     %BT40
        ; Only allow Reserved pages to become Unavailable if we're given the magic password
        TST     r6, #PageFlags_Unavailable ; Unavailable requested?
        TSTNE   r1, #PageFlags_Reserved    ; And currently Reserved?
        BEQ     %FT49
        TST     r1, #PageFlags_Unavailable ; Was it already Unavailable?
        LDREQ   r6, [r2, #-4]
        TSTEQ   r6, #PageFlags_Reserved    ; Or was Reserved specified in the page list?
        BNE     %FT49                      ; Then we're fine
        ; Attempting to claim a Reserved page without the proper permission.
        ; If we're allowed to kernel-pick a page, swap in a replacement. Else
        ; raise an error.
        CMP     r5, #-2
        BNE     %FT96
        LDR     r0, [r8, r4, LSL #2]
        BL      GetNonReservedPage
        BVS     %FT96
        ; Success, retry this page modification
        B       %BT42
49
        STR     lr, [r0, #CAM_PageFlags]
        B       %BT40
50
2586 2587 2588 2589
        ; Do we need to release the existing page?
        CMP     r0, #-1
        BEQ     %FT55
        CMP     r5, #-2
2590
        BEQ     %BT45                   ; Page is currently there, and we want a kernel picked page
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
        ; A page is currently there, but we either want to release it or to swap in a different page. Start by releasing the existing page.
        ; TODO - if we're swapping with another page we'll probably want to preserve the contents (have a flag to control behaviour)
        ; Check page isn't mapped in
        ASSERT  CAM_LogAddr=0
        LDR     r1, [r7, r0, LSL #CAM_EntrySizeLog2]
      [ PMPDebug
        DebugReg r0, "Releasing page: "
        DebugReg r1, "Current addr: "
      ]
        LDR     lr, =Nowhere
        TEQ     r1, lr
        BNE     %FT95
        ; Add the page back into the free pool
        Push    "r2-r5,r12"
        LDR     r4, =ZeroPage+FreePoolDANode
        LDR     r12, [r4, #DANode_PMP]
        LDR     r5, [r4, #DANode_PMPSize]
        STR     r0, [r12, r5, LSL #2]
        ADD     r2, r7, r0, LSL #CAM_EntrySizeLog2
        LDR     r3, [r4, #DANode_Flags]
        LDR     lr, =DynAreaFlags_AccessMask
        AND     r3, r3, lr
2613 2614 2615
        LDR     lr, [r2, #CAM_PageFlags]
        AND     lr, lr, #StickyPageFlags
        ORR     r3, r3, lr
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        STMIB   r2, {r3-r5}
        ADD     r5, r5, #1
        STR     r5, [r4, #DANode_PMPSize]
        Pull    "r2-r5,r12"
        ; Page no longer owned by us
        MOV     r0, #-1
        STR     r0, [r8, r4, LSL #2]
        SUB     r12, r12, #1
55
        ; Map in new page if required
        CMP     r5, #-2
2630 2631
        BHI     %BT40 ; i.e. -1
        BLO     %FT60 ; specific page
2632 2633 2634
      [ PMPDebug
        DebugTX "Kernel-picking page"
      ]
2635
        ; Kernel-picked page required. Find a suitable page in the free pool.
2636 2637
        LDR     r0, =ZeroPage+FreePoolDANode
        LDR     r1, [r0, #DANode_PMP]
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
        LDR     lr, [r0, #DANode_PMPSize]
        Push    "r4,r8"
56
        SUBS    lr, lr, #1
        Pull    "r4,r8",LO
        BLO     %FT95
        LDR     r5, [r1, lr, LSL #2]
        TST     r6, #PageFlags_Unavailable
        BEQ     %FT57
        ; Caller is requesting exclusive use of the page, so avoid picking a
        ; reserved page.
        ; Note: For this case, we don't allow the caller to specify
        ; PageFlags_Reserved to allow use of reserved pages, because we have no
        ; way of knowing whether a particular page was reserved by them or by
        ; someone else.
        ADD     r4, r7, r5, LSL #CAM_EntrySizeLog2
        LDR     r4, [r4, #CAM_PageFlags]
        TST     r4, #PageFlags_Reserved
        BNE     %BT56
57
        LDR     r4, [r0, #DANode_PMPSize]
        SUB     r4, r4, #1
        STR     r4, [r0, #DANode_PMPSize]
        CMP     r4, lr
        LDRNE   r8, [r1, r4, LSL #2] ; Within the PMP, shuffle last free pool page down to replace the page we're taking
        STRNE   r8, [r1, lr, LSL #2]
        ADDNE   r8, r7, r8, LSL #CAM_EntrySizeLog2
        STRNE   lr, [r8, #CAM_PMPIndex] ; And update the CAM
2666
        MOV     r0, #-1
2667 2668 2669
        STR     r0, [r1, r4, LSL #2]
        Pull    "r4,r8"
        ; Add page r5 to our PMP
2670
59
2671 2672 2673 2674 2675 2676
        STR     r5, [r8, r4, LSL #2]
        ADD     r5, r7, r5, LSL #CAM_EntrySizeLog2
        LDR     r0, [r10, #DANode_Flags] ; Use default DA flags, modified by flags given in page list
        LDR     r1, =DynAreaFlags_AccessMask :AND: :NOT: DynAreaFlags_PMPPhysOpAccessMask
        AND     r0, r0, r1
        ORR     r0, r0, r6
2677 2678 2679 2680
        LDR     lr, [r5, #CAM_PageFlags]
        AND     lr, lr, #StickyPageFlags
        ORR     r0, r0, lr
        MOV     lr, r4
2681 2682 2683 2684 2685
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        STMIB   r5, {r0,r10,lr}
        ADD     r12, r12, #1
2686
        B       %BT40
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
60
        ; Check that the requested page isn't locked
        ADD     r0, r7, r5, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        LDMIA   r0, {r0-r1}
      [ PMPDebug
        DebugReg r5, "Claiming page: "
        DebugReg r0, "Current addr: "
        DebugReg r1, "Current flags: "
      ]
        TST     r1, #PageFlags_Unavailable
        BNE     %FT95
2700 2701 2702 2703 2704 2705 2706 2707
        ; Only allow Reserved pages to become Unavailable if we're given the magic password
        TST     r6, #PageFlags_Unavailable ; Unavailable requested?
        TSTNE   r1, #PageFlags_Reserved    ; And currently Reserved?
        BEQ     %FT605
        LDR     lr, [r2, #-4]
        TST     lr, #PageFlags_Reserved
        BEQ     %FT96
605
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
        ; Construct a dummy DANode on the stack so we can use a Batcall to map
        ; in the page
        SUB     sp, sp, #DANode_NodeSize
        ; Copy over real node as basis
        MOV     r0, #DANode_NodeSize
61
        SUBS    r0, r0, #4
        LDR     r1, [r10, r0]
        STR     r1, [sp, r0]
        BGT     %BT61
        ; Adjust some parameters to ensure batcall is happy
        LDR     r0, =Nowhere
        STR     r0, [sp, #DANode_Base]
        LDR     lr, =ZeroPage
        MOV     r0, #0
        STR     r0, [lr, #CDASemaphore] ; Temporarily release so batcall will work
        STR     r0, [sp, #DANode_Size]
        MOV     r0, #4096
        STR     r0, [sp, #DANode_MaxSize]
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
        ; Because we're claiming the page via a Batcall, we need to make sure that the DA flags used for the call are valid as DA flags - i.e. don't touch any pageflags-only flags, because they might overlap the DA flags.
        ; Once the page is ours we'll fix up the other flags to be as the user requested.
    [ (DynAreaFlags_AccessMask :AND: DynAreaFlags_PMPPhysOpAccessMask) <> 0
        LDR     r0, [sp, #DANode_Flags]
      [ PMPDebug
        DebugReg r0, "Area flags: "
      ]
        LDR     lr, =DynAreaFlags_AccessMask :AND: DynAreaFlags_PMPPhysOpAccessMask
        BIC     r0, r0, lr
        AND     lr, r6, lr
        ORR     r0, r0, lr
      [ PMPDebug
        DebugReg r0, "Batcall flags: "
      ]
2741
        STR     r0, [sp, #DANode_Flags]
2742
    ]
2743 2744
        ; Replace handler routine with our own
        STR     r5, [sp, #DANode_Workspace] ; Required page number is handler param
2745
        ADR     r0, PMPGrowHandlerReserved ; If we're dealing with a reserved page, we must specify "RESV", otherwise OS_ChangeDynamicArea won't let us move it
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
        STR     r0, [sp, #DANode_Handler]
        ; Make the call
        Push    "r2"
        ADD     r2, sp, #4
        MOV     r0, #ChangeDyn_Batcall
        MOV     r1, #4096
        SWI     XOS_ChangeDynamicArea
        LDR     r2, =ZeroPage
        STR     sp, [r2, #CDASemaphore]
        Pull    "r2"
        ADD     sp, sp, #DANode_NodeSize
        BVS     %FT99
        ; Everything went OK, remember the new page as being ours
2759
        B       %BT59
Neil Turton's avatar
Neil Turton committed
2760

2761 2762 2763 2764 2765 2766 2767
80
        BL      PMPMemoryMoved
        CLRV
        FRAMSTR r2
        MOV     r3, #0
        FRAMSTR r3
        EXIT
Neil Turton's avatar
Neil Turton committed
2768

2769 2770 2771 2772 2773 2774 2775 2776 2777
90
        PullEnv
        ADRL    r0, ErrorBlock_BadDynamicArea
 [ International
        B       TranslateError
 |
        SETV
        MOV     pc, lr
 ]
2778

2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
91
        ; Failed to claim CDASemaphore
        PullEnv
        ADRL    r0, ErrorBlock_ChDynamNotAllMoved
 [ International
        B       TranslateError
 |
        SETV
        MOV     pc, lr
 ]
Neil Turton's avatar
Neil Turton committed
2789

2790 2791 2792 2793 2794 2795
92
        ; Error during initial list scan - reset parameters as if nothing's been moved
        FRAMLDR r2
        FRAMLDR r3
        ADD     r2, r2, #12
        SUB     r3, r3, #1
2796
        LDR     r12, [r10, #DANode_PMPSize]
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
93
      [ PMPDebug
        DebugTX "-> bad physop page number"
      ]
        ADRL    r0, ErrorBlock_BadPageNumber
        B       %FT98

94
        ; Error during initial list scan - reset parameters as if nothing's been moved
        FRAMLDR r2
        FRAMLDR r3
        ADD     r2, r2, #12
        SUB     r3, r3, #1
2810
        LDR     r12, [r10, #DANode_PMPSize]
2811 2812 2813 2814 2815 2816
95
      [ PMPDebug
        DebugTX "-> physop can't move"
      ]
        ADRL    r0, ErrorBlock_ChDynamNotAllMoved
        B       %FT98
Neil Turton's avatar
Neil Turton committed
2817

2818 2819 2820 2821 2822 2823 2824
96
      [ PMPDebug
        DebugTX "-> physop can't use page"
      ]
        ADRL    r0, ErrorBlock_CantGetPhysMem
        B       %FT98

2825
98
Neil Turton's avatar
Neil Turton committed
2826 2827
 [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
2828 2829
 |
        SETV
Neil Turton's avatar
Neil Turton committed
2830
 ]
2831 2832 2833 2834 2835 2836 2837 2838
99
        BL      PMPMemoryMoved
        FRAMSTR r0
        ; Wind r2, r3 back one entry to point to the entry that's causing the problem
        SUB     r2, r2, #12
        ADD     r3, r3, #1
        FRAMSTR r2
        FRAMSTR r3
Neil Turton's avatar
Neil Turton committed
2839 2840
        EXIT

2841 2842 2843 2844
; r10 -> DANode
DynArea_PMP_PhysOp_WithNode ALTENTRY
        B       %BT01

Neil Turton's avatar
Neil Turton committed
2845

2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
PMPMemoryMoved ROUT
        EntryS  "r0-r2,r5,r11"
        ; In: r10 -> DANode
        ;     r12 = new size (pages)
        ; Update PMPSize, release CDASemaphore, and issue Service_MemoryMoved
        LDR     r0, [r10, #DANode_PMPSize]
        SUBS    r0, r0, r12
        STR     r12, [r10, #DANode_PMPSize]
        RSBLT   r0, r0, #0
        CMP     r0, #DynArea_PMP_BigPageCount
        MOVLO   r0, r0, LSL #12
        LDRHS   r0, =DynArea_PMP_BigByteCount
        LDR     r2, [r10, #DANode_Number]
        ; Release CDASemaphore
        LDR     r11, =ZeroPage
        MOV     r5, #0
        STR     r5, [r11, #CDASemaphore]
   [ DynArea_QuickHandles
        LDR     r11, [r11, #DynArea_ws]
        LDR     r5, DynArea_OD6Signature
        STR     r5, DynArea_OD6PrevSignature
        ORR     r5, r5, #4              ;signal a resize
        STR     r5, DynArea_OD6Signature
        STR     r2, DynArea_OD6Handle
   ]
        MOV     r1,#Service_MemoryMoved
        BL      Issue_Service
      [ PMPParanoid
        BL      ValidatePMPs
      ]
        EXITS

2878 2879 2880 2881 2882
PMPGrowHandlerReserved ROUT
        TEQ     r0, #DAHandler_PreGrow
        LDREQ   r0, =DAHandler_RESV
        STREQ   r12, [r1]
        MOV     pc, lr
2883 2884

PMPGrowHandler ROUT
2885
        TEQ     r0, #DAHandler_PreGrow
2886 2887 2888
        STREQ   r12, [r1]
        MOV     pc, lr

2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
;
; In:  R0 = page number to replace
; Out: Page (& contents) replaced with a non-Reserved page, or R0=error
;
GetNonReservedPage ROUT
        Entry   "r0-r5", DANode_NodeSize
      [ PMPDebug
        DebugReg r0, "GetNonReservedPage for "
      ]
        STR     r0, [sp, #DANode_Workspace]
        ; The page we're looking for a replacement for isn't Unavailable, so we
        ; can cheat and construct a temp DANode on the stack, allowing us to
        ; use a RESV Batcall to handle finding the replacement (and then push
        ; the replaced page into the free pool)
        ;
        ; However, because we want the replacement to be non-Reserved, we must
        ; set up the free pool so that a suitable page is the last page in the
        ; pool (since page replacement code doesn't care about Reserved status
        ; of the replacement). TODO: Find a more robust way of doing this.
        LDR     r0, =ZeroPage
        LDR     r1, [r0, #FreePoolDANode+DANode_PMP]
        LDR     r2, [r0, #FreePoolDANode+DANode_PMPSize]
        SUB     r3, r2, #1
        LDR     r4, [r0, #CamEntriesPointer]
        ADD     r4, r4, #CAM_PageFlags
10
        CMP     r2, #0
        BEQ     %FT90
        SUB     r2, r2, #1
        LDR     r5, [r1, r2, LSL #2]
        LDR     lr, [r4, r5, LSL #CAM_EntrySizeLog2]
        TST     lr, #PageFlags_Reserved
        BNE     %BT10
        ; Replacement found, swap it with the last page
        LDR     lr, [r1, r3, LSL #2]
        STR     r5, [r1, r3, LSL #2] ; Update PMP
        STR     lr, [r1, r2, LSL #2]
        ADD     r5, r4, r5, LSL #CAM_EntrySizeLog2
        ADD     lr, r4, lr, LSL #CAM_EntrySizeLog2
        STR     r3, [r5, #CAM_PMPIndex-CAM_PageFlags] ; Update CAM
        STR     r2, [lr, #CAM_PMPIndex-CAM_PageFlags]
        ; Now construct the DANode and make the Batcall
        MOV     r2, sp
        MOV     r1, #0
        LDR     lr, =ZeroPage
        STR     r1, [lr, #CDASemaphore] ; Temporarily release so batcall will work
        STR     r1, [r2, #DANode_Link]
        STR     r1, [r2, #DANode_Size]
        STR     r1, [r2, #DANode_Title]
        STR     r1, [r2, #DANode_SubLink]
        STR     r1, [r2, #DANode_SparseHWM]
        STR     r1, [r2, #DANode_SortLink]
        STR     r1, [r2, #DANode_PMP]
        STR     r1, [r2, #DANode_PMPSize]
        STR     r1, [r2, #DANode_PMPMaxSize]
        MOV     r1, #DynArea_NewAreas ; A generic non-special DA number
        STR     r1, [r2, #DANode_Number]
        LDR     r1, =Nowhere
        STR     r1, [r2, #DANode_Base]
        LDR     r1, =(AreaFlags_FreePool :AND: :NOT: DynAreaFlags_PMP) :OR: DynAreaFlags_NeedsSpecificPages
        STR     r1, [r2, #DANode_Flags]
        ADR     r1, PMPGrowHandlerReserved
        STR     r1, [r2, #DANode_Handler]
        MOV     r1, #4096
        STR     r1, [r2, #DANode_MaxSize]
        MOV     r0, #ChangeDyn_Batcall
        SWI     XOS_ChangeDynamicArea ; Grow, moving R0 into our fake DA
        FRAMSTR r0, VS
        BVS     %FT80
        ; Because we used Nowhere as the dest addr, the Reserved page won't
        ; have been mapped in anywhere, and won't be part of any PMP. This means
        ; we can't add it back to the free pool by shrinking our fake DA - but
        ; we can just add it in manually.
        LDR     r0, [sp, #DANode_Workspace]
        LDR     r2, =ZeroPage+FreePoolDANode
        LDR     r1, [r2, #DANode_PMP]
        LDR     lr, [r2, #DANode_PMPSize]
        STR     r0, [r1, lr, LSL #2]
        LDR     r1, [r2, #CamEntriesPointer-FreePoolDANode]
        ADD     r1, r1, r0, LSL #CAM_EntrySizeLog2
        STR     r2, [r1, #CAM_PMP]
        STR     lr, [r1, #CAM_PMPIndex]
        ADD     lr, lr, #1
        STR     lr, [r2, #DANode_PMPSize]
        LDR     lr, =AreaFlags_FreePool :AND: DynAreaFlags_AccessMask
        LDR     r2, [r1, #CAM_PageFlags]
        AND     r2, r2, #StickyPageFlags
        ORR     r2, r2, lr
        STR     r2, [r1, #CAM_PageFlags]
80
      [ PMPDebug
        BVC     %FT81
        DebugTX "-> Failed"
        SETV
        B       %FT82
81
        DebugTX "-> Success"
82
      ]
        LDR     r2, =ZeroPage
        STR     sp, [r2, #CDASemaphore]
        EXIT
90
      [ PMPDebug
        DebugTX "-> No free non-reserved pages"
      ]
        ADRL    r0, ErrorBlock_CantGetPhysMem
 [ International
        BL      TranslateError
 |
        SETV
 ]
        FRAMSTR r0
        EXIT        

3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
;**************************************************************************
;
;       DynArea_PMP_LogOp
;
;  Map/unmap pages from logical memory
;
; in:   r0 = reason code (22)
;       r1 = area number
;       r2 = pointer to array of (DA page number, PMP page index, page flags) tuples
;            PMP page index of -1 to unmap (page flags currently ignored)
;            else PMP page index + page flags must be valid
;       r3 = number of entries
;
; out:  r0-r1 preserved (error if not all of region successfully updated)
;       r2 advanced to first entry not processed (or end of list)
;       r3 updated to number of entries not processed (or 0)
;       r10-r12 may be corrupted
;
PMPLogOp_ChunkSize * 384 ; 384 pages = 1.5K stack, or 1.5MB of memory (larger than current max Cache_RangeThreshold - otherwise full cache clean optimisation won't be taken)

Jeffrey Lee's avatar
Jeffrey Lee committed
3024 3025
                              ^ 0, sp
PMPLogOp_PageList             # PMPLogOp_ChunkSize*4 ; List of pages to map out
3026 3027
PMPLogOp_UnsafeMapIn          # 4 ; Nonzero if we've done an 'unsafe' map in (-> PageTableSync required for pages to become visible)
PMPLogOp_GlobalTLBFlushNeeded # 4 ; Nonzero if a global TLB flush needed on exit (large number of pages have been unmapped)
Jeffrey Lee's avatar
Jeffrey Lee committed
3028 3029
                              # 8 ; Padding!
PMPLogOp_FrameSize            # 0
3030 3031 3032 3033 3034 3035 3036 3037 3038

DynArea_PMP_LogOp ROUT
        Entry   "r0-r9", :INDEX:PMPLogOp_FrameSize
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber        ; check area exists
  |
        BL      CheckAreaNumber         ; check area exists
  ]
        BCC     %FT90                   ; [it doesn't]
3039
01
3040
        ; r10 -> DANode
Jeffrey Lee's avatar
Jeffrey Lee committed
3041 3042 3043 3044 3045
        LDR     lr, [r10, #DANode_Flags]
        TST     lr, #DynAreaFlags_PMP
        BEQ     %FT90
        CMP     r3, #0                  ; early-exit for empty requests
        EXIT    EQ
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
        LDR     r7, [r10, #DANode_PMP]
        LDR     r9, [r10, #DANode_PMPMaxSize]
        BL      ClaimCDASemaphore
        BNE     %FT91
        LDR     r11, [r10, #DANode_MaxSize]
        LDR     r12, [r10, #DANode_Base]
        ; Usage in main loop:
        ; r0 = number of cacheable pages being umapped
        ; r1 = offset in temp page list
        ; r2 -> input page list
        ; r3 = length
        ; r4 = current entry DA page index
        ; r5 = current entry PMP page index
        ; r6 = current entry page flags
        ; r7 -> PMP
3061
        ; r8 spare
3062 3063 3064 3065 3066
        ; r9 -> PMPMaxSize
        ; r10 -> DANode
        ; r11 -> DA max size
        ; r12 -> DA base
        MOV     r0, #0
Jeffrey Lee's avatar
Jeffrey Lee committed
3067 3068
        STR     r0, PMPLogOp_UnsafeMapIn
        STR     r0, PMPLogOp_GlobalTLBFlushNeeded
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086
        MOV     r1, #0
      [ PMPDebug
        DebugReg r3, "LogOp len "
      ]
05
        ; Examine the first entry and see if it's a request to map in or map out
        CMP     r3, #0
        BEQ     %FT70
06
        LDMIA   r2, {r4-r6}
      [ PMPDebug
        DebugReg r4, "DA page "
        DebugReg r5, "PMP page "
      ]
        ; Check for silly DA page index
        CMP     r4, r11, LSR #12
        BHS     %FT95
        CMP     r5, #-1
3087
        BNE     %FT20
3088 3089 3090 3091 3092
        ; Map out request - get current page
      [ PMPDebug
        DebugTX "-> Map out"
      ]
        ADD     r4, r12, r4, LSL #12
3093
        PTOp    logical_to_physical
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
        MOVCS   r5, #-1
        BCS     %FT10
        Push    "r3,r10-r11"
        BL      physical_to_ppn
        MOV     r5, r3
        Pull    "r3,r10-r11"
        BCS     %FT95 ; TODO better error
        ; Check to see if the page is cacheable
        LDR     r6, =ZeroPage
        LDR     r6, [r6, #CamEntriesPointer]
        ADD     r6, r6, #CAM_PageFlags
        LDR     r6, [r6, r5, LSL #CAM_EntrySizeLog2]
        TST     r6, #DynAreaFlags_NotCacheable
        ADDEQ   r0, r0, #1
10
      [ PMPDebug
        DebugReg r5, "Current phys page="
      ]
        LDR     r9, [r10, #DANode_PMPMaxSize] ; restore after logical_to_physical clobbered it
        ; Only add to list if we're mapping out
        CMP     r5, #-1
        STRNE   r5, [sp, r1, LSL #2]
        ADDNE   r1, r1, #1
15
        ADD     r2, r2, #12
        SUB     r3, r3, #1
        CMP     r1, #PMPLogOp_ChunkSize
        BNE     %BT05
        BL      LogOp_MapOut
        MOV     r0, #0
        MOV     r1, #0
        B       %BT05

3127
20
3128 3129 3130 3131 3132
        CMP     r1, #0
        BLNE    LogOp_MapOut
        ; Request to map in - examine CAM to see if the page is already at the
        ; requested location (with requested flags)
        CMP     r5, r9
Jeffrey Lee's avatar
Jeffrey Lee committed
3133
        BHS     %FT950
3134 3135 3136 3137
        LDR     r0, =ZeroPage
        LDR     r5, [r7, r5, LSL #2]
        LDR     r0, [r0, #CamEntriesPointer]
        CMP     r5, #-1
Jeffrey Lee's avatar
Jeffrey Lee committed
3138
        BEQ     %FT950
3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        ADD     r0, r0, r5, LSL #CAM_EntrySizeLog2
        LDMIA   r0, {r0, r1}
      [ PMPDebug
        DebugTX "-> Map in "
      ]
        ADD     r4, r12, r4, LSL #12
      [ PMPDebug
        DebugReg r4, "Log addr "
        DebugReg r5, "Desired phys page "
        DebugReg r6, "Desired flags "
        DebugReg r0, "Page currently at "
        DebugReg r1, "With flags "
      ]
3154
        LDR     lr, =DynAreaFlags_PMPLogOpAccessMask
3155 3156 3157 3158 3159 3160 3161
        AND     r6, r6, lr
        BIC     lr, r1, lr
        ORR     r6, r6, lr ; Preserve special flags (e.g. temp uncacheability, PMP membership)
        CMP     r0, r4
        CMPEQ   r6, r1
        BEQ     %FT65
        ; Page needs to be mapped/moved/updated
3162 3163 3164 3165 3166 3167 3168 3169
        ; Only allow Reserved pages to become Unavailable if we're given the magic password
        TST     r6, #PageFlags_Unavailable ; Unavailable requested?
        TSTNE   r1, #PageFlags_Reserved    ; And currently Reserved?
        BEQ     %FT55
        TST     r1, #PageFlags_Unavailable ; Was it already Unavailable?
        LDREQ   lr, [r2, #8]
        TSTEQ   lr, #PageFlags_Reserved    ; Or was Reserved specified in the page list?
        BNE     %FT55                      ; Then we're fine
Jeffrey Lee's avatar
Jeffrey Lee committed
3170
        ; Attempting to claim a Reserved page without the proper permission.
3171 3172 3173
        ; Try and swap in a replacement page.
        MOV     r0, r5
        BL      GetNonReservedPage
Jeffrey Lee's avatar
Jeffrey Lee committed
3174
        MOV     r0, #0                  ; Reinit LogOp_MapOut counters
3175
        MOV     r1, #0
Jeffrey Lee's avatar
Jeffrey Lee committed
3176
        BVC     %BT06                   ; Retry this entry
3177 3178
        B       %FT96
55
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
        Push    "r2-r4,r6,r9,r11"
        MOV     r2, r5
        ; Update our logical size if the page isn't already in position
        CMP     r0, r4
        BEQ     %FT60
        ORR     r6, r6, #PageFlags_Unsafe ; Do unsafe mapping if posible
        ; If the page is currently in our address space, decrease our logical size (-> that mapping is about to go away)
        LDR     r1, [r10, #DANode_Size]
        SUB     lr, r0, r12
        CMP     lr, r11
        SUBLO   r1, r1, #4096
        BICLO   r6, r6, #PageFlags_Unsafe
        ; If there's nothing at the target address, increase our logical size
3192
        PTOp    logical_to_physical
3193 3194 3195
        ADDCS   r1, r1, #4096
        BICCC   r6, r6, #PageFlags_Unsafe
        BCC     %FT57
Jeffrey Lee's avatar
Jeffrey Lee committed
3196
        STR     r1, [r10, #DANode_Size]
3197 3198 3199
      [ PMPDebug
        DebugTX "Nothing at dest addr"
      ]
3200
        ; Also update HWM
3201 3202 3203 3204 3205 3206 3207 3208
        LDR     r1, [r10, #DANode_SparseHWM]
        ADD     lr, r4, #4096
        CMP     r1, r4
        STRLS   lr, [r10, #DANode_SparseHWM]
        B       %FT60
57
        ; There's already a page at the target address. Unmap it before we
        ; replace it (BangCamUpdate isn't smart enough to do this for us)
Jeffrey Lee's avatar
Jeffrey Lee committed
3209
        MOV     r0, r10
Jeffrey Lee's avatar
Jeffrey Lee committed
3210
        Push    "r2,r4,r6,r7,r10"
3211 3212 3213 3214
        LDR     r2, =ZeroPage
        LDR     r7, [r2, #MaxCamEntry]
        BL      physical_to_ppn
        BCS     %FT94
Jeffrey Lee's avatar
Jeffrey Lee committed
3215
        STR     r1, [r0, #DANode_Size]
3216 3217 3218 3219 3220 3221 3222 3223
      [ PMPDebug
        DebugReg r3, "Unmapping existing page first "
      ]
        LDR     r11, [r2, #CamEntriesPointer]
        MOV     r2, r3
        ADD     r11, r11, r3, LSL #CAM_EntrySizeLog2
        LDR     r3, =Nowhere
        LDR     r11, [r11, #CAM_PageFlags] ; Preserve flags
Jeffrey Lee's avatar
Jeffrey Lee committed
3224
        ; We should be able to make this an unsafe op if the page isn't cacheable. But to avoid global TLB flushes when only one or two pages are being unmapped, only make it unsafe if we've already scheduled a global flush.
3225
        TST     r11, #DynAreaFlags_NotCacheable
Jeffrey Lee's avatar
Jeffrey Lee committed
3226
        LDRNE   r7, [sp, #:INDEX:PMPLogOp_GlobalTLBFlushNeeded + 6*4 + 5*4]
Jeffrey Lee's avatar
Jeffrey Lee committed
3227
        TEQNE   r7, #0
3228
        ORRNE   r11, r11, #PageFlags_Unsafe
3229
        PTOp    BangCamUpdate
Jeffrey Lee's avatar
Jeffrey Lee committed
3230
        Pull    "r2,r4,r6,r7,r10"
3231 3232 3233 3234 3235 3236 3237 3238
        ; If the above was unsafe, then it means the below can be unsafe too
        AND     r11, r11, #PageFlags_Unsafe
        ORR     r6, r6, r11
60
        ; Call BangCamUpdate
        MOV     r3, r4
        TST     r6, #PageFlags_Unsafe
        ORR     r11, r6, #DynAreaFlags_PMP
Jeffrey Lee's avatar
Jeffrey Lee committed
3239
        STRNE   pc, [sp, #:INDEX:PMPLogOp_UnsafeMapIn + 6*4]
3240 3241 3242
      [ PMPDebug
        DebugReg r11, "Actual flags "
      ]
3243
        PTOp    BangCamUpdate
3244 3245
        Pull    "r2-r4,r6,r9,r11"
65
Jeffrey Lee's avatar
Jeffrey Lee committed
3246
        MOV     r0, #0                  ; Reinit LogOp_MapOut counters
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
        MOV     r1, #0
        ADD     r2, r2, #12
        SUBS    r3, r3, #1
        BNE     %BT06
70
        MOV     r4, #0
71      ; R4 = error
        ; Store back progress
        FRAMSTR r2
        FRAMSTR r3
        ; Flush any pending changes
        CMP     r1, #0
        BLNE    LogOp_MapOut
      [ PMPDebug
        DebugReg r3, "# not processed "
Jeffrey Lee's avatar
Jeffrey Lee committed
3262 3263 3264 3265
        LDR     r3, PMPLogOp_GlobalTLBFlushNeeded
        DebugReg r3, "GlobalTLBFlushedNeeded? "
        LDR     r3, PMPLogOp_UnsafeMapIn
        DebugReg r3, "UnsafeMapIn? "
3266 3267 3268 3269
        LDR     r3, [r10, #DANode_Size]
        DebugReg r3, "Size "
      ]
        ; Perform any necessary post maintenance
Jeffrey Lee's avatar
Jeffrey Lee committed
3270
        LDR     r0, PMPLogOp_GlobalTLBFlushNeeded
3271 3272 3273 3274 3275
        TEQ     r0, #0
        BEQ     %FT72
        ARMop   MMU_ChangingUncached
        B       %FT75
72
3276
      [ SyncPageTables
Jeffrey Lee's avatar
Jeffrey Lee committed
3277
        LDR     r0, PMPLogOp_UnsafeMapIn
3278 3279
        TEQ     r0, #0
        BEQ     %FT75
3280
        PageTableSync
3281
      ]
Jeffrey Lee's avatar
Jeffrey Lee committed
3282
75
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
        ; Release CDASemaphore
        LDR     r1, =ZeroPage
        MOV     r2, #0
        STR     r2, [r1, #CDASemaphore]
      [ PMPParanoid
        BL      ValidatePMPs
      ]
        CLRV
        MOVS    r0, r4
 [ International
        BLNE    TranslateError
 |
        SETV    NE
 ]
        FRAMSTR r0,VS
        EXIT

90
        ADRL    r0, ErrorBlock_BadDynamicArea
        B       %FT92
91
        ADRL    r0, ErrorBlock_ChDynamNotAllMoved
92
 [ International
        BL      TranslateError
 |
        SETV
 ]
        FRAMSTR r0
        EXIT

94
      [ PMPDebug
        DebugTX "-> failed to find page which is currently mapped in"
      ]
Jeffrey Lee's avatar
Jeffrey Lee committed
3318
        Pull    "r2,r4,r6,r7,r10"
3319
        Pull    "r2-r4,r6,r9,r11"
Jeffrey Lee's avatar
Jeffrey Lee committed
3320 3321
950
        MOV     r1, #0                  ; Don't call LogOp_MapOut
3322 3323 3324 3325 3326 3327
95
      [ PMPDebug
        DebugTX "-> bad logop page number"
      ]
        ADRL    r4, ErrorBlock_BadPageNumber
        B       %BT71
3328 3329 3330 3331 3332 3333
96
      [ PMPDebug
        DebugTX "-> can't use page"
      ]
        ADRL    r4, ErrorBlock_CantGetPhysMem
        B       %BT71
3334

3335 3336 3337
; r10 -> DANode
DynArea_PMP_LogOp_WithNode ALTENTRY
        B       %BT01
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349

LogOp_MapOut ROUT
        Entry   "r0-r12"
        ADD     r3, sp, #14*4
        LDR     r12, =ZeroPage
      [ PMPDebug
        DebugReg r1, "Unmapping # pages "
        DebugReg r0, "With # cacheable "
      ]
        ; r0 = number of cacheable pages being unmapped
        ; r1 = number of pages being unmapped
        ; r3 -> list of physical pages to unmap
Jeffrey Lee's avatar
Jeffrey Lee committed
3350 3351 3352 3353 3354 3355
        ; r10 -> DANode
        ; First go through and make them all uncacheable
        CMP     r0, #0
        BEQ     %FT10

        ; Work out if a global cache flush makes sense
3356 3357
        MOV     r2, r1
        MOV     r11, r0, LSL #12
3358
        ARMop   Cache_RangeThreshold,,,r12
3359 3360
        CMP     r11, r0
        SUBLS   r11, r11, #1
Jeffrey Lee's avatar
Jeffrey Lee committed
3361 3362 3363 3364 3365 3366

        MOV     r7, r3
        LDR     r12, [r12, #CamEntriesPointer]
        LDR     r3, =Nowhere
        MOV     r9, #-1
05
3367
        LDR     r10, [r7], #4
Jeffrey Lee's avatar
Jeffrey Lee committed
3368
      [ PMPDebug
3369
        DebugReg r10, "Uncache page "
Jeffrey Lee's avatar
Jeffrey Lee committed
3370
      ]
3371
        ADD     r10, r12, r10, LSL #CAM_EntrySizeLog2
Jeffrey Lee's avatar
Jeffrey Lee committed
3372 3373
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
3374 3375
        LDMIA   r10, {r0, r6}
        TEQ     r0, r3                  ; Check something actually there (mainly to stop DANode_Size going out of sync if page list requests for same page to be unmapped multiple times)
Jeffrey Lee's avatar
Jeffrey Lee committed
3376
        BEQ     %FT08
3377
        TST     r6, #DynAreaFlags_NotCacheable
Jeffrey Lee's avatar
Jeffrey Lee committed
3378
        BNE     %FT08
3379 3380 3381
        PTOp    MakeTempUncache         ; Update the page table entry to make it uncacheable
        ; r11 bit 0 set if TLB+cache invalidation done on per-page basis
        TST     r11, #1
Jeffrey Lee's avatar
Jeffrey Lee committed
3382 3383
        BEQ     %FT07
        LDR     r4, =ZeroPage
3384
        ARMop   MMU_ChangingEntry,,,r4
Jeffrey Lee's avatar
Jeffrey Lee committed
3385
07
3386
        SUBS    r11, r11, #4096
Jeffrey Lee's avatar
Jeffrey Lee committed
3387 3388
        BLS     %FT09                   ; Can stop if that was the last cacheable page
08
3389
        SUBS    r2, r2, #1
Jeffrey Lee's avatar
Jeffrey Lee committed
3390 3391 3392 3393 3394
        BNE     %BT05

09
        ; Do global TLB+cache invalidate if required
        LDR     r12, =ZeroPage
3395
        TST     r11, #1
Jeffrey Lee's avatar
Jeffrey Lee committed
3396
        BNE     %FT10
3397
      [ PMPDebug
3398
        DebugTX "Global TLB+cache flush"
3399
      ]
3400
        ARMop   MMU_Changing,,,r12
Jeffrey Lee's avatar
Jeffrey Lee committed
3401

3402
10
Jeffrey Lee's avatar
Jeffrey Lee committed
3403 3404 3405 3406 3407
        FRAMLDR r1
        ADD     r7, sp, #14*4
        ; Work out if we should do a global TLB flush after the unmap
        MOV     r5, #0
        LDR     r4, [r7, #:INDEX:PMPLogOp_GlobalTLBFlushNeeded]
3408
        CMP     r4, #0
Jeffrey Lee's avatar
Jeffrey Lee committed
3409
        BNE     %FT20
3410 3411
        CMP     r1, #32 ; Arbitrary TLB size
        BLO     %FT20
Jeffrey Lee's avatar
Jeffrey Lee committed
3412 3413 3414
        ; Global TLB/uncached flush wanted - make BangCAM unsafe, request global flush from main code
        MOV     r5, #PageFlags_Unsafe
        STR     pc, [r7, #:INDEX:PMPLogOp_GlobalTLBFlushNeeded]
3415 3416 3417 3418 3419
      [ PMPDebug
        DebugTX "Doing unsafe unmap"
      ]
20
        ; Now process the pages
Jeffrey Lee's avatar
Jeffrey Lee committed
3420 3421
        MOV     r8,r1
        FRAMLDR r10
3422 3423 3424 3425 3426 3427
        LDR     r12, [r12, #CamEntriesPointer]
        LDR     r3, =Nowhere
        LDR     r9, [r10, #DANode_Size]
25
        LDR     r2, [r7], #4
      [ PMPDebug
Jeffrey Lee's avatar
Jeffrey Lee committed
3428
        DebugReg r2, "Umap page "
3429 3430 3431 3432 3433 3434 3435 3436
      ]
        ADD     r11, r12, r2, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        LDMIA   r11, {r11, lr}
        TEQ     r11, r3                 ; Check something actually there (mainly to stop DANode_Size going out of sync if page list requests for same page to be unmapped multiple times)
        ORR     r11, lr, r5             ; Retain current flags
        SUBNE   r9, r9, #4096
3437
        PTOpNE  BangCamUpdate
3438 3439 3440 3441 3442
        SUBS    r8, r8, #1
        BNE     %BT25
        STR     r9, [r10, #DANode_Size]
        EXIT

3443 3444
        LTORG

3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
;**************************************************************************
;
;       DynArea_PMP_Resize
;
;  Physical resize of PMP - increase/decrease number of physical pages supported
;
; in:   r0 = reason code (23)
;       r1 = area number
;       r2 = resize amount (positive/negative page count)
;
; out:  r0-r1 preserved (error if not all of region successfully updated)
;       r2 = amount area has changed by (unsigned page count)
;       r10-r12 may be corrupted
;
DynArea_PMP_Resize ROUT
        Entry   "r0-r5"
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber        ; check area exists
  |
        BL      CheckAreaNumber         ; check area exists
  ]
        BCC     %FT90                   ; [it doesn't]
3467
01
3468
        ; r10 -> DANode
Jeffrey Lee's avatar
Jeffrey Lee committed
3469 3470 3471
        LDR     lr, [r10, #DANode_Flags]
        TST     lr, #DynAreaFlags_PMP
        BEQ     %FT90
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
      [ PMPDebug
        DebugReg r2, "Resize by: "
      ]
        ; Check CDASemaphore can be claimed, and then release it - we only make
        ; one call to external code so don't have too much to worry about
        ; (things can still go wrong though - e.g. if PMP resize triggers
        ; sys heap resize which then does something silly in
        ; Service_MemoryMoved)
        BL      ClaimCDASemaphore
        BNE     %FT91
        LDR     r3, =ZeroPage
        MOV     r4, #0
        STR     r4, [r3, #CDASemaphore]
        MOVS    r3, r2
        LDR     r5, [r10, #DANode_PMPMaxSize]
        MOVLT   r4, r2
        LDR     r2, [r10, #DANode_PMP]
        BLT     %FT50
        ; Grow by requested amount
        LDR     r4, =ZeroPage
        LDR     r4, [r4, #MaxCamEntry]
        SUB     r4, r4, r5
        ADD     r4, r4, #1              ; Max grow amount
        CMP     r3, r4
        MOVHI   r3, r4
        MOVS    r3, r3, LSL #2          ; -> byte count
        BEQ     %FT99                   ; Get rid of zero-change case
      [ PMPDebug
        DebugReg r3, "Clamped grow bytes: "
        DebugReg r2, "Current PMP: "
        DebugReg r5, "Current PMPMaxSize: "
      ]
        TEQ     r2, #0
        MOVEQ   r0, #HeapReason_Get
        MOVNE   r0, #HeapReason_ExtendBlock
        BL      DoSysHeapOpWithExtension
        BVS     %FT90
      [ PMPDebug
        DebugReg r2, "New PMP: "
      ]
        MOV     r3, r3, LSR #2
      [ PMPDebug
        DebugReg r3, "Return value: "
      ]
        FRAMSTR r3,,r2
        ; Success - initialise new space
        MOV     r4, #-1
10
        SUBS    r3, r3, #1
        STRGE   r4, [r2, r5, LSL #2]
        ADDGE   r5, r5, #1
        BGT     %BT10
        ; Store new details
        STR     r2, [r10, #DANode_PMP]
        STR     r5, [r10, #DANode_PMPMaxSize]
      [ PMPParanoid
        BL      ValidatePMPs
      ]
        EXIT

50
        ; Shrink request. Only shrink if there aren't any pages allocated to
        ; the end of the PMP.
        CMN     r5, r4
        RSBLO   r4, r5, #0              ; Can't shrink more than PMP size
      [ PMPDebug
        DebugReg r4, "Clamped shrink amount: "
      ]
        MOVS    r3, r4, LSL #2
        BEQ     %FT99                   ; No change to make
        ADD     r1, r2, r5, LSL #2
60
        LDR     lr, [r1, #-4]!
        CMP     lr, #-1
        BNE     %FT45
        ADDS    r4, r4, #1
        SUB     r5, r5, #1
        BNE     %BT60
45
        ; r5 = new size
        ; r4 = amount not changed (negative)
        ; r2 = PMP block
      [ PMPDebug
        DebugReg r5, "New size: "
        DebugReg r4, "Not changed: "
        DebugReg r2, "Current PMP: "
      ]
        SUB     r3, r3, r4, LSL #2      ; Calculate new shrink amount
        RSB     r4, r3, #0
        CMP     r5, #0
        MOV     r4, r4, LSR #2          ; Calculate return value
        MOVEQ   r0, #HeapReason_Free
        MOVNE   r0, #HeapReason_ExtendBlock
        BL      DoSysHeapOpWithExtension
        BVS     %FT91
        ; Operation success
        TEQ     r5, #0
        STR     r5, [r10, #DANode_PMPMaxSize]
        MOVEQ   r2, #0
      [ PMPDebug
        DebugReg r2, "New PMP: "
        DebugReg r4, "Return value: "
      ]
        STR     r2, [r10, #DANode_PMP]  ; Paranoia - store back even if we still have a size (just in case ExtendBlock shrink ops gain the ability to move blocks)
        FRAMSTR r4,,r2
      [ PMPParanoid
        BL      ValidatePMPs
      ]
        EXIT

90
        ADRL    r0, ErrorBlock_BadDynamicArea
        B       %FT92
91
        ADRL    r0, ErrorBlock_ChDynamNotAllMoved
92
        ; Failed for some reason.
        ; Note that we don't do anything fancy for grow failures (like try and
        ; do a partial grow) - a grow failure most likely indicates we're low
        ; on system heap space, which is a bad situation to be in.
 [ International
        BL      TranslateError
 |
        SETV
 ]
3597 3598
        FRAMSTR r0
98
3599 3600 3601
      [ PMPParanoid
        BL      ValidatePMPs
      ]
3602
        PullEnv
3603
        MOV     r2, #0
3604
        MOV     pc, lr
3605 3606 3607

99
        CLRV
3608
        B       %BT98
3609

3610 3611 3612
; r10 already contains node ptr
DynArea_PMP_Resize_WithNode ALTENTRY
        B       %BT01
3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678

;**************************************************************************
;
;       DynArea_PMP_GetInfo - Get info on a physical memory pool
;
;       Internal routine called by DynamicAreaSWI
;
;       Although designed for use with PMPs, this call works with regular DAs
;       too (just returns logical page counts for r6 & r7)
;
; in:   r0 = reason code (24)
;       r1 = area number
;
; out:  r2 = current logical size of area (bytes)
;       r3 = base logical address
;       r4 = area flags
;       r5 = maximum logical size of area (bytes)
;       r6 = current physical size of area (pages)
;       r7 = maximum physical size of area (pages)
;       r8 -> title string
;       r10-r12 may be corrupted
;       All other registers preserved
;

DynArea_PMP_GetInfo ROUT
        Entry
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber        ; check area exists
  |
        BL      CheckAreaNumber         ; check area exists
  ]
        BCC     %FT90                   ; [it doesn't]

; r10 -> node, so get info

        LDR     r4, [r10, #DANode_Flags]
        LDR     r2, [r10, #DANode_Size]
        TST     r4, #DynAreaFlags_PMP
        LDR     r3, [r10, #DANode_Base]
        LDR     r5, [r10, #DANode_MaxSize]
        LDRNE   r6, [r10, #DANode_PMPSize]
        LDRNE   r7, [r10, #DANode_PMPMaxSize]
        MOVEQ   r6, r2, LSR #12
        MOVEQ   r7, r5, LSR #12
        LDR     r8, [r10, #DANode_Title]
        CLRV
        EXIT

90
        ADRL    r0, ErrorBlock_BadDynamicArea
 [ International
        BL      TranslateError
 |
        SETV
 ]
        EXIT

;**************************************************************************
;
;       DynArea_PMP_GetPages - Get page mapping info on a physical memory pool
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (25)
;       r1 = area number
;       r2 = pointer to input/output array:
3679 3680 3681 3682
;            +0: PMP page index
;            +4: phys page number
;            +8: DA page index
;            +12: page flags
3683 3684 3685 3686 3687 3688 3689
;       r3 = number of entries
;
; out:  r0-r3 preserved
;       r10-r12 may be corrupted
;       All other registers preserved
;       Array updated with page details
;
3690 3691 3692 3693 3694 3695 3696 3697 3698 3699
; On entry, for each array entry either the PMP page index, phys page number, or
; DA page index must be provided, with the other indices set to -1 (page flags
; are ignored).
;
; On exit, if the page is a member of the PMP, the entries will be filled in as
; appropriate. If the page isn't mapped in (and it was a lookup by PMP page
; index/phys page number) the DA page index will be set to -1. If no physical
; page is allocated (or the page isn't a member of the PMP) the page flags will
; be set to 0.
;
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709

DynArea_PMP_GetPages ROUT
        Entry   "r0-r9"
  [ DynArea_QuickHandles
        BL      QCheckAreaNumber        ; check area exists
  |
        BL      CheckAreaNumber         ; check area exists
  ]
        BCC     %FT90                   ; [it doesn't]
        ; r10 -> DANode
Jeffrey Lee's avatar
Jeffrey Lee committed
3710 3711
        LDR     lr, [r10, #DANode_Flags]
        TST     lr, #DynAreaFlags_PMP
3712
        BEQ     %FT90
Jeffrey Lee's avatar
Jeffrey Lee committed
3713
        LDR     r6, [r10, #DANode_PMP]
3714
        LDR     r9, [r10, #DANode_PMPMaxSize]
3715
        LDR     r11, =ZeroPage
3716
        LDR     r5, [r10, #DANode_Base]
3717 3718 3719
        LDR     r7, [r11, #MaxCamEntry]
        LDR     r11, [r11, #CamEntriesPointer]
        LDR     r12, =DynAreaFlags_PMPLogOpAccessMask
3720 3721 3722 3723
        ; Usage in main loop:
        ; r2 -> input page list
        ; r3 = length
        ; r5 -> DA base
3724 3725
        ; r6 -> PMP
        ; r7 = MaxCamEntry
3726 3727
        ; r9 = PMP size
        ; r10 -> DANode
3728 3729 3730
        ; r11 -> CAM
        ; r12 = DynAreaFlags_PMPLogOpAccessMask
        ; r0, r1, r4 temp
3731 3732 3733
10
        SUBS    r3, r3, #1
        BLT     %FT80
3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746
        ; Get the entry
        LDMIA   r2, {r0, r1, r4}
        ; PMP page provided?
        CMP     r0, #-1
        BNE     %FT50
        ; Phys page provided?
        CMP     r1, #-1
        BNE     %FT20
        ; DA page provided
        ; n.b. skipping any range check here since it won't hurt if the page
        ; doesn't belong to us
        Push    "r3, r5, r9-r11"
        ADD     r4, r5, r4, LSL #12
3747
        PTOp    logical_to_physical
3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
        BLCC    physical_to_ppn
        MOV     r0, r3
        Pull    "r3, r5, r9-r11"
        BCS     %FT15
        ; r0 = PPN, check to see if it belongs to us
        ADD     r1, r11, r0, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIB   r1, {r1, r4, lr}
        TST     r1, #DynAreaFlags_PMP
        BEQ     %FT15
        CMP     r4, r10
        BNE     %FT15
        STR     lr, [r2], #4            ; Store PMP page index
        AND     r1, r1, r12
        STR     r0, [r2], #8            ; Store phys page number, skip DA page index
        STR     r1, [r2], #4            ; Store page flags
        B       %BT10
15
        ; Bad DA page index
        ; PMP page index & phys page number are already known to be -1, so just
        ; store flags
        MOV     r0, #0
        STR     r0, [r2, #12]
        ADD     r2, r2, #16
        B       %BT10

20
        ; Check for silly phys page number
        CMP     r1, r7
        BHI     %FT91
        ADD     r1, r11, r1, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIA   r1, {r0, r1, r4, lr}
        TST     r1, #DynAreaFlags_PMP
        BEQ     %FT25
        CMP     r4, r10
        BNE     %FT25
        LDR     r4, =Nowhere
        STR     lr, [r2], #8            ; Store PMP page index, skip phys page number
        TEQ     r0, r4
        B       %FT55

25
        ; Bad phys page number
        ; PMP page index known to be -1, so store DA page index + flags
        ADD     r2, r2, #8
        MOV     r0, #-1
        MOV     r1, #0
        STMIA   r2!, {r0-r1}
        B       %BT10

50
3805
        ; Check for silly PMP page index
3806
        CMP     r0, r9
3807 3808
        BHS     %FT91
        ; Look up the page that's currently in the PMP
3809 3810 3811
        LDR     r0, [r6, r0, LSL #2]
        ADD     r2, r2, #4
        STR     r0, [r2], #4            ; Store phys page number
3812 3813
        ; Does the page exist?
        CMP     r0, #-1
3814 3815 3816
        LDR     r4, =Nowhere
        ADDNE   r0, r11, r0, LSL #CAM_EntrySizeLog2
        MOVEQ   r1, #0                  ; No physical page, so no flags
3817 3818 3819
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        LDMNEIA r0, {r0-r1}             ; Get log addr, flags from CAM
3820 3821
        TEQNE   r0, r4
55
3822 3823 3824
        MOVEQ   r0, #-1                 ; No physical page, or not mapped
        SUBNE   r0, r0, r5
        MOVNE   r0, r0, LSR #12
3825 3826
        AND     r1, r1, r12             ; Mask returned flags
        STMIA   r2!, {r0-r1}            ; Store DA page index, flags
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843
        B       %BT10

80
        CLRV
        EXIT

90
        ADRL    r0, ErrorBlock_BadDynamicArea
        B       %FT98
91
        ADRL    r0, ErrorBlock_BadPageNumber
98
 [ International
        BL      TranslateError
 |
        SETV
 ]
3844
        FRAMSTR r0
3845 3846 3847
        EXIT


3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
;**************************************************************************
;
;       DynArea_AplSpaceLimit - Read/Adjust application space limit
;
;       Internal routine called by DynamicAreaSWI
;
; in:   r0 = reason code (26)
;       r1 = new application space limit, or 0 to read current limit
;
; out:  r1 = previous application space limit
;       r10-r12 may be corrupted
;       All other registers preserved
;
; Note: Limit value is the (exclusive) upper address, i.e. size + &8000
;

DynArea_AplSpaceLimit ROUT
        LDR     r10, =ZeroPage
        CMP     r1, #0
        LDR     r11, [r10, #SoftAplWorkMaxSize]
        MOVEQ   r1, r11
        CMPNE   r1, r11
        MOVEQ   pc, lr ; Early exit if reading current value, or no change
        Entry   "r2-r5"
        MOVS    r2, r1, LSL #32-Log2PageSize
        BNE     %FT90                   ; Must be page size multiple
        CMP     r1, #&8000+PageSize     ; Assume size of zero will break things
        BLO     %FT90
        CMP     r1, #AplWorkMaxSize     ; And can't be larger than the hard limit
        BHI     %FT90
        LDR     r12, [r10, #AMBControl_ws]
        ADR     r4, AMBAnchorNode
        CMP     r1, r11
        BLO     %FT50
        ; Increasing limit - check dynamic areas
        ;
        ; DANode_List is sorted in address order, so we only need to check the
        ; first entry (being careful to avoid entries with no logical mapping,
        ; like the free pool)
        LDR     r2, [r10, #DAList]
40
        LDR     r3, [r2, #DANode_MaxSize]
        CMP     r3, #0
        BNE     %FT41
        LDR     r2, [r2, #DANode_Link]
        CMP     r2, #0
        BNE     %BT40
        B       %FT80                   ; No dynamic areas? Well, if you say so
41
        LDR     r5, [r2, #DANode_Flags]
        TST     r5, #DynAreaFlags_DoublyMapped
        LDR     r2, [r2, #DANode_Base]
        SUBNE   r2, r2, r3              ; Get true base of doubly-mapped area
        CMP     r2, r1
        BLO     %FT89
        B       %FT80

50
        ; Decreasing limit - check both AMB and non-AMB application space
        LDR     r2, [r10, #AplWorkSize]
        CMP     r1, r2
        BLO     %FT89
        MOV     r3, r4
        SUB     r2, r1, #ApplicationStart
55
        ; Use the PMP size to determine the size (DANode_Size will be zero
        ; for unmapped AMBNodes). This check will need changing if we allow
        ; programs to sparsely map application space.
        ;
        ; Note: For AMBControl, PMPSize should match PMPMaxSize. But we're
        ; checking PMPMaxSize just in case something's gone wrong.
        LDR     r5, [r3, #AMBNode_DANode+DANode_PMPMaxSize]
        CMP     r5, r2, LSR #Log2PageSize
        BHI     %FT89
        LDR     r3, [r3, #AMBNode_next]
        CMP     r3, r4
        BNE     %BT55
        ; Fall through

80
        ; Limit is good - update DANode_AppSpace + AMBControl nodes
        STR     r1, [r10, #SoftAplWorkMaxSize]
        MOV     r3, r4
        SUB     r2, r1, #ApplicationStart
85
        STR     r2, [r3, #AMBNode_DANode+DANode_MaxSize]
        LDR     r3, [r3, #AMBNode_next]
        CMP     r3, r4
        BNE     %BT85
        MOV     r1, r11
        EXIT

89
        ; Overlaps something
        ADRL    r0, ErrorBlock_OverlappingAreas
        B       %FT91
90
        ; Address is outside hard limits
        ADRL    r0, ErrorBlock_BadParameters
91
 [ International
        BL      TranslateError
 |
        SETV
 ]
        EXIT

3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070
;**************************************************************************
;
;       CheckAreaNumber - Try to find area with number r1
;
;       Internal routine called by DynArea_Create
;
; in:   r1 = area number to match
; out:  If match, then
;         C=1, r10 -> node, r11 -> previous node
;       else
;         C=0, r10,r11 corrupted
;       endif

CheckAreaNumber Entry
  [ DynArea_QuickHandles
QCheckAreaNumber_nonQ
  ]
        LDR     r10, =ZeroPage+DAList
        ASSERT  DANode_Link = 0                 ; because DAList has only link
10
        MOV     r11, r10                        ; save prev
        LDR     r10, [r10, #DANode_Link]        ; and load next
        CMP     r10, #1                         ; any more nodes?
        EXIT    CC                              ; no, then no match
        LDR     lr, [r10, #DANode_Number]       ; get number
        CMP     lr, r1                          ; does number match
        BNE     %BT10                           ; no, try next
  [ DynArea_QuickHandles
        Push    "r11"
        LDR     r11, =ZeroPage
        LDR     r11, [r11, #DynArea_ws]
        STR     r1,  DynArea_LastTreacleHandle
        STR     r10, DynArea_LastTreaclePtr
        Pull    "r11"
  ]
        EXIT                                    ; (C=1 from CMP lr,r1)

  [ DynArea_QuickHandles

; QCheckAreaNumber - similar to CheckAreaNumber, but blisteringly quick for system or for quick
;                    numbers. However, DOES NOT return previous node in r11.
;
QCheckAreaNumber ROUT
        Push    "lr"
        LDR     r11, =ZeroPage
        LDR     r11, [r11, #DynArea_ws]
        CMP     r1, #-1
        BEQ     QCheckAreaNumber_nonQ           ;just to protect against -1 as a proposed number
        LDR     lr, DynArea_LastTreacleHandle
        CMP     lr, r1
        LDREQ   r10, DynArea_LastTreaclePtr
        EXIT    EQ                              ;found node is slow one we handled last time (carry set)
        LDR     lr, DynArea_CreatingHandle
        CMP     lr, r1
        LDREQ   r10, DynArea_CreatingPtr
        EXIT    EQ                              ;found node is one we're creating (carry set)
        CMP     r1, #ChangeDyn_MaxArea
        BLS     %FT10
        CMP     r1,#DynArea_NewAreas
        BLO     QCheckAreaNumber_nonQ
        SUB     lr, r1, #DynArea_NewAreas
        CMP     lr, #DynArea_NumQHandles
        BHS     QCheckAreaNumber_nonQ
;quick handle
        ADR     r10, DynArea_QHandleArray
        LDR     r10, [r10, lr, LSL #2]
        CMP     r10, #DynArea_NumQHandles
        MOVLS   r10, #0                         ;not a valid pointer
        CMP     r10, #1
        Pull    "pc"                            ;handle free, carry clear
                                                ;or word is ptr to node, carry set
;system handle
10
        ADR     r10, DynArea_SysQHandleArray
        LDR     r10, [r10, r1, LSL #2]
        CMP     r10, #1
        Pull    "pc"                            ;DA does not exist, carry clear
                                                ;or word is ptr to node, carry set
  ] ;DynArea_QuickHandles

;**************************************************************************
;
;       CheckForOverlappingAreas - Check that given area does not overlap any existing ones
;
;       Internal routine called by DynArea_Create
;
; in:   r3 = base address
;       r4 = area flags (NB if doubly mapped, then have to check both halves for overlap)
;       r5 = size (of each half in doubly mapped areas)
;
; out:  If this area overlaps with an existing one, then
;         r0 -> error
;         V=1
;       else
;         r0 preserved
;         V=0
;       endif
;

CheckForOverlappingAreas Entry "r0-r5"
        TST     r4, #DynAreaFlags_DoublyMapped          ; check if doubly mapped
        BEQ     %FT05                                   ; [not, so don't mangle]

        SUBS    r3, r3, r5                              ; move start address back
        BCC     %FT20                                   ; oh dear! - it went back to below 0
        MOVS    r5, r5, LSL #1                          ; and double size
        BCS     %FT20                                   ; if that wrapped then that's bad, too
05
        ADDS    r5, r5, r3                              ; r5 -> end +1
        BHI     %FT20                                   ; if CS, indicating wrap, and not EQ (ie just ending at 0), then bad

        LDR     lr, =ZeroPage
        LDR     r0, [lr, #IOAllocPtr]
        CMP     r5, r0                                  ; end must be below I/O space (allocated down from high memory)
        BHI     %FT20

Jeffrey Lee's avatar
Jeffrey Lee committed
4071 4072 4073 4074 4075
; check against application space
        LDR     r1, =ZeroPage
        LDR     r1, [r1, #SoftAplWorkMaxSize]
        CMP     r3, r1
        BHS     %FT30                                   ; start is beyond aplspace limit
4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107

20
        ADRL    r0, ErrorBlock_OverlappingAreas
 [ International
        BL      TranslateError
 |
        SETV
 ]
        STR     r0, [sp]
        EXIT

; Now, check against DAList

30
        LDR     lr, =ZeroPage+DAList
        ASSERT  DANode_Link = 0
40
        LDR     lr, [lr, #DANode_Link]
        CMP     lr, #0                                  ; if got to end of list (V=0)
        BEQ     %FT50                                   ; then exit saying OK
        LDR     r0, [lr, #DANode_Base]
        LDR     r1, [lr, #DANode_Flags]
        TST     r1, #DynAreaFlags_DoublyMapped
        LDR     r1, [lr, #DANode_MaxSize]
        SUBNE   r0, r0, r1                              ; if doubly mapped then move back
        MOVNE   r1, r1, LSL #1                          ; and double size
        ADD     r1, r1, r0                              ; r1 -> end
        CMP     r5, r0                                  ; if end of our area is <= start of dyn, then OK wrt dyn areas)
        BLS     %FT50
        CMP     r3, r1                                  ; if start of our area is >= end of dyn, then go onto next area
        BCS     %BT40
        B       %BT20                                   ; else it overlaps
Neil Turton's avatar
Neil Turton committed
4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131

50
        CLRV                                            ; OK exit
        EXIT

;**************************************************************************
;
;       AllocateAreaAddress - Find an area of logical space to use for this area
;
;       Internal routine called by DynArea_Create
;
; in:   r4 = area flags (NB if doubly mapped, we have to find space for both halves)
;       r5 = size (of each half in doubly mapped areas)
;
; out:  If successfully found an address, then
;         r0 preserved
;         r3 = logical address
;         V=0
;       else
;         r0 -> error
;         r3 preserved
;         V=1
;       endif

4132
AllocateAreaAddress Entry "r0-r2,r4-r7"
Neil Turton's avatar
Neil Turton committed
4133 4134 4135 4136 4137
        TST     r4, #DynAreaFlags_DoublyMapped          ; check if doubly mapped
        BEQ     %FT05                                   ; [not, so don't mangle]
        MOVS    r5, r5, LSL #1                          ; double size
        BCS     %FT90                                   ; if that wrapped then that's bad
05
Jeffrey Lee's avatar
Jeffrey Lee committed
4138 4139 4140 4141 4142 4143
        LDR     r3, =DynArea_NewAreasBase
        LDR     r1, =ZeroPage
        LDR     r1, [r1, #SoftAplWorkMaxSize]
        CMP     r1, r3
        MOVHI   r3, r1                                  ; r3 is our current attempt
        LDR     r1, =ZeroPage+DAList                    ; r1 is ptr into dyn areas list
Neil Turton's avatar
Neil Turton committed
4144 4145 4146
10
        ADDS    r7, r3, r5                              ; r7 is our end+1
        BHI     %FT90                                   ; if we wrapped (but not end+1=0) then we failed
Jeffrey Lee's avatar
Jeffrey Lee committed
4147
        LDR     lr, =ZeroPage
4148 4149 4150 4151
        LDR     r2, [lr, #IOAllocPtr]
        CMP     r7, r2
        BHI     %FT90                                   ; if we walked into IOspace (assumed higher than any DA space) then we failed
15
Jeffrey Lee's avatar
Jeffrey Lee committed
4152
        BL      GetNextRange                            ; get next range from DA list (r2=start, r6=end+1)
Neil Turton's avatar
Neil Turton committed
4153 4154 4155
        CMP     r7, r2                                  ; if end(ours) <= start(next) then this is OK
        BLS     %FT80                                   ; (note this also works when r2=-1)
        CMP     r3, r6                                  ; else if start(ours) >= end(next)
4156
        BCS     %BT15                                   ; then get another
Neil Turton's avatar
Neil Turton committed
4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
        MOV     r3, r6                                  ; else make start(ours) := end(next)
        B       %BT10                                   ; and go back for another try

; we've succeeded - just apply unbodge for doubly-mapped areas

80
        TST     r4, #DynAreaFlags_DoublyMapped          ; if doubly mapped
        MOVNE   r5, r5, LSR #1                          ; halve size again
        ADDNE   r3, r3, r5                              ; and advance base address to middle
        CLRV
        EXIT

90
        ADRL    r0, ErrorBlock_CantAllocateArea
  [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
4173 4174
  |
        SETV
Neil Turton's avatar
Neil Turton committed
4175 4176 4177 4178
  ]
        STR     r0, [sp]
        EXIT                                    ; say we can't do it

Jeffrey Lee's avatar
Jeffrey Lee committed
4179 4180
        LTORG

Neil Turton's avatar
Neil Turton committed
4181 4182 4183 4184 4185 4186
;**************************************************************************
;
;       GetNextRange - Get next lowest range from either fixed or dynamic list
;
;       Internal routine called by AllocateAreaAddress
;
Jeffrey Lee's avatar
Jeffrey Lee committed
4187
; in:   r1!0 -> next entry in dyn list
Neil Turton's avatar
Neil Turton committed
4188 4189 4190
;
; out:  r2 = next lowest area base (-1 if none)
;       r6 = end of that range (undefined if none)
Jeffrey Lee's avatar
Jeffrey Lee committed
4191
;       r1 updated to next one (except when r2=-1 on exit)
Neil Turton's avatar
Neil Turton committed
4192 4193
;

4194
GetNextRange Entry "r7,r8"
Neil Turton's avatar
Neil Turton committed
4195 4196 4197
        ASSERT  DANode_Link = 0
        LDR     r7, [r1, #DANode_Link]                  ; get next from dyn
        TEQ     r7, #0                                  ; if none
Jeffrey Lee's avatar
Jeffrey Lee committed
4198 4199
        MOVEQ   r2, #-1                                 ; then use addr -1
        EXIT    EQ
Neil Turton's avatar
Neil Turton committed
4200 4201 4202

        LDR     r8, [r7, #DANode_Flags]                 ; more double trouble
        TST     r8, #DynAreaFlags_DoublyMapped
Jeffrey Lee's avatar
Jeffrey Lee committed
4203
        LDR     r2, [r7, #DANode_Base]
Neil Turton's avatar
Neil Turton committed
4204
        LDR     lr, [r7, #DANode_MaxSize]
Jeffrey Lee's avatar
Jeffrey Lee committed
4205
        SUBNE   r2, r2, lr
Neil Turton's avatar
Neil Turton committed
4206
        MOVNE   lr, lr, LSL #1
Jeffrey Lee's avatar
Jeffrey Lee committed
4207 4208
        ADD     r6, lr, r2                              ; now r2 = start addr, r6 = end+1
        MOV     r1, r7
Neil Turton's avatar
Neil Turton committed
4209 4210
        EXIT

4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
;**************************************************************************

  [ ChocolateSysHeap
;
; CreateChocolateBlockArray
;
; entry: r2 = No. of blocks to be created in array (N)
;        r3 = size of each block in bytes (S, must be multiple of 4)
;
; exit:
;        r2 = address of chocolate block array (parent SysHeap block address)
;        array is initialised to all blocks free
;   OR   V set, r0=error pointer, if error (from OS_Heap)
;
; - A Chocolate block array is only suitable for blocks of single fixed size.
;   These blocks may be allocated and freed in any order, but never resized.
; - Allocating and freeing blocks from a Chocolate block array is much faster
;   than OS_Heap, and the cost of operations is independent of N.
; - A Chocolate block array is a single block in the SysHeap, and looks like this
;   (excluding the internal structure of OS_Heap blocks):
;
;  - array header (3 words):
;    - word 0  =  total no. of blocks in array (N)
;    - word 1  =  size of each block container (S+4)
;    - word 2  -> first free block container in array, or 0 if none free
;  - followed immediately by N block containers, each of form:
;    - container header (1 word):
;      - word 0 :  bits  0..30 = container id (C)
;                                C = (S+4)*I, where I is array index, 0..N-1
;                  bit  31     = 1 if block is free, 0 if block is in use
;    - followed immediately by the S/4 words of the block itself
;      - if the block is in use, these words are as defined by the client
;      - if the block is free, the first word is a pointer to the next free
;        block container (or 0 if end of free list), and the other words
;        are undefined
;
; - A Chocolate block array requires a SysHeap block of 3*4 + N*(S + 4) bytes
;
CreateChocolateBlockArray ROUT
        Push    "r0,r1,r3,r4,r5,lr"
        MOV     r5,r2                ;N
        ADD     r4,r3,#4             ;S+4
        MUL     r3,r5,r4
        ADD     r3,r3,#3*4
        BL      ClaimSysHeapNode
        STRVS   r0,[SP]
        BVS     %FT50
        STR     r5,[r2]
        STR     r4,[r2,#4]
        ADD     r1,r2,#3*4
        STR     r1,[r2,#8]
        MOV     lr,r5
        ADD     r0,r2,#3*4
        MOV     r1,#&80000000        ;free flag
10
        STR     r1,[r0]
        ADD     r3,r0,r4
        STR     r3,[r0,#4]
        ADD     r1,r1,r4
        SUBS    lr,lr,#1
        MOVNE   r0,r3
        BNE     %BT10
        MOV     r1,#0
        STR     r1,[r0,#4]           ;end of free list
50
        Pull    "r0,r1,r3,r4,r5,pc"

;
; ClaimChocolateBlock
;
; entry: r3 = address of parent ChocolateBlockArray (must be valid)
; exit:  r2 = address of allocated block
;        r3 = size of block
;  OR    V set, R0=error (no free blocks)
;
ClaimChocolateBlock ROUT
        Push    "r1,r4,lr"
4288
        MRS     r4,CPSR
Kevin Bracey's avatar
Kevin Bracey committed
4289
        ORR     r1,r4,#I32_bit
4290
        MSR     CPSR_c,r1         ;protect critical manipulation from interrupt re-entry
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
        LDR     r2,[r3,#8]        ;pick up block container at front of free list
        CMP     r2,#0
        BEQ     ClaimChocolateBlock_NoneFree
        LDR     r1,[r2]
        BIC     r1,r1,#&80000000  ;clear the free flag
        STR     r1,[r2]
        LDR     r1,[r2,#4]        ;next free block container
        STR     r1,[r3,#8]        ;put it at front
        ADD     r2,r2,#4          ;address of block
        LDR     r3,[r3,#4]
        SUB     r3,r3,#4          ;size of block
        BIC     r4,r4,#V_bit      ;return with V clear
4303
        MSR     CPSR_cf,r4        ;restore IRQ state
4304 4305 4306 4307 4308 4309
        Pull    "r1,r4,pc"
;
;DON'T even think about internationalisation - this exit route must be fast
ClaimChocolateBlock_NoneFree
        ADR     R0,ChocolateBlock_NFError
        ORR     r4,r4,#V_bit      ;return with V set
4310
        MSR     CPSR_cf,r4        ;restore IRQ state
4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322
        Pull    "r1,r4,pc"

;
; FreeChocolateBlock
;
; entry: r1 = address of parent ChocolateBlockArray (must be valid)
;        r2 = address of block to free (may be invalid)
; exit:  -
;   OR   V set, R0=error (not a ChocolateBlock), r1,r2 still preserved
;
FreeChocolateBlock ROUT
        Push    "r2,r3,r4,lr"
4323
        MRS     r4,CPSR
Kevin Bracey's avatar
Kevin Bracey committed
4324
        ORR     r3,r4,#I32_bit
4325
        MSR     CPSR_c,r3         ;protect critical manipulation from interrupt re-entry
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
        ADD     r3,r1,#12         ;r3 -> first block container
        SUB     r2,r2,#4          ;r2 -> container for block (if valid)
        CMP     r2,r3
        BLO     FreeChocolateBlock_NaffOff
        LDR     lr,[r1,#-4]       ;OS_Heap's size word (naughty!)
        ADD     lr,lr,r1
        CMP     r2,lr
        BHS     FreeChocolateBlock_NaffOff
        LDR     lr,[r2]           ;block container id
        TST     lr,#&80000000     ;free flag
        BNE     FreeChocolateBlock_NaffOff
        ADD     lr,lr,r3          ;lr := address of block container, from container id
        CMP     lr,r2
        BNE     FreeChocolateBlock_NaffOff
;
;we now believe caller is freeing a valid block, currently in use
;
        LDR     lr,[r2]
        ORR     lr,lr,#&80000000
        STR     lr,[r2]           ;set free flag in container id
        LDR     lr,[r1,#8]        ;current front of free list
        STR     lr,[r2,#4]        ;chain free list to block container we are freeing
        STR     r2,[r1,#8]        ;put freed block container at front
        BIC     r4,r4,#V_bit      ;return with V clear
4350
        MSR     CPSR_cf,r4        ;restore IRQ state
4351 4352 4353 4354 4355 4356
        Pull    "r2,r3,r4,pc"
;
;DON'T even think about internationalisation - this exit route must be fast
FreeChocolateBlock_NaffOff
        ADR     R0,ChocolateBlock_NOError
        ORR     r4,r4,#V_bit      ;return with V set
4357
        MSR     CPSR_cf,r4        ;restore IRQ state
4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
        Pull    "r2,r3,r4,pc"

;
;forget internationalisation - if these errors aren't dealt with silently,
;                              the kernel's stuffed anyway
ChocolateBlock_NFError
        DCD     0
        DCB     "Chocolate SysHeap claim failed",0
        ALIGN
ChocolateBlock_NOError
        DCD     0
        DCB     "not a Chocolate SysHeap block",0
        ALIGN

  ] ;ChocolateSysHeap

Neil Turton's avatar
Neil Turton committed
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384
;**************************************************************************
;
;       InitDynamicAreas - Initialise nodes for dynamic areas
;
;       It only initialises free pool, appspace and sysheap nodes
;       The other areas are created properly, after the screen area has been created (improperly)
;
; in:   -
; out:  -
;

4385
InitDynamicAreas Entry "r0-r12"
Jeffrey Lee's avatar
Jeffrey Lee committed
4386
        LDR     lr, =ZeroPage+AppSpaceDANode
4387
        ADRL    r0, InitAppSpaceTable
Neil Turton's avatar
Neil Turton committed
4388 4389 4390
        LDMIA   r0, {r0-r8}
        STMIA   lr, {r0-r8}

Jeffrey Lee's avatar
Jeffrey Lee committed
4391
        LDR     lr, =ZeroPage+FreePoolDANode
4392
        ADRL    r0, InitFreePoolTable
Neil Turton's avatar
Neil Turton committed
4393 4394 4395
        LDMIA   r0, {r0-r8}                     ; copy initial data into node
        STMIA   lr, {r0-r8}

4396 4397
; Initialise the system heap first - we need it to store the PMP for the free
; pool
Neil Turton's avatar
Neil Turton committed
4398 4399 4400 4401 4402 4403 4404 4405

        LDR     r0, =SysHeapStart
        LDR     r1, =magic_heap_descriptor
        MOV     r2, #Nil
        MOV     r3, #hpdsize
        MOV     r4, #32*1024 - (SysHeapStart-SysHeapChunkAddress)
        STMIA   r0, {r1-r4}

Jeffrey Lee's avatar
Jeffrey Lee committed
4406 4407
        LDR     r0, =ZeroPage                   ; initialise module list to empty
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
4408
        STR     r0, [r0, #Module_List]
Jeffrey Lee's avatar
Jeffrey Lee committed
4409 4410 4411 4412
      |
        MOV     lr, #0
        STR     lr, [r0, #Module_List]
      ]
Kevin Bracey's avatar
Kevin Bracey committed
4413

Jeffrey Lee's avatar
Jeffrey Lee committed
4414
        LDR     lr, =ZeroPage+SysHeapDANode     ; initialise system heap node
Jeffrey Lee's avatar
Jeffrey Lee committed
4415
        ADRL    r0, InitSysHeapTable
Neil Turton's avatar
Neil Turton committed
4416 4417
        LDMIA   r0, {r0-r8}
        STMIA   lr, {r0-r8}
Jeffrey Lee's avatar
Jeffrey Lee committed
4418
        LDR     r0, =ZeroPage
4419
 [ FreePoolAddress < SysHeapStart
Jeffrey Lee's avatar
Jeffrey Lee committed
4420
        LDR     lr, =ZeroPage+FreePoolDANode    ; cf comments above/below - what was old code? KJB
4421
 ]
Neil Turton's avatar
Neil Turton committed
4422 4423
        STR     lr, [r0, #DAList]               ; store pointer to 1st node on list (either free pool or sys heap)

4424
; TODO have asserts to check that this all fits OK
4425 4426 4427 4428 4429 4430 4431
  [ ChocolateSysHeap
        ASSERT  ChocolateCBBlocks = ChocolateBlockArrays +  0
        ASSERT  ChocolateSVBlocks = ChocolateBlockArrays +  4
        ASSERT  ChocolateTKBlocks = ChocolateBlockArrays +  8
        ASSERT  ChocolateMRBlocks = ChocolateBlockArrays + 12
        ASSERT  ChocolateMABlocks = ChocolateBlockArrays + 16
        ASSERT  ChocolateMSBlocks = ChocolateBlockArrays + 20
Jeffrey Lee's avatar
Jeffrey Lee committed
4432
        LDR     r1,=ZeroPage+ChocolateBlockArrays
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
        MOV     r2,#MaxChocolateCBBlocks
        MOV     r3,#3*4
        BL      CreateChocolateBlockArray       ; better not fail
        STR     r2,[r1,#0]
        MOV     r2,#MaxChocolateSVBlocks
        MOV     r3,#VecNodeSize
        BL      CreateChocolateBlockArray       ; better not fail
        STR     r2,[r1,#4]
        MOV     r2,#MaxChocolateTKBlocks
        MOV     r3,#TickNodeSize
        BL      CreateChocolateBlockArray       ; better not fail
        STR     r2,[r1,#8]
        MOV     r2,#MaxChocolateMRBlocks
        MOV     r3,#ROMModule_NodeSize
        BL      CreateChocolateBlockArray       ; better not fail
        STR     r2,[r1,#12]
        MOV     r2,#MaxChocolateMABlocks
        MOV     r3,#ModInfo + Incarnation_Postfix + 8
        BL      CreateChocolateBlockArray       ; better not fail
        STR     r2,[r1,#16]
        MOV     r2,#MaxChocolateMSBlocks
        MOV     r3,#ModSWINode_Size
        BL      CreateChocolateBlockArray       ; better not fail
        STR     r2,[r1,#20]

  ]

4460 4461 4462
  [ Oscli_HashedCommands
        MOV     r3,#4*(Oscli_MHashValMask+1)
        BL      ClaimSysHeapNode                ; better not fail
Jeffrey Lee's avatar
Jeffrey Lee committed
4463 4464
        LDR     r0,=ZeroPage
      [ ZeroPage = 0
4465
        STR     r0,[r0,#Oscli_CmdHashSum]
Jeffrey Lee's avatar
Jeffrey Lee committed
4466 4467 4468 4469
      |
        MOV     r3,#0
        STR     r3,[r0,#Oscli_CmdHashSum]
      ]
4470 4471
        STR     r2,[r0,#Oscli_CmdHashLists]
        MOV     r3,#Oscli_MHashValMask+1
Jeffrey Lee's avatar
Jeffrey Lee committed
4472 4473 4474
      [ ZeroPage <> 0
        MOV     r0, #0
      ]
4475 4476 4477 4478 4479 4480
DynArea_OHinit_loop
        STR     r0,[r2],#4
        SUBS    r3,r3,#1
        BNE     DynArea_OHinit_loop
  ]

4481 4482 4483 4484
  [ DynArea_QuickHandles

        LDR     r3, =DynArea_ws_size
        BL      ClaimSysHeapNode                    ; should not give error - kernel boot
4485 4486 4487
      [ PMPDebug
        DebugReg r2, "DynArea_ws="
      ]
Jeffrey Lee's avatar
Jeffrey Lee committed
4488
        LDR     r0, =ZeroPage
4489 4490 4491 4492
        STR     r2, [r0, #DynArea_ws]
        MOV     r11, r2
        LDR     r3, =DynArea_ws_size
        ADD     r3, r3, r2
Jeffrey Lee's avatar
Jeffrey Lee committed
4493 4494 4495
      [ ZeroPage <> 0
        MOV     r0, #0
      ]
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
DynArea_QHinit_loop1
        STR     r0, [r2], #4
        CMP     r2, r3
        BLO     DynArea_QHinit_loop1

        MOV     r1, #-1
        STR     r1, DynArea_CreatingHandle
        STR     r1, DynArea_LastTreacleHandle
        STR     r1, DynArea_LastEnumHandle
        STR     r1, DynArea_OD8Clamp1
        STR     r1, DynArea_OD8Clamp2
        MOV     r1, #&1000
        RSB     r1, r1, #0
        STR     r1, DynArea_OD8Clamp3               ; 4G - 4k

        ADR     r1, DynArea_QHandleArray            ; init all Qhandles as free, free list = 1..DynArea_NumQHandles
        MOV     r2, #1
        STR     r2, DynArea_FreeQHandles
DynArea_QHinit_loop2
        ADD     r2, r2, #1
        STR     r2, [r1], #4
        CMP     r2, #DynArea_NumQHandles
        BLO     DynArea_QHinit_loop2
        MOV     r2, #0
        STR     r2, [r1]                            ; 0 = end of free list

        MOV     r1, #DynArea_NewAreas-1
        ADD     r1, r1, #DynArea_NumQHandles        ; first non-quick DA number -1
        STR     r1, DynArea_TreacleGuess

Jeffrey Lee's avatar
Jeffrey Lee committed
4526
        LDR     r1, =ZeroPage+FreePoolDANode
4527
        STR     r1, DynArea_SortedList              ; initially, FreePool at front of sorted list,
Jeffrey Lee's avatar
Jeffrey Lee committed
4528
        LDR     r2, =ZeroPage+SysHeapDANode
4529 4530 4531 4532 4533
        STR     r2, [r1, #DANode_SortLink]          ; and SysHeap second...
        MOV     r1, #0
        STR     R1, [r2, #DANode_SortLink]          ; ...and last

        ADR     r1, DynArea_SysQHandleArray
Jeffrey Lee's avatar
Jeffrey Lee committed
4534
        LDR     r2, =ZeroPage+SysHeapDANode
4535
        STR     r2, [r1, #ChangeDyn_SysHeap:SHL:2]
Jeffrey Lee's avatar
Jeffrey Lee committed
4536
        LDR     r2, =ZeroPage+FreePoolDANode
4537 4538
        STR     r2, [r1, #ChangeDyn_FreePool:SHL:2]

4539 4540 4541 4542 4543 4544 4545 4546 4547 4548
; Initialise the address lookup table for the current DA's
; Assumes we have at least one DA to start with!
        LDR     r2, =ZeroPage+DAList
        LDR     r2, [r2]
DynArea_AddrLookup_loop
        BL      AddDAToAddrLookupTable
        LDR     r2, [r2, #DANode_Link]
        TEQ     r2, #0
        BNE     DynArea_AddrLookup_loop

4549
  |
Jeffrey Lee's avatar
Jeffrey Lee committed
4550
        ASSERT  ZeroPage = 0
4551 4552 4553 4554 4555
        MOV     r0, #0
        STR     r0, [r0, #DynArea_ws]

  ] ;DynArea_QuickHandles

Jeffrey Lee's avatar
Jeffrey Lee committed
4556 4557
        LDR     r0, =ZeroPage
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
4558
        STR     r0, [r0, #CDASemaphore]         ; clear CDASemaphore
Jeffrey Lee's avatar
Jeffrey Lee committed
4559 4560 4561 4562
      |
        MOV     r2, #0
        STR     r2, [r0, #CDASemaphore]         ; clear CDASemaphore
      ]
Neil Turton's avatar
Neil Turton committed
4563

4564
; Now that the system heap is initialised we can create a page list for the
4565 4566 4567
; free pool and start pushing the free pages into it. However it's highly
; unlikely that we'll be able to build the full page list without having to
; grow the system heap - for which we'd want the fast pages to be available.
4568 4569 4570 4571 4572 4573 4574 4575 4576

; So to cope with this we start by putting the fast pages into the page list,
; growing the system heap for every page we insert (a bit slow but reliable).
; After each grow we try to claim the memory needed for the full page list;
; on success we then switch to a different algorithm which fills the main page
; list.

        SUB     sp, sp, #4                      ; Store the initial list on the stack
        LDR     r5, =ZeroPage
Jeffrey Lee's avatar
Jeffrey Lee committed
4577
        LDR     r6, =ZeroPage+FreePoolDANode
4578 4579 4580 4581 4582 4583 4584 4585 4586
        STR     sp, [r6, #DANode_PMP]
        MOV     r0, #1
        STR     r0, [r6, #DANode_PMPMaxSize]
        MOV     r0, #0
        STR     r0, [r6, #DANode_PMPSize]
        MOV     r0, #-1
        STR     r0, [sp]

        LDR     r0, [r5, #InitUsedStart]
Jeffrey Lee's avatar
Jeffrey Lee committed
4587
        ADD     r0, r0, #DRAMOffset_FirstFixed - DRAMOffset_PageTables
4588
        MOV     r1, #0                          ; start of init block is always 32bit address
4589 4590 4591
        BL      PhysAddrToPageNo
        MOV     r7, r0                          ; r7 = page number of start of static chunk
        LDR     r0, [r5, #InitUsedEnd]
4592 4593
        MOV     r1, r0, LSR #20
        MOV     r0, r0, LSL #12
4594 4595 4596 4597 4598
        BL      PhysAddrToPageNo
        SUB     r8, r0, #1                      ; r8 = page number of last page in statics
        ADD     r9, r5, #PhysRamTable
        LDMIA   r9!, {r0, r10}                  ; get VRAM info
        MOV     r10, r10, LSR #12               ; r10 = current page number
4599 4600 4601 4602
        LDMIA   r9!, {r0, r11}                  ; get first regular RAM chunk
        SUB     r10, r10, #1                    ; set things up so the first call to NextFreePage will return the first page of the block
        MOV     r11, r11, LSR #12
        ADD     r11, r11, #1
4603 4604 4605 4606 4607 4608 4609 4610 4611 4612
        LDR     r4, [r5, #CamEntriesPointer]
10
        ; See if we have enough space
        LDR     r3, [r5, #MaxCamEntry]
        ADD     r3, r3, #1
        MOV     r3, r3, LSL #2
        Push    "r3"
        MOV     r0, #HeapReason_Desc
        BL      DoSysHeapOpWithExtension        ; HACK - check space before calling, to avoid crashing when the grow fails and tries to generate an error (vector table not initialised yet, so crashes when UKSWIV is invoked in order to call MessageTrans)
        Pull    "r3"
4613
        SUB     r2, r2, #4096                   ; Paranoia
4614 4615 4616 4617 4618
        CMP     r2, r3
        BLT     %FT20
        BL      ClaimSysHeapNode
        BVC     %FT40
20
4619 4620
        ; Find a page we can use to grow the system heap
        BL      NextFreePage                    ; n.b. no out-of-pages check
4621
        STR     r10, [sp]
4622
        LDR     lr, =AreaFlags_FreePool :AND: DynAreaFlags_AccessMask
4623 4624 4625 4626 4627 4628 4629 4630 4631 4632
        STR     lr, [r0, #CAM_PageFlags]
        STR     r6, [r0, #CAM_PMP]
        MOV     lr, #0
        STR     lr, [r0, #CAM_PMPIndex]
        MOV     lr, #1
        STR     lr, [r6, #DANode_PMPSize]
        ; Now grow the system heap by 4K. This had better consume the page!
        MOV     r0, #ChangeDyn_SysHeap
        MOV     r1, #4096
        SWI     XOS_ChangeDynamicArea
4633 4634
        B       %BT10

4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650
40
        ; We've successfully allocated the memory for the PMP - start filling
        ; it in. To ensure the pages are in the correct order we need to fill
        ; it from the last entry working backwards, but unfortunately we don't
        ; know exactly how many pages there are - so once we're done we'll have
        ; to shuffle the list down.
        MOV     r0, r3, LSR #2
      [ PMPDebug
        DebugReg r2, "FreePool PMP="
        DebugReg r0, "PMPMaxSize="
      ]
        STR     r0, [r6, #DANode_PMPMaxSize]
        ADD     r3, r2, r3                      ; -> end of list
        MOV     r1, r3
        STR     r2, [r6, #DANode_PMP]
45
4651 4652 4653 4654
        BL      NextFreePage
        CMP     r10, #-1
        STRNE   r10, [r3, #-4]!
        BNE     %BT45                           ; Keep going until we run out of pages
4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668
        ; Left with:
        ; r1 -> end of memory block
        ; r2 -> start of memory block
        ; r3 -> last used entry
        ; Calculate and store size
        SUB     r12, r1, r3
        MOV     r12, r12, LSR #2
        STR     r12, [r6, #DANode_PMPSize]
      [ PMPDebug
        DebugReg r12, "PMPSize="
      ]
        ; Shuffle everything down, and fill in the CAM entries (which we
        ; couldn't do earlier since we didn't know the final PMP indices)
        MOV     r12, #0
4669
        LDR     lr, =AreaFlags_FreePool :AND: DynAreaFlags_AccessMask
4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697
56
        CMP     r3, r1
        LDRNE   r0, [r3], #4
        STRNE   r0, [r2], #4
        ADDNE   r0, r4, r0, LSL #CAM_EntrySizeLog2
        STRNE   lr, [r0, #CAM_PageFlags]
        STRNE   r6, [r0, #CAM_PMP]
        STRNE   r12, [r0, #CAM_PMPIndex]
        ADDNE   r12, r12, #1
        BNE     %BT56
60
        ; Now r2 -> end of used portion
        ; Fill empty space with -1 (although, we could probably free the space - I don't think it's possible it will ever get used)
        MOV     r0, #-1
61
        CMP     r2, r1
        STRNE   r0, [r2], #4
        BNE     %BT61

        ; Free pool should now be ready for business

        ADD     sp, sp, #4
      [ PMPDebug
        DebugTX "InitDynamicAreas done"
      ]
      [ PMPParanoid
        BL      ValidatePMPs
      ]
Neil Turton's avatar
Neil Turton committed
4698 4699
        EXIT

4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776
;
; NextFreePage - Find next page to insert into the free pool on startup
;
; In:
;   r4 -> CAM
;   r7 = page number of start of static chunk
;   r8 = page number of end of static chunk
;   r9 -> next PhysRamTable entry
;   r10 = Current page number
;   r11 = Number of pages left in current chunk
; Out:
;   r0 -> CAM entry for page
;   r10 = Next free page in optimal order, -1 if no more pages
;   r9, r11 updated
;
; We have to move all free pages (ie ones not occupied by the static pages)
; into the free pool.
; By default, pages will get taken from the end of the free pool when other
; dynamic areas are initialised or grown. So make sure that the slowest RAM
; is at the start of the free pool and the fastest is at the end; this is the
; reverse of the order in PhysRamTable. Also, within each group of pages (i.e.
; PhysRamTable entry), we want the pages to be in decreasing physical address
; order - so that when they are moved to a DA they end up in increasing address
; order, leading to more optimal DMA transfer lists.
;
; Also note that the VRAM block is kept at the start of the free pool, mainly
; to match old behaviour (it's not clear whether moving it elsewhere will have
; any significant impact on the system - especially when you consider that
; shrinking screen memory will end up adding the pages to the end of the pool
; rather than the start).
;
; Over time this optimal ordering will be lost, so at a later date it might be
; nice to re-sort pages as they are added back into the free pool (and move the
; VRAM block to the end of PhysRamTable, so that it's in order fast RAM -> slow
; RAM -> fast DMA -> slow DMA -> VRAM, so that sorting by page number is
; all that's required to deal with both contiguity and desirability)
;
; In terms of this routine, we fill the free pool from the highest entry down,
; so we want the first page returned to be the lowest-numbered page from the
; first (non-VRAM) PhysRamTable entry.
;
NextFreePage    ROUT
        Entry
10
        SUBS    r11, r11, #1
        ADD     r10, r10, #1
        BEQ     %FT30
20
        CMP     r10, r7
        CMPHS   r8, r10
        BHS     %BT10                           ; page is in statics
        ; Check the CAM map to see if the page is already taken - this will detect the DMA regions, which aren't included in InitUsedStart/InitUsedEnd
        ADD     r0, r4, r10, LSL #CAM_EntrySizeLog2
        LDR     lr, [r0, #CAM_PageFlags]
        TST     lr, #PageFlags_Unavailable
        BNE     %BT10
        ; Page is good
        EXIT

30
        ; Advance to next block
        LDR     lr, =ZeroPage+PhysRamTable+8
        CMP     lr, r9                          ; if we've just processed the VRAM block, we're done
        BEQ     %FT90
        LDMIA   r9!, {r0, r11}                  ; else get next block
        MOVS    r11, r11, LSR #12               ; if no more blocks left...
        BNE     %BT20
        MOV     r10, #0
        MOV     r9, lr
        LDMDB   lr, {r0, r11}                   ; ...then process VRAM
        MOVS    r11, r11, LSR #12               ; And if no VRAM...
        BNE     %BT20
90                                              ; ...then we're done
        MOV     r10, #-1
        EXIT


4777
        LTORG
Neil Turton's avatar
Neil Turton committed
4778

Neil Turton's avatar
Neil Turton committed
4779
InitFreePoolTable
4780
 [ FreePoolAddress > SysHeapStart
Neil Turton's avatar
Neil Turton committed
4781
        &       0                               ; link: no more nodes on list
4782
 |
Jeffrey Lee's avatar
Jeffrey Lee committed
4783
        &       ZeroPage+SysHeapDANode
4784
 ]
4785 4786
        &       ChangeDyn_FreePool
        &       FreePoolAddress
4787
        &       AreaFlags_FreePool
Neil Turton's avatar
Neil Turton committed
4788 4789 4790 4791 4792 4793 4794
        &       0                               ; size will be updated later
        &       0                               ; max size is computed
        &       0                               ; no workspace needed
        &       0                               ; no handler needed
        &       FreePoolString                  ; title

InitSysHeapTable
4795
 [ FreePoolAddress > SysHeapStart
Jeffrey Lee's avatar
Jeffrey Lee committed
4796
        &       ZeroPage+FreePoolDANode         ; link -> free pool node, since FreePoolAddress > SysHeapStart
4797 4798 4799
 |
        &       0
 ]
Neil Turton's avatar
Neil Turton committed
4800 4801
        &       ChangeDyn_SysHeap
        &       SysHeapStart
4802
        &       AreaFlags_SysHeap
Neil Turton's avatar
Neil Turton committed
4803 4804 4805 4806 4807 4808 4809 4810 4811 4812
        &       32*1024-(SysHeapStart-SysHeapChunkAddress) ; size
        &       SysHeapMaxSize
        &       SysHeapStart                    ; workspace pointer -> base of heap
        &       DynAreaHandler_SysHeap          ; area handler
        &       SysHeapString                   ; title

InitAppSpaceTable
        &       0                               ; link: not on list
        &       ChangeDyn_AplSpace
        &       0                               ; base address
4813
        &       AreaFlags_AppSpace
ROOL's avatar
ROOL committed
4814
        &       32*1024                         ; true size will be set up later
Neil Turton's avatar
Neil Turton committed
4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827
        &       AplWorkMaxSize
        &       0                               ; no workspace needed
        &       0                               ; no handler needed
        &       AppSpaceString                  ; title

FreePoolString
        =       "Free pool", 0
AppSpaceString
        =       "Application space", 0
SysHeapString
        =       "System heap", 0
        ALIGN

4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846


;**************************************************************************
;
;       ClaimCDASemaphore - Claims CDASemaphore if possible
;
; out:  EQ -> CDASemaphore claimed
;       NE -> not claimed
;
ClaimCDASemaphore
        Entry   "r10"
        LDR     r10, =ZeroPage                  ; check we're not in an IRQ
        LDR     lr, [r10, #IRQsema]
        TEQ     lr, #0
        LDREQ   lr, [r10, #CDASemaphore]       ; now also check whether ChangeDynamicArea is already threaded
        TEQEQ   lr, #0
        STREQ   pc, [r10, #CDASemaphore]     ; store non-zero value in CDASemaphore, to indicate we're threaded
        EXIT

Neil Turton's avatar
Neil Turton committed
4847 4848
;**************************************************************************
;
4849 4850
;       ChangeDynamicSWI - implement OS_ChangeDynamicArea (change the
;                          size of a dynamic area)
Neil Turton's avatar
Neil Turton committed
4851 4852
;
; in:   r0 = area number
4853
;       r1 = size of change (in bytes, signed integer)
Neil Turton's avatar
Neil Turton committed
4854
;
4855 4856
; out:  r0   preserved
;       r1 = actual amount moved (in bytes, unsigned integer)
Neil Turton's avatar
Neil Turton committed
4857 4858
;

4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870
  [ DA_Batman
;OR, a special call is allowed for internal use:
;
; in:   r0 = ChangeDyn_Batman (-3)
;       r1 = size of change (in bytes, signed integer)
;       r2 -> pseudo DANode for this call - base of DA will be base of fragment to map/unmap,
;             magnitude of r1 will give size of fragment, sign of r1 is +ve for map -ve for unmap
  ]

ChangeDynamicSWI ROUT
        Push    "r0, r2-r9, r10, lr"

4871 4872
        FastCDA_ProfInit r3

4873
        BL      ClaimCDASemaphore
Neil Turton's avatar
Neil Turton committed
4874 4875 4876 4877 4878 4879
        BNE     failure_IRQgoing

 [ DebugCDA2
        DLINE   "Entering OS_ChangeDynamicArea (new code)"
 ]

4880 4881 4882 4883 4884 4885 4886
  [ DA_Batman
    ;check for special Batman call (which uses OS_ChangeDynamicArea to map or unmap a fragment of a sparse DA)
        CMP     r0, #ChangeDyn_Batcall
        MOVEQ   r10, r2                         ; Batman call passes a pseudo DANode in r2
        BEQ     CDA_handlechecked
  ]

Neil Turton's avatar
Neil Turton committed
4887 4888 4889 4890 4891
        Push    "r1"
        MOV     r1, r0
 [ DebugCDA2
        DREG    r1, "Checking list for area number "
 ]
4892 4893 4894 4895

  [ DynArea_QuickHandles
        BL      QCheckAreaNumber                ; check area number is on list
  |
Neil Turton's avatar
Neil Turton committed
4896
        BL      CheckAreaNumber                 ; check area number is on list
4897 4898
  ]

Neil Turton's avatar
Neil Turton committed
4899 4900 4901 4902 4903 4904 4905
        Pull    "r1"
        BCC     failure_IRQgoingClearSemaphore

 [ DebugCDA2
        DLINE   "Found entry on list"
 ]

4906 4907 4908 4909 4910 4911
  [ DA_Batman
        ;a sparse area is not allowed to do an ordinary grow or shrink
        ;
        LDR     r11, [r10, #DANode_Flags]
        TST     r11, #DynAreaFlags_SparseMap
        BNE     failure_IRQgoingClearSemaphore
4912 4913 4914
        CMP     r0, #ChangeDyn_FreePool         ; Free pool is handled here. Other PMPs call through to their resize handler.
        TSTNE   r11, #DynAreaFlags_PMP
        BNE     CDA_PMP
4915
  ]
4916 4917 4918
      [ PMPParanoid
        BL      ValidatePMPs
      ]
4919 4920 4921 4922

CDA_handlechecked

  [ DynArea_QuickHandles
Jeffrey Lee's avatar
Jeffrey Lee committed
4923
        LDR     r11, =ZeroPage
4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938
        LDR     r11, [r11, #DynArea_ws]
        LDR     r5, DynArea_OD6Signature
        CMP     r0, #ChangeDyn_MaxArea
        BLS     daq_cda_od6done
        CMP     r0, #&FFFFFFF0                  ;don't count special DA numbers, such as ChangeDyn_AplSpace
        BHI     daq_cda_od6done
        TST     r5, #&80000000                  ;if set, disables resize signature for this call
        STREQ   r5, DynArea_OD6PrevSignature
        STREQ   r0, DynArea_OD6Handle
        ORREQ   r5, r5, #4
daq_cda_od6done
        BIC     r5, r5, #&80000000              ;clear any disable
        STR     r5, DynArea_OD6Signature
  ]

Jeffrey Lee's avatar
Jeffrey Lee committed
4939
        LDR     r5, =ZeroPage
Neil Turton's avatar
Neil Turton committed
4940 4941 4942 4943 4944 4945 4946
        LDR     r5, [r5, #Page_Size]            ; r5 = page size throughout
        SUB     r12, r5, #1                     ; r12 = page mask
        ADD     r1, r1, r12
        BICS    r1, r1, r12
        BEQ     IssueServiceMemoryMoved         ; zero pages! (r0 = area number, r1 = size change (0))
        BPL     AreaGrow

4947
AreaShrink ROUT
Neil Turton's avatar
Neil Turton committed
4948 4949 4950 4951 4952 4953 4954
        RSB     r1, r1, #0                      ; make size change positive
 [ DebugCDA2
        DREG    r0, "Shrinking area ", cc
        DREG    r1, " by "
 ]
        MOV     r11, r10                        ; source is area
        CMP     r0, #ChangeDyn_FreePool         ; if source is free pool
4955 4956
        BEQ     ShrinkFreePoolToAppSpace        ; then dest is appspace
        LDR     r12, =ZeroPage+FreePoolDANode   ; else dest is free pool
Neil Turton's avatar
Neil Turton committed
4957

4958 4959
        ASSERT  DANode_PMPMaxSize = DANode_PMPSize +4
        ADD     r2, r12, #DANode_PMPSize
Neil Turton's avatar
Neil Turton committed
4960
        LDMIA   r2, {r2, r3}
4961 4962 4963 4964 4965 4966

      [ PMPDebug
        DebugReg r0,"Shrinking area "
        DebugReg r1,"by "
        DebugReg r2,"FreePool PMPSize "
      ]
Neil Turton's avatar
Neil Turton committed
4967 4968
        SUB     lr, r3, r2                      ; lr = amount dest could grow

4969 4970 4971 4972 4973 4974 4975
      [ ZeroPage = 0
        TEQ     r11, #AppSpaceDANode            ; check if src = appspace
      |
        LDR     r2, =ZeroPage+AppSpaceDANode
        TEQ     r11, r2                         ; check if src = appspace
      ]

Neil Turton's avatar
Neil Turton committed
4976
        LDR     r2, [r11, #DANode_Size]         ; amount src could shrink
4977
        SUBEQ   r2, r2, #&8000                  ; protect first 32K of app space because app space DANode is silly
4978 4979
        CMP     lr, r2, LSR #12
        MOVHI   lr, r2, LSR #12                 ; lr = min(amount dest could grow, amount src could shrink)
Neil Turton's avatar
Neil Turton committed
4980

4981 4982
        CMP     lr, r1, LSR #12
        BHS     %FT15
Neil Turton's avatar
Neil Turton committed
4983 4984 4985

; we can't move all that is required, so move smaller amount

4986 4987
        MOV     r1, lr, LSL #12                 ; move smaller amount

Neil Turton's avatar
Neil Turton committed
4988 4989 4990 4991 4992
        BL      GenNotAllMovedError
        SUB     lr, r5, #1                      ; lr = pagesize mask
        BICS    r1, r1, lr                      ; a pagesize multiple
        BEQ     IssueServiceMemoryMoved
15
Jeffrey Lee's avatar
Jeffrey Lee committed
4993
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
4994
        CMP     r11, #AppSpaceDANode            ; if src <> appspace
Jeffrey Lee's avatar
Jeffrey Lee committed
4995 4996 4997 4998
      |
        LDR     lr, =ZeroPage+AppSpaceDANode
        CMP     r11, lr                         ; if src <> appspace
      ]
Neil Turton's avatar
Neil Turton committed
4999 5000
        BNE     %FT17                           ; then don't call app
        Push    "r10"                           ; save -> to area we tried to shrink
5001
        RSB     r10, r1, #0                     ; Shrinking appspace, make the change amount negative again
Neil Turton's avatar
Neil Turton committed
5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014
        BL      CheckAppSpace
        Pull    "r10"
        BVS     ChangeDynError
17
        BL      CallPreShrink
        BVS     ChangeDynError                  ; (r10 still points to area we tried to shrink)
        CMP     r2, r1                          ; can we move as much as we wanted?
        MOVCS   r2, r1                          ; if not, then move lesser amount (r2 = amount we're moving)
        BLCC    GenNotAllMovedError             ; store error, but continue

        TEQ     r2, #0                          ; if can't move any pages
        BEQ     NoMemoryMoved                   ; then exit, issuing Service_MemoryMoved

5015
        BL      DoTheShrink                     ; Move all the pages around
Neil Turton's avatar
Neil Turton committed
5016

5017
        LDR     r4, =ZeroPage
Jeffrey Lee's avatar
Jeffrey Lee committed
5018
      [ ZeroPage = 0
5019
        STR     r4, [r4, #CDASemaphore]         ; OK to reenter now (we've done the damage)
Jeffrey Lee's avatar
Jeffrey Lee committed
5020 5021 5022 5023
      |
        MOV     lr, #0
        STR     lr, [r4, #CDASemaphore]
      ]
Neil Turton's avatar
Neil Turton committed
5024 5025 5026 5027 5028
        BL      CallPostShrink
        RSB     r1, r2, #0
        LDR     r0, [r11, #DANode_Number]       ; reload dynamic area number
        B       IssueServiceMemoryMoved

5029 5030 5031
GrowFreePoolFromAppSpace ROUT
        ; To reduce code complexity, we treat a grow of the free pool as a
        ; shrink of application space
5032 5033 5034
        GetAppSpaceDANode r10
        LDR     r0, [r10, #DANode_Flags]
        TST     r0, #DynAreaFlags_PMP
5035
        RSB     r1, r1, #0                      ; (AreaShrink assumes negative size on entry)
5036 5037
        MOVEQ   r0, #ChangeDyn_AplSpace
        BEQ     AreaShrink
5038 5039 5040 5041 5042 5043 5044 5045
        ; Shrink of PMP appspace
        ; Check that the app is happy (normally handled by AreaShrink)
        Push    "r10"
        MOV     r10, r1
        BL      CheckAppSpace
        Pull    "r10"
        BVS     ChangeDynError
        ; Call through to CDA_PMP to call the (AMBControl) DA handler
5046
        B       CDA_PMP
5047 5048 5049 5050 5051

ShrinkFreePoolToAppSpace ROUT
        ; To reduce code complexity, we treat a shrink of the free pool as a
        ; grow of application space
        MOV     r0, #ChangeDyn_AplSpace
5052
        GetAppSpaceDANode r10
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
        ; Emulate old behaviour, when this case used to be part of AreaShrink:
        ; * If app space doesn't have enough free space, clamp amount and
        ; generate NotAllMoved error (Regular AreaGrow logic will only do this
        ; if dest is free pool - grows of other areas have always failed
        ; outright if they're incapable of growing that big)
        ; * If shrinking more than current free pool size, attempt to shrink
        ; shrinkable DAs so that as much memory as possible can be moved to app
        ; space
        ; * Call CheckAppSpace (rule seems to be that if app space is touched,
        ; CheckAppSpace must be called, except when it's an explicit grow of
        ; app space)
        MOV     r12, r10                        ; required by e.g. TryToShrinkShrinkables
        ASSERT  DANode_MaxSize = DANode_Size +4
        ADD     r2, r10, #DANode_Size
        LDMIA   r2, {r2, r3}
        SUB     lr, r3, r2                      ; lr = amount dest could grow
 [ ShrinkableDAs
        CMP     r1, lr
        MOVHI   r1, lr
        BLHI    GenNotAllMovedError
        SUB     lr, r5, #1                      ; lr = pagesize mask
        BICS    r1, r1, lr                      ; a pagesize multiple
        BEQ     IssueServiceMemoryMoved
 ]

        LDR     r11, =ZeroPage+FreePoolDANode
 [ ShrinkableDAs
        LDR     r2, [r11, #DANode_PMPSize]      ; amount src could shrink
        CMP     r2, r1, LSR #12
        BLCC    TryToShrinkShrinkables
        BCS     %FT15                           ; [we can now do it all]

; we can't move all that is required, so move smaller amount

        MOV     r1, r2, LSL #12                 ; move smaller amount
 |
        LDR     r2, [r11, #DANode_PMPSize]      ; amount src could shrink
        CMP     r2, lr, LSR #12
        MOVCC   lr, r2, LSL #12                 ; lr = min(amount dest could grow, amount src could shrink)

        CMP     r1, lr
        BLS     %FT15

; we can't move all that is required, so move smaller amount

        MOV     r1, lr                          ; move smaller amount
 ]
        BL      GenNotAllMovedError
        SUB     lr, r5, #1                      ; lr = pagesize mask
        BICS    r1, r1, lr                      ; a pagesize multiple
        BEQ     IssueServiceMemoryMoved
15
        Push    "r10"                           ; save -> to area we tried to shrink
        MOV     r10, r1
        BL      CheckAppSpace
        Pull    "r10"
        BVS     ChangeDynError
5110 5111 5112
        LDR     r12, [r10, #DANode_Flags]
        TST     r12, #DynAreaFlags_PMP
        BNE     CDA_PMP                         ; Make sure PMP AppSpace goes via PMP handler
5113 5114
        ; Fall through...

5115
AreaGrow ROUT
Neil Turton's avatar
Neil Turton committed
5116 5117 5118 5119 5120 5121
 [ DebugCDA2
        DREG    r0, "Growing area ", cc
        DREG    r1, " by "
 ]
        MOV     r12, r10                        ; dest is area specified
        CMP     r0, #ChangeDyn_FreePool         ; if dest is free pool
5122 5123
        BEQ     GrowFreePoolFromAppSpace        ; then src is appspace
        LDR     r11, =ZeroPage+FreePoolDANode   ; else src is free pool (may later be free+apl)
Neil Turton's avatar
Neil Turton committed
5124 5125 5126 5127

        ASSERT  DANode_MaxSize = DANode_Size +4
        ADD     r2, r12, #DANode_Size
        LDMIA   r2, {r2, r3}
5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139

      [ PMPDebug
        DebugReg r0,"Growing area "
        DebugReg r1,"by "
        DebugReg r2,"dest size "
        DebugReg r3,"dest max size "
        Push    "r0"
        LDR     r0, [r11, #DANode_PMPSize]      ; amount src could shrink
        DebugReg r0, "FreePool PMPSize "
        Pull    "r0"
      ]

Neil Turton's avatar
Neil Turton committed
5140 5141 5142 5143 5144
        SUB     lr, r3, r2                      ; lr = amount dest could grow

 [ DebugCDA2
        DREG    lr, "Dest could grow by "
 ]
5145
        LDR     r2, [r11, #DANode_PMPSize]      ; amount src could shrink
Neil Turton's avatar
Neil Turton committed
5146 5147 5148 5149 5150

 [ DebugCDA2
        DREG    r2, "Src could shrink by "
 ]

5151 5152 5153 5154
        CMP     lr, r1                          ; if enough room in dest
        CMPHS   r2, r1, LSR #12                 ; and enough space in src
        MOVHS   r3, r1                          ; then can do full amount
        BHS     %FT65                           ; so skip this bit
Neil Turton's avatar
Neil Turton committed
5155 5156 5157

; we can't move all that is required
;
5158
; check if adding shrinkables or aplspace would allow us to succeed
5159
; if it does then adjust registers, else give error
Neil Turton's avatar
Neil Turton committed
5160 5161 5162 5163 5164
;

 [ DebugCDA2
        DLINE   "Can't move all required using just free pool"
 ]
5165
        B       %FT62
Neil Turton's avatar
Neil Turton committed
5166 5167 5168 5169 5170 5171 5172 5173 5174 5175

61
        BL      GenNotAllMovedError
        SUB     lr, r5, #1                      ; lr = pagesize mask
        BICS    r1, r1, lr                      ; a pagesize multiple
        BEQ     IssueServiceMemoryMoved
        MOV     r3, r1
        B       %FT65

62
Neil Turton's avatar
Neil Turton committed
5176

5177
 [ ShrinkableDAs
Neil Turton's avatar
Neil Turton committed
5178 5179 5180
; growing another area from free pool
; insert code here to check for shrinking shrinkable areas

5181 5182
        CMP     r1, lr                          ; if dest can't grow by this amount,
        BHI     %FT64                           ; we're definitely not doing anything
Neil Turton's avatar
Neil Turton committed
5183

5184
        CMP     r2, r1, LSR #12                 ; this should definitely set C=0 as required by TryToShrinkShrinkables
5185 5186 5187 5188
        Push    "r1,lr"
        MOVCC   r1, r1, LSR #12
        BLCC    TryToShrinkShrinkables
        Pull    "r1,lr"
5189 5190
        MOVCS   r3, r1                          ; if succeeded set r3 to number of bytes to do
        BCS     %FT65                           ; and do it
Neil Turton's avatar
Neil Turton committed
5191 5192 5193 5194 5195
64

; end of inserted code
 ]

5196
        GetAppSpaceDANode r4
5197
        EORS    r6, r4, r12                     ; only take from app space if dest isn't app space!
Jeffrey Lee's avatar
Jeffrey Lee committed
5198 5199 5200 5201 5202 5203 5204 5205 5206
        BEQ     %FT645
        LDR     r6, [r4, #DANode_Flags]
        TST     r6, #DynAreaFlags_PMP
        LDRNE   r6, [r4, #DANode_PMPSize]
        LDREQ   r6, [r4, #DANode_Size]          ; get current size of apl space
        SUBEQ   r6, r6, #&8000                  ; can't take away 0-&7FFF
        MOVEQ   r6, r6, LSR #12
        ADD     r3, r2, r6                      ; add on to amount we could remove from free pool (pages)
645
Neil Turton's avatar
Neil Turton committed
5207 5208 5209

 [ DebugCDA2
        DREG    r6, "Can get from app space an additional ", cc
Jeffrey Lee's avatar
Jeffrey Lee committed
5210
        DREG    r3, " pages making a total of ", cc
5211
        DLINE   " pages"
Neil Turton's avatar
Neil Turton committed
5212 5213
 ]

5214 5215 5216 5217
        CMP     lr, r1                          ; if not enough room in dest
        CMPHS   r3, r1, LSR #12                 ; or src still doesn't have enough
        MOVLO   r1, #0                          ; then don't move any
        BLO     %BT61                           ; and return error
Neil Turton's avatar
Neil Turton committed
5218

5219 5220 5221 5222 5223
        ; To reduce code complexity, first shrink application space into the free pool, then take the combined chunk from the free pool
        Push    "r0-r1,r3"
        LDR     r3, =ZeroPage
        MOV     r0, #0
        STR     r0, [r3, #CDASemaphore]         ; Allow nested call
5224 5225 5226
        SUB     r1, r1, r2, LSL #12             ; total amount minus free pool size = free pool size change needed
        MOV     r0, #ChangeDyn_FreePool
        SWI     XOS_ChangeDynamicArea           ; attempt to grow free pool (by shrinking appspace)
5227
        STR     pc, [r3, #CDASemaphore]         ; reclaim semaphore
5228 5229 5230 5231 5232 5233
        Pull    "r0-r1,r3"
        MOVVS   r1, #0                          ; if failed, report nothing moved
        BVS     %BT61                           ; and return error
        ; Check that the free pool did actually grow large enough
        ; (might have been a partial grow, or maybe something else claimed some
        ; memory while CDASemaphore was unlocked)
5234 5235 5236 5237 5238 5239
        LDR     r3, [r11, #DANode_PMPSize]
        CMP     r3, r1, LSR #12
        MOVLO   r1, #0
        BLO     %BT61

        MOV     r3, r1                          ; amount actually doing (bytes)
Neil Turton's avatar
Neil Turton committed
5240 5241
65

5242
        MOV     r7, r3                          ; set up r7 to be total amount
Neil Turton's avatar
Neil Turton committed
5243 5244 5245 5246 5247 5248 5249 5250 5251

 [ DebugCDA2
        DREG    r3, "Amount actually moving into area = "
        DREG    r7, "Amount coming from 1st src area = "
 ]

; now split up grow into bite-size chunks, and call DoTheGrow to do each one

        Push    "r3"                            ; save original total amount
5252

Neil Turton's avatar
Neil Turton committed
5253
        LDR     lr, [r12, #DANode_Flags]        ; could this area require particular physical pages at all?
5254
        TST     lr, #DynAreaFlags_NeedsSpecificPages+DynAreaFlags_NeedsDMA
5255
        TSTEQ   lr, #DynAreaFlags_PMP           ; detect batcall from PMP PhysOp
Neil Turton's avatar
Neil Turton committed
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270
        BNE     %FT70                           ; [yes it could, so do it in lumps]

        MOV     r1, #0                          ; no page block
        MOV     r2, r3, LSR #12                 ; number of pages to do
        BL      CallPreGrow
        LDRVS   r3, [sp]                        ; if error, haven't done any, so restore total as how much to do
        BVS     %FT95

        Push    "r3, r7"
        MOV     r2, r7, LSR #12
        BL      DoTheGrowNotSpecified
        Pull    "r3, r7"

        LDR     r3, [sp]                        ; restore total amount
        MOV     r1, #0                          ; indicate no page block (and ptr to semaphore)
Jeffrey Lee's avatar
Jeffrey Lee committed
5271
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
5272
        STR     r1, [r1, #CDASemaphore]         ; OK to reenter now (we've done the damage)
Jeffrey Lee's avatar
Jeffrey Lee committed
5273 5274 5275 5276
      |
        LDR     r2, =ZeroPage
        STR     r1, [r2, #CDASemaphore]         ; OK to reenter now (we've done the damage)
      ]
Neil Turton's avatar
Neil Turton committed
5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305
        MOV     r2, r3, LSR #12
        BL      CallPostGrow
        B       %FT80

70
        Push    "r3, r7"
        CMP     r7, #PageBlockChunk             ; only do 1 area, so do min(r7,page)
        MOVHI   r7, #PageBlockChunk
        MOV     r2, r7, LSR #12                 ; number of entries to fill in in page block
        BL      DoTheGrow
        Pull    "r3, r7"
        BVS     %FT95
        CMP     r7, #PageBlockChunk             ; if 1st area is more than 1 page
        SUBHI   r3, r3, #PageBlockChunk         ; then reduce total
        SUBHI   r7, r7, #PageBlockChunk         ; and partial amounts by 1 page and do it again
        BHI     %BT70

80
        Pull    "r3"                            ; restore total amount

        MOV     r1, r3
        LDR     r0, [r12, #DANode_Number]       ; reload dynamic area number
        B       IssueServiceMemoryMoved

95
        Pull    "r1"                            ; restore total amount
        SUB     r1, r1, r3                      ; subtract off amount left, to leave done amount
        B       ChangeDynErrorSomeMoved

5306
GenNotAllMovedError Entry "r0"
Neil Turton's avatar
Neil Turton committed
5307 5308 5309 5310 5311 5312 5313 5314 5315 5316
        ADRL    r0, ErrorBlock_ChDynamNotAllMoved
 [ International
        BL      TranslateError
 ]
        STR     r0, [sp, #2*4]          ; sp -> r0,lr, then stacked r0,r2-r9,r10,lr
        LDR     lr, [sp, #12*4]
        ORR     lr, lr, #V_bit
        STR     lr, [sp, #12*4]
        EXIT

Kevin Bracey's avatar
Kevin Bracey committed
5317 5318
        LTORG

Neil Turton's avatar
Neil Turton committed
5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343
ChangeDynError

; in:   r0 -> error
;       r10 -> area that we tried to shrink/grow

        MOV     r1, #0
ChangeDynErrorSomeMoved
        STR     r0, [sp]
        LDR     lr, [sp, #10*4]
        ORR     lr, lr, #V_bit
        STR     lr, [sp, #10*4]
        B       SomeMemoryMoved

NoMemoryMoved
        MOV     r1, #0                          ; nothing moved
SomeMemoryMoved
        LDR     r0, [r10, #DANode_Number]       ; reload area number

; and drop thru to...

IssueServiceMemoryMoved

; in:   r0 = area number that was shrunk/grown
;       r1 = amount moved (signed)
;
5344 5345 5346 5347
  [ DA_Batman
        CMP     r0, #ChangeDyn_Batcall
        BEQ     ISMM_BatCloak           ; cloaking device (no service issue)
  ]
Neil Turton's avatar
Neil Turton committed
5348 5349 5350 5351 5352 5353
        Push    "r1"
        MOV     r2, r0                  ; r2 = area number
        MOV     r0, r1                  ; amount moved (signed)
        MOV     r1, #Service_MemoryMoved
        BL      Issue_Service
        Pull    "r1"                    ; restore amount moved
5354 5355 5356
  [ DA_Batman
ISMM_BatCloak
  ]
Neil Turton's avatar
Neil Turton committed
5357 5358 5359
        TEQ     r1, #0
        RSBMI   r1, r1, #0              ; r1 on exit = unsigned amount

Jeffrey Lee's avatar
Jeffrey Lee committed
5360 5361
        LDR     r0, =ZeroPage
      [ ZeroPage = 0
Neil Turton's avatar
Neil Turton committed
5362
        STR     r0, [r0, #CDASemaphore] ; clear CDASemaphore
Jeffrey Lee's avatar
Jeffrey Lee committed
5363 5364 5365 5366
      |
        MOV     lr, #0
        STR     lr, [r0, #CDASemaphore] ; clear CDASemaphore
      ]
Neil Turton's avatar
Neil Turton committed
5367 5368 5369
        Pull    "r0, r2-r9, r10, lr"
        ExitSWIHandler

5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385
CDA_PMP ROUT
        ; OS_ChangeDynamicArea implementation for PMPs
        ; Call through to the areas handler routine and let it do all the work
        LDR     r2, =ZeroPage
        MOV     r0, #0
        STR     r0, [r2, #CDASemaphore] ; Ensure handler can call back into us to modify the PMP
        ASSERT  DANode_Handler = DANode_Workspace + 4
        ADD     r12, r10, #DANode_Workspace
        Pull    "r2"
        ; Call with:
        ; r0 = reason
        ; r1 = change amount (pages)
        ; r2 = DA number
        LDR     r11, [r10, #DANode_PMPSize] ; Remember current size
        MOV     r0, #DAHandler_ResizePMP
        MOV     r1, r1, ASR #12
5386 5387 5388 5389 5390
      [ DebugCDA2
        DREG    r2, "CDA_PMP calling ResizePMP for DA ", cc
        DREG    r1, ", page count ", cc
        DREG    r10, ", DANode "
      ]
5391 5392 5393 5394 5395 5396
        MOV     lr, pc
        LDMIA   r12, {r12, pc}          ; Call handler
        ; Exit without issuing service call - PhysOp should have triggered it for us
        MOVVC   r0, r2                  ; r0 = DA number if no error
        MRS     r2, CPSR
        LDR     r10, [r10, #DANode_PMPSize]
5397 5398 5399 5400
      [ DebugCDA2
        DREG    r10, "new size: ", cc
        DREG    r11, " old size: "
      ]
5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434
        SUBS    r1, r10, r11
        RSBLT   r1, r1, #0              ; get unsigned change for OS_ChangeDynamicArea return
        CMP     r1, #DynArea_PMP_BigPageCount ; even though unsigned, use same +2GB clamp
        MOVLO   r1, r1, LSL #12
        LDRHS   r1, =DynArea_PMP_BigByteCount
        TST     r2, #V_bit
        BNE     %FT90
        Pull    "r2-r9,r10,lr"
        ExitSWIHandler
90
        ; If no error pointer, generate can't move error
        TEQ     r0, #0
        ADREQL  r0, ErrorBlock_ChDynamNotAllMoved
 [ International
        BLEQ    TranslateError
 ]
        Pull    "r2-r9,r10,lr"
        ORR     lr, lr, #V_bit
        ExitSWIHandler

; ***********************************************************************************
;
;       GrowFreePool - Try and grow the free pool so it's at least the given size
;
; in:   r1 = desired page count
;       r12 -> dst area node (we won't try and shrink this one)
;
; out:  C=0 => failed to move as much as we wanted
;       C=1 => succeeded in moving as much as we wanted
GrowFreePool ROUT
        Entry   "r0-r2,r11"
        ; Check to see if there's already enough space
        LDR     r11, =ZeroPage+FreePoolDANode
        LDR     r2, [r11, #DANode_PMPSize]
Jeffrey Lee's avatar
Jeffrey Lee committed
5435 5436
        CMP     r2, r1
        EXIT    CS
5437 5438 5439 5440 5441 5442
 [ ShrinkableDAs
        ; Try and shrink shrinkables
        BL      TryToShrinkShrinkables
        EXIT    CS
 ]
        ; Try shrinking application space, if r12 != appspace
5443 5444
        LDR     lr, [r12, #DANode_Number]
        CMP     lr, #ChangeDyn_AplSpace
5445
        BEQ     %FT80
Jeffrey Lee's avatar
Jeffrey Lee committed
5446
        SUB     r0, r1, r2
5447 5448 5449
        LDR     r2, [r11, #CDASemaphore-FreePoolDANode]
        MOV     r0, #0
        STR     r0, [r11, #CDASemaphore-FreePoolDANode] ; Allow nested call
Jeffrey Lee's avatar
Jeffrey Lee committed
5450 5451 5452 5453
        CMP     r1, #DynArea_PMP_BigPageCount
        MOVLT   r1, r1, LSL #12
        LDRGE   r1, =DynArea_PMP_BigByteCount
        MOV     r0, #ChangeDyn_FreePool ; shrink appspace by growing free pool
5454 5455
        SWI     XOS_ChangeDynamicArea
        STR     r2, [r11, #CDASemaphore-FreePoolDANode] ; reclaim semaphore
Jeffrey Lee's avatar
Jeffrey Lee committed
5456 5457 5458 5459
        LDR     r2, [r11, #DANode_PMPSize]
        FRAMLDR r1
        CMP     r2, r1 ; Free pool >= desired size?
        EXIT
5460 5461 5462 5463
80
        CLC
        EXIT

5464
 [ ShrinkableDAs
Neil Turton's avatar
Neil Turton committed
5465 5466
; ***********************************************************************************
;
5467
;       TryToShrinkShrinkables - Attempt to make more space by shrinking shrinkable areas if appropriate
Neil Turton's avatar
Neil Turton committed
5468
;
5469 5470
; in:   r1 = total amount we wish to have in src area (page count, already limited by max_size of destination area)
;       r2 = current size of src area (pages, must be less than r1)
5471
;       r11 -> src area node (we don't do anything unless this is the free pool)
5472
;       r12 -> dst area node (we won't try and shrink this one)
5473
;       C = 0
Neil Turton's avatar
Neil Turton committed
5474
;
5475
; out:  r2 = new size of src area (pages)
5476 5477
;       C=0 => failed to move as much as we wanted
;       C=1 => succeeded in moving as much as we wanted
Neil Turton's avatar
Neil Turton committed
5478

5479
TryToShrinkShrinkables ROUT
5480
        Entry   "r0,r1,r10"
5481 5482
        LDR     lr, [r11, #DANode_Number]
        TEQ     lr, #ChangeDyn_FreePool
Kevin Bracey's avatar
Kevin Bracey committed
5483
        EXIT    NE                              ; if src <> free pool, exit with C, V flags intact
Neil Turton's avatar
Neil Turton committed
5484

5485 5486
        LDR     r10, [r11, #DynArea_ws-FreePoolDANode]
        ADD     r10, r10, #:INDEX:DynArea_ShrinkableSubList-DANode_SubLink ; get shrinkable DA list
Neil Turton's avatar
Neil Turton committed
5487
10
5488
        LDR     r10, [r10, #DANode_SubLink]     ; and load next
Neil Turton's avatar
Neil Turton committed
5489 5490
        CMP     r10, #1                         ; any more nodes?
        EXIT    CC                              ; no, then no match
5491 5492 5493
        TEQ     r10, r12                        ; check area <> dest
        BEQ     %BT10                           ; if not, try next area

5494 5495 5496 5497 5498 5499
        LDR     lr, [r10, #DANode_Flags]
        TST     lr, #DynAreaFlags_PMP
        SUB     lr, r1, r2                      ; lr = amount we still need
        LDREQ   r1, [r10, #DANode_Size]         ; available size of this area
        MOVEQ   r1, r1, LSR #12
        LDRNE   r1, [r10, #DANode_PMPSize]
5500
        CMP     lr, r1
5501 5502 5503 5504 5505 5506 5507 5508
        MOVLO   r1, lr                          ; min(amount we need, size of this area)
        CMP     r1, #DynArea_PMP_BigPageCount
        MOVLO   r1, r1, LSL #12
        RSBLO   r1, r1, #0                      ; make negative - it's a shrink
        LDRHS   r1, =-DynArea_PMP_BigByteCount
        MOV     r0, #0
        LDR     r2, [r11, #CDASemaphore-FreePoolDANode] ; preserve old CDASemaphore (may not be set, e.g. when called from AMBControl)
        STR     r0, [r11, #CDASemaphore-FreePoolDANode] ; momentarily pretend we're not threaded
5509 5510
        LDR     r0, [r10, #DANode_Number]
        SWI     XOS_ChangeDynamicArea
5511 5512 5513
        STR     r2, [r11, #CDASemaphore-FreePoolDANode] ; we're threaded again
        FRAMLDR r1                              ; reload original r1
        LDR     r2, [r11, #DANode_PMPSize]      ; get new size of src area
5514 5515 5516
        CMP     r2, r1
        BCC     %BT10                           ; if still too small, loop
        EXIT                                    ; exit CS indicating success
Neil Turton's avatar
Neil Turton committed
5517 5518
 ]

Neil Turton's avatar
Neil Turton committed
5519 5520 5521 5522 5523 5524 5525 5526
; ***********************************************************************************
;
;       DoTheGrow - Do one chunk of growing, small enough to fit into the page block on the stack
;
; in:   r2 = number of entries to put in page block (for this chunk)
;       r5 = page size
;       r7 = amount taking from src area (in this chunk)
;       (r10 -> dest area)
5527
;       r11 -> src area (always free pool)
Neil Turton's avatar
Neil Turton committed
5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539
;       r12 -> dest area
;
; out:  r0-r2,r4,r6-r9 may be corrupted
;       r3,r5,r10-r12 preserved
;
; Note: Removal is from one area only, the calling routine breaks the chunks up at free/app boundary.

; Temporary (stack frame) workspace used by this routine

                ^       0, sp
NumEntries      #       4                       ; Number of entries to do for this chunk
TotalAmount     #       4                       ; Total size of grow for this chunk (ie entry value of r3)
5540 5541 5542 5543
DestAddr        #       4                       ; Log addr of 1st page being added to dest
DestFlags       #       4                       ; Page flags for destination area
SavedPSR        #       4                       ; PSR before IRQs disabled
Offset1To2      #       4                       ; Offset from 1st to 2nd bank
5544 5545 5546 5547 5548
NumEntries32    #       4                       ; Number of pages with 32bit phys addresses
PageBlock1      #       PageBlockSize64         ; 1st page block, for original page numbers and phys. addrs
PageBlock2      #       PageBlockSize64         ; 2nd page block, for new page numbers and phys. addrs
PageBlock1_32   #       PageBlockSize32         ; 1st page block, only lists pages with 32bit phys addresses
PageBlock2_32   #       PageBlockSize32         ; 2nd page block, for 32bit pages
Neil Turton's avatar
Neil Turton committed
5549

5550
DoTheGrowStackSize *    ((:INDEX: @)+15) :AND: :NOT: 15
Neil Turton's avatar
Neil Turton committed
5551

5552 5553 5554 5555 5556 5557 5558
; Offset1To2 is only used by the first half of the routine. Reuse the space as flags for the second half:
                    ^   :INDEX: Offset1To2, sp
NeedToMoveFlag      #   1                       ; Whether we still need to move the current page
                    #   3                       ; (spare)

DoTheGrow ROUT
        Entry "r3,r5,r10-r12", DoTheGrowStackSize
Neil Turton's avatar
Neil Turton committed
5559 5560 5561 5562

        STR     r2, NumEntries                  ; save number of entries for use later
        STR     r7, TotalAmount                 ; save amount growing by

5563
; Call the pre-grow handler
Neil Turton's avatar
Neil Turton committed
5564

5565
        ADR     r1, PageBlock1
Neil Turton's avatar
Neil Turton committed
5566 5567 5568 5569 5570 5571
        MOV     r3, r7
        BL      CallPreGrow
        EXIT    VS

; now check to see if particular pages are required

5572
        LDR     lr, [r1, #MemPageBlock64_PageNum] ; load page number in 1st entry
Neil Turton's avatar
Neil Turton committed
5573 5574 5575 5576
        CMP     lr, #-1                         ; is it -1?
        BNE     DoTheGrowPagesSpecified         ; if not, then jump to special code

        MOV     r2, r3                          ; amount moving
5577 5578 5579
        ADR     r3, PageBlock1
        BL      DoTheGrowCommon                 ; Move all the pages around
        MOV     r3, r2                          ; r3 = size of change
Neil Turton's avatar
Neil Turton committed
5580 5581 5582 5583 5584 5585
        LDR     r2, NumEntries                  ; restore number of entries in page block
        ADR     r1, PageBlock1                  ; point at page block 1 with page numbers filled in
        BL      CallPostGrow
        CLRV
        EXIT

5586 5587 5588 5589 5590 5591 5592 5593 5594
DoTheGrowPageUnavailable2 ROUT
        ADR     r1, PageBlock1
        LDR     r2, NumEntries
        ASSERT  MemPageBlock64_Size=20
        ADD     r1, r1, r2, LSL #4
        ADD     r1, r1, r2, LSL #2
        LDR     r5, =ZeroPage
        LDR     r5, [r5, #Page_Size]

5595
DoTheGrowPageUnavailable ROUT
Neil Turton's avatar
Neil Turton committed
5596

5597 5598 5599 5600 5601
; In:
;  r0 -> Error pointer
;  r1 -> End of PageBlock1 range which needs reverting
;  r5 = Page size
;
Neil Turton's avatar
Neil Turton committed
5602 5603 5604 5605 5606
; Come here if a required page is not available
; First we need to go back thru all the part of the page block we've already done,
; marking the pages as not being used after all

        ADR     r2, PageBlock1
5607 5608
        LDR     r3, =ZeroPage
        LDR     r3, [r3, #CamEntriesPointer]
Neil Turton's avatar
Neil Turton committed
5609
38
5610 5611 5612 5613
        ASSERT  MemPageBlock64_PageNum=0
        LDR     r4, [r1, #-MemPageBlock64_Size]! ; r4 = physical page number
        ADD     r4, r3, r4, LSL #CAM_EntrySizeLog2 ; point at cam entry
        LDR     lr, [r4, #CAM_PageFlags]
Neil Turton's avatar
Neil Turton committed
5614
        BIC     lr, lr, #PageFlags_Required
5615
        STR     lr, [r4, #CAM_PageFlags]
Neil Turton's avatar
Neil Turton committed
5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628
        TEQ     r1, r2
        BNE     %BT38

; since pre-grow handler exited without an error, we have to keep our promise
; to call the post-grow handler

        MOV     r3, #0                          ; no pages moved
        MOV     r2, #0                          ; no pages moved
        ADR     r1, PageBlock1                  ; not really relevant
        BL      CallPostGrow

 [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
5629
 |
Neil Turton's avatar
Neil Turton committed
5630
        SETV
Robert Sprowson's avatar
Robert Sprowson committed
5631
 ]
Neil Turton's avatar
Neil Turton committed
5632 5633 5634 5635
        EXIT

        MakeErrorBlock  CantGetPhysMem

5636
; r0 = 0 or DAHandler_RESV
5637
; r1 -> first page block
5638 5639 5640 5641 5642
; r2 = number of pages in block
; r3 -> CAM
; r5 = PageSize
; r11 -> source DANode
; r12 -> dest DANode
5643
DoTheGrowPagesSpecified ROUT
Neil Turton's avatar
Neil Turton committed
5644 5645 5646 5647

; First check if any of the pages requested are unavailable
; At the same time as we're doing this, we fill in the log. and phys. addresses in the block

5648 5649 5650 5651 5652 5653 5654 5655
        FastCDA_ProfStart MarkRequired, r4, r6, lr

        ; Set up r6 to be the mask of page flags not allowed
        LDR     lr, =DAHandler_RESV
        EORS    r6, r0, lr
        MOVNE   r6, #PageFlags_Reserved
        ORR     r6, r6, #PageFlags_Unavailable :OR: PageFlags_Required

5656 5657 5658
        ADRL    r9, PageBlock1_32
        MOV     r10, #0

Jeffrey Lee's avatar
Jeffrey Lee committed
5659
        LDR     r0, =ZeroPage
Neil Turton's avatar
Neil Turton committed
5660
        LDR     r0, [r0, #CamEntriesPointer]
5661
05
5662 5663
        ASSERT  MemPageBlock64_PageNum=0
        LDR     r3, [r1], #MemPageBlock64_Size  ; r3 = physical page number
5664 5665 5666
        ADD     r4, r0, r3, LSL #CAM_EntrySizeLog2 ; point at cam entry
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
5667 5668 5669 5670 5671 5672
        LDMIA   r4, {r7, lr}                    ; r7 = log. addr, lr = PPL
        MOV     r8, #0
        STR     r7, [r1, #MemPageBlock64_LogLow-MemPageBlock64_Size] ; store log. addr in page block
        STR     r8, [r1, #MemPageBlock64_LogHigh-MemPageBlock64_Size]
        STR     r7, [r1, #PageBlockSize64+MemPageBlock64_LogLow-MemPageBlock64_Size] ; and in 2nd page block
        STR     r8, [r1, #PageBlockSize64+MemPageBlock64_LogHigh-MemPageBlock64_Size]
Neil Turton's avatar
Neil Turton committed
5673

5674
        TST     lr, r6                          ; if page in use by someone else, or by us, then return error
5675
        ADRNE   r0, ErrorBlock_CantGetPhysMem
5676
        BNE     DoTheGrowPageUnavailable
Neil Turton's avatar
Neil Turton committed
5677
        ORR     lr, lr, #PageFlags_Required     ; set bit in flags to say page will be needed
5678
        STR     lr, [r4, #CAM_PageFlags]        ; and store back
Neil Turton's avatar
Neil Turton committed
5679

5680 5681 5682
        ; Speculative store of page number in 32bit list
        STR     r3, [r9, #MemPageBlock32_PageNum]

5683
; work out physical address direct from physical page number, NOT from logical address, since log addr may be Nowhere (multiply mapped, or PMP)
Neil Turton's avatar
Neil Turton committed
5684

Jeffrey Lee's avatar
Jeffrey Lee committed
5685
        LDR     r4, =ZeroPage+PhysRamTable
5686
06
Neil Turton's avatar
Neil Turton committed
5687 5688
        LDMIA   r4!, {r8, lr}                   ; load phys addr, size
        SUBS    r3, r3, lr, LSR #12             ; subtract off number of pages in this chunk
5689
        BCS     %BT06
Neil Turton's avatar
Neil Turton committed
5690 5691

        ADD     r3, r3, lr, LSR #12             ; put back what could not be subtracted
5692
        ADD     r8, r8, r3                      ; and add onto base address
5693
        MOVS    lr, r8, LSR #20
5694
        MOV     r8, r8, LSL #12
5695 5696 5697 5698 5699 5700 5701 5702 5703 5704
        STR     r8, [r1, #MemPageBlock64_PhysLow-MemPageBlock64_Size] ; store physical address in page block
        STR     lr, [r1, #MemPageBlock64_PhysHigh-MemPageBlock64_Size]

        ; Store details in 32bit page block lists if needed
        STREQ   r7, [r9, #PageBlockSize32+MemPageBlock32_LogAddr] ; Store log addr in 2nd page block
        ASSERT  MemPageBlock32_PhysAddr=MemPageBlock32_LogAddr+4
        ASSERT  MemPageBlock32_Size=MemPageBlock32_LogAddr+8
        ADDEQ   r9, r9, #MemPageBlock32_LogAddr
        ADDEQ   r10, r10, #1
        STMEQIA r9!, {r7, r8}                  ; log + phys addr in 1st page block
Neil Turton's avatar
Neil Turton committed
5705 5706

        SUBS    r2, r2, #1
5707
        BNE     %BT05
5708
        STR     r10, NumEntries32
5709
        FastCDA_ProfEnd MarkRequired, r0, r6, lr
Neil Turton's avatar
Neil Turton committed
5710

5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882
; Look for replacement pages. For API compatibility, we can only replace pages
; in low RAM with other pages from low RAM. This means searching for
; replacements is now a two step process:
;
; 1. Look for replacements for low RAM pages (restricting to only low RAM replacements)
; 2. Look for replacements for high RAM pages (preferring high RAM replacements where possible)
;
; Preferring high RAM over low RAM in step two should help make sure there are
; enough low RAM pages available in the free pool for dealing with future low
; RAM claim requests.
;
; As we find each replacement page, we shuffle it to the back of the free pool,
; so that once we're done we can just reduce the free pool size by N pages in
; order to remove all the pages we're taking.
;
; Additionally, since step 1 can fail, we're now doing the search for
; replacements prior to calling Service_PagesUnsafe (so we don't need to
; make a Service_PagesSafe call if the search fails). Keeping CDASemaphore
; locked during the PagesUnsafe call should be sufficient to ensure that no
; other software messes with the memory map in a way which will invalidate our
; replacement choices.

        FastCDA_ProfStart FindSpare, r6, r8, lr
        LDR     r7, [r11, #DANode_PMP]
        ADR     r1, PageBlock1
        LDR     r2, NumEntries
        LDR     r3, [r11, #DANode_PMPSize]      ; first candidate replacement+1
        SUB     r4, r3, #1                      ; position to swap to
        LDR     r5, =ZeroPage
        LDR     r0, [r5, #CamEntriesPointer]
        LDR     r5, [r5, #MaxCamEntry32]        ; max 32bit page number
10
        SUBS    r2, r2, #1
        BLT     %FT19
        ASSERT  MemPageBlock64_PageNum=0
        LDR     r6, [r1], #MemPageBlock64_Size
        CMP     r6, r5
        BHI     %BT10                           ; 64bit addr, skip it
        ; 32bit page, look for replacement
        ; If the required page is in the free pool, it effectively acts as its
        ; own replacement
        ADD     r8, r0, r6, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIB   r8, {r9,r10,lr}
        TST     r9, #DynAreaFlags_PMP
        BEQ     %FT12
        CMP     r10, r11
        BNE     %FT12
        ; It's a match
11
        ; Remember page num (we'll fill in the phys addr later)
        STR     r6, [r1, #PageBlockSize64-MemPageBlock64_Size]
        ; Swap to back of free pool
        LDR     r9, [r7, r4, LSL #2]
        STR     r6, [r7, r4, LSL #2]
        STR     r9, [r7, lr, LSL #2]
        STR     r4, [r8, #CAM_PMPIndex]
        ADD     r8, r0, r9, LSL #CAM_EntrySizeLog2
        STR     lr, [r8, #CAM_PMPIndex]
        ; Go for the next
        CMP     r3, r4
        MOVHI   r3, r4
        SUB     r4, r4, #1
        B       %BT10
        ; Replacement search
12
        SUBS    r3, r3, #1
        ADRLO   r0, ErrorBlock_CantGetPhysMem
        BLO     DoTheGrowPageUnavailable2       ; ran out of possible replacements
        LDR     r6, [r7, r3, LSL #2]
        CMP     r6, r5
        BHI     %BT12
        ; 32bit page found. Check that it's free for use (it shouldn't be
        ; claimed by anyone, but it might be Required for this grow operation)
        ADD     r8, r0, r6, LSL #CAM_EntrySizeLog2
        LDMIB   r8, {r9,r10,lr}
        TST     r9, #PageFlags_Required
        BNE     %BT12
        B       %BT11

19
        ; 32bit pages dealt with. Now the 64bit ones.
        ADR     r1, PageBlock1
        LDR     r2, NumEntries
        ADD     r5, r5, #1
        ADD     r3, r4, #1
20
        SUBS    r2, r2, #1
        BLT     %FT29
        LDR     lr, [r1, #MemPageBlock64_PhysHigh]
        ASSERT  MemPageBlock64_PageNum=0
        LDR     r6, [r1], #MemPageBlock64_Size
        CMP     lr, #0
        BEQ     %BT20                           ; 32bit phys addr, skip it
        ; 64bit page, look for replacement
        ; If the required page is in the free pool, it effectively acts as its
        ; own replacement
        ADD     r8, r0, r6, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIB   r8, {r9,r10,lr}
        TST     r9, #DynAreaFlags_PMP
        BEQ     %FT22
        CMP     r10, r11
        BNE     %FT22
        ; It's a match
21
        ; Remember page num (we'll fill in the phys addr later)
        STR     r6, [r1, #PageBlockSize64-MemPageBlock64_Size]
        ; Swap to back of free pool
        LDR     r9, [r7, r4, LSL #2]
        STR     r6, [r7, r4, LSL #2]
        STR     r9, [r7, lr, LSL #2]
        STR     r4, [r8, #CAM_PMPIndex]
        ADD     r8, r0, r9, LSL #CAM_EntrySizeLog2
        STR     lr, [r8, #CAM_PMPIndex]
        ; Go for the next
        CMP     r3, r4
        MOVHI   r3, r4
        SUB     r4, r4, #1
        B       %BT20
        ; Replacement search
22
        SUBS    r3, r3, #1
        BHS     %FT23
        ; Ran out of candidate pages
        ; Try 32bit ones if not already tried those
        CMP     r5, #0
        ADREQ   r0, ErrorBlock_CantGetPhysMem
        BEQ     DoTheGrowPageUnavailable2
        MOV     r5, #0
        MOV     r3, r4
23
        LDR     r6, [r7, r3, LSL #2]
        CMP     r6, r5
        BLO     %BT22
        ; Page found. Check that it's free for use (it shouldn't be
        ; claimed by anyone, but it might be Required for this grow operation)
        ADD     r8, r0, r6, LSL #CAM_EntrySizeLog2
        LDMIB   r8, {r9,r10,lr}
        TST     r9, #PageFlags_Required
        BNE     %BT22
        B       %BT21

29
        ; All replacements found
        ; Now fill in their physical addresses (+ PageBlock2_32)
        ADRL    r4, PageBlock2
        ADRL    r1, PageBlock2_32
        LDR     r2, NumEntries
30
        ASSERT  MemPageBlock64_PageNum=0
        LDR     r3, [r4], #MemPageBlock64_Size
        BL      ppn_to_physical                 ; -> r8,r9 = phys, r5 corrupt
        ADRCSL  r0, PhysicalAddressNotFoundError
        BCS     DoTheGrowPageUnavailable2
        ASSERT  MemPageBlock64_PhysLow=MemPageBlock64_Size-8
        ASSERT  MemPageBlock64_PhysHigh=MemPageBlock64_Size-4
        STMDB   r4, {r8,r9}
        ; If the required page was a 32bit page, fill in PageBlock2_32
        ; (replacement should also be 32bit page)
        LDR     r9, [r4, #PageBlock1-PageBlock2-MemPageBlock64_Size+MemPageBlock64_PhysHigh]
        CMP     r9, #0
        STREQ   r8, [r1, #MemPageBlock32_PhysAddr]
        ASSERT  MemPageBlock32_PageNum=0
        STREQ   r3, [r1], #MemPageBlock32_Size
        SUBS    r2, r2, #1
        BNE     %BT30
        FastCDA_ProfEnd FindSpare, r6, r8, lr
Neil Turton's avatar
Neil Turton committed
5883

5884
        FastCDA_ProfStart PagesUnsafe, r0, r6, lr
Neil Turton's avatar
Neil Turton committed
5885 5886
        ADR     r2, PageBlock1                  ; r2 -> 1st page block
        LDR     r3, NumEntries                  ; r3 = number of entries in page block
5887
        LDR     r1, =Service_PagesUnsafe64
Neil Turton's avatar
Neil Turton committed
5888
        BL      Issue_Service
5889 5890 5891 5892 5893
        LDR     r3, NumEntries32
        CMP     r3, #0                          ; any 32bit pages?
        ADRNEL  r2, PageBlock1_32
        MOVNE   r1, #Service_PagesUnsafe
        BLNE    Issue_Service
5894
        FastCDA_ProfEnd PagesUnsafe, r0, r6, lr
Neil Turton's avatar
Neil Turton committed
5895 5896 5897 5898

; now move the pages

        LDR     r2, TotalAmount                 ; amount moving
5899 5900 5901 5902 5903
        LDR     r0, [r11, #DANode_PMP]
        LDR     r3, [r11, #DANode_PMPSize]
        ADD     r0, r0, r3, LSL #2              ; move r0 to point to after end of area
        SUB     r3, r3, r2, LSR #12             ; reduce by amount moving from area
        STR     r3, [r11, #DANode_PMPSize]      ; store reduced source size
Neil Turton's avatar
Neil Turton committed
5904 5905 5906 5907 5908

        LDR     r1, [r12, #DANode_Base]
        LDR     r3, [r12, #DANode_Size]

        LDR     r6, [r12, #DANode_Flags]        ; r6 = dst flags
5909 5910 5911 5912 5913 5914
        ; If dest is a PMP, then this must be the batcall made by PhysOp, and
        ; we don't need to mask the flags.
        TST     r6, #DynAreaFlags_PMP
        LDREQ   lr, =DynAreaFlags_AccessMask
        ANDEQ   r6, r6, lr
        ORREQ   r6, r6, #PageFlags_Unavailable  ; set unavailable bit if regular DA. For PMPs, PMP has control over this.
Neil Turton's avatar
Neil Turton committed
5915 5916
        STR     r6, DestFlags                   ; save for later
        TST     r6, #DynAreaFlags_DoublyMapped  ; check if dst is doubly mapped
5917
        BEQ     %FT65                           ; [it's not, so skip all this, and r9 will be irrelevant]
Neil Turton's avatar
Neil Turton committed
5918 5919 5920 5921

; we must shunt all existing pages in dest area down

        MOVS    r4, r3                          ; amount to do
5922
        BLNE    ShuffleDoublyMappedRegionForGrow
Neil Turton's avatar
Neil Turton committed
5923
        ADD     r9, r3, r2                      ; set up offset from 1st copy to 2nd copy (= new size)
5924
65
Neil Turton's avatar
Neil Turton committed
5925 5926 5927 5928
        STR     r9, Offset1To2                  ; store offset 1st to 2nd copy
        ADD     r1, r1, r3                      ; r1 -> address of 1st extra page
        STR     r1, DestAddr

5929 5930 5931 5932 5933 5934 5935 5936 5937
     [ FastCDA_FIQs
        ; Claim FIQs for this entire loop
        ; (With the old behaviour, for large grows, total time in ReleaseFIQ could be several centiseconds, since the kernel reinstalls the default handler each time)
        FastCDA_ProfStart ClaimFIQ, r6, r1, lr
        MOV     r1, #Service_ClaimFIQ
        BL      Issue_Service
        FastCDA_ProfEnd ClaimFIQ, r6, r1, lr
     ]

Neil Turton's avatar
Neil Turton committed
5938 5939
        LDR     r1, DestAddr
        MOV     r4, #0                          ; amount done
5940 5941 5942
        LDR     r0, =ZeroPage
        LDR     r5, [r0, #Page_Size]
        LDR     r0, [r0, #CamEntriesPointer]    ; point r0 at camentries
Neil Turton's avatar
Neil Turton committed
5943 5944 5945 5946
        LDR     r7, TotalAmount                 ; amount to do
        ADR     r8, PageBlock1
        LDR     r9, Offset1To2
70
5947
        MRS     r14, CPSR
Kevin Bracey's avatar
Kevin Bracey committed
5948
        STR     r14, SavedPSR                   ; save old PSR (note: stack must be flat when we do this!)
Neil Turton's avatar
Neil Turton committed
5949

5950
        ; Grab the flags for the page we're replacing; in order to preserve the contents of the page we may map it to its destination early, causing the flags in the CAM map to be "wrong" when we read them back out later on
5951
        LDR     r11, [r8, #MemPageBlock32_PageNum] ; need to get PPL for page being replaced
5952 5953
        ADD     lr, r0, #CAM_PageFlags          ; point at PPLs, not addresses
        LDR     r11, [lr, r11, LSL #CAM_EntrySizeLog2]
5954 5955 5956
        MOV     lr, #1
        STRB    lr, NeedToMoveFlag

Neil Turton's avatar
Neil Turton committed
5957
        Push    "r0-r4,r7-r12"                  ; save regs used during copy
5958 5959
    [ :LNOT: FastCDA_FIQs
        FastCDA_ProfStart ClaimFIQ, r6, r1, lr
Neil Turton's avatar
Neil Turton committed
5960 5961
        MOV     r1, #Service_ClaimFIQ
        BL      Issue_Service
5962 5963
        FastCDA_ProfEnd ClaimFIQ, r6, r1, lr
    ]
Neil Turton's avatar
Neil Turton committed
5964

5965 5966
        WritePSRc I_bit+SVC_mode, r6            ; disable IRQs round here (we don't want interrupt code to update
                                                ; the old mapping behind us while we're trying to copy it)
Neil Turton's avatar
Neil Turton committed
5967

5968
        LDR     lr, [r8, #PageBlockSize64+MemPageBlock64_PageNum] ; lr = page number of replacement page
5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980

        ; The replacement page will be a member of the free pool PMP - remove it
        ; from the PMP now, before any CAM updates make it hard to get access
        ; to the original page flags
        LDR     r11, =ZeroPage+FreePoolDANode
        ADD     r4, r0, lr, LSL #CAM_EntrySizeLog2
        LDR     r2, [r11, #DANode_PMP]
        LDR     r6, [r4, #CAM_PMPIndex]!
        MOV     r3, #-1
        STR     r3, [r2, r6, LSL #2]
        ; If the required page is a member of a PMP, update its PMP to point to
        ; the replacement page (and update the CAM for the replacement)
5981
        LDR     r6, [r8, #MemPageBlock32_PageNum]
5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995
        TEQ     r6, lr
        BEQ     %FT72                           ; Required == replacement, leave unassociated
        ADD     r6, r0, r6, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIB   r6, {r2, r3, r6}
        TST     r2, #DynAreaFlags_PMP
        BEQ     %FT72
        STMDA   r4, {r3, r6}                    ; Update CAM for replacement to point to PMP of required page
        LDR     r4, [r3, #DANode_PMP]
        STR     lr, [r4, r6, LSL #2]            ; And update PMP of required page to point to replacement
72

5996
        LDR     r6, [r8, #MemPageBlock32_PageNum] ; r6 = page number required
Neil Turton's avatar
Neil Turton committed
5997
        TEQ     r6, lr                          ; if the same
5998
        Pull    "r0-r4,r7-r12", EQ              ; then restore registers
5999
        BEQ     %FT86                           ; and skip copy and first page move
Neil Turton's avatar
Neil Turton committed
6000

6001 6002 6003 6004 6005 6006
        ;mjs
        ; - if the old page is currently mapped in, copy normally
        ; - if the old page is not mapped in, copy via temporary mapping
        ; The old scheme, always copying from other mapping, had interrupt cache coherency hole, at least for
        ; ARM with writeback cache (bug in 3.7, fixed in Ursula, then lost)

6007
        LDR     r6, [r8, #MemPageBlock32_LogAddr] ;logical address of src page
6008

6009
        ; If the required page is in the free pool, we don't need to preserve its contents
6010
        ; TODO - have 'volatile' page flag which can be used to indicate that pages can just be taken? (so will work with any PMP)
6011 6012 6013 6014 6015
        TST     r2, #DynAreaFlags_PMP
        BEQ     %FT73
        CMP     r3, r11
        BEQ     %FT84
73
6016

6017
        LDR     r2, =Nowhere
6018

6019 6020 6021 6022 6023 6024
        ; If the required page isn't mapped in, see if we can map it in at the
        ; target address so that we can copy the contents out. If this fails
        ; we'll fall back to using a temporary mapping via AccessPhysicalAddress
        TEQ     r6, r2
        BNE     %FT75                           ; Source is mapped in, everything is fine
        LDR     r3, [sp, #4]                    ; Get stacked r1 (DestAddr)
6025
        LDR     r11, [sp, #11*4+:INDEX:DestFlags]
6026 6027
        TEQ     r3, r2
        BEQ     %FT75                           ; Dest is Nowhere - must use temp mapping
6028
        LDR     r2, [r8, #MemPageBlock32_PageNum]
6029 6030 6031 6032 6033
        FastCDA_ProfStart MoveNeeded, lr, r4, r7
        BL      Call_CAM_Mapping                ; move needed page to destination
        FastCDA_ProfEnd MoveNeeded, lr, r4, r7
        MOV     r6, r3                          ; r6 = logical address of src for copy
        MOV     lr, #0
6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051
        LDR     r2, =Nowhere
        STRB    lr, [sp, #11*4+:INDEX:NeedToMoveFlag] ; Flag that page has been moved
75

        MOV     lr, #-1
        Push    "lr"                            ; Push dummy oldp flag for ReleasePhysicalAddress

        ; With the only source of replacement pages being the free pool, we only have two situations to deal with:
        ; * Source mapped, dest unmapped
        ; * Source unmapped, dest unmapped
        ; (where 'source' = copy source, i.e. required page, and 'dest' = copy dest, i.e. replacement page)
        TEQ     r6, r2
        BEQ     ReplacePage_BothUnmapped
        ; Fall through to DestUnmapped

ReplacePage_DestUnmapped
        Push    "r1"
        FastCDA_ProfStart AccessPhysical, r2, r1, lr
6052
        LDR     r0, =OSAP_None+DynAreaFlags_NotCacheable
6053 6054
        LDR     r1, [r8, #PageBlockSize64+MemPageBlock64_PhysLow] ; r1,r2 = physical address of dest for copy
        LDR     r2, [r8, #PageBlockSize64+MemPageBlock64_PhysHigh]
6055
        ADD     r3, sp, #4
6056
        PTOp    AccessPhysicalAddress
6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069
        ; r0 = logical address of dest for copy
        FastCDA_ProfEnd AccessPhysical, r2, r1, lr
        Pull    "r1"
        B       ReplacePage_BothMapped

ReplacePage_BothUnmapped
        ; We need to make temp mappings of both pages, but we only have one
        ; PhysicalAccess window. For now, take the easy way out and copy via
        ; a temporary buffer - it'll be a bit slower but since we only move
        ; one page at a time the cache should swallow most of the hit.
        ; Since OS_ChangeDynamicArea can't be used from an IRQ routine, we can
        ; assume that the IRQ stack has enough spare space - so use that rather
        ; than the SVC stack
6070
        Push    "r0,r1"
6071
        FastCDA_ProfStart AccessPhysical, r0, r1, lr
6072
        LDR     r0, =OSAP_None+DynAreaFlags_NotCacheable
6073 6074
        LDR     r1, [r8, #MemPageBlock64_PhysLow] ; r1,r2 = physical address of src for copy
        LDR     r2, [r8, #MemPageBlock64_PhysHigh]
6075
        ADD     r3, sp, #8
6076
        PTOp    AccessPhysicalAddress
6077
        MOV     r6, r0                          ; r6 = logical address of src for copy
6078
        FastCDA_ProfEnd AccessPhysical, r0, r1, lr
6079

6080 6081 6082 6083 6084
        ; Copy to IRQ stack
        MSR     CPSR_c, #IRQ32_mode+I32_bit
        Push    "lr"
        SUB     sp, sp, r5
        MOV     r0, sp
6085
        FastCDA_ProfStart CopyPage, r2, r3, r4
Kevin Bracey's avatar
Kevin Bracey committed
6086
        ADD     lr, r6, r5                      ; lr = end src address
6087
77
Neil Turton's avatar
Neil Turton committed
6088 6089 6090
        LDMIA   r6!, {r2, r3, r4, r7, r9, r10, r11, r12}
        STMIA   r0!, {r2, r3, r4, r7, r9, r10, r11, r12}
        TEQ     r6, lr
6091 6092
        BNE     %BT77
        FastCDA_ProfEnd CopyPage, r2, r3, r4
6093

6094
        ; Now map in dest
6095
        LDR     r0, =OSAP_None+DynAreaFlags_NotCacheable
6096 6097
        LDR     r1, [r8, #PageBlockSize64+MemPageBlock64_PhysLow] ; r1,r2 = physical address of dest for copy
        LDR     r2, [r8, #PageBlockSize64+MemPageBlock64_PhysHigh]
6098
        MOV     r3, #0                          ; no oldp needed
6099
        PTOp    AccessPhysicalAddress
6100 6101 6102 6103 6104 6105 6106 6107 6108
        ; r0 = logical address of dest for copy
        MOV     r6, sp
        FastCDA_ProfStart CopyPage, r2, r3, r4
        ADD     lr, r6, r5                      ; lr = end src address
78
        LDMIA   r6!, {r2, r3, r4, r7, r9, r10, r11, r12}
        STMIA   r0!, {r2, r3, r4, r7, r9, r10, r11, r12}
        TEQ     r6, lr
        BNE     %BT78
6109
        FastCDA_ProfEnd CopyPage, r2, r3, r4
6110 6111 6112 6113 6114 6115
        ; Switch back to SVC and clean up the temp mapping
        ADD     sp, sp, r5
        Pull    "lr"
        MSR     CPSR_c, #SVC32_mode+I32_bit
        Pull    "r0,r1"
        B       ReplacePage_Done
6116

6117 6118 6119 6120
 [ {FALSE}
ReplacePage_SrcUnmapped
        Push    "r0,r1"
        FastCDA_ProfStart AccessPhysical, r0, r1, lr
6121
        LDR     r0, =OSAP_None+DynAreaFlags_NotCacheable
6122 6123
        LDR     r1, [r8, #MemPageBlock64_PhysLow] ; r1,r2 = physical address of src for copy
        LDR     r2, [r8, #MemPageBlock64_PhysHigh]
6124
        ADD     r3, sp, #8
6125
        BL      AccessPhysicalAddress
6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140
        MOV     r6, r0                          ; r6 = logical address of src for copy
        FastCDA_ProfEnd AccessPhysical, r0, r1, lr
        Pull    "r0,r1"
 ]

ReplacePage_BothMapped
        FastCDA_ProfStart CopyPage, r2, r3, r4
        ADD     lr, r6, r5                      ; lr = end src address
79
        LDMIA   r6!, {r2, r3, r4, r7, r9, r10, r11, r12}
        STMIA   r0!, {r2, r3, r4, r7, r9, r10, r11, r12}
        TEQ     r6, lr
        BNE     %BT79

        FastCDA_ProfEnd CopyPage, r2, r3, r4
6141

6142 6143 6144 6145
ReplacePage_Done
        ; Release the temp mapping if necessary
        Pull    "r0"
        CMP     r0, #-1
6146
      [ FastCDA_Prof
6147
        BEQ     %FT80
6148 6149
        FastCDA_ProfStart ReleasePhysical, r2, r3, r4
      ]
6150
        PTOpNE  ReleasePhysicalAddress
6151 6152
      [ FastCDA_Prof
        FastCDA_ProfEnd ReleasePhysical, r2, r3, r4
6153
80
6154
      ]
Neil Turton's avatar
Neil Turton committed
6155 6156 6157

; now check if page we're replacing is in L2PT, and if so then adjust L1PT entries (4 of these)

6158 6159 6160
        LDR     r0, [r8, #MemPageBlock64_LogLow] ; look at logical address of page being replaced
        LDR     r1, [r8, #PageBlockSize64+MemPageBlock64_PhysLow] ; load new physical address for page
        LDR     r2, [r8, #PageBlockSize64+MemPageBlock64_PhysHigh]
6161
        PTOp    UpdateL1PTForPageReplacement
6162 6163

84
Neil Turton's avatar
Neil Turton committed
6164 6165
        Pull    "r0-r4,r7-r12"                  ; restore registers

6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177
        ; mjs
        ; OK, what we are about to do is:
        ;   1) move replacement page in (to replace needed page)
        ;   2) move needed page to required destination
        ; This order means that we don't leave a temporary hole at the logical address we're substituting,
        ; which is vital at least in the horrendous case where the logical page is itself used for L2PT.
        ; However, this means there is a potential temporary degeneracy in the caches, two physical pages
        ; having been seen at the same logical address (undefined behaviour).
        ; So, to be safe, we do a MMUChangingEntry first, for the logical page, which will clean/invalidate
        ; caches and invalidate TLBs, to avoid degeneracy. This is slight overkill in some cases, but vital
        ; to avoid serious grief in the awkward cases. Fortunately, these page substitutions are relatively
        ; rare, so performance is not critical.
6178

Neil Turton's avatar
Neil Turton committed
6179 6180
        BIC     r11, r11, #PageFlags_Required   ; knock off bits that indicate that it was a required page

6181 6182 6183 6184
        ADD     lr, r8, #PageBlockSize64 :AND: &FF0
        ADD     lr, lr, #PageBlockSize64 :AND: :NOT: &FF0
        ASSERT  MemPageBlock64_PageNum=0
        ASSERT  MemPageBlock64_LogLow=4
Neil Turton's avatar
Neil Turton committed
6185 6186
        LDMIA   lr, {r2, r3}                    ; get page number, logical address

6187
        LDR     lr, =Nowhere                    ; No need to clean if the page isn't mapped in
6188
        TEQ     r3, lr
6189
        BEQ     %FT85
6190 6191 6192
      [ FastCDA_Unnecessary
        ; We only need to clean the cache/TLB if the page is cacheable
        TST     r11, #DynAreaFlags_NotCacheable
6193
        BNE     %FT85
6194
      ]
Jeffrey Lee's avatar
Jeffrey Lee committed
6195 6196
        Push    "r0-r2, r4, r11"
        LDR     r4, =ZeroPage
6197 6198 6199
      [ FastCDA_Prof
        FastCDA_ProfStart ChangingEntry, r6, lr, r4
      ]
Jeffrey Lee's avatar
Jeffrey Lee committed
6200
        ; Use BangCam to remove the cacheability on the needed page
6201
        LDR     r2, [r8, #MemPageBlock32_PageNum] ; Get page number of needed page
Jeffrey Lee's avatar
Jeffrey Lee committed
6202 6203 6204 6205 6206 6207
        ORR     r11, r11, #1<<TempUncacheableShift ; Make temp uncache
        BL      Call_CAM_Mapping ; This will flush the TLB for us, but won't flush the cache
        ; So now we flush the cache manually
        MOV     r0, r3
        ADD     r1, r3, #4096
        ARMop   Cache_CleanInvalidateRange,,,r4
6208 6209 6210
      [ FastCDA_Prof
        FastCDA_ProfEnd ChangingEntry, r6, lr, r4
      ]
Jeffrey Lee's avatar
Jeffrey Lee committed
6211
        Pull    "r0-r2, r4, r11"
6212
85
6213

6214
        FastCDA_ProfStart MoveReplacement, r6, lr, r5
6215
        BL      Call_CAM_Mapping                ; move replacement page in
6216
        FastCDA_ProfEnd MoveReplacement, r6, lr, r5
6217
86
6218
        LDR     r2, [r8, #MemPageBlock32_PageNum]
6219 6220
        MOV     r3, r1
        LDR     r11, DestFlags
6221 6222
        LDRB    lr, NeedToMoveFlag
        TEQ     lr, #0
6223
        BEQ     %FT87                           ; don't bother if page already been moved to dest
6224
        FastCDA_ProfStart MoveNeeded, r6, lr, r5
6225
        BL      Call_CAM_Mapping                ; move needed page to destination
6226
        FastCDA_ProfEnd MoveNeeded, r6, lr, r5
6227

6228
87
Neil Turton's avatar
Neil Turton committed
6229
        LDR     lr, SavedPSR
6230
        MSR     CPSR_cf, lr
Neil Turton's avatar
Neil Turton committed
6231

6232
      [ :LNOT: FastCDA_FIQs
Neil Turton's avatar
Neil Turton committed
6233
        Push    "r1"
6234
        FastCDA_ProfStart ReleaseFIQ, r1, lr, r5
Neil Turton's avatar
Neil Turton committed
6235 6236
        MOV     r1, #Service_ReleaseFIQ
        BL      Issue_Service
6237
        FastCDA_ProfEnd ReleaseFIQ, r1, lr, r5
Neil Turton's avatar
Neil Turton committed
6238
        Pull    "r1"
6239 6240 6241 6242
      ]
      [ FastCDA_Prof
        MOV     r5, #4096
      ]
Neil Turton's avatar
Neil Turton committed
6243 6244 6245

        ADD     r1, r1, r5                      ; advance dest ptr
        ADD     r4, r4, r5                      ; increment amount done
6246
        ADD     r8, r8, #MemPageBlock64_Size    ; advance page block ptr
Neil Turton's avatar
Neil Turton committed
6247 6248 6249
        CMP     r4, r7                          ; have we done all?
        BNE     %BT70                           ; [no, so loop]

6250 6251 6252 6253 6254 6255 6256
     [ FastCDA_FIQs
        FastCDA_ProfStart ReleaseFIQ, r1, lr, r2
        MOV     r1, #Service_ReleaseFIQ
        BL      Issue_Service
        FastCDA_ProfEnd ReleaseFIQ, r1, lr, r2
     ]

Neil Turton's avatar
Neil Turton committed
6257 6258 6259
        LDR     r3, [r12, #DANode_Size]
        ADD     r3, r3, r7
        STR     r3, [r12, #DANode_Size]         ; store increased destination size
6260 6261 6262 6263 6264 6265 6266
      [ ZeroPage = 0
        TEQ     r12, #AppSpaceDANode            ; check if dest = appspace
      |
        LDR     lr, =ZeroPage+AppSpaceDANode
        TEQ     r12, lr                         ; check if dest = appspace
      ]
        STREQ   r3, [r12, #MemLimit-AppSpaceDANode] ; update memlimit if so
Neil Turton's avatar
Neil Turton committed
6267 6268 6269

; now issue Service_PagesSafe

6270
        FastCDA_ProfStart PagesSafe, r1, r2, r3
6271 6272 6273 6274 6275 6276
        LDR     r2, NumEntries32
        CMP     r2, #0
        ADRNEL  r4, PageBlock2_32
        ADDNE   r3, r4, #PageBlock1_32-PageBlock2_32
        MOVNE   r1, #Service_PagesSafe
        BLNE    Issue_Service
Neil Turton's avatar
Neil Turton committed
6277 6278
        LDR     r2, NumEntries
        ADR     r3, PageBlock1
6279 6280
        ADRL    r4, PageBlock2
        LDR     r1, =Service_PagesSafe64
Neil Turton's avatar
Neil Turton committed
6281
        BL      Issue_Service
6282
        FastCDA_ProfEnd PagesSafe, r1, r2, r3
Neil Turton's avatar
Neil Turton committed
6283 6284 6285 6286 6287 6288 6289 6290 6291 6292

; now call Post_Grow handler

        LDR     r3, TotalAmount                 ; size of grow
        LDR     r2, NumEntries                  ; restore number of entries in page block
        ADR     r1, PageBlock1                  ; point at page block 1 with page numbers filled in
        BL      CallPostGrow
        CLRV
        EXIT

Kevin Bracey's avatar
Kevin Bracey committed
6293 6294
        LTORG

Neil Turton's avatar
Neil Turton committed
6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311
; ***********************************************************************************
;
;       DoTheGrowNotSpecified - Do one chunk of growing, with no page block
;                               But don't call pre-grow or post-grow either
;
; in:   r2 = number of pages to do (in this chunk)
;       r5 = page size
;       r7 = amount taking from src area (in this chunk)
;       r11 -> src area
;       r12 -> dest area
;
; out:  r0-r2,r4,r6-r9 may be corrupted
;       r3,r5,r10-r12 preserved
;
; Note: Removal is from one area only, the calling routine breaks the chunk at free/app boundary.


6312
DoTheGrowNotSpecified ROUT
6313 6314
        Entry   "r3"
        MOV     r3, #0                          ; no dest page list
Neil Turton's avatar
Neil Turton committed
6315
        MOV     r2, r7                          ; amount moving
6316
        BL      DoTheGrowCommon                 ; Move all the pages around
Neil Turton's avatar
Neil Turton committed
6317 6318 6319 6320 6321 6322 6323 6324 6325
        CLRV
        EXIT

; ***********************************************************************************
;
;       CheckAppSpace - If appspace involved in transfer, issue Service or UpCall
;
;       Internal routine, called by OS_ChangeDynamicArea
;
6326
; in:   r10 = size of appspace change (signed)
Neil Turton's avatar
Neil Turton committed
6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339
;       r11 -> node for src
;       r12 -> node for dest
;
; out:  If appspace not involved, or application said it was OK, then
;         V=0
;         All registers preserved
;       else
;         V=1
;         r0 -> error
;         All other registers preserved
;       endif
;

6340 6341
CheckAppSpace ROUT
        Entry "r0-r3"
Jeffrey Lee's avatar
Jeffrey Lee committed
6342
        LDR     r2, =ZeroPage
Neil Turton's avatar
Neil Turton committed
6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354
        LDR     r3, [r2, #AplWorkSize]
        LDR     r2, [r2, #Curr_Active_Object]
        CMP     r2, r3                          ; check if CAO outside application space
        BHI     %FT20                           ; [it is so issue Service not UpCall]

; CAO in application space, so issue UpCall to check it's OK

        MOV     r0, #UpCall_MovingMemory :AND: &FF
        ORR     r0, r0, #UpCall_MovingMemory :AND: &FFFFFF00
        MOVS    r1, r10
        RSBMI   r1, r1, #0                      ; r1 passed in is always +ve (probably a bug, but should be compat.)

6355 6356 6357
      [ DebugCDA2
        DREG    r10, "Asking UpCall_MovingMemory about "
      ]
Neil Turton's avatar
Neil Turton committed
6358 6359 6360 6361
        SWI     XOS_UpCall
        CMP     r0, #UpCall_Claimed             ; if upcall claimed
        EXIT    EQ                              ; then OK to move memory, so exit (V=0 from CMP)

6362
05
Neil Turton's avatar
Neil Turton committed
6363 6364
        ADR     r0, ErrorBlock_ChDynamCAO
10
6365 6366 6367
      [ DebugCDA2
        DLINE   "Change denied"
      ]
Neil Turton's avatar
Neil Turton committed
6368 6369
 [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
6370 6371
 |
        SETV
Neil Turton's avatar
Neil Turton committed
6372 6373 6374 6375 6376 6377 6378
 ]
        STR     r0, [sp]
        EXIT

; IF service call claimed Then Error AplWSpaceInUse

20
6379 6380 6381 6382
      [ DebugCDA2
        DREG    r10, "Telling Service_Memory about "
      ]
        MOV     r0, r10                         ; amount changing aplspace
Neil Turton's avatar
Neil Turton committed
6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405
        MOV     r1, #Service_Memory
        BL      Issue_Service
        CMP     r1, #Service_Serviced
        ADREQ   r0, ErrorBlock_AplWSpaceInUse   ; if service claimed, then return error
        BEQ     %BT10
        CLRV                                    ; else OK
        EXIT

        MakeErrorBlock AplWSpaceInUse
        MakeErrorBlock ChDynamCAO

; ***********************************************************************************
;
;       CallPreShrink - Call pre-shrink routine
;
; in:   r1 = amount shrinking by (+ve)
;       r5 = page size
;       r11 -> node for area being shrunk
;
; out:  If handler exits VC, then r2 = no. of bytes area can shrink by
;       else r0 -> error block or 0 for generic error, and r2=0
;

6406
CallPreShrink Entry "r0,r3,r4, r12"
Neil Turton's avatar
Neil Turton committed
6407 6408 6409 6410
        LDR     r0, [r11, #DANode_Handler]              ; check if no handler
        CMP     r0, #0                                  ; if none (V=0)
        EXIT    EQ                                      ; then exit

Jeffrey Lee's avatar
Jeffrey Lee committed
6411 6412 6413 6414
        LDR     r4, [r11, #DANode_Flags]                ; Sparse DAs shouldn't
        TST     r4, #DynAreaFlags_SparseMap             ; get grow/shrink calls
        EXIT    NE

Neil Turton's avatar
Neil Turton committed
6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447
        MOV     r0, #DAHandler_PreShrink                ; r0 = reason code
        MOV     r3, r1                                  ; r3 = amount shrinking by
        LDR     r4, [r11, #DANode_Size]                 ; r4 = current size
        ASSERT  DANode_Handler = DANode_Workspace +4
        ADD     r12, r11, #DANode_Workspace
        MOV     lr, pc
        LDMIA   r12, {r12, pc}                          ; load workspace pointer and jump to handler

; shrink amount returned by handler may not be page multiple (according to spec),
; so we'd better make it so.

        SUB     lr, r5, #1
        BIC     r2, r3, lr                              ; make page multiple and move into r2
        EXIT    VC
        TEQ     r0, #0                                  ; if generic error returned
        ADREQL  r0, ErrorBlock_ChDynamNotAllMoved       ; then substitute real error message
 [ International
        BLEQ    TranslateError
 ]
        STR     r0, [sp]
        EXIT

; ***********************************************************************************
;
;       CallPostShrink - Call post-shrink routine
;
; in:   r2 = amount shrinking by (+ve)
;       r5 = page size
;       r11 -> node for area being shrunk
;
; out:  All registers preserved
;

6448
CallPostShrink Entry "r0,r3,r4, r12"
Neil Turton's avatar
Neil Turton committed
6449 6450 6451 6452
        LDR     r0, [r11, #DANode_Handler]              ; check if no handler
        CMP     r0, #0                                  ; if none (V=0)
        EXIT    EQ                                      ; then exit

Jeffrey Lee's avatar
Jeffrey Lee committed
6453 6454 6455 6456
        LDR     r4, [r11, #DANode_Flags]                ; Sparse DAs shouldn't
        TST     r4, #DynAreaFlags_SparseMap             ; get grow/shrink calls
        EXIT    NE

Neil Turton's avatar
Neil Turton committed
6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470
        MOV     r0, #DAHandler_PostShrink               ; r0 = reason code
        MOV     r3, r2                                  ; r3 = amount shrunk by
        LDR     r4, [r11, #DANode_Size]                 ; r4 = new size
        ASSERT  DANode_Handler = DANode_Workspace +4
        ADD     r12, r11, #DANode_Workspace
        MOV     lr, pc
        LDMIA   r12, {r12, pc}                          ; load workspace pointer and jump to handler

        EXIT

; ***********************************************************************************
;
;       CallPreGrow - Call pre-grow routine
;
6471 6472
; in:   r1 -> page block (on stack) if DynAreaFlags_NeedsSpecificPages, else 0
;       r2 = number of pages for this call (i.e. number of entries in block)
Neil Turton's avatar
Neil Turton committed
6473 6474 6475 6476 6477 6478 6479 6480
;       r3 = amount area is growing by
;       r5 = page size
;       r12 -> node for area being grown
;
; out:  If can't grow, then
;         r0 -> error
;         V=1
;       else
6481 6482 6483
;         page block may be updated with page numbers
;         r0 = 0, or "RESV" (&56534552) if reserved pages are allowed
;         All other registers preserved
Neil Turton's avatar
Neil Turton committed
6484 6485 6486 6487
;         V=0
;       endif
;

6488
CallPreGrow ROUT
6489
        Entry   "r4, r12"
6490 6491 6492 6493
        LDR     r0, [r12, #DANode_Flags]
        TST     r0, #DynAreaFlags_NeedsDMA              ; if DMA needed
        BNE     %FT10                                   ; use special PreGrow

Neil Turton's avatar
Neil Turton committed
6494 6495 6496 6497
        LDR     r0, [r12, #DANode_Handler]              ; check if no handler
        CMP     r0, #0                                  ; if none (V=0)
        EXIT    EQ                                      ; then exit

Jeffrey Lee's avatar
Jeffrey Lee committed
6498 6499 6500 6501 6502
        LDR     r4, [r12, #DANode_Flags]                ; Sparse DAs shouldn't
        TST     r4, #DynAreaFlags_SparseMap             ; get grow/shrink calls
        MOVNE   r0, #0
        EXIT    NE

6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520
        ; Initialise the (32bit) page block list if required
        TEQ     r1, #0
        BEQ     %FT03

        FastCDA_ProfStart DoTheGrowInit, r0, r4, lr
        ASSERT  MemPageBlock32_Size = 12
        ADD     lr, r1, r2, LSL #3
        ADD     lr, lr, r2, LSL #2                      ; lr -> off end of page block
        MOV     r0, #-1
02
        ASSERT  MemPageBlock32_PageNum = 0
        STR     r0, [lr, #-MemPageBlock32_Size]!        ; store -1, going backwards
        TEQ     lr, r1                                  ; until the end
        BNE     %BT02
        FastCDA_ProfEnd DoTheGrowInit, r0, r4, lr

03
        ; Call the handler
6521
        FastCDA_ProfStart CallPreGrow, r0, r4, lr
Neil Turton's avatar
Neil Turton committed
6522
        MOV     r0, #DAHandler_PreGrow                  ; r0 = reason code
6523
        ASSERT  DAHandler_PreGrow = 0                   ; By default, r0 is preserved by PreGrow handlers. Make sure it has the value we expect.
Neil Turton's avatar
Neil Turton committed
6524 6525 6526 6527 6528
        LDR     r4, [r12, #DANode_Size]                 ; r4 = current size
        ASSERT  DANode_Handler = DANode_Workspace +4
        ADD     r12, r12, #DANode_Workspace
        MOV     lr, pc
        LDMIA   r12, {r12, pc}                          ; load workspace pointer and jump to handler
6529
        ; Handler should return with R0=DAHandler_RESV if it wants to use reserved pages
6530
        FastCDA_ProfEnd CallPreGrow, r12, r4, lr
6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549
        BVS     %FT05

        ; No error, so shuffle page block list into 64bit format
        TEQ     r1, #0
        EXIT    EQ

        ASSERT  MemPageBlock32_Size = 12
        ASSERT  MemPageBlock64_Size = 20
        ADD     lr, r1, r2, LSL #2
        ADD     r12, lr, r2, LSL #4                     ; r12 -> off end of 64bit block
        ADD     lr, lr, r2, LSL #3                      ; lr -> off end of 32bit block
04
        ASSERT  MemPageBlock32_PageNum = 0
        ASSERT  MemPageBlock64_PageNum = 0
        LDR     r4, [lr, #-MemPageBlock32_Size]!        ; Get page number from 32bit list
        STR     r4, [r12, #-MemPageBlock64_Size]!       ; And store in 64bit list
        TEQ     lr, r1
        BNE     %BT04
        EXIT
Neil Turton's avatar
Neil Turton committed
6550

6551
05
Robert Sprowson's avatar
Robert Sprowson committed
6552
        TEQ     r0, #0                                  ; if generic error returned (V still set)
Neil Turton's avatar
Neil Turton committed
6553 6554 6555 6556 6557 6558
        ADREQL  r0, ErrorBlock_ChDynamNotAllMoved       ; then substitute real error message
 [ International
        BLEQ    TranslateError
 ]
        EXIT

6559 6560 6561 6562 6563 6564 6565 6566 6567 6568
10
        ; Instead of calling the users PreGrow handler, walk PhysRamTable and
        ; the CAM map to look for some free DMAable pages
        ; Note that we don't guarantee physical contiguity - if you want that,
        ; just use OS_Memory 12 or PCI_RAMAlloc instead
        Push    "r1-r3"
        LDR     r0,=ZeroPage+PhysRamTable+4
        MOV     r2,#0                    ; current page number
        LDR     r4,=ZeroPage+CamEntriesPointer
        LDR     r4,[r4]
6569
        ADD     r4,r4,#CAM_PageFlags     ; -> base of PPLs
6570 6571 6572 6573
        LDR     r12,[r0],#8              ; get video chunk flags
20
        ADD     r2,r2,r12,LSR #12        ; advance page number
21
6574
        LDMIA   r0!,{r3,r12}             ; get next chunk details
6575 6576 6577 6578
        CMP     r12,#0
        BEQ     %FT90
        TST     r12,#OSAddRAM_NoDMA
        BNE     %BT20
6579 6580
        CMP     r3,#1:SHL:20             ; stick to lower 4GB for compatibility with old code
        BHS     %BT20
6581 6582 6583
        ; Check the CAM map to see if any pages here are free
        MOV     r12,r12,LSR #12
30
6584
        LDR     lr,[r4,r2,LSL #CAM_EntrySizeLog2]
6585
        TST     lr,#PageFlags_Unavailable :OR: PageFlags_Required
6586
        TSTEQ   lr,#PageFlags_Reserved
6587
        STREQ   r2,[r1],#MemPageBlock64_Size
6588 6589 6590 6591 6592 6593 6594 6595 6596 6597
        SUBEQS  r3,r3,#4096
        BEQ     %FT80
        SUBS    r12,r12,#1
        ADD     r2,r2,#1
        BNE     %BT30
        B       %BT21
80
        ; Success
        CLRV
        Pull    "r1-r3"
6598
        MOV     r0,#0
6599 6600 6601 6602 6603 6604 6605 6606 6607
        EXIT
90
        ; Failure
        SETV
        Pull    "r1-r3"
        MOV     r0,#0
        B       %BT05


Neil Turton's avatar
Neil Turton committed
6608 6609 6610 6611
; ***********************************************************************************
;
;       CallPostGrow - Call post-grow routine
;
6612 6613
; in:   r1 -> page block with actual pages put in if DynAreaFlags_NeedsSpecificPages, else 0
;       r2 = number of entries in block
Neil Turton's avatar
Neil Turton committed
6614 6615 6616 6617 6618
;       r3 = size of change
;       r5 = page size
;       r12 -> node for area being grown
;
; out:  All registers preserved
6619
;       Page list corrupted
Neil Turton's avatar
Neil Turton committed
6620 6621
;

6622 6623
CallPostGrow ROUT
        Entry   "r0,r3,r4, r12"
Neil Turton's avatar
Neil Turton committed
6624 6625 6626 6627
        LDR     r0, [r12, #DANode_Handler]              ; check if no handler
        CMP     r0, #0                                  ; if none (V=0)
        EXIT    EQ                                      ; then exit

Jeffrey Lee's avatar
Jeffrey Lee committed
6628 6629 6630 6631
        LDR     r4, [r12, #DANode_Flags]                ; Sparse DAs shouldn't
        TST     r4, #DynAreaFlags_SparseMap             ; get grow/shrink calls
        EXIT    NE

6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646
        ; Pack list down into 32bit form
        MOVS    r4, r1
        BEQ     %FT30
        Push    "r1, r2"
        ASSERT  MemPageBlock32_PageNum=0
        ASSERT  MemPageBlock64_PageNum=0
20
        SUBS    r2, r2, #1                              ; r2 may be 0 on entry (DoTheGrowPageUnavailable)
        LDRGE   lr, [r1], #MemPageBlock64_Size          ; Only page number is needed
        STRGE   lr, [r4], #MemPageBlock32_Size
        BGT     %BT20
        Pull    "r1, r2"

30
        ; Call handler
6647
        FastCDA_ProfStart CallPostGrow, r0, r4, lr
Neil Turton's avatar
Neil Turton committed
6648 6649 6650 6651 6652 6653
        MOV     r0, #DAHandler_PostGrow                 ; r0 = reason code
        LDR     r4, [r12, #DANode_Size]                 ; r4 = new size
        ASSERT  DANode_Handler = DANode_Workspace +4
        ADD     r12, r12, #DANode_Workspace
        MOV     lr, pc
        LDMIA   r12, {r12, pc}                          ; load workspace pointer and jump to handler
6654
        FastCDA_ProfEnd CallPostGrow, r12, r4, lr
Neil Turton's avatar
Neil Turton committed
6655 6656
        EXIT

6657
 [ ShrinkableDAs
Neil Turton's avatar
Neil Turton committed
6658 6659 6660 6661 6662 6663 6664 6665 6666 6667
; ***********************************************************************************
;
;       CallTestShrink - Call test-shrink routine
;
; in:   r11 -> area node
;
; out:  If handler exits VC, then r3 = no. of bytes area can shrink by
;       else r0 -> error block or 0 for generic error, and r3=0
;

6668
CallTestShrink Entry "r0,r4,r5, r12"
Neil Turton's avatar
Neil Turton committed
6669 6670 6671 6672 6673 6674
        LDR     r0, [r11, #DANode_Handler]              ; check if no handler
        CMP     r0, #0                                  ; if none (V=0)
        EXIT    EQ                                      ; then exit

        MOV     r0, #DAHandler_TestShrink               ; r0 = reason code
        LDR     r4, [r11, #DANode_Size]                 ; r4 = current size
Jeffrey Lee's avatar
Jeffrey Lee committed
6675
        LDR     r5, =ZeroPage
6676
        LDR     r5, [r5, #Page_Size]                    ; set r5 = page size
Neil Turton's avatar
Neil Turton committed
6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687
        ASSERT  DANode_Handler = DANode_Workspace +4
        ADD     r12, r11, #DANode_Workspace
        MOV     lr, pc
        LDMIA   r12, {r12, pc}                          ; load workspace pointer and jump to handler

; shrink amount returned by handler may not be page multiple (according to spec),
; so we'd better make it so.

        SUBVC   lr, r5, #1
        BICVC   r3, r3, lr                              ; make page multiple
        EXIT    VC
Robert Sprowson's avatar
Robert Sprowson committed
6688
        TEQ     r0, #0                                  ; if generic error returned (V still set)
Neil Turton's avatar
Neil Turton committed
6689 6690 6691 6692 6693
        ADREQL  r0, ErrorBlock_ChDynamNotAllMoved       ; then substitute real error message
 [ International
        BLEQ    TranslateError
 ]
        STR     r0, [sp]
6694
        MOV     r3, #0                                  ; indicate no shrink possible
Neil Turton's avatar
Neil Turton committed
6695 6696 6697 6698
        SETV
        EXIT
 ]

Jeffrey Lee's avatar
Jeffrey Lee committed
6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760
; ***********************************************************************************
;
;       DynArea_AbortTrapHandler - Forward AbortTrap requests to dynamic areas
;
; in:   r0-r3 = AbortTrap args
;       r12 -> DANode
;
; out:  If handler exits VC, then r3 = no. of bytes area can shrink by
;       else r0 -> error block or 0 for generic error, and r3=0
;
DynArea_AbortTrapHandler ROUT
        Entry   "r0-r6,r12"
        ; Shuffle everything up so that the AbortTrap args are in R1-R4
        MOV     r4, r3
        MOV     r3, r2
        MOV     r2, r1
        MOV     r1, r0
        ; Prepare the other arguments needed by the DA handler
        LDR     r5, [r12, #DANode_Base]
        LDR     lr, [r12, #DANode_Size]
        ADD     r5, r5, lr
        MOV     r0, #DAHandler_Abort
        LDR     r6, [r12, #DANode_Handler]
        LDR     r12, [r12, #DANode_Workspace]
        ; Call the DA's handler
        ;  In: R0 = DAHandler_Abort (5)
        ;      R1-R4 as AbortTrap handler R0-R3
        ;      R5 = current end of allocated portion of dynamic area
        ;           (invalid for sparse or PMP areas)
        ;      R12 = DA handler workspace
        ; Out: R4 = amount of data processed (== entry R4 if all processed)
        ;      On failure, V set with R0 pointing to error block
        ;      All other regs preserived
      [ NoARMv5
        MOV     lr, pc
        MOV     pc, r6
      |
        BLX     r6
      ]
        BVS     %FT90
        ; Assume it's a failure if output R4 doesn't match the input length
        FRAMLDR r3
        TEQ     r3, r4
        EXIT    EQ
        MOV     r0, #0 ; Use the generic error
90
        ; Not mentioned in ROL's spec, but for consistency with other reason
        ; codes, we'll allow VS with R0=0 to indicate that a default error
        ; should be used.
        TEQ     r0, #0
        ADREQ   r0, ErrorBlock_AbortableDAFailure
        SETV
        FRAMSTR r0
        EXIT

        ; This error should never be visible outside the kernel, so no need
        ; to worry about assigning it an error number, or internationalisation.
ErrorBlock_AbortableDAFailure
        DCD     0
        =       "Abortable DA didn't handle AbortTrap request", 0
        ALIGN

Neil Turton's avatar
Neil Turton committed
6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791
; ***********************************************************************************
;
;       DynAreaHandler_SysHeap - Dynamic area handler for system heap
;       DynAreaHandler_RMA     - Dynamic area handler for RMA
;
; in:   r0 = reason code (0=>pre-grow, 1=>post-grow, 2=>pre-shrink, 3=>post-shrink)
;       r12 -> base of area
;

DynAreaHandler_SysHeap
DynAreaHandler_RMA ROUT
        CMP     r0, #4
        ADDCC   pc, pc, r0, LSL #2
        B       UnknownHandlerError
        B       PreGrow_Heap
        B       PostGrow_Heap
        B       PreShrink_Heap
        B       PostShrink_Heap

PostGrow_Heap
PostShrink_Heap
        STR     r4, [r12, #:INDEX:hpdend] ; store new size

; and drop thru to...

PreGrow_Heap
        CLRV                            ; don't need to do anything here
        MOV     pc, lr                  ; so just exit

PreShrink_Heap
        Push    "r0, lr"
Kevin Bracey's avatar
Kevin Bracey committed
6792
        PHPSEI                          ; disable IRQs round this bit
Neil Turton's avatar
Neil Turton committed
6793 6794 6795 6796 6797 6798 6799 6800 6801
        LDR     r0, [r12, #:INDEX:hpdbase]      ; get minimum size
        SUB     r0, r4, r0              ; r0 = current-minimum = max shrink
        CMP     r3, r0                  ; if requested shrink > max
        MOVHI   r3, r0                  ; then limit it
        SUB     r0, r5, #1              ; r0 = page mask
        BIC     r3, r3, r0              ; round size change down to page multiple
        SUB     r0, r4, r3              ; area size after shrink
        STR     r0, [r12, #:INDEX:hpdend] ; update size

Kevin Bracey's avatar
Kevin Bracey committed
6802
        PLP                             ; restore IRQ status
Neil Turton's avatar
Neil Turton committed
6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815
        CLRV
        Pull    "r0, pc"

AreaName_RMA
        =       "Module area", 0
        ALIGN


UnknownHandlerError
        Push    "lr"
        ADRL    r0, ErrorBlock_UnknownAreaHandler
  [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
6816
  |
Neil Turton's avatar
Neil Turton committed
6817
        SETV
Robert Sprowson's avatar
Robert Sprowson committed
6818
  ]
Neil Turton's avatar
Neil Turton committed
6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830
        Pull    "pc"

DynAreaHandler_Sprites
        CMP     r0, #4
        ADDCC   pc, pc, r0, LSL #2
        B       UnknownHandlerError
        B       PreGrow_Sprite
        B       PostGrow_Sprite
        B       PreShrink_Sprite
        B       PostShrink_Sprite

PostGrow_Sprite
6831
PostShrink_Sprite Entry "r0"
Neil Turton's avatar
Neil Turton committed
6832 6833 6834

; in - r3 = size change (+ve), r4 = new size, r5 = page size

Jeffrey Lee's avatar
Jeffrey Lee committed
6835
        LDR     lr, =ZeroPage+VduDriverWorkSpace
Neil Turton's avatar
Neil Turton committed
6836 6837 6838 6839
        TEQ     r4, #0                  ; if new size = 0
        STREQ   r4, [lr, #SpAreaStart]  ; then set area ptr to zero
        STRNE   r12, [lr, #SpAreaStart] ; else store base address

Jeffrey Lee's avatar
Jeffrey Lee committed
6840
        LDR     r0, =ZeroPage
Neil Turton's avatar
Neil Turton committed
6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859
        LDR     lr, [r0, #SpriteSize]   ; load old size
        STR     r4, [r0, #SpriteSize]   ; and store new size
        BEQ     %FT10                   ; if new size is zero, don't try to update header

        STR     r4, [r12, #saEnd]       ; store new size in header
        TEQ     lr, #0                  ; if old size was zero
        STREQ   lr, [r12, #saNumber]    ; then initialise header (no. of sprites = 0)
        MOVEQ   lr, #saExten
        STREQ   lr, [r12, #saFirst]     ; ptr to first sprite -> after header
        STREQ   lr, [r12, #saFree]      ; ptr to first free byte -> after header

10
        CLRV                            ; don't need to do anything here
        EXIT                            ; so just exit

PreGrow_Sprite
        CLRV                            ; don't need to do anything here
        MOV     pc, lr                  ; so just exit

6860
PreShrink_Sprite Entry "r0"
Neil Turton's avatar
Neil Turton committed
6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881
        TEQ     r4, #0                  ; if current size is zero
        BEQ     %FT10                   ; then any shrink is OK (shouldn't happen)

        LDR     r0, [r12, #saFree]      ; get used amount
        TEQ     r0, #saExten            ; if only header used,
        MOVEQ   r0, #0                  ; then none really in use

        SUB     r0, r4, r0              ; r0 = current-minimum = max shrink
        CMP     r3, r0                  ; if requested shrink > max
        MOVHI   r3, r0                  ; then limit it
        SUB     r0, r5, #1              ; r0 = page mask
        BIC     r3, r3, r0              ; round size change down to page multiple
10
        CLRV
        EXIT

AreaName_SpriteArea
        =       "System sprites", 0
        ALIGN


6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905
DynAreaHandler_RAMDisc ROUT
 [ PMPRAMFS
        CMP     r0, #DAHandler_ResizePMP
        BNE     UnknownHandlerError
        ; r1 = change amount (pages)
        ; r2 = DA number
        Entry   "r2-r11"
        MOV     r10, r1
        MOV     r11, r2
        ; Get current size
        MOV     r0, #DAReason_PMP_GetInfo
        MOV     r1, r2
        SWI     XOS_DynamicArea
        ; R6 = current phys page count
        ; R7 = max size
        ; Check if resizing is allowed
        BLVC    PreGrow_RAMDisc
        EXIT    VS
        CMP     r10, #0
        BLT     RAMDisc_Shrink
        EXIT    EQ
        ; Claim empty space
        MOV     r0, #DAReason_PMP_Resize
      [ {TRUE}
Julie Stamp's avatar
Julie Stamp committed
6906 6907 6908
        ; Limit max size to 2GB-4KB - RAMFS and other bits of the OS won't go past that currently
        LDR     r2, =DynArea_PMP_BigByteCount:SHR:12
        SUB     r2, r2, r6 ; R2 = pages left till maximum
6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990
        CMP     r10, r2
        MOVGT   r10, r2
      ]
        SUB     r2, r6, r7 ; Take into account any mismatch between current max and current page count
        ADDS    r2, r2, r10
        MOV     r9, r2
        SWINE   XOS_DynamicArea
        EXIT    VS
        ; R2 = actual change, use that from now on
        CMP     r9, #0
        ADDGE   r10, r7, r2 ; New PMPMaxSize
        SUBLT   r10, r7, r2 ; Take into account the fact the result is unsigned
        SUB     r10, r10, r6 ; Amount we need to change PMPSize
        ; Claim pages
        MOV     r7, #-2
        MOV     r9, #0
10
        SUBS    r10, r10, #1
        BLT     RAMDisc_PostOp
        Push    "r6,r7,r9"
        MOV     r0, #DAReason_PMP_PhysOp
        MOV     r2, sp
        MOV     r3, #1
        SWI     XOS_DynamicArea
        ADD     sp, sp, #12
        ADDVC   r6, r6, #1
        BVC     %BT10
        B       RAMDisc_PostOp
RAMDisc_Shrink
        ; Unmap all pages (RAMFS will unmap everything on reinit anyway)
        MOV     r0, #0
        MOV     r2, #-1
        MOV     r4, #0
        Push    "r0,r2,r4"
        MOV     r0, #DAReason_PMP_LogOp
15
        MOV     r2, sp
        MOV     r3, #1
        SWI     XOS_DynamicArea
        BVS     %FT16
        ADD     r4, r4, #1
        CMP     r4, #PMPRAMFS_Size
        STRLT   r4, [sp]
        BLT     %BT15
16
        ADD     sp, sp, #12
        ; Release pages
        MOV     r7, #-1
        MOV     r9, #0
        RSB     r10, r10, #0
20
        SUBS    r10, r10, #1
        SUBGES  r6, r6, #1
        BLT     RAMDisc_PostShrink
        Push    "r6,r7,r9"
        MOV     r0, #DAReason_PMP_PhysOp
        MOV     r2, sp
        MOV     r3, #1
        SWI     XOS_DynamicArea
        ADD     sp, sp, #12
        BVC     %BT20
RAMDisc_PostShrink
        ; Release empty space so that things should stay in sync
        MRS     r2, CPSR
        Push    "r0,r2"
        MOV     r0, #DAReason_PMP_GetInfo
        SWI     XOS_DynamicArea
        BVS     %FT30
        SUBS    r2, r6, r7 ; Release all spare page entries
        MOV     r0, #DAReason_PMP_Resize
        SWINE   XOS_DynamicArea
30
        Pull    "r0,r2"
        MSR     CPSR_f, r2
RAMDisc_PostOp
        ; Re-init if necessary
        MRS     r2, CPSR
        BL      PostGrow_RAMDisc
        MSR     CPSR_f, r2
        EXIT

 | ; PMPRAMFS
Neil Turton's avatar
Neil Turton committed
6991 6992 6993 6994 6995 6996 6997
        CMP     r0, #4
        ADDCC   pc, pc, r0, LSL #2
        B       UnknownHandlerError
        B       PreGrow_RAMDisc
        B       PostGrow_RAMDisc
        B       PreShrink_RAMDisc
        B       PostShrink_RAMDisc
6998
 ] ; PMPRAMFS
Neil Turton's avatar
Neil Turton committed
6999 7000

PostGrow_RAMDisc
7001
PostShrink_RAMDisc Entry "r0-r6"
Neil Turton's avatar
Neil Turton committed
7002 7003 7004 7005 7006 7007 7008 7009

; in - r3 = size change (+ve), r4 = new size, r5 = page size
; but we don't really care about any of these

; The only thing we have to do here is ReInit RAMFS, but NOT if
; a) no modules are initialised yet (eg when we're created), or
; b) RAMFS has been unplugged

Jeffrey Lee's avatar
Jeffrey Lee committed
7010
        LDR     r0, =ZeroPage
Neil Turton's avatar
Neil Turton committed
7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043
        LDR     r0, [r0, #Module_List]
        TEQ     r0, #0                  ; any modules yet?
        BEQ     %FT90                   ; no, then don't do anything

        MOV     r0, #ModHandReason_EnumerateROM_Modules
        MOV     r1, #0
        MOV     r2, #-1                 ; enumerate ROM modules looking for RAMFS
10
        SWI     XOS_Module
        BVS     %FT50                   ; no more modules, so it can't be unplugged
        ADR     r5, ramfsname
20
        LDRB    r6, [r3], #1            ; get char from returned module name
        CMP     r6, #" "                ; if a terminator then we have a match
        BLS     %FT30                   ; so check for unplugged
        LowerCase r6, lr                ; else force char to lower case
        LDRB    lr, [r5], #1            ; get char from "ramfs" string
        CMP     lr, r6                  ; and if matches
        BEQ     %BT20                   ; then try next char
        B       %BT10                   ; else try next module

30
        CMP     r4, #-1                 ; is module unplugged?
        BEQ     %FT90                   ; if so, then mustn't reinit it
50
        MOV     r0, #ModHandReason_ReInit ; reinit module
        ADR     r1, ramfsname
        SWI     XOS_Module              ; ignore any errors from this
90
        CLRV
        EXIT

PreGrow_RAMDisc
7044
PreShrink_RAMDisc Entry "r0-r5"
Jeffrey Lee's avatar
Jeffrey Lee committed
7045
        LDR     r0, =ZeroPage
Neil Turton's avatar
Neil Turton committed
7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058
        LDR     r0, [r0, #Module_List]  ; first check if any modules going
        TEQ     r0, #0
        BEQ     %FT90                   ; if not, don't look at filing system

        MOV     r0, #5
        ADR     r1, ramcolondollardotstar
        SWI     XOS_File
        CMPVC   r0, #0
        BVS     %FT90                   ; if no RAMFS then change OK
        BEQ     %FT90                   ; or if no files, then change OK
        ADR     r0, ErrorBlock_RAMFsUnchangeable
 [ International
        BL      TranslateError
Robert Sprowson's avatar
Robert Sprowson committed
7059 7060
 |
        SETV
Neil Turton's avatar
Neil Turton committed
7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088
 ]
        STR     r0, [sp]
        EXIT

90
        CLRV
        EXIT

        MakeErrorBlock  RAMFsUnchangeable

AreaName_RAMDisc
        =       "RAM disc", 0
ramcolondollardotstar
        =       "ram:$.*", 0
ramfsname
        =       "ramfs", 0
        ALIGN


DynAreaHandler_FontArea
        CMP     r0, #4
        ADDCC   pc, pc, r0, LSL #2
        B       UnknownHandlerError
        B       PreGrow_FontArea
        B       PostGrow_FontArea
        B       PreShrink_FontArea
        B       PostShrink_FontArea

7089
PostGrow_FontArea Entry "r0-r2"
Neil Turton's avatar
Neil Turton committed
7090 7091 7092

; in - r3 = size change (+ve), r4 = new size, r5 = page size

Jeffrey Lee's avatar
Jeffrey Lee committed
7093
        LDR     r1, =ZeroPage
Neil Turton's avatar
Neil Turton committed
7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105
        LDR     r1, [r1, #Module_List]  ; any modules active?
        TEQ     r1, #0
        MOVNE   r1, r4                  ; there are, so inform font manager of size change
        SWINE   XFont_ChangeArea
        CLRV
        EXIT

PostShrink_FontArea
PreGrow_FontArea
        CLRV                            ; don't need to do anything here
        MOV     pc, lr                  ; so just exit

7106
PreShrink_FontArea Entry "r0-r2"
Neil Turton's avatar
Neil Turton committed
7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218
        MOV     r1, #-1                 ; ask font manager for minimum size of font area
        MOV     r2, #0                  ; default value if no font manager
        SWI     XFont_ChangeArea        ; out: r2 = minimum size

        SUB     r0, r4, r2              ; r0 = current-minimum = max shrink
        CMP     r3, r0                  ; if requested shrink > max
        MOVHI   r3, r0                  ; then limit it
        SUB     r0, r5, #1              ; r0 = page mask
        BIC     r3, r3, r0              ; round size change down to page multiple

        SUB     r1, r4, r3              ; r1 = new size
        SWI     XFont_ChangeArea        ; tell font manager to reduce usage

        CLRV
        EXIT

AreaName_FontArea
        =       "Font cache", 0
        ALIGN


; **** New screen stuff ****
;
;
; This source collects together all the new routines needed to make
; the screen into a new dynamic area.
;
; It has the following dependencies elsewhere in the kernel before
; it can be expected to work:
;
; * Definition of AP_Screen in ChangeDyn needs doubly_mapped and
;   name_is_token bits set
; * name_is_token handling needs adding (or scrap the bit designation)
; * Call to CreateNewScreenArea from NewReset to create area
; * Tim says doubly-mapped areas are broken - this must be fixed first
; * Old CDA routine may be retired, since screen is its last client
; * Has Tim completed the rest of this work?
;
; Once these routines work, they should be grafted into appropriate
; places in the kernel sources
;
; This source is not intended for stand-alone assembly: it should be
; plumbed into the kernel source build
;
; Version history - remove this once integrated with kernel sources
;
; Vsn  Date      Who  What
; ---  --------  ---  ----------------------------------------------
; 000  23/08/93  amg  Written
; 001  24/08/93  amg  Fixes and changes following review by TMD
; 002  03/09/93  tmd  Updated to work!

; *********************************************************************
; Create a new style dynamic area for the screen
; *********************************************************************

; Entry requirements
; none

AreaName_Screen
        =       "Screen memory",0               ;needs replacing with message token
        ALIGN

; *********************************************************************
; Handler despatch routine for screen dynamic area
; *********************************************************************

DynAreaHandler_Screen                           ;despatch routine for pre/post grow/shrink handlers
        CMP     r0, #4
        ADDCC   pc, pc, R0, LSL #2
        B       UnknownHandlerError             ;already defined in ChangeDyn
        B       PreGrow_Screen                  ;the rest are defined here
        B       PostGrow_Screen
        B       PreShrink_Screen
        B       PostShrink_Screen

;The sequence of events which these handlers must do is:
;
;Grow Screen
;
;Pre : Remove cursors
;      Work out which physical page numbers are needed and return a list
;CDA : Move existing pages lower in memory within first copy (ie change logical address
;        associated with physical pages)
;      Locate and free the next physical pages in line (if used a page swap must occur)
;      Assign the new pages logical addresses in the gap between the end of the present
;        logical range and the start of the second physical range
;Post: Adjust screen memory contents & screen start addresses to retain screen display
;
;Shrink Screen
;
;Pre : Remove cursors
;      Adjust screen memory contents & screen start addresses to retain screen display
;CDA : Move pages from screen to free pool (creates a gap in first logical range)
;      Close up the gap in logical addressing
;Post: Restore cursors
;

; ***********************************************************************************
; Handlers for the screen dynamic area
; ***********************************************************************************

;Pregrow entry parameters
; R0 = 0 (reason code)
; R1 -> page block (entries set to -1)
; R2 = number of entries in page block == number of pages area is growing by
; R3 = number of bytes area is growing by (r2 * pagesize)
; R4 = current size (bytes)
; R5 = page size
;
; exit with V clear, all preserved

7219
PreGrow_Screen  Entry   "r0-r2,r4"
Neil Turton's avatar
Neil Turton committed
7220
        LDR     r0, [WsPtr, #CursorFlags]       ; test if VDU inited yet
Kevin Bracey's avatar
Kevin Bracey committed
7221
        LDRB    lr, [WsPtr, #ExternalFramestore]
Neil Turton's avatar
Neil Turton committed
7222
        TEQ     r0, #0                          ; if not, CursorFlags will be zero
Kevin Bracey's avatar
Kevin Bracey committed
7223 7224 7225
        BEQ     %FT05
        TEQ     lr, #0
        SWIEQ   XOS_RemoveCursors               ; if VDU inited, then remove cursors
Neil Turton's avatar
Neil Turton committed
7226

Kevin Bracey's avatar
Kevin Bracey committed
7227
05      ADRL    r0, PageShifts-1
Neil Turton's avatar
Neil Turton committed
7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249
        LDRB    r0, [r0, r5, LSR #12]           ; grab log2Pagesize for shifting
        MOV     r4, r4, LSR r0                  ; change present size into number of pages
                                                ; since page numbers are 0 to n-1 thus n
                                                ; is the first page number we want to insist on
10
        STR     r4, [r1], #12                   ; store physical page number and increment to next
        SUBS    r2, r2, #1                      ; one less to do
        ADDNE   r4, r4, #1                      ; next physical page number
        BNE     %BT10                           ; continue until all pages done
        CLRV                                    ; ok, so I'm paranoid...
        EXIT

; **********************************************************************

;PostGrow entry parameters
;R0 = 1 (reason code)
;R1 -> page block (only physical page numbers are meaningful)
;R2 = number of entries in page block (= number of pages area grew by)
;R3 = number of bytes area grew by
;R4 = new size of area (bytes)
;R5 = page size

7250
PostGrow_Screen Entry   "r0,r5"
Neil Turton's avatar
Neil Turton committed
7251 7252 7253 7254
        LDR     r0, [WsPtr, #CursorFlags]       ; test if VDU inited (CursorFlags=0 => not)
        TEQ     r0, #0
        BEQ     %FT90                           ; if not inited, do nothing

Kevin Bracey's avatar
Kevin Bracey committed
7255
        PHPSEI  r5                              ; disable IRQs
Neil Turton's avatar
Neil Turton committed
7256 7257 7258 7259

        MOV     r0, r3                          ; move number of bytes area grew by into r0
        BL      InsertPages                     ; only call InsertPages if VDU inited

Kevin Bracey's avatar
Kevin Bracey committed
7260
        PLP     r5                              ; restore IRQ state
Neil Turton's avatar
Neil Turton committed
7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274
        SWI     XOS_RestoreCursors              ; and restore cursors
90
        CLRV
        EXIT

; ***********************************************************************

;PreShrink Entry parameters
;R0 = 2 (reason code)
;R3 = number of bytes area is shrinking by
;R4 = current size of area (bytes)
;R5 = page size
;R12 = vdu workspace

7275
PreShrink_Screen Entry   "R0-R2,R4-R5"
Neil Turton's avatar
Neil Turton committed
7276 7277 7278 7279 7280

        ;need to check whether the proposed shrink still leaves enough for
        ;the current amount needed by the vdu drivers, if it doesn't we
        ;reduce R3 to be the most we can spare (in whole pages)

Jeffrey Lee's avatar
Jeffrey Lee committed
7281
        LDR     LR, =ZeroPage
7282 7283
        LDR     LR, [LR, #VideoSizeFlags]          ;is VRAM suitable for general use?
        TST     LR, #OSAddRAM_VRAMNotForGeneralUse ;if not - don't shrink screen memory
Kevin Bracey's avatar
Kevin Bracey committed
7284
        MOVNE   R3, #0
Neil Turton's avatar
Neil Turton committed
7285 7286
        SUB     R2, R5, #1                      ;make a page mask

Kevin Bracey's avatar
Kevin Bracey committed
7287 7288 7289 7290 7291 7292 7293 7294 7295
        LDRB    R14, [R12, #ExternalFramestore]
        TEQ     R14, #0                         ;okay to shrink if using external framestore
        BEQ     %FT12
        RSB     R0, R3, #0                      ;R0= -(number of bytes) for RemovePages
        BL      RemovePages
        CLRV
        EXIT

12      LDR     R5, [R12, #ScreenSize]          ;get current minimum size
Neil Turton's avatar
Neil Turton committed
7296 7297 7298 7299 7300 7301 7302 7303

        SUB     R1, R4, R5                      ;R1 = maximum shrink (current - screensize)
        CMP     R3, R1                          ;if requested shrink > max...
        MOVHI   R3, R1                          ;...then limit it, and...
        BICS    R3, R3, R2                      ;...round down to multiple of page size
        BEQ     %FT10                           ;don't shuffle screen data if resultant
                                                ;shrink is 0 bytes/0 pages
        SWI     XOS_RemoveCursors
Kevin Bracey's avatar
Kevin Bracey committed
7304
        PHPSEI  R5                              ;disable interrupts
Neil Turton's avatar
Neil Turton committed
7305 7306
        RSB     R0, R3, #0                      ;R0= -(number of bytes) for RemovePages
        BL      RemovePages                     ;entry: R0 = -(number of bytes)
Kevin Bracey's avatar
Kevin Bracey committed
7307
        PLP     R5                              ;restore interrupts
Neil Turton's avatar
Neil Turton committed
7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319
10
        CLRV
        EXIT

; ************************************************************************

;PostShrink Entry parameters
;R0 = 3 (reason code)
;R3 = number of bytes area shrank by
;R4 = new size of area (bytes)
;R5 = page size

7320
PostShrink_Screen Entry
Neil Turton's avatar
Neil Turton committed
7321 7322 7323 7324 7325 7326
        SWI     XOS_RestoreCursors
        CLRV                                    ;ok, so I'm paranoid...
        EXIT

; ************************************************************************

7327
        LTORG
Neil Turton's avatar
Neil Turton committed
7328

7329 7330 7331 7332 7333 7334 7335 7336
      [ PMPParanoid
ValidatePMPs ROUT
        EntryS  "r0-r12"
        ; Validate PMPs against the CAM
        LDR     r0, =ZeroPage
        LDR     r1, [r0, #DAList]
        LDR     r2, [r0, #CamEntriesPointer]
        LDR     r3, [r0, #MaxCamEntry]
7337
        MOV     lr, #0
7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377
10
        LDR     r4, [r1, #DANode_Flags]
        TST     r4, #DynAreaFlags_PMP
        BEQ     %FT25
        LDR     r5, [r1, #DANode_PMP]
        LDR     r6, [r1, #DANode_PMPSize]
        LDR     r7, [r1, #DANode_PMPMaxSize]
        LDR     r0, [r1, #DANode_Size]
        CMP     r6, r7
        BLHI    %FT90
15
        SUBS    r7, r7, #1
        BLO     %FT20
        LDR     r8, [r5, r7, LSL #2]
        CMP     r8, #-1
        BEQ     %BT15
        CMP     r8, r3
        BLHI    %FT90
        ADD     r9, r2, r8, LSL #CAM_EntrySizeLog2
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIA   r9, {r9-r12}
        TST     r10, #DynAreaFlags_PMP
        BLEQ    %FT90
        CMP     r11, r1
        CMPEQ   r12, r7
        BLNE    %FT90
        SUBS    r6, r6, #1
        BLLO    %FT90
        LDR     r10, =Nowhere
        CMP     r9, r10
        SUBNE   r0, r0, #4096
        B       %BT15
20
        CMP     r6, #0
        CMPEQ   r0, #0
        BLNE    %FT90
25
7378 7379 7380
        CMP     lr, #0
        BNE     %FT26
        ; Iterate through regular DAs
7381 7382 7383
        LDR     r1, [r1, #DANode_Link]
        CMP     r1, #0
        BNE     %BT10
7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396
        ; Iterate through AMBControl nodes
        LDR     r12, =ZeroPage+AMBControl_ws
        LDR     r12, [r12]
        CMP     r12, #0
        BEQ     %FT29
        ADR     lr, AMBAnchorNode
        ADD     r1, lr, #AMBNode_DANode
26
        LDR     r1, [r1, #AMBNode_next-AMBNode_DANode]
        CMP     r1, lr
        ADDNE   r1, r1, #AMBNode_DANode
        BNE     %BT10
29
7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444
        ; Validate CAM against PMPs
        MOV     r0, #0
30
        ASSERT  CAM_LogAddr=0
        ASSERT  CAM_PageFlags=4
        ASSERT  CAM_PMP=8
        ASSERT  CAM_PMPIndex=12
        LDMIA   r2!, {r4-r7}
        TST     r5, #DynAreaFlags_PMP
        BEQ     %FT35
        LDR     r8, [r6, #DANode_Flags]
        TST     r8, #DynAreaFlags_PMP
        BLEQ    %FT90
        LDR     r8, [r6, #DANode_PMP]
        LDR     r9, [r6, #DANode_PMPMaxSize]
        LDR     r10, [r6, #DANode_PMPSize]
        CMP     r7, r9
        BLHS    %FT90
        LDR     r8, [r8, r7, LSL #2]
        CMP     r0, r8
        BLNE    %FT90
35
        ADD     r0, r0, #1
        CMP     r0, r3
        BLS     %BT30
        EXITS

90
        Push    "lr"
        DebugTX "PMP corrupt"
        DebugReg r0
        DebugReg r1
        DebugReg r2
        DebugReg r3
        DebugReg r4
        DebugReg r5
        DebugReg r6
        DebugReg r7
        DebugReg r8
        DebugReg r9
        DebugReg r10
        DebugReg r11
        DebugReg r12
        Pull     "r0"
        DebugReg r0
        B        .
      ] ; PMPParanoid

Neil Turton's avatar
Neil Turton committed
7445
        END