1 /* Actual hypercalls, which allow guests to actually do something.
2 Copyright (C) 2006 Rusty Russell IBM Corporation
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
22 #include <asm/pgtable.h>
23 #include <irq_vectors.h>
26 static void do_hcall(struct lguest *lg, struct lguest_regs *regs)
29 case LHCALL_FLUSH_ASYNC:
31 case LHCALL_LGUEST_INIT:
32 kill_guest(lg, "already have lguest_data");
36 lgread(lg, msg, regs->edx, sizeof(msg));
37 msg[sizeof(msg)-1] = '\0';
38 kill_guest(lg, "CRASH: %s", msg);
41 case LHCALL_FLUSH_TLB:
43 guest_pagetable_clear_all(lg);
45 guest_pagetable_flush_user(lg);
47 case LHCALL_GET_WALLCLOCK: {
49 ktime_get_real_ts(&ts);
50 regs->eax = ts.tv_sec;
54 regs->eax = bind_dma(lg, regs->edx, regs->ebx,
55 regs->ecx >> 8, regs->ecx & 0xFF);
58 send_dma(lg, regs->edx, regs->ebx);
61 load_guest_gdt(lg, regs->edx, regs->ebx);
63 case LHCALL_LOAD_IDT_ENTRY:
64 load_guest_idt_entry(lg, regs->edx, regs->ebx, regs->ecx);
66 case LHCALL_NEW_PGTABLE:
67 guest_new_pagetable(lg, regs->edx);
69 case LHCALL_SET_STACK:
70 guest_set_stack(lg, regs->edx, regs->ebx, regs->ecx);
73 guest_set_pte(lg, regs->edx, regs->ebx, mkgpte(regs->ecx));
76 guest_set_pmd(lg, regs->edx, regs->ebx);
79 guest_load_tls(lg, regs->edx);
81 case LHCALL_SET_CLOCKEVENT:
82 guest_set_clockevent(lg, regs->edx);
91 kill_guest(lg, "Bad hypercall %li\n", regs->eax);
95 /* We always do queued calls before actual hypercall. */
96 static void do_async_hcalls(struct lguest *lg)
99 u8 st[LHCALL_RING_SIZE];
101 if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
104 for (i = 0; i < ARRAY_SIZE(st); i++) {
105 struct lguest_regs regs;
106 unsigned int n = lg->next_hcall;
111 if (++lg->next_hcall == LHCALL_RING_SIZE)
114 if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax)
115 || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx)
116 || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx)
117 || get_user(regs.ebx, &lg->lguest_data->hcalls[n].ebx)) {
118 kill_guest(lg, "Fetching async hypercalls");
123 if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
124 kill_guest(lg, "Writing result for async hypercall");
128 if (lg->dma_is_pending)
133 static void initialize(struct lguest *lg)
137 if (lg->regs->eax != LHCALL_LGUEST_INIT) {
138 kill_guest(lg, "hypercall %li before LGUEST_INIT",
143 /* We only tell the guest to use the TSC if it's reliable. */
144 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable())
149 lg->lguest_data = (struct lguest_data __user *)lg->regs->edx;
150 /* We check here so we can simply copy_to_user/from_user */
151 if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) {
152 kill_guest(lg, "bad guest page %p", lg->lguest_data);
155 if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
156 || get_user(lg->noirq_end, &lg->lguest_data->noirq_end)
157 /* We reserve the top pgd entry. */
158 || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem)
159 || put_user(tsc_speed, &lg->lguest_data->tsc_khz)
160 || put_user(lg->guestid, &lg->lguest_data->guestid))
161 kill_guest(lg, "bad guest page %p", lg->lguest_data);
163 /* This is the one case where the above accesses might have
164 * been the first write to a Guest page. This may have caused
165 * a copy-on-write fault, but the Guest might be referring to
166 * the old (read-only) page. */
167 guest_pagetable_clear_all(lg);
170 /* Even if we go out to userspace and come back, we don't want to do
171 * the hypercall again. */
172 static void clear_hcall(struct lguest *lg)
174 lg->regs->trapnum = 255;
177 void do_hypercalls(struct lguest *lg)
179 if (unlikely(!lg->lguest_data)) {
180 if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) {
188 if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) {
189 do_hcall(lg, lg->regs);