|
root / base / usr / src / uts / intel / ia32
ia32 Plain Text 793 lines 21.1 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

# Copyright 2021, Richard Lowe.

This directory contains ia32 specific code that is nevertheless still used.
This includes

krtld
	the relocation engine shared with the userland link-editor.

sys
	headers still used by various parts of the system, largely describing
	the register set of the intel 32bit processor
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Copyright 2019 Joyent, Inc.
 */

/*
 * While this is no longer relevant to the kernel, we keep it in its
 * traditional location to match the other variants, used from the ld
 * code.
 */

#define	ELF_TARGET_386
#if defined(DO_RELOC_LIBLD)
#undef DO_RELOC_LIBLD
#define	DO_RELOC_LIBLD_X86
#endif
#include	<stdio.h>
#include	"sgs.h"
#include	"machdep.h"
#include	"libld.h"
#include	"reloc.h"
#include	"conv.h"
#include	"msg.h"

/*
 * We need to build this code differently when it is used for
 * cross linking:
 *	- Data alignment requirements can differ from those
 *		of the running system, so we can't access data
 *		in units larger than a byte
 *	- We have to include code to do byte swapping when the
 *		target and linker host use different byte ordering,
 *		but such code is a waste when running natively.
 */
#if !defined(DO_RELOC_LIBLD) || defined(__x86)
#define	DORELOC_NATIVE
#endif

/*
 * This table represents the current relocations that do_reloc() is able to
 * process.  The relocations below that are marked SPECIAL are relocations that
 * take special processing and shouldn't actually ever be passed to do_reloc().
 */
const Rel_entry	reloc_table[R_386_NUM] = {
	[R_386_NONE]		= {0, FLG_RE_NOTREL, 0, 0, 0},
	[R_386_32]		= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_PC32]		= {0, FLG_RE_PCREL, 4, 0, 0},
	[R_386_GOT32]		= {0, FLG_RE_GOTADD, 4, 0, 0},
	[R_386_PLT32]		= {0, FLG_RE_PLTREL | FLG_RE_PCREL, 4, 0, 0},
	[R_386_COPY]		= {0, FLG_RE_NOTREL, 0, 0, 0},	/* SPECIAL */
	[R_386_GLOB_DAT]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_JMP_SLOT]	= {0, FLG_RE_NOTREL, 4, 0, 0},	/* SPECIAL */
	[R_386_RELATIVE]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_GOTOFF]		= {0, FLG_RE_GOTREL, 4, 0, 0},
	[R_386_GOTPC]		= {0, FLG_RE_PCREL | FLG_RE_GOTPC |
	    FLG_RE_LOCLBND, 4, 0, 0},
	[R_386_32PLT]		= {0, FLG_RE_PLTREL, 4, 0, 0},
	[R_386_TLS_GD_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
	    FLG_RE_TLSGD, 4, 0, 0},
	[R_386_TLS_LDM_PLT]	= {0, FLG_RE_PLTREL | FLG_RE_PCREL |
	    FLG_RE_TLSLD, 4, 0, 0},
	[R_386_TLS_TPOFF]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_TLS_IE]		= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
	[R_386_TLS_GOTIE]	= {0, FLG_RE_GOTADD | FLG_RE_TLSIE, 4, 0, 0},
	[R_386_TLS_LE]		= {0, FLG_RE_TLSLE, 4, 0, 0},
	[R_386_TLS_GD]		= {0, FLG_RE_GOTADD | FLG_RE_TLSGD, 4, 0, 0},
	[R_386_TLS_LDM]		= {0, FLG_RE_GOTADD | FLG_RE_TLSLD, 4, 0, 0},
	[R_386_16]		= {0, FLG_RE_NOTREL, 2, 0, 0},
	[R_386_PC16]		= {0, FLG_RE_PCREL, 2, 0, 0},
	[R_386_8]		= {0, FLG_RE_NOTREL, 1, 0, 0},
	[R_386_PC8]		= {0, FLG_RE_PCREL, 1, 0, 0},
	[R_386_TLS_GD_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_GD_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_GD_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_GD_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_PUSH]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDM_POP]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LDO_32]	= {0, FLG_RE_TLSLD, 4, 0, 0},
	[R_386_TLS_IE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_LE_32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_DTPMOD32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_TLS_DTPOFF32]	= {0, FLG_RE_NOTREL, 4, 0, 0},
	[R_386_TLS_TPOFF32]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_SIZE32]		= {0, FLG_RE_SIZE | FLG_RE_VERIFY, 4, 0, 0},
	[R_386_TLS_GOTDESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_DESC_CALL]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_TLS_DESC]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_IRELATIVE]	= {0, FLG_RE_NOTSUP, 0, 0, 0},
	[R_386_GOT32X]		= {0, FLG_RE_GOTADD, 4, 0, 0},
};
#if	(R_386_NUM != (R_386_GOT32X + 1))
#error	"R_386_NUM has grown"
#endif

