blob: 143a5c193ed3d0d640581a1cb03f97b67dcaedc2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Qiaowei Ren57319d82014-11-14 07:18:27 -08002#ifndef _ASM_X86_MPX_H
3#define _ASM_X86_MPX_H
4
5#include <linux/types.h>
Ingo Molnar589ee622017-02-04 00:16:44 +01006#include <linux/mm_types.h>
7
Qiaowei Ren57319d82014-11-14 07:18:27 -08008#include <asm/ptrace.h>
Dave Hansenfcc7ffd2014-11-14 07:18:28 -08009#include <asm/insn.h>
Qiaowei Ren57319d82014-11-14 07:18:27 -080010
Dave Hansenfe3d1972014-11-14 07:18:29 -080011/*
12 * NULL is theoretically a valid place to put the bounds
13 * directory, so point this at an invalid address.
14 */
15#define MPX_INVALID_BOUNDS_DIR ((void __user *)-1)
16#define MPX_BNDCFG_ENABLE_FLAG 0x1
17#define MPX_BD_ENTRY_VALID_FLAG 0x1
18
Dave Hansen613fcb72015-06-07 11:37:05 -070019/*
20 * The upper 28 bits [47:20] of the virtual address in 64-bit
21 * are used to index into bounds directory (BD).
22 *
23 * The directory is 2G (2^31) in size, and with 8-byte entries
24 * it has 2^28 entries.
Qiaowei Ren57319d82014-11-14 07:18:27 -080025 */
Dave Hansen613fcb72015-06-07 11:37:05 -070026#define MPX_BD_SIZE_BYTES_64 (1UL<<31)
27#define MPX_BD_ENTRY_BYTES_64 8
28#define MPX_BD_NR_ENTRIES_64 (MPX_BD_SIZE_BYTES_64/MPX_BD_ENTRY_BYTES_64)
29
30/*
31 * The 32-bit directory is 4MB (2^22) in size, and with 4-byte
32 * entries it has 2^20 entries.
Qiaowei Ren57319d82014-11-14 07:18:27 -080033 */
Dave Hansen613fcb72015-06-07 11:37:05 -070034#define MPX_BD_SIZE_BYTES_32 (1UL<<22)
35#define MPX_BD_ENTRY_BYTES_32 4
36#define MPX_BD_NR_ENTRIES_32 (MPX_BD_SIZE_BYTES_32/MPX_BD_ENTRY_BYTES_32)
Qiaowei Ren57319d82014-11-14 07:18:27 -080037
Dave Hansen613fcb72015-06-07 11:37:05 -070038/*
39 * A 64-bit table is 4MB total in size, and an entry is
40 * 4 64-bit pointers in size.
41 */
42#define MPX_BT_SIZE_BYTES_64 (1UL<<22)
43#define MPX_BT_ENTRY_BYTES_64 32
44#define MPX_BT_NR_ENTRIES_64 (MPX_BT_SIZE_BYTES_64/MPX_BT_ENTRY_BYTES_64)
Qiaowei Ren57319d82014-11-14 07:18:27 -080045
Dave Hansen613fcb72015-06-07 11:37:05 -070046/*
47 * A 32-bit table is 16kB total in size, and an entry is
48 * 4 32-bit pointers in size.
49 */
50#define MPX_BT_SIZE_BYTES_32 (1UL<<14)
51#define MPX_BT_ENTRY_BYTES_32 16
52#define MPX_BT_NR_ENTRIES_32 (MPX_BT_SIZE_BYTES_32/MPX_BT_ENTRY_BYTES_32)
Qiaowei Ren57319d82014-11-14 07:18:27 -080053
Dave Hansenfe3d1972014-11-14 07:18:29 -080054#define MPX_BNDSTA_TAIL 2
55#define MPX_BNDCFG_TAIL 12
56#define MPX_BNDSTA_ADDR_MASK (~((1UL<<MPX_BNDSTA_TAIL)-1))
Dave Hansenfe3d1972014-11-14 07:18:29 -080057#define MPX_BNDCFG_ADDR_MASK (~((1UL<<MPX_BNDCFG_TAIL)-1))
Qiaowei Ren57319d82014-11-14 07:18:27 -080058#define MPX_BNDSTA_ERROR_CODE 0x3
59
Eric W. Biederman8d68fa02018-01-03 19:22:04 -060060struct mpx_fault_info {
61 void __user *addr;
62 void __user *lower;
63 void __user *upper;
64};
65
Dave Hansenfcc7ffd2014-11-14 07:18:28 -080066#ifdef CONFIG_X86_INTEL_MPX
Dave Hansen5a28fc92019-04-19 12:47:47 -070067
68extern int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs);
69extern int mpx_handle_bd_fault(void);
70
Dave Hansenfe3d1972014-11-14 07:18:29 -080071static inline int kernel_managing_mpx_tables(struct mm_struct *mm)
72{
Mark Rutlandcb02de92016-12-16 12:40:55 +000073 return (mm->context.bd_addr != MPX_INVALID_BOUNDS_DIR);
Dave Hansenfe3d1972014-11-14 07:18:29 -080074}
Dave Hansen5a28fc92019-04-19 12:47:47 -070075
Dave Hansenfe3d1972014-11-14 07:18:29 -080076static inline void mpx_mm_init(struct mm_struct *mm)
77{
78 /*
79 * NULL is theoretically a valid place to put the bounds
80 * directory, so point this at an invalid address.
81 */
Mark Rutlandcb02de92016-12-16 12:40:55 +000082 mm->context.bd_addr = MPX_INVALID_BOUNDS_DIR;
Dave Hansenfe3d1972014-11-14 07:18:29 -080083}
Kirill A. Shutemov44b04912017-07-17 01:59:51 +030084
Dave Hansen5a28fc92019-04-19 12:47:47 -070085extern void mpx_notify_unmap(struct mm_struct *mm, unsigned long start, unsigned long end);
86extern unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len, unsigned long flags);
87
Dave Hansenfcc7ffd2014-11-14 07:18:28 -080088#else
Eric W. Biederman8d68fa02018-01-03 19:22:04 -060089static inline int mpx_fault_info(struct mpx_fault_info *info, struct pt_regs *regs)
Dave Hansenfcc7ffd2014-11-14 07:18:28 -080090{
Eric W. Biederman8d68fa02018-01-03 19:22:04 -060091 return -EINVAL;
Dave Hansenfcc7ffd2014-11-14 07:18:28 -080092}
Dave Hansen46a6e0c2015-06-07 11:37:02 -070093static inline int mpx_handle_bd_fault(void)
Dave Hansenfe3d1972014-11-14 07:18:29 -080094{
95 return -EINVAL;
96}
97static inline int kernel_managing_mpx_tables(struct mm_struct *mm)
98{
99 return 0;
100}
101static inline void mpx_mm_init(struct mm_struct *mm)
102{
103}
Dave Hansen1de4fa12014-11-14 07:18:31 -0800104static inline void mpx_notify_unmap(struct mm_struct *mm,
Dave Hansen1de4fa12014-11-14 07:18:31 -0800105 unsigned long start, unsigned long end)
106{
107}
Kirill A. Shutemov44b04912017-07-17 01:59:51 +0300108
109static inline unsigned long mpx_unmapped_area_check(unsigned long addr,
110 unsigned long len, unsigned long flags)
111{
112 return addr;
113}
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800114#endif /* CONFIG_X86_INTEL_MPX */
115
Qiaowei Ren57319d82014-11-14 07:18:27 -0800116#endif /* _ASM_X86_MPX_H */