|
| 1 | +/* |
| 2 | + * Copyright(c) 2017 Intel Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of version 2 of the GNU General Public License as |
| 6 | + * published by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but |
| 9 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | + * General Public License for more details. |
| 12 | + * |
| 13 | + * This code is based in part on work published here: |
| 14 | + * |
| 15 | + * https://github.com/IAIK/KAISER |
| 16 | + * |
| 17 | + * The original work was written by and and signed off by for the Linux |
| 18 | + * kernel by: |
| 19 | + * |
| 20 | + * Signed-off-by: Richard Fellner <richard.fellner@student.tugraz.at> |
| 21 | + * Signed-off-by: Moritz Lipp <moritz.lipp@iaik.tugraz.at> |
| 22 | + * Signed-off-by: Daniel Gruss <daniel.gruss@iaik.tugraz.at> |
| 23 | + * Signed-off-by: Michael Schwarz <michael.schwarz@iaik.tugraz.at> |
| 24 | + * |
| 25 | + * Major changes to the original code by: Dave Hansen <dave.hansen@intel.com> |
| 26 | + * Mostly rewritten by Thomas Gleixner <tglx@linutronix.de> and |
| 27 | + * Andy Lutomirsky <luto@amacapital.net> |
| 28 | + */ |
| 29 | +#include <linux/kernel.h> |
| 30 | +#include <linux/errno.h> |
| 31 | +#include <linux/string.h> |
| 32 | +#include <linux/types.h> |
| 33 | +#include <linux/bug.h> |
| 34 | +#include <linux/init.h> |
| 35 | +#include <linux/spinlock.h> |
| 36 | +#include <linux/mm.h> |
| 37 | +#include <linux/uaccess.h> |
| 38 | + |
| 39 | +#include <asm/cpufeature.h> |
| 40 | +#include <asm/hypervisor.h> |
| 41 | +#include <asm/cmdline.h> |
| 42 | +#include <asm/pti.h> |
| 43 | +#include <asm/pgtable.h> |
| 44 | +#include <asm/pgalloc.h> |
| 45 | +#include <asm/tlbflush.h> |
| 46 | +#include <asm/desc.h> |
| 47 | + |
| 48 | +#undef pr_fmt |
| 49 | +#define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt |
| 50 | + |
| 51 | +static void __init pti_print_if_insecure(const char *reason) |
| 52 | +{ |
| 53 | + if (boot_cpu_has_bug(X86_BUG_CPU_INSECURE)) |
| 54 | + pr_info("%s\n", reason); |
| 55 | +} |
| 56 | + |
| 57 | +void __init pti_check_boottime_disable(void) |
| 58 | +{ |
| 59 | + if (hypervisor_is_type(X86_HYPER_XEN_PV)) { |
| 60 | + pti_print_if_insecure("disabled on XEN PV."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (cmdline_find_option_bool(boot_command_line, "nopti")) { |
| 65 | + pti_print_if_insecure("disabled on command line."); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (!boot_cpu_has_bug(X86_BUG_CPU_INSECURE)) |
| 70 | + return; |
| 71 | + |
| 72 | + setup_force_cpu_cap(X86_FEATURE_PTI); |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | + * Initialize kernel page table isolation |
| 77 | + */ |
| 78 | +void __init pti_init(void) |
| 79 | +{ |
| 80 | + if (!static_cpu_has(X86_FEATURE_PTI)) |
| 81 | + return; |
| 82 | + |
| 83 | + pr_info("enabled\n"); |
| 84 | +} |
0 commit comments