/*
 * Write a single relocated value to its reference location.
 * We assume we wish to add the relocation amount, value, to the
 * value of the address already present at the offset.
 *
 * NAME			VALUE	FIELD		CALCULATION
 *
 * R_386_NONE		 0	none		none
 * R_386_32		 1	word32		S + A
 * R_386_PC32		 2	word32		S + A - P
 * R_386_GOT32		 3	word32		G + A - GOT / G + A
 * R_386_PLT32		 4	word32		L + A - P
 * R_386_COPY		 5	none		none
 * R_386_GLOB_DAT	 6	word32		S
 * R_386_JMP_SLOT	 7	word32		S
 * R_386_RELATIVE	 8	word32		B + A
 * R_386_GOTOFF		 9	word32		S + A - GOT
 * R_386_GOTPC		10	word32		GOT + A - P
 * R_386_32PLT		11	word32		L + A
 * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
 * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
 * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
 * R_386_TLS_IE		15	word32		@indntpoff(S)
 * R_386_TLS_GOTIE	16	word32		@gotntpoff(S)
 * R_386_TLS_LE		17	word32		@ntpoff(S)
 * R_386_TLS_GD		18	word32		@tlsgd(S)
 * R_386_TLS_LDM	19	word32		@tlsldm(S)
 * R_386_16		20	word16		S + A
 * R_386_PC16		21	word16		S + A - P
 * R_386_8		22	word8		S + A
 * R_386_PC8		23	word8		S + A - P
 * R_386_TLS_GD_32	24	word32
 * R_386_TLS_GD_PUSH	25	word32
 * R_386_TLS_GD_CALL	26	word32
 * R_386_TLS_GD_POP	27	word32
 * R_386_TLS_LDM_32	28	word32
 * R_386_TLS_LDM_PUSH	29	word32
 * R_386_TLS_LDM_CALL	30	word32
 * R_386_TLS_LDM_POP	31	word32
 * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
 * R_386_TLS_IE_32	33	word32		@gotpoff(S)
 * R_386_TLS_LE_32	34	word32		@tpoff(S)
 * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
 * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
 * R_386_TLS_TPOFF32	37	word32
 * R_386_SIZE32		38	word32		Z + A
 * R_386_TLS_GOTDESC	39	word32
 * R_386_TLS_DESC_CALL	40	none		none
 * R_386_TLS_DESC	41	word32
 * R_386_IRELATIVE	42	word32		indirect (B + A)
 * R_386_GOT32X		43	word32		G + A - GOT / G + A
 *
 * Relocations are from Table 3.6: Relocation Types from the 386 psABI.
 * (with reference also to the thread-local storage ABIs)
 *
 * Relocation calculations:
 *
 * CALCULATION uses the following notation:
 *	A	the addend used
 *	B	the base address of the shared object in memory
 *	G	the offset into the global offset table
 *	GOT	the address of teh global offset table
 *	L	the procedure linkage entry
 *	P	the place of the storage unit being relocated
 *	S	the value of the symbol
 *	Z	the size of the symbol whose index resides in the relocation
 *		entry
 *
 *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
 *	   a Tls_index structure (for passing to __tls_get_addr()). The
 *	   instructions referencing this entry will be bound to the first
 *	   of the two GOT entries.
 *
 *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
 *	   a Tls_index structure (for passing to __tls_get_addr()). The
 *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
 *	   ti_module will be filled in at run-time. The call to
 *	   __tls_get_addr() will return the starting offset of the dynamic
 *	   TLS block.
 *
 *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
 *
 *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
 *
 *	@dtpmod(x): calculate the module id of the object containing symbol x.
 *
 * The calculations in the CALCULATION column are assumed to have
 * been performed before calling this function except for the addition of
 * the addresses in the instructions.
 */
/* BEGIN CSTYLED */
#if defined(DO_RELOC_LIBLD)
/*ARGSUSED5*/
int
do_reloc_ld(Rel_desc *rdesc, uchar_t *off, Xword *value,
    rel_desc_sname_func_t rel_desc_sname_func,
    const char *file, int bswap, void *lml)
#else
int
do_reloc_rtld(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
    const char *file, void *lml)
