Artifact
c0f9160e791be92e630a2f72b94be6f5e1c93984:
- File
kregion.fs
— part of check-in
[295cbe28ec]
at
2019-10-03 14:35:43
on branch trunk
— HOSTPREFIX env variable
(user:
bernd
size: 2968)
0000: 5c 20 63 72 79 70 74 6f 20 72 65 67 69 6f 6e 20 \ crypto region
0010: 62 61 73 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e based allocation
0020: 0a 0a 5c 20 43 6f 70 79 72 69 67 68 74 20 c2 a9 ..\ Copyright ©
0030: 20 32 30 31 34 20 20 20 42 65 72 6e 64 20 50 61 2014 Bernd Pa
0040: 79 73 61 6e 0a 0a 5c 20 54 68 69 73 20 70 72 6f ysan..\ This pro
0050: 67 72 61 6d 20 69 73 20 66 72 65 65 20 73 6f 66 gram is free sof
0060: 74 77 61 72 65 3a 20 79 6f 75 20 63 61 6e 20 72 tware: you can r
0070: 65 64 69 73 74 72 69 62 75 74 65 20 69 74 20 61 edistribute it a
0080: 6e 64 2f 6f 72 20 6d 6f 64 69 66 79 0a 5c 20 69 nd/or modify.\ i
0090: 74 20 75 6e 64 65 72 20 74 68 65 20 74 65 72 6d t under the term
00a0: 73 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 66 s of the GNU Aff
00b0: 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 6c ero General Publ
00c0: 69 63 20 4c 69 63 65 6e 73 65 20 61 73 20 70 75 ic License as pu
00d0: 62 6c 69 73 68 65 64 20 62 79 0a 5c 20 74 68 65 blished by.\ the
00e0: 20 46 72 65 65 20 53 6f 66 74 77 61 72 65 20 46 Free Software F
00f0: 6f 75 6e 64 61 74 69 6f 6e 2c 20 65 69 74 68 65 oundation, eithe
0100: 72 20 76 65 72 73 69 6f 6e 20 33 20 6f 66 20 74 r version 3 of t
0110: 68 65 20 4c 69 63 65 6e 73 65 2c 20 6f 72 0a 5c he License, or.\
0120: 20 28 61 74 20 79 6f 75 72 20 6f 70 74 69 6f 6e (at your option
0130: 29 20 61 6e 79 20 6c 61 74 65 72 20 76 65 72 73 ) any later vers
0140: 69 6f 6e 2e 0a 0a 5c 20 54 68 69 73 20 70 72 6f ion...\ This pro
0150: 67 72 61 6d 20 69 73 20 64 69 73 74 72 69 62 75 gram is distribu
0160: 74 65 64 20 69 6e 20 74 68 65 20 68 6f 70 65 20 ted in the hope
0170: 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 that it will be
0180: 75 73 65 66 75 6c 2c 0a 5c 20 62 75 74 20 57 49 useful,.\ but WI
0190: 54 48 4f 55 54 20 41 4e 59 20 57 41 52 52 41 4e THOUT ANY WARRAN
01a0: 54 59 3b 20 77 69 74 68 6f 75 74 20 65 76 65 6e TY; without even
01b0: 20 74 68 65 20 69 6d 70 6c 69 65 64 20 77 61 72 the implied war
01c0: 72 61 6e 74 79 20 6f 66 0a 5c 20 4d 45 52 43 48 ranty of.\ MERCH
01d0: 41 4e 54 41 42 49 4c 49 54 59 20 6f 72 20 46 49 ANTABILITY or FI
01e0: 54 4e 45 53 53 20 46 4f 52 20 41 20 50 41 52 54 TNESS FOR A PART
01f0: 49 43 55 4c 41 52 20 50 55 52 50 4f 53 45 2e 20 ICULAR PURPOSE.
0200: 20 53 65 65 20 74 68 65 0a 5c 20 47 4e 55 20 41 See the.\ GNU A
0210: 66 66 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 ffero General Pu
0220: 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f 72 blic License for
0230: 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a 0a more details...
0240: 5c 20 59 6f 75 20 73 68 6f 75 6c 64 20 68 61 76 \ You should hav
0250: 65 20 72 65 63 65 69 76 65 64 20 61 20 63 6f 70 e received a cop
0260: 79 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 66 y of the GNU Aff
0270: 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 6c ero General Publ
0280: 69 63 20 4c 69 63 65 6e 73 65 0a 5c 20 61 6c 6f ic License.\ alo
0290: 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 6f ng with this pro
02a0: 67 72 61 6d 2e 20 20 49 66 20 6e 6f 74 2c 20 73 gram. If not, s
02b0: 65 65 20 3c 68 74 74 70 3a 2f 2f 77 77 77 2e 67 ee <http://www.g
02c0: 6e 75 2e 6f 72 67 2f 6c 69 63 65 6e 73 65 73 2f nu.org/licenses/
02d0: 3e 2e 0a 0a 32 56 61 72 69 61 62 6c 65 20 6b 72 >...2Variable kr
02e0: 65 67 69 6f 6e 20 5c 20 63 75 72 72 65 6e 74 20 egion \ current
02f0: 72 65 67 69 6f 6e 20 70 6f 69 6e 74 65 72 20 2b region pointer +
0300: 20 72 65 6d 61 69 6e 64 65 72 0a 56 61 72 69 61 remainder.Varia
0310: 62 6c 65 20 6b 66 72 65 65 36 34 27 20 5c 20 66 ble kfree64' \ f
0320: 72 65 65 20 6c 69 73 74 20 66 6f 72 20 36 34 20 ree list for 64
0330: 62 79 74 65 73 20 6b 65 79 73 0a 0a 24 34 30 30 bytes keys..$400
0340: 30 20 43 6f 6e 73 74 61 6e 74 20 2f 6b 72 65 67 0 Constant /kreg
0350: 69 6f 6e 0a 24 31 30 30 30 30 20 43 6f 6e 73 74 ion.$10000 Const
0360: 61 6e 74 20 2f 6b 72 65 67 69 6f 6e 2d 6d 61 78 ant /kregion-max
0370: 20 5c 20 74 68 65 20 75 73 75 61 6c 20 6d 61 78 \ the usual max
0380: 69 6d 75 6d 20 6c 6f 63 6b 65 64 20 6d 65 6d 6f imum locked memo
0390: 72 79 20 69 73 20 70 61 74 68 65 74 69 63 0a 0a ry is pathetic..
03a0: 24 32 30 20 43 6f 6e 73 74 61 6e 74 20 63 72 79 $20 Constant cry
03b0: 70 74 2d 61 6c 69 67 6e 0a 0a 30 20 56 61 6c 75 pt-align..0 Valu
03c0: 65 20 2f 6b 72 65 67 69 6f 6e 23 0a 0a 3a 20 6b e /kregion#..: k
03d0: 61 6c 69 67 6e 20 28 20 61 64 64 72 20 2d 2d 20 align ( addr --
03e0: 61 64 64 72 27 20 29 0a 20 20 20 20 5b 20 63 72 addr' ). [ cr
03f0: 79 70 74 2d 61 6c 69 67 6e 20 31 2d 20 5d 4c 20 ypt-align 1- ]L
0400: 2b 20 5b 20 63 72 79 70 74 2d 61 6c 69 67 6e 20 + [ crypt-align
0410: 6e 65 67 61 74 65 20 5d 4c 20 61 6e 64 20 3b 0a negate ]L and ;.
0420: 0a 3a 20 6b 61 6c 6c 6f 63 20 28 20 6c 65 6e 20 .: kalloc ( len
0430: 2d 2d 20 61 64 64 72 20 29 0a 20 20 20 20 5c 47 -- addr ). \G
0440: 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c 65 6e 20 allocate a len
0450: 62 79 74 65 20 62 6c 6f 63 6b 20 6f 66 20 6e 6f byte block of no
0460: 6e 2d 73 77 61 70 70 61 62 6c 65 20 73 74 75 66 n-swappable stuf
0470: 66 0a 20 20 20 20 6b 61 6c 69 67 6e 20 3e 72 0a f. kalign >r.
0480: 20 20 20 20 72 40 20 2f 6b 72 65 67 69 6f 6e 20 r@ /kregion
0490: 75 3e 20 21 21 6b 72 2d 73 69 7a 65 21 21 0a 20 u> !!kr-size!!.
04a0: 20 20 20 6b 72 65 67 69 6f 6e 20 32 40 20 64 75 kregion 2@ du
04b0: 70 20 72 40 20 75 3c 20 49 46 0a 09 2f 6b 72 65 p r@ u< IF../kre
04c0: 67 69 6f 6e 20 2b 74 6f 20 2f 6b 72 65 67 69 6f gion +to /kregio
04d0: 6e 23 20 20 32 64 72 6f 70 20 2f 6b 72 65 67 69 n# 2drop /kregi
04e0: 6f 6e 0a 09 5c 20 77 65 20 68 61 76 65 20 74 6f on..\ we have to
04f0: 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 6c fall back to al
0500: 6c 6f 63 2b 67 75 61 72 64 20 69 66 20 77 65 20 loc+guard if we
0510: 77 61 6e 74 20 6d 6f 72 65 20 74 68 61 6e 20 36 want more than 6
0520: 34 6b 0a 09 2f 6b 72 65 67 69 6f 6e 23 20 2f 6b 4k../kregion# /k
0530: 72 65 67 69 6f 6e 2d 6d 61 78 20 75 3e 20 49 46 region-max u> IF
0540: 20 20 61 6c 6c 6f 63 2b 67 75 61 72 64 20 20 45 alloc+guard E
0550: 4c 53 45 20 20 61 6c 6c 6f 63 2b 6c 6f 63 6b 20 LSE alloc+lock
0560: 20 54 48 45 4e 0a 09 2f 6b 72 65 67 69 6f 6e 20 THEN../kregion
0570: 32 64 75 70 20 6b 72 65 67 69 6f 6e 20 32 21 20 2dup kregion 2!
0580: 20 54 48 45 4e 0a 20 20 20 20 6f 76 65 72 20 73 THEN. over s
0590: 77 61 70 20 72 3e 20 73 61 66 65 2f 73 74 72 69 wap r> safe/stri
05a0: 6e 67 20 6b 72 65 67 69 6f 6e 20 32 21 20 28 20 ng kregion 2! (
05b0: 6b 61 6c 6c 6f 63 28 20 2e 22 20 6b 61 6c 6c 6f kalloc( ." kallo
05c0: 63 3a 20 22 20 64 75 70 20 68 65 78 2e 20 63 72 c: " dup hex. cr
05d0: 20 29 20 3b 0a 0a 3a 6e 6f 6e 61 6d 65 20 64 65 ) ;..:noname de
05e0: 66 65 72 73 20 27 69 6d 61 67 65 20 20 23 30 2e fers 'image #0.
05f0: 20 6b 72 65 67 69 6f 6e 20 32 21 20 20 30 20 74 kregion 2! 0 t
0600: 6f 20 2f 6b 72 65 67 69 6f 6e 23 20 6b 66 72 65 o /kregion# kfre
0610: 65 36 34 27 20 6f 66 66 20 3b 20 69 73 20 27 69 e64' off ; is 'i
0620: 6d 61 67 65 0a 0a 5c 20 66 69 78 65 64 20 73 69 mage..\ fixed si
0630: 7a 65 20 73 65 63 72 65 74 73 20 61 72 65 20 61 ze secrets are a
0640: 73 73 75 6d 65 64 20 74 6f 20 62 65 20 61 6c 6c ssumed to be all
0650: 20 36 34 20 62 79 74 65 73 20 6c 6f 6e 67 0a 5c 64 bytes long.\
0660: 20 69 66 20 74 68 65 79 20 61 72 65 20 6a 75 73 if they are jus
0670: 74 20 33 32 20 62 79 74 65 73 2c 20 74 68 65 20 t 32 bytes, the
0680: 73 65 63 6f 6e 64 20 68 61 6c 66 20 69 73 20 61 second half is a
0690: 6c 6c 20 7a 65 72 6f 0a 0a 3a 20 6b 61 6c 6c 6f ll zero..: kallo
06a0: 63 36 34 20 28 20 2d 2d 20 61 64 64 72 20 29 0a c64 ( -- addr ).
06b0: 20 20 20 20 6b 66 72 65 65 36 34 27 20 40 20 3f kfree64' @ ?
06c0: 64 75 70 2d 69 66 20 20 64 75 70 20 40 20 6b 66 dup-if dup @ kf
06d0: 72 65 65 36 34 27 20 21 20 64 75 70 20 6f 66 66 ree64' ! dup off
06e0: 20 20 65 78 69 74 20 20 74 68 65 6e 0a 20 20 20 exit then.
06f0: 20 36 34 20 6b 61 6c 6c 6f 63 20 3b 0a 3a 20 6b 64 kalloc ;.: k
0700: 66 72 65 65 36 34 20 28 20 61 64 64 72 20 2d 2d free64 ( addr --
0710: 20 29 0a 20 20 20 20 64 75 70 20 36 34 20 65 72 ). dup 64 er
0720: 61 73 65 20 6b 66 72 65 65 36 34 27 20 40 20 6f ase kfree64' @ o
0730: 76 65 72 20 21 20 6b 66 72 65 65 36 34 27 20 21 ver ! kfree64' !
0740: 20 3b 0a 3a 20 6b 61 6c 6c 6f 63 36 34 3f 20 28 ;.: kalloc64? (
0750: 20 61 64 64 72 20 2d 2d 20 61 64 64 72 27 20 29 addr -- addr' )
0760: 0a 20 20 20 20 64 75 70 20 40 20 49 46 20 20 40 . dup @ IF @
0770: 20 20 45 58 49 54 20 20 54 48 45 4e 20 20 64 72 EXIT THEN dr
0780: 6f 70 20 6b 61 6c 6c 6f 63 36 34 20 3b 0a 0a 33 op kalloc64 ;..3
0790: 32 20 62 75 66 66 65 72 3a 20 7a 65 72 6f 33 32 2 buffer: zero32
07a0: 0a 0a 3a 20 73 65 63 2d 66 72 65 65 20 28 20 61 ..: sec-free ( a
07b0: 64 64 72 20 2d 2d 20 29 20 64 75 70 20 40 20 64 ddr -- ) dup @ d
07c0: 75 70 20 20 49 46 20 20 6b 66 72 65 65 36 34 20 up IF kfree64
07d0: 6f 66 66 20 45 58 49 54 20 20 54 48 45 4e 20 20 off EXIT THEN
07e0: 32 64 72 6f 70 20 3b 0a 3a 20 73 65 63 21 20 28 2drop ;.: sec! (
07f0: 20 61 64 64 72 31 20 75 31 20 61 64 64 72 32 20 addr1 u1 addr2
0800: 2d 2d 20 29 0a 20 20 20 20 6f 76 65 72 20 30 3d -- ). over 0=
0810: 20 49 46 20 20 73 65 63 2d 66 72 65 65 20 32 64 IF sec-free 2d
0820: 72 6f 70 20 20 45 58 49 54 20 20 54 48 45 4e 0a rop EXIT THEN.
0830: 20 20 20 20 3e 72 20 72 40 20 6b 61 6c 6c 6f 63 >r r@ kalloc
0840: 36 34 3f 20 64 75 70 20 72 3e 20 21 20 24 34 30 64? dup r> ! $40
0850: 20 73 6d 6f 76 65 20 3b 0a 3a 20 73 65 63 40 20 smove ;.: sec@
0860: 28 20 61 64 64 72 20 2d 2d 20 61 64 64 72 31 20 ( addr -- addr1
0870: 75 31 20 29 0a 20 20 20 20 40 20 64 75 70 20 49 u1 ). @ dup I
0880: 46 20 20 24 34 30 0a 09 6f 76 65 72 20 24 32 30 F $40..over $20
0890: 20 2b 20 24 32 30 20 7a 65 72 6f 33 32 20 6f 76 + $20 zero32 ov
08a0: 65 72 20 73 74 72 3d 20 49 46 20 20 32 2f 0a 09 er str= IF 2/..
08b0: 20 20 20 20 6f 76 65 72 20 24 31 30 20 2b 20 24 over $10 + $
08c0: 31 30 20 7a 65 72 6f 33 32 20 6f 76 65 72 20 73 10 zero32 over s
08d0: 74 72 3d 20 49 46 20 20 32 2f 20 20 54 48 45 4e tr= IF 2/ THEN
08e0: 0a 09 54 48 45 4e 0a 20 20 20 20 45 4c 53 45 20 ..THEN. ELSE
08f0: 30 20 54 48 45 4e 20 3b 0a 3a 20 73 65 63 2b 21 0 THEN ;.: sec+!
0900: 20 28 20 61 64 64 72 31 20 75 31 20 61 64 64 72 ( addr1 u1 addr
0910: 32 20 2d 2d 20 29 0a 20 20 20 20 64 75 70 20 40 2 -- ). dup @
0920: 20 30 3d 20 49 46 20 20 73 65 63 21 20 20 45 4c 0= IF sec! EL
0930: 53 45 20 20 73 65 63 40 20 64 75 70 20 3e 72 20 SE sec@ dup >r
0940: 2b 20 24 34 30 20 72 3e 20 2d 20 73 6d 6f 76 65 + $40 r> - smove
0950: 20 20 54 48 45 4e 20 3b 0a 0a 3a 20 73 65 63 2b THEN ;..: sec+
0960: 5b 5d 21 20 28 20 61 64 64 72 31 20 75 31 20 61 []! ( addr1 u1 a
0970: 64 64 72 32 20 2d 2d 20 29 20 3e 72 0a 20 20 20 ddr2 -- ) >r.
0980: 20 7b 20 7c 20 77 5e 20 73 65 63 20 7d 20 73 65 { | w^ sec } se
0990: 63 20 73 65 63 21 20 73 65 63 20 63 65 6c 6c 20 c sec! sec cell
09a0: 72 3e 20 24 2b 21 20 3b 0a 0a 3a 20 73 65 63 5b r> $+! ;..: sec[
09b0: 5d 40 20 28 20 69 20 61 64 64 72 20 2d 2d 20 61 ]@ ( i addr -- a
09c0: 64 64 72 20 75 20 29 20 20 24 5b 5d 20 73 65 63 ddr u ) $[] sec
09d0: 40 20 3b 0a 0a 3a 20 73 65 63 5b 5d 66 72 65 65 @ ;..: sec[]free
09e0: 20 28 20 61 64 64 72 20 2d 2d 20 29 20 3e 72 0a ( addr -- ) >r.
09f0: 20 20 20 20 72 40 20 24 40 20 62 6f 75 6e 64 73 r@ $@ bounds
0a00: 20 3f 44 4f 0a 09 49 20 73 65 63 2d 66 72 65 65 ?DO..I sec-free
0a10: 0a 20 20 20 20 63 65 6c 6c 20 2b 4c 4f 4f 50 0a . cell +LOOP.
0a20: 20 20 20 20 72 3e 20 24 66 72 65 65 20 3b 0a 0a r> $free ;..
0a30: 73 74 6f 72 61 67 65 20 63 6c 61 73 73 20 65 6e storage class en
0a40: 64 2d 63 6c 61 73 73 20 63 72 79 70 74 6f 2d 61 d-class crypto-a
0a50: 6c 6c 6f 63 0a 0a 3a 6e 6f 6e 61 6d 65 20 20 28 lloc..:noname (
0a60: 20 6c 65 6e 20 2d 2d 20 61 64 64 72 20 29 0a 20 len -- addr ).
0a70: 20 20 20 5b 20 63 72 79 70 74 2d 61 6c 69 67 6e [ crypt-align
0a80: 20 63 65 6c 6c 2d 20 63 72 79 70 74 2d 61 6c 69 cell- crypt-ali
0a90: 67 6e 20 31 2d 20 2b 20 5d 4c 20 2b 0a 20 20 20 gn 1- + ]L +.
0aa0: 20 5b 20 63 72 79 70 74 2d 61 6c 69 67 6e 20 6e [ crypt-align n
0ab0: 65 67 61 74 65 20 5d 4c 20 61 6e 64 20 6b 61 6c egate ]L and kal
0ac0: 6c 6f 63 20 5b 20 63 72 79 70 74 2d 61 6c 69 67 loc [ crypt-alig
0ad0: 6e 20 63 65 6c 6c 2d 20 5d 4c 20 2b 0a 3b 20 63 n cell- ]L +.; c
0ae0: 72 79 70 74 6f 2d 61 6c 6c 6f 63 20 74 6f 20 3a rypto-alloc to :
0af0: 61 6c 6c 6f 63 61 74 65 0a 27 20 64 72 6f 70 20 allocate.' drop
0b00: 63 72 79 70 74 6f 2d 61 6c 6c 6f 63 20 74 6f 20 crypto-alloc to
0b10: 3a 66 72 65 65 0a 5c 20 77 65 20 6e 65 76 65 72 :free.\ we never
0b20: 20 66 72 65 65 20 74 68 65 73 65 20 63 6c 61 73 free these clas
0b30: 73 65 73 2c 20 74 68 65 79 20 61 72 65 20 70 65 ses, they are pe
0b40: 72 2d 74 61 73 6b 20 74 65 6d 70 6f 72 61 72 79 r-task temporary
0b50: 20 73 74 6f 72 61 67 65 73 0a 0a 63 72 79 70 74 storages..crypt
0b60: 6f 2d 61 6c 6c 6f 63 20 27 20 6e 65 77 20 73 74 o-alloc ' new st
0b70: 61 74 69 63 2d 61 20 77 69 74 68 2d 61 6c 6c 6f atic-a with-allo
0b80: 63 61 74 65 72 20 43 6f 6e 73 74 61 6e 74 20 63 cater Constant c
0b90: 72 79 70 74 6f 2d 61 0a rypto-a.