ia64/xen-unstable

annotate xen/common/event_channel.c @ 1258:bab99b03bc2b

bitkeeper revision 1.825.3.17 (40693a952A4uQatvbQf9p4edlPR6ww)

irq.c, sched.h, event_channel.c, Rules.mk:
Fix a memory bug in new PIRQ code in Xen.
author kaf24@scramble.cl.cam.ac.uk
date Tue Mar 30 09:15:01 2004 +0000 (2004-03-30)
parents 34aaad33c28d
children 9c5521fa1195
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@1239 24 #include <xen/irq.h>
kaf24@954 25
kaf24@1127 26 #include <hypervisor-ifs/hypervisor-if.h>
kaf24@1127 27 #include <hypervisor-ifs/event_channel.h>
kaf24@1127 28
kaf24@1218 29 #define INIT_EVENT_CHANNELS 16
kaf24@1218 30 #define MAX_EVENT_CHANNELS 1024
kaf24@954 31
kaf24@1127 32 static int get_free_port(struct task_struct *p)
kaf24@954 33 {
kaf24@1127 34 int max, port;
kaf24@1127 35 event_channel_t *chn;
kaf24@1127 36
kaf24@1127 37 max = p->max_event_channel;
kaf24@1127 38 chn = p->event_channel;
kaf24@1127 39
kaf24@1127 40 for ( port = 0; port < max; port++ )
kaf24@1127 41 if ( chn[port].state == ECS_FREE )
kaf24@1127 42 break;
kaf24@1127 43
kaf24@1127 44 if ( port == max )
kaf24@1127 45 {
kaf24@1127 46 if ( max == MAX_EVENT_CHANNELS )
kaf24@1127 47 return -ENOSPC;
kaf24@1127 48
kaf24@1258 49 max *= 2;
kaf24@1127 50
kaf24@1127 51 chn = kmalloc(max * sizeof(event_channel_t), GFP_KERNEL);
kaf24@1127 52 if ( unlikely(chn == NULL) )
kaf24@1127 53 return -ENOMEM;
kaf24@1127 54
kaf24@1127 55 memset(chn, 0, max * sizeof(event_channel_t));
kaf24@1127 56
kaf24@1127 57 if ( p->event_channel != NULL )
kaf24@1127 58 {
kaf24@1127 59 memcpy(chn, p->event_channel, (max/2) * sizeof(event_channel_t));
kaf24@1127 60 kfree(p->event_channel);
kaf24@1127 61 }
kaf24@1127 62
kaf24@1127 63 p->event_channel = chn;
kaf24@1127 64 p->max_event_channel = max;
kaf24@1127 65 }
kaf24@1127 66
kaf24@1127 67 return port;
kaf24@1127 68 }
kaf24@1127 69
kaf24@1218 70 static long evtchn_bind_interdomain(evtchn_bind_interdomain_t *bind)
kaf24@1127 71 {
kaf24@1129 72 struct task_struct *p1, *p2;
kaf24@1129 73 int port1 = 0, port2 = 0;
kaf24@1218 74 domid_t dom1 = bind->dom1, dom2 = bind->dom2;
kaf24@954 75 long rc = 0;
kaf24@954 76
kaf24@1127 77 if ( !IS_PRIV(current) )
kaf24@1127 78 return -EPERM;
kaf24@954 79
kaf24@1129 80 if ( dom1 == DOMID_SELF )
kaf24@1129 81 dom1 = current->domain;
kaf24@1129 82 if ( dom2 == DOMID_SELF )
kaf24@1135 83 dom2 = current->domain;
kaf24@1127 84
kaf24@1129 85 if ( ((p1 = find_domain_by_id(dom1)) == NULL) ||
kaf24@1129 86 ((p2 = find_domain_by_id(dom2)) == NULL) )
kaf24@1127 87 {
kaf24@1129 88 if ( p1 != NULL )
kaf24@1129 89 put_task_struct(p1);
kaf24@1127 90 return -ESRCH;
kaf24@1127 91 }
kaf24@1127 92
kaf24@1127 93 /* Avoid deadlock by first acquiring lock of domain with smaller id. */
kaf24@1129 94 if ( dom1 < dom2 )
kaf24@954 95 {
kaf24@1129 96 spin_lock(&p1->event_channel_lock);
kaf24@1129 97 spin_lock(&p2->event_channel_lock);
kaf24@954 98 }
kaf24@954 99 else
kaf24@954 100 {
kaf24@1135 101 if ( p1 != p2 )
kaf24@1135 102 spin_lock(&p2->event_channel_lock);
kaf24@1129 103 spin_lock(&p1->event_channel_lock);
kaf24@954 104 }
kaf24@954 105
kaf24@1129 106 if ( (port1 = get_free_port(p1)) < 0 )
kaf24@954 107 {
kaf24@1129 108 rc = port1;
kaf24@1127 109 goto out;
kaf24@954 110 }
kaf24@954 111
kaf24@1129 112 if ( (port2 = get_free_port(p2)) < 0 )
kaf24@954 113 {
kaf24@1129 114 rc = port2;
kaf24@1127 115 goto out;
kaf24@1127 116 }
kaf24@954 117
kaf24@1218 118 p1->event_channel[port1].u.remote.dom = p2;
kaf24@1218 119 p1->event_channel[port1].u.remote.port = (u16)port2;
kaf24@1218 120 p1->event_channel[port1].state = ECS_INTERDOMAIN;
kaf24@954 121
kaf24@1218 122 p2->event_channel[port2].u.remote.dom = p1;
kaf24@1218 123 p2->event_channel[port2].u.remote.port = (u16)port1;
kaf24@1218 124 p2->event_channel[port2].state = ECS_INTERDOMAIN;
kaf24@1127 125
kaf24@1218 126 evtchn_set_pending(p1, port1);
kaf24@1218 127 evtchn_set_pending(p2, port2);
kaf24@954 128
kaf24@954 129 out:
kaf24@1129 130 spin_unlock(&p1->event_channel_lock);
kaf24@1135 131 if ( p1 != p2 )
kaf24@1135 132 spin_unlock(&p2->event_channel_lock);
kaf24@1127 133
kaf24@1129 134 put_task_struct(p1);
kaf24@1129 135 put_task_struct(p2);
kaf24@1127 136
kaf24@1218 137 bind->port1 = port1;
kaf24@1218 138 bind->port2 = port2;
kaf24@954 139
kaf24@954 140 return rc;
kaf24@954 141 }
kaf24@954 142
kaf24@954 143
kaf24@1218 144 static long evtchn_bind_virq(evtchn_bind_virq_t *bind)
kaf24@1218 145 {
kaf24@1218 146 struct task_struct *p = current;
kaf24@1218 147 int virq = bind->virq;
kaf24@1218 148 int port;
kaf24@1218 149
kaf24@1235 150 if ( virq >= ARRAY_SIZE(p->virq_to_evtchn) )
kaf24@1218 151 return -EINVAL;
kaf24@1218 152
kaf24@1218 153 spin_lock(&p->event_channel_lock);
kaf24@1218 154
kaf24@1218 155 /*
kaf24@1218 156 * Port 0 is the fallback port for VIRQs that haven't been explicitly
kaf24@1249 157 * bound yet. The exception is the 'misdirect VIRQ', which is permanently
kaf24@1218 158 * bound to port 0.
kaf24@1218 159 */
kaf24@1218 160 if ( ((port = p->virq_to_evtchn[virq]) != 0) ||
kaf24@1249 161 (virq == VIRQ_MISDIRECT) ||
kaf24@1218 162 ((port = get_free_port(p)) < 0) )
kaf24@1218 163 goto out;
kaf24@1218 164
kaf24@1218 165 p->event_channel[port].state = ECS_VIRQ;
kaf24@1218 166 p->event_channel[port].u.virq = virq;
kaf24@1218 167
kaf24@1218 168 p->virq_to_evtchn[virq] = port;
kaf24@1218 169
kaf24@1218 170 out:
kaf24@1218 171 spin_unlock(&p->event_channel_lock);
kaf24@1218 172
kaf24@1218 173 if ( port < 0 )
kaf24@1218 174 return port;
kaf24@1218 175
kaf24@1218 176 bind->port = port;
kaf24@1218 177 return 0;
kaf24@1218 178 }
kaf24@1218 179
kaf24@1218 180
kaf24@1235 181 static long evtchn_bind_pirq(evtchn_bind_pirq_t *bind)
kaf24@1235 182 {
kaf24@1235 183 struct task_struct *p = current;
kaf24@1235 184 int pirq = bind->pirq;
kaf24@1239 185 int port, rc;
kaf24@1235 186
kaf24@1235 187 if ( pirq >= ARRAY_SIZE(p->pirq_to_evtchn) )
kaf24@1235 188 return -EINVAL;
kaf24@1235 189
kaf24@1235 190 spin_lock(&p->event_channel_lock);
kaf24@1235 191
kaf24@1239 192 if ( ((rc = port = p->pirq_to_evtchn[pirq]) != 0) ||
kaf24@1239 193 ((rc = port = get_free_port(p)) < 0) )
kaf24@1235 194 goto out;
kaf24@1235 195
kaf24@1239 196 p->pirq_to_evtchn[pirq] = port;
kaf24@1253 197 rc = pirq_guest_bind(p, pirq,
kaf24@1253 198 !!(bind->flags & BIND_PIRQ__WILL_SHARE));
kaf24@1253 199 if ( rc != 0 )
kaf24@1239 200 {
kaf24@1239 201 p->pirq_to_evtchn[pirq] = 0;
kaf24@1251 202 DPRINTK("Couldn't bind to PIRQ %d (error=%d)\n", pirq, rc);
kaf24@1239 203 goto out;
kaf24@1239 204 }
kaf24@1239 205
kaf24@1235 206 p->event_channel[port].state = ECS_PIRQ;
kaf24@1235 207 p->event_channel[port].u.pirq = pirq;
kaf24@1235 208
kaf24@1235 209 out:
kaf24@1235 210 spin_unlock(&p->event_channel_lock);
kaf24@1235 211
kaf24@1239 212 if ( rc < 0 )
kaf24@1239 213 return rc;
kaf24@1235 214
kaf24@1235 215 bind->port = port;
kaf24@1235 216 return 0;
kaf24@1235 217 }
kaf24@1235 218
kaf24@1235 219
kaf24@1218 220 static long __evtchn_close(struct task_struct *p1, int port1)
kaf24@954 221 {
kaf24@1129 222 struct task_struct *p2 = NULL;
kaf24@1129 223 event_channel_t *chn1, *chn2;
kaf24@1129 224 int port2;
kaf24@954 225 long rc = 0;
kaf24@954 226
kaf24@954 227 again:
kaf24@1129 228 spin_lock(&p1->event_channel_lock);
kaf24@954 229
kaf24@1129 230 chn1 = p1->event_channel;
kaf24@954 231
kaf24@1249 232 /* NB. Port 0 is special (VIRQ_MISDIRECT). Never let it be closed. */
kaf24@1218 233 if ( (port1 <= 0) || (port1 >= p1->max_event_channel) )
kaf24@954 234 {
kaf24@954 235 rc = -EINVAL;
kaf24@954 236 goto out;
kaf24@954 237 }
kaf24@954 238
kaf24@1218 239 switch ( chn1[port1].state )
kaf24@954 240 {
kaf24@1218 241 case ECS_FREE:
kaf24@1218 242 rc = -EINVAL;
kaf24@1218 243 goto out;
kaf24@1218 244
kaf24@1218 245 case ECS_UNBOUND:
kaf24@1218 246 break;
kaf24@1218 247
kaf24@1218 248 case ECS_PIRQ:
kaf24@1239 249 if ( (rc = pirq_guest_unbind(p1, chn1[port1].u.pirq)) == 0 )
kaf24@1239 250 p1->pirq_to_evtchn[chn1[port1].u.pirq] = 0;
kaf24@1218 251 break;
kaf24@1218 252
kaf24@1218 253 case ECS_VIRQ:
kaf24@1218 254 p1->virq_to_evtchn[chn1[port1].u.virq] = 0;
kaf24@1218 255 break;
kaf24@1218 256
kaf24@1218 257 case ECS_INTERDOMAIN:
kaf24@1129 258 if ( p2 == NULL )
kaf24@954 259 {
kaf24@1218 260 p2 = chn1[port1].u.remote.dom;
kaf24@1129 261 get_task_struct(p2);
kaf24@1127 262
kaf24@1129 263 if ( p1->domain < p2->domain )
kaf24@954 264 {
kaf24@1129 265 spin_lock(&p2->event_channel_lock);
kaf24@954 266 }
kaf24@1135 267 else if ( p1 != p2 )
kaf24@954 268 {
kaf24@1129 269 spin_unlock(&p1->event_channel_lock);
kaf24@1129 270 spin_lock(&p2->event_channel_lock);
kaf24@954 271 goto again;
kaf24@954 272 }
kaf24@954 273 }
kaf24@1218 274 else if ( p2 != chn1[port1].u.remote.dom )
kaf24@954 275 {
kaf24@954 276 rc = -EINVAL;
kaf24@954 277 goto out;
kaf24@954 278 }
kaf24@954 279
kaf24@1129 280 chn2 = p2->event_channel;
kaf24@1218 281 port2 = chn1[port1].u.remote.port;
kaf24@954 282
kaf24@1129 283 if ( port2 >= p2->max_event_channel )
kaf24@1127 284 BUG();
kaf24@1218 285 if ( chn2[port2].state != ECS_INTERDOMAIN )
kaf24@1127 286 BUG();
kaf24@1218 287 if ( chn2[port2].u.remote.dom != p1 )
kaf24@1127 288 BUG();
kaf24@954 289
kaf24@1258 290 chn2[port2].state = ECS_UNBOUND;
kaf24@1218 291 evtchn_set_exception(p2, port2);
kaf24@1218 292
kaf24@1218 293 break;
kaf24@1218 294
kaf24@1218 295 default:
kaf24@1218 296 BUG();
kaf24@954 297 }
kaf24@954 298
kaf24@1258 299 chn1[port1].state = ECS_FREE;
kaf24@1218 300 evtchn_set_exception(p1, port1);
kaf24@1133 301
kaf24@954 302 out:
kaf24@1129 303 if ( p2 != NULL )
kaf24@954 304 {
kaf24@1135 305 if ( p1 != p2 )
kaf24@1135 306 spin_unlock(&p2->event_channel_lock);
kaf24@1129 307 put_task_struct(p2);
kaf24@954 308 }
kaf24@954 309
kaf24@1145 310 spin_unlock(&p1->event_channel_lock);
kaf24@1145 311
kaf24@954 312 return rc;
kaf24@954 313 }
kaf24@954 314
kaf24@954 315
kaf24@1218 316 static long evtchn_close(evtchn_close_t *close)
kaf24@1127 317 {
kaf24@1129 318 struct task_struct *p;
kaf24@1127 319 long rc;
kaf24@1129 320 domid_t dom = close->dom;
kaf24@1127 321
kaf24@1129 322 if ( dom == DOMID_SELF )
kaf24@1129 323 dom = current->domain;
kaf24@1127 324 else if ( !IS_PRIV(current) )
kaf24@1127 325 return -EPERM;
kaf24@1127 326
kaf24@1129 327 if ( (p = find_domain_by_id(dom)) == NULL )
kaf24@1127 328 return -ESRCH;
kaf24@1127 329
kaf24@1218 330 rc = __evtchn_close(p, close->port);
kaf24@1127 331
kaf24@1129 332 put_task_struct(p);
kaf24@1127 333 return rc;
kaf24@1127 334 }
kaf24@1127 335
kaf24@1127 336
kaf24@1218 337 static long evtchn_send(int lport)
kaf24@954 338 {
kaf24@954 339 struct task_struct *lp = current, *rp;
kaf24@1127 340 int rport;
kaf24@954 341
kaf24@954 342 spin_lock(&lp->event_channel_lock);
kaf24@954 343
kaf24@1127 344 if ( unlikely(lport < 0) ||
kaf24@1127 345 unlikely(lport >= lp->max_event_channel) ||
kaf24@1218 346 unlikely(lp->event_channel[lport].state != ECS_INTERDOMAIN) )
kaf24@954 347 {
kaf24@954 348 spin_unlock(&lp->event_channel_lock);
kaf24@954 349 return -EINVAL;
kaf24@954 350 }
kaf24@954 351
kaf24@1218 352 rp = lp->event_channel[lport].u.remote.dom;
kaf24@1218 353 rport = lp->event_channel[lport].u.remote.port;
kaf24@1127 354
kaf24@1127 355 get_task_struct(rp);
kaf24@954 356
kaf24@954 357 spin_unlock(&lp->event_channel_lock);
kaf24@954 358
kaf24@1218 359 evtchn_set_pending(rp, rport);
kaf24@954 360
kaf24@954 361 put_task_struct(rp);
kaf24@1127 362
kaf24@954 363 return 0;
kaf24@954 364 }
kaf24@954 365
kaf24@954 366
kaf24@1218 367 static long evtchn_status(evtchn_status_t *status)
kaf24@954 368 {
kaf24@1129 369 struct task_struct *p;
kaf24@1218 370 domid_t dom = status->dom;
kaf24@1218 371 int port = status->port;
kaf24@1129 372 event_channel_t *chn;
kaf24@1127 373
kaf24@1129 374 if ( dom == DOMID_SELF )
kaf24@1129 375 dom = current->domain;
kaf24@1127 376 else if ( !IS_PRIV(current) )
kaf24@1127 377 return -EPERM;
kaf24@1127 378
kaf24@1129 379 if ( (p = find_domain_by_id(dom)) == NULL )
kaf24@1127 380 return -ESRCH;
kaf24@954 381
kaf24@1129 382 spin_lock(&p->event_channel_lock);
kaf24@954 383
kaf24@1129 384 chn = p->event_channel;
kaf24@954 385
kaf24@1129 386 if ( (port < 0) || (port >= p->max_event_channel) )
kaf24@954 387 {
kaf24@1129 388 spin_unlock(&p->event_channel_lock);
kaf24@1127 389 return -EINVAL;
kaf24@1127 390 }
kaf24@1127 391
kaf24@1129 392 switch ( chn[port].state )
kaf24@1127 393 {
kaf24@1127 394 case ECS_FREE:
kaf24@1127 395 status->status = EVTCHNSTAT_closed;
kaf24@1127 396 break;
kaf24@1218 397 case ECS_UNBOUND:
kaf24@1218 398 status->status = EVTCHNSTAT_unbound;
kaf24@1127 399 break;
kaf24@1218 400 case ECS_INTERDOMAIN:
kaf24@1218 401 status->status = EVTCHNSTAT_interdomain;
kaf24@1218 402 status->u.interdomain.dom = chn[port].u.remote.dom->domain;
kaf24@1218 403 status->u.interdomain.port = chn[port].u.remote.port;
kaf24@1218 404 break;
kaf24@1218 405 case ECS_PIRQ:
kaf24@1218 406 status->status = EVTCHNSTAT_pirq;
kaf24@1218 407 status->u.pirq = chn[port].u.pirq;
kaf24@1218 408 break;
kaf24@1218 409 case ECS_VIRQ:
kaf24@1218 410 status->status = EVTCHNSTAT_virq;
kaf24@1218 411 status->u.virq = chn[port].u.virq;
kaf24@1127 412 break;
kaf24@1127 413 default:
kaf24@1127 414 BUG();
kaf24@954 415 }
kaf24@954 416
kaf24@1129 417 spin_unlock(&p->event_channel_lock);
kaf24@1127 418 return 0;
kaf24@954 419 }
kaf24@954 420
kaf24@954 421
kaf24@1127 422 long do_event_channel_op(evtchn_op_t *uop)
kaf24@954 423 {
kaf24@954 424 long rc;
kaf24@1127 425 evtchn_op_t op;
kaf24@954 426
kaf24@1127 427 if ( copy_from_user(&op, uop, sizeof(op)) != 0 )
kaf24@1127 428 return -EFAULT;
kaf24@1127 429
kaf24@1127 430 switch ( op.cmd )
kaf24@954 431 {
kaf24@1218 432 case EVTCHNOP_bind_interdomain:
kaf24@1218 433 rc = evtchn_bind_interdomain(&op.u.bind_interdomain);
kaf24@1235 434 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1218 435 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1218 436 break;
kaf24@1218 437
kaf24@1218 438 case EVTCHNOP_bind_virq:
kaf24@1218 439 rc = evtchn_bind_virq(&op.u.bind_virq);
kaf24@1235 440 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1235 441 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@1235 442 break;
kaf24@1235 443
kaf24@1235 444 case EVTCHNOP_bind_pirq:
kaf24@1235 445 rc = evtchn_bind_pirq(&op.u.bind_pirq);
kaf24@1235 446 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1127 447 rc = -EFAULT; /* Cleaning up here would be a mess! */
kaf24@954 448 break;
kaf24@954 449
kaf24@954 450 case EVTCHNOP_close:
kaf24@1218 451 rc = evtchn_close(&op.u.close);
kaf24@954 452 break;
kaf24@954 453
kaf24@954 454 case EVTCHNOP_send:
kaf24@1218 455 rc = evtchn_send(op.u.send.local_port);
kaf24@954 456 break;
kaf24@954 457
kaf24@954 458 case EVTCHNOP_status:
kaf24@1218 459 rc = evtchn_status(&op.u.status);
kaf24@1235 460 if ( (rc == 0) && (copy_to_user(uop, &op, sizeof(op)) != 0) )
kaf24@1127 461 rc = -EFAULT;
kaf24@954 462 break;
kaf24@954 463
kaf24@954 464 default:
kaf24@954 465 rc = -ENOSYS;
kaf24@954 466 break;
kaf24@954 467 }
kaf24@954 468
kaf24@954 469 return rc;
kaf24@954 470 }
kaf24@954 471
kaf24@954 472
kaf24@1218 473 int init_event_channels(struct task_struct *p)
kaf24@1218 474 {
kaf24@1218 475 spin_lock_init(&p->event_channel_lock);
kaf24@1218 476 p->event_channel = kmalloc(INIT_EVENT_CHANNELS * sizeof(event_channel_t),
kaf24@1218 477 GFP_KERNEL);
kaf24@1218 478 if ( unlikely(p->event_channel == NULL) )
kaf24@1218 479 return -ENOMEM;
kaf24@1218 480 p->max_event_channel = INIT_EVENT_CHANNELS;
kaf24@1218 481 memset(p->event_channel, 0, INIT_EVENT_CHANNELS * sizeof(event_channel_t));
kaf24@1218 482 p->event_channel[0].state = ECS_VIRQ;
kaf24@1249 483 p->event_channel[0].u.virq = VIRQ_MISDIRECT;
kaf24@1218 484 return 0;
kaf24@1218 485 }
kaf24@1218 486
kaf24@1218 487
kaf24@954 488 void destroy_event_channels(struct task_struct *p)
kaf24@954 489 {
kaf24@954 490 int i;
kaf24@954 491 if ( p->event_channel != NULL )
kaf24@954 492 {
kaf24@954 493 for ( i = 0; i < p->max_event_channel; i++ )
kaf24@1218 494 (void)__evtchn_close(p, i);
kaf24@954 495 kfree(p->event_channel);
kaf24@954 496 }
kaf24@954 497 }