#endif
{
/* END CSTYLED */
#ifdef DO_RELOC_LIBLD
#define	sym (* rel_desc_sname_func)(rdesc)
	uchar_t	rtype = rdesc->rel_rtype;
#endif
	const Rel_entry	*rep;

	rep = &reloc_table[rtype];

	switch (rep->re_fsize) {
	case 1:
		/* LINTED */
		*((uchar_t *)off) += (uchar_t)(*value);
		break;

	case 2:
#if defined(DORELOC_NATIVE)
		/* LINTED */
		*((Half *)off) += (Half)(*value);
#else
		{
			Half	v;
			uchar_t	*v_bytes = (uchar_t *)&v;

			if (bswap) {
				UL_ASSIGN_BSWAP_HALF(v_bytes, off);
				v += *value;
				UL_ASSIGN_BSWAP_HALF(off, v_bytes);
			} else {
				UL_ASSIGN_HALF(v_bytes, off);
				v += *value;
				UL_ASSIGN_HALF(off, v_bytes);
			}
		}
#endif
		break;

	case 4:
#if defined(DORELOC_NATIVE)
		/* LINTED */
		*((Xword *)off) += *value;
#else
		{
			Word	v;
			uchar_t	*v_bytes = (uchar_t *)&v;

			if (bswap) {
				UL_ASSIGN_BSWAP_WORD(v_bytes, off);
				v += *value;
				UL_ASSIGN_BSWAP_WORD(off, v_bytes);
			} else {
				UL_ASSIGN_WORD(v_bytes, off);
				v += *value;
				UL_ASSIGN_WORD(off, v_bytes);
			}
		}
#endif
		break;
	default:
		/*
		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
		 */
		REL_ERR_UNSUPSZ(lml, file, sym, rtype, rep->re_fsize);
		return (0);
	}
	return (1);

#ifdef DO_RELOC_LIBLD
#undef sym
#endif
}
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# include global definitions
include ../../../../Makefile.master

HDRS=			\
	kdi_regs.h	\
	privmregs.h	\
	privregs.h

LINKS=			\
	asm_linkage.h	\
	machtypes.h	\
	psw.h		\
	pte.h		\
	reg.h		\
	stack.h		\
	trap.h		\
	traptrace.h

ROOTINCISA= $(ROOT)/usr/include/ia32
ROOTDIR= $(ROOTINCISA)/sys
ROOTDIRS= $(ROOTDIR)

ROOTHDRS= $(HDRS:%=$(ROOTDIR)/%)
ROOTLINKS= $(LINKS:%=$(ROOTDIR)/%)

CHECKHDRS= $(HDRS:%.h=%.check)

$(ROOTDIR)/asm_linkage.h	: LINK_TARGET = ../../sys/asm_linkage.h
$(ROOTDIR)/machtypes.h		: LINK_TARGET = ../../sys/machtypes.h
$(ROOTDIR)/psw.h		: LINK_TARGET = ../../sys/psw.h
$(ROOTDIR)/pte.h		: LINK_TARGET = ../../sys/pte.h
$(ROOTDIR)/reg.h		: LINK_TARGET = ../../sys/reg.h
$(ROOTDIR)/stack.h		: LINK_TARGET = ../../sys/stack.h
$(ROOTDIR)/trap.h		: LINK_TARGET = ../../sys/trap.h
$(ROOTDIR)/traptrace.h		: LINK_TARGET = ../../sys/traptrace.h

# install rules
$(ROOTDIR)/%: %
	$(INS.file)

.KEEP_STATE:

.PARALLEL: $(CHECKHDRS) $(ROOTHDRS) $(ROOTLINKS)

install_h: $(ROOTINCISA) .WAIT $(ROOTDIRS) .WAIT $(ROOTHDRS) $(ROOTLINKS)

$(ROOTINCISA):
	$(INS.dir)

$(ROOTDIRS):	$(ROOTINCISA)
	$(INS.dir)

$(ROOTLINKS):
	$(RM) $@; $(SYMLINK) $(LINK_TARGET) $@

check:	$(CHECKHDRS)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2018 Joyent, Inc.
 */

#ifndef _IA32_SYS_KDI_REGS_H
#define	_IA32_SYS_KDI_REGS_H

