Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / arch / x86 / kernel / livepatch.c
1 /*
2  * livepatch.c - x86-specific Kernel Live Patching Core
3  *
4  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5  * Copyright (C) 2014 SUSE
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <asm/cacheflush.h>
24 #include <asm/page_types.h>
25 #include <asm/elf.h>
26 #include <asm/livepatch.h>
27
28 /**
29  * klp_write_module_reloc() - write a relocation in a module
30  * @mod:        module in which the section to be modified is found
31  * @type:       ELF relocation type (see asm/elf.h)
32  * @loc:        address that the relocation should be written to
33  * @value:      relocation value (sym address + addend)
34  *
35  * This function writes a relocation to the specified location for
36  * a particular module.
37  */
38 int klp_write_module_reloc(struct module *mod, unsigned long type,
39                            unsigned long loc, unsigned long value)
40 {
41         int ret, numpages, size = 4;
42         bool readonly;
43         unsigned long val;
44         unsigned long core = (unsigned long)mod->module_core;
45         unsigned long core_ro_size = mod->core_ro_size;
46         unsigned long core_size = mod->core_size;
47
48         switch (type) {
49         case R_X86_64_NONE:
50                 return 0;
51         case R_X86_64_64:
52                 val = value;
53                 size = 8;
54                 break;
55         case R_X86_64_32:
56                 val = (u32)value;
57                 break;
58         case R_X86_64_32S:
59                 val = (s32)value;
60                 break;
61         case R_X86_64_PC32:
62                 val = (u32)(value - loc);
63                 break;
64         default:
65                 /* unsupported relocation type */
66                 return -EINVAL;
67         }
68
69         if (loc < core || loc >= core + core_size)
70                 /* loc does not point to any symbol inside the module */
71                 return -EINVAL;
72
73         if (loc < core + core_ro_size)
74                 readonly = true;
75         else
76                 readonly = false;
77
78         /* determine if the relocation spans a page boundary */
79         numpages = ((loc & PAGE_MASK) == ((loc + size) & PAGE_MASK)) ? 1 : 2;
80
81         if (readonly)
82                 set_memory_rw(loc & PAGE_MASK, numpages);
83
84         ret = probe_kernel_write((void *)loc, &val, size);
85
86         if (readonly)
87                 set_memory_ro(loc & PAGE_MASK, numpages);
88
89         return ret;
90 }