locore.s revision 1.33 1 /* $NetBSD: locore.s,v 1.33 1999/03/24 05:51:15 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1980, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah $Hdr: locore.s 1.66 92/12/22$
41 * @(#)locore.s 8.6 (Berkeley) 5/27/94
42 */
43
44 #include "opt_compat_netbsd.h"
45
46 #include "assym.h"
47 #include <machine/asm.h>
48 #include <machine/trap.h>
49
50 | Remember this is a fun project!
51
52 .data
53 GLOBAL(mon_crp)
54 .long 0,0
55
56 | This is for kvm_mkdb, and should be the address of the beginning
57 | of the kernel text segment (not necessarily the same as kernbase).
58 .text
59 GLOBAL(kernel_text)
60
61 | This is the entry point, as well as the end of the temporary stack
62 | used during process switch (one 8K page ending at start)
63 ASGLOBAL(tmpstk)
64 ASGLOBAL(start)
65
66 | The first step, after disabling interrupts, is to map enough of the kernel
67 | into high virtual address space so that we can use position dependent code.
68 | This is a tricky task on the sun3x because the MMU is already enabled and
69 | the ROM monitor provides no indication of where the root MMU table is mapped.
70 | Therefore we must use one of the 68030's 'transparent translation' registers
71 | to define a range in the address space where the MMU translation is
72 | turned off. Once this is complete we can modify the MMU table directly
73 | without the need for it to be mapped into virtual memory.
74 | All code must be position independent until otherwise noted, as the
75 | boot loader has loaded us into low memory but all the symbols in this
76 | code have been linked high.
77 movw #PSL_HIGHIPL, sr | no interrupts
78 movl #KERNBASE, a5 | for vtop conversion
79 lea _C_LABEL(mon_crp), a0 | where to store the CRP
80 subl a5, a0
81 | Note: borrowing mon_crp for tt0 setup...
82 movl #0x3F8107, a0@ | map the low 1GB v=p with the
83 .long 0xf0100800 | transparent translation reg0
84 | [ pmove a0@, tt0 ]
85 | In order to map the kernel into high memory we will copy the root table
86 | entry which maps the 16 megabytes of memory starting at 0x0 into the
87 | entry which maps the 16 megabytes starting at KERNBASE.
88 pmove crp, a0@ | Get monitor CPU root pointer
89 movl a0@(4), a1 | 2nd word is PA of level A table
90
91 movl a1, a0 | compute the descriptor address
92 addl #0x3e0, a1 | for VA starting at KERNBASE
93 movl a0@, a1@ | copy descriptor type
94 movl a0@(4), a1@(4) | copy physical address
95
96 | Kernel is now double mapped at zero and KERNBASE.
97 | Force a long jump to the relocated code (high VA).
98 movl #IC_CLEAR, d0 | Flush the I-cache
99 movc d0, cacr
100 jmp L_high_code:l | long jump
101
102 L_high_code:
103 | We are now running in the correctly relocated kernel, so
104 | we are no longer restricted to position-independent code.
105 | It is handy to leave transparent translation enabled while
106 | for the low 1GB while _bootstrap() is doing its thing.
107
108 | Do bootstrap stuff needed before main() gets called.
109 | Our boot loader leaves a copy of the kernel's exec header
110 | just before the start of the kernel text segment, so the
111 | kernel can sanity-check the DDB symbols at [end...esym].
112 | Pass the struct exec at tmpstk-32 to _bootstrap().
113 | Also, make sure the initial frame pointer is zero so that
114 | the backtrace algorithm used by KGDB terminates nicely.
115 lea _ASM_LABEL(tmpstk)-32, sp
116 movl #0,a6
117 jsr _C_LABEL(_bootstrap) | See locore2.c
118
119 | Now turn off the transparent translation of the low 1GB.
120 | (this also flushes the ATC)
121 clrl sp@-
122 .long 0xf0170800 | pmove sp@,tt0
123 addql #4,sp
124
125 | Now that _bootstrap() is done using the PROM functions,
126 | we can safely set the sfc/dfc to something != FC_CONTROL
127 moveq #FC_USERD, d0 | make movs access "user data"
128 movc d0, sfc | space for copyin/copyout
129 movc d0, dfc
130
131 | Setup process zero user/kernel stacks.
132 movl _C_LABEL(proc0paddr),a1 | get proc0 pcb addr
133 lea a1@(USPACE-4),sp | set SSP to last word
134 movl #USRSTACK-4,a2
135 movl a2,usp | init user SP
136
137 | Note curpcb was already set in _bootstrap().
138 | Will do fpu initialization during autoconfig (see fpu.c)
139 | The interrupt vector table and stack are now ready.
140 | Interrupts will be enabled later, AFTER autoconfiguration
141 | is finished, to avoid spurrious interrupts.
142
143 /*
144 * Final preparation for calling main.
145 *
146 * Create a fake exception frame that returns to user mode,
147 * and save its address in p->p_md.md_regs for cpu_fork().
148 * The new frames for process 1 and 2 will be adjusted by
149 * cpu_set_kpc() to arrange for a call to a kernel function
150 * before the new process does its rte out to user mode.
151 */
152 clrw sp@- | tf_format,tf_vector
153 clrl sp@- | tf_pc (filled in later)
154 movw #PSL_USER,sp@- | tf_sr for user mode
155 clrl sp@- | tf_stackadj
156 lea sp@(-64),sp | tf_regs[16]
157 movl sp,a1 | a1=trapframe
158 lea _C_LABEL(proc0),a0 | proc0.p_md.md_regs =
159 movl a1,a0@(P_MDREGS) | trapframe
160 movl a2,a1@(FR_SP) | a2 == usp (from above)
161 pea a1@ | push &trapframe
162 jbsr _C_LABEL(main) | main(&trapframe)
163 addql #4,sp | help DDB backtrace
164 trap #15 | should not get here
165
166 | This is used by cpu_fork() to return to user mode.
167 | It is called with SP pointing to a struct trapframe.
168 GLOBAL(proc_do_uret)
169 movl sp@(FR_SP),a0 | grab and load
170 movl a0,usp | user SP
171 moveml sp@+,#0x7FFF | load most registers (all but SSP)
172 addql #8,sp | pop SSP and stack adjust count
173 rte
174
175 /*
176 * proc_trampoline:
177 * This is used by cpu_set_kpc() to "push" a function call onto the
178 * kernel stack of some process, very much like a signal delivery.
179 * When we get here, the stack has:
180 *
181 * SP+8: switchframe from before cpu_set_kpc
182 * SP+4: void *arg;
183 * SP: u_long func;
184 *
185 * On entry, the switchframe pushed by cpu_set_kpc has already been
186 * popped off the stack, so all this needs to do is pop the function
187 * pointer into a register, call it, then pop the arg, and finally
188 * return using the switchframe that remains on the stack.
189 */
190 GLOBAL(proc_trampoline)
191 movl sp@+,a0 | function pointer
192 jbsr a0@ | (*func)(arg)
193 addql #4,sp | toss the arg
194 rts | as cpu_switch would do
195
196 | That is all the assembly startup code we need on the sun3x!
197 | The rest of this is like the hp300/locore.s where possible.
198
199 /*
200 * Trap/interrupt vector routines
201 */
202 #include <m68k/m68k/trap_subr.s>
203
204 GLOBAL(buserr)
205 tstl _C_LABEL(nofault) | device probe?
206 jeq _C_LABEL(addrerr) | no, handle as usual
207 movl _C_LABEL(nofault),sp@- | yes,
208 jbsr _C_LABEL(longjmp) | longjmp(nofault)
209 GLOBAL(addrerr)
210 clrl sp@- | stack adjust count
211 moveml #0xFFFF,sp@- | save user registers
212 movl usp,a0 | save the user SP
213 movl a0,sp@(FR_SP) | in the savearea
214 lea sp@(FR_HW),a1 | grab base of HW berr frame
215 moveq #0,d0
216 movw a1@(10),d0 | grab SSW for fault processing
217 btst #12,d0 | RB set?
218 jeq LbeX0 | no, test RC
219 bset #14,d0 | yes, must set FB
220 movw d0,a1@(10) | for hardware too
221 LbeX0:
222 btst #13,d0 | RC set?
223 jeq LbeX1 | no, skip
224 bset #15,d0 | yes, must set FC
225 movw d0,a1@(10) | for hardware too
226 LbeX1:
227 btst #8,d0 | data fault?
228 jeq Lbe0 | no, check for hard cases
229 movl a1@(16),d1 | fault address is as given in frame
230 jra Lbe10 | thats it
231 Lbe0:
232 btst #4,a1@(6) | long (type B) stack frame?
233 jne Lbe4 | yes, go handle
234 movl a1@(2),d1 | no, can use save PC
235 btst #14,d0 | FB set?
236 jeq Lbe3 | no, try FC
237 addql #4,d1 | yes, adjust address
238 jra Lbe10 | done
239 Lbe3:
240 btst #15,d0 | FC set?
241 jeq Lbe10 | no, done
242 addql #2,d1 | yes, adjust address
243 jra Lbe10 | done
244 Lbe4:
245 movl a1@(36),d1 | long format, use stage B address
246 btst #15,d0 | FC set?
247 jeq Lbe10 | no, all done
248 subql #2,d1 | yes, adjust address
249 Lbe10:
250 movl d1,sp@- | push fault VA
251 movl d0,sp@- | and padded SSW
252 movw a1@(6),d0 | get frame format/vector offset
253 andw #0x0FFF,d0 | clear out frame format
254 cmpw #12,d0 | address error vector?
255 jeq Lisaerr | yes, go to it
256
257 /* MMU-specific code to determine reason for bus error. */
258 movl d1,a0 | fault address
259 movl sp@,d0 | function code from ssw
260 btst #8,d0 | data fault?
261 jne Lbe10a
262 movql #1,d0 | user program access FC
263 | (we dont separate data/program)
264 btst #5,a1@ | supervisor mode?
265 jeq Lbe10a | if no, done
266 movql #5,d0 | else supervisor program access
267 Lbe10a:
268 ptestr d0,a0@,#7 | do a table search
269 pmove psr,sp@ | save result
270 movb sp@,d1
271 btst #2,d1 | invalid? (incl. limit viol and berr)
272 jeq Lmightnotbemerr | no -> wp check
273 btst #7,d1 | is it MMU table berr?
274 jeq Lismerr | no, must be fast
275 jra Lisberr1 | real bus err needs not be fast
276 Lmightnotbemerr:
277 btst #3,d1 | write protect bit set?
278 jeq Lisberr1 | no, must be bus error
279 movl sp@,d0 | ssw into low word of d0
280 andw #0xc0,d0 | write protect is set on page:
281 cmpw #0x40,d0 | was it read cycle?
282 jeq Lisberr1 | yes, was not WPE, must be bus err
283 /* End of MMU-specific bus error code. */
284
285 Lismerr:
286 movl #T_MMUFLT,sp@- | show that we are an MMU fault
287 jra _ASM_LABEL(faultstkadj) | and deal with it
288 Lisaerr:
289 movl #T_ADDRERR,sp@- | mark address error
290 jra _ASM_LABEL(faultstkadj) | and deal with it
291 Lisberr1:
292 clrw sp@ | re-clear pad word
293 Lisberr:
294 movl #T_BUSERR,sp@- | mark bus error
295 jra _ASM_LABEL(faultstkadj) | and deal with it
296
297 /*
298 * FP exceptions.
299 */
300 GLOBAL(fpfline)
301 clrl sp@- | stack adjust count
302 moveml #0xFFFF,sp@- | save registers
303 moveq #T_FPEMULI,d0 | denote as FP emulation trap
304 jra _ASM_LABEL(fault) | do it
305
306 GLOBAL(fpunsupp)
307 clrl sp@- | stack adjust count
308 moveml #0xFFFF,sp@- | save registers
309 moveq #T_FPEMULD,d0 | denote as FP emulation trap
310 jra _ASM_LABEL(fault) | do it
311
312 /*
313 * Handles all other FP coprocessor exceptions.
314 * Note that since some FP exceptions generate mid-instruction frames
315 * and may cause signal delivery, we need to test for stack adjustment
316 * after the trap call.
317 */
318 GLOBAL(fpfault)
319 clrl sp@- | stack adjust count
320 moveml #0xFFFF,sp@- | save user registers
321 movl usp,a0 | and save
322 movl a0,sp@(FR_SP) | the user stack pointer
323 clrl sp@- | no VA arg
324 movl _C_LABEL(curpcb),a0 | current pcb
325 lea a0@(PCB_FPCTX),a0 | address of FP savearea
326 fsave a0@ | save state
327 tstb a0@ | null state frame?
328 jeq Lfptnull | yes, safe
329 clrw d0 | no, need to tweak BIU
330 movb a0@(1),d0 | get frame size
331 bset #3,a0@(0,d0:w) | set exc_pend bit of BIU
332 Lfptnull:
333 fmovem fpsr,sp@- | push fpsr as code argument
334 frestore a0@ | restore state
335 movl #T_FPERR,sp@- | push type arg
336 jra _ASM_LABEL(faultstkadj) | call trap and deal with stack cleanup
337
338 /*
339 * Other exceptions only cause four and six word stack frame and require
340 * no post-trap stack adjustment.
341 */
342 GLOBAL(badtrap)
343 clrl sp@- | stack adjust count
344 moveml #0xFFFF,sp@- | save std frame regs
345 jbsr _C_LABEL(straytrap) | report
346 moveml sp@+,#0xFFFF | restore regs
347 addql #4, sp | stack adjust count
348 jra _ASM_LABEL(rei) | all done
349
350 /*
351 * Trap 0 is for system calls
352 */
353 GLOBAL(trap0)
354 clrl sp@- | stack adjust count
355 moveml #0xFFFF,sp@- | save user registers
356 movl usp,a0 | save the user SP
357 movl a0,sp@(FR_SP) | in the savearea
358 movl d0,sp@- | push syscall number
359 jbsr _C_LABEL(syscall) | handle it
360 addql #4,sp | pop syscall arg
361 movl sp@(FR_SP),a0 | grab and restore
362 movl a0,usp | user SP
363 moveml sp@+,#0x7FFF | restore most registers
364 addql #8,sp | pop SP and stack adjust
365 jra _ASM_LABEL(rei) | all done
366
367 /*
368 * Trap 12 is the entry point for the cachectl "syscall"
369 * cachectl(command, addr, length)
370 * command in d0, addr in a1, length in d1
371 */
372 GLOBAL(trap12)
373 movl _C_LABEL(curproc),sp@- | push curproc pointer
374 movl d1,sp@- | push length
375 movl a1,sp@- | push addr
376 movl d0,sp@- | push command
377 jbsr _C_LABEL(cachectl1) | do it
378 lea sp@(16),sp | pop args
379 jra _ASM_LABEL(rei) | all done
380
381 /*
382 * Trace (single-step) trap. Kernel-mode is special.
383 * User mode traps are simply passed on to trap().
384 */
385 GLOBAL(trace)
386 clrl sp@- | stack adjust count
387 moveml #0xFFFF,sp@-
388 moveq #T_TRACE,d0
389 btst #5,sp@(FR_HW) | was supervisor mode?
390 jne _ASM_LABEL(kbrkpt) | yes, kernel brkpt
391 jra _ASM_LABEL(fault) | no, user-mode fault
392
393 /*
394 * Trap 15 is used for:
395 * - GDB breakpoints (in user programs)
396 * - KGDB breakpoints (in the kernel)
397 * - trace traps for SUN binaries (not fully supported yet)
398 * User mode traps are simply passed to trap().
399 */
400 GLOBAL(trap15)
401 clrl sp@- | stack adjust count
402 moveml #0xFFFF,sp@-
403 moveq #T_TRAP15,d0
404 btst #5,sp@(FR_HW) | was supervisor mode?
405 jne _ASM_LABEL(kbrkpt) | yes, kernel brkpt
406 jra _ASM_LABEL(fault) | no, user-mode fault
407
408 ASLOCAL(kbrkpt)
409 | Kernel-mode breakpoint or trace trap. (d0=trap_type)
410 | Save the system sp rather than the user sp.
411 movw #PSL_HIGHIPL,sr | lock out interrupts
412 lea sp@(FR_SIZE),a6 | Save stack pointer
413 movl a6,sp@(FR_SP) | from before trap
414
415 | If we are not on tmpstk switch to it.
416 | (so debugger can change the stack pointer)
417 movl a6,d1
418 cmpl #_ASM_LABEL(tmpstk),d1
419 jls Lbrkpt2 | already on tmpstk
420 | Copy frame to the temporary stack
421 movl sp,a0 | a0=src
422 lea _ASM_LABEL(tmpstk)-96,a1 | a1=dst
423 movl a1,sp | sp=new frame
424 moveq #FR_SIZE,d1
425 Lbrkpt1:
426 movl a0@+,a1@+
427 subql #4,d1
428 bgt Lbrkpt1
429
430 Lbrkpt2:
431 | Call the trap handler for the kernel debugger.
432 | Do not call trap() to handle it, so that we can
433 | set breakpoints in trap() if we want. We know
434 | the trap type is either T_TRACE or T_BREAKPOINT.
435 movl d0,sp@- | push trap type
436 jbsr _C_LABEL(trap_kdebug)
437 addql #4,sp | pop args
438
439 | The stack pointer may have been modified, or
440 | data below it modified (by kgdb push call),
441 | so push the hardware frame at the current sp
442 | before restoring registers and returning.
443 movl sp@(FR_SP),a0 | modified sp
444 lea sp@(FR_SIZE),a1 | end of our frame
445 movl a1@-,a0@- | copy 2 longs with
446 movl a1@-,a0@- | ... predecrement
447 movl a0,sp@(FR_SP) | sp = h/w frame
448 moveml sp@+,#0x7FFF | restore all but sp
449 movl sp@,sp | ... and sp
450 rte | all done
451
452 /* Use common m68k sigreturn */
453 #include <m68k/m68k/sigreturn.s>
454
455 /*
456 * Interrupt handlers. Most are auto-vectored,
457 * and hard-wired the same way on all sun3 models.
458 * Format in the stack is:
459 * d0,d1,a0,a1, sr, pc, vo
460 */
461
462 #define INTERRUPT_SAVEREG \
463 moveml #0xC0C0,sp@-
464
465 #define INTERRUPT_RESTORE \
466 moveml sp@+,#0x0303
467
468 /*
469 * This is the common auto-vector interrupt handler,
470 * for which the CPU provides the vector=0x18+level.
471 * These are installed in the interrupt vector table.
472 */
473 .align 2
474 GLOBAL(_isr_autovec)
475 INTERRUPT_SAVEREG
476 jbsr _C_LABEL(isr_autovec)
477 INTERRUPT_RESTORE
478 jra _ASM_LABEL(rei)
479
480 /* clock: see clock.c */
481 .align 2
482 GLOBAL(_isr_clock)
483 INTERRUPT_SAVEREG
484 jbsr _C_LABEL(clock_intr)
485 INTERRUPT_RESTORE
486 jra _ASM_LABEL(rei)
487
488 | Handler for all vectored interrupts (i.e. VME interrupts)
489 .align 2
490 GLOBAL(_isr_vectored)
491 INTERRUPT_SAVEREG
492 jbsr _C_LABEL(isr_vectored)
493 INTERRUPT_RESTORE
494 jra _ASM_LABEL(rei)
495
496 #undef INTERRUPT_SAVEREG
497 #undef INTERRUPT_RESTORE
498
499 /* interrupt counters (needed by vmstat) */
500 GLOBAL(intrnames)
501 .asciz "spur" | 0
502 .asciz "lev1" | 1
503 .asciz "lev2" | 2
504 .asciz "lev3" | 3
505 .asciz "lev4" | 4
506 .asciz "clock" | 5
507 .asciz "lev6" | 6
508 .asciz "nmi" | 7
509 GLOBAL(eintrnames)
510
511 .data
512 .even
513 GLOBAL(intrcnt)
514 .long 0,0,0,0,0,0,0,0,0,0
515 GLOBAL(eintrcnt)
516 .text
517
518 /*
519 * Emulation of VAX REI instruction.
520 *
521 * This code is (mostly) un-altered from the hp300 code,
522 * except that sun machines do not need a simulated SIR
523 * because they have a real software interrupt register.
524 *
525 * This code deals with checking for and servicing ASTs
526 * (profiling, scheduling) and software interrupts (network, softclock).
527 * We check for ASTs first, just like the VAX. To avoid excess overhead
528 * the T_ASTFLT handling code will also check for software interrupts so we
529 * do not have to do it here. After identifying that we need an AST we
530 * drop the IPL to allow device interrupts.
531 *
532 * This code is complicated by the fact that sendsig may have been called
533 * necessitating a stack cleanup.
534 */
535
536 ASGLOBAL(rei)
537 #ifdef DIAGNOSTIC
538 tstl _C_LABEL(panicstr) | have we paniced?
539 jne Ldorte | yes, do not make matters worse
540 #endif
541 tstl _C_LABEL(astpending) | AST pending?
542 jeq Ldorte | no, done
543 Lrei1:
544 btst #5,sp@ | yes, are we returning to user mode?
545 jne Ldorte | no, done
546 movw #PSL_LOWIPL,sr | lower SPL
547 clrl sp@- | stack adjust
548 moveml #0xFFFF,sp@- | save all registers
549 movl usp,a1 | including
550 movl a1,sp@(FR_SP) | the users SP
551 clrl sp@- | VA == none
552 clrl sp@- | code == none
553 movl #T_ASTFLT,sp@- | type == async system trap
554 jbsr _C_LABEL(trap) | go handle it
555 lea sp@(12),sp | pop value args
556 movl sp@(FR_SP),a0 | restore user SP
557 movl a0,usp | from save area
558 movw sp@(FR_ADJ),d0 | need to adjust stack?
559 jne Laststkadj | yes, go to it
560 moveml sp@+,#0x7FFF | no, restore most user regs
561 addql #8,sp | toss SP and stack adjust
562 rte | and do real RTE
563 Laststkadj:
564 lea sp@(FR_HW),a1 | pointer to HW frame
565 addql #8,a1 | source pointer
566 movl a1,a0 | source
567 addw d0,a0 | + hole size = dest pointer
568 movl a1@-,a0@- | copy
569 movl a1@-,a0@- | 8 bytes
570 movl a0,sp@(FR_SP) | new SSP
571 moveml sp@+,#0x7FFF | restore user registers
572 movl sp@,sp | and our SP
573 Ldorte:
574 rte | real return
575
576 /*
577 * Initialization is at the beginning of this file, because the
578 * kernel entry point needs to be at zero for compatibility with
579 * the Sun boot loader. This works on Sun machines because the
580 * interrupt vector table for reset is NOT at address zero.
581 * (The MMU has a "boot" bit that forces access to the PROM)
582 */
583
584 /*
585 * Use common m68k sigcode.
586 */
587 #include <m68k/m68k/sigcode.s>
588
589 .text
590
591 /*
592 * Primitives
593 */
594
595 /*
596 * Use common m68k support routines.
597 */
598 #include <m68k/m68k/support.s>
599
600 BSS(want_resched,4)
601
602 /*
603 * Use common m68k process manipulation routines.
604 */
605 #include <m68k/m68k/proc_subr.s>
606
607 | Message for Lbadsw panic
608 Lsw0:
609 .asciz "cpu_switch"
610 .even
611
612 .data
613 GLOBAL(masterpaddr) | XXX compatibility (debuggers)
614 GLOBAL(curpcb)
615 .long 0
616 ASBSS(nullpcb,SIZEOF_PCB)
617 .text
618
619 /*
620 * At exit of a process, do a cpu_switch for the last time.
621 * Switch to a safe stack and PCB, and select a new process to run. The
622 * old stack and u-area will be freed by the reaper.
623 */
624 ENTRY(switch_exit)
625 movl sp@(4),a0 | struct proc *p
626 | save state into garbage pcb
627 movl #_ASM_LABEL(nullpcb),_C_LABEL(curpcb)
628 lea _ASM_LABEL(tmpstk),sp | goto a tmp stack
629
630 /* Schedule the vmspace and stack to be freed. */
631 movl a0,sp@- | exit2(p)
632 jbsr _C_LABEL(exit2)
633
634 /* Don't pop the proc; pass it to cpu_switch(). */
635
636 jra _C_LABEL(cpu_switch)
637
638 /*
639 * When no processes are on the runq, cpu_switch() branches to idle
640 * to wait for something to come ready.
641 */
642 .data
643 GLOBAL(Idle_count)
644 .long 0
645 .text
646
647 Lidle:
648 stop #PSL_LOWIPL
649 GLOBAL(_Idle) | See clock.c
650 movw #PSL_HIGHIPL,sr
651 addql #1, _C_LABEL(Idle_count)
652 tstl _C_LABEL(whichqs)
653 jeq Lidle
654 movw #PSL_LOWIPL,sr
655 jra Lsw1
656
657 Lbadsw:
658 movl #Lsw0,sp@-
659 jbsr _C_LABEL(panic)
660 /*NOTREACHED*/
661
662 /*
663 * cpu_switch()
664 * Hacked for sun3
665 * XXX - Arg 1 is a proc pointer (curproc) but this doesn't use it.
666 * XXX - Sould we use p->p_addr instead of curpcb? -gwr
667 */
668 ENTRY(cpu_switch)
669 movl _C_LABEL(curpcb),a1 | current pcb
670 movw sr,a1@(PCB_PS) | save sr before changing ipl
671 #ifdef notyet
672 movl _C_LABEL(curproc),sp@- | remember last proc running
673 #endif
674 clrl _C_LABEL(curproc)
675
676 Lsw1:
677 /*
678 * Find the highest-priority queue that isn't empty,
679 * then take the first proc from that queue.
680 */
681 clrl d0
682 lea _C_LABEL(whichqs),a0
683 movl a0@,d1
684 Lswchk:
685 btst d0,d1
686 jne Lswfnd
687 addqb #1,d0
688 cmpb #32,d0
689 jne Lswchk
690 jra _C_LABEL(_Idle)
691 Lswfnd:
692 movw #PSL_HIGHIPL,sr | lock out interrupts
693 movl a0@,d1 | and check again...
694 bclr d0,d1
695 jeq Lsw1 | proc moved, rescan
696 movl d1,a0@ | update whichqs
697 moveq #1,d1 | double check for higher priority
698 lsll d0,d1 | process (which may have snuck in
699 subql #1,d1 | while we were finding this one)
700 andl a0@,d1
701 jeq Lswok | no one got in, continue
702 movl a0@,d1
703 bset d0,d1 | otherwise put this one back
704 movl d1,a0@
705 jra Lsw1 | and rescan
706 Lswok:
707 movl d0,d1
708 lslb #3,d1 | convert queue number to index
709 addl #_qs,d1 | locate queue (q)
710 movl d1,a1
711 cmpl a1@(P_FORW),a1 | anyone on queue?
712 jeq Lbadsw | no, panic
713 movl a1@(P_FORW),a0 | p = q->p_forw
714 movl a0@(P_FORW),a1@(P_FORW) | q->p_forw = p->p_forw
715 movl a0@(P_FORW),a1 | q = p->p_forw
716 movl a0@(P_BACK),a1@(P_BACK) | q->p_back = p->p_back
717 cmpl a0@(P_FORW),d1 | anyone left on queue?
718 jeq Lsw2 | no, skip
719 movl _C_LABEL(whichqs),d1
720 bset d0,d1 | yes, reset bit
721 movl d1,_C_LABEL(whichqs)
722 Lsw2:
723 movl a0,_C_LABEL(curproc)
724 clrl _C_LABEL(want_resched)
725 #ifdef notyet
726 movl sp@+,a1 | XXX - Make this work!
727 cmpl a0,a1 | switching to same proc?
728 jeq Lswdone | yes, skip save and restore
729 #endif
730 /*
731 * Save state of previous process in its pcb.
732 */
733 movl _C_LABEL(curpcb),a1
734 moveml #0xFCFC,a1@(PCB_REGS) | save non-scratch registers
735 movl usp,a2 | grab USP (a2 has been saved)
736 movl a2,a1@(PCB_USP) | and save it
737
738 tstl _C_LABEL(fputype) | Do we have an fpu?
739 jeq Lswnofpsave | No? Then don't try save.
740 lea a1@(PCB_FPCTX),a2 | pointer to FP save area
741 fsave a2@ | save FP state
742 tstb a2@ | null state frame?
743 jeq Lswnofpsave | yes, all done
744 fmovem fp0-fp7,a2@(FPF_REGS) | save FP general regs
745 fmovem fpcr/fpsr/fpi,a2@(FPF_FPCR) | save FP control regs
746 Lswnofpsave:
747
748 /*
749 * Now that we have saved all the registers that must be
750 * preserved, we are free to use those registers until
751 * we load the registers for the switched-to process.
752 * In this section, keep: a0=curproc, a1=curpcb
753 */
754
755 #ifdef DIAGNOSTIC
756 tstl a0@(P_WCHAN)
757 jne Lbadsw
758 cmpb #SRUN,a0@(P_STAT)
759 jne Lbadsw
760 #endif
761 clrl a0@(P_BACK) | clear back link
762 movl a0@(P_ADDR),a1 | get p_addr
763 movl a1,_C_LABEL(curpcb)
764
765 /*
766 * Load the new VM context (new MMU root pointer)
767 */
768 movl a0@(P_VMSPACE),a2 | vm = p->p_vmspace
769 #ifdef DIAGNOSTIC
770 tstl a2 | vm == VM_MAP_NULL?
771 jeq Lbadsw | panic
772 #endif
773 #ifdef PMAP_DEBUG
774 /* When debugging just call _pmap_switch(). */
775 movl a2@(VM_PMAP),a2 | pmap = vm->vm_map.pmap
776 pea a2@ | push pmap
777 jbsr _C_LABEL(_pmap_switch) | _pmap_switch(pmap)
778 addql #4,sp
779 movl _C_LABEL(curpcb),a1 | restore p_addr
780 #else
781 /* Otherwise, use this inline version. */
782 lea _C_LABEL(kernel_crp), a3 | our CPU Root Ptr. (CRP)
783 movl a2@(VM_PMAP),a2 | pmap = vm->vm_map.pmap
784 movl a2@(PM_A_PHYS),d0 | phys = pmap->pm_a_phys
785 cmpl a3@(4),d0 | == kernel_crp.rp_addr ?
786 jeq Lsame_mmuctx | skip loadcrp/flush
787 /* OK, it is a new MMU context. Load it up. */
788 movl d0,a3@(4)
789 movl #CACHE_CLR,d0
790 movc d0,cacr | invalidate cache(s)
791 pflusha | flush entire TLB
792 pmove a3@,crp | load new user root pointer
793 Lsame_mmuctx:
794 #endif
795
796 /*
797 * Reload the registers for the new process.
798 * After this point we can only use d0,d1,a0,a1
799 */
800 moveml a1@(PCB_REGS),#0xFCFC | reload registers
801 movl a1@(PCB_USP),a0
802 movl a0,usp | and USP
803
804 tstl _C_LABEL(fputype) | If we don't have an fpu,
805 jeq Lres_skip | don't try to restore it.
806 lea a1@(PCB_FPCTX),a0 | pointer to FP save area
807 tstb a0@ | null state frame?
808 jeq Lresfprest | yes, easy
809 fmovem a0@(FPF_FPCR),fpcr/fpsr/fpi | restore FP control regs
810 fmovem a0@(FPF_REGS),fp0-fp7 | restore FP general regs
811 Lresfprest:
812 frestore a0@ | restore state
813 Lres_skip:
814 movw a1@(PCB_PS),d0 | no, restore PS
815 #ifdef DIAGNOSTIC
816 btst #13,d0 | supervisor mode?
817 jeq Lbadsw | no? panic!
818 #endif
819 movw d0,sr | OK, restore PS
820 moveq #1,d0 | return 1 (for alternate returns)
821 rts
822
823 /*
824 * savectx(pcb)
825 * Update pcb, saving current processor state.
826 */
827 ENTRY(savectx)
828 movl sp@(4),a1
829 movw sr,a1@(PCB_PS)
830 movl usp,a0 | grab USP
831 movl a0,a1@(PCB_USP) | and save it
832 moveml #0xFCFC,a1@(PCB_REGS) | save non-scratch registers
833
834 tstl _C_LABEL(fputype) | Do we have FPU?
835 jeq Lsavedone | No? Then don't save state.
836 lea a1@(PCB_FPCTX),a0 | pointer to FP save area
837 fsave a0@ | save FP state
838 tstb a0@ | null state frame?
839 jeq Lsavedone | yes, all done
840 fmovem fp0-fp7,a0@(FPF_REGS) | save FP general regs
841 fmovem fpcr/fpsr/fpi,a0@(FPF_FPCR) | save FP control regs
842 Lsavedone:
843 moveq #0,d0 | return 0
844 rts
845
846 /* suline() */
847
848 #ifdef DEBUG
849 .data
850 ASGLOBAL(fulltflush)
851 .long 0
852 ASGLOBAL(fullcflush)
853 .long 0
854 .text
855 #endif
856
857 /*
858 * Invalidate entire TLB.
859 */
860 ENTRY(TBIA)
861 _C_LABEL(_TBIA):
862 pflusha
863 movl #DC_CLEAR,d0
864 movc d0,cacr | invalidate on-chip d-cache
865 rts
866
867 /*
868 * Invalidate any TLB entry for given VA (TB Invalidate Single)
869 */
870 ENTRY(TBIS)
871 #ifdef DEBUG
872 tstl _ASM_LABEL(fulltflush) | being conservative?
873 jne _C_LABEL(_TBIA) | yes, flush entire TLB
874 #endif
875 movl sp@(4),a0
876 pflush #0,#0,a0@ | flush address from both sides
877 movl #DC_CLEAR,d0
878 movc d0,cacr | invalidate on-chip data cache
879 rts
880
881 /*
882 * Invalidate supervisor side of TLB
883 */
884 ENTRY(TBIAS)
885 #ifdef DEBUG
886 tstl _ASM_LABEL(fulltflush) | being conservative?
887 jne _C_LABEL(_TBIA) | yes, flush everything
888 #endif
889 pflush #4,#4 | flush supervisor TLB entries
890 movl #DC_CLEAR,d0
891 movc d0,cacr | invalidate on-chip d-cache
892 rts
893
894 /*
895 * Invalidate user side of TLB
896 */
897 ENTRY(TBIAU)
898 #ifdef DEBUG
899 tstl _ASM_LABEL(fulltflush) | being conservative?
900 jne _C_LABEL(_TBIA) | yes, flush everything
901 #endif
902 pflush #0,#4 | flush user TLB entries
903 movl #DC_CLEAR,d0
904 movc d0,cacr | invalidate on-chip d-cache
905 rts
906
907 /*
908 * Invalidate instruction cache
909 */
910 ENTRY(ICIA)
911 movl #IC_CLEAR,d0
912 movc d0,cacr | invalidate i-cache
913 rts
914
915 /*
916 * Invalidate data cache.
917 * NOTE: we do not flush 68030 on-chip cache as there are no aliasing
918 * problems with DC_WA. The only cases we have to worry about are context
919 * switch and TLB changes, both of which are handled "in-line" in resume
920 * and TBI*.
921 */
922 ENTRY(DCIA)
923 __DCIA:
924 rts
925
926 ENTRY(DCIS)
927 __DCIS:
928 rts
929
930 /*
931 * Invalidate data cache.
932 */
933 ENTRY(DCIU)
934 movl #DC_CLEAR,d0
935 movc d0,cacr | invalidate on-chip d-cache
936 rts
937
938 /* ICPL, ICPP, DCPL, DCPP, DCPA, DCFL, DCFP */
939
940 ENTRY(PCIA)
941 movl #DC_CLEAR,d0
942 movc d0,cacr | invalidate on-chip d-cache
943 rts
944
945 ENTRY(ecacheon)
946 rts
947
948 ENTRY(ecacheoff)
949 rts
950
951 /*
952 * Get callers current SP value.
953 * Note that simply taking the address of a local variable in a C function
954 * doesn't work because callee saved registers may be outside the stack frame
955 * defined by A6 (e.g. GCC generated code).
956 *
957 * [I don't think the ENTRY() macro will do the right thing with this -- glass]
958 */
959 GLOBAL(getsp)
960 movl sp,d0 | get current SP
961 addql #4,d0 | compensate for return address
962 rts
963
964 ENTRY(getsfc)
965 movc sfc,d0
966 rts
967
968 ENTRY(getdfc)
969 movc dfc,d0
970 rts
971
972 ENTRY(getvbr)
973 movc vbr, d0
974 rts
975
976 ENTRY(setvbr)
977 movl sp@(4), d0
978 movc d0, vbr
979 rts
980
981 /*
982 * Load a new CPU Root Pointer (CRP) into the MMU.
983 * void loadcrp(struct mmu_rootptr *);
984 */
985 ENTRY(loadcrp)
986 movl sp@(4),a0 | arg1: &CRP
987 movl #CACHE_CLR,d0
988 movc d0,cacr | invalidate cache(s)
989 pflusha | flush entire TLB
990 pmove a0@,crp | load new user root pointer
991 rts
992
993 /*
994 * Get the physical address of the PTE for a given VA.
995 */
996 ENTRY(ptest_addr)
997 movl sp@(4),a0 | VA
998 ptestr #5,a0@,#7,a1 | a1 = addr of PTE
999 movl a1,d0
1000 rts
1001
1002 /*
1003 * Set processor priority level calls. Most are implemented with
1004 * inline asm expansions. However, we need one instantiation here
1005 * in case some non-optimized code makes external references.
1006 * Most places will use the inlined functions param.h supplies.
1007 */
1008
1009 ENTRY(_getsr)
1010 clrl d0
1011 movw sr,d0
1012 rts
1013
1014 ENTRY(_spl)
1015 clrl d0
1016 movw sr,d0
1017 movl sp@(4),d1
1018 movw d1,sr
1019 rts
1020
1021 ENTRY(_splraise)
1022 clrl d0
1023 movw sr,d0
1024 movl d0,d1
1025 andl #PSL_HIGHIPL,d1 | old &= PSL_HIGHIPL
1026 cmpl sp@(4),d1 | (old - new)
1027 bge Lsplr
1028 movl sp@(4),d1
1029 movw d1,sr
1030 Lsplr:
1031 rts
1032
1033 /*
1034 * Save and restore 68881 state.
1035 */
1036 ENTRY(m68881_save)
1037 movl sp@(4),a0 | save area pointer
1038 fsave a0@ | save state
1039 tstb a0@ | null state frame?
1040 jeq Lm68881sdone | yes, all done
1041 fmovem fp0-fp7,a0@(FPF_REGS) | save FP general regs
1042 fmovem fpcr/fpsr/fpi,a0@(FPF_FPCR) | save FP control regs
1043 Lm68881sdone:
1044 rts
1045
1046 ENTRY(m68881_restore)
1047 movl sp@(4),a0 | save area pointer
1048 tstb a0@ | null state frame?
1049 jeq Lm68881rdone | yes, easy
1050 fmovem a0@(FPF_FPCR),fpcr/fpsr/fpi | restore FP control regs
1051 fmovem a0@(FPF_REGS),fp0-fp7 | restore FP general regs
1052 Lm68881rdone:
1053 frestore a0@ | restore state
1054 rts
1055
1056 /*
1057 * _delay(unsigned N)
1058 * Delay for at least (N/256) microseconds.
1059 * This routine depends on the variable: delay_divisor
1060 * which should be set based on the CPU clock rate.
1061 * XXX: Currently this is set based on the CPU model,
1062 * XXX: but this should be determined at run time...
1063 */
1064 GLOBAL(_delay)
1065 | d0 = arg = (usecs << 8)
1066 movl sp@(4),d0
1067 | d1 = delay_divisor;
1068 movl _C_LABEL(delay_divisor),d1
1069 L_delay:
1070 subl d1,d0
1071 jgt L_delay
1072 rts
1073
1074
1075 | Define some addresses, mostly so DDB can print useful info.
1076 | Not using _C_LABEL() here because these symbols are never
1077 | referenced by any C code, and if the leading underscore
1078 | ever goes away, these lines turn into syntax errors...
1079 .set _KERNBASE,KERNBASE
1080 .set _MONSTART,SUN3X_MONSTART
1081 .set _PROM_BASE,SUN3X_PROM_BASE
1082 .set _MONEND,SUN3X_MONEND
1083
1084 |The end!
1085