1*ca5e5d48Sriastradh /* $Id: nmi.c,v 1.6 2022/05/15 12:45:33 riastradh Exp $ */
24b64c2cbSyamt
34b64c2cbSyamt /*-
4d654e928Syamt * Copyright (c)2009,2011 YAMAMOTO Takashi,
54b64c2cbSyamt * All rights reserved.
64b64c2cbSyamt *
74b64c2cbSyamt * Redistribution and use in source and binary forms, with or without
84b64c2cbSyamt * modification, are permitted provided that the following conditions
94b64c2cbSyamt * are met:
104b64c2cbSyamt * 1. Redistributions of source code must retain the above copyright
114b64c2cbSyamt * notice, this list of conditions and the following disclaimer.
124b64c2cbSyamt * 2. Redistributions in binary form must reproduce the above copyright
134b64c2cbSyamt * notice, this list of conditions and the following disclaimer in the
144b64c2cbSyamt * documentation and/or other materials provided with the distribution.
154b64c2cbSyamt *
164b64c2cbSyamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174b64c2cbSyamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184b64c2cbSyamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194b64c2cbSyamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204b64c2cbSyamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214b64c2cbSyamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224b64c2cbSyamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234b64c2cbSyamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244b64c2cbSyamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254b64c2cbSyamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264b64c2cbSyamt * SUCH DAMAGE.
274b64c2cbSyamt */
284b64c2cbSyamt
294b64c2cbSyamt #include <sys/cdefs.h>
30*ca5e5d48Sriastradh __KERNEL_RCSID(0, "$NetBSD: nmi.c,v 1.6 2022/05/15 12:45:33 riastradh Exp $");
314b64c2cbSyamt
324b64c2cbSyamt /*
334b64c2cbSyamt * nmi dispatcher.
344b64c2cbSyamt *
354b64c2cbSyamt * XXX no need to be nmi-specific.
364b64c2cbSyamt * actual assumptions are:
374b64c2cbSyamt * - dispatch() is called with preemption disabled.
384b64c2cbSyamt * - handlers never block.
394b64c2cbSyamt * - establish() and disestablish() are called within a thread context.
404b64c2cbSyamt * (thus can block)
414b64c2cbSyamt */
424b64c2cbSyamt
434b64c2cbSyamt #include <sys/param.h>
444b64c2cbSyamt #include <sys/kernel.h>
454b64c2cbSyamt #include <sys/systm.h>
464b64c2cbSyamt #include <sys/atomic.h>
474b64c2cbSyamt #include <sys/kmem.h>
484b64c2cbSyamt #include <sys/mutex.h>
49d654e928Syamt #include <sys/pserialize.h>
504b64c2cbSyamt
514b64c2cbSyamt #include <x86/nmi.h>
524b64c2cbSyamt
534b64c2cbSyamt struct nmi_handler {
544b64c2cbSyamt int (*n_func)(const struct trapframe *, void *);
554b64c2cbSyamt void *n_arg;
564b64c2cbSyamt struct nmi_handler *n_next;
574b64c2cbSyamt };
584b64c2cbSyamt
594b64c2cbSyamt static kmutex_t nmi_list_lock; /* serialize establish and disestablish */
60d654e928Syamt static pserialize_t nmi_psz;
614b64c2cbSyamt static nmi_handler_t *nmi_handlers; /* list of handlers */
624b64c2cbSyamt
634b64c2cbSyamt /*
644b64c2cbSyamt * nmi_establish: establish an nmi handler
654b64c2cbSyamt *
664b64c2cbSyamt * => can block.
674b64c2cbSyamt * => returns an opaque handle.
684b64c2cbSyamt */
694b64c2cbSyamt
704b64c2cbSyamt nmi_handler_t *
nmi_establish(int (* func)(const struct trapframe *,void *),void * arg)714b64c2cbSyamt nmi_establish(int (*func)(const struct trapframe *, void *), void *arg)
724b64c2cbSyamt {
734b64c2cbSyamt struct nmi_handler *n;
744b64c2cbSyamt
754b64c2cbSyamt n = kmem_alloc(sizeof(*n), KM_SLEEP);
764b64c2cbSyamt n->n_func = func;
774b64c2cbSyamt n->n_arg = arg;
784b64c2cbSyamt
794b64c2cbSyamt /*
804b64c2cbSyamt * put it into the list.
814b64c2cbSyamt */
824b64c2cbSyamt
834b64c2cbSyamt mutex_enter(&nmi_list_lock);
844b64c2cbSyamt n->n_next = nmi_handlers;
85*ca5e5d48Sriastradh atomic_store_release(&nmi_handlers, n);
864b64c2cbSyamt mutex_exit(&nmi_list_lock);
874b64c2cbSyamt
884b64c2cbSyamt return n;
894b64c2cbSyamt }
904b64c2cbSyamt
914b64c2cbSyamt /*
924b64c2cbSyamt * nmi_disestablish: disestablish an nmi handler.
934b64c2cbSyamt *
944b64c2cbSyamt * => can block.
954b64c2cbSyamt * => take an opaque handle. it must be one returned by nmi_establish.
964b64c2cbSyamt */
974b64c2cbSyamt
984b64c2cbSyamt void
nmi_disestablish(nmi_handler_t * handle)994b64c2cbSyamt nmi_disestablish(nmi_handler_t *handle)
1004b64c2cbSyamt {
1014b64c2cbSyamt nmi_handler_t *n;
1024b64c2cbSyamt nmi_handler_t **pp;
1034b64c2cbSyamt
1044b64c2cbSyamt KASSERT(handle != NULL);
1054b64c2cbSyamt
1064b64c2cbSyamt /*
1074b64c2cbSyamt * remove the handler from the list.
1084b64c2cbSyamt */
1094b64c2cbSyamt
1104b64c2cbSyamt mutex_enter(&nmi_list_lock);
1114b64c2cbSyamt for (pp = &nmi_handlers, n = *pp; n != NULL; n = *pp) {
1124b64c2cbSyamt if (n == handle) {
1134b64c2cbSyamt break;
1144b64c2cbSyamt }
1154b64c2cbSyamt pp = &n->n_next;
1164b64c2cbSyamt }
1174b64c2cbSyamt #if defined(DIAGNOSTIC)
1184b64c2cbSyamt if (n == NULL) {
1194b64c2cbSyamt mutex_exit(&nmi_list_lock);
1204b64c2cbSyamt panic("%s: invalid handle %p", __func__, handle);
1214b64c2cbSyamt }
1224b64c2cbSyamt #endif /* defined(DIAGNOSTIC) */
123*ca5e5d48Sriastradh atomic_store_relaxed(pp, n->n_next);
1244b64c2cbSyamt mutex_exit(&nmi_list_lock); /* mutex_exit implies a store fence */
1254b64c2cbSyamt
1264b64c2cbSyamt /*
1274b64c2cbSyamt * before freeing 'n', ensure that no other cpus are
1284b64c2cbSyamt * in the middle of nmi_dispatch.
1294b64c2cbSyamt */
1304b64c2cbSyamt
131d654e928Syamt pserialize_perform(nmi_psz);
1324b64c2cbSyamt kmem_free(n, sizeof(*n));
1334b64c2cbSyamt }
1344b64c2cbSyamt
1354b64c2cbSyamt /*
1364b64c2cbSyamt * nmi_dispatch: dispatch an nmi.
1374b64c2cbSyamt *
1384b64c2cbSyamt * => called by interrupts, thus preempt disabled.
1394b64c2cbSyamt */
1404b64c2cbSyamt
1414b64c2cbSyamt int
nmi_dispatch(const struct trapframe * tf)1424b64c2cbSyamt nmi_dispatch(const struct trapframe *tf)
1434b64c2cbSyamt {
1444b64c2cbSyamt const struct nmi_handler *n;
1454b64c2cbSyamt int handled = 0;
1464b64c2cbSyamt
147d654e928Syamt /*
148d654e928Syamt * XXX abstraction violation
149d654e928Syamt *
150d654e928Syamt * we don't bother to call pserialize_read_enter/pserialize_read_exit
151d654e928Syamt * because they are not necessary here as we are sure our IPL is
1522312c4caSrmind * higher than IPL_SOFTSERIAL. better to avoid unnecessary calls as
153d654e928Syamt * we are in a dangerous context. (NMI)
154d654e928Syamt */
155d654e928Syamt
156*ca5e5d48Sriastradh for (n = atomic_load_consume(&nmi_handlers);
1574b64c2cbSyamt n != NULL;
158*ca5e5d48Sriastradh n = atomic_load_relaxed(&n->n_next)) {
1594b64c2cbSyamt handled |= (*n->n_func)(tf, n->n_arg);
1604b64c2cbSyamt }
1614b64c2cbSyamt return handled;
1624b64c2cbSyamt }
1634b64c2cbSyamt
1644b64c2cbSyamt void
nmi_init(void)1654b64c2cbSyamt nmi_init(void)
1664b64c2cbSyamt {
1674b64c2cbSyamt
1684b64c2cbSyamt mutex_init(&nmi_list_lock, MUTEX_DEFAULT, IPL_NONE);
169d654e928Syamt nmi_psz = pserialize_create();
1704b64c2cbSyamt }
171