-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathcswtch.s
More file actions
2638 lines (2278 loc) · 88.6 KB
/
Copy pathcswtch.s
File metadata and controls
2638 lines (2278 loc) · 88.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
230
231
232
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
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
/*
* Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* @OSF_COPYRIGHT@
*/
#include <ppc/asm.h>
#include <ppc/proc_reg.h>
#include <cpus.h>
#include <assym.s>
#include <debug.h>
#include <mach/ppc/vm_param.h>
#include <ppc/exception.h>
#define FPVECDBG 0
#define GDDBG 0
.text
/*
* void load_context(thread_t thread)
*
* Load the context for the first kernel thread, and go.
*
* NOTE - if DEBUG is set, the former routine is a piece
* of C capable of printing out debug info before calling the latter,
* otherwise both entry points are identical.
*/
ENTRY2(load_context, Load_context, TAG_NO_FRAME_USED)
/*
* Since this is the first thread, we came in on the interrupt
* stack. The first thread never returns, so there is no need to
* worry about saving its frame, hence we can reset the istackptr
* back to the saved_state structure at it's top
*/
/*
* get new thread pointer and set it into the active_threads pointer
*
*/
mfsprg r6,0
lwz r0,PP_INTSTACK_TOP_SS(r6)
lwz r11,PP_CPU_DATA(r6)
stw r0,PP_ISTACKPTR(r6)
stw r3,CPU_ACTIVE_THREAD(r11)
/* Find the new stack and store it in active_stacks */
lwz r12,PP_ACTIVE_STACKS(r6)
lwz r1,THREAD_KERNEL_STACK(r3)
lwz r9,THREAD_TOP_ACT(r3) /* Point to the active activation */
stw r1,0(r12)
li r0,0 /* Clear a register */
lwz r8,ACT_MACT_PCB(r9) /* Get the savearea used */
lwz r10,SAVflags(r8) /* Get the savearea flags */
rlwinm r7,r8,0,0,19 /* Switch to savearea base */
lwz r11,SAVprev(r8) /* Get the previous savearea */
mfmsr r5 /* Since we are passing control, get our MSR values */
lwz r1,saver1(r8) /* Load new stack pointer */
rlwinm r10,r10,0,1,31 /* Remove the attached flag */
stw r0,saver3(r8) /* Make sure we pass in a 0 for the continuation */
lwz r7,SACvrswap(r7) /* Get the translation from virtual to real */
stw r0,FM_BACKPTR(r1) /* zero backptr */
stw r5,savesrr1(r8) /* Pass our MSR to the new guy */
stw r10,SAVflags(r8) /* Pass back the flags */
xor r3,r7,r8 /* Get the physical address of the new context save area */
stw r11,ACT_MACT_PCB(r9) /* Unstack our savearea */
b EXT(exception_exit) /* Go end it all... */
/* struct thread_shuttle *Switch_context(struct thread_shuttle *old,
* void (*cont)(void),
* struct thread_shuttle *new)
*
* Switch from one thread to another. If a continuation is supplied, then
* we do not need to save callee save registers.
*
*/
/* void Call_continuation( void (*continuation)(void), vm_offset_t stack_ptr)
*/
ENTRY(Call_continuation, TAG_NO_FRAME_USED)
mtlr r3
mr r1, r4 /* Load new stack pointer */
blr /* Jump to the continuation */
/*
* Get the old kernel stack, and store into the thread structure.
* See if a continuation is supplied, and skip state save if so.
* NB. Continuations are no longer used, so this test is omitted,
* as should the second argument, but it is in generic code.
* We always save state. This does not hurt even if continuations
* are put back in.
*/
/* Context switches are double jumps. We pass the following to the
* context switch firmware call:
*
* R3 = switchee's savearea
* R4 = old thread
* R5 = new SRR0
* R6 = new SRR1
*
* savesrr0 is set to go to switch_in
* savesrr1 is set to uninterruptible with translation on
*/
ENTRY(Switch_context, TAG_NO_FRAME_USED)
mfsprg r6,0 /* Get the per_proc block */
lwz r12,PP_ACTIVE_STACKS(r6)
#if DEBUG
lwz r11,PP_ISTACKPTR(r6) ; (DEBUG/TRACE) make sure we are not
mr. r11,r11 ; (DEBUG/TRACE) on the interrupt
bne+ notonintstack ; (DEBUG/TRACE) stack
BREAKPOINT_TRAP
notonintstack:
#endif
stw r4,THREAD_CONTINUATION(r3)
cmpwi cr1,r4,0 /* used waaaay down below */
lwz r11,0(r12)
stw r11,THREAD_KERNEL_STACK(r3)
/*
* Make the new thread the current thread.
*/
lwz r11,PP_CPU_DATA(r6)
stw r5, CPU_ACTIVE_THREAD(r11)
lwz r11,THREAD_KERNEL_STACK(r5)
lwz r5,THREAD_TOP_ACT(r5)
lwz r10,PP_ACTIVE_STACKS(r6)
lwz r7,CTHREAD_SELF(r5) ; Pick up the user assist word
lwz r8,ACT_MACT_PCB(r5) /* Get the PCB for the new guy */
stw r11,0(r10) ; Save the kernel stack address
stw r7,UAW(r6) ; Save the assist word for the "ultra fast path"
lwz r7,ACT_MACT_SPF(r5) ; Get the special flags
lwz r10,ACT_KLOADED(r5)
li r0,0
cmpwi cr0,r10,0
lwz r10,PP_ACTIVE_KLOADED(r6)
stw r7,spcFlags(r6) ; Set per_proc copy of the special flags
beq cr0,.L_sw_ctx_not_kld
stw r5,0(r10)
b .L_sw_ctx_cont
.L_sw_ctx_not_kld:
stw r0,0(r10) /* act_kloaded = 0 */
.L_sw_ctx_cont:
lis r10,hi16(EXT(trcWork)) ; Get top of trace mask
rlwinm r7,r8,0,0,19 /* Switch to savearea base */
ori r10,r10,lo16(EXT(trcWork)) ; Get bottom of mask
lwz r11,SAVprev(r8) /* Get the previous of the switchee's savearea */
lwz r10,traceMask(r10) ; Get the enabled traces
lis r0,hi16(CutTrace) ; Trace FW call
mr. r10,r10 ; Any tracing going on?
ori r0,r0,lo16(CutTrace) ; Trace FW call
beq+ cswNoTrc ; No trace today, dude...
mr r10,r3 ; Save across trace
lwz r2,THREAD_TOP_ACT(r3) ; Trace old activation
mr r3,r11 ; Trace prev savearea
sc ; Cut trace entry of context switch
mr r3,r10 ; Restore
cswNoTrc: mfmsr r6 /* Get the MSR because the switched to thread should inherit it */
lwz r7,SACvrswap(r7) /* Get the translation from virtual to real */
lis r0,hi16(SwitchContextCall) /* Top part of switch context */
lis r9,hi16(EXT(switch_in)) /* Get top of switch in routine */
stw r11,ACT_MACT_PCB(r5) /* Dequeue the savearea we're switching to */
rlwinm r6,r6,0,MSR_FP_BIT+1,MSR_FP_BIT-1 /* Turn off the FP */
ori r9,r9,lo16(EXT(switch_in)) /* Bottom half of switch in */
lwz r5,savesrr0(r8) /* Set up the new SRR0 */
rlwinm r6,r6,0,MSR_VEC_BIT+1,MSR_VEC_BIT-1 /* Turn off the vector */
mr r4,r3 /* Save our old thread to pass back */
stw r9,savesrr0(r8) /* Make us jump to the switch in routine */
li r10,MSR_SUPERVISOR_INT_OFF /* Get the switcher's MSR */
lwz r9,SAVflags(r8) /* Get the flags */
stw r10,savesrr1(r8) /* Set up for switch in */
rlwinm r9,r9,0,15,13 /* Reset the syscall flag */
ori r0,r0,lo16(SwitchContextCall) /* Bottom part of switch context */
rlwinm r9,r9,0,1,31 /* Clear the attached flag */
xor r3,r7,r8 /* Get the physical address of the new context save area */
stw r9,SAVflags(r8) /* Set the flags */
/* if blocking on continuation avoid saving state */
bne cr1,1f
sc /* Switch to the new context */
/* We come back here in the new thread context
* R4 was set to hold the old thread pointer, but switch_in will put it into
* R3 where it belongs.
*/
blr /* Jump into the new thread */
1: stw r5,savesrr0(r8) /* go to real pc */
stw r4,saver3(r8) /* must pass back old thread */
b EXT(exception_exit) /* blocking on continuation, avoid state save */
/*
* All switched to threads come here first to clean up the old thread.
* We need to do the following contortions because we need to keep
* the LR clean. And because we need to manipulate the savearea chain
* with translation on. If we could, this should be done in lowmem_vectors
* before translation is turned on. But we can't, dang it!
*
* R3 = switcher's savearea
* saver4 = old thread in switcher's save
* saver5 = new SRR0 in switcher's save
* saver6 = new SRR1 in switcher's save
*/
ENTRY(switch_in, TAG_NO_FRAME_USED)
lwz r4,saver4(r3) /* Get the old thread */
li r8,MSR_VM_OFF /* Set to everything off */
lwz r9,THREAD_TOP_ACT(r4) /* Get the switched from ACT */
lwz r5,saver5(r3) /* Get the srr0 value */
lwz r10,ACT_MACT_PCB(r9) /* Get the top PCB on the old thread */
lwz r6,saver6(r3) /* Get the srr1 value */
stw r3,ACT_MACT_PCB(r9) /* Put the new one on top */
stw r10,SAVprev(r3) /* Chain on the old one */
mr r3,r4 /* Pass back the old thread */
mtsrr0 r5 /* Set return point */
mtsrr1 r6 /* Set return MSR */
rfi /* Jam... */
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
.long 0
/*
* void fpu_save(void)
*
* To do the floating point and VMX, we keep three thread pointers: one
* to the current thread, one to the thread that has the floating point context
* loaded into the FPU registers, and one for the VMX owner.
*
* Each of these threads has three PCB pointers. The normal PCB, the FPU pcb,
* and the VMX pcb. There is also a bit for each in the savearea flags.
* When we take an exception, or need to use the FPU/VMX in the kernel, we call
* this routine. It checks to see if there is an owner thread for the facility.
* If so, it saves the facility's state information in the normal PCB. Then, it
* turns on the appropriate flag in the savearea to indicate that the state is
* in that particular savearea. Also, the thread pointer for the owner in
* the per_processor block is cleared. Note that we don't have to worry about the
* PCB pointers in the thread because whenever the state is loaded, the associated
* savearea is released and the pointer cleared. This is done so that the facility
* context always migrates to the normal savearea/PCB. This always insures that
* no more than 2 saveareas are used for a thread.
*
* When the context is loaded into the facility, the associated PCB is released if
* its usage flags indicate that it is empty. (Note that return from exception and
* context switch honor these flags and won't release a savearea if there is unrestored
* facility context.) The per_processor is set to point to the facility owner's
* thread and the associated PCB pointer within the thread is cleared because
* the PCB has been released.
*
* Part of loading a context is to release the savearea. If the savearea contains
* other context, the savearea cannot be released. So, what we're left with is
* that there will be no normal context savearea, but one for the as-not-yet
* restored facility savearea. Again, when that context is reloaded, the PCB
* is released, and when it is again stored, it goes into the "normal" savearea.
*
* So, what do we do when there is no general context, and we have some FPU/VMX
* state to save? Heck if I know, but it happens when we switch threads when
* we shortcut system calls. The question is: does the target thread carry the
* FPU/VMX context with it or not? Actually, it don't matter, not one little bit.
* If we are asked to save it, we gotta. It's a really lousy way to do it, but
* short of starting over with FPUs, it's what's what. Therefore, we'll
* allocate an FPU context save and attach it.
*
* Actually, it's not quite that simple: since we aren't in
* in interrupt handler context (that's only in fpu_switch) we can't use
* quickfret to merge FPU into general context. So, if there is an FPU
* savearea, we need to use that. So what we do is: if there is FPU context
* use that. If there is a general context, then use that. If neither,
* allocate a savearea and make that the FPU context.
*
* The next thing we have to do is to allow the kernel to use both the
* floating point and Altivec. It is not recommended, but there may be a
* good reason to do so. So, what we need to do is to treat each of the
* three types of context the same, by keeping a LIFO chain of states.
* We do have a problem with that in that there can be multiple levels of
* kernel context. For example, we are using floating point and we take a
* page fault, and somehow start using the FPU, and take another page fault,
* etc.
*
* Anyway, we will hope that we only reasonably use floating point and vectors in
* the kernel. And try to pack the context in as few saveareas as possible.
*
* The way we keep these "levels" of floating point or vector context straight is
* to remember the top of the normal savearea chain when we activate the
* facility when it is first used. Then, when we save that context, this value
* is saved in its level field.
*
* What the level concept gives us is a way to distinguish between multiple
* independent contexts under the same thread activation. Any time we take
* any kind of interruption (trap, system call, I/O interruption), we are,
* in effect, running with a different context even though we are in the
* same thread. The top savearea address is used only as a marker. It does not
* point to any context associated with the float or vector context. For example,
* the top savearea pointer will always be 0 for the user context, because there
* it it always last on the list.
*
* As normal context is unstacked, the first facility context is checked and
* if there is a match, the facility savearea is released. This is because we
* are returning to a level before the facility saved there was used. In effect,
* this allows us to unwind the facility context saveareas at different rates.
*
* In conjunction with the current activation, these markers are also used to
* determine the state of the facility enablement. Whenever the facility context is
* "live," i.e., loaded in the hardware registers and belonging to the currently
* running context, the facility is enabled before dispatch.
*
* There is nothing special about using floating point or vector facilities,
* no preliminary saving, enabling, or disabling. You just use them. The only exception
* is during context switching on an SMP system. In this case, the context must
* be saved as there is no guarantee that the thread will resume on the same
* processor. This is not a good thing, not at all.
*
* Whenever we switch out a thread with a dirty context, we always need to save it
* because it can wake up on a different processor. However, once the context has
* been saved, we don't need to save it again until it gets dirty, nor do we need
* to reload it unless someone else's context has been loaded. To handle this
* optimization, we need 3 things. We need to know what processor the saved context
* was last loaded on, whether the loaded context could be dirty, and if we've already
* saved it.
*
* Whenever the facility is enabled, the processor ID is saved in the activation. This
* will show which processor has dirty data. When a context switch occurs, the facility
* contexts are saved, but are still remembered as live. The next time we need to
* context switch, we first check if the state is live, and if not, do no state
* saving. Then we check if the state has already been save and if not, save it.
* The facility is always disabled on a context switch. On a UP, this save function
* does not occur.
*
* Whenever a facility unavailable interruption occurs, the current state is saved
* if it is live and unsaved. However, if the live state is the same as the new
* one to be loaded, the processor ID is checked and if it is the current processor
* the state does not need to be loaded or saved. The facility is simply enabled.
*
* Once allocated, facility saveareas are not released until a return is made to a
* previous level. Once a facility has been enabled, there is no way to tell if
* it will ever be used again, but it is likely. Therefore, discarding a savearea
* when its context is made live is extra overhead. So, we don't do it, but we
* do mark the savearea contents as invalid.
*
*/
/*
; The following is the actual way it is implemented. It doesn't quite match
; the above text. I need to go and fix that.
;
; Context download (operates on owner's data):
;
; 0) enable facility
; 1) if no owner exit to context restore
; 2) if context processor != current processor exit to context restore
; 3) if current activation == owner activation:
; 1) if curr level == active level:
; 1) if top facility savearea exists:
; invalidate savearea by setting level to 1
; 2) enable facility for user
; 3) exit
;
; 2) else go to 5
;
; 4) if curr level == active level:
; 1) if top facility savearea exists:
; 1) if top save level == active level exit to context restore
;
; 5) allocate savearea
; 1) if there is a facility save and it is invalid, select it, and break
; 2) scan normal list for free facility area, select if found, and break
; 3) scan other facility for free save: select, if found, and break
; 4) allocate a new save area
;
; 6) save context
; 7) mark facility save with curr level
; 8) if reusing cached savearea (case #1) exit to context restore
; 9) set facility save backchain to facility top savearea
; 10) set facility top to savearea
; 11) exit to context restore
;
;
; Context restore/upload (operates on current activation's data):
;
; 1) set current to activation
; 2) set active level to current level
; 3) set context processor to current processor
; 4) if no facility savearea or top save level != curr level
; initialize facility registers to empty value
; 5) else
; 1) load registers from savearea
; 2) invalidate save area by setting level to 1
;
; 6) enable facility for user
; 7) exit to interrupt return
;
;
; Context save (operates on current activation's data; only used during context switch):
; (context switch always disables the facility)
;
; 1) if no owner exit
; 2) if owner != current activation exit
; 3) if context processor != current processor
; 1) clear owner
; 2) exit
;
; 4) if facility top savearea level exists and == active level exit
; 5) if curr level != active level exit
; 6) allocate savearea
; 1) if there is a facility save and it is invalid, select it, and break
; 2) scan normal list for free facility area, select if found, and break
; 3) scan other facility for free save: select, if found, and break
; 4) allocate a new save area
; 7) save context
; 8) mark facility savearea with curr level
; 9) if reusing cached savearea (case #1) exit
; 10) set facility save backchain to facility top savearea
; 11) set facility top to savearea
; 12) exit
;
;
; Exception exit (hw_exceptions):
;
; 1) disable return facility
; 2) if returning savearea != active level
; 1) if owner != current activation exit
; 2) if context processor != current processor:
; 1) clear owner
; 2) exit
;
; 3) if new level != active level exit
; 4) enable return facility
; 5) exit
;
; 3) if no facility savearea exit
; 4) if top save level == active or top is invalid
; 1) dequeue top facility savearea
; 2) set active level to new top savearea's level
; 3) release savearea
; 4) if owner == current activation clear owner
; 5) exit
;
;
;
;
; if (owner == activation) && (curr level == active level)
; && (activation processor == current processor) ::= context live
*/
ENTRY(fpu_save, TAG_NO_FRAME_USED)
mfmsr r0 ; Get the MSR
rlwinm r0,r0,0,MSR_FP_BIT+1,MSR_FP_BIT-1 ; Turn off floating point forever
rlwinm r2,r0,0,MSR_EE_BIT+1,MSR_EE_BIT-1 ; But do interrupts only for now
ori r2,r2,MASK(MSR_FP) ; Enable the floating point feature for now also
mtmsr r2 ; Set the MSR
isync
mfsprg r6,0 ; Get the per_processor block
lwz r12,PP_FPU_THREAD(r6) ; Get the thread that owns the FPU
#if FPVECDBG
mr r7,r0 ; (TEST/DEBUG)
li r4,0 ; (TEST/DEBUG)
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
mr. r3,r12 ; (TEST/DEBUG)
li r2,0x6F00 ; (TEST/DEBUG)
li r5,0 ; (TEST/DEBUG)
beq- noowneryet ; (TEST/DEBUG)
lwz r4,ACT_MACT_FPUlvl(r12) ; (TEST/DEBUG)
lwz r5,ACT_MACT_FPU(r12) ; (TEST/DEBUG)
noowneryet: oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
mr r0,r7 ; (TEST/DEBUG)
#endif
mflr r2 ; Save the return address
lwz r10,PP_CPU_DATA(r6) ; Get the CPU data pointer
lhz r11,PP_CPU_NUMBER(r6) ; Get our CPU number
mr. r12,r12 ; Anyone own the FPU?
lwz r10,CPU_ACTIVE_THREAD(r10) ; Get the pointer to the active thread
beq- fsret ; Nobody owns the FPU, no save required...
lwz r10,THREAD_TOP_ACT(r10) ; Now get the activation that is running
lwz r9,ACT_MACT_FPUcpu(r12) ; Get the last CPU to use this context
cmplw r12,r10 ; Do we own the FPU?
cmplw cr1,r9,r11 ; Was the context for this processor?
bne+ fsret ; Facility belongs to some other activation...
li r3,0 ; Assume we need a fix-me-up
beq- cr1,fsgoodcpu ; Facility last used on this processor...
stw r3,PP_FPU_THREAD(r6) ; Clear owner because it was really on the other processor
b fsret ; Bail now with no save...
fsgoodcpu: lwz r3,ACT_MACT_FPU(r12) ; Get the current FPU savearea for the thread
lwz r9,ACT_MACT_FPUlvl(r12) ; Get our current level indicator
cmplwi cr1,r3,0 ; Have we ever saved this facility context?
beq- cr1,fsneedone ; Never saved it, so we need an area...
lwz r8,SAVlvlfp(r3) ; Get the level this savearea is for
cmplwi r8,1 ; See if it is a spare
cmplw cr1,r9,r8 ; Correct level?
beq+ fsusespare ; We have a spare to use...
beq- cr1,fsret ; The current level is already saved, bail out...
fsneedone: li r3,0 ; Tell the routine to allocate an area if none found
bl fpsrchsave ; Find a free savearea
mfsprg r6,0 ; Get back per_processor block
oris r7,r7,hi16(SAVfpuvalid) ; Set the allocated bit
lwz r12,PP_FPU_THREAD(r6) ; Get back our thread
mtlr r2 ; Restore return
lwz r8,ACT_MACT_FPU(r12) ; Get the current top floating point savearea
lwz r9,ACT_MACT_FPUlvl(r12) ; Get our current level indicator again
stw r3,ACT_MACT_FPU(r12) ; Set this as the latest FPU savearea for the thread
stw r8,SAVprefp(r3) ; And then chain this in front
stw r7,SAVflags(r3) ; Set the validity flags
stw r12,SAVact(r3) ; Make sure we point to the right guy
fsusespare: stw r9,SAVlvlfp(r3) ; And set the level this savearea is for
;
; Save the current FPU state into the PCB of the thread that owns it.
;
la r11,savefp0(r3) ; Point to the 1st line
dcbz 0,r11 ; Allocate the first savearea line
la r11,savefp4(r3) /* Point to the 2nd line */
stfd f0,savefp0(r3)
dcbz 0,r11 /* allocate it */
stfd f1,savefp1(r3)
stfd f2,savefp2(r3)
la r11,savefp8(r3) /* Point to the 3rd line */
stfd f3,savefp3(r3)
dcbz 0,r11 /* allocate it */
stfd f4,savefp4(r3)
stfd f5,savefp5(r3)
stfd f6,savefp6(r3)
la r11,savefp12(r3) /* Point to the 4th line */
stfd f7,savefp7(r3)
dcbz 0,r11 /* allocate it */
stfd f8,savefp8(r3)
stfd f9,savefp9(r3)
stfd f10,savefp10(r3)
la r11,savefp16(r3) /* Point to the 5th line */
stfd f11,savefp11(r3)
dcbz 0,r11 /* allocate it */
stfd f12,savefp12(r3)
stfd f13,savefp13(r3)
stfd f14,savefp14(r3)
la r11,savefp20(r3) /* Point to the 6th line */
stfd f15,savefp15(r3)
stfd f16,savefp16(r3)
stfd f17,savefp17(r3)
stfd f18,savefp18(r3)
la r11,savefp24(r3) /* Point to the 7th line */
stfd f19,savefp19(r3)
dcbz 0,r11 /* allocate it */
stfd f20,savefp20(r3)
lwz r10,liveFPSCR(r6) ; Get the previously saved FPSCR
stfd f21,savefp21(r3)
stfd f22,savefp22(r3)
li r9,0 ; Just clear this out
la r11,savefp28(r3) /* Point to the 8th line */
stfd f23,savefp23(r3)
dcbz 0,r11 /* allocate it */
stfd f24,savefp24(r3)
stfd f25,savefp25(r3)
stfd f26,savefp26(r3)
stfd f27,savefp27(r3)
stfd f28,savefp28(r3)
; Note that we just save the FPSCR here for ease. It is really already saved
; in the "normal" context area of the savearea.
stw r9,savefpscrpad(r3) ; Save the FPSCR pad
stw r10,savefpscr(r3) ; Save the FPSCR
stfd f29,savefp29(r3)
stfd f30,savefp30(r3)
stfd f31,savefp31(r3)
lfd f0,savefp0(r3) ; We need to restore F0 because we used it
; to get the FPSCR
#if 0
la r9,savefp0(r3) ; (TEST/DEBUG)
la r10,savefp31(r3) ; (TEST/DEBUG)
chkkillmedead:
lha r8,0(r9) ; (TEST/DEBUG)
addi r9,r9,8 ; (TEST/DEBUG)
cmpwi r8,-8 ; (TEST/DEBUG)
cmplw cr1,r9,r10 ; (TEST/DEBUG)
bne+ dontkillmedead ; (TEST/DEBUG)
BREAKPOINT_TRAP ; (TEST/DEBUG)
dontkillmedead: ; (TEST/DEBUG)
ble+ cr1,chkkillmedead ; (TEST/DEBUG)
#endif
fsret: mtmsr r0 ; Put interrupts on if they were and floating point off
isync
blr
/*
* fpu_switch()
*
* Entered to handle the floating-point unavailable exception and
* switch fpu context
*
* This code is run in virtual address mode on with interrupts off.
*
* Upon exit, the code returns to the users context with the floating
* point facility turned on.
*
* ENTRY: VM switched ON
* Interrupts OFF
* State is saved in savearea pointed to by R4.
* All other registers are free.
*
*/
ENTRY(fpu_switch, TAG_NO_FRAME_USED)
#if DEBUG
#if GDDBG
mr r7,r4 ; Save input parameter
lis r3,hi16(EXT(fpu_trap_count)) ; Get address of FP trap counter
ori r3,r3,lo16(EXT(fpu_trap_count)) ; Get address of FP trap counter
lwz r1,0(r3)
lis r5,hi16(EXT(GratefulDeb)) ; Point to top of display
ori r5,r5,lo16(EXT(GratefulDeb)) ; Put in bottom part
addi r1,r1,1
mtlr r5 ; Set link register
stw r1,0(r3)
mr r4,r1
li r3,0
blrl ; Display count
mr r4,r7 ; Restore the parameter
#else
lis r3,hi16(EXT(fpu_trap_count)) ; Get address of FP trap counter
ori r3,r3,lo16(EXT(fpu_trap_count)) ; Get address of FP trap counter
lwz r1,0(r3)
addi r1,r1,1
stw r1,0(r3)
#endif
#endif /* DEBUG */
mfsprg r6,0 ; Get the per_processor block
mfmsr r19 ; Get the current MSR
lwz r10,PP_CPU_DATA(r6) ; Get the CPU data pointer
lwz r12,PP_FPU_THREAD(r6) ; Get the thread that owns the FPU
lwz r10,CPU_ACTIVE_THREAD(r10) ; Get the pointer to the active thread
ori r19,r19,lo16(MASK(MSR_FP)) ; Enable the floating point feature
lwz r17,THREAD_TOP_ACT(r10) ; Now get the activation that is running
; R12 has the "old" activation
; R17 has the "new" activation
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F01 ; (TEST/DEBUG)
mr r3,r12 ; (TEST/DEBUG)
mr r5,r17 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
mr. r12,r12 ; See if there is any live FP status
lhz r18,PP_CPU_NUMBER(r6) ; Get the current CPU, we will need it later
mtmsr r19 ; Enable floating point instructions
isync
beq- fsnosave ; No live context, so nothing to save...
lwz r19,ACT_MACT_FPUcpu(r12) ; Get the "old" active CPU
lwz r15,ACT_MACT_PCB(r12) ; Get the current level of the "old" one
cmplw r18,r19 ; Check the CPU that the old context is live on
lwz r14,ACT_MACT_FPU(r12) ; Point to the top of the old context stack
bne- fsnosave ; Context is not live if used on a different CPU...
lwz r13,ACT_MACT_FPUlvl(r12) ; Get the "old" active level
;
; First, check to see if all we are doing is enabling because the
; "new" context is live.
;
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F02 ; (TEST/DEBUG)
mr r1,r15 ; (TEST/DEBUG)
mr r3,r13 ; (TEST/DEBUG)
mr r5,r14 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
cmplw cr1,r12,r17 ; Are the "old" activation and the "new" the same?
cmplwi cr2,r14,0 ; Is there any saved context on the "old" activation?
bne+ cr1,fsmstsave ; The activations are different so "old" context must be saved...
;
; Here we know that both the "old" and "new" activations are the same. We will
; check the current level and active levels. If they are the same, the context is
; already live, so all we do is turn on the facility and invalidate the top
; savearea.
;
; If the current level, the active level, and the top savearea level are the
; same, then the context was saved as part of a thread context switch and neither
; needs saving or restoration.
;
; In all other cases, the context must be saved unless we are just re-enabling
; floating point.
;
cmplw r13,r15 ; Are the levels the same?
cmplwi cr2,r14,0 ; Is there any saved context?
bne- fsmstsave ; Levels are different, we need to save...
beq- cr2,fsenable ; No saved context at all, enable and go...
lwz r20,SAVlvlfp(r14) ; Get the level of the top savearea
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F03 ; (TEST/DEBUG)
mr r3,r15 ; (TEST/DEBUG)
mr r5,r20 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
cmplw r15,r20 ; Is the top level the same as the current?
li r0,1 ; Get the invalid flag
bne- fsenable ; Not the same, just enable and go...
stw r0,SAVlvlfp(r14) ; Invalidate that top savearea
b fsenable ; Then enable and go...
;
; We need to save the "old" context here. The LIFO queueing scheme works
; out for all cases because if both the "new" and "old" activations are the
; same, there can not be any saved state to load. the "new" level is
; truely new.
;
; When we save the context, we either use a new savearea, or the free
; one that is cached at the head of the list.
fsmstsave: beq- cr2,fsgetsave ; There is no possible cached save area
lwz r5,SAVlvlfp(r14) ; Get the level of first facility savearea
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F04 ; (TEST/DEBUG)
mr r3,r15 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
mr r3,r14 ; Assume we are invalid
cmplwi r5,1 ; Is it invalid?
cmplw cr1,r5,r13 ; Is the SA level the active one?
beq+ fsusecache ; Invalid, just use it...
beq- cr1,fsnosave ; The SA level is active, it is already saved...
fsgetsave: mr r3,r4 ; Use the interrupt save as the context savearea if none cached
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F05 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
bl fpsrchsave ; Find a free savearea
stw r3,ACT_MACT_FPU(r12) ; Set this as the latest context savearea for the thread
mfsprg r6,0 ; Get back per_processor block
stw r14,SAVprefp(r3) ; And then chain this in front
oris r7,r7,hi16(SAVfpuvalid) ; Set the allocated bit
stw r12,SAVact(r3) ; Make sure we point to the right guy
stw r7,SAVflags(r3) ; Set the allocation flags
fsusecache: la r11,savefp0(r3) ; Point to the 1st line in area
stw r13,SAVlvlfp(r3) ; Set this context level
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F06 ; (TEST/DEBUG)
mr r5,r13 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
;
; Now we will actually save the old context
;
dcbz 0,r11 ; Allocate the output area
la r11,savefp4(r3) ; Point to the 2nd line
stfd f0,savefp0(r3)
dcbz 0,r11 ; Allocate cache
stfd f1,savefp1(r3)
stfd f2,savefp2(r3)
la r11,savefp8(r3) ; Point to the 3rd line
stfd f3,savefp3(r3)
dcbz 0,r11 ; Allocate cache
stfd f4,savefp4(r3)
stfd f5,savefp5(r3)
stfd f6,savefp6(r3)
la r11,savefp12(r3) ; Point to the 4th line
stfd f7,savefp7(r3)
dcbz 0,r11 ; Allocate cache
stfd f8,savefp8(r3)
stfd f9,savefp9(r3)
stfd f10,savefp10(r3)
la r11,savefp16(r3) ; Point to the 5th line
stfd f11,savefp11(r3)
dcbz 0,r11 ; Allocate cache
stfd f12,savefp12(r3)
stfd f13,savefp13(r3)
stfd f14,savefp14(r3)
la r11,savefp20(r3) ; Point to the 6th line
stfd f15,savefp15(r3)
dcbz 0,r11 ; Allocate cache
stfd f16,savefp16(r3)
stfd f17,savefp17(r3)
stfd f18,savefp18(r3)
la r11,savefp24(r3) ; Point to the 7th line
stfd f19,savefp19(r3)
dcbz 0,r11 ; Allocate cache
stfd f20,savefp20(r3)
li r14,0 ; Clear this for now
lwz r15,liveFPSCR(r6) ; Get the previously saved FPSCR
stfd f21,savefp21(r3)
stfd f22,savefp22(r3)
la r11,savefp28(r3) ; Point to the 8th line
stfd f23,savefp23(r3)
dcbz 0,r11 ; allocate it
stfd f24,savefp24(r3)
stfd f25,savefp25(r3)
stfd f26,savefp26(r3)
la r11,savefpscrpad(r3) ; Point to the 9th line
stfd f27,savefp27(r3)
dcbz 0,r11 ; allocate it
stfd f28,savefp28(r3)
stfd f29,savefp29(r3)
stfd f30,savefp30(r3)
stfd f31,savefp31(r3)
; Note that we just save the FPSCR here for ease. It is really already saved
; in the "normal" context area of the savearea.
stw r14,savefpscrpad(r3) ; Save the FPSCR pad
stw r15,savefpscr(r3) ; Save the FPSCR
;
; The context is all saved now and the facility is free.
;
; Now check out the "new" and see if we need to load up his context.
; If we do (and this should be the normal case), do it and then invalidate the
; savearea. (This will keep it cached for quick access next time around.)
;
; If we do not (remember, we already took care of the case where we just enable
; the FPU), we need to fill the registers with junk, because this level has
; never used them before and some thieving bastard could hack the old values
; of some thread! Just imagine what would happen if they could! Why, nothing
; would be safe! My God! It is terrifying!
;
fsnosave: lwz r15,ACT_MACT_PCB(r17) ; Get the current level of the "new" one
lwz r14,ACT_MACT_FPU(r17) ; Point to the top of the "new" context stack
lwz r13,ACT_MACT_FPUlvl(r17) ; Get the "new" active level
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F07 ; (TEST/DEBUG)
mr r1,r15 ; (TEST/DEBUG)
mr r3,r14 ; (TEST/DEBUG)
mr r5,r13 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
cmplwi cr1,r14,0 ; Do we possibly have some context to load?
stw r15,ACT_MACT_FPUlvl(r17) ; Set the "new" active level
stw r18,ACT_MACT_FPUcpu(r17) ; Set the active CPU
la r11,savefp0(r14) ; Point to first line to bring in
stw r17,PP_FPU_THREAD(r6) ; Store current thread address in fpu_thread to claim fpu for thread
beq+ cr1,MakeSureThatNoTerroristsCanHurtUsByGod ; No "new" context to load...
lwz r0,SAVlvlfp(r14) ; Get the level of first facility savearea
cmplw r0,r15 ; Top level correct to load?
bne- MakeSureThatNoTerroristsCanHurtUsByGod ; No, go initialize...
#if FPVECDBG
lis r0,HIGH_ADDR(CutTrace) ; (TEST/DEBUG)
li r2,0x7F08 ; (TEST/DEBUG)
oris r0,r0,LOW_ADDR(CutTrace) ; (TEST/DEBUG)
sc ; (TEST/DEBUG)
#endif
dcbt 0,r11 ; Touch line in
li r0,1 ; Get the level invalid indication
la r11,savefp4(r14) ; Point to next line
dcbt 0,r11 ; Touch line in
lfd f0, savefp0(r14)
lfd f1,savefp1(r14)
stw r0,SAVlvlfp(r14) ; Mark the savearea invalid because we are activating again
lfd f2,savefp2(r14)
la r11,savefp8(r14) ; Point to next line
lfd f3,savefp3(r14)
dcbt 0,r11 ; Touch line in
lfd f4,savefp4(r14)
lfd f5,savefp5(r14)
lfd f6,savefp6(r14)
la r11,savefp12(r14) ; Point to next line
lfd f7,savefp7(r14)
dcbt 0,r11 ; Touch line in
lfd f8,savefp8(r14)
lfd f9,savefp9(r14)
lfd f10,savefp10(r14)
la r11,savefp16(r14) ; Point to next line
lfd f11,savefp11(r14)
dcbt 0,r11 ; Touch line in
lfd f12,savefp12(r14)
lfd f13,savefp13(r14)
lfd f14,savefp14(r14)
la r11,savefp20(r14) ; Point to next line
lfd f15,savefp15(r14)
dcbt 0,r11 ; Touch line in
lfd f16,savefp16(r14)
lfd f17,savefp17(r14)
lfd f18,savefp18(r14)
la r11,savefp24(r14) ; Point to next line
lfd f19,savefp19(r14)
dcbt 0,r11 ; Touch line in
lfd f20,savefp20(r14)
lfd f21,savefp21(r14)
la r11,savefp28(r14) ; Point to next line
lfd f22,savefp22(r14)
lfd f23,savefp23(r14)
dcbt 0,r11 ; Touch line in
lfd f24,savefp24(r14)
lfd f25,savefp25(r14)
lfd f26,savefp26(r14)
lfd f27,savefp27(r14)
lfd f28,savefp28(r14)