Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mundt | a23ba43 | 2007-11-28 20:19:38 +0900 | [diff] [blame] | 2 | * arch/sh/mm/extable_64.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 Richard Curnow |
| 5 | * Copyright (C) 2003, 2004 Paul Mundt |
| 6 | * |
| 7 | * Cloned from the 2.5 SH version.. |
Paul Mundt | a23ba43 | 2007-11-28 20:19:38 +0900 | [diff] [blame] | 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
Thomas Meyer | a94c33d | 2017-07-10 15:51:58 -0700 | [diff] [blame] | 13 | #include <linux/bsearch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/rwsem.h> |
Paul Gortmaker | d92280d | 2016-07-23 14:01:45 -0400 | [diff] [blame] | 15 | #include <linux/extable.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | extern unsigned long copy_user_memcpy, copy_user_memcpy_end; |
| 19 | extern void __copy_user_fixup(void); |
| 20 | |
| 21 | static const struct exception_table_entry __copy_user_fixup_ex = { |
| 22 | .fixup = (unsigned long)&__copy_user_fixup, |
| 23 | }; |
| 24 | |
Paul Mundt | a23ba43 | 2007-11-28 20:19:38 +0900 | [diff] [blame] | 25 | /* |
| 26 | * Some functions that may trap due to a bad user-mode address have too |
| 27 | * many loads and stores in them to make it at all practical to label |
| 28 | * each one and put them all in the main exception table. |
| 29 | * |
| 30 | * In particular, the fast memcpy routine is like this. It's fix-up is |
| 31 | * just to fall back to a slow byte-at-a-time copy, which is handled the |
| 32 | * conventional way. So it's functionally OK to just handle any trap |
| 33 | * occurring in the fast memcpy with that fixup. |
| 34 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static const struct exception_table_entry *check_exception_ranges(unsigned long addr) |
| 36 | { |
| 37 | if ((addr >= (unsigned long)©_user_memcpy) && |
| 38 | (addr <= (unsigned long)©_user_memcpy_end)) |
| 39 | return &__copy_user_fixup_ex; |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
Thomas Meyer | a94c33d | 2017-07-10 15:51:58 -0700 | [diff] [blame] | 44 | static int cmp_ex_search(const void *key, const void *elt) |
| 45 | { |
| 46 | const struct exception_table_entry *_elt = elt; |
| 47 | unsigned long _key = *(unsigned long *)key; |
| 48 | |
| 49 | /* avoid overflow */ |
| 50 | if (_key > _elt->insn) |
| 51 | return 1; |
| 52 | if (_key < _elt->insn) |
| 53 | return -1; |
| 54 | return 0; |
| 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* Simple binary search */ |
| 58 | const struct exception_table_entry * |
Thomas Meyer | a94c33d | 2017-07-10 15:51:58 -0700 | [diff] [blame] | 59 | search_extable(const struct exception_table_entry *base, |
| 60 | const size_t num, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | unsigned long value) |
| 62 | { |
| 63 | const struct exception_table_entry *mid; |
| 64 | |
| 65 | mid = check_exception_ranges(value); |
| 66 | if (mid) |
| 67 | return mid; |
| 68 | |
Thomas Meyer | a94c33d | 2017-07-10 15:51:58 -0700 | [diff] [blame] | 69 | return bsearch(&value, base, num, |
| 70 | sizeof(struct exception_table_entry), cmp_ex_search); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int fixup_exception(struct pt_regs *regs) |
| 74 | { |
| 75 | const struct exception_table_entry *fixup; |
| 76 | |
| 77 | fixup = search_exception_tables(regs->pc); |
| 78 | if (fixup) { |
| 79 | regs->pc = fixup->fixup; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |