Path: blob/master/drivers/lguest/interrupts_and_traps.c
15109 views
/*P:8001* Interrupts (traps) are complicated enough to earn their own file.2* There are three classes of interrupts:3*4* 1) Real hardware interrupts which occur while we're running the Guest,5* 2) Interrupts for virtual devices attached to the Guest, and6* 3) Traps and faults from the Guest.7*8* Real hardware interrupts must be delivered to the Host, not the Guest.9* Virtual interrupts must be delivered to the Guest, but we make them look10* just like real hardware would deliver them. Traps from the Guest can be set11* up to go directly back into the Guest, but sometimes the Host wants to see12* them first, so we also have a way of "reflecting" them into the Guest as if13* they had been delivered to it directly.14:*/15#include <linux/uaccess.h>16#include <linux/interrupt.h>17#include <linux/module.h>18#include <linux/sched.h>19#include "lg.h"2021/* Allow Guests to use a non-128 (ie. non-Linux) syscall trap. */22static unsigned int syscall_vector = SYSCALL_VECTOR;23module_param(syscall_vector, uint, 0444);2425/* The address of the interrupt handler is split into two bits: */26static unsigned long idt_address(u32 lo, u32 hi)27{28return (lo & 0x0000FFFF) | (hi & 0xFFFF0000);29}3031/*32* The "type" of the interrupt handler is a 4 bit field: we only support a33* couple of types.34*/35static int idt_type(u32 lo, u32 hi)36{37return (hi >> 8) & 0xF;38}3940/* An IDT entry can't be used unless the "present" bit is set. */41static bool idt_present(u32 lo, u32 hi)42{43return (hi & 0x8000);44}4546/*47* We need a helper to "push" a value onto the Guest's stack, since that's a48* big part of what delivering an interrupt does.49*/50static void push_guest_stack(struct lg_cpu *cpu, unsigned long *gstack, u32 val)51{52/* Stack grows upwards: move stack then write value. */53*gstack -= 4;54lgwrite(cpu, *gstack, u32, val);55}5657/*H:21058* The set_guest_interrupt() routine actually delivers the interrupt or59* trap. The mechanics of delivering traps and interrupts to the Guest are the60* same, except some traps have an "error code" which gets pushed onto the61* stack as well: the caller tells us if this is one.62*63* "lo" and "hi" are the two parts of the Interrupt Descriptor Table for this64* interrupt or trap. It's split into two parts for traditional reasons: gcc65* on i386 used to be frightened by 64 bit numbers.66*67* We set up the stack just like the CPU does for a real interrupt, so it's68* identical for the Guest (and the standard "iret" instruction will undo69* it).70*/71static void set_guest_interrupt(struct lg_cpu *cpu, u32 lo, u32 hi,72bool has_err)73{74unsigned long gstack, origstack;75u32 eflags, ss, irq_enable;76unsigned long virtstack;7778/*79* There are two cases for interrupts: one where the Guest is already80* in the kernel, and a more complex one where the Guest is in81* userspace. We check the privilege level to find out.82*/83if ((cpu->regs->ss&0x3) != GUEST_PL) {84/*85* The Guest told us their kernel stack with the SET_STACK86* hypercall: both the virtual address and the segment.87*/88virtstack = cpu->esp1;89ss = cpu->ss1;9091origstack = gstack = guest_pa(cpu, virtstack);92/*93* We push the old stack segment and pointer onto the new94* stack: when the Guest does an "iret" back from the interrupt95* handler the CPU will notice they're dropping privilege96* levels and expect these here.97*/98push_guest_stack(cpu, &gstack, cpu->regs->ss);99push_guest_stack(cpu, &gstack, cpu->regs->esp);100} else {101/* We're staying on the same Guest (kernel) stack. */102virtstack = cpu->regs->esp;103ss = cpu->regs->ss;104105origstack = gstack = guest_pa(cpu, virtstack);106}107108/*109* Remember that we never let the Guest actually disable interrupts, so110* the "Interrupt Flag" bit is always set. We copy that bit from the111* Guest's "irq_enabled" field into the eflags word: we saw the Guest112* copy it back in "lguest_iret".113*/114eflags = cpu->regs->eflags;115if (get_user(irq_enable, &cpu->lg->lguest_data->irq_enabled) == 0116&& !(irq_enable & X86_EFLAGS_IF))117eflags &= ~X86_EFLAGS_IF;118119/*120* An interrupt is expected to push three things on the stack: the old121* "eflags" word, the old code segment, and the old instruction122* pointer.123*/124push_guest_stack(cpu, &gstack, eflags);125push_guest_stack(cpu, &gstack, cpu->regs->cs);126push_guest_stack(cpu, &gstack, cpu->regs->eip);127128/* For the six traps which supply an error code, we push that, too. */129if (has_err)130push_guest_stack(cpu, &gstack, cpu->regs->errcode);131132/*133* Now we've pushed all the old state, we change the stack, the code134* segment and the address to execute.135*/136cpu->regs->ss = ss;137cpu->regs->esp = virtstack + (gstack - origstack);138cpu->regs->cs = (__KERNEL_CS|GUEST_PL);139cpu->regs->eip = idt_address(lo, hi);140141/*142* There are two kinds of interrupt handlers: 0xE is an "interrupt143* gate" which expects interrupts to be disabled on entry.144*/145if (idt_type(lo, hi) == 0xE)146if (put_user(0, &cpu->lg->lguest_data->irq_enabled))147kill_guest(cpu, "Disabling interrupts");148}149150/*H:205151* Virtual Interrupts.152*153* interrupt_pending() returns the first pending interrupt which isn't blocked154* by the Guest. It is called before every entry to the Guest, and just before155* we go to sleep when the Guest has halted itself.156*/157unsigned int interrupt_pending(struct lg_cpu *cpu, bool *more)158{159unsigned int irq;160DECLARE_BITMAP(blk, LGUEST_IRQS);161162/* If the Guest hasn't even initialized yet, we can do nothing. */163if (!cpu->lg->lguest_data)164return LGUEST_IRQS;165166/*167* Take our "irqs_pending" array and remove any interrupts the Guest168* wants blocked: the result ends up in "blk".169*/170if (copy_from_user(&blk, cpu->lg->lguest_data->blocked_interrupts,171sizeof(blk)))172return LGUEST_IRQS;173bitmap_andnot(blk, cpu->irqs_pending, blk, LGUEST_IRQS);174175/* Find the first interrupt. */176irq = find_first_bit(blk, LGUEST_IRQS);177*more = find_next_bit(blk, LGUEST_IRQS, irq+1);178179return irq;180}181182/*183* This actually diverts the Guest to running an interrupt handler, once an184* interrupt has been identified by interrupt_pending().185*/186void try_deliver_interrupt(struct lg_cpu *cpu, unsigned int irq, bool more)187{188struct desc_struct *idt;189190BUG_ON(irq >= LGUEST_IRQS);191192/*193* They may be in the middle of an iret, where they asked us never to194* deliver interrupts.195*/196if (cpu->regs->eip >= cpu->lg->noirq_start &&197(cpu->regs->eip < cpu->lg->noirq_end))198return;199200/* If they're halted, interrupts restart them. */201if (cpu->halted) {202/* Re-enable interrupts. */203if (put_user(X86_EFLAGS_IF, &cpu->lg->lguest_data->irq_enabled))204kill_guest(cpu, "Re-enabling interrupts");205cpu->halted = 0;206} else {207/* Otherwise we check if they have interrupts disabled. */208u32 irq_enabled;209if (get_user(irq_enabled, &cpu->lg->lguest_data->irq_enabled))210irq_enabled = 0;211if (!irq_enabled) {212/* Make sure they know an IRQ is pending. */213put_user(X86_EFLAGS_IF,214&cpu->lg->lguest_data->irq_pending);215return;216}217}218219/*220* Look at the IDT entry the Guest gave us for this interrupt. The221* first 32 (FIRST_EXTERNAL_VECTOR) entries are for traps, so we skip222* over them.223*/224idt = &cpu->arch.idt[FIRST_EXTERNAL_VECTOR+irq];225/* If they don't have a handler (yet?), we just ignore it */226if (idt_present(idt->a, idt->b)) {227/* OK, mark it no longer pending and deliver it. */228clear_bit(irq, cpu->irqs_pending);229/*230* set_guest_interrupt() takes the interrupt descriptor and a231* flag to say whether this interrupt pushes an error code onto232* the stack as well: virtual interrupts never do.233*/234set_guest_interrupt(cpu, idt->a, idt->b, false);235}236237/*238* Every time we deliver an interrupt, we update the timestamp in the239* Guest's lguest_data struct. It would be better for the Guest if we240* did this more often, but it can actually be quite slow: doing it241* here is a compromise which means at least it gets updated every242* timer interrupt.243*/244write_timestamp(cpu);245246/*247* If there are no other interrupts we want to deliver, clear248* the pending flag.249*/250if (!more)251put_user(0, &cpu->lg->lguest_data->irq_pending);252}253254/* And this is the routine when we want to set an interrupt for the Guest. */255void set_interrupt(struct lg_cpu *cpu, unsigned int irq)256{257/*258* Next time the Guest runs, the core code will see if it can deliver259* this interrupt.260*/261set_bit(irq, cpu->irqs_pending);262263/*264* Make sure it sees it; it might be asleep (eg. halted), or running265* the Guest right now, in which case kick_process() will knock it out.266*/267if (!wake_up_process(cpu->tsk))268kick_process(cpu->tsk);269}270/*:*/271272/*273* Linux uses trap 128 for system calls. Plan9 uses 64, and Ron Minnich sent274* me a patch, so we support that too. It'd be a big step for lguest if half275* the Plan 9 user base were to start using it.276*277* Actually now I think of it, it's possible that Ron *is* half the Plan 9278* userbase. Oh well.279*/280static bool could_be_syscall(unsigned int num)281{282/* Normal Linux SYSCALL_VECTOR or reserved vector? */283return num == SYSCALL_VECTOR || num == syscall_vector;284}285286/* The syscall vector it wants must be unused by Host. */287bool check_syscall_vector(struct lguest *lg)288{289u32 vector;290291if (get_user(vector, &lg->lguest_data->syscall_vec))292return false;293294return could_be_syscall(vector);295}296297int init_interrupts(void)298{299/* If they want some strange system call vector, reserve it now */300if (syscall_vector != SYSCALL_VECTOR) {301if (test_bit(syscall_vector, used_vectors) ||302vector_used_by_percpu_irq(syscall_vector)) {303printk(KERN_ERR "lg: couldn't reserve syscall %u\n",304syscall_vector);305return -EBUSY;306}307set_bit(syscall_vector, used_vectors);308}309310return 0;311}312313void free_interrupts(void)314{315if (syscall_vector != SYSCALL_VECTOR)316clear_bit(syscall_vector, used_vectors);317}318319/*H:220320* Now we've got the routines to deliver interrupts, delivering traps like321* page fault is easy. The only trick is that Intel decided that some traps322* should have error codes:323*/324static bool has_err(unsigned int trap)325{326return (trap == 8 || (trap >= 10 && trap <= 14) || trap == 17);327}328329/* deliver_trap() returns true if it could deliver the trap. */330bool deliver_trap(struct lg_cpu *cpu, unsigned int num)331{332/*333* Trap numbers are always 8 bit, but we set an impossible trap number334* for traps inside the Switcher, so check that here.335*/336if (num >= ARRAY_SIZE(cpu->arch.idt))337return false;338339/*340* Early on the Guest hasn't set the IDT entries (or maybe it put a341* bogus one in): if we fail here, the Guest will be killed.342*/343if (!idt_present(cpu->arch.idt[num].a, cpu->arch.idt[num].b))344return false;345set_guest_interrupt(cpu, cpu->arch.idt[num].a,346cpu->arch.idt[num].b, has_err(num));347return true;348}349350/*H:250351* Here's the hard part: returning to the Host every time a trap happens352* and then calling deliver_trap() and re-entering the Guest is slow.353* Particularly because Guest userspace system calls are traps (usually trap354* 128).355*356* So we'd like to set up the IDT to tell the CPU to deliver traps directly357* into the Guest. This is possible, but the complexities cause the size of358* this file to double! However, 150 lines of code is worth writing for taking359* system calls down from 1750ns to 270ns. Plus, if lguest didn't do it, all360* the other hypervisors would beat it up at lunchtime.361*362* This routine indicates if a particular trap number could be delivered363* directly.364*/365static bool direct_trap(unsigned int num)366{367/*368* Hardware interrupts don't go to the Guest at all (except system369* call).370*/371if (num >= FIRST_EXTERNAL_VECTOR && !could_be_syscall(num))372return false;373374/*375* The Host needs to see page faults (for shadow paging and to save the376* fault address), general protection faults (in/out emulation) and377* device not available (TS handling), invalid opcode fault (kvm hcall),378* and of course, the hypercall trap.379*/380return num != 14 && num != 13 && num != 7 &&381num != 6 && num != LGUEST_TRAP_ENTRY;382}383/*:*/384385/*M:005386* The Guest has the ability to turn its interrupt gates into trap gates,387* if it is careful. The Host will let trap gates can go directly to the388* Guest, but the Guest needs the interrupts atomically disabled for an389* interrupt gate. It can do this by pointing the trap gate at instructions390* within noirq_start and noirq_end, where it can safely disable interrupts.391*/392393/*M:006394* The Guests do not use the sysenter (fast system call) instruction,395* because it's hardcoded to enter privilege level 0 and so can't go direct.396* It's about twice as fast as the older "int 0x80" system call, so it might397* still be worthwhile to handle it in the Switcher and lcall down to the398* Guest. The sysenter semantics are hairy tho: search for that keyword in399* entry.S400:*/401402/*H:260403* When we make traps go directly into the Guest, we need to make sure404* the kernel stack is valid (ie. mapped in the page tables). Otherwise, the405* CPU trying to deliver the trap will fault while trying to push the interrupt406* words on the stack: this is called a double fault, and it forces us to kill407* the Guest.408*409* Which is deeply unfair, because (literally!) it wasn't the Guests' fault.410*/411void pin_stack_pages(struct lg_cpu *cpu)412{413unsigned int i;414415/*416* Depending on the CONFIG_4KSTACKS option, the Guest can have one or417* two pages of stack space.418*/419for (i = 0; i < cpu->lg->stack_pages; i++)420/*421* The stack grows *upwards*, so the address we're given is the422* start of the page after the kernel stack. Subtract one to423* get back onto the first stack page, and keep subtracting to424* get to the rest of the stack pages.425*/426pin_page(cpu, cpu->esp1 - 1 - i * PAGE_SIZE);427}428429/*430* Direct traps also mean that we need to know whenever the Guest wants to use431* a different kernel stack, so we can change the IDT entries to use that432* stack. The IDT entries expect a virtual address, so unlike most addresses433* the Guest gives us, the "esp" (stack pointer) value here is virtual, not434* physical.435*436* In Linux each process has its own kernel stack, so this happens a lot: we437* change stacks on each context switch.438*/439void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages)440{441/*442* You're not allowed a stack segment with privilege level 0: bad Guest!443*/444if ((seg & 0x3) != GUEST_PL)445kill_guest(cpu, "bad stack segment %i", seg);446/* We only expect one or two stack pages. */447if (pages > 2)448kill_guest(cpu, "bad stack pages %u", pages);449/* Save where the stack is, and how many pages */450cpu->ss1 = seg;451cpu->esp1 = esp;452cpu->lg->stack_pages = pages;453/* Make sure the new stack pages are mapped */454pin_stack_pages(cpu);455}456457/*458* All this reference to mapping stacks leads us neatly into the other complex459* part of the Host: page table handling.460*/461462/*H:235463* This is the routine which actually checks the Guest's IDT entry and464* transfers it into the entry in "struct lguest":465*/466static void set_trap(struct lg_cpu *cpu, struct desc_struct *trap,467unsigned int num, u32 lo, u32 hi)468{469u8 type = idt_type(lo, hi);470471/* We zero-out a not-present entry */472if (!idt_present(lo, hi)) {473trap->a = trap->b = 0;474return;475}476477/* We only support interrupt and trap gates. */478if (type != 0xE && type != 0xF)479kill_guest(cpu, "bad IDT type %i", type);480481/*482* We only copy the handler address, present bit, privilege level and483* type. The privilege level controls where the trap can be triggered484* manually with an "int" instruction. This is usually GUEST_PL,485* except for system calls which userspace can use.486*/487trap->a = ((__KERNEL_CS|GUEST_PL)<<16) | (lo&0x0000FFFF);488trap->b = (hi&0xFFFFEF00);489}490491/*H:230492* While we're here, dealing with delivering traps and interrupts to the493* Guest, we might as well complete the picture: how the Guest tells us where494* it wants them to go. This would be simple, except making traps fast495* requires some tricks.496*497* We saw the Guest setting Interrupt Descriptor Table (IDT) entries with the498* LHCALL_LOAD_IDT_ENTRY hypercall before: that comes here.499*/500void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int num, u32 lo, u32 hi)501{502/*503* Guest never handles: NMI, doublefault, spurious interrupt or504* hypercall. We ignore when it tries to set them.505*/506if (num == 2 || num == 8 || num == 15 || num == LGUEST_TRAP_ENTRY)507return;508509/*510* Mark the IDT as changed: next time the Guest runs we'll know we have511* to copy this again.512*/513cpu->changed |= CHANGED_IDT;514515/* Check that the Guest doesn't try to step outside the bounds. */516if (num >= ARRAY_SIZE(cpu->arch.idt))517kill_guest(cpu, "Setting idt entry %u", num);518else519set_trap(cpu, &cpu->arch.idt[num], num, lo, hi);520}521522/*523* The default entry for each interrupt points into the Switcher routines which524* simply return to the Host. The run_guest() loop will then call525* deliver_trap() to bounce it back into the Guest.526*/527static void default_idt_entry(struct desc_struct *idt,528int trap,529const unsigned long handler,530const struct desc_struct *base)531{532/* A present interrupt gate. */533u32 flags = 0x8e00;534535/*536* Set the privilege level on the entry for the hypercall: this allows537* the Guest to use the "int" instruction to trigger it.538*/539if (trap == LGUEST_TRAP_ENTRY)540flags |= (GUEST_PL << 13);541else if (base)542/*543* Copy privilege level from what Guest asked for. This allows544* debug (int 3) traps from Guest userspace, for example.545*/546flags |= (base->b & 0x6000);547548/* Now pack it into the IDT entry in its weird format. */549idt->a = (LGUEST_CS<<16) | (handler&0x0000FFFF);550idt->b = (handler&0xFFFF0000) | flags;551}552553/* When the Guest first starts, we put default entries into the IDT. */554void setup_default_idt_entries(struct lguest_ro_state *state,555const unsigned long *def)556{557unsigned int i;558559for (i = 0; i < ARRAY_SIZE(state->guest_idt); i++)560default_idt_entry(&state->guest_idt[i], i, def[i], NULL);561}562563/*H:240564* We don't use the IDT entries in the "struct lguest" directly, instead565* we copy them into the IDT which we've set up for Guests on this CPU, just566* before we run the Guest. This routine does that copy.567*/568void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt,569const unsigned long *def)570{571unsigned int i;572573/*574* We can simply copy the direct traps, otherwise we use the default575* ones in the Switcher: they will return to the Host.576*/577for (i = 0; i < ARRAY_SIZE(cpu->arch.idt); i++) {578const struct desc_struct *gidt = &cpu->arch.idt[i];579580/* If no Guest can ever override this trap, leave it alone. */581if (!direct_trap(i))582continue;583584/*585* Only trap gates (type 15) can go direct to the Guest.586* Interrupt gates (type 14) disable interrupts as they are587* entered, which we never let the Guest do. Not present588* entries (type 0x0) also can't go direct, of course.589*590* If it can't go direct, we still need to copy the priv. level:591* they might want to give userspace access to a software592* interrupt.593*/594if (idt_type(gidt->a, gidt->b) == 0xF)595idt[i] = *gidt;596else597default_idt_entry(&idt[i], i, def[i], gidt);598}599}600601/*H:200602* The Guest Clock.603*604* There are two sources of virtual interrupts. We saw one in lguest_user.c:605* the Launcher sending interrupts for virtual devices. The other is the Guest606* timer interrupt.607*608* The Guest uses the LHCALL_SET_CLOCKEVENT hypercall to tell us how long to609* the next timer interrupt (in nanoseconds). We use the high-resolution timer610* infrastructure to set a callback at that time.611*612* 0 means "turn off the clock".613*/614void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta)615{616ktime_t expires;617618if (unlikely(delta == 0)) {619/* Clock event device is shutting down. */620hrtimer_cancel(&cpu->hrt);621return;622}623624/*625* We use wallclock time here, so the Guest might not be running for626* all the time between now and the timer interrupt it asked for. This627* is almost always the right thing to do.628*/629expires = ktime_add_ns(ktime_get_real(), delta);630hrtimer_start(&cpu->hrt, expires, HRTIMER_MODE_ABS);631}632633/* This is the function called when the Guest's timer expires. */634static enum hrtimer_restart clockdev_fn(struct hrtimer *timer)635{636struct lg_cpu *cpu = container_of(timer, struct lg_cpu, hrt);637638/* Remember the first interrupt is the timer interrupt. */639set_interrupt(cpu, 0);640return HRTIMER_NORESTART;641}642643/* This sets up the timer for this Guest. */644void init_clockdev(struct lg_cpu *cpu)645{646hrtimer_init(&cpu->hrt, CLOCK_REALTIME, HRTIMER_MODE_ABS);647cpu->hrt.function = clockdev_fn;648}649650651