Home | History | Annotate | Line # | Download | only in i386
      1 /*	$NetBSD: gdt.c,v 1.75 2026/08/23 22:08:40 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by John T. Kohl, by Charles M. Hannum, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: gdt.c,v 1.75 2026/08/23 22:08:40 riastradh Exp $");
     34 
     35 #include "opt_multiprocessor.h"
     36 #include "opt_xen.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/proc.h>
     41 #include <sys/mutex.h>
     42 #include <sys/cpu.h>
     43 
     44 #include <uvm/uvm.h>
     45 
     46 #include <machine/gdt.h>
     47 #include <machine/pmap_private.h>
     48 
     49 #define NSLOTS(sz)	\
     50 	(((sz) - DYNSEL_START) / sizeof(union descriptor))
     51 #define NDYNSLOTS	NSLOTS(MAXGDTSIZ)
     52 
     53 typedef struct {
     54 	bool busy[NDYNSLOTS];
     55 	size_t nslots;
     56 } gdt_bitmap_t;
     57 
     58 /* size of GDT in bytes */
     59 #ifdef XENPV
     60 const size_t gdt_size = FIRST_RESERVED_GDT_BYTE;
     61 #else
     62 const size_t gdt_size = MAXGDTSIZ;
     63 #endif
     64 
     65 /* bitmap of busy slots */
     66 static gdt_bitmap_t gdt_bitmap;
     67 
     68 #ifndef XENPV
     69 static int ldt_count;	/* number of LDTs */
     70 static int ldt_max = 1000;/* max number of LDTs */
     71 static void setgdt(int, const void *, size_t, int, int, int, int);
     72 static int gdt_get_slot(void);
     73 static void gdt_put_slot(int);
     74 #endif
     75 void gdt_init(void);
     76 
     77 void
     78 update_descriptor(union descriptor *table, union descriptor *entry)
     79 {
     80 #ifndef XENPV
     81 	*table = *entry;
     82 #else
     83 	paddr_t pa;
     84 	pt_entry_t *ptp;
     85 
     86 	ptp = kvtopte((vaddr_t)table);
     87 	pa = (*ptp & PTE_4KFRAME) | ((vaddr_t)table & ~PTE_4KFRAME);
     88 	if (HYPERVISOR_update_descriptor(pa, entry->raw[0], entry->raw[1]))
     89 		panic("HYPERVISOR_update_descriptor failed\n");
     90 #endif
     91 }
     92 
     93 #ifndef XENPV
     94 /*
     95  * Called on a newly-allocated GDT slot, so no race between CPUs.
     96  */
     97 static void
     98 setgdt(int slot, const void *base, size_t limit, int type, int dpl, int def32,
     99     int gran)
    100 {
    101 	struct segment_descriptor *sd;
    102 	CPU_INFO_ITERATOR cii;
    103 	struct cpu_info *ci;
    104 	int idx;
    105 
    106 	idx = IDXSEL(GDYNSEL(slot, SEL_KPL));
    107 	sd = &gdtstore[idx].sd;
    108 	setsegment(sd, base, limit, type, dpl, def32, gran);
    109 	for (CPU_INFO_FOREACH(cii, ci)) {
    110 		if (ci->ci_gdt != NULL)
    111 			update_descriptor(&ci->ci_gdt[idx],
    112 			    (union descriptor *)sd);
    113 	}
    114 }
    115 #endif
    116 
    117 /*
    118  * gdt_init()
    119  *
    120  *	Create a permanent Global Descriptor Table (GDT) for the
    121  *	primary CPU.  This replaces the second tepmorary GDT that was
    122  *	allocated in pmap_bootstrap with pmap_bootstrap_valloc and
    123  *	pmap_bootstrap_palloc -- which in turn replaced the initial
    124  *	temporary GDT allocated on the stack early at boot and
    125  *	initialized with initgdt.
    126  *
    127  *	1. Allocate permanent space for the primary CPU's GDT with
    128  *	   uvm_km(9).
    129  *
    130  *	2. Copy the temporary GDT's contents over.  See initgdt for the
    131  *	   original initialization; it was copied from the initial
    132  *	   temporary GDT to the second temporary GDT in init386.
    133  *
    134  *	3. Make sure the GCPU_SEL segment descriptor points to
    135  *	   &cpu_info_primary.
    136  *
    137  *	   XXX Is this necessary?  It appears to be redundant with
    138  *	   initgdt.
    139  *
    140  *	4. Load the permanent GDT address into the Global Descriptor
    141  *	   Table Register (GDTR) with LGDT (via gdt_init_cpu).
    142  */
    143 void
    144 gdt_init(void)
    145 {
    146 	union descriptor *old_gdt;
    147 	struct vm_page *pg;
    148 	vaddr_t va;
    149 	struct cpu_info *ci = &cpu_info_primary;
    150 
    151 	/* Initialize the global values */
    152 	memset(&gdt_bitmap.busy, 0, sizeof(gdt_bitmap.busy));
    153 	gdt_bitmap.nslots = NSLOTS(gdt_size);
    154 
    155 	old_gdt = gdtstore;
    156 
    157 	/* Allocate gdt_size bytes of memory. */
    158 	gdtstore = (union descriptor *)uvm_km_alloc(kernel_map, gdt_size, 0,
    159 	    UVM_KMF_VAONLY);
    160 	for (va = (vaddr_t)gdtstore; va < (vaddr_t)gdtstore + gdt_size;
    161 	    va += PAGE_SIZE) {
    162 		pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
    163 		if (pg == NULL) {
    164 			panic("gdt_init: no pages");
    165 		}
    166 		pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
    167 		    VM_PROT_READ | VM_PROT_WRITE, 0);
    168 	}
    169 	pmap_update(pmap_kernel());
    170 
    171 	/* Copy the initial bootstrap GDT into the new area. */
    172 	memcpy(gdtstore, old_gdt, NGDT * sizeof(gdtstore[0]));
    173 	ci->ci_gdt = gdtstore;
    174 	setsegment(&ci->ci_gdt[GCPU_SEL].sd, ci,
    175 	    sizeof(struct cpu_info) - 1, SDT_MEMRWA, SEL_KPL, 1, 0);
    176 
    177 	gdt_init_cpu(ci);
    178 }
    179 
    180 /*
    181  * Allocate shadow GDT for a secondary CPU. It contains the same values as the
    182  * GDT present in cpu0 (gdtstore).
    183  */
    184 void
    185 gdt_alloc_cpu(struct cpu_info *ci)
    186 {
    187 	uint64_t ticket;
    188 	struct vm_page *pg;
    189 	vaddr_t va;
    190 
    191 	ci->ci_gdt = (union descriptor *)uvm_km_alloc(kernel_map, gdt_size,
    192 	    0, UVM_KMF_VAONLY);
    193 	for (va = (vaddr_t)ci->ci_gdt; va < (vaddr_t)ci->ci_gdt + gdt_size;
    194 	    va += PAGE_SIZE) {
    195 		while (ticket = uvm_wait_prepare(),
    196 		    (pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO))
    197 		    == NULL) {
    198 			uvm_wait("gdt_alloc_cpu", ticket);
    199 		}
    200 		pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
    201 		    VM_PROT_READ | VM_PROT_WRITE, 0);
    202 	}
    203 	pmap_update(pmap_kernel());
    204 
    205 	memcpy(ci->ci_gdt, gdtstore, gdt_size);
    206 
    207 	setsegment(&ci->ci_gdt[GCPU_SEL].sd, ci,
    208 	    sizeof(struct cpu_info) - 1, SDT_MEMRWA, SEL_KPL, 1, 0);
    209 }
    210 
    211 /*
    212  * Load appropriate GDT descriptor into the currently running CPU, which must
    213  * be ci.
    214  */
    215 void
    216 gdt_init_cpu(struct cpu_info *ci)
    217 {
    218 #ifndef XENPV
    219 	struct region_descriptor region;
    220 
    221 	setregion(&region, ci->ci_gdt, gdt_size - 1);
    222 	lgdt(&region);
    223 #else
    224 	size_t len = roundup(gdt_size, PAGE_SIZE);
    225 	unsigned long frames[len >> PAGE_SHIFT];
    226 	vaddr_t va;
    227 	pt_entry_t *ptp;
    228 	size_t f;
    229 
    230 	for (va = (vaddr_t)ci->ci_gdt, f = 0;
    231 	    va < (vaddr_t)ci->ci_gdt + gdt_size;
    232 	    va += PAGE_SIZE, f++) {
    233 		KASSERT(va >= VM_MIN_KERNEL_ADDRESS);
    234 		ptp = kvtopte(va);
    235 		frames[f] = *ptp >> PAGE_SHIFT;
    236 
    237 		/*
    238 		 * Our own
    239 		 * 	pmap_pte_clearbits(ptp, PTE_W)
    240 		 * but without spl(), since %fs is not set up properly yet; ie
    241 		 * curcpu() won't work at this point and spl() will break.
    242 		 */
    243 		if (HYPERVISOR_update_va_mapping((vaddr_t)va,
    244 		    *ptp & ~PTE_W, UVMF_INVLPG) < 0) {
    245 			panic("%s page RO update failed.\n", __func__);
    246 		}
    247 	}
    248 
    249 	if (HYPERVISOR_set_gdt(frames, gdt_size / sizeof(gdtstore[0])))
    250 		panic("HYPERVISOR_set_gdt failed!\n");
    251 	lgdt_finish();
    252 #endif
    253 }
    254 
    255 #ifndef XENPV
    256 static int
    257 gdt_get_slot(void)
    258 {
    259 	size_t i;
    260 
    261 	KASSERT(mutex_owned(&cpu_lock));
    262 
    263 	for (i = 0; i < gdt_bitmap.nslots; i++) {
    264 		if (!gdt_bitmap.busy[i]) {
    265 			gdt_bitmap.busy[i] = true;
    266 			return (int)i;
    267 		}
    268 	}
    269 	panic("gdt_get_slot: out of memory");
    270 
    271 	/* NOTREACHED */
    272 	return 0;
    273 }
    274 
    275 static void
    276 gdt_put_slot(int slot)
    277 {
    278 	KASSERT(mutex_owned(&cpu_lock));
    279 	KASSERT(slot < gdt_bitmap.nslots);
    280 	gdt_bitmap.busy[slot] = false;
    281 }
    282 
    283 int
    284 tss_alloc(const struct i386tss *tss)
    285 {
    286 	int slot;
    287 
    288 	mutex_enter(&cpu_lock);
    289 	slot = gdt_get_slot();
    290 	setgdt(slot, tss, sizeof(struct i386tss) + IOMAPSIZE - 1,
    291 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
    292 	mutex_exit(&cpu_lock);
    293 
    294 	return GDYNSEL(slot, SEL_KPL);
    295 }
    296 
    297 void
    298 tss_free(int sel)
    299 {
    300 
    301 	mutex_enter(&cpu_lock);
    302 	gdt_put_slot(IDXDYNSEL(sel));
    303 	mutex_exit(&cpu_lock);
    304 }
    305 
    306 int
    307 ldt_alloc(void *ldtp, size_t len)
    308 {
    309 	int slot;
    310 
    311 	KASSERT(mutex_owned(&cpu_lock));
    312 
    313 	if (ldt_count >= ldt_max) {
    314 		return -1;
    315 	}
    316 	ldt_count++;
    317 
    318 	slot = gdt_get_slot();
    319 	setgdt(slot, ldtp, len - 1, SDT_SYSLDT, SEL_KPL, 0, 0);
    320 
    321 	return GDYNSEL(slot, SEL_KPL);
    322 }
    323 
    324 void
    325 ldt_free(int sel)
    326 {
    327 	int slot;
    328 
    329 	KASSERT(mutex_owned(&cpu_lock));
    330 	KASSERT(ldt_count > 0);
    331 
    332 	slot = IDXDYNSEL(sel);
    333 	gdt_put_slot(slot);
    334 	ldt_count--;
    335 }
    336 #endif
    337