\ ========== Copyright Header Begin ==========================================
\ Hypervisor Software File: savecpu.fth
\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
\ - Do no alter or remove copyright notices
\ - Redistribution and use of this software in source and binary forms, with
\ or without modification, are permitted provided that the following
\ - Redistribution of source code must retain the above copyright notice,
\ this list of conditions and the following disclaimer.
\ - Redistribution in binary form must reproduce the above copyright notice,
\ this list of conditions and the following disclaimer in the
\ documentation and/or other materials provided with the distribution.
\ Neither the name of Sun Microsystems, Inc. or the names of contributors
\ may be used to endorse or promote products derived from this software
\ without specific prior written permission.
\ This software is provided "AS IS," without a warranty of any kind.
\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
\ You acknowledge that this software is not designed, licensed or
\ intended for use in the design, construction, operation or maintenance of
\ ========== Copyright Header End ============================================
id: @(#)savecpu.fth 1.1 06/02/16
copyright: Copyright 2006 Sun Microsystems, Inc. All Rights Reserved
copyright: Use is subject to license terms.
also assembler definitions
: save-reg ( reg offset -- ) \ where?
postpone offset-of %asi stxa
: load-reg ( area reg -- ) \ where?
>r postpone offset-of %asi r> ldxa
: write-reg ( reg area offset -- ) %asi stxa ;
: read-reg ( area offset reg -- ) >r %asi r> ldxa ;
\ else, restore, jmpl %g7
label restore-cpu-state ( -- )
%g5 %g1 load-reg %cwp %g1 0 wrcwp \ EARLY!!!
%l5 %g1 load-reg %tpc-2 %g1 0 wrtpc
%l5 %g1 load-reg %tnpc-2 %g1 0 wrtnpc
%l5 %g1 load-reg %tstate-2 %g1 0 wrtstate
%l5 %g1 load-reg %tt-2 %g1 0 wrtt
%l5 %g1 load-reg %tpc-1 %g1 0 wrtpc
%l5 %g1 load-reg %tnpc-1 %g1 0 wrtnpc
%l5 %g1 load-reg %tstate-1 %g1 0 wrtstate
%l5 %g1 load-reg %tt-1 %g1 0 wrtt
\ Now restore original state..
%l5 %g1 load-reg %tl-c %g1 0 cmp \ TL=0??
%l5 %g1 load-reg %pc %g1 0 wrtpc
%l5 %g1 load-reg %npc %g1 0 wrtnpc
%l5 %g1 load-reg %tstate-c %g1 0 wrtstate
%l5 %g1 load-reg %y %g1 0 wry
%l5 %g1 load-reg %pil %g1 0 wrpil
%l5 %g1 load-reg %cansave %g1 0 wrcansave
%l5 %g1 load-reg %canrestore %g1 0 wrcanrestore
%l5 %g1 load-reg %cleanwin %g1 0 wrcleanwin
%l5 %g1 load-reg %otherwin %g1 0 wrotherwin
%l5 %g1 load-reg %wstate %g1 0 wrwstate
%l5 %g1 load-reg %fprs %g1 0 wrfprs
%g0 0 wrgl 8 0 do %l5 offset-of %g0 i /x* + %g0 i + read-reg loop
%g0 1 wrgl 8 0 do %l5 offset-of %a0 i /x* + %g0 i + read-reg loop
\ Restore locals, Ins Outs
\ Address of window regs.
%l5 window-registers %g6 add
8 0 do %g6 i /x* %o0 i + read-reg loop \ %o0-%o7
%g5 %g1 load-reg full-save?
%g5 %g1 load-reg %pcontext
%g5 %g1 load-reg %scontext
\ recover how many we saved
8 0 do %g6 i /x* %l0 i + read-reg loop \ %l0-%l7
8 0 do %g6 i /x* %i0 i + read-reg loop \ %i0-%i7
8 0 do %g6 i /x* %l0 i + read-reg loop \ %l0-%l7
8 0 do %g6 i /x* %i0 i + read-reg loop \ %i0-%i7
\ copy the state we destroy into TL=2 equivs.
\ %g7 = pc to jump to after save, cwp=0, gl=0, tl=0, ie=1
label save-cpu-state ( -- )
%g1 rdtl %g1 %g5 save-reg %tl-c
%g1 rdtpc %g1 %g5 save-reg %tpc-c
%g1 rdtnpc %g1 %g5 save-reg %tnpc-c
%g1 rdtstate %g1 %g5 save-reg %tstate-c
%g1 rdy %g1 %g5 save-reg %y
%g1 rdpil %g1 %g5 save-reg %pil
%g1 rdcwp %g1 %g5 save-reg %cwp
%g1 rdcansave %g1 %g5 save-reg %cansave
%g1 rdcanrestore %g1 %g5 save-reg %canrestore
%g1 rdcleanwin %g1 %g5 save-reg %cleanwin
%g1 rdotherwin %g1 %g5 save-reg %otherwin
%g1 rdwstate %g1 %g5 save-reg %wstate
%g1 rdfprs %g1 %g5 save-reg %fprs
%g6 %g5 save-reg full-save?
%g1 rdgl %g1 %g5 save-reg %gl
%g1 rdtt %g1 %g5 save-reg %tt-c
%g1 rdtpc %g1 %g5 save-reg %tpc-2
%g1 rdtnpc %g1 %g5 save-reg %tnpc-2
%g1 rdtstate %g1 %g5 save-reg %tstate-2
%g1 rdtt %g1 %g5 save-reg %tt-2
%g1 rdtpc %g1 %g5 save-reg %tpc-1
%g1 rdtnpc %g1 %g5 save-reg %tnpc-1
%g1 rdtstate %g1 %g5 save-reg %tstate-1
%g1 rdtt %g1 %g5 save-reg %tt-1
\ Address of window regs.
%g5 window-registers %g6 add
8 0 do %o0 i + %g6 i /x* write-reg loop \ %o0-%o7
%g5 %g1 load-reg full-save?
%g1 %g5 save-reg %pcontext
%g1 %g5 save-reg %scontext
%g0 %g0 %g2 h# 21 stxa \ Set Primary context to 0
\ Rebuild %PSTATE, %CCR, %ASI from %TSTATE and save them
%g1 d# 63 d# 39 - %g2 sllx
8 0 do %l0 i + %g6 i /x* write-reg loop \ %l0-%l7
8 0 do %i0 i + %g6 i /x* write-reg loop \ %i0-%i7
%g1 %g5 save-reg %nwins \ How many?
8 0 do %l0 i + %g6 i /x* write-reg loop \ %l0-%l7
8 0 do %i0 i + %g6 i /x* write-reg loop \ %i0-%i7
%g5 %g1 load-reg %cwp %g1 0 wrcwp
%g0 1 wrgl 8 0 do %g0 i + %l5 offset-of %a0 i /x* + write-reg loop
%g0 0 wrgl 8 0 do %g0 i + %l5 offset-of %g0 i /x* + write-reg loop
label setup-small-forth-engine
prom-main-task %g5 up setx \ Set User Pointer
%g1 %l0 %g1 add \ CPU Save Area VA
%g1 /min-cpu-save %g1 add
%g1 /fth-exception-stack sp add \ Data Stack set
%g1 /fth-exception-stack 2/ rp add
rombase base set \ set base
[ifdef] WOULD-BE-NICE-TO-HAVE-HERE
\ We cant put this here because it has a fwd ref to save state
\ we also depends upon this code. So its [ifdef]d out and left for
\ %g1 is the structure offset
label small-forth-save-state
\ OK, we have to switch to the original fault-tl and tpc, tnpc
\ we got here from the restore which does a retry at tl=1 having
\ setup the tpc, tnpc to 'return', everything else should be restored
%g2 %g0 h# 20 %g5 ldxa \ CPU struct PA
%g5 %g1 %g5 add \ CPU save area