win-pvdrivers

annotate xenpci/xenbus.c @ 406:b9028997f48b

More changes to try and prevent lockups on save and restore
author James Harper <james.harper@bendigoit.com.au>
date Fri Jul 25 11:17:41 2008 +1000 (2008-07-25)
parents 1a1ddc1beba6
children a7cf863172cf
rev   line source
james@0 1 /*
james@0 2 PV Drivers for Windows Xen HVM Domains
james@0 3 Copyright (C) 2007 James Harper
james@0 4
james@0 5 This program is free software; you can redistribute it and/or
james@0 6 modify it under the terms of the GNU General Public License
james@0 7 as published by the Free Software Foundation; either version 2
james@0 8 of the License, or (at your option) any later version.
james@0 9
james@0 10 This program is distributed in the hope that it will be useful,
james@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
james@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
james@0 13 GNU General Public License for more details.
james@0 14
james@0 15 You should have received a copy of the GNU General Public License
james@0 16 along with this program; if not, write to the Free Software
james@0 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
james@0 18 */
james@0 19
james@0 20 #include "xenpci.h"
james@0 21 #include "io/xs_wire.h"
james@0 22 #include <stdlib.h>
james@0 23
james@0 24 #pragma warning( disable : 4204 )
james@0 25 #pragma warning( disable : 4221 )
james@0 26
andy@15 27 struct write_req {
andy@15 28 const void *data;
andy@15 29 unsigned len;
james@0 30 };
james@0 31
andy@328 32 static DDKAPI void
james@0 33 XenBus_ReadThreadProc(PVOID StartContext);
andy@328 34 static DDKAPI void
james@0 35 XenBus_WatchThreadProc(PVOID StartContext);
andy@328 36 static DDKAPI BOOLEAN
james@0 37 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext);
james@0 38
james@404 39 /* called with xenbus_mutex held */
james@258 40 static int allocate_xenbus_id(PXENPCI_DEVICE_DATA xpdd)
james@0 41 {
james@0 42 static int probe;
james@0 43 int o_probe;
james@390 44 KIRQL old_irql;
james@0 45
james@342 46 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 47
james@0 48 for (;;)
james@0 49 {
james@390 50 KeAcquireSpinLock(&xpdd->xenbus_id_lock, &old_irql);
andy@15 51 if (xpdd->nr_live_reqs < NR_XB_REQS)
james@0 52 break;
james@390 53 KeReleaseSpinLock(&xpdd->xenbus_id_lock, old_irql);
james@390 54 KeWaitForSingleObject(&xpdd->xenbus_id_event, Executive, KernelMode, FALSE, NULL);
james@0 55 }
james@0 56
james@0 57 o_probe = probe;
james@0 58
james@0 59 for (;;)
james@0 60 {
andy@15 61 if (!xpdd->req_info[o_probe].In_Use)
james@0 62 break;
andy@15 63 o_probe = (o_probe + 1) % NR_XB_REQS;
james@390 64 ASSERT(o_probe != probe);
james@0 65 }
andy@15 66 xpdd->nr_live_reqs++;
andy@15 67 xpdd->req_info[o_probe].In_Use = 1;
andy@15 68 probe = (o_probe + 1) % NR_XB_REQS;
james@390 69 KeReleaseSpinLock(&xpdd->xenbus_id_lock, old_irql);
andy@15 70 KeInitializeEvent(&xpdd->req_info[o_probe].WaitEvent, SynchronizationEvent, FALSE);
james@0 71
james@342 72 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 73
james@0 74 return o_probe;
james@0 75 }
james@0 76
james@404 77 /* called with xenbus_mutex held */
james@258 78 static void release_xenbus_id(PXENPCI_DEVICE_DATA xpdd, int id)
james@0 79 {
james@390 80 KIRQL old_irql;
james@390 81
james@390 82 ASSERT(xpdd->req_info[id].In_Use);
james@390 83 KeAcquireSpinLock(&xpdd->xenbus_id_lock, &old_irql);
james@390 84 xpdd->req_info[id].In_Use = 0;
james@390 85 xpdd->nr_live_reqs--;
james@390 86 xpdd->req_info[id].In_Use = 0;
james@390 87 if (xpdd->nr_live_reqs == NR_XB_REQS - 1)
james@390 88 KeSetEvent(&xpdd->xenbus_id_event, IO_NO_INCREMENT, FALSE);
james@390 89 KeReleaseSpinLock(&xpdd->xenbus_id_lock, old_irql);
james@0 90 }
james@0 91
james@157 92 // This routine free's the rep structure if there was an error!!!
james@0 93 static char *errmsg(struct xsd_sockmsg *rep)
james@0 94 {
james@0 95 char *res;
andy@15 96
james@0 97 if (!rep) {
james@0 98 char msg[] = "No reply";
james@0 99 size_t len = strlen(msg) + 1;
james@0 100 return memcpy(ExAllocatePoolWithTag(NonPagedPool, len, XENPCI_POOL_TAG), msg, len);
james@0 101 }
james@0 102 if (rep->type != XS_ERROR)
james@0 103 return NULL;
james@0 104 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
james@0 105 memcpy(res, rep + 1, rep->len);
james@0 106 res[rep->len] = 0;
james@0 107 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 108 return res;
james@0 109 }
james@0 110
james@0 111 static void memcpy_from_ring(const void *Ring,
james@0 112 void *Dest,
james@0 113 int off,
james@0 114 int len)
james@0 115 {
james@0 116 int c1, c2;
james@0 117 const char *ring = Ring;
james@0 118 char *dest = Dest;
james@0 119 c1 = min(len, XENSTORE_RING_SIZE - off);
james@0 120 c2 = len - c1;
james@0 121 memcpy(dest, ring + off, c1);
james@0 122 memcpy(dest + c1, ring, c2);
james@0 123 }
james@0 124
james@404 125 /* called with xenbus_mutex held */
andy@15 126 static void xb_write(
james@258 127 PXENPCI_DEVICE_DATA xpdd,
andy@13 128 int type,
andy@13 129 int req_id,
andy@13 130 xenbus_transaction_t trans_id,
andy@13 131 const struct write_req *req,
andy@13 132 int nr_reqs)
james@0 133 {
james@0 134 XENSTORE_RING_IDX prod;
james@0 135 int r;
james@0 136 size_t len = 0;
james@0 137 const struct write_req *cur_req;
james@0 138 size_t req_off;
james@0 139 size_t total_off;
james@0 140 size_t this_chunk;
james@0 141 struct xsd_sockmsg m = {type, req_id, trans_id };
james@0 142 struct write_req header_req = { &m, sizeof(m) };
james@0 143
james@342 144 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 145
james@0 146 for (r = 0; r < nr_reqs; r++)
james@145 147 len += (size_t)req[r].len;
james@145 148 m.len = (ULONG)len;
james@0 149 len += sizeof(m);
james@0 150
james@0 151 cur_req = &header_req;
james@0 152
james@390 153 ASSERT(len <= XENSTORE_RING_SIZE);
james@0 154 /* Wait for the ring to drain to the point where we can send the
james@0 155 message. */
andy@15 156 prod = xpdd->xen_store_interface->req_prod;
james@0 157
james@390 158 while (prod + len - xpdd->xen_store_interface->req_cons > XENSTORE_RING_SIZE)
james@0 159 {
james@0 160 /* Wait for there to be space on the ring */
james@390 161 KeWaitForSingleObject(&xpdd->XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@15 162 prod = xpdd->xen_store_interface->req_prod;
james@0 163 }
james@0 164
james@0 165 /* We're now guaranteed to be able to send the message without
james@0 166 overflowing the ring. Do so. */
james@0 167
james@0 168 total_off = 0;
james@0 169 req_off = 0;
james@0 170
james@0 171 while (total_off < len)
james@0 172 {
james@0 173 this_chunk = min(cur_req->len - req_off,XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
andy@15 174 memcpy((char *)xpdd->xen_store_interface->req + MASK_XENSTORE_IDX(prod), (char *)cur_req->data + req_off, this_chunk);
james@145 175 prod += (XENSTORE_RING_IDX)this_chunk;
james@0 176 req_off += this_chunk;
james@0 177 total_off += this_chunk;
james@0 178 if (req_off == cur_req->len)
james@0 179 {
james@0 180 req_off = 0;
james@0 181 if (cur_req == &header_req)
james@0 182 cur_req = req;
james@0 183 else
james@0 184 cur_req++;
james@0 185 }
james@0 186 }
james@0 187
james@0 188 /* Remote must see entire message before updating indexes */
james@0 189 KeMemoryBarrier();
james@0 190
james@145 191 xpdd->xen_store_interface->req_prod += (XENSTORE_RING_IDX)len;
james@0 192
james@0 193 /* Send evtchn to notify remote */
james@258 194 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn);
james@0 195
james@342 196 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 197 }
james@0 198
james@404 199 /* called with xenbus_mutex held */
james@0 200 static struct xsd_sockmsg *
andy@13 201 xenbus_msg_reply(
james@258 202 PXENPCI_DEVICE_DATA xpdd,
andy@13 203 int type,
andy@13 204 xenbus_transaction_t trans,
andy@13 205 struct write_req *io,
andy@13 206 int nr_reqs)
james@0 207 {
james@0 208 int id;
james@0 209
james@342 210 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 211
james@258 212 id = allocate_xenbus_id(xpdd);
andy@94 213
james@258 214 xb_write(xpdd, type, id, trans, io, nr_reqs);
james@95 215
andy@15 216 KeWaitForSingleObject(&xpdd->req_info[id].WaitEvent, Executive, KernelMode, FALSE, NULL);
james@95 217
james@258 218 release_xenbus_id(xpdd, id);
james@0 219
james@342 220 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@96 221
andy@94 222 return xpdd->req_info[id].Reply;
james@0 223 }
james@0 224
james@404 225 /*
james@404 226 Called at PASSIVE_LEVEL
james@404 227 Acquires the mutex
james@404 228 */
james@0 229 char *
andy@15 230 XenBus_Read(
andy@13 231 PVOID Context,
andy@13 232 xenbus_transaction_t xbt,
andy@13 233 const char *path,
andy@13 234 char **value)
james@0 235 {
james@258 236 PXENPCI_DEVICE_DATA xpdd = Context;
james@145 237 struct write_req req[] = { {path, (ULONG)strlen(path) + 1} };
andy@13 238 struct xsd_sockmsg *rep;
andy@13 239 char *res;
andy@13 240 char *msg;
james@0 241
james@342 242 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 243
james@272 244 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 245
james@404 246 // get mutex or wait for mutex to be acquired
james@404 247
james@404 248 ExAcquireFastMutex(&xpdd->xenbus_mutex);
james@258 249 rep = xenbus_msg_reply(xpdd, XS_READ, xbt, req, ARRAY_SIZE(req));
james@404 250 ExReleaseFastMutex(&xpdd->xenbus_mutex);
andy@13 251 msg = errmsg(rep);
andy@13 252 if (msg) {
andy@13 253 *value = NULL;
andy@13 254 return msg;
andy@13 255 }
andy@13 256 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
andy@13 257 memcpy(res, rep + 1, rep->len);
andy@13 258 res[rep->len] = 0;
andy@13 259 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 260 *value = res;
james@95 261
james@342 262 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 263
andy@13 264 return NULL;
james@0 265 }
james@0 266
james@404 267 /*
james@404 268 Called at PASSIVE_LEVEL
james@404 269 Acquires the mutex
james@404 270 */
james@0 271 char *
andy@13 272 XenBus_Write(
andy@13 273 PVOID Context,
andy@13 274 xenbus_transaction_t xbt,
andy@13 275 const char *path,
andy@13 276 const char *value)
james@0 277 {
james@258 278 PXENPCI_DEVICE_DATA xpdd = Context;
andy@13 279 struct write_req req[] = {
james@145 280 {path, (ULONG)strlen(path) + 1},
andy@264 281 {value, (ULONG)strlen(value)},
andy@13 282 };
andy@13 283 struct xsd_sockmsg *rep;
andy@13 284 char *msg;
james@0 285
james@342 286 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 287
james@272 288 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 289
james@404 290 ExAcquireFastMutex(&xpdd->xenbus_mutex);
james@258 291 rep = xenbus_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
james@404 292 ExReleaseFastMutex(&xpdd->xenbus_mutex);
andy@13 293 msg = errmsg(rep);
andy@13 294 if (msg)
andy@13 295 return msg;
andy@13 296 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 297
james@342 298 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 299
andy@13 300 return NULL;
james@0 301 }
james@0 302
james@390 303 NTSTATUS
james@342 304 XenBus_Connect(PXENPCI_DEVICE_DATA xpdd)
james@342 305 {
james@342 306 PHYSICAL_ADDRESS pa_xen_store_interface;
james@342 307 xen_ulong_t xen_store_mfn;
james@390 308 NTSTATUS status;
james@342 309
james@342 310 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
james@342 311 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
james@342 312 pa_xen_store_interface.QuadPart = (ULONGLONG)xen_store_mfn << PAGE_SHIFT;
james@342 313 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
james@342 314
james@342 315 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Interrupt, xpdd);
james@390 316
james@390 317 xpdd->XenBus_ShuttingDown = FALSE;
james@390 318 KeMemoryBarrier();
james@390 319
james@390 320 status = PsCreateSystemThread(&xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_ReadThreadProc, xpdd);
james@390 321 if (!NT_SUCCESS(status))
james@390 322 {
james@390 323 KdPrint((__DRIVER_NAME " Could not start read thread\n"));
james@390 324 return status;
james@390 325 }
james@390 326
james@390 327 status = PsCreateSystemThread(&xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_WatchThreadProc, xpdd);
james@390 328 if (!NT_SUCCESS(status))
james@390 329 {
james@390 330 KdPrint((__DRIVER_NAME " Could not start watch thread\n"));
james@390 331 return status;
james@390 332 }
james@390 333
james@390 334 return STATUS_SUCCESS;
james@342 335 }
james@342 336
james@0 337 NTSTATUS
james@258 338 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
james@0 339 {
james@390 340 NTSTATUS status;
james@35 341 int i;
andy@37 342
james@267 343 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@35 344
james@272 345 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@35 346
james@406 347 ExInitializeFastMutex(&xpdd->watch_mutex);
james@404 348 ExInitializeFastMutex(&xpdd->xenbus_mutex);
james@128 349
james@35 350 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@128 351 {
andy@38 352 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 353 }
james@0 354
andy@15 355 KeInitializeEvent(&xpdd->XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
andy@15 356 KeInitializeEvent(&xpdd->XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
james@390 357 KeInitializeEvent(&xpdd->xenbus_id_event, SynchronizationEvent, FALSE);
james@390 358 KeInitializeSpinLock(&xpdd->xenbus_id_lock);
andy@38 359 xpdd->XenBus_ShuttingDown = FALSE;
james@0 360
james@390 361 status = XenBus_Connect(xpdd);
james@390 362 if (!NT_SUCCESS(status))
andy@101 363 {
james@390 364 return status;
andy@101 365 }
james@342 366
james@267 367 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 368
james@0 369 return STATUS_SUCCESS;
james@0 370 }
james@0 371
james@390 372 #if 0
james@0 373 NTSTATUS
james@258 374 XenBus_Stop(PXENPCI_DEVICE_DATA xpdd)
james@0 375 {
james@0 376 int i;
james@0 377
james@267 378 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@35 379
james@0 380 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 381 {
andy@38 382 if (xpdd->XenBus_WatchEntries[i].Active)
james@258 383 XenBus_RemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path,
andy@38 384 xpdd->XenBus_WatchEntries[i].ServiceRoutine,
andy@38 385 xpdd->XenBus_WatchEntries[i].ServiceContext);
james@0 386 }
james@0 387
james@258 388 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
james@0 389
james@267 390 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@35 391
james@35 392 return STATUS_SUCCESS;
james@35 393 }
james@390 394 #endif
james@35 395
james@406 396 char *
james@406 397 XenBus_SendRemWatch(
james@406 398 PVOID context,
james@406 399 xenbus_transaction_t xbt,
james@406 400 const char *path,
james@406 401 const int index)
james@406 402 {
james@406 403 struct xsd_sockmsg *rep;
james@406 404 char *msg;
james@406 405 char Token[20];
james@406 406 struct write_req req[2];
james@406 407
james@406 408 req[0].data = path;
james@406 409 req[0].len = (ULONG)strlen(path) + 1;
james@406 410
james@406 411 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", index);
james@406 412 req[1].data = Token;
james@406 413 req[1].len = (ULONG)strlen(Token) + 1;
james@406 414
james@406 415 rep = xenbus_msg_reply(context, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@406 416
james@406 417 msg = errmsg(rep);
james@406 418 if (msg)
james@406 419 return msg;
james@406 420
james@406 421 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@406 422
james@406 423 return NULL;
james@406 424 }
james@406 425
james@35 426 NTSTATUS
james@390 427 XenBus_StopThreads(PXENPCI_DEVICE_DATA xpdd)
james@35 428 {
andy@56 429 //KWAIT_BLOCK WaitBlockArray[2];
james@406 430 int i;
james@35 431 PVOID WaitArray[2];
james@35 432
james@406 433 FUNCTION_ENTER();
james@406 434
james@272 435 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 436
james@406 437 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@406 438 ExAcquireFastMutex(&xpdd->watch_mutex);
james@406 439 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
james@406 440 if (xpdd->XenBus_WatchEntries[i].Active)
james@406 441 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@406 442 }
james@406 443 ExReleaseFastMutex(&xpdd->watch_mutex);
james@406 444
andy@38 445 xpdd->XenBus_ShuttingDown = TRUE;
james@406 446 KeMemoryBarrier();
james@35 447
james@406 448 KdPrint((__DRIVER_NAME " Setting ReadThreadEvent\n"));
james@385 449 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, IO_NO_INCREMENT, FALSE);
james@406 450 KdPrint((__DRIVER_NAME " Setting WatchThreadEvent\n"));
james@385 451 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, IO_NO_INCREMENT, FALSE);
andy@38 452 ObReferenceObjectByHandle(xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[0], NULL);
andy@38 453 ObReferenceObjectByHandle(xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[1], NULL);
james@406 454 KdPrint((__DRIVER_NAME " Waiting for ReadThread\n"));
james@48 455 KeWaitForSingleObject(WaitArray[0], Executive, KernelMode, FALSE, NULL);
james@406 456 KdPrint((__DRIVER_NAME " Waiting for WatchThread\n"));
james@48 457 KeWaitForSingleObject(WaitArray[1], Executive, KernelMode, FALSE, NULL);
james@406 458 KdPrint((__DRIVER_NAME " Done\n"));
james@406 459
andy@38 460 xpdd->XenBus_ShuttingDown = FALSE;
james@35 461
andy@15 462 ZwClose(xpdd->XenBus_WatchThreadHandle);
andy@15 463 ZwClose(xpdd->XenBus_ReadThreadHandle);
james@0 464
james@406 465 FUNCTION_EXIT();
james@35 466
james@0 467 return STATUS_SUCCESS;
james@0 468 }
james@0 469
james@0 470 char *
andy@13 471 XenBus_List(
andy@13 472 PVOID Context,
andy@13 473 xenbus_transaction_t xbt,
andy@13 474 const char *pre,
andy@13 475 char ***contents)
james@0 476 {
james@258 477 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 478 struct xsd_sockmsg *reply, *repmsg;
james@145 479 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 480 ULONG nr_elems, x, i;
james@0 481 char **res;
james@0 482 char *msg;
james@0 483
james@134 484 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 485
james@272 486 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 487
james@258 488 repmsg = xenbus_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 489 msg = errmsg(repmsg);
andy@15 490 if (msg)
andy@15 491 {
james@0 492 *contents = NULL;
james@134 493 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 494 return msg;
james@0 495 }
james@0 496 reply = repmsg + 1;
james@0 497 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 498 {
james@0 499 nr_elems += (((char *)reply)[x] == 0);
andy@15 500 }
andy@15 501 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 502 XENPCI_POOL_TAG);
andy@15 503 for (x = i = 0; i < nr_elems; i++)
andy@15 504 {
james@145 505 int l = (int)strlen((char *)reply + x);
james@0 506 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 507 memcpy(res[i], (char *)reply + x, l + 1);
james@0 508 x += l + 1;
james@0 509 }
james@0 510 res[i] = NULL;
james@0 511 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 512 *contents = res;
james@134 513 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 514 return NULL;
james@0 515 }
james@0 516
andy@328 517 static DDKAPI void
andy@15 518 XenBus_ReadThreadProc(PVOID StartContext)
james@0 519 {
james@0 520 int NewWriteIndex;
james@0 521 struct xsd_sockmsg msg;
james@0 522 char *payload;
james@0 523 char *path, *token;
james@258 524 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@0 525
james@0 526 for(;;)
james@0 527 {
andy@15 528 KeWaitForSingleObject(&xpdd->XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 529 if (xpdd->XenBus_ShuttingDown)
james@35 530 {
james@35 531 KdPrint((__DRIVER_NAME " Shutdown detected in ReadThreadProc\n"));
james@35 532 PsTerminateSystemThread(0);
james@35 533 }
andy@15 534 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
james@0 535 {
james@0 536 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
andy@15 537 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg))
james@0 538 {
james@0 539 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@0 540 break;
james@0 541 }
james@0 542 KeMemoryBarrier();
andy@15 543 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
andy@15 544 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(msg));
andy@15 545 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@0 546 {
james@0 547 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@0 548 break;
james@0 549 }
james@0 550
andy@20 551 if (msg.type != XS_WATCH_EVENT)
andy@20 552 {
andy@20 553 xpdd->req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@20 554 memcpy_from_ring(xpdd->xen_store_interface->rsp,
andy@20 555 xpdd->req_info[msg.req_id].Reply,
andy@20 556 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
andy@20 557 msg.len + sizeof(msg));
andy@20 558 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@385 559 KeSetEvent(&xpdd->req_info[msg.req_id].WaitEvent, IO_NO_INCREMENT, FALSE);
andy@20 560 }
andy@20 561 else // a watch: add to watch ring and signal watch thread
james@0 562 {
james@0 563 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@15 564 memcpy_from_ring(xpdd->xen_store_interface->rsp, payload,
andy@15 565 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), msg.len + sizeof(msg));
andy@15 566 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 567 path = payload + sizeof(msg);
james@0 568 token = path + strlen(path) + 1;
james@0 569
andy@15 570 NewWriteIndex = (xpdd->XenBus_WatchRingWriteIndex + 1) & 127;
andy@15 571 if (NewWriteIndex != xpdd->XenBus_WatchRingReadIndex)
james@0 572 {
andy@15 573 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Path, path, 128);
andy@15 574 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Token, token, 10);
andy@15 575 xpdd->XenBus_WatchRingWriteIndex = NewWriteIndex;
james@0 576 }
james@0 577 else
james@0 578 {
james@0 579 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
james@0 580 // drop the message on the floor
james@0 581 continue;
james@0 582 }
james@0 583
james@0 584 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
james@385 585 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, IO_NO_INCREMENT, FALSE);
james@0 586 }
james@0 587 }
james@0 588 }
james@0 589 }
james@0 590
andy@328 591 static DDKAPI void
james@0 592 XenBus_WatchThreadProc(PVOID StartContext)
james@0 593 {
james@0 594 int index;
james@0 595 PXENBUS_WATCH_ENTRY entry;
james@258 596 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@0 597
james@0 598 for(;;)
james@0 599 {
andy@37 600 KeWaitForSingleObject(&xpdd->XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
james@406 601 ExAcquireFastMutex(&xpdd->watch_mutex);
andy@38 602 if (xpdd->XenBus_ShuttingDown)
james@35 603 {
james@35 604 KdPrint((__DRIVER_NAME " Shutdown detected in WatchThreadProc\n"));
james@406 605 ExReleaseFastMutex(&xpdd->watch_mutex);
james@35 606 PsTerminateSystemThread(0);
james@35 607 }
andy@38 608 while (xpdd->XenBus_WatchRingReadIndex != xpdd->XenBus_WatchRingWriteIndex)
james@0 609 {
andy@15 610 xpdd->XenBus_WatchRingReadIndex =
andy@15 611 (xpdd->XenBus_WatchRingReadIndex + 1) % WATCH_RING_SIZE;
andy@15 612 index = atoi(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Token);
james@0 613
andy@15 614 entry = &xpdd->XenBus_WatchEntries[index];
james@128 615 if (!entry->Active || !entry->ServiceRoutine)
james@0 616 {
james@138 617 KdPrint((__DRIVER_NAME " No watch for index %d\n", index));
james@0 618 continue;
james@0 619 }
james@0 620 entry->Count++;
andy@15 621 entry->ServiceRoutine(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
james@0 622 }
james@406 623 ExReleaseFastMutex(&xpdd->watch_mutex);
james@0 624 }
james@0 625 }
james@0 626
james@404 627 /*
james@404 628 Called at PASSIVE_LEVEL
james@404 629 Acquires the mutex
james@404 630 */
james@279 631 static char *
james@279 632 XenBus_SendAddWatch(
james@279 633 PVOID Context,
james@279 634 xenbus_transaction_t xbt,
james@279 635 const char *Path,
james@279 636 int slot)
james@279 637 {
james@279 638 PXENPCI_DEVICE_DATA xpdd = Context;
james@279 639 struct xsd_sockmsg *rep;
james@279 640 char *msg;
james@279 641 char Token[20];
james@279 642 struct write_req req[2];
james@279 643
james@279 644 req[0].data = Path;
james@279 645 req[0].len = (ULONG)strlen(Path) + 1;
james@279 646
james@279 647 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
james@279 648 req[1].data = Token;
james@279 649 req[1].len = (ULONG)strlen(Token) + 1;
james@279 650
james@404 651 ExAcquireFastMutex(&xpdd->xenbus_mutex);
james@279 652 rep = xenbus_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@404 653 ExReleaseFastMutex(&xpdd->xenbus_mutex);
james@404 654
james@279 655 msg = errmsg(rep);
james@283 656 if (!msg)
james@283 657 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@279 658
james@279 659 return msg;
james@279 660 }
james@279 661
james@342 662 /* called at PASSIVE_LEVEL */
james@390 663 NTSTATUS
james@279 664 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
james@279 665 {
james@390 666 NTSTATUS status;
james@279 667 int i;
james@341 668
andy@398 669 FUNCTION_ENTER();
james@342 670
james@390 671 status = XenBus_Connect(xpdd);
james@390 672 if (!NT_SUCCESS(status))
james@390 673 {
james@390 674 return status;
james@390 675 }
james@279 676
james@279 677 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@341 678 {
james@279 679 if (xpdd->XenBus_WatchEntries[i].Active)
james@341 680 {
james@341 681 KdPrint((__DRIVER_NAME " Adding watch for path = %s\n", xpdd->XenBus_WatchEntries[i].Path));
james@279 682 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@341 683 }
james@341 684 }
andy@398 685 FUNCTION_EXIT();
james@390 686
james@390 687 return STATUS_SUCCESS;
james@279 688 }
james@279 689
james@0 690 char *
andy@15 691 XenBus_AddWatch(
andy@13 692 PVOID Context,
andy@13 693 xenbus_transaction_t xbt,
andy@13 694 const char *Path,
andy@13 695 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 696 PVOID ServiceContext)
james@0 697 {
james@258 698 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 699 char *msg;
james@0 700 int i;
andy@94 701 PXENBUS_WATCH_ENTRY w_entry;
james@0 702
james@267 703 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 704
james@272 705 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 706
andy@94 707 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 708
james@406 709 ExAcquireFastMutex(&xpdd->watch_mutex);
james@128 710
james@0 711 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 712 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 713 break;
james@0 714
james@0 715 if (i == MAX_WATCH_ENTRIES)
james@0 716 {
james@0 717 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@406 718 ExReleaseFastMutex(&xpdd->watch_mutex);
james@0 719 return NULL;
james@0 720 }
james@0 721
andy@94 722 /* must init watchentry before starting watch */
james@128 723
andy@94 724 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 725 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 726 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 727 w_entry->ServiceContext = ServiceContext;
andy@94 728 w_entry->Count = 0;
andy@94 729 w_entry->Active = 1;
andy@94 730
james@406 731 ExReleaseFastMutex(&xpdd->watch_mutex);
james@128 732
james@279 733 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
james@0 734
andy@93 735 if (msg)
andy@93 736 {
andy@93 737 xpdd->XenBus_WatchEntries[i].Active = 0;
james@341 738 //KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch (%s)\n", msg));
andy@93 739 return msg;
andy@93 740 }
andy@93 741
james@341 742 //KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 743
james@0 744 return NULL;
james@0 745 }
james@0 746
james@0 747 char *
andy@13 748 XenBus_RemWatch(
andy@13 749 PVOID Context,
andy@13 750 xenbus_transaction_t xbt,
andy@13 751 const char *Path,
andy@13 752 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 753 PVOID ServiceContext)
james@0 754 {
james@258 755 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 756 char *msg;
james@0 757 int i;
james@0 758
james@267 759 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 760 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 761
james@406 762 ExAcquireFastMutex(&xpdd->watch_mutex);
james@128 763
james@0 764 // check that Path < 128 chars
james@0 765
james@0 766 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
andy@15 767 if (xpdd->XenBus_WatchEntries[i].Active == 1
andy@15 768 && strcmp(xpdd->XenBus_WatchEntries[i].Path, Path) == 0
andy@15 769 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 770 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@0 771 break;
james@0 772 }
james@0 773
james@0 774 if (i == MAX_WATCH_ENTRIES)
james@0 775 {
james@406 776 ExReleaseFastMutex(&xpdd->watch_mutex);
james@0 777 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 778 return NULL;
james@0 779 }
james@0 780
james@128 781 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 782 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 783
james@406 784 ExReleaseFastMutex(&xpdd->watch_mutex);
james@0 785
james@406 786 msg = XenBus_SendRemWatch(Context, xbt, Path, i);
james@406 787
james@267 788 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 789
james@406 790 return msg;
james@0 791 }
james@0 792
james@0 793
james@0 794 char *
andy@13 795 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 796 {
james@258 797 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 798 /* xenstored becomes angry if you send a length 0 message, so just
james@0 799 shove a nul terminator on the end */
james@0 800 struct write_req req = { "", 1};
james@0 801 struct xsd_sockmsg *rep;
james@0 802 char *err;
james@0 803
james@134 804 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 805 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 806
james@258 807 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 808 err = errmsg(rep);
james@0 809 if (err)
james@0 810 return err;
james@0 811 *xbt = atoi((char *)(rep + 1));
james@0 812 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 813 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 814
james@134 815 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 816
james@0 817 return NULL;
james@0 818 }
james@0 819
james@0 820 char *
andy@15 821 XenBus_EndTransaction(
andy@15 822 PVOID Context,
andy@15 823 xenbus_transaction_t t,
andy@15 824 int abort,
andy@15 825 int *retry)
james@0 826 {
james@258 827 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 828 struct xsd_sockmsg *rep;
james@0 829 struct write_req req;
james@0 830 char *err;
james@0 831
james@134 832 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 833
james@0 834 *retry = 0;
james@0 835
james@0 836 req.data = abort ? "F" : "T";
james@0 837 req.len = 2;
james@258 838 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 839 err = errmsg(rep);
james@0 840 if (err) {
james@0 841 if (!strcmp(err, "EAGAIN")) {
james@0 842 *retry = 1;
james@0 843 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 844 return NULL;
james@0 845 } else {
james@0 846 return err;
james@0 847 }
james@0 848 }
james@0 849 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 850
james@134 851 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 852
james@0 853 return NULL;
james@0 854 }
james@0 855
andy@328 856 static DDKAPI BOOLEAN
james@0 857 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 858 {
james@258 859 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
andy@15 860
james@0 861 UNREFERENCED_PARAMETER(Interrupt);
james@0 862
james@134 863 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 864
james@385 865 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, IO_NO_INCREMENT, FALSE);
james@0 866
james@134 867 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 868
james@0 869 return TRUE;
james@0 870 }
james@0 871
james@0 872 char *
andy@15 873 XenBus_Printf(
andy@15 874 PVOID Context,
andy@15 875 xenbus_transaction_t xbt,
andy@15 876 const char *path,
andy@15 877 const char *fmt,
andy@15 878 ...)
james@0 879 {
james@258 880 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 881 va_list ap;
andy@101 882 char buf[512];
james@95 883 char *retval;
james@95 884
james@134 885 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 886 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 887
james@0 888 va_start(ap, fmt);
james@0 889 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 890 va_end(ap);
james@258 891 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 892
james@134 893 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 894
james@95 895 return retval;
andy@37 896 }