Fix a number of interrupt related issues.
[dragonfly.git] / sys / i386 / isa / ipl.s
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1989, 1990 William F. Jolitz.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)ipl.s
38 *
39 * $FreeBSD: src/sys/i386/isa/ipl.s,v 1.32.2.3 2002/05/16 16:03:56 bde Exp $
46a3f46d 40 * $DragonFly: src/sys/i386/isa/Attic/ipl.s,v 1.14 2003/10/02 22:26:59 dillon Exp $
984263bc
MD
41 */
42
43
44/*
45 * AT/386
46 * Vector interrupt control section
47 *
984263bc
MD
48 * *_imask - Interrupt masks for various spl*() functions
49 * ipending - Pending interrupts (set when a masked interrupt occurs)
50 */
51
52 .data
53 ALIGN_DATA
54
55/* current priority (all off) */
984263bc 56
2954c92f
MD
57 .globl tty_imask
58tty_imask: .long SWI_TTY_MASK
59 .globl bio_imask
60bio_imask: .long SWI_CLOCK_MASK | SWI_CAMBIO_MASK
61 .globl net_imask
62net_imask: .long SWI_NET_MASK | SWI_CAMNET_MASK
63 .globl cam_imask
64cam_imask: .long SWI_CAMBIO_MASK | SWI_CAMNET_MASK
65 .globl soft_imask
66soft_imask: .long SWI_MASK
67 .globl softnet_imask
68softnet_imask: .long SWI_NET_MASK
69 .globl softtty_imask
70softtty_imask: .long SWI_TTY_MASK
984263bc 71
984263bc 72 .text
46a3f46d
MD
73 /*
74 * GENERAL NOTES
75 *
76 * - fast interrupts are always called with a critical section
77 * held
78 *
79 * - we release our critical section when scheduling interrupt
80 * or softinterrupt threads in order so they can preempt
81 * (unless we are called manually from a critical section, in
82 * which case there will still be a critical section and
83 * they won't preempt anyway).
84 *
85 * - TD_NEST_COUNT prevents splz from nesting too deeply within
86 * itself. It is *not* actually an interrupt nesting count.
87 * PCPU(intr_nesting_level) is an interrupt nesting count.
88 *
89 * - We have to be careful in regards to local interrupts
90 * occuring simultaniously with our doreti and splz
91 * processing.
92 */
984263bc 93
ef0fdad1
MD
94 /*
95 * DORETI
96 *
97 * Handle return from interrupts, traps and syscalls. This function
98 * checks the cpl for unmasked pending interrupts (fast, normal, or
99 * soft) and schedules them if appropriate, then irets.
71ef2f5c
MD
100 *
101 * If we are in a critical section we cannot run any pending ints
102 * nor can be play with mp_lock.
46a3f46d 103 *
ef0fdad1 104 */
984263bc 105 SUPERALIGN_TEXT
2954c92f
MD
106 .type doreti,@function
107doreti:
108 FAKE_MCOUNT(bintr) /* init "from" bintr -> doreti */
ef0fdad1 109 popl %eax /* cpl to restore */
2954c92f 110 movl PCPU(curthread),%ebx
ef0fdad1 111 cli /* interlock with TDPRI_CRIT */
235957ed
MD
112 cmpl $0,PCPU(reqflags) /* short cut if nothing to do */
113 je 5f
ef0fdad1
MD
114 movl %eax,TD_CPL(%ebx) /* save cpl being restored */
115 cmpl $TDPRI_CRIT,TD_PRI(%ebx) /* can't unpend if in critical sec */
116 jge 5f
117 addl $TDPRI_CRIT,TD_PRI(%ebx) /* force all ints to pending */
984263bc 118doreti_next:
ef0fdad1
MD
119 sti /* allow new interrupts */
120 movl %eax,%ecx /* cpl being restored */
121 notl %ecx
122 cli /* disallow YYY remove */
96728c05 123#ifdef SMP
235957ed 124 testl $RQF_IPIQ,PCPU(reqflags)
96728c05
MD
125 jnz doreti_ipiq
126#endif
a2a5ad0d
MD
127 testl PCPU(fpending),%ecx /* check for an unmasked fast int */
128 jnz doreti_fast
129
130 testl PCPU(ipending),%ecx
131 jnz doreti_intr
235957ed
MD
132
133 testl $RQF_AST_MASK,PCPU(reqflags) /* any pending ASTs? */
26a0694b 134 jz 2f
984263bc 135 testl $PSL_VM,TF_EFLAGS(%esp)
ef0fdad1 136 jz 1f
235957ed
MD
137 cmpl $1,in_vm86call /* YYY make per 'cpu'? */
138 jnz doreti_ast
ef0fdad1 1391:
984263bc 140 testb $SEL_RPL_MASK,TF_CS(%esp)
235957ed 141 jnz doreti_ast
96728c05 1422:
984263bc 143 /*
ef0fdad1 144 * Nothing left to do, finish up. Interrupts are still disabled.
5c323556
MD
145 * If our temporary cpl mask is 0 then we have processed all pending
146 * fast and normal ints including those requiring the MP lock,
147 * and we have processed as many of the reqflags as possible based
148 * on whether we came from user mode or not. So if %eax is 0 we
149 * can clear the interrupt-related reqflags.
984263bc 150 */
ef0fdad1 151 subl $TDPRI_CRIT,TD_PRI(%ebx) /* interlocked with cli */
a2a5ad0d
MD
152 testl %eax,%eax
153 jnz 5f
5c323556 154 andl $~RQF_INTPEND,PCPU(reqflags)
ef0fdad1 1555:
984263bc 156 MEXITCOUNT
984263bc 157 .globl doreti_popl_fs
ef0fdad1
MD
158 .globl doreti_popl_es
159 .globl doreti_popl_ds
160 .globl doreti_iret
984263bc
MD
161 .globl doreti_syscall_ret
162doreti_syscall_ret:
163doreti_popl_fs:
164 popl %fs
984263bc
MD
165doreti_popl_es:
166 popl %es
984263bc
MD
167doreti_popl_ds:
168 popl %ds
169 popal
170 addl $8,%esp
984263bc
MD
171doreti_iret:
172 iret
173
174 ALIGN_TEXT
175 .globl doreti_iret_fault
176doreti_iret_fault:
177 subl $8,%esp
178 pushal
179 pushl %ds
180 .globl doreti_popl_ds_fault
181doreti_popl_ds_fault:
182 pushl %es
183 .globl doreti_popl_es_fault
184doreti_popl_es_fault:
185 pushl %fs
186 .globl doreti_popl_fs_fault
187doreti_popl_fs_fault:
188 movl $0,TF_ERR(%esp) /* XXX should be the error code */
189 movl $T_PROTFLT,TF_TRAPNO(%esp)
190 jmp alltraps_with_regs_pushed
191
984263bc 192 /*
ef0fdad1 193 * FAST interrupt pending
984263bc 194 */
ef0fdad1
MD
195 ALIGN_TEXT
196doreti_fast:
2954c92f 197 andl PCPU(fpending),%ecx /* only check fast ints */
984263bc 198 bsfl %ecx, %ecx /* locate the next dispatchable int */
2954c92f 199 btrl %ecx, PCPU(fpending) /* is it really still pending? */
ef0fdad1 200 jnc doreti_next
96728c05
MD
201 pushl %eax /* YYY cpl (expected by frame) */
202#ifdef SMP
203 pushl %ecx /* save ecx */
204 call try_mplock
205 popl %ecx
206 testl %eax,%eax
207 jz 1f
03aa8d99 208 /* MP lock successful */
96728c05 209#endif
03aa8d99
MD
210 incl PCPU(intr_nesting_level)
211 call *fastunpend(,%ecx,4)
212 decl PCPU(intr_nesting_level)
96728c05
MD
213#ifdef SMP
214 call rel_mplock
215#endif
216 popl %eax
217 jmp doreti_next
2181:
219 btsl %ecx, PCPU(fpending) /* oops, couldn't get the MP lock */
3c23a41a 220 popl %eax /* add to temp. cpl mask to ignore */
96728c05 221 orl PCPU(fpending),%eax
ef0fdad1
MD
222 jmp doreti_next
223
984263bc 224 /*
ef0fdad1 225 * INTR interrupt pending
96728c05 226 *
46a3f46d
MD
227 * Temporarily back-out our critical section to allow an interrupt
228 * preempt us when we schedule it. Bump intr_nesting_level to
229 * prevent the switch code from recursing via splz too deeply.
984263bc 230 */
ef0fdad1
MD
231 ALIGN_TEXT
232doreti_intr:
2954c92f 233 andl PCPU(ipending),%ecx /* only check normal ints */
ef0fdad1 234 bsfl %ecx, %ecx /* locate the next dispatchable int */
2954c92f 235 btrl %ecx, PCPU(ipending) /* is it really still pending? */
ef0fdad1 236 jnc doreti_next
984263bc
MD
237 pushl %eax
238 pushl %ecx
46a3f46d 239 incl TD_NEST_COUNT(%ebx) /* prevent doreti/splz nesting */
a2a5ad0d 240 subl $TDPRI_CRIT,TD_PRI(%ebx) /* so we can preempt */
2954c92f 241 call sched_ithd /* YYY must pull in imasks */
96728c05 242 addl $TDPRI_CRIT,TD_PRI(%ebx)
46a3f46d 243 decl TD_NEST_COUNT(%ebx)
984263bc 244 addl $4,%esp
984263bc 245 popl %eax
ef0fdad1 246 jmp doreti_next
984263bc 247
984263bc 248 /*
235957ed
MD
249 * AST pending. We clear RQF_AST_SIGNAL automatically, the others
250 * are cleared by the trap as they are processed.
a2a5ad0d
MD
251 *
252 * Temporarily back-out our critical section because trap() can be
253 * a long-winded call, and we want to be more syscall-like.
254 *
235957ed
MD
255 * YYY theoretically we can call lwkt_switch directly if all we need
256 * to do is a reschedule.
984263bc 257 */
235957ed
MD
258doreti_ast:
259 andl $~RQF_AST_SIGNAL,PCPU(reqflags)
a2a5ad0d
MD
260 sti
261 movl %eax,%esi /* save cpl (can't use stack) */
262 movl $T_ASTFLT,TF_TRAPNO(%esp)
a2a5ad0d
MD
263 subl $TDPRI_CRIT,TD_PRI(%ebx)
2641: call trap
265 addl $TDPRI_CRIT,TD_PRI(%ebx)
96728c05
MD
266 movl %esi,%eax /* restore cpl for loop */
267 jmp doreti_next
268
269#ifdef SMP
270 /*
235957ed 271 * IPIQ message pending. We clear RQF_IPIQ automatically.
96728c05
MD
272 */
273doreti_ipiq:
03aa8d99 274 incl PCPU(intr_nesting_level)
235957ed 275 andl $~RQF_IPIQ,PCPU(reqflags)
96728c05 276 call lwkt_process_ipiq
03aa8d99 277 decl PCPU(intr_nesting_level)
ef0fdad1 278 movl TD_CPL(%ebx),%eax /* retrieve cpl again for loop */
984263bc
MD
279 jmp doreti_next
280
96728c05 281#endif
ef0fdad1
MD
282
283 /*
284 * SPLZ() a C callable procedure to dispatch any unmasked pending
285 * interrupts regardless of critical section nesting. ASTs
286 * are not dispatched.
26a0694b
MD
287 *
288 * YYY at the moment I leave us in a critical section so as
289 * not to have to mess with the cpls which will soon be obsolete.
ef0fdad1
MD
290 */
291 SUPERALIGN_TEXT
292
293ENTRY(splz)
26a0694b 294 pushfl
ef0fdad1 295 pushl %ebx
2954c92f 296 movl PCPU(curthread),%ebx
ef0fdad1 297 movl TD_CPL(%ebx),%eax
26a0694b 298 addl $TDPRI_CRIT,TD_PRI(%ebx)
ef0fdad1
MD
299
300splz_next:
26a0694b 301 cli
ef0fdad1
MD
302 movl %eax,%ecx /* ecx = ~CPL */
303 notl %ecx
96728c05 304#ifdef SMP
235957ed 305 testl $RQF_IPIQ,PCPU(reqflags)
96728c05
MD
306 jnz splz_ipiq
307#endif
a2a5ad0d
MD
308 testl PCPU(fpending),%ecx /* check for an unmasked fast int */
309 jnz splz_fast
310
311 testl PCPU(ipending),%ecx
312 jnz splz_intr
96728c05 313
26a0694b 314 subl $TDPRI_CRIT,TD_PRI(%ebx)
235957ed 315
3c23a41a
MD
316 /*
317 * Nothing left to do, finish up. Interrupts are still disabled.
318 * If our temporary cpl mask is 0 then we have processed everything
319 * (including any pending fast ints requiring the MP lock), and
235957ed 320 * we can clear RQF_INTPEND.
3c23a41a 321 */
a2a5ad0d
MD
322 testl %eax,%eax
323 jnz 5f
235957ed 324 andl $~RQF_INTPEND,PCPU(reqflags)
a2a5ad0d 3255:
ef0fdad1 326 popl %ebx
26a0694b 327 popfl
984263bc
MD
328 ret
329
ef0fdad1
MD
330 /*
331 * FAST interrupt pending
332 */
984263bc 333 ALIGN_TEXT
ef0fdad1 334splz_fast:
2954c92f 335 andl PCPU(fpending),%ecx /* only check fast ints */
ef0fdad1 336 bsfl %ecx, %ecx /* locate the next dispatchable int */
2954c92f 337 btrl %ecx, PCPU(fpending) /* is it really still pending? */
ef0fdad1
MD
338 jnc splz_next
339 pushl %eax
96728c05
MD
340#ifdef SMP
341 pushl %ecx
342 call try_mplock
343 popl %ecx
344 testl %eax,%eax
345 jz 1f
346#endif
46a3f46d 347 incl PCPU(intr_nesting_level)
2954c92f 348 call *fastunpend(,%ecx,4)
46a3f46d 349 decl PCPU(intr_nesting_level)
96728c05
MD
350#ifdef SMP
351 call rel_mplock
352#endif
353 popl %eax
354 jmp splz_next
3551:
356 btsl %ecx, PCPU(fpending) /* oops, couldn't get the MP lock */
ef0fdad1 357 popl %eax
96728c05 358 orl PCPU(fpending),%eax
ef0fdad1 359 jmp splz_next
984263bc 360
ef0fdad1
MD
361 /*
362 * INTR interrupt pending
96728c05
MD
363 *
364 * Temporarily back-out our critical section to allow the interrupt
365 * preempt us.
ef0fdad1 366 */
984263bc 367 ALIGN_TEXT
ef0fdad1 368splz_intr:
2954c92f 369 andl PCPU(ipending),%ecx /* only check normal ints */
ef0fdad1 370 bsfl %ecx, %ecx /* locate the next dispatchable int */
2954c92f 371 btrl %ecx, PCPU(ipending) /* is it really still pending? */
ef0fdad1 372 jnc splz_next
26a0694b 373 sti
ef0fdad1 374 pushl %eax
984263bc 375 pushl %ecx
96728c05 376 subl $TDPRI_CRIT,TD_PRI(%ebx)
46a3f46d 377 incl TD_NEST_COUNT(%ebx) /* prevent doreti/splz nesting */
2954c92f 378 call sched_ithd /* YYY must pull in imasks */
96728c05 379 addl $TDPRI_CRIT,TD_PRI(%ebx)
46a3f46d 380 decl TD_NEST_COUNT(%ebx) /* prevent doreti/splz nesting */
ef0fdad1
MD
381 addl $4,%esp
382 popl %eax
383 jmp splz_next
984263bc 384
96728c05
MD
385#ifdef SMP
386splz_ipiq:
235957ed 387 andl $~RQF_IPIQ,PCPU(reqflags)
96728c05
MD
388 pushl %eax
389 call lwkt_process_ipiq
390 popl %eax
391 jmp splz_next
392#endif
393
ef0fdad1
MD
394 /*
395 * APIC/ICU specific ipl functions provide masking and unmasking
396 * calls for userland.
397 */
984263bc
MD
398
399#ifdef APIC_IO
400#include "i386/isa/apic_ipl.s"
401#else
402#include "i386/isa/icu_ipl.s"
403#endif /* APIC_IO */