ia64/xen-unstable

view extras/mini-os/events.c @ 12244:665a28dffb7d

[XEND] Don't hide the 'type' SXP config item from HVM guests

Should allow network devices to properly initialised in HVM guests.

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Nov 03 11:42:57 2006 +0000 (2006-11-03)
parents fc8ae086f706
children 97c552510470
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 ****************************************************************************
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
5 ****************************************************************************
6 *
7 * File: events.c
8 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
9 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
10 *
11 * Date: Jul 2003, changes Jun 2005
12 *
13 * Environment: Xen Minimal OS
14 * Description: Deals with events recieved on event channels
15 *
16 ****************************************************************************
17 */
19 #include <os.h>
20 #include <mm.h>
21 #include <hypervisor.h>
22 #include <events.h>
23 #include <lib.h>
25 #define NR_EVS 1024
27 /* this represents a event handler. Chaining or sharing is not allowed */
28 typedef struct _ev_action_t {
29 evtchn_handler_t handler;
30 void *data;
31 u32 count;
32 } ev_action_t;
35 static ev_action_t ev_actions[NR_EVS];
36 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);
39 /*
40 * Demux events to different handlers.
41 */
42 int do_event(evtchn_port_t port, struct pt_regs *regs)
43 {
44 ev_action_t *action;
45 if (port >= NR_EVS) {
46 printk("Port number too large: %d\n", port);
47 goto out;
48 }
50 action = &ev_actions[port];
51 action->count++;
53 /* call the handler */
54 action->handler(port, regs, action->data);
56 out:
57 clear_evtchn(port);
59 return 1;
61 }
63 evtchn_port_t bind_evtchn(evtchn_port_t port, evtchn_handler_t handler,
64 void *data)
65 {
66 if(ev_actions[port].handler != default_handler)
67 printk("WARN: Handler for port %d already registered, replacing\n",
68 port);
70 ev_actions[port].data = data;
71 wmb();
72 ev_actions[port].handler = handler;
74 /* Finally unmask the port */
75 unmask_evtchn(port);
77 return port;
78 }
80 void unbind_evtchn(evtchn_port_t port )
81 {
82 if (ev_actions[port].handler == default_handler)
83 printk("WARN: No handler for port %d when unbinding\n", port);
84 ev_actions[port].handler = default_handler;
85 wmb();
86 ev_actions[port].data = NULL;
87 }
89 int bind_virq(uint32_t virq, evtchn_handler_t handler, void *data)
90 {
91 evtchn_bind_virq_t op;
93 /* Try to bind the virq to a port */
94 op.virq = virq;
95 op.vcpu = smp_processor_id();
97 if ( HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, &op) != 0 )
98 {
99 printk("Failed to bind virtual IRQ %d\n", virq);
100 return 1;
101 }
102 bind_evtchn(op.port, handler, data);
103 return 0;
104 }
106 #if defined(__x86_64__)
107 /* Allocate 4 pages for the irqstack */
108 #define STACK_PAGES 4
109 char irqstack[1024 * 4 * STACK_PAGES];
111 static struct pda
112 {
113 int irqcount; /* offset 0 (used in x86_64.S) */
114 char *irqstackptr; /* 8 */
115 } cpu0_pda;
116 #endif
118 /*
119 * Initially all events are without a handler and disabled
120 */
121 void init_events(void)
122 {
123 int i;
124 #if defined(__x86_64__)
125 asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0));
126 wrmsrl(0xc0000101, &cpu0_pda); /* 0xc0000101 is MSR_GS_BASE */
127 cpu0_pda.irqcount = -1;
128 cpu0_pda.irqstackptr = irqstack + 1024 * 4 * STACK_PAGES;
129 #endif
130 /* inintialise event handler */
131 for ( i = 0; i < NR_EVS; i++ )
132 {
133 ev_actions[i].handler = default_handler;
134 mask_evtchn(i);
135 }
136 }
138 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
139 {
140 printk("[Port %d] - event received\n", port);
141 }
143 /* Create a port available to the pal for exchanging notifications.
144 Returns the result of the hypervisor call. */
146 /* Unfortunate confusion of terminology: the port is unbound as far
147 as Xen is concerned, but we automatically bind a handler to it
148 from inside mini-os. */
150 int evtchn_alloc_unbound(domid_t pal, evtchn_handler_t handler,
151 void *data, evtchn_port_t *port)
152 {
153 evtchn_alloc_unbound_t op;
154 op.dom = DOMID_SELF;
155 op.remote_dom = pal;
156 int err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
157 if (err)
158 return err;
159 *port = bind_evtchn(op.port, handler, data);
160 return err;
161 }
163 /* Connect to a port so as to allow the exchange of notifications with
164 the pal. Returns the result of the hypervisor call. */
166 int evtchn_bind_interdomain(domid_t pal, evtchn_port_t remote_port,
167 evtchn_handler_t handler, void *data,
168 evtchn_port_t *local_port)
169 {
170 evtchn_bind_interdomain_t op;
171 op.remote_dom = pal;
172 op.remote_port = remote_port;
173 int err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, &op);
174 if (err)
175 return err;
176 evtchn_port_t port = op.local_port;
177 clear_evtchn(port); /* Without, handler gets invoked now! */
178 *local_port = bind_evtchn(port, handler, data);
179 return err;
180 }