2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013-2015
5 * Copyright (c) Valentine Sinitsyn, 2014
8 * Jan Kiszka <jan.kiszka@siemens.com>
9 * Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
19 #include <linux/miscdevice.h>
20 #include <linux/firmware.h>
22 #include <linux/smp.h>
23 #include <linux/uaccess.h>
24 #include <linux/reboot.h>
25 #include <linux/vmalloc.h>
28 #include <asm/cacheflush.h>
29 #include <asm/tlbflush.h>
32 #include "jailhouse.h"
37 #include <jailhouse/header.h>
38 #include <jailhouse/hypercall.h>
39 #include <generated/version.h>
42 #error 64-bit kernel required!
45 #if JAILHOUSE_CELL_ID_NAMELEN != JAILHOUSE_CELL_NAME_MAXLEN
46 # warning JAILHOUSE_CELL_ID_NAMELEN and JAILHOUSE_CELL_NAME_MAXLEN out of sync!
49 /* For compatibility with older kernel versions */
50 #include <linux/version.h>
53 #define JAILHOUSE_AMD_FW_NAME "jailhouse-amd.bin"
54 #define JAILHOUSE_INTEL_FW_NAME "jailhouse-intel.bin"
56 #define JAILHOUSE_FW_NAME "jailhouse.bin"
59 MODULE_DESCRIPTION("Management driver for Jailhouse partitioning hypervisor");
60 MODULE_LICENSE("GPL");
62 MODULE_FIRMWARE(JAILHOUSE_AMD_FW_NAME);
63 MODULE_FIRMWARE(JAILHOUSE_INTEL_FW_NAME);
65 MODULE_FIRMWARE(JAILHOUSE_FW_NAME);
67 MODULE_VERSION(JAILHOUSE_VERSION);
69 DEFINE_MUTEX(jailhouse_lock);
70 bool jailhouse_enabled;
72 static struct device *jailhouse_dev;
73 static void *hypervisor_mem;
74 static unsigned long hv_core_and_percpu_size;
75 static atomic_t call_done;
76 static int error_code;
79 bool jailhouse_use_vmcall;
81 static void init_hypercall(void)
83 jailhouse_use_vmcall = boot_cpu_has(X86_FEATURE_VMX);
85 #else /* !CONFIG_X86 */
86 static void init_hypercall(void)
91 static long get_max_cpus(u32 cpu_set_size,
92 const struct jailhouse_system __user *system_config)
95 (u8 __user *)jailhouse_cell_cpu_set(
96 (const struct jailhouse_cell_desc * __force)
97 &system_config->root_cell);
98 unsigned int pos = cpu_set_size;
103 if (get_user(bitmap, cpu_set + pos))
105 max_cpu_id = fls(bitmap);
107 return pos * 8 + max_cpu_id;
112 void *jailhouse_ioremap(phys_addr_t phys, unsigned long virt,
115 struct vm_struct *vma;
117 size = PAGE_ALIGN(size);
119 vma = __get_vm_area(size, VM_IOREMAP, virt,
120 virt + size + PAGE_SIZE);
122 vma = __get_vm_area(size, VM_IOREMAP, VMALLOC_START,
126 vma->phys_addr = phys;
128 if (ioremap_page_range((unsigned long)vma->addr,
129 (unsigned long)vma->addr + size, phys,
138 static void enter_hypervisor(void *info)
140 struct jailhouse_header *header = info;
141 unsigned int cpu = smp_processor_id();
142 int (*entry)(unsigned int);
145 entry = header->entry + (unsigned long) hypervisor_mem;
147 if (cpu < header->max_cpus)
148 /* either returns 0 or the same error code across all CPUs */
156 #if defined(CONFIG_X86) && LINUX_VERSION_CODE >= KERNEL_VERSION(4,0,0)
157 /* on Intel, VMXE is now on - update the shadow */
161 atomic_inc(&call_done);
164 static inline const char * jailhouse_fw_name(void)
167 if (boot_cpu_has(X86_FEATURE_SVM))
168 return JAILHOUSE_AMD_FW_NAME;
169 if (boot_cpu_has(X86_FEATURE_VMX))
170 return JAILHOUSE_INTEL_FW_NAME;
173 return JAILHOUSE_FW_NAME;
177 static int jailhouse_cmd_enable(struct jailhouse_system __user *arg)
179 const struct firmware *hypervisor;
180 struct jailhouse_system config_header;
181 struct jailhouse_system *config;
182 struct jailhouse_memory *hv_mem = &config_header.hypervisor_memory;
183 struct jailhouse_header *header;
184 unsigned long remap_addr = 0;
185 void __iomem *console = NULL;
186 unsigned long config_size;
191 fw_name = jailhouse_fw_name();
193 pr_err("jailhouse: Missing or unsupported HVM technology\n");
197 if (copy_from_user(&config_header, arg, sizeof(config_header)))
200 if (memcmp(config_header.signature, JAILHOUSE_SYSTEM_SIGNATURE,
201 sizeof(config_header.signature)) != 0) {
202 pr_err("jailhouse: Not a system configuration\n");
206 config_header.root_cell.name[JAILHOUSE_CELL_NAME_MAXLEN] = 0;
208 max_cpus = get_max_cpus(config_header.root_cell.cpu_set_size, arg);
211 if (max_cpus > UINT_MAX)
214 if (mutex_lock_interruptible(&jailhouse_lock) != 0)
218 if (jailhouse_enabled || !try_module_get(THIS_MODULE))
221 err = request_firmware(&hypervisor, fw_name, jailhouse_dev);
223 pr_err("jailhouse: Missing hypervisor image %s\n", fw_name);
224 goto error_put_module;
227 header = (struct jailhouse_header *)hypervisor->data;
230 if (memcmp(header->signature, JAILHOUSE_SIGNATURE,
231 sizeof(header->signature)) != 0 ||
232 hypervisor->size >= hv_mem->size)
233 goto error_release_fw;
235 hv_core_and_percpu_size = header->core_size +
236 max_cpus * header->percpu_size;
237 config_size = jailhouse_system_config_size(&config_header);
238 if (hv_core_and_percpu_size >= hv_mem->size ||
239 config_size >= hv_mem->size - hv_core_and_percpu_size)
240 goto error_release_fw;
242 #ifdef JAILHOUSE_BORROW_ROOT_PT
243 remap_addr = JAILHOUSE_BASE;
245 hypervisor_mem = jailhouse_ioremap(hv_mem->phys_start, remap_addr,
247 if (!hypervisor_mem) {
248 pr_err("jailhouse: Unable to map RAM reserved for hypervisor "
249 "at %08lx\n", (unsigned long)hv_mem->phys_start);
250 goto error_release_fw;
253 memcpy(hypervisor_mem, hypervisor->data, hypervisor->size);
254 memset(hypervisor_mem + hypervisor->size, 0,
255 hv_mem->size - hypervisor->size);
257 header = (struct jailhouse_header *)hypervisor_mem;
258 header->max_cpus = max_cpus;
261 * ARMv8 requires to clean D-cache and invalidate I-cache for memory
262 * containing new instructions. On x86 this is a NOP. On ARMv7 the
263 * firmware does its own cache maintenance, so it is an
264 * extraneous (but harmless) flush.
266 flush_icache_range((unsigned long)hypervisor_mem,
267 (unsigned long)(hypervisor_mem + header->core_size));
269 config = (struct jailhouse_system *)
270 (hypervisor_mem + hv_core_and_percpu_size);
271 if (copy_from_user(config, arg, config_size)) {
276 if (config->debug_console.flags & JAILHOUSE_MEM_IO) {
277 #ifdef JAILHOUSE_BORROW_ROOT_PT
278 console = ioremap(config->debug_console.phys_start,
279 config->debug_console.size);
282 pr_err("jailhouse: Unable to map hypervisor debug "
283 "console at %08lx\n",
284 (unsigned long)config->debug_console.phys_start);
287 /* The hypervisor has no notion of address spaces, so we need
288 * to enforce conversion. */
289 header->debug_console_base = (void * __force)console;
291 header->debug_console_base =
292 (void * __force) config->debug_console.phys_start;
296 err = jailhouse_cell_prepare_root(&config->root_cell);
304 header->online_cpus = num_online_cpus();
306 atomic_set(&call_done, 0);
307 on_each_cpu(enter_hypervisor, header, 0);
308 while (atomic_read(&call_done) != num_online_cpus())
315 goto error_free_cell;
321 release_firmware(hypervisor);
323 jailhouse_cell_register_root();
325 jailhouse_enabled = true;
327 mutex_unlock(&jailhouse_lock);
329 pr_info("The Jailhouse is opening.\n");
334 jailhouse_cell_delete_root();
337 vunmap(hypervisor_mem);
342 release_firmware(hypervisor);
345 module_put(THIS_MODULE);
348 mutex_unlock(&jailhouse_lock);
352 static void leave_hypervisor(void *info)
357 /* Touch each hypervisor page we may need during the switch so that
358 * the active mm definitely contains all mappings. At least x86 does
359 * not support taking any faults while switching worlds. */
360 for (page = hypervisor_mem;
361 page < hypervisor_mem + hv_core_and_percpu_size;
363 readl((void __iomem *)page);
365 /* either returns 0 or the same error code across all CPUs */
366 err = jailhouse_call(JAILHOUSE_HC_DISABLE);
370 #if defined(CONFIG_X86) && LINUX_VERSION_CODE >= KERNEL_VERSION(4,0,0)
371 /* on Intel, VMXE is now off - update the shadow */
375 atomic_inc(&call_done);
378 static int jailhouse_cmd_disable(void)
382 if (mutex_lock_interruptible(&jailhouse_lock) != 0)
385 if (!jailhouse_enabled) {
394 if (num_online_cpus() != cpumask_weight(&root_cell->cpus_assigned)) {
396 * Not all assigned CPUs are currently online. If we disable
397 * now, we will loose the offlined ones.
406 atomic_set(&call_done, 0);
407 on_each_cpu(leave_hypervisor, NULL, 0);
408 while (atomic_read(&call_done) != num_online_cpus())
417 vunmap(hypervisor_mem);
419 jailhouse_cell_delete_all();
420 jailhouse_enabled = false;
421 module_put(THIS_MODULE);
423 pr_info("The Jailhouse was closed.\n");
426 mutex_unlock(&jailhouse_lock);
431 static long jailhouse_ioctl(struct file *file, unsigned int ioctl,
437 case JAILHOUSE_ENABLE:
438 err = jailhouse_cmd_enable(
439 (struct jailhouse_system __user *)arg);
441 case JAILHOUSE_DISABLE:
442 err = jailhouse_cmd_disable();
444 case JAILHOUSE_CELL_CREATE:
445 err = jailhouse_cmd_cell_create(
446 (struct jailhouse_cell_create __user *)arg);
448 case JAILHOUSE_CELL_LOAD:
449 err = jailhouse_cmd_cell_load(
450 (struct jailhouse_cell_load __user *)arg);
452 case JAILHOUSE_CELL_START:
453 err = jailhouse_cmd_cell_start((const char __user *)arg);
455 case JAILHOUSE_CELL_DESTROY:
456 err = jailhouse_cmd_cell_destroy((const char __user *)arg);
466 static const struct file_operations jailhouse_fops = {
467 .owner = THIS_MODULE,
468 .unlocked_ioctl = jailhouse_ioctl,
469 .compat_ioctl = jailhouse_ioctl,
470 .llseek = noop_llseek,
473 static struct miscdevice jailhouse_misc_dev = {
474 .minor = MISC_DYNAMIC_MINOR,
476 .fops = &jailhouse_fops,
479 static int jailhouse_shutdown_notify(struct notifier_block *unused1,
480 unsigned long unused2, void *unused3)
484 err = jailhouse_cmd_disable();
485 if (err && err != -EINVAL)
486 pr_emerg("jailhouse: ordered shutdown failed!\n");
491 static struct notifier_block jailhouse_shutdown_nb = {
492 .notifier_call = jailhouse_shutdown_notify,
495 static int __init jailhouse_init(void)
499 jailhouse_dev = root_device_register("jailhouse");
500 if (IS_ERR(jailhouse_dev))
501 return PTR_ERR(jailhouse_dev);
503 err = jailhouse_sysfs_init(jailhouse_dev);
507 err = misc_register(&jailhouse_misc_dev);
511 err = jailhouse_pci_register();
515 register_reboot_notifier(&jailhouse_shutdown_nb);
521 misc_deregister(&jailhouse_misc_dev);
524 jailhouse_sysfs_exit(jailhouse_dev);
527 root_device_unregister(jailhouse_dev);
531 static void __exit jailhouse_exit(void)
533 unregister_reboot_notifier(&jailhouse_shutdown_nb);
534 misc_deregister(&jailhouse_misc_dev);
535 jailhouse_sysfs_exit(jailhouse_dev);
536 jailhouse_pci_unregister();
537 root_device_unregister(jailhouse_dev);
540 module_init(jailhouse_init);
541 module_exit(jailhouse_exit);