1 /*P:500 Just as userspace programs request kernel operations through a system
2 * call, the Guest requests Host operations through a "hypercall". You might
3 * notice this nomenclature doesn't really follow any logic, but the name has
4 * been around for long enough that we're stuck with it. As you'd expect, this
5 * code is basically a one big switch statement. :*/
7 /* Copyright (C) 2006 Rusty Russell IBM Corporation
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/uaccess.h>
24 #include <linux/syscalls.h>
27 #include <asm/pgtable.h>
28 #include <irq_vectors.h>
31 static void do_hcall(struct lguest *lg, struct lguest_regs *regs)
34 case LHCALL_FLUSH_ASYNC:
36 case LHCALL_LGUEST_INIT:
37 kill_guest(lg, "already have lguest_data");
41 lgread(lg, msg, regs->edx, sizeof(msg));
42 msg[sizeof(msg)-1] = '\0';
43 kill_guest(lg, "CRASH: %s", msg);
46 case LHCALL_FLUSH_TLB:
48 guest_pagetable_clear_all(lg);
50 guest_pagetable_flush_user(lg);
52 case LHCALL_GET_WALLCLOCK: {
54 ktime_get_real_ts(&ts);
55 regs->eax = ts.tv_sec;
59 regs->eax = bind_dma(lg, regs->edx, regs->ebx,
60 regs->ecx >> 8, regs->ecx & 0xFF);
63 send_dma(lg, regs->edx, regs->ebx);
66 load_guest_gdt(lg, regs->edx, regs->ebx);
68 case LHCALL_LOAD_IDT_ENTRY:
69 load_guest_idt_entry(lg, regs->edx, regs->ebx, regs->ecx);
71 case LHCALL_NEW_PGTABLE:
72 guest_new_pagetable(lg, regs->edx);
74 case LHCALL_SET_STACK:
75 guest_set_stack(lg, regs->edx, regs->ebx, regs->ecx);
78 guest_set_pte(lg, regs->edx, regs->ebx, mkgpte(regs->ecx));
81 guest_set_pmd(lg, regs->edx, regs->ebx);
84 guest_load_tls(lg, regs->edx);
86 case LHCALL_SET_CLOCKEVENT:
87 guest_set_clockevent(lg, regs->edx);
96 kill_guest(lg, "Bad hypercall %li\n", regs->eax);
100 /* We always do queued calls before actual hypercall. */
101 static void do_async_hcalls(struct lguest *lg)
104 u8 st[LHCALL_RING_SIZE];
106 if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st)))
109 for (i = 0; i < ARRAY_SIZE(st); i++) {
110 struct lguest_regs regs;
111 unsigned int n = lg->next_hcall;
116 if (++lg->next_hcall == LHCALL_RING_SIZE)
119 if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax)
120 || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx)
121 || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx)
122 || get_user(regs.ebx, &lg->lguest_data->hcalls[n].ebx)) {
123 kill_guest(lg, "Fetching async hypercalls");
128 if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) {
129 kill_guest(lg, "Writing result for async hypercall");
133 if (lg->dma_is_pending)
138 static void initialize(struct lguest *lg)
142 if (lg->regs->eax != LHCALL_LGUEST_INIT) {
143 kill_guest(lg, "hypercall %li before LGUEST_INIT",
148 /* We only tell the guest to use the TSC if it's reliable. */
149 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable())
154 lg->lguest_data = (struct lguest_data __user *)lg->regs->edx;
155 /* We check here so we can simply copy_to_user/from_user */
156 if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) {
157 kill_guest(lg, "bad guest page %p", lg->lguest_data);
160 if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start)
161 || get_user(lg->noirq_end, &lg->lguest_data->noirq_end)
162 /* We reserve the top pgd entry. */
163 || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem)
164 || put_user(tsc_speed, &lg->lguest_data->tsc_khz)
165 || put_user(lg->guestid, &lg->lguest_data->guestid))
166 kill_guest(lg, "bad guest page %p", lg->lguest_data);
168 /* This is the one case where the above accesses might have
169 * been the first write to a Guest page. This may have caused
170 * a copy-on-write fault, but the Guest might be referring to
171 * the old (read-only) page. */
172 guest_pagetable_clear_all(lg);
175 /* Even if we go out to userspace and come back, we don't want to do
176 * the hypercall again. */
177 static void clear_hcall(struct lguest *lg)
179 lg->regs->trapnum = 255;
182 void do_hypercalls(struct lguest *lg)
184 if (unlikely(!lg->lguest_data)) {
185 if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) {
193 if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) {
194 do_hcall(lg, lg->regs);