#ifdef __cplusplus
extern "C" {
#endif

#define	KDIREG_NGREG	21

/*
 * %ss appears in a different place than a typical struct regs, since the
 * machine won't save %ss on a trap entry from the same privilege level.
 */

#define	KDIREG_SAVFP	0
#define	KDIREG_SAVPC	1
#define	KDIREG_SS	2
#define	KDIREG_GS	3
#define	KDIREG_FS	4
#define	KDIREG_ES	5
#define	KDIREG_DS	6
#define	KDIREG_EDI	7
#define	KDIREG_ESI	8
#define	KDIREG_EBP	9
#define	KDIREG_ESP	10
#define	KDIREG_EBX	11
#define	KDIREG_EDX	12
#define	KDIREG_ECX	13
#define	KDIREG_EAX	14
#define	KDIREG_TRAPNO	15
#define	KDIREG_ERR	16
#define	KDIREG_EIP	17
#define	KDIREG_CS	18
#define	KDIREG_EFLAGS	19
#define	KDIREG_UESP	20

#define	KDIREG_PC	KDIREG_EIP
#define	KDIREG_SP	KDIREG_ESP
#define	KDIREG_FP	KDIREG_EBP

#ifdef __cplusplus
}
#endif

#endif /* _IA32_SYS_KDI_REGS_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_IA32_SYS_PRIVMREGS_H
#define	_IA32_SYS_PRIVMREGS_H

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(__i386)
#error	"non-i386 code depends on i386 privileged header!"
#endif

#ifndef _ASM

#define	PM_GREGS (1 << 0)
#define	PM_CRREGS (1 << 1)
#define	PM_DRREGS (1 << 2)

/*
 * This structure is intended to represent a complete machine state for a CPU,
 * when that information is available.  It is only for use internally between
 * KMDB and the kernel, or within MDB.  Note that this isn't yet finished.
 */
typedef struct privmregs {
	ulong_t pm_flags;
	/* general registers */
	struct regs pm_gregs;
	/* cr0-8 */
	ulong_t pm_cr[8];
	/* dr0-8 */
	ulong_t pm_dr[8];
} privmregs_t;

#endif /* !_ASM */

#ifdef __cplusplus
}
#endif

#endif	/* !_IA32_SYS_PRIVMREGS_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_IA32_SYS_PRIVREGS_H
#define	_IA32_SYS_PRIVREGS_H

