1 /* $OpenBSD: gdt.c,v 1.44 2023/01/30 10:49:05 jsg Exp $ */
2 /* $NetBSD: gdt.c,v 1.28 2002/12/14 09:38:50 junyoung Exp $ */
3
4 /*-
5 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by John T. Kohl and Charles M. Hannum.
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 * The initial GDT is setup for the boot processor. The GDT holds
35 * NGDT descriptors.
36 *
37 * Every CPU in a system has its own copy of the GDT. The only real difference
38 * between the two are currently that there is a cpu-specific segment holding
39 * the struct cpu_info of the processor, for simplicity at getting cpu_info
40 * fields from assembly.
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mutex.h>
46
47 #include <machine/gdt.h>
48 #include <machine/tss.h>
49
50 struct mutex gdt_lock_store = MUTEX_INITIALIZER(IPL_HIGH);
51
52 /*
53 * Lock and unlock the GDT.
54 */
55 #define gdt_lock() (mtx_enter(&gdt_lock_store))
56 #define gdt_unlock() (mtx_leave(&gdt_lock_store))
57
58 /* XXX needs spinlocking if we ever mean to go finegrained. */
59 void
setgdt(int sel,void * base,size_t limit,int type,int dpl,int def32,int gran)60 setgdt(int sel, void *base, size_t limit, int type, int dpl, int def32,
61 int gran)
62 {
63 struct segment_descriptor *sd = &cpu_info_primary.ci_gdt[sel].sd;
64 CPU_INFO_ITERATOR cii;
65 struct cpu_info *ci;
66
67 KASSERT(sel < NGDT);
68
69 setsegment(sd, base, limit, type, dpl, def32, gran);
70 CPU_INFO_FOREACH(cii, ci)
71 if (ci->ci_gdt != NULL && ci != &cpu_info_primary)
72 ci->ci_gdt[sel].sd = *sd;
73 }
74
75 /*
76 * Initialize the GDT subsystem. Called from autoconf().
77 */
78 void
gdt_init(void)79 gdt_init(void)
80 {
81 struct cpu_info *ci = &cpu_info_primary;
82
83 setsegment(&ci->ci_gdt[GCPU_SEL].sd, ci, sizeof(struct cpu_info)-1,
84 SDT_MEMRWA, SEL_KPL, 0, 0);
85
86 gdt_init_cpu(ci);
87 }
88
89 #ifdef MULTIPROCESSOR
90 /*
91 * Allocate shadow GDT for a slave cpu.
92 */
93 void
gdt_alloc_cpu(struct cpu_info * ci)94 gdt_alloc_cpu(struct cpu_info *ci)
95 {
96 bcopy(cpu_info_primary.ci_gdt, ci->ci_gdt, GDT_SIZE);
97 setsegment(&ci->ci_gdt[GCPU_SEL].sd, ci, sizeof(struct cpu_info)-1,
98 SDT_MEMRWA, SEL_KPL, 0, 0);
99 }
100 #endif /* MULTIPROCESSOR */
101
102
103 /*
104 * Load appropriate gdt descriptor; we better be running on *ci
105 * (for the most part, this is how a cpu knows who it is).
106 */
107 void
gdt_init_cpu(struct cpu_info * ci)108 gdt_init_cpu(struct cpu_info *ci)
109 {
110 struct region_descriptor region;
111
112 setsegment(&ci->ci_gdt[GTSS_SEL].sd, ci->ci_tss,
113 sizeof(*ci->ci_tss)-1, SDT_SYS386TSS, SEL_KPL, 0, 0);
114 setsegment(&ci->ci_gdt[GNMITSS_SEL].sd, ci->ci_nmi_tss,
115 sizeof(*ci->ci_nmi_tss)-1, SDT_SYS386TSS, SEL_KPL, 0, 0);
116
117 setregion(®ion, ci->ci_gdt, GDT_SIZE - 1);
118 lgdt(®ion);
119
120 ltr(GSEL(GTSS_SEL, SEL_KPL));
121 lldt(0);
122 }
123