1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
|
/*
* QEMU Xen emulation: Shared/overlay pages support
*
* Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Authors: David Woodhouse <dwmw2@infradead.org>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu/host-utils.h"
#include "qemu/module.h"
#include "qemu/main-loop.h"
#include "qemu/cutils.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
#include "qom/object.h"
#include "migration/vmstate.h"
#include "hw/sysbus.h"
#include "hw/xen/xen.h"
#include "hw/xen/xen_backend_ops.h"
#include "xen_overlay.h"
#include "xen_evtchn.h"
#include "xen_primary_console.h"
#include "xen_xenstore.h"
#include "system/kvm.h"
#include "system/kvm_xen.h"
#include "trace.h"
#include "xenstore_impl.h"
#include "hw/xen/interface/io/xs_wire.h"
#include "hw/xen/interface/event_channel.h"
#include "hw/xen/interface/grant_table.h"
#define TYPE_XEN_XENSTORE "xen-xenstore"
OBJECT_DECLARE_SIMPLE_TYPE(XenXenstoreState, XEN_XENSTORE)
#define ENTRIES_PER_FRAME_V1 (XEN_PAGE_SIZE / sizeof(grant_entry_v1_t))
#define ENTRIES_PER_FRAME_V2 (XEN_PAGE_SIZE / sizeof(grant_entry_v2_t))
#define XENSTORE_HEADER_SIZE ((unsigned int)sizeof(struct xsd_sockmsg))
struct XenXenstoreState {
/*< private >*/
SysBusDevice busdev;
/*< public >*/
XenstoreImplState *impl;
GList *watch_events; /* for the guest */
MemoryRegion xenstore_page;
struct xenstore_domain_interface *xs;
uint8_t req_data[XENSTORE_HEADER_SIZE + XENSTORE_PAYLOAD_MAX];
uint8_t rsp_data[XENSTORE_HEADER_SIZE + XENSTORE_PAYLOAD_MAX];
uint32_t req_offset;
uint32_t rsp_offset;
bool rsp_pending;
bool fatal_error;
evtchn_port_t guest_port;
evtchn_port_t be_port;
struct xenevtchn_handle *eh;
uint8_t *impl_state;
uint32_t impl_state_size;
struct xengntdev_handle *gt;
void *granted_xs;
};
struct XenXenstoreState *xen_xenstore_singleton;
static void xen_xenstore_event(void *opaque);
static void fire_watch_cb(void *opaque, const char *path, const char *token);
static struct xenstore_backend_ops emu_xenstore_backend_ops;
static void G_GNUC_PRINTF (4, 5) relpath_printf(XenXenstoreState *s,
GList *perms,
const char *relpath,
const char *fmt, ...)
{
gchar *abspath;
gchar *value;
va_list args;
GByteArray *data;
int err;
abspath = g_strdup_printf("/local/domain/%u/%s", xen_domid, relpath);
va_start(args, fmt);
value = g_strdup_vprintf(fmt, args);
va_end(args);
data = g_byte_array_new_take((void *)value, strlen(value));
err = xs_impl_write(s->impl, DOMID_QEMU, XBT_NULL, abspath, data);
assert(!err);
g_byte_array_unref(data);
err = xs_impl_set_perms(s->impl, DOMID_QEMU, XBT_NULL, abspath, perms);
assert(!err);
g_free(abspath);
}
static void xen_xenstore_realize(DeviceState *dev, Error **errp)
{
XenXenstoreState *s = XEN_XENSTORE(dev);
GList *perms;
if (xen_mode != XEN_EMULATE) {
error_setg(errp, "Xen xenstore support is for Xen emulation");
return;
}
memory_region_init_ram(&s->xenstore_page, OBJECT(dev), "xen:xenstore_page",
XEN_PAGE_SIZE, &error_abort);
memory_region_set_enabled(&s->xenstore_page, true);
s->xs = memory_region_get_ram_ptr(&s->xenstore_page);
memset(s->xs, 0, XEN_PAGE_SIZE);
/* We can't map it this early as KVM isn't ready */
xen_xenstore_singleton = s;
s->eh = xen_be_evtchn_open();
if (!s->eh) {
error_setg(errp, "Xenstore evtchn port init failed");
return;
}
aio_set_fd_handler(qemu_get_aio_context(), xen_be_evtchn_fd(s->eh),
xen_xenstore_event, NULL, NULL, NULL, s);
s->impl = xs_impl_create(xen_domid);
/* Populate the default nodes */
/* Nodes owned by 'dom0' but readable by the guest */
perms = g_list_append(NULL, xs_perm_as_string(XS_PERM_NONE, DOMID_QEMU));
perms = g_list_append(perms, xs_perm_as_string(XS_PERM_READ, xen_domid));
relpath_printf(s, perms, "", "%s", "");
relpath_printf(s, perms, "domid", "%u", xen_domid);
relpath_printf(s, perms, "control/platform-feature-xs_reset_watches", "%u", 1);
relpath_printf(s, perms, "control/platform-feature-multiprocessor-suspend", "%u", 1);
relpath_printf(s, perms, "platform/acpi", "%u", 1);
relpath_printf(s, perms, "platform/acpi_s3", "%u", 1);
relpath_printf(s, perms, "platform/acpi_s4", "%u", 1);
relpath_printf(s, perms, "platform/acpi_laptop_slate", "%u", 0);
g_list_free_full(perms, g_free);
/* Nodes owned by the guest */
perms = g_list_append(NULL, xs_perm_as_string(XS_PERM_NONE, xen_domid));
relpath_printf(s, perms, "attr", "%s", "");
relpath_printf(s, perms, "control/shutdown", "%s", "");
relpath_printf(s, perms, "control/feature-poweroff", "%u", 1);
relpath_printf(s, perms, "control/feature-reboot", "%u", 1);
relpath_printf(s, perms, "control/feature-suspend", "%u", 1);
relpath_printf(s, perms, "control/feature-s3", "%u", 1);
relpath_printf(s, perms, "control/feature-s4", "%u", 1);
relpath_printf(s, perms, "data", "%s", "");
relpath_printf(s, perms, "device", "%s", "");
relpath_printf(s, perms, "drivers", "%s", "");
relpath_printf(s, perms, "error", "%s", "");
relpath_printf(s, perms, "feature", "%s", "");
g_list_free_full(perms, g_free);
xen_xenstore_ops = &emu_xenstore_backend_ops;
}
static bool xen_xenstore_is_needed(void *opaque)
{
return xen_mode == XEN_EMULATE;
}
static int xen_xenstore_pre_save(void *opaque)
{
XenXenstoreState *s = opaque;
GByteArray *save;
if (s->eh) {
s->guest_port = xen_be_evtchn_get_guest_port(s->eh);
}
g_free(s->impl_state);
save = xs_impl_serialize(s->impl);
s->impl_state = save->data;
s->impl_state_size = save->len;
g_byte_array_free(save, false);
return 0;
}
static int xen_xenstore_post_load(void *opaque, int ver)
{
XenXenstoreState *s = opaque;
GByteArray *save;
int ret;
/*
* As qemu/dom0, rebind to the guest's port. The Windows drivers may
* unbind the XenStore evtchn and rebind to it, having obtained the
* "remote" port through EVTCHNOP_status. In the case that migration
* occurs while it's unbound, the "remote" port needs to be the same
* as before so that the guest can find it, but should remain unbound.
*/
if (s->guest_port) {
int be_port = xen_be_evtchn_bind_interdomain(s->eh, xen_domid,
s->guest_port);
if (be_port < 0) {
return be_port;
}
s->be_port = be_port;
}
save = g_byte_array_new_take(s->impl_state, s->impl_state_size);
s->impl_state = NULL;
s->impl_state_size = 0;
ret = xs_impl_deserialize(s->impl, save, xen_domid, fire_watch_cb, s);
return ret;
}
static const VMStateDescription xen_xenstore_vmstate = {
.name = "xen_xenstore",
.unmigratable = 1, /* The PV back ends don't migrate yet */
.version_id = 1,
.minimum_version_id = 1,
.needed = xen_xenstore_is_needed,
.pre_save = xen_xenstore_pre_save,
.post_load = xen_xenstore_post_load,
.fields = (const VMStateField[]) {
VMSTATE_UINT8_ARRAY(req_data, XenXenstoreState,
sizeof_field(XenXenstoreState, req_data)),
VMSTATE_UINT8_ARRAY(rsp_data, XenXenstoreState,
sizeof_field(XenXenstoreState, rsp_data)),
VMSTATE_UINT32(req_offset, XenXenstoreState),
VMSTATE_UINT32(rsp_offset, XenXenstoreState),
VMSTATE_BOOL(rsp_pending, XenXenstoreState),
VMSTATE_UINT32(guest_port, XenXenstoreState),
VMSTATE_BOOL(fatal_error, XenXenstoreState),
VMSTATE_UINT32(impl_state_size, XenXenstoreState),
VMSTATE_VARRAY_UINT32_ALLOC(impl_state, XenXenstoreState,
impl_state_size, 0,
vmstate_info_uint8, uint8_t),
VMSTATE_END_OF_LIST()
}
};
static void xen_xenstore_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
dc->realize = xen_xenstore_realize;
dc->vmsd = &xen_xenstore_vmstate;
}
static const TypeInfo xen_xenstore_info = {
.name = TYPE_XEN_XENSTORE,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(XenXenstoreState),
.class_init = xen_xenstore_class_init,
};
void xen_xenstore_create(void)
{
DeviceState *dev = sysbus_create_simple(TYPE_XEN_XENSTORE, -1, NULL);
xen_xenstore_singleton = XEN_XENSTORE(dev);
/*
* Defer the init (xen_xenstore_reset()) until KVM is set up and the
* overlay page can be mapped.
*/
}
static void xen_xenstore_register_types(void)
{
type_register_static(&xen_xenstore_info);
}
type_init(xen_xenstore_register_types)
uint16_t xen_xenstore_get_port(void)
{
XenXenstoreState *s = xen_xenstore_singleton;
if (!s) {
return 0;
}
return s->guest_port;
}
static bool req_pending(XenXenstoreState *s)
{
struct xsd_sockmsg *req = (struct xsd_sockmsg *)s->req_data;
return s->req_offset == XENSTORE_HEADER_SIZE + req->len;
}
static void reset_req(XenXenstoreState *s)
{
memset(s->req_data, 0, sizeof(s->req_data));
s->req_offset = 0;
}
static void reset_rsp(XenXenstoreState *s)
{
s->rsp_pending = false;
memset(s->rsp_data, 0, sizeof(s->rsp_data));
s->rsp_offset = 0;
}
static void xs_error(XenXenstoreState *s, unsigned int id,
xs_transaction_t tx_id, int errnum)
{
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
const char *errstr = NULL;
for (unsigned int i = 0; i < ARRAY_SIZE(xsd_errors); i++) {
const struct xsd_errors *xsd_error = &xsd_errors[i];
if (xsd_error->errnum == errnum) {
errstr = xsd_error->errstring;
break;
}
}
assert(errstr);
trace_xenstore_error(id, tx_id, errstr);
rsp->type = XS_ERROR;
rsp->req_id = id;
rsp->tx_id = tx_id;
rsp->len = (uint32_t)strlen(errstr) + 1;
memcpy(&rsp[1], errstr, rsp->len);
}
static void xs_ok(XenXenstoreState *s, unsigned int type, unsigned int req_id,
xs_transaction_t tx_id)
{
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
const char *okstr = "OK";
rsp->type = type;
rsp->req_id = req_id;
rsp->tx_id = tx_id;
rsp->len = (uint32_t)strlen(okstr) + 1;
memcpy(&rsp[1], okstr, rsp->len);
}
/*
* The correct request and response formats are documented in xen.git:
* docs/misc/xenstore.txt. A summary is given below for convenience.
* The '|' symbol represents a NUL character.
*
* ---------- Database read, write and permissions operations ----------
*
* READ <path>| <value|>
* WRITE <path>|<value|>
* Store and read the octet string <value> at <path>.
* WRITE creates any missing parent paths, with empty values.
*
* MKDIR <path>|
* Ensures that the <path> exists, by necessary by creating
* it and any missing parents with empty values. If <path>
* or any parent already exists, its value is left unchanged.
*
* RM <path>|
* Ensures that the <path> does not exist, by deleting
* it and all of its children. It is not an error if <path> does
* not exist, but it _is_ an error if <path>'s immediate parent
* does not exist either.
*
* DIRECTORY <path>| <child-leaf-name>|*
* Gives a list of the immediate children of <path>, as only the
* leafnames. The resulting children are each named
* <path>/<child-leaf-name>.
*
* DIRECTORY_PART <path>|<offset> <gencnt>|<child-leaf-name>|*
* Same as DIRECTORY, but to be used for children lists longer than
* XENSTORE_PAYLOAD_MAX. Input are <path> and the byte offset into
* the list of children to return. Return values are the generation
* count <gencnt> of the node (to be used to ensure the node hasn't
* changed between two reads: <gencnt> being the same for multiple
* reads guarantees the node hasn't changed) and the list of children
* starting at the specified <offset> of the complete list.
*
* GET_PERMS <path>| <perm-as-string>|+
* SET_PERMS <path>|<perm-as-string>|+?
* <perm-as-string> is one of the following
* w<domid> write only
* r<domid> read only
* b<domid> both read and write
* n<domid> no access
* See https://wiki.xen.org/wiki/XenBus section
* `Permissions' for details of the permissions system.
* It is possible to set permissions for the special watch paths
* "@introduceDomain" and "@releaseDomain" to enable receiving those
* watches in unprivileged domains.
*
* ---------- Watches ----------
*
* WATCH <wpath>|<token>|?
* Adds a watch.
*
* When a <path> is modified (including path creation, removal,
* contents change or permissions change) this generates an event
* on the changed <path>. Changes made in transactions cause an
* event only if and when committed. Each occurring event is
* matched against all the watches currently set up, and each
* matching watch results in a WATCH_EVENT message (see below).
*
* The event's path matches the watch's <wpath> if it is an child
* of <wpath>.
*
* <wpath> can be a <path> to watch or @<wspecial>. In the
* latter case <wspecial> may have any syntax but it matches
* (according to the rules above) only the following special
* events which are invented by xenstored:
* @introduceDomain occurs on INTRODUCE
* @releaseDomain occurs on any domain crash or
* shutdown, and also on RELEASE
* and domain destruction
* <wspecial> events are sent to privileged callers or explicitly
* via SET_PERMS enabled domains only.
*
* When a watch is first set up it is triggered once straight
* away, with <path> equal to <wpath>. Watches may be triggered
* spuriously. The tx_id in a WATCH request is ignored.
*
* Watches are supposed to be restricted by the permissions
* system but in practice the implementation is imperfect.
* Applications should not rely on being sent a notification for
* paths that they cannot read; however, an application may rely
* on being sent a watch when a path which it _is_ able to read
* is deleted even if that leaves only a nonexistent unreadable
* parent. A notification may omitted if a node's permissions
* are changed so as to make it unreadable, in which case future
* notifications may be suppressed (and if the node is later made
* readable, some notifications may have been lost).
*
* WATCH_EVENT <epath>|<token>|
* Unsolicited `reply' generated for matching modification events
* as described above. req_id and tx_id are both 0.
*
* <epath> is the event's path, ie the actual path that was
* modified; however if the event was the recursive removal of an
* parent of <wpath>, <epath> is just
* <wpath> (rather than the actual path which was removed). So
* <epath> is a child of <wpath>, regardless.
*
* Iff <wpath> for the watch was specified as a relative pathname,
* the <epath> path will also be relative (with the same base,
* obviously).
*
* UNWATCH <wpath>|<token>|?
*
* RESET_WATCHES |
* Reset all watches and transactions of the caller.
*
* ---------- Transactions ----------
*
* TRANSACTION_START | <transid>|
* <transid> is an opaque uint32_t allocated by xenstored
* represented as unsigned decimal. After this, transaction may
* be referenced by using <transid> (as 32-bit binary) in the
* tx_id request header field. When transaction is started whole
* db is copied; reads and writes happen on the copy.
* It is not legal to send non-0 tx_id in TRANSACTION_START.
*
* TRANSACTION_END T|
* TRANSACTION_END F|
* tx_id must refer to existing transaction. After this
* request the tx_id is no longer valid and may be reused by
* xenstore. If F, the transaction is discarded. If T,
* it is committed: if there were any other intervening writes
* then our END gets get EAGAIN.
*
* The plan is that in the future only intervening `conflicting'
* writes cause EAGAIN, meaning only writes or other commits
* which changed paths which were read or written in the
* transaction at hand.
*
*/
static void xs_read(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data, unsigned int len)
{
const char *path = (const char *)req_data;
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
uint8_t *rsp_data = (uint8_t *)&rsp[1];
g_autoptr(GByteArray) data = g_byte_array_new();
int err;
if (len == 0 || req_data[len - 1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
trace_xenstore_read(tx_id, path);
err = xs_impl_read(s->impl, xen_domid, tx_id, path, data);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
rsp->type = XS_READ;
rsp->req_id = req_id;
rsp->tx_id = tx_id;
rsp->len = 0;
len = data->len;
if (len > XENSTORE_PAYLOAD_MAX) {
xs_error(s, req_id, tx_id, E2BIG);
return;
}
if (!len) {
return;
}
memcpy(&rsp_data[rsp->len], data->data, len);
rsp->len += len;
}
static void xs_write(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
g_autoptr(GByteArray) data = g_byte_array_new();
const char *path;
int err;
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
path = (const char *)req_data;
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
g_byte_array_append(data, req_data, len);
trace_xenstore_write(tx_id, path);
err = xs_impl_write(s->impl, xen_domid, tx_id, path, data);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_WRITE, req_id, tx_id);
}
static void xs_mkdir(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
g_autoptr(GByteArray) data = g_byte_array_new();
const char *path;
int err;
if (len == 0 || req_data[len - 1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
path = (const char *)req_data;
trace_xenstore_mkdir(tx_id, path);
err = xs_impl_read(s->impl, xen_domid, tx_id, path, data);
if (err == ENOENT) {
err = xs_impl_write(s->impl, xen_domid, tx_id, path, data);
}
if (!err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_MKDIR, req_id, tx_id);
}
static void xs_append_strings(XenXenstoreState *s, struct xsd_sockmsg *rsp,
GList *strings, unsigned int start, bool truncate)
{
uint8_t *rsp_data = (uint8_t *)&rsp[1];
GList *l;
for (l = strings; l; l = l->next) {
size_t len = strlen(l->data) + 1; /* Including the NUL termination */
char *str = l->data;
if (rsp->len + len > XENSTORE_PAYLOAD_MAX) {
if (truncate) {
len = XENSTORE_PAYLOAD_MAX - rsp->len;
if (!len) {
return;
}
} else {
xs_error(s, rsp->req_id, rsp->tx_id, E2BIG);
return;
}
}
if (start) {
if (start >= len) {
start -= len;
continue;
}
str += start;
len -= start;
start = 0;
}
memcpy(&rsp_data[rsp->len], str, len);
rsp->len += len;
}
/* XS_DIRECTORY_PART wants an extra NUL to indicate the end */
if (truncate && rsp->len < XENSTORE_PAYLOAD_MAX) {
rsp_data[rsp->len++] = '\0';
}
}
static void xs_directory(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
GList *items = NULL;
const char *path;
int err;
if (len == 0 || req_data[len - 1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
path = (const char *)req_data;
trace_xenstore_directory(tx_id, path);
err = xs_impl_directory(s->impl, xen_domid, tx_id, path, NULL, &items);
if (err != 0) {
xs_error(s, req_id, tx_id, err);
return;
}
rsp->type = XS_DIRECTORY;
rsp->req_id = req_id;
rsp->tx_id = tx_id;
rsp->len = 0;
xs_append_strings(s, rsp, items, 0, false);
g_list_free_full(items, g_free);
}
static void xs_directory_part(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
const char *offset_str, *path = (const char *)req_data;
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
char *rsp_data = (char *)&rsp[1];
uint64_t gencnt = 0;
unsigned int offset;
GList *items = NULL;
int err;
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
offset_str = (const char *)req_data;
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
if (len) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
if (qemu_strtoui(offset_str, NULL, 10, &offset) < 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
trace_xenstore_directory_part(tx_id, path, offset);
err = xs_impl_directory(s->impl, xen_domid, tx_id, path, &gencnt, &items);
if (err != 0) {
xs_error(s, req_id, tx_id, err);
return;
}
rsp->type = XS_DIRECTORY_PART;
rsp->req_id = req_id;
rsp->tx_id = tx_id;
rsp->len = snprintf(rsp_data, XENSTORE_PAYLOAD_MAX, "%" PRIu64, gencnt) + 1;
xs_append_strings(s, rsp, items, offset, true);
g_list_free_full(items, g_free);
}
static void xs_transaction_start(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
char *rsp_data = (char *)&rsp[1];
int err;
if (len != 1 || req_data[0] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
rsp->type = XS_TRANSACTION_START;
rsp->req_id = req_id;
rsp->tx_id = tx_id;
rsp->len = 0;
err = xs_impl_transaction_start(s->impl, xen_domid, &tx_id);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
trace_xenstore_transaction_start(tx_id);
rsp->len = snprintf(rsp_data, XENSTORE_PAYLOAD_MAX, "%u", tx_id);
assert(rsp->len < XENSTORE_PAYLOAD_MAX);
rsp->len++;
}
static void xs_transaction_end(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
bool commit;
int err;
if (len != 2 || req_data[1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
switch (req_data[0]) {
case 'T':
commit = true;
break;
case 'F':
commit = false;
break;
default:
xs_error(s, req_id, tx_id, EINVAL);
return;
}
trace_xenstore_transaction_end(tx_id, commit);
err = xs_impl_transaction_end(s->impl, xen_domid, tx_id, commit);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_TRANSACTION_END, req_id, tx_id);
}
static void xs_rm(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data, unsigned int len)
{
const char *path = (const char *)req_data;
int err;
if (len == 0 || req_data[len - 1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
trace_xenstore_rm(tx_id, path);
err = xs_impl_rm(s->impl, xen_domid, tx_id, path);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_RM, req_id, tx_id);
}
static void xs_get_perms(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
const char *path = (const char *)req_data;
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
GList *perms = NULL;
int err;
if (len == 0 || req_data[len - 1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
trace_xenstore_get_perms(tx_id, path);
err = xs_impl_get_perms(s->impl, xen_domid, tx_id, path, &perms);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
rsp->type = XS_GET_PERMS;
rsp->req_id = req_id;
rsp->tx_id = tx_id;
rsp->len = 0;
xs_append_strings(s, rsp, perms, 0, false);
g_list_free_full(perms, g_free);
}
static void xs_set_perms(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
const char *path = (const char *)req_data;
uint8_t *perm;
GList *perms = NULL;
int err;
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
perm = req_data;
while (len--) {
if (*req_data++ == '\0') {
perms = g_list_append(perms, perm);
perm = req_data;
}
}
/*
* Note that there may be trailing garbage at the end of the buffer.
* This is explicitly permitted by the '?' at the end of the definition:
*
* SET_PERMS <path>|<perm-as-string>|+?
*/
trace_xenstore_set_perms(tx_id, path);
err = xs_impl_set_perms(s->impl, xen_domid, tx_id, path, perms);
g_list_free(perms);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_SET_PERMS, req_id, tx_id);
}
static void xs_watch(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
const char *token, *path = (const char *)req_data;
int err;
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
token = (const char *)req_data;
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
/*
* Note that there may be trailing garbage at the end of the buffer.
* This is explicitly permitted by the '?' at the end of the definition:
*
* WATCH <wpath>|<token>|?
*/
trace_xenstore_watch(path, token);
err = xs_impl_watch(s->impl, xen_domid, path, token, fire_watch_cb, s);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_WATCH, req_id, tx_id);
}
static void xs_unwatch(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
const char *token, *path = (const char *)req_data;
int err;
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
token = (const char *)req_data;
while (len--) {
if (*req_data++ == '\0') {
break;
}
if (len == 0) {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
}
trace_xenstore_unwatch(path, token);
err = xs_impl_unwatch(s->impl, xen_domid, path, token, fire_watch_cb, s);
if (err) {
xs_error(s, req_id, tx_id, err);
return;
}
xs_ok(s, XS_UNWATCH, req_id, tx_id);
}
static void xs_reset_watches(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *req_data,
unsigned int len)
{
if (len == 0 || req_data[len - 1] != '\0') {
xs_error(s, req_id, tx_id, EINVAL);
return;
}
trace_xenstore_reset_watches();
xs_impl_reset_watches(s->impl, xen_domid);
xs_ok(s, XS_RESET_WATCHES, req_id, tx_id);
}
static void xs_priv(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *data,
unsigned int len)
{
xs_error(s, req_id, tx_id, EACCES);
}
static void xs_unimpl(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *data,
unsigned int len)
{
xs_error(s, req_id, tx_id, ENOSYS);
}
typedef void (*xs_impl)(XenXenstoreState *s, unsigned int req_id,
xs_transaction_t tx_id, uint8_t *data,
unsigned int len);
struct xsd_req {
const char *name;
xs_impl fn;
};
#define XSD_REQ(_type, _fn) \
[_type] = { .name = #_type, .fn = _fn }
struct xsd_req xsd_reqs[] = {
XSD_REQ(XS_READ, xs_read),
XSD_REQ(XS_WRITE, xs_write),
XSD_REQ(XS_MKDIR, xs_mkdir),
XSD_REQ(XS_DIRECTORY, xs_directory),
XSD_REQ(XS_DIRECTORY_PART, xs_directory_part),
XSD_REQ(XS_TRANSACTION_START, xs_transaction_start),
XSD_REQ(XS_TRANSACTION_END, xs_transaction_end),
XSD_REQ(XS_RM, xs_rm),
XSD_REQ(XS_GET_PERMS, xs_get_perms),
XSD_REQ(XS_SET_PERMS, xs_set_perms),
XSD_REQ(XS_WATCH, xs_watch),
XSD_REQ(XS_UNWATCH, xs_unwatch),
XSD_REQ(XS_CONTROL, xs_priv),
XSD_REQ(XS_INTRODUCE, xs_priv),
XSD_REQ(XS_RELEASE, xs_priv),
XSD_REQ(XS_IS_DOMAIN_INTRODUCED, xs_priv),
XSD_REQ(XS_RESUME, xs_priv),
XSD_REQ(XS_SET_TARGET, xs_priv),
XSD_REQ(XS_RESET_WATCHES, xs_reset_watches),
};
static void process_req(XenXenstoreState *s)
{
struct xsd_sockmsg *req = (struct xsd_sockmsg *)s->req_data;
xs_impl handler = NULL;
assert(req_pending(s));
assert(!s->rsp_pending);
if (req->type < ARRAY_SIZE(xsd_reqs)) {
handler = xsd_reqs[req->type].fn;
}
if (!handler) {
handler = &xs_unimpl;
}
handler(s, req->req_id, req->tx_id, (uint8_t *)&req[1], req->len);
s->rsp_pending = true;
reset_req(s);
}
static unsigned int copy_from_ring(XenXenstoreState *s, uint8_t *ptr,
unsigned int len)
{
if (!len) {
return 0;
}
XENSTORE_RING_IDX prod = qatomic_read(&s->xs->req_prod);
XENSTORE_RING_IDX cons = qatomic_read(&s->xs->req_cons);
unsigned int copied = 0;
/* Ensure the ring contents don't cross the req_prod access. */
smp_rmb();
while (len) {
unsigned int avail = prod - cons;
unsigned int offset = MASK_XENSTORE_IDX(cons);
unsigned int copylen = avail;
if (avail > XENSTORE_RING_SIZE) {
error_report("XenStore ring handling error");
s->fatal_error = true;
break;
} else if (avail == 0) {
break;
}
if (copylen > len) {
copylen = len;
}
if (copylen > XENSTORE_RING_SIZE - offset) {
copylen = XENSTORE_RING_SIZE - offset;
}
memcpy(ptr, &s->xs->req[offset], copylen);
copied += copylen;
ptr += copylen;
len -= copylen;
cons += copylen;
}
/*
* Not sure this ever mattered except on Alpha, but this barrier
* is to ensure that the update to req_cons is globally visible
* only after we have consumed all the data from the ring, and we
* don't end up seeing data written to the ring *after* the other
* end sees the update and writes more to the ring. Xen's own
* xenstored has the same barrier here (although with no comment
* at all, obviously, because it's Xen code).
*/
smp_mb();
qatomic_set(&s->xs->req_cons, cons);
return copied;
}
static unsigned int copy_to_ring(XenXenstoreState *s, uint8_t *ptr,
unsigned int len)
{
if (!len) {
return 0;
}
XENSTORE_RING_IDX cons = qatomic_read(&s->xs->rsp_cons);
XENSTORE_RING_IDX prod = qatomic_read(&s->xs->rsp_prod);
unsigned int copied = 0;
/*
* This matches the barrier in copy_to_ring() (or the guest's
* equivalent) between writing the data to the ring and updating
* rsp_prod. It protects against the pathological case (which
* again I think never happened except on Alpha) where our
* subsequent writes to the ring could *cross* the read of
* rsp_cons and the guest could see the new data when it was
* intending to read the old.
*/
smp_mb();
while (len) {
unsigned int avail = cons + XENSTORE_RING_SIZE - prod;
unsigned int offset = MASK_XENSTORE_IDX(prod);
unsigned int copylen = len;
if (avail > XENSTORE_RING_SIZE) {
error_report("XenStore ring handling error");
s->fatal_error = true;
break;
} else if (avail == 0) {
break;
}
if (copylen > avail) {
copylen = avail;
}
if (copylen > XENSTORE_RING_SIZE - offset) {
copylen = XENSTORE_RING_SIZE - offset;
}
memcpy(&s->xs->rsp[offset], ptr, copylen);
copied += copylen;
ptr += copylen;
len -= copylen;
prod += copylen;
}
/* Ensure the ring contents are seen before rsp_prod update. */
smp_wmb();
qatomic_set(&s->xs->rsp_prod, prod);
return copied;
}
static unsigned int get_req(XenXenstoreState *s)
{
unsigned int copied = 0;
if (s->fatal_error) {
return 0;
}
assert(!req_pending(s));
if (s->req_offset < XENSTORE_HEADER_SIZE) {
void *ptr = s->req_data + s->req_offset;
unsigned int len = XENSTORE_HEADER_SIZE;
unsigned int copylen = copy_from_ring(s, ptr, len);
copied += copylen;
s->req_offset += copylen;
}
if (s->req_offset >= XENSTORE_HEADER_SIZE) {
struct xsd_sockmsg *req = (struct xsd_sockmsg *)s->req_data;
if (req->len > (uint32_t)XENSTORE_PAYLOAD_MAX) {
error_report("Illegal XenStore request");
s->fatal_error = true;
return 0;
}
void *ptr = s->req_data + s->req_offset;
unsigned int len = XENSTORE_HEADER_SIZE + req->len - s->req_offset;
unsigned int copylen = copy_from_ring(s, ptr, len);
copied += copylen;
s->req_offset += copylen;
}
return copied;
}
static unsigned int put_rsp(XenXenstoreState *s)
{
if (s->fatal_error) {
return 0;
}
assert(s->rsp_pending);
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
assert(s->rsp_offset < XENSTORE_HEADER_SIZE + rsp->len);
void *ptr = s->rsp_data + s->rsp_offset;
unsigned int len = XENSTORE_HEADER_SIZE + rsp->len - s->rsp_offset;
unsigned int copylen = copy_to_ring(s, ptr, len);
s->rsp_offset += copylen;
/* Have we produced a complete response? */
if (s->rsp_offset == XENSTORE_HEADER_SIZE + rsp->len) {
reset_rsp(s);
}
return copylen;
}
static void deliver_watch(XenXenstoreState *s, const char *path,
const char *token)
{
struct xsd_sockmsg *rsp = (struct xsd_sockmsg *)s->rsp_data;
uint8_t *rsp_data = (uint8_t *)&rsp[1];
unsigned int len;
assert(!s->rsp_pending);
trace_xenstore_watch_event(path, token);
rsp->type = XS_WATCH_EVENT;
rsp->req_id = 0;
rsp->tx_id = 0;
rsp->len = 0;
len = strlen(path);
/* XENSTORE_ABS/REL_PATH_MAX should ensure there can be no overflow */
assert(rsp->len + len < XENSTORE_PAYLOAD_MAX);
memcpy(&rsp_data[rsp->len], path, len);
rsp->len += len;
rsp_data[rsp->len] = '\0';
rsp->len++;
len = strlen(token);
/*
* It is possible for the guest to have chosen a token that will
* not fit (along with the patch) into a watch event. We have no
* choice but to drop the event if this is the case.
*/
if (rsp->len + len >= XENSTORE_PAYLOAD_MAX) {
return;
}
memcpy(&rsp_data[rsp->len], token, len);
rsp->len += len;
rsp_data[rsp->len] = '\0';
rsp->len++;
s->rsp_pending = true;
}
struct watch_event {
char *path;
char *token;
};
static void free_watch_event(struct watch_event *ev)
{
if (ev) {
g_free(ev->path);
g_free(ev->token);
g_free(ev);
}
}
static void queue_watch(XenXenstoreState *s, const char *path,
const char *token)
{
struct watch_event *ev = g_new0(struct watch_event, 1);
ev->path = g_strdup(path);
ev->token = g_strdup(token);
s->watch_events = g_list_append(s->watch_events, ev);
}
static void fire_watch_cb(void *opaque, const char *path, const char *token)
{
XenXenstoreState *s = opaque;
assert(bql_locked());
/*
* If there's a response pending, we obviously can't scribble over
* it. But if there's a request pending, it has dibs on the buffer
* too.
*
* In the common case of a watch firing due to backend activity
* when the ring was otherwise idle, we should be able to copy the
* strings directly into the rsp_data and thence the actual ring,
* without needing to perform any allocations and queue them.
*/
if (s->rsp_pending || req_pending(s)) {
queue_watch(s, path, token);
} else {
deliver_watch(s, path, token);
/*
* Attempt to queue the message into the actual ring, and send
* the event channel notification if any bytes are copied.
*/
if (s->rsp_pending && put_rsp(s) > 0) {
xen_be_evtchn_notify(s->eh, s->be_port);
}
}
}
static void process_watch_events(XenXenstoreState *s)
{
struct watch_event *ev = s->watch_events->data;
deliver_watch(s, ev->path, ev->token);
s->watch_events = g_list_remove(s->watch_events, ev);
free_watch_event(ev);
}
static void xen_xenstore_event(void *opaque)
{
XenXenstoreState *s = opaque;
evtchn_port_t port = xen_be_evtchn_pending(s->eh);
unsigned int copied_to, copied_from;
bool processed, notify = false;
if (port != s->be_port) {
return;
}
/* We know this is a no-op. */
xen_be_evtchn_unmask(s->eh, port);
do {
copied_to = copied_from = 0;
processed = false;
if (!s->rsp_pending && s->watch_events) {
process_watch_events(s);
}
if (s->rsp_pending) {
copied_to = put_rsp(s);
}
if (!req_pending(s)) {
copied_from = get_req(s);
}
if (req_pending(s) && !s->rsp_pending && !s->watch_events) {
process_req(s);
processed = true;
}
notify |= copied_to || copied_from;
} while (copied_to || copied_from || processed);
if (notify) {
xen_be_evtchn_notify(s->eh, s->be_port);
}
}
static void alloc_guest_port(XenXenstoreState *s)
{
struct evtchn_alloc_unbound alloc = {
.dom = DOMID_SELF,
.remote_dom = DOMID_QEMU,
};
if (!xen_evtchn_alloc_unbound_op(&alloc)) {
s->guest_port = alloc.port;
}
}
int xen_xenstore_reset(void)
{
XenXenstoreState *s = xen_xenstore_singleton;
int console_port;
GList *perms;
int err;
if (!s) {
return -ENOTSUP;
}
s->req_offset = s->rsp_offset = 0;
s->rsp_pending = false;
if (!memory_region_is_mapped(&s->xenstore_page)) {
uint64_t gpa = XEN_SPECIAL_PFN(XENSTORE) << TARGET_PAGE_BITS;
xen_overlay_do_map_page(&s->xenstore_page, gpa);
}
alloc_guest_port(s);
/*
* As qemu/dom0, bind to the guest's port. For incoming migration, this
* will be unbound as the guest's evtchn table is overwritten. We then
* rebind to the correct guest port in xen_xenstore_post_load().
*/
err = xen_be_evtchn_bind_interdomain(s->eh, xen_domid, s->guest_port);
if (err < 0) {
return err;
}
s->be_port = err;
/* Create frontend store nodes */
perms = g_list_append(NULL, xs_perm_as_string(XS_PERM_NONE, DOMID_QEMU));
perms = g_list_append(perms, xs_perm_as_string(XS_PERM_READ, xen_domid));
relpath_printf(s, perms, "store/port", "%u", s->guest_port);
relpath_printf(s, perms, "store/ring-ref", "%lu",
XEN_SPECIAL_PFN(XENSTORE));
console_port = xen_primary_console_get_port();
if (console_port) {
relpath_printf(s, perms, "console/ring-ref", "%lu",
XEN_SPECIAL_PFN(CONSOLE));
relpath_printf(s, perms, "console/port", "%u", console_port);
relpath_printf(s, perms, "console/state", "%u", XenbusStateInitialised);
}
g_list_free_full(perms, g_free);
/*
* We don't actually access the guest's page through the grant, because
* this isn't real Xen, and we can just use the page we gave it in the
* first place. Map the grant anyway, mostly for cosmetic purposes so
* it *looks* like it's in use in the guest-visible grant table.
*/
s->gt = qemu_xen_gnttab_open();
uint32_t xs_gntref = GNTTAB_RESERVED_XENSTORE;
s->granted_xs = qemu_xen_gnttab_map_refs(s->gt, 1, xen_domid, &xs_gntref,
PROT_READ | PROT_WRITE);
return 0;
}
struct qemu_xs_handle {
XenstoreImplState *impl;
GList *watches;
QEMUBH *watch_bh;
};
struct qemu_xs_watch {
struct qemu_xs_handle *h;
char *path;
xs_watch_fn fn;
void *opaque;
GList *events;
};
static char *xs_be_get_domain_path(struct qemu_xs_handle *h, unsigned int domid)
{
return g_strdup_printf("/local/domain/%u", domid);
}
static char **xs_be_directory(struct qemu_xs_handle *h, xs_transaction_t t,
const char *path, unsigned int *num)
{
GList *items = NULL, *l;
unsigned int i = 0;
char **items_ret;
int err;
err = xs_impl_directory(h->impl, DOMID_QEMU, t, path, NULL, &items);
if (err) {
errno = err;
return NULL;
}
items_ret = g_new0(char *, g_list_length(items) + 1);
*num = 0;
for (l = items; l; l = l->next) {
items_ret[i++] = l->data;
(*num)++;
}
g_list_free(items);
return items_ret;
}
static void *xs_be_read(struct qemu_xs_handle *h, xs_transaction_t t,
const char *path, unsigned int *len)
{
GByteArray *data = g_byte_array_new();
bool free_segment = false;
int err;
err = xs_impl_read(h->impl, DOMID_QEMU, t, path, data);
if (err) {
free_segment = true;
errno = err;
} else {
if (len) {
*len = data->len;
}
/* The xen-bus-helper code expects to get NUL terminated string! */
g_byte_array_append(data, (void *)"", 1);
}
return g_byte_array_free(data, free_segment);
}
static bool xs_be_write(struct qemu_xs_handle *h, xs_transaction_t t,
const char *path, const void *data, unsigned int len)
{
GByteArray *gdata = g_byte_array_new();
int err;
g_byte_array_append(gdata, data, len);
err = xs_impl_write(h->impl, DOMID_QEMU, t, path, gdata);
g_byte_array_unref(gdata);
if (err) {
errno = err;
return false;
}
return true;
}
static bool xs_be_create(struct qemu_xs_handle *h, xs_transaction_t t,
unsigned int owner, unsigned int domid,
unsigned int perms, const char *path)
{
g_autoptr(GByteArray) data = g_byte_array_new();
GList *perms_list = NULL;
int err;
/* mkdir does this */
err = xs_impl_read(h->impl, DOMID_QEMU, t, path, data);
if (err == ENOENT) {
err = xs_impl_write(h->impl, DOMID_QEMU, t, path, data);
}
if (err) {
errno = err;
return false;
}
perms_list = g_list_append(perms_list,
xs_perm_as_string(XS_PERM_NONE, owner));
perms_list = g_list_append(perms_list,
xs_perm_as_string(perms, domid));
err = xs_impl_set_perms(h->impl, DOMID_QEMU, t, path, perms_list);
g_list_free_full(perms_list, g_free);
if (err) {
errno = err;
return false;
}
return true;
}
static bool xs_be_destroy(struct qemu_xs_handle *h, xs_transaction_t t,
const char *path)
{
int err = xs_impl_rm(h->impl, DOMID_QEMU, t, path);
if (err) {
errno = err;
return false;
}
return true;
}
static void be_watch_bh(void *_h)
{
struct qemu_xs_handle *h = _h;
GList *l;
for (l = h->watches; l; l = l->next) {
struct qemu_xs_watch *w = l->data;
while (w->events) {
struct watch_event *ev = w->events->data;
w->fn(w->opaque, ev->path);
w->events = g_list_remove(w->events, ev);
free_watch_event(ev);
}
}
}
static void xs_be_watch_cb(void *opaque, const char *path, const char *token)
{
struct watch_event *ev = g_new0(struct watch_event, 1);
struct qemu_xs_watch *w = opaque;
/* We don't care about the token */
ev->path = g_strdup(path);
w->events = g_list_append(w->events, ev);
qemu_bh_schedule(w->h->watch_bh);
}
static struct qemu_xs_watch *xs_be_watch(struct qemu_xs_handle *h,
const char *path, xs_watch_fn fn,
void *opaque)
{
struct qemu_xs_watch *w = g_new0(struct qemu_xs_watch, 1);
int err;
w->h = h;
w->fn = fn;
w->opaque = opaque;
err = xs_impl_watch(h->impl, DOMID_QEMU, path, NULL, xs_be_watch_cb, w);
if (err) {
errno = err;
g_free(w);
return NULL;
}
w->path = g_strdup(path);
h->watches = g_list_append(h->watches, w);
return w;
}
static void xs_be_unwatch(struct qemu_xs_handle *h, struct qemu_xs_watch *w)
{
xs_impl_unwatch(h->impl, DOMID_QEMU, w->path, NULL, xs_be_watch_cb, w);
h->watches = g_list_remove(h->watches, w);
g_list_free_full(w->events, (GDestroyNotify)free_watch_event);
g_free(w->path);
g_free(w);
}
static xs_transaction_t xs_be_transaction_start(struct qemu_xs_handle *h)
{
unsigned int new_tx = XBT_NULL;
int err = xs_impl_transaction_start(h->impl, DOMID_QEMU, &new_tx);
if (err) {
errno = err;
return XBT_NULL;
}
return new_tx;
}
static bool xs_be_transaction_end(struct qemu_xs_handle *h, xs_transaction_t t,
bool abort)
{
int err = xs_impl_transaction_end(h->impl, DOMID_QEMU, t, !abort);
if (err) {
errno = err;
return false;
}
return true;
}
static struct qemu_xs_handle *xs_be_open(void)
{
XenXenstoreState *s = xen_xenstore_singleton;
struct qemu_xs_handle *h;
if (!s || !s->impl) {
errno = -ENOSYS;
return NULL;
}
h = g_new0(struct qemu_xs_handle, 1);
h->impl = s->impl;
h->watch_bh = aio_bh_new(qemu_get_aio_context(), be_watch_bh, h);
return h;
}
static void xs_be_close(struct qemu_xs_handle *h)
{
while (h->watches) {
struct qemu_xs_watch *w = h->watches->data;
xs_be_unwatch(h, w);
}
qemu_bh_delete(h->watch_bh);
g_free(h);
}
static struct xenstore_backend_ops emu_xenstore_backend_ops = {
.open = xs_be_open,
.close = xs_be_close,
.get_domain_path = xs_be_get_domain_path,
.directory = xs_be_directory,
.read = xs_be_read,
.write = xs_be_write,
.create = xs_be_create,
.destroy = xs_be_destroy,
.watch = xs_be_watch,
.unwatch = xs_be_unwatch,
.transaction_start = xs_be_transaction_start,
.transaction_end = xs_be_transaction_end,
};
|