xref: /freebsd/sys/x86/linux/linux_x86.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/signal.h>
34 #include <x86/specialreg.h>
35 #include <x86/trap.h>
36 #include <x86/x86_var.h>
37 
38 #include <x86/linux/linux_x86.h>
39 
40 #define LINUX_T_UNKNOWN  255
41 static int _bsd_to_linux_trapcode[] = {
42 	LINUX_T_UNKNOWN,	/* 0 */
43 	6,			/* 1  T_PRIVINFLT */
44 	LINUX_T_UNKNOWN,	/* 2 */
45 	3,			/* 3  T_BPTFLT */
46 	LINUX_T_UNKNOWN,	/* 4 */
47 	LINUX_T_UNKNOWN,	/* 5 */
48 	16,			/* 6  T_ARITHTRAP */
49 	254,			/* 7  T_ASTFLT */
50 	LINUX_T_UNKNOWN,	/* 8 */
51 	13,			/* 9  T_PROTFLT */
52 	1,			/* 10 T_TRCTRAP */
53 	LINUX_T_UNKNOWN,	/* 11 */
54 	14,			/* 12 T_PAGEFLT */
55 	LINUX_T_UNKNOWN,	/* 13 */
56 	17,			/* 14 T_ALIGNFLT */
57 	LINUX_T_UNKNOWN,	/* 15 */
58 	LINUX_T_UNKNOWN,	/* 16 */
59 	LINUX_T_UNKNOWN,	/* 17 */
60 	0,			/* 18 T_DIVIDE */
61 	2,			/* 19 T_NMI */
62 	4,			/* 20 T_OFLOW */
63 	5,			/* 21 T_BOUND */
64 	7,			/* 22 T_DNA */
65 	8,			/* 23 T_DOUBLEFLT */
66 	9,			/* 24 T_FPOPFLT */
67 	10,			/* 25 T_TSSFLT */
68 	11,			/* 26 T_SEGNPFLT */
69 	12,			/* 27 T_STKFLT */
70 	18,			/* 28 T_MCHK */
71 	19,			/* 29 T_XMMFLT */
72 	15			/* 30 T_RESERVED */
73 };
74 
75 /*
76  * If FreeBSD & Linux have a difference of opinion about what a trap
77  * means, deal with it here.
78  */
79 int
80 linux_translate_traps(int signal, int trap_code)
81 {
82 	if (signal != SIGBUS)
83 		return (signal);
84 	switch (trap_code) {
85 	case T_PROTFLT:
86 	case T_TSSFLT:
87 	case T_DOUBLEFLT:
88 	case T_PAGEFLT:
89 		return (SIGSEGV);
90 	default:
91 		return (signal);
92 	}
93 }
94 
95 int
96 bsd_to_linux_trapcode(int code)
97 {
98 
99 	return (code < nitems(_bsd_to_linux_trapcode) ?
100 	    _bsd_to_linux_trapcode[code] : LINUX_T_UNKNOWN);
101 }
102 
103 u_int
104 linux_x86_elf_hwcap2(void)
105 {
106 	static u_int elf_hwcap2 = 0;
107 	static bool elf_hwcap2_valid = false;
108 
109 	if (!elf_hwcap2_valid) {
110 		if ((cpu_stdext_feature & CPUID_STDEXT_FSGSBASE) != 0)
111 			elf_hwcap2 |= LINUX_HWCAP2_FSGSBASE;
112 		elf_hwcap2_valid = true;
113 	}
114 	return (elf_hwcap2);
115 }
116