ia64/xen-unstable

annotate xen/common/event_channel.c @ 1218:fc8ea15b6dcf

bitkeeper revision 1.825.1.2 (4062c7cfNjG5kiKHfguNA2SIXnllng)

Many files:
New IRQ upcall world.
evtchn.c:
Rename: xenolinux-2.4.25-sparse/arch/xen/kernel/hypervisor.c -> xenolinux-2.4.25-sparse/arch/xen/kernel/evtchn.c
author kaf24@scramble.cl.cam.ac.uk
date Thu Mar 25 11:51:43 2004 +0000 (2004-03-25)
parents 9f85adafc1e1
children 53a93ee1224d d2c1f58f2edd 916d919c54b7
rev   line source
kaf24@954 1 /******************************************************************************
kaf24@954 2 * event_channel.c
kaf24@954 3 *
kaf24@954 4 * Event channels between domains.
kaf24@954 5 *
kaf24@1127 6 * Copyright (c) 2003-2004, K A Fraser.
kaf24@954 7 *
kaf24@954 8 * This program is distributed in the hope that it will be useful,
kaf24@954 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
kaf24@954 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kaf24@954 11 * GNU General Public License for more details.
kaf24@954 12 *
kaf24@954 13 * You should have received a copy of the GNU General Public License
kaf24@954 14 * along with this program; if not, write to the Free Software
kaf24@954 15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
kaf24@954 16 */
kaf24@954 17
kaf24@1210 18 #include <xen/config.h>
kaf24@1210 19 #include <xen/init.h>
kaf24@1210 20 #include <xen/lib.h>
kaf24@1210 21 #include <xen/errno.h>
kaf24@1210 22 #include <xen/sched.h>
kaf24@1210 23 #include <xen/event.h>
kaf24@954 24
kaf24@1127 25 #include <hypervisor-ifs/hypervisor-if.h>
kaf24@1127 26 #include <hypervisor-ifs/event_channel.h>
kaf24@1127 27
kaf24@1218 28 #define INIT_EVENT_CHANNELS 16
kaf24@1218 29 #define MAX_EVENT_CHANNELS 1024
kaf24@954 30
kaf24@1127 31 static int get_free_port(struct task_struct *p)
kaf24@954 32 {
kaf24@1127 33 int max, port;
kaf24@1127 34 event_channel_t *chn;
kaf24@1127 35
kaf24@1127 36 max = p->max_event_channel;
kaf24@1127 37 chn = p->event_channel;
kaf24@1127 38
kaf24@1127 39 for ( port = 0; port < max; port++ )
kaf24@1127 40 if ( chn[port].state == ECS_FREE )
kaf24@1127 41 break;
kaf24@1127 42
kaf24@1127 43 if ( port == max )
kaf24@1127 44 {
kaf24@1127 45 if ( max == MAX_EVENT_CHANNELS )
kaf24@1127 46 return -ENOSPC;
kaf24@1127 47
kaf24@1127 48 max = (max == 0) ? 4 : (max * 2);
kaf24@1127 49
kaf24@1127 50 chn = kmalloc(max * sizeof(event_channel_t), GFP_KERNEL);
kaf24@1127 51 if ( unlikely(chn == NULL) )
kaf24@1127 52 return -ENOMEM;
kaf24@1127 53
kaf24@1127 54 memset(chn, 0, max * sizeof(event_channel_t));
kaf24@1127 55
kaf24@1127 56 if ( p->event_channel != NULL )
kaf24@1127 57 {
kaf24@1127 58 memcpy(chn, p->event_channel, (max/2) * sizeof(event_channel_t));
kaf24@1127 59 kfree(p->event_channel);
kaf24@1127 60 }
kaf24@1127 61
kaf24@1127 62 p->event_channel = chn;
kaf24@1127 63 p->max_event_channel = max;
kaf24@1127 64 }
kaf24@1127 65
kaf24@1127 66 return port;
kaf24@1127 67 }
kaf24@1127 68
kaf24@1218 69 static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
kaf24@1127 70 {
kaf24@1129 71 struct task_struct *p1, *p2;
kaf24@1129 72 int port1 = 0, port2 = 0;
kaf24@1218 73 domid_t dom1 = bind->dom1, dom2 = bind->dom2;
kaf24@954 74 long rc = 0;
kaf24@954 75
kaf24@1127 76 if ( !IS_PRIV(current) )
kaf24@1127 77 return -EPERM;
kaf24@954 78
kaf24@1129 79 if ( dom1 == DOMID_SELF )
kaf24@1129 80 dom1 = current->domain;
kaf24@1129 81 if ( dom2 == DOMID_SELF )
kaf24@1135 82 dom2 = current->domain;
kaf24@1127 83
kaf24@1129 84 if ( ((p1 = find_domain_by_id(dom1)) == NULL) ||
kaf24@1129 85 ((p2 = find_domain_by_id(dom2)) == NULL) )
kaf24@1127 86 {
kaf24@1129 87 if ( p1 != NULL )
kaf24@1129 88 put_task_struct(p1);
kaf24@1127 89 return -ESRCH;
kaf24@1127 90 }
kaf24@1127 91
kaf24@1127 92 /* Avoid deadlock by first acquiring lock of domain with smaller id. */
kaf24@1129 93 if ( dom1 < dom2 )
kaf24@954 94 {
kaf24@1129 95 spin_lock(&p1->event_channel_lock);
kaf24@1129 96 spin_lock(&p2->event_channel_lock);
kaf24@954 97 }
kaf24@954 98 else
kaf24@954 99 {
kaf24@1135 100 if ( p1 != p2 )
kaf24@1135 101 spin_lock(&p2->event_channel_lock);
kaf24@1129 102 spin_lock(&p1->event_channel_lock);
kaf24@954 103 }
kaf24@954 104
kaf24@1129 105 if ( (port1 = get_free_port(p1)) < 0 )
kaf24@954 106 {
kaf24@1129 107 rc = port1;
kaf24@1127 108 goto out;
kaf24@954 109 }
kaf24@954 110
kaf24@1129 111 if ( (port2 = get_free_port(p2)) < 0 )
kaf24@954 112 {
kaf24@1129 113 rc = port2;
kaf24@1127 114 goto out;
kaf24@1127 115 }
kaf24@954 116
kaf24@1218 117 p1->event_channel[port1].u.remote.dom = p2;
kaf24@1218 118 p1->event_channel[port1].u.remote.port = (u16)port2;
kaf24@1218 119 p1->event_channel[port1].state = ECS_INTERDOMAIN;
kaf24@954 120
kaf24@1218 121 p2->event_channel[port2].u.remote.dom = p1;
kaf24@1218 122 p2->event_channel[port2].u.remote.port = (u16)port1;
kaf24@1218 123 p2->event_channel[port2].state = ECS_INTERDOMAIN;
kaf24@1127 124
kaf24@1218 125 evtchn_set_pending(p1, port1);
kaf24@1218 126 evtchn_set_pending(p2, port2);
kaf24@954 127
kaf24@954 128 out:
kaf24@1129 129 spin_unlock(&p1->event_channel_lock);
kaf24@1135 130 if ( p1 != p2 )
kaf24@1135 131 spin_unlock(&p2->event_channel_lock);
kaf24@1127 132
kaf24@1129 133 put_task_struct(p1);
kaf24@1129 134 put_task_struct(p2);
kaf24@1127 135
kaf24@1218 136 bind->port1 = port1;
kaf24@1218 137 bind->port2 = port2;
kaf24@954 138
kaf24@954 139 return rc;
kaf24@954 140 }
kaf24@954 141
kaf24@954 142
kaf24@1218 143 static long evtchn_bind_virq(evtchn_bind_virq_t *bind)
kaf24@1218 144 {
kaf24@1218 145 struct task_struct *p = current;
kaf24@1218 146 int virq = bind->virq;
kaf24@1218 147 int port;
kaf24@1218 148
kaf24@1218 149 if ( virq >= NR_VIRQS )
kaf24@1218 150 return -EINVAL;
kaf24@1218 151
kaf24@1218 152 spin_lock(&p->event_channel_lock);
kaf24@1218 153
kaf24@1218 154 /*
kaf24@1218 155 * Port 0 is the fallback port for VIRQs that haven't been explicitly
kaf24@1218 156 * bound yet. The exception is the 'error VIRQ', which is permanently
kaf24@1218 157 * bound to port 0.
kaf24@1218 158 */
kaf24@1218 159 if ( ((port = p->virq_to_evtchn[virq]) != 0) ||
kaf24@1218 160 (virq == VIRQ_ERROR) ||
kaf24@1218 161 ((port = get_free_port(p)) < 0) )
kaf24@1218 162 goto out;
kaf24@1218 163
kaf24@1218 164 p->event_channel[port].state = ECS_VIRQ;
kaf24@1218 165 p->event_channel[port].u.virq = virq;
kaf24@1218 166
kaf24@1218 167 p->virq_to_evtchn[virq] = port;
kaf24@1218 168
kaf24@1218 169 out:
kaf24@1218 170 spin_unlock(&p->event_channel_lock);
kaf24@1218 171
kaf24@1218 172 if ( port < 0 )
kaf24@1218 173 return port;
kaf24@1218 174
kaf24@1218 175 bind->port = port;
kaf24@1218 176 return 0;
kaf24@1218 177 }
kaf24@1218 178
kaf24@1218 179
kaf24@1218 180 static long __evtchn_close(struct task_struct *p1, int port1)
kaf24@954 181 {
kaf24@1129 182 struct task_struct *p2 = NULL;
kaf24@1129 183 event_channel_t *chn1, *chn2;
kaf24@1129 184 int port2;
kaf24@954 185 long rc = 0;
kaf24@954 186
kaf24@954 187 again:
kaf24@1129 188 spin_lock(&p1->event_channel_lock);
kaf24@954 189
kaf24@1129 190 chn1 = p1->event_channel;
kaf24@954 191
kaf24@1218 192 /* NB. Port 0 is special (VIRQ_ERROR). Never let it be closed. */
kaf24@1218 193 if ( (port1 <= 0) || (port1 >= p1->max_event_channel) )
kaf24@954 194 {
kaf24@954 195 rc = -EINVAL;
kaf24@954 196 goto out;
kaf24@954 197 }
kaf24@954 198
kaf24@1218 199 switch ( chn1[port1].state )
kaf24@954 200 {
kaf24@1218 201 case ECS_FREE:
kaf24@1218 202 rc = -EINVAL;
kaf24@1218 203 goto out;
kaf24@1218 204
kaf24@1218 205 case ECS_UNBOUND:
kaf24@1218 206 break;
kaf24@1218 207
kaf24@1218 208 case ECS_PIRQ:
kaf24@1218 209 p1->pirq_to_evtchn[chn1[port1].u.pirq] = 0;
kaf24@1218 210 break;
kaf24@1218 211
kaf24@1218 212 case ECS_VIRQ:
kaf24@1218 213 p1->virq_to_evtchn[chn1[port1].u.virq] = 0;
kaf24@1218 214 break;
kaf24@1218 215
kaf24@1218 216 case ECS_INTERDOMAIN:
kaf24@1129 217 if ( p2 == NULL )
kaf24@954 218 {
kaf24@1218 219 p2 = chn1[port1].u.remote.dom;
kaf24@1129 220 get_task_struct(p2);
kaf24@1127 221
kaf24@1129 222 if ( p1->domain < p2->domain )
kaf24@954 223 {
kaf24@1129 224 spin_lock(&p2->event_channel_lock);
kaf24@954 225 }
kaf24@1135 226 else if ( p1 != p2 )
kaf24@954 227 {
kaf24@1129 228 spin_unlock(&p1->event_channel_lock);
kaf24@1129 229 spin_lock(&p2->event_channel_lock);
kaf24@954 230 goto again;
kaf24@954 231 }
kaf24@954 232 }
kaf24@1218 233 else if ( p2 != chn1[port1].u.remote.dom )
kaf24@954 234 {
kaf24@954 235 rc = -EINVAL;
kaf24@954 236 goto out;
kaf24@954 237 }
kaf24@954 238
kaf24@1129 239 chn2 = p2->event_channel;
kaf24@1218 240 port2 = chn1[port1].u.remote.port;
kaf24@954 241
kaf24@1129 242 if ( port2 >= p2->max_event_channel )
kaf24@1127 243 BUG();
kaf24@1218 244 if ( chn2[port2].state != ECS_INTERDOMAIN )
kaf24@1127 245 BUG();
kaf24@1218 246 if ( chn2[port2].u.remote.dom != p1 )
kaf24@1127 247 BUG();
kaf24@954 248
kaf24@1218 249 chn2[port2].state = ECS_UNBOUND;
kaf24@1218 250 chn2[port2].u.remote.dom = NULL;
kaf24@1218 251 chn2[port2].u.remote.port = 0xFFFF;
kaf24@1127 252
kaf24@1218 253 evtchn_set_exception(p2, port2);
kaf24@1218 254
kaf24@1218 255 break;
kaf24@1218 256
kaf24@1218 257 default:
kaf24@1218 258 BUG();
kaf24@954 259 }
kaf24@954 260
kaf24@1218 261 chn1[port1].state = ECS_FREE;
kaf24@1218 262 chn1[port1].u.remote.dom = NULL;
kaf24@1218 263 chn1[port1].u.remote.port = 0xFFFF;
kaf24@954 264
kaf24@1218 265 evtchn_set_exception(p1, port1);
kaf24@1133 266
kaf24@954 267 out:
kaf24@1129 268 if ( p2 != NULL )
kaf24@954 269 {
kaf24@1135 270 if ( p1 != p2 )
kaf24@1135 271 spin_unlock(&p2->event_channel_lock);
kaf24@1129 272 put_task_struct(p2);
kaf24@954 273 }
kaf24@954 274
kaf24@1145 275 spin_unlock(&p1->event_channel_lock);
kaf24@1145 276
kaf24@954 277 return rc;
kaf24@954 278 }
kaf24@954 279
kaf24@954 280
kaf24@1218 281 static long evtchn_close(evtchn_close_t *close)
kaf24@1127 282 {
kaf24@1129 283 struct task_struct *p;
kaf24@1127 284 long rc;
kaf24@1129 285 domid_t dom = close->dom;
kaf24@1127 286
kaf24@1129 287 if ( dom == DOMID_SELF )
kaf24@1129 288 dom = current->domain;
kaf24@1127 289 else if ( !IS_PRIV(current) )
kaf24@1127 290 return -EPERM;
kaf24@1127 291
kaf24@1129 292 if ( (p = find_domain_by_id(dom)) == NULL )
kaf24@1127 293 return -ESRCH;
kaf24@1127 294
kaf24@1218 295 rc = __evtchn_close(p, close->port);
kaf24@1127 296
kaf24@1129 297 put_task_struct(p);
kaf24@1127 298 return rc;
kaf24@1127 299 }
kaf24@1127 300
kaf24@1127 301
kaf24@1218 302 static long evtchn_send(int lport)
kaf24@954 303 {
kaf24@954 304 struct task_struct *lp = current, *rp;
kaf24@1127 305 int rport;
kaf24@954 306
kaf24@954 307 spin_lock(&lp->event_channel_lock);
kaf24@954 308
kaf24@1127 309 if ( unlikely(lport < 0) ||
kaf24@1127 310 unlikely(lport >= lp->max_event_channel) ||
kaf24@1218 311 unlikely(lp->event_channel[lport].state != ECS_INTERDOMAIN) )
kaf24@954 312 {
kaf24@954 313 spin_unlock(&lp->event_channel_lock);
kaf24@954 314 return -EINVAL;
kaf24@954 315 }
kaf24@954 316
kaf24@1218 317 rp = lp->event_channel[lport].u.remote.dom;
kaf24@1218 318 rport = lp->event_channel[lport].u.remote.port;
kaf24@1127 319
kaf24@1127 320 get_task_struct(rp);
kaf24@954 321
kaf24@954 322 spin_unlock(&lp->event_channel_lock);
kaf24@954 323
kaf24@1218 324 evtchn_set_pending(rp, rport);
kaf24@954 325
kaf24@954 326 put_task_struct(rp);
kaf24@1127 327
kaf24@954 328 return 0;
kaf24@954 329 }
kaf24@954 330
kaf24@954 331
kaf24@1218 332 static long evtchn_status(evtchn_status_t *status)
kaf24@954 333 {
kaf24@1129 334 struct task_struct *p;
kaf24@1218 335 domid_t dom = status->dom;
kaf24@1218 336 int port = status->port;
kaf24@1129 337 event_channel_t *chn;
kaf24@1127 338
kaf24@1129 339 if ( dom == DOMID_SELF )
kaf24@1129 340 dom = current->domain;
kaf24@1127 341 else if ( !IS_PRIV(current) )
kaf24@1127 342 return -EPERM;
kaf24@1127 343
kaf24@1129 344 if ( (p = find_domain_by_id(dom)) == NULL )
kaf24@1127 345 return -ESRCH;
kaf24@954 346
kaf24@1129 347 spin_lock(&p->event_channel_lock);
kaf24@954 348
kaf24@1129 349 chn = p->event_channel;
kaf24@954 350
kaf24@1129 351 if ( (port < 0) || (port >= p->max_event_channel) )
kaf24@954 352 {
kaf24@1129 353 spin_unlock(&p->event_channel_lock);
kaf24@1127 354 return -EINVAL;
kaf24@1127 355 }
kaf24@1127 356
kaf24@1129 357 switch ( chn[port].state )
kaf24@1127 358 {
kaf24@1127 359 case ECS_FREE:
kaf24@1127 360 status->status = EVTCHNSTAT_closed;
kaf24@1127 361 break;
kaf24@1218 362 case ECS_UNBOUND:
kaf24@1218 363 status->status = EVTCHNSTAT_unbound;
kaf24@1127 364 break;
kaf24@1218 365 case ECS_INTERDOMAIN:
kaf24@1218 366 status->status = EVTCHNSTAT_interdomain;
kaf24@1218 367 status->u.interdomain.dom = chn[port].u.remote.dom->domain;
kaf24@1218 368 status->u.interdomain.port = chn[port].u.remote.port;
kaf24@1218 369 break;
kaf24@1218 370 case ECS_PIRQ:
kaf24@1218 371 status->status = EVTCHNSTAT_pirq;
kaf24@1218 372 status->u.pirq = chn[port].u.pirq;
kaf24@1218 373 break;
kaf24@1218 374 case ECS_VIRQ:
kaf24@1218 375 status->status = EVTCHNSTAT_virq;
kaf24@1218 376 status->u.virq = chn[port].u.virq;
kaf24@1127 377 break;
kaf24@1127 378 default:
kaf24@1127 379 BUG();
kaf24@954 380 }
kaf24@954 381
kaf24@1129 382 spin_unlock(&p->event_channel_lock);
kaf24@1127 383 return 0;
kaf24@954 384 }
kaf24@954 385
kaf24@954 386
kaf24@1127 387 long do_event_channel_op(evtchn_op_t *uop)
kaf24@954 388 {
kaf24@954 389 long rc;
kaf24@1127 390 evtchn_op_t op;
kaf24@954 391
kaf24@1127 392 if ( copy_from_user(&op, uop, sizeof(op)) != 0 )
kaf24@1127 393 return -EFAULT;
kaf24@1127 394
kaf24@1127 395 switch ( op.cmd )
kaf24@954 396 {
kaf24@1218 397 case EVTCHNOP_bind_interdomain:
kaf24@1218 398 rc = evtchn_bind_interdomain(&op.u.bind_interdomain);
kaf24@1218 399 if ( copy_to_user(uop, &op, sizeof(op)) != 0 )
kaf24@1218 400 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1218 401 break;
kaf24@1218 402
kaf24@1218 403 case EVTCHNOP_bind_virq:
kaf24@1218 404 rc = evtchn_bind_virq(&op.u.bind_virq);
kaf24@1127 405 if ( copy_to_user(uop, &op, sizeof(op)) != 0 )
kaf24@1127 406 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@954 407 break;
kaf24@954 408
kaf24@954 409 case EVTCHNOP_close:
kaf24@1218 410 rc = evtchn_close(&op.u.close);
kaf24@954 411 break;
kaf24@954 412
kaf24@954 413 case EVTCHNOP_send:
kaf24@1218 414 rc = evtchn_send(op.u.send.local_port);
kaf24@954 415 break;
kaf24@954 416
kaf24@954 417 case EVTCHNOP_status:
kaf24@1218 418 rc = evtchn_status(&op.u.status);
kaf24@1127 419 if ( copy_to_user(uop, &op, sizeof(op)) != 0 )
kaf24@1127 420 rc = -EFAULT;
kaf24@954 421 break;
kaf24@954 422
kaf24@954 423 default:
kaf24@954 424 rc = -ENOSYS;
kaf24@954 425 break;
kaf24@954 426 }
kaf24@954 427
kaf24@954 428 return rc;
kaf24@954 429 }
kaf24@954 430
kaf24@954 431
kaf24@1218 432 int init_event_channels(struct task_struct *p)
kaf24@1218 433 {
kaf24@1218 434 spin_lock_init(&p->event_channel_lock);
kaf24@1218 435 p->event_channel = kmalloc(INIT_EVENT_CHANNELS * sizeof(event_channel_t),
kaf24@1218 436 GFP_KERNEL);
kaf24@1218 437 if ( unlikely(p->event_channel == NULL) )
kaf24@1218 438 return -ENOMEM;
kaf24@1218 439 p->max_event_channel = INIT_EVENT_CHANNELS;
kaf24@1218 440 memset(p->event_channel, 0, INIT_EVENT_CHANNELS * sizeof(event_channel_t));
kaf24@1218 441 p->event_channel[0].state = ECS_VIRQ;
kaf24@1218 442 p->event_channel[0].u.virq = VIRQ_ERROR;
kaf24@1218 443 return 0;
kaf24@1218 444 }
kaf24@1218 445
kaf24@1218 446
kaf24@954 447 void destroy_event_channels(struct task_struct *p)
kaf24@954 448 {
kaf24@954 449 int i;
kaf24@954 450 if ( p->event_channel != NULL )
kaf24@954 451 {
kaf24@954 452 for ( i = 0; i < p->max_event_channel; i++ )
kaf24@1218 453 (void)__evtchn_close(p, i);
kaf24@954 454 kfree(p->event_channel);
kaf24@954 455 }
kaf24@954 456 }