SWI(9)
π SWI π
okay bestie, sit down, hydrate, and let me tell you about swi_add, swi_remove and swi_sched β the software interrupt handler girlies of the FreeBSD kernel. this is from the Kernel Developer's Manual, section 9, and it's genuinely so good. π
β¨ 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 *tty_intr_event;
extern struct intr_event *clk_intr_event;
extern void *vm_ih;
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);
β¨ DESCRIPTION β¨
so these functions are the whole point of the page, and what they do is register and schedule software interrupt handlers. π software interrupt handlers get attached to a software interrupt thread, exactly the same way hardware interrupt handlers get attached to a hardware interrupt thread. and the way multiple handlers can be attached to the same thread?? iconic. lowkey the whole design.
the reason you'd even want this: software interrupt handlers let you queue up the less critical processing from inside a hardware interrupt handler, so that work can be done at a later time. deferring, but make it cute. β¨
and here's the thing that makes software interrupt threads special β they're not like other kernel threads. they're treated as an interrupt thread. which means:
- time spent executing these threads counts as interrupt time
- they can be run via a lightweight context switch
main character energy, honestly. π
πΈ swi_add() πΈ
swi_add() adds a new software interrupt handler to a specified interrupt event. let's go through the args because every single one matters:
eventpβ an optional pointer to astruct intr_eventpointer. if it points to an existing event that holds a list of interrupt handlers, then your handler gets attached to that event. otherwise, a new event gets created, and ifeventpis notNULL, then the pointer at that address gets modified to point to the newly created event. π«Άnameβ associates a name with a 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β 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.flagsβ specifies 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 used to schedule the handler for execution later on. keep it safe, bestie. π
πΈ swi_remove() πΈ
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. it's giving cleanup era. π§
πΈ swi_sched() πΈ
swi_sched() schedules an interrupt handler and its associated thread to run. the cookie argument says which software interrupt handler should be scheduled to run. the flags argument says how and when it should run, and it's a mask of one or more</s> of these flags:
SWI_DELAYβ says 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. attaching a handler to the clock software interrupt thread and using this flag when scheduling a software interrupt handler can be used to implement the functionality performed bysetdelayed()in earlier versions of FreeBSD. β¨SWI_FROMNMIβ says 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. π
πΈ the global girlies πΈ
tty_intr_eventandclk_intr_eventare pointers to the software interrupt handlers for the tty and clock software interrupts, respectively.tty_intr_eventis used to hang tty software interrupt handlers off of the same thread.clk_intr_eventis used to hang delayed handlers off of the clock software interrupt thread so that the functionality ofsetdelayed()can be obtained in conjunction withSWI_DELAY.vm_ihis the handler cookie used to schedule software interrupt threads to run for the VM subsystem. π¦
β¨ RETURN VALUES β¨
swi_add() and swi_remove() return zero on success and non-zero on failure. that's it, that's the whole vibe check. π
β¨ 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.
β¨ SEE ALSO β¨
go read those too, they're part of the lore. π«Ά
β¨ 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.
the glow-up is real, no cap. π
β¨ BUGS β¨
most of the global variables described in this manual page should not be global, or at the very least should not be declared in <sys/interrupt.h>. not me exposing the whole thing π
and that's the page, bestie!! this one came from the Debian crew, dated July 25, 2020 β swi(9), kernel developer's manual, forever iconic. mwah πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms