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-config.h>
17 #define SHUTDOWN_NONE 0
18 #define SHUTDOWN_STARTED 1
21 * @defgroup Control Control Subsystem
23 * The control subsystem provides services for managing cells and the
24 * hypervisor during runtime. It implements the hypercall interface and
25 * performs the required access control and parameter validation for it.
30 extern struct jailhouse_system *system_config;
32 unsigned int next_cpu(unsigned int cpu, struct cpu_set *cpu_set,
36 * Loop-generating macro for iterating over all CPUs in a set.
37 * @param cpu Iteration variable holding the current CPU ID
39 * @param set CPU set to iterate over (struct cpu_set).
41 * @see for_each_cpu_except
43 #define for_each_cpu(cpu, set) \
45 (cpu) = next_cpu((cpu), (set), -1), \
46 (cpu) <= (set)->max_cpu_id; \
50 * Loop-generating macro for iterating over all CPUs in a set, except the
52 * @param cpu Iteration variable holding the current CPU ID
54 * @param set CPU set to iterate over (struct cpu_set).
55 * @param exception CPU to skip if it is part of the set.
59 #define for_each_cpu_except(cpu, set, exception) \
61 (cpu) = next_cpu((cpu), (set), (exception)), \
62 (cpu) <= (set)->max_cpu_id; \
66 * Loop-generating macro for iterating over all registered cells.
67 * @param cell Iteration variable holding the reference to the current
68 * cell (struct cell *).
70 * @see for_each_non_root_cell
72 #define for_each_cell(cell) \
73 for ((cell) = &root_cell; (cell); (cell) = (cell)->next)
76 * Loop-generating macro for iterating over all registered cells, expect the
78 * @param cell Iteration variable holding the reference to the current
79 * cell (struct cell *).
83 #define for_each_non_root_cell(cell) \
84 for ((cell) = root_cell.next; (cell); (cell) = (cell)->next)
87 * Check if the CPU is assigned to the specified cell.
88 * @param cell Cell the CPU may belong to.
89 * @param cpu_id ID of the CPU.
91 * @return True if the CPU is assigned to the cell.
93 static inline bool cell_owns_cpu(struct cell *cell, unsigned int cpu_id)
95 return (cpu_id <= cell->cpu_set->max_cpu_id &&
96 test_bit(cpu_id, cell->cpu_set->bitmap));
99 bool cpu_id_valid(unsigned long cpu_id);
101 int check_mem_regions(const struct jailhouse_cell_desc *config);
102 int cell_init(struct cell *cell);
104 void config_commit(struct cell *cell_added_removed);
106 long hypercall(unsigned long code, unsigned long arg1, unsigned long arg2);
108 void __attribute__((noreturn)) panic_stop(void);
109 void panic_park(void);
112 * Suspend a remote CPU.
113 * @param cpu_id ID of the target CPU.
115 * Suspension means that the target CPU is no longer executing cell code or
116 * arbitrary hypervisor code. It may actively wait in the hypervisor context,
117 * so the suspension time should be kept short.
119 * The function waits for the target CPU to enter suspended state.
121 * This service can be used to synchronize with other CPUs before performing
124 * @note This function must not be invoked for the caller's CPU.
126 * @see arch_resume_cpu
127 * @see arch_reset_cpu
130 void arch_suspend_cpu(unsigned int cpu_id);
133 * Resume a suspended remote CPU.
134 * @param cpu_id ID of the target CPU.
136 * @note This function must not be invoked for the caller's CPU.
138 * @see arch_suspend_cpu
140 void arch_resume_cpu(unsigned int cpu_id);
143 * Reset a suspended remote CPU and resumes its execution.
144 * @param cpu_id ID of the target CPU.
146 * Sets the target CPU into the architecture-specific reset set and resumes its
149 * @note This function must not be invoked for the caller's CPU or if the
150 * target CPU is not in suspend state.
152 * @see arch_suspend_cpu
154 void arch_reset_cpu(unsigned int cpu_id);
157 * Park a suspended remote CPU.
158 * @param cpu_id ID of the target CPU.
160 * Parking means that the target CPU does not execute cell code but can handle
161 * asynchronous events again. Parking is not implemented as busy-waiting and
162 * may set the CPU into an appropriate power-saving mode. The CPU can therefore
163 * be left in this state for an undefined time.
165 * Parking may destroy the cell-visible CPU state and cannot be used to resume
166 * cell execution in the previous state without additional measures.
168 * @note This function must not be invoked for the caller's CPU or if the
169 * target CPU is not in suspend state.
171 * @see arch_suspend_cpu
173 void arch_park_cpu(unsigned int cpu_id);
176 * Releases hypervisor control over the target CPU.
177 * @param cpu_id ID of the target CPU.
179 * @note This function must not be invoked for the caller's CPU.
181 * @note The target CPU need not be suspended before calling the function.
183 * @note The caller has to ensure that the target CPU has enough time to reach
184 * the shutdown position before destroying the code path it has to take to get
185 * there. This can be ensured by bringing the CPU online again under Linux
186 * before cleaning up the hypervisor.
188 void arch_shutdown_cpu(unsigned int cpu_id);
191 * Performs the architecture-specific steps for mapping a memory region into a
192 * cell's address space.
193 * @param cell Cell for which the mapping shall be done.
194 * @param mem Memory region to map.
196 * @return 0 on success, negative error code otherwise.
198 * @see arch_unmap_memory_region
200 int arch_map_memory_region(struct cell *cell,
201 const struct jailhouse_memory *mem);
204 * Performs the architecture-specific steps for unmapping a memory region from
205 * a cell's address space.
206 * @param cell Cell for which the unmapping shall be done.
207 * @param mem Memory region to unmap.
209 * @return 0 on success, negative error code otherwise.
211 * @see arch_map_memory_region
213 int arch_unmap_memory_region(struct cell *cell,
214 const struct jailhouse_memory *mem);
217 * Performs the architecture-specific steps for invalidating memory caches
218 * after memory regions have been unmapped from a cell.
219 * This function should be called after memory got unmapped or memory access
220 * got restricted, and the cell should keep running.
221 * @param cell Cell for which the caches should get flushed
223 * @see per_cpu::flush_vcpu_caches
225 void arch_flush_cell_vcpu_caches(struct cell *cell);
228 * Performs the architecture-specific steps for creating a new cell.
229 * @param cell Data structure of the new cell.
231 * @return 0 on success, negative error code otherwise.
233 * @see arch_cell_destroy
235 int arch_cell_create(struct cell *cell);
238 * Performs the architecture-specific steps for destroying a cell.
239 * @param cell Cell to be destroyed.
241 * @see arch_cell_create
243 void arch_cell_destroy(struct cell *cell);
246 * Performs the architecture-specific steps for applying configuration changes.
247 * @param cell_added_removed Cell that was added or removed to/from the
251 * @see pci_config_commit
253 void arch_config_commit(struct cell *cell_added_removed);
256 * Shutdown architecture-specific subsystems while disabling the hypervisor.
258 void arch_shutdown(void);
261 * Performs the architecture-specifc steps to stop the current CPU on panic.
263 * @note This function never returns.
267 void __attribute__((noreturn)) arch_panic_stop(void);
270 * Performs the architecture-specific steps to park the current CPU on panic.
272 * @note This function only marks the CPU as parked and then returns to the
277 void arch_panic_park(void);