^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Alignment access counters and corresponding user-space interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2009 ST Microelectronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 - 2010 Paul Mundt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/alignment.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static unsigned long se_user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static unsigned long se_sys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static unsigned long se_half;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static unsigned long se_word;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static unsigned long se_dword;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static unsigned long se_multi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) valid! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int se_usermode = UM_WARN | UM_FIXUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* 0: no warning 1: print a warning message, disabled by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int se_kernmode_warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) core_param(alignment, se_usermode, int, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void inc_unaligned_byte_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) se_half++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void inc_unaligned_word_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) se_word++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void inc_unaligned_dword_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) se_dword++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void inc_unaligned_multi_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) se_multi++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) void inc_unaligned_user_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) se_user++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void inc_unaligned_kernel_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) se_sys++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * This defaults to the global policy which can be set from the command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * line, while processes can overload their preferences via prctl().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned int unaligned_user_action(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int action = se_usermode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (current->thread.flags & SH_THREAD_UAC_SIGBUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) action &= ~UM_FIXUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) action |= UM_SIGNAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (current->thread.flags & SH_THREAD_UAC_NOPRINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) action &= ~UM_WARN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int get_unalign_ctl(struct task_struct *tsk, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return put_user(tsk->thread.flags & SH_THREAD_UAC_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) (unsigned int __user *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) tsk->thread.flags = (tsk->thread.flags & ~SH_THREAD_UAC_MASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) (val & SH_THREAD_UAC_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) void unaligned_fixups_notify(struct task_struct *tsk, insn_size_t insn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (user_mode(regs) && (se_usermode & UM_WARN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pr_notice_ratelimited("Fixing up unaligned userspace access "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) tsk->comm, task_pid_nr(tsk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) (void *)instruction_pointer(regs), insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) else if (se_kernmode_warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pr_notice_ratelimited("Fixing up unaligned kernel access "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) tsk->comm, task_pid_nr(tsk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) (void *)instruction_pointer(regs), insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static const char *se_usermode_action[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "ignored",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "warn",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "fixup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "fixup+warn",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) "signal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) "signal+warn"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int alignment_proc_show(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) seq_printf(m, "User:\t\t%lu\n", se_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) seq_printf(m, "System:\t\t%lu\n", se_sys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) seq_printf(m, "Half:\t\t%lu\n", se_half);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) seq_printf(m, "Word:\t\t%lu\n", se_word);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) seq_printf(m, "DWord:\t\t%lu\n", se_dword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) seq_printf(m, "Multi:\t\t%lu\n", se_multi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) seq_printf(m, "User faults:\t%i (%s)\n", se_usermode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) se_usermode_action[se_usermode]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) seq_printf(m, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) se_kernmode_warn ? "+warn" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int alignment_proc_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return single_open(file, alignment_proc_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static ssize_t alignment_proc_write(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) const char __user *buffer, size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int *data = PDE_DATA(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) char mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (get_user(mode, buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (mode >= '0' && mode <= '5')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *data = mode - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static const struct proc_ops alignment_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .proc_open = alignment_proc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .proc_release = single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .proc_write = alignment_proc_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * This needs to be done after sysctl_init, otherwise sys/ will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * overwritten. Actually, this shouldn't be in sys/ at all since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * it isn't a sysctl, and it doesn't contain sysctl information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * We now locate it in /proc/cpu/alignment instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int __init alignment_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct proc_dir_entry *dir, *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dir = proc_mkdir("cpu", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) &alignment_proc_ops, &se_usermode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) &alignment_proc_ops, &se_kernmode_warn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fs_initcall(alignment_init);