xref: /titanic_51/usr/src/psm/stand/bootblks/common/util.fth (revision 4a6ec905b96eb96a398c346f59e034a90ce8ad37)
1\
2\ CDDL HEADER START
3\
4\ The contents of this file are subject to the terms of the
5\ Common Development and Distribution License (the "License").
6\ You may not use this file except in compliance with the License.
7\
8\ You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9\ or http://www.opensolaris.org/os/licensing.
10\ See the License for the specific language governing permissions
11\ and limitations under the License.
12\
13\ When distributing Covered Code, include this CDDL HEADER in each
14\ file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15\ If applicable, add the following below this CDDL HEADER, with the
16\ fields enclosed by brackets "[]" replaced with your own identifying
17\ information: Portions Copyright [yyyy] [name of copyright owner]
18\
19\ CDDL HEADER END
20\
21\
22\ ident	"%Z%%M%	%I%	%E% SMI"
23\ Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24\ Use is subject to license terms.
25\
26
27id: %Z%%M%	%I%	%E% SMI
28purpose: utility words
29copyright: Copyright 2007 Sun Microsystems, Inc. All Rights Reserved
30
31
32[ifdef] doheaders
33headers
34[else]
35headerless
36[then]
37
38d# 256  constant  /buf-len
39
40\
41\	useful counting words
42\
43: roundup ( x y -- x' )  1- tuck +  swap invert and  ;
44
45
46\
47\	various useful string manipulation words
48\
49
50: cstrlen ( cstr -- len )
51   dup begin
52      dup c@
53   while
54      char+
55   repeat swap -
56;
57
58: cscount ( cstr -- adr,len )  dup cstrlen  ;
59
60\ Append str1 to the end of str2
61: $append ( adr,len1 adr,len2 -- )
62   2over 2over  ca+ swap move   ( adr,len1 adr,len2 )
63   rot + ca+ 0 swap c!  drop    (  )
64;
65
66: $=  ( str1$ str2$ -- same? )
67   rot tuck <>  if
68      3drop false exit
69   then  comp 0=
70;
71
72\ advance str by 1
73: str++  ( adr len --  adr' len' )
74   swap 1+  swap 1-
75;
76
77: die  ( str -- )
78   cr  type  cr abort
79;
80
81: diag-cr?  ( -- )  diagnostic-mode?  if  cr  then  ;
82
83
84: find-abort ( name$ -- )
85   cr  ." Can't find " type  cr abort
86;
87
88: get-package ( pkg$ -- ph )
89   2dup  find-package 0=  if
90      find-abort
91   then                       ( pkg$ ph )
92   nip nip                    ( ph )
93;
94
95
96\
97\	CIF words for I/O and memory
98\
99" /openprom/client-services" get-package  constant cif-ph
100
101instance defer cif-open     ( dev$ -- ihandle|0 )
102instance defer cif-close    ( ihandle -- )
103instance defer cif-read     ( len adr ihandle -- #read )
104instance defer cif-seek     ( low high ihandle -- -1|0|1 )
105instance defer cif-release  ( size virt -- )
106
107: find-cif-method ( adr,len -- acf )
108   2dup  cif-ph find-method 0=  if    ( adr,len )
109      find-abort
110   then                               ( adr,len acf )
111   nip nip                            ( acf )
112;
113
114" open"     find-cif-method to cif-open
115" close"    find-cif-method to cif-close
116" read"     find-cif-method to cif-read
117" seek"     find-cif-method to cif-seek
118" release"  find-cif-method to cif-release
119
120
121" /chosen" get-package  constant chosen-ph
122
123: get-property  ( name$ ph -- prop$ )
124   >r 2dup  r>  get-package-property  if   ( name$ )
125      find-abort
126   then                                    ( name$ prop$ )
127   2swap  2drop                            ( prop$ )
128;
129
130: get-string-prop  ( name$ ph -- val$ )
131   get-property decode-string            ( prop$' val$ )
132   2swap 2drop                           ( val$ )
133;
134
135: get-int-prop  ( name$ ph -- n )
136   get-property decode-int               ( prop$' n )
137   nip nip                               ( n )
138;
139
140\
141\	memory allocation
142\	we bypass cif claim so we can do large page
143\	allocations like promif can
144\
145
146" mmu"    chosen-ph  get-int-prop  constant mmu-ih
147
148" memory" chosen-ph  get-int-prop  constant mem-ih
149
150: mmu-claim  ( [ virt ] size align -- base )
151   " claim" mmu-ih $call-method
152;
153
154: mmu-map  ( phys.lo phys.hi virt size -- )
155   -1  " map" mmu-ih $call-method
156;
157
158: mem-claim  ( size align -- phys.lo phys.hi )
159   " claim" mem-ih $call-method
160;
161
162: (mem-alloc)   ( size virt align -- virt )
163   \ claim memory first since it may throw if fragmented
164   rot  2dup swap  mem-claim           ( virt align size phys.lo phys.hi )
165   >r >r  rot ?dup  if                 ( align size virt  r: phys.lo phys.hi )
166      \ we picked virt - zero alignment
167      over 0  mmu-claim                ( align size virt  r: phys.lo phys.hi )
168   else                                ( align size  r: phys.lo phys.hi )
169      \ OBP picks virt - pass alignment
170      2dup swap  mmu-claim             ( align size virt  r: phys.lo phys.hi )
171   then                                ( align size virt  r: phys.lo phys.hi )
172   r> r>  2over swap  mmu-map          ( align size virt )
173   nip nip                             ( virt )
174;
175
176: vmem-alloc ( size virt -- virt )
177   swap  h# 2000 roundup  swap
178   1 (mem-alloc)
179;
180
181: mem-alloc ( size -- virt )
182   h# 2000  roundup
183   0 1 (mem-alloc)
184;
185
186: mem-free  ( virt size -- )
187   h# 2000  roundup
188   swap  cif-release    (  )
189;
190
191
192
193\ put ramdisk fcode 256 bytes from end of bootblk
194\ (currently 244 bytes in size)
195d# 256               constant /rd-fcode
196d# 8192 /rd-fcode -  constant rd-offset
197
198: open-abort  ( file$ -- )
199   cr  ." Can't open "  type  cr abort
200;
201
202/buf-len  buffer: open-cstr
203
204: dev-open ( dev$ -- ih | 0 )
205   \ copy to C string for open
206   0  over open-cstr +  c!
207   open-cstr swap  move
208   open-cstr  cif-open
209;
210
211: dev-close ( ih -- )
212   cif-close
213;
214
215: read-disk    ( adr len off ih -- )
216   dup >r  0 swap  cif-seek  if     ( adr len  r: ih )
217      " seek failed"  die
218   then
219
220   tuck  swap r>  cif-read  <>  if  (  )
221      " read failed"  die
222   then
223;
224