]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/fiq.c
libfido2: update to 1.13.0
[FreeBSD/FreeBSD.git] / sys / arm / arm / fiq.c
1 /*      $NetBSD: fiq.c,v 1.5 2002/04/03 23:33:27 thorpej Exp $  */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
7  * All rights reserved.
8  *
9  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed for the NetBSD Project by
22  *      Wasabi Systems, Inc.
23  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24  *    or promote products derived from this software without specific prior
25  *    written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43
44 #include <machine/armreg.h>
45 #include <machine/cpufunc.h>
46 #include <machine/fiq.h>
47 #include <vm/vm.h>
48 #include <machine/pcb.h>
49 #include <vm/pmap.h>
50 #include <machine/cpu.h>
51
52 TAILQ_HEAD(, fiqhandler) fiqhandler_stack =
53     TAILQ_HEAD_INITIALIZER(fiqhandler_stack);
54
55 extern char *fiq_nullhandler_code;
56 extern uint32_t fiq_nullhandler_size;
57
58 /*
59  * fiq_installhandler:
60  *
61  *      Actually install the FIQ handler down at the FIQ vector.
62  *
63  *      The FIQ vector is fixed by the hardware definition as the
64  *      seventh 32-bit word in the vector page.
65  *
66  *      Note: If the FIQ is invoked via an extra layer of
67  *      indirection, the actual FIQ code store lives in the
68  *      data segment, so there is no need to manipulate
69  *      the vector page's protection.
70  */
71 static void
72 fiq_installhandler(void *func, size_t size)
73 {
74         const uint32_t fiqvector = 7 * sizeof(uint32_t);
75
76         memcpy((void *)(vector_page + fiqvector), func, size);
77         icache_sync((vm_offset_t) fiqvector, size);
78 }
79
80 /*
81  * fiq_claim:
82  *
83  *      Claim the FIQ vector.
84  */
85 int
86 fiq_claim(struct fiqhandler *fh)
87 {
88         struct fiqhandler *ofh;
89         u_int oldirqstate;
90         int error = 0;
91
92         if (fh->fh_size > 0x100)
93                 return (EFBIG);
94
95         oldirqstate = disable_interrupts(PSR_F);
96
97         if ((ofh = TAILQ_FIRST(&fiqhandler_stack)) != NULL) {
98                 if ((ofh->fh_flags & FH_CANPUSH) == 0) {
99                         error = EBUSY;
100                         goto out;
101                 }
102
103                 /* Save the previous FIQ handler's registers. */
104                 if (ofh->fh_regs != NULL)
105                         fiq_getregs(ofh->fh_regs);
106         }
107
108         /* Set FIQ mode registers to ours. */
109         if (fh->fh_regs != NULL)
110                 fiq_setregs(fh->fh_regs);
111
112         TAILQ_INSERT_HEAD(&fiqhandler_stack, fh, fh_list);
113
114         /* Now copy the actual handler into place. */
115         fiq_installhandler(fh->fh_func, fh->fh_size);
116
117         /* Make sure FIQs are enabled when we return. */
118         oldirqstate &= ~PSR_F;
119
120  out:
121         restore_interrupts(oldirqstate);
122         return (error);
123 }
124
125 /*
126  * fiq_release:
127  *
128  *      Release the FIQ vector.
129  */
130 void
131 fiq_release(struct fiqhandler *fh)
132 {
133         u_int oldirqstate;
134         struct fiqhandler *ofh;
135
136         oldirqstate = disable_interrupts(PSR_F);
137
138         /*
139          * If we are the currently active FIQ handler, then we
140          * need to save our registers and pop the next one back
141          * into the vector.
142          */
143         if (fh == TAILQ_FIRST(&fiqhandler_stack)) {
144                 if (fh->fh_regs != NULL)
145                         fiq_getregs(fh->fh_regs);
146                 TAILQ_REMOVE(&fiqhandler_stack, fh, fh_list);
147                 if ((ofh = TAILQ_FIRST(&fiqhandler_stack)) != NULL) {
148                         if (ofh->fh_regs != NULL)
149                                 fiq_setregs(ofh->fh_regs);
150                         fiq_installhandler(ofh->fh_func, ofh->fh_size);
151                 }
152         } else
153                 TAILQ_REMOVE(&fiqhandler_stack, fh, fh_list);
154
155         if (TAILQ_FIRST(&fiqhandler_stack) == NULL) {
156                 /* Copy the NULL handler back down into the vector. */
157                 fiq_installhandler(fiq_nullhandler_code, fiq_nullhandler_size);
158
159                 /* Make sure FIQs are disabled when we return. */
160                 oldirqstate |= PSR_F;
161         }
162
163         restore_interrupts(oldirqstate);
164 }