xref: /titanic_41/usr/src/uts/intel/ia32/os/desctbls.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Copyright (c) 1992 Terrence R. Lambert.
31  * Copyright (c) 1990 The Regents of the University of California.
32  * All rights reserved.
33  *
34  * This code is derived from software contributed to Berkeley by
35  * William Jolitz.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	from: @(#)machdep.c	7.4 (Berkeley) 6/3/91
66  */
67 
68 #include <sys/types.h>
69 #include <sys/sysmacros.h>
70 #include <sys/tss.h>
71 #include <sys/segments.h>
72 #include <sys/trap.h>
73 #include <sys/cpuvar.h>
74 #include <sys/bootconf.h>
75 #include <sys/x86_archext.h>
76 #include <sys/controlregs.h>
77 #include <sys/archsystm.h>
78 #include <sys/machsystm.h>
79 #include <sys/kobj.h>
80 #include <sys/cmn_err.h>
81 #include <sys/reboot.h>
82 #include <sys/kdi.h>
83 #include <sys/mach_mmu.h>
84 #include <sys/systm.h>
85 
86 #ifdef __xpv
87 #include <sys/hypervisor.h>
88 #include <vm/as.h>
89 #endif
90 
91 #include <sys/promif.h>
92 #include <sys/bootinfo.h>
93 #include <vm/kboot_mmu.h>
94 #include <vm/hat_pte.h>
95 
96 /*
97  * cpu0 and default tables and structures.
98  */
99 user_desc_t	*gdt0;
100 #if !defined(__xpv)
101 desctbr_t	gdt0_default_r;
102 #endif
103 
104 gate_desc_t	*idt0; 		/* interrupt descriptor table */
105 #if defined(__i386)
106 desctbr_t	idt0_default_r;		/* describes idt0 in IDTR format */
107 #endif
108 
109 struct tss	*ktss0;			/* kernel task state structure */
110 
111 #if defined(__i386)
112 struct tss	*dftss0;		/* #DF double-fault exception */
113 #endif	/* __i386 */
114 
115 user_desc_t	zero_udesc;		/* base zero user desc native procs */
116 user_desc_t	null_udesc;		/* null user descriptor */
117 system_desc_t	null_sdesc;		/* null system descriptor */
118 
119 #if defined(__amd64)
120 user_desc_t	zero_u32desc;		/* 32-bit compatibility procs */
121 #endif	/* __amd64 */
122 
123 #if defined(__amd64)
124 user_desc_t	ucs_on;
125 user_desc_t	ucs_off;
126 user_desc_t	ucs32_on;
127 user_desc_t	ucs32_off;
128 #endif	/* __amd64 */
129 
130 #pragma	align	16(dblfault_stack0)
131 char		dblfault_stack0[DEFAULTSTKSZ];
132 
133 extern void	fast_null(void);
134 extern hrtime_t	get_hrtime(void);
135 extern hrtime_t	gethrvtime(void);
136 extern hrtime_t	get_hrestime(void);
137 extern uint64_t	getlgrp(void);
138 
139 void (*(fasttable[]))(void) = {
140 	fast_null,			/* T_FNULL routine */
141 	fast_null,			/* T_FGETFP routine (initially null) */
142 	fast_null,			/* T_FSETFP routine (initially null) */
143 	(void (*)())get_hrtime,		/* T_GETHRTIME */
144 	(void (*)())gethrvtime,		/* T_GETHRVTIME */
145 	(void (*)())get_hrestime,	/* T_GETHRESTIME */
146 	(void (*)())getlgrp		/* T_GETLGRP */
147 };
148 
149 /*
150  * Structure containing pre-computed descriptors to allow us to temporarily
151  * interpose on a standard handler.
152  */
153 struct interposing_handler {
154 	int ih_inum;
155 	gate_desc_t ih_interp_desc;
156 	gate_desc_t ih_default_desc;
157 };
158 
159 /*
160  * The brand infrastructure interposes on two handlers, and we use one as a
161  * NULL signpost.
162  */
163 static struct interposing_handler brand_tbl[3];
164 
165 /*
166  * software prototypes for default local descriptor table
167  */
168 
169 /*
170  * Routines for loading segment descriptors in format the hardware
171  * can understand.
172  */
173 
174 #if defined(__amd64)
175 
176 /*
177  * In long mode we have the new L or long mode attribute bit
178  * for code segments. Only the conforming bit in type is used along
179  * with descriptor priority and present bits. Default operand size must
180  * be zero when in long mode. In 32-bit compatibility mode all fields
181  * are treated as in legacy mode. For data segments while in long mode
182  * only the present bit is loaded.
183  */
184 void
185 set_usegd(user_desc_t *dp, uint_t lmode, void *base, size_t size,
186     uint_t type, uint_t dpl, uint_t gran, uint_t defopsz)
187 {
188 	ASSERT(lmode == SDP_SHORT || lmode == SDP_LONG);
189 
190 	/*
191 	 * 64-bit long mode.
192 	 */
193 	if (lmode == SDP_LONG)
194 		dp->usd_def32 = 0;		/* 32-bit operands only */
195 	else
196 		/*
197 		 * 32-bit compatibility mode.
198 		 */
199 		dp->usd_def32 = defopsz;	/* 0 = 16, 1 = 32-bit ops */
200 
201 	dp->usd_long = lmode;	/* 64-bit mode */
202 	dp->usd_type = type;
203 	dp->usd_dpl = dpl;
204 	dp->usd_p = 1;
205 	dp->usd_gran = gran;		/* 0 = bytes, 1 = pages */
206 
207 	dp->usd_lobase = (uintptr_t)base;
208 	dp->usd_midbase = (uintptr_t)base >> 16;
209 	dp->usd_hibase = (uintptr_t)base >> (16 + 8);
210 	dp->usd_lolimit = size;
211 	dp->usd_hilimit = (uintptr_t)size >> 16;
212 }
213 
214 #elif defined(__i386)
215 
216 /*
217  * Install user segment descriptor for code and data.
218  */
219 void
220 set_usegd(user_desc_t *dp, void *base, size_t size, uint_t type,
221     uint_t dpl, uint_t gran, uint_t defopsz)
222 {
223 	dp->usd_lolimit = size;
224 	dp->usd_hilimit = (uintptr_t)size >> 16;
225 
226 	dp->usd_lobase = (uintptr_t)base;
227 	dp->usd_midbase = (uintptr_t)base >> 16;
228 	dp->usd_hibase = (uintptr_t)base >> (16 + 8);
229 
230 	dp->usd_type = type;
231 	dp->usd_dpl = dpl;
232 	dp->usd_p = 1;
233 	dp->usd_def32 = defopsz;	/* 0 = 16, 1 = 32 bit operands */
234 	dp->usd_gran = gran;		/* 0 = bytes, 1 = pages */
235 }
236 
237 #endif	/* __i386 */
238 
239 /*
240  * Install system segment descriptor for LDT and TSS segments.
241  */
242 
243 #if defined(__amd64)
244 
245 void
246 set_syssegd(system_desc_t *dp, void *base, size_t size, uint_t type,
247     uint_t dpl)
248 {
249 	dp->ssd_lolimit = size;
250 	dp->ssd_hilimit = (uintptr_t)size >> 16;
251 
252 	dp->ssd_lobase = (uintptr_t)base;
253 	dp->ssd_midbase = (uintptr_t)base >> 16;
254 	dp->ssd_hibase = (uintptr_t)base >> (16 + 8);
255 	dp->ssd_hi64base = (uintptr_t)base >> (16 + 8 + 8);
256 
257 	dp->ssd_type = type;
258 	dp->ssd_zero1 = 0;	/* must be zero */
259 	dp->ssd_zero2 = 0;
260 	dp->ssd_dpl = dpl;
261 	dp->ssd_p = 1;
262 	dp->ssd_gran = 0;	/* force byte units */
263 }
264 
265 void *
266 get_ssd_base(system_desc_t *dp)
267 {
268 	uintptr_t	base;
269 
270 	base = (uintptr_t)dp->ssd_lobase |
271 	    (uintptr_t)dp->ssd_midbase << 16 |
272 	    (uintptr_t)dp->ssd_hibase << (16 + 8) |
273 	    (uintptr_t)dp->ssd_hi64base << (16 + 8 + 8);
274 	return ((void *)base);
275 }
276 
277 #elif defined(__i386)
278 
279 void
280 set_syssegd(system_desc_t *dp, void *base, size_t size, uint_t type,
281     uint_t dpl)
282 {
283 	dp->ssd_lolimit = size;
284 	dp->ssd_hilimit = (uintptr_t)size >> 16;
285 
286 	dp->ssd_lobase = (uintptr_t)base;
287 	dp->ssd_midbase = (uintptr_t)base >> 16;
288 	dp->ssd_hibase = (uintptr_t)base >> (16 + 8);
289 
290 	dp->ssd_type = type;
291 	dp->ssd_zero = 0;	/* must be zero */
292 	dp->ssd_dpl = dpl;
293 	dp->ssd_p = 1;
294 	dp->ssd_gran = 0;	/* force byte units */
295 }
296 
297 void *
298 get_ssd_base(system_desc_t *dp)
299 {
300 	uintptr_t	base;
301 
302 	base = (uintptr_t)dp->ssd_lobase |
303 	    (uintptr_t)dp->ssd_midbase << 16 |
304 	    (uintptr_t)dp->ssd_hibase << (16 + 8);
305 	return ((void *)base);
306 }
307 
308 #endif	/* __i386 */
309 
310 /*
311  * Install gate segment descriptor for interrupt, trap, call and task gates.
312  */
313 
314 #if defined(__amd64)
315 
316 void
317 set_gatesegd(gate_desc_t *dp, void (*func)(void), selector_t sel,
318     uint_t type, uint_t dpl)
319 {
320 	dp->sgd_looffset = (uintptr_t)func;
321 	dp->sgd_hioffset = (uintptr_t)func >> 16;
322 	dp->sgd_hi64offset = (uintptr_t)func >> (16 + 16);
323 
324 	dp->sgd_selector =  (uint16_t)sel;
325 
326 	/*
327 	 * For 64 bit native we use the IST stack mechanism
328 	 * for double faults. All other traps use the CPL = 0
329 	 * (tss_rsp0) stack.
330 	 */
331 #if !defined(__xpv)
332 	if (type == T_DBLFLT)
333 		dp->sgd_ist = 1;
334 	else
335 #endif
336 		dp->sgd_ist = 0;
337 
338 	dp->sgd_type = type;
339 	dp->sgd_dpl = dpl;
340 	dp->sgd_p = 1;
341 }
342 
343 #elif defined(__i386)
344 
345 void
346 set_gatesegd(gate_desc_t *dp, void (*func)(void), selector_t sel,
347     uint_t type, uint_t dpl)
348 {
349 	dp->sgd_looffset = (uintptr_t)func;
350 	dp->sgd_hioffset = (uintptr_t)func >> 16;
351 
352 	dp->sgd_selector =  (uint16_t)sel;
353 	dp->sgd_stkcpy = 0;	/* always zero bytes */
354 	dp->sgd_type = type;
355 	dp->sgd_dpl = dpl;
356 	dp->sgd_p = 1;
357 }
358 
359 #endif	/* __i386 */
360 
361 /*
362  * Updates a single user descriptor in the the GDT of the current cpu.
363  * Caller is responsible for preventing cpu migration.
364  */
365 
366 void
367 gdt_update_usegd(uint_t sidx, user_desc_t *udp)
368 {
369 #if defined(__xpv)
370 
371 	uint64_t dpa = CPU->cpu_m.mcpu_gdtpa + sizeof (*udp) * sidx;
372 
373 	if (HYPERVISOR_update_descriptor(pa_to_ma(dpa), *(uint64_t *)udp))
374 		panic("gdt_update_usegd: HYPERVISOR_update_descriptor");
375 
376 #else	/* __xpv */
377 
378 	CPU->cpu_gdt[sidx] = *udp;
379 
380 #endif	/* __xpv */
381 }
382 
383 /*
384  * Writes single descriptor pointed to by udp into a processes
385  * LDT entry pointed to by ldp.
386  */
387 int
388 ldt_update_segd(user_desc_t *ldp, user_desc_t *udp)
389 {
390 #if defined(__xpv)
391 
392 	uint64_t dpa;
393 
394 	dpa = mmu_ptob(hat_getpfnum(kas.a_hat, (caddr_t)ldp)) |
395 	    ((uintptr_t)ldp & PAGEOFFSET);
396 
397 	/*
398 	 * The hypervisor is a little more restrictive about what it
399 	 * supports in the LDT.
400 	 */
401 	if (HYPERVISOR_update_descriptor(pa_to_ma(dpa), *(uint64_t *)udp) != 0)
402 		return (EINVAL);
403 
404 #else	/* __xpv */
405 
406 	*ldp = *udp;
407 
408 #endif	/* __xpv */
409 	return (0);
410 }
411 
412 #if defined(__xpv)
413 
414 /*
415  * Converts hw format gate descriptor into pseudo-IDT format for the hypervisor.
416  * Returns true if a valid entry was written.
417  */
418 int
419 xen_idt_to_trap_info(uint_t vec, gate_desc_t *sgd, void *ti_arg)
420 {
421 	trap_info_t *ti = ti_arg;	/* XXPV	Aargh - segments.h comment */
422 
423 	/*
424 	 * skip holes in the IDT
425 	 */
426 	if (GATESEG_GETOFFSET(sgd) == 0)
427 		return (0);
428 
429 	ASSERT(sgd->sgd_type == SDT_SYSIGT);
430 	ti->vector = vec;
431 	TI_SET_DPL(ti, sgd->sgd_dpl);
432 
433 	/*
434 	 * Is this an interrupt gate?
435 	 */
436 	if (sgd->sgd_type == SDT_SYSIGT) {
437 		/* LINTED */
438 		TI_SET_IF(ti, 1);
439 	}
440 	ti->cs = sgd->sgd_selector;
441 #if defined(__amd64)
442 	ti->cs |= SEL_KPL;	/* force into ring 3. see KCS_SEL  */
443 #endif
444 	ti->address = GATESEG_GETOFFSET(sgd);
445 	return (1);
446 }
447 
448 /*
449  * Convert a single hw format gate descriptor and write it into our virtual IDT.
450  */
451 void
452 xen_idt_write(gate_desc_t *sgd, uint_t vec)
453 {
454 	trap_info_t trapinfo[2];
455 
456 	bzero(trapinfo, sizeof (trapinfo));
457 	if (xen_idt_to_trap_info(vec, sgd, &trapinfo[0]) == 0)
458 		return;
459 	if (xen_set_trap_table(trapinfo) != 0)
460 		panic("xen_idt_write: xen_set_trap_table() failed");
461 }
462 
463 #endif	/* __xpv */
464 
465 #if defined(__amd64)
466 
467 /*
468  * Build kernel GDT.
469  */
470 
471 static void
472 init_gdt_common(user_desc_t *gdt)
473 {
474 	int i;
475 
476 	/*
477 	 * 64-bit kernel code segment.
478 	 */
479 	set_usegd(&gdt[GDT_KCODE], SDP_LONG, NULL, 0, SDT_MEMERA, SEL_KPL,
480 	    SDP_PAGES, SDP_OP32);
481 
482 	/*
483 	 * 64-bit kernel data segment. The limit attribute is ignored in 64-bit
484 	 * mode, but we set it here to 0xFFFF so that we can use the SYSRET
485 	 * instruction to return from system calls back to 32-bit applications.
486 	 * SYSRET doesn't update the base, limit, or attributes of %ss or %ds
487 	 * descriptors. We therefore must ensure that the kernel uses something,
488 	 * though it will be ignored by hardware, that is compatible with 32-bit
489 	 * apps. For the same reason we must set the default op size of this
490 	 * descriptor to 32-bit operands.
491 	 */
492 	set_usegd(&gdt[GDT_KDATA], SDP_LONG, NULL, -1, SDT_MEMRWA,
493 	    SEL_KPL, SDP_PAGES, SDP_OP32);
494 	gdt[GDT_KDATA].usd_def32 = 1;
495 
496 	/*
497 	 * 64-bit user code segment.
498 	 */
499 	set_usegd(&gdt[GDT_UCODE], SDP_LONG, NULL, 0, SDT_MEMERA, SEL_UPL,
500 	    SDP_PAGES, SDP_OP32);
501 
502 	/*
503 	 * 32-bit user code segment.
504 	 */
505 	set_usegd(&gdt[GDT_U32CODE], SDP_SHORT, NULL, -1, SDT_MEMERA,
506 	    SEL_UPL, SDP_PAGES, SDP_OP32);
507 
508 	/*
509 	 * See gdt_ucode32() and gdt_ucode_native().
510 	 */
511 	ucs_on = ucs_off = gdt[GDT_UCODE];
512 	ucs_off.usd_p = 0;	/* forces #np fault */
513 
514 	ucs32_on = ucs32_off = gdt[GDT_U32CODE];
515 	ucs32_off.usd_p = 0;	/* forces #np fault */
516 
517 	/*
518 	 * 32 and 64 bit data segments can actually share the same descriptor.
519 	 * In long mode only the present bit is checked but all other fields
520 	 * are loaded. But in compatibility mode all fields are interpreted
521 	 * as in legacy mode so they must be set correctly for a 32-bit data
522 	 * segment.
523 	 */
524 	set_usegd(&gdt[GDT_UDATA], SDP_SHORT, NULL, -1, SDT_MEMRWA, SEL_UPL,
525 	    SDP_PAGES, SDP_OP32);
526 
527 #if !defined(__xpv)
528 
529 	/*
530 	 * The 64-bit kernel has no default LDT. By default, the LDT descriptor
531 	 * in the GDT is 0.
532 	 */
533 
534 	/*
535 	 * Kernel TSS
536 	 */
537 	set_syssegd((system_desc_t *)&gdt[GDT_KTSS], ktss0,
538 	    sizeof (*ktss0) - 1, SDT_SYSTSS, SEL_KPL);
539 
540 #endif	/* !__xpv */
541 
542 	/*
543 	 * Initialize fs and gs descriptors for 32 bit processes.
544 	 * Only attributes and limits are initialized, the effective
545 	 * base address is programmed via fsbase/gsbase.
546 	 */
547 	set_usegd(&gdt[GDT_LWPFS], SDP_SHORT, NULL, -1, SDT_MEMRWA,
548 	    SEL_UPL, SDP_PAGES, SDP_OP32);
549 	set_usegd(&gdt[GDT_LWPGS], SDP_SHORT, NULL, -1, SDT_MEMRWA,
550 	    SEL_UPL, SDP_PAGES, SDP_OP32);
551 
552 	/*
553 	 * Initialize the descriptors set aside for brand usage.
554 	 * Only attributes and limits are initialized.
555 	 */
556 	for (i = GDT_BRANDMIN; i <= GDT_BRANDMAX; i++)
557 		set_usegd(&gdt0[i], SDP_SHORT, NULL, -1, SDT_MEMRWA,
558 		    SEL_UPL, SDP_PAGES, SDP_OP32);
559 
560 	/*
561 	 * Initialize convenient zero base user descriptors for clearing
562 	 * lwp private %fs and %gs descriptors in GDT. See setregs() for
563 	 * an example.
564 	 */
565 	set_usegd(&zero_udesc, SDP_LONG, 0, 0, SDT_MEMRWA, SEL_UPL,
566 	    SDP_BYTES, SDP_OP32);
567 	set_usegd(&zero_u32desc, SDP_SHORT, 0, -1, SDT_MEMRWA, SEL_UPL,
568 	    SDP_PAGES, SDP_OP32);
569 }
570 
571 #if defined(__xpv)
572 
573 static user_desc_t *
574 init_gdt(void)
575 {
576 	uint64_t gdtpa;
577 	ulong_t ma[1];		/* XXPV should be a memory_t */
578 	ulong_t addr;
579 
580 #if !defined(__lint)
581 	/*
582 	 * Our gdt is never larger than a single page.
583 	 */
584 	ASSERT((sizeof (*gdt0) * NGDT) <= PAGESIZE);
585 #endif
586 	gdt0 = (user_desc_t *)BOP_ALLOC(bootops, (caddr_t)GDT_VA,
587 	    PAGESIZE, PAGESIZE);
588 	bzero(gdt0, PAGESIZE);
589 
590 	init_gdt_common(gdt0);
591 
592 	/*
593 	 * XXX Since we never invoke kmdb until after the kernel takes
594 	 * over the descriptor tables why not have it use the kernel's
595 	 * selectors?
596 	 */
597 	if (boothowto & RB_DEBUG) {
598 		set_usegd(&gdt0[GDT_B32DATA], SDP_LONG, NULL, -1, SDT_MEMRWA,
599 		    SEL_KPL, SDP_PAGES, SDP_OP32);
600 		set_usegd(&gdt0[GDT_B64CODE], SDP_LONG, NULL, -1, SDT_MEMERA,
601 		    SEL_KPL, SDP_PAGES, SDP_OP32);
602 	}
603 
604 	/*
605 	 * Clear write permission for page containing the gdt and install it.
606 	 */
607 	gdtpa = pfn_to_pa(va_to_pfn(gdt0));
608 	ma[0] = (ulong_t)(pa_to_ma(gdtpa) >> PAGESHIFT);
609 	kbm_read_only((uintptr_t)gdt0, gdtpa);
610 	xen_set_gdt(ma, NGDT);
611 
612 	/*
613 	 * Reload the segment registers to use the new GDT.
614 	 * On 64-bit, fixup KCS_SEL to be in ring 3.
615 	 * See KCS_SEL in segments.h.
616 	 */
617 	load_segment_registers((KCS_SEL | SEL_KPL), KFS_SEL, KGS_SEL, KDS_SEL);
618 
619 	/*
620 	 *  setup %gs for kernel
621 	 */
622 	xen_set_segment_base(SEGBASE_GS_KERNEL, (ulong_t)&cpus[0]);
623 
624 	/*
625 	 * XX64 We should never dereference off "other gsbase" or
626 	 * "fsbase".  So, we should arrange to point FSBASE and
627 	 * KGSBASE somewhere truly awful e.g. point it at the last
628 	 * valid address below the hole so that any attempts to index
629 	 * off them cause an exception.
630 	 *
631 	 * For now, point it at 8G -- at least it should be unmapped
632 	 * until some 64-bit processes run.
633 	 */
634 	addr = 0x200000000ul;
635 	xen_set_segment_base(SEGBASE_FS, addr);
636 	xen_set_segment_base(SEGBASE_GS_USER, addr);
637 	xen_set_segment_base(SEGBASE_GS_USER_SEL, 0);
638 
639 	return (gdt0);
640 }
641 
642 #else	/* __xpv */
643 
644 static user_desc_t *
645 init_gdt(void)
646 {
647 	desctbr_t	r_bgdt, r_gdt;
648 	user_desc_t	*bgdt;
649 
650 #if !defined(__lint)
651 	/*
652 	 * Our gdt is never larger than a single page.
653 	 */
654 	ASSERT((sizeof (*gdt0) * NGDT) <= PAGESIZE);
655 #endif
656 	gdt0 = (user_desc_t *)BOP_ALLOC(bootops, (caddr_t)GDT_VA,
657 	    PAGESIZE, PAGESIZE);
658 	bzero(gdt0, PAGESIZE);
659 
660 	init_gdt_common(gdt0);
661 
662 	/*
663 	 * Copy in from boot's gdt to our gdt.
664 	 * Entry 0 is the null descriptor by definition.
665 	 */
666 	rd_gdtr(&r_bgdt);
667 	bgdt = (user_desc_t *)r_bgdt.dtr_base;
668 	if (bgdt == NULL)
669 		panic("null boot gdt");
670 
671 	gdt0[GDT_B32DATA] = bgdt[GDT_B32DATA];
672 	gdt0[GDT_B32CODE] = bgdt[GDT_B32CODE];
673 	gdt0[GDT_B16CODE] = bgdt[GDT_B16CODE];
674 	gdt0[GDT_B16DATA] = bgdt[GDT_B16DATA];
675 	gdt0[GDT_B64CODE] = bgdt[GDT_B64CODE];
676 
677 	/*
678 	 * Install our new GDT
679 	 */
680 	r_gdt.dtr_limit = (sizeof (*gdt0) * NGDT) - 1;
681 	r_gdt.dtr_base = (uintptr_t)gdt0;
682 	wr_gdtr(&r_gdt);
683 
684 	/*
685 	 * Reload the segment registers to use the new GDT
686 	 */
687 	load_segment_registers(KCS_SEL, KFS_SEL, KGS_SEL, KDS_SEL);
688 
689 	/*
690 	 *  setup %gs for kernel
691 	 */
692 	wrmsr(MSR_AMD_GSBASE, (uint64_t)&cpus[0]);
693 
694 	/*
695 	 * XX64 We should never dereference off "other gsbase" or
696 	 * "fsbase".  So, we should arrange to point FSBASE and
697 	 * KGSBASE somewhere truly awful e.g. point it at the last
698 	 * valid address below the hole so that any attempts to index
699 	 * off them cause an exception.
700 	 *
701 	 * For now, point it at 8G -- at least it should be unmapped
702 	 * until some 64-bit processes run.
703 	 */
704 	wrmsr(MSR_AMD_FSBASE, 0x200000000ul);
705 	wrmsr(MSR_AMD_KGSBASE, 0x200000000ul);
706 	return (gdt0);
707 }
708 
709 #endif	/* __xpv */
710 
711 #elif defined(__i386)
712 
713 static void
714 init_gdt_common(user_desc_t *gdt)
715 {
716 	int i;
717 
718 	/*
719 	 * Text and data for both kernel and user span entire 32 bit
720 	 * address space.
721 	 */
722 
723 	/*
724 	 * kernel code segment.
725 	 */
726 	set_usegd(&gdt[GDT_KCODE], NULL, -1, SDT_MEMERA, SEL_KPL, SDP_PAGES,
727 	    SDP_OP32);
728 
729 	/*
730 	 * kernel data segment.
731 	 */
732 	set_usegd(&gdt[GDT_KDATA], NULL, -1, SDT_MEMRWA, SEL_KPL, SDP_PAGES,
733 	    SDP_OP32);
734 
735 	/*
736 	 * user code segment.
737 	 */
738 	set_usegd(&gdt[GDT_UCODE], NULL, -1, SDT_MEMERA, SEL_UPL, SDP_PAGES,
739 	    SDP_OP32);
740 
741 	/*
742 	 * user data segment.
743 	 */
744 	set_usegd(&gdt[GDT_UDATA], NULL, -1, SDT_MEMRWA, SEL_UPL, SDP_PAGES,
745 	    SDP_OP32);
746 
747 #if !defined(__xpv)
748 
749 	/*
750 	 * TSS for T_DBLFLT (double fault) handler
751 	 */
752 	set_syssegd((system_desc_t *)&gdt[GDT_DBFLT], dftss0,
753 	    sizeof (*dftss0) - 1, SDT_SYSTSS, SEL_KPL);
754 
755 	/*
756 	 * TSS for kernel
757 	 */
758 	set_syssegd((system_desc_t *)&gdt[GDT_KTSS], ktss0,
759 	    sizeof (*ktss0) - 1, SDT_SYSTSS, SEL_KPL);
760 
761 #endif	/* !__xpv */
762 
763 	/*
764 	 * %gs selector for kernel
765 	 */
766 	set_usegd(&gdt[GDT_GS], &cpus[0], sizeof (struct cpu) -1, SDT_MEMRWA,
767 	    SEL_KPL, SDP_BYTES, SDP_OP32);
768 
769 	/*
770 	 * Initialize lwp private descriptors.
771 	 * Only attributes and limits are initialized, the effective
772 	 * base address is programmed via fsbase/gsbase.
773 	 */
774 	set_usegd(&gdt[GDT_LWPFS], NULL, (size_t)-1, SDT_MEMRWA, SEL_UPL,
775 	    SDP_PAGES, SDP_OP32);
776 	set_usegd(&gdt[GDT_LWPGS], NULL, (size_t)-1, SDT_MEMRWA, SEL_UPL,
777 	    SDP_PAGES, SDP_OP32);
778 
779 	/*
780 	 * Initialize the descriptors set aside for brand usage.
781 	 * Only attributes and limits are initialized.
782 	 */
783 	for (i = GDT_BRANDMIN; i <= GDT_BRANDMAX; i++)
784 		set_usegd(&gdt0[i], NULL, (size_t)-1, SDT_MEMRWA, SEL_UPL,
785 		    SDP_PAGES, SDP_OP32);
786 	/*
787 	 * Initialize convenient zero base user descriptor for clearing
788 	 * lwp  private %fs and %gs descriptors in GDT. See setregs() for
789 	 * an example.
790 	 */
791 	set_usegd(&zero_udesc, NULL, -1, SDT_MEMRWA, SEL_UPL,
792 	    SDP_BYTES, SDP_OP32);
793 }
794 
795 #if defined(__xpv)
796 
797 static user_desc_t *
798 init_gdt(void)
799 {
800 	uint64_t gdtpa;
801 	ulong_t ma[1];		/* XXPV should be a memory_t */
802 
803 #if !defined(__lint)
804 	/*
805 	 * Our gdt is never larger than a single page.
806 	 */
807 	ASSERT((sizeof (*gdt0) * NGDT) <= PAGESIZE);
808 #endif
809 	gdt0 = (user_desc_t *)BOP_ALLOC(bootops, (caddr_t)GDT_VA,
810 	    PAGESIZE, PAGESIZE);
811 	bzero(gdt0, PAGESIZE);
812 
813 	init_gdt_common(gdt0);
814 	gdtpa = pfn_to_pa(va_to_pfn(gdt0));
815 
816 	/*
817 	 * XXX Since we never invoke kmdb until after the kernel takes
818 	 * over the descriptor tables why not have it use the kernel's
819 	 * selectors?
820 	 */
821 	if (boothowto & RB_DEBUG) {
822 		set_usegd(&gdt0[GDT_B32DATA], NULL, -1, SDT_MEMRWA, SEL_KPL,
823 		    SDP_PAGES, SDP_OP32);
824 		set_usegd(&gdt0[GDT_B32CODE], NULL, -1, SDT_MEMERA, SEL_KPL,
825 		    SDP_PAGES, SDP_OP32);
826 	}
827 
828 	/*
829 	 * Clear write permission for page containing the gdt and install it.
830 	 */
831 	ma[0] = (ulong_t)(pa_to_ma(gdtpa) >> PAGESHIFT);
832 	kbm_read_only((uintptr_t)gdt0, gdtpa);
833 	xen_set_gdt(ma, NGDT);
834 
835 	/*
836 	 * Reload the segment registers to use the new GDT
837 	 */
838 	load_segment_registers(
839 	    KCS_SEL, KDS_SEL, KDS_SEL, KFS_SEL, KGS_SEL, KDS_SEL);
840 
841 	return (gdt0);
842 }
843 
844 #else	/* __xpv */
845 
846 static user_desc_t *
847 init_gdt(void)
848 {
849 	desctbr_t	r_bgdt, r_gdt;
850 	user_desc_t	*bgdt;
851 
852 #if !defined(__lint)
853 	/*
854 	 * Our gdt is never larger than a single page.
855 	 */
856 	ASSERT((sizeof (*gdt0) * NGDT) <= PAGESIZE);
857 #endif
858 	/*
859 	 * XXX this allocation belongs in our caller, not here.
860 	 */
861 	gdt0 = (user_desc_t *)BOP_ALLOC(bootops, (caddr_t)GDT_VA,
862 	    PAGESIZE, PAGESIZE);
863 	bzero(gdt0, PAGESIZE);
864 
865 	init_gdt_common(gdt0);
866 
867 	/*
868 	 * Copy in from boot's gdt to our gdt entries.
869 	 * Entry 0 is null descriptor by definition.
870 	 */
871 	rd_gdtr(&r_bgdt);
872 	bgdt = (user_desc_t *)r_bgdt.dtr_base;
873 	if (bgdt == NULL)
874 		panic("null boot gdt");
875 
876 	gdt0[GDT_B32DATA] = bgdt[GDT_B32DATA];
877 	gdt0[GDT_B32CODE] = bgdt[GDT_B32CODE];
878 	gdt0[GDT_B16CODE] = bgdt[GDT_B16CODE];
879 	gdt0[GDT_B16DATA] = bgdt[GDT_B16DATA];
880 
881 	/*
882 	 * Install our new GDT
883 	 */
884 	r_gdt.dtr_limit = (sizeof (*gdt0) * NGDT) - 1;
885 	r_gdt.dtr_base = (uintptr_t)gdt0;
886 	wr_gdtr(&r_gdt);
887 
888 	/*
889 	 * Reload the segment registers to use the new GDT
890 	 */
891 	load_segment_registers(
892 	    KCS_SEL, KDS_SEL, KDS_SEL, KFS_SEL, KGS_SEL, KDS_SEL);
893 
894 	return (gdt0);
895 }
896 
897 #endif	/* __xpv */
898 #endif	/* __i386 */
899 
900 /*
901  * Build kernel IDT.
902  *
903  * Note that for amd64 we pretty much require every gate to be an interrupt
904  * gate which blocks interrupts atomically on entry; that's because of our
905  * dependency on using 'swapgs' every time we come into the kernel to find
906  * the cpu structure. If we get interrupted just before doing that, %cs could
907  * be in kernel mode (so that the trap prolog doesn't do a swapgs), but
908  * %gsbase is really still pointing at something in userland. Bad things will
909  * ensue. We also use interrupt gates for i386 as well even though this is not
910  * required for some traps.
911  *
912  * Perhaps they should have invented a trap gate that does an atomic swapgs?
913  */
914 static void
915 init_idt_common(gate_desc_t *idt)
916 {
917 	set_gatesegd(&idt[T_ZERODIV], &div0trap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
918 	set_gatesegd(&idt[T_SGLSTP], &dbgtrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
919 	set_gatesegd(&idt[T_NMIFLT], &nmiint, KCS_SEL, SDT_SYSIGT, TRP_KPL);
920 	set_gatesegd(&idt[T_BPTFLT], &brktrap, KCS_SEL, SDT_SYSIGT, TRP_UPL);
921 	set_gatesegd(&idt[T_OVFLW], &ovflotrap, KCS_SEL, SDT_SYSIGT, TRP_UPL);
922 	set_gatesegd(&idt[T_BOUNDFLT], &boundstrap, KCS_SEL, SDT_SYSIGT,
923 	    TRP_KPL);
924 	set_gatesegd(&idt[T_ILLINST], &invoptrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
925 	set_gatesegd(&idt[T_NOEXTFLT], &ndptrap,  KCS_SEL, SDT_SYSIGT, TRP_KPL);
926 
927 	/*
928 	 * double fault handler.
929 	 *
930 	 * Note that on the hypervisor a guest does not receive #df faults.
931 	 * Instead a failsafe event is injected into the guest if its selectors
932 	 * and/or stack is in a broken state. See xen_failsafe_callback.
933 	 */
934 #if !defined(__xpv)
935 #if defined(__amd64)
936 
937 	set_gatesegd(&idt[T_DBLFLT], &syserrtrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
938 
939 #elif defined(__i386)
940 
941 	/*
942 	 * task gate required.
943 	 */
944 	set_gatesegd(&idt[T_DBLFLT], NULL, DFTSS_SEL, SDT_SYSTASKGT, TRP_KPL);
945 
946 #endif	/* __i386 */
947 #endif	/* !__xpv */
948 
949 	/*
950 	 * T_EXTOVRFLT coprocessor-segment-overrun not supported.
951 	 */
952 
953 	set_gatesegd(&idt[T_TSSFLT], &invtsstrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
954 	set_gatesegd(&idt[T_SEGFLT], &segnptrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
955 	set_gatesegd(&idt[T_STKFLT], &stktrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
956 	set_gatesegd(&idt[T_GPFLT], &gptrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
957 	set_gatesegd(&idt[T_PGFLT], &pftrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
958 	set_gatesegd(&idt[T_EXTERRFLT], &ndperr, KCS_SEL, SDT_SYSIGT, TRP_KPL);
959 	set_gatesegd(&idt[T_ALIGNMENT], &achktrap, KCS_SEL, SDT_SYSIGT,
960 	    TRP_KPL);
961 	set_gatesegd(&idt[T_MCE], &mcetrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
962 	set_gatesegd(&idt[T_SIMDFPE], &xmtrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
963 
964 	/*
965 	 * install "int80" handler at, well, 0x80.
966 	 */
967 	set_gatesegd(&idt0[T_INT80], &sys_int80, KCS_SEL, SDT_SYSIGT, TRP_UPL);
968 
969 	/*
970 	 * install fast trap handler at 210.
971 	 */
972 	set_gatesegd(&idt[T_FASTTRAP], &fasttrap, KCS_SEL, SDT_SYSIGT, TRP_UPL);
973 
974 	/*
975 	 * System call handler.
976 	 */
977 #if defined(__amd64)
978 	set_gatesegd(&idt[T_SYSCALLINT], &sys_syscall_int, KCS_SEL, SDT_SYSIGT,
979 	    TRP_UPL);
980 
981 #elif defined(__i386)
982 	set_gatesegd(&idt[T_SYSCALLINT], &sys_call, KCS_SEL, SDT_SYSIGT,
983 	    TRP_UPL);
984 #endif	/* __i386 */
985 
986 	/*
987 	 * Install the DTrace interrupt handler for the pid provider.
988 	 */
989 	set_gatesegd(&idt[T_DTRACE_RET], &dtrace_ret, KCS_SEL,
990 	    SDT_SYSIGT, TRP_UPL);
991 
992 	/*
993 	 * Prepare interposing descriptors for the branded "int80"
994 	 * and syscall handlers and cache copies of the default
995 	 * descriptors.
996 	 */
997 	brand_tbl[0].ih_inum = T_INT80;
998 	brand_tbl[0].ih_default_desc = idt0[T_INT80];
999 	set_gatesegd(&(brand_tbl[0].ih_interp_desc), &brand_sys_int80, KCS_SEL,
1000 	    SDT_SYSIGT, TRP_UPL);
1001 
1002 	brand_tbl[1].ih_inum = T_SYSCALLINT;
1003 	brand_tbl[1].ih_default_desc = idt0[T_SYSCALLINT];
1004 
1005 #if defined(__amd64)
1006 	set_gatesegd(&(brand_tbl[1].ih_interp_desc), &brand_sys_syscall_int,
1007 	    KCS_SEL, SDT_SYSIGT, TRP_UPL);
1008 #elif defined(__i386)
1009 	set_gatesegd(&(brand_tbl[1].ih_interp_desc), &brand_sys_call,
1010 	    KCS_SEL, SDT_SYSIGT, TRP_UPL);
1011 #endif	/* __i386 */
1012 
1013 	brand_tbl[2].ih_inum = 0;
1014 }
1015 
1016 #if defined(__xpv)
1017 
1018 static void
1019 init_idt(gate_desc_t *idt)
1020 {
1021 	bzero(idt, sizeof (*idt) * NIDT);
1022 	init_idt_common(idt);
1023 }
1024 
1025 #else	/* __xpv */
1026 
1027 static void
1028 init_idt(gate_desc_t *idt)
1029 {
1030 	char	ivctname[80];
1031 	void	(*ivctptr)(void);
1032 	int	i;
1033 
1034 	/*
1035 	 * Initialize entire table with 'reserved' trap and then overwrite
1036 	 * specific entries. T_EXTOVRFLT (9) is unsupported and reserved
1037 	 * since it can only be generated on a 386 processor. 15 is also
1038 	 * unsupported and reserved.
1039 	 */
1040 	for (i = 0; i < NIDT; i++)
1041 		set_gatesegd(&idt[i], &resvtrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
1042 
1043 	/*
1044 	 * 20-31 reserved
1045 	 */
1046 	for (i = 20; i < 32; i++)
1047 		set_gatesegd(&idt[i], &invaltrap, KCS_SEL, SDT_SYSIGT, TRP_KPL);
1048 
1049 	/*
1050 	 * interrupts 32 - 255
1051 	 */
1052 	for (i = 32; i < 256; i++) {
1053 		(void) snprintf(ivctname, sizeof (ivctname), "ivct%d", i);
1054 		ivctptr = (void (*)(void))kobj_getsymvalue(ivctname, 0);
1055 		if (ivctptr == NULL)
1056 			panic("kobj_getsymvalue(%s) failed", ivctname);
1057 
1058 		set_gatesegd(&idt[i], ivctptr, KCS_SEL, SDT_SYSIGT, TRP_KPL);
1059 	}
1060 
1061 	/*
1062 	 * Now install the common ones. Note that it will overlay some
1063 	 * entries installed above like T_SYSCALLINT, T_FASTTRAP etc.
1064 	 */
1065 	init_idt_common(idt);
1066 }
1067 
1068 #endif	/* __xpv */
1069 
1070 /*
1071  * The kernel does not deal with LDTs unless a user explicitly creates
1072  * one. Under normal circumstances, the LDTR contains 0. Any process attempting
1073  * to reference the LDT will therefore cause a #gp. System calls made via the
1074  * obsolete lcall mechanism are emulated by the #gp fault handler.
1075  */
1076 static void
1077 init_ldt(void)
1078 {
1079 #if defined(__xpv)
1080 	xen_set_ldt(NULL, 0);
1081 #else
1082 	wr_ldtr(0);
1083 #endif
1084 }
1085 
1086 #if !defined(__xpv)
1087 #if defined(__amd64)
1088 
1089 static void
1090 init_tss(void)
1091 {
1092 	/*
1093 	 * tss_rsp0 is dynamically filled in by resume() on each context switch.
1094 	 * All exceptions but #DF will run on the thread stack.
1095 	 * Set up the double fault stack here.
1096 	 */
1097 	ktss0->tss_ist1 =
1098 	    (uint64_t)&dblfault_stack0[sizeof (dblfault_stack0)];
1099 
1100 	/*
1101 	 * Set I/O bit map offset equal to size of TSS segment limit
1102 	 * for no I/O permission map. This will force all user I/O
1103 	 * instructions to generate #gp fault.
1104 	 */
1105 	ktss0->tss_bitmapbase = sizeof (*ktss0);
1106 
1107 	/*
1108 	 * Point %tr to descriptor for ktss0 in gdt.
1109 	 */
1110 	wr_tsr(KTSS_SEL);
1111 }
1112 
1113 #elif defined(__i386)
1114 
1115 static void
1116 init_tss(void)
1117 {
1118 	/*
1119 	 * ktss0->tss_esp dynamically filled in by resume() on each
1120 	 * context switch.
1121 	 */
1122 	ktss0->tss_ss0	= KDS_SEL;
1123 	ktss0->tss_eip	= (uint32_t)_start;
1124 	ktss0->tss_ds	= ktss0->tss_es = ktss0->tss_ss = KDS_SEL;
1125 	ktss0->tss_cs	= KCS_SEL;
1126 	ktss0->tss_fs	= KFS_SEL;
1127 	ktss0->tss_gs	= KGS_SEL;
1128 	ktss0->tss_ldt	= ULDT_SEL;
1129 
1130 	/*
1131 	 * Initialize double fault tss.
1132 	 */
1133 	dftss0->tss_esp0 = (uint32_t)&dblfault_stack0[sizeof (dblfault_stack0)];
1134 	dftss0->tss_ss0	= KDS_SEL;
1135 
1136 	/*
1137 	 * tss_cr3 will get initialized in hat_kern_setup() once our page
1138 	 * tables have been setup.
1139 	 */
1140 	dftss0->tss_eip	= (uint32_t)syserrtrap;
1141 	dftss0->tss_esp	= (uint32_t)&dblfault_stack0[sizeof (dblfault_stack0)];
1142 	dftss0->tss_cs	= KCS_SEL;
1143 	dftss0->tss_ds	= KDS_SEL;
1144 	dftss0->tss_es	= KDS_SEL;
1145 	dftss0->tss_ss	= KDS_SEL;
1146 	dftss0->tss_fs	= KFS_SEL;
1147 	dftss0->tss_gs	= KGS_SEL;
1148 
1149 	/*
1150 	 * Set I/O bit map offset equal to size of TSS segment limit
1151 	 * for no I/O permission map. This will force all user I/O
1152 	 * instructions to generate #gp fault.
1153 	 */
1154 	ktss0->tss_bitmapbase = sizeof (*ktss0);
1155 
1156 	/*
1157 	 * Point %tr to descriptor for ktss0 in gdt.
1158 	 */
1159 	wr_tsr(KTSS_SEL);
1160 }
1161 
1162 #endif	/* __i386 */
1163 #endif	/* !__xpv */
1164 
1165 #if defined(__xpv)
1166 
1167 void
1168 init_desctbls(void)
1169 {
1170 	uint_t vec;
1171 	user_desc_t *gdt;
1172 
1173 	/*
1174 	 * Setup and install our GDT.
1175 	 */
1176 	gdt = init_gdt();
1177 
1178 	/*
1179 	 * Store static pa of gdt to speed up pa_to_ma() translations
1180 	 * on lwp context switches.
1181 	 */
1182 	ASSERT(IS_P2ALIGNED((uintptr_t)gdt, PAGESIZE));
1183 	CPU->cpu_gdt = gdt;
1184 	CPU->cpu_m.mcpu_gdtpa = pfn_to_pa(va_to_pfn(gdt));
1185 
1186 	/*
1187 	 * Setup and install our IDT.
1188 	 */
1189 #if !defined(__lint)
1190 	ASSERT(NIDT * sizeof (*idt0) <= PAGESIZE);
1191 #endif
1192 	idt0 = (gate_desc_t *)BOP_ALLOC(bootops, (caddr_t)IDT_VA,
1193 	    PAGESIZE, PAGESIZE);
1194 	init_idt(idt0);
1195 	for (vec = 0; vec < NIDT; vec++)
1196 		xen_idt_write(&idt0[vec], vec);
1197 
1198 	CPU->cpu_idt = idt0;
1199 
1200 	/*
1201 	 * set default kernel stack
1202 	 */
1203 	xen_stack_switch(KDS_SEL,
1204 	    (ulong_t)&dblfault_stack0[sizeof (dblfault_stack0)]);
1205 
1206 	xen_init_callbacks();
1207 
1208 	init_ldt();
1209 }
1210 
1211 #else	/* __xpv */
1212 
1213 void
1214 init_desctbls(void)
1215 {
1216 	user_desc_t *gdt;
1217 	desctbr_t idtr;
1218 
1219 	/*
1220 	 * Allocate IDT and TSS structures on unique pages for better
1221 	 * performance in virtual machines.
1222 	 */
1223 #if !defined(__lint)
1224 	ASSERT(NIDT * sizeof (*idt0) <= PAGESIZE);
1225 #endif
1226 	idt0 = (gate_desc_t *)BOP_ALLOC(bootops, (caddr_t)IDT_VA,
1227 	    PAGESIZE, PAGESIZE);
1228 #if !defined(__lint)
1229 	ASSERT(sizeof (*ktss0) <= PAGESIZE);
1230 #endif
1231 	ktss0 = (struct tss *)BOP_ALLOC(bootops, (caddr_t)KTSS_VA,
1232 	    PAGESIZE, PAGESIZE);
1233 
1234 #if defined(__i386)
1235 #if !defined(__lint)
1236 	ASSERT(sizeof (*dftss0) <= PAGESIZE);
1237 #endif
1238 	dftss0 = (struct tss *)BOP_ALLOC(bootops, (caddr_t)DFTSS_VA,
1239 	    PAGESIZE, PAGESIZE);
1240 #endif
1241 
1242 	/*
1243 	 * Setup and install our GDT.
1244 	 */
1245 	gdt = init_gdt();
1246 	ASSERT(IS_P2ALIGNED((uintptr_t)gdt, PAGESIZE));
1247 	CPU->cpu_gdt = gdt;
1248 
1249 	/*
1250 	 * Setup and install our IDT.
1251 	 */
1252 	init_idt(idt0);
1253 
1254 	idtr.dtr_base = (uintptr_t)idt0;
1255 	idtr.dtr_limit = (NIDT * sizeof (*idt0)) - 1;
1256 	wr_idtr(&idtr);
1257 	CPU->cpu_idt = idt0;
1258 
1259 #if defined(__i386)
1260 	/*
1261 	 * We maintain a description of idt0 in convenient IDTR format
1262 	 * for #pf's on some older pentium processors. See pentium_pftrap().
1263 	 */
1264 	idt0_default_r = idtr;
1265 #endif	/* __i386 */
1266 
1267 	init_tss();
1268 	CPU->cpu_tss = ktss0;
1269 	init_ldt();
1270 }
1271 
1272 #endif	/* __xpv */
1273 
1274 /*
1275  * In the early kernel, we need to set up a simple GDT to run on.
1276  *
1277  * XXPV	Can dboot use this too?  See dboot_gdt.s
1278  */
1279 void
1280 init_boot_gdt(user_desc_t *bgdt)
1281 {
1282 #if defined(__amd64)
1283 	set_usegd(&bgdt[GDT_B32DATA], SDP_LONG, NULL, -1, SDT_MEMRWA, SEL_KPL,
1284 	    SDP_PAGES, SDP_OP32);
1285 	set_usegd(&bgdt[GDT_B64CODE], SDP_LONG, NULL, -1, SDT_MEMERA, SEL_KPL,
1286 	    SDP_PAGES, SDP_OP32);
1287 #elif defined(__i386)
1288 	set_usegd(&bgdt[GDT_B32DATA], NULL, -1, SDT_MEMRWA, SEL_KPL,
1289 	    SDP_PAGES, SDP_OP32);
1290 	set_usegd(&bgdt[GDT_B32CODE], NULL, -1, SDT_MEMERA, SEL_KPL,
1291 	    SDP_PAGES, SDP_OP32);
1292 #endif	/* __i386 */
1293 }
1294 
1295 /*
1296  * Enable interpositioning on the system call path by rewriting the
1297  * sys{call|enter} MSRs and the syscall-related entries in the IDT to use
1298  * the branded entry points.
1299  */
1300 void
1301 brand_interpositioning_enable(void)
1302 {
1303 	gate_desc_t	*idt = CPU->cpu_idt;
1304 	int 		i;
1305 
1306 	ASSERT(curthread->t_preempt != 0 || getpil() >= DISP_LEVEL);
1307 
1308 	for (i = 0; brand_tbl[i].ih_inum; i++) {
1309 		idt[brand_tbl[i].ih_inum] = brand_tbl[i].ih_interp_desc;
1310 #if defined(__xpv)
1311 		xen_idt_write(&idt[brand_tbl[i].ih_inum],
1312 		    brand_tbl[i].ih_inum);
1313 #endif
1314 	}
1315 
1316 #if defined(__amd64)
1317 #if defined(__xpv)
1318 
1319 	/*
1320 	 * Currently the hypervisor only supports 64-bit syscalls via
1321 	 * syscall instruction. The 32-bit syscalls are handled by
1322 	 * interrupt gate above.
1323 	 */
1324 	xen_set_callback(brand_sys_syscall, CALLBACKTYPE_syscall,
1325 	    CALLBACKF_mask_events);
1326 
1327 #else
1328 
1329 	if (x86_feature & X86_ASYSC) {
1330 		wrmsr(MSR_AMD_LSTAR, (uintptr_t)brand_sys_syscall);
1331 		wrmsr(MSR_AMD_CSTAR, (uintptr_t)brand_sys_syscall32);
1332 	}
1333 
1334 #endif
1335 #endif	/* __amd64 */
1336 
1337 	if (x86_feature & X86_SEP)
1338 		wrmsr(MSR_INTC_SEP_EIP, (uintptr_t)brand_sys_sysenter);
1339 }
1340 
1341 /*
1342  * Disable interpositioning on the system call path by rewriting the
1343  * sys{call|enter} MSRs and the syscall-related entries in the IDT to use
1344  * the standard entry points, which bypass the interpositioning hooks.
1345  */
1346 void
1347 brand_interpositioning_disable(void)
1348 {
1349 	gate_desc_t	*idt = CPU->cpu_idt;
1350 	int i;
1351 
1352 	ASSERT(curthread->t_preempt != 0 || getpil() >= DISP_LEVEL);
1353 
1354 	for (i = 0; brand_tbl[i].ih_inum; i++) {
1355 		idt[brand_tbl[i].ih_inum] = brand_tbl[i].ih_default_desc;
1356 #if defined(__xpv)
1357 		xen_idt_write(&idt[brand_tbl[i].ih_inum],
1358 		    brand_tbl[i].ih_inum);
1359 #endif
1360 	}
1361 
1362 #if defined(__amd64)
1363 #if defined(__xpv)
1364 
1365 	/*
1366 	 * See comment above in brand_interpositioning_enable.
1367 	 */
1368 	xen_set_callback(sys_syscall, CALLBACKTYPE_syscall,
1369 	    CALLBACKF_mask_events);
1370 
1371 #else
1372 
1373 	if (x86_feature & X86_ASYSC) {
1374 		wrmsr(MSR_AMD_LSTAR, (uintptr_t)sys_syscall);
1375 		wrmsr(MSR_AMD_CSTAR, (uintptr_t)sys_syscall32);
1376 	}
1377 
1378 #endif
1379 #endif	/* __amd64 */
1380 
1381 	if (x86_feature & X86_SEP)
1382 		wrmsr(MSR_INTC_SEP_EIP, (uintptr_t)sys_sysenter);
1383 }
1384