1 /*	$NetBSD: gdt.c,v 1.53 2012/02/24 08:06:07 cherry 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.53 2012/02/24 08:06:07 cherry 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 
48 #ifndef XEN
49 int gdt_size[1];	/* total number of GDT entries */
50 int gdt_count[1];	/* number of GDT entries in use */
51 int gdt_next[1];	/* next available slot for sweeping */
52 int gdt_free[1];	/* next free slot; terminated with GNULL_SEL */
53 #else
54 int gdt_size[2];	/* total number of GDT entries */
55 int gdt_count[2];	/* number of GDT entries in use */
56 int gdt_next[2];	/* next available slot for sweeping */
57 int gdt_free[2];	/* next free slot; terminated with GNULL_SEL */
58 #endif
59 
60 static int ldt_count;	/* number of LDTs */
61 static int ldt_max = 1000;/* max number of LDTs */
62 
63 void gdt_init(void);
64 void gdt_grow(int);
65 int gdt_get_slot1(int);
66 void gdt_put_slot1(int, int);
67 
68 void
update_descriptor(union descriptor * table,union descriptor * entry)69 update_descriptor(union descriptor *table, union descriptor *entry)
70 {
71 #ifndef XEN
72 	*table = *entry;
73 #else
74 	paddr_t pa;
75 	pt_entry_t *ptp;
76 
77 	ptp = kvtopte((vaddr_t)table);
78 	pa = (*ptp & PG_FRAME) | ((vaddr_t)table & ~PG_FRAME);
79 	if (HYPERVISOR_update_descriptor(pa, entry->raw[0], entry->raw[1]))
80 		panic("HYPERVISOR_update_descriptor failed\n");
81 #endif
82 }
83 
84 void
setgdt(int sel,const void * base,size_t limit,int type,int dpl,int def32,int gran)85 setgdt(int sel, const void *base, size_t limit,
86     int type, int dpl, int def32, int gran)
87 {
88 	struct segment_descriptor *sd = &gdt[sel].sd;
89 	CPU_INFO_ITERATOR cii;
90 	struct cpu_info *ci;
91 
92 #ifdef XEN
93 	if (type == SDT_SYS386TSS) {
94 		/* printk("XXX TSS descriptor not supported in GDT\n"); */
95 		return;
96 	}
97 #endif
98 	setsegment(sd, base, limit, type, dpl, def32, gran);
99 	for (CPU_INFO_FOREACH(cii, ci)) {
100 		if (ci->ci_gdt != NULL)
101 			update_descriptor(&ci->ci_gdt[sel],
102 			    (union descriptor *)sd);
103 	}
104 }
105 
106 /*
107  * Initialize the GDT subsystem.  Called from autoconf().
108  */
109 void
gdt_init(void)110 gdt_init(void)
111 {
112 	size_t max_len, min_len;
113 	union descriptor *old_gdt;
114 	struct vm_page *pg;
115 	vaddr_t va;
116 	struct cpu_info *ci = &cpu_info_primary;
117 
118 	max_len = MAXGDTSIZ * sizeof(gdt[0]);
119 	min_len = MINGDTSIZ * sizeof(gdt[0]);
120 
121 	gdt_size[0] = MINGDTSIZ;
122 	gdt_count[0] = NGDT;
123 	gdt_next[0] = NGDT;
124 	gdt_free[0] = GNULL_SEL;
125 #ifdef XEN
126 	max_len = max_len * 2;
127 	gdt_size[1] = 0;
128 	gdt_count[1] = MAXGDTSIZ;
129 	gdt_next[1] = MAXGDTSIZ;
130 	gdt_free[1] = GNULL_SEL;
131 #endif
132 
133 	old_gdt = gdt;
134 	gdt = (union descriptor *)uvm_km_alloc(kernel_map, max_len,
135 	    0, UVM_KMF_VAONLY);
136 	for (va = (vaddr_t)gdt; va < (vaddr_t)gdt + min_len; va += PAGE_SIZE) {
137 		pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
138 		if (pg == NULL) {
139 			panic("gdt_init: no pages");
140 		}
141 		pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
142 		    VM_PROT_READ | VM_PROT_WRITE, 0);
143 	}
144 	pmap_update(pmap_kernel());
145 	memcpy(gdt, old_gdt, NGDT * sizeof(gdt[0]));
146 	ci->ci_gdt = gdt;
147 	setsegment(&ci->ci_gdt[GCPU_SEL].sd, ci, 0xfffff,
148 	    SDT_MEMRWA, SEL_KPL, 1, 1);
149 
150 	gdt_init_cpu(ci);
151 }
152 
153 /*
154  * Allocate shadow GDT for a slave CPU.
155  */
156 void
gdt_alloc_cpu(struct cpu_info * ci)157 gdt_alloc_cpu(struct cpu_info *ci)
158 {
159 	int max_len = MAXGDTSIZ * sizeof(gdt[0]);
160 	int min_len = MINGDTSIZ * sizeof(gdt[0]);
161 	struct vm_page *pg;
162 	vaddr_t va;
163 
164 	ci->ci_gdt = (union descriptor *)uvm_km_alloc(kernel_map, max_len,
165 	    0, UVM_KMF_VAONLY);
166 	for (va = (vaddr_t)ci->ci_gdt; va < (vaddr_t)ci->ci_gdt + min_len;
167 	    va += PAGE_SIZE) {
168 		while ((pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO))
169 		    == NULL) {
170 			uvm_wait("gdt_alloc_cpu");
171 		}
172 		pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
173 		    VM_PROT_READ | VM_PROT_WRITE, 0);
174 	}
175 	pmap_update(pmap_kernel());
176 	memset(ci->ci_gdt, 0, min_len);
177 	memcpy(ci->ci_gdt, gdt, gdt_count[0] * sizeof(gdt[0]));
178 	setsegment(&ci->ci_gdt[GCPU_SEL].sd, ci, 0xfffff,
179 	    SDT_MEMRWA, SEL_KPL, 1, 1);
180 }
181 
182 
183 /*
184  * Load appropriate gdt descriptor; we better be running on *ci
185  * (for the most part, this is how a CPU knows who it is).
186  */
187 void
gdt_init_cpu(struct cpu_info * ci)188 gdt_init_cpu(struct cpu_info *ci)
189 {
190 #ifndef XEN
191 	struct region_descriptor region;
192 	size_t max_len;
193 
194 	max_len = MAXGDTSIZ * sizeof(gdt[0]);
195 	setregion(&region, ci->ci_gdt, max_len - 1);
196 	lgdt(&region);
197 #else
198 	size_t len = gdt_size[0] * sizeof(gdt[0]);
199 	unsigned long frames[len >> PAGE_SHIFT];
200 	vaddr_t va;
201 	pt_entry_t *ptp;
202 	int f;
203 
204 	for (va = (vaddr_t)ci->ci_gdt, f = 0;
205 	     va < (vaddr_t)ci->ci_gdt + len;
206 	     va += PAGE_SIZE, f++) {
207 		KASSERT(va >= VM_MIN_KERNEL_ADDRESS);
208 		ptp = kvtopte(va);
209 		frames[f] = *ptp >> PAGE_SHIFT;
210 		{
211 		   /*
212 		    * pmap_pte_clearbits(ptp, PG_RW);
213 		    * but without spl(), since %fs is not setup
214 		    * properly yet, ie; curcpu() won't work at this
215 		    * point and spl() will break.
216 		    */
217 			if (HYPERVISOR_update_va_mapping((vaddr_t)va,
218 				*ptp & ~PG_RW, UVMF_INVLPG) < 0) {
219 				panic("%s page RO update failed.\n", __func__);
220 			}
221 		}
222 	}
223 
224 	if (HYPERVISOR_set_gdt(frames, gdt_size[0]))
225 		panic("HYPERVISOR_set_gdt failed!\n");
226 	lgdt_finish();
227 #endif
228 }
229 
230 #if defined(MULTIPROCESSOR) && !defined(XEN)
231 
232 void
gdt_reload_cpu(struct cpu_info * ci)233 gdt_reload_cpu(struct cpu_info *ci)
234 {
235 	struct region_descriptor region;
236 	size_t max_len;
237 
238 	max_len = MAXGDTSIZ * sizeof(gdt[0]);
239 	setregion(&region, ci->ci_gdt, max_len - 1);
240 	lgdt(&region);
241 }
242 #endif
243 
244 
245 /*
246  * Grow the GDT.
247  */
248 void
gdt_grow(int which)249 gdt_grow(int which)
250 {
251 	size_t old_len, new_len;
252 	CPU_INFO_ITERATOR cii;
253 	struct cpu_info *ci;
254 	struct vm_page *pg;
255 	vaddr_t va;
256 
257 	old_len = gdt_size[which] * sizeof(gdt[0]);
258 	gdt_size[which] <<= 1;
259 	new_len = old_len << 1;
260 
261 #ifdef XEN
262 	if (which != 0) {
263 		size_t max_len = MAXGDTSIZ * sizeof(gdt[0]);
264 		if (old_len == 0) {
265 			gdt_size[which] = MINGDTSIZ;
266 			new_len = gdt_size[which] * sizeof(gdt[0]);
267 		}
268 		for (CPU_INFO_FOREACH(cii, ci)) {
269 			for(va = (vaddr_t)(ci->ci_gdt) + old_len + max_len;
270 			    va < (vaddr_t)(ci->ci_gdt) + new_len + max_len;
271 			    va += PAGE_SIZE) {
272 				while ((pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO))
273 				       == NULL) {
274 					uvm_wait("gdt_grow");
275 				}
276 				pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
277 					       VM_PROT_READ | VM_PROT_WRITE, 0);
278 			}
279 		}
280 		return;
281 	}
282 #endif
283 
284 	for (CPU_INFO_FOREACH(cii, ci)) {
285 		for (va = (vaddr_t)(ci->ci_gdt) + old_len;
286 		     va < (vaddr_t)(ci->ci_gdt) + new_len;
287 		     va += PAGE_SIZE) {
288 			while ((pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO)) ==
289 			    NULL) {
290 				uvm_wait("gdt_grow");
291 			}
292 			pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
293 			    VM_PROT_READ | VM_PROT_WRITE, 0);
294 		}
295 	}
296 
297 	pmap_update(pmap_kernel());
298 }
299 
300 /*
301  * Allocate a GDT slot as follows:
302  * 1) If there are entries on the free list, use those.
303  * 2) If there are fewer than gdt_size entries in use, there are free slots
304  *    near the end that we can sweep through.
305  * 3) As a last resort, we increase the size of the GDT, and sweep through
306  *    the new slots.
307  */
308 
309 int
gdt_get_slot(void)310 gdt_get_slot(void)
311 {
312 
313 	KASSERT(mutex_owned(&cpu_lock));
314 
315 	return gdt_get_slot1(0);
316 }
317 
318 int
gdt_get_slot1(int which)319 gdt_get_slot1(int which)
320 {
321 	int slot;
322 	size_t offset;
323 
324 	KASSERT(mutex_owned(&cpu_lock));
325 
326 	if (gdt_free[which] != GNULL_SEL) {
327 		slot = gdt_free[which];
328 		gdt_free[which] = gdt[slot].gd.gd_selector;
329 	} else {
330 		offset = which * MAXGDTSIZ * sizeof(gdt[0]);
331 		if (gdt_next[which] != gdt_count[which] + offset)
332 			panic("gdt_get_slot botch 1");
333 		if (gdt_next[which] - offset >= gdt_size[which]) {
334 			if (gdt_size[which] >= MAXGDTSIZ)
335 				panic("gdt_get_slot botch 2");
336 			gdt_grow(which);
337 		}
338 		slot = gdt_next[which]++;
339 	}
340 
341 	gdt_count[which]++;
342 	return (slot);
343 }
344 
345 /*
346  * Deallocate a GDT slot, putting it on the free list.
347  */
348 void
gdt_put_slot(int slot)349 gdt_put_slot(int slot)
350 {
351 
352 	KASSERT(mutex_owned(&cpu_lock));
353 
354 	gdt_put_slot1(slot, 0);
355 }
356 
357 void
gdt_put_slot1(int slot,int which)358 gdt_put_slot1(int slot, int which)
359 {
360 	union descriptor d;
361 	d.raw[0] = 0;
362 	d.raw[1] = 0;
363 
364 	KASSERT(mutex_owned(&cpu_lock));
365 
366 	gdt_count[which]--;
367 
368 	d.gd.gd_type = SDT_SYSNULL;
369 	d.gd.gd_selector = gdt_free[which];
370 	update_descriptor(&gdt[slot], &d);
371 
372 	gdt_free[which] = slot;
373 }
374 
375 #ifndef XEN
376 int
tss_alloc(const struct i386tss * tss)377 tss_alloc(const struct i386tss *tss)
378 {
379 	int slot;
380 
381 	mutex_enter(&cpu_lock);
382 	slot = gdt_get_slot();
383 	setgdt(slot, tss, sizeof(struct i386tss) + IOMAPSIZE - 1,
384 	    SDT_SYS386TSS, SEL_KPL, 0, 0);
385 	mutex_exit(&cpu_lock);
386 
387 	return GSEL(slot, SEL_KPL);
388 }
389 
390 void
tss_free(int sel)391 tss_free(int sel)
392 {
393 
394 	mutex_enter(&cpu_lock);
395 	gdt_put_slot(IDXSEL(sel));
396 	mutex_exit(&cpu_lock);
397 }
398 #endif
399 
400 int
ldt_alloc(union descriptor * ldtp,size_t len)401 ldt_alloc(union descriptor *ldtp, size_t len)
402 {
403 	int slot;
404 
405 	KASSERT(mutex_owned(&cpu_lock));
406 
407 	if (ldt_count >= ldt_max) {
408 		return -1;
409 	}
410 	ldt_count++;
411 
412 #ifndef XEN
413 	slot = gdt_get_slot();
414 	setgdt(slot, ldtp, len - 1, SDT_SYSLDT, SEL_KPL, 0, 0);
415 #else
416 	slot = gdt_get_slot1(1);
417 	cpu_info_primary.ci_gdt[slot].ld.ld_base = (uint32_t)ldtp;
418 	cpu_info_primary.ci_gdt[slot].ld.ld_entries =
419 	    len / sizeof(union descriptor);
420 #endif
421 
422 	return GSEL(slot, SEL_KPL);
423 }
424 
425 void
ldt_free(int sel)426 ldt_free(int sel)
427 {
428 	int slot;
429 
430 	KASSERT(mutex_owned(&cpu_lock));
431 	KASSERT(ldt_count > 0);
432 
433 	slot = IDXSEL(sel);
434 #ifndef XEN
435 	gdt_put_slot(slot);
436 #else
437 	gdt_put_slot1(slot, 1);
438 #endif
439 	ldt_count--;
440 }
441