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
|
/*
* Copyright (c) 1997, Stefan Esser <se@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*
*/
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/rtprio.h>
#include <sys/systm.h>
#include <sys/ipl.h>
#include <sys/interrupt.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/ktr.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/unistd.h>
#include <sys/vmmeter.h>
#include <machine/atomic.h>
#include <machine/cpu.h>
struct swilist {
swihand_t *sl_handler;
struct swilist *sl_next;
};
static struct swilist swilists[NSWI];
u_long softintr_count[NSWI];
static struct proc *softithd;
volatile u_int sdelayed;
volatile u_int spending;
static void start_softintr(void *);
static void intr_soft(void *);
void
register_swi(intr, handler)
int intr;
swihand_t *handler;
{
struct swilist *slp, *slq;
int s;
if (intr < 0 || intr >= NSWI)
panic("register_swi: bad intr %d", intr);
if (handler == swi_generic || handler == swi_null)
panic("register_swi: bad handler %p", (void *)handler);
slp = &swilists[intr];
s = splhigh();
if (shandlers[intr] == swi_null)
shandlers[intr] = handler;
else {
if (slp->sl_next == NULL) {
slp->sl_handler = shandlers[intr];
shandlers[intr] = swi_generic;
}
slq = malloc(sizeof(*slq), M_DEVBUF, M_NOWAIT);
if (slq == NULL)
panic("register_swi: malloc failed");
slq->sl_handler = handler;
slq->sl_next = NULL;
while (slp->sl_next != NULL)
slp = slp->sl_next;
slp->sl_next = slq;
}
splx(s);
}
void
swi_dispatcher(intr)
int intr;
{
struct swilist *slp;
slp = &swilists[intr];
do {
(*slp->sl_handler)();
slp = slp->sl_next;
} while (slp != NULL);
}
void
unregister_swi(intr, handler)
int intr;
swihand_t *handler;
{
struct swilist *slfoundpred, *slp, *slq;
int s;
if (intr < 0 || intr >= NSWI)
panic("unregister_swi: bad intr %d", intr);
if (handler == swi_generic || handler == swi_null)
panic("unregister_swi: bad handler %p", (void *)handler);
slp = &swilists[intr];
s = splhigh();
if (shandlers[intr] == handler)
shandlers[intr] = swi_null;
else if (slp->sl_next != NULL) {
slfoundpred = NULL;
for (slq = slp->sl_next; slq != NULL;
slp = slq, slq = slp->sl_next)
if (slq->sl_handler == handler)
slfoundpred = slp;
slp = &swilists[intr];
if (slfoundpred != NULL) {
slq = slfoundpred->sl_next;
slfoundpred->sl_next = slq->sl_next;
free(slq, M_DEVBUF);
} else if (slp->sl_handler == handler) {
slq = slp->sl_next;
slp->sl_next = slq->sl_next;
slp->sl_handler = slq->sl_handler;
free(slq, M_DEVBUF);
}
if (slp->sl_next == NULL)
shandlers[intr] = slp->sl_handler;
}
splx(s);
}
int
ithread_priority(flags)
int flags;
{
int pri;
switch (flags) {
case INTR_TYPE_TTY: /* keyboard or parallel port */
pri = PI_TTYLOW;
break;
case (INTR_TYPE_TTY | INTR_FAST): /* sio */
pri = PI_TTYHIGH;
break;
case INTR_TYPE_BIO:
/*
* XXX We need to refine this. BSD/OS distinguishes
* between tape and disk priorities.
*/
pri = PI_DISK;
break;
case INTR_TYPE_NET:
pri = PI_NET;
break;
case INTR_TYPE_CAM:
pri = PI_DISK; /* XXX or PI_CAM? */
break;
case INTR_TYPE_MISC:
pri = PI_DULL; /* don't care */
break;
/* We didn't specify an interrupt level. */
default:
panic("ithread_priority: no interrupt type in flags");
}
return pri;
}
/*
* Schedule the soft interrupt handler thread.
*/
void
sched_softintr(void)
{
atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */
/*
* If we don't have an interrupt resource or an interrupt thread for
* this IRQ, log it as a stray interrupt.
*/
if (softithd == NULL)
panic("soft interrupt scheduled too early");
CTR3(KTR_INTR, "sched_softintr pid %d(%s) spending=0x%x",
softithd->p_pid, softithd->p_comm, spending);
/*
* Get the sched lock and see if the thread is on whichkqs yet.
* If not, put it on there. In any case, kick everyone so that if
* the new thread is higher priority than their current thread, it
* gets run now.
*/
mtx_enter(&sched_lock, MTX_SPIN);
if (softithd->p_stat == SWAIT) { /* not on run queue */
CTR1(KTR_INTR, "sched_softintr: setrunqueue %d",
softithd->p_pid);
/* membar_lock(); */
softithd->p_stat = SRUN;
setrunqueue(softithd);
aston();
}
mtx_exit(&sched_lock, MTX_SPIN);
#if 0
aston(); /* ??? check priorities first? */
#else
need_resched();
#endif
}
SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
/*
* Start soft interrupt thread.
*/
static void
start_softintr(dummy)
void *dummy;
{
int error;
if (softithd != NULL) { /* we already have a thread */
printf("start_softintr: already running");
return;
}
error = kthread_create(intr_soft, NULL, &softithd,
RFSTOPPED | RFHIGHPID, "softinterrupt");
if (error)
panic("start_softintr: kthread_create error %d\n", error);
softithd->p_rtprio.type = RTP_PRIO_ITHREAD;
softithd->p_rtprio.prio = PI_SOFT; /* soft interrupt */
softithd->p_stat = SWAIT; /* we're idle */
softithd->p_flag |= P_NOLOAD;
}
/*
* Software interrupt process code.
*/
static void
intr_soft(dummy)
void *dummy;
{
int i;
u_int pend;
/* Main loop */
for (;;) {
CTR3(KTR_INTR, "intr_soft pid %d(%s) spending=0x%x",
curproc->p_pid, curproc->p_comm, spending);
/*
* Service interrupts. If another interrupt arrives
* while we are running, they will set spending to
* denote that we should make another pass.
*/
pend = atomic_readandclear_int(&spending);
while ((i = ffs(pend))) {
i--;
atomic_add_long(&softintr_count[i], 1);
pend &= ~ (1 << i);
mtx_enter(&Giant, MTX_DEF);
if (shandlers[i] == swi_generic)
swi_dispatcher(i);
else
(shandlers[i])();
mtx_exit(&Giant, MTX_DEF);
}
/*
* Processed all our interrupts. Now get the sched
* lock. This may take a while and spending may get
* set again, so we have to check it again.
*/
mtx_enter(&sched_lock, MTX_SPIN);
if (spending == 0) {
CTR1(KTR_INTR, "intr_soft pid %d: done",
curproc->p_pid);
curproc->p_stat = SWAIT; /* we're idle */
mi_switch();
CTR1(KTR_INTR, "intr_soft pid %d: resumed",
curproc->p_pid);
}
mtx_exit(&sched_lock, MTX_SPIN);
}
}
/*
* Bits in the spending bitmap variable must be set atomically because
* spending may be manipulated by interrupts or other cpu's without holding
* any locks.
*
* Note: setbits uses a locked or, making simple cases MP safe.
*/
#define DO_SETBITS(name, var, bits) \
void name(void) \
{ \
atomic_set_int(var, bits); \
sched_softintr(); \
}
#define DO_SETBITS_AND_NO_MORE(name, var, bits) \
void name(void) \
{ \
atomic_set_int(var, bits); \
}
DO_SETBITS(setsoftcamnet,&spending, SWI_CAMNET_PENDING)
DO_SETBITS(setsoftcambio,&spending, SWI_CAMBIO_PENDING)
DO_SETBITS(setsoftclock, &spending, SWI_CLOCK_PENDING)
DO_SETBITS(setsoftnet, &spending, SWI_NET_PENDING)
DO_SETBITS(setsofttty, &spending, SWI_TTY_PENDING)
DO_SETBITS(setsoftvm, &spending, SWI_VM_PENDING)
DO_SETBITS(setsofttq, &spending, SWI_TQ_PENDING)
DO_SETBITS_AND_NO_MORE(schedsoftcamnet, &sdelayed, SWI_CAMNET_PENDING)
DO_SETBITS_AND_NO_MORE(schedsoftcambio, &sdelayed, SWI_CAMBIO_PENDING)
DO_SETBITS_AND_NO_MORE(schedsoftnet, &sdelayed, SWI_NET_PENDING)
DO_SETBITS_AND_NO_MORE(schedsofttty, &sdelayed, SWI_TTY_PENDING)
DO_SETBITS_AND_NO_MORE(schedsoftvm, &sdelayed, SWI_VM_PENDING)
DO_SETBITS_AND_NO_MORE(schedsofttq, &sdelayed, SWI_TQ_PENDING)
void
setdelayed(void)
{
int pend;
pend = atomic_readandclear_int(&sdelayed);
if (pend != 0) {
atomic_set_int(&spending, pend);
sched_softintr();
}
}
intrmask_t
softclockpending(void)
{
return (spending & SWI_CLOCK_PENDING);
}
/*
* Dummy spl calls. The only reason for these is to not break
* all the code which expects to call them.
*/
void spl0 (void) {}
void splx (intrmask_t x) {}
intrmask_t splq(intrmask_t mask) { return 0; }
intrmask_t splbio(void) { return 0; }
intrmask_t splcam(void) { return 0; }
intrmask_t splclock(void) { return 0; }
intrmask_t splhigh(void) { return 0; }
intrmask_t splimp(void) { return 0; }
intrmask_t splnet(void) { return 0; }
intrmask_t splsoftcam(void) { return 0; }
intrmask_t splsoftcambio(void) { return 0; }
intrmask_t splsoftcamnet(void) { return 0; }
intrmask_t splsoftclock(void) { return 0; }
intrmask_t splsofttty(void) { return 0; }
intrmask_t splsoftvm(void) { return 0; }
intrmask_t splsofttq(void) { return 0; }
intrmask_t splstatclock(void) { return 0; }
intrmask_t spltty(void) { return 0; }
intrmask_t splvm(void) { return 0; }
|