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