]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/vmachmon_asm.s
a1039dceb7fbf75a2d10fe869b880197edd6d7c7
[apple/xnu.git] / osfmk / ppc / vmachmon_asm.s
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 #include <assym.s>
23 #include <debug.h>
24 #include <ppc/asm.h>
25 #include <ppc/proc_reg.h>
26 #include <ppc/exception.h>
27
28 /*
29 * This file contains implementations for the Virtual Machine Monitor
30 * facility.
31 */
32
33
34 /*
35 * int vmm_dispatch(savearea, act);
36
37 * vmm_dispatch is a PPC only system call. It is used with a selector (first
38 * parameter) to determine what function to enter. This is treated as an extension
39 * of hw_exceptions.
40 *
41 * Inputs:
42 * R4 = current activation
43 * R16 = current thread
44 * R30 = current savearea
45 */
46
47 .align 5 /* Line up on cache line */
48 .globl EXT(vmm_dispatch_table)
49
50 LEXT(vmm_dispatch_table)
51
52 /* Don't change the order of these routines in the table. It's */
53 /* OK to add new routines, but they must be added at the bottom. */
54
55 .long EXT(vmm_get_version_sel) ; Get the version of the VMM interface
56 .long EXT(vmm_get_features_sel) ; Get the features of the VMM interface
57 .long EXT(vmm_init_context_sel) ; Initializes a new VMM context
58 .long EXT(vmm_tear_down_context) ; Tears down a previously-allocated VMM context
59 .long EXT(vmm_tear_down_all) ; Tears down all VMMs
60 .long EXT(vmm_map_page) ; Maps a page from the main address space into the VM space
61 .long EXT(vmm_get_page_mapping) ; Returns client va associated with VM va
62 .long EXT(vmm_unmap_page) ; Unmaps a page from the VM space
63 .long EXT(vmm_unmap_all_pages) ; Unmaps all pages from the VM space
64 .long EXT(vmm_get_page_dirty_flag) ; Gets the change bit for a page and optionally clears it
65 .long EXT(vmm_get_float_state) ; Gets current floating point state
66 .long EXT(vmm_get_vector_state) ; Gets current vector state
67 .long EXT(vmm_set_timer) ; Sets a timer value
68 .long EXT(vmm_get_timer) ; Gets a timer value
69 .long EXT(switchIntoVM) ; Switches to the VM context
70
71 .set vmm_count,(.-EXT(vmm_dispatch_table))/4 ; Get the top number
72
73
74 .align 5
75 .globl EXT(vmm_dispatch)
76
77 LEXT(vmm_dispatch)
78
79 lwz r11,saver3(r30) ; Get the selector
80 mr r3,r4 ; All of our functions want the activation as the first parm
81 lis r10,hi16(EXT(vmm_dispatch_table)) ; Get top half of table
82 cmplwi r11,kVmmExecuteVM ; Should we switch to the VM now?
83 cmplwi cr1,r11,vmm_count ; See if we have a valid selector
84 ori r10,r10,lo16(EXT(vmm_dispatch_table)) ; Get low half of table
85 lwz r4,saver4(r30) ; Get 1st parameter after selector
86 beq+ EXT(switchIntoVM) ; Yes, go switch to it....
87 rlwinm r11,r11,2,0,29 ; Index into table
88 bgt- cr1,vmmBogus ; It is a bogus entry
89 lwzx r10,r10,r11 ; Get address of routine
90 lwz r5,saver5(r30) ; Get 2nd parameter after selector
91 lwz r6,saver6(r30) ; Get 3rd parameter after selector
92 mtlr r10 ; Set the routine address
93 lwz r7,saver7(r30) ; Get 4th parameter after selector
94 ;
95 ; NOTE: currently the most paramters for any call is 4. We will support at most 8 because we
96 ; do not want to get into any stack based parms. However, here is where we need to add
97 ; code for the 5th - 8th parms if we need them.
98 ;
99
100 blrl ; Call function
101
102 stw r3,saver3(r30) ; Pass back the return code
103 li r3,1 ; Set normal return with check for AST
104 b EXT(ppcscret) ; Go back to handler...
105
106 vmmBogus: eqv r3,r3,r3 ; Bogus selector, treat like a bogus system call
107 b EXT(ppcscret) ; Go back to handler...
108
109
110 .align 5
111 .globl EXT(vmm_get_version_sel)
112
113 LEXT(vmm_get_version_sel) ; Selector based version of get version
114
115 lis r3,hi16(EXT(vmm_get_version))
116 ori r3,r3,lo16(EXT(vmm_get_version))
117 b selcomm
118
119
120 .align 5
121 .globl EXT(vmm_get_features_sel)
122
123 LEXT(vmm_get_features_sel) ; Selector based version of get features
124
125 lis r3,hi16(EXT(vmm_get_features_sel))
126 ori r3,r3,lo16(EXT(vmm_get_features_sel))
127 b selcomm
128
129
130 .align 5
131 .globl EXT(vmm_init_context_sel)
132
133 LEXT(vmm_init_context_sel) ; Selector based version of init context
134
135 lis r3,hi16(EXT(vmm_init_context_sel))
136 ori r3,r3,lo16(EXT(vmm_init_context_sel))
137
138 selcomm: mtlr r3 ; Set the real routine address
139 mr r3,r30 ; Pass in the savearea
140 blrl ; Call the function
141 b EXT(ppcscret) ; Go back to handler...
142
143 /*
144 * Here is where we transition to the virtual machine.
145 *
146 * We will swap the register context in the savearea with that which is saved in our shared
147 * context area. We will validity check a bit and clear any nasty bits in the MSR and force
148 * the manditory ones on.
149 *
150 * Then we will setup the new address space to run with, and anything else that is normally part
151 * of a context switch.
152 *
153 * Still need to figure out final floats and vectors. For now, we will go brute
154 * force and when we go into the VM, we will force save any normal floats and
155 * vectors. Then we will hide them and swap the VM copy (if any) into the normal
156 * chain. When we exit VM we will do the opposite. This is not as fast as I would
157 * like it to be.
158 *
159 *
160 */
161
162 .align 5
163 .globl EXT(switchIntoVM)
164
165 LEXT(switchIntoVM)
166 lwz r5,vmmControl(r3) ; Pick up the control table address
167 subi r4,r4,1 ; Switch to zero offset
168 rlwinm. r2,r5,0,0,30 ; Is there a context there? (Note: we will ignore bit 31 so that we
169 ; do not try this while we are transitioning off to on
170 cmplwi cr1,r4,kVmmMaxContextsPerThread ; Is the index valid?
171 beq- vmmBogus ; Not started, treat like a bogus system call
172 mulli r2,r4,vmmCEntrySize ; Get displacement from index
173 bgt- cr1,swvmmBogus ; Index is bogus...
174 add r2,r2,r5 ; Point to the entry
175
176 lwz r4,vmmFlags(r2) ; Get the flags for the selected entry
177 lwz r5,vmmContextKern(r2) ; Get the context area address
178 rlwinm. r26,r4,0,vmmInUseb,vmmInUseb ; See if the slot is in use
179 bne+ swvmChkIntcpt ; We are so cool. Go do check for immediate intercepts...
180
181 swvmmBogus: li r2,kVmmBogusContext ; Set bogus index return
182 li r3,1 ; Set normal return with check for AST
183 stw r2,saver3(r30) ; Pass back the return code
184 b EXT(ppcscret) ; Go back to handler...
185
186 ;
187 ; Here we check for any immediate intercepts. So far, the only
188 ; one of these is a timer pop. We will not dispatch if the timer has
189 ; already popped. They need to either reset the timer (i.e. set timer
190 ; to 0) or to set a future time.
191 ;
192
193 swvmChkIntcpt:
194 rlwinm. r26,r4,0,vmmTimerPopb,vmmTimerPopb ; Did the timer pop?
195 beq+ swvmDoSwitch ; No...
196
197 li r2,kVmmReturnNull ; Set null return
198 li r3,1 ; Set normal return with check for AST
199 stw r2,saver3(r30) ; Pass back the return code
200 stw r2,return_code(r5) ; Save the exit code
201 b EXT(ppcscret) ; Go back to handler...
202
203 ;
204 ; Here is where we actually swap into the VM (alternate) context.
205 ; We will bulk do a wholesale swap of the registers in the context area (the VMs)
206 ; with the ones in the savearea (our main code). During the copy, we will fix up the
207 ; MSR, forcing on a few bits and turning off a few others. Then we will deal with the
208 ; PMAP and other per_proc stuff. Finally, we will exit back through the main exception
209 ; handler to deal with unstacking saveareas and ASTs, etc.
210 ;
211
212 swvmDoSwitch:
213
214 ;
215 ; First, we save the volatile registers we care about. Remember, all register
216 ; handling here is pretty funky anyway, so we just pick the ones that are ok.
217 ;
218 mr r26,r3 ; Save the activation pointer
219 mr r28,r5 ; Save the context pointer
220 mr r27,r2 ; Save the context entry
221
222 bl vmmxcng ; Exchange the vector and floating point contexts
223 mr r5,r28 ; Restore this register
224
225 lwz r11,ACT_MACT_SPF(r26) ; Get the special flags
226 lwz r3,vmmPmap(r27) ; Get the pointer to the PMAP
227 oris r15,r11,hi16(runningVM) ; ; Show that we are swapped to the VM right now
228 bl EXT(hw_set_user_space_dis) ; Swap the address spaces
229 lwz r17,vmmFlags(r27) ; Get the status flags
230 mfsprg r10,0 ; Get the per_proc
231 rlwinm. r0,r17,0,vmmMapDoneb,vmmMapDoneb ; Did we just do a map function?
232 stw r27,vmmCEntry(r26) ; Remember what context we are running
233 andc r17,r17,r0 ; Turn off map flag
234 beq+ swvmNoMap ; No mapping done...
235
236 ;
237 ; This little bit of hoopala here (triggered by vmmMapDone) is
238 ; a performance enhancement. This will change the returning savearea
239 ; to look like we had a DSI rather than a system call. Then, setting
240 ; the redrive bit, the exception handler will redrive the exception as
241 ; a DSI, entering the last mapped address into the hash table. This keeps
242 ; double faults from happening. Note that there is only a gain if the VM
243 ; takes a fault, then the emulator resolves it only, and then begins
244 ; the VM execution again. It seems like this should be the normal case.
245 ;
246
247 lwz r3,SAVflags(r30) ; Pick up the savearea flags
248 lwz r2,vmmLastMap(r27) ; Get the last mapped address
249 li r20,T_DATA_ACCESS ; Change to DSI fault
250 oris r3,r3,hi16(SAVredrive) ; Set exception redrive
251 stw r2,savedar(r30) ; Set the DAR to the last thing we mapped
252 stw r3,SAVflags(r30) ; Turn on the redrive request
253 lis r2,hi16(MASK(DSISR_HASH)) ; Set PTE/DBAT miss
254 stw r20,saveexception(r30) ; Say we need to emulate a DSI
255 stw r2,savedsisr(r30) ; Pretend we have a PTE miss
256
257 swvmNoMap: rlwimi r15,r17,32-(floatCngbit-vmmFloatCngdb),floatCngbit,vectorCngbit ; Shift and insert changed bits
258 rlwimi r17,r11,8,24,31 ; Save the old spf flags
259 stw r15,spcFlags(r10) ; Set per_proc copy of the special flags
260 stw r15,ACT_MACT_SPF(r26) ; Get the special flags
261
262 stw r17,vmmFlags(r27) ; Set the status flags
263
264 bl swapCtxt ; First, swap the general register state
265
266 lwz r17,vmmContextKern(r27) ; Get the comm area
267
268 lwz r15,vmmCntrl(r17) ; Get the control flags
269
270 rlwinm. r0,r15,0,vmmFloatLoadb,vmmFloatLoadb ; Are there new floating point values?
271 li r14,vmmppcFPRs ; Get displacement to the new values
272 andc r15,r15,r0 ; Clear the bit
273 beq+ swvmNoNewFloats ; Nope, good...
274
275 lwz r3,ACT_MACT_FPU(r26) ; Get the FPU savearea
276 dcbt r14,r18 ; Touch in first line of new stuff
277 mr. r3,r3 ; Is there one?
278 bne+ swvmGotFloat ; Yes...
279
280 bl EXT(save_get) ; Get a savearea
281
282 li r11,0 ; Get a 0
283 lis r7,hi16(SAVfpuvalid) ; Set the allocated bit
284 stw r3,ACT_MACT_FPU(r26) ; Set the floating point savearea
285 stw r7,SAVflags(r3) ; Set the validity flags
286 stw r11,SAVlvlfp(r3) ; Set the context level
287
288 swvmGotFloat:
289 dcbt r14,r17 ; Touch in first line of new stuff
290 la r4,savefp0(r3) ; Point to the destination
291 mr r21,r3 ; Save the save area
292 la r3,vmmppcFPRs(r17) ; Point to the source
293 li r5,33*8 ; Get the size (32 FP + FPSCR at 8 bytes each)
294
295 bl EXT(bcopy) ; Copy the new values
296
297 lwz r11,ACT_MACT_SPF(r26) ; Get the special flags
298 stw r15,vmmCntrl(r17) ; Save the control flags sans vmmFloatLoad
299 rlwinm r11,r11,0,floatCngbit+1,floatCngbit-1 ; Clear the changed bit here
300 lwz r14,vmmStat(r17) ; Get the status flags
301 mfsprg r10,0 ; Get the per_proc
302 stw r11,ACT_MACT_SPF(r26) ; Get the special flags
303 rlwinm r14,r14,0,vmmFloatCngdb+1,vmmFloatCngdb-1 ; Clear the changed flag
304 stw r11,spcFlags(r10) ; Set per_proc copy of the special flags
305 stw r14,vmmStat(r17) ; Set the status flags sans vmmFloatCngd
306 lwz r11,savefpscrpad(r21) ; Get the new fpscr pad
307 lwz r14,savefpscr(r21) ; Get the new fpscr
308 stw r11,savexfpscrpad(r30) ; Save the new fpscr pad
309 stw r14,savexfpscr(r30) ; Save the new fpscr
310
311 swvmNoNewFloats:
312 rlwinm. r0,r15,0,vmmVectLoadb,vmmVectLoadb ; Are there new vector values?
313 li r14,vmmppcVRs ; Get displacement to the new values
314 andc r15,r15,r0 ; Clear the bit
315 beq+ swvmNoNewVects ; Nope, good...
316
317 lwz r3,ACT_MACT_VMX(r26) ; Get the vector savearea
318 dcbt r14,r27 ; Touch in first line of new stuff
319 mr. r3,r3 ; Is there one?
320 bne+ swvmGotVect ; Yes...
321
322 bl EXT(save_get) ; Get a savearea
323
324 li r21,0 ; Get a 0
325 lis r7,hi16(SAVvmxvalid) ; Set the allocated bit
326 stw r3,ACT_MACT_VMX(r26) ; Set the vector savearea indication
327 stw r7,SAVflags(r3) ; Set the validity flags
328 stw r21,SAVlvlvec(r3) ; Set the context level
329
330 swvmGotVect:
331 dcbt r14,r17 ; Touch in first line of new stuff
332 mr r21,r3 ; Save the pointer to the savearea
333 la r4,savevr0(r3) ; Point to the destination
334 la r3,vmmppcVRs(r17) ; Point to the source
335 li r5,33*16 ; Get the size (32 vectors + VSCR at 16 bytes each)
336
337 bl EXT(bcopy) ; Copy the new values
338
339 lwz r11,ACT_MACT_SPF(r26) ; Get the special flags
340 stw r15,vmmCntrl(r17) ; Save the control flags sans vmmVectLoad
341 rlwinm r11,r11,0,vectorCngbit+1,vectorCngbit-1 ; Clear the changed bit here
342 lwz r14,vmmStat(r17) ; Get the status flags
343 mfsprg r10,0 ; Get the per_proc
344 stw r11,ACT_MACT_SPF(r26) ; Get the special flags
345 rlwinm r14,r14,0,vmmVectCngdb+1,vmmVectCngdb-1 ; Clear the changed flag
346 eqv r15,r15,r15 ; Get all foxes
347 stw r11,spcFlags(r10) ; Set per_proc copy of the special flags
348 stw r14,vmmStat(r17) ; Set the status flags sans vmmVectCngd
349 stw r15,savevrvalid(r21) ; Set the valid bits to all foxes
350
351 swvmNoNewVects:
352 li r3,1 ; Show normal exit with check for AST
353 mr r9,r26 ; Move the activation pointer
354 b EXT(ppcscret) ; Go back to handler...
355
356
357 ;
358 ; Here is where we exchange the emulator floating and vector contexts
359 ; for the virtual machines. Remember, this is not so efficient and needs
360 ; a rewrite. Also remember the funky register conventions (i.e.,
361 ; we need to know what our callers need saved and what our callees trash.
362 ;
363 ; Note: we expect R26 to contain the activation and R27 to contain the context
364 ; entry pointer.
365 ;
366
367 vmmxcng: mflr r21 ; Save the return point
368 bl EXT(fpu_save) ; Save any floating point context
369 bl EXT(vec_save) ; Save any vector point context
370
371 lis r10,hi16(EXT(per_proc_info)) ; Get top of first per_proc
372 li r8,PP_FPU_THREAD ; Index to FPU owner
373 ori r10,r10,lo16(EXT(per_proc_info)) ; Get bottom of first per_proc
374 lis r6,hi16(EXT(real_ncpus)) ; Get number of CPUs
375 li r7,0 ; Get set to clear
376 ori r6,r6,lo16(EXT(real_ncpus)) ; Get number of CPUs
377 li r9,PP_VMX_THREAD ; Index to vector owner
378 lwz r6,0(r6) ; Get the actual CPU count
379
380 vmmrt1: lwarx r3,r8,r10 ; Get FPU owner
381 cmplw r3,r26 ; Do we own it?
382 bne vmmrt2 ; Nope...
383 stwcx. r7,r8,r10 ; Clear it
384 bne- vmmrt1 ; Someone else diddled, try again....
385
386 vmmrt2: lwarx r3,r9,r10 ; Get vector owner
387 cmplw r3,r26 ; Do we own it?
388 bne vmmxnvec ; Nope...
389 stwcx. r7,r9,r10 ; Clear it
390 bne- vmmrt2 ; Someone else diddled, try again....
391
392 vmmxnvec: addic. r6,r6,-1 ; Done with all CPUs?
393 addi r10,r10,ppSize ; On to the next
394 bgt vmmrt1 ; Do all processors...
395
396 ;
397 ; At this point, the FP and Vector states for the current activation
398 ; are saved and not live on any processor. Also, they should be the
399 ; only contexts on the activation. Note that because we are currently
400 ; taking the cowardly way out and insuring that no contexts are live,
401 ; we do not need to worry about the CPU fields.
402 ;
403
404 lwz r8,ACT_MACT_FPU(r26) ; Get the FPU savearea
405 lwz r9,ACT_MACT_VMX(r26) ; Get the vector savearea
406 lwz r10,vmmFPU_pcb(r27) ; Get the FPU savearea
407 lwz r11,vmmVMX_pcb(r27) ; Get the vector savearea
408 li r7,0 ; Clear this
409 mtlr r21 ; Restore the return
410 stw r10,ACT_MACT_FPU(r26) ; Set the FPU savearea
411 stw r11,ACT_MACT_VMX(r26) ; Set the vector savearea
412 stw r8,vmmFPU_pcb(r27) ; Set the FPU savearea
413 stw r9,vmmVMX_pcb(r27) ; Set the vector savearea
414 stw r7,ACT_MACT_FPUlvl(r26) ; Make sure the level is clear
415 stw r7,ACT_MACT_VMXlvl(r26) ; Make sure the level is clear
416
417 mr. r8,r8 ; Do we have any old floating point context?
418 lwz r7,savexfpscrpad(r30) ; Get first part of latest fpscr
419 lwz r9,savexfpscr(r30) ; Get second part of the latest fpscr
420 beq- xcngnold ; Nope...
421 stw r7,savefpscrpad(r8) ; Set first part of fpscr
422 stw r9,savefpscr(r8) ; Set fpscr
423
424 xcngnold: mr. r10,r10 ; Any new context?
425 li r7,0 ; Assume no FP
426 li r9,0 ; Assume no FP
427 beq- xcngnnew ; Nope...
428 lwz r7,savefpscrpad(r10) ; Get first part of latest fpscr
429 lwz r9,savefpscr(r10) ; Get second part of the latest fpscr
430
431 xcngnnew: stw r7,savexfpscrpad(r30) ; Set the fpsc
432 stw r9,savexfpscr(r30) ; Set the fpscr
433 blr ; Return...
434
435 ;
436 ; Here is where we exit from vmm mode. We do this on any kind of exception.
437 ; Interruptions (decrementer, external, etc.) are another story though.
438 ; These we just pass through. We also switch back explicity when requested.
439 ; This will happen in response to a timer pop and some kinds of ASTs.
440 ;
441 ; Inputs:
442 ; R3 = activation
443 ; R4 = savearea
444 ;
445
446 .align 5
447 .globl EXT(vmm_exit)
448
449 LEXT(vmm_exit)
450
451 lwz r2,vmmCEntry(r3) ; Get the context that is active
452 lwz r12,ACT_VMMAP(r3) ; Get the VM_MAP for this guy
453 lwz r11,ACT_MACT_SPF(r3) ; Get the special flags
454 lwz r19,vmmFlags(r2) ; Get the status flags
455 mr r16,r3 ; R16 is safe to use for the activation address
456
457 rlwimi r19,r11,floatCngbit-vmmFloatCngdb,vmmFloatCngdb,vmmVectCngdb ; Shift and insert changed bits
458 li r0,0 ; Get a zero
459 rlwimi r11,r19,vmmSpfSaveb,floatCngbit,vectorCngbit ; Restore the saved part of the spf
460 lwz r3,VMMAP_PMAP(r12) ; Get the pmap for the activation
461 rlwinm r11,r11,0,runningVMbit+1,runningVMbit-1 ; Clear the "in VM" flag
462 stw r0,vmmCEntry(r16) ; Clear pointer to active context
463 stw r19,vmmFlags(r2) ; Set the status flags
464 mfsprg r10,0 ; Get the per_proc block
465 stw r11,ACT_MACT_SPF(r16) ; Get the special flags
466 stw r11,spcFlags(r10) ; Set per_proc copy of the special flags
467
468 mr r26,r16 ; Save the activation pointer
469 mr r27,r2 ; Save the context entry
470
471 bl EXT(hw_set_user_space_dis) ; Swap the address spaces back to the emulator
472
473 bl vmmxcng ; Exchange the vector and floating point contexts
474
475 mr r2,r27 ; Restore
476 lwz r5,vmmContextKern(r2) ; Get the context area address
477 mr r3,r16 ; Restore activation address
478 stw r19,vmmStat(r5) ; Save the changed and popped flags
479 bl swapCtxt ; Exchange the VM context for the emulator one
480 stw r8,saver3(r30) ; Set the return code as the return value also
481 b EXT(retFromVM) ; Go back to handler...
482
483
484 ;
485 ; Here is where we force exit from vmm mode. We do this when as
486 ; part of termination and is used to insure that we are not executing
487 ; in an alternate context. Because this is called from C we need to save
488 ; all non-volatile registers.
489 ;
490 ; Inputs:
491 ; R3 = activation
492 ; R4 = user savearea
493 ; Interruptions disabled
494 ;
495
496 .align 5
497 .globl EXT(vmm_force_exit)
498
499 LEXT(vmm_force_exit)
500
501 stwu r1,-(FM_ALIGN(20*4)+FM_SIZE)(r1) ; Get enough space for the registers
502 mflr r0 ; Save the return
503 stmw r13,FM_ARG0(r1) ; Save all non-volatile registers
504 stw r0,(FM_ALIGN(20*4)+FM_SIZE+FM_LR_SAVE)(r1) ; Save the return
505
506 lwz r2,vmmCEntry(r3) ; Get the context that is active
507 lwz r11,ACT_MACT_SPF(r3) ; Get the special flags
508 lwz r19,vmmFlags(r2) ; Get the status flags
509 lwz r12,ACT_VMMAP(r3) ; Get the VM_MAP for this guy
510
511 rlwimi r19,r11,floatCngbit-vmmFloatCngdb,vmmFloatCngdb,vmmVectCngdb ; Shift and insert changed bits
512 mr r26,r3 ; Save the activation pointer
513 rlwimi r11,r19,vmmSpfSaveb,floatCngbit,vectorCngbit ; Restore the saved part of the spf
514 li r0,0 ; Get a zero
515 rlwinm r9,r11,0,runningVMbit+1,runningVMbit-1 ; Clear the "in VM" flag
516 cmplw r9,r11 ; Check if we were in a vm
517 lwz r3,VMMAP_PMAP(r12) ; Get the pmap for the activation
518 beq- vfeNotRun ; We were not in a vm....
519 stw r0,vmmCEntry(r26) ; Clear pointer to active context
520 mfsprg r10,0 ; Get the per_proc block
521 stw r9,ACT_MACT_SPF(r26) ; Get the special flags
522 stw r9,spcFlags(r10) ; Set per_proc copy of the special flags
523
524 mr r27,r2 ; Save the context entry
525 mr r30,r4 ; Save the savearea
526
527 bl EXT(hw_set_user_space_dis) ; Swap the address spaces back to the emulator
528
529 bl vmmxcng ; Exchange the vector and floating point contexts
530
531 lwz r5,vmmContextKern(r27) ; Get the context area address
532 stw r19,vmmStat(r5) ; Save the changed and popped flags
533 bl swapCtxt ; Exchange the VM context for the emulator one
534
535 li r8,kVmmReturnNull ; Set a null return when we force an intercept
536 stw r8,saver3(r30) ; Set the return code as the return value also
537
538
539 vfeNotRun: lmw r13,FM_ARG0(r1) ; Restore all non-volatile registers
540 lwz r1,0(r1) ; Pop the stack
541 lwz r0,FM_LR_SAVE(r1) ; Get the return address
542 mtlr r0 ; Set return
543 blr
544
545 ;
546 ; Note: we will not do any DCBTs to the savearea. It was just stored to a few cycles ago and should
547 ; still be in the cache. Note also that the context area registers map identically to the savearea.
548 ;
549 ; NOTE: we do not save any of the non-volatile registers through this swap code
550 ; NOTE NOTE: R16 is important to save!!!!
551 ; NOTE: I am too dumb to figure out a faster way to swap 5 lines of memory. So I go for
552 ; the simple way
553
554 .align 5
555
556 swapCtxt: addi r6,r5,vmm_proc_state ; Point to the state
557 li r25,32 ; Get a cache size increment
558 addi r4,r30,savesrr0 ; Point to the start of the savearea
559 dcbt 0,r6 ; Touch in the first line of the context area
560
561 lwz r14,saveexception(r30) ; Get the exception code
562 lwz r7,savesrr0(r4) ; First line of savearea
563 lwz r8,savesrr1(r4)
564 lwz r9,saver0(r4)
565 cmplwi cr1,r14,T_SYSTEM_CALL ; Are we switching because of a system call?
566 lwz r10,saver1(r4)
567 lwz r11,saver2(r4)
568 lwz r12,saver3(r4)
569 lwz r13,saver4(r4)
570 lwz r14,saver5(r4)
571
572 dcbt r25,r6 ; Touch second line of context area
573 addi r25,r25,32 ; Bump
574
575 lwz r15,savesrr0(r6) ; First line of context
576 lis r22,hi16(MSR_IMPORT_BITS) ; Get the MSR bits that are controllable by user
577 lwz r23,savesrr1(r6)
578 ori r22,r25,lo16(MSR_IMPORT_BITS) ; Get the rest of the MSR bits that are controllable by user
579 lwz r17,saver0(r6)
580 lwz r18,saver1(r6)
581 and r23,r23,r22 ; Keep only the controllable bits
582 lwz r19,saver2(r6)
583 oris r23,r23,hi16(MSR_EXPORT_MASK_SET) ; Force on the required bits
584 lwz r20,saver3(r6)
585 ori r23,r23,lo16(MSR_EXPORT_MASK_SET) ; Force on the other required bits
586 lwz r21,saver4(r6)
587 lwz r22,saver5(r6)
588
589 dcbt r25,r6 ; Touch third line of context area
590 addi r25,r25,32 ; Bump (r25 is 64 now)
591
592 stw r7,savesrr0(r6) ; Save emulator context into the context area
593 stw r8,savesrr1(r6)
594 stw r9,saver0(r6)
595 stw r10,saver1(r6)
596 stw r11,saver2(r6)
597 stw r12,saver3(r6)
598 stw r13,saver4(r6)
599 stw r14,saver5(r6)
600
601 ;
602 ; Save the first 3 parameters if we are an SC (we will take care of the last later)
603 ;
604 bne+ cr1,swapnotsc ; Skip next if not an SC exception...
605 stw r12,return_params+0(r5) ; Save the first return
606 stw r13,return_params+4(r5) ; Save the second return
607 stw r14,return_params+8(r5) ; Save the third return
608
609 swapnotsc: stw r15,savesrr0(r4) ; Save vm context into the savearea
610 stw r23,savesrr1(r4)
611 stw r17,saver0(r4)
612 stw r18,saver1(r4)
613 stw r19,saver2(r4)
614 stw r20,saver3(r4)
615 stw r21,saver4(r4)
616 stw r22,saver5(r4)
617
618 ;
619 ; The first hunk is swapped, do the rest in a loop
620 ;
621 li r23,4 ; Four more hunks to swap
622
623
624 swaploop: addi r4,r4,32 ; Bump savearea pointer
625 addi r6,r6,32 ; Bump context area pointer
626 addic. r23,r23,-1 ; Count down
627 dcbt r25,r6 ; Touch 4th, 5th, and 6th and 7th which are extra
628
629 lwz r7,0(r4) ; Read savearea
630 lwz r8,4(r4)
631 lwz r9,8(r4)
632 lwz r10,12(r4)
633 lwz r11,16(r4)
634 lwz r12,20(r4)
635 lwz r13,24(r4)
636 lwz r14,28(r4)
637
638 lwz r15,0(r6) ; Read vm context
639 lwz r24,4(r6)
640 lwz r17,8(r6)
641 lwz r18,12(r6)
642 lwz r19,16(r6)
643 lwz r20,20(r6)
644 lwz r21,24(r6)
645 lwz r22,28(r6)
646
647 stw r7,0(r6) ; Write context
648 stw r8,4(r6)
649 stw r9,8(r6)
650 stw r10,12(r6)
651 stw r11,16(r6)
652 stw r12,20(r6)
653 stw r13,24(r6)
654 stw r14,28(r6)
655
656 stw r15,0(r4) ; Write vm context
657 stw r24,4(r4)
658 stw r17,8(r4)
659 stw r18,12(r4)
660 stw r19,16(r4)
661 stw r20,20(r4)
662 stw r21,24(r4)
663 stw r22,28(r4)
664
665 bgt+ swaploop ; Do it all...
666
667 ;
668 ; Cobble up the exception return code and save any specific return values
669 ;
670
671 lwz r7,saveexception(r30) ; Pick up the exception code
672 rlwinm r8,r7,30,24,31 ; Convert exception to return code
673 cmplwi r7,T_DATA_ACCESS ; Was this a DSI?
674 stw r8,return_code(r5) ; Save the exit code
675 cmplwi cr1,r7,T_INSTRUCTION_ACCESS ; Exiting because of an ISI?
676 beq+ swapDSI ; Yeah...
677 cmplwi r7,T_ALIGNMENT ; Alignment exception?
678 beq+ cr1,swapISI ; We had an ISI...
679 cmplwi cr1,r7,T_SYSTEM_CALL ; Exiting because of an system call?
680 beq+ swapDSI ; An alignment exception looks like a DSI...
681 beq+ cr1,swapSC ; We had a system call...
682
683 blr ; Return...
684
685 ;
686 ; Set exit returns for a DSI or alignment exception
687 ;
688
689 swapDSI: lwz r10,savedar(r30) ; Get the DAR
690 lwz r7,savedsisr(r30) ; and the DSISR
691 stw r10,return_params+0(r5) ; Save DAR as first return parm
692 stw r7,return_params+4(r5) ; Save DSISR as second return parm
693 blr ; Return...
694
695 ;
696 ; Set exit returns for a ISI
697 ;
698
699 swapISI: lwz r7,savesrr1+vmm_proc_state(r5) ; Get the SRR1 value
700 lwz r10,savesrr0+vmm_proc_state(r5) ; Get the PC as failing address
701 rlwinm r7,r7,0,1,4 ; Save the bits that match the DSISR
702 stw r10,return_params+0(r5) ; Save PC as first return parm
703 stw r7,return_params+4(r5) ; Save the pseudo-DSISR as second return parm
704 blr ; Return...
705
706 ;
707 ; Set exit returns for a system call (note: we did the first 3 earlier)
708 ; Do we really need to pass parameters back here????
709 ;
710
711 swapSC: lwz r10,saver6+vmm_proc_state(r5) ; Get the fourth paramter
712 stw r10,return_params+12(r5) ; Save it
713 blr ; Return...
714