#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: gdt.c,v 1.48 2022/08/20 23:48:50 riastradh Exp $");
#include "opt_multiprocessor.h"
#include "opt_xen.h"
#include "opt_user_ldt.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/mutex.h>
#include <sys/cpu.h>
#include <uvm/uvm.h>
#include <machine/gdt.h>
#include <machine/pmap_private.h>
#ifdef XENPV
#include <xen/hypervisor.h>
#endif
#define NSLOTS(sz) \
(((sz) - DYNSEL_START) / sizeof(struct sys_segment_descriptor))
#define NDYNSLOTS NSLOTS(MAXGDTSIZ)
typedef struct {
bool busy[NDYNSLOTS];
size_t nslots;
} gdt_bitmap_t;
#ifdef XENPV
const size_t gdt_size = FIRST_RESERVED_GDT_BYTE;
#else
const size_t gdt_size = MAXGDTSIZ;
#endif
static gdt_bitmap_t gdt_bitmap;
#if defined(USER_LDT) || !defined(XENPV)
static void set_sys_gdt(int, void *, size_t, int, int, int);
#endif
void
update_descriptor(void *tp, void *ep)
{
uint64_t *table, *entry;
table = tp;
entry = ep;
#ifndef XENPV
*table = *entry;
#else
paddr_t pa;
if (!pmap_extract_ma(pmap_kernel(), (vaddr_t)table, &pa) ||
HYPERVISOR_update_descriptor(pa, *entry))
panic("HYPERVISOR_update_descriptor failed");
#endif
}
#if defined(USER_LDT) || !defined(XENPV)
static void
set_sys_gdt(int slot, void *base, size_t limit, int type, int dpl, int gran)
{
union {
struct sys_segment_descriptor sd;
uint64_t bits[2];
} d;
CPU_INFO_ITERATOR cii;
struct cpu_info *ci;
int idx;
set_sys_segment(&d.sd, base, limit, type, dpl, gran);
idx = IDXSEL(GDYNSEL(slot, SEL_KPL));
for (CPU_INFO_FOREACH(cii, ci)) {
KASSERT(ci->ci_gdt != NULL);
update_descriptor(&ci->ci_gdt[idx + 0], &d.bits[0]);
update_descriptor(&ci->ci_gdt[idx + 1], &d.bits[1]);
}
}
#endif
void
gdt_init(void)
{
char *old_gdt;
struct cpu_info *ci = &cpu_info_primary;
memset(&gdt_bitmap.busy, 0, sizeof(gdt_bitmap.busy));
gdt_bitmap.nslots = NSLOTS(gdt_size);
old_gdt = gdtstore;
#ifdef __HAVE_PCPU_AREA
gdtstore = (char *)&pcpuarea->ent[cpu_index(ci)].gdt;
#else
struct vm_page *pg;
vaddr_t va;
gdtstore = (char *)uvm_km_alloc(kernel_map, gdt_size, 0,
UVM_KMF_VAONLY);
for (va = (vaddr_t)gdtstore; va < (vaddr_t)gdtstore + gdt_size;
va += PAGE_SIZE) {
pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
if (pg == NULL) {
panic("gdt_init: no pages");
}
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
VM_PROT_READ | VM_PROT_WRITE, 0);
}
pmap_update(pmap_kernel());
#endif
memcpy(gdtstore, old_gdt, DYNSEL_START);
ci->ci_gdt = (void *)gdtstore;
#ifndef XENPV
set_sys_segment(GDT_ADDR_SYS(gdtstore, GLDT_SEL), ldtstore,
LDT_SIZE - 1, SDT_SYSLDT, SEL_KPL, 0);
#endif
gdt_init_cpu(ci);
}
void
gdt_alloc_cpu(struct cpu_info *ci)
{
#ifdef __HAVE_PCPU_AREA
ci->ci_gdt = (union descriptor *)&pcpuarea->ent[cpu_index(ci)].gdt;
#else
struct vm_page *pg;
vaddr_t va;
ci->ci_gdt = (union descriptor *)uvm_km_alloc(kernel_map, gdt_size,
0, UVM_KMF_VAONLY);
for (va = (vaddr_t)ci->ci_gdt; va < (vaddr_t)ci->ci_gdt + gdt_size;
va += PAGE_SIZE) {
while ((pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO))
== NULL) {
uvm_wait("gdt_alloc_cpu");
}
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
VM_PROT_READ | VM_PROT_WRITE, 0);
}
pmap_update(pmap_kernel());
#endif
memcpy(ci->ci_gdt, gdtstore, gdt_size);
}
void
gdt_init_cpu(struct cpu_info *ci)
{
struct region_descriptor region;
KASSERT(curcpu() == ci);
setregion(®ion, ci->ci_gdt, (uint16_t)(gdt_size - 1));
lgdt(®ion);
}
#if !defined(XENPV) || defined(USER_LDT)
static int
gdt_get_slot(void)
{
size_t i;
KASSERT(mutex_owned(&cpu_lock));
for (i = 0; i < gdt_bitmap.nslots; i++) {
if (!gdt_bitmap.busy[i]) {
gdt_bitmap.busy[i] = true;
return (int)i;
}
}
panic("gdt_get_slot: out of memory");
return 0;
}
static void
gdt_put_slot(int slot)
{
KASSERT(mutex_owned(&cpu_lock));
KASSERT(slot < gdt_bitmap.nslots);
gdt_bitmap.busy[slot] = false;
}
#endif
int
tss_alloc(struct x86_64_tss *tss)
{
#ifndef XENPV
int slot;
mutex_enter(&cpu_lock);
slot = gdt_get_slot();
set_sys_gdt(slot, tss, sizeof(struct x86_64_tss) - 1, SDT_SYS386TSS,
SEL_KPL, 0);
mutex_exit(&cpu_lock);
return GDYNSEL(slot, SEL_KPL);
#else
return GSEL(GNULL_SEL, SEL_KPL);
#endif
}
void
tss_free(int sel)
{
#ifndef XENPV
mutex_enter(&cpu_lock);
gdt_put_slot(IDXDYNSEL(sel));
mutex_exit(&cpu_lock);
#else
KASSERT(sel == GSEL(GNULL_SEL, SEL_KPL));
#endif
}
#ifdef USER_LDT
int
ldt_alloc(void *ldtp, size_t len)
{
int slot;
KASSERT(mutex_owned(&cpu_lock));
slot = gdt_get_slot();
set_sys_gdt(slot, ldtp, len - 1, SDT_SYSLDT, SEL_KPL, 0);
return GDYNSEL(slot, SEL_KPL);
}
void
ldt_free(int sel)
{
int slot;
KASSERT(mutex_owned(&cpu_lock));
slot = IDXDYNSEL(sel);
gdt_put_slot(slot);
}
#endif
#ifdef XENPV
void
lgdt(struct region_descriptor *desc)
{
paddr_t frames[16];
size_t i;
vaddr_t va;
va = desc->rd_base + desc->rd_limit + 1;
memset((void *)va, 0, roundup(va, PAGE_SIZE) - va);
for (i = 0; i < roundup(desc->rd_limit, PAGE_SIZE) >> PAGE_SHIFT; i++) {
va = desc->rd_base + (i << PAGE_SHIFT);
frames[i] = ((paddr_t)xpmap_ptetomach((pt_entry_t *)va)) >>
PAGE_SHIFT;
pmap_pte_clearbits(kvtopte(va), PTE_W);
}
if (HYPERVISOR_set_gdt(frames, (desc->rd_limit + 1) >> 3))
panic("lgdt(): HYPERVISOR_set_gdt() failed");
lgdt_finish();
}
#endif