#ifdef __cplusplus
extern "C" {
#endif

/*
 * This file describes the cpu's privileged register set, and
 * how the machine state is saved on the stack when a trap occurs.
 */

#if !defined(__i386)
#error	"non-i386 code depends on i386 privileged header!"
#endif

#ifndef _ASM

/*
 * This is NOT the structure to use for general purpose debugging;
 * see /proc for that.  This is NOT the structure to use to decode
 * the ucontext or grovel about in a core file; see <sys/regset.h>.
 */

struct regs {
	/*
	 * Extra frame for mdb to follow through high level interrupts and
	 * system traps.  Set them to 0 to terminate stacktrace.
	 */
	greg_t  r_savfp;	/* a copy of %ebp */
	greg_t  r_savpc;	/* a copy of %eip */

	greg_t	r_gs;
	greg_t	r_fs;
	greg_t	r_es;
	greg_t	r_ds;
	greg_t	r_edi;
	greg_t	r_esi;
	greg_t	r_ebp;
	greg_t	r_esp;
	greg_t	r_ebx;
	greg_t	r_edx;
	greg_t	r_ecx;
	greg_t	r_eax;
	greg_t	r_trapno;
	greg_t	r_err;
	greg_t	r_eip;
	greg_t	r_cs;
	greg_t	r_efl;
	greg_t	r_uesp;
	greg_t	r_ss;
};

#define	r_r0	r_eax		/* r0 for portability */
#define	r_r1	r_edx		/* r1 for portability */
#define	r_fp	r_ebp		/* system frame pointer */
#define	r_sp	r_uesp		/* user stack pointer */
#define	r_pc	r_eip		/* user's instruction pointer */
#define	r_ps	r_efl		/* user's EFLAGS */

#define	GREG_NUM	8	/* Number of regs between %edi and %eax */

#ifdef _KERNEL
#define	lwptoregs(lwp)	((struct regs *)((lwp)->lwp_regs))
#endif /* _KERNEL */

#else	/* !_ASM */

#if defined(_MACHDEP)

#include <sys/machprivregs.h>

/*
 * Save current frame on the stack.  Uses %eax.
 */
#define	__FRAME_PUSH				\
	subl	$8, %esp;			\
	movl	REGOFF_EIP(%esp), %eax;		\
	movl	%eax, REGOFF_SAVPC(%esp);	\
	movl	%ebp, REGOFF_SAVFP(%esp);

/*
 * Save segment registers on the stack.
 */
#define	__SEGREGS_PUSH		\
	subl	$16, %esp;	\
	movw	%ds, 12(%esp);	\
	movw	%es, 8(%esp);	\
	movw	%fs, 4(%esp);	\
	movw	%gs, 0(%esp);

/*
 * Load segment register with kernel selectors.
 * %gs must be the last one to be set to make the
 * check in cmnint valid.
 */
#define	__SEGREGS_LOAD_KERNEL	\
	movw	$KDS_SEL, %cx;	\
	movw	%cx, %ds;	\
	movw	%cx, %es;	\
	movw	$KFS_SEL, %cx;	\
	movw	$KGS_SEL, %dx;	\
	movw	%cx, %fs;	\
	movw	%dx, %gs;

/*
 * Restore segment registers off the stack.
 *
 * NOTE THE ORDER IS VITAL!
 *
 * Also note the subtle interdependency with kern_gpfault()
 * that needs to disassemble these instructions to diagnose
 * what happened when things (like bad segment register
 * values) go horribly wrong.
 */
#define	__SEGREGS_POP		\
	movw	0(%esp), %gs;	\
	movw	4(%esp), %fs;	\
	movw	8(%esp), %es;	\
	movw	12(%esp), %ds;	\
	addl	$16, %esp;

/*
 * Macros for saving all registers necessary on interrupt entry,
 * and restoring them on exit.
 */
#define	INTR_PUSH			\
	cld;				\
	pusha;				\
	__SEGREGS_PUSH			\
	__FRAME_PUSH			\
	cmpw	$KGS_SEL, REGOFF_GS(%esp); \
	je	8f;			\
	movl	$0, REGOFF_SAVFP(%esp);	\
	__SEGREGS_LOAD_KERNEL		\
8:	CLEAN_CS

#define	__INTR_POP			\
	popa;				\
	addl	$8, %esp;	/* get TRAPNO and ERR off the stack */

#define	INTR_POP_USER			\
	addl	$8, %esp;	/* get extra frame off the stack */ \
	__SEGREGS_POP			\
	__INTR_POP

#define	INTR_POP_KERNEL					\
	addl	$24, %esp;	/* skip extra frame and segment registers */ \
	__INTR_POP
/*
 * Macros for saving all registers necessary on system call entry,
 * and restoring them on exit.
 */
#define	SYSCALL_PUSH			\
	cld;				\
	pusha;				\
	__SEGREGS_PUSH			\
	subl	$8, %esp;		\
	pushfl;				\
	popl	%ecx;			\
	orl	$PS_IE, %ecx;		\
	movl	%ecx, REGOFF_EFL(%esp);	\
	movl	$0, REGOFF_SAVPC(%esp);	\
	movl	$0, REGOFF_SAVFP(%esp);	\
	__SEGREGS_LOAD_KERNEL;		\

#define	SYSENTER_PUSH			\
	cld;				\
	pusha;				\
	__SEGREGS_PUSH			\
	subl	$8, %esp;		\
	movl	$0, REGOFF_SAVPC(%esp);	\
	movl	$0, REGOFF_SAVFP(%esp);	\
	__SEGREGS_LOAD_KERNEL

#define	SYSCALL_POP			\
	INTR_POP_USER

#endif	/* _MACHDEP */

/*
 * This is used to set eflags to known values at the head of an
 * interrupt gate handler, i.e. interrupts are -already- disabled.
 */
#define	INTGATE_INIT_KERNEL_FLAGS	\
	pushl	$F_OFF;			\
	popfl

#endif	/* !_ASM */

#include <sys/controlregs.h>

/* Control register layout for panic dump */

#define	CREGSZ		36
#define	CREG_GDT	0
#define	CREG_IDT	8
#define	CREG_LDT	16
#define	CREG_TASKR	18
#define	CREG_CR0	20
#define	CREG_CR2	24
#define	CREG_CR3	28
#define	CREG_CR4	32

#if !defined(_ASM) && defined(_INT64_TYPE)

typedef	uint64_t	creg64_t;

struct cregs {
	creg64_t	cr_gdt;
	creg64_t	cr_idt;
	uint16_t	cr_ldt;
	uint16_t	cr_task;
	uint32_t	cr_cr0;
	uint32_t	cr_cr2;
	uint32_t	cr_cr3;
	uint32_t	cr_cr4;
};

#if defined(_KERNEL)
extern void getcregs(struct cregs *);
#endif	/* _KERNEL */

#endif	/* !_ASM && _INT64_TYPE */

#ifdef __cplusplus
}
#endif

#endif	/* !_IA32_SYS_PRIVREGS_H */