Jon Medhurst | 0ab4c02 | 2011-07-06 11:25:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/kernel/kprobes-common.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>. |
| 5 | * |
Jon Medhurst | 6c8df33 | 2011-07-07 10:21:40 +0100 | [diff] [blame] | 6 | * Some contents moved here from arch/arm/include/asm/kprobes-arm.c which is |
| 7 | * Copyright (C) 2006, 2007 Motorola Inc. |
| 8 | * |
Jon Medhurst | 0ab4c02 | 2011-07-06 11:25:18 +0100 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/kprobes.h> |
| 16 | |
| 17 | #include "kprobes.h" |
| 18 | |
| 19 | |
Jon Medhurst | aea4902 | 2011-07-07 19:58:29 +0100 | [diff] [blame^] | 20 | #ifndef find_str_pc_offset |
| 21 | |
Jon Medhurst | 6c8df33 | 2011-07-07 10:21:40 +0100 | [diff] [blame] | 22 | /* |
| 23 | * For STR and STM instructions, an ARM core may choose to use either |
| 24 | * a +8 or a +12 displacement from the current instruction's address. |
| 25 | * Whichever value is chosen for a given core, it must be the same for |
| 26 | * both instructions and may not change. This function measures it. |
| 27 | */ |
| 28 | |
| 29 | int str_pc_offset; |
| 30 | |
| 31 | void __init find_str_pc_offset(void) |
| 32 | { |
| 33 | int addr, scratch, ret; |
| 34 | |
| 35 | __asm__ ( |
| 36 | "sub %[ret], pc, #4 \n\t" |
| 37 | "str pc, %[addr] \n\t" |
| 38 | "ldr %[scr], %[addr] \n\t" |
| 39 | "sub %[ret], %[scr], %[ret] \n\t" |
| 40 | : [ret] "=r" (ret), [scr] "=r" (scratch), [addr] "+m" (addr)); |
| 41 | |
| 42 | str_pc_offset = ret; |
| 43 | } |
| 44 | |
Jon Medhurst | aea4902 | 2011-07-07 19:58:29 +0100 | [diff] [blame^] | 45 | #endif /* !find_str_pc_offset */ |
| 46 | |
Jon Medhurst | 6c8df33 | 2011-07-07 10:21:40 +0100 | [diff] [blame] | 47 | |
| 48 | void __init arm_kprobe_decode_init(void) |
| 49 | { |
| 50 | find_str_pc_offset(); |
| 51 | } |
| 52 | |
| 53 | |
Jon Medhurst | 0ab4c02 | 2011-07-06 11:25:18 +0100 | [diff] [blame] | 54 | static unsigned long __kprobes __check_eq(unsigned long cpsr) |
| 55 | { |
| 56 | return cpsr & PSR_Z_BIT; |
| 57 | } |
| 58 | |
| 59 | static unsigned long __kprobes __check_ne(unsigned long cpsr) |
| 60 | { |
| 61 | return (~cpsr) & PSR_Z_BIT; |
| 62 | } |
| 63 | |
| 64 | static unsigned long __kprobes __check_cs(unsigned long cpsr) |
| 65 | { |
| 66 | return cpsr & PSR_C_BIT; |
| 67 | } |
| 68 | |
| 69 | static unsigned long __kprobes __check_cc(unsigned long cpsr) |
| 70 | { |
| 71 | return (~cpsr) & PSR_C_BIT; |
| 72 | } |
| 73 | |
| 74 | static unsigned long __kprobes __check_mi(unsigned long cpsr) |
| 75 | { |
| 76 | return cpsr & PSR_N_BIT; |
| 77 | } |
| 78 | |
| 79 | static unsigned long __kprobes __check_pl(unsigned long cpsr) |
| 80 | { |
| 81 | return (~cpsr) & PSR_N_BIT; |
| 82 | } |
| 83 | |
| 84 | static unsigned long __kprobes __check_vs(unsigned long cpsr) |
| 85 | { |
| 86 | return cpsr & PSR_V_BIT; |
| 87 | } |
| 88 | |
| 89 | static unsigned long __kprobes __check_vc(unsigned long cpsr) |
| 90 | { |
| 91 | return (~cpsr) & PSR_V_BIT; |
| 92 | } |
| 93 | |
| 94 | static unsigned long __kprobes __check_hi(unsigned long cpsr) |
| 95 | { |
| 96 | cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */ |
| 97 | return cpsr & PSR_C_BIT; |
| 98 | } |
| 99 | |
| 100 | static unsigned long __kprobes __check_ls(unsigned long cpsr) |
| 101 | { |
| 102 | cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */ |
| 103 | return (~cpsr) & PSR_C_BIT; |
| 104 | } |
| 105 | |
| 106 | static unsigned long __kprobes __check_ge(unsigned long cpsr) |
| 107 | { |
| 108 | cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 109 | return (~cpsr) & PSR_N_BIT; |
| 110 | } |
| 111 | |
| 112 | static unsigned long __kprobes __check_lt(unsigned long cpsr) |
| 113 | { |
| 114 | cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 115 | return cpsr & PSR_N_BIT; |
| 116 | } |
| 117 | |
| 118 | static unsigned long __kprobes __check_gt(unsigned long cpsr) |
| 119 | { |
| 120 | unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 121 | temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */ |
| 122 | return (~temp) & PSR_N_BIT; |
| 123 | } |
| 124 | |
| 125 | static unsigned long __kprobes __check_le(unsigned long cpsr) |
| 126 | { |
| 127 | unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */ |
| 128 | temp |= (cpsr << 1); /* PSR_N_BIT |= PSR_Z_BIT */ |
| 129 | return temp & PSR_N_BIT; |
| 130 | } |
| 131 | |
| 132 | static unsigned long __kprobes __check_al(unsigned long cpsr) |
| 133 | { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | kprobe_check_cc * const kprobe_condition_checks[16] = { |
| 138 | &__check_eq, &__check_ne, &__check_cs, &__check_cc, |
| 139 | &__check_mi, &__check_pl, &__check_vs, &__check_vc, |
| 140 | &__check_hi, &__check_ls, &__check_ge, &__check_lt, |
| 141 | &__check_gt, &__check_le, &__check_al, &__check_al |
| 142 | }; |