1/* $NetBSD: idt.c,v 1.4 2016/08/27 14:19:47 maxv Exp $ */
2
3/*-
4 * Copyright (c) 1996, 1997, 1998, 2000, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility NASA Ames Research Center, and by Andrew Doran.
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*-
34 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * William Jolitz.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)machdep.c 7.4 (Berkeley) 6/3/91
65 */
66
67#include <sys/cdefs.h>
68__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.4 2016/08/27 14:19:47 maxv Exp $");
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/mutex.h>
73#include <sys/cpu.h>
74#include <sys/atomic.h>
75
76#include <machine/segments.h>
77
78#if !defined(XEN)
79
80struct gate_descriptor *idt;
81static char idt_allocmap[NIDT];
82
83/*
84 * Allocate an IDT vector slot within the given range.
85 * cpu_lock will be held unless single threaded during early boot.
86 */
87int
88idt_vec_alloc(int low, int high)
89{
90 int vec;
91
92 KASSERT(mutex_owned(&cpu_lock) || !mp_online);
93
94 for (vec = low; vec <= high; vec++) {
95 if (idt_allocmap[vec] == 0) {
96 /* idt_vec_free() can be unlocked, so membar. */
97 membar_sync();
98 idt_allocmap[vec] = 1;
99 return vec;
100 }
101 }
102 return 0;
103}
104
105void
106idt_vec_reserve(int vec)
107{
108 int result;
109
110 KASSERT(mutex_owned(&cpu_lock) || !mp_online);
111
112 result = idt_vec_alloc(vec, vec);
113 if (result != vec) {
114 panic("%s: failed to reserve vec %d", __func__, vec);
115 }
116}
117
118void
119idt_vec_set(int vec, void (*function)(void))
120{
121
122 KASSERT(mutex_owned(&cpu_lock) || !mp_online);
123 KASSERT(idt_allocmap[vec] == 1);
124 setgate(&idt[vec], function, 0, SDT_SYS386IGT, SEL_KPL,
125 GSEL(GCODE_SEL, SEL_KPL));
126}
127
128/*
129 * Free IDT vector. No locking required as release is atomic.
130 */
131void
132idt_vec_free(int vec)
133{
134
135 unsetgate(&idt[vec]);
136 idt_allocmap[vec] = 0;
137}
138
139#endif /* !defined(XEN) */
140