Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / arch / sun4u / reenter.fth
CommitLineData
920dae64
AT
1\ ========== Copyright Header Begin ==========================================
2\
3\ Hypervisor Software File: reenter.fth
4\
5\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6\
7\ - Do no alter or remove copyright notices
8\
9\ - Redistribution and use of this software in source and binary forms, with
10\ or without modification, are permitted provided that the following
11\ conditions are met:
12\
13\ - Redistribution of source code must retain the above copyright notice,
14\ this list of conditions and the following disclaimer.
15\
16\ - Redistribution in binary form must reproduce the above copyright notice,
17\ this list of conditions and the following disclaimer in the
18\ documentation and/or other materials provided with the distribution.
19\
20\ Neither the name of Sun Microsystems, Inc. or the names of contributors
21\ may be used to endorse or promote products derived from this software
22\ without specific prior written permission.
23\
24\ This software is provided "AS IS," without a warranty of any kind.
25\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
26\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
27\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
28\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
29\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
30\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
31\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
32\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
33\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
34\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
35\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36\
37\ You acknowledge that this software is not designed, licensed or
38\ intended for use in the design, construction, operation or maintenance of
39\ any nuclear facility.
40\
41\ ========== Copyright Header End ============================================
42id: @(#)reenter.fth 1.22 07/06/22
43purpose:
44copyright: Copyright 2007 Sun Microsystems, Inc. All Rights Reserved
45copyright: Use is subject to license terms.
46
47\ Ways to enter the Forth interpreter, and their implications:
48\ a) Power up - no autoboot
49\ Entry mechanism: prom-cold-code, which is entered from the boot-state
50\ reset vector, distinguished this from a watchdog reset by looking
51\ at the watchdog bit in the system error register. prom-cold-code
52\ then initializes the machine and executes the Forth startup
53\ sequence.
54\ Action: perform the startup initialization sequence, then boot
55\ the operating system if autoboot is specified in the configuration.
56\ Otherwise enter the text interpreter.
57\ b) Power up - abort key
58\ Action: Finish initialization procedure, but enter interpreter
59\ instead of autoboot
60\ c) Breakpoint (= single step)
61\ Entry mechanism: the trap table entry reads the psr into %l0 and
62\ jumps to save-state . save-state saves %l0 into the error-reset-trap
63\ field of the cpu-state save area.
64\ Signature: The trap type field of the saved tbr indicates the
65\ breakpoint trap vector, the error-reset-trap field of the cpu-state save
66\ area contains a valid psr value (as opposed to -1; see the
67\ "Error reset" case), and no other special conditions are present
68\ (see "exittomon", "abortent")
69\ Action: perform the action indicated by the "handle-breakpoint"
70\ routine; usually disassembles the breakpointed instruction and
71\ enters the text interpreter
72\ d) Error trap (e.g. Data Access Exception)
73\ Entry mechanism: the trap table entry reads the psr into %l0 and
74\ jumps to save-state . save-state saves %l0 into the error-reset-trap
75\ field of the cpu-state save area.
76\ Signature: The trap type field of the saved tbr indicates the
77\ breakpoint trap vector, the error-reset-trap field of the cpu-state save
78\ area contains a valid psr value (as opposed to -1; see the
79\ "Error reset" case), and no other special conditions are present
80\ (see "exittomon", "abortent")
81\ Action: display exception name and enter text interpreter
82\ e) Exittomon - Return from Unix - don't expect to re-enter
83\ Entry mechanism: sun4m/machdep.c:halt() calls romp->v_exit_to_mon,
84\ which resolves to reenter.fth:reenter . "reenter" executes the
85\ breakpoint trap instruction, which proceeds as in the "breakpoint"
86\ case.
87\ Signature: This looks almost like the "Breakpoint" case, and is
88\ distinguised from that case by the fact that the saved %pc value
89\ indicates the address of the trap instruction within the "reenter"
90\ routine.
91\ Action: Display "Type 'go' to resume", then enter text interpreter
92\ Problem: kadb may have usurped the breakpoint trap table entry
93\ Idea: Maybe instead of start_mon_clock(), stop_mon_clock(), we should
94\ provide romvecs for this, or maybe the monitor should just do it
95\ automatically whenever the interpreter is entered or one of the
96\ the monitor's I/O routines (maybe just mayget) is called. (No, just
97\ mayget() is insufficient, because we could lose characters when
98\ scrolling the screen.
99\ Note: Unix displays a message through the PROM's printf routine
100\ before calling romp->v_exit_to_mon. The message depends on
101\ the reason for the halt:
102\ "Halted" Executed from sun4m/machdep.c:boot(), from
103\ reboot system call entry (in os/kern_xxx.c),
104\ but only if RB_HALT flag from the user's
105\ parameters to the system call is set.
106\ spl6() is performed before calling halt()
107\ "bootflags" The RB_HALT flag was set when Unix parsed the
108\ boot flags. Restartable. spl6() NOT called
109\ (Is that the right thing?)
110\ "system map tables too large"
111\ Unix has an internal configuration problem
112\ "no memory" Unix was too big for the available phys. memory
113\
114\ Note: Unix starts the monitor clock before calling romp->v_exit_to_mon
115\ Note: Unix does not intend to be restarted in most cases, but
116\ in the "bootflags" case above, restarting is reasonable.
117\ Halt() remembers to stop the monitor clock if exit_to_mon returns.
118\
119\ f) Abortent - keyboard abort from Unix (L1-A or Break) (continuable)
120\ Entry mechanism: either sundev/zs_async.c or sundev/kbd.c calls
121\ sun4m/locore.s:montrap(romp->v_abortent), which then calls that
122\ abortent routine. Currently, the abortent romvec resolves to
123\ reenter.fth:reeenter, the same as for "Exittomon"
124\ Signature: Currently, this is indistinguishable from the "Exittomon"
125\ case, unless Forth is willing to dig around in the window registers
126\ to identify the "montrap()" return address (a silly way to do it;
127\ it would be easier to provide a separate "abort-reenter" routine).
128\ Action: Currently the same as for "Exittomon"
129\ Suggestion: the action should be similar to "L1-A when Forth running"
130\ Note: if kadb is installed (Unix saw "-d" flag), kadb is entered
131\ instead of the monitor.
132\ Note: Execution of Unix is frequently resumed after this kind of abort
133\ Note: Unix does NOT start the monitor clock before calling
134\ romp->v_abortent
135\
136\ g) Bootme - return from Unix with auto-reboot
137\ Entry mechanism: sun4m/machdep.c:boot(), which is called from the
138\ reboot system call code in os/kern_xxx.c. boot() calls
139\ romp->v_boot_me, which calls a Forth entry resolving to
140\ bootparams.fth:boot-me
141\ Note: Never returns to Unix; instead reboots the system
142\ Note: Monitor clock has been started at spl6() (pil 13).
143\ Note: Argument is a C string which is either "", "-s", or the argument
144\ to the reboot user command
145\ Note: Doesn't enter the Forth text interpreter
146\
147\ h) Callvec - call Unix subroutine which then returns back to Forth
148\ Entry mechanism: Forth calls a subroutine whose address has been
149\ exported by Unix. That subroutine returns, reentering the
150\ Forth word (callvec or sync) from which it was called.
151\ Note: Unix doesn't do anything to restore it's state. Maybe this is
152\ wrong. Perhaps Unix should be in charge of setting up its own
153\ stack, context, trap table, and interrupts again?
154\ Note: This is usually executed after a keyboard abort or a watchdog.
155\
156\ i) fwritestr - terminal emulator called from Unix
157\ Entry mechanism: Unix calls either "putchar()", "mayput()", or
158\ "fwritestr()". putchar() and mayput() are C subroutines which
159\ themselves call "fwritestr()". fwritestr() is a C to Forth entry
160\ point, which establishes a temporary Forth environment on the
161\ C stack, then calls a Forth word.
162\ Note: When executing fwritestr(), we can lose keyboard characters
163\ if Unix has disabled the Forth level 14 clock handler. We need
164\ to make sure that Unix leaves that handler enabled during startup
165\ up until the point where Unix takes over the keyboard.
166\
167\ j) L1-A or break from keyboard when Forth is running
168\ Entry mechanism: The level 14 clock interrupt handler checks for an
169\ abort (L1-A from the keyboard or break from a tty) and sets the
170\ "aborted?" variable if one is seen. Just before the assembly
171\ language part of the interrupt handler returns, it checks
172\ "aborted?", and if it is set, the handler jumps to save-state
173\ instead of returning. The CPU state at that point has been
174\ restored to the same state that existed just when the interrupt
175\ handler was entered, i.e. as if the trap had directly vectored
176\ to "save-state" instead of to the interrupt handler. That way,
177\ the user does not see any artifacts of the interrupt handler itself.
178\ Signature: The "aborted?" variable is set. Otherwise looks like a
179\ a breakpoint.
180\ Action: Displays "Keyboard abort. Type 'go' to resume"
181\
182\ k) L1-A from keyboard with interrupts off and polling mayget()
183\ Entry mechanism: an application is calling the PROM's mayget()
184\ routine in order to get a characters. Level 14 interrupts are
185\ turned off. The input device is the Sun keyboard. mayget()
186\ call key_check() to poll the keyboard. The key is determined
187\ to be the A of an L1-A sequence. key_check() calls _enterforth
188\ == reenter , which sets the establishes Forth's trap table and
189\ executes a trap #7f instruction.
190\ Signature: This looks like the "Abortent" case above. If necessary
191\ it could be distinguished by looking on the C stack to see the
192\ interruption of the key_check routine.
193\ Action: Displays "Type 'go' to resume"
194\
195\ l) From a watchdog reset
196\ Entry mechanism: prom-cold-code, which is entered from the boot-state
197\ reset vector, destinguished this from a power-on reset by looking
198\ at the watchdog bit in the system error register. prom-cold-code
199\ then puts a discernable value (-1) in %l0, jumps to save-state,
200\ and save-state saves %l0 in the cpu-state area.
201\ Signature: the "error-reset-trap" field in the cpu-state area contains -1.
202\ Otherwise looks like a breakpoint.
203\ Action: If watchdog-reboot? is set, execute the autoboot sequence,
204\ otherwise enter the text interpreter.
205
206
207\ Enter the monitor with no complaints or error messages.
208
209only forth also hidden also forth definitions
210
211headerless
212
213code reclaim-machine ( -- )
214 %g0 h# 14 wrpstate \ IE=0, FPU=1, PRIV=1
215[ifdef] SUN4V
216 %g0 %o0 move
217 %g0 %o1 move
218 %g0 h# 20 %o5 add \ MMU_TSB_CTX0
219 %g0 h# 80 always htrapif
220 %g0 %o0 move
221 %g0 %o1 move
222 %g0 h# 21 %o5 add \ MMU_TSB_CTXNON0
223 %g0 h# 80 always htrapif
224[then]
225 trap-table %o0 set
226 %o0 0 wrtba
227 %g0 h# f wrpil
228 %g0 h# 16 wrpstate \ IE=1, FPU=1, PRIV=1
229c;
230
231label exittomon
232
233 flushw
234
235 \ Undo the effects of "fentry" because
236 \ of the Client Interface Service EXIT
237
238 \ We don't want to depend on the data
239 \ stack pointer being the same, because
240 \ the routine may have left a return value
241 \ on the stack.
242
243 %o6 V9_SP_BIAS d# 16 na+ sp nget
244 %o6 V9_SP_BIAS d# 24 na+ rp nget
245
246 \ Restore these in case of multiple levels of cross-language calls;
247 \ if the Forth word that was just executed called a C subroutine,
248 \ then saved-sp and saved-rp could have been changed.
249
250 sp 'user saved-sp nput
251 rp 'user saved-rp nput
252
253 \ Restore the Globals
254
255 %o6 V9_SP_BIAS d# 17 na+ %g1 nget
256 %o6 V9_SP_BIAS d# 18 na+ %g2 nget
257 %o6 V9_SP_BIAS d# 19 na+ %g3 nget
258 %o6 V9_SP_BIAS d# 20 na+ %g4 nget
259 %o6 V9_SP_BIAS d# 21 na+ %g5 nget
260 %o6 V9_SP_BIAS d# 22 na+ %g6 nget
261 %o6 V9_SP_BIAS d# 23 na+ %g7 nget
262
263 \ Back to the previous frame
264 %g0 0 %g0 restore
265
266 \ Save the client program's trap base register
267 %o0 rdtba
268
269 %o1 rdpstate
270 %o1 2 %o2 andn
271 %o2 0 wrpstate
272
273 \ Set the trap base register to Forth's trap table
274 trap-table %o2 set
275 %o2 0 wrtba
276 nop nop nop
277
278 begin
279 %g0 h# 7f always trapif
280 again \ Not restartable; keep exiting
281 nop
282
283end-code
284create exittomon-end
285
286label reenter ( -- )
287
288 flushw
289
290 \ Undo the effects of "fentry" because
291 \ of the Client Interface Service ENTER
292
293 \ We don't want to depend on the data
294 \ stack pointer being the same, because
295 \ the routine may have left a return value
296 \ on the stack.
297
298 %o6 V9_SP_BIAS d# 16 na+ sp nget
299 %o6 V9_SP_BIAS d# 24 na+ rp nget
300
301 \ Restore these in case of multiple levels of cross-language calls;
302 \ if the Forth word that was just executed called a C subroutine,
303 \ then saved-sp and saved-rp could have been changed.
304
305 sp 'user saved-sp nput
306 rp 'user saved-rp nput
307
308 \ Restore the Globals
309
310 %o6 V9_SP_BIAS d# 17 na+ %g1 nget
311 %o6 V9_SP_BIAS d# 18 na+ %g2 nget
312 %o6 V9_SP_BIAS d# 19 na+ %g3 nget
313 %o6 V9_SP_BIAS d# 20 na+ %g4 nget
314 %o6 V9_SP_BIAS d# 21 na+ %g5 nget
315 %o6 V9_SP_BIAS d# 22 na+ %g6 nget
316 %o6 V9_SP_BIAS d# 23 na+ %g7 nget
317
318 \ Back to the previous frame
319 %g0 0 %g0 restore
320
321 \ Save the client program's trap base register
322 %o0 rdtba
323
324 %o1 rdpstate
325 %o1 2 %o2 andn
326 %o2 0 wrpstate
327
328 \ Set the trap base register to Forth's trap table
329 trap-table %o2 set
330 %o2 0 wrtba
331 nop nop nop
332
333
334 \ %o0 : Original %tba
335 \ %o1 : Original %pstate
336
337 %g0 h# 7f always trapif
338
339 \ %o0 : %tba
340 \ %o1 : %pstate
341
342 \ Restore the client's trap base register
343 %o0 0 wrtba
344 nop nop nop
345
346 %o1 0 wrpstate
347
348 %o7 8 %g0 jmpl
349 nop
350end-code
351create reenter-end
352
353: .go-message ( -- )
354 \ Restarting is only possible if the state that was first saved
355 \ is from a restartable exception.
356 state-valid @ -1 = already-go? and if
357 restartable? on
358 ['] interpret behavior ['] (interpret = if
359 ." Type 'go' to resume" cr
360 then
361 then
362;
363
364headers
3650 >debugger-hook per-cpu-defer: debugger-hook
366
367\
368\ KADB uses this *and* the per cpu version!
369\
370: init-debugger-hook ( xt -- ) ['] debugger-hook 3 perform-action ;
371
372stand-init: Init debugger-hook
373 ['] noop init-debugger-hook
374;
375
376headerless
377defer rearm-alarms ' noop is rearm-alarms
378: reset-debugger-hook ( -- ) ['] noop to debugger-hook ;
379: enter-debugger ( -- )
380 addr debugger-hook token@ reset-debugger-hook execute
381;
382
383h# 3 constant xir-trap#
384: is-xir-trap? ( -- flag ) last-trap# xir-trap# = ;
385: is-breakpoint-trap? ( -- flag ) last-trap# breakpoint-trap# = ;
386: inside? ( label end -- flag ) %pc -rot within ;
387
388\ Hooks to take care of special platform specific handling.
389defer xir-trap-hook ' noop is xir-trap-hook
390defer exittomon-hook ' noop is exittomon-hook
391
392defer reenter-hook ( -- ) ' noop is reenter-hook
393
394: .trap ( -- )
395 talign \ Hack; recover in case dp is misaligned
396
397 aborted? @ if
398 aborted? off rearm-alarms
399 enter-debugger hex cr .go-message exit
400 then
401
402 is-xir-trap? if
403 reset-debugger-hook
404 xir-trap-hook
405 then
406 is-breakpoint-trap? 0= if
407 enter-debugger (do-last-trap) exit
408 then
409
410 \ We want to stay at the 'ok' prompt
411 \ so, uninstall the debugger-hook
412 reset-debugger-hook
413
414 exittomon exittomon-end inside? if
415 obp-control-relinquished? if
416 client-exited-chain
417 else
418 \ Client program dropped to ok-prompt by unsuccessful boot
419 client-fail-exited-chain
420 then
421 false to obp-control-relinquished?
422 exittomon-hook
423 ." Program terminated" cr exit
424 then
425 reenter reenter-end inside? if
426 reenter-hook
427 .go-message
428 %o0 to %tba
429 %o1 to %pstate
430 then
431;
432: (restart ( -- )
433 \ If the PC is pointing to the trap instruction in "reenter",
434 \ adjust %pc and %npc to skip that trap instruction.
435 reenter reenter-end inside? if
436 0w
437
438 0 to %o2
439
440 %pc la1+ to %pc
441 %npc la1+ to %npc
442 then
443 (crestart
444;
445: mp-ok ( -- )
446 mid@ <# ascii } hold u#s ascii { hold u#> type ." ok "
447;
448
449: mp-system? ( -- n-1 )
450 -1 max-mondo-target# 0 ?do
451 i mid-ok? if
452 i >cpu-struct >cpu-status @ if 1+ then
453 then
454 loop
455;
456
457: ?mp-prompt ( -- )
458 ['] "ok" is (ok)
459 mp-system? if ['] mp-ok is (ok) then
460;
461
462stand-init: Install .exception
463 ['] .trap is .exception
464;
465headers
466only forth also definitions