xref: /freebsd/sys/x86/linux/linux_x86.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1996 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/signal.h>
31 #include <x86/specialreg.h>
32 #include <x86/trap.h>
33 #include <x86/x86_var.h>
34 
35 #include <x86/linux/linux_x86.h>
36 
37 #define LINUX_T_UNKNOWN  255
38 static int _bsd_to_linux_trapcode[] = {
39 	LINUX_T_UNKNOWN,	/* 0 */
40 	6,			/* 1  T_PRIVINFLT */
41 	LINUX_T_UNKNOWN,	/* 2 */
42 	3,			/* 3  T_BPTFLT */
43 	LINUX_T_UNKNOWN,	/* 4 */
44 	LINUX_T_UNKNOWN,	/* 5 */
45 	16,			/* 6  T_ARITHTRAP */
46 	254,			/* 7  T_ASTFLT */
47 	LINUX_T_UNKNOWN,	/* 8 */
48 	13,			/* 9  T_PROTFLT */
49 	1,			/* 10 T_TRCTRAP */
50 	LINUX_T_UNKNOWN,	/* 11 */
51 	14,			/* 12 T_PAGEFLT */
52 	LINUX_T_UNKNOWN,	/* 13 */
53 	17,			/* 14 T_ALIGNFLT */
54 	LINUX_T_UNKNOWN,	/* 15 */
55 	LINUX_T_UNKNOWN,	/* 16 */
56 	LINUX_T_UNKNOWN,	/* 17 */
57 	0,			/* 18 T_DIVIDE */
58 	2,			/* 19 T_NMI */
59 	4,			/* 20 T_OFLOW */
60 	5,			/* 21 T_BOUND */
61 	7,			/* 22 T_DNA */
62 	8,			/* 23 T_DOUBLEFLT */
63 	9,			/* 24 T_FPOPFLT */
64 	10,			/* 25 T_TSSFLT */
65 	11,			/* 26 T_SEGNPFLT */
66 	12,			/* 27 T_STKFLT */
67 	18,			/* 28 T_MCHK */
68 	19,			/* 29 T_XMMFLT */
69 	15			/* 30 T_RESERVED */
70 };
71 
72 /*
73  * If FreeBSD & Linux have a difference of opinion about what a trap
74  * means, deal with it here.
75  */
76 int
77 linux_translate_traps(int signal, int trap_code)
78 {
79 	if (signal != SIGBUS)
80 		return (signal);
81 	switch (trap_code) {
82 	case T_PROTFLT:
83 	case T_TSSFLT:
84 	case T_DOUBLEFLT:
85 	case T_PAGEFLT:
86 		return (SIGSEGV);
87 	default:
88 		return (signal);
89 	}
90 }
91 
92 int
93 bsd_to_linux_trapcode(int code)
94 {
95 
96 	return (code < nitems(_bsd_to_linux_trapcode) ?
97 	    _bsd_to_linux_trapcode[code] : LINUX_T_UNKNOWN);
98 }
99 
100 u_int
101 linux_x86_elf_hwcap2(void)
102 {
103 	static u_int elf_hwcap2 = 0;
104 	static bool elf_hwcap2_valid = false;
105 
106 	if (!elf_hwcap2_valid) {
107 		if ((cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) != 0)
108 			elf_hwcap2 |= LINUX_HWCAP2_FSGSBASE;
109 		elf_hwcap2_valid = true;
110 	}
111 	return (elf_hwcap2);
112 }
113