2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013-2015
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include <linux/cpu.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
23 #include <jailhouse/hypercall.h>
25 struct cell *root_cell;
27 static LIST_HEAD(cells);
28 static cpumask_t offlined_cpus;
30 void jailhouse_cell_kobj_release(struct kobject *kobj)
32 struct cell *cell = container_of(kobj, struct cell, kobj);
34 jailhouse_pci_cell_cleanup(cell);
35 vfree(cell->memory_regions);
39 struct cell *jailhouse_cell_create(const struct jailhouse_cell_desc *cell_desc)
44 if (cell_desc->num_memory_regions >=
45 ULONG_MAX / sizeof(struct jailhouse_memory))
46 return ERR_PTR(-EINVAL);
48 cell = kzalloc(sizeof(*cell), GFP_KERNEL);
50 return ERR_PTR(-ENOMEM);
52 INIT_LIST_HEAD(&cell->entry);
54 bitmap_copy(cpumask_bits(&cell->cpus_assigned),
55 jailhouse_cell_cpu_set(cell_desc),
56 min(nr_cpumask_bits, (int)cell_desc->cpu_set_size * 8));
58 cell->num_memory_regions = cell_desc->num_memory_regions;
59 cell->memory_regions = vmalloc(sizeof(struct jailhouse_memory) *
60 cell->num_memory_regions);
61 if (!cell->memory_regions) {
63 return ERR_PTR(-ENOMEM);
66 memcpy(cell->memory_regions, jailhouse_cell_mem_regions(cell_desc),
67 sizeof(struct jailhouse_memory) * cell->num_memory_regions);
69 err = jailhouse_pci_cell_setup(cell, cell_desc);
71 vfree(cell->memory_regions);
76 err = jailhouse_sysfs_cell_create(cell, cell_desc->name);
78 /* cleanup done by jailhouse_sysfs_cell_create */
84 void jailhouse_cell_register(struct cell *cell)
86 list_add_tail(&cell->entry, &cells);
87 jailhouse_sysfs_cell_register(cell);
90 static struct cell *find_cell(struct jailhouse_cell_id *cell_id)
94 list_for_each_entry(cell, &cells, entry)
95 if (cell_id->id == cell->id ||
96 (cell_id->id == JAILHOUSE_CELL_ID_UNUSED &&
97 strcmp(kobject_name(&cell->kobj), cell_id->name) == 0))
102 void jailhouse_cell_delete(struct cell *cell)
104 list_del(&cell->entry);
105 jailhouse_sysfs_cell_delete(cell);
108 int jailhouse_cell_prepare_root(const struct jailhouse_cell_desc *cell_desc)
110 root_cell = jailhouse_cell_create(cell_desc);
111 if (IS_ERR(root_cell))
112 return PTR_ERR(root_cell);
114 cpumask_and(&root_cell->cpus_assigned, &root_cell->cpus_assigned,
120 void jailhouse_cell_register_root(void)
122 jailhouse_pci_do_all_devices(root_cell, JAILHOUSE_PCI_TYPE_IVSHMEM,
123 JAILHOUSE_PCI_ACTION_ADD);
126 jailhouse_cell_register(root_cell);
129 void jailhouse_cell_delete_root(void)
131 jailhouse_cell_delete(root_cell);
134 void jailhouse_cell_delete_all(void)
136 struct cell *cell, *tmp;
139 jailhouse_pci_do_all_devices(root_cell, JAILHOUSE_PCI_TYPE_IVSHMEM,
140 JAILHOUSE_PCI_ACTION_DEL);
142 jailhouse_pci_do_all_devices(root_cell, JAILHOUSE_PCI_TYPE_DEVICE,
143 JAILHOUSE_PCI_ACTION_RELEASE);
145 list_for_each_entry_safe(cell, tmp, &cells, entry)
146 jailhouse_cell_delete(cell);
148 for_each_cpu(cpu, &offlined_cpus) {
149 if (cpu_up(cpu) != 0)
150 pr_err("Jailhouse: failed to bring CPU %d back "
152 cpumask_clear_cpu(cpu, &offlined_cpus);
156 int jailhouse_cmd_cell_create(struct jailhouse_cell_create __user *arg)
158 struct jailhouse_cell_create cell_params;
159 struct jailhouse_cell_desc *config;
160 struct jailhouse_cell_id cell_id;
161 void __user *user_config;
166 if (copy_from_user(&cell_params, arg, sizeof(cell_params)))
169 config = kmalloc(cell_params.config_size, GFP_USER | __GFP_NOWARN);
173 user_config = (void __user *)(unsigned long)cell_params.config_address;
174 if (copy_from_user(config, user_config, cell_params.config_size)) {
176 goto kfree_config_out;
179 if (memcmp(config->signature, JAILHOUSE_CELL_DESC_SIGNATURE,
180 sizeof(config->signature)) != 0) {
181 pr_err("jailhouse: Not a cell configuration\n");
183 goto kfree_config_out;
186 config->name[JAILHOUSE_CELL_NAME_MAXLEN] = 0;
188 if (mutex_lock_interruptible(&jailhouse_lock) != 0) {
190 goto kfree_config_out;
193 if (!jailhouse_enabled) {
198 cell_id.id = JAILHOUSE_CELL_ID_UNUSED;
199 memcpy(cell_id.name, config->name, sizeof(cell_id.name));
200 if (find_cell(&cell_id) != NULL) {
205 cell = jailhouse_cell_create(config);
211 if (!cpumask_subset(&cell->cpus_assigned, &root_cell->cpus_assigned)) {
213 goto error_cell_delete;
216 for_each_cpu(cpu, &cell->cpus_assigned) {
217 if (cpu_online(cpu)) {
220 goto error_cpu_online;
221 cpumask_set_cpu(cpu, &offlined_cpus);
223 cpumask_clear_cpu(cpu, &root_cell->cpus_assigned);
226 jailhouse_pci_do_all_devices(cell, JAILHOUSE_PCI_TYPE_DEVICE,
227 JAILHOUSE_PCI_ACTION_CLAIM);
229 id = jailhouse_call_arg1(JAILHOUSE_HC_CELL_CREATE, __pa(config));
232 goto error_cpu_online;
236 jailhouse_cell_register(cell);
238 pr_info("Created Jailhouse cell \"%s\"\n", config->name);
241 mutex_unlock(&jailhouse_lock);
249 for_each_cpu(cpu, &cell->cpus_assigned) {
250 if (!cpu_online(cpu) && cpu_up(cpu) == 0)
251 cpumask_clear_cpu(cpu, &offlined_cpus);
252 cpumask_set_cpu(cpu, &root_cell->cpus_assigned);
256 jailhouse_cell_delete(cell);
260 static int cell_management_prologue(struct jailhouse_cell_id *cell_id,
261 struct cell **cell_ptr)
263 cell_id->name[JAILHOUSE_CELL_ID_NAMELEN] = 0;
265 if (mutex_lock_interruptible(&jailhouse_lock) != 0)
268 if (!jailhouse_enabled) {
269 mutex_unlock(&jailhouse_lock);
273 *cell_ptr = find_cell(cell_id);
274 if (*cell_ptr == NULL) {
275 mutex_unlock(&jailhouse_lock);
281 #define MEM_REQ_FLAGS (JAILHOUSE_MEM_WRITE | JAILHOUSE_MEM_LOADABLE)
283 static int load_image(struct cell *cell,
284 struct jailhouse_preload_image __user *uimage)
286 struct jailhouse_preload_image image;
287 const struct jailhouse_memory *mem;
288 unsigned int regions, page_offs;
289 u64 image_offset, phys_start;
293 if (copy_from_user(&image, uimage, sizeof(image)))
296 mem = cell->memory_regions;
297 for (regions = cell->num_memory_regions; regions > 0; regions--) {
298 image_offset = image.target_address - mem->virt_start;
299 if (image.target_address >= mem->virt_start &&
300 image_offset < mem->size) {
301 if (image.size > mem->size - image_offset ||
302 (mem->flags & MEM_REQ_FLAGS) != MEM_REQ_FLAGS)
311 phys_start = (mem->phys_start + image_offset) & PAGE_MASK;
312 page_offs = offset_in_page(image_offset);
313 image_mem = jailhouse_ioremap(phys_start, 0,
314 PAGE_ALIGN(image.size + page_offs));
316 pr_err("jailhouse: Unable to map cell RAM at %08llx "
317 "for image loading\n",
318 (unsigned long long)(mem->phys_start + image_offset));
322 if (copy_from_user(image_mem + page_offs,
323 (void __user *)(unsigned long)image.source_address,
332 int jailhouse_cmd_cell_load(struct jailhouse_cell_load __user *arg)
334 struct jailhouse_preload_image __user *image = arg->image;
335 struct jailhouse_cell_load cell_load;
340 if (copy_from_user(&cell_load, arg, sizeof(cell_load)))
343 err = cell_management_prologue(&cell_load.cell_id, &cell);
347 err = jailhouse_call_arg1(JAILHOUSE_HC_CELL_SET_LOADABLE, cell->id);
351 for (n = cell_load.num_preload_images; n > 0; n--, image++) {
352 err = load_image(cell, image);
358 mutex_unlock(&jailhouse_lock);
363 int jailhouse_cmd_cell_start(const char __user *arg)
365 struct jailhouse_cell_id cell_id;
369 if (copy_from_user(&cell_id, arg, sizeof(cell_id)))
372 err = cell_management_prologue(&cell_id, &cell);
376 err = jailhouse_call_arg1(JAILHOUSE_HC_CELL_START, cell->id);
378 mutex_unlock(&jailhouse_lock);
383 int jailhouse_cmd_cell_destroy(const char __user *arg)
385 struct jailhouse_cell_id cell_id;
390 if (copy_from_user(&cell_id, arg, sizeof(cell_id)))
393 err = cell_management_prologue(&cell_id, &cell);
397 err = jailhouse_call_arg1(JAILHOUSE_HC_CELL_DESTROY, cell->id);
401 for_each_cpu(cpu, &cell->cpus_assigned) {
402 if (cpumask_test_cpu(cpu, &offlined_cpus)) {
403 if (cpu_up(cpu) != 0)
404 pr_err("Jailhouse: failed to bring CPU %d "
405 "back online\n", cpu);
406 cpumask_clear_cpu(cpu, &offlined_cpus);
408 cpumask_set_cpu(cpu, &root_cell->cpus_assigned);
411 jailhouse_pci_do_all_devices(cell, JAILHOUSE_PCI_TYPE_DEVICE,
412 JAILHOUSE_PCI_ACTION_RELEASE);
414 pr_info("Destroyed Jailhouse cell \"%s\"\n",
415 kobject_name(&cell->kobj));
417 jailhouse_cell_delete(cell);
420 mutex_unlock(&jailhouse_lock);