SWI(9)
π SWI(9) β like, the kernel developer's manual, but make it make sense β¨
okay so bestie, this is SWI(9) from the Kernel Developer's Manual and it's literally all about software interrupt handlers β registering them, removing them, scheduling them. no cap, it's giving "the plumbing behind the plumbing" and honestly? iconic. π
πΈ NAME πΈ
swi_add, swi_remove, swi_sched β register and schedule software interrupt handlers π¦
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/interrupt.h>
extern struct intr_event *clk_intr_event;
int
swi_add(struct intr_event **eventp, const char *name,
driver_intr_t handler, void *arg, int pri, enum intr_type flags,
void **cookiep);
int
swi_remove(void *cookie);
void
swi_sched(void *cookie, int flags);
like, just copy that exactly into your file and you're so valid π
π DESCRIPTION π
so these functions register and schedule software interrupt handlers, periodt. software interrupt handlers get attached to a software interrupt thread, the exact same way hardware interrupt handlers get attached to a hardware interrupt thread. and here's the gag: multiple handlers can be attached to the same thread. π
the whole point? software interrupt handlers let you queue up less critical processing inside of hardware interrupt handlers so all that work can happen at a later time. main character energy β the hardware handler does the quick important bit and the software thread swoops in later, slay. π¦
software interrupt threads are different from other kernel threads in that they're treated as an interrupt thread. which means:
- time spent executing these threads is counted as interrupt time, and
- they can be run via a lightweight context switch.
ngl that's the whole vibe check right there.
π«Ά swi_add() β the new-handler maker π«Ά
swi_add() adds a new software interrupt handler to a specified interrupt event. let's break it down:
eventpβ an optional pointer to astruct intr_eventpointer. if this argument points to an existing event that holds a list of interrupt handlers, your handler gets attached to that event. otherwise a new event is created, and ifeventpis notNULL, then the pointer at that address will be modified to point to the newly created event. π that's it, that's the pointer-to-a-pointer lore.nameβ associates a name with this specific handler. this name is appended to the name of the software interrupt thread that this handler is attached to.handlerβ the function that will be executed when the handler is scheduled to run.argβ passed in as the only parameter tohandlerwhen the function is executed.priβ specifies the priority of this interrupt handler relative to other software interrupt handlers. if an interrupt event is created, then this value is used as the vector, and theflagsargument is used to specify the attributes of a handler such asINTR_MPSAFE.cookiepβ points to avoid *cookie. this cookie gets set to a value that uniquely identifies this handler, and it's what you use to schedule the handler for execution later on. keep it safe, bestie π
π§ swi_remove() β the cleanup girlie π§
swi_remove() tears down an interrupt handler pointed to by the cookie argument. it detaches the interrupt handler from the associated interrupt event and frees its memory. bye bye, no notes, we love a clean exit π
π₯ swi_sched() β the "go now (or not yet)" π₯
swi_sched() schedules an interrupt handler and its associated thread to run.
cookieβ specifies which software interrupt handler should be scheduled to run.flagsβ specifies how and when the handler should be run, and it's a mask of one or more of these flags:SWI_DELAYβ specifies that the kernel should mark the specified handler as needing to run, but the kernel should not schedule the software interrupt thread to run. instead,handlerwill be executed the next time that the software interrupt thread runs after being scheduled by another event. lowkey a "you're on deck, not up to bat" situation πSWI_FROMNMIβ specifies thatswi_sched()is called from NMI context and should be careful about used KPIs. on platforms allowing IPI sending from NMI context it immediately wakesclk_intr_eventvia the IPI, otherwise it works just likeSWI_DELAY. π careful with this one.
also: clk_intr_event is a pointer to the struct intr_event used to hang delayed handlers off of the clock interrupt, and is invoked directly by hardclock(9). β
π RETURN VALUES π
swi_add() and swi_remove() return zero on success and non-zero on failure. that's the whole thing, short and sweet π
π ERRORS π
swi_add() will fail if:
[EAGAIN]β the system-imposed limit on the total number of processes under execution would be exceeded. the limit is given by thesysctl(3)MIB variableKERN_MAXPROC.[EINVAL]β theflagsargument specifiesINTR_ENTROPY.[EINVAL]β theeventpargument points to a hardware interrupt thread.[EINVAL]β either of thenameorhandlerarguments areNULL.[EINVAL]β theINTR_EXCLflag is specified and the interrupt event pointed to byeventpalready has at least one handler, or the interrupt event already has an exclusive handler.
π₯Ί swi_remove() will fail if:
[EINVAL]β a software interrupt handler pointed to bycookieisNULL.
fr fr, check your cookies before you call these. NULL is not the vibe. π
π SEE ALSO π
hardclock(9), intr_event(9), taskqueue(9)
go read those next, iykyk πβ¨
π HISTORY π
swi_add()andswi_sched()first appeared in FreeBSD 5.0.- they replaced the
register_swi()function which appeared in FreeBSD 3.0, and thesetsoft*()andschedsoft*()functions which date back to at least 4.4BSD. swi_remove()first appeared in FreeBSD 6.1.
a whole glow-up timeline, honestly π«Άπ₯
okay that's the page!! this one came from Debian, dated October 12, 2022, and it's SWI(9) in the Kernel Developer's Manual β love that for us, mwah ππΈ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms