Implement an upcall mechanism to support userland LWKT. This mechanism will
[dragonfly.git] / sys / i386 / isa / ipl.s
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 $
40  * $DragonFly: src/sys/i386/isa/Attic/ipl.s,v 1.15 2003/11/21 05:29:08 dillon Exp $
41  */
42
43
44 /*
45  * AT/386
46  * Vector interrupt control section
47  *
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) */
56
57         .globl  tty_imask
58 tty_imask:      .long   SWI_TTY_MASK
59         .globl  bio_imask
60 bio_imask:      .long   SWI_CLOCK_MASK | SWI_CAMBIO_MASK
61         .globl  net_imask
62 net_imask:      .long   SWI_NET_MASK | SWI_CAMNET_MASK
63         .globl  cam_imask
64 cam_imask:      .long   SWI_CAMBIO_MASK | SWI_CAMNET_MASK
65         .globl  soft_imask
66 soft_imask:     .long   SWI_MASK
67         .globl  softnet_imask
68 softnet_imask:  .long   SWI_NET_MASK
69         .globl  softtty_imask
70 softtty_imask:  .long   SWI_TTY_MASK
71
72         .text
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          */
93
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.
100          *
101          * If we are in a critical section we cannot run any pending ints
102          * nor can be play with mp_lock.
103          *
104          */
105         SUPERALIGN_TEXT
106         .type   doreti,@function
107 doreti:
108         FAKE_MCOUNT(bintr)              /* init "from" bintr -> doreti */
109         popl    %eax                    /* cpl to restore */
110         movl    PCPU(curthread),%ebx
111         cli                             /* interlock with TDPRI_CRIT */
112         cmpl    $0,PCPU(reqflags)       /* short cut if nothing to do */
113         je      5f
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 */
118 doreti_next:
119         sti                             /* allow new interrupts */
120         movl    %eax,%ecx               /* cpl being restored */
121         notl    %ecx
122         cli                             /* disallow YYY remove */
123 #ifdef SMP
124         testl   $RQF_IPIQ,PCPU(reqflags)
125         jnz     doreti_ipiq
126 #endif
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
132
133         testl   $RQF_AST_MASK,PCPU(reqflags) /* any pending ASTs? */
134         jz      2f
135         testl   $PSL_VM,TF_EFLAGS(%esp)
136         jz      1f
137         cmpl    $1,in_vm86call          /* YYY make per 'cpu'? */
138         jnz     doreti_ast
139 1:
140         testb   $SEL_RPL_MASK,TF_CS(%esp)
141         jnz     doreti_ast
142 2:
143         /*
144          * Nothing left to do, finish up.  Interrupts are still disabled.
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.
150          */
151         subl    $TDPRI_CRIT,TD_PRI(%ebx)        /* interlocked with cli */
152         testl   %eax,%eax
153         jnz     5f
154         andl    $~RQF_INTPEND,PCPU(reqflags)
155 5:
156         MEXITCOUNT
157         .globl  doreti_popl_fs
158         .globl  doreti_popl_es
159         .globl  doreti_popl_ds
160         .globl  doreti_iret
161         .globl  doreti_syscall_ret
162 doreti_syscall_ret:
163 doreti_popl_fs:
164         popl    %fs
165 doreti_popl_es:
166         popl    %es
167 doreti_popl_ds:
168         popl    %ds
169         popal
170         addl    $8,%esp
171 doreti_iret:
172         iret
173
174         ALIGN_TEXT
175         .globl  doreti_iret_fault
176 doreti_iret_fault:
177         subl    $8,%esp
178         pushal
179         pushl   %ds
180         .globl  doreti_popl_ds_fault
181 doreti_popl_ds_fault:
182         pushl   %es
183         .globl  doreti_popl_es_fault
184 doreti_popl_es_fault:
185         pushl   %fs
186         .globl  doreti_popl_fs_fault
187 doreti_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
192         /*
193          * FAST interrupt pending
194          */
195         ALIGN_TEXT
196 doreti_fast:
197         andl    PCPU(fpending),%ecx     /* only check fast ints */
198         bsfl    %ecx, %ecx              /* locate the next dispatchable int */
199         btrl    %ecx, PCPU(fpending)    /* is it really still pending? */
200         jnc     doreti_next
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
208         /* MP lock successful */
209 #endif
210         incl    PCPU(intr_nesting_level)
211         call    *fastunpend(,%ecx,4)
212         decl    PCPU(intr_nesting_level)
213 #ifdef SMP
214         call    rel_mplock
215 #endif
216         popl    %eax
217         jmp     doreti_next
218 1:
219         btsl    %ecx, PCPU(fpending)    /* oops, couldn't get the MP lock */
220         popl    %eax                    /* add to temp. cpl mask to ignore */
221         orl     PCPU(fpending),%eax
222         jmp     doreti_next
223
224         /*
225          *  INTR interrupt pending
226          *
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.
230          */
231         ALIGN_TEXT
232 doreti_intr:
233         andl    PCPU(ipending),%ecx     /* only check normal ints */
234         bsfl    %ecx, %ecx              /* locate the next dispatchable int */
235         btrl    %ecx, PCPU(ipending)    /* is it really still pending? */
236         jnc     doreti_next
237         pushl   %eax
238         pushl   %ecx
239         incl    TD_NEST_COUNT(%ebx)     /* prevent doreti/splz nesting */
240         subl    $TDPRI_CRIT,TD_PRI(%ebx) /* so we can preempt */
241         call    sched_ithd              /* YYY must pull in imasks */
242         addl    $TDPRI_CRIT,TD_PRI(%ebx)
243         decl    TD_NEST_COUNT(%ebx)
244         addl    $4,%esp
245         popl    %eax
246         jmp     doreti_next
247
248         /*
249          * AST pending.  We clear RQF_AST_SIGNAL automatically, the others
250          * are cleared by the trap as they are processed.
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          *
255          * YYY theoretically we can call lwkt_switch directly if all we need
256          * to do is a reschedule.
257          */
258 doreti_ast:
259         andl    $~(RQF_AST_SIGNAL|RQF_AST_UPCALL),PCPU(reqflags)
260         sti
261         movl    %eax,%esi               /* save cpl (can't use stack) */
262         movl    $T_ASTFLT,TF_TRAPNO(%esp)
263         subl    $TDPRI_CRIT,TD_PRI(%ebx)
264 1:      call    trap
265         addl    $TDPRI_CRIT,TD_PRI(%ebx)
266         movl    %esi,%eax               /* restore cpl for loop */
267         jmp     doreti_next
268
269 #ifdef SMP
270         /*
271          * IPIQ message pending.  We clear RQF_IPIQ automatically.
272          */
273 doreti_ipiq:
274         incl    PCPU(intr_nesting_level)
275         andl    $~RQF_IPIQ,PCPU(reqflags)
276         call    lwkt_process_ipiq
277         decl    PCPU(intr_nesting_level)
278         movl    TD_CPL(%ebx),%eax       /* retrieve cpl again for loop */
279         jmp     doreti_next
280
281 #endif
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.
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.
290          */
291         SUPERALIGN_TEXT
292
293 ENTRY(splz)
294         pushfl
295         pushl   %ebx
296         movl    PCPU(curthread),%ebx
297         movl    TD_CPL(%ebx),%eax
298         addl    $TDPRI_CRIT,TD_PRI(%ebx)
299
300 splz_next:
301         cli
302         movl    %eax,%ecx               /* ecx = ~CPL */
303         notl    %ecx
304 #ifdef SMP
305         testl   $RQF_IPIQ,PCPU(reqflags)
306         jnz     splz_ipiq
307 #endif
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
313
314         subl    $TDPRI_CRIT,TD_PRI(%ebx)
315
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
320          * we can clear RQF_INTPEND.
321          */
322         testl   %eax,%eax
323         jnz     5f
324         andl    $~RQF_INTPEND,PCPU(reqflags)
325 5:
326         popl    %ebx
327         popfl
328         ret
329
330         /*
331          * FAST interrupt pending
332          */
333         ALIGN_TEXT
334 splz_fast:
335         andl    PCPU(fpending),%ecx     /* only check fast ints */
336         bsfl    %ecx, %ecx              /* locate the next dispatchable int */
337         btrl    %ecx, PCPU(fpending)    /* is it really still pending? */
338         jnc     splz_next
339         pushl   %eax
340 #ifdef SMP
341         pushl   %ecx
342         call    try_mplock
343         popl    %ecx
344         testl   %eax,%eax
345         jz      1f
346 #endif
347         incl    PCPU(intr_nesting_level)
348         call    *fastunpend(,%ecx,4)
349         decl    PCPU(intr_nesting_level)
350 #ifdef SMP
351         call    rel_mplock
352 #endif
353         popl    %eax
354         jmp     splz_next
355 1:
356         btsl    %ecx, PCPU(fpending)    /* oops, couldn't get the MP lock */
357         popl    %eax
358         orl     PCPU(fpending),%eax
359         jmp     splz_next
360
361         /*
362          *  INTR interrupt pending
363          *
364          *  Temporarily back-out our critical section to allow the interrupt
365          *  preempt us.
366          */
367         ALIGN_TEXT
368 splz_intr:
369         andl    PCPU(ipending),%ecx     /* only check normal ints */
370         bsfl    %ecx, %ecx              /* locate the next dispatchable int */
371         btrl    %ecx, PCPU(ipending)    /* is it really still pending? */
372         jnc     splz_next
373         sti
374         pushl   %eax
375         pushl   %ecx
376         subl    $TDPRI_CRIT,TD_PRI(%ebx)
377         incl    TD_NEST_COUNT(%ebx)     /* prevent doreti/splz nesting */
378         call    sched_ithd              /* YYY must pull in imasks */
379         addl    $TDPRI_CRIT,TD_PRI(%ebx)
380         decl    TD_NEST_COUNT(%ebx)     /* prevent doreti/splz nesting */
381         addl    $4,%esp
382         popl    %eax
383         jmp     splz_next
384
385 #ifdef SMP
386 splz_ipiq:
387         andl    $~RQF_IPIQ,PCPU(reqflags)
388         pushl   %eax
389         call    lwkt_process_ipiq
390         popl    %eax
391         jmp     splz_next
392 #endif
393
394         /*
395          * APIC/ICU specific ipl functions provide masking and unmasking
396          * calls for userland.
397          */
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 */