2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013
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 <asm/bitops.h>
14 #include <asm/percpu.h>
15 #include <jailhouse/cell.h>
16 #include <jailhouse/cell-config.h>
18 #define SHUTDOWN_NONE 0
19 #define SHUTDOWN_STARTED 1
22 * @defgroup Control Control Subsystem
24 * The control subsystem provides services for managing cells and the
25 * hypervisor during runtime. It implements the hypercall interface and
26 * performs the required access control and parameter validation for it.
31 extern struct jailhouse_system *system_config;
33 unsigned int next_cpu(unsigned int cpu, struct cpu_set *cpu_set,
37 * Loop-generating macro for iterating over all CPUs in a set.
38 * @param cpu Iteration variable holding the current CPU ID
40 * @param set CPU set to iterate over (struct cpu_set).
42 * @see for_each_cpu_except
44 #define for_each_cpu(cpu, set) \
46 (cpu) = next_cpu((cpu), (set), -1), \
47 (cpu) <= (set)->max_cpu_id; \
51 * Loop-generating macro for iterating over all CPUs in a set, except the
53 * @param cpu Iteration variable holding the current CPU ID
55 * @param set CPU set to iterate over (struct cpu_set).
56 * @param exception CPU to skip if it is part of the set.
60 #define for_each_cpu_except(cpu, set, exception) \
62 (cpu) = next_cpu((cpu), (set), (exception)), \
63 (cpu) <= (set)->max_cpu_id; \
67 * Loop-generating macro for iterating over all registered cells.
68 * @param cell Iteration variable holding the reference to the current
69 * cell (struct cell *).
71 * @see for_each_non_root_cell
73 #define for_each_cell(cell) \
74 for ((cell) = &root_cell; (cell); (cell) = (cell)->next)
77 * Loop-generating macro for iterating over all registered cells, expect the
79 * @param cell Iteration variable holding the reference to the current
80 * cell (struct cell *).
84 #define for_each_non_root_cell(cell) \
85 for ((cell) = root_cell.next; (cell); (cell) = (cell)->next)
88 * Loop-generating macro for iterating over all memory regions of a
90 * @param mem Iteration variable holding the reference to the current
91 * memory region (const struct jailhouse_memory *).
92 * @param config Cell or system configuration containing the regions.
93 * @param counter Helper variable (unsigned int).
95 #define for_each_mem_region(mem, config, counter) \
96 for ((mem) = jailhouse_cell_mem_regions(config), (counter) = 0; \
97 (counter) < (config)->num_memory_regions; \
101 * Check if the CPU is assigned to the specified cell.
102 * @param cell Cell the CPU may belong to.
103 * @param cpu_id ID of the CPU.
105 * @return True if the CPU is assigned to the cell.
107 static inline bool cell_owns_cpu(struct cell *cell, unsigned int cpu_id)
109 return (cpu_id <= cell->cpu_set->max_cpu_id &&
110 test_bit(cpu_id, cell->cpu_set->bitmap));
113 bool cpu_id_valid(unsigned long cpu_id);
115 int check_mem_regions(const struct jailhouse_cell_desc *config);
116 int cell_init(struct cell *cell);
118 void config_commit(struct cell *cell_added_removed);
120 long hypercall(unsigned long code, unsigned long arg1, unsigned long arg2);
122 void __attribute__((noreturn)) panic_stop(void);
123 void panic_park(void);
126 * Suspend a remote CPU.
127 * @param cpu_id ID of the target CPU.
129 * Suspension means that the target CPU is no longer executing cell code or
130 * arbitrary hypervisor code. It may actively wait in the hypervisor context,
131 * so the suspension time should be kept short.
133 * The function waits for the target CPU to enter suspended state.
135 * This service can be used to synchronize with other CPUs before performing
138 * @note This function must not be invoked for the caller's CPU.
140 * @see arch_resume_cpu
141 * @see arch_reset_cpu
144 void arch_suspend_cpu(unsigned int cpu_id);
147 * Resume a suspended remote CPU.
148 * @param cpu_id ID of the target CPU.
150 * @note This function must not be invoked for the caller's CPU.
152 * @see arch_suspend_cpu
154 void arch_resume_cpu(unsigned int cpu_id);
157 * Reset a suspended remote CPU and resumes its execution.
158 * @param cpu_id ID of the target CPU.
160 * Sets the target CPU into the architecture-specific reset set and resumes its
163 * @note This function must not be invoked for the caller's CPU or if the
164 * target CPU is not in suspend state.
166 * @see arch_suspend_cpu
168 void arch_reset_cpu(unsigned int cpu_id);
171 * Park a suspended remote CPU.
172 * @param cpu_id ID of the target CPU.
174 * Parking means that the target CPU does not execute cell code but can handle
175 * asynchronous events again. Parking is not implemented as busy-waiting and
176 * may set the CPU into an appropriate power-saving mode. The CPU can therefore
177 * be left in this state for an undefined time.
179 * Parking may destroy the cell-visible CPU state and cannot be used to resume
180 * cell execution in the previous state without additional measures.
182 * @note This function must not be invoked for the caller's CPU or if the
183 * target CPU is not in suspend state.
185 * @see arch_suspend_cpu
187 void arch_park_cpu(unsigned int cpu_id);
190 * Releases hypervisor control over the target CPU.
191 * @param cpu_id ID of the target CPU.
193 * @note This function must not be invoked for the caller's CPU.
195 * @note The target CPU need not be suspended before calling the function.
197 * @note The caller has to ensure that the target CPU has enough time to reach
198 * the shutdown position before destroying the code path it has to take to get
199 * there. This can be ensured by bringing the CPU online again under Linux
200 * before cleaning up the hypervisor.
202 void arch_shutdown_cpu(unsigned int cpu_id);
205 * Performs the architecture-specific steps for mapping a memory region into a
206 * cell's address space.
207 * @param cell Cell for which the mapping shall be done.
208 * @param mem Memory region to map.
210 * @return 0 on success, negative error code otherwise.
212 * @see arch_unmap_memory_region
214 int arch_map_memory_region(struct cell *cell,
215 const struct jailhouse_memory *mem);
218 * Performs the architecture-specific steps for unmapping a memory region from
219 * a cell's address space.
220 * @param cell Cell for which the unmapping shall be done.
221 * @param mem Memory region to unmap.
223 * @return 0 on success, negative error code otherwise.
225 * @see arch_map_memory_region
227 int arch_unmap_memory_region(struct cell *cell,
228 const struct jailhouse_memory *mem);
231 * Performs the architecture-specific steps for invalidating memory caches
232 * after memory regions have been unmapped from a cell.
233 * This function should be called after memory got unmapped or memory access
234 * got restricted, and the cell should keep running.
235 * @param cell Cell for which the caches should get flushed
237 * @see per_cpu::flush_vcpu_caches
239 void arch_flush_cell_vcpu_caches(struct cell *cell);
242 * Performs the architecture-specific steps for creating a new cell.
243 * @param cell Data structure of the new cell.
245 * @return 0 on success, negative error code otherwise.
247 * @see arch_cell_destroy
249 int arch_cell_create(struct cell *cell);
252 * Performs the architecture-specific steps for destroying a cell.
253 * @param cell Cell to be destroyed.
255 * @see arch_cell_create
257 void arch_cell_destroy(struct cell *cell);
260 * Performs the architecture-specific steps for applying configuration changes.
261 * @param cell_added_removed Cell that was added or removed to/from the
265 * @see pci_config_commit
267 void arch_config_commit(struct cell *cell_added_removed);
270 * Shutdown architecture-specific subsystems while disabling the hypervisor.
272 void arch_shutdown(void);
275 * Performs the architecture-specifc steps to stop the current CPU on panic.
277 * @note This function never returns.
281 void __attribute__((noreturn)) arch_panic_stop(void);
284 * Performs the architecture-specific steps to park the current CPU on panic.
286 * @note This function only marks the CPU as parked and then returns to the
291 void arch_panic_park(void);